blob: 521279ead929e0e1366739533af36e0ab480e33d [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;
36# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +010037static int directx_enabled(void);
38static void directx_binddc(void);
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010039#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020040
Bram Moolenaar065bbac2016-02-20 13:08:46 +010041#ifdef FEAT_MENU
42static int gui_mswin_get_menu_height(int fix_window);
43#endif
44
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020045#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
46 int
47gui_mch_set_rendering_options(char_u *s)
48{
49#ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020050 char_u *p, *q;
51
52 int dx_enable = 0;
53 int dx_flags = 0;
54 float dx_gamma = 0.0f;
55 float dx_contrast = 0.0f;
56 float dx_level = 0.0f;
57 int dx_geom = 0;
58 int dx_renmode = 0;
59 int dx_taamode = 0;
60
61 /* parse string as rendering options. */
62 for (p = s; p != NULL && *p != NUL; )
63 {
64 char_u item[256];
65 char_u name[128];
66 char_u value[128];
67
Bram Moolenaarcde88542015-08-11 19:14:00 +020068 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020069 if (p == NULL)
70 break;
71 q = &item[0];
72 copy_option_part(&q, name, sizeof(name), ":");
73 if (q == NULL)
74 return FAIL;
75 copy_option_part(&q, value, sizeof(value), ":");
76
77 if (STRCMP(name, "type") == 0)
78 {
79 if (STRCMP(value, "directx") == 0)
80 dx_enable = 1;
81 else
82 return FAIL;
83 }
84 else if (STRCMP(name, "gamma") == 0)
85 {
86 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010087 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020088 }
89 else if (STRCMP(name, "contrast") == 0)
90 {
91 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010092 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020093 }
94 else if (STRCMP(name, "level") == 0)
95 {
96 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010097 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020098 }
99 else if (STRCMP(name, "geom") == 0)
100 {
101 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100102 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200103 if (dx_geom < 0 || dx_geom > 2)
104 return FAIL;
105 }
106 else if (STRCMP(name, "renmode") == 0)
107 {
108 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100109 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200110 if (dx_renmode < 0 || dx_renmode > 6)
111 return FAIL;
112 }
113 else if (STRCMP(name, "taamode") == 0)
114 {
115 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100116 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200117 if (dx_taamode < 0 || dx_taamode > 3)
118 return FAIL;
119 }
120 else
121 return FAIL;
122 }
123
124 /* Enable DirectX/DirectWrite */
125 if (dx_enable)
126 {
127 if (!directx_enabled())
128 return FAIL;
129 DWriteContext_SetRenderingParams(s_dwc, NULL);
130 if (dx_flags)
131 {
132 DWriteRenderingParams param;
133 DWriteContext_GetRenderingParams(s_dwc, &param);
134 if (dx_flags & (1 << 0))
135 param.gamma = dx_gamma;
136 if (dx_flags & (1 << 1))
137 param.enhancedContrast = dx_contrast;
138 if (dx_flags & (1 << 2))
139 param.clearTypeLevel = dx_level;
140 if (dx_flags & (1 << 3))
141 param.pixelGeometry = dx_geom;
142 if (dx_flags & (1 << 4))
143 param.renderingMode = dx_renmode;
144 if (dx_flags & (1 << 5))
145 param.textAntialiasMode = dx_taamode;
146 DWriteContext_SetRenderingParams(s_dwc, &param);
147 }
148 }
149 s_directx_enabled = dx_enable;
150
151 return OK;
152#else
153 return FAIL;
154#endif
155}
156#endif
157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158/*
159 * These are new in Windows ME/XP, only defined in recent compilers.
160 */
161#ifndef HANDLE_WM_XBUTTONUP
162# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
163 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
164#endif
165#ifndef HANDLE_WM_XBUTTONDOWN
166# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
167 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
168#endif
169#ifndef HANDLE_WM_XBUTTONDBLCLK
170# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
171 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
172#endif
173
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100174
175#include "version.h" /* used by dialog box routine for default title */
176#ifdef DEBUG
177# include <tchar.h>
178#endif
179
180/* cproto fails on missing include files */
181#ifndef PROTO
182
183#ifndef __MINGW32__
184# include <shellapi.h>
185#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100186#if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100187# include <commctrl.h>
188#endif
189#include <windowsx.h>
190
191#ifdef GLOBAL_IME
192# include "glbl_ime.h"
193#endif
194
195#endif /* PROTO */
196
197#ifdef FEAT_MENU
198# define MENUHINTS /* show menu hints in command line */
199#endif
200
201/* Some parameters for dialog boxes. All in pixels. */
202#define DLG_PADDING_X 10
203#define DLG_PADDING_Y 10
204#define DLG_OLD_STYLE_PADDING_X 5
205#define DLG_OLD_STYLE_PADDING_Y 5
206#define DLG_VERT_PADDING_X 4 /* For vertical buttons */
207#define DLG_VERT_PADDING_Y 4
208#define DLG_ICON_WIDTH 34
209#define DLG_ICON_HEIGHT 34
210#define DLG_MIN_WIDTH 150
211#define DLG_FONT_NAME "MS Sans Serif"
212#define DLG_FONT_POINT_SIZE 8
213#define DLG_MIN_MAX_WIDTH 400
214#define DLG_MIN_MAX_HEIGHT 400
215
216#define DLG_NONBUTTON_CONTROL 5000 /* First ID of non-button controls */
217
218#ifndef WM_XBUTTONDOWN /* For Win2K / winME ONLY */
219# define WM_XBUTTONDOWN 0x020B
220# define WM_XBUTTONUP 0x020C
221# define WM_XBUTTONDBLCLK 0x020D
222# define MK_XBUTTON1 0x0020
223# define MK_XBUTTON2 0x0040
224#endif
225
226#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100228 * Define a few things for generating prototypes. This is just to avoid
229 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100231# define APIENTRY
232# define CALLBACK
233# define CONST
234# define FAR
235# define NEAR
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200236# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100237# define _cdecl
238typedef int BOOL;
239typedef int BYTE;
240typedef int DWORD;
241typedef int WCHAR;
242typedef int ENUMLOGFONT;
243typedef int FINDREPLACE;
244typedef int HANDLE;
245typedef int HBITMAP;
246typedef int HBRUSH;
247typedef int HDROP;
248typedef int INT;
249typedef int LOGFONT[];
250typedef int LPARAM;
251typedef int LPCREATESTRUCT;
252typedef int LPCSTR;
253typedef int LPCTSTR;
254typedef int LPRECT;
255typedef int LPSTR;
256typedef int LPWINDOWPOS;
257typedef int LPWORD;
258typedef int LRESULT;
259typedef int HRESULT;
260# undef MSG
261typedef int MSG;
262typedef int NEWTEXTMETRIC;
263typedef int OSVERSIONINFO;
264typedef int PWORD;
265typedef int RECT;
266typedef int UINT;
267typedef int WORD;
268typedef int WPARAM;
269typedef int POINT;
270typedef void *HINSTANCE;
271typedef void *HMENU;
272typedef void *HWND;
273typedef void *HDC;
274typedef void VOID;
275typedef int LPNMHDR;
276typedef int LONG;
277typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200278typedef int UINT_PTR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100279#endif
280
281#ifndef GET_X_LPARAM
282# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
283#endif
284
285static void _OnPaint( HWND hwnd);
286static void clear_rect(RECT *rcp);
287
288static WORD s_dlgfntheight; /* height of the dialog font */
289static WORD s_dlgfntwidth; /* width of the dialog font */
290
291#ifdef FEAT_MENU
292static HMENU s_menuBar = NULL;
293#endif
294#ifdef FEAT_TEAROFF
295static void rebuild_tearoff(vimmenu_T *menu);
296static HBITMAP s_htearbitmap; /* bitmap used to indicate tearoff */
297#endif
298
299/* Flag that is set while processing a message that must not be interrupted by
300 * processing another message. */
301static int s_busy_processing = FALSE;
302
303static int destroying = FALSE; /* call DestroyWindow() ourselves */
304
305#ifdef MSWIN_FIND_REPLACE
306static UINT s_findrep_msg = 0; /* set in gui_w[16/32].c */
307static FINDREPLACE s_findrep_struct;
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200308# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100309static FINDREPLACEW s_findrep_struct_w;
310# endif
311static HWND s_findrep_hwnd = NULL;
312static int s_findrep_is_find; /* TRUE for find dialog, FALSE
313 for find/replace dialog */
314#endif
315
316static HINSTANCE s_hinst = NULL;
Bram Moolenaar85b11762016-02-27 18:13:23 +0100317#if !defined(FEAT_GUI)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100318static
319#endif
320HWND s_hwnd = NULL;
321static HDC s_hdc = NULL;
322static HBRUSH s_brush = NULL;
323
324#ifdef FEAT_TOOLBAR
325static HWND s_toolbarhwnd = NULL;
326static WNDPROC s_toolbar_wndproc = NULL;
327#endif
328
329#ifdef FEAT_GUI_TABLINE
330static HWND s_tabhwnd = NULL;
331static WNDPROC s_tabline_wndproc = NULL;
332static int showing_tabline = 0;
333#endif
334
335static WPARAM s_wParam = 0;
336static LPARAM s_lParam = 0;
337
338static HWND s_textArea = NULL;
339static UINT s_uMsg = 0;
340
341static char_u *s_textfield; /* Used by dialogs to pass back strings */
342
343static int s_need_activate = FALSE;
344
345/* This variable is set when waiting for an event, which is the only moment
346 * scrollbar dragging can be done directly. It's not allowed while commands
347 * are executed, because it may move the cursor and that may cause unexpected
348 * problems (e.g., while ":s" is working).
349 */
350static int allow_scrollbar = FALSE;
351
352#ifdef GLOBAL_IME
353# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
354#else
355# define MyTranslateMessage(x) TranslateMessage(x)
356#endif
357
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100358#if defined(FEAT_DIRECTX)
359 static int
360directx_enabled(void)
361{
362 if (s_dwc != NULL)
363 return 1;
364 else if (s_directx_load_attempted)
365 return 0;
366 /* load DirectX */
367 DWrite_Init();
368 s_directx_load_attempted = 1;
369 s_dwc = DWriteContext_Open();
370 directx_binddc();
371 return s_dwc != NULL ? 1 : 0;
372}
373
374 static void
375directx_binddc(void)
376{
377 if (s_textArea != NULL)
378 {
379 RECT rect;
380 GetClientRect(s_textArea, &rect);
381 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
382 }
383}
384#endif
385
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200386#if defined(FEAT_MBYTE) || defined(GLOBAL_IME)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100387 /* use of WindowProc depends on wide_WindowProc */
388# define MyWindowProc vim_WindowProc
389#else
390 /* use ordinary WindowProc */
391# define MyWindowProc DefWindowProc
392#endif
393
394extern int current_font_height; /* this is in os_mswin.c */
395
396static struct
397{
398 UINT key_sym;
399 char_u vim_code0;
400 char_u vim_code1;
401} special_keys[] =
402{
403 {VK_UP, 'k', 'u'},
404 {VK_DOWN, 'k', 'd'},
405 {VK_LEFT, 'k', 'l'},
406 {VK_RIGHT, 'k', 'r'},
407
408 {VK_F1, 'k', '1'},
409 {VK_F2, 'k', '2'},
410 {VK_F3, 'k', '3'},
411 {VK_F4, 'k', '4'},
412 {VK_F5, 'k', '5'},
413 {VK_F6, 'k', '6'},
414 {VK_F7, 'k', '7'},
415 {VK_F8, 'k', '8'},
416 {VK_F9, 'k', '9'},
417 {VK_F10, 'k', ';'},
418
419 {VK_F11, 'F', '1'},
420 {VK_F12, 'F', '2'},
421 {VK_F13, 'F', '3'},
422 {VK_F14, 'F', '4'},
423 {VK_F15, 'F', '5'},
424 {VK_F16, 'F', '6'},
425 {VK_F17, 'F', '7'},
426 {VK_F18, 'F', '8'},
427 {VK_F19, 'F', '9'},
428 {VK_F20, 'F', 'A'},
429
430 {VK_F21, 'F', 'B'},
431#ifdef FEAT_NETBEANS_INTG
432 {VK_PAUSE, 'F', 'B'}, /* Pause == F21 (see gui_gtk_x11.c) */
433#endif
434 {VK_F22, 'F', 'C'},
435 {VK_F23, 'F', 'D'},
436 {VK_F24, 'F', 'E'}, /* winuser.h defines up to F24 */
437
438 {VK_HELP, '%', '1'},
439 {VK_BACK, 'k', 'b'},
440 {VK_INSERT, 'k', 'I'},
441 {VK_DELETE, 'k', 'D'},
442 {VK_HOME, 'k', 'h'},
443 {VK_END, '@', '7'},
444 {VK_PRIOR, 'k', 'P'},
445 {VK_NEXT, 'k', 'N'},
446 {VK_PRINT, '%', '9'},
447 {VK_ADD, 'K', '6'},
448 {VK_SUBTRACT, 'K', '7'},
449 {VK_DIVIDE, 'K', '8'},
450 {VK_MULTIPLY, 'K', '9'},
451 {VK_SEPARATOR, 'K', 'A'}, /* Keypad Enter */
452 {VK_DECIMAL, 'K', 'B'},
453
454 {VK_NUMPAD0, 'K', 'C'},
455 {VK_NUMPAD1, 'K', 'D'},
456 {VK_NUMPAD2, 'K', 'E'},
457 {VK_NUMPAD3, 'K', 'F'},
458 {VK_NUMPAD4, 'K', 'G'},
459 {VK_NUMPAD5, 'K', 'H'},
460 {VK_NUMPAD6, 'K', 'I'},
461 {VK_NUMPAD7, 'K', 'J'},
462 {VK_NUMPAD8, 'K', 'K'},
463 {VK_NUMPAD9, 'K', 'L'},
464
465 /* Keys that we want to be able to use any modifier with: */
466 {VK_SPACE, ' ', NUL},
467 {VK_TAB, TAB, NUL},
468 {VK_ESCAPE, ESC, NUL},
469 {NL, NL, NUL},
470 {CAR, CAR, NUL},
471
472 /* End of list marker: */
473 {0, 0, 0}
474};
475
476/* Local variables */
477static int s_button_pending = -1;
478
479/* s_getting_focus is set when we got focus but didn't see mouse-up event yet,
480 * so don't reset s_button_pending. */
481static int s_getting_focus = FALSE;
482
483static int s_x_pending;
484static int s_y_pending;
485static UINT s_kFlags_pending;
486static UINT s_wait_timer = 0; /* Timer for get char from user */
487static int s_timed_out = FALSE;
488static int dead_key = 0; /* 0: no dead key, 1: dead key pressed */
489
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100490#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100491/* balloon-eval WM_NOTIFY_HANDLER */
492static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
493static void TrackUserActivity(UINT uMsg);
494#endif
495
496/*
497 * For control IME.
498 *
499 * These LOGFONT used for IME.
500 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100501#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100502/* holds LOGFONT for 'guifontwide' if available, otherwise 'guifont' */
503static LOGFONT norm_logfont;
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100504#endif
505#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100506/* holds LOGFONT for 'guifont' always. */
507static LOGFONT sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100508#endif
509
510#ifdef FEAT_MBYTE_IME
511static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
512#endif
513
514#if defined(FEAT_BROWSE)
515static char_u *convert_filter(char_u *s);
516#endif
517
518#ifdef DEBUG_PRINT_ERROR
519/*
520 * Print out the last Windows error message
521 */
522 static void
523print_windows_error(void)
524{
525 LPVOID lpMsgBuf;
526
527 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
528 NULL, GetLastError(),
529 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
530 (LPTSTR) &lpMsgBuf, 0, NULL);
531 TRACE1("Error: %s\n", lpMsgBuf);
532 LocalFree(lpMsgBuf);
533}
534#endif
535
536/*
537 * Cursor blink functions.
538 *
539 * This is a simple state machine:
540 * BLINK_NONE not blinking at all
541 * BLINK_OFF blinking, cursor is not shown
542 * BLINK_ON blinking, cursor is shown
543 */
544
545#define BLINK_NONE 0
546#define BLINK_OFF 1
547#define BLINK_ON 2
548
549static int blink_state = BLINK_NONE;
550static long_u blink_waittime = 700;
551static long_u blink_ontime = 400;
552static long_u blink_offtime = 250;
553static UINT blink_timer = 0;
554
Bram Moolenaar703a8042016-06-04 16:24:32 +0200555 int
556gui_mch_is_blinking(void)
557{
558 return blink_state != BLINK_NONE;
559}
560
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200561 int
562gui_mch_is_blink_off(void)
563{
564 return blink_state == BLINK_OFF;
565}
566
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100567 void
568gui_mch_set_blinking(long wait, long on, long off)
569{
570 blink_waittime = wait;
571 blink_ontime = on;
572 blink_offtime = off;
573}
574
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100575 static VOID CALLBACK
576_OnBlinkTimer(
577 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100578 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100579 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100580 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100581{
582 MSG msg;
583
584 /*
585 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
586 */
587
588 KillTimer(NULL, idEvent);
589
590 /* Eat spurious WM_TIMER messages */
591 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
592 ;
593
594 if (blink_state == BLINK_ON)
595 {
596 gui_undraw_cursor();
597 blink_state = BLINK_OFF;
598 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
599 (TIMERPROC)_OnBlinkTimer);
600 }
601 else
602 {
603 gui_update_cursor(TRUE, FALSE);
604 blink_state = BLINK_ON;
605 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100606 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100607 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100608#if defined(FEAT_DIRECTX)
609 if (IS_ENABLE_DIRECTX())
610 DWriteContext_Flush(s_dwc);
611#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100612}
613
614 static void
615gui_mswin_rm_blink_timer(void)
616{
617 MSG msg;
618
619 if (blink_timer != 0)
620 {
621 KillTimer(NULL, blink_timer);
622 /* Eat spurious WM_TIMER messages */
623 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
624 ;
625 blink_timer = 0;
626 }
627}
628
629/*
630 * Stop the cursor blinking. Show the cursor if it wasn't shown.
631 */
632 void
633gui_mch_stop_blink(void)
634{
635 gui_mswin_rm_blink_timer();
636 if (blink_state == BLINK_OFF)
637 gui_update_cursor(TRUE, FALSE);
638 blink_state = BLINK_NONE;
639}
640
641/*
642 * Start the cursor blinking. If it was already blinking, this restarts the
643 * waiting time and shows the cursor.
644 */
645 void
646gui_mch_start_blink(void)
647{
648 gui_mswin_rm_blink_timer();
649
650 /* Only switch blinking on if none of the times is zero */
651 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
652 {
653 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
654 (TIMERPROC)_OnBlinkTimer);
655 blink_state = BLINK_ON;
656 gui_update_cursor(TRUE, FALSE);
657 }
658}
659
660/*
661 * Call-back routines.
662 */
663
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100664 static VOID CALLBACK
665_OnTimer(
666 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100667 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100668 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100669 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100670{
671 MSG msg;
672
673 /*
674 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
675 */
676 KillTimer(NULL, idEvent);
677 s_timed_out = TRUE;
678
679 /* Eat spurious WM_TIMER messages */
680 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
681 ;
682 if (idEvent == s_wait_timer)
683 s_wait_timer = 0;
684}
685
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100686 static void
687_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100688 HWND hwnd UNUSED,
689 UINT ch UNUSED,
690 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100691{
692 dead_key = 1;
693}
694
695/*
696 * Convert Unicode character "ch" to bytes in "string[slen]".
697 * When "had_alt" is TRUE the ALT key was included in "ch".
698 * Return the length.
699 */
700 static int
701char_to_string(int ch, char_u *string, int slen, int had_alt)
702{
703 int len;
704 int i;
705#ifdef FEAT_MBYTE
706 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200707 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100708
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200709 wstring[0] = ch;
710 len = 1;
711
712 /* "ch" is a UTF-16 character. Convert it to a string of bytes. When
713 * "enc_codepage" is non-zero use the standard Win32 function,
714 * otherwise use our own conversion function (e.g., for UTF-8). */
715 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100716 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200717 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
718 (LPSTR)string, slen, 0, NULL);
719 /* If we had included the ALT key into the character but now the
720 * upper bit is no longer set, that probably means the conversion
721 * failed. Convert the original character and set the upper bit
722 * afterwards. */
723 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100724 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200725 wstring[0] = ch & 0x7f;
726 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
727 (LPSTR)string, slen, 0, NULL);
728 if (len == 1) /* safety check */
729 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100730 }
731 }
732 else
733 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100734 len = 1;
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200735 ws = utf16_to_enc(wstring, &len);
736 if (ws == NULL)
737 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100738 else
739 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200740 if (len > slen) /* just in case */
741 len = slen;
742 mch_memmove(string, ws, len);
743 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100744 }
745 }
746
747 if (len == 0)
748#endif
749 {
750 string[0] = ch;
751 len = 1;
752 }
753
754 for (i = 0; i < len; ++i)
755 if (string[i] == CSI && len <= slen - 2)
756 {
757 /* Insert CSI as K_CSI. */
758 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
759 string[++i] = KS_EXTRA;
760 string[++i] = (int)KE_CSI;
761 len += 2;
762 }
763
764 return len;
765}
766
767/*
768 * Key hit, add it to the input buffer.
769 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100770 static void
771_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100772 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100773 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100774 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100775{
776 char_u string[40];
777 int len = 0;
778
779 dead_key = 0;
780
781 len = char_to_string(ch, string, 40, FALSE);
782 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
783 {
784 trash_input_buf();
785 got_int = TRUE;
786 }
787
788 add_to_input_buf(string, len);
789}
790
791/*
792 * Alt-Key hit, add it to the input buffer.
793 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100794 static void
795_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100796 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100797 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100798 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100799{
800 char_u string[40]; /* Enough for multibyte character */
801 int len;
802 int modifiers;
803 int ch = cch; /* special keys are negative */
804
805 dead_key = 0;
806
807 /* TRACE("OnSysChar(%d, %c)\n", ch, ch); */
808
809 /* OK, we have a character key (given by ch) which was entered with the
810 * ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
811 * that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
812 * CAPSLOCK is pressed) at this point.
813 */
814 modifiers = MOD_MASK_ALT;
815 if (GetKeyState(VK_SHIFT) & 0x8000)
816 modifiers |= MOD_MASK_SHIFT;
817 if (GetKeyState(VK_CONTROL) & 0x8000)
818 modifiers |= MOD_MASK_CTRL;
819
820 ch = simplify_key(ch, &modifiers);
821 /* remove the SHIFT modifier for keys where it's already included, e.g.,
822 * '(' and '*' */
823 if (ch < 0x100 && !isalpha(ch) && isprint(ch))
824 modifiers &= ~MOD_MASK_SHIFT;
825
826 /* Interpret the ALT key as making the key META, include SHIFT, etc. */
827 ch = extract_modifiers(ch, &modifiers);
828 if (ch == CSI)
829 ch = K_CSI;
830
831 len = 0;
832 if (modifiers)
833 {
834 string[len++] = CSI;
835 string[len++] = KS_MODIFIER;
836 string[len++] = modifiers;
837 }
838
839 if (IS_SPECIAL((int)ch))
840 {
841 string[len++] = CSI;
842 string[len++] = K_SECOND((int)ch);
843 string[len++] = K_THIRD((int)ch);
844 }
845 else
846 {
847 /* Although the documentation isn't clear about it, we assume "ch" is
848 * a Unicode character. */
849 len += char_to_string(ch, string + len, 40 - len, TRUE);
850 }
851
852 add_to_input_buf(string, len);
853}
854
855 static void
856_OnMouseEvent(
857 int button,
858 int x,
859 int y,
860 int repeated_click,
861 UINT keyFlags)
862{
863 int vim_modifiers = 0x0;
864
865 s_getting_focus = FALSE;
866
867 if (keyFlags & MK_SHIFT)
868 vim_modifiers |= MOUSE_SHIFT;
869 if (keyFlags & MK_CONTROL)
870 vim_modifiers |= MOUSE_CTRL;
871 if (GetKeyState(VK_MENU) & 0x8000)
872 vim_modifiers |= MOUSE_ALT;
873
874 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
875}
876
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100877 static void
878_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100879 HWND hwnd UNUSED,
880 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100881 int x,
882 int y,
883 UINT keyFlags)
884{
885 static LONG s_prevTime = 0;
886
887 LONG currentTime = GetMessageTime();
888 int button = -1;
889 int repeated_click;
890
891 /* Give main window the focus: this is so the cursor isn't hollow. */
892 (void)SetFocus(s_hwnd);
893
894 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
895 button = MOUSE_LEFT;
896 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
897 button = MOUSE_MIDDLE;
898 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
899 button = MOUSE_RIGHT;
900 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
901 {
902#ifndef GET_XBUTTON_WPARAM
903# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
904#endif
905 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
906 }
907 else if (s_uMsg == WM_CAPTURECHANGED)
908 {
909 /* on W95/NT4, somehow you get in here with an odd Msg
910 * if you press one button while holding down the other..*/
911 if (s_button_pending == MOUSE_LEFT)
912 button = MOUSE_RIGHT;
913 else
914 button = MOUSE_LEFT;
915 }
916 if (button >= 0)
917 {
918 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
919
920 /*
921 * Holding down the left and right buttons simulates pushing the middle
922 * button.
923 */
924 if (repeated_click
925 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
926 || (button == MOUSE_RIGHT
927 && s_button_pending == MOUSE_LEFT)))
928 {
929 /*
930 * Hmm, gui.c will ignore more than one button down at a time, so
931 * pretend we let go of it first.
932 */
933 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
934 button = MOUSE_MIDDLE;
935 repeated_click = FALSE;
936 s_button_pending = -1;
937 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
938 }
939 else if ((repeated_click)
940 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
941 {
942 if (s_button_pending > -1)
943 {
944 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
945 s_button_pending = -1;
946 }
947 /* TRACE("Button down at x %d, y %d\n", x, y); */
948 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
949 }
950 else
951 {
952 /*
953 * If this is the first press (i.e. not a multiple click) don't
954 * action immediately, but store and wait for:
955 * i) button-up
956 * ii) mouse move
957 * iii) another button press
958 * before using it.
959 * This enables us to make left+right simulate middle button,
960 * without left or right being actioned first. The side-effect is
961 * that if you click and hold the mouse without dragging, the
962 * cursor doesn't move until you release the button. In practice
963 * this is hardly a problem.
964 */
965 s_button_pending = button;
966 s_x_pending = x;
967 s_y_pending = y;
968 s_kFlags_pending = keyFlags;
969 }
970
971 s_prevTime = currentTime;
972 }
973}
974
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100975 static void
976_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100977 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100978 int x,
979 int y,
980 UINT keyFlags)
981{
982 int button;
983
984 s_getting_focus = FALSE;
985 if (s_button_pending > -1)
986 {
987 /* Delayed action for mouse down event */
988 _OnMouseEvent(s_button_pending, s_x_pending,
989 s_y_pending, FALSE, s_kFlags_pending);
990 s_button_pending = -1;
991 }
992 if (s_uMsg == WM_MOUSEMOVE)
993 {
994 /*
995 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
996 * down.
997 */
998 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
999 | MK_XBUTTON1 | MK_XBUTTON2)))
1000 {
1001 gui_mouse_moved(x, y);
1002 return;
1003 }
1004
1005 /*
1006 * While button is down, keep grabbing mouse move events when
1007 * the mouse goes outside the window
1008 */
1009 SetCapture(s_textArea);
1010 button = MOUSE_DRAG;
1011 /* TRACE(" move at x %d, y %d\n", x, y); */
1012 }
1013 else
1014 {
1015 ReleaseCapture();
1016 button = MOUSE_RELEASE;
1017 /* TRACE(" up at x %d, y %d\n", x, y); */
1018 }
1019
1020 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1021}
1022
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001023 static void
1024_OnSizeTextArea(
1025 HWND hwnd UNUSED,
1026 UINT state UNUSED,
1027 int cx UNUSED,
1028 int cy UNUSED)
1029{
1030#if defined(FEAT_DIRECTX)
1031 if (IS_ENABLE_DIRECTX())
1032 directx_binddc();
1033#endif
1034}
1035
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001036#ifdef FEAT_MENU
1037/*
1038 * Find the vimmenu_T with the given id
1039 */
1040 static vimmenu_T *
1041gui_mswin_find_menu(
1042 vimmenu_T *pMenu,
1043 int id)
1044{
1045 vimmenu_T *pChildMenu;
1046
1047 while (pMenu)
1048 {
1049 if (pMenu->id == (UINT)id)
1050 break;
1051 if (pMenu->children != NULL)
1052 {
1053 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1054 if (pChildMenu)
1055 {
1056 pMenu = pChildMenu;
1057 break;
1058 }
1059 }
1060 pMenu = pMenu->next;
1061 }
1062 return pMenu;
1063}
1064
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001065 static void
1066_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001067 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001068 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001069 HWND hwndCtl UNUSED,
1070 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001071{
1072 vimmenu_T *pMenu;
1073
1074 pMenu = gui_mswin_find_menu(root_menu, id);
1075 if (pMenu)
1076 gui_menu_cb(pMenu);
1077}
1078#endif
1079
1080#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001081# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001082/*
1083 * copy useful data from structure LPFINDREPLACE to structure LPFINDREPLACEW
1084 */
1085 static void
1086findrep_atow(LPFINDREPLACEW lpfrw, LPFINDREPLACE lpfr)
1087{
1088 WCHAR *wp;
1089
1090 lpfrw->hwndOwner = lpfr->hwndOwner;
1091 lpfrw->Flags = lpfr->Flags;
1092
1093 wp = enc_to_utf16((char_u *)lpfr->lpstrFindWhat, NULL);
1094 wcsncpy(lpfrw->lpstrFindWhat, wp, lpfrw->wFindWhatLen - 1);
1095 vim_free(wp);
1096
1097 /* the field "lpstrReplaceWith" doesn't need to be copied */
1098}
1099
1100/*
1101 * copy useful data from structure LPFINDREPLACEW to structure LPFINDREPLACE
1102 */
1103 static void
1104findrep_wtoa(LPFINDREPLACE lpfr, LPFINDREPLACEW lpfrw)
1105{
1106 char_u *p;
1107
1108 lpfr->Flags = lpfrw->Flags;
1109
1110 p = utf16_to_enc((short_u*)lpfrw->lpstrFindWhat, NULL);
1111 vim_strncpy((char_u *)lpfr->lpstrFindWhat, p, lpfr->wFindWhatLen - 1);
1112 vim_free(p);
1113
1114 p = utf16_to_enc((short_u*)lpfrw->lpstrReplaceWith, NULL);
1115 vim_strncpy((char_u *)lpfr->lpstrReplaceWith, p, lpfr->wReplaceWithLen - 1);
1116 vim_free(p);
1117}
1118# endif
1119
1120/*
1121 * Handle a Find/Replace window message.
1122 */
1123 static void
1124_OnFindRepl(void)
1125{
1126 int flags = 0;
1127 int down;
1128
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001129# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001130 /* If the OS is Windows NT, and 'encoding' differs from active codepage:
1131 * convert text from wide string. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001132 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001133 {
1134 findrep_wtoa(&s_findrep_struct, &s_findrep_struct_w);
1135 }
1136# endif
1137
1138 if (s_findrep_struct.Flags & FR_DIALOGTERM)
1139 /* Give main window the focus back. */
1140 (void)SetFocus(s_hwnd);
1141
1142 if (s_findrep_struct.Flags & FR_FINDNEXT)
1143 {
1144 flags = FRD_FINDNEXT;
1145
1146 /* Give main window the focus back: this is so the cursor isn't
1147 * hollow. */
1148 (void)SetFocus(s_hwnd);
1149 }
1150 else if (s_findrep_struct.Flags & FR_REPLACE)
1151 {
1152 flags = FRD_REPLACE;
1153
1154 /* Give main window the focus back: this is so the cursor isn't
1155 * hollow. */
1156 (void)SetFocus(s_hwnd);
1157 }
1158 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1159 {
1160 flags = FRD_REPLACEALL;
1161 }
1162
1163 if (flags != 0)
1164 {
1165 /* Call the generic GUI function to do the actual work. */
1166 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1167 flags |= FRD_WHOLE_WORD;
1168 if (s_findrep_struct.Flags & FR_MATCHCASE)
1169 flags |= FRD_MATCH_CASE;
1170 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
1171 gui_do_findrepl(flags, (char_u *)s_findrep_struct.lpstrFindWhat,
1172 (char_u *)s_findrep_struct.lpstrReplaceWith, down);
1173 }
1174}
1175#endif
1176
1177 static void
1178HandleMouseHide(UINT uMsg, LPARAM lParam)
1179{
1180 static LPARAM last_lParam = 0L;
1181
1182 /* We sometimes get a mousemove when the mouse didn't move... */
1183 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1184 {
1185 if (lParam == last_lParam)
1186 return;
1187 last_lParam = lParam;
1188 }
1189
1190 /* Handle specially, to centralise coding. We need to be sure we catch all
1191 * possible events which should cause us to restore the cursor (as it is a
1192 * shared resource, we take full responsibility for it).
1193 */
1194 switch (uMsg)
1195 {
1196 case WM_KEYUP:
1197 case WM_CHAR:
1198 /*
1199 * blank out the pointer if necessary
1200 */
1201 if (p_mh)
1202 gui_mch_mousehide(TRUE);
1203 break;
1204
1205 case WM_SYSKEYUP: /* show the pointer when a system-key is pressed */
1206 case WM_SYSCHAR:
1207 case WM_MOUSEMOVE: /* show the pointer on any mouse action */
1208 case WM_LBUTTONDOWN:
1209 case WM_LBUTTONUP:
1210 case WM_MBUTTONDOWN:
1211 case WM_MBUTTONUP:
1212 case WM_RBUTTONDOWN:
1213 case WM_RBUTTONUP:
1214 case WM_XBUTTONDOWN:
1215 case WM_XBUTTONUP:
1216 case WM_NCMOUSEMOVE:
1217 case WM_NCLBUTTONDOWN:
1218 case WM_NCLBUTTONUP:
1219 case WM_NCMBUTTONDOWN:
1220 case WM_NCMBUTTONUP:
1221 case WM_NCRBUTTONDOWN:
1222 case WM_NCRBUTTONUP:
1223 case WM_KILLFOCUS:
1224 /*
1225 * if the pointer is currently hidden, then we should show it.
1226 */
1227 gui_mch_mousehide(FALSE);
1228 break;
1229 }
1230}
1231
1232 static LRESULT CALLBACK
1233_TextAreaWndProc(
1234 HWND hwnd,
1235 UINT uMsg,
1236 WPARAM wParam,
1237 LPARAM lParam)
1238{
1239 /*
1240 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1241 hwnd, uMsg, wParam, lParam);
1242 */
1243
1244 HandleMouseHide(uMsg, lParam);
1245
1246 s_uMsg = uMsg;
1247 s_wParam = wParam;
1248 s_lParam = lParam;
1249
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001250#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001251 TrackUserActivity(uMsg);
1252#endif
1253
1254 switch (uMsg)
1255 {
1256 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1257 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1258 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1259 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1260 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1261 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1262 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1263 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1264 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1265 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1266 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1267 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1268 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1269 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001270 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001271
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001272#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001273 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1274 return TRUE;
1275#endif
1276 default:
1277 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1278 }
1279}
1280
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001281#if defined(FEAT_MBYTE) \
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001282 || defined(GLOBAL_IME) \
1283 || defined(PROTO)
1284# ifdef PROTO
1285typedef int WINAPI;
1286# endif
1287
1288 LRESULT WINAPI
1289vim_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1290{
1291# ifdef GLOBAL_IME
1292 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
1293# else
1294 if (wide_WindowProc)
1295 return DefWindowProcW(hwnd, message, wParam, lParam);
1296 return DefWindowProc(hwnd, message, wParam, lParam);
1297#endif
1298}
1299#endif
1300
1301/*
1302 * Called when the foreground or background color has been changed.
1303 */
1304 void
1305gui_mch_new_colors(void)
1306{
1307 /* nothing to do? */
1308}
1309
1310/*
1311 * Set the colors to their default values.
1312 */
1313 void
1314gui_mch_def_colors(void)
1315{
1316 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1317 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1318 gui.def_norm_pixel = gui.norm_pixel;
1319 gui.def_back_pixel = gui.back_pixel;
1320}
1321
1322/*
1323 * Open the GUI window which was created by a call to gui_mch_init().
1324 */
1325 int
1326gui_mch_open(void)
1327{
1328#ifndef SW_SHOWDEFAULT
1329# define SW_SHOWDEFAULT 10 /* Borland 5.0 doesn't have it */
1330#endif
1331 /* Actually open the window, if not already visible
1332 * (may be done already in gui_mch_set_shellsize) */
1333 if (!IsWindowVisible(s_hwnd))
1334 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1335
1336#ifdef MSWIN_FIND_REPLACE
1337 /* Init replace string here, so that we keep it when re-opening the
1338 * dialog. */
1339 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1340#endif
1341
1342 return OK;
1343}
1344
1345/*
1346 * Get the position of the top left corner of the window.
1347 */
1348 int
1349gui_mch_get_winpos(int *x, int *y)
1350{
1351 RECT rect;
1352
1353 GetWindowRect(s_hwnd, &rect);
1354 *x = rect.left;
1355 *y = rect.top;
1356 return OK;
1357}
1358
1359/*
1360 * Set the position of the top left corner of the window to the given
1361 * coordinates.
1362 */
1363 void
1364gui_mch_set_winpos(int x, int y)
1365{
1366 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1367 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1368}
1369 void
1370gui_mch_set_text_area_pos(int x, int y, int w, int h)
1371{
1372 static int oldx = 0;
1373 static int oldy = 0;
1374
1375 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1376
1377#ifdef FEAT_TOOLBAR
1378 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1379 SendMessage(s_toolbarhwnd, WM_SIZE,
1380 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1381#endif
1382#if defined(FEAT_GUI_TABLINE)
1383 if (showing_tabline)
1384 {
1385 int top = 0;
1386 RECT rect;
1387
1388# ifdef FEAT_TOOLBAR
1389 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1390 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1391# endif
1392 GetClientRect(s_hwnd, &rect);
1393 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1394 }
1395#endif
1396
1397 /* When side scroll bar is unshown, the size of window will change.
1398 * then, the text area move left or right. thus client rect should be
1399 * forcedly redrawn. (Yasuhiro Matsumoto) */
1400 if (oldx != x || oldy != y)
1401 {
1402 InvalidateRect(s_hwnd, NULL, FALSE);
1403 oldx = x;
1404 oldy = y;
1405 }
1406}
1407
1408
1409/*
1410 * Scrollbar stuff:
1411 */
1412
1413 void
1414gui_mch_enable_scrollbar(
1415 scrollbar_T *sb,
1416 int flag)
1417{
1418 ShowScrollBar(sb->id, SB_CTL, flag);
1419
1420 /* TODO: When the window is maximized, the size of the window stays the
1421 * same, thus the size of the text area changes. On Win98 it's OK, on Win
1422 * NT 4.0 it's not... */
1423}
1424
1425 void
1426gui_mch_set_scrollbar_pos(
1427 scrollbar_T *sb,
1428 int x,
1429 int y,
1430 int w,
1431 int h)
1432{
1433 SetWindowPos(sb->id, NULL, x, y, w, h,
1434 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1435}
1436
1437 void
1438gui_mch_create_scrollbar(
1439 scrollbar_T *sb,
1440 int orient) /* SBAR_VERT or SBAR_HORIZ */
1441{
1442 sb->id = CreateWindow(
1443 "SCROLLBAR", "Scrollbar",
1444 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
1445 10, /* Any value will do for now */
1446 10, /* Any value will do for now */
1447 s_hwnd, NULL,
1448 s_hinst, NULL);
1449}
1450
1451/*
1452 * Find the scrollbar with the given hwnd.
1453 */
1454 static scrollbar_T *
1455gui_mswin_find_scrollbar(HWND hwnd)
1456{
1457 win_T *wp;
1458
1459 if (gui.bottom_sbar.id == hwnd)
1460 return &gui.bottom_sbar;
1461 FOR_ALL_WINDOWS(wp)
1462 {
1463 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1464 return &wp->w_scrollbars[SBAR_LEFT];
1465 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1466 return &wp->w_scrollbars[SBAR_RIGHT];
1467 }
1468 return NULL;
1469}
1470
1471/*
1472 * Get the character size of a font.
1473 */
1474 static void
1475GetFontSize(GuiFont font)
1476{
1477 HWND hwnd = GetDesktopWindow();
1478 HDC hdc = GetWindowDC(hwnd);
1479 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
1480 TEXTMETRIC tm;
1481
1482 GetTextMetrics(hdc, &tm);
1483 gui.char_width = tm.tmAveCharWidth + tm.tmOverhang;
1484
1485 gui.char_height = tm.tmHeight + p_linespace;
1486
1487 SelectFont(hdc, hfntOld);
1488
1489 ReleaseDC(hwnd, hdc);
1490}
1491
1492/*
1493 * Adjust gui.char_height (after 'linespace' was changed).
1494 */
1495 int
1496gui_mch_adjust_charheight(void)
1497{
1498 GetFontSize(gui.norm_font);
1499 return OK;
1500}
1501
1502 static GuiFont
1503get_font_handle(LOGFONT *lf)
1504{
1505 HFONT font = NULL;
1506
1507 /* Load the font */
1508 font = CreateFontIndirect(lf);
1509
1510 if (font == NULL)
1511 return NOFONT;
1512
1513 return (GuiFont)font;
1514}
1515
1516 static int
1517pixels_to_points(int pixels, int vertical)
1518{
1519 int points;
1520 HWND hwnd;
1521 HDC hdc;
1522
1523 hwnd = GetDesktopWindow();
1524 hdc = GetWindowDC(hwnd);
1525
1526 points = MulDiv(pixels, 72,
1527 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1528
1529 ReleaseDC(hwnd, hdc);
1530
1531 return points;
1532}
1533
1534 GuiFont
1535gui_mch_get_font(
1536 char_u *name,
1537 int giveErrorIfMissing)
1538{
1539 LOGFONT lf;
1540 GuiFont font = NOFONT;
1541
1542 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1543 font = get_font_handle(&lf);
1544 if (font == NOFONT && giveErrorIfMissing)
1545 EMSG2(_(e_font), name);
1546 return font;
1547}
1548
1549#if defined(FEAT_EVAL) || defined(PROTO)
1550/*
1551 * Return the name of font "font" in allocated memory.
1552 * Don't know how to get the actual name, thus use the provided name.
1553 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001554 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001555gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001556{
1557 if (name == NULL)
1558 return NULL;
1559 return vim_strsave(name);
1560}
1561#endif
1562
1563 void
1564gui_mch_free_font(GuiFont font)
1565{
1566 if (font)
1567 DeleteObject((HFONT)font);
1568}
1569
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001570/*
1571 * Return the Pixel value (color) for the given color name.
1572 * Return INVALCOLOR for error.
1573 */
1574 guicolor_T
1575gui_mch_get_color(char_u *name)
1576{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001577 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001578
1579 typedef struct SysColorTable
1580 {
1581 char *name;
1582 int color;
1583 } SysColorTable;
1584
1585 static SysColorTable sys_table[] =
1586 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001587 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1588 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001589#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001590 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1591#endif
1592 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1593 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1594 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1595 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1596 {"SYS_DESKTOP", COLOR_DESKTOP},
1597 {"SYS_INFOBK", COLOR_INFOBK},
1598 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1599 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001600 {"SYS_BTNFACE", COLOR_BTNFACE},
1601 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1602 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1603 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1604 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1605 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1606 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1607 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1608 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1609 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1610 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1611 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1612 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1613 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1614 {"SYS_MENU", COLOR_MENU},
1615 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1616 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1617 {"SYS_WINDOW", COLOR_WINDOW},
1618 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1619 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1620 };
1621
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001622 /*
1623 * Try to look up a system colour.
1624 */
1625 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1626 if (STRICMP(name, sys_table[i].name) == 0)
1627 return GetSysColor(sys_table[i].color);
1628
Bram Moolenaarab302212016-04-26 20:59:29 +02001629 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001630}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001631
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001632 guicolor_T
1633gui_mch_get_rgb_color(int r, int g, int b)
1634{
1635 return gui_get_rgb_color_cmn(r, g, b);
1636}
1637
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001638/*
1639 * Return OK if the key with the termcap name "name" is supported.
1640 */
1641 int
1642gui_mch_haskey(char_u *name)
1643{
1644 int i;
1645
1646 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1647 if (name[0] == special_keys[i].vim_code0 &&
1648 name[1] == special_keys[i].vim_code1)
1649 return OK;
1650 return FAIL;
1651}
1652
1653 void
1654gui_mch_beep(void)
1655{
1656 MessageBeep(MB_OK);
1657}
1658/*
1659 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1660 */
1661 void
1662gui_mch_invert_rectangle(
1663 int r,
1664 int c,
1665 int nr,
1666 int nc)
1667{
1668 RECT rc;
1669
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001670#if defined(FEAT_DIRECTX)
1671 if (IS_ENABLE_DIRECTX())
1672 DWriteContext_Flush(s_dwc);
1673#endif
1674
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001675 /*
1676 * Note: InvertRect() excludes right and bottom of rectangle.
1677 */
1678 rc.left = FILL_X(c);
1679 rc.top = FILL_Y(r);
1680 rc.right = rc.left + nc * gui.char_width;
1681 rc.bottom = rc.top + nr * gui.char_height;
1682 InvertRect(s_hdc, &rc);
1683}
1684
1685/*
1686 * Iconify the GUI window.
1687 */
1688 void
1689gui_mch_iconify(void)
1690{
1691 ShowWindow(s_hwnd, SW_MINIMIZE);
1692}
1693
1694/*
1695 * Draw a cursor without focus.
1696 */
1697 void
1698gui_mch_draw_hollow_cursor(guicolor_T color)
1699{
1700 HBRUSH hbr;
1701 RECT rc;
1702
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001703#if defined(FEAT_DIRECTX)
1704 if (IS_ENABLE_DIRECTX())
1705 DWriteContext_Flush(s_dwc);
1706#endif
1707
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001708 /*
1709 * Note: FrameRect() excludes right and bottom of rectangle.
1710 */
1711 rc.left = FILL_X(gui.col);
1712 rc.top = FILL_Y(gui.row);
1713 rc.right = rc.left + gui.char_width;
1714#ifdef FEAT_MBYTE
1715 if (mb_lefthalve(gui.row, gui.col))
1716 rc.right += gui.char_width;
1717#endif
1718 rc.bottom = rc.top + gui.char_height;
1719 hbr = CreateSolidBrush(color);
1720 FrameRect(s_hdc, &rc, hbr);
1721 DeleteBrush(hbr);
1722}
1723/*
1724 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1725 * color "color".
1726 */
1727 void
1728gui_mch_draw_part_cursor(
1729 int w,
1730 int h,
1731 guicolor_T color)
1732{
1733 HBRUSH hbr;
1734 RECT rc;
1735
1736 /*
1737 * Note: FillRect() excludes right and bottom of rectangle.
1738 */
1739 rc.left =
1740#ifdef FEAT_RIGHTLEFT
1741 /* vertical line should be on the right of current point */
1742 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1743#endif
1744 FILL_X(gui.col);
1745 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1746 rc.right = rc.left + w;
1747 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001748
1749#if defined(FEAT_DIRECTX)
1750 if (IS_ENABLE_DIRECTX())
1751 DWriteContext_Flush(s_dwc);
1752#endif
1753
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001754 hbr = CreateSolidBrush(color);
1755 FillRect(s_hdc, &rc, hbr);
1756 DeleteBrush(hbr);
1757}
1758
1759
1760/*
1761 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1762 * dead key's nominal character and re-post the original message.
1763 */
1764 static void
1765outputDeadKey_rePost(MSG originalMsg)
1766{
1767 static MSG deadCharExpel;
1768
1769 if (!dead_key)
1770 return;
1771
1772 dead_key = 0;
1773
1774 /* Make Windows generate the dead key's character */
1775 deadCharExpel.message = originalMsg.message;
1776 deadCharExpel.hwnd = originalMsg.hwnd;
1777 deadCharExpel.wParam = VK_SPACE;
1778
1779 MyTranslateMessage(&deadCharExpel);
1780
1781 /* re-generate the current character free of the dead char influence */
1782 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1783 originalMsg.lParam);
1784}
1785
1786
1787/*
1788 * Process a single Windows message.
1789 * If one is not available we hang until one is.
1790 */
1791 static void
1792process_message(void)
1793{
1794 MSG msg;
1795 UINT vk = 0; /* Virtual key */
1796 char_u string[40];
1797 int i;
1798 int modifiers = 0;
1799 int key;
1800#ifdef FEAT_MENU
1801 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1802#endif
1803
1804 pGetMessage(&msg, NULL, 0, 0);
1805
1806#ifdef FEAT_OLE
1807 /* Look after OLE Automation commands */
1808 if (msg.message == WM_OLE)
1809 {
1810 char_u *str = (char_u *)msg.lParam;
1811 if (str == NULL || *str == NUL)
1812 {
1813 /* Message can't be ours, forward it. Fixes problem with Ultramon
1814 * 3.0.4 */
1815 pDispatchMessage(&msg);
1816 }
1817 else
1818 {
1819 add_to_input_buf(str, (int)STRLEN(str));
1820 vim_free(str); /* was allocated in CVim::SendKeys() */
1821 }
1822 return;
1823 }
1824#endif
1825
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001826#ifdef MSWIN_FIND_REPLACE
1827 /* Don't process messages used by the dialog */
1828 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1829 {
1830 HandleMouseHide(msg.message, msg.lParam);
1831 return;
1832 }
1833#endif
1834
1835 /*
1836 * Check if it's a special key that we recognise. If not, call
1837 * TranslateMessage().
1838 */
1839 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1840 {
1841 vk = (int) msg.wParam;
1842
1843 /*
1844 * Handle dead keys in special conditions in other cases we let Windows
1845 * handle them and do not interfere.
1846 *
1847 * The dead_key flag must be reset on several occasions:
1848 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1849 * consumed at that point (This is when we let Windows combine the
1850 * dead character on its own)
1851 *
1852 * - Before doing something special such as regenerating keypresses to
1853 * expel the dead character as this could trigger an infinite loop if
1854 * for some reason MyTranslateMessage() do not trigger a call
1855 * immediately to _OnChar() (or _OnSysChar()).
1856 */
1857 if (dead_key)
1858 {
1859 /*
1860 * If a dead key was pressed and the user presses VK_SPACE,
1861 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1862 * with the dead char now, so do nothing special and let Windows
1863 * handle it.
1864 *
1865 * Note that VK_SPACE combines with the dead_key's character and
1866 * only one WM_CHAR will be generated by TranslateMessage(), in
1867 * the two other cases two WM_CHAR will be generated: the dead
1868 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1869 * user expects.
1870 */
1871 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1872 {
1873 dead_key = 0;
1874 MyTranslateMessage(&msg);
1875 return;
1876 }
1877 /* In modes where we are not typing, dead keys should behave
1878 * normally */
1879 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1880 {
1881 outputDeadKey_rePost(msg);
1882 return;
1883 }
1884 }
1885
1886 /* Check for CTRL-BREAK */
1887 if (vk == VK_CANCEL)
1888 {
1889 trash_input_buf();
1890 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001891 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001892 string[0] = Ctrl_C;
1893 add_to_input_buf(string, 1);
1894 }
1895
1896 for (i = 0; special_keys[i].key_sym != 0; i++)
1897 {
1898 /* ignore VK_SPACE when ALT key pressed: system menu */
1899 if (special_keys[i].key_sym == vk
1900 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1901 {
1902 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001903 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001904 * is a key that would normally trigger the dead key nominal
1905 * character output (such as a NUMPAD printable character or
1906 * the TAB key, etc...).
1907 */
1908 if (dead_key && (special_keys[i].vim_code0 == 'K'
1909 || vk == VK_TAB || vk == CAR))
1910 {
1911 outputDeadKey_rePost(msg);
1912 return;
1913 }
1914
1915#ifdef FEAT_MENU
1916 /* Check for <F10>: Windows selects the menu. When <F10> is
1917 * mapped we want to use the mapping instead. */
1918 if (vk == VK_F10
1919 && gui.menu_is_active
1920 && check_map(k10, State, FALSE, TRUE, FALSE,
1921 NULL, NULL) == NULL)
1922 break;
1923#endif
1924 if (GetKeyState(VK_SHIFT) & 0x8000)
1925 modifiers |= MOD_MASK_SHIFT;
1926 /*
1927 * Don't use caps-lock as shift, because these are special keys
1928 * being considered here, and we only want letters to get
1929 * shifted -- webb
1930 */
1931 /*
1932 if (GetKeyState(VK_CAPITAL) & 0x0001)
1933 modifiers ^= MOD_MASK_SHIFT;
1934 */
1935 if (GetKeyState(VK_CONTROL) & 0x8000)
1936 modifiers |= MOD_MASK_CTRL;
1937 if (GetKeyState(VK_MENU) & 0x8000)
1938 modifiers |= MOD_MASK_ALT;
1939
1940 if (special_keys[i].vim_code1 == NUL)
1941 key = special_keys[i].vim_code0;
1942 else
1943 key = TO_SPECIAL(special_keys[i].vim_code0,
1944 special_keys[i].vim_code1);
1945 key = simplify_key(key, &modifiers);
1946 if (key == CSI)
1947 key = K_CSI;
1948
1949 if (modifiers)
1950 {
1951 string[0] = CSI;
1952 string[1] = KS_MODIFIER;
1953 string[2] = modifiers;
1954 add_to_input_buf(string, 3);
1955 }
1956
1957 if (IS_SPECIAL(key))
1958 {
1959 string[0] = CSI;
1960 string[1] = K_SECOND(key);
1961 string[2] = K_THIRD(key);
1962 add_to_input_buf(string, 3);
1963 }
1964 else
1965 {
1966 int len;
1967
1968 /* Handle "key" as a Unicode character. */
1969 len = char_to_string(key, string, 40, FALSE);
1970 add_to_input_buf(string, len);
1971 }
1972 break;
1973 }
1974 }
1975 if (special_keys[i].key_sym == 0)
1976 {
1977 /* Some keys need C-S- where they should only need C-.
1978 * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1979 * system startup (Helmut Stiegler, 2003 Oct 3). */
1980 if (vk != 0xff
1981 && (GetKeyState(VK_CONTROL) & 0x8000)
1982 && !(GetKeyState(VK_SHIFT) & 0x8000)
1983 && !(GetKeyState(VK_MENU) & 0x8000))
1984 {
1985 /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */
1986 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1987 {
1988 string[0] = Ctrl_HAT;
1989 add_to_input_buf(string, 1);
1990 }
1991 /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */
1992 else if (vk == 0xBD) /* QWERTY for CTRL-'-' */
1993 {
1994 string[0] = Ctrl__;
1995 add_to_input_buf(string, 1);
1996 }
1997 /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */
1998 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
1999 {
2000 string[0] = Ctrl_AT;
2001 add_to_input_buf(string, 1);
2002 }
2003 else
2004 MyTranslateMessage(&msg);
2005 }
2006 else
2007 MyTranslateMessage(&msg);
2008 }
2009 }
2010#ifdef FEAT_MBYTE_IME
2011 else if (msg.message == WM_IME_NOTIFY)
2012 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2013 else if (msg.message == WM_KEYUP && im_get_status())
2014 /* added for non-MS IME (Yasuhiro Matsumoto) */
2015 MyTranslateMessage(&msg);
2016#endif
2017#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
2018/* GIME_TEST */
2019 else if (msg.message == WM_IME_STARTCOMPOSITION)
2020 {
2021 POINT point;
2022
2023 global_ime_set_font(&norm_logfont);
2024 point.x = FILL_X(gui.col);
2025 point.y = FILL_Y(gui.row);
2026 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
2027 global_ime_set_position(&point);
2028 }
2029#endif
2030
2031#ifdef FEAT_MENU
2032 /* Check for <F10>: Default effect is to select the menu. When <F10> is
2033 * mapped we need to stop it here to avoid strange effects (e.g., for the
2034 * key-up event) */
2035 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2036 NULL, NULL) == NULL)
2037#endif
2038 pDispatchMessage(&msg);
2039}
2040
2041/*
2042 * Catch up with any queued events. This may put keyboard input into the
2043 * input buffer, call resize call-backs, trigger timers etc. If there is
2044 * nothing in the event queue (& no timers pending), then we return
2045 * immediately.
2046 */
2047 void
2048gui_mch_update(void)
2049{
2050 MSG msg;
2051
2052 if (!s_busy_processing)
2053 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2054 && !vim_is_input_buf_full())
2055 process_message();
2056}
2057
Bram Moolenaar4231da42016-06-02 14:30:04 +02002058 static void
2059remove_any_timer(void)
2060{
2061 MSG msg;
2062
2063 if (s_wait_timer != 0 && !s_timed_out)
2064 {
2065 KillTimer(NULL, s_wait_timer);
2066
2067 /* Eat spurious WM_TIMER messages */
2068 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2069 ;
2070 s_wait_timer = 0;
2071 }
2072}
2073
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002074/*
2075 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2076 * from the keyboard.
2077 * wtime == -1 Wait forever.
2078 * wtime == 0 This should never happen.
2079 * wtime > 0 Wait wtime milliseconds for a character.
2080 * Returns OK if a character was found to be available within the given time,
2081 * or FAIL otherwise.
2082 */
2083 int
2084gui_mch_wait_for_chars(int wtime)
2085{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002086 int focus;
2087
2088 s_timed_out = FALSE;
2089
2090 if (wtime > 0)
2091 {
2092 /* Don't do anything while processing a (scroll) message. */
2093 if (s_busy_processing)
2094 return FAIL;
2095 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)wtime,
2096 (TIMERPROC)_OnTimer);
2097 }
2098
2099 allow_scrollbar = TRUE;
2100
2101 focus = gui.in_focus;
2102 while (!s_timed_out)
2103 {
2104 /* Stop or start blinking when focus changes */
2105 if (gui.in_focus != focus)
2106 {
2107 if (gui.in_focus)
2108 gui_mch_start_blink();
2109 else
2110 gui_mch_stop_blink();
2111 focus = gui.in_focus;
2112 }
2113
2114 if (s_need_activate)
2115 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002116 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002117 s_need_activate = FALSE;
2118 }
2119
Bram Moolenaar4231da42016-06-02 14:30:04 +02002120#ifdef FEAT_TIMERS
2121 did_add_timer = FALSE;
2122#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002123#ifdef MESSAGE_QUEUE
Bram Moolenaar62426e12017-08-13 15:37:58 +02002124 /* Check channel I/O while waiting for a message. */
Bram Moolenaar9186a272016-02-23 19:34:01 +01002125 for (;;)
2126 {
2127 MSG msg;
2128
2129 parse_queued_messages();
2130
Bram Moolenaar62426e12017-08-13 15:37:58 +02002131 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2132 {
2133 process_message();
2134 break;
2135 }
2136 else if (MsgWaitForMultipleObjects(0, NULL, FALSE, 100, QS_ALLINPUT)
2137 != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002138 break;
2139 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002140#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002141 /*
2142 * Don't use gui_mch_update() because then we will spin-lock until a
2143 * char arrives, instead we use GetMessage() to hang until an
2144 * event arrives. No need to check for input_buf_full because we are
2145 * returning as soon as it contains a single char -- webb
2146 */
2147 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002148#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002149
2150 if (input_available())
2151 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002152 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002153 allow_scrollbar = FALSE;
2154
2155 /* Clear pending mouse button, the release event may have been
2156 * taken by the dialog window. But don't do this when getting
2157 * focus, we need the mouse-up event then. */
2158 if (!s_getting_focus)
2159 s_button_pending = -1;
2160
2161 return OK;
2162 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002163
2164#ifdef FEAT_TIMERS
2165 if (did_add_timer)
2166 {
2167 /* Need to recompute the waiting time. */
2168 remove_any_timer();
2169 break;
2170 }
2171#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002172 }
2173 allow_scrollbar = FALSE;
2174 return FAIL;
2175}
2176
2177/*
2178 * Clear a rectangular region of the screen from text pos (row1, col1) to
2179 * (row2, col2) inclusive.
2180 */
2181 void
2182gui_mch_clear_block(
2183 int row1,
2184 int col1,
2185 int row2,
2186 int col2)
2187{
2188 RECT rc;
2189
2190 /*
2191 * Clear one extra pixel at the far right, for when bold characters have
2192 * spilled over to the window border.
2193 * Note: FillRect() excludes right and bottom of rectangle.
2194 */
2195 rc.left = FILL_X(col1);
2196 rc.top = FILL_Y(row1);
2197 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2198 rc.bottom = FILL_Y(row2 + 1);
2199 clear_rect(&rc);
2200}
2201
2202/*
2203 * Clear the whole text window.
2204 */
2205 void
2206gui_mch_clear_all(void)
2207{
2208 RECT rc;
2209
2210 rc.left = 0;
2211 rc.top = 0;
2212 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2213 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2214 clear_rect(&rc);
2215}
2216/*
2217 * Menu stuff.
2218 */
2219
2220 void
2221gui_mch_enable_menu(int flag)
2222{
2223#ifdef FEAT_MENU
2224 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2225#endif
2226}
2227
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002228 void
2229gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002230 int x UNUSED,
2231 int y UNUSED,
2232 int w UNUSED,
2233 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002234{
2235 /* It will be in the right place anyway */
2236}
2237
2238#if defined(FEAT_MENU) || defined(PROTO)
2239/*
2240 * Make menu item hidden or not hidden
2241 */
2242 void
2243gui_mch_menu_hidden(
2244 vimmenu_T *menu,
2245 int hidden)
2246{
2247 /*
2248 * This doesn't do what we want. Hmm, just grey the menu items for now.
2249 */
2250 /*
2251 if (hidden)
2252 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2253 else
2254 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2255 */
2256 gui_mch_menu_grey(menu, hidden);
2257}
2258
2259/*
2260 * This is called after setting all the menus to grey/hidden or not.
2261 */
2262 void
2263gui_mch_draw_menubar(void)
2264{
2265 DrawMenuBar(s_hwnd);
2266}
2267#endif /*FEAT_MENU*/
2268
2269#ifndef PROTO
2270void
2271#ifdef VIMDLL
2272_export
2273#endif
2274_cdecl
2275SaveInst(HINSTANCE hInst)
2276{
2277 s_hinst = hInst;
2278}
2279#endif
2280
2281/*
2282 * Return the RGB value of a pixel as a long.
2283 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002284 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002285gui_mch_get_rgb(guicolor_T pixel)
2286{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002287 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2288 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002289}
2290
2291#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2292/* Convert pixels in X to dialog units */
2293 static WORD
2294PixelToDialogX(int numPixels)
2295{
2296 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2297}
2298
2299/* Convert pixels in Y to dialog units */
2300 static WORD
2301PixelToDialogY(int numPixels)
2302{
2303 return (WORD)((numPixels * 8) / s_dlgfntheight);
2304}
2305
2306/* Return the width in pixels of the given text in the given DC. */
2307 static int
2308GetTextWidth(HDC hdc, char_u *str, int len)
2309{
2310 SIZE size;
2311
2312 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2313 return size.cx;
2314}
2315
2316#ifdef FEAT_MBYTE
2317/*
2318 * Return the width in pixels of the given text in the given DC, taking care
2319 * of 'encoding' to active codepage conversion.
2320 */
2321 static int
2322GetTextWidthEnc(HDC hdc, char_u *str, int len)
2323{
2324 SIZE size;
2325 WCHAR *wstr;
2326 int n;
2327 int wlen = len;
2328
2329 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2330 {
2331 /* 'encoding' differs from active codepage: convert text and use wide
2332 * function */
2333 wstr = enc_to_utf16(str, &wlen);
2334 if (wstr != NULL)
2335 {
2336 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2337 vim_free(wstr);
2338 if (n)
2339 return size.cx;
2340 }
2341 }
2342
2343 return GetTextWidth(hdc, str, len);
2344}
2345#else
2346# define GetTextWidthEnc(h, s, l) GetTextWidth((h), (s), (l))
2347#endif
2348
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002349static void get_work_area(RECT *spi_rect);
2350
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002351/*
2352 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002353 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2354 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002355 */
2356 static BOOL
2357CenterWindow(
2358 HWND hwndChild,
2359 HWND hwndParent)
2360{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002361 HMONITOR mon;
2362 MONITORINFO moninfo;
2363 RECT rChild, rParent, rScreen;
2364 int wChild, hChild, wParent, hParent;
2365 int xNew, yNew;
2366 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002367
2368 GetWindowRect(hwndChild, &rChild);
2369 wChild = rChild.right - rChild.left;
2370 hChild = rChild.bottom - rChild.top;
2371
2372 /* If Vim is minimized put the window in the middle of the screen. */
2373 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002374 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002375 else
2376 GetWindowRect(hwndParent, &rParent);
2377 wParent = rParent.right - rParent.left;
2378 hParent = rParent.bottom - rParent.top;
2379
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002380 moninfo.cbSize = sizeof(MONITORINFO);
2381 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2382 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002383 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002384 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002385 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002386 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002387 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002388 hdc = GetDC(hwndChild);
2389 rScreen.left = 0;
2390 rScreen.top = 0;
2391 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2392 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2393 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002394 }
2395
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002396 xNew = rParent.left + ((wParent - wChild) / 2);
2397 if (xNew < rScreen.left)
2398 xNew = rScreen.left;
2399 else if ((xNew + wChild) > rScreen.right)
2400 xNew = rScreen.right - wChild;
2401
2402 yNew = rParent.top + ((hParent - hChild) / 2);
2403 if (yNew < rScreen.top)
2404 yNew = rScreen.top;
2405 else if ((yNew + hChild) > rScreen.bottom)
2406 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002407
2408 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2409 SWP_NOSIZE | SWP_NOZORDER);
2410}
2411#endif /* FEAT_GUI_DIALOG */
2412
2413void
2414gui_mch_activate_window(void)
2415{
2416 (void)SetActiveWindow(s_hwnd);
2417}
2418
2419#if defined(FEAT_TOOLBAR) || defined(PROTO)
2420 void
2421gui_mch_show_toolbar(int showit)
2422{
2423 if (s_toolbarhwnd == NULL)
2424 return;
2425
2426 if (showit)
2427 {
2428# ifdef FEAT_MBYTE
2429# ifndef TB_SETUNICODEFORMAT
2430 /* For older compilers. We assume this never changes. */
2431# define TB_SETUNICODEFORMAT 0x2005
2432# endif
2433 /* Enable/disable unicode support */
2434 int uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2435 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2436# endif
2437 ShowWindow(s_toolbarhwnd, SW_SHOW);
2438 }
2439 else
2440 ShowWindow(s_toolbarhwnd, SW_HIDE);
2441}
2442
2443/* Then number of bitmaps is fixed. Exit is missing! */
2444#define TOOLBAR_BITMAP_COUNT 31
2445
2446#endif
2447
2448#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2449 static void
2450add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2451{
2452#ifdef FEAT_MBYTE
2453 WCHAR *wn = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002454
2455 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2456 {
2457 /* 'encoding' differs from active codepage: convert menu name
2458 * and use wide function */
2459 wn = enc_to_utf16(item_text, NULL);
2460 if (wn != NULL)
2461 {
2462 MENUITEMINFOW infow;
2463
2464 infow.cbSize = sizeof(infow);
2465 infow.fMask = MIIM_TYPE | MIIM_ID;
2466 infow.wID = item_id;
2467 infow.fType = MFT_STRING;
2468 infow.dwTypeData = wn;
2469 infow.cch = (UINT)wcslen(wn);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002470 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002471 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002472 }
2473 }
2474
2475 if (wn == NULL)
2476#endif
2477 {
2478 MENUITEMINFO info;
2479
2480 info.cbSize = sizeof(info);
2481 info.fMask = MIIM_TYPE | MIIM_ID;
2482 info.wID = item_id;
2483 info.fType = MFT_STRING;
2484 info.dwTypeData = (LPTSTR)item_text;
2485 info.cch = (UINT)STRLEN(item_text);
2486 InsertMenuItem(pmenu, item_id, FALSE, &info);
2487 }
2488}
2489
2490 static void
2491show_tabline_popup_menu(void)
2492{
2493 HMENU tab_pmenu;
2494 long rval;
2495 POINT pt;
2496
2497 /* When ignoring events don't show the menu. */
2498 if (hold_gui_events
2499# ifdef FEAT_CMDWIN
2500 || cmdwin_type != 0
2501# endif
2502 )
2503 return;
2504
2505 tab_pmenu = CreatePopupMenu();
2506 if (tab_pmenu == NULL)
2507 return;
2508
2509 if (first_tabpage->tp_next != NULL)
2510 add_tabline_popup_menu_entry(tab_pmenu,
2511 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2512 add_tabline_popup_menu_entry(tab_pmenu,
2513 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2514 add_tabline_popup_menu_entry(tab_pmenu,
2515 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2516
2517 GetCursorPos(&pt);
2518 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2519 NULL);
2520
2521 DestroyMenu(tab_pmenu);
2522
2523 /* Add the string cmd into input buffer */
2524 if (rval > 0)
2525 {
2526 TCHITTESTINFO htinfo;
2527 int idx;
2528
2529 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2530 return;
2531
2532 htinfo.pt.x = pt.x;
2533 htinfo.pt.y = pt.y;
2534 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2535 if (idx == -1)
2536 idx = 0;
2537 else
2538 idx += 1;
2539
2540 send_tabline_menu_event(idx, (int)rval);
2541 }
2542}
2543
2544/*
2545 * Show or hide the tabline.
2546 */
2547 void
2548gui_mch_show_tabline(int showit)
2549{
2550 if (s_tabhwnd == NULL)
2551 return;
2552
2553 if (!showit != !showing_tabline)
2554 {
2555 if (showit)
2556 ShowWindow(s_tabhwnd, SW_SHOW);
2557 else
2558 ShowWindow(s_tabhwnd, SW_HIDE);
2559 showing_tabline = showit;
2560 }
2561}
2562
2563/*
2564 * Return TRUE when tabline is displayed.
2565 */
2566 int
2567gui_mch_showing_tabline(void)
2568{
2569 return s_tabhwnd != NULL && showing_tabline;
2570}
2571
2572/*
2573 * Update the labels of the tabline.
2574 */
2575 void
2576gui_mch_update_tabline(void)
2577{
2578 tabpage_T *tp;
2579 TCITEM tie;
2580 int nr = 0;
2581 int curtabidx = 0;
2582 int tabadded = 0;
2583#ifdef FEAT_MBYTE
2584 static int use_unicode = FALSE;
2585 int uu;
2586 WCHAR *wstr = NULL;
2587#endif
2588
2589 if (s_tabhwnd == NULL)
2590 return;
2591
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002592#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002593# ifndef CCM_SETUNICODEFORMAT
2594 /* For older compilers. We assume this never changes. */
2595# define CCM_SETUNICODEFORMAT 0x2005
2596# endif
2597 uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2598 if (uu != use_unicode)
2599 {
2600 /* Enable/disable unicode support */
2601 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2602 use_unicode = uu;
2603 }
2604#endif
2605
2606 tie.mask = TCIF_TEXT;
2607 tie.iImage = -1;
2608
2609 /* Disable redraw for tab updates to eliminate O(N^2) draws. */
2610 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2611
2612 /* Add a label for each tab page. They all contain the same text area. */
2613 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2614 {
2615 if (tp == curtab)
2616 curtabidx = nr;
2617
2618 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2619 {
2620 /* Add the tab */
2621 tie.pszText = "-Empty-";
2622 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2623 tabadded = 1;
2624 }
2625
2626 get_tabline_label(tp, FALSE);
2627 tie.pszText = (LPSTR)NameBuff;
2628#ifdef FEAT_MBYTE
2629 wstr = NULL;
2630 if (use_unicode)
2631 {
2632 /* Need to go through Unicode. */
2633 wstr = enc_to_utf16(NameBuff, NULL);
2634 if (wstr != NULL)
2635 {
2636 TCITEMW tiw;
2637
2638 tiw.mask = TCIF_TEXT;
2639 tiw.iImage = -1;
2640 tiw.pszText = wstr;
2641 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2642 vim_free(wstr);
2643 }
2644 }
2645 if (wstr == NULL)
2646#endif
2647 {
2648 TabCtrl_SetItem(s_tabhwnd, nr, &tie);
2649 }
2650 }
2651
2652 /* Remove any old labels. */
2653 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2654 TabCtrl_DeleteItem(s_tabhwnd, nr);
2655
2656 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2657 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2658
2659 /* Re-enable redraw and redraw. */
2660 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2661 RedrawWindow(s_tabhwnd, NULL, NULL,
2662 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2663
2664 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2665 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2666}
2667
2668/*
2669 * Set the current tab to "nr". First tab is 1.
2670 */
2671 void
2672gui_mch_set_curtab(int nr)
2673{
2674 if (s_tabhwnd == NULL)
2675 return;
2676
2677 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2678 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2679}
2680
2681#endif
2682
2683/*
2684 * ":simalt" command.
2685 */
2686 void
2687ex_simalt(exarg_T *eap)
2688{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002689 char_u *keys = eap->arg;
2690 int fill_typebuf = FALSE;
2691 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002692
2693 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2694 while (*keys)
2695 {
2696 if (*keys == '~')
2697 *keys = ' '; /* for showing system menu */
2698 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2699 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002700 fill_typebuf = TRUE;
2701 }
2702 if (fill_typebuf)
2703 {
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002704 /* Put a NOP in the typeahead buffer so that the message will get
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002705 * processed. */
2706 key_name[0] = K_SPECIAL;
2707 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002708 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002709 key_name[3] = NUL;
2710 typebuf_was_filled = TRUE;
2711 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002712 }
2713}
2714
2715/*
2716 * Create the find & replace dialogs.
2717 * You can't have both at once: ":find" when replace is showing, destroys
2718 * the replace dialog first, and the other way around.
2719 */
2720#ifdef MSWIN_FIND_REPLACE
2721 static void
2722initialise_findrep(char_u *initial_string)
2723{
2724 int wword = FALSE;
2725 int mcase = !p_ic;
2726 char_u *entry_text;
2727
2728 /* Get the search string to use. */
2729 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2730
2731 s_findrep_struct.hwndOwner = s_hwnd;
2732 s_findrep_struct.Flags = FR_DOWN;
2733 if (mcase)
2734 s_findrep_struct.Flags |= FR_MATCHCASE;
2735 if (wword)
2736 s_findrep_struct.Flags |= FR_WHOLEWORD;
2737 if (entry_text != NULL && *entry_text != NUL)
2738 vim_strncpy((char_u *)s_findrep_struct.lpstrFindWhat, entry_text,
2739 s_findrep_struct.wFindWhatLen - 1);
2740 vim_free(entry_text);
2741}
2742#endif
2743
2744 static void
2745set_window_title(HWND hwnd, char *title)
2746{
2747#ifdef FEAT_MBYTE
2748 if (title != NULL && enc_codepage >= 0 && enc_codepage != (int)GetACP())
2749 {
2750 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002751
2752 /* Convert the title from 'encoding' to UTF-16. */
2753 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2754 if (wbuf != NULL)
2755 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002756 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002757 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002758 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002759 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002760 }
2761#endif
2762 (void)SetWindowText(hwnd, (LPCSTR)title);
2763}
2764
2765 void
2766gui_mch_find_dialog(exarg_T *eap)
2767{
2768#ifdef MSWIN_FIND_REPLACE
2769 if (s_findrep_msg != 0)
2770 {
2771 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2772 DestroyWindow(s_findrep_hwnd);
2773
2774 if (!IsWindow(s_findrep_hwnd))
2775 {
2776 initialise_findrep(eap->arg);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002777# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002778 /* If the OS is Windows NT, and 'encoding' differs from active
2779 * codepage: convert text and use wide function. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002780 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002781 {
2782 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2783 s_findrep_hwnd = FindTextW(
2784 (LPFINDREPLACEW) &s_findrep_struct_w);
2785 }
2786 else
2787# endif
2788 s_findrep_hwnd = FindText((LPFINDREPLACE) &s_findrep_struct);
2789 }
2790
2791 set_window_title(s_findrep_hwnd,
2792 _("Find string (use '\\\\' to find a '\\')"));
2793 (void)SetFocus(s_findrep_hwnd);
2794
2795 s_findrep_is_find = TRUE;
2796 }
2797#endif
2798}
2799
2800
2801 void
2802gui_mch_replace_dialog(exarg_T *eap)
2803{
2804#ifdef MSWIN_FIND_REPLACE
2805 if (s_findrep_msg != 0)
2806 {
2807 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2808 DestroyWindow(s_findrep_hwnd);
2809
2810 if (!IsWindow(s_findrep_hwnd))
2811 {
2812 initialise_findrep(eap->arg);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002813# ifdef FEAT_MBYTE
2814 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002815 {
2816 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2817 s_findrep_hwnd = ReplaceTextW(
2818 (LPFINDREPLACEW) &s_findrep_struct_w);
2819 }
2820 else
2821# endif
2822 s_findrep_hwnd = ReplaceText(
2823 (LPFINDREPLACE) &s_findrep_struct);
2824 }
2825
2826 set_window_title(s_findrep_hwnd,
2827 _("Find & Replace (use '\\\\' to find a '\\')"));
2828 (void)SetFocus(s_findrep_hwnd);
2829
2830 s_findrep_is_find = FALSE;
2831 }
2832#endif
2833}
2834
2835
2836/*
2837 * Set visibility of the pointer.
2838 */
2839 void
2840gui_mch_mousehide(int hide)
2841{
2842 if (hide != gui.pointer_hidden)
2843 {
2844 ShowCursor(!hide);
2845 gui.pointer_hidden = hide;
2846 }
2847}
2848
2849#ifdef FEAT_MENU
2850 static void
2851gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2852{
2853 /* Unhide the mouse, we don't get move events here. */
2854 gui_mch_mousehide(FALSE);
2855
2856 (void)TrackPopupMenu(
2857 (HMENU)menu->submenu_id,
2858 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2859 x, y,
2860 (int)0, /*reserved param*/
2861 s_hwnd,
2862 NULL);
2863 /*
2864 * NOTE: The pop-up menu can eat the mouse up event.
2865 * We deal with this in normal.c.
2866 */
2867}
2868#endif
2869
2870/*
2871 * Got a message when the system will go down.
2872 */
2873 static void
2874_OnEndSession(void)
2875{
2876 getout_preserve_modified(1);
2877}
2878
2879/*
2880 * Get this message when the user clicks on the cross in the top right corner
2881 * of a Windows95 window.
2882 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002883 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002884_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002885{
2886 gui_shell_closed();
2887}
2888
2889/*
2890 * Get a message when the window is being destroyed.
2891 */
2892 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002893_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002894{
2895 if (!destroying)
2896 _OnClose(hwnd);
2897}
2898
2899 static void
2900_OnPaint(
2901 HWND hwnd)
2902{
2903 if (!IsMinimized(hwnd))
2904 {
2905 PAINTSTRUCT ps;
2906
2907 out_flush(); /* make sure all output has been processed */
2908 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002909
2910#ifdef FEAT_MBYTE
2911 /* prevent multi-byte characters from misprinting on an invalid
2912 * rectangle */
2913 if (has_mbyte)
2914 {
2915 RECT rect;
2916
2917 GetClientRect(hwnd, &rect);
2918 ps.rcPaint.left = rect.left;
2919 ps.rcPaint.right = rect.right;
2920 }
2921#endif
2922
2923 if (!IsRectEmpty(&ps.rcPaint))
2924 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002925 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2926 ps.rcPaint.right - ps.rcPaint.left + 1,
2927 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2928 }
2929
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002930 EndPaint(hwnd, &ps);
2931 }
2932}
2933
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002934 static void
2935_OnSize(
2936 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002937 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002938 int cx,
2939 int cy)
2940{
2941 if (!IsMinimized(hwnd))
2942 {
2943 gui_resize_shell(cx, cy);
2944
2945#ifdef FEAT_MENU
2946 /* Menu bar may wrap differently now */
2947 gui_mswin_get_menu_height(TRUE);
2948#endif
2949 }
2950}
2951
2952 static void
2953_OnSetFocus(
2954 HWND hwnd,
2955 HWND hwndOldFocus)
2956{
2957 gui_focus_change(TRUE);
2958 s_getting_focus = TRUE;
2959 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2960}
2961
2962 static void
2963_OnKillFocus(
2964 HWND hwnd,
2965 HWND hwndNewFocus)
2966{
2967 gui_focus_change(FALSE);
2968 s_getting_focus = FALSE;
2969 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2970}
2971
2972/*
2973 * Get a message when the user switches back to vim
2974 */
2975 static LRESULT
2976_OnActivateApp(
2977 HWND hwnd,
2978 BOOL fActivate,
2979 DWORD dwThreadId)
2980{
2981 /* we call gui_focus_change() in _OnSetFocus() */
2982 /* gui_focus_change((int)fActivate); */
2983 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2984}
2985
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002986 void
2987gui_mch_destroy_scrollbar(scrollbar_T *sb)
2988{
2989 DestroyWindow(sb->id);
2990}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002991
2992/*
2993 * Get current mouse coordinates in text window.
2994 */
2995 void
2996gui_mch_getmouse(int *x, int *y)
2997{
2998 RECT rct;
2999 POINT mp;
3000
3001 (void)GetWindowRect(s_textArea, &rct);
3002 (void)GetCursorPos((LPPOINT)&mp);
3003 *x = (int)(mp.x - rct.left);
3004 *y = (int)(mp.y - rct.top);
3005}
3006
3007/*
3008 * Move mouse pointer to character at (x, y).
3009 */
3010 void
3011gui_mch_setmouse(int x, int y)
3012{
3013 RECT rct;
3014
3015 (void)GetWindowRect(s_textArea, &rct);
3016 (void)SetCursorPos(x + gui.border_offset + rct.left,
3017 y + gui.border_offset + rct.top);
3018}
3019
3020 static void
3021gui_mswin_get_valid_dimensions(
3022 int w,
3023 int h,
3024 int *valid_w,
3025 int *valid_h)
3026{
3027 int base_width, base_height;
3028
3029 base_width = gui_get_base_width()
3030 + (GetSystemMetrics(SM_CXFRAME) +
3031 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
3032 base_height = gui_get_base_height()
3033 + (GetSystemMetrics(SM_CYFRAME) +
3034 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3035 + GetSystemMetrics(SM_CYCAPTION)
3036#ifdef FEAT_MENU
3037 + gui_mswin_get_menu_height(FALSE)
3038#endif
3039 ;
3040 *valid_w = base_width +
3041 ((w - base_width) / gui.char_width) * gui.char_width;
3042 *valid_h = base_height +
3043 ((h - base_height) / gui.char_height) * gui.char_height;
3044}
3045
3046 void
3047gui_mch_flash(int msec)
3048{
3049 RECT rc;
3050
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003051#if defined(FEAT_DIRECTX)
3052 if (IS_ENABLE_DIRECTX())
3053 DWriteContext_Flush(s_dwc);
3054#endif
3055
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003056 /*
3057 * Note: InvertRect() excludes right and bottom of rectangle.
3058 */
3059 rc.left = 0;
3060 rc.top = 0;
3061 rc.right = gui.num_cols * gui.char_width;
3062 rc.bottom = gui.num_rows * gui.char_height;
3063 InvertRect(s_hdc, &rc);
3064 gui_mch_flush(); /* make sure it's displayed */
3065
3066 ui_delay((long)msec, TRUE); /* wait for a few msec */
3067
3068 InvertRect(s_hdc, &rc);
3069}
3070
3071/*
3072 * Return flags used for scrolling.
3073 * The SW_INVALIDATE is required when part of the window is covered or
3074 * off-screen. Refer to MS KB Q75236.
3075 */
3076 static int
3077get_scroll_flags(void)
3078{
3079 HWND hwnd;
3080 RECT rcVim, rcOther, rcDest;
3081
3082 GetWindowRect(s_hwnd, &rcVim);
3083
3084 /* Check if the window is partly above or below the screen. We don't care
3085 * about partly left or right of the screen, it is not relevant when
3086 * scrolling up or down. */
3087 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
3088 return SW_INVALIDATE;
3089
3090 /* Check if there is an window (partly) on top of us. */
3091 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3092 if (IsWindowVisible(hwnd))
3093 {
3094 GetWindowRect(hwnd, &rcOther);
3095 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3096 return SW_INVALIDATE;
3097 }
3098 return 0;
3099}
3100
3101/*
3102 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3103 * may not be scrolled out properly.
3104 * For gVim, when _OnScroll() is repeated, the character at the
3105 * previous cursor position may be left drawn after scroll.
3106 * The problem can be avoided by calling GetPixel() to get a pixel in
3107 * the region before ScrollWindowEx().
3108 */
3109 static void
3110intel_gpu_workaround(void)
3111{
3112 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3113}
3114
3115/*
3116 * Delete the given number of lines from the given row, scrolling up any
3117 * text further down within the scroll region.
3118 */
3119 void
3120gui_mch_delete_lines(
3121 int row,
3122 int num_lines)
3123{
3124 RECT rc;
3125
3126 intel_gpu_workaround();
3127
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003128#if defined(FEAT_DIRECTX)
3129 // Commit drawing queue before ScrollWindowEx.
3130 if (IS_ENABLE_DIRECTX())
3131 DWriteContext_Flush(s_dwc);
3132#endif
3133
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003134 rc.left = FILL_X(gui.scroll_region_left);
3135 rc.right = FILL_X(gui.scroll_region_right + 1);
3136 rc.top = FILL_Y(row);
3137 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3138
3139 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
3140 &rc, &rc, NULL, NULL, get_scroll_flags());
3141
3142 UpdateWindow(s_textArea);
3143 /* This seems to be required to avoid the cursor disappearing when
3144 * scrolling such that the cursor ends up in the top-left character on
3145 * the screen... But why? (Webb) */
3146 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3147 /* gui.cursor_is_valid = FALSE; */
3148
3149 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3150 gui.scroll_region_left,
3151 gui.scroll_region_bot, gui.scroll_region_right);
3152}
3153
3154/*
3155 * Insert the given number of lines before the given row, scrolling down any
3156 * following text within the scroll region.
3157 */
3158 void
3159gui_mch_insert_lines(
3160 int row,
3161 int num_lines)
3162{
3163 RECT rc;
3164
3165 intel_gpu_workaround();
3166
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003167#if defined(FEAT_DIRECTX)
3168 // Commit drawing queue before ScrollWindowEx.
3169 if (IS_ENABLE_DIRECTX())
3170 DWriteContext_Flush(s_dwc);
3171#endif
3172
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003173 rc.left = FILL_X(gui.scroll_region_left);
3174 rc.right = FILL_X(gui.scroll_region_right + 1);
3175 rc.top = FILL_Y(row);
3176 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3177 /* The SW_INVALIDATE is required when part of the window is covered or
3178 * off-screen. How do we avoid it when it's not needed? */
3179 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
3180 &rc, &rc, NULL, NULL, get_scroll_flags());
3181
3182 UpdateWindow(s_textArea);
3183
3184 gui_clear_block(row, gui.scroll_region_left,
3185 row + num_lines - 1, gui.scroll_region_right);
3186}
3187
3188
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003189 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003190gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003191{
3192#if defined(FEAT_DIRECTX)
3193 DWriteContext_Close(s_dwc);
3194 DWrite_Final();
3195 s_dwc = NULL;
3196#endif
3197
3198 ReleaseDC(s_textArea, s_hdc);
3199 DeleteObject(s_brush);
3200
3201#ifdef FEAT_TEAROFF
3202 /* Unload the tearoff bitmap */
3203 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3204#endif
3205
3206 /* Destroy our window (if we have one). */
3207 if (s_hwnd != NULL)
3208 {
3209 destroying = TRUE; /* ignore WM_DESTROY message now */
3210 DestroyWindow(s_hwnd);
3211 }
3212
3213#ifdef GLOBAL_IME
3214 global_ime_end();
3215#endif
3216}
3217
3218 static char_u *
3219logfont2name(LOGFONT lf)
3220{
3221 char *p;
3222 char *res;
3223 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003224 char *quality_name;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003225 char *font_name = lf.lfFaceName;
3226
3227 charset_name = charset_id2name((int)lf.lfCharSet);
3228#ifdef FEAT_MBYTE
3229 /* Convert a font name from the current codepage to 'encoding'.
3230 * TODO: Use Wide APIs (including LOGFONTW) instead of ANSI APIs. */
3231 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
3232 {
3233 int len;
3234 acp_to_enc((char_u *)lf.lfFaceName, (int)strlen(lf.lfFaceName),
3235 (char_u **)&font_name, &len);
3236 }
3237#endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003238 quality_name = quality_id2name((int)lf.lfQuality);
3239
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003240 res = (char *)alloc((unsigned)(strlen(font_name) + 20
3241 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
3242 if (res != NULL)
3243 {
3244 p = res;
3245 /* make a normal font string out of the lf thing:*/
3246 sprintf((char *)p, "%s:h%d", font_name, pixels_to_points(
3247 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
3248 while (*p)
3249 {
3250 if (*p == ' ')
3251 *p = '_';
3252 ++p;
3253 }
3254 if (lf.lfItalic)
3255 STRCAT(p, ":i");
3256 if (lf.lfWeight >= FW_BOLD)
3257 STRCAT(p, ":b");
3258 if (lf.lfUnderline)
3259 STRCAT(p, ":u");
3260 if (lf.lfStrikeOut)
3261 STRCAT(p, ":s");
3262 if (charset_name != NULL)
3263 {
3264 STRCAT(p, ":c");
3265 STRCAT(p, charset_name);
3266 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003267 if (quality_name != NULL)
3268 {
3269 STRCAT(p, ":q");
3270 STRCAT(p, quality_name);
3271 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003272 }
3273
3274#ifdef FEAT_MBYTE
3275 if (font_name != lf.lfFaceName)
3276 vim_free(font_name);
3277#endif
3278 return (char_u *)res;
3279}
3280
3281
3282#ifdef FEAT_MBYTE_IME
3283/*
3284 * Set correct LOGFONT to IME. Use 'guifontwide' if available, otherwise use
3285 * 'guifont'
3286 */
3287 static void
3288update_im_font(void)
3289{
3290 LOGFONT lf_wide;
3291
3292 if (p_guifontwide != NULL && *p_guifontwide != NUL
3293 && gui.wide_font != NOFONT
3294 && GetObject((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
3295 norm_logfont = lf_wide;
3296 else
3297 norm_logfont = sub_logfont;
3298 im_set_font(&norm_logfont);
3299}
3300#endif
3301
3302#ifdef FEAT_MBYTE
3303/*
3304 * Handler of gui.wide_font (p_guifontwide) changed notification.
3305 */
3306 void
3307gui_mch_wide_font_changed(void)
3308{
3309 LOGFONT lf;
3310
3311# ifdef FEAT_MBYTE_IME
3312 update_im_font();
3313# endif
3314
3315 gui_mch_free_font(gui.wide_ital_font);
3316 gui.wide_ital_font = NOFONT;
3317 gui_mch_free_font(gui.wide_bold_font);
3318 gui.wide_bold_font = NOFONT;
3319 gui_mch_free_font(gui.wide_boldital_font);
3320 gui.wide_boldital_font = NOFONT;
3321
3322 if (gui.wide_font
3323 && GetObject((HFONT)gui.wide_font, sizeof(lf), &lf))
3324 {
3325 if (!lf.lfItalic)
3326 {
3327 lf.lfItalic = TRUE;
3328 gui.wide_ital_font = get_font_handle(&lf);
3329 lf.lfItalic = FALSE;
3330 }
3331 if (lf.lfWeight < FW_BOLD)
3332 {
3333 lf.lfWeight = FW_BOLD;
3334 gui.wide_bold_font = get_font_handle(&lf);
3335 if (!lf.lfItalic)
3336 {
3337 lf.lfItalic = TRUE;
3338 gui.wide_boldital_font = get_font_handle(&lf);
3339 }
3340 }
3341 }
3342}
3343#endif
3344
3345/*
3346 * Initialise vim to use the font with the given name.
3347 * Return FAIL if the font could not be loaded, OK otherwise.
3348 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003349 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003350gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003351{
3352 LOGFONT lf;
3353 GuiFont font = NOFONT;
3354 char_u *p;
3355
3356 /* Load the font */
3357 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3358 font = get_font_handle(&lf);
3359 if (font == NOFONT)
3360 return FAIL;
3361
3362 if (font_name == NULL)
3363 font_name = (char_u *)lf.lfFaceName;
3364#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3365 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003366#endif
3367#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003368 sub_logfont = lf;
3369#endif
3370#ifdef FEAT_MBYTE_IME
3371 update_im_font();
3372#endif
3373 gui_mch_free_font(gui.norm_font);
3374 gui.norm_font = font;
3375 current_font_height = lf.lfHeight;
3376 GetFontSize(font);
3377
3378 p = logfont2name(lf);
3379 if (p != NULL)
3380 {
3381 hl_set_font_name(p);
3382
3383 /* When setting 'guifont' to "*" replace it with the actual font name.
3384 * */
3385 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3386 {
3387 vim_free(p_guifont);
3388 p_guifont = p;
3389 }
3390 else
3391 vim_free(p);
3392 }
3393
3394 gui_mch_free_font(gui.ital_font);
3395 gui.ital_font = NOFONT;
3396 gui_mch_free_font(gui.bold_font);
3397 gui.bold_font = NOFONT;
3398 gui_mch_free_font(gui.boldital_font);
3399 gui.boldital_font = NOFONT;
3400
3401 if (!lf.lfItalic)
3402 {
3403 lf.lfItalic = TRUE;
3404 gui.ital_font = get_font_handle(&lf);
3405 lf.lfItalic = FALSE;
3406 }
3407 if (lf.lfWeight < FW_BOLD)
3408 {
3409 lf.lfWeight = FW_BOLD;
3410 gui.bold_font = get_font_handle(&lf);
3411 if (!lf.lfItalic)
3412 {
3413 lf.lfItalic = TRUE;
3414 gui.boldital_font = get_font_handle(&lf);
3415 }
3416 }
3417
3418 return OK;
3419}
3420
3421#ifndef WPF_RESTORETOMAXIMIZED
3422# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3423#endif
3424
3425/*
3426 * Return TRUE if the GUI window is maximized, filling the whole screen.
3427 */
3428 int
3429gui_mch_maximized(void)
3430{
3431 WINDOWPLACEMENT wp;
3432
3433 wp.length = sizeof(WINDOWPLACEMENT);
3434 if (GetWindowPlacement(s_hwnd, &wp))
3435 return wp.showCmd == SW_SHOWMAXIMIZED
3436 || (wp.showCmd == SW_SHOWMINIMIZED
3437 && wp.flags == WPF_RESTORETOMAXIMIZED);
3438
3439 return 0;
3440}
3441
3442/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003443 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3444 * is set. Compute the new Rows and Columns. This is like resizing the
3445 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003446 */
3447 void
3448gui_mch_newfont(void)
3449{
3450 RECT rect;
3451
3452 GetWindowRect(s_hwnd, &rect);
3453 if (win_socket_id == 0)
3454 {
3455 gui_resize_shell(rect.right - rect.left
3456 - (GetSystemMetrics(SM_CXFRAME) +
3457 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3458 rect.bottom - rect.top
3459 - (GetSystemMetrics(SM_CYFRAME) +
3460 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3461 - GetSystemMetrics(SM_CYCAPTION)
3462#ifdef FEAT_MENU
3463 - gui_mswin_get_menu_height(FALSE)
3464#endif
3465 );
3466 }
3467 else
3468 {
3469 /* Inside another window, don't use the frame and border. */
3470 gui_resize_shell(rect.right - rect.left,
3471 rect.bottom - rect.top
3472#ifdef FEAT_MENU
3473 - gui_mswin_get_menu_height(FALSE)
3474#endif
3475 );
3476 }
3477}
3478
3479/*
3480 * Set the window title
3481 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003482 void
3483gui_mch_settitle(
3484 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003485 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003486{
3487 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3488}
3489
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003490#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003491/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3492 * misc2.c! */
3493static LPCSTR mshape_idcs[] =
3494{
3495 IDC_ARROW, /* arrow */
3496 MAKEINTRESOURCE(0), /* blank */
3497 IDC_IBEAM, /* beam */
3498 IDC_SIZENS, /* updown */
3499 IDC_SIZENS, /* udsizing */
3500 IDC_SIZEWE, /* leftright */
3501 IDC_SIZEWE, /* lrsizing */
3502 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003503 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003504 IDC_ARROW, /* crosshair */
3505 IDC_ARROW, /* hand1 */
3506 IDC_ARROW, /* hand2 */
3507 IDC_ARROW, /* pencil */
3508 IDC_ARROW, /* question */
3509 IDC_ARROW, /* right-arrow */
3510 IDC_UPARROW, /* up-arrow */
3511 IDC_ARROW /* last one */
3512};
3513
3514 void
3515mch_set_mouse_shape(int shape)
3516{
3517 LPCSTR idc;
3518
3519 if (shape == MSHAPE_HIDE)
3520 ShowCursor(FALSE);
3521 else
3522 {
3523 if (shape >= MSHAPE_NUMBERED)
3524 idc = IDC_ARROW;
3525 else
3526 idc = mshape_idcs[shape];
3527#ifdef SetClassLongPtr
3528 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3529#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003530 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003531#endif
3532 if (!p_mh)
3533 {
3534 POINT mp;
3535
3536 /* Set the position to make it redrawn with the new shape. */
3537 (void)GetCursorPos((LPPOINT)&mp);
3538 (void)SetCursorPos(mp.x, mp.y);
3539 ShowCursor(TRUE);
3540 }
3541 }
3542}
3543#endif
3544
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003545#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003546/*
3547 * The file browser exists in two versions: with "W" uses wide characters,
3548 * without "W" the current codepage. When FEAT_MBYTE is defined and on
3549 * Windows NT/2000/XP the "W" functions are used.
3550 */
3551
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003552# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003553/*
3554 * Wide version of convert_filter().
3555 */
3556 static WCHAR *
3557convert_filterW(char_u *s)
3558{
3559 char_u *tmp;
3560 int len;
3561 WCHAR *res;
3562
3563 tmp = convert_filter(s);
3564 if (tmp == NULL)
3565 return NULL;
3566 len = (int)STRLEN(s) + 3;
3567 res = enc_to_utf16(tmp, &len);
3568 vim_free(tmp);
3569 return res;
3570}
3571
3572/*
3573 * Wide version of gui_mch_browse(). Keep in sync!
3574 */
3575 static char_u *
3576gui_mch_browseW(
3577 int saving,
3578 char_u *title,
3579 char_u *dflt,
3580 char_u *ext,
3581 char_u *initdir,
3582 char_u *filter)
3583{
3584 /* We always use the wide function. This means enc_to_utf16() must work,
3585 * otherwise it fails miserably! */
3586 OPENFILENAMEW fileStruct;
3587 WCHAR fileBuf[MAXPATHL];
3588 WCHAR *wp;
3589 int i;
3590 WCHAR *titlep = NULL;
3591 WCHAR *extp = NULL;
3592 WCHAR *initdirp = NULL;
3593 WCHAR *filterp;
3594 char_u *p;
3595
3596 if (dflt == NULL)
3597 fileBuf[0] = NUL;
3598 else
3599 {
3600 wp = enc_to_utf16(dflt, NULL);
3601 if (wp == NULL)
3602 fileBuf[0] = NUL;
3603 else
3604 {
3605 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3606 fileBuf[i] = wp[i];
3607 fileBuf[i] = NUL;
3608 vim_free(wp);
3609 }
3610 }
3611
3612 /* Convert the filter to Windows format. */
3613 filterp = convert_filterW(filter);
3614
3615 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003616# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003617 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003618 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003619# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003620 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003621# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003622
3623 if (title != NULL)
3624 titlep = enc_to_utf16(title, NULL);
3625 fileStruct.lpstrTitle = titlep;
3626
3627 if (ext != NULL)
3628 extp = enc_to_utf16(ext, NULL);
3629 fileStruct.lpstrDefExt = extp;
3630
3631 fileStruct.lpstrFile = fileBuf;
3632 fileStruct.nMaxFile = MAXPATHL;
3633 fileStruct.lpstrFilter = filterp;
3634 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3635 /* has an initial dir been specified? */
3636 if (initdir != NULL && *initdir != NUL)
3637 {
3638 /* Must have backslashes here, no matter what 'shellslash' says */
3639 initdirp = enc_to_utf16(initdir, NULL);
3640 if (initdirp != NULL)
3641 {
3642 for (wp = initdirp; *wp != NUL; ++wp)
3643 if (*wp == '/')
3644 *wp = '\\';
3645 }
3646 fileStruct.lpstrInitialDir = initdirp;
3647 }
3648
3649 /*
3650 * TODO: Allow selection of multiple files. Needs another arg to this
3651 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3652 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3653 * files that don't exist yet, so I haven't put it in. What about
3654 * OFN_PATHMUSTEXIST?
3655 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3656 */
3657 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003658# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003659 if (curbuf->b_p_bin)
3660 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003661# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003662 if (saving)
3663 {
3664 if (!GetSaveFileNameW(&fileStruct))
3665 return NULL;
3666 }
3667 else
3668 {
3669 if (!GetOpenFileNameW(&fileStruct))
3670 return NULL;
3671 }
3672
3673 vim_free(filterp);
3674 vim_free(initdirp);
3675 vim_free(titlep);
3676 vim_free(extp);
3677
3678 /* Convert from UCS2 to 'encoding'. */
3679 p = utf16_to_enc(fileBuf, NULL);
3680 if (p != NULL)
3681 /* when out of memory we get garbage for non-ASCII chars */
3682 STRCPY(fileBuf, p);
3683 vim_free(p);
3684
3685 /* Give focus back to main window (when using MDI). */
3686 SetFocus(s_hwnd);
3687
3688 /* Shorten the file name if possible */
3689 return vim_strsave(shorten_fname1((char_u *)fileBuf));
3690}
3691# endif /* FEAT_MBYTE */
3692
3693
3694/*
3695 * Convert the string s to the proper format for a filter string by replacing
3696 * the \t and \n delimiters with \0.
3697 * Returns the converted string in allocated memory.
3698 *
3699 * Keep in sync with convert_filterW() above!
3700 */
3701 static char_u *
3702convert_filter(char_u *s)
3703{
3704 char_u *res;
3705 unsigned s_len = (unsigned)STRLEN(s);
3706 unsigned i;
3707
3708 res = alloc(s_len + 3);
3709 if (res != NULL)
3710 {
3711 for (i = 0; i < s_len; ++i)
3712 if (s[i] == '\t' || s[i] == '\n')
3713 res[i] = '\0';
3714 else
3715 res[i] = s[i];
3716 res[s_len] = NUL;
3717 /* Add two extra NULs to make sure it's properly terminated. */
3718 res[s_len + 1] = NUL;
3719 res[s_len + 2] = NUL;
3720 }
3721 return res;
3722}
3723
3724/*
3725 * Select a directory.
3726 */
3727 char_u *
3728gui_mch_browsedir(char_u *title, char_u *initdir)
3729{
3730 /* We fake this: Use a filter that doesn't select anything and a default
3731 * file name that won't be used. */
3732 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3733 initdir, (char_u *)_("Directory\t*.nothing\n"));
3734}
3735
3736/*
3737 * Pop open a file browser and return the file selected, in allocated memory,
3738 * or NULL if Cancel is hit.
3739 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3740 * title - Title message for the file browser dialog.
3741 * dflt - Default name of file.
3742 * ext - Default extension to be added to files without extensions.
3743 * initdir - directory in which to open the browser (NULL = current dir)
3744 * filter - Filter for matched files to choose from.
3745 *
3746 * Keep in sync with gui_mch_browseW() above!
3747 */
3748 char_u *
3749gui_mch_browse(
3750 int saving,
3751 char_u *title,
3752 char_u *dflt,
3753 char_u *ext,
3754 char_u *initdir,
3755 char_u *filter)
3756{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003757# ifdef FEAT_MBYTE
3758 return gui_mch_browseW(saving, title, dflt, ext, initdir, filter);
3759# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003760 OPENFILENAME fileStruct;
3761 char_u fileBuf[MAXPATHL];
3762 char_u *initdirp = NULL;
3763 char_u *filterp;
3764 char_u *p;
3765
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003766 if (dflt == NULL)
3767 fileBuf[0] = NUL;
3768 else
3769 vim_strncpy(fileBuf, dflt, MAXPATHL - 1);
3770
3771 /* Convert the filter to Windows format. */
3772 filterp = convert_filter(filter);
3773
3774 vim_memset(&fileStruct, 0, sizeof(OPENFILENAME));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003775# ifdef OPENFILENAME_SIZE_VERSION_400
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003776 /* be compatible with Windows NT 4.0 */
3777 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003778# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003779 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003780# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003781
3782 fileStruct.lpstrTitle = (LPSTR)title;
3783 fileStruct.lpstrDefExt = (LPSTR)ext;
3784
3785 fileStruct.lpstrFile = (LPSTR)fileBuf;
3786 fileStruct.nMaxFile = MAXPATHL;
3787 fileStruct.lpstrFilter = (LPSTR)filterp;
3788 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3789 /* has an initial dir been specified? */
3790 if (initdir != NULL && *initdir != NUL)
3791 {
3792 /* Must have backslashes here, no matter what 'shellslash' says */
3793 initdirp = vim_strsave(initdir);
3794 if (initdirp != NULL)
3795 for (p = initdirp; *p != NUL; ++p)
3796 if (*p == '/')
3797 *p = '\\';
3798 fileStruct.lpstrInitialDir = (LPSTR)initdirp;
3799 }
3800
3801 /*
3802 * TODO: Allow selection of multiple files. Needs another arg to this
3803 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3804 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3805 * files that don't exist yet, so I haven't put it in. What about
3806 * OFN_PATHMUSTEXIST?
3807 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3808 */
3809 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003810# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003811 if (curbuf->b_p_bin)
3812 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003813# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003814 if (saving)
3815 {
3816 if (!GetSaveFileName(&fileStruct))
3817 return NULL;
3818 }
3819 else
3820 {
3821 if (!GetOpenFileName(&fileStruct))
3822 return NULL;
3823 }
3824
3825 vim_free(filterp);
3826 vim_free(initdirp);
3827
3828 /* Give focus back to main window (when using MDI). */
3829 SetFocus(s_hwnd);
3830
3831 /* Shorten the file name if possible */
3832 return vim_strsave(shorten_fname1((char_u *)fileBuf));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003833# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003834}
3835#endif /* FEAT_BROWSE */
3836
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003837 static void
3838_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003839 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003840 HDROP hDrop)
3841{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003842#define BUFPATHLEN _MAX_PATH
3843#define DRAGQVAL 0xFFFFFFFF
3844#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003845 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaar4033c552017-09-16 20:54:51 +02003846#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003847 char szFile[BUFPATHLEN];
3848 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3849 UINT i;
3850 char_u **fnames;
3851 POINT pt;
3852 int_u modifiers = 0;
3853
3854 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3855
3856 /* Obtain dropped position */
3857 DragQueryPoint(hDrop, &pt);
3858 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3859
3860 reset_VIsual();
3861
3862 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3863
3864 if (fnames != NULL)
3865 for (i = 0; i < cFiles; ++i)
3866 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003867#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003868 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3869 fnames[i] = utf16_to_enc(wszFile, NULL);
3870 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02003871#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003872 {
3873 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3874 fnames[i] = vim_strsave((char_u *)szFile);
3875 }
3876 }
3877
3878 DragFinish(hDrop);
3879
3880 if (fnames != NULL)
3881 {
3882 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3883 modifiers |= MOUSE_SHIFT;
3884 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3885 modifiers |= MOUSE_CTRL;
3886 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3887 modifiers |= MOUSE_ALT;
3888
3889 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3890
3891 s_need_activate = TRUE;
3892 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003893}
3894
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003895 static int
3896_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003897 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003898 HWND hwndCtl,
3899 UINT code,
3900 int pos)
3901{
3902 static UINT prev_code = 0; /* code of previous call */
3903 scrollbar_T *sb, *sb_info;
3904 long val;
3905 int dragging = FALSE;
3906 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003907 SCROLLINFO si;
3908
3909 si.cbSize = sizeof(si);
3910 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003911
3912 sb = gui_mswin_find_scrollbar(hwndCtl);
3913 if (sb == NULL)
3914 return 0;
3915
3916 if (sb->wp != NULL) /* Left or right scrollbar */
3917 {
3918 /*
3919 * Careful: need to get scrollbar info out of first (left) scrollbar
3920 * for window, but keep real scrollbar too because we must pass it to
3921 * gui_drag_scrollbar().
3922 */
3923 sb_info = &sb->wp->w_scrollbars[0];
3924 }
3925 else /* Bottom scrollbar */
3926 sb_info = sb;
3927 val = sb_info->value;
3928
3929 switch (code)
3930 {
3931 case SB_THUMBTRACK:
3932 val = pos;
3933 dragging = TRUE;
3934 if (sb->scroll_shift > 0)
3935 val <<= sb->scroll_shift;
3936 break;
3937 case SB_LINEDOWN:
3938 val++;
3939 break;
3940 case SB_LINEUP:
3941 val--;
3942 break;
3943 case SB_PAGEDOWN:
3944 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3945 break;
3946 case SB_PAGEUP:
3947 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3948 break;
3949 case SB_TOP:
3950 val = 0;
3951 break;
3952 case SB_BOTTOM:
3953 val = sb_info->max;
3954 break;
3955 case SB_ENDSCROLL:
3956 if (prev_code == SB_THUMBTRACK)
3957 {
3958 /*
3959 * "pos" only gives us 16-bit data. In case of large file,
3960 * use GetScrollPos() which returns 32-bit. Unfortunately it
3961 * is not valid while the scrollbar is being dragged.
3962 */
3963 val = GetScrollPos(hwndCtl, SB_CTL);
3964 if (sb->scroll_shift > 0)
3965 val <<= sb->scroll_shift;
3966 }
3967 break;
3968
3969 default:
3970 /* TRACE("Unknown scrollbar event %d\n", code); */
3971 return 0;
3972 }
3973 prev_code = code;
3974
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003975 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3976 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003977
3978 /*
3979 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3980 */
3981 if (sb->wp != NULL)
3982 {
3983 scrollbar_T *sba = sb->wp->w_scrollbars;
3984 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3985
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003986 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003987 }
3988
3989 /* Don't let us be interrupted here by another message. */
3990 s_busy_processing = TRUE;
3991
3992 /* When "allow_scrollbar" is FALSE still need to remember the new
3993 * position, but don't actually scroll by setting "dont_scroll". */
3994 dont_scroll = !allow_scrollbar;
3995
3996 gui_drag_scrollbar(sb, val, dragging);
3997
3998 s_busy_processing = FALSE;
3999 dont_scroll = dont_scroll_save;
4000
4001 return 0;
4002}
4003
4004
4005/*
4006 * Get command line arguments.
4007 * Use "prog" as the name of the program and "cmdline" as the arguments.
4008 * Copy the arguments to allocated memory.
4009 * Return the number of arguments (including program name).
4010 * Return pointers to the arguments in "argvp". Memory is allocated with
4011 * malloc(), use free() instead of vim_free().
4012 * Return pointer to buffer in "tofree".
4013 * Returns zero when out of memory.
4014 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004015 int
4016get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
4017{
4018 int i;
4019 char *p;
4020 char *progp;
4021 char *pnew = NULL;
4022 char *newcmdline;
4023 int inquote;
4024 int argc;
4025 char **argv = NULL;
4026 int round;
4027
4028 *tofree = NULL;
4029
4030#ifdef FEAT_MBYTE
4031 /* Try using the Unicode version first, it takes care of conversion when
4032 * 'encoding' is changed. */
4033 argc = get_cmd_argsW(&argv);
4034 if (argc != 0)
4035 goto done;
4036#endif
4037
4038 /* Handle the program name. Remove the ".exe" extension, and find the 1st
4039 * non-space. */
4040 p = strrchr(prog, '.');
4041 if (p != NULL)
4042 *p = NUL;
4043 for (progp = prog; *progp == ' '; ++progp)
4044 ;
4045
4046 /* The command line is copied to allocated memory, so that we can change
4047 * it. Add the size of the string, the separating NUL and a terminating
4048 * NUL. */
4049 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
4050 if (newcmdline == NULL)
4051 return 0;
4052
4053 /*
4054 * First round: count the number of arguments ("pnew" == NULL).
4055 * Second round: produce the arguments.
4056 */
4057 for (round = 1; round <= 2; ++round)
4058 {
4059 /* First argument is the program name. */
4060 if (pnew != NULL)
4061 {
4062 argv[0] = pnew;
4063 strcpy(pnew, progp);
4064 pnew += strlen(pnew);
4065 *pnew++ = NUL;
4066 }
4067
4068 /*
4069 * Isolate each argument and put it in argv[].
4070 */
4071 p = cmdline;
4072 argc = 1;
4073 while (*p != NUL)
4074 {
4075 inquote = FALSE;
4076 if (pnew != NULL)
4077 argv[argc] = pnew;
4078 ++argc;
4079 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
4080 {
4081 /* Backslashes are only special when followed by a double
4082 * quote. */
4083 i = (int)strspn(p, "\\");
4084 if (p[i] == '"')
4085 {
4086 /* Halve the number of backslashes. */
4087 if (i > 1 && pnew != NULL)
4088 {
4089 vim_memset(pnew, '\\', i / 2);
4090 pnew += i / 2;
4091 }
4092
4093 /* Even nr of backslashes toggles quoting, uneven copies
4094 * the double quote. */
4095 if ((i & 1) == 0)
4096 inquote = !inquote;
4097 else if (pnew != NULL)
4098 *pnew++ = '"';
4099 p += i + 1;
4100 }
4101 else if (i > 0)
4102 {
4103 /* Copy span of backslashes unmodified. */
4104 if (pnew != NULL)
4105 {
4106 vim_memset(pnew, '\\', i);
4107 pnew += i;
4108 }
4109 p += i;
4110 }
4111 else
4112 {
4113 if (pnew != NULL)
4114 *pnew++ = *p;
4115#ifdef FEAT_MBYTE
4116 /* Can't use mb_* functions, because 'encoding' is not
4117 * initialized yet here. */
4118 if (IsDBCSLeadByte(*p))
4119 {
4120 ++p;
4121 if (pnew != NULL)
4122 *pnew++ = *p;
4123 }
4124#endif
4125 ++p;
4126 }
4127 }
4128
4129 if (pnew != NULL)
4130 *pnew++ = NUL;
4131 while (*p == ' ' || *p == '\t')
4132 ++p; /* advance until a non-space */
4133 }
4134
4135 if (round == 1)
4136 {
4137 argv = (char **)malloc((argc + 1) * sizeof(char *));
4138 if (argv == NULL )
4139 {
4140 free(newcmdline);
4141 return 0; /* malloc error */
4142 }
4143 pnew = newcmdline;
4144 *tofree = newcmdline;
4145 }
4146 }
4147
4148#ifdef FEAT_MBYTE
4149done:
4150#endif
4151 argv[argc] = NULL; /* NULL-terminated list */
4152 *argvp = argv;
4153 return argc;
4154}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
4156#ifdef FEAT_XPM_W32
4157# include "xpm_w32.h"
4158#endif
4159
4160#ifdef PROTO
4161# define WINAPI
4162#endif
4163
4164#ifdef __MINGW32__
4165/*
4166 * Add a lot of missing defines.
4167 * They are not always missing, we need the #ifndef's.
4168 */
4169# ifndef _cdecl
4170# define _cdecl
4171# endif
4172# ifndef IsMinimized
4173# define IsMinimized(hwnd) IsIconic(hwnd)
4174# endif
4175# ifndef IsMaximized
4176# define IsMaximized(hwnd) IsZoomed(hwnd)
4177# endif
4178# ifndef SelectFont
4179# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
4180# endif
4181# ifndef GetStockBrush
4182# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
4183# endif
4184# ifndef DeleteBrush
4185# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
4186# endif
4187
4188# ifndef HANDLE_WM_RBUTTONDBLCLK
4189# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4190 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4191# endif
4192# ifndef HANDLE_WM_MBUTTONUP
4193# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
4194 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4195# endif
4196# ifndef HANDLE_WM_MBUTTONDBLCLK
4197# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4198 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4199# endif
4200# ifndef HANDLE_WM_LBUTTONDBLCLK
4201# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4202 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4203# endif
4204# ifndef HANDLE_WM_RBUTTONDOWN
4205# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
4206 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4207# endif
4208# ifndef HANDLE_WM_MOUSEMOVE
4209# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
4210 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4211# endif
4212# ifndef HANDLE_WM_RBUTTONUP
4213# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
4214 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4215# endif
4216# ifndef HANDLE_WM_MBUTTONDOWN
4217# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
4218 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4219# endif
4220# ifndef HANDLE_WM_LBUTTONUP
4221# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
4222 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4223# endif
4224# ifndef HANDLE_WM_LBUTTONDOWN
4225# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
4226 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4227# endif
4228# ifndef HANDLE_WM_SYSCHAR
4229# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
4230 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4231# endif
4232# ifndef HANDLE_WM_ACTIVATEAPP
4233# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
4234 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
4235# endif
4236# ifndef HANDLE_WM_WINDOWPOSCHANGING
4237# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
4238 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
4239# endif
4240# ifndef HANDLE_WM_VSCROLL
4241# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
4242 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4243# endif
4244# ifndef HANDLE_WM_SETFOCUS
4245# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
4246 ((fn)((hwnd), (HWND)(wParam)), 0L)
4247# endif
4248# ifndef HANDLE_WM_KILLFOCUS
4249# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
4250 ((fn)((hwnd), (HWND)(wParam)), 0L)
4251# endif
4252# ifndef HANDLE_WM_HSCROLL
4253# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
4254 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4255# endif
4256# ifndef HANDLE_WM_DROPFILES
4257# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
4258 ((fn)((hwnd), (HDROP)(wParam)), 0L)
4259# endif
4260# ifndef HANDLE_WM_CHAR
4261# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
4262 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4263# endif
4264# ifndef HANDLE_WM_SYSDEADCHAR
4265# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
4266 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4267# endif
4268# ifndef HANDLE_WM_DEADCHAR
4269# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4270 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4271# endif
4272#endif /* __MINGW32__ */
4273
4274
4275/* Some parameters for tearoff menus. All in pixels. */
4276#define TEAROFF_PADDING_X 2
4277#define TEAROFF_BUTTON_PAD_X 8
4278#define TEAROFF_MIN_WIDTH 200
4279#define TEAROFF_SUBMENU_LABEL ">>"
4280#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4281
4282
4283/* For the Intellimouse: */
4284#ifndef WM_MOUSEWHEEL
4285#define WM_MOUSEWHEEL 0x20a
4286#endif
4287
4288
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004289#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290# define ID_BEVAL_TOOLTIP 200
4291# define BEVAL_TEXT_LEN MAXPATHL
4292
Bram Moolenaar167632f2010-05-26 21:42:54 +02004293#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004294/* Work around old versions of basetsd.h which wrongly declares
4295 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02004296# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00004297# define UINT_PTR UINT
4298#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004299
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004300static void make_tooltip(BalloonEval *beval, char *text, POINT pt);
4301static void delete_tooltip(BalloonEval *beval);
4302static VOID CALLBACK BevalTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004303
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004305static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004307
Bram Moolenaar82881492012-11-20 16:53:39 +01004308
4309/* cproto fails on missing include files */
4310#ifndef PROTO
4311
Bram Moolenaar45360022005-07-21 21:08:21 +00004312/*
4313 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004314 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004315 */
Bram Moolenaar82881492012-11-20 16:53:39 +01004316# include <pshpack1.h>
4317
4318#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004319
4320typedef struct _DllVersionInfo
4321{
4322 DWORD cbSize;
4323 DWORD dwMajorVersion;
4324 DWORD dwMinorVersion;
4325 DWORD dwBuildNumber;
4326 DWORD dwPlatformID;
4327} DLLVERSIONINFO;
4328
Bram Moolenaar82881492012-11-20 16:53:39 +01004329#ifndef PROTO
4330# include <poppack.h>
4331#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004332
Bram Moolenaar45360022005-07-21 21:08:21 +00004333typedef struct tagTOOLINFOA_NEW
4334{
4335 UINT cbSize;
4336 UINT uFlags;
4337 HWND hwnd;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004338 UINT_PTR uId;
Bram Moolenaar45360022005-07-21 21:08:21 +00004339 RECT rect;
4340 HINSTANCE hinst;
4341 LPSTR lpszText;
4342 LPARAM lParam;
4343} TOOLINFO_NEW;
4344
4345typedef struct tagNMTTDISPINFO_NEW
4346{
4347 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004348 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004349 char szText[80];
4350 HINSTANCE hinst;
4351 UINT uFlags;
4352 LPARAM lParam;
4353} NMTTDISPINFO_NEW;
4354
Bram Moolenaar45360022005-07-21 21:08:21 +00004355typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4356#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004357# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358#endif
4359
Bram Moolenaar45360022005-07-21 21:08:21 +00004360#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004361# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004362#endif
4363
4364#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004365# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004366#endif
4367
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004368#endif /* defined(FEAT_BEVAL_GUI) */
Bram Moolenaar45360022005-07-21 21:08:21 +00004369
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004370#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4371/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4372 * it here if LPNMTTDISPINFO isn't defined.
4373 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4374 * _MSC_VER. */
4375# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4376typedef struct tagNMTTDISPINFOA {
4377 NMHDR hdr;
4378 LPSTR lpszText;
4379 char szText[80];
4380 HINSTANCE hinst;
4381 UINT uFlags;
4382 LPARAM lParam;
4383} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4384# define LPNMTTDISPINFO LPNMTTDISPINFOA
4385
4386# ifdef FEAT_MBYTE
4387typedef struct tagNMTTDISPINFOW {
4388 NMHDR hdr;
4389 LPWSTR lpszText;
4390 WCHAR szText[80];
4391 HINSTANCE hinst;
4392 UINT uFlags;
4393 LPARAM lParam;
4394} NMTTDISPINFOW, *LPNMTTDISPINFOW;
4395# endif
4396# endif
4397#endif
4398
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004399#ifndef TTN_GETDISPINFOW
4400# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4401#endif
4402
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403/* Local variables: */
4404
4405#ifdef FEAT_MENU
4406static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408
4409/*
4410 * Use the system font for dialogs and tear-off menus. Remove this line to
4411 * use DLG_FONT_NAME.
4412 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004413#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414
4415#define VIM_NAME "vim"
4416#define VIM_CLASS "Vim"
4417#define VIM_CLASSW L"Vim"
4418
4419/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4420 * thus there should be room for every dialog. For tearoffs it's made bigger
4421 * when needed. */
4422#define DLG_ALLOC_SIZE 16 * 1024
4423
4424/*
4425 * stuff for dialogs, menus, tearoffs etc.
4426 */
4427static LRESULT APIENTRY dialog_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004428#ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429static LRESULT APIENTRY tearoff_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431static PWORD
4432add_dialog_element(
4433 PWORD p,
4434 DWORD lStyle,
4435 WORD x,
4436 WORD y,
4437 WORD w,
4438 WORD h,
4439 WORD Id,
4440 WORD clss,
4441 const char *caption);
4442static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004443static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004444#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447static void get_dialog_font_metrics(void);
4448
4449static int dialog_default_button = -1;
4450
4451/* Intellimouse support */
4452static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
4454static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4455#ifdef FEAT_TOOLBAR
4456static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004457static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458static int get_toolbar_bitmap(vimmenu_T *menu);
4459#endif
4460
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004461#ifdef FEAT_GUI_TABLINE
4462static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004463static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004464#endif
4465
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466#ifdef FEAT_MBYTE_IME
4467static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4468static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4469#endif
4470#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4471# ifdef NOIME
4472typedef struct tagCOMPOSITIONFORM {
4473 DWORD dwStyle;
4474 POINT ptCurrentPos;
4475 RECT rcArea;
4476} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4477typedef HANDLE HIMC;
4478# endif
4479
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004480static HINSTANCE hLibImm = NULL;
4481static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4482static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4483static HIMC (WINAPI *pImmGetContext)(HWND);
4484static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4485static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4486static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4487static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
4488static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
4489static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
4490static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4491static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004492static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493static void dyn_imm_load(void);
4494#else
4495# define pImmGetCompositionStringA ImmGetCompositionStringA
4496# define pImmGetCompositionStringW ImmGetCompositionStringW
4497# define pImmGetContext ImmGetContext
4498# define pImmAssociateContext ImmAssociateContext
4499# define pImmReleaseContext ImmReleaseContext
4500# define pImmGetOpenStatus ImmGetOpenStatus
4501# define pImmSetOpenStatus ImmSetOpenStatus
4502# define pImmGetCompositionFont ImmGetCompositionFontA
4503# define pImmSetCompositionFont ImmSetCompositionFontA
4504# define pImmSetCompositionWindow ImmSetCompositionWindow
4505# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004506# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507#endif
4508
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509#ifdef FEAT_MENU
4510/*
4511 * Figure out how high the menu bar is at the moment.
4512 */
4513 static int
4514gui_mswin_get_menu_height(
4515 int fix_window) /* If TRUE, resize window if menu height changed */
4516{
4517 static int old_menu_height = -1;
4518
4519 RECT rc1, rc2;
4520 int num;
4521 int menu_height;
4522
4523 if (gui.menu_is_active)
4524 num = GetMenuItemCount(s_menuBar);
4525 else
4526 num = 0;
4527
4528 if (num == 0)
4529 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004530 else if (IsMinimized(s_hwnd))
4531 {
4532 /* The height of the menu cannot be determined while the window is
4533 * minimized. Take the previous height if the menu is changed in that
4534 * state, to avoid that Vim's vertical window size accidentally
4535 * increases due to the unaccounted-for menu height. */
4536 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 else
4539 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004540 /*
4541 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4542 * seem to have been set yet, so menu wraps in default window
4543 * width which is very narrow. Instead just return height of a
4544 * single menu item. Will still be wrong when the menu really
4545 * should wrap over more than one line.
4546 */
4547 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4548 if (gui.starting)
4549 menu_height = rc1.bottom - rc1.top + 1;
4550 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004552 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4553 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
4555 }
4556
4557 if (fix_window && menu_height != old_menu_height)
4558 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004559 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 }
Bram Moolenaar71371b12015-03-24 17:57:12 +01004561 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
4563 return menu_height;
4564}
4565#endif /*FEAT_MENU*/
4566
4567
4568/*
4569 * Setup for the Intellimouse
4570 */
4571 static void
4572init_mouse_wheel(void)
4573{
4574
4575#ifndef SPI_GETWHEELSCROLLLINES
4576# define SPI_GETWHEELSCROLLLINES 104
4577#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004578#ifndef SPI_SETWHEELSCROLLLINES
4579# define SPI_SETWHEELSCROLLLINES 105
4580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581
4582#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4583#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4584#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4585#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4586
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 mouse_scroll_lines = 3; /* reasonable default */
4588
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004589 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4590 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4591 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592}
4593
4594
4595/* Intellimouse wheel handler */
4596 static void
4597_OnMouseWheel(
4598 HWND hwnd,
4599 short zDelta)
4600{
4601/* Treat a mouse wheel event as if it were a scroll request */
4602 int i;
4603 int size;
4604 HWND hwndCtl;
4605
4606 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4607 {
4608 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4609 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4610 }
4611 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4612 {
4613 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4614 size = curwin->w_scrollbars[SBAR_LEFT].size;
4615 }
4616 else
4617 return;
4618
4619 size = curwin->w_height;
4620 if (mouse_scroll_lines == 0)
4621 init_mouse_wheel();
4622
4623 if (mouse_scroll_lines > 0
4624 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4625 {
4626 for (i = mouse_scroll_lines; i > 0; --i)
4627 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4628 }
4629 else
4630 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
4631}
4632
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004633#ifdef USE_SYSMENU_FONT
4634/*
4635 * Get Menu Font.
4636 * Return OK or FAIL.
4637 */
4638 static int
4639gui_w32_get_menu_font(LOGFONT *lf)
4640{
4641 NONCLIENTMETRICS nm;
4642
4643 nm.cbSize = sizeof(NONCLIENTMETRICS);
4644 if (!SystemParametersInfo(
4645 SPI_GETNONCLIENTMETRICS,
4646 sizeof(NONCLIENTMETRICS),
4647 &nm,
4648 0))
4649 return FAIL;
4650 *lf = nm.lfMenuFont;
4651 return OK;
4652}
4653#endif
4654
4655
4656#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4657/*
4658 * Set the GUI tabline font to the system menu font
4659 */
4660 static void
4661set_tabline_font(void)
4662{
4663 LOGFONT lfSysmenu;
4664 HFONT font;
4665 HWND hwnd;
4666 HDC hdc;
4667 HFONT hfntOld;
4668 TEXTMETRIC tm;
4669
4670 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4671 return;
4672
4673 font = CreateFontIndirect(&lfSysmenu);
4674
4675 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4676
4677 /*
4678 * Compute the height of the font used for the tab text
4679 */
4680 hwnd = GetDesktopWindow();
4681 hdc = GetWindowDC(hwnd);
4682 hfntOld = SelectFont(hdc, font);
4683
4684 GetTextMetrics(hdc, &tm);
4685
4686 SelectFont(hdc, hfntOld);
4687 ReleaseDC(hwnd, hdc);
4688
4689 /*
4690 * The space used by the tab border and the space between the tab label
4691 * and the tab border is included as 7.
4692 */
4693 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4694}
4695#endif
4696
Bram Moolenaar520470a2005-06-16 21:59:56 +00004697/*
4698 * Invoked when a setting was changed.
4699 */
4700 static LRESULT CALLBACK
4701_OnSettingChange(UINT n)
4702{
4703 if (n == SPI_SETWHEELSCROLLLINES)
4704 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4705 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004706#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4707 if (n == SPI_SETNONCLIENTMETRICS)
4708 set_tabline_font();
4709#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004710 return 0;
4711}
4712
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713#ifdef FEAT_NETBEANS_INTG
4714 static void
4715_OnWindowPosChanged(
4716 HWND hwnd,
4717 const LPWINDOWPOS lpwpos)
4718{
4719 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004720 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721
4722 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4723 || lpwpos->cx != cx || lpwpos->cy != cy))
4724 {
4725 x = lpwpos->x;
4726 y = lpwpos->y;
4727 cx = lpwpos->cx;
4728 cy = lpwpos->cy;
4729 netbeans_frame_moved(x, y);
4730 }
4731 /* Allow to send WM_SIZE and WM_MOVE */
4732 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4733}
4734#endif
4735
4736 static int
4737_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 UINT fwSide,
4739 LPRECT lprc)
4740{
4741 int w, h;
4742 int valid_w, valid_h;
4743 int w_offset, h_offset;
4744
4745 w = lprc->right - lprc->left;
4746 h = lprc->bottom - lprc->top;
4747 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4748 w_offset = w - valid_w;
4749 h_offset = h - valid_h;
4750
4751 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4752 || fwSide == WMSZ_BOTTOMLEFT)
4753 lprc->left += w_offset;
4754 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4755 || fwSide == WMSZ_BOTTOMRIGHT)
4756 lprc->right -= w_offset;
4757
4758 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4759 || fwSide == WMSZ_TOPRIGHT)
4760 lprc->top += h_offset;
4761 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4762 || fwSide == WMSZ_BOTTOMRIGHT)
4763 lprc->bottom -= h_offset;
4764 return TRUE;
4765}
4766
4767
4768
4769 static LRESULT CALLBACK
4770_WndProc(
4771 HWND hwnd,
4772 UINT uMsg,
4773 WPARAM wParam,
4774 LPARAM lParam)
4775{
4776 /*
4777 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4778 hwnd, uMsg, wParam, lParam);
4779 */
4780
4781 HandleMouseHide(uMsg, lParam);
4782
4783 s_uMsg = uMsg;
4784 s_wParam = wParam;
4785 s_lParam = lParam;
4786
4787 switch (uMsg)
4788 {
4789 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4790 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4791 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4792 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4793 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4794 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4795 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4796 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4797 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4798#ifdef FEAT_MENU
4799 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4800#endif
4801 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4802 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4803 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4804 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4805 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4806 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4807 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4808 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4809 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4810#ifdef FEAT_NETBEANS_INTG
4811 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4812#endif
4813
Bram Moolenaarafa24992006-03-27 20:58:26 +00004814#ifdef FEAT_GUI_TABLINE
4815 case WM_RBUTTONUP:
4816 {
4817 if (gui_mch_showing_tabline())
4818 {
4819 POINT pt;
4820 RECT rect;
4821
4822 /*
4823 * If the cursor is on the tabline, display the tab menu
4824 */
4825 GetCursorPos((LPPOINT)&pt);
4826 GetWindowRect(s_textArea, &rect);
4827 if (pt.y < rect.top)
4828 {
4829 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004830 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004831 }
4832 }
4833 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4834 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004835 case WM_LBUTTONDBLCLK:
4836 {
4837 /*
4838 * If the user double clicked the tabline, create a new tab
4839 */
4840 if (gui_mch_showing_tabline())
4841 {
4842 POINT pt;
4843 RECT rect;
4844
4845 GetCursorPos((LPPOINT)&pt);
4846 GetWindowRect(s_textArea, &rect);
4847 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004848 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004849 }
4850 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4851 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004852#endif
4853
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 case WM_QUERYENDSESSION: /* System wants to go down. */
4855 gui_shell_closed(); /* Will exit when no changed buffers. */
4856 return FALSE; /* Do NOT allow system to go down. */
4857
4858 case WM_ENDSESSION:
4859 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004860 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004862 return 0L;
4863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 break;
4865
4866 case WM_CHAR:
4867 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4868 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004869 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 return 0L;
4871
4872 case WM_SYSCHAR:
4873 /*
4874 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4875 * shortcut key, handle like a typed ALT key, otherwise call Windows
4876 * ALT key handling.
4877 */
4878#ifdef FEAT_MENU
4879 if ( !gui.menu_is_active
4880 || p_wak[0] == 'n'
4881 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4882 )
4883#endif
4884 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004885 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 return 0L;
4887 }
4888#ifdef FEAT_MENU
4889 else
4890 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4891#endif
4892
4893 case WM_SYSKEYUP:
4894#ifdef FEAT_MENU
4895 /* This used to be done only when menu is active: ALT key is used for
4896 * that. But that caused problems when menu is disabled and using
4897 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4898 * are received, mouse pointer remains hidden. */
4899 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4900#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004901 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902#endif
4903
4904 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004905 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906
4907 case WM_MOUSEWHEEL:
4908 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004909 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910
Bram Moolenaar520470a2005-06-16 21:59:56 +00004911 /* Notification for change in SystemParametersInfo() */
4912 case WM_SETTINGCHANGE:
4913 return _OnSettingChange((UINT)wParam);
4914
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004915#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 case WM_NOTIFY:
4917 switch (((LPNMHDR) lParam)->code)
4918 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004919# ifdef FEAT_MBYTE
4920 case TTN_GETDISPINFOW:
4921# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004922 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004924 LPNMHDR hdr = (LPNMHDR)lParam;
4925 char_u *str = NULL;
4926 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004927
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004928 vim_free(tt_text);
4929 tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004930
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004931# ifdef FEAT_GUI_TABLINE
4932 if (gui_mch_showing_tabline()
4933 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004934 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004935 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004936 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004937 * Mouse is over the GUI tabline. Display the
4938 * tooltip for the tab under the cursor
4939 *
4940 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004941 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004942 GetCursorPos(&pt);
4943 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004944 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004945 TCHITTESTINFO htinfo;
4946 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004947
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004948 /*
4949 * Get the tab under the cursor
4950 */
4951 htinfo.pt.x = pt.x;
4952 htinfo.pt.y = pt.y;
4953 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4954 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004955 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004956 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004957
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004958 tp = find_tabpage(idx + 1);
4959 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004960 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004961 get_tabline_label(tp, TRUE);
4962 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004963 }
4964 }
4965 }
4966 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004967# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004968# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004969# ifdef FEAT_GUI_TABLINE
4970 else
4971# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004973 UINT idButton;
4974 vimmenu_T *pMenu;
4975
4976 idButton = (UINT) hdr->idFrom;
4977 pMenu = gui_mswin_find_menu(root_menu, idButton);
4978 if (pMenu)
4979 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004981# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004982 if (str != NULL)
4983 {
4984# ifdef FEAT_MBYTE
4985 if (hdr->code == TTN_GETDISPINFOW)
4986 {
4987 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4988
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004989 /* Set the maximum width, this also enables using
4990 * \n for line break. */
4991 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4992 0, 500);
4993
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004994 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004995 lpdi->lpszText = tt_text;
4996 /* can't show tooltip if failed */
4997 }
4998 else
4999# endif
5000 {
5001 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
5002
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005003 /* Set the maximum width, this also enables using
5004 * \n for line break. */
5005 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5006 0, 500);
5007
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005008 if (STRLEN(str) < sizeof(lpdi->szText)
5009 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005010 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005011 sizeof(lpdi->szText) - 1);
5012 else
5013 lpdi->lpszText = tt_text;
5014 }
5015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
5017 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005018# ifdef FEAT_GUI_TABLINE
5019 case TCN_SELCHANGE:
5020 if (gui_mch_showing_tabline()
5021 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005022 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005023 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005024 return 0L;
5025 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005026 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00005027
5028 case NM_RCLICK:
5029 if (gui_mch_showing_tabline()
5030 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005031 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00005032 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005033 return 0L;
5034 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00005035 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005036# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005038# ifdef FEAT_GUI_TABLINE
5039 if (gui_mch_showing_tabline()
5040 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
5041 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5042# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 break;
5044 }
5045 break;
5046#endif
5047#if defined(MENUHINTS) && defined(FEAT_MENU)
5048 case WM_MENUSELECT:
5049 if (((UINT) HIWORD(wParam)
5050 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
5051 == MF_HILITE
5052 && (State & CMDLINE) == 0)
5053 {
5054 UINT idButton;
5055 vimmenu_T *pMenu;
5056 static int did_menu_tip = FALSE;
5057
5058 if (did_menu_tip)
5059 {
5060 msg_clr_cmdline();
5061 setcursor();
5062 out_flush();
5063 did_menu_tip = FALSE;
5064 }
5065
5066 idButton = (UINT)LOWORD(wParam);
5067 pMenu = gui_mswin_find_menu(root_menu, idButton);
5068 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
5069 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
5070 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005071 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005073 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 setcursor();
5075 out_flush();
5076 did_menu_tip = TRUE;
5077 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01005078 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 }
5080 break;
5081#endif
5082 case WM_NCHITTEST:
5083 {
5084 LRESULT result;
5085 int x, y;
5086 int xPos = GET_X_LPARAM(lParam);
5087
5088 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
5089 if (result == HTCLIENT)
5090 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005091#ifdef FEAT_GUI_TABLINE
5092 if (gui_mch_showing_tabline())
5093 {
5094 int yPos = GET_Y_LPARAM(lParam);
5095 RECT rct;
5096
5097 /* If the cursor is on the GUI tabline, don't process this
5098 * event */
5099 GetWindowRect(s_textArea, &rct);
5100 if (yPos < rct.top)
5101 return result;
5102 }
5103#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02005104 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 xPos -= x;
5106
5107 if (xPos < 48) /* <VN> TODO should use system metric? */
5108 return HTBOTTOMLEFT;
5109 else
5110 return HTBOTTOMRIGHT;
5111 }
5112 else
5113 return result;
5114 }
5115 /* break; notreached */
5116
5117#ifdef FEAT_MBYTE_IME
5118 case WM_IME_NOTIFY:
5119 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5120 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005121 return 1L;
5122
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 case WM_IME_COMPOSITION:
5124 if (!_OnImeComposition(hwnd, wParam, lParam))
5125 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005126 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127#endif
5128
5129 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005131 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 {
5133 _OnFindRepl();
5134 }
5135#endif
5136 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5137 }
5138
Bram Moolenaar2787ab92011-12-14 15:23:59 +01005139 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140}
5141
5142/*
5143 * End of call-back routines
5144 */
5145
5146/* parent window, if specified with -P */
5147HWND vim_parent_hwnd = NULL;
5148
5149 static BOOL CALLBACK
5150FindWindowTitle(HWND hwnd, LPARAM lParam)
5151{
5152 char buf[2048];
5153 char *title = (char *)lParam;
5154
5155 if (GetWindowText(hwnd, buf, sizeof(buf)))
5156 {
5157 if (strstr(buf, title) != NULL)
5158 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005159 /* Found it. Store the window ref. and quit searching if MDI
5160 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005162 if (vim_parent_hwnd != NULL)
5163 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
5165 }
5166 return TRUE; /* continue searching */
5167}
5168
5169/*
5170 * Invoked for '-P "title"' argument: search for parent application to open
5171 * our window in.
5172 */
5173 void
5174gui_mch_set_parent(char *title)
5175{
5176 EnumWindows(FindWindowTitle, (LPARAM)title);
5177 if (vim_parent_hwnd == NULL)
5178 {
5179 EMSG2(_("E671: Cannot find window title \"%s\""), title);
5180 mch_exit(2);
5181 }
5182}
5183
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005184#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 static void
5186ole_error(char *arg)
5187{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005188 char buf[IOSIZE];
5189
5190 /* Can't use EMSG() here, we have not finished initialisation yet. */
5191 vim_snprintf(buf, IOSIZE,
5192 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
5193 arg);
5194 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197
5198/*
5199 * Parse the GUI related command-line arguments. Any arguments used are
5200 * deleted from argv, and *argc is decremented accordingly. This is called
5201 * when vim is started, whether or not the GUI has been started.
5202 */
5203 void
5204gui_mch_prepare(int *argc, char **argv)
5205{
5206 int silent = FALSE;
5207 int idx;
5208
5209 /* Check for special OLE command line parameters */
5210 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5211 {
5212 /* Check for a "-silent" argument first. */
5213 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5214 && (argv[2][0] == '-' || argv[2][0] == '/'))
5215 {
5216 silent = TRUE;
5217 idx = 2;
5218 }
5219 else
5220 idx = 1;
5221
5222 /* Register Vim as an OLE Automation server */
5223 if (STRICMP(argv[idx] + 1, "register") == 0)
5224 {
5225#ifdef FEAT_OLE
5226 RegisterMe(silent);
5227 mch_exit(0);
5228#else
5229 if (!silent)
5230 ole_error("register");
5231 mch_exit(2);
5232#endif
5233 }
5234
5235 /* Unregister Vim as an OLE Automation server */
5236 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5237 {
5238#ifdef FEAT_OLE
5239 UnregisterMe(!silent);
5240 mch_exit(0);
5241#else
5242 if (!silent)
5243 ole_error("unregister");
5244 mch_exit(2);
5245#endif
5246 }
5247
5248 /* Ignore an -embedding argument. It is only relevant if the
5249 * application wants to treat the case when it is started manually
5250 * differently from the case where it is started via automation (and
5251 * we don't).
5252 */
5253 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5254 {
5255#ifdef FEAT_OLE
5256 *argc = 1;
5257#else
5258 ole_error("embedding");
5259 mch_exit(2);
5260#endif
5261 }
5262 }
5263
5264#ifdef FEAT_OLE
5265 {
5266 int bDoRestart = FALSE;
5267
5268 InitOLE(&bDoRestart);
5269 /* automatically exit after registering */
5270 if (bDoRestart)
5271 mch_exit(0);
5272 }
5273#endif
5274
5275#ifdef FEAT_NETBEANS_INTG
5276 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005277 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 int arg;
5279
5280 for (arg = 1; arg < *argc; arg++)
5281 if (strncmp("-nb", argv[arg], 3) == 0)
5282 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 netbeansArg = argv[arg];
5284 mch_memmove(&argv[arg], &argv[arg + 1],
5285 (--*argc - arg) * sizeof(char *));
5286 argv[*argc] = NULL;
5287 break; /* enough? */
5288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 }
5290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291}
5292
5293/*
5294 * Initialise the GUI. Create all the windows, set up all the call-backs
5295 * etc.
5296 */
5297 int
5298gui_mch_init(void)
5299{
5300 const char szVimWndClass[] = VIM_CLASS;
5301 const char szTextAreaClass[] = "VimTextArea";
5302 WNDCLASS wndclass;
5303#ifdef FEAT_MBYTE
5304 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005305 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 WNDCLASSW wndclassw;
5307#endif
5308#ifdef GLOBAL_IME
5309 ATOM atom;
5310#endif
5311
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 /* Return here if the window was already opened (happens when
5313 * gui_mch_dialog() is called early). */
5314 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005315 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316
5317 /*
5318 * Load the tearoff bitmap
5319 */
5320#ifdef FEAT_TEAROFF
5321 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
5322#endif
5323
5324 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5325 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5326#ifdef FEAT_MENU
5327 gui.menu_height = 0; /* Windows takes care of this */
5328#endif
5329 gui.border_width = 0;
5330
5331 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5332
5333#ifdef FEAT_MBYTE
5334 /* First try using the wide version, so that we can use any title.
5335 * Otherwise only characters in the active codepage will work. */
5336 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
5337 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005338 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 wndclassw.lpfnWndProc = _WndProc;
5340 wndclassw.cbClsExtra = 0;
5341 wndclassw.cbWndExtra = 0;
5342 wndclassw.hInstance = s_hinst;
5343 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5344 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5345 wndclassw.hbrBackground = s_brush;
5346 wndclassw.lpszMenuName = NULL;
5347 wndclassw.lpszClassName = szVimWndClassW;
5348
5349 if ((
5350#ifdef GLOBAL_IME
5351 atom =
5352#endif
5353 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005354 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 else
5356 wide_WindowProc = TRUE;
5357 }
5358
5359 if (!wide_WindowProc)
5360#endif
5361
5362 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
5363 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005364 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 wndclass.lpfnWndProc = _WndProc;
5366 wndclass.cbClsExtra = 0;
5367 wndclass.cbWndExtra = 0;
5368 wndclass.hInstance = s_hinst;
5369 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
5370 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5371 wndclass.hbrBackground = s_brush;
5372 wndclass.lpszMenuName = NULL;
5373 wndclass.lpszClassName = szVimWndClass;
5374
5375 if ((
5376#ifdef GLOBAL_IME
5377 atom =
5378#endif
5379 RegisterClass(&wndclass)) == 0)
5380 return FAIL;
5381 }
5382
5383 if (vim_parent_hwnd != NULL)
5384 {
5385#ifdef HAVE_TRY_EXCEPT
5386 __try
5387 {
5388#endif
5389 /* Open inside the specified parent window.
5390 * TODO: last argument should point to a CLIENTCREATESTRUCT
5391 * structure. */
5392 s_hwnd = CreateWindowEx(
5393 WS_EX_MDICHILD,
5394 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005395 WS_OVERLAPPEDWINDOW | WS_CHILD
5396 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5398 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5399 100, /* Any value will do */
5400 100, /* Any value will do */
5401 vim_parent_hwnd, NULL,
5402 s_hinst, NULL);
5403#ifdef HAVE_TRY_EXCEPT
5404 }
5405 __except(EXCEPTION_EXECUTE_HANDLER)
5406 {
5407 /* NOP */
5408 }
5409#endif
5410 if (s_hwnd == NULL)
5411 {
5412 EMSG(_("E672: Unable to open window inside MDI application"));
5413 mch_exit(2);
5414 }
5415 }
5416 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005417 {
5418 /* If the provided windowid is not valid reset it to zero, so that it
5419 * is ignored and we open our own window. */
5420 if (IsWindow((HWND)win_socket_id) <= 0)
5421 win_socket_id = 0;
5422
5423 /* Create a window. If win_socket_id is not zero without border and
5424 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00005426 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005427 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5428 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005429 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5430 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5431 100, /* Any value will do */
5432 100, /* Any value will do */
5433 NULL, NULL,
5434 s_hinst, NULL);
5435 if (s_hwnd != NULL && win_socket_id != 0)
5436 {
5437 SetParent(s_hwnd, (HWND)win_socket_id);
5438 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5439 }
5440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441
5442 if (s_hwnd == NULL)
5443 return FAIL;
5444
5445#ifdef GLOBAL_IME
5446 global_ime_init(atom, s_hwnd);
5447#endif
5448#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5449 dyn_imm_load();
5450#endif
5451
5452 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005453#ifdef FEAT_MBYTE
5454 if (wide_WindowProc)
5455 {
5456 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
5457 {
5458 wndclassw.style = CS_OWNDC;
5459 wndclassw.lpfnWndProc = _TextAreaWndProc;
5460 wndclassw.cbClsExtra = 0;
5461 wndclassw.cbWndExtra = 0;
5462 wndclassw.hInstance = s_hinst;
5463 wndclassw.hIcon = NULL;
5464 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5465 wndclassw.hbrBackground = NULL;
5466 wndclassw.lpszMenuName = NULL;
5467 wndclassw.lpszClassName = szTextAreaClassW;
5468
5469 if (RegisterClassW(&wndclassw) == 0)
5470 return FAIL;
5471 }
5472 }
5473 else
5474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
5476 {
5477 wndclass.style = CS_OWNDC;
5478 wndclass.lpfnWndProc = _TextAreaWndProc;
5479 wndclass.cbClsExtra = 0;
5480 wndclass.cbWndExtra = 0;
5481 wndclass.hInstance = s_hinst;
5482 wndclass.hIcon = NULL;
5483 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5484 wndclass.hbrBackground = NULL;
5485 wndclass.lpszMenuName = NULL;
5486 wndclass.lpszClassName = szTextAreaClass;
5487
5488 if (RegisterClass(&wndclass) == 0)
5489 return FAIL;
5490 }
5491 s_textArea = CreateWindowEx(
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005492 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 szTextAreaClass, "Vim text area",
5494 WS_CHILD | WS_VISIBLE, 0, 0,
5495 100, /* Any value will do for now */
5496 100, /* Any value will do for now */
5497 s_hwnd, NULL,
5498 s_hinst, NULL);
5499
5500 if (s_textArea == NULL)
5501 return FAIL;
5502
Bram Moolenaar20321902016-02-17 12:30:17 +01005503#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005504 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5505 {
5506 HANDLE hIcon = NULL;
5507
5508 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005509 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005510 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005511#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005512
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513#ifdef FEAT_MENU
5514 s_menuBar = CreateMenu();
5515#endif
5516 s_hdc = GetDC(s_textArea);
5517
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519
5520 /* Do we need to bother with this? */
5521 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5522
5523 /* Get background/foreground colors from the system */
5524 gui_mch_def_colors();
5525
5526 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5527 * file) */
5528 set_normal_colors();
5529
5530 /*
5531 * Check that none of the colors are the same as the background color.
5532 * Then store the current values as the defaults.
5533 */
5534 gui_check_colors();
5535 gui.def_norm_pixel = gui.norm_pixel;
5536 gui.def_back_pixel = gui.back_pixel;
5537
5538 /* Get the colors for the highlight groups (gui_check_colors() might have
5539 * changed them) */
5540 highlight_gui_started();
5541
5542 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005543 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005545 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546
5547 /*
5548 * Set up for Intellimouse processing
5549 */
5550 init_mouse_wheel();
5551
5552 /*
5553 * compute a couple of metrics used for the dialogs
5554 */
5555 get_dialog_font_metrics();
5556#ifdef FEAT_TOOLBAR
5557 /*
5558 * Create the toolbar
5559 */
5560 initialise_toolbar();
5561#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005562#ifdef FEAT_GUI_TABLINE
5563 /*
5564 * Create the tabline
5565 */
5566 initialise_tabline();
5567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568#ifdef MSWIN_FIND_REPLACE
5569 /*
5570 * Initialise the dialog box stuff
5571 */
5572 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5573
5574 /* Initialise the struct */
5575 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005576 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005578 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5580 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5581 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005582# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00005583 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
5584 s_findrep_struct_w.lpstrFindWhat =
5585 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5586 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
5587 s_findrep_struct_w.lpstrReplaceWith =
5588 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5589 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
5590 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
5591 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5592# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005595#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005596# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5597/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5598# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005599# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005600# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005601# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005602 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005603 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005604#endif
5605
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005606#ifdef FEAT_RENDER_OPTIONS
5607 if (p_rop)
5608 (void)gui_mch_set_rendering_options(p_rop);
5609#endif
5610
Bram Moolenaar748bf032005-02-02 23:04:36 +00005611theend:
5612 /* Display any pending error messages */
5613 display_errors();
5614
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 return OK;
5616}
5617
5618/*
5619 * Get the size of the screen, taking position on multiple monitors into
5620 * account (if supported).
5621 */
5622 static void
5623get_work_area(RECT *spi_rect)
5624{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005625 HMONITOR mon;
5626 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005628 /* work out which monitor the window is on, and get *it's* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005629 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005630 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005632 moninfo.cbSize = sizeof(MONITORINFO);
5633 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005635 *spi_rect = moninfo.rcWork;
5636 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 }
5638 }
5639 /* this is the old method... */
5640 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5641}
5642
5643/*
5644 * Set the size of the window to the given width and height in pixels.
5645 */
5646 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005647gui_mch_set_shellsize(
5648 int width,
5649 int height,
5650 int min_width UNUSED,
5651 int min_height UNUSED,
5652 int base_width UNUSED,
5653 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005654 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655{
5656 RECT workarea_rect;
5657 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 WINDOWPLACEMENT wndpl;
5659
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005660 /* Try to keep window completely on screen. */
5661 /* Get position of the screen work area. This is the part that is not
5662 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 get_work_area(&workarea_rect);
5664
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005665 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005666 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 wndpl.length = sizeof(WINDOWPLACEMENT);
5668 GetWindowPlacement(s_hwnd, &wndpl);
5669
5670 /* Resizing a maximized window looks very strange, unzoom it first.
5671 * But don't do it when still starting up, it may have been requested in
5672 * the shortcut. */
5673 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5674 {
5675 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5676 /* Need to get the settings of the normal window. */
5677 GetWindowPlacement(s_hwnd, &wndpl);
5678 }
5679
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005681 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005682 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005683 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005684 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 + GetSystemMetrics(SM_CYCAPTION)
5686#ifdef FEAT_MENU
5687 + gui_mswin_get_menu_height(FALSE)
5688#endif
5689 ;
5690
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005691 /* The following should take care of keeping Vim on the same monitor, no
5692 * matter if the secondary monitor is left or right of the primary
5693 * monitor. */
5694 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5695 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005696
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005697 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005698 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005699 && wndpl.rcNormalPosition.right > workarea_rect.right)
5700 OffsetRect(&wndpl.rcNormalPosition,
5701 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005703 if ((direction & RESIZE_HOR)
5704 && wndpl.rcNormalPosition.left < workarea_rect.left)
5705 OffsetRect(&wndpl.rcNormalPosition,
5706 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707
Bram Moolenaarafa24992006-03-27 20:58:26 +00005708 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005709 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5710 OffsetRect(&wndpl.rcNormalPosition,
5711 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005713 if ((direction & RESIZE_VERT)
5714 && wndpl.rcNormalPosition.top < workarea_rect.top)
5715 OffsetRect(&wndpl.rcNormalPosition,
5716 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717
5718 /* set window position - we should use SetWindowPlacement rather than
5719 * SetWindowPos as the MSDN docs say the coord systems returned by
5720 * these two are not compatible. */
5721 SetWindowPlacement(s_hwnd, &wndpl);
5722
5723 SetActiveWindow(s_hwnd);
5724 SetFocus(s_hwnd);
5725
5726#ifdef FEAT_MENU
5727 /* Menu may wrap differently now */
5728 gui_mswin_get_menu_height(!gui.starting);
5729#endif
5730}
5731
5732
5733 void
5734gui_mch_set_scrollbar_thumb(
5735 scrollbar_T *sb,
5736 long val,
5737 long size,
5738 long max)
5739{
5740 SCROLLINFO info;
5741
5742 sb->scroll_shift = 0;
5743 while (max > 32767)
5744 {
5745 max = (max + 1) >> 1;
5746 val >>= 1;
5747 size >>= 1;
5748 ++sb->scroll_shift;
5749 }
5750
5751 if (sb->scroll_shift > 0)
5752 ++size;
5753
5754 info.cbSize = sizeof(info);
5755 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5756 info.nPos = val;
5757 info.nMin = 0;
5758 info.nMax = max;
5759 info.nPage = size;
5760 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5761}
5762
5763
5764/*
5765 * Set the current text font.
5766 */
5767 void
5768gui_mch_set_font(GuiFont font)
5769{
5770 gui.currFont = font;
5771}
5772
5773
5774/*
5775 * Set the current text foreground color.
5776 */
5777 void
5778gui_mch_set_fg_color(guicolor_T color)
5779{
5780 gui.currFgColor = color;
5781}
5782
5783/*
5784 * Set the current text background color.
5785 */
5786 void
5787gui_mch_set_bg_color(guicolor_T color)
5788{
5789 gui.currBgColor = color;
5790}
5791
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005792/*
5793 * Set the current text special color.
5794 */
5795 void
5796gui_mch_set_sp_color(guicolor_T color)
5797{
5798 gui.currSpColor = color;
5799}
5800
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005801#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802/*
5803 * Multi-byte handling, originally by Sung-Hoon Baek.
5804 * First static functions (no prototypes generated).
5805 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005806# ifdef _MSC_VER
5807# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5808# endif
5809# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810
5811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 * handle WM_IME_NOTIFY message
5813 */
5814 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005815_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816{
5817 LRESULT lResult = 0;
5818 HIMC hImc;
5819
5820 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5821 return lResult;
5822 switch (dwCommand)
5823 {
5824 case IMN_SETOPENSTATUS:
5825 if (pImmGetOpenStatus(hImc))
5826 {
5827 pImmSetCompositionFont(hImc, &norm_logfont);
5828 im_set_position(gui.row, gui.col);
5829
5830 /* Disable langmap */
5831 State &= ~LANGMAP;
5832 if (State & INSERT)
5833 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005834#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 /* Unshown 'keymap' in status lines */
5836 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5837 {
5838 /* Save cursor position */
5839 int old_row = gui.row;
5840 int old_col = gui.col;
5841
5842 // This must be called here before
5843 // status_redraw_curbuf(), otherwise the mode
5844 // message may appear in the wrong position.
5845 showmode();
5846 status_redraw_curbuf();
5847 update_screen(0);
5848 /* Restore cursor position */
5849 gui.row = old_row;
5850 gui.col = old_col;
5851 }
5852#endif
5853 }
5854 }
5855 gui_update_cursor(TRUE, FALSE);
5856 lResult = 0;
5857 break;
5858 }
5859 pImmReleaseContext(hWnd, hImc);
5860 return lResult;
5861}
5862
5863 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005864_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865{
5866 char_u *ret;
5867 int len;
5868
5869 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5870 return 0;
5871
5872 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5873 if (ret != NULL)
5874 {
5875 add_to_input_buf_csi(ret, len);
5876 vim_free(ret);
5877 return 1;
5878 }
5879 return 0;
5880}
5881
5882/*
5883 * get the current composition string, in UCS-2; *lenp is the number of
5884 * *lenp is the number of Unicode characters.
5885 */
5886 static short_u *
5887GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5888{
5889 LONG ret;
5890 LPWSTR wbuf = NULL;
5891 char_u *buf;
5892
5893 if (!pImmGetContext)
5894 return NULL; /* no imm32.dll */
5895
5896 /* Try Unicode; this'll always work on NT regardless of codepage. */
5897 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5898 if (ret == 0)
5899 return NULL; /* empty */
5900
5901 if (ret > 0)
5902 {
5903 /* Allocate the requested buffer plus space for the NUL character. */
5904 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
5905 if (wbuf != NULL)
5906 {
5907 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5908 *lenp = ret / sizeof(WCHAR);
5909 }
5910 return (short_u *)wbuf;
5911 }
5912
5913 /* ret < 0; we got an error, so try the ANSI version. This'll work
5914 * on 9x/ME, but only if the codepage happens to be set to whatever
5915 * we're inputting. */
5916 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5917 if (ret <= 0)
5918 return NULL; /* empty or error */
5919
5920 buf = alloc(ret);
5921 if (buf == NULL)
5922 return NULL;
5923 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5924
5925 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005926 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 vim_free(buf);
5928
5929 return (short_u *)wbuf;
5930}
5931
5932/*
5933 * void GetResultStr()
5934 *
5935 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5936 * get complete composition string
5937 */
5938 static char_u *
5939GetResultStr(HWND hwnd, int GCS, int *lenp)
5940{
5941 HIMC hIMC; /* Input context handle. */
5942 short_u *buf = NULL;
5943 char_u *convbuf = NULL;
5944
5945 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5946 return NULL;
5947
5948 /* Reads in the composition string. */
5949 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5950 if (buf == NULL)
5951 return NULL;
5952
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005953 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 pImmReleaseContext(hwnd, hIMC);
5955 vim_free(buf);
5956 return convbuf;
5957}
5958#endif
5959
5960/* For global functions we need prototypes. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005961#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962
5963/*
5964 * set font to IM.
5965 */
5966 void
5967im_set_font(LOGFONT *lf)
5968{
5969 HIMC hImc;
5970
5971 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5972 {
5973 pImmSetCompositionFont(hImc, lf);
5974 pImmReleaseContext(s_hwnd, hImc);
5975 }
5976}
5977
5978/*
5979 * Notify cursor position to IM.
5980 */
5981 void
5982im_set_position(int row, int col)
5983{
5984 HIMC hImc;
5985
5986 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5987 {
5988 COMPOSITIONFORM cfs;
5989
5990 cfs.dwStyle = CFS_POINT;
5991 cfs.ptCurrentPos.x = FILL_X(col);
5992 cfs.ptCurrentPos.y = FILL_Y(row);
5993 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
5994 pImmSetCompositionWindow(hImc, &cfs);
5995
5996 pImmReleaseContext(s_hwnd, hImc);
5997 }
5998}
5999
6000/*
6001 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6002 */
6003 void
6004im_set_active(int active)
6005{
6006 HIMC hImc;
6007 static HIMC hImcOld = (HIMC)0;
6008
6009 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
6010 {
6011 if (p_imdisable)
6012 {
6013 if (hImcOld == (HIMC)0)
6014 {
6015 hImcOld = pImmGetContext(s_hwnd);
6016 if (hImcOld)
6017 pImmAssociateContext(s_hwnd, (HIMC)0);
6018 }
6019 active = FALSE;
6020 }
6021 else if (hImcOld != (HIMC)0)
6022 {
6023 pImmAssociateContext(s_hwnd, hImcOld);
6024 hImcOld = (HIMC)0;
6025 }
6026
6027 hImc = pImmGetContext(s_hwnd);
6028 if (hImc)
6029 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006030 /*
6031 * for Korean ime
6032 */
6033 HKL hKL = GetKeyboardLayout(0);
6034
6035 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6036 {
6037 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6038 static BOOL bSaved = FALSE;
6039
6040 if (active)
6041 {
6042 /* if we have a saved conversion status, restore it */
6043 if (bSaved)
6044 pImmSetConversionStatus(hImc, dwConversionSaved,
6045 dwSentenceSaved);
6046 bSaved = FALSE;
6047 }
6048 else
6049 {
6050 /* save conversion status and disable korean */
6051 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6052 &dwSentenceSaved))
6053 {
6054 bSaved = TRUE;
6055 pImmSetConversionStatus(hImc,
6056 dwConversionSaved & ~(IME_CMODE_NATIVE
6057 | IME_CMODE_FULLSHAPE),
6058 dwSentenceSaved);
6059 }
6060 }
6061 }
6062
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 pImmSetOpenStatus(hImc, active);
6064 pImmReleaseContext(s_hwnd, hImc);
6065 }
6066 }
6067}
6068
6069/*
6070 * Get IM status. When IM is on, return not 0. Else return 0.
6071 */
6072 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006073im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074{
6075 int status = 0;
6076 HIMC hImc;
6077
6078 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6079 {
6080 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6081 pImmReleaseContext(s_hwnd, hImc);
6082 }
6083 return status;
6084}
6085
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006086#endif /* FEAT_MBYTE_IME */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087
6088#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
6089/* Win32 with GLOBAL IME */
6090
6091/*
6092 * Notify cursor position to IM.
6093 */
6094 void
6095im_set_position(int row, int col)
6096{
6097 /* Win32 with GLOBAL IME */
6098 POINT p;
6099
6100 p.x = FILL_X(col);
6101 p.y = FILL_Y(row);
6102 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
6103 global_ime_set_position(&p);
6104}
6105
6106/*
6107 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6108 */
6109 void
6110im_set_active(int active)
6111{
6112 global_ime_set_status(active);
6113}
6114
6115/*
6116 * Get IM status. When IM is on, return not 0. Else return 0.
6117 */
6118 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006119im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120{
6121 return global_ime_get_status();
6122}
6123#endif
6124
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006125#ifdef FEAT_MBYTE
6126/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006127 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006128 */
6129 static void
6130latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6131{
6132 int c;
6133
Bram Moolenaarca003e12006-03-17 23:19:38 +00006134 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006135 {
6136 c = *text++;
6137 switch (c)
6138 {
6139 case 0xa4: c = 0x20ac; break; /* euro */
6140 case 0xa6: c = 0x0160; break; /* S hat */
6141 case 0xa8: c = 0x0161; break; /* S -hat */
6142 case 0xb4: c = 0x017d; break; /* Z hat */
6143 case 0xb8: c = 0x017e; break; /* Z -hat */
6144 case 0xbc: c = 0x0152; break; /* OE */
6145 case 0xbd: c = 0x0153; break; /* oe */
6146 case 0xbe: c = 0x0178; break; /* Y */
6147 }
6148 *unicodebuf++ = c;
6149 }
6150}
6151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152
6153#ifdef FEAT_RIGHTLEFT
6154/*
6155 * What is this for? In the case where you are using Win98 or Win2K or later,
6156 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6157 * reverses the string sent to the TextOut... family. This sucks, because we
6158 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6159 * way to tell Windblows not to do this!
6160 *
6161 * The short of it is that this 'RevOut' only gets called if you are running
6162 * one of the new, "improved" MS OSes, and only if you are running in
6163 * 'rightleft' mode. It makes display take *slightly* longer, but not
6164 * noticeably so.
6165 */
6166 static void
6167RevOut( HDC s_hdc,
6168 int col,
6169 int row,
6170 UINT foptions,
6171 CONST RECT *pcliprect,
6172 LPCTSTR text,
6173 UINT len,
6174 CONST INT *padding)
6175{
6176 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006178 for (ix = 0; ix < (int)len; ++ix)
6179 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6180 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181}
6182#endif
6183
6184 void
6185gui_mch_draw_string(
6186 int row,
6187 int col,
6188 char_u *text,
6189 int len,
6190 int flags)
6191{
6192 static int *padding = NULL;
6193 static int pad_size = 0;
6194 int i;
6195 const RECT *pcliprect = NULL;
6196 UINT foptions = 0;
6197#ifdef FEAT_MBYTE
6198 static WCHAR *unicodebuf = NULL;
6199 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006200 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 int n = 0;
6202#endif
6203 HPEN hpen, old_pen;
6204 int y;
6205
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 /*
6207 * Italic and bold text seems to have an extra row of pixels at the bottom
6208 * (below where the bottom of the character should be). If we draw the
6209 * characters with a solid background, the top row of pixels in the
6210 * character below will be overwritten. We can fix this by filling in the
6211 * background ourselves, to the correct character proportions, and then
6212 * writing the character in transparent mode. Still have a problem when
6213 * the character is "_", which gets written on to the character below.
6214 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6215 * pixel in their slots, which fixes the problem with the bottom row of
6216 * pixels. We still need this code because otherwise the top row of pixels
6217 * becomes a problem. - webb.
6218 */
6219 static HBRUSH hbr_cache[2] = {NULL, NULL};
6220 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6221 static int brush_lru = 0;
6222 HBRUSH hbr;
6223 RECT rc;
6224
6225 if (!(flags & DRAW_TRANSP))
6226 {
6227 /*
6228 * Clear background first.
6229 * Note: FillRect() excludes right and bottom of rectangle.
6230 */
6231 rc.left = FILL_X(col);
6232 rc.top = FILL_Y(row);
6233#ifdef FEAT_MBYTE
6234 if (has_mbyte)
6235 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006237 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 }
6239 else
6240#endif
6241 rc.right = FILL_X(col + len);
6242 rc.bottom = FILL_Y(row + 1);
6243
6244 /* Cache the created brush, that saves a lot of time. We need two:
6245 * one for cursor background and one for the normal background. */
6246 if (gui.currBgColor == brush_color[0])
6247 {
6248 hbr = hbr_cache[0];
6249 brush_lru = 1;
6250 }
6251 else if (gui.currBgColor == brush_color[1])
6252 {
6253 hbr = hbr_cache[1];
6254 brush_lru = 0;
6255 }
6256 else
6257 {
6258 if (hbr_cache[brush_lru] != NULL)
6259 DeleteBrush(hbr_cache[brush_lru]);
6260 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6261 brush_color[brush_lru] = gui.currBgColor;
6262 hbr = hbr_cache[brush_lru];
6263 brush_lru = !brush_lru;
6264 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006265
6266#if defined(FEAT_DIRECTX)
6267 if (IS_ENABLE_DIRECTX())
6268 DWriteContext_FillRect(s_dwc, &rc, gui.currBgColor);
6269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 FillRect(s_hdc, &rc, hbr);
6271
6272 SetBkMode(s_hdc, TRANSPARENT);
6273
6274 /*
6275 * When drawing block cursor, prevent inverted character spilling
6276 * over character cell (can happen with bold/italic)
6277 */
6278 if (flags & DRAW_CURSOR)
6279 {
6280 pcliprect = &rc;
6281 foptions = ETO_CLIPPED;
6282 }
6283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 SetTextColor(s_hdc, gui.currFgColor);
6285 SelectFont(s_hdc, gui.currFont);
6286
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006287#ifdef FEAT_DIRECTX
6288 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006289 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006290#endif
6291
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6293 {
6294 vim_free(padding);
6295 pad_size = Columns;
6296
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006297 /* Don't give an out-of-memory message here, it would call us
6298 * recursively. */
6299 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 if (padding != NULL)
6301 for (i = 0; i < pad_size; i++)
6302 padding[i] = gui.char_width;
6303 }
6304
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 /*
6306 * We have to provide the padding argument because italic and bold versions
6307 * of fixed-width fonts are often one pixel or so wider than their normal
6308 * versions.
6309 * No check for DRAW_BOLD, Windows will have done it already.
6310 */
6311
6312#ifdef FEAT_MBYTE
6313 /* Check if there are any UTF-8 characters. If not, use normal text
6314 * output to speed up output. */
6315 if (enc_utf8)
6316 for (n = 0; n < len; ++n)
6317 if (text[n] >= 0x80)
6318 break;
6319
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006320#if defined(FEAT_DIRECTX)
6321 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6322 * required that unicode drawing routine, currently. So this forces it
6323 * enabled. */
6324 if (enc_utf8 && IS_ENABLE_DIRECTX())
6325 n = 0; /* Keep n < len, to enter block for unicode. */
6326#endif
6327
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006329 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006331 if ((enc_utf8
6332 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6333 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 && (unicodebuf == NULL || len > unibuflen))
6335 {
6336 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006337 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338
6339 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006340 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341
6342 unibuflen = len;
6343 }
6344
6345 if (enc_utf8 && n < len && unicodebuf != NULL)
6346 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006347 /* Output UTF-8 characters. Composing characters should be
6348 * handled here. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006349 int i;
6350 int wlen; /* string length in words */
6351 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 int cells; /* cell width of string up to composing char */
6353 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006354 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006356 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006357 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006359 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006361 c = utf_ptr2char(text + i);
6362 if (c >= 0x10000)
6363 {
6364 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006365 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6366 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006367 }
6368 else
6369 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006370 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006371 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006372
6373 if (utf_iscomposing(c))
6374 cw = 0;
6375 else
6376 {
6377 cw = utf_char2cells(c);
6378 if (cw > 2) /* don't use 4 for unprintable char */
6379 cw = 1;
6380 }
6381
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 if (unicodepdy != NULL)
6383 {
6384 /* Use unicodepdy to make characters fit as we expect, even
6385 * when the font uses different widths (e.g., bold character
6386 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006387 if (c >= 0x10000)
6388 {
6389 unicodepdy[wlen - 2] = cw * gui.char_width;
6390 unicodepdy[wlen - 1] = 0;
6391 }
6392 else
6393 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 }
6395 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006396 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006397 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006399#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006400 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006401 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006402 /* Add one to "cells" for italics. */
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006403 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006404 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006405 gui.char_width, gui.currFgColor,
6406 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006407 }
6408 else
6409#endif
6410 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6411 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 len = cells; /* used for underlining */
6413 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006414 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 {
6416 /* If we want to display codepage data, and the current CP is not the
6417 * ANSI one, we need to go via Unicode. */
6418 if (unicodebuf != NULL)
6419 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006420 if (enc_latin9)
6421 latin9_to_ucs(text, len, unicodebuf);
6422 else
6423 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 MB_PRECOMPOSED,
6425 (char *)text, len,
6426 (LPWSTR)unicodebuf, unibuflen);
6427 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006428 {
6429 /* Use unicodepdy to make characters fit as we expect, even
6430 * when the font uses different widths (e.g., bold character
6431 * is wider). */
6432 if (unicodepdy != NULL)
6433 {
6434 int i;
6435 int cw;
6436
6437 for (i = 0; i < len; ++i)
6438 {
6439 cw = utf_char2cells(unicodebuf[i]);
6440 if (cw > 2)
6441 cw = 1;
6442 unicodepdy[i] = cw * gui.char_width;
6443 }
6444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006446 foptions, pcliprect, unicodebuf, len, unicodepdy);
6447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 }
6449 }
6450 else
6451#endif
6452 {
6453#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006454 /* Windows will mess up RL text, so we have to draw it character by
6455 * character. Only do this if RL is on, since it's slow. */
6456 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6458 foptions, pcliprect, (char *)text, len, padding);
6459 else
6460#endif
6461 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6462 foptions, pcliprect, (char *)text, len, padding);
6463 }
6464
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006465#if defined(FEAT_DIRECTX)
6466 if (IS_ENABLE_DIRECTX() &&
6467 (flags & (DRAW_UNDERL | DRAW_STRIKE | DRAW_UNDERC | DRAW_CURSOR)))
6468 DWriteContext_Flush(s_dwc);
6469#endif
6470
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006471 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 if (flags & DRAW_UNDERL)
6473 {
6474 hpen = CreatePen(PS_SOLID, 1, gui.currFgColor);
6475 old_pen = SelectObject(s_hdc, hpen);
6476 /* When p_linespace is 0, overwrite the bottom row of pixels.
6477 * Otherwise put the line just below the character. */
6478 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 if (p_linespace > 1)
6480 y -= p_linespace - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 MoveToEx(s_hdc, FILL_X(col), y, NULL);
6482 /* Note: LineTo() excludes the last pixel in the line. */
6483 LineTo(s_hdc, FILL_X(col + len), y);
6484 DeleteObject(SelectObject(s_hdc, old_pen));
6485 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006486
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006487 /* Strikethrough */
6488 if (flags & DRAW_STRIKE)
6489 {
6490 hpen = CreatePen(PS_SOLID, 1, gui.currSpColor);
6491 old_pen = SelectObject(s_hdc, hpen);
6492 y = FILL_Y(row + 1) - gui.char_height/2;
6493 MoveToEx(s_hdc, FILL_X(col), y, NULL);
6494 /* Note: LineTo() excludes the last pixel in the line. */
6495 LineTo(s_hdc, FILL_X(col + len), y);
6496 DeleteObject(SelectObject(s_hdc, old_pen));
6497 }
6498
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006499 /* Undercurl */
6500 if (flags & DRAW_UNDERC)
6501 {
6502 int x;
6503 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006504 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006505
6506 y = FILL_Y(row + 1) - 1;
6507 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6508 {
6509 offset = val[x % 8];
6510 SetPixel(s_hdc, x, y - offset, gui.currSpColor);
6511 }
6512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513}
6514
6515
6516/*
6517 * Output routines.
6518 */
6519
6520/* Flush any output to the screen */
6521 void
6522gui_mch_flush(void)
6523{
6524# if defined(__BORLANDC__)
6525 /*
6526 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6527 * prototype declaration.
6528 * The compiler complains if __stdcall is not used in both declarations.
6529 */
6530 BOOL __stdcall GdiFlush(void);
6531# endif
6532
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006533#if defined(FEAT_DIRECTX)
6534 if (IS_ENABLE_DIRECTX())
6535 DWriteContext_Flush(s_dwc);
6536#endif
6537
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 GdiFlush();
6539}
6540
6541 static void
6542clear_rect(RECT *rcp)
6543{
6544 HBRUSH hbr;
6545
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006546#if defined(FEAT_DIRECTX)
6547 if (IS_ENABLE_DIRECTX())
6548 {
6549 DWriteContext_FillRect(s_dwc, rcp, gui.back_pixel);
6550 return;
6551 }
6552#endif
6553
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 hbr = CreateSolidBrush(gui.back_pixel);
6555 FillRect(s_hdc, rcp, hbr);
6556 DeleteBrush(hbr);
6557}
6558
6559
Bram Moolenaarc716c302006-01-21 22:12:51 +00006560 void
6561gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6562{
6563 RECT workarea_rect;
6564
6565 get_work_area(&workarea_rect);
6566
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006567 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006568 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006569 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006570
6571 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6572 * the menubar for MSwin, we subtract it from the screen height, so that
6573 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006574 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006575 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006576 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006577 - GetSystemMetrics(SM_CYCAPTION)
6578#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006579 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006580#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006581 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006582}
6583
6584
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585#if defined(FEAT_MENU) || defined(PROTO)
6586/*
6587 * Add a sub menu to the menu bar.
6588 */
6589 void
6590gui_mch_add_menu(
6591 vimmenu_T *menu,
6592 int pos)
6593{
6594 vimmenu_T *parent = menu->parent;
6595
6596 menu->submenu_id = CreatePopupMenu();
6597 menu->id = s_menu_id++;
6598
6599 if (menu_is_menubar(menu->name))
6600 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601#ifdef FEAT_MBYTE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006602 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006604 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6605 {
6606 /* 'encoding' differs from active codepage: convert menu name
6607 * and use wide function */
6608 wn = enc_to_utf16(menu->name, NULL);
6609 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006611 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006613 infow.cbSize = sizeof(infow);
6614 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6615 | MIIM_SUBMENU;
6616 infow.dwItemData = (long_u)menu;
6617 infow.wID = menu->id;
6618 infow.fType = MFT_STRING;
6619 infow.dwTypeData = wn;
6620 infow.cch = (UINT)wcslen(wn);
6621 infow.hSubMenu = menu->submenu_id;
6622 InsertMenuItemW((parent == NULL)
6623 ? s_menuBar : parent->submenu_id,
6624 (UINT)pos, TRUE, &infow);
6625 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006629 if (wn == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630#endif
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006631 {
6632 MENUITEMINFO info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006634 info.cbSize = sizeof(info);
6635 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
6636 info.dwItemData = (long_u)menu;
6637 info.wID = menu->id;
6638 info.fType = MFT_STRING;
6639 info.dwTypeData = (LPTSTR)menu->name;
6640 info.cch = (UINT)STRLEN(menu->name);
6641 info.hSubMenu = menu->submenu_id;
6642 InsertMenuItem((parent == NULL)
6643 ? s_menuBar : parent->submenu_id,
6644 (UINT)pos, TRUE, &info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 }
6646 }
6647
6648 /* Fix window size if menu may have wrapped */
6649 if (parent == NULL)
6650 gui_mswin_get_menu_height(!gui.starting);
6651#ifdef FEAT_TEAROFF
6652 else if (IsWindow(parent->tearoff_handle))
6653 rebuild_tearoff(parent);
6654#endif
6655}
6656
6657 void
6658gui_mch_show_popupmenu(vimmenu_T *menu)
6659{
6660 POINT mp;
6661
6662 (void)GetCursorPos((LPPOINT)&mp);
6663 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6664}
6665
6666 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006667gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668{
6669 vimmenu_T *menu = gui_find_menu(path_name);
6670
6671 if (menu != NULL)
6672 {
6673 POINT p;
6674
6675 /* Find the position of the current cursor */
6676 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006677 if (mouse_pos)
6678 {
6679 int mx, my;
6680
6681 gui_mch_getmouse(&mx, &my);
6682 p.x += mx;
6683 p.y += my;
6684 }
6685 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006687 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6689 }
6690 msg_scroll = FALSE;
6691 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6692 }
6693}
6694
6695#if defined(FEAT_TEAROFF) || defined(PROTO)
6696/*
6697 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6698 * create it as a pseudo-"tearoff menu".
6699 */
6700 void
6701gui_make_tearoff(char_u *path_name)
6702{
6703 vimmenu_T *menu = gui_find_menu(path_name);
6704
6705 /* Found the menu, so tear it off. */
6706 if (menu != NULL)
6707 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6708}
6709#endif
6710
6711/*
6712 * Add a menu item to a menu
6713 */
6714 void
6715gui_mch_add_menu_item(
6716 vimmenu_T *menu,
6717 int idx)
6718{
6719 vimmenu_T *parent = menu->parent;
6720
6721 menu->id = s_menu_id++;
6722 menu->submenu_id = NULL;
6723
6724#ifdef FEAT_TEAROFF
6725 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6726 {
6727 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6728 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6729 }
6730 else
6731#endif
6732#ifdef FEAT_TOOLBAR
6733 if (menu_is_toolbar(parent->name))
6734 {
6735 TBBUTTON newtb;
6736
6737 vim_memset(&newtb, 0, sizeof(newtb));
6738 if (menu_is_separator(menu->name))
6739 {
6740 newtb.iBitmap = 0;
6741 newtb.fsStyle = TBSTYLE_SEP;
6742 }
6743 else
6744 {
6745 newtb.iBitmap = get_toolbar_bitmap(menu);
6746 newtb.fsStyle = TBSTYLE_BUTTON;
6747 }
6748 newtb.idCommand = menu->id;
6749 newtb.fsState = TBSTATE_ENABLED;
6750 newtb.iString = 0;
6751 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6752 (LPARAM)&newtb);
6753 menu->submenu_id = (HMENU)-1;
6754 }
6755 else
6756#endif
6757 {
6758#ifdef FEAT_MBYTE
6759 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760
6761 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6762 {
6763 /* 'encoding' differs from active codepage: convert menu item name
6764 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006765 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 if (wn != NULL)
6767 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006768 InsertMenuW(parent->submenu_id, (UINT)idx,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 (menu_is_separator(menu->name)
6770 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6771 (UINT)menu->id, wn);
6772 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 }
6774 }
6775 if (wn == NULL)
6776#endif
6777 InsertMenu(parent->submenu_id, (UINT)idx,
6778 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
6779 | MF_BYPOSITION,
6780 (UINT)menu->id, (LPCTSTR)menu->name);
6781#ifdef FEAT_TEAROFF
6782 if (IsWindow(parent->tearoff_handle))
6783 rebuild_tearoff(parent);
6784#endif
6785 }
6786}
6787
6788/*
6789 * Destroy the machine specific menu widget.
6790 */
6791 void
6792gui_mch_destroy_menu(vimmenu_T *menu)
6793{
6794#ifdef FEAT_TOOLBAR
6795 /*
6796 * is this a toolbar button?
6797 */
6798 if (menu->submenu_id == (HMENU)-1)
6799 {
6800 int iButton;
6801
6802 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6803 (WPARAM)menu->id, 0);
6804 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6805 }
6806 else
6807#endif
6808 {
6809 if (menu->parent != NULL
6810 && menu_is_popup(menu->parent->dname)
6811 && menu->parent->submenu_id != NULL)
6812 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6813 else
6814 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6815 if (menu->submenu_id != NULL)
6816 DestroyMenu(menu->submenu_id);
6817#ifdef FEAT_TEAROFF
6818 if (IsWindow(menu->tearoff_handle))
6819 DestroyWindow(menu->tearoff_handle);
6820 if (menu->parent != NULL
6821 && menu->parent->children != NULL
6822 && IsWindow(menu->parent->tearoff_handle))
6823 {
6824 /* This menu must not show up when rebuilding the tearoff window. */
6825 menu->modes = 0;
6826 rebuild_tearoff(menu->parent);
6827 }
6828#endif
6829 }
6830}
6831
6832#ifdef FEAT_TEAROFF
6833 static void
6834rebuild_tearoff(vimmenu_T *menu)
6835{
6836 /*hackish*/
6837 char_u tbuf[128];
6838 RECT trect;
6839 RECT rct;
6840 RECT roct;
6841 int x, y;
6842
6843 HWND thwnd = menu->tearoff_handle;
6844
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006845 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 if (GetWindowRect(thwnd, &trect)
6847 && GetWindowRect(s_hwnd, &rct)
6848 && GetClientRect(s_hwnd, &roct))
6849 {
6850 x = trect.left - rct.left;
6851 y = (trect.top - rct.bottom + roct.bottom);
6852 }
6853 else
6854 {
6855 x = y = 0xffffL;
6856 }
6857 DestroyWindow(thwnd);
6858 if (menu->children != NULL)
6859 {
6860 gui_mch_tearoff(tbuf, menu, x, y);
6861 if (IsWindow(menu->tearoff_handle))
6862 (void) SetWindowPos(menu->tearoff_handle,
6863 NULL,
6864 (int)trect.left,
6865 (int)trect.top,
6866 0, 0,
6867 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6868 }
6869}
6870#endif /* FEAT_TEAROFF */
6871
6872/*
6873 * Make a menu either grey or not grey.
6874 */
6875 void
6876gui_mch_menu_grey(
6877 vimmenu_T *menu,
6878 int grey)
6879{
6880#ifdef FEAT_TOOLBAR
6881 /*
6882 * is this a toolbar button?
6883 */
6884 if (menu->submenu_id == (HMENU)-1)
6885 {
6886 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6887 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6888 }
6889 else
6890#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006891 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6892 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893
6894#ifdef FEAT_TEAROFF
6895 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6896 {
6897 WORD menuID;
6898 HWND menuHandle;
6899
6900 /*
6901 * A tearoff button has changed state.
6902 */
6903 if (menu->children == NULL)
6904 menuID = (WORD)(menu->id);
6905 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006906 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6908 if (menuHandle)
6909 EnableWindow(menuHandle, !grey);
6910
6911 }
6912#endif
6913}
6914
6915#endif /* FEAT_MENU */
6916
6917
6918/* define some macros used to make the dialogue creation more readable */
6919
6920#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6921#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006922#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923
6924#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6925/*
6926 * stuff for dialogs
6927 */
6928
6929/*
6930 * The callback routine used by all the dialogs. Very simple. First,
6931 * acknowledges the INITDIALOG message so that Windows knows to do standard
6932 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6933 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6934 * number.
6935 */
6936 static LRESULT CALLBACK
6937dialog_callback(
6938 HWND hwnd,
6939 UINT message,
6940 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006941 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942{
6943 if (message == WM_INITDIALOG)
6944 {
6945 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
6946 /* Set focus to the dialog. Set the default button, if specified. */
6947 (void)SetFocus(hwnd);
6948 if (dialog_default_button > IDCANCEL)
6949 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006950 else
6951 /* We don't have a default, set focus on another element of the
6952 * dialog window, probably the icon */
6953 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 return FALSE;
6955 }
6956
6957 if (message == WM_COMMAND)
6958 {
6959 int button = LOWORD(wParam);
6960
6961 /* Don't end the dialog if something was selected that was
6962 * not a button.
6963 */
6964 if (button >= DLG_NONBUTTON_CONTROL)
6965 return TRUE;
6966
6967 /* If the edit box exists, copy the string. */
6968 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006969 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006970# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006971 /* If the OS is Windows NT, and 'encoding' differs from active
6972 * codepage: use wide function and convert text. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006973 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02006974 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006975 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
6976 char_u *p;
6977
6978 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6979 p = utf16_to_enc(wp, NULL);
6980 vim_strncpy(s_textfield, p, IOSIZE);
6981 vim_free(p);
6982 vim_free(wp);
6983 }
6984 else
6985# endif
6986 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006987 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989
6990 /*
6991 * Need to check for IDOK because if the user just hits Return to
6992 * accept the default value, some reason this is what we get.
6993 */
6994 if (button == IDOK)
6995 {
6996 if (dialog_default_button > IDCANCEL)
6997 EndDialog(hwnd, dialog_default_button);
6998 }
6999 else
7000 EndDialog(hwnd, button - IDCANCEL);
7001 return TRUE;
7002 }
7003
7004 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7005 {
7006 EndDialog(hwnd, 0);
7007 return TRUE;
7008 }
7009 return FALSE;
7010}
7011
7012/*
7013 * Create a dialog dynamically from the parameter strings.
7014 * type = type of dialog (question, alert, etc.)
7015 * title = dialog title. may be NULL for default title.
7016 * message = text to display. Dialog sizes to accommodate it.
7017 * buttons = '\n' separated list of button captions, default first.
7018 * dfltbutton = number of default button.
7019 *
7020 * This routine returns 1 if the first button is pressed,
7021 * 2 for the second, etc.
7022 *
7023 * 0 indicates Esc was pressed.
7024 * -1 for unexpected error
7025 *
7026 * If stubbing out this fn, return 1.
7027 */
7028
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007029static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030{
7031 "IDR_VIM",
7032 "IDR_VIM_ERROR",
7033 "IDR_VIM_ALERT",
7034 "IDR_VIM_INFO",
7035 "IDR_VIM_QUESTION"
7036};
7037
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 int
7039gui_mch_dialog(
7040 int type,
7041 char_u *title,
7042 char_u *message,
7043 char_u *buttons,
7044 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007045 char_u *textfield,
7046 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047{
7048 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007049 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 int numButtons;
7051 int *buttonWidths, *buttonPositions;
7052 int buttonYpos;
7053 int nchar, i;
7054 DWORD lStyle;
7055 int dlgwidth = 0;
7056 int dlgheight;
7057 int editboxheight;
7058 int horizWidth = 0;
7059 int msgheight;
7060 char_u *pstart;
7061 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007062 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 char_u *tbuffer;
7064 RECT rect;
7065 HWND hwnd;
7066 HDC hdc;
7067 HFONT font, oldFont;
7068 TEXTMETRIC fontInfo;
7069 int fontHeight;
7070 int textWidth, minButtonWidth, messageWidth;
7071 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007072 int maxDialogHeight;
7073 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 int vertical;
7075 int dlgPaddingX;
7076 int dlgPaddingY;
7077#ifdef USE_SYSMENU_FONT
7078 LOGFONT lfSysmenu;
7079 int use_lfSysmenu = FALSE;
7080#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007081 garray_T ga;
7082 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083
7084#ifndef NO_CONSOLE
7085 /* Don't output anything in silent mode ("ex -s") */
7086 if (silent_mode)
7087 return dfltbutton; /* return default option */
7088#endif
7089
Bram Moolenaar748bf032005-02-02 23:04:36 +00007090 if (s_hwnd == NULL)
7091 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092
7093 if ((type < 0) || (type > VIM_LAST_TYPE))
7094 type = 0;
7095
7096 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007097 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007098 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007099 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100
7101 if (p == NULL)
7102 return -1;
7103
7104 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007105 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7107 * const.
7108 */
7109 tbuffer = vim_strsave(buttons);
7110 if (tbuffer == NULL)
7111 return -1;
7112
7113 --dfltbutton; /* Change from one-based to zero-based */
7114
7115 /* Count buttons */
7116 numButtons = 1;
7117 for (i = 0; tbuffer[i] != '\0'; i++)
7118 {
7119 if (tbuffer[i] == DLG_BUTTON_SEP)
7120 numButtons++;
7121 }
7122 if (dfltbutton >= numButtons)
7123 dfltbutton = -1;
7124
7125 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007126 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 if (buttonWidths == NULL)
7128 return -1;
7129
7130 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007131 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 if (buttonPositions == NULL)
7133 return -1;
7134
7135 /*
7136 * Calculate how big the dialog must be.
7137 */
7138 hwnd = GetDesktopWindow();
7139 hdc = GetWindowDC(hwnd);
7140#ifdef USE_SYSMENU_FONT
7141 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7142 {
7143 font = CreateFontIndirect(&lfSysmenu);
7144 use_lfSysmenu = TRUE;
7145 }
7146 else
7147#endif
7148 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7149 VARIABLE_PITCH , DLG_FONT_NAME);
7150 if (s_usenewlook)
7151 {
7152 oldFont = SelectFont(hdc, font);
7153 dlgPaddingX = DLG_PADDING_X;
7154 dlgPaddingY = DLG_PADDING_Y;
7155 }
7156 else
7157 {
7158 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7159 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7160 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7161 }
7162 GetTextMetrics(hdc, &fontInfo);
7163 fontHeight = fontInfo.tmHeight;
7164
7165 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007166 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167
7168 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007169 if (s_hwnd == NULL)
7170 {
7171 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172
Bram Moolenaarc716c302006-01-21 22:12:51 +00007173 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007174 get_work_area(&workarea_rect);
7175 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7176 if (maxDialogWidth > 600)
7177 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007178 /* Leave some room for the taskbar. */
7179 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007180 }
7181 else
7182 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02007183 /* Use our own window for the size, unless it's very small. */
7184 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007185 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02007186 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007187 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007188 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
7189 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007190
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007191 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007192 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007193 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02007194 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007195 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
7196 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
7197 }
7198
7199 /* Set dlgwidth to width of message.
7200 * Copy the message into "ga", changing NL to CR-NL and inserting line
7201 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 pstart = message;
7203 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007204 msgheight = 0;
7205 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 do
7207 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007208 msgheight += fontHeight; /* at least one line */
7209
7210 /* Need to figure out where to break the string. The system does it
7211 * at a word boundary, which would mean we can't compute the number of
7212 * wrapped lines. */
7213 textWidth = 0;
7214 last_white = NULL;
7215 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007216 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007217#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007218 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007219#else
7220 l = 1;
7221#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01007222 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007223 && textWidth > maxDialogWidth * 3 / 4)
7224 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007225 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007226 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007227 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007228 /* Line will wrap. */
7229 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007230 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007231 textWidth = 0;
7232
7233 if (last_white != NULL)
7234 {
7235 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007236 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007237 pend = last_white + 1;
7238 last_white = NULL;
7239 }
7240 ga_append(&ga, '\r');
7241 ga_append(&ga, '\n');
7242 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007243 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007244
7245 while (--l >= 0)
7246 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007247 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007248 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007250
7251 ga_append(&ga, '\r');
7252 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 pstart = pend + 1;
7254 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007255
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007256 if (ga.ga_data != NULL)
7257 message = ga.ga_data;
7258
Bram Moolenaar748bf032005-02-02 23:04:36 +00007259 messageWidth += 10; /* roundoff space */
7260
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02007262 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7263 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264
7265 if (msgheight < DLG_ICON_HEIGHT)
7266 msgheight = DLG_ICON_HEIGHT;
7267
7268 /*
7269 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007270 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007272 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 if (!vertical)
7274 {
7275 // Place buttons horizontally if they fit.
7276 horizWidth = dlgPaddingX;
7277 pstart = tbuffer;
7278 i = 0;
7279 do
7280 {
7281 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7282 if (pend == NULL)
7283 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007284 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 if (textWidth < minButtonWidth)
7286 textWidth = minButtonWidth;
7287 textWidth += dlgPaddingX; /* Padding within button */
7288 buttonWidths[i] = textWidth;
7289 buttonPositions[i++] = horizWidth;
7290 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7291 pstart = pend + 1;
7292 } while (*pend != NUL);
7293
7294 if (horizWidth > maxDialogWidth)
7295 vertical = TRUE; // Too wide to fit on the screen.
7296 else if (horizWidth > dlgwidth)
7297 dlgwidth = horizWidth;
7298 }
7299
7300 if (vertical)
7301 {
7302 // Stack buttons vertically.
7303 pstart = tbuffer;
7304 do
7305 {
7306 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7307 if (pend == NULL)
7308 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007309 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 textWidth += dlgPaddingX; /* Padding within button */
7311 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7312 if (textWidth > dlgwidth)
7313 dlgwidth = textWidth;
7314 pstart = pend + 1;
7315 } while (*pend != NUL);
7316 }
7317
7318 if (dlgwidth < DLG_MIN_WIDTH)
7319 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7320
7321 /* start to fill in the dlgtemplate information. addressing by WORDs */
7322 if (s_usenewlook)
7323 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7324 else
7325 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7326
7327 add_long(lStyle);
7328 add_long(0); // (lExtendedStyle)
7329 pnumitems = p; /*save where the number of items must be stored*/
7330 add_word(0); // NumberOfItems(will change later)
7331 add_word(10); // x
7332 add_word(10); // y
7333 add_word(PixelToDialogX(dlgwidth)); // cx
7334
7335 // Dialog height.
7336 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007337 dlgheight = msgheight + 2 * dlgPaddingY
7338 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 else
7340 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7341
7342 // Dialog needs to be taller if contains an edit box.
7343 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7344 if (textfield != NULL)
7345 dlgheight += editboxheight;
7346
Bram Moolenaara95d8232013-08-07 15:27:11 +02007347 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7348 if (dlgheight > maxDialogHeight)
7349 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007350 msgheight = msgheight - (dlgheight - maxDialogHeight);
7351 dlgheight = maxDialogHeight;
7352 scroll_flag = WS_VSCROLL;
7353 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7354 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007355 }
7356
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 add_word(PixelToDialogY(dlgheight));
7358
7359 add_word(0); // Menu
7360 add_word(0); // Class
7361
7362 /* copy the title of the dialog */
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007363 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7364 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 p += nchar;
7366
7367 if (s_usenewlook)
7368 {
7369 /* do the font, since DS_3DLOOK doesn't work properly */
7370#ifdef USE_SYSMENU_FONT
7371 if (use_lfSysmenu)
7372 {
7373 /* point size */
7374 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7375 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007376 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 }
7378 else
7379#endif
7380 {
7381 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007382 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 }
7384 p += nchar;
7385 }
7386
7387 buttonYpos = msgheight + 2 * dlgPaddingY;
7388
7389 if (textfield != NULL)
7390 buttonYpos += editboxheight;
7391
7392 pstart = tbuffer;
7393 if (!vertical)
7394 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7395 for (i = 0; i < numButtons; i++)
7396 {
7397 /* get end of this button. */
7398 for ( pend = pstart;
7399 *pend && (*pend != DLG_BUTTON_SEP);
7400 pend++)
7401 ;
7402
7403 if (*pend)
7404 *pend = '\0';
7405
7406 /*
7407 * old NOTE:
7408 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7409 * the focus to the first tab-able button and in so doing makes that
7410 * the default!! Grrr. Workaround: Make the default button the only
7411 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7412 * he/she can use arrow keys.
7413 *
7414 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007415 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 * dialog. Also needed for when the textfield is the default control.
7417 * It appears to work now (perhaps not on Win95?).
7418 */
7419 if (vertical)
7420 {
7421 p = add_dialog_element(p,
7422 (i == dfltbutton
7423 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7424 PixelToDialogX(DLG_VERT_PADDING_X),
7425 PixelToDialogY(buttonYpos /* TBK */
7426 + 2 * fontHeight * i),
7427 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7428 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007429 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 }
7431 else
7432 {
7433 p = add_dialog_element(p,
7434 (i == dfltbutton
7435 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7436 PixelToDialogX(horizWidth + buttonPositions[i]),
7437 PixelToDialogY(buttonYpos), /* TBK */
7438 PixelToDialogX(buttonWidths[i]),
7439 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007440 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 }
7442 pstart = pend + 1; /*next button*/
7443 }
7444 *pnumitems += numButtons;
7445
7446 /* Vim icon */
7447 p = add_dialog_element(p, SS_ICON,
7448 PixelToDialogX(dlgPaddingX),
7449 PixelToDialogY(dlgPaddingY),
7450 PixelToDialogX(DLG_ICON_WIDTH),
7451 PixelToDialogY(DLG_ICON_HEIGHT),
7452 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7453 dlg_icons[type]);
7454
Bram Moolenaar748bf032005-02-02 23:04:36 +00007455 /* Dialog message */
7456 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7457 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7458 PixelToDialogY(dlgPaddingY),
7459 (WORD)(PixelToDialogX(messageWidth) + 1),
7460 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007461 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462
7463 /* Edit box */
7464 if (textfield != NULL)
7465 {
7466 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7467 PixelToDialogX(2 * dlgPaddingX),
7468 PixelToDialogY(2 * dlgPaddingY + msgheight),
7469 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7470 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007471 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 *pnumitems += 1;
7473 }
7474
7475 *pnumitems += 2;
7476
7477 SelectFont(hdc, oldFont);
7478 DeleteObject(font);
7479 ReleaseDC(hwnd, hdc);
7480
7481 /* Let the dialog_callback() function know which button to make default
7482 * If we have an edit box, make that the default. We also need to tell
7483 * dialog_callback() if this dialog contains an edit box or not. We do
7484 * this by setting s_textfield if it does.
7485 */
7486 if (textfield != NULL)
7487 {
7488 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7489 s_textfield = textfield;
7490 }
7491 else
7492 {
7493 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7494 s_textfield = NULL;
7495 }
7496
7497 /* show the dialog box modally and get a return value */
7498 nchar = (int)DialogBoxIndirect(
7499 s_hinst,
7500 (LPDLGTEMPLATE)pdlgtemplate,
7501 s_hwnd,
7502 (DLGPROC)dialog_callback);
7503
7504 LocalFree(LocalHandle(pdlgtemplate));
7505 vim_free(tbuffer);
7506 vim_free(buttonWidths);
7507 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007508 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509
7510 /* Focus back to our window (for when MDI is used). */
7511 (void)SetFocus(s_hwnd);
7512
7513 return nchar;
7514}
7515
7516#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007517
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518/*
7519 * Put a simple element (basic class) onto a dialog template in memory.
7520 * return a pointer to where the next item should be added.
7521 *
7522 * parameters:
7523 * lStyle = additional style flags
7524 * (be careful, NT3.51 & Win32s will ignore the new ones)
7525 * x,y = x & y positions IN DIALOG UNITS
7526 * w,h = width and height IN DIALOG UNITS
7527 * Id = ID used in messages
7528 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7529 * caption = usually text or resource name
7530 *
7531 * TODO: use the length information noted here to enable the dialog creation
7532 * routines to work out more exactly how much memory they need to alloc.
7533 */
7534 static PWORD
7535add_dialog_element(
7536 PWORD p,
7537 DWORD lStyle,
7538 WORD x,
7539 WORD y,
7540 WORD w,
7541 WORD h,
7542 WORD Id,
7543 WORD clss,
7544 const char *caption)
7545{
7546 int nchar;
7547
7548 p = lpwAlign(p); /* Align to dword boundary*/
7549 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7550 *p++ = LOWORD(lStyle);
7551 *p++ = HIWORD(lStyle);
7552 *p++ = 0; // LOWORD (lExtendedStyle)
7553 *p++ = 0; // HIWORD (lExtendedStyle)
7554 *p++ = x;
7555 *p++ = y;
7556 *p++ = w;
7557 *p++ = h;
7558 *p++ = Id; //9 or 10 words in all
7559
7560 *p++ = (WORD)0xffff;
7561 *p++ = clss; //2 more here
7562
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007563 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 p += nchar;
7565
7566 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7567
7568 return p; //total = 15+ (strlen(caption)) words
7569 // = 30 + 2(strlen(caption) bytes reqd
7570}
7571
7572
7573/*
7574 * Helper routine. Take an input pointer, return closest pointer that is
7575 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7576 */
7577 static LPWORD
7578lpwAlign(
7579 LPWORD lpIn)
7580{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007581 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007583 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 ul += 3;
7585 ul >>= 2;
7586 ul <<= 2;
7587 return (LPWORD)ul;
7588}
7589
7590/*
7591 * Helper routine. Takes second parameter as Ansi string, copies it to first
7592 * parameter as wide character (16-bits / char) string, and returns integer
7593 * number of wide characters (words) in string (including the trailing wide
7594 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007595 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7596 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 static int
7598nCopyAnsiToWideChar(
7599 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007600 LPSTR lpAnsiIn,
7601 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602{
7603 int nChar = 0;
7604#ifdef FEAT_MBYTE
7605 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7606 int i;
7607 WCHAR *wn;
7608
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007609 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 {
7611 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007612 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 if (wn != NULL)
7614 {
7615 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007616 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 vim_free(wn);
7618 }
7619 }
7620 if (nChar == 0)
7621 /* Use Win32 conversion function. */
7622 nChar = MultiByteToWideChar(
7623 enc_codepage > 0 ? enc_codepage : CP_ACP,
7624 MB_PRECOMPOSED,
7625 lpAnsiIn, len,
7626 lpWCStr, len);
7627 for (i = 0; i < nChar; ++i)
7628 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7629 lpWCStr[i] = (WORD)' ';
7630#else
7631 do
7632 {
7633 if (*lpAnsiIn == '\t')
7634 *lpWCStr++ = (WORD)' ';
7635 else
7636 *lpWCStr++ = (WORD)*lpAnsiIn;
7637 nChar++;
7638 } while (*lpAnsiIn++);
7639#endif
7640
7641 return nChar;
7642}
7643
7644
7645#ifdef FEAT_TEAROFF
7646/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007647 * Lookup menu handle from "menu_id".
7648 */
7649 static HMENU
7650tearoff_lookup_menuhandle(
7651 vimmenu_T *menu,
7652 WORD menu_id)
7653{
7654 for ( ; menu != NULL; menu = menu->next)
7655 {
7656 if (menu->modes == 0) /* this menu has just been deleted */
7657 continue;
7658 if (menu_is_separator(menu->dname))
7659 continue;
7660 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7661 return menu->submenu_id;
7662 }
7663 return NULL;
7664}
7665
7666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 * The callback function for all the modeless dialogs that make up the
7668 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7669 * thinking its menus have been clicked), and go away when closed.
7670 */
7671 static LRESULT CALLBACK
7672tearoff_callback(
7673 HWND hwnd,
7674 UINT message,
7675 WPARAM wParam,
7676 LPARAM lParam)
7677{
7678 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007679 {
7680 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683
7684 /* May show the mouse pointer again. */
7685 HandleMouseHide(message, lParam);
7686
7687 if (message == WM_COMMAND)
7688 {
7689 if ((WORD)(LOWORD(wParam)) & 0x8000)
7690 {
7691 POINT mp;
7692 RECT rect;
7693
7694 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7695 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007696 vimmenu_T *menu;
7697
7698 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007700 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7702 (int)rect.right - 8,
7703 (int)mp.y,
7704 (int)0, /*reserved param*/
7705 s_hwnd,
7706 NULL);
7707 /*
7708 * NOTE: The pop-up menu can eat the mouse up event.
7709 * We deal with this in normal.c.
7710 */
7711 }
7712 }
7713 else
7714 /* Pass on messages to the main Vim window */
7715 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7716 /*
7717 * Give main window the focus back: this is so after
7718 * choosing a tearoff button you can start typing again
7719 * straight away.
7720 */
7721 (void)SetFocus(s_hwnd);
7722 return TRUE;
7723 }
7724 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7725 {
7726 DestroyWindow(hwnd);
7727 return TRUE;
7728 }
7729
7730 /* When moved around, give main window the focus back. */
7731 if (message == WM_EXITSIZEMOVE)
7732 (void)SetActiveWindow(s_hwnd);
7733
7734 return FALSE;
7735}
7736#endif
7737
7738
7739/*
7740 * Decide whether to use the "new look" (small, non-bold font) or the "old
7741 * look" (big, clanky font) for dialogs, and work out a few values for use
7742 * later accordingly.
7743 */
7744 static void
7745get_dialog_font_metrics(void)
7746{
7747 HDC hdc;
7748 HFONT hfontTools = 0;
7749 DWORD dlgFontSize;
7750 SIZE size;
7751#ifdef USE_SYSMENU_FONT
7752 LOGFONT lfSysmenu;
7753#endif
7754
7755 s_usenewlook = FALSE;
7756
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007758 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7759 hfontTools = CreateFontIndirect(&lfSysmenu);
7760 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761#endif
7762 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7763 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7764
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007765 if (hfontTools)
7766 {
7767 hdc = GetDC(s_hwnd);
7768 SelectObject(hdc, hfontTools);
7769 /*
7770 * GetTextMetrics() doesn't return the right value in
7771 * tmAveCharWidth, so we have to figure out the dialog base units
7772 * ourselves.
7773 */
7774 GetTextExtentPoint(hdc,
7775 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7776 52, &size);
7777 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007779 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7780 s_dlgfntheight = (WORD)size.cy;
7781 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 }
7783
7784 if (!s_usenewlook)
7785 {
7786 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7787 s_dlgfntwidth = LOWORD(dlgFontSize);
7788 s_dlgfntheight = HIWORD(dlgFontSize);
7789 }
7790}
7791
7792#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7793/*
7794 * Create a pseudo-"tearoff menu" based on the child
7795 * items of a given menu pointer.
7796 */
7797 static void
7798gui_mch_tearoff(
7799 char_u *title,
7800 vimmenu_T *menu,
7801 int initX,
7802 int initY)
7803{
7804 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7805 int template_len;
7806 int nchar, textWidth, submenuWidth;
7807 DWORD lStyle;
7808 DWORD lExtendedStyle;
7809 WORD dlgwidth;
7810 WORD menuID;
7811 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007812 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 vimmenu_T *the_menu = menu;
7814 HWND hwnd;
7815 HDC hdc;
7816 HFONT font, oldFont;
7817 int col, spaceWidth, len;
7818 int columnWidths[2];
7819 char_u *label, *text;
7820 int acLen = 0;
7821 int nameLen;
7822 int padding0, padding1, padding2 = 0;
7823 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007824 int x;
7825 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826#ifdef USE_SYSMENU_FONT
7827 LOGFONT lfSysmenu;
7828 int use_lfSysmenu = FALSE;
7829#endif
7830
7831 /*
7832 * If this menu is already torn off, move it to the mouse position.
7833 */
7834 if (IsWindow(menu->tearoff_handle))
7835 {
7836 POINT mp;
7837 if (GetCursorPos((LPPOINT)&mp))
7838 {
7839 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7840 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7841 }
7842 return;
7843 }
7844
7845 /*
7846 * Create a new tearoff.
7847 */
7848 if (*title == MNU_HIDDEN_CHAR)
7849 title++;
7850
7851 /* Allocate memory to store the dialog template. It's made bigger when
7852 * needed. */
7853 template_len = DLG_ALLOC_SIZE;
7854 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7855 if (p == NULL)
7856 return;
7857
7858 hwnd = GetDesktopWindow();
7859 hdc = GetWindowDC(hwnd);
7860#ifdef USE_SYSMENU_FONT
7861 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7862 {
7863 font = CreateFontIndirect(&lfSysmenu);
7864 use_lfSysmenu = TRUE;
7865 }
7866 else
7867#endif
7868 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7869 VARIABLE_PITCH , DLG_FONT_NAME);
7870 if (s_usenewlook)
7871 oldFont = SelectFont(hdc, font);
7872 else
7873 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7874
7875 /* Calculate width of a single space. Used for padding columns to the
7876 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007877 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878
7879 /* Figure out max width of the text column, the accelerator column and the
7880 * optional submenu column. */
7881 submenuWidth = 0;
7882 for (col = 0; col < 2; col++)
7883 {
7884 columnWidths[col] = 0;
7885 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7886 {
7887 /* Use "dname" here to compute the width of the visible text. */
7888 text = (col == 0) ? pmenu->dname : pmenu->actext;
7889 if (text != NULL && *text != NUL)
7890 {
7891 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7892 if (textWidth > columnWidths[col])
7893 columnWidths[col] = textWidth;
7894 }
7895 if (pmenu->children != NULL)
7896 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7897 }
7898 }
7899 if (columnWidths[1] == 0)
7900 {
7901 /* no accelerators */
7902 if (submenuWidth != 0)
7903 columnWidths[0] += submenuWidth;
7904 else
7905 columnWidths[0] += spaceWidth;
7906 }
7907 else
7908 {
7909 /* there is an accelerator column */
7910 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7911 columnWidths[1] += submenuWidth;
7912 }
7913
7914 /*
7915 * Now find the total width of our 'menu'.
7916 */
7917 textWidth = columnWidths[0] + columnWidths[1];
7918 if (submenuWidth != 0)
7919 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007920 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7922 textWidth += submenuWidth;
7923 }
7924 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7925 if (textWidth > dlgwidth)
7926 dlgwidth = textWidth;
7927 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7928
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 /* start to fill in the dlgtemplate information. addressing by WORDs */
7930 if (s_usenewlook)
7931 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7932 else
7933 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7934
7935 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7936 *p++ = LOWORD(lStyle);
7937 *p++ = HIWORD(lStyle);
7938 *p++ = LOWORD(lExtendedStyle);
7939 *p++ = HIWORD(lExtendedStyle);
7940 pnumitems = p; /* save where the number of items must be stored */
7941 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007942 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007944 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 else
7946 *p++ = PixelToDialogX(initX); // x
7947 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007948 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 else
7950 *p++ = PixelToDialogY(initY); // y
7951 *p++ = PixelToDialogX(dlgwidth); // cx
7952 ptrueheight = p;
7953 *p++ = 0; // dialog height: changed later anyway
7954 *p++ = 0; // Menu
7955 *p++ = 0; // Class
7956
7957 /* copy the title of the dialog */
7958 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007959 ? (LPSTR)title
7960 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 p += nchar;
7962
7963 if (s_usenewlook)
7964 {
7965 /* do the font, since DS_3DLOOK doesn't work properly */
7966#ifdef USE_SYSMENU_FONT
7967 if (use_lfSysmenu)
7968 {
7969 /* point size */
7970 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7971 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007972 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 }
7974 else
7975#endif
7976 {
7977 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007978 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 }
7980 p += nchar;
7981 }
7982
7983 /*
7984 * Loop over all the items in the menu.
7985 * But skip over the tearbar.
7986 */
7987 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7988 menu = menu->children->next;
7989 else
7990 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007991 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 for ( ; menu != NULL; menu = menu->next)
7993 {
7994 if (menu->modes == 0) /* this menu has just been deleted */
7995 continue;
7996 if (menu_is_separator(menu->dname))
7997 {
7998 sepPadding += 3;
7999 continue;
8000 }
8001
8002 /* Check if there still is plenty of room in the template. Make it
8003 * larger when needed. */
8004 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8005 {
8006 WORD *newp;
8007
8008 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8009 if (newp != NULL)
8010 {
8011 template_len += 4096;
8012 mch_memmove(newp, pdlgtemplate,
8013 (char *)p - (char *)pdlgtemplate);
8014 p = newp + (p - pdlgtemplate);
8015 pnumitems = newp + (pnumitems - pdlgtemplate);
8016 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8017 LocalFree(LocalHandle(pdlgtemplate));
8018 pdlgtemplate = newp;
8019 }
8020 }
8021
8022 /* Figure out minimal length of this menu label. Use "name" for the
8023 * actual text, "dname" for estimating the displayed size. "name"
8024 * has "&a" for mnemonic and includes the accelerator. */
8025 len = nameLen = (int)STRLEN(menu->name);
8026 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8027 (int)STRLEN(menu->dname))) / spaceWidth;
8028 len += padding0;
8029
8030 if (menu->actext != NULL)
8031 {
8032 acLen = (int)STRLEN(menu->actext);
8033 len += acLen;
8034 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8035 }
8036 else
8037 textWidth = 0;
8038 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8039 len += padding1;
8040
8041 if (menu->children == NULL)
8042 {
8043 padding2 = submenuWidth / spaceWidth;
8044 len += padding2;
8045 menuID = (WORD)(menu->id);
8046 }
8047 else
8048 {
8049 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008050 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 }
8052
8053 /* Allocate menu label and fill it in */
8054 text = label = alloc((unsigned)len + 1);
8055 if (label == NULL)
8056 break;
8057
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008058 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 text = vim_strchr(text, TAB); /* stop at TAB before actext */
8060 if (text == NULL)
8061 text = label + nameLen; /* no actext, use whole name */
8062 while (padding0-- > 0)
8063 *text++ = ' ';
8064 if (menu->actext != NULL)
8065 {
8066 STRNCPY(text, menu->actext, acLen);
8067 text += acLen;
8068 }
8069 while (padding1-- > 0)
8070 *text++ = ' ';
8071 if (menu->children != NULL)
8072 {
8073 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8074 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8075 }
8076 else
8077 {
8078 while (padding2-- > 0)
8079 *text++ = ' ';
8080 }
8081 *text = NUL;
8082
8083 /*
8084 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8085 * W95/NT4 it makes the tear-off look more like a menu.
8086 */
8087 p = add_dialog_element(p,
8088 BS_PUSHBUTTON|BS_LEFT,
8089 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8090 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8091 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8092 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008093 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 vim_free(label);
8095 (*pnumitems)++;
8096 }
8097
8098 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8099
8100
8101 /* show modelessly */
Bram Moolenaar66857f42017-10-22 16:43:20 +02008102 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 s_hinst,
8104 (LPDLGTEMPLATE)pdlgtemplate,
8105 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02008106 (DLGPROC)tearoff_callback,
8107 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108
8109 LocalFree(LocalHandle(pdlgtemplate));
8110 SelectFont(hdc, oldFont);
8111 DeleteObject(font);
8112 ReleaseDC(hwnd, hdc);
8113
8114 /*
8115 * Reassert ourselves as the active window. This is so that after creating
8116 * a tearoff, the user doesn't have to click with the mouse just to start
8117 * typing again!
8118 */
8119 (void)SetActiveWindow(s_hwnd);
8120
8121 /* make sure the right buttons are enabled */
8122 force_menu_update = TRUE;
8123}
8124#endif
8125
8126#if defined(FEAT_TOOLBAR) || defined(PROTO)
8127#include "gui_w32_rc.h"
8128
8129/* This not defined in older SDKs */
8130# ifndef TBSTYLE_FLAT
8131# define TBSTYLE_FLAT 0x0800
8132# endif
8133
8134/*
8135 * Create the toolbar, initially unpopulated.
8136 * (just like the menu, there are no defaults, it's all
8137 * set up through menu.vim)
8138 */
8139 static void
8140initialise_toolbar(void)
8141{
8142 InitCommonControls();
8143 s_toolbarhwnd = CreateToolbarEx(
8144 s_hwnd,
8145 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8146 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008147 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 s_hinst,
8149 IDR_TOOLBAR1, // id of initial bitmap
8150 NULL,
8151 0, // initial number of buttons
8152 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8153 TOOLBAR_BUTTON_HEIGHT,
8154 TOOLBAR_BUTTON_WIDTH,
8155 TOOLBAR_BUTTON_HEIGHT,
8156 sizeof(TBBUTTON)
8157 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008158 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159
8160 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
8161}
8162
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008163 static LRESULT CALLBACK
8164toolbar_wndproc(
8165 HWND hwnd,
8166 UINT uMsg,
8167 WPARAM wParam,
8168 LPARAM lParam)
8169{
8170 HandleMouseHide(uMsg, lParam);
8171 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8172}
8173
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 static int
8175get_toolbar_bitmap(vimmenu_T *menu)
8176{
8177 int i = -1;
8178
8179 /*
8180 * Check user bitmaps first, unless builtin is specified.
8181 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008182 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {
8184 char_u fname[MAXPATHL];
8185 HANDLE hbitmap = NULL;
8186
8187 if (menu->iconfile != NULL)
8188 {
8189 gui_find_iconfile(menu->iconfile, fname, "bmp");
8190 hbitmap = LoadImage(
8191 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008192 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 IMAGE_BITMAP,
8194 TOOLBAR_BUTTON_WIDTH,
8195 TOOLBAR_BUTTON_HEIGHT,
8196 LR_LOADFROMFILE |
8197 LR_LOADMAP3DCOLORS
8198 );
8199 }
8200
8201 /*
8202 * If the LoadImage call failed, or the "icon=" file
8203 * didn't exist or wasn't specified, try the menu name
8204 */
8205 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008206 && (gui_find_bitmap(
8207#ifdef FEAT_MULTI_LANG
8208 menu->en_dname != NULL ? menu->en_dname :
8209#endif
8210 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 hbitmap = LoadImage(
8212 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008213 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 IMAGE_BITMAP,
8215 TOOLBAR_BUTTON_WIDTH,
8216 TOOLBAR_BUTTON_HEIGHT,
8217 LR_LOADFROMFILE |
8218 LR_LOADMAP3DCOLORS
8219 );
8220
8221 if (hbitmap != NULL)
8222 {
8223 TBADDBITMAP tbAddBitmap;
8224
8225 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008226 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227
8228 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8229 (WPARAM)1, (LPARAM)&tbAddBitmap);
8230 /* i will be set to -1 if it fails */
8231 }
8232 }
8233 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8234 i = menu->iconidx;
8235
8236 return i;
8237}
8238#endif
8239
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008240#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8241 static void
8242initialise_tabline(void)
8243{
8244 InitCommonControls();
8245
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008246 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008247 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008248 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8249 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008250 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008251
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008252 gui.tabline_height = TABLINE_HEIGHT;
8253
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008254# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008255 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008256# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008257}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008258
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008259/*
8260 * Get tabpage_T from POINT.
8261 */
8262 static tabpage_T *
8263GetTabFromPoint(
8264 HWND hWnd,
8265 POINT pt)
8266{
8267 tabpage_T *ptp = NULL;
8268
8269 if (gui_mch_showing_tabline())
8270 {
8271 TCHITTESTINFO htinfo;
8272 htinfo.pt = pt;
8273 /* ignore if a window under cusor is not tabcontrol. */
8274 if (s_tabhwnd == hWnd)
8275 {
8276 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8277 if (idx != -1)
8278 ptp = find_tabpage(idx + 1);
8279 }
8280 }
8281 return ptp;
8282}
8283
8284static POINT s_pt = {0, 0};
8285static HCURSOR s_hCursor = NULL;
8286
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008287 static LRESULT CALLBACK
8288tabline_wndproc(
8289 HWND hwnd,
8290 UINT uMsg,
8291 WPARAM wParam,
8292 LPARAM lParam)
8293{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008294 POINT pt;
8295 tabpage_T *tp;
8296 RECT rect;
8297 int nCenter;
8298 int idx0;
8299 int idx1;
8300
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008301 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008302
8303 switch (uMsg)
8304 {
8305 case WM_LBUTTONDOWN:
8306 {
8307 s_pt.x = GET_X_LPARAM(lParam);
8308 s_pt.y = GET_Y_LPARAM(lParam);
8309 SetCapture(hwnd);
8310 s_hCursor = GetCursor(); /* backup default cursor */
8311 break;
8312 }
8313 case WM_MOUSEMOVE:
8314 if (GetCapture() == hwnd
8315 && ((wParam & MK_LBUTTON)) != 0)
8316 {
8317 pt.x = GET_X_LPARAM(lParam);
8318 pt.y = s_pt.y;
8319 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8320 {
8321 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8322
8323 tp = GetTabFromPoint(hwnd, pt);
8324 if (tp != NULL)
8325 {
8326 idx0 = tabpage_index(curtab) - 1;
8327 idx1 = tabpage_index(tp) - 1;
8328
8329 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8330 nCenter = rect.left + (rect.right - rect.left) / 2;
8331
8332 /* Check if the mouse cursor goes over the center of
8333 * the next tab to prevent "flickering". */
8334 if ((idx0 < idx1) && (nCenter < pt.x))
8335 {
8336 tabpage_move(idx1 + 1);
8337 update_screen(0);
8338 }
8339 else if ((idx1 < idx0) && (pt.x < nCenter))
8340 {
8341 tabpage_move(idx1);
8342 update_screen(0);
8343 }
8344 }
8345 }
8346 }
8347 break;
8348 case WM_LBUTTONUP:
8349 {
8350 if (GetCapture() == hwnd)
8351 {
8352 SetCursor(s_hCursor);
8353 ReleaseCapture();
8354 }
8355 break;
8356 }
8357 default:
8358 break;
8359 }
8360
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008361 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8362}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008363#endif
8364
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8366/*
8367 * Make the GUI window come to the foreground.
8368 */
8369 void
8370gui_mch_set_foreground(void)
8371{
8372 if (IsIconic(s_hwnd))
8373 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8374 SetForegroundWindow(s_hwnd);
8375}
8376#endif
8377
8378#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8379 static void
8380dyn_imm_load(void)
8381{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008382 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 if (hLibImm == NULL)
8384 return;
8385
8386 pImmGetCompositionStringA
8387 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8388 pImmGetCompositionStringW
8389 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8390 pImmGetContext
8391 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8392 pImmAssociateContext
8393 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8394 pImmReleaseContext
8395 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8396 pImmGetOpenStatus
8397 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8398 pImmSetOpenStatus
8399 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
8400 pImmGetCompositionFont
8401 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
8402 pImmSetCompositionFont
8403 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
8404 pImmSetCompositionWindow
8405 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8406 pImmGetConversionStatus
8407 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008408 pImmSetConversionStatus
8409 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410
8411 if ( pImmGetCompositionStringA == NULL
8412 || pImmGetCompositionStringW == NULL
8413 || pImmGetContext == NULL
8414 || pImmAssociateContext == NULL
8415 || pImmReleaseContext == NULL
8416 || pImmGetOpenStatus == NULL
8417 || pImmSetOpenStatus == NULL
8418 || pImmGetCompositionFont == NULL
8419 || pImmSetCompositionFont == NULL
8420 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008421 || pImmGetConversionStatus == NULL
8422 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 {
8424 FreeLibrary(hLibImm);
8425 hLibImm = NULL;
8426 pImmGetContext = NULL;
8427 return;
8428 }
8429
8430 return;
8431}
8432
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433#endif
8434
8435#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8436
8437# ifdef FEAT_XPM_W32
8438# define IMAGE_XPM 100
8439# endif
8440
8441typedef struct _signicon_t
8442{
8443 HANDLE hImage;
8444 UINT uType;
8445#ifdef FEAT_XPM_W32
8446 HANDLE hShape; /* Mask bitmap handle */
8447#endif
8448} signicon_t;
8449
8450 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008451gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452{
8453 signicon_t *sign;
8454 int x, y, w, h;
8455
8456 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8457 return;
8458
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008459#if defined(FEAT_DIRECTX)
8460 if (IS_ENABLE_DIRECTX())
8461 DWriteContext_Flush(s_dwc);
8462#endif
8463
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 x = TEXT_X(col);
8465 y = TEXT_Y(row);
8466 w = gui.char_width * 2;
8467 h = gui.char_height;
8468 switch (sign->uType)
8469 {
8470 case IMAGE_BITMAP:
8471 {
8472 HDC hdcMem;
8473 HBITMAP hbmpOld;
8474
8475 hdcMem = CreateCompatibleDC(s_hdc);
8476 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8477 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8478 SelectObject(hdcMem, hbmpOld);
8479 DeleteDC(hdcMem);
8480 }
8481 break;
8482 case IMAGE_ICON:
8483 case IMAGE_CURSOR:
8484 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8485 break;
8486#ifdef FEAT_XPM_W32
8487 case IMAGE_XPM:
8488 {
8489 HDC hdcMem;
8490 HBITMAP hbmpOld;
8491
8492 hdcMem = CreateCompatibleDC(s_hdc);
8493 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8494 /* Make hole */
8495 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8496
8497 SelectObject(hdcMem, sign->hImage);
8498 /* Paint sign */
8499 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8500 SelectObject(hdcMem, hbmpOld);
8501 DeleteDC(hdcMem);
8502 }
8503 break;
8504#endif
8505 }
8506}
8507
8508 static void
8509close_signicon_image(signicon_t *sign)
8510{
8511 if (sign)
8512 switch (sign->uType)
8513 {
8514 case IMAGE_BITMAP:
8515 DeleteObject((HGDIOBJ)sign->hImage);
8516 break;
8517 case IMAGE_CURSOR:
8518 DestroyCursor((HCURSOR)sign->hImage);
8519 break;
8520 case IMAGE_ICON:
8521 DestroyIcon((HICON)sign->hImage);
8522 break;
8523#ifdef FEAT_XPM_W32
8524 case IMAGE_XPM:
8525 DeleteObject((HBITMAP)sign->hImage);
8526 DeleteObject((HBITMAP)sign->hShape);
8527 break;
8528#endif
8529 }
8530}
8531
8532 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008533gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534{
8535 signicon_t sign, *psign;
8536 char_u *ext;
8537
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008539 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 if (ext > signfile)
8541 {
8542 int do_load = 1;
8543
8544 if (!STRICMP(ext, ".bmp"))
8545 sign.uType = IMAGE_BITMAP;
8546 else if (!STRICMP(ext, ".ico"))
8547 sign.uType = IMAGE_ICON;
8548 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8549 sign.uType = IMAGE_CURSOR;
8550 else
8551 do_load = 0;
8552
8553 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008554 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 gui.char_width * 2, gui.char_height,
8556 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8557#ifdef FEAT_XPM_W32
8558 if (!STRICMP(ext, ".xpm"))
8559 {
8560 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008561 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8562 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 }
8564#endif
8565 }
8566
8567 psign = NULL;
8568 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8569 != NULL)
8570 *psign = sign;
8571
8572 if (!psign)
8573 {
8574 if (sign.hImage)
8575 close_signicon_image(&sign);
8576 EMSG(_(e_signdata));
8577 }
8578 return (void *)psign;
8579
8580}
8581
8582 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008583gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584{
8585 if (sign)
8586 {
8587 close_signicon_image((signicon_t *)sign);
8588 vim_free(sign);
8589 }
8590}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008593#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594
8595/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008596 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008598 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8600 * to get current mouse position).
8601 *
8602 * Trying to use as more Windows services as possible, and as less
8603 * IE version as possible :)).
8604 *
8605 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8606 * BalloonEval struct.
8607 * 2) Enable/Disable simply create/kill BalloonEval Timer
8608 * 3) When there was enough inactivity, timer procedure posts
8609 * async request to debugger
8610 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8611 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008612 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 */
8614
Bram Moolenaar45360022005-07-21 21:08:21 +00008615/*
8616 * determine whether installed Common Controls support multiline tooltips
8617 * (i.e. their version is >= 4.70
8618 */
8619 int
8620multiline_balloon_available(void)
8621{
8622 HINSTANCE hDll;
8623 static char comctl_dll[] = "comctl32.dll";
8624 static int multiline_tip = MAYBE;
8625
8626 if (multiline_tip != MAYBE)
8627 return multiline_tip;
8628
8629 hDll = GetModuleHandle(comctl_dll);
8630 if (hDll != NULL)
8631 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008632 DLLGETVERSIONPROC pGetVer;
8633 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008634
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008635 if (pGetVer != NULL)
8636 {
8637 DLLVERSIONINFO dvi;
8638 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008639
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008640 ZeroMemory(&dvi, sizeof(dvi));
8641 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008642
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008643 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008644
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008645 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008646 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008647 || (dvi.dwMajorVersion == 4
8648 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008649 {
8650 multiline_tip = TRUE;
8651 return multiline_tip;
8652 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008653 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008654 else
8655 {
8656 /* there is chance we have ancient CommCtl 4.70
8657 which doesn't export DllGetVersion */
8658 DWORD dwHandle = 0;
8659 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8660 if (len > 0)
8661 {
8662 VS_FIXEDFILEINFO *ver;
8663 UINT vlen = 0;
8664 void *data = alloc(len);
8665
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008666 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008667 && GetFileVersionInfo(comctl_dll, 0, len, data)
8668 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8669 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008670 && HIWORD(ver->dwFileVersionMS) > 4)
8671 || ((HIWORD(ver->dwFileVersionMS) == 4
8672 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008673 {
8674 vim_free(data);
8675 multiline_tip = TRUE;
8676 return multiline_tip;
8677 }
8678 vim_free(data);
8679 }
8680 }
8681 }
8682 multiline_tip = FALSE;
8683 return multiline_tip;
8684}
8685
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008687make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688{
Bram Moolenaar45360022005-07-21 21:08:21 +00008689 TOOLINFO *pti;
8690 int ToolInfoSize;
8691
8692 if (multiline_balloon_available() == TRUE)
8693 ToolInfoSize = sizeof(TOOLINFO_NEW);
8694 else
8695 ToolInfoSize = sizeof(TOOLINFO);
8696
8697 pti = (TOOLINFO *)alloc(ToolInfoSize);
8698 if (pti == NULL)
8699 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700
8701 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
8702 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8703 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8704 beval->target, NULL, s_hinst, NULL);
8705
8706 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8707 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8708
Bram Moolenaar45360022005-07-21 21:08:21 +00008709 pti->cbSize = ToolInfoSize;
8710 pti->uFlags = TTF_SUBCLASS;
8711 pti->hwnd = beval->target;
8712 pti->hinst = 0; /* Don't use string resources */
8713 pti->uId = ID_BEVAL_TOOLTIP;
8714
8715 if (multiline_balloon_available() == TRUE)
8716 {
8717 RECT rect;
8718 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
8719 pti->lpszText = LPSTR_TEXTCALLBACK;
8720 ptin->lParam = (LPARAM)text;
8721 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
8722 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8723 (LPARAM)rect.right);
8724 }
8725 else
8726 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727
8728 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00008729 pti->rect.left = pt.x - 3;
8730 pti->rect.top = pt.y - 3;
8731 pti->rect.right = pt.x + 3;
8732 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733
Bram Moolenaar45360022005-07-21 21:08:21 +00008734 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 /* Make tooltip appear sooner */
8736 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008737 /* I've performed some tests and it seems the longest possible life time
8738 * of tooltip is 30 seconds */
8739 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 /*
8741 * HACK: force tooltip to appear, because it'll not appear until
8742 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008743 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008745 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00008747 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748}
8749
8750 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008751delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008753 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754}
8755
8756 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008757BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008758 HWND hwnd UNUSED,
8759 UINT uMsg UNUSED,
8760 UINT_PTR idEvent UNUSED,
8761 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762{
8763 POINT pt;
8764 RECT rect;
8765
8766 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8767 return;
8768
8769 GetCursorPos(&pt);
8770 if (WindowFromPoint(pt) != s_textArea)
8771 return;
8772
8773 ScreenToClient(s_textArea, &pt);
8774 GetClientRect(s_textArea, &rect);
8775 if (!PtInRect(&rect, pt))
8776 return;
8777
8778 if (LastActivity > 0
8779 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8780 && (cur_beval->showState != ShS_PENDING
8781 || abs(cur_beval->x - pt.x) > 3
8782 || abs(cur_beval->y - pt.y) > 3))
8783 {
8784 /* Pointer resting in one place long enough, it's time to show
8785 * the tooltip. */
8786 cur_beval->showState = ShS_PENDING;
8787 cur_beval->x = pt.x;
8788 cur_beval->y = pt.y;
8789
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008790 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791
8792 if (cur_beval->msgCB != NULL)
8793 (*cur_beval->msgCB)(cur_beval, 0);
8794 }
8795}
8796
8797 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008798gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008800 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008802 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803}
8804
8805 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008806gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008808 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 if (beval == NULL)
8810 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008811 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008812 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008813 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814}
8815
8816 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008817gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818{
8819 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008820
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008821 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 if (beval->showState == ShS_SHOWING)
8823 return;
8824 GetCursorPos(&pt);
8825 ScreenToClient(s_textArea, &pt);
8826
8827 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008829 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 gui_mch_disable_beval_area(cur_beval);
8831 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008832 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008834 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835}
8836
8837 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008838gui_mch_create_beval_area(
8839 void *target, /* ignored, always use s_textArea */
8840 char_u *mesg,
8841 void (*mesgCB)(BalloonEval *, int),
8842 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843{
8844 /* partially stolen from gui_beval.c */
8845 BalloonEval *beval;
8846
8847 if (mesg != NULL && mesgCB != NULL)
8848 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01008849 IEMSG(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 return NULL;
8851 }
8852
8853 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
8854 if (beval != NULL)
8855 {
8856 beval->target = s_textArea;
8857 beval->balloon = NULL;
8858
8859 beval->showState = ShS_NEUTRAL;
8860 beval->x = 0;
8861 beval->y = 0;
8862 beval->msg = mesg;
8863 beval->msgCB = mesgCB;
8864 beval->clientData = clientData;
8865
8866 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 cur_beval = beval;
8868
8869 if (p_beval)
8870 gui_mch_enable_beval_area(beval);
8871
8872 }
8873 return beval;
8874}
8875
8876 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008877Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878{
8879 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8880 return;
8881
8882 if (cur_beval != NULL)
8883 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008884 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008886 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008887 // TRACE0("TTN_SHOW {{{");
8888 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008889 break;
8890 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008891 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 delete_tooltip(cur_beval);
8893 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008894 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895
8896 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008897 break;
8898 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008899 {
8900 /* if you get there then we have new common controls */
8901 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8902 info->lpszText = (LPSTR)info->lParam;
8903 info->uFlags |= TTF_DI_SETITEM;
8904 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008905 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 }
8907 }
8908}
8909
8910 static void
8911TrackUserActivity(UINT uMsg)
8912{
8913 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8914 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8915 LastActivity = GetTickCount();
8916}
8917
8918 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008919gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920{
8921 vim_free(beval);
8922}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008923#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924
8925#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8926/*
8927 * We have multiple signs to draw at the same location. Draw the
8928 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8929 */
8930 void
8931netbeans_draw_multisign_indicator(int row)
8932{
8933 int i;
8934 int y;
8935 int x;
8936
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008937 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008938 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008939
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 x = 0;
8941 y = TEXT_Y(row);
8942
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008943#if defined(FEAT_DIRECTX)
8944 if (IS_ENABLE_DIRECTX())
8945 DWriteContext_Flush(s_dwc);
8946#endif
8947
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 for (i = 0; i < gui.char_height - 3; i++)
8949 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8950
8951 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8952 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8953 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8954 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8955 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8956 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8957 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8958}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008959#endif