blob: fc566abee931fda0e3ebaed0b02b0674c2a94378 [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 Moolenaarb8e0bdb2014-11-12 16:10:48 +010037#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020038
Bram Moolenaar065bbac2016-02-20 13:08:46 +010039#ifdef FEAT_MENU
40static int gui_mswin_get_menu_height(int fix_window);
41#endif
42
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010043#if defined(FEAT_DIRECTX) || defined(PROTO)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020044 int
45directx_enabled(void)
46{
47 if (s_dwc != NULL)
48 return 1;
49 else if (s_directx_load_attempted)
50 return 0;
51 /* load DirectX */
52 DWrite_Init();
53 s_directx_load_attempted = 1;
54 s_dwc = DWriteContext_Open();
55 return s_dwc != NULL ? 1 : 0;
56}
57#endif
58
59#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
60 int
61gui_mch_set_rendering_options(char_u *s)
62{
63#ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020064 char_u *p, *q;
65
66 int dx_enable = 0;
67 int dx_flags = 0;
68 float dx_gamma = 0.0f;
69 float dx_contrast = 0.0f;
70 float dx_level = 0.0f;
71 int dx_geom = 0;
72 int dx_renmode = 0;
73 int dx_taamode = 0;
74
75 /* parse string as rendering options. */
76 for (p = s; p != NULL && *p != NUL; )
77 {
78 char_u item[256];
79 char_u name[128];
80 char_u value[128];
81
Bram Moolenaarcde88542015-08-11 19:14:00 +020082 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020083 if (p == NULL)
84 break;
85 q = &item[0];
86 copy_option_part(&q, name, sizeof(name), ":");
87 if (q == NULL)
88 return FAIL;
89 copy_option_part(&q, value, sizeof(value), ":");
90
91 if (STRCMP(name, "type") == 0)
92 {
93 if (STRCMP(value, "directx") == 0)
94 dx_enable = 1;
95 else
96 return FAIL;
97 }
98 else if (STRCMP(name, "gamma") == 0)
99 {
100 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100101 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200102 }
103 else if (STRCMP(name, "contrast") == 0)
104 {
105 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100106 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200107 }
108 else if (STRCMP(name, "level") == 0)
109 {
110 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100111 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200112 }
113 else if (STRCMP(name, "geom") == 0)
114 {
115 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100116 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200117 if (dx_geom < 0 || dx_geom > 2)
118 return FAIL;
119 }
120 else if (STRCMP(name, "renmode") == 0)
121 {
122 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100123 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200124 if (dx_renmode < 0 || dx_renmode > 6)
125 return FAIL;
126 }
127 else if (STRCMP(name, "taamode") == 0)
128 {
129 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100130 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200131 if (dx_taamode < 0 || dx_taamode > 3)
132 return FAIL;
133 }
134 else
135 return FAIL;
136 }
137
138 /* Enable DirectX/DirectWrite */
139 if (dx_enable)
140 {
141 if (!directx_enabled())
142 return FAIL;
143 DWriteContext_SetRenderingParams(s_dwc, NULL);
144 if (dx_flags)
145 {
146 DWriteRenderingParams param;
147 DWriteContext_GetRenderingParams(s_dwc, &param);
148 if (dx_flags & (1 << 0))
149 param.gamma = dx_gamma;
150 if (dx_flags & (1 << 1))
151 param.enhancedContrast = dx_contrast;
152 if (dx_flags & (1 << 2))
153 param.clearTypeLevel = dx_level;
154 if (dx_flags & (1 << 3))
155 param.pixelGeometry = dx_geom;
156 if (dx_flags & (1 << 4))
157 param.renderingMode = dx_renmode;
158 if (dx_flags & (1 << 5))
159 param.textAntialiasMode = dx_taamode;
160 DWriteContext_SetRenderingParams(s_dwc, &param);
161 }
162 }
163 s_directx_enabled = dx_enable;
164
165 return OK;
166#else
167 return FAIL;
168#endif
169}
170#endif
171
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172/*
173 * These are new in Windows ME/XP, only defined in recent compilers.
174 */
175#ifndef HANDLE_WM_XBUTTONUP
176# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
177 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
178#endif
179#ifndef HANDLE_WM_XBUTTONDOWN
180# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
181 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
182#endif
183#ifndef HANDLE_WM_XBUTTONDBLCLK
184# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
185 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
186#endif
187
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100188
189#include "version.h" /* used by dialog box routine for default title */
190#ifdef DEBUG
191# include <tchar.h>
192#endif
193
194/* cproto fails on missing include files */
195#ifndef PROTO
196
197#ifndef __MINGW32__
198# include <shellapi.h>
199#endif
200#if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL) || defined(FEAT_GUI_TABLINE)
201# include <commctrl.h>
202#endif
203#include <windowsx.h>
204
205#ifdef GLOBAL_IME
206# include "glbl_ime.h"
207#endif
208
209#endif /* PROTO */
210
211#ifdef FEAT_MENU
212# define MENUHINTS /* show menu hints in command line */
213#endif
214
215/* Some parameters for dialog boxes. All in pixels. */
216#define DLG_PADDING_X 10
217#define DLG_PADDING_Y 10
218#define DLG_OLD_STYLE_PADDING_X 5
219#define DLG_OLD_STYLE_PADDING_Y 5
220#define DLG_VERT_PADDING_X 4 /* For vertical buttons */
221#define DLG_VERT_PADDING_Y 4
222#define DLG_ICON_WIDTH 34
223#define DLG_ICON_HEIGHT 34
224#define DLG_MIN_WIDTH 150
225#define DLG_FONT_NAME "MS Sans Serif"
226#define DLG_FONT_POINT_SIZE 8
227#define DLG_MIN_MAX_WIDTH 400
228#define DLG_MIN_MAX_HEIGHT 400
229
230#define DLG_NONBUTTON_CONTROL 5000 /* First ID of non-button controls */
231
232#ifndef WM_XBUTTONDOWN /* For Win2K / winME ONLY */
233# define WM_XBUTTONDOWN 0x020B
234# define WM_XBUTTONUP 0x020C
235# define WM_XBUTTONDBLCLK 0x020D
236# define MK_XBUTTON1 0x0020
237# define MK_XBUTTON2 0x0040
238#endif
239
240#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100242 * Define a few things for generating prototypes. This is just to avoid
243 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100245# define APIENTRY
246# define CALLBACK
247# define CONST
248# define FAR
249# define NEAR
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200250# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100251# define _cdecl
252typedef int BOOL;
253typedef int BYTE;
254typedef int DWORD;
255typedef int WCHAR;
256typedef int ENUMLOGFONT;
257typedef int FINDREPLACE;
258typedef int HANDLE;
259typedef int HBITMAP;
260typedef int HBRUSH;
261typedef int HDROP;
262typedef int INT;
263typedef int LOGFONT[];
264typedef int LPARAM;
265typedef int LPCREATESTRUCT;
266typedef int LPCSTR;
267typedef int LPCTSTR;
268typedef int LPRECT;
269typedef int LPSTR;
270typedef int LPWINDOWPOS;
271typedef int LPWORD;
272typedef int LRESULT;
273typedef int HRESULT;
274# undef MSG
275typedef int MSG;
276typedef int NEWTEXTMETRIC;
277typedef int OSVERSIONINFO;
278typedef int PWORD;
279typedef int RECT;
280typedef int UINT;
281typedef int WORD;
282typedef int WPARAM;
283typedef int POINT;
284typedef void *HINSTANCE;
285typedef void *HMENU;
286typedef void *HWND;
287typedef void *HDC;
288typedef void VOID;
289typedef int LPNMHDR;
290typedef int LONG;
291typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200292typedef int UINT_PTR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100293#endif
294
295#ifndef GET_X_LPARAM
296# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
297#endif
298
299static void _OnPaint( HWND hwnd);
300static void clear_rect(RECT *rcp);
301
302static WORD s_dlgfntheight; /* height of the dialog font */
303static WORD s_dlgfntwidth; /* width of the dialog font */
304
305#ifdef FEAT_MENU
306static HMENU s_menuBar = NULL;
307#endif
308#ifdef FEAT_TEAROFF
309static void rebuild_tearoff(vimmenu_T *menu);
310static HBITMAP s_htearbitmap; /* bitmap used to indicate tearoff */
311#endif
312
313/* Flag that is set while processing a message that must not be interrupted by
314 * processing another message. */
315static int s_busy_processing = FALSE;
316
317static int destroying = FALSE; /* call DestroyWindow() ourselves */
318
319#ifdef MSWIN_FIND_REPLACE
320static UINT s_findrep_msg = 0; /* set in gui_w[16/32].c */
321static FINDREPLACE s_findrep_struct;
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200322# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100323static FINDREPLACEW s_findrep_struct_w;
324# endif
325static HWND s_findrep_hwnd = NULL;
326static int s_findrep_is_find; /* TRUE for find dialog, FALSE
327 for find/replace dialog */
328#endif
329
330static HINSTANCE s_hinst = NULL;
Bram Moolenaar85b11762016-02-27 18:13:23 +0100331#if !defined(FEAT_GUI)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100332static
333#endif
334HWND s_hwnd = NULL;
335static HDC s_hdc = NULL;
336static HBRUSH s_brush = NULL;
337
338#ifdef FEAT_TOOLBAR
339static HWND s_toolbarhwnd = NULL;
340static WNDPROC s_toolbar_wndproc = NULL;
341#endif
342
343#ifdef FEAT_GUI_TABLINE
344static HWND s_tabhwnd = NULL;
345static WNDPROC s_tabline_wndproc = NULL;
346static int showing_tabline = 0;
347#endif
348
349static WPARAM s_wParam = 0;
350static LPARAM s_lParam = 0;
351
352static HWND s_textArea = NULL;
353static UINT s_uMsg = 0;
354
355static char_u *s_textfield; /* Used by dialogs to pass back strings */
356
357static int s_need_activate = FALSE;
358
359/* This variable is set when waiting for an event, which is the only moment
360 * scrollbar dragging can be done directly. It's not allowed while commands
361 * are executed, because it may move the cursor and that may cause unexpected
362 * problems (e.g., while ":s" is working).
363 */
364static int allow_scrollbar = FALSE;
365
366#ifdef GLOBAL_IME
367# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
368#else
369# define MyTranslateMessage(x) TranslateMessage(x)
370#endif
371
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200372#if defined(FEAT_MBYTE) || defined(GLOBAL_IME)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100373 /* use of WindowProc depends on wide_WindowProc */
374# define MyWindowProc vim_WindowProc
375#else
376 /* use ordinary WindowProc */
377# define MyWindowProc DefWindowProc
378#endif
379
380extern int current_font_height; /* this is in os_mswin.c */
381
382static struct
383{
384 UINT key_sym;
385 char_u vim_code0;
386 char_u vim_code1;
387} special_keys[] =
388{
389 {VK_UP, 'k', 'u'},
390 {VK_DOWN, 'k', 'd'},
391 {VK_LEFT, 'k', 'l'},
392 {VK_RIGHT, 'k', 'r'},
393
394 {VK_F1, 'k', '1'},
395 {VK_F2, 'k', '2'},
396 {VK_F3, 'k', '3'},
397 {VK_F4, 'k', '4'},
398 {VK_F5, 'k', '5'},
399 {VK_F6, 'k', '6'},
400 {VK_F7, 'k', '7'},
401 {VK_F8, 'k', '8'},
402 {VK_F9, 'k', '9'},
403 {VK_F10, 'k', ';'},
404
405 {VK_F11, 'F', '1'},
406 {VK_F12, 'F', '2'},
407 {VK_F13, 'F', '3'},
408 {VK_F14, 'F', '4'},
409 {VK_F15, 'F', '5'},
410 {VK_F16, 'F', '6'},
411 {VK_F17, 'F', '7'},
412 {VK_F18, 'F', '8'},
413 {VK_F19, 'F', '9'},
414 {VK_F20, 'F', 'A'},
415
416 {VK_F21, 'F', 'B'},
417#ifdef FEAT_NETBEANS_INTG
418 {VK_PAUSE, 'F', 'B'}, /* Pause == F21 (see gui_gtk_x11.c) */
419#endif
420 {VK_F22, 'F', 'C'},
421 {VK_F23, 'F', 'D'},
422 {VK_F24, 'F', 'E'}, /* winuser.h defines up to F24 */
423
424 {VK_HELP, '%', '1'},
425 {VK_BACK, 'k', 'b'},
426 {VK_INSERT, 'k', 'I'},
427 {VK_DELETE, 'k', 'D'},
428 {VK_HOME, 'k', 'h'},
429 {VK_END, '@', '7'},
430 {VK_PRIOR, 'k', 'P'},
431 {VK_NEXT, 'k', 'N'},
432 {VK_PRINT, '%', '9'},
433 {VK_ADD, 'K', '6'},
434 {VK_SUBTRACT, 'K', '7'},
435 {VK_DIVIDE, 'K', '8'},
436 {VK_MULTIPLY, 'K', '9'},
437 {VK_SEPARATOR, 'K', 'A'}, /* Keypad Enter */
438 {VK_DECIMAL, 'K', 'B'},
439
440 {VK_NUMPAD0, 'K', 'C'},
441 {VK_NUMPAD1, 'K', 'D'},
442 {VK_NUMPAD2, 'K', 'E'},
443 {VK_NUMPAD3, 'K', 'F'},
444 {VK_NUMPAD4, 'K', 'G'},
445 {VK_NUMPAD5, 'K', 'H'},
446 {VK_NUMPAD6, 'K', 'I'},
447 {VK_NUMPAD7, 'K', 'J'},
448 {VK_NUMPAD8, 'K', 'K'},
449 {VK_NUMPAD9, 'K', 'L'},
450
451 /* Keys that we want to be able to use any modifier with: */
452 {VK_SPACE, ' ', NUL},
453 {VK_TAB, TAB, NUL},
454 {VK_ESCAPE, ESC, NUL},
455 {NL, NL, NUL},
456 {CAR, CAR, NUL},
457
458 /* End of list marker: */
459 {0, 0, 0}
460};
461
462/* Local variables */
463static int s_button_pending = -1;
464
465/* s_getting_focus is set when we got focus but didn't see mouse-up event yet,
466 * so don't reset s_button_pending. */
467static int s_getting_focus = FALSE;
468
469static int s_x_pending;
470static int s_y_pending;
471static UINT s_kFlags_pending;
472static UINT s_wait_timer = 0; /* Timer for get char from user */
473static int s_timed_out = FALSE;
474static int dead_key = 0; /* 0: no dead key, 1: dead key pressed */
475
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100476#ifdef FEAT_BEVAL
477/* balloon-eval WM_NOTIFY_HANDLER */
478static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
479static void TrackUserActivity(UINT uMsg);
480#endif
481
482/*
483 * For control IME.
484 *
485 * These LOGFONT used for IME.
486 */
487#ifdef FEAT_MBYTE
488# ifdef USE_IM_CONTROL
489/* holds LOGFONT for 'guifontwide' if available, otherwise 'guifont' */
490static LOGFONT norm_logfont;
491/* holds LOGFONT for 'guifont' always. */
492static LOGFONT sub_logfont;
493# endif
494#endif
495
496#ifdef FEAT_MBYTE_IME
497static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
498#endif
499
500#if defined(FEAT_BROWSE)
501static char_u *convert_filter(char_u *s);
502#endif
503
504#ifdef DEBUG_PRINT_ERROR
505/*
506 * Print out the last Windows error message
507 */
508 static void
509print_windows_error(void)
510{
511 LPVOID lpMsgBuf;
512
513 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
514 NULL, GetLastError(),
515 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
516 (LPTSTR) &lpMsgBuf, 0, NULL);
517 TRACE1("Error: %s\n", lpMsgBuf);
518 LocalFree(lpMsgBuf);
519}
520#endif
521
522/*
523 * Cursor blink functions.
524 *
525 * This is a simple state machine:
526 * BLINK_NONE not blinking at all
527 * BLINK_OFF blinking, cursor is not shown
528 * BLINK_ON blinking, cursor is shown
529 */
530
531#define BLINK_NONE 0
532#define BLINK_OFF 1
533#define BLINK_ON 2
534
535static int blink_state = BLINK_NONE;
536static long_u blink_waittime = 700;
537static long_u blink_ontime = 400;
538static long_u blink_offtime = 250;
539static UINT blink_timer = 0;
540
Bram Moolenaar703a8042016-06-04 16:24:32 +0200541 int
542gui_mch_is_blinking(void)
543{
544 return blink_state != BLINK_NONE;
545}
546
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200547 int
548gui_mch_is_blink_off(void)
549{
550 return blink_state == BLINK_OFF;
551}
552
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100553 void
554gui_mch_set_blinking(long wait, long on, long off)
555{
556 blink_waittime = wait;
557 blink_ontime = on;
558 blink_offtime = off;
559}
560
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100561 static VOID CALLBACK
562_OnBlinkTimer(
563 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100564 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100565 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100566 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100567{
568 MSG msg;
569
570 /*
571 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
572 */
573
574 KillTimer(NULL, idEvent);
575
576 /* Eat spurious WM_TIMER messages */
577 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
578 ;
579
580 if (blink_state == BLINK_ON)
581 {
582 gui_undraw_cursor();
583 blink_state = BLINK_OFF;
584 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
585 (TIMERPROC)_OnBlinkTimer);
586 }
587 else
588 {
589 gui_update_cursor(TRUE, FALSE);
590 blink_state = BLINK_ON;
591 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100592 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100593 }
594}
595
596 static void
597gui_mswin_rm_blink_timer(void)
598{
599 MSG msg;
600
601 if (blink_timer != 0)
602 {
603 KillTimer(NULL, blink_timer);
604 /* Eat spurious WM_TIMER messages */
605 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
606 ;
607 blink_timer = 0;
608 }
609}
610
611/*
612 * Stop the cursor blinking. Show the cursor if it wasn't shown.
613 */
614 void
615gui_mch_stop_blink(void)
616{
617 gui_mswin_rm_blink_timer();
618 if (blink_state == BLINK_OFF)
619 gui_update_cursor(TRUE, FALSE);
620 blink_state = BLINK_NONE;
621}
622
623/*
624 * Start the cursor blinking. If it was already blinking, this restarts the
625 * waiting time and shows the cursor.
626 */
627 void
628gui_mch_start_blink(void)
629{
630 gui_mswin_rm_blink_timer();
631
632 /* Only switch blinking on if none of the times is zero */
633 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
634 {
635 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
636 (TIMERPROC)_OnBlinkTimer);
637 blink_state = BLINK_ON;
638 gui_update_cursor(TRUE, FALSE);
639 }
640}
641
642/*
643 * Call-back routines.
644 */
645
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100646 static VOID CALLBACK
647_OnTimer(
648 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100649 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100650 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100651 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100652{
653 MSG msg;
654
655 /*
656 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
657 */
658 KillTimer(NULL, idEvent);
659 s_timed_out = TRUE;
660
661 /* Eat spurious WM_TIMER messages */
662 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
663 ;
664 if (idEvent == s_wait_timer)
665 s_wait_timer = 0;
666}
667
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100668 static void
669_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100670 HWND hwnd UNUSED,
671 UINT ch UNUSED,
672 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100673{
674 dead_key = 1;
675}
676
677/*
678 * Convert Unicode character "ch" to bytes in "string[slen]".
679 * When "had_alt" is TRUE the ALT key was included in "ch".
680 * Return the length.
681 */
682 static int
683char_to_string(int ch, char_u *string, int slen, int had_alt)
684{
685 int len;
686 int i;
687#ifdef FEAT_MBYTE
688 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200689 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100690
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200691 wstring[0] = ch;
692 len = 1;
693
694 /* "ch" is a UTF-16 character. Convert it to a string of bytes. When
695 * "enc_codepage" is non-zero use the standard Win32 function,
696 * otherwise use our own conversion function (e.g., for UTF-8). */
697 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100698 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200699 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
700 (LPSTR)string, slen, 0, NULL);
701 /* If we had included the ALT key into the character but now the
702 * upper bit is no longer set, that probably means the conversion
703 * failed. Convert the original character and set the upper bit
704 * afterwards. */
705 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100706 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200707 wstring[0] = ch & 0x7f;
708 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
709 (LPSTR)string, slen, 0, NULL);
710 if (len == 1) /* safety check */
711 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100712 }
713 }
714 else
715 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100716 len = 1;
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200717 ws = utf16_to_enc(wstring, &len);
718 if (ws == NULL)
719 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100720 else
721 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200722 if (len > slen) /* just in case */
723 len = slen;
724 mch_memmove(string, ws, len);
725 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100726 }
727 }
728
729 if (len == 0)
730#endif
731 {
732 string[0] = ch;
733 len = 1;
734 }
735
736 for (i = 0; i < len; ++i)
737 if (string[i] == CSI && len <= slen - 2)
738 {
739 /* Insert CSI as K_CSI. */
740 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
741 string[++i] = KS_EXTRA;
742 string[++i] = (int)KE_CSI;
743 len += 2;
744 }
745
746 return len;
747}
748
749/*
750 * Key hit, add it to the input buffer.
751 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100752 static void
753_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100754 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100755 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100756 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100757{
758 char_u string[40];
759 int len = 0;
760
761 dead_key = 0;
762
763 len = char_to_string(ch, string, 40, FALSE);
764 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
765 {
766 trash_input_buf();
767 got_int = TRUE;
768 }
769
770 add_to_input_buf(string, len);
771}
772
773/*
774 * Alt-Key hit, add it to the input buffer.
775 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100776 static void
777_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100778 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100779 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100780 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100781{
782 char_u string[40]; /* Enough for multibyte character */
783 int len;
784 int modifiers;
785 int ch = cch; /* special keys are negative */
786
787 dead_key = 0;
788
789 /* TRACE("OnSysChar(%d, %c)\n", ch, ch); */
790
791 /* OK, we have a character key (given by ch) which was entered with the
792 * ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
793 * that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
794 * CAPSLOCK is pressed) at this point.
795 */
796 modifiers = MOD_MASK_ALT;
797 if (GetKeyState(VK_SHIFT) & 0x8000)
798 modifiers |= MOD_MASK_SHIFT;
799 if (GetKeyState(VK_CONTROL) & 0x8000)
800 modifiers |= MOD_MASK_CTRL;
801
802 ch = simplify_key(ch, &modifiers);
803 /* remove the SHIFT modifier for keys where it's already included, e.g.,
804 * '(' and '*' */
805 if (ch < 0x100 && !isalpha(ch) && isprint(ch))
806 modifiers &= ~MOD_MASK_SHIFT;
807
808 /* Interpret the ALT key as making the key META, include SHIFT, etc. */
809 ch = extract_modifiers(ch, &modifiers);
810 if (ch == CSI)
811 ch = K_CSI;
812
813 len = 0;
814 if (modifiers)
815 {
816 string[len++] = CSI;
817 string[len++] = KS_MODIFIER;
818 string[len++] = modifiers;
819 }
820
821 if (IS_SPECIAL((int)ch))
822 {
823 string[len++] = CSI;
824 string[len++] = K_SECOND((int)ch);
825 string[len++] = K_THIRD((int)ch);
826 }
827 else
828 {
829 /* Although the documentation isn't clear about it, we assume "ch" is
830 * a Unicode character. */
831 len += char_to_string(ch, string + len, 40 - len, TRUE);
832 }
833
834 add_to_input_buf(string, len);
835}
836
837 static void
838_OnMouseEvent(
839 int button,
840 int x,
841 int y,
842 int repeated_click,
843 UINT keyFlags)
844{
845 int vim_modifiers = 0x0;
846
847 s_getting_focus = FALSE;
848
849 if (keyFlags & MK_SHIFT)
850 vim_modifiers |= MOUSE_SHIFT;
851 if (keyFlags & MK_CONTROL)
852 vim_modifiers |= MOUSE_CTRL;
853 if (GetKeyState(VK_MENU) & 0x8000)
854 vim_modifiers |= MOUSE_ALT;
855
856 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
857}
858
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100859 static void
860_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100861 HWND hwnd UNUSED,
862 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100863 int x,
864 int y,
865 UINT keyFlags)
866{
867 static LONG s_prevTime = 0;
868
869 LONG currentTime = GetMessageTime();
870 int button = -1;
871 int repeated_click;
872
873 /* Give main window the focus: this is so the cursor isn't hollow. */
874 (void)SetFocus(s_hwnd);
875
876 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
877 button = MOUSE_LEFT;
878 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
879 button = MOUSE_MIDDLE;
880 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
881 button = MOUSE_RIGHT;
882 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
883 {
884#ifndef GET_XBUTTON_WPARAM
885# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
886#endif
887 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
888 }
889 else if (s_uMsg == WM_CAPTURECHANGED)
890 {
891 /* on W95/NT4, somehow you get in here with an odd Msg
892 * if you press one button while holding down the other..*/
893 if (s_button_pending == MOUSE_LEFT)
894 button = MOUSE_RIGHT;
895 else
896 button = MOUSE_LEFT;
897 }
898 if (button >= 0)
899 {
900 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
901
902 /*
903 * Holding down the left and right buttons simulates pushing the middle
904 * button.
905 */
906 if (repeated_click
907 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
908 || (button == MOUSE_RIGHT
909 && s_button_pending == MOUSE_LEFT)))
910 {
911 /*
912 * Hmm, gui.c will ignore more than one button down at a time, so
913 * pretend we let go of it first.
914 */
915 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
916 button = MOUSE_MIDDLE;
917 repeated_click = FALSE;
918 s_button_pending = -1;
919 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
920 }
921 else if ((repeated_click)
922 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
923 {
924 if (s_button_pending > -1)
925 {
926 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
927 s_button_pending = -1;
928 }
929 /* TRACE("Button down at x %d, y %d\n", x, y); */
930 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
931 }
932 else
933 {
934 /*
935 * If this is the first press (i.e. not a multiple click) don't
936 * action immediately, but store and wait for:
937 * i) button-up
938 * ii) mouse move
939 * iii) another button press
940 * before using it.
941 * This enables us to make left+right simulate middle button,
942 * without left or right being actioned first. The side-effect is
943 * that if you click and hold the mouse without dragging, the
944 * cursor doesn't move until you release the button. In practice
945 * this is hardly a problem.
946 */
947 s_button_pending = button;
948 s_x_pending = x;
949 s_y_pending = y;
950 s_kFlags_pending = keyFlags;
951 }
952
953 s_prevTime = currentTime;
954 }
955}
956
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100957 static void
958_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100959 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100960 int x,
961 int y,
962 UINT keyFlags)
963{
964 int button;
965
966 s_getting_focus = FALSE;
967 if (s_button_pending > -1)
968 {
969 /* Delayed action for mouse down event */
970 _OnMouseEvent(s_button_pending, s_x_pending,
971 s_y_pending, FALSE, s_kFlags_pending);
972 s_button_pending = -1;
973 }
974 if (s_uMsg == WM_MOUSEMOVE)
975 {
976 /*
977 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
978 * down.
979 */
980 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
981 | MK_XBUTTON1 | MK_XBUTTON2)))
982 {
983 gui_mouse_moved(x, y);
984 return;
985 }
986
987 /*
988 * While button is down, keep grabbing mouse move events when
989 * the mouse goes outside the window
990 */
991 SetCapture(s_textArea);
992 button = MOUSE_DRAG;
993 /* TRACE(" move at x %d, y %d\n", x, y); */
994 }
995 else
996 {
997 ReleaseCapture();
998 button = MOUSE_RELEASE;
999 /* TRACE(" up at x %d, y %d\n", x, y); */
1000 }
1001
1002 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1003}
1004
1005#ifdef FEAT_MENU
1006/*
1007 * Find the vimmenu_T with the given id
1008 */
1009 static vimmenu_T *
1010gui_mswin_find_menu(
1011 vimmenu_T *pMenu,
1012 int id)
1013{
1014 vimmenu_T *pChildMenu;
1015
1016 while (pMenu)
1017 {
1018 if (pMenu->id == (UINT)id)
1019 break;
1020 if (pMenu->children != NULL)
1021 {
1022 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1023 if (pChildMenu)
1024 {
1025 pMenu = pChildMenu;
1026 break;
1027 }
1028 }
1029 pMenu = pMenu->next;
1030 }
1031 return pMenu;
1032}
1033
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001034 static void
1035_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001036 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001037 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001038 HWND hwndCtl UNUSED,
1039 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001040{
1041 vimmenu_T *pMenu;
1042
1043 pMenu = gui_mswin_find_menu(root_menu, id);
1044 if (pMenu)
1045 gui_menu_cb(pMenu);
1046}
1047#endif
1048
1049#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001050# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001051/*
1052 * copy useful data from structure LPFINDREPLACE to structure LPFINDREPLACEW
1053 */
1054 static void
1055findrep_atow(LPFINDREPLACEW lpfrw, LPFINDREPLACE lpfr)
1056{
1057 WCHAR *wp;
1058
1059 lpfrw->hwndOwner = lpfr->hwndOwner;
1060 lpfrw->Flags = lpfr->Flags;
1061
1062 wp = enc_to_utf16((char_u *)lpfr->lpstrFindWhat, NULL);
1063 wcsncpy(lpfrw->lpstrFindWhat, wp, lpfrw->wFindWhatLen - 1);
1064 vim_free(wp);
1065
1066 /* the field "lpstrReplaceWith" doesn't need to be copied */
1067}
1068
1069/*
1070 * copy useful data from structure LPFINDREPLACEW to structure LPFINDREPLACE
1071 */
1072 static void
1073findrep_wtoa(LPFINDREPLACE lpfr, LPFINDREPLACEW lpfrw)
1074{
1075 char_u *p;
1076
1077 lpfr->Flags = lpfrw->Flags;
1078
1079 p = utf16_to_enc((short_u*)lpfrw->lpstrFindWhat, NULL);
1080 vim_strncpy((char_u *)lpfr->lpstrFindWhat, p, lpfr->wFindWhatLen - 1);
1081 vim_free(p);
1082
1083 p = utf16_to_enc((short_u*)lpfrw->lpstrReplaceWith, NULL);
1084 vim_strncpy((char_u *)lpfr->lpstrReplaceWith, p, lpfr->wReplaceWithLen - 1);
1085 vim_free(p);
1086}
1087# endif
1088
1089/*
1090 * Handle a Find/Replace window message.
1091 */
1092 static void
1093_OnFindRepl(void)
1094{
1095 int flags = 0;
1096 int down;
1097
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001098# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001099 /* If the OS is Windows NT, and 'encoding' differs from active codepage:
1100 * convert text from wide string. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001101 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001102 {
1103 findrep_wtoa(&s_findrep_struct, &s_findrep_struct_w);
1104 }
1105# endif
1106
1107 if (s_findrep_struct.Flags & FR_DIALOGTERM)
1108 /* Give main window the focus back. */
1109 (void)SetFocus(s_hwnd);
1110
1111 if (s_findrep_struct.Flags & FR_FINDNEXT)
1112 {
1113 flags = FRD_FINDNEXT;
1114
1115 /* Give main window the focus back: this is so the cursor isn't
1116 * hollow. */
1117 (void)SetFocus(s_hwnd);
1118 }
1119 else if (s_findrep_struct.Flags & FR_REPLACE)
1120 {
1121 flags = FRD_REPLACE;
1122
1123 /* Give main window the focus back: this is so the cursor isn't
1124 * hollow. */
1125 (void)SetFocus(s_hwnd);
1126 }
1127 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1128 {
1129 flags = FRD_REPLACEALL;
1130 }
1131
1132 if (flags != 0)
1133 {
1134 /* Call the generic GUI function to do the actual work. */
1135 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1136 flags |= FRD_WHOLE_WORD;
1137 if (s_findrep_struct.Flags & FR_MATCHCASE)
1138 flags |= FRD_MATCH_CASE;
1139 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
1140 gui_do_findrepl(flags, (char_u *)s_findrep_struct.lpstrFindWhat,
1141 (char_u *)s_findrep_struct.lpstrReplaceWith, down);
1142 }
1143}
1144#endif
1145
1146 static void
1147HandleMouseHide(UINT uMsg, LPARAM lParam)
1148{
1149 static LPARAM last_lParam = 0L;
1150
1151 /* We sometimes get a mousemove when the mouse didn't move... */
1152 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1153 {
1154 if (lParam == last_lParam)
1155 return;
1156 last_lParam = lParam;
1157 }
1158
1159 /* Handle specially, to centralise coding. We need to be sure we catch all
1160 * possible events which should cause us to restore the cursor (as it is a
1161 * shared resource, we take full responsibility for it).
1162 */
1163 switch (uMsg)
1164 {
1165 case WM_KEYUP:
1166 case WM_CHAR:
1167 /*
1168 * blank out the pointer if necessary
1169 */
1170 if (p_mh)
1171 gui_mch_mousehide(TRUE);
1172 break;
1173
1174 case WM_SYSKEYUP: /* show the pointer when a system-key is pressed */
1175 case WM_SYSCHAR:
1176 case WM_MOUSEMOVE: /* show the pointer on any mouse action */
1177 case WM_LBUTTONDOWN:
1178 case WM_LBUTTONUP:
1179 case WM_MBUTTONDOWN:
1180 case WM_MBUTTONUP:
1181 case WM_RBUTTONDOWN:
1182 case WM_RBUTTONUP:
1183 case WM_XBUTTONDOWN:
1184 case WM_XBUTTONUP:
1185 case WM_NCMOUSEMOVE:
1186 case WM_NCLBUTTONDOWN:
1187 case WM_NCLBUTTONUP:
1188 case WM_NCMBUTTONDOWN:
1189 case WM_NCMBUTTONUP:
1190 case WM_NCRBUTTONDOWN:
1191 case WM_NCRBUTTONUP:
1192 case WM_KILLFOCUS:
1193 /*
1194 * if the pointer is currently hidden, then we should show it.
1195 */
1196 gui_mch_mousehide(FALSE);
1197 break;
1198 }
1199}
1200
1201 static LRESULT CALLBACK
1202_TextAreaWndProc(
1203 HWND hwnd,
1204 UINT uMsg,
1205 WPARAM wParam,
1206 LPARAM lParam)
1207{
1208 /*
1209 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1210 hwnd, uMsg, wParam, lParam);
1211 */
1212
1213 HandleMouseHide(uMsg, lParam);
1214
1215 s_uMsg = uMsg;
1216 s_wParam = wParam;
1217 s_lParam = lParam;
1218
1219#ifdef FEAT_BEVAL
1220 TrackUserActivity(uMsg);
1221#endif
1222
1223 switch (uMsg)
1224 {
1225 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1226 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1227 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1228 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1229 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1230 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1231 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1232 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1233 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1234 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1235 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1236 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1237 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1238 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
1239
1240#ifdef FEAT_BEVAL
1241 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1242 return TRUE;
1243#endif
1244 default:
1245 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1246 }
1247}
1248
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001249#if defined(FEAT_MBYTE) \
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001250 || defined(GLOBAL_IME) \
1251 || defined(PROTO)
1252# ifdef PROTO
1253typedef int WINAPI;
1254# endif
1255
1256 LRESULT WINAPI
1257vim_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1258{
1259# ifdef GLOBAL_IME
1260 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
1261# else
1262 if (wide_WindowProc)
1263 return DefWindowProcW(hwnd, message, wParam, lParam);
1264 return DefWindowProc(hwnd, message, wParam, lParam);
1265#endif
1266}
1267#endif
1268
1269/*
1270 * Called when the foreground or background color has been changed.
1271 */
1272 void
1273gui_mch_new_colors(void)
1274{
1275 /* nothing to do? */
1276}
1277
1278/*
1279 * Set the colors to their default values.
1280 */
1281 void
1282gui_mch_def_colors(void)
1283{
1284 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1285 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1286 gui.def_norm_pixel = gui.norm_pixel;
1287 gui.def_back_pixel = gui.back_pixel;
1288}
1289
1290/*
1291 * Open the GUI window which was created by a call to gui_mch_init().
1292 */
1293 int
1294gui_mch_open(void)
1295{
1296#ifndef SW_SHOWDEFAULT
1297# define SW_SHOWDEFAULT 10 /* Borland 5.0 doesn't have it */
1298#endif
1299 /* Actually open the window, if not already visible
1300 * (may be done already in gui_mch_set_shellsize) */
1301 if (!IsWindowVisible(s_hwnd))
1302 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1303
1304#ifdef MSWIN_FIND_REPLACE
1305 /* Init replace string here, so that we keep it when re-opening the
1306 * dialog. */
1307 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1308#endif
1309
1310 return OK;
1311}
1312
1313/*
1314 * Get the position of the top left corner of the window.
1315 */
1316 int
1317gui_mch_get_winpos(int *x, int *y)
1318{
1319 RECT rect;
1320
1321 GetWindowRect(s_hwnd, &rect);
1322 *x = rect.left;
1323 *y = rect.top;
1324 return OK;
1325}
1326
1327/*
1328 * Set the position of the top left corner of the window to the given
1329 * coordinates.
1330 */
1331 void
1332gui_mch_set_winpos(int x, int y)
1333{
1334 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1335 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1336}
1337 void
1338gui_mch_set_text_area_pos(int x, int y, int w, int h)
1339{
1340 static int oldx = 0;
1341 static int oldy = 0;
1342
1343 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1344
1345#ifdef FEAT_TOOLBAR
1346 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1347 SendMessage(s_toolbarhwnd, WM_SIZE,
1348 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1349#endif
1350#if defined(FEAT_GUI_TABLINE)
1351 if (showing_tabline)
1352 {
1353 int top = 0;
1354 RECT rect;
1355
1356# ifdef FEAT_TOOLBAR
1357 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1358 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1359# endif
1360 GetClientRect(s_hwnd, &rect);
1361 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1362 }
1363#endif
1364
1365 /* When side scroll bar is unshown, the size of window will change.
1366 * then, the text area move left or right. thus client rect should be
1367 * forcedly redrawn. (Yasuhiro Matsumoto) */
1368 if (oldx != x || oldy != y)
1369 {
1370 InvalidateRect(s_hwnd, NULL, FALSE);
1371 oldx = x;
1372 oldy = y;
1373 }
1374}
1375
1376
1377/*
1378 * Scrollbar stuff:
1379 */
1380
1381 void
1382gui_mch_enable_scrollbar(
1383 scrollbar_T *sb,
1384 int flag)
1385{
1386 ShowScrollBar(sb->id, SB_CTL, flag);
1387
1388 /* TODO: When the window is maximized, the size of the window stays the
1389 * same, thus the size of the text area changes. On Win98 it's OK, on Win
1390 * NT 4.0 it's not... */
1391}
1392
1393 void
1394gui_mch_set_scrollbar_pos(
1395 scrollbar_T *sb,
1396 int x,
1397 int y,
1398 int w,
1399 int h)
1400{
1401 SetWindowPos(sb->id, NULL, x, y, w, h,
1402 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1403}
1404
1405 void
1406gui_mch_create_scrollbar(
1407 scrollbar_T *sb,
1408 int orient) /* SBAR_VERT or SBAR_HORIZ */
1409{
1410 sb->id = CreateWindow(
1411 "SCROLLBAR", "Scrollbar",
1412 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
1413 10, /* Any value will do for now */
1414 10, /* Any value will do for now */
1415 s_hwnd, NULL,
1416 s_hinst, NULL);
1417}
1418
1419/*
1420 * Find the scrollbar with the given hwnd.
1421 */
1422 static scrollbar_T *
1423gui_mswin_find_scrollbar(HWND hwnd)
1424{
1425 win_T *wp;
1426
1427 if (gui.bottom_sbar.id == hwnd)
1428 return &gui.bottom_sbar;
1429 FOR_ALL_WINDOWS(wp)
1430 {
1431 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1432 return &wp->w_scrollbars[SBAR_LEFT];
1433 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1434 return &wp->w_scrollbars[SBAR_RIGHT];
1435 }
1436 return NULL;
1437}
1438
1439/*
1440 * Get the character size of a font.
1441 */
1442 static void
1443GetFontSize(GuiFont font)
1444{
1445 HWND hwnd = GetDesktopWindow();
1446 HDC hdc = GetWindowDC(hwnd);
1447 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
1448 TEXTMETRIC tm;
1449
1450 GetTextMetrics(hdc, &tm);
1451 gui.char_width = tm.tmAveCharWidth + tm.tmOverhang;
1452
1453 gui.char_height = tm.tmHeight + p_linespace;
1454
1455 SelectFont(hdc, hfntOld);
1456
1457 ReleaseDC(hwnd, hdc);
1458}
1459
1460/*
1461 * Adjust gui.char_height (after 'linespace' was changed).
1462 */
1463 int
1464gui_mch_adjust_charheight(void)
1465{
1466 GetFontSize(gui.norm_font);
1467 return OK;
1468}
1469
1470 static GuiFont
1471get_font_handle(LOGFONT *lf)
1472{
1473 HFONT font = NULL;
1474
1475 /* Load the font */
1476 font = CreateFontIndirect(lf);
1477
1478 if (font == NULL)
1479 return NOFONT;
1480
1481 return (GuiFont)font;
1482}
1483
1484 static int
1485pixels_to_points(int pixels, int vertical)
1486{
1487 int points;
1488 HWND hwnd;
1489 HDC hdc;
1490
1491 hwnd = GetDesktopWindow();
1492 hdc = GetWindowDC(hwnd);
1493
1494 points = MulDiv(pixels, 72,
1495 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1496
1497 ReleaseDC(hwnd, hdc);
1498
1499 return points;
1500}
1501
1502 GuiFont
1503gui_mch_get_font(
1504 char_u *name,
1505 int giveErrorIfMissing)
1506{
1507 LOGFONT lf;
1508 GuiFont font = NOFONT;
1509
1510 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1511 font = get_font_handle(&lf);
1512 if (font == NOFONT && giveErrorIfMissing)
1513 EMSG2(_(e_font), name);
1514 return font;
1515}
1516
1517#if defined(FEAT_EVAL) || defined(PROTO)
1518/*
1519 * Return the name of font "font" in allocated memory.
1520 * Don't know how to get the actual name, thus use the provided name.
1521 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001522 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001523gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001524{
1525 if (name == NULL)
1526 return NULL;
1527 return vim_strsave(name);
1528}
1529#endif
1530
1531 void
1532gui_mch_free_font(GuiFont font)
1533{
1534 if (font)
1535 DeleteObject((HFONT)font);
1536}
1537
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001538/*
1539 * Return the Pixel value (color) for the given color name.
1540 * Return INVALCOLOR for error.
1541 */
1542 guicolor_T
1543gui_mch_get_color(char_u *name)
1544{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001545 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001546
1547 typedef struct SysColorTable
1548 {
1549 char *name;
1550 int color;
1551 } SysColorTable;
1552
1553 static SysColorTable sys_table[] =
1554 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001555 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1556 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001557#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001558 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1559#endif
1560 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1561 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1562 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1563 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1564 {"SYS_DESKTOP", COLOR_DESKTOP},
1565 {"SYS_INFOBK", COLOR_INFOBK},
1566 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1567 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001568 {"SYS_BTNFACE", COLOR_BTNFACE},
1569 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1570 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1571 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1572 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1573 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1574 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1575 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1576 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1577 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1578 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1579 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1580 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1581 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1582 {"SYS_MENU", COLOR_MENU},
1583 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1584 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1585 {"SYS_WINDOW", COLOR_WINDOW},
1586 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1587 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1588 };
1589
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001590 /*
1591 * Try to look up a system colour.
1592 */
1593 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1594 if (STRICMP(name, sys_table[i].name) == 0)
1595 return GetSysColor(sys_table[i].color);
1596
Bram Moolenaarab302212016-04-26 20:59:29 +02001597 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001598}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001599
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001600/*
1601 * Return OK if the key with the termcap name "name" is supported.
1602 */
1603 int
1604gui_mch_haskey(char_u *name)
1605{
1606 int i;
1607
1608 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1609 if (name[0] == special_keys[i].vim_code0 &&
1610 name[1] == special_keys[i].vim_code1)
1611 return OK;
1612 return FAIL;
1613}
1614
1615 void
1616gui_mch_beep(void)
1617{
1618 MessageBeep(MB_OK);
1619}
1620/*
1621 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1622 */
1623 void
1624gui_mch_invert_rectangle(
1625 int r,
1626 int c,
1627 int nr,
1628 int nc)
1629{
1630 RECT rc;
1631
1632 /*
1633 * Note: InvertRect() excludes right and bottom of rectangle.
1634 */
1635 rc.left = FILL_X(c);
1636 rc.top = FILL_Y(r);
1637 rc.right = rc.left + nc * gui.char_width;
1638 rc.bottom = rc.top + nr * gui.char_height;
1639 InvertRect(s_hdc, &rc);
1640}
1641
1642/*
1643 * Iconify the GUI window.
1644 */
1645 void
1646gui_mch_iconify(void)
1647{
1648 ShowWindow(s_hwnd, SW_MINIMIZE);
1649}
1650
1651/*
1652 * Draw a cursor without focus.
1653 */
1654 void
1655gui_mch_draw_hollow_cursor(guicolor_T color)
1656{
1657 HBRUSH hbr;
1658 RECT rc;
1659
1660 /*
1661 * Note: FrameRect() excludes right and bottom of rectangle.
1662 */
1663 rc.left = FILL_X(gui.col);
1664 rc.top = FILL_Y(gui.row);
1665 rc.right = rc.left + gui.char_width;
1666#ifdef FEAT_MBYTE
1667 if (mb_lefthalve(gui.row, gui.col))
1668 rc.right += gui.char_width;
1669#endif
1670 rc.bottom = rc.top + gui.char_height;
1671 hbr = CreateSolidBrush(color);
1672 FrameRect(s_hdc, &rc, hbr);
1673 DeleteBrush(hbr);
1674}
1675/*
1676 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1677 * color "color".
1678 */
1679 void
1680gui_mch_draw_part_cursor(
1681 int w,
1682 int h,
1683 guicolor_T color)
1684{
1685 HBRUSH hbr;
1686 RECT rc;
1687
1688 /*
1689 * Note: FillRect() excludes right and bottom of rectangle.
1690 */
1691 rc.left =
1692#ifdef FEAT_RIGHTLEFT
1693 /* vertical line should be on the right of current point */
1694 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1695#endif
1696 FILL_X(gui.col);
1697 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1698 rc.right = rc.left + w;
1699 rc.bottom = rc.top + h;
1700 hbr = CreateSolidBrush(color);
1701 FillRect(s_hdc, &rc, hbr);
1702 DeleteBrush(hbr);
1703}
1704
1705
1706/*
1707 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1708 * dead key's nominal character and re-post the original message.
1709 */
1710 static void
1711outputDeadKey_rePost(MSG originalMsg)
1712{
1713 static MSG deadCharExpel;
1714
1715 if (!dead_key)
1716 return;
1717
1718 dead_key = 0;
1719
1720 /* Make Windows generate the dead key's character */
1721 deadCharExpel.message = originalMsg.message;
1722 deadCharExpel.hwnd = originalMsg.hwnd;
1723 deadCharExpel.wParam = VK_SPACE;
1724
1725 MyTranslateMessage(&deadCharExpel);
1726
1727 /* re-generate the current character free of the dead char influence */
1728 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1729 originalMsg.lParam);
1730}
1731
1732
1733/*
1734 * Process a single Windows message.
1735 * If one is not available we hang until one is.
1736 */
1737 static void
1738process_message(void)
1739{
1740 MSG msg;
1741 UINT vk = 0; /* Virtual key */
1742 char_u string[40];
1743 int i;
1744 int modifiers = 0;
1745 int key;
1746#ifdef FEAT_MENU
1747 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1748#endif
1749
1750 pGetMessage(&msg, NULL, 0, 0);
1751
1752#ifdef FEAT_OLE
1753 /* Look after OLE Automation commands */
1754 if (msg.message == WM_OLE)
1755 {
1756 char_u *str = (char_u *)msg.lParam;
1757 if (str == NULL || *str == NUL)
1758 {
1759 /* Message can't be ours, forward it. Fixes problem with Ultramon
1760 * 3.0.4 */
1761 pDispatchMessage(&msg);
1762 }
1763 else
1764 {
1765 add_to_input_buf(str, (int)STRLEN(str));
1766 vim_free(str); /* was allocated in CVim::SendKeys() */
1767 }
1768 return;
1769 }
1770#endif
1771
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001772#ifdef MSWIN_FIND_REPLACE
1773 /* Don't process messages used by the dialog */
1774 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1775 {
1776 HandleMouseHide(msg.message, msg.lParam);
1777 return;
1778 }
1779#endif
1780
1781 /*
1782 * Check if it's a special key that we recognise. If not, call
1783 * TranslateMessage().
1784 */
1785 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1786 {
1787 vk = (int) msg.wParam;
1788
1789 /*
1790 * Handle dead keys in special conditions in other cases we let Windows
1791 * handle them and do not interfere.
1792 *
1793 * The dead_key flag must be reset on several occasions:
1794 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1795 * consumed at that point (This is when we let Windows combine the
1796 * dead character on its own)
1797 *
1798 * - Before doing something special such as regenerating keypresses to
1799 * expel the dead character as this could trigger an infinite loop if
1800 * for some reason MyTranslateMessage() do not trigger a call
1801 * immediately to _OnChar() (or _OnSysChar()).
1802 */
1803 if (dead_key)
1804 {
1805 /*
1806 * If a dead key was pressed and the user presses VK_SPACE,
1807 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1808 * with the dead char now, so do nothing special and let Windows
1809 * handle it.
1810 *
1811 * Note that VK_SPACE combines with the dead_key's character and
1812 * only one WM_CHAR will be generated by TranslateMessage(), in
1813 * the two other cases two WM_CHAR will be generated: the dead
1814 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1815 * user expects.
1816 */
1817 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1818 {
1819 dead_key = 0;
1820 MyTranslateMessage(&msg);
1821 return;
1822 }
1823 /* In modes where we are not typing, dead keys should behave
1824 * normally */
1825 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1826 {
1827 outputDeadKey_rePost(msg);
1828 return;
1829 }
1830 }
1831
1832 /* Check for CTRL-BREAK */
1833 if (vk == VK_CANCEL)
1834 {
1835 trash_input_buf();
1836 got_int = TRUE;
1837 string[0] = Ctrl_C;
1838 add_to_input_buf(string, 1);
1839 }
1840
1841 for (i = 0; special_keys[i].key_sym != 0; i++)
1842 {
1843 /* ignore VK_SPACE when ALT key pressed: system menu */
1844 if (special_keys[i].key_sym == vk
1845 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1846 {
1847 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001848 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001849 * is a key that would normally trigger the dead key nominal
1850 * character output (such as a NUMPAD printable character or
1851 * the TAB key, etc...).
1852 */
1853 if (dead_key && (special_keys[i].vim_code0 == 'K'
1854 || vk == VK_TAB || vk == CAR))
1855 {
1856 outputDeadKey_rePost(msg);
1857 return;
1858 }
1859
1860#ifdef FEAT_MENU
1861 /* Check for <F10>: Windows selects the menu. When <F10> is
1862 * mapped we want to use the mapping instead. */
1863 if (vk == VK_F10
1864 && gui.menu_is_active
1865 && check_map(k10, State, FALSE, TRUE, FALSE,
1866 NULL, NULL) == NULL)
1867 break;
1868#endif
1869 if (GetKeyState(VK_SHIFT) & 0x8000)
1870 modifiers |= MOD_MASK_SHIFT;
1871 /*
1872 * Don't use caps-lock as shift, because these are special keys
1873 * being considered here, and we only want letters to get
1874 * shifted -- webb
1875 */
1876 /*
1877 if (GetKeyState(VK_CAPITAL) & 0x0001)
1878 modifiers ^= MOD_MASK_SHIFT;
1879 */
1880 if (GetKeyState(VK_CONTROL) & 0x8000)
1881 modifiers |= MOD_MASK_CTRL;
1882 if (GetKeyState(VK_MENU) & 0x8000)
1883 modifiers |= MOD_MASK_ALT;
1884
1885 if (special_keys[i].vim_code1 == NUL)
1886 key = special_keys[i].vim_code0;
1887 else
1888 key = TO_SPECIAL(special_keys[i].vim_code0,
1889 special_keys[i].vim_code1);
1890 key = simplify_key(key, &modifiers);
1891 if (key == CSI)
1892 key = K_CSI;
1893
1894 if (modifiers)
1895 {
1896 string[0] = CSI;
1897 string[1] = KS_MODIFIER;
1898 string[2] = modifiers;
1899 add_to_input_buf(string, 3);
1900 }
1901
1902 if (IS_SPECIAL(key))
1903 {
1904 string[0] = CSI;
1905 string[1] = K_SECOND(key);
1906 string[2] = K_THIRD(key);
1907 add_to_input_buf(string, 3);
1908 }
1909 else
1910 {
1911 int len;
1912
1913 /* Handle "key" as a Unicode character. */
1914 len = char_to_string(key, string, 40, FALSE);
1915 add_to_input_buf(string, len);
1916 }
1917 break;
1918 }
1919 }
1920 if (special_keys[i].key_sym == 0)
1921 {
1922 /* Some keys need C-S- where they should only need C-.
1923 * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1924 * system startup (Helmut Stiegler, 2003 Oct 3). */
1925 if (vk != 0xff
1926 && (GetKeyState(VK_CONTROL) & 0x8000)
1927 && !(GetKeyState(VK_SHIFT) & 0x8000)
1928 && !(GetKeyState(VK_MENU) & 0x8000))
1929 {
1930 /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */
1931 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1932 {
1933 string[0] = Ctrl_HAT;
1934 add_to_input_buf(string, 1);
1935 }
1936 /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */
1937 else if (vk == 0xBD) /* QWERTY for CTRL-'-' */
1938 {
1939 string[0] = Ctrl__;
1940 add_to_input_buf(string, 1);
1941 }
1942 /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */
1943 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
1944 {
1945 string[0] = Ctrl_AT;
1946 add_to_input_buf(string, 1);
1947 }
1948 else
1949 MyTranslateMessage(&msg);
1950 }
1951 else
1952 MyTranslateMessage(&msg);
1953 }
1954 }
1955#ifdef FEAT_MBYTE_IME
1956 else if (msg.message == WM_IME_NOTIFY)
1957 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
1958 else if (msg.message == WM_KEYUP && im_get_status())
1959 /* added for non-MS IME (Yasuhiro Matsumoto) */
1960 MyTranslateMessage(&msg);
1961#endif
1962#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
1963/* GIME_TEST */
1964 else if (msg.message == WM_IME_STARTCOMPOSITION)
1965 {
1966 POINT point;
1967
1968 global_ime_set_font(&norm_logfont);
1969 point.x = FILL_X(gui.col);
1970 point.y = FILL_Y(gui.row);
1971 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
1972 global_ime_set_position(&point);
1973 }
1974#endif
1975
1976#ifdef FEAT_MENU
1977 /* Check for <F10>: Default effect is to select the menu. When <F10> is
1978 * mapped we need to stop it here to avoid strange effects (e.g., for the
1979 * key-up event) */
1980 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
1981 NULL, NULL) == NULL)
1982#endif
1983 pDispatchMessage(&msg);
1984}
1985
1986/*
1987 * Catch up with any queued events. This may put keyboard input into the
1988 * input buffer, call resize call-backs, trigger timers etc. If there is
1989 * nothing in the event queue (& no timers pending), then we return
1990 * immediately.
1991 */
1992 void
1993gui_mch_update(void)
1994{
1995 MSG msg;
1996
1997 if (!s_busy_processing)
1998 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
1999 && !vim_is_input_buf_full())
2000 process_message();
2001}
2002
Bram Moolenaar4231da42016-06-02 14:30:04 +02002003 static void
2004remove_any_timer(void)
2005{
2006 MSG msg;
2007
2008 if (s_wait_timer != 0 && !s_timed_out)
2009 {
2010 KillTimer(NULL, s_wait_timer);
2011
2012 /* Eat spurious WM_TIMER messages */
2013 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2014 ;
2015 s_wait_timer = 0;
2016 }
2017}
2018
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002019/*
2020 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2021 * from the keyboard.
2022 * wtime == -1 Wait forever.
2023 * wtime == 0 This should never happen.
2024 * wtime > 0 Wait wtime milliseconds for a character.
2025 * Returns OK if a character was found to be available within the given time,
2026 * or FAIL otherwise.
2027 */
2028 int
2029gui_mch_wait_for_chars(int wtime)
2030{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002031 int focus;
2032
2033 s_timed_out = FALSE;
2034
2035 if (wtime > 0)
2036 {
2037 /* Don't do anything while processing a (scroll) message. */
2038 if (s_busy_processing)
2039 return FAIL;
2040 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)wtime,
2041 (TIMERPROC)_OnTimer);
2042 }
2043
2044 allow_scrollbar = TRUE;
2045
2046 focus = gui.in_focus;
2047 while (!s_timed_out)
2048 {
2049 /* Stop or start blinking when focus changes */
2050 if (gui.in_focus != focus)
2051 {
2052 if (gui.in_focus)
2053 gui_mch_start_blink();
2054 else
2055 gui_mch_stop_blink();
2056 focus = gui.in_focus;
2057 }
2058
2059 if (s_need_activate)
2060 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002061 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002062 s_need_activate = FALSE;
2063 }
2064
Bram Moolenaar4231da42016-06-02 14:30:04 +02002065#ifdef FEAT_TIMERS
2066 did_add_timer = FALSE;
2067#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002068#ifdef MESSAGE_QUEUE
Bram Moolenaar9186a272016-02-23 19:34:01 +01002069 /* Check channel while waiting message. */
2070 for (;;)
2071 {
2072 MSG msg;
2073
2074 parse_queued_messages();
2075
2076 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarf28d8712016-04-02 15:59:40 +02002077 || MsgWaitForMultipleObjects(0, NULL, FALSE, 100, QS_ALLINPUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002078 != WAIT_TIMEOUT)
2079 break;
2080 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002081#endif
2082
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002083 /*
2084 * Don't use gui_mch_update() because then we will spin-lock until a
2085 * char arrives, instead we use GetMessage() to hang until an
2086 * event arrives. No need to check for input_buf_full because we are
2087 * returning as soon as it contains a single char -- webb
2088 */
2089 process_message();
2090
2091 if (input_available())
2092 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002093 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002094 allow_scrollbar = FALSE;
2095
2096 /* Clear pending mouse button, the release event may have been
2097 * taken by the dialog window. But don't do this when getting
2098 * focus, we need the mouse-up event then. */
2099 if (!s_getting_focus)
2100 s_button_pending = -1;
2101
2102 return OK;
2103 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002104
2105#ifdef FEAT_TIMERS
2106 if (did_add_timer)
2107 {
2108 /* Need to recompute the waiting time. */
2109 remove_any_timer();
2110 break;
2111 }
2112#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002113 }
2114 allow_scrollbar = FALSE;
2115 return FAIL;
2116}
2117
2118/*
2119 * Clear a rectangular region of the screen from text pos (row1, col1) to
2120 * (row2, col2) inclusive.
2121 */
2122 void
2123gui_mch_clear_block(
2124 int row1,
2125 int col1,
2126 int row2,
2127 int col2)
2128{
2129 RECT rc;
2130
2131 /*
2132 * Clear one extra pixel at the far right, for when bold characters have
2133 * spilled over to the window border.
2134 * Note: FillRect() excludes right and bottom of rectangle.
2135 */
2136 rc.left = FILL_X(col1);
2137 rc.top = FILL_Y(row1);
2138 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2139 rc.bottom = FILL_Y(row2 + 1);
2140 clear_rect(&rc);
2141}
2142
2143/*
2144 * Clear the whole text window.
2145 */
2146 void
2147gui_mch_clear_all(void)
2148{
2149 RECT rc;
2150
2151 rc.left = 0;
2152 rc.top = 0;
2153 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2154 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2155 clear_rect(&rc);
2156}
2157/*
2158 * Menu stuff.
2159 */
2160
2161 void
2162gui_mch_enable_menu(int flag)
2163{
2164#ifdef FEAT_MENU
2165 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2166#endif
2167}
2168
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002169 void
2170gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002171 int x UNUSED,
2172 int y UNUSED,
2173 int w UNUSED,
2174 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002175{
2176 /* It will be in the right place anyway */
2177}
2178
2179#if defined(FEAT_MENU) || defined(PROTO)
2180/*
2181 * Make menu item hidden or not hidden
2182 */
2183 void
2184gui_mch_menu_hidden(
2185 vimmenu_T *menu,
2186 int hidden)
2187{
2188 /*
2189 * This doesn't do what we want. Hmm, just grey the menu items for now.
2190 */
2191 /*
2192 if (hidden)
2193 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2194 else
2195 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2196 */
2197 gui_mch_menu_grey(menu, hidden);
2198}
2199
2200/*
2201 * This is called after setting all the menus to grey/hidden or not.
2202 */
2203 void
2204gui_mch_draw_menubar(void)
2205{
2206 DrawMenuBar(s_hwnd);
2207}
2208#endif /*FEAT_MENU*/
2209
2210#ifndef PROTO
2211void
2212#ifdef VIMDLL
2213_export
2214#endif
2215_cdecl
2216SaveInst(HINSTANCE hInst)
2217{
2218 s_hinst = hInst;
2219}
2220#endif
2221
2222/*
2223 * Return the RGB value of a pixel as a long.
2224 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002225 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002226gui_mch_get_rgb(guicolor_T pixel)
2227{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002228 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2229 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002230}
2231
2232#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2233/* Convert pixels in X to dialog units */
2234 static WORD
2235PixelToDialogX(int numPixels)
2236{
2237 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2238}
2239
2240/* Convert pixels in Y to dialog units */
2241 static WORD
2242PixelToDialogY(int numPixels)
2243{
2244 return (WORD)((numPixels * 8) / s_dlgfntheight);
2245}
2246
2247/* Return the width in pixels of the given text in the given DC. */
2248 static int
2249GetTextWidth(HDC hdc, char_u *str, int len)
2250{
2251 SIZE size;
2252
2253 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2254 return size.cx;
2255}
2256
2257#ifdef FEAT_MBYTE
2258/*
2259 * Return the width in pixels of the given text in the given DC, taking care
2260 * of 'encoding' to active codepage conversion.
2261 */
2262 static int
2263GetTextWidthEnc(HDC hdc, char_u *str, int len)
2264{
2265 SIZE size;
2266 WCHAR *wstr;
2267 int n;
2268 int wlen = len;
2269
2270 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2271 {
2272 /* 'encoding' differs from active codepage: convert text and use wide
2273 * function */
2274 wstr = enc_to_utf16(str, &wlen);
2275 if (wstr != NULL)
2276 {
2277 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2278 vim_free(wstr);
2279 if (n)
2280 return size.cx;
2281 }
2282 }
2283
2284 return GetTextWidth(hdc, str, len);
2285}
2286#else
2287# define GetTextWidthEnc(h, s, l) GetTextWidth((h), (s), (l))
2288#endif
2289
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002290static void get_work_area(RECT *spi_rect);
2291
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002292/*
2293 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002294 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2295 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002296 */
2297 static BOOL
2298CenterWindow(
2299 HWND hwndChild,
2300 HWND hwndParent)
2301{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002302 HMONITOR mon;
2303 MONITORINFO moninfo;
2304 RECT rChild, rParent, rScreen;
2305 int wChild, hChild, wParent, hParent;
2306 int xNew, yNew;
2307 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002308
2309 GetWindowRect(hwndChild, &rChild);
2310 wChild = rChild.right - rChild.left;
2311 hChild = rChild.bottom - rChild.top;
2312
2313 /* If Vim is minimized put the window in the middle of the screen. */
2314 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002315 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002316 else
2317 GetWindowRect(hwndParent, &rParent);
2318 wParent = rParent.right - rParent.left;
2319 hParent = rParent.bottom - rParent.top;
2320
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002321 moninfo.cbSize = sizeof(MONITORINFO);
2322 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2323 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002324 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002325 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002326 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002327 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002328 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002329 hdc = GetDC(hwndChild);
2330 rScreen.left = 0;
2331 rScreen.top = 0;
2332 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2333 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2334 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002335 }
2336
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002337 xNew = rParent.left + ((wParent - wChild) / 2);
2338 if (xNew < rScreen.left)
2339 xNew = rScreen.left;
2340 else if ((xNew + wChild) > rScreen.right)
2341 xNew = rScreen.right - wChild;
2342
2343 yNew = rParent.top + ((hParent - hChild) / 2);
2344 if (yNew < rScreen.top)
2345 yNew = rScreen.top;
2346 else if ((yNew + hChild) > rScreen.bottom)
2347 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002348
2349 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2350 SWP_NOSIZE | SWP_NOZORDER);
2351}
2352#endif /* FEAT_GUI_DIALOG */
2353
2354void
2355gui_mch_activate_window(void)
2356{
2357 (void)SetActiveWindow(s_hwnd);
2358}
2359
2360#if defined(FEAT_TOOLBAR) || defined(PROTO)
2361 void
2362gui_mch_show_toolbar(int showit)
2363{
2364 if (s_toolbarhwnd == NULL)
2365 return;
2366
2367 if (showit)
2368 {
2369# ifdef FEAT_MBYTE
2370# ifndef TB_SETUNICODEFORMAT
2371 /* For older compilers. We assume this never changes. */
2372# define TB_SETUNICODEFORMAT 0x2005
2373# endif
2374 /* Enable/disable unicode support */
2375 int uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2376 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2377# endif
2378 ShowWindow(s_toolbarhwnd, SW_SHOW);
2379 }
2380 else
2381 ShowWindow(s_toolbarhwnd, SW_HIDE);
2382}
2383
2384/* Then number of bitmaps is fixed. Exit is missing! */
2385#define TOOLBAR_BITMAP_COUNT 31
2386
2387#endif
2388
2389#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2390 static void
2391add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2392{
2393#ifdef FEAT_MBYTE
2394 WCHAR *wn = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002395
2396 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2397 {
2398 /* 'encoding' differs from active codepage: convert menu name
2399 * and use wide function */
2400 wn = enc_to_utf16(item_text, NULL);
2401 if (wn != NULL)
2402 {
2403 MENUITEMINFOW infow;
2404
2405 infow.cbSize = sizeof(infow);
2406 infow.fMask = MIIM_TYPE | MIIM_ID;
2407 infow.wID = item_id;
2408 infow.fType = MFT_STRING;
2409 infow.dwTypeData = wn;
2410 infow.cch = (UINT)wcslen(wn);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002411 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002412 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002413 }
2414 }
2415
2416 if (wn == NULL)
2417#endif
2418 {
2419 MENUITEMINFO info;
2420
2421 info.cbSize = sizeof(info);
2422 info.fMask = MIIM_TYPE | MIIM_ID;
2423 info.wID = item_id;
2424 info.fType = MFT_STRING;
2425 info.dwTypeData = (LPTSTR)item_text;
2426 info.cch = (UINT)STRLEN(item_text);
2427 InsertMenuItem(pmenu, item_id, FALSE, &info);
2428 }
2429}
2430
2431 static void
2432show_tabline_popup_menu(void)
2433{
2434 HMENU tab_pmenu;
2435 long rval;
2436 POINT pt;
2437
2438 /* When ignoring events don't show the menu. */
2439 if (hold_gui_events
2440# ifdef FEAT_CMDWIN
2441 || cmdwin_type != 0
2442# endif
2443 )
2444 return;
2445
2446 tab_pmenu = CreatePopupMenu();
2447 if (tab_pmenu == NULL)
2448 return;
2449
2450 if (first_tabpage->tp_next != NULL)
2451 add_tabline_popup_menu_entry(tab_pmenu,
2452 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2453 add_tabline_popup_menu_entry(tab_pmenu,
2454 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2455 add_tabline_popup_menu_entry(tab_pmenu,
2456 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2457
2458 GetCursorPos(&pt);
2459 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2460 NULL);
2461
2462 DestroyMenu(tab_pmenu);
2463
2464 /* Add the string cmd into input buffer */
2465 if (rval > 0)
2466 {
2467 TCHITTESTINFO htinfo;
2468 int idx;
2469
2470 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2471 return;
2472
2473 htinfo.pt.x = pt.x;
2474 htinfo.pt.y = pt.y;
2475 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2476 if (idx == -1)
2477 idx = 0;
2478 else
2479 idx += 1;
2480
2481 send_tabline_menu_event(idx, (int)rval);
2482 }
2483}
2484
2485/*
2486 * Show or hide the tabline.
2487 */
2488 void
2489gui_mch_show_tabline(int showit)
2490{
2491 if (s_tabhwnd == NULL)
2492 return;
2493
2494 if (!showit != !showing_tabline)
2495 {
2496 if (showit)
2497 ShowWindow(s_tabhwnd, SW_SHOW);
2498 else
2499 ShowWindow(s_tabhwnd, SW_HIDE);
2500 showing_tabline = showit;
2501 }
2502}
2503
2504/*
2505 * Return TRUE when tabline is displayed.
2506 */
2507 int
2508gui_mch_showing_tabline(void)
2509{
2510 return s_tabhwnd != NULL && showing_tabline;
2511}
2512
2513/*
2514 * Update the labels of the tabline.
2515 */
2516 void
2517gui_mch_update_tabline(void)
2518{
2519 tabpage_T *tp;
2520 TCITEM tie;
2521 int nr = 0;
2522 int curtabidx = 0;
2523 int tabadded = 0;
2524#ifdef FEAT_MBYTE
2525 static int use_unicode = FALSE;
2526 int uu;
2527 WCHAR *wstr = NULL;
2528#endif
2529
2530 if (s_tabhwnd == NULL)
2531 return;
2532
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002533#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002534# ifndef CCM_SETUNICODEFORMAT
2535 /* For older compilers. We assume this never changes. */
2536# define CCM_SETUNICODEFORMAT 0x2005
2537# endif
2538 uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2539 if (uu != use_unicode)
2540 {
2541 /* Enable/disable unicode support */
2542 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2543 use_unicode = uu;
2544 }
2545#endif
2546
2547 tie.mask = TCIF_TEXT;
2548 tie.iImage = -1;
2549
2550 /* Disable redraw for tab updates to eliminate O(N^2) draws. */
2551 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2552
2553 /* Add a label for each tab page. They all contain the same text area. */
2554 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2555 {
2556 if (tp == curtab)
2557 curtabidx = nr;
2558
2559 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2560 {
2561 /* Add the tab */
2562 tie.pszText = "-Empty-";
2563 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2564 tabadded = 1;
2565 }
2566
2567 get_tabline_label(tp, FALSE);
2568 tie.pszText = (LPSTR)NameBuff;
2569#ifdef FEAT_MBYTE
2570 wstr = NULL;
2571 if (use_unicode)
2572 {
2573 /* Need to go through Unicode. */
2574 wstr = enc_to_utf16(NameBuff, NULL);
2575 if (wstr != NULL)
2576 {
2577 TCITEMW tiw;
2578
2579 tiw.mask = TCIF_TEXT;
2580 tiw.iImage = -1;
2581 tiw.pszText = wstr;
2582 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2583 vim_free(wstr);
2584 }
2585 }
2586 if (wstr == NULL)
2587#endif
2588 {
2589 TabCtrl_SetItem(s_tabhwnd, nr, &tie);
2590 }
2591 }
2592
2593 /* Remove any old labels. */
2594 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2595 TabCtrl_DeleteItem(s_tabhwnd, nr);
2596
2597 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2598 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2599
2600 /* Re-enable redraw and redraw. */
2601 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2602 RedrawWindow(s_tabhwnd, NULL, NULL,
2603 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2604
2605 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2606 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2607}
2608
2609/*
2610 * Set the current tab to "nr". First tab is 1.
2611 */
2612 void
2613gui_mch_set_curtab(int nr)
2614{
2615 if (s_tabhwnd == NULL)
2616 return;
2617
2618 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2619 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2620}
2621
2622#endif
2623
2624/*
2625 * ":simalt" command.
2626 */
2627 void
2628ex_simalt(exarg_T *eap)
2629{
2630 char_u *keys = eap->arg;
2631
2632 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2633 while (*keys)
2634 {
2635 if (*keys == '~')
2636 *keys = ' '; /* for showing system menu */
2637 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2638 keys++;
2639 }
2640}
2641
2642/*
2643 * Create the find & replace dialogs.
2644 * You can't have both at once: ":find" when replace is showing, destroys
2645 * the replace dialog first, and the other way around.
2646 */
2647#ifdef MSWIN_FIND_REPLACE
2648 static void
2649initialise_findrep(char_u *initial_string)
2650{
2651 int wword = FALSE;
2652 int mcase = !p_ic;
2653 char_u *entry_text;
2654
2655 /* Get the search string to use. */
2656 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2657
2658 s_findrep_struct.hwndOwner = s_hwnd;
2659 s_findrep_struct.Flags = FR_DOWN;
2660 if (mcase)
2661 s_findrep_struct.Flags |= FR_MATCHCASE;
2662 if (wword)
2663 s_findrep_struct.Flags |= FR_WHOLEWORD;
2664 if (entry_text != NULL && *entry_text != NUL)
2665 vim_strncpy((char_u *)s_findrep_struct.lpstrFindWhat, entry_text,
2666 s_findrep_struct.wFindWhatLen - 1);
2667 vim_free(entry_text);
2668}
2669#endif
2670
2671 static void
2672set_window_title(HWND hwnd, char *title)
2673{
2674#ifdef FEAT_MBYTE
2675 if (title != NULL && enc_codepage >= 0 && enc_codepage != (int)GetACP())
2676 {
2677 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002678
2679 /* Convert the title from 'encoding' to UTF-16. */
2680 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2681 if (wbuf != NULL)
2682 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002683 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002684 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002685 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002686 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002687 }
2688#endif
2689 (void)SetWindowText(hwnd, (LPCSTR)title);
2690}
2691
2692 void
2693gui_mch_find_dialog(exarg_T *eap)
2694{
2695#ifdef MSWIN_FIND_REPLACE
2696 if (s_findrep_msg != 0)
2697 {
2698 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2699 DestroyWindow(s_findrep_hwnd);
2700
2701 if (!IsWindow(s_findrep_hwnd))
2702 {
2703 initialise_findrep(eap->arg);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002704# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002705 /* If the OS is Windows NT, and 'encoding' differs from active
2706 * codepage: convert text and use wide function. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002707 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002708 {
2709 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2710 s_findrep_hwnd = FindTextW(
2711 (LPFINDREPLACEW) &s_findrep_struct_w);
2712 }
2713 else
2714# endif
2715 s_findrep_hwnd = FindText((LPFINDREPLACE) &s_findrep_struct);
2716 }
2717
2718 set_window_title(s_findrep_hwnd,
2719 _("Find string (use '\\\\' to find a '\\')"));
2720 (void)SetFocus(s_findrep_hwnd);
2721
2722 s_findrep_is_find = TRUE;
2723 }
2724#endif
2725}
2726
2727
2728 void
2729gui_mch_replace_dialog(exarg_T *eap)
2730{
2731#ifdef MSWIN_FIND_REPLACE
2732 if (s_findrep_msg != 0)
2733 {
2734 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2735 DestroyWindow(s_findrep_hwnd);
2736
2737 if (!IsWindow(s_findrep_hwnd))
2738 {
2739 initialise_findrep(eap->arg);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002740# ifdef FEAT_MBYTE
2741 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002742 {
2743 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2744 s_findrep_hwnd = ReplaceTextW(
2745 (LPFINDREPLACEW) &s_findrep_struct_w);
2746 }
2747 else
2748# endif
2749 s_findrep_hwnd = ReplaceText(
2750 (LPFINDREPLACE) &s_findrep_struct);
2751 }
2752
2753 set_window_title(s_findrep_hwnd,
2754 _("Find & Replace (use '\\\\' to find a '\\')"));
2755 (void)SetFocus(s_findrep_hwnd);
2756
2757 s_findrep_is_find = FALSE;
2758 }
2759#endif
2760}
2761
2762
2763/*
2764 * Set visibility of the pointer.
2765 */
2766 void
2767gui_mch_mousehide(int hide)
2768{
2769 if (hide != gui.pointer_hidden)
2770 {
2771 ShowCursor(!hide);
2772 gui.pointer_hidden = hide;
2773 }
2774}
2775
2776#ifdef FEAT_MENU
2777 static void
2778gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2779{
2780 /* Unhide the mouse, we don't get move events here. */
2781 gui_mch_mousehide(FALSE);
2782
2783 (void)TrackPopupMenu(
2784 (HMENU)menu->submenu_id,
2785 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2786 x, y,
2787 (int)0, /*reserved param*/
2788 s_hwnd,
2789 NULL);
2790 /*
2791 * NOTE: The pop-up menu can eat the mouse up event.
2792 * We deal with this in normal.c.
2793 */
2794}
2795#endif
2796
2797/*
2798 * Got a message when the system will go down.
2799 */
2800 static void
2801_OnEndSession(void)
2802{
2803 getout_preserve_modified(1);
2804}
2805
2806/*
2807 * Get this message when the user clicks on the cross in the top right corner
2808 * of a Windows95 window.
2809 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002810 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002811_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002812{
2813 gui_shell_closed();
2814}
2815
2816/*
2817 * Get a message when the window is being destroyed.
2818 */
2819 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002820_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002821{
2822 if (!destroying)
2823 _OnClose(hwnd);
2824}
2825
2826 static void
2827_OnPaint(
2828 HWND hwnd)
2829{
2830 if (!IsMinimized(hwnd))
2831 {
2832 PAINTSTRUCT ps;
2833
2834 out_flush(); /* make sure all output has been processed */
2835 (void)BeginPaint(hwnd, &ps);
2836#if defined(FEAT_DIRECTX)
2837 if (IS_ENABLE_DIRECTX())
2838 DWriteContext_BeginDraw(s_dwc);
2839#endif
2840
2841#ifdef FEAT_MBYTE
2842 /* prevent multi-byte characters from misprinting on an invalid
2843 * rectangle */
2844 if (has_mbyte)
2845 {
2846 RECT rect;
2847
2848 GetClientRect(hwnd, &rect);
2849 ps.rcPaint.left = rect.left;
2850 ps.rcPaint.right = rect.right;
2851 }
2852#endif
2853
2854 if (!IsRectEmpty(&ps.rcPaint))
2855 {
2856#if defined(FEAT_DIRECTX)
2857 if (IS_ENABLE_DIRECTX())
2858 DWriteContext_BindDC(s_dwc, s_hdc, &ps.rcPaint);
2859#endif
2860 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2861 ps.rcPaint.right - ps.rcPaint.left + 1,
2862 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2863 }
2864
2865#if defined(FEAT_DIRECTX)
2866 if (IS_ENABLE_DIRECTX())
2867 DWriteContext_EndDraw(s_dwc);
2868#endif
2869 EndPaint(hwnd, &ps);
2870 }
2871}
2872
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002873 static void
2874_OnSize(
2875 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002876 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002877 int cx,
2878 int cy)
2879{
2880 if (!IsMinimized(hwnd))
2881 {
2882 gui_resize_shell(cx, cy);
2883
2884#ifdef FEAT_MENU
2885 /* Menu bar may wrap differently now */
2886 gui_mswin_get_menu_height(TRUE);
2887#endif
2888 }
2889}
2890
2891 static void
2892_OnSetFocus(
2893 HWND hwnd,
2894 HWND hwndOldFocus)
2895{
2896 gui_focus_change(TRUE);
2897 s_getting_focus = TRUE;
2898 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2899}
2900
2901 static void
2902_OnKillFocus(
2903 HWND hwnd,
2904 HWND hwndNewFocus)
2905{
2906 gui_focus_change(FALSE);
2907 s_getting_focus = FALSE;
2908 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2909}
2910
2911/*
2912 * Get a message when the user switches back to vim
2913 */
2914 static LRESULT
2915_OnActivateApp(
2916 HWND hwnd,
2917 BOOL fActivate,
2918 DWORD dwThreadId)
2919{
2920 /* we call gui_focus_change() in _OnSetFocus() */
2921 /* gui_focus_change((int)fActivate); */
2922 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2923}
2924
2925#if defined(FEAT_WINDOWS) || defined(PROTO)
2926 void
2927gui_mch_destroy_scrollbar(scrollbar_T *sb)
2928{
2929 DestroyWindow(sb->id);
2930}
2931#endif
2932
2933/*
2934 * Get current mouse coordinates in text window.
2935 */
2936 void
2937gui_mch_getmouse(int *x, int *y)
2938{
2939 RECT rct;
2940 POINT mp;
2941
2942 (void)GetWindowRect(s_textArea, &rct);
2943 (void)GetCursorPos((LPPOINT)&mp);
2944 *x = (int)(mp.x - rct.left);
2945 *y = (int)(mp.y - rct.top);
2946}
2947
2948/*
2949 * Move mouse pointer to character at (x, y).
2950 */
2951 void
2952gui_mch_setmouse(int x, int y)
2953{
2954 RECT rct;
2955
2956 (void)GetWindowRect(s_textArea, &rct);
2957 (void)SetCursorPos(x + gui.border_offset + rct.left,
2958 y + gui.border_offset + rct.top);
2959}
2960
2961 static void
2962gui_mswin_get_valid_dimensions(
2963 int w,
2964 int h,
2965 int *valid_w,
2966 int *valid_h)
2967{
2968 int base_width, base_height;
2969
2970 base_width = gui_get_base_width()
2971 + (GetSystemMetrics(SM_CXFRAME) +
2972 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
2973 base_height = gui_get_base_height()
2974 + (GetSystemMetrics(SM_CYFRAME) +
2975 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
2976 + GetSystemMetrics(SM_CYCAPTION)
2977#ifdef FEAT_MENU
2978 + gui_mswin_get_menu_height(FALSE)
2979#endif
2980 ;
2981 *valid_w = base_width +
2982 ((w - base_width) / gui.char_width) * gui.char_width;
2983 *valid_h = base_height +
2984 ((h - base_height) / gui.char_height) * gui.char_height;
2985}
2986
2987 void
2988gui_mch_flash(int msec)
2989{
2990 RECT rc;
2991
2992 /*
2993 * Note: InvertRect() excludes right and bottom of rectangle.
2994 */
2995 rc.left = 0;
2996 rc.top = 0;
2997 rc.right = gui.num_cols * gui.char_width;
2998 rc.bottom = gui.num_rows * gui.char_height;
2999 InvertRect(s_hdc, &rc);
3000 gui_mch_flush(); /* make sure it's displayed */
3001
3002 ui_delay((long)msec, TRUE); /* wait for a few msec */
3003
3004 InvertRect(s_hdc, &rc);
3005}
3006
3007/*
3008 * Return flags used for scrolling.
3009 * The SW_INVALIDATE is required when part of the window is covered or
3010 * off-screen. Refer to MS KB Q75236.
3011 */
3012 static int
3013get_scroll_flags(void)
3014{
3015 HWND hwnd;
3016 RECT rcVim, rcOther, rcDest;
3017
3018 GetWindowRect(s_hwnd, &rcVim);
3019
3020 /* Check if the window is partly above or below the screen. We don't care
3021 * about partly left or right of the screen, it is not relevant when
3022 * scrolling up or down. */
3023 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
3024 return SW_INVALIDATE;
3025
3026 /* Check if there is an window (partly) on top of us. */
3027 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3028 if (IsWindowVisible(hwnd))
3029 {
3030 GetWindowRect(hwnd, &rcOther);
3031 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3032 return SW_INVALIDATE;
3033 }
3034 return 0;
3035}
3036
3037/*
3038 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3039 * may not be scrolled out properly.
3040 * For gVim, when _OnScroll() is repeated, the character at the
3041 * previous cursor position may be left drawn after scroll.
3042 * The problem can be avoided by calling GetPixel() to get a pixel in
3043 * the region before ScrollWindowEx().
3044 */
3045 static void
3046intel_gpu_workaround(void)
3047{
3048 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3049}
3050
3051/*
3052 * Delete the given number of lines from the given row, scrolling up any
3053 * text further down within the scroll region.
3054 */
3055 void
3056gui_mch_delete_lines(
3057 int row,
3058 int num_lines)
3059{
3060 RECT rc;
3061
3062 intel_gpu_workaround();
3063
3064 rc.left = FILL_X(gui.scroll_region_left);
3065 rc.right = FILL_X(gui.scroll_region_right + 1);
3066 rc.top = FILL_Y(row);
3067 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3068
3069 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
3070 &rc, &rc, NULL, NULL, get_scroll_flags());
3071
3072 UpdateWindow(s_textArea);
3073 /* This seems to be required to avoid the cursor disappearing when
3074 * scrolling such that the cursor ends up in the top-left character on
3075 * the screen... But why? (Webb) */
3076 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3077 /* gui.cursor_is_valid = FALSE; */
3078
3079 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3080 gui.scroll_region_left,
3081 gui.scroll_region_bot, gui.scroll_region_right);
3082}
3083
3084/*
3085 * Insert the given number of lines before the given row, scrolling down any
3086 * following text within the scroll region.
3087 */
3088 void
3089gui_mch_insert_lines(
3090 int row,
3091 int num_lines)
3092{
3093 RECT rc;
3094
3095 intel_gpu_workaround();
3096
3097 rc.left = FILL_X(gui.scroll_region_left);
3098 rc.right = FILL_X(gui.scroll_region_right + 1);
3099 rc.top = FILL_Y(row);
3100 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3101 /* The SW_INVALIDATE is required when part of the window is covered or
3102 * off-screen. How do we avoid it when it's not needed? */
3103 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
3104 &rc, &rc, NULL, NULL, get_scroll_flags());
3105
3106 UpdateWindow(s_textArea);
3107
3108 gui_clear_block(row, gui.scroll_region_left,
3109 row + num_lines - 1, gui.scroll_region_right);
3110}
3111
3112
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003113 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003114gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003115{
3116#if defined(FEAT_DIRECTX)
3117 DWriteContext_Close(s_dwc);
3118 DWrite_Final();
3119 s_dwc = NULL;
3120#endif
3121
3122 ReleaseDC(s_textArea, s_hdc);
3123 DeleteObject(s_brush);
3124
3125#ifdef FEAT_TEAROFF
3126 /* Unload the tearoff bitmap */
3127 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3128#endif
3129
3130 /* Destroy our window (if we have one). */
3131 if (s_hwnd != NULL)
3132 {
3133 destroying = TRUE; /* ignore WM_DESTROY message now */
3134 DestroyWindow(s_hwnd);
3135 }
3136
3137#ifdef GLOBAL_IME
3138 global_ime_end();
3139#endif
3140}
3141
3142 static char_u *
3143logfont2name(LOGFONT lf)
3144{
3145 char *p;
3146 char *res;
3147 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003148 char *quality_name;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003149 char *font_name = lf.lfFaceName;
3150
3151 charset_name = charset_id2name((int)lf.lfCharSet);
3152#ifdef FEAT_MBYTE
3153 /* Convert a font name from the current codepage to 'encoding'.
3154 * TODO: Use Wide APIs (including LOGFONTW) instead of ANSI APIs. */
3155 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
3156 {
3157 int len;
3158 acp_to_enc((char_u *)lf.lfFaceName, (int)strlen(lf.lfFaceName),
3159 (char_u **)&font_name, &len);
3160 }
3161#endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003162 quality_name = quality_id2name((int)lf.lfQuality);
3163
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003164 res = (char *)alloc((unsigned)(strlen(font_name) + 20
3165 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
3166 if (res != NULL)
3167 {
3168 p = res;
3169 /* make a normal font string out of the lf thing:*/
3170 sprintf((char *)p, "%s:h%d", font_name, pixels_to_points(
3171 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
3172 while (*p)
3173 {
3174 if (*p == ' ')
3175 *p = '_';
3176 ++p;
3177 }
3178 if (lf.lfItalic)
3179 STRCAT(p, ":i");
3180 if (lf.lfWeight >= FW_BOLD)
3181 STRCAT(p, ":b");
3182 if (lf.lfUnderline)
3183 STRCAT(p, ":u");
3184 if (lf.lfStrikeOut)
3185 STRCAT(p, ":s");
3186 if (charset_name != NULL)
3187 {
3188 STRCAT(p, ":c");
3189 STRCAT(p, charset_name);
3190 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003191 if (quality_name != NULL)
3192 {
3193 STRCAT(p, ":q");
3194 STRCAT(p, quality_name);
3195 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003196 }
3197
3198#ifdef FEAT_MBYTE
3199 if (font_name != lf.lfFaceName)
3200 vim_free(font_name);
3201#endif
3202 return (char_u *)res;
3203}
3204
3205
3206#ifdef FEAT_MBYTE_IME
3207/*
3208 * Set correct LOGFONT to IME. Use 'guifontwide' if available, otherwise use
3209 * 'guifont'
3210 */
3211 static void
3212update_im_font(void)
3213{
3214 LOGFONT lf_wide;
3215
3216 if (p_guifontwide != NULL && *p_guifontwide != NUL
3217 && gui.wide_font != NOFONT
3218 && GetObject((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
3219 norm_logfont = lf_wide;
3220 else
3221 norm_logfont = sub_logfont;
3222 im_set_font(&norm_logfont);
3223}
3224#endif
3225
3226#ifdef FEAT_MBYTE
3227/*
3228 * Handler of gui.wide_font (p_guifontwide) changed notification.
3229 */
3230 void
3231gui_mch_wide_font_changed(void)
3232{
3233 LOGFONT lf;
3234
3235# ifdef FEAT_MBYTE_IME
3236 update_im_font();
3237# endif
3238
3239 gui_mch_free_font(gui.wide_ital_font);
3240 gui.wide_ital_font = NOFONT;
3241 gui_mch_free_font(gui.wide_bold_font);
3242 gui.wide_bold_font = NOFONT;
3243 gui_mch_free_font(gui.wide_boldital_font);
3244 gui.wide_boldital_font = NOFONT;
3245
3246 if (gui.wide_font
3247 && GetObject((HFONT)gui.wide_font, sizeof(lf), &lf))
3248 {
3249 if (!lf.lfItalic)
3250 {
3251 lf.lfItalic = TRUE;
3252 gui.wide_ital_font = get_font_handle(&lf);
3253 lf.lfItalic = FALSE;
3254 }
3255 if (lf.lfWeight < FW_BOLD)
3256 {
3257 lf.lfWeight = FW_BOLD;
3258 gui.wide_bold_font = get_font_handle(&lf);
3259 if (!lf.lfItalic)
3260 {
3261 lf.lfItalic = TRUE;
3262 gui.wide_boldital_font = get_font_handle(&lf);
3263 }
3264 }
3265 }
3266}
3267#endif
3268
3269/*
3270 * Initialise vim to use the font with the given name.
3271 * Return FAIL if the font could not be loaded, OK otherwise.
3272 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003273 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003274gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003275{
3276 LOGFONT lf;
3277 GuiFont font = NOFONT;
3278 char_u *p;
3279
3280 /* Load the font */
3281 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3282 font = get_font_handle(&lf);
3283 if (font == NOFONT)
3284 return FAIL;
3285
3286 if (font_name == NULL)
3287 font_name = (char_u *)lf.lfFaceName;
3288#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3289 norm_logfont = lf;
3290 sub_logfont = lf;
3291#endif
3292#ifdef FEAT_MBYTE_IME
3293 update_im_font();
3294#endif
3295 gui_mch_free_font(gui.norm_font);
3296 gui.norm_font = font;
3297 current_font_height = lf.lfHeight;
3298 GetFontSize(font);
3299
3300 p = logfont2name(lf);
3301 if (p != NULL)
3302 {
3303 hl_set_font_name(p);
3304
3305 /* When setting 'guifont' to "*" replace it with the actual font name.
3306 * */
3307 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3308 {
3309 vim_free(p_guifont);
3310 p_guifont = p;
3311 }
3312 else
3313 vim_free(p);
3314 }
3315
3316 gui_mch_free_font(gui.ital_font);
3317 gui.ital_font = NOFONT;
3318 gui_mch_free_font(gui.bold_font);
3319 gui.bold_font = NOFONT;
3320 gui_mch_free_font(gui.boldital_font);
3321 gui.boldital_font = NOFONT;
3322
3323 if (!lf.lfItalic)
3324 {
3325 lf.lfItalic = TRUE;
3326 gui.ital_font = get_font_handle(&lf);
3327 lf.lfItalic = FALSE;
3328 }
3329 if (lf.lfWeight < FW_BOLD)
3330 {
3331 lf.lfWeight = FW_BOLD;
3332 gui.bold_font = get_font_handle(&lf);
3333 if (!lf.lfItalic)
3334 {
3335 lf.lfItalic = TRUE;
3336 gui.boldital_font = get_font_handle(&lf);
3337 }
3338 }
3339
3340 return OK;
3341}
3342
3343#ifndef WPF_RESTORETOMAXIMIZED
3344# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3345#endif
3346
3347/*
3348 * Return TRUE if the GUI window is maximized, filling the whole screen.
3349 */
3350 int
3351gui_mch_maximized(void)
3352{
3353 WINDOWPLACEMENT wp;
3354
3355 wp.length = sizeof(WINDOWPLACEMENT);
3356 if (GetWindowPlacement(s_hwnd, &wp))
3357 return wp.showCmd == SW_SHOWMAXIMIZED
3358 || (wp.showCmd == SW_SHOWMINIMIZED
3359 && wp.flags == WPF_RESTORETOMAXIMIZED);
3360
3361 return 0;
3362}
3363
3364/*
3365 * Called when the font changed while the window is maximized. Compute the
3366 * new Rows and Columns. This is like resizing the window.
3367 */
3368 void
3369gui_mch_newfont(void)
3370{
3371 RECT rect;
3372
3373 GetWindowRect(s_hwnd, &rect);
3374 if (win_socket_id == 0)
3375 {
3376 gui_resize_shell(rect.right - rect.left
3377 - (GetSystemMetrics(SM_CXFRAME) +
3378 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3379 rect.bottom - rect.top
3380 - (GetSystemMetrics(SM_CYFRAME) +
3381 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3382 - GetSystemMetrics(SM_CYCAPTION)
3383#ifdef FEAT_MENU
3384 - gui_mswin_get_menu_height(FALSE)
3385#endif
3386 );
3387 }
3388 else
3389 {
3390 /* Inside another window, don't use the frame and border. */
3391 gui_resize_shell(rect.right - rect.left,
3392 rect.bottom - rect.top
3393#ifdef FEAT_MENU
3394 - gui_mswin_get_menu_height(FALSE)
3395#endif
3396 );
3397 }
3398}
3399
3400/*
3401 * Set the window title
3402 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003403 void
3404gui_mch_settitle(
3405 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003406 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003407{
3408 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3409}
3410
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003411#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003412/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3413 * misc2.c! */
3414static LPCSTR mshape_idcs[] =
3415{
3416 IDC_ARROW, /* arrow */
3417 MAKEINTRESOURCE(0), /* blank */
3418 IDC_IBEAM, /* beam */
3419 IDC_SIZENS, /* updown */
3420 IDC_SIZENS, /* udsizing */
3421 IDC_SIZEWE, /* leftright */
3422 IDC_SIZEWE, /* lrsizing */
3423 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003424 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003425 IDC_ARROW, /* crosshair */
3426 IDC_ARROW, /* hand1 */
3427 IDC_ARROW, /* hand2 */
3428 IDC_ARROW, /* pencil */
3429 IDC_ARROW, /* question */
3430 IDC_ARROW, /* right-arrow */
3431 IDC_UPARROW, /* up-arrow */
3432 IDC_ARROW /* last one */
3433};
3434
3435 void
3436mch_set_mouse_shape(int shape)
3437{
3438 LPCSTR idc;
3439
3440 if (shape == MSHAPE_HIDE)
3441 ShowCursor(FALSE);
3442 else
3443 {
3444 if (shape >= MSHAPE_NUMBERED)
3445 idc = IDC_ARROW;
3446 else
3447 idc = mshape_idcs[shape];
3448#ifdef SetClassLongPtr
3449 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3450#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003451 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003452#endif
3453 if (!p_mh)
3454 {
3455 POINT mp;
3456
3457 /* Set the position to make it redrawn with the new shape. */
3458 (void)GetCursorPos((LPPOINT)&mp);
3459 (void)SetCursorPos(mp.x, mp.y);
3460 ShowCursor(TRUE);
3461 }
3462 }
3463}
3464#endif
3465
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003466#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003467/*
3468 * The file browser exists in two versions: with "W" uses wide characters,
3469 * without "W" the current codepage. When FEAT_MBYTE is defined and on
3470 * Windows NT/2000/XP the "W" functions are used.
3471 */
3472
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003473# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003474/*
3475 * Wide version of convert_filter().
3476 */
3477 static WCHAR *
3478convert_filterW(char_u *s)
3479{
3480 char_u *tmp;
3481 int len;
3482 WCHAR *res;
3483
3484 tmp = convert_filter(s);
3485 if (tmp == NULL)
3486 return NULL;
3487 len = (int)STRLEN(s) + 3;
3488 res = enc_to_utf16(tmp, &len);
3489 vim_free(tmp);
3490 return res;
3491}
3492
3493/*
3494 * Wide version of gui_mch_browse(). Keep in sync!
3495 */
3496 static char_u *
3497gui_mch_browseW(
3498 int saving,
3499 char_u *title,
3500 char_u *dflt,
3501 char_u *ext,
3502 char_u *initdir,
3503 char_u *filter)
3504{
3505 /* We always use the wide function. This means enc_to_utf16() must work,
3506 * otherwise it fails miserably! */
3507 OPENFILENAMEW fileStruct;
3508 WCHAR fileBuf[MAXPATHL];
3509 WCHAR *wp;
3510 int i;
3511 WCHAR *titlep = NULL;
3512 WCHAR *extp = NULL;
3513 WCHAR *initdirp = NULL;
3514 WCHAR *filterp;
3515 char_u *p;
3516
3517 if (dflt == NULL)
3518 fileBuf[0] = NUL;
3519 else
3520 {
3521 wp = enc_to_utf16(dflt, NULL);
3522 if (wp == NULL)
3523 fileBuf[0] = NUL;
3524 else
3525 {
3526 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3527 fileBuf[i] = wp[i];
3528 fileBuf[i] = NUL;
3529 vim_free(wp);
3530 }
3531 }
3532
3533 /* Convert the filter to Windows format. */
3534 filterp = convert_filterW(filter);
3535
3536 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003537# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003538 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003539 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003540# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003541 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003542# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003543
3544 if (title != NULL)
3545 titlep = enc_to_utf16(title, NULL);
3546 fileStruct.lpstrTitle = titlep;
3547
3548 if (ext != NULL)
3549 extp = enc_to_utf16(ext, NULL);
3550 fileStruct.lpstrDefExt = extp;
3551
3552 fileStruct.lpstrFile = fileBuf;
3553 fileStruct.nMaxFile = MAXPATHL;
3554 fileStruct.lpstrFilter = filterp;
3555 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3556 /* has an initial dir been specified? */
3557 if (initdir != NULL && *initdir != NUL)
3558 {
3559 /* Must have backslashes here, no matter what 'shellslash' says */
3560 initdirp = enc_to_utf16(initdir, NULL);
3561 if (initdirp != NULL)
3562 {
3563 for (wp = initdirp; *wp != NUL; ++wp)
3564 if (*wp == '/')
3565 *wp = '\\';
3566 }
3567 fileStruct.lpstrInitialDir = initdirp;
3568 }
3569
3570 /*
3571 * TODO: Allow selection of multiple files. Needs another arg to this
3572 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3573 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3574 * files that don't exist yet, so I haven't put it in. What about
3575 * OFN_PATHMUSTEXIST?
3576 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3577 */
3578 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003579# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003580 if (curbuf->b_p_bin)
3581 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003582# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003583 if (saving)
3584 {
3585 if (!GetSaveFileNameW(&fileStruct))
3586 return NULL;
3587 }
3588 else
3589 {
3590 if (!GetOpenFileNameW(&fileStruct))
3591 return NULL;
3592 }
3593
3594 vim_free(filterp);
3595 vim_free(initdirp);
3596 vim_free(titlep);
3597 vim_free(extp);
3598
3599 /* Convert from UCS2 to 'encoding'. */
3600 p = utf16_to_enc(fileBuf, NULL);
3601 if (p != NULL)
3602 /* when out of memory we get garbage for non-ASCII chars */
3603 STRCPY(fileBuf, p);
3604 vim_free(p);
3605
3606 /* Give focus back to main window (when using MDI). */
3607 SetFocus(s_hwnd);
3608
3609 /* Shorten the file name if possible */
3610 return vim_strsave(shorten_fname1((char_u *)fileBuf));
3611}
3612# endif /* FEAT_MBYTE */
3613
3614
3615/*
3616 * Convert the string s to the proper format for a filter string by replacing
3617 * the \t and \n delimiters with \0.
3618 * Returns the converted string in allocated memory.
3619 *
3620 * Keep in sync with convert_filterW() above!
3621 */
3622 static char_u *
3623convert_filter(char_u *s)
3624{
3625 char_u *res;
3626 unsigned s_len = (unsigned)STRLEN(s);
3627 unsigned i;
3628
3629 res = alloc(s_len + 3);
3630 if (res != NULL)
3631 {
3632 for (i = 0; i < s_len; ++i)
3633 if (s[i] == '\t' || s[i] == '\n')
3634 res[i] = '\0';
3635 else
3636 res[i] = s[i];
3637 res[s_len] = NUL;
3638 /* Add two extra NULs to make sure it's properly terminated. */
3639 res[s_len + 1] = NUL;
3640 res[s_len + 2] = NUL;
3641 }
3642 return res;
3643}
3644
3645/*
3646 * Select a directory.
3647 */
3648 char_u *
3649gui_mch_browsedir(char_u *title, char_u *initdir)
3650{
3651 /* We fake this: Use a filter that doesn't select anything and a default
3652 * file name that won't be used. */
3653 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3654 initdir, (char_u *)_("Directory\t*.nothing\n"));
3655}
3656
3657/*
3658 * Pop open a file browser and return the file selected, in allocated memory,
3659 * or NULL if Cancel is hit.
3660 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3661 * title - Title message for the file browser dialog.
3662 * dflt - Default name of file.
3663 * ext - Default extension to be added to files without extensions.
3664 * initdir - directory in which to open the browser (NULL = current dir)
3665 * filter - Filter for matched files to choose from.
3666 *
3667 * Keep in sync with gui_mch_browseW() above!
3668 */
3669 char_u *
3670gui_mch_browse(
3671 int saving,
3672 char_u *title,
3673 char_u *dflt,
3674 char_u *ext,
3675 char_u *initdir,
3676 char_u *filter)
3677{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003678# ifdef FEAT_MBYTE
3679 return gui_mch_browseW(saving, title, dflt, ext, initdir, filter);
3680# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003681 OPENFILENAME fileStruct;
3682 char_u fileBuf[MAXPATHL];
3683 char_u *initdirp = NULL;
3684 char_u *filterp;
3685 char_u *p;
3686
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003687 if (dflt == NULL)
3688 fileBuf[0] = NUL;
3689 else
3690 vim_strncpy(fileBuf, dflt, MAXPATHL - 1);
3691
3692 /* Convert the filter to Windows format. */
3693 filterp = convert_filter(filter);
3694
3695 vim_memset(&fileStruct, 0, sizeof(OPENFILENAME));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003696# ifdef OPENFILENAME_SIZE_VERSION_400
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003697 /* be compatible with Windows NT 4.0 */
3698 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003699# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003700 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003701# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003702
3703 fileStruct.lpstrTitle = (LPSTR)title;
3704 fileStruct.lpstrDefExt = (LPSTR)ext;
3705
3706 fileStruct.lpstrFile = (LPSTR)fileBuf;
3707 fileStruct.nMaxFile = MAXPATHL;
3708 fileStruct.lpstrFilter = (LPSTR)filterp;
3709 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3710 /* has an initial dir been specified? */
3711 if (initdir != NULL && *initdir != NUL)
3712 {
3713 /* Must have backslashes here, no matter what 'shellslash' says */
3714 initdirp = vim_strsave(initdir);
3715 if (initdirp != NULL)
3716 for (p = initdirp; *p != NUL; ++p)
3717 if (*p == '/')
3718 *p = '\\';
3719 fileStruct.lpstrInitialDir = (LPSTR)initdirp;
3720 }
3721
3722 /*
3723 * TODO: Allow selection of multiple files. Needs another arg to this
3724 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3725 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3726 * files that don't exist yet, so I haven't put it in. What about
3727 * OFN_PATHMUSTEXIST?
3728 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3729 */
3730 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003731# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003732 if (curbuf->b_p_bin)
3733 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003734# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003735 if (saving)
3736 {
3737 if (!GetSaveFileName(&fileStruct))
3738 return NULL;
3739 }
3740 else
3741 {
3742 if (!GetOpenFileName(&fileStruct))
3743 return NULL;
3744 }
3745
3746 vim_free(filterp);
3747 vim_free(initdirp);
3748
3749 /* Give focus back to main window (when using MDI). */
3750 SetFocus(s_hwnd);
3751
3752 /* Shorten the file name if possible */
3753 return vim_strsave(shorten_fname1((char_u *)fileBuf));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003754# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003755}
3756#endif /* FEAT_BROWSE */
3757
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003758 static void
3759_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003760 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003761 HDROP hDrop)
3762{
3763#ifdef FEAT_WINDOWS
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003764# define BUFPATHLEN _MAX_PATH
3765# define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003766# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003767 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003768# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003769 char szFile[BUFPATHLEN];
3770 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3771 UINT i;
3772 char_u **fnames;
3773 POINT pt;
3774 int_u modifiers = 0;
3775
3776 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3777
3778 /* Obtain dropped position */
3779 DragQueryPoint(hDrop, &pt);
3780 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3781
3782 reset_VIsual();
3783
3784 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3785
3786 if (fnames != NULL)
3787 for (i = 0; i < cFiles; ++i)
3788 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003789# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003790 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3791 fnames[i] = utf16_to_enc(wszFile, NULL);
3792 else
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003793# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003794 {
3795 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3796 fnames[i] = vim_strsave((char_u *)szFile);
3797 }
3798 }
3799
3800 DragFinish(hDrop);
3801
3802 if (fnames != NULL)
3803 {
3804 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3805 modifiers |= MOUSE_SHIFT;
3806 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3807 modifiers |= MOUSE_CTRL;
3808 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3809 modifiers |= MOUSE_ALT;
3810
3811 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3812
3813 s_need_activate = TRUE;
3814 }
3815#endif
3816}
3817
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003818 static int
3819_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003820 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003821 HWND hwndCtl,
3822 UINT code,
3823 int pos)
3824{
3825 static UINT prev_code = 0; /* code of previous call */
3826 scrollbar_T *sb, *sb_info;
3827 long val;
3828 int dragging = FALSE;
3829 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003830 SCROLLINFO si;
3831
3832 si.cbSize = sizeof(si);
3833 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003834
3835 sb = gui_mswin_find_scrollbar(hwndCtl);
3836 if (sb == NULL)
3837 return 0;
3838
3839 if (sb->wp != NULL) /* Left or right scrollbar */
3840 {
3841 /*
3842 * Careful: need to get scrollbar info out of first (left) scrollbar
3843 * for window, but keep real scrollbar too because we must pass it to
3844 * gui_drag_scrollbar().
3845 */
3846 sb_info = &sb->wp->w_scrollbars[0];
3847 }
3848 else /* Bottom scrollbar */
3849 sb_info = sb;
3850 val = sb_info->value;
3851
3852 switch (code)
3853 {
3854 case SB_THUMBTRACK:
3855 val = pos;
3856 dragging = TRUE;
3857 if (sb->scroll_shift > 0)
3858 val <<= sb->scroll_shift;
3859 break;
3860 case SB_LINEDOWN:
3861 val++;
3862 break;
3863 case SB_LINEUP:
3864 val--;
3865 break;
3866 case SB_PAGEDOWN:
3867 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3868 break;
3869 case SB_PAGEUP:
3870 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3871 break;
3872 case SB_TOP:
3873 val = 0;
3874 break;
3875 case SB_BOTTOM:
3876 val = sb_info->max;
3877 break;
3878 case SB_ENDSCROLL:
3879 if (prev_code == SB_THUMBTRACK)
3880 {
3881 /*
3882 * "pos" only gives us 16-bit data. In case of large file,
3883 * use GetScrollPos() which returns 32-bit. Unfortunately it
3884 * is not valid while the scrollbar is being dragged.
3885 */
3886 val = GetScrollPos(hwndCtl, SB_CTL);
3887 if (sb->scroll_shift > 0)
3888 val <<= sb->scroll_shift;
3889 }
3890 break;
3891
3892 default:
3893 /* TRACE("Unknown scrollbar event %d\n", code); */
3894 return 0;
3895 }
3896 prev_code = code;
3897
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003898 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3899 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003900
3901 /*
3902 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3903 */
3904 if (sb->wp != NULL)
3905 {
3906 scrollbar_T *sba = sb->wp->w_scrollbars;
3907 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3908
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003909 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003910 }
3911
3912 /* Don't let us be interrupted here by another message. */
3913 s_busy_processing = TRUE;
3914
3915 /* When "allow_scrollbar" is FALSE still need to remember the new
3916 * position, but don't actually scroll by setting "dont_scroll". */
3917 dont_scroll = !allow_scrollbar;
3918
3919 gui_drag_scrollbar(sb, val, dragging);
3920
3921 s_busy_processing = FALSE;
3922 dont_scroll = dont_scroll_save;
3923
3924 return 0;
3925}
3926
3927
3928/*
3929 * Get command line arguments.
3930 * Use "prog" as the name of the program and "cmdline" as the arguments.
3931 * Copy the arguments to allocated memory.
3932 * Return the number of arguments (including program name).
3933 * Return pointers to the arguments in "argvp". Memory is allocated with
3934 * malloc(), use free() instead of vim_free().
3935 * Return pointer to buffer in "tofree".
3936 * Returns zero when out of memory.
3937 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003938 int
3939get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
3940{
3941 int i;
3942 char *p;
3943 char *progp;
3944 char *pnew = NULL;
3945 char *newcmdline;
3946 int inquote;
3947 int argc;
3948 char **argv = NULL;
3949 int round;
3950
3951 *tofree = NULL;
3952
3953#ifdef FEAT_MBYTE
3954 /* Try using the Unicode version first, it takes care of conversion when
3955 * 'encoding' is changed. */
3956 argc = get_cmd_argsW(&argv);
3957 if (argc != 0)
3958 goto done;
3959#endif
3960
3961 /* Handle the program name. Remove the ".exe" extension, and find the 1st
3962 * non-space. */
3963 p = strrchr(prog, '.');
3964 if (p != NULL)
3965 *p = NUL;
3966 for (progp = prog; *progp == ' '; ++progp)
3967 ;
3968
3969 /* The command line is copied to allocated memory, so that we can change
3970 * it. Add the size of the string, the separating NUL and a terminating
3971 * NUL. */
3972 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
3973 if (newcmdline == NULL)
3974 return 0;
3975
3976 /*
3977 * First round: count the number of arguments ("pnew" == NULL).
3978 * Second round: produce the arguments.
3979 */
3980 for (round = 1; round <= 2; ++round)
3981 {
3982 /* First argument is the program name. */
3983 if (pnew != NULL)
3984 {
3985 argv[0] = pnew;
3986 strcpy(pnew, progp);
3987 pnew += strlen(pnew);
3988 *pnew++ = NUL;
3989 }
3990
3991 /*
3992 * Isolate each argument and put it in argv[].
3993 */
3994 p = cmdline;
3995 argc = 1;
3996 while (*p != NUL)
3997 {
3998 inquote = FALSE;
3999 if (pnew != NULL)
4000 argv[argc] = pnew;
4001 ++argc;
4002 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
4003 {
4004 /* Backslashes are only special when followed by a double
4005 * quote. */
4006 i = (int)strspn(p, "\\");
4007 if (p[i] == '"')
4008 {
4009 /* Halve the number of backslashes. */
4010 if (i > 1 && pnew != NULL)
4011 {
4012 vim_memset(pnew, '\\', i / 2);
4013 pnew += i / 2;
4014 }
4015
4016 /* Even nr of backslashes toggles quoting, uneven copies
4017 * the double quote. */
4018 if ((i & 1) == 0)
4019 inquote = !inquote;
4020 else if (pnew != NULL)
4021 *pnew++ = '"';
4022 p += i + 1;
4023 }
4024 else if (i > 0)
4025 {
4026 /* Copy span of backslashes unmodified. */
4027 if (pnew != NULL)
4028 {
4029 vim_memset(pnew, '\\', i);
4030 pnew += i;
4031 }
4032 p += i;
4033 }
4034 else
4035 {
4036 if (pnew != NULL)
4037 *pnew++ = *p;
4038#ifdef FEAT_MBYTE
4039 /* Can't use mb_* functions, because 'encoding' is not
4040 * initialized yet here. */
4041 if (IsDBCSLeadByte(*p))
4042 {
4043 ++p;
4044 if (pnew != NULL)
4045 *pnew++ = *p;
4046 }
4047#endif
4048 ++p;
4049 }
4050 }
4051
4052 if (pnew != NULL)
4053 *pnew++ = NUL;
4054 while (*p == ' ' || *p == '\t')
4055 ++p; /* advance until a non-space */
4056 }
4057
4058 if (round == 1)
4059 {
4060 argv = (char **)malloc((argc + 1) * sizeof(char *));
4061 if (argv == NULL )
4062 {
4063 free(newcmdline);
4064 return 0; /* malloc error */
4065 }
4066 pnew = newcmdline;
4067 *tofree = newcmdline;
4068 }
4069 }
4070
4071#ifdef FEAT_MBYTE
4072done:
4073#endif
4074 argv[argc] = NULL; /* NULL-terminated list */
4075 *argvp = argv;
4076 return argc;
4077}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
4079#ifdef FEAT_XPM_W32
4080# include "xpm_w32.h"
4081#endif
4082
4083#ifdef PROTO
4084# define WINAPI
4085#endif
4086
4087#ifdef __MINGW32__
4088/*
4089 * Add a lot of missing defines.
4090 * They are not always missing, we need the #ifndef's.
4091 */
4092# ifndef _cdecl
4093# define _cdecl
4094# endif
4095# ifndef IsMinimized
4096# define IsMinimized(hwnd) IsIconic(hwnd)
4097# endif
4098# ifndef IsMaximized
4099# define IsMaximized(hwnd) IsZoomed(hwnd)
4100# endif
4101# ifndef SelectFont
4102# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
4103# endif
4104# ifndef GetStockBrush
4105# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
4106# endif
4107# ifndef DeleteBrush
4108# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
4109# endif
4110
4111# ifndef HANDLE_WM_RBUTTONDBLCLK
4112# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4113 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4114# endif
4115# ifndef HANDLE_WM_MBUTTONUP
4116# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
4117 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4118# endif
4119# ifndef HANDLE_WM_MBUTTONDBLCLK
4120# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4121 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4122# endif
4123# ifndef HANDLE_WM_LBUTTONDBLCLK
4124# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4125 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4126# endif
4127# ifndef HANDLE_WM_RBUTTONDOWN
4128# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
4129 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4130# endif
4131# ifndef HANDLE_WM_MOUSEMOVE
4132# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
4133 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4134# endif
4135# ifndef HANDLE_WM_RBUTTONUP
4136# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
4137 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4138# endif
4139# ifndef HANDLE_WM_MBUTTONDOWN
4140# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
4141 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4142# endif
4143# ifndef HANDLE_WM_LBUTTONUP
4144# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
4145 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4146# endif
4147# ifndef HANDLE_WM_LBUTTONDOWN
4148# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
4149 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4150# endif
4151# ifndef HANDLE_WM_SYSCHAR
4152# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
4153 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4154# endif
4155# ifndef HANDLE_WM_ACTIVATEAPP
4156# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
4157 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
4158# endif
4159# ifndef HANDLE_WM_WINDOWPOSCHANGING
4160# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
4161 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
4162# endif
4163# ifndef HANDLE_WM_VSCROLL
4164# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
4165 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4166# endif
4167# ifndef HANDLE_WM_SETFOCUS
4168# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
4169 ((fn)((hwnd), (HWND)(wParam)), 0L)
4170# endif
4171# ifndef HANDLE_WM_KILLFOCUS
4172# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
4173 ((fn)((hwnd), (HWND)(wParam)), 0L)
4174# endif
4175# ifndef HANDLE_WM_HSCROLL
4176# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
4177 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4178# endif
4179# ifndef HANDLE_WM_DROPFILES
4180# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
4181 ((fn)((hwnd), (HDROP)(wParam)), 0L)
4182# endif
4183# ifndef HANDLE_WM_CHAR
4184# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
4185 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4186# endif
4187# ifndef HANDLE_WM_SYSDEADCHAR
4188# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
4189 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4190# endif
4191# ifndef HANDLE_WM_DEADCHAR
4192# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4193 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4194# endif
4195#endif /* __MINGW32__ */
4196
4197
4198/* Some parameters for tearoff menus. All in pixels. */
4199#define TEAROFF_PADDING_X 2
4200#define TEAROFF_BUTTON_PAD_X 8
4201#define TEAROFF_MIN_WIDTH 200
4202#define TEAROFF_SUBMENU_LABEL ">>"
4203#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4204
4205
4206/* For the Intellimouse: */
4207#ifndef WM_MOUSEWHEEL
4208#define WM_MOUSEWHEEL 0x20a
4209#endif
4210
4211
4212#ifdef FEAT_BEVAL
4213# define ID_BEVAL_TOOLTIP 200
4214# define BEVAL_TEXT_LEN MAXPATHL
4215
Bram Moolenaar167632f2010-05-26 21:42:54 +02004216#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004217/* Work around old versions of basetsd.h which wrongly declares
4218 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02004219# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00004220# define UINT_PTR UINT
4221#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004222
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004223static void make_tooltip(BalloonEval *beval, char *text, POINT pt);
4224static void delete_tooltip(BalloonEval *beval);
4225static VOID CALLBACK BevalTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004226
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004228static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004230
Bram Moolenaar82881492012-11-20 16:53:39 +01004231
4232/* cproto fails on missing include files */
4233#ifndef PROTO
4234
Bram Moolenaar45360022005-07-21 21:08:21 +00004235/*
4236 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004237 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004238 */
Bram Moolenaar82881492012-11-20 16:53:39 +01004239# include <pshpack1.h>
4240
4241#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004242
4243typedef struct _DllVersionInfo
4244{
4245 DWORD cbSize;
4246 DWORD dwMajorVersion;
4247 DWORD dwMinorVersion;
4248 DWORD dwBuildNumber;
4249 DWORD dwPlatformID;
4250} DLLVERSIONINFO;
4251
Bram Moolenaar82881492012-11-20 16:53:39 +01004252#ifndef PROTO
4253# include <poppack.h>
4254#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004255
Bram Moolenaar45360022005-07-21 21:08:21 +00004256typedef struct tagTOOLINFOA_NEW
4257{
4258 UINT cbSize;
4259 UINT uFlags;
4260 HWND hwnd;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004261 UINT_PTR uId;
Bram Moolenaar45360022005-07-21 21:08:21 +00004262 RECT rect;
4263 HINSTANCE hinst;
4264 LPSTR lpszText;
4265 LPARAM lParam;
4266} TOOLINFO_NEW;
4267
4268typedef struct tagNMTTDISPINFO_NEW
4269{
4270 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004271 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004272 char szText[80];
4273 HINSTANCE hinst;
4274 UINT uFlags;
4275 LPARAM lParam;
4276} NMTTDISPINFO_NEW;
4277
Bram Moolenaar45360022005-07-21 21:08:21 +00004278typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4279#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004280# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281#endif
4282
Bram Moolenaar45360022005-07-21 21:08:21 +00004283#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004284# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004285#endif
4286
4287#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004288# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004289#endif
4290
4291#endif /* defined(FEAT_BEVAL) */
4292
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004293#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4294/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4295 * it here if LPNMTTDISPINFO isn't defined.
4296 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4297 * _MSC_VER. */
4298# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4299typedef struct tagNMTTDISPINFOA {
4300 NMHDR hdr;
4301 LPSTR lpszText;
4302 char szText[80];
4303 HINSTANCE hinst;
4304 UINT uFlags;
4305 LPARAM lParam;
4306} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4307# define LPNMTTDISPINFO LPNMTTDISPINFOA
4308
4309# ifdef FEAT_MBYTE
4310typedef struct tagNMTTDISPINFOW {
4311 NMHDR hdr;
4312 LPWSTR lpszText;
4313 WCHAR szText[80];
4314 HINSTANCE hinst;
4315 UINT uFlags;
4316 LPARAM lParam;
4317} NMTTDISPINFOW, *LPNMTTDISPINFOW;
4318# endif
4319# endif
4320#endif
4321
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004322#ifndef TTN_GETDISPINFOW
4323# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4324#endif
4325
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326/* Local variables: */
4327
4328#ifdef FEAT_MENU
4329static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331
4332/*
4333 * Use the system font for dialogs and tear-off menus. Remove this line to
4334 * use DLG_FONT_NAME.
4335 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004336#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337
4338#define VIM_NAME "vim"
4339#define VIM_CLASS "Vim"
4340#define VIM_CLASSW L"Vim"
4341
4342/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4343 * thus there should be room for every dialog. For tearoffs it's made bigger
4344 * when needed. */
4345#define DLG_ALLOC_SIZE 16 * 1024
4346
4347/*
4348 * stuff for dialogs, menus, tearoffs etc.
4349 */
4350static LRESULT APIENTRY dialog_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004351#ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352static LRESULT APIENTRY tearoff_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354static PWORD
4355add_dialog_element(
4356 PWORD p,
4357 DWORD lStyle,
4358 WORD x,
4359 WORD y,
4360 WORD w,
4361 WORD h,
4362 WORD Id,
4363 WORD clss,
4364 const char *caption);
4365static LPWORD lpwAlign(LPWORD);
4366static int nCopyAnsiToWideChar(LPWORD, LPSTR);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004367#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370static void get_dialog_font_metrics(void);
4371
4372static int dialog_default_button = -1;
4373
4374/* Intellimouse support */
4375static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376
4377static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4378#ifdef FEAT_TOOLBAR
4379static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004380static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381static int get_toolbar_bitmap(vimmenu_T *menu);
4382#endif
4383
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004384#ifdef FEAT_GUI_TABLINE
4385static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004386static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004387#endif
4388
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389#ifdef FEAT_MBYTE_IME
4390static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4391static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4392#endif
4393#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4394# ifdef NOIME
4395typedef struct tagCOMPOSITIONFORM {
4396 DWORD dwStyle;
4397 POINT ptCurrentPos;
4398 RECT rcArea;
4399} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4400typedef HANDLE HIMC;
4401# endif
4402
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004403static HINSTANCE hLibImm = NULL;
4404static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4405static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4406static HIMC (WINAPI *pImmGetContext)(HWND);
4407static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4408static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4409static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4410static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
4411static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
4412static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
4413static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4414static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004415static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416static void dyn_imm_load(void);
4417#else
4418# define pImmGetCompositionStringA ImmGetCompositionStringA
4419# define pImmGetCompositionStringW ImmGetCompositionStringW
4420# define pImmGetContext ImmGetContext
4421# define pImmAssociateContext ImmAssociateContext
4422# define pImmReleaseContext ImmReleaseContext
4423# define pImmGetOpenStatus ImmGetOpenStatus
4424# define pImmSetOpenStatus ImmSetOpenStatus
4425# define pImmGetCompositionFont ImmGetCompositionFontA
4426# define pImmSetCompositionFont ImmSetCompositionFontA
4427# define pImmSetCompositionWindow ImmSetCompositionWindow
4428# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004429# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430#endif
4431
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432#ifdef FEAT_MENU
4433/*
4434 * Figure out how high the menu bar is at the moment.
4435 */
4436 static int
4437gui_mswin_get_menu_height(
4438 int fix_window) /* If TRUE, resize window if menu height changed */
4439{
4440 static int old_menu_height = -1;
4441
4442 RECT rc1, rc2;
4443 int num;
4444 int menu_height;
4445
4446 if (gui.menu_is_active)
4447 num = GetMenuItemCount(s_menuBar);
4448 else
4449 num = 0;
4450
4451 if (num == 0)
4452 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004453 else if (IsMinimized(s_hwnd))
4454 {
4455 /* The height of the menu cannot be determined while the window is
4456 * minimized. Take the previous height if the menu is changed in that
4457 * state, to avoid that Vim's vertical window size accidentally
4458 * increases due to the unaccounted-for menu height. */
4459 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 else
4462 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004463 /*
4464 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4465 * seem to have been set yet, so menu wraps in default window
4466 * width which is very narrow. Instead just return height of a
4467 * single menu item. Will still be wrong when the menu really
4468 * should wrap over more than one line.
4469 */
4470 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4471 if (gui.starting)
4472 menu_height = rc1.bottom - rc1.top + 1;
4473 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004475 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4476 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 }
4478 }
4479
4480 if (fix_window && menu_height != old_menu_height)
4481 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004482 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 }
Bram Moolenaar71371b12015-03-24 17:57:12 +01004484 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485
4486 return menu_height;
4487}
4488#endif /*FEAT_MENU*/
4489
4490
4491/*
4492 * Setup for the Intellimouse
4493 */
4494 static void
4495init_mouse_wheel(void)
4496{
4497
4498#ifndef SPI_GETWHEELSCROLLLINES
4499# define SPI_GETWHEELSCROLLLINES 104
4500#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004501#ifndef SPI_SETWHEELSCROLLLINES
4502# define SPI_SETWHEELSCROLLLINES 105
4503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
4505#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4506#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4507#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4508#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4509
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 mouse_scroll_lines = 3; /* reasonable default */
4511
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004512 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4513 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4514 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515}
4516
4517
4518/* Intellimouse wheel handler */
4519 static void
4520_OnMouseWheel(
4521 HWND hwnd,
4522 short zDelta)
4523{
4524/* Treat a mouse wheel event as if it were a scroll request */
4525 int i;
4526 int size;
4527 HWND hwndCtl;
4528
4529 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4530 {
4531 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4532 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4533 }
4534 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4535 {
4536 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4537 size = curwin->w_scrollbars[SBAR_LEFT].size;
4538 }
4539 else
4540 return;
4541
4542 size = curwin->w_height;
4543 if (mouse_scroll_lines == 0)
4544 init_mouse_wheel();
4545
4546 if (mouse_scroll_lines > 0
4547 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4548 {
4549 for (i = mouse_scroll_lines; i > 0; --i)
4550 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4551 }
4552 else
4553 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
4554}
4555
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004556#ifdef USE_SYSMENU_FONT
4557/*
4558 * Get Menu Font.
4559 * Return OK or FAIL.
4560 */
4561 static int
4562gui_w32_get_menu_font(LOGFONT *lf)
4563{
4564 NONCLIENTMETRICS nm;
4565
4566 nm.cbSize = sizeof(NONCLIENTMETRICS);
4567 if (!SystemParametersInfo(
4568 SPI_GETNONCLIENTMETRICS,
4569 sizeof(NONCLIENTMETRICS),
4570 &nm,
4571 0))
4572 return FAIL;
4573 *lf = nm.lfMenuFont;
4574 return OK;
4575}
4576#endif
4577
4578
4579#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4580/*
4581 * Set the GUI tabline font to the system menu font
4582 */
4583 static void
4584set_tabline_font(void)
4585{
4586 LOGFONT lfSysmenu;
4587 HFONT font;
4588 HWND hwnd;
4589 HDC hdc;
4590 HFONT hfntOld;
4591 TEXTMETRIC tm;
4592
4593 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4594 return;
4595
4596 font = CreateFontIndirect(&lfSysmenu);
4597
4598 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4599
4600 /*
4601 * Compute the height of the font used for the tab text
4602 */
4603 hwnd = GetDesktopWindow();
4604 hdc = GetWindowDC(hwnd);
4605 hfntOld = SelectFont(hdc, font);
4606
4607 GetTextMetrics(hdc, &tm);
4608
4609 SelectFont(hdc, hfntOld);
4610 ReleaseDC(hwnd, hdc);
4611
4612 /*
4613 * The space used by the tab border and the space between the tab label
4614 * and the tab border is included as 7.
4615 */
4616 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4617}
4618#endif
4619
Bram Moolenaar520470a2005-06-16 21:59:56 +00004620/*
4621 * Invoked when a setting was changed.
4622 */
4623 static LRESULT CALLBACK
4624_OnSettingChange(UINT n)
4625{
4626 if (n == SPI_SETWHEELSCROLLLINES)
4627 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4628 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004629#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4630 if (n == SPI_SETNONCLIENTMETRICS)
4631 set_tabline_font();
4632#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004633 return 0;
4634}
4635
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636#ifdef FEAT_NETBEANS_INTG
4637 static void
4638_OnWindowPosChanged(
4639 HWND hwnd,
4640 const LPWINDOWPOS lpwpos)
4641{
4642 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004643 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644
4645 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4646 || lpwpos->cx != cx || lpwpos->cy != cy))
4647 {
4648 x = lpwpos->x;
4649 y = lpwpos->y;
4650 cx = lpwpos->cx;
4651 cy = lpwpos->cy;
4652 netbeans_frame_moved(x, y);
4653 }
4654 /* Allow to send WM_SIZE and WM_MOVE */
4655 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4656}
4657#endif
4658
4659 static int
4660_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 UINT fwSide,
4662 LPRECT lprc)
4663{
4664 int w, h;
4665 int valid_w, valid_h;
4666 int w_offset, h_offset;
4667
4668 w = lprc->right - lprc->left;
4669 h = lprc->bottom - lprc->top;
4670 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4671 w_offset = w - valid_w;
4672 h_offset = h - valid_h;
4673
4674 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4675 || fwSide == WMSZ_BOTTOMLEFT)
4676 lprc->left += w_offset;
4677 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4678 || fwSide == WMSZ_BOTTOMRIGHT)
4679 lprc->right -= w_offset;
4680
4681 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4682 || fwSide == WMSZ_TOPRIGHT)
4683 lprc->top += h_offset;
4684 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4685 || fwSide == WMSZ_BOTTOMRIGHT)
4686 lprc->bottom -= h_offset;
4687 return TRUE;
4688}
4689
4690
4691
4692 static LRESULT CALLBACK
4693_WndProc(
4694 HWND hwnd,
4695 UINT uMsg,
4696 WPARAM wParam,
4697 LPARAM lParam)
4698{
4699 /*
4700 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4701 hwnd, uMsg, wParam, lParam);
4702 */
4703
4704 HandleMouseHide(uMsg, lParam);
4705
4706 s_uMsg = uMsg;
4707 s_wParam = wParam;
4708 s_lParam = lParam;
4709
4710 switch (uMsg)
4711 {
4712 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4713 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4714 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4715 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4716 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4717 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4718 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4719 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4720 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4721#ifdef FEAT_MENU
4722 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4723#endif
4724 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4725 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4726 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4727 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4728 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4729 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4730 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4731 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4732 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4733#ifdef FEAT_NETBEANS_INTG
4734 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4735#endif
4736
Bram Moolenaarafa24992006-03-27 20:58:26 +00004737#ifdef FEAT_GUI_TABLINE
4738 case WM_RBUTTONUP:
4739 {
4740 if (gui_mch_showing_tabline())
4741 {
4742 POINT pt;
4743 RECT rect;
4744
4745 /*
4746 * If the cursor is on the tabline, display the tab menu
4747 */
4748 GetCursorPos((LPPOINT)&pt);
4749 GetWindowRect(s_textArea, &rect);
4750 if (pt.y < rect.top)
4751 {
4752 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004753 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004754 }
4755 }
4756 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4757 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004758 case WM_LBUTTONDBLCLK:
4759 {
4760 /*
4761 * If the user double clicked the tabline, create a new tab
4762 */
4763 if (gui_mch_showing_tabline())
4764 {
4765 POINT pt;
4766 RECT rect;
4767
4768 GetCursorPos((LPPOINT)&pt);
4769 GetWindowRect(s_textArea, &rect);
4770 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004771 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004772 }
4773 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4774 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004775#endif
4776
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 case WM_QUERYENDSESSION: /* System wants to go down. */
4778 gui_shell_closed(); /* Will exit when no changed buffers. */
4779 return FALSE; /* Do NOT allow system to go down. */
4780
4781 case WM_ENDSESSION:
4782 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004783 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004785 return 0L;
4786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 break;
4788
4789 case WM_CHAR:
4790 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4791 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004792 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 return 0L;
4794
4795 case WM_SYSCHAR:
4796 /*
4797 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4798 * shortcut key, handle like a typed ALT key, otherwise call Windows
4799 * ALT key handling.
4800 */
4801#ifdef FEAT_MENU
4802 if ( !gui.menu_is_active
4803 || p_wak[0] == 'n'
4804 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4805 )
4806#endif
4807 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004808 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 return 0L;
4810 }
4811#ifdef FEAT_MENU
4812 else
4813 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4814#endif
4815
4816 case WM_SYSKEYUP:
4817#ifdef FEAT_MENU
4818 /* This used to be done only when menu is active: ALT key is used for
4819 * that. But that caused problems when menu is disabled and using
4820 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4821 * are received, mouse pointer remains hidden. */
4822 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4823#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004824 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825#endif
4826
4827 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004828 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829
4830 case WM_MOUSEWHEEL:
4831 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004832 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833
Bram Moolenaar520470a2005-06-16 21:59:56 +00004834 /* Notification for change in SystemParametersInfo() */
4835 case WM_SETTINGCHANGE:
4836 return _OnSettingChange((UINT)wParam);
4837
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004838#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 case WM_NOTIFY:
4840 switch (((LPNMHDR) lParam)->code)
4841 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004842# ifdef FEAT_MBYTE
4843 case TTN_GETDISPINFOW:
4844# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004845 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004847 LPNMHDR hdr = (LPNMHDR)lParam;
4848 char_u *str = NULL;
4849 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004850
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004851 vim_free(tt_text);
4852 tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004853
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004854# ifdef FEAT_GUI_TABLINE
4855 if (gui_mch_showing_tabline()
4856 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004857 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004858 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004859 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004860 * Mouse is over the GUI tabline. Display the
4861 * tooltip for the tab under the cursor
4862 *
4863 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004864 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004865 GetCursorPos(&pt);
4866 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004867 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004868 TCHITTESTINFO htinfo;
4869 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004870
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004871 /*
4872 * Get the tab under the cursor
4873 */
4874 htinfo.pt.x = pt.x;
4875 htinfo.pt.y = pt.y;
4876 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4877 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004878 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004879 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004880
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004881 tp = find_tabpage(idx + 1);
4882 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004883 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004884 get_tabline_label(tp, TRUE);
4885 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004886 }
4887 }
4888 }
4889 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004890# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004891# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004892# ifdef FEAT_GUI_TABLINE
4893 else
4894# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004896 UINT idButton;
4897 vimmenu_T *pMenu;
4898
4899 idButton = (UINT) hdr->idFrom;
4900 pMenu = gui_mswin_find_menu(root_menu, idButton);
4901 if (pMenu)
4902 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004904# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004905 if (str != NULL)
4906 {
4907# ifdef FEAT_MBYTE
4908 if (hdr->code == TTN_GETDISPINFOW)
4909 {
4910 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4911
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004912 /* Set the maximum width, this also enables using
4913 * \n for line break. */
4914 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4915 0, 500);
4916
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004917 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004918 lpdi->lpszText = tt_text;
4919 /* can't show tooltip if failed */
4920 }
4921 else
4922# endif
4923 {
4924 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4925
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004926 /* Set the maximum width, this also enables using
4927 * \n for line break. */
4928 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4929 0, 500);
4930
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004931 if (STRLEN(str) < sizeof(lpdi->szText)
4932 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004933 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004934 sizeof(lpdi->szText) - 1);
4935 else
4936 lpdi->lpszText = tt_text;
4937 }
4938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 }
4940 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004941# ifdef FEAT_GUI_TABLINE
4942 case TCN_SELCHANGE:
4943 if (gui_mch_showing_tabline()
4944 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004945 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004946 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004947 return 0L;
4948 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004949 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004950
4951 case NM_RCLICK:
4952 if (gui_mch_showing_tabline()
4953 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004954 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004955 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004956 return 0L;
4957 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004958 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004959# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004961# ifdef FEAT_GUI_TABLINE
4962 if (gui_mch_showing_tabline()
4963 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4964 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4965# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 break;
4967 }
4968 break;
4969#endif
4970#if defined(MENUHINTS) && defined(FEAT_MENU)
4971 case WM_MENUSELECT:
4972 if (((UINT) HIWORD(wParam)
4973 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4974 == MF_HILITE
4975 && (State & CMDLINE) == 0)
4976 {
4977 UINT idButton;
4978 vimmenu_T *pMenu;
4979 static int did_menu_tip = FALSE;
4980
4981 if (did_menu_tip)
4982 {
4983 msg_clr_cmdline();
4984 setcursor();
4985 out_flush();
4986 did_menu_tip = FALSE;
4987 }
4988
4989 idButton = (UINT)LOWORD(wParam);
4990 pMenu = gui_mswin_find_menu(root_menu, idButton);
4991 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4992 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4993 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004994 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004996 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 setcursor();
4998 out_flush();
4999 did_menu_tip = TRUE;
5000 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01005001 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 }
5003 break;
5004#endif
5005 case WM_NCHITTEST:
5006 {
5007 LRESULT result;
5008 int x, y;
5009 int xPos = GET_X_LPARAM(lParam);
5010
5011 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
5012 if (result == HTCLIENT)
5013 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005014#ifdef FEAT_GUI_TABLINE
5015 if (gui_mch_showing_tabline())
5016 {
5017 int yPos = GET_Y_LPARAM(lParam);
5018 RECT rct;
5019
5020 /* If the cursor is on the GUI tabline, don't process this
5021 * event */
5022 GetWindowRect(s_textArea, &rct);
5023 if (yPos < rct.top)
5024 return result;
5025 }
5026#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02005027 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 xPos -= x;
5029
5030 if (xPos < 48) /* <VN> TODO should use system metric? */
5031 return HTBOTTOMLEFT;
5032 else
5033 return HTBOTTOMRIGHT;
5034 }
5035 else
5036 return result;
5037 }
5038 /* break; notreached */
5039
5040#ifdef FEAT_MBYTE_IME
5041 case WM_IME_NOTIFY:
5042 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5043 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005044 return 1L;
5045
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 case WM_IME_COMPOSITION:
5047 if (!_OnImeComposition(hwnd, wParam, lParam))
5048 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005049 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050#endif
5051
5052 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005054 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 {
5056 _OnFindRepl();
5057 }
5058#endif
5059 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5060 }
5061
Bram Moolenaar2787ab92011-12-14 15:23:59 +01005062 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063}
5064
5065/*
5066 * End of call-back routines
5067 */
5068
5069/* parent window, if specified with -P */
5070HWND vim_parent_hwnd = NULL;
5071
5072 static BOOL CALLBACK
5073FindWindowTitle(HWND hwnd, LPARAM lParam)
5074{
5075 char buf[2048];
5076 char *title = (char *)lParam;
5077
5078 if (GetWindowText(hwnd, buf, sizeof(buf)))
5079 {
5080 if (strstr(buf, title) != NULL)
5081 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005082 /* Found it. Store the window ref. and quit searching if MDI
5083 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005085 if (vim_parent_hwnd != NULL)
5086 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 }
5088 }
5089 return TRUE; /* continue searching */
5090}
5091
5092/*
5093 * Invoked for '-P "title"' argument: search for parent application to open
5094 * our window in.
5095 */
5096 void
5097gui_mch_set_parent(char *title)
5098{
5099 EnumWindows(FindWindowTitle, (LPARAM)title);
5100 if (vim_parent_hwnd == NULL)
5101 {
5102 EMSG2(_("E671: Cannot find window title \"%s\""), title);
5103 mch_exit(2);
5104 }
5105}
5106
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005107#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 static void
5109ole_error(char *arg)
5110{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005111 char buf[IOSIZE];
5112
5113 /* Can't use EMSG() here, we have not finished initialisation yet. */
5114 vim_snprintf(buf, IOSIZE,
5115 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
5116 arg);
5117 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120
5121/*
5122 * Parse the GUI related command-line arguments. Any arguments used are
5123 * deleted from argv, and *argc is decremented accordingly. This is called
5124 * when vim is started, whether or not the GUI has been started.
5125 */
5126 void
5127gui_mch_prepare(int *argc, char **argv)
5128{
5129 int silent = FALSE;
5130 int idx;
5131
5132 /* Check for special OLE command line parameters */
5133 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5134 {
5135 /* Check for a "-silent" argument first. */
5136 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5137 && (argv[2][0] == '-' || argv[2][0] == '/'))
5138 {
5139 silent = TRUE;
5140 idx = 2;
5141 }
5142 else
5143 idx = 1;
5144
5145 /* Register Vim as an OLE Automation server */
5146 if (STRICMP(argv[idx] + 1, "register") == 0)
5147 {
5148#ifdef FEAT_OLE
5149 RegisterMe(silent);
5150 mch_exit(0);
5151#else
5152 if (!silent)
5153 ole_error("register");
5154 mch_exit(2);
5155#endif
5156 }
5157
5158 /* Unregister Vim as an OLE Automation server */
5159 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5160 {
5161#ifdef FEAT_OLE
5162 UnregisterMe(!silent);
5163 mch_exit(0);
5164#else
5165 if (!silent)
5166 ole_error("unregister");
5167 mch_exit(2);
5168#endif
5169 }
5170
5171 /* Ignore an -embedding argument. It is only relevant if the
5172 * application wants to treat the case when it is started manually
5173 * differently from the case where it is started via automation (and
5174 * we don't).
5175 */
5176 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5177 {
5178#ifdef FEAT_OLE
5179 *argc = 1;
5180#else
5181 ole_error("embedding");
5182 mch_exit(2);
5183#endif
5184 }
5185 }
5186
5187#ifdef FEAT_OLE
5188 {
5189 int bDoRestart = FALSE;
5190
5191 InitOLE(&bDoRestart);
5192 /* automatically exit after registering */
5193 if (bDoRestart)
5194 mch_exit(0);
5195 }
5196#endif
5197
5198#ifdef FEAT_NETBEANS_INTG
5199 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005200 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 int arg;
5202
5203 for (arg = 1; arg < *argc; arg++)
5204 if (strncmp("-nb", argv[arg], 3) == 0)
5205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 netbeansArg = argv[arg];
5207 mch_memmove(&argv[arg], &argv[arg + 1],
5208 (--*argc - arg) * sizeof(char *));
5209 argv[*argc] = NULL;
5210 break; /* enough? */
5211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 }
5213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214}
5215
5216/*
5217 * Initialise the GUI. Create all the windows, set up all the call-backs
5218 * etc.
5219 */
5220 int
5221gui_mch_init(void)
5222{
5223 const char szVimWndClass[] = VIM_CLASS;
5224 const char szTextAreaClass[] = "VimTextArea";
5225 WNDCLASS wndclass;
5226#ifdef FEAT_MBYTE
5227 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005228 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 WNDCLASSW wndclassw;
5230#endif
5231#ifdef GLOBAL_IME
5232 ATOM atom;
5233#endif
5234
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 /* Return here if the window was already opened (happens when
5236 * gui_mch_dialog() is called early). */
5237 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005238 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239
5240 /*
5241 * Load the tearoff bitmap
5242 */
5243#ifdef FEAT_TEAROFF
5244 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
5245#endif
5246
5247 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5248 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5249#ifdef FEAT_MENU
5250 gui.menu_height = 0; /* Windows takes care of this */
5251#endif
5252 gui.border_width = 0;
5253
5254 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5255
5256#ifdef FEAT_MBYTE
5257 /* First try using the wide version, so that we can use any title.
5258 * Otherwise only characters in the active codepage will work. */
5259 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
5260 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005261 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 wndclassw.lpfnWndProc = _WndProc;
5263 wndclassw.cbClsExtra = 0;
5264 wndclassw.cbWndExtra = 0;
5265 wndclassw.hInstance = s_hinst;
5266 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5267 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5268 wndclassw.hbrBackground = s_brush;
5269 wndclassw.lpszMenuName = NULL;
5270 wndclassw.lpszClassName = szVimWndClassW;
5271
5272 if ((
5273#ifdef GLOBAL_IME
5274 atom =
5275#endif
5276 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005277 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 else
5279 wide_WindowProc = TRUE;
5280 }
5281
5282 if (!wide_WindowProc)
5283#endif
5284
5285 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
5286 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005287 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 wndclass.lpfnWndProc = _WndProc;
5289 wndclass.cbClsExtra = 0;
5290 wndclass.cbWndExtra = 0;
5291 wndclass.hInstance = s_hinst;
5292 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
5293 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5294 wndclass.hbrBackground = s_brush;
5295 wndclass.lpszMenuName = NULL;
5296 wndclass.lpszClassName = szVimWndClass;
5297
5298 if ((
5299#ifdef GLOBAL_IME
5300 atom =
5301#endif
5302 RegisterClass(&wndclass)) == 0)
5303 return FAIL;
5304 }
5305
5306 if (vim_parent_hwnd != NULL)
5307 {
5308#ifdef HAVE_TRY_EXCEPT
5309 __try
5310 {
5311#endif
5312 /* Open inside the specified parent window.
5313 * TODO: last argument should point to a CLIENTCREATESTRUCT
5314 * structure. */
5315 s_hwnd = CreateWindowEx(
5316 WS_EX_MDICHILD,
5317 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005318 WS_OVERLAPPEDWINDOW | WS_CHILD
5319 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5321 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5322 100, /* Any value will do */
5323 100, /* Any value will do */
5324 vim_parent_hwnd, NULL,
5325 s_hinst, NULL);
5326#ifdef HAVE_TRY_EXCEPT
5327 }
5328 __except(EXCEPTION_EXECUTE_HANDLER)
5329 {
5330 /* NOP */
5331 }
5332#endif
5333 if (s_hwnd == NULL)
5334 {
5335 EMSG(_("E672: Unable to open window inside MDI application"));
5336 mch_exit(2);
5337 }
5338 }
5339 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005340 {
5341 /* If the provided windowid is not valid reset it to zero, so that it
5342 * is ignored and we open our own window. */
5343 if (IsWindow((HWND)win_socket_id) <= 0)
5344 win_socket_id = 0;
5345
5346 /* Create a window. If win_socket_id is not zero without border and
5347 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00005349 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005350 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5351 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005352 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5353 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5354 100, /* Any value will do */
5355 100, /* Any value will do */
5356 NULL, NULL,
5357 s_hinst, NULL);
5358 if (s_hwnd != NULL && win_socket_id != 0)
5359 {
5360 SetParent(s_hwnd, (HWND)win_socket_id);
5361 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5362 }
5363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364
5365 if (s_hwnd == NULL)
5366 return FAIL;
5367
5368#ifdef GLOBAL_IME
5369 global_ime_init(atom, s_hwnd);
5370#endif
5371#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5372 dyn_imm_load();
5373#endif
5374
5375 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005376#ifdef FEAT_MBYTE
5377 if (wide_WindowProc)
5378 {
5379 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
5380 {
5381 wndclassw.style = CS_OWNDC;
5382 wndclassw.lpfnWndProc = _TextAreaWndProc;
5383 wndclassw.cbClsExtra = 0;
5384 wndclassw.cbWndExtra = 0;
5385 wndclassw.hInstance = s_hinst;
5386 wndclassw.hIcon = NULL;
5387 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5388 wndclassw.hbrBackground = NULL;
5389 wndclassw.lpszMenuName = NULL;
5390 wndclassw.lpszClassName = szTextAreaClassW;
5391
5392 if (RegisterClassW(&wndclassw) == 0)
5393 return FAIL;
5394 }
5395 }
5396 else
5397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
5399 {
5400 wndclass.style = CS_OWNDC;
5401 wndclass.lpfnWndProc = _TextAreaWndProc;
5402 wndclass.cbClsExtra = 0;
5403 wndclass.cbWndExtra = 0;
5404 wndclass.hInstance = s_hinst;
5405 wndclass.hIcon = NULL;
5406 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5407 wndclass.hbrBackground = NULL;
5408 wndclass.lpszMenuName = NULL;
5409 wndclass.lpszClassName = szTextAreaClass;
5410
5411 if (RegisterClass(&wndclass) == 0)
5412 return FAIL;
5413 }
5414 s_textArea = CreateWindowEx(
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005415 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 szTextAreaClass, "Vim text area",
5417 WS_CHILD | WS_VISIBLE, 0, 0,
5418 100, /* Any value will do for now */
5419 100, /* Any value will do for now */
5420 s_hwnd, NULL,
5421 s_hinst, NULL);
5422
5423 if (s_textArea == NULL)
5424 return FAIL;
5425
Bram Moolenaar20321902016-02-17 12:30:17 +01005426#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005427 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5428 {
5429 HANDLE hIcon = NULL;
5430
5431 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005432 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005433 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005434#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005435
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436#ifdef FEAT_MENU
5437 s_menuBar = CreateMenu();
5438#endif
5439 s_hdc = GetDC(s_textArea);
5440
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441#ifdef FEAT_WINDOWS
5442 DragAcceptFiles(s_hwnd, TRUE);
5443#endif
5444
5445 /* Do we need to bother with this? */
5446 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5447
5448 /* Get background/foreground colors from the system */
5449 gui_mch_def_colors();
5450
5451 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5452 * file) */
5453 set_normal_colors();
5454
5455 /*
5456 * Check that none of the colors are the same as the background color.
5457 * Then store the current values as the defaults.
5458 */
5459 gui_check_colors();
5460 gui.def_norm_pixel = gui.norm_pixel;
5461 gui.def_back_pixel = gui.back_pixel;
5462
5463 /* Get the colors for the highlight groups (gui_check_colors() might have
5464 * changed them) */
5465 highlight_gui_started();
5466
5467 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005468 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005470 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471
5472 /*
5473 * Set up for Intellimouse processing
5474 */
5475 init_mouse_wheel();
5476
5477 /*
5478 * compute a couple of metrics used for the dialogs
5479 */
5480 get_dialog_font_metrics();
5481#ifdef FEAT_TOOLBAR
5482 /*
5483 * Create the toolbar
5484 */
5485 initialise_toolbar();
5486#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005487#ifdef FEAT_GUI_TABLINE
5488 /*
5489 * Create the tabline
5490 */
5491 initialise_tabline();
5492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493#ifdef MSWIN_FIND_REPLACE
5494 /*
5495 * Initialise the dialog box stuff
5496 */
5497 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5498
5499 /* Initialise the struct */
5500 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005501 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005503 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5505 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5506 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005507# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00005508 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
5509 s_findrep_struct_w.lpstrFindWhat =
5510 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5511 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
5512 s_findrep_struct_w.lpstrReplaceWith =
5513 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5514 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
5515 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
5516 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5517# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005520#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005521# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5522/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5523# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005524# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005525# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005526# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005527 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005528 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005529#endif
5530
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005531#ifdef FEAT_RENDER_OPTIONS
5532 if (p_rop)
5533 (void)gui_mch_set_rendering_options(p_rop);
5534#endif
5535
Bram Moolenaar748bf032005-02-02 23:04:36 +00005536theend:
5537 /* Display any pending error messages */
5538 display_errors();
5539
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 return OK;
5541}
5542
5543/*
5544 * Get the size of the screen, taking position on multiple monitors into
5545 * account (if supported).
5546 */
5547 static void
5548get_work_area(RECT *spi_rect)
5549{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005550 HMONITOR mon;
5551 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005553 /* work out which monitor the window is on, and get *it's* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005554 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005555 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005557 moninfo.cbSize = sizeof(MONITORINFO);
5558 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005560 *spi_rect = moninfo.rcWork;
5561 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 }
5563 }
5564 /* this is the old method... */
5565 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5566}
5567
5568/*
5569 * Set the size of the window to the given width and height in pixels.
5570 */
5571 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005572gui_mch_set_shellsize(
5573 int width,
5574 int height,
5575 int min_width UNUSED,
5576 int min_height UNUSED,
5577 int base_width UNUSED,
5578 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005579 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580{
5581 RECT workarea_rect;
5582 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 WINDOWPLACEMENT wndpl;
5584
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005585 /* Try to keep window completely on screen. */
5586 /* Get position of the screen work area. This is the part that is not
5587 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 get_work_area(&workarea_rect);
5589
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005590 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005591 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 wndpl.length = sizeof(WINDOWPLACEMENT);
5593 GetWindowPlacement(s_hwnd, &wndpl);
5594
5595 /* Resizing a maximized window looks very strange, unzoom it first.
5596 * But don't do it when still starting up, it may have been requested in
5597 * the shortcut. */
5598 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5599 {
5600 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5601 /* Need to get the settings of the normal window. */
5602 GetWindowPlacement(s_hwnd, &wndpl);
5603 }
5604
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005606 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005607 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005608 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005609 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 + GetSystemMetrics(SM_CYCAPTION)
5611#ifdef FEAT_MENU
5612 + gui_mswin_get_menu_height(FALSE)
5613#endif
5614 ;
5615
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005616 /* The following should take care of keeping Vim on the same monitor, no
5617 * matter if the secondary monitor is left or right of the primary
5618 * monitor. */
5619 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5620 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005621
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005622 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005623 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005624 && wndpl.rcNormalPosition.right > workarea_rect.right)
5625 OffsetRect(&wndpl.rcNormalPosition,
5626 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005628 if ((direction & RESIZE_HOR)
5629 && wndpl.rcNormalPosition.left < workarea_rect.left)
5630 OffsetRect(&wndpl.rcNormalPosition,
5631 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632
Bram Moolenaarafa24992006-03-27 20:58:26 +00005633 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005634 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5635 OffsetRect(&wndpl.rcNormalPosition,
5636 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005638 if ((direction & RESIZE_VERT)
5639 && wndpl.rcNormalPosition.top < workarea_rect.top)
5640 OffsetRect(&wndpl.rcNormalPosition,
5641 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642
5643 /* set window position - we should use SetWindowPlacement rather than
5644 * SetWindowPos as the MSDN docs say the coord systems returned by
5645 * these two are not compatible. */
5646 SetWindowPlacement(s_hwnd, &wndpl);
5647
5648 SetActiveWindow(s_hwnd);
5649 SetFocus(s_hwnd);
5650
5651#ifdef FEAT_MENU
5652 /* Menu may wrap differently now */
5653 gui_mswin_get_menu_height(!gui.starting);
5654#endif
5655}
5656
5657
5658 void
5659gui_mch_set_scrollbar_thumb(
5660 scrollbar_T *sb,
5661 long val,
5662 long size,
5663 long max)
5664{
5665 SCROLLINFO info;
5666
5667 sb->scroll_shift = 0;
5668 while (max > 32767)
5669 {
5670 max = (max + 1) >> 1;
5671 val >>= 1;
5672 size >>= 1;
5673 ++sb->scroll_shift;
5674 }
5675
5676 if (sb->scroll_shift > 0)
5677 ++size;
5678
5679 info.cbSize = sizeof(info);
5680 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5681 info.nPos = val;
5682 info.nMin = 0;
5683 info.nMax = max;
5684 info.nPage = size;
5685 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5686}
5687
5688
5689/*
5690 * Set the current text font.
5691 */
5692 void
5693gui_mch_set_font(GuiFont font)
5694{
5695 gui.currFont = font;
5696}
5697
5698
5699/*
5700 * Set the current text foreground color.
5701 */
5702 void
5703gui_mch_set_fg_color(guicolor_T color)
5704{
5705 gui.currFgColor = color;
5706}
5707
5708/*
5709 * Set the current text background color.
5710 */
5711 void
5712gui_mch_set_bg_color(guicolor_T color)
5713{
5714 gui.currBgColor = color;
5715}
5716
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005717/*
5718 * Set the current text special color.
5719 */
5720 void
5721gui_mch_set_sp_color(guicolor_T color)
5722{
5723 gui.currSpColor = color;
5724}
5725
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726#if defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)
5727/*
5728 * Multi-byte handling, originally by Sung-Hoon Baek.
5729 * First static functions (no prototypes generated).
5730 */
5731#ifdef _MSC_VER
5732# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5733#endif
5734#include <imm.h>
5735
5736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 * handle WM_IME_NOTIFY message
5738 */
5739 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005740_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741{
5742 LRESULT lResult = 0;
5743 HIMC hImc;
5744
5745 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5746 return lResult;
5747 switch (dwCommand)
5748 {
5749 case IMN_SETOPENSTATUS:
5750 if (pImmGetOpenStatus(hImc))
5751 {
5752 pImmSetCompositionFont(hImc, &norm_logfont);
5753 im_set_position(gui.row, gui.col);
5754
5755 /* Disable langmap */
5756 State &= ~LANGMAP;
5757 if (State & INSERT)
5758 {
5759#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
5760 /* Unshown 'keymap' in status lines */
5761 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5762 {
5763 /* Save cursor position */
5764 int old_row = gui.row;
5765 int old_col = gui.col;
5766
5767 // This must be called here before
5768 // status_redraw_curbuf(), otherwise the mode
5769 // message may appear in the wrong position.
5770 showmode();
5771 status_redraw_curbuf();
5772 update_screen(0);
5773 /* Restore cursor position */
5774 gui.row = old_row;
5775 gui.col = old_col;
5776 }
5777#endif
5778 }
5779 }
5780 gui_update_cursor(TRUE, FALSE);
5781 lResult = 0;
5782 break;
5783 }
5784 pImmReleaseContext(hWnd, hImc);
5785 return lResult;
5786}
5787
5788 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005789_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790{
5791 char_u *ret;
5792 int len;
5793
5794 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5795 return 0;
5796
5797 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5798 if (ret != NULL)
5799 {
5800 add_to_input_buf_csi(ret, len);
5801 vim_free(ret);
5802 return 1;
5803 }
5804 return 0;
5805}
5806
5807/*
5808 * get the current composition string, in UCS-2; *lenp is the number of
5809 * *lenp is the number of Unicode characters.
5810 */
5811 static short_u *
5812GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5813{
5814 LONG ret;
5815 LPWSTR wbuf = NULL;
5816 char_u *buf;
5817
5818 if (!pImmGetContext)
5819 return NULL; /* no imm32.dll */
5820
5821 /* Try Unicode; this'll always work on NT regardless of codepage. */
5822 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5823 if (ret == 0)
5824 return NULL; /* empty */
5825
5826 if (ret > 0)
5827 {
5828 /* Allocate the requested buffer plus space for the NUL character. */
5829 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
5830 if (wbuf != NULL)
5831 {
5832 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5833 *lenp = ret / sizeof(WCHAR);
5834 }
5835 return (short_u *)wbuf;
5836 }
5837
5838 /* ret < 0; we got an error, so try the ANSI version. This'll work
5839 * on 9x/ME, but only if the codepage happens to be set to whatever
5840 * we're inputting. */
5841 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5842 if (ret <= 0)
5843 return NULL; /* empty or error */
5844
5845 buf = alloc(ret);
5846 if (buf == NULL)
5847 return NULL;
5848 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5849
5850 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005851 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 vim_free(buf);
5853
5854 return (short_u *)wbuf;
5855}
5856
5857/*
5858 * void GetResultStr()
5859 *
5860 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5861 * get complete composition string
5862 */
5863 static char_u *
5864GetResultStr(HWND hwnd, int GCS, int *lenp)
5865{
5866 HIMC hIMC; /* Input context handle. */
5867 short_u *buf = NULL;
5868 char_u *convbuf = NULL;
5869
5870 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5871 return NULL;
5872
5873 /* Reads in the composition string. */
5874 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5875 if (buf == NULL)
5876 return NULL;
5877
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005878 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 pImmReleaseContext(hwnd, hIMC);
5880 vim_free(buf);
5881 return convbuf;
5882}
5883#endif
5884
5885/* For global functions we need prototypes. */
5886#if (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) || defined(PROTO)
5887
5888/*
5889 * set font to IM.
5890 */
5891 void
5892im_set_font(LOGFONT *lf)
5893{
5894 HIMC hImc;
5895
5896 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5897 {
5898 pImmSetCompositionFont(hImc, lf);
5899 pImmReleaseContext(s_hwnd, hImc);
5900 }
5901}
5902
5903/*
5904 * Notify cursor position to IM.
5905 */
5906 void
5907im_set_position(int row, int col)
5908{
5909 HIMC hImc;
5910
5911 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5912 {
5913 COMPOSITIONFORM cfs;
5914
5915 cfs.dwStyle = CFS_POINT;
5916 cfs.ptCurrentPos.x = FILL_X(col);
5917 cfs.ptCurrentPos.y = FILL_Y(row);
5918 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
5919 pImmSetCompositionWindow(hImc, &cfs);
5920
5921 pImmReleaseContext(s_hwnd, hImc);
5922 }
5923}
5924
5925/*
5926 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5927 */
5928 void
5929im_set_active(int active)
5930{
5931 HIMC hImc;
5932 static HIMC hImcOld = (HIMC)0;
5933
5934 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
5935 {
5936 if (p_imdisable)
5937 {
5938 if (hImcOld == (HIMC)0)
5939 {
5940 hImcOld = pImmGetContext(s_hwnd);
5941 if (hImcOld)
5942 pImmAssociateContext(s_hwnd, (HIMC)0);
5943 }
5944 active = FALSE;
5945 }
5946 else if (hImcOld != (HIMC)0)
5947 {
5948 pImmAssociateContext(s_hwnd, hImcOld);
5949 hImcOld = (HIMC)0;
5950 }
5951
5952 hImc = pImmGetContext(s_hwnd);
5953 if (hImc)
5954 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005955 /*
5956 * for Korean ime
5957 */
5958 HKL hKL = GetKeyboardLayout(0);
5959
5960 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5961 {
5962 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5963 static BOOL bSaved = FALSE;
5964
5965 if (active)
5966 {
5967 /* if we have a saved conversion status, restore it */
5968 if (bSaved)
5969 pImmSetConversionStatus(hImc, dwConversionSaved,
5970 dwSentenceSaved);
5971 bSaved = FALSE;
5972 }
5973 else
5974 {
5975 /* save conversion status and disable korean */
5976 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5977 &dwSentenceSaved))
5978 {
5979 bSaved = TRUE;
5980 pImmSetConversionStatus(hImc,
5981 dwConversionSaved & ~(IME_CMODE_NATIVE
5982 | IME_CMODE_FULLSHAPE),
5983 dwSentenceSaved);
5984 }
5985 }
5986 }
5987
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 pImmSetOpenStatus(hImc, active);
5989 pImmReleaseContext(s_hwnd, hImc);
5990 }
5991 }
5992}
5993
5994/*
5995 * Get IM status. When IM is on, return not 0. Else return 0.
5996 */
5997 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005998im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999{
6000 int status = 0;
6001 HIMC hImc;
6002
6003 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6004 {
6005 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6006 pImmReleaseContext(s_hwnd, hImc);
6007 }
6008 return status;
6009}
6010
6011#endif /* FEAT_MBYTE && FEAT_MBYTE_IME */
6012
6013#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
6014/* Win32 with GLOBAL IME */
6015
6016/*
6017 * Notify cursor position to IM.
6018 */
6019 void
6020im_set_position(int row, int col)
6021{
6022 /* Win32 with GLOBAL IME */
6023 POINT p;
6024
6025 p.x = FILL_X(col);
6026 p.y = FILL_Y(row);
6027 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
6028 global_ime_set_position(&p);
6029}
6030
6031/*
6032 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6033 */
6034 void
6035im_set_active(int active)
6036{
6037 global_ime_set_status(active);
6038}
6039
6040/*
6041 * Get IM status. When IM is on, return not 0. Else return 0.
6042 */
6043 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006044im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045{
6046 return global_ime_get_status();
6047}
6048#endif
6049
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006050#ifdef FEAT_MBYTE
6051/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006052 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006053 */
6054 static void
6055latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6056{
6057 int c;
6058
Bram Moolenaarca003e12006-03-17 23:19:38 +00006059 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006060 {
6061 c = *text++;
6062 switch (c)
6063 {
6064 case 0xa4: c = 0x20ac; break; /* euro */
6065 case 0xa6: c = 0x0160; break; /* S hat */
6066 case 0xa8: c = 0x0161; break; /* S -hat */
6067 case 0xb4: c = 0x017d; break; /* Z hat */
6068 case 0xb8: c = 0x017e; break; /* Z -hat */
6069 case 0xbc: c = 0x0152; break; /* OE */
6070 case 0xbd: c = 0x0153; break; /* oe */
6071 case 0xbe: c = 0x0178; break; /* Y */
6072 }
6073 *unicodebuf++ = c;
6074 }
6075}
6076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077
6078#ifdef FEAT_RIGHTLEFT
6079/*
6080 * What is this for? In the case where you are using Win98 or Win2K or later,
6081 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6082 * reverses the string sent to the TextOut... family. This sucks, because we
6083 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6084 * way to tell Windblows not to do this!
6085 *
6086 * The short of it is that this 'RevOut' only gets called if you are running
6087 * one of the new, "improved" MS OSes, and only if you are running in
6088 * 'rightleft' mode. It makes display take *slightly* longer, but not
6089 * noticeably so.
6090 */
6091 static void
6092RevOut( HDC s_hdc,
6093 int col,
6094 int row,
6095 UINT foptions,
6096 CONST RECT *pcliprect,
6097 LPCTSTR text,
6098 UINT len,
6099 CONST INT *padding)
6100{
6101 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006103 for (ix = 0; ix < (int)len; ++ix)
6104 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6105 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106}
6107#endif
6108
6109 void
6110gui_mch_draw_string(
6111 int row,
6112 int col,
6113 char_u *text,
6114 int len,
6115 int flags)
6116{
6117 static int *padding = NULL;
6118 static int pad_size = 0;
6119 int i;
6120 const RECT *pcliprect = NULL;
6121 UINT foptions = 0;
6122#ifdef FEAT_MBYTE
6123 static WCHAR *unicodebuf = NULL;
6124 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006125 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 int n = 0;
6127#endif
6128 HPEN hpen, old_pen;
6129 int y;
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006130#ifdef FEAT_DIRECTX
6131 int font_is_ttf_or_vector = 0;
6132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 /*
6135 * Italic and bold text seems to have an extra row of pixels at the bottom
6136 * (below where the bottom of the character should be). If we draw the
6137 * characters with a solid background, the top row of pixels in the
6138 * character below will be overwritten. We can fix this by filling in the
6139 * background ourselves, to the correct character proportions, and then
6140 * writing the character in transparent mode. Still have a problem when
6141 * the character is "_", which gets written on to the character below.
6142 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6143 * pixel in their slots, which fixes the problem with the bottom row of
6144 * pixels. We still need this code because otherwise the top row of pixels
6145 * becomes a problem. - webb.
6146 */
6147 static HBRUSH hbr_cache[2] = {NULL, NULL};
6148 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6149 static int brush_lru = 0;
6150 HBRUSH hbr;
6151 RECT rc;
6152
6153 if (!(flags & DRAW_TRANSP))
6154 {
6155 /*
6156 * Clear background first.
6157 * Note: FillRect() excludes right and bottom of rectangle.
6158 */
6159 rc.left = FILL_X(col);
6160 rc.top = FILL_Y(row);
6161#ifdef FEAT_MBYTE
6162 if (has_mbyte)
6163 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006165 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 }
6167 else
6168#endif
6169 rc.right = FILL_X(col + len);
6170 rc.bottom = FILL_Y(row + 1);
6171
6172 /* Cache the created brush, that saves a lot of time. We need two:
6173 * one for cursor background and one for the normal background. */
6174 if (gui.currBgColor == brush_color[0])
6175 {
6176 hbr = hbr_cache[0];
6177 brush_lru = 1;
6178 }
6179 else if (gui.currBgColor == brush_color[1])
6180 {
6181 hbr = hbr_cache[1];
6182 brush_lru = 0;
6183 }
6184 else
6185 {
6186 if (hbr_cache[brush_lru] != NULL)
6187 DeleteBrush(hbr_cache[brush_lru]);
6188 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6189 brush_color[brush_lru] = gui.currBgColor;
6190 hbr = hbr_cache[brush_lru];
6191 brush_lru = !brush_lru;
6192 }
6193 FillRect(s_hdc, &rc, hbr);
6194
6195 SetBkMode(s_hdc, TRANSPARENT);
6196
6197 /*
6198 * When drawing block cursor, prevent inverted character spilling
6199 * over character cell (can happen with bold/italic)
6200 */
6201 if (flags & DRAW_CURSOR)
6202 {
6203 pcliprect = &rc;
6204 foptions = ETO_CLIPPED;
6205 }
6206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 SetTextColor(s_hdc, gui.currFgColor);
6208 SelectFont(s_hdc, gui.currFont);
6209
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006210#ifdef FEAT_DIRECTX
6211 if (IS_ENABLE_DIRECTX())
6212 {
6213 TEXTMETRIC tm;
6214
6215 GetTextMetrics(s_hdc, &tm);
6216 if (tm.tmPitchAndFamily & (TMPF_TRUETYPE | TMPF_VECTOR))
6217 {
6218 font_is_ttf_or_vector = 1;
6219 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
6220 }
6221 }
6222#endif
6223
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6225 {
6226 vim_free(padding);
6227 pad_size = Columns;
6228
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006229 /* Don't give an out-of-memory message here, it would call us
6230 * recursively. */
6231 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 if (padding != NULL)
6233 for (i = 0; i < pad_size; i++)
6234 padding[i] = gui.char_width;
6235 }
6236
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 /*
6238 * We have to provide the padding argument because italic and bold versions
6239 * of fixed-width fonts are often one pixel or so wider than their normal
6240 * versions.
6241 * No check for DRAW_BOLD, Windows will have done it already.
6242 */
6243
6244#ifdef FEAT_MBYTE
6245 /* Check if there are any UTF-8 characters. If not, use normal text
6246 * output to speed up output. */
6247 if (enc_utf8)
6248 for (n = 0; n < len; ++n)
6249 if (text[n] >= 0x80)
6250 break;
6251
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006252#if defined(FEAT_DIRECTX)
6253 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6254 * required that unicode drawing routine, currently. So this forces it
6255 * enabled. */
6256 if (enc_utf8 && IS_ENABLE_DIRECTX())
6257 n = 0; /* Keep n < len, to enter block for unicode. */
6258#endif
6259
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006261 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006263 if ((enc_utf8
6264 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6265 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 && (unicodebuf == NULL || len > unibuflen))
6267 {
6268 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006269 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270
6271 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006272 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273
6274 unibuflen = len;
6275 }
6276
6277 if (enc_utf8 && n < len && unicodebuf != NULL)
6278 {
6279 /* Output UTF-8 characters. Caller has already separated
6280 * composing characters. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006281 int i;
6282 int wlen; /* string length in words */
6283 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 int cells; /* cell width of string up to composing char */
6285 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006286 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006288 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006289 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006291 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006293 c = utf_ptr2char(text + i);
6294 if (c >= 0x10000)
6295 {
6296 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006297 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6298 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006299 }
6300 else
6301 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006302 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006303 }
6304 cw = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 if (cw > 2) /* don't use 4 for unprintable char */
6306 cw = 1;
6307 if (unicodepdy != NULL)
6308 {
6309 /* Use unicodepdy to make characters fit as we expect, even
6310 * when the font uses different widths (e.g., bold character
6311 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006312 if (c >= 0x10000)
6313 {
6314 unicodepdy[wlen - 2] = cw * gui.char_width;
6315 unicodepdy[wlen - 1] = 0;
6316 }
6317 else
6318 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 }
6320 cells += cw;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006321 i += utfc_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006322 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006324#if defined(FEAT_DIRECTX)
6325 if (IS_ENABLE_DIRECTX() && font_is_ttf_or_vector)
6326 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006327 /* Add one to "cells" for italics. */
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006328 DWriteContext_DrawText(s_dwc, s_hdc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006329 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006330 gui.char_width, gui.currFgColor);
6331 }
6332 else
6333#endif
6334 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6335 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 len = cells; /* used for underlining */
6337 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006338 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 {
6340 /* If we want to display codepage data, and the current CP is not the
6341 * ANSI one, we need to go via Unicode. */
6342 if (unicodebuf != NULL)
6343 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006344 if (enc_latin9)
6345 latin9_to_ucs(text, len, unicodebuf);
6346 else
6347 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 MB_PRECOMPOSED,
6349 (char *)text, len,
6350 (LPWSTR)unicodebuf, unibuflen);
6351 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006352 {
6353 /* Use unicodepdy to make characters fit as we expect, even
6354 * when the font uses different widths (e.g., bold character
6355 * is wider). */
6356 if (unicodepdy != NULL)
6357 {
6358 int i;
6359 int cw;
6360
6361 for (i = 0; i < len; ++i)
6362 {
6363 cw = utf_char2cells(unicodebuf[i]);
6364 if (cw > 2)
6365 cw = 1;
6366 unicodepdy[i] = cw * gui.char_width;
6367 }
6368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006370 foptions, pcliprect, unicodebuf, len, unicodepdy);
6371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 }
6373 }
6374 else
6375#endif
6376 {
6377#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006378 /* Windows will mess up RL text, so we have to draw it character by
6379 * character. Only do this if RL is on, since it's slow. */
6380 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6382 foptions, pcliprect, (char *)text, len, padding);
6383 else
6384#endif
6385 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6386 foptions, pcliprect, (char *)text, len, padding);
6387 }
6388
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006389 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 if (flags & DRAW_UNDERL)
6391 {
6392 hpen = CreatePen(PS_SOLID, 1, gui.currFgColor);
6393 old_pen = SelectObject(s_hdc, hpen);
6394 /* When p_linespace is 0, overwrite the bottom row of pixels.
6395 * Otherwise put the line just below the character. */
6396 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 if (p_linespace > 1)
6398 y -= p_linespace - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 MoveToEx(s_hdc, FILL_X(col), y, NULL);
6400 /* Note: LineTo() excludes the last pixel in the line. */
6401 LineTo(s_hdc, FILL_X(col + len), y);
6402 DeleteObject(SelectObject(s_hdc, old_pen));
6403 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006404
6405 /* Undercurl */
6406 if (flags & DRAW_UNDERC)
6407 {
6408 int x;
6409 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006410 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006411
6412 y = FILL_Y(row + 1) - 1;
6413 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6414 {
6415 offset = val[x % 8];
6416 SetPixel(s_hdc, x, y - offset, gui.currSpColor);
6417 }
6418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419}
6420
6421
6422/*
6423 * Output routines.
6424 */
6425
6426/* Flush any output to the screen */
6427 void
6428gui_mch_flush(void)
6429{
6430# if defined(__BORLANDC__)
6431 /*
6432 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6433 * prototype declaration.
6434 * The compiler complains if __stdcall is not used in both declarations.
6435 */
6436 BOOL __stdcall GdiFlush(void);
6437# endif
6438
6439 GdiFlush();
6440}
6441
6442 static void
6443clear_rect(RECT *rcp)
6444{
6445 HBRUSH hbr;
6446
6447 hbr = CreateSolidBrush(gui.back_pixel);
6448 FillRect(s_hdc, rcp, hbr);
6449 DeleteBrush(hbr);
6450}
6451
6452
Bram Moolenaarc716c302006-01-21 22:12:51 +00006453 void
6454gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6455{
6456 RECT workarea_rect;
6457
6458 get_work_area(&workarea_rect);
6459
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006460 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006461 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006462 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006463
6464 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6465 * the menubar for MSwin, we subtract it from the screen height, so that
6466 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006467 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006468 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006469 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006470 - GetSystemMetrics(SM_CYCAPTION)
6471#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006472 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006473#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006474 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006475}
6476
6477
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478#if defined(FEAT_MENU) || defined(PROTO)
6479/*
6480 * Add a sub menu to the menu bar.
6481 */
6482 void
6483gui_mch_add_menu(
6484 vimmenu_T *menu,
6485 int pos)
6486{
6487 vimmenu_T *parent = menu->parent;
6488
6489 menu->submenu_id = CreatePopupMenu();
6490 menu->id = s_menu_id++;
6491
6492 if (menu_is_menubar(menu->name))
6493 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494#ifdef FEAT_MBYTE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006495 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006497 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6498 {
6499 /* 'encoding' differs from active codepage: convert menu name
6500 * and use wide function */
6501 wn = enc_to_utf16(menu->name, NULL);
6502 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006504 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006506 infow.cbSize = sizeof(infow);
6507 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6508 | MIIM_SUBMENU;
6509 infow.dwItemData = (long_u)menu;
6510 infow.wID = menu->id;
6511 infow.fType = MFT_STRING;
6512 infow.dwTypeData = wn;
6513 infow.cch = (UINT)wcslen(wn);
6514 infow.hSubMenu = menu->submenu_id;
6515 InsertMenuItemW((parent == NULL)
6516 ? s_menuBar : parent->submenu_id,
6517 (UINT)pos, TRUE, &infow);
6518 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006522 if (wn == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523#endif
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006524 {
6525 MENUITEMINFO info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006527 info.cbSize = sizeof(info);
6528 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
6529 info.dwItemData = (long_u)menu;
6530 info.wID = menu->id;
6531 info.fType = MFT_STRING;
6532 info.dwTypeData = (LPTSTR)menu->name;
6533 info.cch = (UINT)STRLEN(menu->name);
6534 info.hSubMenu = menu->submenu_id;
6535 InsertMenuItem((parent == NULL)
6536 ? s_menuBar : parent->submenu_id,
6537 (UINT)pos, TRUE, &info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 }
6539 }
6540
6541 /* Fix window size if menu may have wrapped */
6542 if (parent == NULL)
6543 gui_mswin_get_menu_height(!gui.starting);
6544#ifdef FEAT_TEAROFF
6545 else if (IsWindow(parent->tearoff_handle))
6546 rebuild_tearoff(parent);
6547#endif
6548}
6549
6550 void
6551gui_mch_show_popupmenu(vimmenu_T *menu)
6552{
6553 POINT mp;
6554
6555 (void)GetCursorPos((LPPOINT)&mp);
6556 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6557}
6558
6559 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006560gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561{
6562 vimmenu_T *menu = gui_find_menu(path_name);
6563
6564 if (menu != NULL)
6565 {
6566 POINT p;
6567
6568 /* Find the position of the current cursor */
6569 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006570 if (mouse_pos)
6571 {
6572 int mx, my;
6573
6574 gui_mch_getmouse(&mx, &my);
6575 p.x += mx;
6576 p.y += my;
6577 }
6578 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 {
6580 p.x += TEXT_X(W_WINCOL(curwin) + curwin->w_wcol + 1);
6581 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6582 }
6583 msg_scroll = FALSE;
6584 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6585 }
6586}
6587
6588#if defined(FEAT_TEAROFF) || defined(PROTO)
6589/*
6590 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6591 * create it as a pseudo-"tearoff menu".
6592 */
6593 void
6594gui_make_tearoff(char_u *path_name)
6595{
6596 vimmenu_T *menu = gui_find_menu(path_name);
6597
6598 /* Found the menu, so tear it off. */
6599 if (menu != NULL)
6600 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6601}
6602#endif
6603
6604/*
6605 * Add a menu item to a menu
6606 */
6607 void
6608gui_mch_add_menu_item(
6609 vimmenu_T *menu,
6610 int idx)
6611{
6612 vimmenu_T *parent = menu->parent;
6613
6614 menu->id = s_menu_id++;
6615 menu->submenu_id = NULL;
6616
6617#ifdef FEAT_TEAROFF
6618 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6619 {
6620 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6621 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6622 }
6623 else
6624#endif
6625#ifdef FEAT_TOOLBAR
6626 if (menu_is_toolbar(parent->name))
6627 {
6628 TBBUTTON newtb;
6629
6630 vim_memset(&newtb, 0, sizeof(newtb));
6631 if (menu_is_separator(menu->name))
6632 {
6633 newtb.iBitmap = 0;
6634 newtb.fsStyle = TBSTYLE_SEP;
6635 }
6636 else
6637 {
6638 newtb.iBitmap = get_toolbar_bitmap(menu);
6639 newtb.fsStyle = TBSTYLE_BUTTON;
6640 }
6641 newtb.idCommand = menu->id;
6642 newtb.fsState = TBSTATE_ENABLED;
6643 newtb.iString = 0;
6644 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6645 (LPARAM)&newtb);
6646 menu->submenu_id = (HMENU)-1;
6647 }
6648 else
6649#endif
6650 {
6651#ifdef FEAT_MBYTE
6652 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653
6654 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6655 {
6656 /* 'encoding' differs from active codepage: convert menu item name
6657 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006658 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 if (wn != NULL)
6660 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006661 InsertMenuW(parent->submenu_id, (UINT)idx,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 (menu_is_separator(menu->name)
6663 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6664 (UINT)menu->id, wn);
6665 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 }
6667 }
6668 if (wn == NULL)
6669#endif
6670 InsertMenu(parent->submenu_id, (UINT)idx,
6671 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
6672 | MF_BYPOSITION,
6673 (UINT)menu->id, (LPCTSTR)menu->name);
6674#ifdef FEAT_TEAROFF
6675 if (IsWindow(parent->tearoff_handle))
6676 rebuild_tearoff(parent);
6677#endif
6678 }
6679}
6680
6681/*
6682 * Destroy the machine specific menu widget.
6683 */
6684 void
6685gui_mch_destroy_menu(vimmenu_T *menu)
6686{
6687#ifdef FEAT_TOOLBAR
6688 /*
6689 * is this a toolbar button?
6690 */
6691 if (menu->submenu_id == (HMENU)-1)
6692 {
6693 int iButton;
6694
6695 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6696 (WPARAM)menu->id, 0);
6697 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6698 }
6699 else
6700#endif
6701 {
6702 if (menu->parent != NULL
6703 && menu_is_popup(menu->parent->dname)
6704 && menu->parent->submenu_id != NULL)
6705 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6706 else
6707 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6708 if (menu->submenu_id != NULL)
6709 DestroyMenu(menu->submenu_id);
6710#ifdef FEAT_TEAROFF
6711 if (IsWindow(menu->tearoff_handle))
6712 DestroyWindow(menu->tearoff_handle);
6713 if (menu->parent != NULL
6714 && menu->parent->children != NULL
6715 && IsWindow(menu->parent->tearoff_handle))
6716 {
6717 /* This menu must not show up when rebuilding the tearoff window. */
6718 menu->modes = 0;
6719 rebuild_tearoff(menu->parent);
6720 }
6721#endif
6722 }
6723}
6724
6725#ifdef FEAT_TEAROFF
6726 static void
6727rebuild_tearoff(vimmenu_T *menu)
6728{
6729 /*hackish*/
6730 char_u tbuf[128];
6731 RECT trect;
6732 RECT rct;
6733 RECT roct;
6734 int x, y;
6735
6736 HWND thwnd = menu->tearoff_handle;
6737
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006738 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 if (GetWindowRect(thwnd, &trect)
6740 && GetWindowRect(s_hwnd, &rct)
6741 && GetClientRect(s_hwnd, &roct))
6742 {
6743 x = trect.left - rct.left;
6744 y = (trect.top - rct.bottom + roct.bottom);
6745 }
6746 else
6747 {
6748 x = y = 0xffffL;
6749 }
6750 DestroyWindow(thwnd);
6751 if (menu->children != NULL)
6752 {
6753 gui_mch_tearoff(tbuf, menu, x, y);
6754 if (IsWindow(menu->tearoff_handle))
6755 (void) SetWindowPos(menu->tearoff_handle,
6756 NULL,
6757 (int)trect.left,
6758 (int)trect.top,
6759 0, 0,
6760 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6761 }
6762}
6763#endif /* FEAT_TEAROFF */
6764
6765/*
6766 * Make a menu either grey or not grey.
6767 */
6768 void
6769gui_mch_menu_grey(
6770 vimmenu_T *menu,
6771 int grey)
6772{
6773#ifdef FEAT_TOOLBAR
6774 /*
6775 * is this a toolbar button?
6776 */
6777 if (menu->submenu_id == (HMENU)-1)
6778 {
6779 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6780 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6781 }
6782 else
6783#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006784 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6785 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786
6787#ifdef FEAT_TEAROFF
6788 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6789 {
6790 WORD menuID;
6791 HWND menuHandle;
6792
6793 /*
6794 * A tearoff button has changed state.
6795 */
6796 if (menu->children == NULL)
6797 menuID = (WORD)(menu->id);
6798 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006799 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6801 if (menuHandle)
6802 EnableWindow(menuHandle, !grey);
6803
6804 }
6805#endif
6806}
6807
6808#endif /* FEAT_MENU */
6809
6810
6811/* define some macros used to make the dialogue creation more readable */
6812
6813#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6814#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006815#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816
6817#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6818/*
6819 * stuff for dialogs
6820 */
6821
6822/*
6823 * The callback routine used by all the dialogs. Very simple. First,
6824 * acknowledges the INITDIALOG message so that Windows knows to do standard
6825 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6826 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6827 * number.
6828 */
6829 static LRESULT CALLBACK
6830dialog_callback(
6831 HWND hwnd,
6832 UINT message,
6833 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006834 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835{
6836 if (message == WM_INITDIALOG)
6837 {
6838 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
6839 /* Set focus to the dialog. Set the default button, if specified. */
6840 (void)SetFocus(hwnd);
6841 if (dialog_default_button > IDCANCEL)
6842 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006843 else
6844 /* We don't have a default, set focus on another element of the
6845 * dialog window, probably the icon */
6846 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 return FALSE;
6848 }
6849
6850 if (message == WM_COMMAND)
6851 {
6852 int button = LOWORD(wParam);
6853
6854 /* Don't end the dialog if something was selected that was
6855 * not a button.
6856 */
6857 if (button >= DLG_NONBUTTON_CONTROL)
6858 return TRUE;
6859
6860 /* If the edit box exists, copy the string. */
6861 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006862 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006863# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006864 /* If the OS is Windows NT, and 'encoding' differs from active
6865 * codepage: use wide function and convert text. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006866 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02006867 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006868 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
6869 char_u *p;
6870
6871 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6872 p = utf16_to_enc(wp, NULL);
6873 vim_strncpy(s_textfield, p, IOSIZE);
6874 vim_free(p);
6875 vim_free(wp);
6876 }
6877 else
6878# endif
6879 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006880 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882
6883 /*
6884 * Need to check for IDOK because if the user just hits Return to
6885 * accept the default value, some reason this is what we get.
6886 */
6887 if (button == IDOK)
6888 {
6889 if (dialog_default_button > IDCANCEL)
6890 EndDialog(hwnd, dialog_default_button);
6891 }
6892 else
6893 EndDialog(hwnd, button - IDCANCEL);
6894 return TRUE;
6895 }
6896
6897 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6898 {
6899 EndDialog(hwnd, 0);
6900 return TRUE;
6901 }
6902 return FALSE;
6903}
6904
6905/*
6906 * Create a dialog dynamically from the parameter strings.
6907 * type = type of dialog (question, alert, etc.)
6908 * title = dialog title. may be NULL for default title.
6909 * message = text to display. Dialog sizes to accommodate it.
6910 * buttons = '\n' separated list of button captions, default first.
6911 * dfltbutton = number of default button.
6912 *
6913 * This routine returns 1 if the first button is pressed,
6914 * 2 for the second, etc.
6915 *
6916 * 0 indicates Esc was pressed.
6917 * -1 for unexpected error
6918 *
6919 * If stubbing out this fn, return 1.
6920 */
6921
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006922static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923{
6924 "IDR_VIM",
6925 "IDR_VIM_ERROR",
6926 "IDR_VIM_ALERT",
6927 "IDR_VIM_INFO",
6928 "IDR_VIM_QUESTION"
6929};
6930
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 int
6932gui_mch_dialog(
6933 int type,
6934 char_u *title,
6935 char_u *message,
6936 char_u *buttons,
6937 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006938 char_u *textfield,
6939 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940{
6941 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006942 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 int numButtons;
6944 int *buttonWidths, *buttonPositions;
6945 int buttonYpos;
6946 int nchar, i;
6947 DWORD lStyle;
6948 int dlgwidth = 0;
6949 int dlgheight;
6950 int editboxheight;
6951 int horizWidth = 0;
6952 int msgheight;
6953 char_u *pstart;
6954 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006955 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 char_u *tbuffer;
6957 RECT rect;
6958 HWND hwnd;
6959 HDC hdc;
6960 HFONT font, oldFont;
6961 TEXTMETRIC fontInfo;
6962 int fontHeight;
6963 int textWidth, minButtonWidth, messageWidth;
6964 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006965 int maxDialogHeight;
6966 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 int vertical;
6968 int dlgPaddingX;
6969 int dlgPaddingY;
6970#ifdef USE_SYSMENU_FONT
6971 LOGFONT lfSysmenu;
6972 int use_lfSysmenu = FALSE;
6973#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006974 garray_T ga;
6975 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976
6977#ifndef NO_CONSOLE
6978 /* Don't output anything in silent mode ("ex -s") */
6979 if (silent_mode)
6980 return dfltbutton; /* return default option */
6981#endif
6982
Bram Moolenaar748bf032005-02-02 23:04:36 +00006983 if (s_hwnd == NULL)
6984 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985
6986 if ((type < 0) || (type > VIM_LAST_TYPE))
6987 type = 0;
6988
6989 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006990 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006991 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006992 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993
6994 if (p == NULL)
6995 return -1;
6996
6997 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006998 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7000 * const.
7001 */
7002 tbuffer = vim_strsave(buttons);
7003 if (tbuffer == NULL)
7004 return -1;
7005
7006 --dfltbutton; /* Change from one-based to zero-based */
7007
7008 /* Count buttons */
7009 numButtons = 1;
7010 for (i = 0; tbuffer[i] != '\0'; i++)
7011 {
7012 if (tbuffer[i] == DLG_BUTTON_SEP)
7013 numButtons++;
7014 }
7015 if (dfltbutton >= numButtons)
7016 dfltbutton = -1;
7017
7018 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007019 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 if (buttonWidths == NULL)
7021 return -1;
7022
7023 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007024 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 if (buttonPositions == NULL)
7026 return -1;
7027
7028 /*
7029 * Calculate how big the dialog must be.
7030 */
7031 hwnd = GetDesktopWindow();
7032 hdc = GetWindowDC(hwnd);
7033#ifdef USE_SYSMENU_FONT
7034 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7035 {
7036 font = CreateFontIndirect(&lfSysmenu);
7037 use_lfSysmenu = TRUE;
7038 }
7039 else
7040#endif
7041 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7042 VARIABLE_PITCH , DLG_FONT_NAME);
7043 if (s_usenewlook)
7044 {
7045 oldFont = SelectFont(hdc, font);
7046 dlgPaddingX = DLG_PADDING_X;
7047 dlgPaddingY = DLG_PADDING_Y;
7048 }
7049 else
7050 {
7051 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7052 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7053 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7054 }
7055 GetTextMetrics(hdc, &fontInfo);
7056 fontHeight = fontInfo.tmHeight;
7057
7058 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007059 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060
7061 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007062 if (s_hwnd == NULL)
7063 {
7064 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065
Bram Moolenaarc716c302006-01-21 22:12:51 +00007066 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007067 get_work_area(&workarea_rect);
7068 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7069 if (maxDialogWidth > 600)
7070 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007071 /* Leave some room for the taskbar. */
7072 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007073 }
7074 else
7075 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02007076 /* Use our own window for the size, unless it's very small. */
7077 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007078 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02007079 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007080 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007081 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
7082 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007083
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007084 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007085 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007086 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02007087 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007088 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
7089 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
7090 }
7091
7092 /* Set dlgwidth to width of message.
7093 * Copy the message into "ga", changing NL to CR-NL and inserting line
7094 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 pstart = message;
7096 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007097 msgheight = 0;
7098 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 do
7100 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007101 msgheight += fontHeight; /* at least one line */
7102
7103 /* Need to figure out where to break the string. The system does it
7104 * at a word boundary, which would mean we can't compute the number of
7105 * wrapped lines. */
7106 textWidth = 0;
7107 last_white = NULL;
7108 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007109 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007110#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007111 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007112#else
7113 l = 1;
7114#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01007115 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007116 && textWidth > maxDialogWidth * 3 / 4)
7117 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007118 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007119 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007120 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007121 /* Line will wrap. */
7122 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007123 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007124 textWidth = 0;
7125
7126 if (last_white != NULL)
7127 {
7128 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007129 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007130 pend = last_white + 1;
7131 last_white = NULL;
7132 }
7133 ga_append(&ga, '\r');
7134 ga_append(&ga, '\n');
7135 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007136 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007137
7138 while (--l >= 0)
7139 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007140 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007141 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007143
7144 ga_append(&ga, '\r');
7145 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 pstart = pend + 1;
7147 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007148
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007149 if (ga.ga_data != NULL)
7150 message = ga.ga_data;
7151
Bram Moolenaar748bf032005-02-02 23:04:36 +00007152 messageWidth += 10; /* roundoff space */
7153
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02007155 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7156 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157
7158 if (msgheight < DLG_ICON_HEIGHT)
7159 msgheight = DLG_ICON_HEIGHT;
7160
7161 /*
7162 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007163 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007165 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 if (!vertical)
7167 {
7168 // Place buttons horizontally if they fit.
7169 horizWidth = dlgPaddingX;
7170 pstart = tbuffer;
7171 i = 0;
7172 do
7173 {
7174 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7175 if (pend == NULL)
7176 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007177 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 if (textWidth < minButtonWidth)
7179 textWidth = minButtonWidth;
7180 textWidth += dlgPaddingX; /* Padding within button */
7181 buttonWidths[i] = textWidth;
7182 buttonPositions[i++] = horizWidth;
7183 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7184 pstart = pend + 1;
7185 } while (*pend != NUL);
7186
7187 if (horizWidth > maxDialogWidth)
7188 vertical = TRUE; // Too wide to fit on the screen.
7189 else if (horizWidth > dlgwidth)
7190 dlgwidth = horizWidth;
7191 }
7192
7193 if (vertical)
7194 {
7195 // Stack buttons vertically.
7196 pstart = tbuffer;
7197 do
7198 {
7199 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7200 if (pend == NULL)
7201 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007202 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 textWidth += dlgPaddingX; /* Padding within button */
7204 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7205 if (textWidth > dlgwidth)
7206 dlgwidth = textWidth;
7207 pstart = pend + 1;
7208 } while (*pend != NUL);
7209 }
7210
7211 if (dlgwidth < DLG_MIN_WIDTH)
7212 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7213
7214 /* start to fill in the dlgtemplate information. addressing by WORDs */
7215 if (s_usenewlook)
7216 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7217 else
7218 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7219
7220 add_long(lStyle);
7221 add_long(0); // (lExtendedStyle)
7222 pnumitems = p; /*save where the number of items must be stored*/
7223 add_word(0); // NumberOfItems(will change later)
7224 add_word(10); // x
7225 add_word(10); // y
7226 add_word(PixelToDialogX(dlgwidth)); // cx
7227
7228 // Dialog height.
7229 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007230 dlgheight = msgheight + 2 * dlgPaddingY
7231 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 else
7233 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7234
7235 // Dialog needs to be taller if contains an edit box.
7236 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7237 if (textfield != NULL)
7238 dlgheight += editboxheight;
7239
Bram Moolenaara95d8232013-08-07 15:27:11 +02007240 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7241 if (dlgheight > maxDialogHeight)
7242 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007243 msgheight = msgheight - (dlgheight - maxDialogHeight);
7244 dlgheight = maxDialogHeight;
7245 scroll_flag = WS_VSCROLL;
7246 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7247 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007248 }
7249
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 add_word(PixelToDialogY(dlgheight));
7251
7252 add_word(0); // Menu
7253 add_word(0); // Class
7254
7255 /* copy the title of the dialog */
7256 nchar = nCopyAnsiToWideChar(p, (title ?
7257 (LPSTR)title :
7258 (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
7259 p += nchar;
7260
7261 if (s_usenewlook)
7262 {
7263 /* do the font, since DS_3DLOOK doesn't work properly */
7264#ifdef USE_SYSMENU_FONT
7265 if (use_lfSysmenu)
7266 {
7267 /* point size */
7268 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7269 GetDeviceCaps(hdc, LOGPIXELSY));
7270 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
7271 }
7272 else
7273#endif
7274 {
7275 *p++ = DLG_FONT_POINT_SIZE; // point size
7276 nchar = nCopyAnsiToWideChar(p, TEXT(DLG_FONT_NAME));
7277 }
7278 p += nchar;
7279 }
7280
7281 buttonYpos = msgheight + 2 * dlgPaddingY;
7282
7283 if (textfield != NULL)
7284 buttonYpos += editboxheight;
7285
7286 pstart = tbuffer;
7287 if (!vertical)
7288 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7289 for (i = 0; i < numButtons; i++)
7290 {
7291 /* get end of this button. */
7292 for ( pend = pstart;
7293 *pend && (*pend != DLG_BUTTON_SEP);
7294 pend++)
7295 ;
7296
7297 if (*pend)
7298 *pend = '\0';
7299
7300 /*
7301 * old NOTE:
7302 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7303 * the focus to the first tab-able button and in so doing makes that
7304 * the default!! Grrr. Workaround: Make the default button the only
7305 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7306 * he/she can use arrow keys.
7307 *
7308 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007309 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 * dialog. Also needed for when the textfield is the default control.
7311 * It appears to work now (perhaps not on Win95?).
7312 */
7313 if (vertical)
7314 {
7315 p = add_dialog_element(p,
7316 (i == dfltbutton
7317 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7318 PixelToDialogX(DLG_VERT_PADDING_X),
7319 PixelToDialogY(buttonYpos /* TBK */
7320 + 2 * fontHeight * i),
7321 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7322 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007323 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 }
7325 else
7326 {
7327 p = add_dialog_element(p,
7328 (i == dfltbutton
7329 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7330 PixelToDialogX(horizWidth + buttonPositions[i]),
7331 PixelToDialogY(buttonYpos), /* TBK */
7332 PixelToDialogX(buttonWidths[i]),
7333 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007334 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 }
7336 pstart = pend + 1; /*next button*/
7337 }
7338 *pnumitems += numButtons;
7339
7340 /* Vim icon */
7341 p = add_dialog_element(p, SS_ICON,
7342 PixelToDialogX(dlgPaddingX),
7343 PixelToDialogY(dlgPaddingY),
7344 PixelToDialogX(DLG_ICON_WIDTH),
7345 PixelToDialogY(DLG_ICON_HEIGHT),
7346 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7347 dlg_icons[type]);
7348
Bram Moolenaar748bf032005-02-02 23:04:36 +00007349 /* Dialog message */
7350 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7351 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7352 PixelToDialogY(dlgPaddingY),
7353 (WORD)(PixelToDialogX(messageWidth) + 1),
7354 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007355 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356
7357 /* Edit box */
7358 if (textfield != NULL)
7359 {
7360 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7361 PixelToDialogX(2 * dlgPaddingX),
7362 PixelToDialogY(2 * dlgPaddingY + msgheight),
7363 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7364 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007365 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 *pnumitems += 1;
7367 }
7368
7369 *pnumitems += 2;
7370
7371 SelectFont(hdc, oldFont);
7372 DeleteObject(font);
7373 ReleaseDC(hwnd, hdc);
7374
7375 /* Let the dialog_callback() function know which button to make default
7376 * If we have an edit box, make that the default. We also need to tell
7377 * dialog_callback() if this dialog contains an edit box or not. We do
7378 * this by setting s_textfield if it does.
7379 */
7380 if (textfield != NULL)
7381 {
7382 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7383 s_textfield = textfield;
7384 }
7385 else
7386 {
7387 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7388 s_textfield = NULL;
7389 }
7390
7391 /* show the dialog box modally and get a return value */
7392 nchar = (int)DialogBoxIndirect(
7393 s_hinst,
7394 (LPDLGTEMPLATE)pdlgtemplate,
7395 s_hwnd,
7396 (DLGPROC)dialog_callback);
7397
7398 LocalFree(LocalHandle(pdlgtemplate));
7399 vim_free(tbuffer);
7400 vim_free(buttonWidths);
7401 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007402 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403
7404 /* Focus back to our window (for when MDI is used). */
7405 (void)SetFocus(s_hwnd);
7406
7407 return nchar;
7408}
7409
7410#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007411
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412/*
7413 * Put a simple element (basic class) onto a dialog template in memory.
7414 * return a pointer to where the next item should be added.
7415 *
7416 * parameters:
7417 * lStyle = additional style flags
7418 * (be careful, NT3.51 & Win32s will ignore the new ones)
7419 * x,y = x & y positions IN DIALOG UNITS
7420 * w,h = width and height IN DIALOG UNITS
7421 * Id = ID used in messages
7422 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7423 * caption = usually text or resource name
7424 *
7425 * TODO: use the length information noted here to enable the dialog creation
7426 * routines to work out more exactly how much memory they need to alloc.
7427 */
7428 static PWORD
7429add_dialog_element(
7430 PWORD p,
7431 DWORD lStyle,
7432 WORD x,
7433 WORD y,
7434 WORD w,
7435 WORD h,
7436 WORD Id,
7437 WORD clss,
7438 const char *caption)
7439{
7440 int nchar;
7441
7442 p = lpwAlign(p); /* Align to dword boundary*/
7443 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7444 *p++ = LOWORD(lStyle);
7445 *p++ = HIWORD(lStyle);
7446 *p++ = 0; // LOWORD (lExtendedStyle)
7447 *p++ = 0; // HIWORD (lExtendedStyle)
7448 *p++ = x;
7449 *p++ = y;
7450 *p++ = w;
7451 *p++ = h;
7452 *p++ = Id; //9 or 10 words in all
7453
7454 *p++ = (WORD)0xffff;
7455 *p++ = clss; //2 more here
7456
7457 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption); //strlen(caption)+1
7458 p += nchar;
7459
7460 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7461
7462 return p; //total = 15+ (strlen(caption)) words
7463 // = 30 + 2(strlen(caption) bytes reqd
7464}
7465
7466
7467/*
7468 * Helper routine. Take an input pointer, return closest pointer that is
7469 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7470 */
7471 static LPWORD
7472lpwAlign(
7473 LPWORD lpIn)
7474{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007475 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007477 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 ul += 3;
7479 ul >>= 2;
7480 ul <<= 2;
7481 return (LPWORD)ul;
7482}
7483
7484/*
7485 * Helper routine. Takes second parameter as Ansi string, copies it to first
7486 * parameter as wide character (16-bits / char) string, and returns integer
7487 * number of wide characters (words) in string (including the trailing wide
7488 * char NULL). Partly taken from the Win32SDK samples.
7489 */
7490 static int
7491nCopyAnsiToWideChar(
7492 LPWORD lpWCStr,
7493 LPSTR lpAnsiIn)
7494{
7495 int nChar = 0;
7496#ifdef FEAT_MBYTE
7497 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7498 int i;
7499 WCHAR *wn;
7500
7501 if (enc_codepage == 0 && (int)GetACP() != enc_codepage)
7502 {
7503 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007504 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 if (wn != NULL)
7506 {
7507 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007508 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 vim_free(wn);
7510 }
7511 }
7512 if (nChar == 0)
7513 /* Use Win32 conversion function. */
7514 nChar = MultiByteToWideChar(
7515 enc_codepage > 0 ? enc_codepage : CP_ACP,
7516 MB_PRECOMPOSED,
7517 lpAnsiIn, len,
7518 lpWCStr, len);
7519 for (i = 0; i < nChar; ++i)
7520 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7521 lpWCStr[i] = (WORD)' ';
7522#else
7523 do
7524 {
7525 if (*lpAnsiIn == '\t')
7526 *lpWCStr++ = (WORD)' ';
7527 else
7528 *lpWCStr++ = (WORD)*lpAnsiIn;
7529 nChar++;
7530 } while (*lpAnsiIn++);
7531#endif
7532
7533 return nChar;
7534}
7535
7536
7537#ifdef FEAT_TEAROFF
7538/*
7539 * The callback function for all the modeless dialogs that make up the
7540 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7541 * thinking its menus have been clicked), and go away when closed.
7542 */
7543 static LRESULT CALLBACK
7544tearoff_callback(
7545 HWND hwnd,
7546 UINT message,
7547 WPARAM wParam,
7548 LPARAM lParam)
7549{
7550 if (message == WM_INITDIALOG)
7551 return (TRUE);
7552
7553 /* May show the mouse pointer again. */
7554 HandleMouseHide(message, lParam);
7555
7556 if (message == WM_COMMAND)
7557 {
7558 if ((WORD)(LOWORD(wParam)) & 0x8000)
7559 {
7560 POINT mp;
7561 RECT rect;
7562
7563 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7564 {
7565 (void)TrackPopupMenu(
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007566 (HMENU)(long_u)(LOWORD(wParam) ^ 0x8000),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7568 (int)rect.right - 8,
7569 (int)mp.y,
7570 (int)0, /*reserved param*/
7571 s_hwnd,
7572 NULL);
7573 /*
7574 * NOTE: The pop-up menu can eat the mouse up event.
7575 * We deal with this in normal.c.
7576 */
7577 }
7578 }
7579 else
7580 /* Pass on messages to the main Vim window */
7581 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7582 /*
7583 * Give main window the focus back: this is so after
7584 * choosing a tearoff button you can start typing again
7585 * straight away.
7586 */
7587 (void)SetFocus(s_hwnd);
7588 return TRUE;
7589 }
7590 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7591 {
7592 DestroyWindow(hwnd);
7593 return TRUE;
7594 }
7595
7596 /* When moved around, give main window the focus back. */
7597 if (message == WM_EXITSIZEMOVE)
7598 (void)SetActiveWindow(s_hwnd);
7599
7600 return FALSE;
7601}
7602#endif
7603
7604
7605/*
7606 * Decide whether to use the "new look" (small, non-bold font) or the "old
7607 * look" (big, clanky font) for dialogs, and work out a few values for use
7608 * later accordingly.
7609 */
7610 static void
7611get_dialog_font_metrics(void)
7612{
7613 HDC hdc;
7614 HFONT hfontTools = 0;
7615 DWORD dlgFontSize;
7616 SIZE size;
7617#ifdef USE_SYSMENU_FONT
7618 LOGFONT lfSysmenu;
7619#endif
7620
7621 s_usenewlook = FALSE;
7622
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007624 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7625 hfontTools = CreateFontIndirect(&lfSysmenu);
7626 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627#endif
7628 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7629 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7630
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007631 if (hfontTools)
7632 {
7633 hdc = GetDC(s_hwnd);
7634 SelectObject(hdc, hfontTools);
7635 /*
7636 * GetTextMetrics() doesn't return the right value in
7637 * tmAveCharWidth, so we have to figure out the dialog base units
7638 * ourselves.
7639 */
7640 GetTextExtentPoint(hdc,
7641 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7642 52, &size);
7643 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007645 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7646 s_dlgfntheight = (WORD)size.cy;
7647 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 }
7649
7650 if (!s_usenewlook)
7651 {
7652 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7653 s_dlgfntwidth = LOWORD(dlgFontSize);
7654 s_dlgfntheight = HIWORD(dlgFontSize);
7655 }
7656}
7657
7658#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7659/*
7660 * Create a pseudo-"tearoff menu" based on the child
7661 * items of a given menu pointer.
7662 */
7663 static void
7664gui_mch_tearoff(
7665 char_u *title,
7666 vimmenu_T *menu,
7667 int initX,
7668 int initY)
7669{
7670 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7671 int template_len;
7672 int nchar, textWidth, submenuWidth;
7673 DWORD lStyle;
7674 DWORD lExtendedStyle;
7675 WORD dlgwidth;
7676 WORD menuID;
7677 vimmenu_T *pmenu;
7678 vimmenu_T *the_menu = menu;
7679 HWND hwnd;
7680 HDC hdc;
7681 HFONT font, oldFont;
7682 int col, spaceWidth, len;
7683 int columnWidths[2];
7684 char_u *label, *text;
7685 int acLen = 0;
7686 int nameLen;
7687 int padding0, padding1, padding2 = 0;
7688 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007689 int x;
7690 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691#ifdef USE_SYSMENU_FONT
7692 LOGFONT lfSysmenu;
7693 int use_lfSysmenu = FALSE;
7694#endif
7695
7696 /*
7697 * If this menu is already torn off, move it to the mouse position.
7698 */
7699 if (IsWindow(menu->tearoff_handle))
7700 {
7701 POINT mp;
7702 if (GetCursorPos((LPPOINT)&mp))
7703 {
7704 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7705 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7706 }
7707 return;
7708 }
7709
7710 /*
7711 * Create a new tearoff.
7712 */
7713 if (*title == MNU_HIDDEN_CHAR)
7714 title++;
7715
7716 /* Allocate memory to store the dialog template. It's made bigger when
7717 * needed. */
7718 template_len = DLG_ALLOC_SIZE;
7719 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7720 if (p == NULL)
7721 return;
7722
7723 hwnd = GetDesktopWindow();
7724 hdc = GetWindowDC(hwnd);
7725#ifdef USE_SYSMENU_FONT
7726 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7727 {
7728 font = CreateFontIndirect(&lfSysmenu);
7729 use_lfSysmenu = TRUE;
7730 }
7731 else
7732#endif
7733 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7734 VARIABLE_PITCH , DLG_FONT_NAME);
7735 if (s_usenewlook)
7736 oldFont = SelectFont(hdc, font);
7737 else
7738 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7739
7740 /* Calculate width of a single space. Used for padding columns to the
7741 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007742 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743
7744 /* Figure out max width of the text column, the accelerator column and the
7745 * optional submenu column. */
7746 submenuWidth = 0;
7747 for (col = 0; col < 2; col++)
7748 {
7749 columnWidths[col] = 0;
7750 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7751 {
7752 /* Use "dname" here to compute the width of the visible text. */
7753 text = (col == 0) ? pmenu->dname : pmenu->actext;
7754 if (text != NULL && *text != NUL)
7755 {
7756 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7757 if (textWidth > columnWidths[col])
7758 columnWidths[col] = textWidth;
7759 }
7760 if (pmenu->children != NULL)
7761 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7762 }
7763 }
7764 if (columnWidths[1] == 0)
7765 {
7766 /* no accelerators */
7767 if (submenuWidth != 0)
7768 columnWidths[0] += submenuWidth;
7769 else
7770 columnWidths[0] += spaceWidth;
7771 }
7772 else
7773 {
7774 /* there is an accelerator column */
7775 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7776 columnWidths[1] += submenuWidth;
7777 }
7778
7779 /*
7780 * Now find the total width of our 'menu'.
7781 */
7782 textWidth = columnWidths[0] + columnWidths[1];
7783 if (submenuWidth != 0)
7784 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007785 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7787 textWidth += submenuWidth;
7788 }
7789 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7790 if (textWidth > dlgwidth)
7791 dlgwidth = textWidth;
7792 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7793
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 /* start to fill in the dlgtemplate information. addressing by WORDs */
7795 if (s_usenewlook)
7796 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7797 else
7798 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7799
7800 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7801 *p++ = LOWORD(lStyle);
7802 *p++ = HIWORD(lStyle);
7803 *p++ = LOWORD(lExtendedStyle);
7804 *p++ = HIWORD(lExtendedStyle);
7805 pnumitems = p; /* save where the number of items must be stored */
7806 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007807 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007809 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 else
7811 *p++ = PixelToDialogX(initX); // x
7812 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007813 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 else
7815 *p++ = PixelToDialogY(initY); // y
7816 *p++ = PixelToDialogX(dlgwidth); // cx
7817 ptrueheight = p;
7818 *p++ = 0; // dialog height: changed later anyway
7819 *p++ = 0; // Menu
7820 *p++ = 0; // Class
7821
7822 /* copy the title of the dialog */
7823 nchar = nCopyAnsiToWideChar(p, ((*title)
7824 ? (LPSTR)title
7825 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
7826 p += nchar;
7827
7828 if (s_usenewlook)
7829 {
7830 /* do the font, since DS_3DLOOK doesn't work properly */
7831#ifdef USE_SYSMENU_FONT
7832 if (use_lfSysmenu)
7833 {
7834 /* point size */
7835 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7836 GetDeviceCaps(hdc, LOGPIXELSY));
7837 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
7838 }
7839 else
7840#endif
7841 {
7842 *p++ = DLG_FONT_POINT_SIZE; // point size
7843 nchar = nCopyAnsiToWideChar (p, TEXT(DLG_FONT_NAME));
7844 }
7845 p += nchar;
7846 }
7847
7848 /*
7849 * Loop over all the items in the menu.
7850 * But skip over the tearbar.
7851 */
7852 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7853 menu = menu->children->next;
7854 else
7855 menu = menu->children;
7856 for ( ; menu != NULL; menu = menu->next)
7857 {
7858 if (menu->modes == 0) /* this menu has just been deleted */
7859 continue;
7860 if (menu_is_separator(menu->dname))
7861 {
7862 sepPadding += 3;
7863 continue;
7864 }
7865
7866 /* Check if there still is plenty of room in the template. Make it
7867 * larger when needed. */
7868 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7869 {
7870 WORD *newp;
7871
7872 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7873 if (newp != NULL)
7874 {
7875 template_len += 4096;
7876 mch_memmove(newp, pdlgtemplate,
7877 (char *)p - (char *)pdlgtemplate);
7878 p = newp + (p - pdlgtemplate);
7879 pnumitems = newp + (pnumitems - pdlgtemplate);
7880 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7881 LocalFree(LocalHandle(pdlgtemplate));
7882 pdlgtemplate = newp;
7883 }
7884 }
7885
7886 /* Figure out minimal length of this menu label. Use "name" for the
7887 * actual text, "dname" for estimating the displayed size. "name"
7888 * has "&a" for mnemonic and includes the accelerator. */
7889 len = nameLen = (int)STRLEN(menu->name);
7890 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7891 (int)STRLEN(menu->dname))) / spaceWidth;
7892 len += padding0;
7893
7894 if (menu->actext != NULL)
7895 {
7896 acLen = (int)STRLEN(menu->actext);
7897 len += acLen;
7898 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7899 }
7900 else
7901 textWidth = 0;
7902 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7903 len += padding1;
7904
7905 if (menu->children == NULL)
7906 {
7907 padding2 = submenuWidth / spaceWidth;
7908 len += padding2;
7909 menuID = (WORD)(menu->id);
7910 }
7911 else
7912 {
7913 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007914 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 }
7916
7917 /* Allocate menu label and fill it in */
7918 text = label = alloc((unsigned)len + 1);
7919 if (label == NULL)
7920 break;
7921
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007922 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 text = vim_strchr(text, TAB); /* stop at TAB before actext */
7924 if (text == NULL)
7925 text = label + nameLen; /* no actext, use whole name */
7926 while (padding0-- > 0)
7927 *text++ = ' ';
7928 if (menu->actext != NULL)
7929 {
7930 STRNCPY(text, menu->actext, acLen);
7931 text += acLen;
7932 }
7933 while (padding1-- > 0)
7934 *text++ = ' ';
7935 if (menu->children != NULL)
7936 {
7937 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7938 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7939 }
7940 else
7941 {
7942 while (padding2-- > 0)
7943 *text++ = ' ';
7944 }
7945 *text = NUL;
7946
7947 /*
7948 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7949 * W95/NT4 it makes the tear-off look more like a menu.
7950 */
7951 p = add_dialog_element(p,
7952 BS_PUSHBUTTON|BS_LEFT,
7953 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7954 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7955 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7956 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007957 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 vim_free(label);
7959 (*pnumitems)++;
7960 }
7961
7962 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7963
7964
7965 /* show modelessly */
7966 the_menu->tearoff_handle = CreateDialogIndirect(
7967 s_hinst,
7968 (LPDLGTEMPLATE)pdlgtemplate,
7969 s_hwnd,
7970 (DLGPROC)tearoff_callback);
7971
7972 LocalFree(LocalHandle(pdlgtemplate));
7973 SelectFont(hdc, oldFont);
7974 DeleteObject(font);
7975 ReleaseDC(hwnd, hdc);
7976
7977 /*
7978 * Reassert ourselves as the active window. This is so that after creating
7979 * a tearoff, the user doesn't have to click with the mouse just to start
7980 * typing again!
7981 */
7982 (void)SetActiveWindow(s_hwnd);
7983
7984 /* make sure the right buttons are enabled */
7985 force_menu_update = TRUE;
7986}
7987#endif
7988
7989#if defined(FEAT_TOOLBAR) || defined(PROTO)
7990#include "gui_w32_rc.h"
7991
7992/* This not defined in older SDKs */
7993# ifndef TBSTYLE_FLAT
7994# define TBSTYLE_FLAT 0x0800
7995# endif
7996
7997/*
7998 * Create the toolbar, initially unpopulated.
7999 * (just like the menu, there are no defaults, it's all
8000 * set up through menu.vim)
8001 */
8002 static void
8003initialise_toolbar(void)
8004{
8005 InitCommonControls();
8006 s_toolbarhwnd = CreateToolbarEx(
8007 s_hwnd,
8008 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8009 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008010 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 s_hinst,
8012 IDR_TOOLBAR1, // id of initial bitmap
8013 NULL,
8014 0, // initial number of buttons
8015 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8016 TOOLBAR_BUTTON_HEIGHT,
8017 TOOLBAR_BUTTON_WIDTH,
8018 TOOLBAR_BUTTON_HEIGHT,
8019 sizeof(TBBUTTON)
8020 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008021 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022
8023 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
8024}
8025
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008026 static LRESULT CALLBACK
8027toolbar_wndproc(
8028 HWND hwnd,
8029 UINT uMsg,
8030 WPARAM wParam,
8031 LPARAM lParam)
8032{
8033 HandleMouseHide(uMsg, lParam);
8034 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8035}
8036
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 static int
8038get_toolbar_bitmap(vimmenu_T *menu)
8039{
8040 int i = -1;
8041
8042 /*
8043 * Check user bitmaps first, unless builtin is specified.
8044 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008045 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 {
8047 char_u fname[MAXPATHL];
8048 HANDLE hbitmap = NULL;
8049
8050 if (menu->iconfile != NULL)
8051 {
8052 gui_find_iconfile(menu->iconfile, fname, "bmp");
8053 hbitmap = LoadImage(
8054 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008055 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 IMAGE_BITMAP,
8057 TOOLBAR_BUTTON_WIDTH,
8058 TOOLBAR_BUTTON_HEIGHT,
8059 LR_LOADFROMFILE |
8060 LR_LOADMAP3DCOLORS
8061 );
8062 }
8063
8064 /*
8065 * If the LoadImage call failed, or the "icon=" file
8066 * didn't exist or wasn't specified, try the menu name
8067 */
8068 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008069 && (gui_find_bitmap(
8070#ifdef FEAT_MULTI_LANG
8071 menu->en_dname != NULL ? menu->en_dname :
8072#endif
8073 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 hbitmap = LoadImage(
8075 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008076 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 IMAGE_BITMAP,
8078 TOOLBAR_BUTTON_WIDTH,
8079 TOOLBAR_BUTTON_HEIGHT,
8080 LR_LOADFROMFILE |
8081 LR_LOADMAP3DCOLORS
8082 );
8083
8084 if (hbitmap != NULL)
8085 {
8086 TBADDBITMAP tbAddBitmap;
8087
8088 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008089 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090
8091 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8092 (WPARAM)1, (LPARAM)&tbAddBitmap);
8093 /* i will be set to -1 if it fails */
8094 }
8095 }
8096 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8097 i = menu->iconidx;
8098
8099 return i;
8100}
8101#endif
8102
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008103#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8104 static void
8105initialise_tabline(void)
8106{
8107 InitCommonControls();
8108
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008109 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008110 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008111 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8112 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008113 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008114
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008115 gui.tabline_height = TABLINE_HEIGHT;
8116
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008117# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008118 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008119# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008120}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008121
8122 static LRESULT CALLBACK
8123tabline_wndproc(
8124 HWND hwnd,
8125 UINT uMsg,
8126 WPARAM wParam,
8127 LPARAM lParam)
8128{
8129 HandleMouseHide(uMsg, lParam);
8130 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8131}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008132#endif
8133
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8135/*
8136 * Make the GUI window come to the foreground.
8137 */
8138 void
8139gui_mch_set_foreground(void)
8140{
8141 if (IsIconic(s_hwnd))
8142 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8143 SetForegroundWindow(s_hwnd);
8144}
8145#endif
8146
8147#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8148 static void
8149dyn_imm_load(void)
8150{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008151 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 if (hLibImm == NULL)
8153 return;
8154
8155 pImmGetCompositionStringA
8156 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8157 pImmGetCompositionStringW
8158 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8159 pImmGetContext
8160 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8161 pImmAssociateContext
8162 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8163 pImmReleaseContext
8164 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8165 pImmGetOpenStatus
8166 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8167 pImmSetOpenStatus
8168 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
8169 pImmGetCompositionFont
8170 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
8171 pImmSetCompositionFont
8172 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
8173 pImmSetCompositionWindow
8174 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8175 pImmGetConversionStatus
8176 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008177 pImmSetConversionStatus
8178 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179
8180 if ( pImmGetCompositionStringA == NULL
8181 || pImmGetCompositionStringW == NULL
8182 || pImmGetContext == NULL
8183 || pImmAssociateContext == NULL
8184 || pImmReleaseContext == NULL
8185 || pImmGetOpenStatus == NULL
8186 || pImmSetOpenStatus == NULL
8187 || pImmGetCompositionFont == NULL
8188 || pImmSetCompositionFont == NULL
8189 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008190 || pImmGetConversionStatus == NULL
8191 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {
8193 FreeLibrary(hLibImm);
8194 hLibImm = NULL;
8195 pImmGetContext = NULL;
8196 return;
8197 }
8198
8199 return;
8200}
8201
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202#endif
8203
8204#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8205
8206# ifdef FEAT_XPM_W32
8207# define IMAGE_XPM 100
8208# endif
8209
8210typedef struct _signicon_t
8211{
8212 HANDLE hImage;
8213 UINT uType;
8214#ifdef FEAT_XPM_W32
8215 HANDLE hShape; /* Mask bitmap handle */
8216#endif
8217} signicon_t;
8218
8219 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008220gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221{
8222 signicon_t *sign;
8223 int x, y, w, h;
8224
8225 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8226 return;
8227
8228 x = TEXT_X(col);
8229 y = TEXT_Y(row);
8230 w = gui.char_width * 2;
8231 h = gui.char_height;
8232 switch (sign->uType)
8233 {
8234 case IMAGE_BITMAP:
8235 {
8236 HDC hdcMem;
8237 HBITMAP hbmpOld;
8238
8239 hdcMem = CreateCompatibleDC(s_hdc);
8240 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8241 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8242 SelectObject(hdcMem, hbmpOld);
8243 DeleteDC(hdcMem);
8244 }
8245 break;
8246 case IMAGE_ICON:
8247 case IMAGE_CURSOR:
8248 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8249 break;
8250#ifdef FEAT_XPM_W32
8251 case IMAGE_XPM:
8252 {
8253 HDC hdcMem;
8254 HBITMAP hbmpOld;
8255
8256 hdcMem = CreateCompatibleDC(s_hdc);
8257 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8258 /* Make hole */
8259 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8260
8261 SelectObject(hdcMem, sign->hImage);
8262 /* Paint sign */
8263 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8264 SelectObject(hdcMem, hbmpOld);
8265 DeleteDC(hdcMem);
8266 }
8267 break;
8268#endif
8269 }
8270}
8271
8272 static void
8273close_signicon_image(signicon_t *sign)
8274{
8275 if (sign)
8276 switch (sign->uType)
8277 {
8278 case IMAGE_BITMAP:
8279 DeleteObject((HGDIOBJ)sign->hImage);
8280 break;
8281 case IMAGE_CURSOR:
8282 DestroyCursor((HCURSOR)sign->hImage);
8283 break;
8284 case IMAGE_ICON:
8285 DestroyIcon((HICON)sign->hImage);
8286 break;
8287#ifdef FEAT_XPM_W32
8288 case IMAGE_XPM:
8289 DeleteObject((HBITMAP)sign->hImage);
8290 DeleteObject((HBITMAP)sign->hShape);
8291 break;
8292#endif
8293 }
8294}
8295
8296 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008297gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298{
8299 signicon_t sign, *psign;
8300 char_u *ext;
8301
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008303 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 if (ext > signfile)
8305 {
8306 int do_load = 1;
8307
8308 if (!STRICMP(ext, ".bmp"))
8309 sign.uType = IMAGE_BITMAP;
8310 else if (!STRICMP(ext, ".ico"))
8311 sign.uType = IMAGE_ICON;
8312 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8313 sign.uType = IMAGE_CURSOR;
8314 else
8315 do_load = 0;
8316
8317 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008318 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 gui.char_width * 2, gui.char_height,
8320 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8321#ifdef FEAT_XPM_W32
8322 if (!STRICMP(ext, ".xpm"))
8323 {
8324 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008325 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8326 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 }
8328#endif
8329 }
8330
8331 psign = NULL;
8332 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8333 != NULL)
8334 *psign = sign;
8335
8336 if (!psign)
8337 {
8338 if (sign.hImage)
8339 close_signicon_image(&sign);
8340 EMSG(_(e_signdata));
8341 }
8342 return (void *)psign;
8343
8344}
8345
8346 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008347gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348{
8349 if (sign)
8350 {
8351 close_signicon_image((signicon_t *)sign);
8352 vim_free(sign);
8353 }
8354}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356
8357#if defined(FEAT_BEVAL) || defined(PROTO)
8358
8359/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008360 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 *
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00008362 * The only reused thing is gui_beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8364 * to get current mouse position).
8365 *
8366 * Trying to use as more Windows services as possible, and as less
8367 * IE version as possible :)).
8368 *
8369 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8370 * BalloonEval struct.
8371 * 2) Enable/Disable simply create/kill BalloonEval Timer
8372 * 3) When there was enough inactivity, timer procedure posts
8373 * async request to debugger
8374 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8375 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008376 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 */
8378
Bram Moolenaar45360022005-07-21 21:08:21 +00008379/*
8380 * determine whether installed Common Controls support multiline tooltips
8381 * (i.e. their version is >= 4.70
8382 */
8383 int
8384multiline_balloon_available(void)
8385{
8386 HINSTANCE hDll;
8387 static char comctl_dll[] = "comctl32.dll";
8388 static int multiline_tip = MAYBE;
8389
8390 if (multiline_tip != MAYBE)
8391 return multiline_tip;
8392
8393 hDll = GetModuleHandle(comctl_dll);
8394 if (hDll != NULL)
8395 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008396 DLLGETVERSIONPROC pGetVer;
8397 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008398
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008399 if (pGetVer != NULL)
8400 {
8401 DLLVERSIONINFO dvi;
8402 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008403
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008404 ZeroMemory(&dvi, sizeof(dvi));
8405 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008406
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008407 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008408
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008409 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008410 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008411 || (dvi.dwMajorVersion == 4
8412 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008413 {
8414 multiline_tip = TRUE;
8415 return multiline_tip;
8416 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008417 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008418 else
8419 {
8420 /* there is chance we have ancient CommCtl 4.70
8421 which doesn't export DllGetVersion */
8422 DWORD dwHandle = 0;
8423 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8424 if (len > 0)
8425 {
8426 VS_FIXEDFILEINFO *ver;
8427 UINT vlen = 0;
8428 void *data = alloc(len);
8429
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008430 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008431 && GetFileVersionInfo(comctl_dll, 0, len, data)
8432 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8433 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008434 && HIWORD(ver->dwFileVersionMS) > 4)
8435 || ((HIWORD(ver->dwFileVersionMS) == 4
8436 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008437 {
8438 vim_free(data);
8439 multiline_tip = TRUE;
8440 return multiline_tip;
8441 }
8442 vim_free(data);
8443 }
8444 }
8445 }
8446 multiline_tip = FALSE;
8447 return multiline_tip;
8448}
8449
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008451make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452{
Bram Moolenaar45360022005-07-21 21:08:21 +00008453 TOOLINFO *pti;
8454 int ToolInfoSize;
8455
8456 if (multiline_balloon_available() == TRUE)
8457 ToolInfoSize = sizeof(TOOLINFO_NEW);
8458 else
8459 ToolInfoSize = sizeof(TOOLINFO);
8460
8461 pti = (TOOLINFO *)alloc(ToolInfoSize);
8462 if (pti == NULL)
8463 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464
8465 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
8466 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8467 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8468 beval->target, NULL, s_hinst, NULL);
8469
8470 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8471 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8472
Bram Moolenaar45360022005-07-21 21:08:21 +00008473 pti->cbSize = ToolInfoSize;
8474 pti->uFlags = TTF_SUBCLASS;
8475 pti->hwnd = beval->target;
8476 pti->hinst = 0; /* Don't use string resources */
8477 pti->uId = ID_BEVAL_TOOLTIP;
8478
8479 if (multiline_balloon_available() == TRUE)
8480 {
8481 RECT rect;
8482 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
8483 pti->lpszText = LPSTR_TEXTCALLBACK;
8484 ptin->lParam = (LPARAM)text;
8485 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
8486 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8487 (LPARAM)rect.right);
8488 }
8489 else
8490 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491
8492 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00008493 pti->rect.left = pt.x - 3;
8494 pti->rect.top = pt.y - 3;
8495 pti->rect.right = pt.x + 3;
8496 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497
Bram Moolenaar45360022005-07-21 21:08:21 +00008498 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 /* Make tooltip appear sooner */
8500 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008501 /* I've performed some tests and it seems the longest possible life time
8502 * of tooltip is 30 seconds */
8503 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 /*
8505 * HACK: force tooltip to appear, because it'll not appear until
8506 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008507 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008509 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00008511 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512}
8513
8514 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008515delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008517 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518}
8519
8520 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008521BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008522 HWND hwnd UNUSED,
8523 UINT uMsg UNUSED,
8524 UINT_PTR idEvent UNUSED,
8525 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526{
8527 POINT pt;
8528 RECT rect;
8529
8530 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8531 return;
8532
8533 GetCursorPos(&pt);
8534 if (WindowFromPoint(pt) != s_textArea)
8535 return;
8536
8537 ScreenToClient(s_textArea, &pt);
8538 GetClientRect(s_textArea, &rect);
8539 if (!PtInRect(&rect, pt))
8540 return;
8541
8542 if (LastActivity > 0
8543 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8544 && (cur_beval->showState != ShS_PENDING
8545 || abs(cur_beval->x - pt.x) > 3
8546 || abs(cur_beval->y - pt.y) > 3))
8547 {
8548 /* Pointer resting in one place long enough, it's time to show
8549 * the tooltip. */
8550 cur_beval->showState = ShS_PENDING;
8551 cur_beval->x = pt.x;
8552 cur_beval->y = pt.y;
8553
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008554 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555
8556 if (cur_beval->msgCB != NULL)
8557 (*cur_beval->msgCB)(cur_beval, 0);
8558 }
8559}
8560
8561 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008562gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008564 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008566 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567}
8568
8569 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008570gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008572 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 if (beval == NULL)
8574 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008575 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008576 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008577 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578}
8579
8580 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008581gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582{
8583 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008584
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008585 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 if (beval->showState == ShS_SHOWING)
8587 return;
8588 GetCursorPos(&pt);
8589 ScreenToClient(s_textArea, &pt);
8590
8591 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008593 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 gui_mch_disable_beval_area(cur_beval);
8595 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008596 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008598 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599}
8600
8601 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008602gui_mch_create_beval_area(
8603 void *target, /* ignored, always use s_textArea */
8604 char_u *mesg,
8605 void (*mesgCB)(BalloonEval *, int),
8606 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607{
8608 /* partially stolen from gui_beval.c */
8609 BalloonEval *beval;
8610
8611 if (mesg != NULL && mesgCB != NULL)
8612 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01008613 IEMSG(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 return NULL;
8615 }
8616
8617 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
8618 if (beval != NULL)
8619 {
8620 beval->target = s_textArea;
8621 beval->balloon = NULL;
8622
8623 beval->showState = ShS_NEUTRAL;
8624 beval->x = 0;
8625 beval->y = 0;
8626 beval->msg = mesg;
8627 beval->msgCB = mesgCB;
8628 beval->clientData = clientData;
8629
8630 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 cur_beval = beval;
8632
8633 if (p_beval)
8634 gui_mch_enable_beval_area(beval);
8635
8636 }
8637 return beval;
8638}
8639
8640 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008641Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642{
8643 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8644 return;
8645
8646 if (cur_beval != NULL)
8647 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008648 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008650 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008651 // TRACE0("TTN_SHOW {{{");
8652 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008653 break;
8654 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008655 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 delete_tooltip(cur_beval);
8657 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008658 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659
8660 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008661 break;
8662 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008663 {
8664 /* if you get there then we have new common controls */
8665 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8666 info->lpszText = (LPSTR)info->lParam;
8667 info->uFlags |= TTF_DI_SETITEM;
8668 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008669 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 }
8671 }
8672}
8673
8674 static void
8675TrackUserActivity(UINT uMsg)
8676{
8677 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8678 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8679 LastActivity = GetTickCount();
8680}
8681
8682 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008683gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684{
8685 vim_free(beval);
8686}
8687#endif /* FEAT_BEVAL */
8688
8689#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8690/*
8691 * We have multiple signs to draw at the same location. Draw the
8692 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8693 */
8694 void
8695netbeans_draw_multisign_indicator(int row)
8696{
8697 int i;
8698 int y;
8699 int x;
8700
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008701 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008702 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008703
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 x = 0;
8705 y = TEXT_Y(row);
8706
8707 for (i = 0; i < gui.char_height - 3; i++)
8708 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8709
8710 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8711 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8712 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8713 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8714 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8715 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8716 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8717}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008718#endif