blob: 611902557e2ed83c752f46d37ad479bdfac12bd3 [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{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002630 char_u *keys = eap->arg;
2631 int fill_typebuf = FALSE;
2632 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002633
2634 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2635 while (*keys)
2636 {
2637 if (*keys == '~')
2638 *keys = ' '; /* for showing system menu */
2639 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2640 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002641 fill_typebuf = TRUE;
2642 }
2643 if (fill_typebuf)
2644 {
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002645 /* Put a NOP in the typeahead buffer so that the message will get
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002646 * processed. */
2647 key_name[0] = K_SPECIAL;
2648 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002649 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002650 key_name[3] = NUL;
2651 typebuf_was_filled = TRUE;
2652 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002653 }
2654}
2655
2656/*
2657 * Create the find & replace dialogs.
2658 * You can't have both at once: ":find" when replace is showing, destroys
2659 * the replace dialog first, and the other way around.
2660 */
2661#ifdef MSWIN_FIND_REPLACE
2662 static void
2663initialise_findrep(char_u *initial_string)
2664{
2665 int wword = FALSE;
2666 int mcase = !p_ic;
2667 char_u *entry_text;
2668
2669 /* Get the search string to use. */
2670 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2671
2672 s_findrep_struct.hwndOwner = s_hwnd;
2673 s_findrep_struct.Flags = FR_DOWN;
2674 if (mcase)
2675 s_findrep_struct.Flags |= FR_MATCHCASE;
2676 if (wword)
2677 s_findrep_struct.Flags |= FR_WHOLEWORD;
2678 if (entry_text != NULL && *entry_text != NUL)
2679 vim_strncpy((char_u *)s_findrep_struct.lpstrFindWhat, entry_text,
2680 s_findrep_struct.wFindWhatLen - 1);
2681 vim_free(entry_text);
2682}
2683#endif
2684
2685 static void
2686set_window_title(HWND hwnd, char *title)
2687{
2688#ifdef FEAT_MBYTE
2689 if (title != NULL && enc_codepage >= 0 && enc_codepage != (int)GetACP())
2690 {
2691 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002692
2693 /* Convert the title from 'encoding' to UTF-16. */
2694 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2695 if (wbuf != NULL)
2696 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002697 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002698 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002699 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002700 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002701 }
2702#endif
2703 (void)SetWindowText(hwnd, (LPCSTR)title);
2704}
2705
2706 void
2707gui_mch_find_dialog(exarg_T *eap)
2708{
2709#ifdef MSWIN_FIND_REPLACE
2710 if (s_findrep_msg != 0)
2711 {
2712 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2713 DestroyWindow(s_findrep_hwnd);
2714
2715 if (!IsWindow(s_findrep_hwnd))
2716 {
2717 initialise_findrep(eap->arg);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002718# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002719 /* If the OS is Windows NT, and 'encoding' differs from active
2720 * codepage: convert text and use wide function. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002721 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002722 {
2723 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2724 s_findrep_hwnd = FindTextW(
2725 (LPFINDREPLACEW) &s_findrep_struct_w);
2726 }
2727 else
2728# endif
2729 s_findrep_hwnd = FindText((LPFINDREPLACE) &s_findrep_struct);
2730 }
2731
2732 set_window_title(s_findrep_hwnd,
2733 _("Find string (use '\\\\' to find a '\\')"));
2734 (void)SetFocus(s_findrep_hwnd);
2735
2736 s_findrep_is_find = TRUE;
2737 }
2738#endif
2739}
2740
2741
2742 void
2743gui_mch_replace_dialog(exarg_T *eap)
2744{
2745#ifdef MSWIN_FIND_REPLACE
2746 if (s_findrep_msg != 0)
2747 {
2748 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2749 DestroyWindow(s_findrep_hwnd);
2750
2751 if (!IsWindow(s_findrep_hwnd))
2752 {
2753 initialise_findrep(eap->arg);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002754# ifdef FEAT_MBYTE
2755 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002756 {
2757 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2758 s_findrep_hwnd = ReplaceTextW(
2759 (LPFINDREPLACEW) &s_findrep_struct_w);
2760 }
2761 else
2762# endif
2763 s_findrep_hwnd = ReplaceText(
2764 (LPFINDREPLACE) &s_findrep_struct);
2765 }
2766
2767 set_window_title(s_findrep_hwnd,
2768 _("Find & Replace (use '\\\\' to find a '\\')"));
2769 (void)SetFocus(s_findrep_hwnd);
2770
2771 s_findrep_is_find = FALSE;
2772 }
2773#endif
2774}
2775
2776
2777/*
2778 * Set visibility of the pointer.
2779 */
2780 void
2781gui_mch_mousehide(int hide)
2782{
2783 if (hide != gui.pointer_hidden)
2784 {
2785 ShowCursor(!hide);
2786 gui.pointer_hidden = hide;
2787 }
2788}
2789
2790#ifdef FEAT_MENU
2791 static void
2792gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2793{
2794 /* Unhide the mouse, we don't get move events here. */
2795 gui_mch_mousehide(FALSE);
2796
2797 (void)TrackPopupMenu(
2798 (HMENU)menu->submenu_id,
2799 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2800 x, y,
2801 (int)0, /*reserved param*/
2802 s_hwnd,
2803 NULL);
2804 /*
2805 * NOTE: The pop-up menu can eat the mouse up event.
2806 * We deal with this in normal.c.
2807 */
2808}
2809#endif
2810
2811/*
2812 * Got a message when the system will go down.
2813 */
2814 static void
2815_OnEndSession(void)
2816{
2817 getout_preserve_modified(1);
2818}
2819
2820/*
2821 * Get this message when the user clicks on the cross in the top right corner
2822 * of a Windows95 window.
2823 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002824 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002825_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002826{
2827 gui_shell_closed();
2828}
2829
2830/*
2831 * Get a message when the window is being destroyed.
2832 */
2833 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002834_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002835{
2836 if (!destroying)
2837 _OnClose(hwnd);
2838}
2839
2840 static void
2841_OnPaint(
2842 HWND hwnd)
2843{
2844 if (!IsMinimized(hwnd))
2845 {
2846 PAINTSTRUCT ps;
2847
2848 out_flush(); /* make sure all output has been processed */
2849 (void)BeginPaint(hwnd, &ps);
2850#if defined(FEAT_DIRECTX)
2851 if (IS_ENABLE_DIRECTX())
2852 DWriteContext_BeginDraw(s_dwc);
2853#endif
2854
2855#ifdef FEAT_MBYTE
2856 /* prevent multi-byte characters from misprinting on an invalid
2857 * rectangle */
2858 if (has_mbyte)
2859 {
2860 RECT rect;
2861
2862 GetClientRect(hwnd, &rect);
2863 ps.rcPaint.left = rect.left;
2864 ps.rcPaint.right = rect.right;
2865 }
2866#endif
2867
2868 if (!IsRectEmpty(&ps.rcPaint))
2869 {
2870#if defined(FEAT_DIRECTX)
2871 if (IS_ENABLE_DIRECTX())
2872 DWriteContext_BindDC(s_dwc, s_hdc, &ps.rcPaint);
2873#endif
2874 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2875 ps.rcPaint.right - ps.rcPaint.left + 1,
2876 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2877 }
2878
2879#if defined(FEAT_DIRECTX)
2880 if (IS_ENABLE_DIRECTX())
2881 DWriteContext_EndDraw(s_dwc);
2882#endif
2883 EndPaint(hwnd, &ps);
2884 }
2885}
2886
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002887 static void
2888_OnSize(
2889 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002890 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002891 int cx,
2892 int cy)
2893{
2894 if (!IsMinimized(hwnd))
2895 {
2896 gui_resize_shell(cx, cy);
2897
2898#ifdef FEAT_MENU
2899 /* Menu bar may wrap differently now */
2900 gui_mswin_get_menu_height(TRUE);
2901#endif
2902 }
2903}
2904
2905 static void
2906_OnSetFocus(
2907 HWND hwnd,
2908 HWND hwndOldFocus)
2909{
2910 gui_focus_change(TRUE);
2911 s_getting_focus = TRUE;
2912 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2913}
2914
2915 static void
2916_OnKillFocus(
2917 HWND hwnd,
2918 HWND hwndNewFocus)
2919{
2920 gui_focus_change(FALSE);
2921 s_getting_focus = FALSE;
2922 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2923}
2924
2925/*
2926 * Get a message when the user switches back to vim
2927 */
2928 static LRESULT
2929_OnActivateApp(
2930 HWND hwnd,
2931 BOOL fActivate,
2932 DWORD dwThreadId)
2933{
2934 /* we call gui_focus_change() in _OnSetFocus() */
2935 /* gui_focus_change((int)fActivate); */
2936 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2937}
2938
2939#if defined(FEAT_WINDOWS) || defined(PROTO)
2940 void
2941gui_mch_destroy_scrollbar(scrollbar_T *sb)
2942{
2943 DestroyWindow(sb->id);
2944}
2945#endif
2946
2947/*
2948 * Get current mouse coordinates in text window.
2949 */
2950 void
2951gui_mch_getmouse(int *x, int *y)
2952{
2953 RECT rct;
2954 POINT mp;
2955
2956 (void)GetWindowRect(s_textArea, &rct);
2957 (void)GetCursorPos((LPPOINT)&mp);
2958 *x = (int)(mp.x - rct.left);
2959 *y = (int)(mp.y - rct.top);
2960}
2961
2962/*
2963 * Move mouse pointer to character at (x, y).
2964 */
2965 void
2966gui_mch_setmouse(int x, int y)
2967{
2968 RECT rct;
2969
2970 (void)GetWindowRect(s_textArea, &rct);
2971 (void)SetCursorPos(x + gui.border_offset + rct.left,
2972 y + gui.border_offset + rct.top);
2973}
2974
2975 static void
2976gui_mswin_get_valid_dimensions(
2977 int w,
2978 int h,
2979 int *valid_w,
2980 int *valid_h)
2981{
2982 int base_width, base_height;
2983
2984 base_width = gui_get_base_width()
2985 + (GetSystemMetrics(SM_CXFRAME) +
2986 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
2987 base_height = gui_get_base_height()
2988 + (GetSystemMetrics(SM_CYFRAME) +
2989 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
2990 + GetSystemMetrics(SM_CYCAPTION)
2991#ifdef FEAT_MENU
2992 + gui_mswin_get_menu_height(FALSE)
2993#endif
2994 ;
2995 *valid_w = base_width +
2996 ((w - base_width) / gui.char_width) * gui.char_width;
2997 *valid_h = base_height +
2998 ((h - base_height) / gui.char_height) * gui.char_height;
2999}
3000
3001 void
3002gui_mch_flash(int msec)
3003{
3004 RECT rc;
3005
3006 /*
3007 * Note: InvertRect() excludes right and bottom of rectangle.
3008 */
3009 rc.left = 0;
3010 rc.top = 0;
3011 rc.right = gui.num_cols * gui.char_width;
3012 rc.bottom = gui.num_rows * gui.char_height;
3013 InvertRect(s_hdc, &rc);
3014 gui_mch_flush(); /* make sure it's displayed */
3015
3016 ui_delay((long)msec, TRUE); /* wait for a few msec */
3017
3018 InvertRect(s_hdc, &rc);
3019}
3020
3021/*
3022 * Return flags used for scrolling.
3023 * The SW_INVALIDATE is required when part of the window is covered or
3024 * off-screen. Refer to MS KB Q75236.
3025 */
3026 static int
3027get_scroll_flags(void)
3028{
3029 HWND hwnd;
3030 RECT rcVim, rcOther, rcDest;
3031
3032 GetWindowRect(s_hwnd, &rcVim);
3033
3034 /* Check if the window is partly above or below the screen. We don't care
3035 * about partly left or right of the screen, it is not relevant when
3036 * scrolling up or down. */
3037 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
3038 return SW_INVALIDATE;
3039
3040 /* Check if there is an window (partly) on top of us. */
3041 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3042 if (IsWindowVisible(hwnd))
3043 {
3044 GetWindowRect(hwnd, &rcOther);
3045 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3046 return SW_INVALIDATE;
3047 }
3048 return 0;
3049}
3050
3051/*
3052 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3053 * may not be scrolled out properly.
3054 * For gVim, when _OnScroll() is repeated, the character at the
3055 * previous cursor position may be left drawn after scroll.
3056 * The problem can be avoided by calling GetPixel() to get a pixel in
3057 * the region before ScrollWindowEx().
3058 */
3059 static void
3060intel_gpu_workaround(void)
3061{
3062 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3063}
3064
3065/*
3066 * Delete the given number of lines from the given row, scrolling up any
3067 * text further down within the scroll region.
3068 */
3069 void
3070gui_mch_delete_lines(
3071 int row,
3072 int num_lines)
3073{
3074 RECT rc;
3075
3076 intel_gpu_workaround();
3077
3078 rc.left = FILL_X(gui.scroll_region_left);
3079 rc.right = FILL_X(gui.scroll_region_right + 1);
3080 rc.top = FILL_Y(row);
3081 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3082
3083 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
3084 &rc, &rc, NULL, NULL, get_scroll_flags());
3085
3086 UpdateWindow(s_textArea);
3087 /* This seems to be required to avoid the cursor disappearing when
3088 * scrolling such that the cursor ends up in the top-left character on
3089 * the screen... But why? (Webb) */
3090 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3091 /* gui.cursor_is_valid = FALSE; */
3092
3093 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3094 gui.scroll_region_left,
3095 gui.scroll_region_bot, gui.scroll_region_right);
3096}
3097
3098/*
3099 * Insert the given number of lines before the given row, scrolling down any
3100 * following text within the scroll region.
3101 */
3102 void
3103gui_mch_insert_lines(
3104 int row,
3105 int num_lines)
3106{
3107 RECT rc;
3108
3109 intel_gpu_workaround();
3110
3111 rc.left = FILL_X(gui.scroll_region_left);
3112 rc.right = FILL_X(gui.scroll_region_right + 1);
3113 rc.top = FILL_Y(row);
3114 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3115 /* The SW_INVALIDATE is required when part of the window is covered or
3116 * off-screen. How do we avoid it when it's not needed? */
3117 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
3118 &rc, &rc, NULL, NULL, get_scroll_flags());
3119
3120 UpdateWindow(s_textArea);
3121
3122 gui_clear_block(row, gui.scroll_region_left,
3123 row + num_lines - 1, gui.scroll_region_right);
3124}
3125
3126
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003127 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003128gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003129{
3130#if defined(FEAT_DIRECTX)
3131 DWriteContext_Close(s_dwc);
3132 DWrite_Final();
3133 s_dwc = NULL;
3134#endif
3135
3136 ReleaseDC(s_textArea, s_hdc);
3137 DeleteObject(s_brush);
3138
3139#ifdef FEAT_TEAROFF
3140 /* Unload the tearoff bitmap */
3141 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3142#endif
3143
3144 /* Destroy our window (if we have one). */
3145 if (s_hwnd != NULL)
3146 {
3147 destroying = TRUE; /* ignore WM_DESTROY message now */
3148 DestroyWindow(s_hwnd);
3149 }
3150
3151#ifdef GLOBAL_IME
3152 global_ime_end();
3153#endif
3154}
3155
3156 static char_u *
3157logfont2name(LOGFONT lf)
3158{
3159 char *p;
3160 char *res;
3161 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003162 char *quality_name;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003163 char *font_name = lf.lfFaceName;
3164
3165 charset_name = charset_id2name((int)lf.lfCharSet);
3166#ifdef FEAT_MBYTE
3167 /* Convert a font name from the current codepage to 'encoding'.
3168 * TODO: Use Wide APIs (including LOGFONTW) instead of ANSI APIs. */
3169 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
3170 {
3171 int len;
3172 acp_to_enc((char_u *)lf.lfFaceName, (int)strlen(lf.lfFaceName),
3173 (char_u **)&font_name, &len);
3174 }
3175#endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003176 quality_name = quality_id2name((int)lf.lfQuality);
3177
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003178 res = (char *)alloc((unsigned)(strlen(font_name) + 20
3179 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
3180 if (res != NULL)
3181 {
3182 p = res;
3183 /* make a normal font string out of the lf thing:*/
3184 sprintf((char *)p, "%s:h%d", font_name, pixels_to_points(
3185 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
3186 while (*p)
3187 {
3188 if (*p == ' ')
3189 *p = '_';
3190 ++p;
3191 }
3192 if (lf.lfItalic)
3193 STRCAT(p, ":i");
3194 if (lf.lfWeight >= FW_BOLD)
3195 STRCAT(p, ":b");
3196 if (lf.lfUnderline)
3197 STRCAT(p, ":u");
3198 if (lf.lfStrikeOut)
3199 STRCAT(p, ":s");
3200 if (charset_name != NULL)
3201 {
3202 STRCAT(p, ":c");
3203 STRCAT(p, charset_name);
3204 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003205 if (quality_name != NULL)
3206 {
3207 STRCAT(p, ":q");
3208 STRCAT(p, quality_name);
3209 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003210 }
3211
3212#ifdef FEAT_MBYTE
3213 if (font_name != lf.lfFaceName)
3214 vim_free(font_name);
3215#endif
3216 return (char_u *)res;
3217}
3218
3219
3220#ifdef FEAT_MBYTE_IME
3221/*
3222 * Set correct LOGFONT to IME. Use 'guifontwide' if available, otherwise use
3223 * 'guifont'
3224 */
3225 static void
3226update_im_font(void)
3227{
3228 LOGFONT lf_wide;
3229
3230 if (p_guifontwide != NULL && *p_guifontwide != NUL
3231 && gui.wide_font != NOFONT
3232 && GetObject((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
3233 norm_logfont = lf_wide;
3234 else
3235 norm_logfont = sub_logfont;
3236 im_set_font(&norm_logfont);
3237}
3238#endif
3239
3240#ifdef FEAT_MBYTE
3241/*
3242 * Handler of gui.wide_font (p_guifontwide) changed notification.
3243 */
3244 void
3245gui_mch_wide_font_changed(void)
3246{
3247 LOGFONT lf;
3248
3249# ifdef FEAT_MBYTE_IME
3250 update_im_font();
3251# endif
3252
3253 gui_mch_free_font(gui.wide_ital_font);
3254 gui.wide_ital_font = NOFONT;
3255 gui_mch_free_font(gui.wide_bold_font);
3256 gui.wide_bold_font = NOFONT;
3257 gui_mch_free_font(gui.wide_boldital_font);
3258 gui.wide_boldital_font = NOFONT;
3259
3260 if (gui.wide_font
3261 && GetObject((HFONT)gui.wide_font, sizeof(lf), &lf))
3262 {
3263 if (!lf.lfItalic)
3264 {
3265 lf.lfItalic = TRUE;
3266 gui.wide_ital_font = get_font_handle(&lf);
3267 lf.lfItalic = FALSE;
3268 }
3269 if (lf.lfWeight < FW_BOLD)
3270 {
3271 lf.lfWeight = FW_BOLD;
3272 gui.wide_bold_font = get_font_handle(&lf);
3273 if (!lf.lfItalic)
3274 {
3275 lf.lfItalic = TRUE;
3276 gui.wide_boldital_font = get_font_handle(&lf);
3277 }
3278 }
3279 }
3280}
3281#endif
3282
3283/*
3284 * Initialise vim to use the font with the given name.
3285 * Return FAIL if the font could not be loaded, OK otherwise.
3286 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003287 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003288gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003289{
3290 LOGFONT lf;
3291 GuiFont font = NOFONT;
3292 char_u *p;
3293
3294 /* Load the font */
3295 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3296 font = get_font_handle(&lf);
3297 if (font == NOFONT)
3298 return FAIL;
3299
3300 if (font_name == NULL)
3301 font_name = (char_u *)lf.lfFaceName;
3302#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3303 norm_logfont = lf;
3304 sub_logfont = lf;
3305#endif
3306#ifdef FEAT_MBYTE_IME
3307 update_im_font();
3308#endif
3309 gui_mch_free_font(gui.norm_font);
3310 gui.norm_font = font;
3311 current_font_height = lf.lfHeight;
3312 GetFontSize(font);
3313
3314 p = logfont2name(lf);
3315 if (p != NULL)
3316 {
3317 hl_set_font_name(p);
3318
3319 /* When setting 'guifont' to "*" replace it with the actual font name.
3320 * */
3321 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3322 {
3323 vim_free(p_guifont);
3324 p_guifont = p;
3325 }
3326 else
3327 vim_free(p);
3328 }
3329
3330 gui_mch_free_font(gui.ital_font);
3331 gui.ital_font = NOFONT;
3332 gui_mch_free_font(gui.bold_font);
3333 gui.bold_font = NOFONT;
3334 gui_mch_free_font(gui.boldital_font);
3335 gui.boldital_font = NOFONT;
3336
3337 if (!lf.lfItalic)
3338 {
3339 lf.lfItalic = TRUE;
3340 gui.ital_font = get_font_handle(&lf);
3341 lf.lfItalic = FALSE;
3342 }
3343 if (lf.lfWeight < FW_BOLD)
3344 {
3345 lf.lfWeight = FW_BOLD;
3346 gui.bold_font = get_font_handle(&lf);
3347 if (!lf.lfItalic)
3348 {
3349 lf.lfItalic = TRUE;
3350 gui.boldital_font = get_font_handle(&lf);
3351 }
3352 }
3353
3354 return OK;
3355}
3356
3357#ifndef WPF_RESTORETOMAXIMIZED
3358# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3359#endif
3360
3361/*
3362 * Return TRUE if the GUI window is maximized, filling the whole screen.
3363 */
3364 int
3365gui_mch_maximized(void)
3366{
3367 WINDOWPLACEMENT wp;
3368
3369 wp.length = sizeof(WINDOWPLACEMENT);
3370 if (GetWindowPlacement(s_hwnd, &wp))
3371 return wp.showCmd == SW_SHOWMAXIMIZED
3372 || (wp.showCmd == SW_SHOWMINIMIZED
3373 && wp.flags == WPF_RESTORETOMAXIMIZED);
3374
3375 return 0;
3376}
3377
3378/*
3379 * Called when the font changed while the window is maximized. Compute the
3380 * new Rows and Columns. This is like resizing the window.
3381 */
3382 void
3383gui_mch_newfont(void)
3384{
3385 RECT rect;
3386
3387 GetWindowRect(s_hwnd, &rect);
3388 if (win_socket_id == 0)
3389 {
3390 gui_resize_shell(rect.right - rect.left
3391 - (GetSystemMetrics(SM_CXFRAME) +
3392 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3393 rect.bottom - rect.top
3394 - (GetSystemMetrics(SM_CYFRAME) +
3395 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3396 - GetSystemMetrics(SM_CYCAPTION)
3397#ifdef FEAT_MENU
3398 - gui_mswin_get_menu_height(FALSE)
3399#endif
3400 );
3401 }
3402 else
3403 {
3404 /* Inside another window, don't use the frame and border. */
3405 gui_resize_shell(rect.right - rect.left,
3406 rect.bottom - rect.top
3407#ifdef FEAT_MENU
3408 - gui_mswin_get_menu_height(FALSE)
3409#endif
3410 );
3411 }
3412}
3413
3414/*
3415 * Set the window title
3416 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003417 void
3418gui_mch_settitle(
3419 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003420 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003421{
3422 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3423}
3424
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003425#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003426/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3427 * misc2.c! */
3428static LPCSTR mshape_idcs[] =
3429{
3430 IDC_ARROW, /* arrow */
3431 MAKEINTRESOURCE(0), /* blank */
3432 IDC_IBEAM, /* beam */
3433 IDC_SIZENS, /* updown */
3434 IDC_SIZENS, /* udsizing */
3435 IDC_SIZEWE, /* leftright */
3436 IDC_SIZEWE, /* lrsizing */
3437 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003438 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003439 IDC_ARROW, /* crosshair */
3440 IDC_ARROW, /* hand1 */
3441 IDC_ARROW, /* hand2 */
3442 IDC_ARROW, /* pencil */
3443 IDC_ARROW, /* question */
3444 IDC_ARROW, /* right-arrow */
3445 IDC_UPARROW, /* up-arrow */
3446 IDC_ARROW /* last one */
3447};
3448
3449 void
3450mch_set_mouse_shape(int shape)
3451{
3452 LPCSTR idc;
3453
3454 if (shape == MSHAPE_HIDE)
3455 ShowCursor(FALSE);
3456 else
3457 {
3458 if (shape >= MSHAPE_NUMBERED)
3459 idc = IDC_ARROW;
3460 else
3461 idc = mshape_idcs[shape];
3462#ifdef SetClassLongPtr
3463 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3464#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003465 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003466#endif
3467 if (!p_mh)
3468 {
3469 POINT mp;
3470
3471 /* Set the position to make it redrawn with the new shape. */
3472 (void)GetCursorPos((LPPOINT)&mp);
3473 (void)SetCursorPos(mp.x, mp.y);
3474 ShowCursor(TRUE);
3475 }
3476 }
3477}
3478#endif
3479
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003480#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003481/*
3482 * The file browser exists in two versions: with "W" uses wide characters,
3483 * without "W" the current codepage. When FEAT_MBYTE is defined and on
3484 * Windows NT/2000/XP the "W" functions are used.
3485 */
3486
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003487# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003488/*
3489 * Wide version of convert_filter().
3490 */
3491 static WCHAR *
3492convert_filterW(char_u *s)
3493{
3494 char_u *tmp;
3495 int len;
3496 WCHAR *res;
3497
3498 tmp = convert_filter(s);
3499 if (tmp == NULL)
3500 return NULL;
3501 len = (int)STRLEN(s) + 3;
3502 res = enc_to_utf16(tmp, &len);
3503 vim_free(tmp);
3504 return res;
3505}
3506
3507/*
3508 * Wide version of gui_mch_browse(). Keep in sync!
3509 */
3510 static char_u *
3511gui_mch_browseW(
3512 int saving,
3513 char_u *title,
3514 char_u *dflt,
3515 char_u *ext,
3516 char_u *initdir,
3517 char_u *filter)
3518{
3519 /* We always use the wide function. This means enc_to_utf16() must work,
3520 * otherwise it fails miserably! */
3521 OPENFILENAMEW fileStruct;
3522 WCHAR fileBuf[MAXPATHL];
3523 WCHAR *wp;
3524 int i;
3525 WCHAR *titlep = NULL;
3526 WCHAR *extp = NULL;
3527 WCHAR *initdirp = NULL;
3528 WCHAR *filterp;
3529 char_u *p;
3530
3531 if (dflt == NULL)
3532 fileBuf[0] = NUL;
3533 else
3534 {
3535 wp = enc_to_utf16(dflt, NULL);
3536 if (wp == NULL)
3537 fileBuf[0] = NUL;
3538 else
3539 {
3540 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3541 fileBuf[i] = wp[i];
3542 fileBuf[i] = NUL;
3543 vim_free(wp);
3544 }
3545 }
3546
3547 /* Convert the filter to Windows format. */
3548 filterp = convert_filterW(filter);
3549
3550 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003551# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003552 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003553 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003554# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003555 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003556# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003557
3558 if (title != NULL)
3559 titlep = enc_to_utf16(title, NULL);
3560 fileStruct.lpstrTitle = titlep;
3561
3562 if (ext != NULL)
3563 extp = enc_to_utf16(ext, NULL);
3564 fileStruct.lpstrDefExt = extp;
3565
3566 fileStruct.lpstrFile = fileBuf;
3567 fileStruct.nMaxFile = MAXPATHL;
3568 fileStruct.lpstrFilter = filterp;
3569 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3570 /* has an initial dir been specified? */
3571 if (initdir != NULL && *initdir != NUL)
3572 {
3573 /* Must have backslashes here, no matter what 'shellslash' says */
3574 initdirp = enc_to_utf16(initdir, NULL);
3575 if (initdirp != NULL)
3576 {
3577 for (wp = initdirp; *wp != NUL; ++wp)
3578 if (*wp == '/')
3579 *wp = '\\';
3580 }
3581 fileStruct.lpstrInitialDir = initdirp;
3582 }
3583
3584 /*
3585 * TODO: Allow selection of multiple files. Needs another arg to this
3586 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3587 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3588 * files that don't exist yet, so I haven't put it in. What about
3589 * OFN_PATHMUSTEXIST?
3590 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3591 */
3592 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003593# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003594 if (curbuf->b_p_bin)
3595 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003596# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003597 if (saving)
3598 {
3599 if (!GetSaveFileNameW(&fileStruct))
3600 return NULL;
3601 }
3602 else
3603 {
3604 if (!GetOpenFileNameW(&fileStruct))
3605 return NULL;
3606 }
3607
3608 vim_free(filterp);
3609 vim_free(initdirp);
3610 vim_free(titlep);
3611 vim_free(extp);
3612
3613 /* Convert from UCS2 to 'encoding'. */
3614 p = utf16_to_enc(fileBuf, NULL);
3615 if (p != NULL)
3616 /* when out of memory we get garbage for non-ASCII chars */
3617 STRCPY(fileBuf, p);
3618 vim_free(p);
3619
3620 /* Give focus back to main window (when using MDI). */
3621 SetFocus(s_hwnd);
3622
3623 /* Shorten the file name if possible */
3624 return vim_strsave(shorten_fname1((char_u *)fileBuf));
3625}
3626# endif /* FEAT_MBYTE */
3627
3628
3629/*
3630 * Convert the string s to the proper format for a filter string by replacing
3631 * the \t and \n delimiters with \0.
3632 * Returns the converted string in allocated memory.
3633 *
3634 * Keep in sync with convert_filterW() above!
3635 */
3636 static char_u *
3637convert_filter(char_u *s)
3638{
3639 char_u *res;
3640 unsigned s_len = (unsigned)STRLEN(s);
3641 unsigned i;
3642
3643 res = alloc(s_len + 3);
3644 if (res != NULL)
3645 {
3646 for (i = 0; i < s_len; ++i)
3647 if (s[i] == '\t' || s[i] == '\n')
3648 res[i] = '\0';
3649 else
3650 res[i] = s[i];
3651 res[s_len] = NUL;
3652 /* Add two extra NULs to make sure it's properly terminated. */
3653 res[s_len + 1] = NUL;
3654 res[s_len + 2] = NUL;
3655 }
3656 return res;
3657}
3658
3659/*
3660 * Select a directory.
3661 */
3662 char_u *
3663gui_mch_browsedir(char_u *title, char_u *initdir)
3664{
3665 /* We fake this: Use a filter that doesn't select anything and a default
3666 * file name that won't be used. */
3667 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3668 initdir, (char_u *)_("Directory\t*.nothing\n"));
3669}
3670
3671/*
3672 * Pop open a file browser and return the file selected, in allocated memory,
3673 * or NULL if Cancel is hit.
3674 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3675 * title - Title message for the file browser dialog.
3676 * dflt - Default name of file.
3677 * ext - Default extension to be added to files without extensions.
3678 * initdir - directory in which to open the browser (NULL = current dir)
3679 * filter - Filter for matched files to choose from.
3680 *
3681 * Keep in sync with gui_mch_browseW() above!
3682 */
3683 char_u *
3684gui_mch_browse(
3685 int saving,
3686 char_u *title,
3687 char_u *dflt,
3688 char_u *ext,
3689 char_u *initdir,
3690 char_u *filter)
3691{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003692# ifdef FEAT_MBYTE
3693 return gui_mch_browseW(saving, title, dflt, ext, initdir, filter);
3694# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003695 OPENFILENAME fileStruct;
3696 char_u fileBuf[MAXPATHL];
3697 char_u *initdirp = NULL;
3698 char_u *filterp;
3699 char_u *p;
3700
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003701 if (dflt == NULL)
3702 fileBuf[0] = NUL;
3703 else
3704 vim_strncpy(fileBuf, dflt, MAXPATHL - 1);
3705
3706 /* Convert the filter to Windows format. */
3707 filterp = convert_filter(filter);
3708
3709 vim_memset(&fileStruct, 0, sizeof(OPENFILENAME));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003710# ifdef OPENFILENAME_SIZE_VERSION_400
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003711 /* be compatible with Windows NT 4.0 */
3712 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003713# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003714 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003715# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003716
3717 fileStruct.lpstrTitle = (LPSTR)title;
3718 fileStruct.lpstrDefExt = (LPSTR)ext;
3719
3720 fileStruct.lpstrFile = (LPSTR)fileBuf;
3721 fileStruct.nMaxFile = MAXPATHL;
3722 fileStruct.lpstrFilter = (LPSTR)filterp;
3723 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3724 /* has an initial dir been specified? */
3725 if (initdir != NULL && *initdir != NUL)
3726 {
3727 /* Must have backslashes here, no matter what 'shellslash' says */
3728 initdirp = vim_strsave(initdir);
3729 if (initdirp != NULL)
3730 for (p = initdirp; *p != NUL; ++p)
3731 if (*p == '/')
3732 *p = '\\';
3733 fileStruct.lpstrInitialDir = (LPSTR)initdirp;
3734 }
3735
3736 /*
3737 * TODO: Allow selection of multiple files. Needs another arg to this
3738 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3739 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3740 * files that don't exist yet, so I haven't put it in. What about
3741 * OFN_PATHMUSTEXIST?
3742 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3743 */
3744 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003745# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003746 if (curbuf->b_p_bin)
3747 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003748# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003749 if (saving)
3750 {
3751 if (!GetSaveFileName(&fileStruct))
3752 return NULL;
3753 }
3754 else
3755 {
3756 if (!GetOpenFileName(&fileStruct))
3757 return NULL;
3758 }
3759
3760 vim_free(filterp);
3761 vim_free(initdirp);
3762
3763 /* Give focus back to main window (when using MDI). */
3764 SetFocus(s_hwnd);
3765
3766 /* Shorten the file name if possible */
3767 return vim_strsave(shorten_fname1((char_u *)fileBuf));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003768# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003769}
3770#endif /* FEAT_BROWSE */
3771
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003772 static void
3773_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003774 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003775 HDROP hDrop)
3776{
3777#ifdef FEAT_WINDOWS
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003778# define BUFPATHLEN _MAX_PATH
3779# define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003780# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003781 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003782# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003783 char szFile[BUFPATHLEN];
3784 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3785 UINT i;
3786 char_u **fnames;
3787 POINT pt;
3788 int_u modifiers = 0;
3789
3790 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3791
3792 /* Obtain dropped position */
3793 DragQueryPoint(hDrop, &pt);
3794 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3795
3796 reset_VIsual();
3797
3798 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3799
3800 if (fnames != NULL)
3801 for (i = 0; i < cFiles; ++i)
3802 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003803# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003804 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3805 fnames[i] = utf16_to_enc(wszFile, NULL);
3806 else
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003807# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003808 {
3809 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3810 fnames[i] = vim_strsave((char_u *)szFile);
3811 }
3812 }
3813
3814 DragFinish(hDrop);
3815
3816 if (fnames != NULL)
3817 {
3818 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3819 modifiers |= MOUSE_SHIFT;
3820 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3821 modifiers |= MOUSE_CTRL;
3822 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3823 modifiers |= MOUSE_ALT;
3824
3825 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3826
3827 s_need_activate = TRUE;
3828 }
3829#endif
3830}
3831
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003832 static int
3833_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003834 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003835 HWND hwndCtl,
3836 UINT code,
3837 int pos)
3838{
3839 static UINT prev_code = 0; /* code of previous call */
3840 scrollbar_T *sb, *sb_info;
3841 long val;
3842 int dragging = FALSE;
3843 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003844 SCROLLINFO si;
3845
3846 si.cbSize = sizeof(si);
3847 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003848
3849 sb = gui_mswin_find_scrollbar(hwndCtl);
3850 if (sb == NULL)
3851 return 0;
3852
3853 if (sb->wp != NULL) /* Left or right scrollbar */
3854 {
3855 /*
3856 * Careful: need to get scrollbar info out of first (left) scrollbar
3857 * for window, but keep real scrollbar too because we must pass it to
3858 * gui_drag_scrollbar().
3859 */
3860 sb_info = &sb->wp->w_scrollbars[0];
3861 }
3862 else /* Bottom scrollbar */
3863 sb_info = sb;
3864 val = sb_info->value;
3865
3866 switch (code)
3867 {
3868 case SB_THUMBTRACK:
3869 val = pos;
3870 dragging = TRUE;
3871 if (sb->scroll_shift > 0)
3872 val <<= sb->scroll_shift;
3873 break;
3874 case SB_LINEDOWN:
3875 val++;
3876 break;
3877 case SB_LINEUP:
3878 val--;
3879 break;
3880 case SB_PAGEDOWN:
3881 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3882 break;
3883 case SB_PAGEUP:
3884 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3885 break;
3886 case SB_TOP:
3887 val = 0;
3888 break;
3889 case SB_BOTTOM:
3890 val = sb_info->max;
3891 break;
3892 case SB_ENDSCROLL:
3893 if (prev_code == SB_THUMBTRACK)
3894 {
3895 /*
3896 * "pos" only gives us 16-bit data. In case of large file,
3897 * use GetScrollPos() which returns 32-bit. Unfortunately it
3898 * is not valid while the scrollbar is being dragged.
3899 */
3900 val = GetScrollPos(hwndCtl, SB_CTL);
3901 if (sb->scroll_shift > 0)
3902 val <<= sb->scroll_shift;
3903 }
3904 break;
3905
3906 default:
3907 /* TRACE("Unknown scrollbar event %d\n", code); */
3908 return 0;
3909 }
3910 prev_code = code;
3911
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003912 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3913 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003914
3915 /*
3916 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3917 */
3918 if (sb->wp != NULL)
3919 {
3920 scrollbar_T *sba = sb->wp->w_scrollbars;
3921 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3922
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003923 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003924 }
3925
3926 /* Don't let us be interrupted here by another message. */
3927 s_busy_processing = TRUE;
3928
3929 /* When "allow_scrollbar" is FALSE still need to remember the new
3930 * position, but don't actually scroll by setting "dont_scroll". */
3931 dont_scroll = !allow_scrollbar;
3932
3933 gui_drag_scrollbar(sb, val, dragging);
3934
3935 s_busy_processing = FALSE;
3936 dont_scroll = dont_scroll_save;
3937
3938 return 0;
3939}
3940
3941
3942/*
3943 * Get command line arguments.
3944 * Use "prog" as the name of the program and "cmdline" as the arguments.
3945 * Copy the arguments to allocated memory.
3946 * Return the number of arguments (including program name).
3947 * Return pointers to the arguments in "argvp". Memory is allocated with
3948 * malloc(), use free() instead of vim_free().
3949 * Return pointer to buffer in "tofree".
3950 * Returns zero when out of memory.
3951 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003952 int
3953get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
3954{
3955 int i;
3956 char *p;
3957 char *progp;
3958 char *pnew = NULL;
3959 char *newcmdline;
3960 int inquote;
3961 int argc;
3962 char **argv = NULL;
3963 int round;
3964
3965 *tofree = NULL;
3966
3967#ifdef FEAT_MBYTE
3968 /* Try using the Unicode version first, it takes care of conversion when
3969 * 'encoding' is changed. */
3970 argc = get_cmd_argsW(&argv);
3971 if (argc != 0)
3972 goto done;
3973#endif
3974
3975 /* Handle the program name. Remove the ".exe" extension, and find the 1st
3976 * non-space. */
3977 p = strrchr(prog, '.');
3978 if (p != NULL)
3979 *p = NUL;
3980 for (progp = prog; *progp == ' '; ++progp)
3981 ;
3982
3983 /* The command line is copied to allocated memory, so that we can change
3984 * it. Add the size of the string, the separating NUL and a terminating
3985 * NUL. */
3986 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
3987 if (newcmdline == NULL)
3988 return 0;
3989
3990 /*
3991 * First round: count the number of arguments ("pnew" == NULL).
3992 * Second round: produce the arguments.
3993 */
3994 for (round = 1; round <= 2; ++round)
3995 {
3996 /* First argument is the program name. */
3997 if (pnew != NULL)
3998 {
3999 argv[0] = pnew;
4000 strcpy(pnew, progp);
4001 pnew += strlen(pnew);
4002 *pnew++ = NUL;
4003 }
4004
4005 /*
4006 * Isolate each argument and put it in argv[].
4007 */
4008 p = cmdline;
4009 argc = 1;
4010 while (*p != NUL)
4011 {
4012 inquote = FALSE;
4013 if (pnew != NULL)
4014 argv[argc] = pnew;
4015 ++argc;
4016 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
4017 {
4018 /* Backslashes are only special when followed by a double
4019 * quote. */
4020 i = (int)strspn(p, "\\");
4021 if (p[i] == '"')
4022 {
4023 /* Halve the number of backslashes. */
4024 if (i > 1 && pnew != NULL)
4025 {
4026 vim_memset(pnew, '\\', i / 2);
4027 pnew += i / 2;
4028 }
4029
4030 /* Even nr of backslashes toggles quoting, uneven copies
4031 * the double quote. */
4032 if ((i & 1) == 0)
4033 inquote = !inquote;
4034 else if (pnew != NULL)
4035 *pnew++ = '"';
4036 p += i + 1;
4037 }
4038 else if (i > 0)
4039 {
4040 /* Copy span of backslashes unmodified. */
4041 if (pnew != NULL)
4042 {
4043 vim_memset(pnew, '\\', i);
4044 pnew += i;
4045 }
4046 p += i;
4047 }
4048 else
4049 {
4050 if (pnew != NULL)
4051 *pnew++ = *p;
4052#ifdef FEAT_MBYTE
4053 /* Can't use mb_* functions, because 'encoding' is not
4054 * initialized yet here. */
4055 if (IsDBCSLeadByte(*p))
4056 {
4057 ++p;
4058 if (pnew != NULL)
4059 *pnew++ = *p;
4060 }
4061#endif
4062 ++p;
4063 }
4064 }
4065
4066 if (pnew != NULL)
4067 *pnew++ = NUL;
4068 while (*p == ' ' || *p == '\t')
4069 ++p; /* advance until a non-space */
4070 }
4071
4072 if (round == 1)
4073 {
4074 argv = (char **)malloc((argc + 1) * sizeof(char *));
4075 if (argv == NULL )
4076 {
4077 free(newcmdline);
4078 return 0; /* malloc error */
4079 }
4080 pnew = newcmdline;
4081 *tofree = newcmdline;
4082 }
4083 }
4084
4085#ifdef FEAT_MBYTE
4086done:
4087#endif
4088 argv[argc] = NULL; /* NULL-terminated list */
4089 *argvp = argv;
4090 return argc;
4091}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092
4093#ifdef FEAT_XPM_W32
4094# include "xpm_w32.h"
4095#endif
4096
4097#ifdef PROTO
4098# define WINAPI
4099#endif
4100
4101#ifdef __MINGW32__
4102/*
4103 * Add a lot of missing defines.
4104 * They are not always missing, we need the #ifndef's.
4105 */
4106# ifndef _cdecl
4107# define _cdecl
4108# endif
4109# ifndef IsMinimized
4110# define IsMinimized(hwnd) IsIconic(hwnd)
4111# endif
4112# ifndef IsMaximized
4113# define IsMaximized(hwnd) IsZoomed(hwnd)
4114# endif
4115# ifndef SelectFont
4116# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
4117# endif
4118# ifndef GetStockBrush
4119# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
4120# endif
4121# ifndef DeleteBrush
4122# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
4123# endif
4124
4125# ifndef HANDLE_WM_RBUTTONDBLCLK
4126# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4127 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4128# endif
4129# ifndef HANDLE_WM_MBUTTONUP
4130# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
4131 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4132# endif
4133# ifndef HANDLE_WM_MBUTTONDBLCLK
4134# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4135 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4136# endif
4137# ifndef HANDLE_WM_LBUTTONDBLCLK
4138# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4139 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4140# endif
4141# ifndef HANDLE_WM_RBUTTONDOWN
4142# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
4143 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4144# endif
4145# ifndef HANDLE_WM_MOUSEMOVE
4146# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
4147 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4148# endif
4149# ifndef HANDLE_WM_RBUTTONUP
4150# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
4151 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4152# endif
4153# ifndef HANDLE_WM_MBUTTONDOWN
4154# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
4155 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4156# endif
4157# ifndef HANDLE_WM_LBUTTONUP
4158# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
4159 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4160# endif
4161# ifndef HANDLE_WM_LBUTTONDOWN
4162# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
4163 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4164# endif
4165# ifndef HANDLE_WM_SYSCHAR
4166# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
4167 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4168# endif
4169# ifndef HANDLE_WM_ACTIVATEAPP
4170# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
4171 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
4172# endif
4173# ifndef HANDLE_WM_WINDOWPOSCHANGING
4174# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
4175 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
4176# endif
4177# ifndef HANDLE_WM_VSCROLL
4178# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
4179 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4180# endif
4181# ifndef HANDLE_WM_SETFOCUS
4182# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
4183 ((fn)((hwnd), (HWND)(wParam)), 0L)
4184# endif
4185# ifndef HANDLE_WM_KILLFOCUS
4186# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
4187 ((fn)((hwnd), (HWND)(wParam)), 0L)
4188# endif
4189# ifndef HANDLE_WM_HSCROLL
4190# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
4191 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4192# endif
4193# ifndef HANDLE_WM_DROPFILES
4194# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
4195 ((fn)((hwnd), (HDROP)(wParam)), 0L)
4196# endif
4197# ifndef HANDLE_WM_CHAR
4198# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
4199 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4200# endif
4201# ifndef HANDLE_WM_SYSDEADCHAR
4202# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
4203 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4204# endif
4205# ifndef HANDLE_WM_DEADCHAR
4206# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4207 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4208# endif
4209#endif /* __MINGW32__ */
4210
4211
4212/* Some parameters for tearoff menus. All in pixels. */
4213#define TEAROFF_PADDING_X 2
4214#define TEAROFF_BUTTON_PAD_X 8
4215#define TEAROFF_MIN_WIDTH 200
4216#define TEAROFF_SUBMENU_LABEL ">>"
4217#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4218
4219
4220/* For the Intellimouse: */
4221#ifndef WM_MOUSEWHEEL
4222#define WM_MOUSEWHEEL 0x20a
4223#endif
4224
4225
4226#ifdef FEAT_BEVAL
4227# define ID_BEVAL_TOOLTIP 200
4228# define BEVAL_TEXT_LEN MAXPATHL
4229
Bram Moolenaar167632f2010-05-26 21:42:54 +02004230#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004231/* Work around old versions of basetsd.h which wrongly declares
4232 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02004233# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00004234# define UINT_PTR UINT
4235#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004236
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004237static void make_tooltip(BalloonEval *beval, char *text, POINT pt);
4238static void delete_tooltip(BalloonEval *beval);
4239static VOID CALLBACK BevalTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004240
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004242static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004244
Bram Moolenaar82881492012-11-20 16:53:39 +01004245
4246/* cproto fails on missing include files */
4247#ifndef PROTO
4248
Bram Moolenaar45360022005-07-21 21:08:21 +00004249/*
4250 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004251 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004252 */
Bram Moolenaar82881492012-11-20 16:53:39 +01004253# include <pshpack1.h>
4254
4255#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004256
4257typedef struct _DllVersionInfo
4258{
4259 DWORD cbSize;
4260 DWORD dwMajorVersion;
4261 DWORD dwMinorVersion;
4262 DWORD dwBuildNumber;
4263 DWORD dwPlatformID;
4264} DLLVERSIONINFO;
4265
Bram Moolenaar82881492012-11-20 16:53:39 +01004266#ifndef PROTO
4267# include <poppack.h>
4268#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004269
Bram Moolenaar45360022005-07-21 21:08:21 +00004270typedef struct tagTOOLINFOA_NEW
4271{
4272 UINT cbSize;
4273 UINT uFlags;
4274 HWND hwnd;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004275 UINT_PTR uId;
Bram Moolenaar45360022005-07-21 21:08:21 +00004276 RECT rect;
4277 HINSTANCE hinst;
4278 LPSTR lpszText;
4279 LPARAM lParam;
4280} TOOLINFO_NEW;
4281
4282typedef struct tagNMTTDISPINFO_NEW
4283{
4284 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004285 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004286 char szText[80];
4287 HINSTANCE hinst;
4288 UINT uFlags;
4289 LPARAM lParam;
4290} NMTTDISPINFO_NEW;
4291
Bram Moolenaar45360022005-07-21 21:08:21 +00004292typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4293#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004294# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295#endif
4296
Bram Moolenaar45360022005-07-21 21:08:21 +00004297#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004298# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004299#endif
4300
4301#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004302# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004303#endif
4304
4305#endif /* defined(FEAT_BEVAL) */
4306
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004307#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4308/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4309 * it here if LPNMTTDISPINFO isn't defined.
4310 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4311 * _MSC_VER. */
4312# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4313typedef struct tagNMTTDISPINFOA {
4314 NMHDR hdr;
4315 LPSTR lpszText;
4316 char szText[80];
4317 HINSTANCE hinst;
4318 UINT uFlags;
4319 LPARAM lParam;
4320} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4321# define LPNMTTDISPINFO LPNMTTDISPINFOA
4322
4323# ifdef FEAT_MBYTE
4324typedef struct tagNMTTDISPINFOW {
4325 NMHDR hdr;
4326 LPWSTR lpszText;
4327 WCHAR szText[80];
4328 HINSTANCE hinst;
4329 UINT uFlags;
4330 LPARAM lParam;
4331} NMTTDISPINFOW, *LPNMTTDISPINFOW;
4332# endif
4333# endif
4334#endif
4335
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004336#ifndef TTN_GETDISPINFOW
4337# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4338#endif
4339
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340/* Local variables: */
4341
4342#ifdef FEAT_MENU
4343static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345
4346/*
4347 * Use the system font for dialogs and tear-off menus. Remove this line to
4348 * use DLG_FONT_NAME.
4349 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004350#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351
4352#define VIM_NAME "vim"
4353#define VIM_CLASS "Vim"
4354#define VIM_CLASSW L"Vim"
4355
4356/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4357 * thus there should be room for every dialog. For tearoffs it's made bigger
4358 * when needed. */
4359#define DLG_ALLOC_SIZE 16 * 1024
4360
4361/*
4362 * stuff for dialogs, menus, tearoffs etc.
4363 */
4364static LRESULT APIENTRY dialog_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004365#ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366static LRESULT APIENTRY tearoff_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368static PWORD
4369add_dialog_element(
4370 PWORD p,
4371 DWORD lStyle,
4372 WORD x,
4373 WORD y,
4374 WORD w,
4375 WORD h,
4376 WORD Id,
4377 WORD clss,
4378 const char *caption);
4379static LPWORD lpwAlign(LPWORD);
4380static int nCopyAnsiToWideChar(LPWORD, LPSTR);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004381#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384static void get_dialog_font_metrics(void);
4385
4386static int dialog_default_button = -1;
4387
4388/* Intellimouse support */
4389static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
4391static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4392#ifdef FEAT_TOOLBAR
4393static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004394static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395static int get_toolbar_bitmap(vimmenu_T *menu);
4396#endif
4397
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004398#ifdef FEAT_GUI_TABLINE
4399static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004400static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004401#endif
4402
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403#ifdef FEAT_MBYTE_IME
4404static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4405static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4406#endif
4407#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4408# ifdef NOIME
4409typedef struct tagCOMPOSITIONFORM {
4410 DWORD dwStyle;
4411 POINT ptCurrentPos;
4412 RECT rcArea;
4413} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4414typedef HANDLE HIMC;
4415# endif
4416
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004417static HINSTANCE hLibImm = NULL;
4418static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4419static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4420static HIMC (WINAPI *pImmGetContext)(HWND);
4421static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4422static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4423static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4424static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
4425static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
4426static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
4427static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4428static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004429static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430static void dyn_imm_load(void);
4431#else
4432# define pImmGetCompositionStringA ImmGetCompositionStringA
4433# define pImmGetCompositionStringW ImmGetCompositionStringW
4434# define pImmGetContext ImmGetContext
4435# define pImmAssociateContext ImmAssociateContext
4436# define pImmReleaseContext ImmReleaseContext
4437# define pImmGetOpenStatus ImmGetOpenStatus
4438# define pImmSetOpenStatus ImmSetOpenStatus
4439# define pImmGetCompositionFont ImmGetCompositionFontA
4440# define pImmSetCompositionFont ImmSetCompositionFontA
4441# define pImmSetCompositionWindow ImmSetCompositionWindow
4442# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004443# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444#endif
4445
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446#ifdef FEAT_MENU
4447/*
4448 * Figure out how high the menu bar is at the moment.
4449 */
4450 static int
4451gui_mswin_get_menu_height(
4452 int fix_window) /* If TRUE, resize window if menu height changed */
4453{
4454 static int old_menu_height = -1;
4455
4456 RECT rc1, rc2;
4457 int num;
4458 int menu_height;
4459
4460 if (gui.menu_is_active)
4461 num = GetMenuItemCount(s_menuBar);
4462 else
4463 num = 0;
4464
4465 if (num == 0)
4466 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004467 else if (IsMinimized(s_hwnd))
4468 {
4469 /* The height of the menu cannot be determined while the window is
4470 * minimized. Take the previous height if the menu is changed in that
4471 * state, to avoid that Vim's vertical window size accidentally
4472 * increases due to the unaccounted-for menu height. */
4473 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 else
4476 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004477 /*
4478 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4479 * seem to have been set yet, so menu wraps in default window
4480 * width which is very narrow. Instead just return height of a
4481 * single menu item. Will still be wrong when the menu really
4482 * should wrap over more than one line.
4483 */
4484 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4485 if (gui.starting)
4486 menu_height = rc1.bottom - rc1.top + 1;
4487 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004489 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4490 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 }
4492 }
4493
4494 if (fix_window && menu_height != old_menu_height)
4495 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004496 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 }
Bram Moolenaar71371b12015-03-24 17:57:12 +01004498 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499
4500 return menu_height;
4501}
4502#endif /*FEAT_MENU*/
4503
4504
4505/*
4506 * Setup for the Intellimouse
4507 */
4508 static void
4509init_mouse_wheel(void)
4510{
4511
4512#ifndef SPI_GETWHEELSCROLLLINES
4513# define SPI_GETWHEELSCROLLLINES 104
4514#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004515#ifndef SPI_SETWHEELSCROLLLINES
4516# define SPI_SETWHEELSCROLLLINES 105
4517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518
4519#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4520#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4521#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4522#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4523
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 mouse_scroll_lines = 3; /* reasonable default */
4525
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004526 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4527 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4528 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529}
4530
4531
4532/* Intellimouse wheel handler */
4533 static void
4534_OnMouseWheel(
4535 HWND hwnd,
4536 short zDelta)
4537{
4538/* Treat a mouse wheel event as if it were a scroll request */
4539 int i;
4540 int size;
4541 HWND hwndCtl;
4542
4543 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4544 {
4545 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4546 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4547 }
4548 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4549 {
4550 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4551 size = curwin->w_scrollbars[SBAR_LEFT].size;
4552 }
4553 else
4554 return;
4555
4556 size = curwin->w_height;
4557 if (mouse_scroll_lines == 0)
4558 init_mouse_wheel();
4559
4560 if (mouse_scroll_lines > 0
4561 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4562 {
4563 for (i = mouse_scroll_lines; i > 0; --i)
4564 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4565 }
4566 else
4567 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
4568}
4569
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004570#ifdef USE_SYSMENU_FONT
4571/*
4572 * Get Menu Font.
4573 * Return OK or FAIL.
4574 */
4575 static int
4576gui_w32_get_menu_font(LOGFONT *lf)
4577{
4578 NONCLIENTMETRICS nm;
4579
4580 nm.cbSize = sizeof(NONCLIENTMETRICS);
4581 if (!SystemParametersInfo(
4582 SPI_GETNONCLIENTMETRICS,
4583 sizeof(NONCLIENTMETRICS),
4584 &nm,
4585 0))
4586 return FAIL;
4587 *lf = nm.lfMenuFont;
4588 return OK;
4589}
4590#endif
4591
4592
4593#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4594/*
4595 * Set the GUI tabline font to the system menu font
4596 */
4597 static void
4598set_tabline_font(void)
4599{
4600 LOGFONT lfSysmenu;
4601 HFONT font;
4602 HWND hwnd;
4603 HDC hdc;
4604 HFONT hfntOld;
4605 TEXTMETRIC tm;
4606
4607 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4608 return;
4609
4610 font = CreateFontIndirect(&lfSysmenu);
4611
4612 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4613
4614 /*
4615 * Compute the height of the font used for the tab text
4616 */
4617 hwnd = GetDesktopWindow();
4618 hdc = GetWindowDC(hwnd);
4619 hfntOld = SelectFont(hdc, font);
4620
4621 GetTextMetrics(hdc, &tm);
4622
4623 SelectFont(hdc, hfntOld);
4624 ReleaseDC(hwnd, hdc);
4625
4626 /*
4627 * The space used by the tab border and the space between the tab label
4628 * and the tab border is included as 7.
4629 */
4630 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4631}
4632#endif
4633
Bram Moolenaar520470a2005-06-16 21:59:56 +00004634/*
4635 * Invoked when a setting was changed.
4636 */
4637 static LRESULT CALLBACK
4638_OnSettingChange(UINT n)
4639{
4640 if (n == SPI_SETWHEELSCROLLLINES)
4641 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4642 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004643#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4644 if (n == SPI_SETNONCLIENTMETRICS)
4645 set_tabline_font();
4646#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004647 return 0;
4648}
4649
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650#ifdef FEAT_NETBEANS_INTG
4651 static void
4652_OnWindowPosChanged(
4653 HWND hwnd,
4654 const LPWINDOWPOS lpwpos)
4655{
4656 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004657 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658
4659 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4660 || lpwpos->cx != cx || lpwpos->cy != cy))
4661 {
4662 x = lpwpos->x;
4663 y = lpwpos->y;
4664 cx = lpwpos->cx;
4665 cy = lpwpos->cy;
4666 netbeans_frame_moved(x, y);
4667 }
4668 /* Allow to send WM_SIZE and WM_MOVE */
4669 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4670}
4671#endif
4672
4673 static int
4674_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 UINT fwSide,
4676 LPRECT lprc)
4677{
4678 int w, h;
4679 int valid_w, valid_h;
4680 int w_offset, h_offset;
4681
4682 w = lprc->right - lprc->left;
4683 h = lprc->bottom - lprc->top;
4684 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4685 w_offset = w - valid_w;
4686 h_offset = h - valid_h;
4687
4688 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4689 || fwSide == WMSZ_BOTTOMLEFT)
4690 lprc->left += w_offset;
4691 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4692 || fwSide == WMSZ_BOTTOMRIGHT)
4693 lprc->right -= w_offset;
4694
4695 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4696 || fwSide == WMSZ_TOPRIGHT)
4697 lprc->top += h_offset;
4698 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4699 || fwSide == WMSZ_BOTTOMRIGHT)
4700 lprc->bottom -= h_offset;
4701 return TRUE;
4702}
4703
4704
4705
4706 static LRESULT CALLBACK
4707_WndProc(
4708 HWND hwnd,
4709 UINT uMsg,
4710 WPARAM wParam,
4711 LPARAM lParam)
4712{
4713 /*
4714 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4715 hwnd, uMsg, wParam, lParam);
4716 */
4717
4718 HandleMouseHide(uMsg, lParam);
4719
4720 s_uMsg = uMsg;
4721 s_wParam = wParam;
4722 s_lParam = lParam;
4723
4724 switch (uMsg)
4725 {
4726 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4727 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4728 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4729 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4730 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4731 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4732 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4733 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4734 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4735#ifdef FEAT_MENU
4736 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4737#endif
4738 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4739 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4740 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4741 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4742 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4743 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4744 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4745 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4746 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4747#ifdef FEAT_NETBEANS_INTG
4748 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4749#endif
4750
Bram Moolenaarafa24992006-03-27 20:58:26 +00004751#ifdef FEAT_GUI_TABLINE
4752 case WM_RBUTTONUP:
4753 {
4754 if (gui_mch_showing_tabline())
4755 {
4756 POINT pt;
4757 RECT rect;
4758
4759 /*
4760 * If the cursor is on the tabline, display the tab menu
4761 */
4762 GetCursorPos((LPPOINT)&pt);
4763 GetWindowRect(s_textArea, &rect);
4764 if (pt.y < rect.top)
4765 {
4766 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004767 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004768 }
4769 }
4770 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4771 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004772 case WM_LBUTTONDBLCLK:
4773 {
4774 /*
4775 * If the user double clicked the tabline, create a new tab
4776 */
4777 if (gui_mch_showing_tabline())
4778 {
4779 POINT pt;
4780 RECT rect;
4781
4782 GetCursorPos((LPPOINT)&pt);
4783 GetWindowRect(s_textArea, &rect);
4784 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004785 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004786 }
4787 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4788 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004789#endif
4790
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 case WM_QUERYENDSESSION: /* System wants to go down. */
4792 gui_shell_closed(); /* Will exit when no changed buffers. */
4793 return FALSE; /* Do NOT allow system to go down. */
4794
4795 case WM_ENDSESSION:
4796 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004797 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004799 return 0L;
4800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 break;
4802
4803 case WM_CHAR:
4804 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4805 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004806 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 return 0L;
4808
4809 case WM_SYSCHAR:
4810 /*
4811 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4812 * shortcut key, handle like a typed ALT key, otherwise call Windows
4813 * ALT key handling.
4814 */
4815#ifdef FEAT_MENU
4816 if ( !gui.menu_is_active
4817 || p_wak[0] == 'n'
4818 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4819 )
4820#endif
4821 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004822 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 return 0L;
4824 }
4825#ifdef FEAT_MENU
4826 else
4827 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4828#endif
4829
4830 case WM_SYSKEYUP:
4831#ifdef FEAT_MENU
4832 /* This used to be done only when menu is active: ALT key is used for
4833 * that. But that caused problems when menu is disabled and using
4834 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4835 * are received, mouse pointer remains hidden. */
4836 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4837#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004838 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839#endif
4840
4841 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004842 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843
4844 case WM_MOUSEWHEEL:
4845 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004846 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847
Bram Moolenaar520470a2005-06-16 21:59:56 +00004848 /* Notification for change in SystemParametersInfo() */
4849 case WM_SETTINGCHANGE:
4850 return _OnSettingChange((UINT)wParam);
4851
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004852#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 case WM_NOTIFY:
4854 switch (((LPNMHDR) lParam)->code)
4855 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004856# ifdef FEAT_MBYTE
4857 case TTN_GETDISPINFOW:
4858# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004859 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004861 LPNMHDR hdr = (LPNMHDR)lParam;
4862 char_u *str = NULL;
4863 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004864
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004865 vim_free(tt_text);
4866 tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004867
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004868# ifdef FEAT_GUI_TABLINE
4869 if (gui_mch_showing_tabline()
4870 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004871 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004872 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004873 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004874 * Mouse is over the GUI tabline. Display the
4875 * tooltip for the tab under the cursor
4876 *
4877 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004878 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004879 GetCursorPos(&pt);
4880 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004881 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004882 TCHITTESTINFO htinfo;
4883 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004884
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004885 /*
4886 * Get the tab under the cursor
4887 */
4888 htinfo.pt.x = pt.x;
4889 htinfo.pt.y = pt.y;
4890 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4891 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004892 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004893 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004894
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004895 tp = find_tabpage(idx + 1);
4896 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004897 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004898 get_tabline_label(tp, TRUE);
4899 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004900 }
4901 }
4902 }
4903 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004904# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004905# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004906# ifdef FEAT_GUI_TABLINE
4907 else
4908# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004910 UINT idButton;
4911 vimmenu_T *pMenu;
4912
4913 idButton = (UINT) hdr->idFrom;
4914 pMenu = gui_mswin_find_menu(root_menu, idButton);
4915 if (pMenu)
4916 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004918# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004919 if (str != NULL)
4920 {
4921# ifdef FEAT_MBYTE
4922 if (hdr->code == TTN_GETDISPINFOW)
4923 {
4924 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)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 Moolenaar36f692d2008-11-20 16:10:17 +00004931 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004932 lpdi->lpszText = tt_text;
4933 /* can't show tooltip if failed */
4934 }
4935 else
4936# endif
4937 {
4938 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4939
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004940 /* Set the maximum width, this also enables using
4941 * \n for line break. */
4942 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4943 0, 500);
4944
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004945 if (STRLEN(str) < sizeof(lpdi->szText)
4946 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004947 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004948 sizeof(lpdi->szText) - 1);
4949 else
4950 lpdi->lpszText = tt_text;
4951 }
4952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 }
4954 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004955# ifdef FEAT_GUI_TABLINE
4956 case TCN_SELCHANGE:
4957 if (gui_mch_showing_tabline()
4958 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004959 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004960 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004961 return 0L;
4962 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004963 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004964
4965 case NM_RCLICK:
4966 if (gui_mch_showing_tabline()
4967 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004968 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004969 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004970 return 0L;
4971 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004972 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004973# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004975# ifdef FEAT_GUI_TABLINE
4976 if (gui_mch_showing_tabline()
4977 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4978 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4979# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 break;
4981 }
4982 break;
4983#endif
4984#if defined(MENUHINTS) && defined(FEAT_MENU)
4985 case WM_MENUSELECT:
4986 if (((UINT) HIWORD(wParam)
4987 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4988 == MF_HILITE
4989 && (State & CMDLINE) == 0)
4990 {
4991 UINT idButton;
4992 vimmenu_T *pMenu;
4993 static int did_menu_tip = FALSE;
4994
4995 if (did_menu_tip)
4996 {
4997 msg_clr_cmdline();
4998 setcursor();
4999 out_flush();
5000 did_menu_tip = FALSE;
5001 }
5002
5003 idButton = (UINT)LOWORD(wParam);
5004 pMenu = gui_mswin_find_menu(root_menu, idButton);
5005 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
5006 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
5007 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005008 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005010 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 setcursor();
5012 out_flush();
5013 did_menu_tip = TRUE;
5014 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01005015 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
5017 break;
5018#endif
5019 case WM_NCHITTEST:
5020 {
5021 LRESULT result;
5022 int x, y;
5023 int xPos = GET_X_LPARAM(lParam);
5024
5025 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
5026 if (result == HTCLIENT)
5027 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005028#ifdef FEAT_GUI_TABLINE
5029 if (gui_mch_showing_tabline())
5030 {
5031 int yPos = GET_Y_LPARAM(lParam);
5032 RECT rct;
5033
5034 /* If the cursor is on the GUI tabline, don't process this
5035 * event */
5036 GetWindowRect(s_textArea, &rct);
5037 if (yPos < rct.top)
5038 return result;
5039 }
5040#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02005041 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 xPos -= x;
5043
5044 if (xPos < 48) /* <VN> TODO should use system metric? */
5045 return HTBOTTOMLEFT;
5046 else
5047 return HTBOTTOMRIGHT;
5048 }
5049 else
5050 return result;
5051 }
5052 /* break; notreached */
5053
5054#ifdef FEAT_MBYTE_IME
5055 case WM_IME_NOTIFY:
5056 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5057 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005058 return 1L;
5059
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 case WM_IME_COMPOSITION:
5061 if (!_OnImeComposition(hwnd, wParam, lParam))
5062 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005063 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064#endif
5065
5066 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005068 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 {
5070 _OnFindRepl();
5071 }
5072#endif
5073 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5074 }
5075
Bram Moolenaar2787ab92011-12-14 15:23:59 +01005076 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077}
5078
5079/*
5080 * End of call-back routines
5081 */
5082
5083/* parent window, if specified with -P */
5084HWND vim_parent_hwnd = NULL;
5085
5086 static BOOL CALLBACK
5087FindWindowTitle(HWND hwnd, LPARAM lParam)
5088{
5089 char buf[2048];
5090 char *title = (char *)lParam;
5091
5092 if (GetWindowText(hwnd, buf, sizeof(buf)))
5093 {
5094 if (strstr(buf, title) != NULL)
5095 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005096 /* Found it. Store the window ref. and quit searching if MDI
5097 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005099 if (vim_parent_hwnd != NULL)
5100 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 }
5102 }
5103 return TRUE; /* continue searching */
5104}
5105
5106/*
5107 * Invoked for '-P "title"' argument: search for parent application to open
5108 * our window in.
5109 */
5110 void
5111gui_mch_set_parent(char *title)
5112{
5113 EnumWindows(FindWindowTitle, (LPARAM)title);
5114 if (vim_parent_hwnd == NULL)
5115 {
5116 EMSG2(_("E671: Cannot find window title \"%s\""), title);
5117 mch_exit(2);
5118 }
5119}
5120
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005121#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 static void
5123ole_error(char *arg)
5124{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005125 char buf[IOSIZE];
5126
5127 /* Can't use EMSG() here, we have not finished initialisation yet. */
5128 vim_snprintf(buf, IOSIZE,
5129 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
5130 arg);
5131 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134
5135/*
5136 * Parse the GUI related command-line arguments. Any arguments used are
5137 * deleted from argv, and *argc is decremented accordingly. This is called
5138 * when vim is started, whether or not the GUI has been started.
5139 */
5140 void
5141gui_mch_prepare(int *argc, char **argv)
5142{
5143 int silent = FALSE;
5144 int idx;
5145
5146 /* Check for special OLE command line parameters */
5147 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5148 {
5149 /* Check for a "-silent" argument first. */
5150 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5151 && (argv[2][0] == '-' || argv[2][0] == '/'))
5152 {
5153 silent = TRUE;
5154 idx = 2;
5155 }
5156 else
5157 idx = 1;
5158
5159 /* Register Vim as an OLE Automation server */
5160 if (STRICMP(argv[idx] + 1, "register") == 0)
5161 {
5162#ifdef FEAT_OLE
5163 RegisterMe(silent);
5164 mch_exit(0);
5165#else
5166 if (!silent)
5167 ole_error("register");
5168 mch_exit(2);
5169#endif
5170 }
5171
5172 /* Unregister Vim as an OLE Automation server */
5173 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5174 {
5175#ifdef FEAT_OLE
5176 UnregisterMe(!silent);
5177 mch_exit(0);
5178#else
5179 if (!silent)
5180 ole_error("unregister");
5181 mch_exit(2);
5182#endif
5183 }
5184
5185 /* Ignore an -embedding argument. It is only relevant if the
5186 * application wants to treat the case when it is started manually
5187 * differently from the case where it is started via automation (and
5188 * we don't).
5189 */
5190 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5191 {
5192#ifdef FEAT_OLE
5193 *argc = 1;
5194#else
5195 ole_error("embedding");
5196 mch_exit(2);
5197#endif
5198 }
5199 }
5200
5201#ifdef FEAT_OLE
5202 {
5203 int bDoRestart = FALSE;
5204
5205 InitOLE(&bDoRestart);
5206 /* automatically exit after registering */
5207 if (bDoRestart)
5208 mch_exit(0);
5209 }
5210#endif
5211
5212#ifdef FEAT_NETBEANS_INTG
5213 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005214 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 int arg;
5216
5217 for (arg = 1; arg < *argc; arg++)
5218 if (strncmp("-nb", argv[arg], 3) == 0)
5219 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 netbeansArg = argv[arg];
5221 mch_memmove(&argv[arg], &argv[arg + 1],
5222 (--*argc - arg) * sizeof(char *));
5223 argv[*argc] = NULL;
5224 break; /* enough? */
5225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 }
5227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228}
5229
5230/*
5231 * Initialise the GUI. Create all the windows, set up all the call-backs
5232 * etc.
5233 */
5234 int
5235gui_mch_init(void)
5236{
5237 const char szVimWndClass[] = VIM_CLASS;
5238 const char szTextAreaClass[] = "VimTextArea";
5239 WNDCLASS wndclass;
5240#ifdef FEAT_MBYTE
5241 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005242 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 WNDCLASSW wndclassw;
5244#endif
5245#ifdef GLOBAL_IME
5246 ATOM atom;
5247#endif
5248
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 /* Return here if the window was already opened (happens when
5250 * gui_mch_dialog() is called early). */
5251 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005252 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253
5254 /*
5255 * Load the tearoff bitmap
5256 */
5257#ifdef FEAT_TEAROFF
5258 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
5259#endif
5260
5261 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5262 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5263#ifdef FEAT_MENU
5264 gui.menu_height = 0; /* Windows takes care of this */
5265#endif
5266 gui.border_width = 0;
5267
5268 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5269
5270#ifdef FEAT_MBYTE
5271 /* First try using the wide version, so that we can use any title.
5272 * Otherwise only characters in the active codepage will work. */
5273 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
5274 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005275 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 wndclassw.lpfnWndProc = _WndProc;
5277 wndclassw.cbClsExtra = 0;
5278 wndclassw.cbWndExtra = 0;
5279 wndclassw.hInstance = s_hinst;
5280 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5281 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5282 wndclassw.hbrBackground = s_brush;
5283 wndclassw.lpszMenuName = NULL;
5284 wndclassw.lpszClassName = szVimWndClassW;
5285
5286 if ((
5287#ifdef GLOBAL_IME
5288 atom =
5289#endif
5290 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005291 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 else
5293 wide_WindowProc = TRUE;
5294 }
5295
5296 if (!wide_WindowProc)
5297#endif
5298
5299 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
5300 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005301 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 wndclass.lpfnWndProc = _WndProc;
5303 wndclass.cbClsExtra = 0;
5304 wndclass.cbWndExtra = 0;
5305 wndclass.hInstance = s_hinst;
5306 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
5307 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5308 wndclass.hbrBackground = s_brush;
5309 wndclass.lpszMenuName = NULL;
5310 wndclass.lpszClassName = szVimWndClass;
5311
5312 if ((
5313#ifdef GLOBAL_IME
5314 atom =
5315#endif
5316 RegisterClass(&wndclass)) == 0)
5317 return FAIL;
5318 }
5319
5320 if (vim_parent_hwnd != NULL)
5321 {
5322#ifdef HAVE_TRY_EXCEPT
5323 __try
5324 {
5325#endif
5326 /* Open inside the specified parent window.
5327 * TODO: last argument should point to a CLIENTCREATESTRUCT
5328 * structure. */
5329 s_hwnd = CreateWindowEx(
5330 WS_EX_MDICHILD,
5331 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005332 WS_OVERLAPPEDWINDOW | WS_CHILD
5333 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5335 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5336 100, /* Any value will do */
5337 100, /* Any value will do */
5338 vim_parent_hwnd, NULL,
5339 s_hinst, NULL);
5340#ifdef HAVE_TRY_EXCEPT
5341 }
5342 __except(EXCEPTION_EXECUTE_HANDLER)
5343 {
5344 /* NOP */
5345 }
5346#endif
5347 if (s_hwnd == NULL)
5348 {
5349 EMSG(_("E672: Unable to open window inside MDI application"));
5350 mch_exit(2);
5351 }
5352 }
5353 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005354 {
5355 /* If the provided windowid is not valid reset it to zero, so that it
5356 * is ignored and we open our own window. */
5357 if (IsWindow((HWND)win_socket_id) <= 0)
5358 win_socket_id = 0;
5359
5360 /* Create a window. If win_socket_id is not zero without border and
5361 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00005363 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005364 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5365 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005366 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5367 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5368 100, /* Any value will do */
5369 100, /* Any value will do */
5370 NULL, NULL,
5371 s_hinst, NULL);
5372 if (s_hwnd != NULL && win_socket_id != 0)
5373 {
5374 SetParent(s_hwnd, (HWND)win_socket_id);
5375 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5376 }
5377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378
5379 if (s_hwnd == NULL)
5380 return FAIL;
5381
5382#ifdef GLOBAL_IME
5383 global_ime_init(atom, s_hwnd);
5384#endif
5385#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5386 dyn_imm_load();
5387#endif
5388
5389 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005390#ifdef FEAT_MBYTE
5391 if (wide_WindowProc)
5392 {
5393 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
5394 {
5395 wndclassw.style = CS_OWNDC;
5396 wndclassw.lpfnWndProc = _TextAreaWndProc;
5397 wndclassw.cbClsExtra = 0;
5398 wndclassw.cbWndExtra = 0;
5399 wndclassw.hInstance = s_hinst;
5400 wndclassw.hIcon = NULL;
5401 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5402 wndclassw.hbrBackground = NULL;
5403 wndclassw.lpszMenuName = NULL;
5404 wndclassw.lpszClassName = szTextAreaClassW;
5405
5406 if (RegisterClassW(&wndclassw) == 0)
5407 return FAIL;
5408 }
5409 }
5410 else
5411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
5413 {
5414 wndclass.style = CS_OWNDC;
5415 wndclass.lpfnWndProc = _TextAreaWndProc;
5416 wndclass.cbClsExtra = 0;
5417 wndclass.cbWndExtra = 0;
5418 wndclass.hInstance = s_hinst;
5419 wndclass.hIcon = NULL;
5420 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5421 wndclass.hbrBackground = NULL;
5422 wndclass.lpszMenuName = NULL;
5423 wndclass.lpszClassName = szTextAreaClass;
5424
5425 if (RegisterClass(&wndclass) == 0)
5426 return FAIL;
5427 }
5428 s_textArea = CreateWindowEx(
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005429 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 szTextAreaClass, "Vim text area",
5431 WS_CHILD | WS_VISIBLE, 0, 0,
5432 100, /* Any value will do for now */
5433 100, /* Any value will do for now */
5434 s_hwnd, NULL,
5435 s_hinst, NULL);
5436
5437 if (s_textArea == NULL)
5438 return FAIL;
5439
Bram Moolenaar20321902016-02-17 12:30:17 +01005440#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005441 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5442 {
5443 HANDLE hIcon = NULL;
5444
5445 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005446 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005447 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005448#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005449
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450#ifdef FEAT_MENU
5451 s_menuBar = CreateMenu();
5452#endif
5453 s_hdc = GetDC(s_textArea);
5454
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455#ifdef FEAT_WINDOWS
5456 DragAcceptFiles(s_hwnd, TRUE);
5457#endif
5458
5459 /* Do we need to bother with this? */
5460 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5461
5462 /* Get background/foreground colors from the system */
5463 gui_mch_def_colors();
5464
5465 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5466 * file) */
5467 set_normal_colors();
5468
5469 /*
5470 * Check that none of the colors are the same as the background color.
5471 * Then store the current values as the defaults.
5472 */
5473 gui_check_colors();
5474 gui.def_norm_pixel = gui.norm_pixel;
5475 gui.def_back_pixel = gui.back_pixel;
5476
5477 /* Get the colors for the highlight groups (gui_check_colors() might have
5478 * changed them) */
5479 highlight_gui_started();
5480
5481 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005482 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005484 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485
5486 /*
5487 * Set up for Intellimouse processing
5488 */
5489 init_mouse_wheel();
5490
5491 /*
5492 * compute a couple of metrics used for the dialogs
5493 */
5494 get_dialog_font_metrics();
5495#ifdef FEAT_TOOLBAR
5496 /*
5497 * Create the toolbar
5498 */
5499 initialise_toolbar();
5500#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005501#ifdef FEAT_GUI_TABLINE
5502 /*
5503 * Create the tabline
5504 */
5505 initialise_tabline();
5506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507#ifdef MSWIN_FIND_REPLACE
5508 /*
5509 * Initialise the dialog box stuff
5510 */
5511 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5512
5513 /* Initialise the struct */
5514 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005515 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005517 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5519 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5520 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005521# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00005522 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
5523 s_findrep_struct_w.lpstrFindWhat =
5524 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5525 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
5526 s_findrep_struct_w.lpstrReplaceWith =
5527 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5528 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
5529 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
5530 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5531# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005534#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005535# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5536/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5537# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005538# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005539# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005540# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005541 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005542 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005543#endif
5544
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005545#ifdef FEAT_RENDER_OPTIONS
5546 if (p_rop)
5547 (void)gui_mch_set_rendering_options(p_rop);
5548#endif
5549
Bram Moolenaar748bf032005-02-02 23:04:36 +00005550theend:
5551 /* Display any pending error messages */
5552 display_errors();
5553
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 return OK;
5555}
5556
5557/*
5558 * Get the size of the screen, taking position on multiple monitors into
5559 * account (if supported).
5560 */
5561 static void
5562get_work_area(RECT *spi_rect)
5563{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005564 HMONITOR mon;
5565 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005567 /* work out which monitor the window is on, and get *it's* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005568 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005569 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005571 moninfo.cbSize = sizeof(MONITORINFO);
5572 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005574 *spi_rect = moninfo.rcWork;
5575 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 }
5577 }
5578 /* this is the old method... */
5579 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5580}
5581
5582/*
5583 * Set the size of the window to the given width and height in pixels.
5584 */
5585 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005586gui_mch_set_shellsize(
5587 int width,
5588 int height,
5589 int min_width UNUSED,
5590 int min_height UNUSED,
5591 int base_width UNUSED,
5592 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005593 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594{
5595 RECT workarea_rect;
5596 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 WINDOWPLACEMENT wndpl;
5598
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005599 /* Try to keep window completely on screen. */
5600 /* Get position of the screen work area. This is the part that is not
5601 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 get_work_area(&workarea_rect);
5603
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005604 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005605 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 wndpl.length = sizeof(WINDOWPLACEMENT);
5607 GetWindowPlacement(s_hwnd, &wndpl);
5608
5609 /* Resizing a maximized window looks very strange, unzoom it first.
5610 * But don't do it when still starting up, it may have been requested in
5611 * the shortcut. */
5612 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5613 {
5614 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5615 /* Need to get the settings of the normal window. */
5616 GetWindowPlacement(s_hwnd, &wndpl);
5617 }
5618
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005620 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005621 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005622 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005623 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 + GetSystemMetrics(SM_CYCAPTION)
5625#ifdef FEAT_MENU
5626 + gui_mswin_get_menu_height(FALSE)
5627#endif
5628 ;
5629
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005630 /* The following should take care of keeping Vim on the same monitor, no
5631 * matter if the secondary monitor is left or right of the primary
5632 * monitor. */
5633 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5634 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005635
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005636 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005637 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005638 && wndpl.rcNormalPosition.right > workarea_rect.right)
5639 OffsetRect(&wndpl.rcNormalPosition,
5640 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005642 if ((direction & RESIZE_HOR)
5643 && wndpl.rcNormalPosition.left < workarea_rect.left)
5644 OffsetRect(&wndpl.rcNormalPosition,
5645 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646
Bram Moolenaarafa24992006-03-27 20:58:26 +00005647 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005648 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5649 OffsetRect(&wndpl.rcNormalPosition,
5650 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005652 if ((direction & RESIZE_VERT)
5653 && wndpl.rcNormalPosition.top < workarea_rect.top)
5654 OffsetRect(&wndpl.rcNormalPosition,
5655 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656
5657 /* set window position - we should use SetWindowPlacement rather than
5658 * SetWindowPos as the MSDN docs say the coord systems returned by
5659 * these two are not compatible. */
5660 SetWindowPlacement(s_hwnd, &wndpl);
5661
5662 SetActiveWindow(s_hwnd);
5663 SetFocus(s_hwnd);
5664
5665#ifdef FEAT_MENU
5666 /* Menu may wrap differently now */
5667 gui_mswin_get_menu_height(!gui.starting);
5668#endif
5669}
5670
5671
5672 void
5673gui_mch_set_scrollbar_thumb(
5674 scrollbar_T *sb,
5675 long val,
5676 long size,
5677 long max)
5678{
5679 SCROLLINFO info;
5680
5681 sb->scroll_shift = 0;
5682 while (max > 32767)
5683 {
5684 max = (max + 1) >> 1;
5685 val >>= 1;
5686 size >>= 1;
5687 ++sb->scroll_shift;
5688 }
5689
5690 if (sb->scroll_shift > 0)
5691 ++size;
5692
5693 info.cbSize = sizeof(info);
5694 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5695 info.nPos = val;
5696 info.nMin = 0;
5697 info.nMax = max;
5698 info.nPage = size;
5699 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5700}
5701
5702
5703/*
5704 * Set the current text font.
5705 */
5706 void
5707gui_mch_set_font(GuiFont font)
5708{
5709 gui.currFont = font;
5710}
5711
5712
5713/*
5714 * Set the current text foreground color.
5715 */
5716 void
5717gui_mch_set_fg_color(guicolor_T color)
5718{
5719 gui.currFgColor = color;
5720}
5721
5722/*
5723 * Set the current text background color.
5724 */
5725 void
5726gui_mch_set_bg_color(guicolor_T color)
5727{
5728 gui.currBgColor = color;
5729}
5730
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005731/*
5732 * Set the current text special color.
5733 */
5734 void
5735gui_mch_set_sp_color(guicolor_T color)
5736{
5737 gui.currSpColor = color;
5738}
5739
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740#if defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)
5741/*
5742 * Multi-byte handling, originally by Sung-Hoon Baek.
5743 * First static functions (no prototypes generated).
5744 */
5745#ifdef _MSC_VER
5746# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5747#endif
5748#include <imm.h>
5749
5750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 * handle WM_IME_NOTIFY message
5752 */
5753 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005754_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755{
5756 LRESULT lResult = 0;
5757 HIMC hImc;
5758
5759 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5760 return lResult;
5761 switch (dwCommand)
5762 {
5763 case IMN_SETOPENSTATUS:
5764 if (pImmGetOpenStatus(hImc))
5765 {
5766 pImmSetCompositionFont(hImc, &norm_logfont);
5767 im_set_position(gui.row, gui.col);
5768
5769 /* Disable langmap */
5770 State &= ~LANGMAP;
5771 if (State & INSERT)
5772 {
5773#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
5774 /* Unshown 'keymap' in status lines */
5775 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5776 {
5777 /* Save cursor position */
5778 int old_row = gui.row;
5779 int old_col = gui.col;
5780
5781 // This must be called here before
5782 // status_redraw_curbuf(), otherwise the mode
5783 // message may appear in the wrong position.
5784 showmode();
5785 status_redraw_curbuf();
5786 update_screen(0);
5787 /* Restore cursor position */
5788 gui.row = old_row;
5789 gui.col = old_col;
5790 }
5791#endif
5792 }
5793 }
5794 gui_update_cursor(TRUE, FALSE);
5795 lResult = 0;
5796 break;
5797 }
5798 pImmReleaseContext(hWnd, hImc);
5799 return lResult;
5800}
5801
5802 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005803_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804{
5805 char_u *ret;
5806 int len;
5807
5808 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5809 return 0;
5810
5811 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5812 if (ret != NULL)
5813 {
5814 add_to_input_buf_csi(ret, len);
5815 vim_free(ret);
5816 return 1;
5817 }
5818 return 0;
5819}
5820
5821/*
5822 * get the current composition string, in UCS-2; *lenp is the number of
5823 * *lenp is the number of Unicode characters.
5824 */
5825 static short_u *
5826GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5827{
5828 LONG ret;
5829 LPWSTR wbuf = NULL;
5830 char_u *buf;
5831
5832 if (!pImmGetContext)
5833 return NULL; /* no imm32.dll */
5834
5835 /* Try Unicode; this'll always work on NT regardless of codepage. */
5836 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5837 if (ret == 0)
5838 return NULL; /* empty */
5839
5840 if (ret > 0)
5841 {
5842 /* Allocate the requested buffer plus space for the NUL character. */
5843 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
5844 if (wbuf != NULL)
5845 {
5846 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5847 *lenp = ret / sizeof(WCHAR);
5848 }
5849 return (short_u *)wbuf;
5850 }
5851
5852 /* ret < 0; we got an error, so try the ANSI version. This'll work
5853 * on 9x/ME, but only if the codepage happens to be set to whatever
5854 * we're inputting. */
5855 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5856 if (ret <= 0)
5857 return NULL; /* empty or error */
5858
5859 buf = alloc(ret);
5860 if (buf == NULL)
5861 return NULL;
5862 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5863
5864 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005865 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 vim_free(buf);
5867
5868 return (short_u *)wbuf;
5869}
5870
5871/*
5872 * void GetResultStr()
5873 *
5874 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5875 * get complete composition string
5876 */
5877 static char_u *
5878GetResultStr(HWND hwnd, int GCS, int *lenp)
5879{
5880 HIMC hIMC; /* Input context handle. */
5881 short_u *buf = NULL;
5882 char_u *convbuf = NULL;
5883
5884 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5885 return NULL;
5886
5887 /* Reads in the composition string. */
5888 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5889 if (buf == NULL)
5890 return NULL;
5891
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005892 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 pImmReleaseContext(hwnd, hIMC);
5894 vim_free(buf);
5895 return convbuf;
5896}
5897#endif
5898
5899/* For global functions we need prototypes. */
5900#if (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) || defined(PROTO)
5901
5902/*
5903 * set font to IM.
5904 */
5905 void
5906im_set_font(LOGFONT *lf)
5907{
5908 HIMC hImc;
5909
5910 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5911 {
5912 pImmSetCompositionFont(hImc, lf);
5913 pImmReleaseContext(s_hwnd, hImc);
5914 }
5915}
5916
5917/*
5918 * Notify cursor position to IM.
5919 */
5920 void
5921im_set_position(int row, int col)
5922{
5923 HIMC hImc;
5924
5925 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5926 {
5927 COMPOSITIONFORM cfs;
5928
5929 cfs.dwStyle = CFS_POINT;
5930 cfs.ptCurrentPos.x = FILL_X(col);
5931 cfs.ptCurrentPos.y = FILL_Y(row);
5932 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
5933 pImmSetCompositionWindow(hImc, &cfs);
5934
5935 pImmReleaseContext(s_hwnd, hImc);
5936 }
5937}
5938
5939/*
5940 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5941 */
5942 void
5943im_set_active(int active)
5944{
5945 HIMC hImc;
5946 static HIMC hImcOld = (HIMC)0;
5947
5948 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
5949 {
5950 if (p_imdisable)
5951 {
5952 if (hImcOld == (HIMC)0)
5953 {
5954 hImcOld = pImmGetContext(s_hwnd);
5955 if (hImcOld)
5956 pImmAssociateContext(s_hwnd, (HIMC)0);
5957 }
5958 active = FALSE;
5959 }
5960 else if (hImcOld != (HIMC)0)
5961 {
5962 pImmAssociateContext(s_hwnd, hImcOld);
5963 hImcOld = (HIMC)0;
5964 }
5965
5966 hImc = pImmGetContext(s_hwnd);
5967 if (hImc)
5968 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005969 /*
5970 * for Korean ime
5971 */
5972 HKL hKL = GetKeyboardLayout(0);
5973
5974 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5975 {
5976 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5977 static BOOL bSaved = FALSE;
5978
5979 if (active)
5980 {
5981 /* if we have a saved conversion status, restore it */
5982 if (bSaved)
5983 pImmSetConversionStatus(hImc, dwConversionSaved,
5984 dwSentenceSaved);
5985 bSaved = FALSE;
5986 }
5987 else
5988 {
5989 /* save conversion status and disable korean */
5990 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5991 &dwSentenceSaved))
5992 {
5993 bSaved = TRUE;
5994 pImmSetConversionStatus(hImc,
5995 dwConversionSaved & ~(IME_CMODE_NATIVE
5996 | IME_CMODE_FULLSHAPE),
5997 dwSentenceSaved);
5998 }
5999 }
6000 }
6001
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 pImmSetOpenStatus(hImc, active);
6003 pImmReleaseContext(s_hwnd, hImc);
6004 }
6005 }
6006}
6007
6008/*
6009 * Get IM status. When IM is on, return not 0. Else return 0.
6010 */
6011 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006012im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013{
6014 int status = 0;
6015 HIMC hImc;
6016
6017 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6018 {
6019 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6020 pImmReleaseContext(s_hwnd, hImc);
6021 }
6022 return status;
6023}
6024
6025#endif /* FEAT_MBYTE && FEAT_MBYTE_IME */
6026
6027#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
6028/* Win32 with GLOBAL IME */
6029
6030/*
6031 * Notify cursor position to IM.
6032 */
6033 void
6034im_set_position(int row, int col)
6035{
6036 /* Win32 with GLOBAL IME */
6037 POINT p;
6038
6039 p.x = FILL_X(col);
6040 p.y = FILL_Y(row);
6041 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
6042 global_ime_set_position(&p);
6043}
6044
6045/*
6046 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6047 */
6048 void
6049im_set_active(int active)
6050{
6051 global_ime_set_status(active);
6052}
6053
6054/*
6055 * Get IM status. When IM is on, return not 0. Else return 0.
6056 */
6057 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006058im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059{
6060 return global_ime_get_status();
6061}
6062#endif
6063
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006064#ifdef FEAT_MBYTE
6065/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006066 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006067 */
6068 static void
6069latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6070{
6071 int c;
6072
Bram Moolenaarca003e12006-03-17 23:19:38 +00006073 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006074 {
6075 c = *text++;
6076 switch (c)
6077 {
6078 case 0xa4: c = 0x20ac; break; /* euro */
6079 case 0xa6: c = 0x0160; break; /* S hat */
6080 case 0xa8: c = 0x0161; break; /* S -hat */
6081 case 0xb4: c = 0x017d; break; /* Z hat */
6082 case 0xb8: c = 0x017e; break; /* Z -hat */
6083 case 0xbc: c = 0x0152; break; /* OE */
6084 case 0xbd: c = 0x0153; break; /* oe */
6085 case 0xbe: c = 0x0178; break; /* Y */
6086 }
6087 *unicodebuf++ = c;
6088 }
6089}
6090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091
6092#ifdef FEAT_RIGHTLEFT
6093/*
6094 * What is this for? In the case where you are using Win98 or Win2K or later,
6095 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6096 * reverses the string sent to the TextOut... family. This sucks, because we
6097 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6098 * way to tell Windblows not to do this!
6099 *
6100 * The short of it is that this 'RevOut' only gets called if you are running
6101 * one of the new, "improved" MS OSes, and only if you are running in
6102 * 'rightleft' mode. It makes display take *slightly* longer, but not
6103 * noticeably so.
6104 */
6105 static void
6106RevOut( HDC s_hdc,
6107 int col,
6108 int row,
6109 UINT foptions,
6110 CONST RECT *pcliprect,
6111 LPCTSTR text,
6112 UINT len,
6113 CONST INT *padding)
6114{
6115 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006117 for (ix = 0; ix < (int)len; ++ix)
6118 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6119 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120}
6121#endif
6122
6123 void
6124gui_mch_draw_string(
6125 int row,
6126 int col,
6127 char_u *text,
6128 int len,
6129 int flags)
6130{
6131 static int *padding = NULL;
6132 static int pad_size = 0;
6133 int i;
6134 const RECT *pcliprect = NULL;
6135 UINT foptions = 0;
6136#ifdef FEAT_MBYTE
6137 static WCHAR *unicodebuf = NULL;
6138 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006139 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 int n = 0;
6141#endif
6142 HPEN hpen, old_pen;
6143 int y;
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006144#ifdef FEAT_DIRECTX
6145 int font_is_ttf_or_vector = 0;
6146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 /*
6149 * Italic and bold text seems to have an extra row of pixels at the bottom
6150 * (below where the bottom of the character should be). If we draw the
6151 * characters with a solid background, the top row of pixels in the
6152 * character below will be overwritten. We can fix this by filling in the
6153 * background ourselves, to the correct character proportions, and then
6154 * writing the character in transparent mode. Still have a problem when
6155 * the character is "_", which gets written on to the character below.
6156 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6157 * pixel in their slots, which fixes the problem with the bottom row of
6158 * pixels. We still need this code because otherwise the top row of pixels
6159 * becomes a problem. - webb.
6160 */
6161 static HBRUSH hbr_cache[2] = {NULL, NULL};
6162 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6163 static int brush_lru = 0;
6164 HBRUSH hbr;
6165 RECT rc;
6166
6167 if (!(flags & DRAW_TRANSP))
6168 {
6169 /*
6170 * Clear background first.
6171 * Note: FillRect() excludes right and bottom of rectangle.
6172 */
6173 rc.left = FILL_X(col);
6174 rc.top = FILL_Y(row);
6175#ifdef FEAT_MBYTE
6176 if (has_mbyte)
6177 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006179 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 }
6181 else
6182#endif
6183 rc.right = FILL_X(col + len);
6184 rc.bottom = FILL_Y(row + 1);
6185
6186 /* Cache the created brush, that saves a lot of time. We need two:
6187 * one for cursor background and one for the normal background. */
6188 if (gui.currBgColor == brush_color[0])
6189 {
6190 hbr = hbr_cache[0];
6191 brush_lru = 1;
6192 }
6193 else if (gui.currBgColor == brush_color[1])
6194 {
6195 hbr = hbr_cache[1];
6196 brush_lru = 0;
6197 }
6198 else
6199 {
6200 if (hbr_cache[brush_lru] != NULL)
6201 DeleteBrush(hbr_cache[brush_lru]);
6202 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6203 brush_color[brush_lru] = gui.currBgColor;
6204 hbr = hbr_cache[brush_lru];
6205 brush_lru = !brush_lru;
6206 }
6207 FillRect(s_hdc, &rc, hbr);
6208
6209 SetBkMode(s_hdc, TRANSPARENT);
6210
6211 /*
6212 * When drawing block cursor, prevent inverted character spilling
6213 * over character cell (can happen with bold/italic)
6214 */
6215 if (flags & DRAW_CURSOR)
6216 {
6217 pcliprect = &rc;
6218 foptions = ETO_CLIPPED;
6219 }
6220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 SetTextColor(s_hdc, gui.currFgColor);
6222 SelectFont(s_hdc, gui.currFont);
6223
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006224#ifdef FEAT_DIRECTX
6225 if (IS_ENABLE_DIRECTX())
6226 {
6227 TEXTMETRIC tm;
6228
6229 GetTextMetrics(s_hdc, &tm);
6230 if (tm.tmPitchAndFamily & (TMPF_TRUETYPE | TMPF_VECTOR))
6231 {
6232 font_is_ttf_or_vector = 1;
6233 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
6234 }
6235 }
6236#endif
6237
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6239 {
6240 vim_free(padding);
6241 pad_size = Columns;
6242
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006243 /* Don't give an out-of-memory message here, it would call us
6244 * recursively. */
6245 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 if (padding != NULL)
6247 for (i = 0; i < pad_size; i++)
6248 padding[i] = gui.char_width;
6249 }
6250
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 /*
6252 * We have to provide the padding argument because italic and bold versions
6253 * of fixed-width fonts are often one pixel or so wider than their normal
6254 * versions.
6255 * No check for DRAW_BOLD, Windows will have done it already.
6256 */
6257
6258#ifdef FEAT_MBYTE
6259 /* Check if there are any UTF-8 characters. If not, use normal text
6260 * output to speed up output. */
6261 if (enc_utf8)
6262 for (n = 0; n < len; ++n)
6263 if (text[n] >= 0x80)
6264 break;
6265
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006266#if defined(FEAT_DIRECTX)
6267 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6268 * required that unicode drawing routine, currently. So this forces it
6269 * enabled. */
6270 if (enc_utf8 && IS_ENABLE_DIRECTX())
6271 n = 0; /* Keep n < len, to enter block for unicode. */
6272#endif
6273
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006275 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006277 if ((enc_utf8
6278 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6279 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 && (unicodebuf == NULL || len > unibuflen))
6281 {
6282 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006283 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284
6285 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006286 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287
6288 unibuflen = len;
6289 }
6290
6291 if (enc_utf8 && n < len && unicodebuf != NULL)
6292 {
6293 /* Output UTF-8 characters. Caller has already separated
6294 * composing characters. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006295 int i;
6296 int wlen; /* string length in words */
6297 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 int cells; /* cell width of string up to composing char */
6299 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006300 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006302 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006303 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006305 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006307 c = utf_ptr2char(text + i);
6308 if (c >= 0x10000)
6309 {
6310 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006311 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6312 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006313 }
6314 else
6315 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006316 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006317 }
6318 cw = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 if (cw > 2) /* don't use 4 for unprintable char */
6320 cw = 1;
6321 if (unicodepdy != NULL)
6322 {
6323 /* Use unicodepdy to make characters fit as we expect, even
6324 * when the font uses different widths (e.g., bold character
6325 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006326 if (c >= 0x10000)
6327 {
6328 unicodepdy[wlen - 2] = cw * gui.char_width;
6329 unicodepdy[wlen - 1] = 0;
6330 }
6331 else
6332 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 }
6334 cells += cw;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006335 i += utfc_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006336 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006338#if defined(FEAT_DIRECTX)
6339 if (IS_ENABLE_DIRECTX() && font_is_ttf_or_vector)
6340 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006341 /* Add one to "cells" for italics. */
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006342 DWriteContext_DrawText(s_dwc, s_hdc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006343 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006344 gui.char_width, gui.currFgColor);
6345 }
6346 else
6347#endif
6348 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6349 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 len = cells; /* used for underlining */
6351 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006352 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 {
6354 /* If we want to display codepage data, and the current CP is not the
6355 * ANSI one, we need to go via Unicode. */
6356 if (unicodebuf != NULL)
6357 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006358 if (enc_latin9)
6359 latin9_to_ucs(text, len, unicodebuf);
6360 else
6361 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 MB_PRECOMPOSED,
6363 (char *)text, len,
6364 (LPWSTR)unicodebuf, unibuflen);
6365 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006366 {
6367 /* Use unicodepdy to make characters fit as we expect, even
6368 * when the font uses different widths (e.g., bold character
6369 * is wider). */
6370 if (unicodepdy != NULL)
6371 {
6372 int i;
6373 int cw;
6374
6375 for (i = 0; i < len; ++i)
6376 {
6377 cw = utf_char2cells(unicodebuf[i]);
6378 if (cw > 2)
6379 cw = 1;
6380 unicodepdy[i] = cw * gui.char_width;
6381 }
6382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006384 foptions, pcliprect, unicodebuf, len, unicodepdy);
6385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 }
6387 }
6388 else
6389#endif
6390 {
6391#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006392 /* Windows will mess up RL text, so we have to draw it character by
6393 * character. Only do this if RL is on, since it's slow. */
6394 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6396 foptions, pcliprect, (char *)text, len, padding);
6397 else
6398#endif
6399 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6400 foptions, pcliprect, (char *)text, len, padding);
6401 }
6402
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006403 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 if (flags & DRAW_UNDERL)
6405 {
6406 hpen = CreatePen(PS_SOLID, 1, gui.currFgColor);
6407 old_pen = SelectObject(s_hdc, hpen);
6408 /* When p_linespace is 0, overwrite the bottom row of pixels.
6409 * Otherwise put the line just below the character. */
6410 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 if (p_linespace > 1)
6412 y -= p_linespace - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 MoveToEx(s_hdc, FILL_X(col), y, NULL);
6414 /* Note: LineTo() excludes the last pixel in the line. */
6415 LineTo(s_hdc, FILL_X(col + len), y);
6416 DeleteObject(SelectObject(s_hdc, old_pen));
6417 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006418
6419 /* Undercurl */
6420 if (flags & DRAW_UNDERC)
6421 {
6422 int x;
6423 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006424 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006425
6426 y = FILL_Y(row + 1) - 1;
6427 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6428 {
6429 offset = val[x % 8];
6430 SetPixel(s_hdc, x, y - offset, gui.currSpColor);
6431 }
6432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433}
6434
6435
6436/*
6437 * Output routines.
6438 */
6439
6440/* Flush any output to the screen */
6441 void
6442gui_mch_flush(void)
6443{
6444# if defined(__BORLANDC__)
6445 /*
6446 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6447 * prototype declaration.
6448 * The compiler complains if __stdcall is not used in both declarations.
6449 */
6450 BOOL __stdcall GdiFlush(void);
6451# endif
6452
6453 GdiFlush();
6454}
6455
6456 static void
6457clear_rect(RECT *rcp)
6458{
6459 HBRUSH hbr;
6460
6461 hbr = CreateSolidBrush(gui.back_pixel);
6462 FillRect(s_hdc, rcp, hbr);
6463 DeleteBrush(hbr);
6464}
6465
6466
Bram Moolenaarc716c302006-01-21 22:12:51 +00006467 void
6468gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6469{
6470 RECT workarea_rect;
6471
6472 get_work_area(&workarea_rect);
6473
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006474 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006475 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006476 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006477
6478 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6479 * the menubar for MSwin, we subtract it from the screen height, so that
6480 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006481 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006482 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006483 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006484 - GetSystemMetrics(SM_CYCAPTION)
6485#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006486 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006487#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006488 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006489}
6490
6491
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492#if defined(FEAT_MENU) || defined(PROTO)
6493/*
6494 * Add a sub menu to the menu bar.
6495 */
6496 void
6497gui_mch_add_menu(
6498 vimmenu_T *menu,
6499 int pos)
6500{
6501 vimmenu_T *parent = menu->parent;
6502
6503 menu->submenu_id = CreatePopupMenu();
6504 menu->id = s_menu_id++;
6505
6506 if (menu_is_menubar(menu->name))
6507 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508#ifdef FEAT_MBYTE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006509 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006511 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6512 {
6513 /* 'encoding' differs from active codepage: convert menu name
6514 * and use wide function */
6515 wn = enc_to_utf16(menu->name, NULL);
6516 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006518 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006520 infow.cbSize = sizeof(infow);
6521 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6522 | MIIM_SUBMENU;
6523 infow.dwItemData = (long_u)menu;
6524 infow.wID = menu->id;
6525 infow.fType = MFT_STRING;
6526 infow.dwTypeData = wn;
6527 infow.cch = (UINT)wcslen(wn);
6528 infow.hSubMenu = menu->submenu_id;
6529 InsertMenuItemW((parent == NULL)
6530 ? s_menuBar : parent->submenu_id,
6531 (UINT)pos, TRUE, &infow);
6532 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006536 if (wn == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537#endif
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006538 {
6539 MENUITEMINFO info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006541 info.cbSize = sizeof(info);
6542 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
6543 info.dwItemData = (long_u)menu;
6544 info.wID = menu->id;
6545 info.fType = MFT_STRING;
6546 info.dwTypeData = (LPTSTR)menu->name;
6547 info.cch = (UINT)STRLEN(menu->name);
6548 info.hSubMenu = menu->submenu_id;
6549 InsertMenuItem((parent == NULL)
6550 ? s_menuBar : parent->submenu_id,
6551 (UINT)pos, TRUE, &info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 }
6553 }
6554
6555 /* Fix window size if menu may have wrapped */
6556 if (parent == NULL)
6557 gui_mswin_get_menu_height(!gui.starting);
6558#ifdef FEAT_TEAROFF
6559 else if (IsWindow(parent->tearoff_handle))
6560 rebuild_tearoff(parent);
6561#endif
6562}
6563
6564 void
6565gui_mch_show_popupmenu(vimmenu_T *menu)
6566{
6567 POINT mp;
6568
6569 (void)GetCursorPos((LPPOINT)&mp);
6570 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6571}
6572
6573 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006574gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575{
6576 vimmenu_T *menu = gui_find_menu(path_name);
6577
6578 if (menu != NULL)
6579 {
6580 POINT p;
6581
6582 /* Find the position of the current cursor */
6583 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006584 if (mouse_pos)
6585 {
6586 int mx, my;
6587
6588 gui_mch_getmouse(&mx, &my);
6589 p.x += mx;
6590 p.y += my;
6591 }
6592 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 {
6594 p.x += TEXT_X(W_WINCOL(curwin) + curwin->w_wcol + 1);
6595 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6596 }
6597 msg_scroll = FALSE;
6598 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6599 }
6600}
6601
6602#if defined(FEAT_TEAROFF) || defined(PROTO)
6603/*
6604 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6605 * create it as a pseudo-"tearoff menu".
6606 */
6607 void
6608gui_make_tearoff(char_u *path_name)
6609{
6610 vimmenu_T *menu = gui_find_menu(path_name);
6611
6612 /* Found the menu, so tear it off. */
6613 if (menu != NULL)
6614 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6615}
6616#endif
6617
6618/*
6619 * Add a menu item to a menu
6620 */
6621 void
6622gui_mch_add_menu_item(
6623 vimmenu_T *menu,
6624 int idx)
6625{
6626 vimmenu_T *parent = menu->parent;
6627
6628 menu->id = s_menu_id++;
6629 menu->submenu_id = NULL;
6630
6631#ifdef FEAT_TEAROFF
6632 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6633 {
6634 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6635 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6636 }
6637 else
6638#endif
6639#ifdef FEAT_TOOLBAR
6640 if (menu_is_toolbar(parent->name))
6641 {
6642 TBBUTTON newtb;
6643
6644 vim_memset(&newtb, 0, sizeof(newtb));
6645 if (menu_is_separator(menu->name))
6646 {
6647 newtb.iBitmap = 0;
6648 newtb.fsStyle = TBSTYLE_SEP;
6649 }
6650 else
6651 {
6652 newtb.iBitmap = get_toolbar_bitmap(menu);
6653 newtb.fsStyle = TBSTYLE_BUTTON;
6654 }
6655 newtb.idCommand = menu->id;
6656 newtb.fsState = TBSTATE_ENABLED;
6657 newtb.iString = 0;
6658 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6659 (LPARAM)&newtb);
6660 menu->submenu_id = (HMENU)-1;
6661 }
6662 else
6663#endif
6664 {
6665#ifdef FEAT_MBYTE
6666 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667
6668 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6669 {
6670 /* 'encoding' differs from active codepage: convert menu item name
6671 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006672 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 if (wn != NULL)
6674 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006675 InsertMenuW(parent->submenu_id, (UINT)idx,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 (menu_is_separator(menu->name)
6677 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6678 (UINT)menu->id, wn);
6679 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 }
6681 }
6682 if (wn == NULL)
6683#endif
6684 InsertMenu(parent->submenu_id, (UINT)idx,
6685 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
6686 | MF_BYPOSITION,
6687 (UINT)menu->id, (LPCTSTR)menu->name);
6688#ifdef FEAT_TEAROFF
6689 if (IsWindow(parent->tearoff_handle))
6690 rebuild_tearoff(parent);
6691#endif
6692 }
6693}
6694
6695/*
6696 * Destroy the machine specific menu widget.
6697 */
6698 void
6699gui_mch_destroy_menu(vimmenu_T *menu)
6700{
6701#ifdef FEAT_TOOLBAR
6702 /*
6703 * is this a toolbar button?
6704 */
6705 if (menu->submenu_id == (HMENU)-1)
6706 {
6707 int iButton;
6708
6709 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6710 (WPARAM)menu->id, 0);
6711 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6712 }
6713 else
6714#endif
6715 {
6716 if (menu->parent != NULL
6717 && menu_is_popup(menu->parent->dname)
6718 && menu->parent->submenu_id != NULL)
6719 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6720 else
6721 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6722 if (menu->submenu_id != NULL)
6723 DestroyMenu(menu->submenu_id);
6724#ifdef FEAT_TEAROFF
6725 if (IsWindow(menu->tearoff_handle))
6726 DestroyWindow(menu->tearoff_handle);
6727 if (menu->parent != NULL
6728 && menu->parent->children != NULL
6729 && IsWindow(menu->parent->tearoff_handle))
6730 {
6731 /* This menu must not show up when rebuilding the tearoff window. */
6732 menu->modes = 0;
6733 rebuild_tearoff(menu->parent);
6734 }
6735#endif
6736 }
6737}
6738
6739#ifdef FEAT_TEAROFF
6740 static void
6741rebuild_tearoff(vimmenu_T *menu)
6742{
6743 /*hackish*/
6744 char_u tbuf[128];
6745 RECT trect;
6746 RECT rct;
6747 RECT roct;
6748 int x, y;
6749
6750 HWND thwnd = menu->tearoff_handle;
6751
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006752 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 if (GetWindowRect(thwnd, &trect)
6754 && GetWindowRect(s_hwnd, &rct)
6755 && GetClientRect(s_hwnd, &roct))
6756 {
6757 x = trect.left - rct.left;
6758 y = (trect.top - rct.bottom + roct.bottom);
6759 }
6760 else
6761 {
6762 x = y = 0xffffL;
6763 }
6764 DestroyWindow(thwnd);
6765 if (menu->children != NULL)
6766 {
6767 gui_mch_tearoff(tbuf, menu, x, y);
6768 if (IsWindow(menu->tearoff_handle))
6769 (void) SetWindowPos(menu->tearoff_handle,
6770 NULL,
6771 (int)trect.left,
6772 (int)trect.top,
6773 0, 0,
6774 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6775 }
6776}
6777#endif /* FEAT_TEAROFF */
6778
6779/*
6780 * Make a menu either grey or not grey.
6781 */
6782 void
6783gui_mch_menu_grey(
6784 vimmenu_T *menu,
6785 int grey)
6786{
6787#ifdef FEAT_TOOLBAR
6788 /*
6789 * is this a toolbar button?
6790 */
6791 if (menu->submenu_id == (HMENU)-1)
6792 {
6793 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6794 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6795 }
6796 else
6797#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006798 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6799 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800
6801#ifdef FEAT_TEAROFF
6802 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6803 {
6804 WORD menuID;
6805 HWND menuHandle;
6806
6807 /*
6808 * A tearoff button has changed state.
6809 */
6810 if (menu->children == NULL)
6811 menuID = (WORD)(menu->id);
6812 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006813 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6815 if (menuHandle)
6816 EnableWindow(menuHandle, !grey);
6817
6818 }
6819#endif
6820}
6821
6822#endif /* FEAT_MENU */
6823
6824
6825/* define some macros used to make the dialogue creation more readable */
6826
6827#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6828#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006829#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830
6831#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6832/*
6833 * stuff for dialogs
6834 */
6835
6836/*
6837 * The callback routine used by all the dialogs. Very simple. First,
6838 * acknowledges the INITDIALOG message so that Windows knows to do standard
6839 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6840 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6841 * number.
6842 */
6843 static LRESULT CALLBACK
6844dialog_callback(
6845 HWND hwnd,
6846 UINT message,
6847 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006848 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849{
6850 if (message == WM_INITDIALOG)
6851 {
6852 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
6853 /* Set focus to the dialog. Set the default button, if specified. */
6854 (void)SetFocus(hwnd);
6855 if (dialog_default_button > IDCANCEL)
6856 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006857 else
6858 /* We don't have a default, set focus on another element of the
6859 * dialog window, probably the icon */
6860 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 return FALSE;
6862 }
6863
6864 if (message == WM_COMMAND)
6865 {
6866 int button = LOWORD(wParam);
6867
6868 /* Don't end the dialog if something was selected that was
6869 * not a button.
6870 */
6871 if (button >= DLG_NONBUTTON_CONTROL)
6872 return TRUE;
6873
6874 /* If the edit box exists, copy the string. */
6875 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006876 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006877# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006878 /* If the OS is Windows NT, and 'encoding' differs from active
6879 * codepage: use wide function and convert text. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006880 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02006881 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006882 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
6883 char_u *p;
6884
6885 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6886 p = utf16_to_enc(wp, NULL);
6887 vim_strncpy(s_textfield, p, IOSIZE);
6888 vim_free(p);
6889 vim_free(wp);
6890 }
6891 else
6892# endif
6893 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006894 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896
6897 /*
6898 * Need to check for IDOK because if the user just hits Return to
6899 * accept the default value, some reason this is what we get.
6900 */
6901 if (button == IDOK)
6902 {
6903 if (dialog_default_button > IDCANCEL)
6904 EndDialog(hwnd, dialog_default_button);
6905 }
6906 else
6907 EndDialog(hwnd, button - IDCANCEL);
6908 return TRUE;
6909 }
6910
6911 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6912 {
6913 EndDialog(hwnd, 0);
6914 return TRUE;
6915 }
6916 return FALSE;
6917}
6918
6919/*
6920 * Create a dialog dynamically from the parameter strings.
6921 * type = type of dialog (question, alert, etc.)
6922 * title = dialog title. may be NULL for default title.
6923 * message = text to display. Dialog sizes to accommodate it.
6924 * buttons = '\n' separated list of button captions, default first.
6925 * dfltbutton = number of default button.
6926 *
6927 * This routine returns 1 if the first button is pressed,
6928 * 2 for the second, etc.
6929 *
6930 * 0 indicates Esc was pressed.
6931 * -1 for unexpected error
6932 *
6933 * If stubbing out this fn, return 1.
6934 */
6935
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006936static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937{
6938 "IDR_VIM",
6939 "IDR_VIM_ERROR",
6940 "IDR_VIM_ALERT",
6941 "IDR_VIM_INFO",
6942 "IDR_VIM_QUESTION"
6943};
6944
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 int
6946gui_mch_dialog(
6947 int type,
6948 char_u *title,
6949 char_u *message,
6950 char_u *buttons,
6951 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006952 char_u *textfield,
6953 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954{
6955 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006956 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 int numButtons;
6958 int *buttonWidths, *buttonPositions;
6959 int buttonYpos;
6960 int nchar, i;
6961 DWORD lStyle;
6962 int dlgwidth = 0;
6963 int dlgheight;
6964 int editboxheight;
6965 int horizWidth = 0;
6966 int msgheight;
6967 char_u *pstart;
6968 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006969 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 char_u *tbuffer;
6971 RECT rect;
6972 HWND hwnd;
6973 HDC hdc;
6974 HFONT font, oldFont;
6975 TEXTMETRIC fontInfo;
6976 int fontHeight;
6977 int textWidth, minButtonWidth, messageWidth;
6978 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006979 int maxDialogHeight;
6980 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 int vertical;
6982 int dlgPaddingX;
6983 int dlgPaddingY;
6984#ifdef USE_SYSMENU_FONT
6985 LOGFONT lfSysmenu;
6986 int use_lfSysmenu = FALSE;
6987#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006988 garray_T ga;
6989 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990
6991#ifndef NO_CONSOLE
6992 /* Don't output anything in silent mode ("ex -s") */
6993 if (silent_mode)
6994 return dfltbutton; /* return default option */
6995#endif
6996
Bram Moolenaar748bf032005-02-02 23:04:36 +00006997 if (s_hwnd == NULL)
6998 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999
7000 if ((type < 0) || (type > VIM_LAST_TYPE))
7001 type = 0;
7002
7003 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007004 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007005 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007006 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007
7008 if (p == NULL)
7009 return -1;
7010
7011 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007012 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7014 * const.
7015 */
7016 tbuffer = vim_strsave(buttons);
7017 if (tbuffer == NULL)
7018 return -1;
7019
7020 --dfltbutton; /* Change from one-based to zero-based */
7021
7022 /* Count buttons */
7023 numButtons = 1;
7024 for (i = 0; tbuffer[i] != '\0'; i++)
7025 {
7026 if (tbuffer[i] == DLG_BUTTON_SEP)
7027 numButtons++;
7028 }
7029 if (dfltbutton >= numButtons)
7030 dfltbutton = -1;
7031
7032 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007033 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 if (buttonWidths == NULL)
7035 return -1;
7036
7037 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007038 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 if (buttonPositions == NULL)
7040 return -1;
7041
7042 /*
7043 * Calculate how big the dialog must be.
7044 */
7045 hwnd = GetDesktopWindow();
7046 hdc = GetWindowDC(hwnd);
7047#ifdef USE_SYSMENU_FONT
7048 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7049 {
7050 font = CreateFontIndirect(&lfSysmenu);
7051 use_lfSysmenu = TRUE;
7052 }
7053 else
7054#endif
7055 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7056 VARIABLE_PITCH , DLG_FONT_NAME);
7057 if (s_usenewlook)
7058 {
7059 oldFont = SelectFont(hdc, font);
7060 dlgPaddingX = DLG_PADDING_X;
7061 dlgPaddingY = DLG_PADDING_Y;
7062 }
7063 else
7064 {
7065 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7066 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7067 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7068 }
7069 GetTextMetrics(hdc, &fontInfo);
7070 fontHeight = fontInfo.tmHeight;
7071
7072 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007073 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074
7075 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007076 if (s_hwnd == NULL)
7077 {
7078 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
Bram Moolenaarc716c302006-01-21 22:12:51 +00007080 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007081 get_work_area(&workarea_rect);
7082 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7083 if (maxDialogWidth > 600)
7084 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007085 /* Leave some room for the taskbar. */
7086 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007087 }
7088 else
7089 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02007090 /* Use our own window for the size, unless it's very small. */
7091 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007092 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02007093 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007094 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007095 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
7096 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007097
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007098 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007099 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007100 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02007101 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007102 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
7103 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
7104 }
7105
7106 /* Set dlgwidth to width of message.
7107 * Copy the message into "ga", changing NL to CR-NL and inserting line
7108 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 pstart = message;
7110 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007111 msgheight = 0;
7112 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 do
7114 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007115 msgheight += fontHeight; /* at least one line */
7116
7117 /* Need to figure out where to break the string. The system does it
7118 * at a word boundary, which would mean we can't compute the number of
7119 * wrapped lines. */
7120 textWidth = 0;
7121 last_white = NULL;
7122 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007123 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007124#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007125 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007126#else
7127 l = 1;
7128#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01007129 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007130 && textWidth > maxDialogWidth * 3 / 4)
7131 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007132 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007133 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007134 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007135 /* Line will wrap. */
7136 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007137 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007138 textWidth = 0;
7139
7140 if (last_white != NULL)
7141 {
7142 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007143 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007144 pend = last_white + 1;
7145 last_white = NULL;
7146 }
7147 ga_append(&ga, '\r');
7148 ga_append(&ga, '\n');
7149 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007150 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007151
7152 while (--l >= 0)
7153 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007154 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007155 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007157
7158 ga_append(&ga, '\r');
7159 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 pstart = pend + 1;
7161 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007162
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007163 if (ga.ga_data != NULL)
7164 message = ga.ga_data;
7165
Bram Moolenaar748bf032005-02-02 23:04:36 +00007166 messageWidth += 10; /* roundoff space */
7167
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02007169 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7170 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171
7172 if (msgheight < DLG_ICON_HEIGHT)
7173 msgheight = DLG_ICON_HEIGHT;
7174
7175 /*
7176 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007177 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007179 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 if (!vertical)
7181 {
7182 // Place buttons horizontally if they fit.
7183 horizWidth = dlgPaddingX;
7184 pstart = tbuffer;
7185 i = 0;
7186 do
7187 {
7188 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7189 if (pend == NULL)
7190 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007191 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 if (textWidth < minButtonWidth)
7193 textWidth = minButtonWidth;
7194 textWidth += dlgPaddingX; /* Padding within button */
7195 buttonWidths[i] = textWidth;
7196 buttonPositions[i++] = horizWidth;
7197 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7198 pstart = pend + 1;
7199 } while (*pend != NUL);
7200
7201 if (horizWidth > maxDialogWidth)
7202 vertical = TRUE; // Too wide to fit on the screen.
7203 else if (horizWidth > dlgwidth)
7204 dlgwidth = horizWidth;
7205 }
7206
7207 if (vertical)
7208 {
7209 // Stack buttons vertically.
7210 pstart = tbuffer;
7211 do
7212 {
7213 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7214 if (pend == NULL)
7215 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007216 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 textWidth += dlgPaddingX; /* Padding within button */
7218 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7219 if (textWidth > dlgwidth)
7220 dlgwidth = textWidth;
7221 pstart = pend + 1;
7222 } while (*pend != NUL);
7223 }
7224
7225 if (dlgwidth < DLG_MIN_WIDTH)
7226 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7227
7228 /* start to fill in the dlgtemplate information. addressing by WORDs */
7229 if (s_usenewlook)
7230 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7231 else
7232 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7233
7234 add_long(lStyle);
7235 add_long(0); // (lExtendedStyle)
7236 pnumitems = p; /*save where the number of items must be stored*/
7237 add_word(0); // NumberOfItems(will change later)
7238 add_word(10); // x
7239 add_word(10); // y
7240 add_word(PixelToDialogX(dlgwidth)); // cx
7241
7242 // Dialog height.
7243 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007244 dlgheight = msgheight + 2 * dlgPaddingY
7245 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 else
7247 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7248
7249 // Dialog needs to be taller if contains an edit box.
7250 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7251 if (textfield != NULL)
7252 dlgheight += editboxheight;
7253
Bram Moolenaara95d8232013-08-07 15:27:11 +02007254 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7255 if (dlgheight > maxDialogHeight)
7256 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007257 msgheight = msgheight - (dlgheight - maxDialogHeight);
7258 dlgheight = maxDialogHeight;
7259 scroll_flag = WS_VSCROLL;
7260 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7261 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007262 }
7263
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 add_word(PixelToDialogY(dlgheight));
7265
7266 add_word(0); // Menu
7267 add_word(0); // Class
7268
7269 /* copy the title of the dialog */
7270 nchar = nCopyAnsiToWideChar(p, (title ?
7271 (LPSTR)title :
7272 (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
7273 p += nchar;
7274
7275 if (s_usenewlook)
7276 {
7277 /* do the font, since DS_3DLOOK doesn't work properly */
7278#ifdef USE_SYSMENU_FONT
7279 if (use_lfSysmenu)
7280 {
7281 /* point size */
7282 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7283 GetDeviceCaps(hdc, LOGPIXELSY));
7284 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
7285 }
7286 else
7287#endif
7288 {
7289 *p++ = DLG_FONT_POINT_SIZE; // point size
7290 nchar = nCopyAnsiToWideChar(p, TEXT(DLG_FONT_NAME));
7291 }
7292 p += nchar;
7293 }
7294
7295 buttonYpos = msgheight + 2 * dlgPaddingY;
7296
7297 if (textfield != NULL)
7298 buttonYpos += editboxheight;
7299
7300 pstart = tbuffer;
7301 if (!vertical)
7302 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7303 for (i = 0; i < numButtons; i++)
7304 {
7305 /* get end of this button. */
7306 for ( pend = pstart;
7307 *pend && (*pend != DLG_BUTTON_SEP);
7308 pend++)
7309 ;
7310
7311 if (*pend)
7312 *pend = '\0';
7313
7314 /*
7315 * old NOTE:
7316 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7317 * the focus to the first tab-able button and in so doing makes that
7318 * the default!! Grrr. Workaround: Make the default button the only
7319 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7320 * he/she can use arrow keys.
7321 *
7322 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007323 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 * dialog. Also needed for when the textfield is the default control.
7325 * It appears to work now (perhaps not on Win95?).
7326 */
7327 if (vertical)
7328 {
7329 p = add_dialog_element(p,
7330 (i == dfltbutton
7331 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7332 PixelToDialogX(DLG_VERT_PADDING_X),
7333 PixelToDialogY(buttonYpos /* TBK */
7334 + 2 * fontHeight * i),
7335 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7336 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007337 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 }
7339 else
7340 {
7341 p = add_dialog_element(p,
7342 (i == dfltbutton
7343 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7344 PixelToDialogX(horizWidth + buttonPositions[i]),
7345 PixelToDialogY(buttonYpos), /* TBK */
7346 PixelToDialogX(buttonWidths[i]),
7347 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007348 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 }
7350 pstart = pend + 1; /*next button*/
7351 }
7352 *pnumitems += numButtons;
7353
7354 /* Vim icon */
7355 p = add_dialog_element(p, SS_ICON,
7356 PixelToDialogX(dlgPaddingX),
7357 PixelToDialogY(dlgPaddingY),
7358 PixelToDialogX(DLG_ICON_WIDTH),
7359 PixelToDialogY(DLG_ICON_HEIGHT),
7360 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7361 dlg_icons[type]);
7362
Bram Moolenaar748bf032005-02-02 23:04:36 +00007363 /* Dialog message */
7364 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7365 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7366 PixelToDialogY(dlgPaddingY),
7367 (WORD)(PixelToDialogX(messageWidth) + 1),
7368 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007369 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370
7371 /* Edit box */
7372 if (textfield != NULL)
7373 {
7374 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7375 PixelToDialogX(2 * dlgPaddingX),
7376 PixelToDialogY(2 * dlgPaddingY + msgheight),
7377 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7378 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007379 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 *pnumitems += 1;
7381 }
7382
7383 *pnumitems += 2;
7384
7385 SelectFont(hdc, oldFont);
7386 DeleteObject(font);
7387 ReleaseDC(hwnd, hdc);
7388
7389 /* Let the dialog_callback() function know which button to make default
7390 * If we have an edit box, make that the default. We also need to tell
7391 * dialog_callback() if this dialog contains an edit box or not. We do
7392 * this by setting s_textfield if it does.
7393 */
7394 if (textfield != NULL)
7395 {
7396 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7397 s_textfield = textfield;
7398 }
7399 else
7400 {
7401 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7402 s_textfield = NULL;
7403 }
7404
7405 /* show the dialog box modally and get a return value */
7406 nchar = (int)DialogBoxIndirect(
7407 s_hinst,
7408 (LPDLGTEMPLATE)pdlgtemplate,
7409 s_hwnd,
7410 (DLGPROC)dialog_callback);
7411
7412 LocalFree(LocalHandle(pdlgtemplate));
7413 vim_free(tbuffer);
7414 vim_free(buttonWidths);
7415 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007416 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417
7418 /* Focus back to our window (for when MDI is used). */
7419 (void)SetFocus(s_hwnd);
7420
7421 return nchar;
7422}
7423
7424#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007425
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426/*
7427 * Put a simple element (basic class) onto a dialog template in memory.
7428 * return a pointer to where the next item should be added.
7429 *
7430 * parameters:
7431 * lStyle = additional style flags
7432 * (be careful, NT3.51 & Win32s will ignore the new ones)
7433 * x,y = x & y positions IN DIALOG UNITS
7434 * w,h = width and height IN DIALOG UNITS
7435 * Id = ID used in messages
7436 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7437 * caption = usually text or resource name
7438 *
7439 * TODO: use the length information noted here to enable the dialog creation
7440 * routines to work out more exactly how much memory they need to alloc.
7441 */
7442 static PWORD
7443add_dialog_element(
7444 PWORD p,
7445 DWORD lStyle,
7446 WORD x,
7447 WORD y,
7448 WORD w,
7449 WORD h,
7450 WORD Id,
7451 WORD clss,
7452 const char *caption)
7453{
7454 int nchar;
7455
7456 p = lpwAlign(p); /* Align to dword boundary*/
7457 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7458 *p++ = LOWORD(lStyle);
7459 *p++ = HIWORD(lStyle);
7460 *p++ = 0; // LOWORD (lExtendedStyle)
7461 *p++ = 0; // HIWORD (lExtendedStyle)
7462 *p++ = x;
7463 *p++ = y;
7464 *p++ = w;
7465 *p++ = h;
7466 *p++ = Id; //9 or 10 words in all
7467
7468 *p++ = (WORD)0xffff;
7469 *p++ = clss; //2 more here
7470
7471 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption); //strlen(caption)+1
7472 p += nchar;
7473
7474 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7475
7476 return p; //total = 15+ (strlen(caption)) words
7477 // = 30 + 2(strlen(caption) bytes reqd
7478}
7479
7480
7481/*
7482 * Helper routine. Take an input pointer, return closest pointer that is
7483 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7484 */
7485 static LPWORD
7486lpwAlign(
7487 LPWORD lpIn)
7488{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007489 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007491 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 ul += 3;
7493 ul >>= 2;
7494 ul <<= 2;
7495 return (LPWORD)ul;
7496}
7497
7498/*
7499 * Helper routine. Takes second parameter as Ansi string, copies it to first
7500 * parameter as wide character (16-bits / char) string, and returns integer
7501 * number of wide characters (words) in string (including the trailing wide
7502 * char NULL). Partly taken from the Win32SDK samples.
7503 */
7504 static int
7505nCopyAnsiToWideChar(
7506 LPWORD lpWCStr,
7507 LPSTR lpAnsiIn)
7508{
7509 int nChar = 0;
7510#ifdef FEAT_MBYTE
7511 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7512 int i;
7513 WCHAR *wn;
7514
7515 if (enc_codepage == 0 && (int)GetACP() != enc_codepage)
7516 {
7517 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007518 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 if (wn != NULL)
7520 {
7521 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007522 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 vim_free(wn);
7524 }
7525 }
7526 if (nChar == 0)
7527 /* Use Win32 conversion function. */
7528 nChar = MultiByteToWideChar(
7529 enc_codepage > 0 ? enc_codepage : CP_ACP,
7530 MB_PRECOMPOSED,
7531 lpAnsiIn, len,
7532 lpWCStr, len);
7533 for (i = 0; i < nChar; ++i)
7534 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7535 lpWCStr[i] = (WORD)' ';
7536#else
7537 do
7538 {
7539 if (*lpAnsiIn == '\t')
7540 *lpWCStr++ = (WORD)' ';
7541 else
7542 *lpWCStr++ = (WORD)*lpAnsiIn;
7543 nChar++;
7544 } while (*lpAnsiIn++);
7545#endif
7546
7547 return nChar;
7548}
7549
7550
7551#ifdef FEAT_TEAROFF
7552/*
7553 * The callback function for all the modeless dialogs that make up the
7554 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7555 * thinking its menus have been clicked), and go away when closed.
7556 */
7557 static LRESULT CALLBACK
7558tearoff_callback(
7559 HWND hwnd,
7560 UINT message,
7561 WPARAM wParam,
7562 LPARAM lParam)
7563{
7564 if (message == WM_INITDIALOG)
7565 return (TRUE);
7566
7567 /* May show the mouse pointer again. */
7568 HandleMouseHide(message, lParam);
7569
7570 if (message == WM_COMMAND)
7571 {
7572 if ((WORD)(LOWORD(wParam)) & 0x8000)
7573 {
7574 POINT mp;
7575 RECT rect;
7576
7577 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7578 {
7579 (void)TrackPopupMenu(
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007580 (HMENU)(long_u)(LOWORD(wParam) ^ 0x8000),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7582 (int)rect.right - 8,
7583 (int)mp.y,
7584 (int)0, /*reserved param*/
7585 s_hwnd,
7586 NULL);
7587 /*
7588 * NOTE: The pop-up menu can eat the mouse up event.
7589 * We deal with this in normal.c.
7590 */
7591 }
7592 }
7593 else
7594 /* Pass on messages to the main Vim window */
7595 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7596 /*
7597 * Give main window the focus back: this is so after
7598 * choosing a tearoff button you can start typing again
7599 * straight away.
7600 */
7601 (void)SetFocus(s_hwnd);
7602 return TRUE;
7603 }
7604 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7605 {
7606 DestroyWindow(hwnd);
7607 return TRUE;
7608 }
7609
7610 /* When moved around, give main window the focus back. */
7611 if (message == WM_EXITSIZEMOVE)
7612 (void)SetActiveWindow(s_hwnd);
7613
7614 return FALSE;
7615}
7616#endif
7617
7618
7619/*
7620 * Decide whether to use the "new look" (small, non-bold font) or the "old
7621 * look" (big, clanky font) for dialogs, and work out a few values for use
7622 * later accordingly.
7623 */
7624 static void
7625get_dialog_font_metrics(void)
7626{
7627 HDC hdc;
7628 HFONT hfontTools = 0;
7629 DWORD dlgFontSize;
7630 SIZE size;
7631#ifdef USE_SYSMENU_FONT
7632 LOGFONT lfSysmenu;
7633#endif
7634
7635 s_usenewlook = FALSE;
7636
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007638 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7639 hfontTools = CreateFontIndirect(&lfSysmenu);
7640 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641#endif
7642 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7643 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7644
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007645 if (hfontTools)
7646 {
7647 hdc = GetDC(s_hwnd);
7648 SelectObject(hdc, hfontTools);
7649 /*
7650 * GetTextMetrics() doesn't return the right value in
7651 * tmAveCharWidth, so we have to figure out the dialog base units
7652 * ourselves.
7653 */
7654 GetTextExtentPoint(hdc,
7655 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7656 52, &size);
7657 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007659 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7660 s_dlgfntheight = (WORD)size.cy;
7661 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 }
7663
7664 if (!s_usenewlook)
7665 {
7666 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7667 s_dlgfntwidth = LOWORD(dlgFontSize);
7668 s_dlgfntheight = HIWORD(dlgFontSize);
7669 }
7670}
7671
7672#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7673/*
7674 * Create a pseudo-"tearoff menu" based on the child
7675 * items of a given menu pointer.
7676 */
7677 static void
7678gui_mch_tearoff(
7679 char_u *title,
7680 vimmenu_T *menu,
7681 int initX,
7682 int initY)
7683{
7684 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7685 int template_len;
7686 int nchar, textWidth, submenuWidth;
7687 DWORD lStyle;
7688 DWORD lExtendedStyle;
7689 WORD dlgwidth;
7690 WORD menuID;
7691 vimmenu_T *pmenu;
7692 vimmenu_T *the_menu = menu;
7693 HWND hwnd;
7694 HDC hdc;
7695 HFONT font, oldFont;
7696 int col, spaceWidth, len;
7697 int columnWidths[2];
7698 char_u *label, *text;
7699 int acLen = 0;
7700 int nameLen;
7701 int padding0, padding1, padding2 = 0;
7702 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007703 int x;
7704 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705#ifdef USE_SYSMENU_FONT
7706 LOGFONT lfSysmenu;
7707 int use_lfSysmenu = FALSE;
7708#endif
7709
7710 /*
7711 * If this menu is already torn off, move it to the mouse position.
7712 */
7713 if (IsWindow(menu->tearoff_handle))
7714 {
7715 POINT mp;
7716 if (GetCursorPos((LPPOINT)&mp))
7717 {
7718 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7719 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7720 }
7721 return;
7722 }
7723
7724 /*
7725 * Create a new tearoff.
7726 */
7727 if (*title == MNU_HIDDEN_CHAR)
7728 title++;
7729
7730 /* Allocate memory to store the dialog template. It's made bigger when
7731 * needed. */
7732 template_len = DLG_ALLOC_SIZE;
7733 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7734 if (p == NULL)
7735 return;
7736
7737 hwnd = GetDesktopWindow();
7738 hdc = GetWindowDC(hwnd);
7739#ifdef USE_SYSMENU_FONT
7740 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7741 {
7742 font = CreateFontIndirect(&lfSysmenu);
7743 use_lfSysmenu = TRUE;
7744 }
7745 else
7746#endif
7747 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7748 VARIABLE_PITCH , DLG_FONT_NAME);
7749 if (s_usenewlook)
7750 oldFont = SelectFont(hdc, font);
7751 else
7752 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7753
7754 /* Calculate width of a single space. Used for padding columns to the
7755 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007756 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757
7758 /* Figure out max width of the text column, the accelerator column and the
7759 * optional submenu column. */
7760 submenuWidth = 0;
7761 for (col = 0; col < 2; col++)
7762 {
7763 columnWidths[col] = 0;
7764 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7765 {
7766 /* Use "dname" here to compute the width of the visible text. */
7767 text = (col == 0) ? pmenu->dname : pmenu->actext;
7768 if (text != NULL && *text != NUL)
7769 {
7770 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7771 if (textWidth > columnWidths[col])
7772 columnWidths[col] = textWidth;
7773 }
7774 if (pmenu->children != NULL)
7775 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7776 }
7777 }
7778 if (columnWidths[1] == 0)
7779 {
7780 /* no accelerators */
7781 if (submenuWidth != 0)
7782 columnWidths[0] += submenuWidth;
7783 else
7784 columnWidths[0] += spaceWidth;
7785 }
7786 else
7787 {
7788 /* there is an accelerator column */
7789 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7790 columnWidths[1] += submenuWidth;
7791 }
7792
7793 /*
7794 * Now find the total width of our 'menu'.
7795 */
7796 textWidth = columnWidths[0] + columnWidths[1];
7797 if (submenuWidth != 0)
7798 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007799 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7801 textWidth += submenuWidth;
7802 }
7803 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7804 if (textWidth > dlgwidth)
7805 dlgwidth = textWidth;
7806 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7807
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 /* start to fill in the dlgtemplate information. addressing by WORDs */
7809 if (s_usenewlook)
7810 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7811 else
7812 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7813
7814 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7815 *p++ = LOWORD(lStyle);
7816 *p++ = HIWORD(lStyle);
7817 *p++ = LOWORD(lExtendedStyle);
7818 *p++ = HIWORD(lExtendedStyle);
7819 pnumitems = p; /* save where the number of items must be stored */
7820 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007821 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007823 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 else
7825 *p++ = PixelToDialogX(initX); // x
7826 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007827 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 else
7829 *p++ = PixelToDialogY(initY); // y
7830 *p++ = PixelToDialogX(dlgwidth); // cx
7831 ptrueheight = p;
7832 *p++ = 0; // dialog height: changed later anyway
7833 *p++ = 0; // Menu
7834 *p++ = 0; // Class
7835
7836 /* copy the title of the dialog */
7837 nchar = nCopyAnsiToWideChar(p, ((*title)
7838 ? (LPSTR)title
7839 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
7840 p += nchar;
7841
7842 if (s_usenewlook)
7843 {
7844 /* do the font, since DS_3DLOOK doesn't work properly */
7845#ifdef USE_SYSMENU_FONT
7846 if (use_lfSysmenu)
7847 {
7848 /* point size */
7849 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7850 GetDeviceCaps(hdc, LOGPIXELSY));
7851 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
7852 }
7853 else
7854#endif
7855 {
7856 *p++ = DLG_FONT_POINT_SIZE; // point size
7857 nchar = nCopyAnsiToWideChar (p, TEXT(DLG_FONT_NAME));
7858 }
7859 p += nchar;
7860 }
7861
7862 /*
7863 * Loop over all the items in the menu.
7864 * But skip over the tearbar.
7865 */
7866 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7867 menu = menu->children->next;
7868 else
7869 menu = menu->children;
7870 for ( ; menu != NULL; menu = menu->next)
7871 {
7872 if (menu->modes == 0) /* this menu has just been deleted */
7873 continue;
7874 if (menu_is_separator(menu->dname))
7875 {
7876 sepPadding += 3;
7877 continue;
7878 }
7879
7880 /* Check if there still is plenty of room in the template. Make it
7881 * larger when needed. */
7882 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7883 {
7884 WORD *newp;
7885
7886 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7887 if (newp != NULL)
7888 {
7889 template_len += 4096;
7890 mch_memmove(newp, pdlgtemplate,
7891 (char *)p - (char *)pdlgtemplate);
7892 p = newp + (p - pdlgtemplate);
7893 pnumitems = newp + (pnumitems - pdlgtemplate);
7894 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7895 LocalFree(LocalHandle(pdlgtemplate));
7896 pdlgtemplate = newp;
7897 }
7898 }
7899
7900 /* Figure out minimal length of this menu label. Use "name" for the
7901 * actual text, "dname" for estimating the displayed size. "name"
7902 * has "&a" for mnemonic and includes the accelerator. */
7903 len = nameLen = (int)STRLEN(menu->name);
7904 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7905 (int)STRLEN(menu->dname))) / spaceWidth;
7906 len += padding0;
7907
7908 if (menu->actext != NULL)
7909 {
7910 acLen = (int)STRLEN(menu->actext);
7911 len += acLen;
7912 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7913 }
7914 else
7915 textWidth = 0;
7916 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7917 len += padding1;
7918
7919 if (menu->children == NULL)
7920 {
7921 padding2 = submenuWidth / spaceWidth;
7922 len += padding2;
7923 menuID = (WORD)(menu->id);
7924 }
7925 else
7926 {
7927 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007928 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 }
7930
7931 /* Allocate menu label and fill it in */
7932 text = label = alloc((unsigned)len + 1);
7933 if (label == NULL)
7934 break;
7935
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007936 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 text = vim_strchr(text, TAB); /* stop at TAB before actext */
7938 if (text == NULL)
7939 text = label + nameLen; /* no actext, use whole name */
7940 while (padding0-- > 0)
7941 *text++ = ' ';
7942 if (menu->actext != NULL)
7943 {
7944 STRNCPY(text, menu->actext, acLen);
7945 text += acLen;
7946 }
7947 while (padding1-- > 0)
7948 *text++ = ' ';
7949 if (menu->children != NULL)
7950 {
7951 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7952 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7953 }
7954 else
7955 {
7956 while (padding2-- > 0)
7957 *text++ = ' ';
7958 }
7959 *text = NUL;
7960
7961 /*
7962 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7963 * W95/NT4 it makes the tear-off look more like a menu.
7964 */
7965 p = add_dialog_element(p,
7966 BS_PUSHBUTTON|BS_LEFT,
7967 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7968 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7969 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7970 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007971 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 vim_free(label);
7973 (*pnumitems)++;
7974 }
7975
7976 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7977
7978
7979 /* show modelessly */
7980 the_menu->tearoff_handle = CreateDialogIndirect(
7981 s_hinst,
7982 (LPDLGTEMPLATE)pdlgtemplate,
7983 s_hwnd,
7984 (DLGPROC)tearoff_callback);
7985
7986 LocalFree(LocalHandle(pdlgtemplate));
7987 SelectFont(hdc, oldFont);
7988 DeleteObject(font);
7989 ReleaseDC(hwnd, hdc);
7990
7991 /*
7992 * Reassert ourselves as the active window. This is so that after creating
7993 * a tearoff, the user doesn't have to click with the mouse just to start
7994 * typing again!
7995 */
7996 (void)SetActiveWindow(s_hwnd);
7997
7998 /* make sure the right buttons are enabled */
7999 force_menu_update = TRUE;
8000}
8001#endif
8002
8003#if defined(FEAT_TOOLBAR) || defined(PROTO)
8004#include "gui_w32_rc.h"
8005
8006/* This not defined in older SDKs */
8007# ifndef TBSTYLE_FLAT
8008# define TBSTYLE_FLAT 0x0800
8009# endif
8010
8011/*
8012 * Create the toolbar, initially unpopulated.
8013 * (just like the menu, there are no defaults, it's all
8014 * set up through menu.vim)
8015 */
8016 static void
8017initialise_toolbar(void)
8018{
8019 InitCommonControls();
8020 s_toolbarhwnd = CreateToolbarEx(
8021 s_hwnd,
8022 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8023 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008024 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 s_hinst,
8026 IDR_TOOLBAR1, // id of initial bitmap
8027 NULL,
8028 0, // initial number of buttons
8029 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8030 TOOLBAR_BUTTON_HEIGHT,
8031 TOOLBAR_BUTTON_WIDTH,
8032 TOOLBAR_BUTTON_HEIGHT,
8033 sizeof(TBBUTTON)
8034 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008035 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036
8037 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
8038}
8039
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008040 static LRESULT CALLBACK
8041toolbar_wndproc(
8042 HWND hwnd,
8043 UINT uMsg,
8044 WPARAM wParam,
8045 LPARAM lParam)
8046{
8047 HandleMouseHide(uMsg, lParam);
8048 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8049}
8050
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 static int
8052get_toolbar_bitmap(vimmenu_T *menu)
8053{
8054 int i = -1;
8055
8056 /*
8057 * Check user bitmaps first, unless builtin is specified.
8058 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008059 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 {
8061 char_u fname[MAXPATHL];
8062 HANDLE hbitmap = NULL;
8063
8064 if (menu->iconfile != NULL)
8065 {
8066 gui_find_iconfile(menu->iconfile, fname, "bmp");
8067 hbitmap = LoadImage(
8068 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008069 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 IMAGE_BITMAP,
8071 TOOLBAR_BUTTON_WIDTH,
8072 TOOLBAR_BUTTON_HEIGHT,
8073 LR_LOADFROMFILE |
8074 LR_LOADMAP3DCOLORS
8075 );
8076 }
8077
8078 /*
8079 * If the LoadImage call failed, or the "icon=" file
8080 * didn't exist or wasn't specified, try the menu name
8081 */
8082 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008083 && (gui_find_bitmap(
8084#ifdef FEAT_MULTI_LANG
8085 menu->en_dname != NULL ? menu->en_dname :
8086#endif
8087 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 hbitmap = LoadImage(
8089 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008090 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 IMAGE_BITMAP,
8092 TOOLBAR_BUTTON_WIDTH,
8093 TOOLBAR_BUTTON_HEIGHT,
8094 LR_LOADFROMFILE |
8095 LR_LOADMAP3DCOLORS
8096 );
8097
8098 if (hbitmap != NULL)
8099 {
8100 TBADDBITMAP tbAddBitmap;
8101
8102 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008103 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104
8105 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8106 (WPARAM)1, (LPARAM)&tbAddBitmap);
8107 /* i will be set to -1 if it fails */
8108 }
8109 }
8110 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8111 i = menu->iconidx;
8112
8113 return i;
8114}
8115#endif
8116
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008117#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8118 static void
8119initialise_tabline(void)
8120{
8121 InitCommonControls();
8122
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008123 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008124 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008125 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8126 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008127 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008128
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008129 gui.tabline_height = TABLINE_HEIGHT;
8130
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008131# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008132 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008133# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008134}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008135
8136 static LRESULT CALLBACK
8137tabline_wndproc(
8138 HWND hwnd,
8139 UINT uMsg,
8140 WPARAM wParam,
8141 LPARAM lParam)
8142{
8143 HandleMouseHide(uMsg, lParam);
8144 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8145}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008146#endif
8147
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8149/*
8150 * Make the GUI window come to the foreground.
8151 */
8152 void
8153gui_mch_set_foreground(void)
8154{
8155 if (IsIconic(s_hwnd))
8156 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8157 SetForegroundWindow(s_hwnd);
8158}
8159#endif
8160
8161#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8162 static void
8163dyn_imm_load(void)
8164{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008165 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 if (hLibImm == NULL)
8167 return;
8168
8169 pImmGetCompositionStringA
8170 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8171 pImmGetCompositionStringW
8172 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8173 pImmGetContext
8174 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8175 pImmAssociateContext
8176 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8177 pImmReleaseContext
8178 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8179 pImmGetOpenStatus
8180 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8181 pImmSetOpenStatus
8182 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
8183 pImmGetCompositionFont
8184 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
8185 pImmSetCompositionFont
8186 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
8187 pImmSetCompositionWindow
8188 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8189 pImmGetConversionStatus
8190 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008191 pImmSetConversionStatus
8192 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193
8194 if ( pImmGetCompositionStringA == NULL
8195 || pImmGetCompositionStringW == NULL
8196 || pImmGetContext == NULL
8197 || pImmAssociateContext == NULL
8198 || pImmReleaseContext == NULL
8199 || pImmGetOpenStatus == NULL
8200 || pImmSetOpenStatus == NULL
8201 || pImmGetCompositionFont == NULL
8202 || pImmSetCompositionFont == NULL
8203 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008204 || pImmGetConversionStatus == NULL
8205 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 {
8207 FreeLibrary(hLibImm);
8208 hLibImm = NULL;
8209 pImmGetContext = NULL;
8210 return;
8211 }
8212
8213 return;
8214}
8215
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216#endif
8217
8218#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8219
8220# ifdef FEAT_XPM_W32
8221# define IMAGE_XPM 100
8222# endif
8223
8224typedef struct _signicon_t
8225{
8226 HANDLE hImage;
8227 UINT uType;
8228#ifdef FEAT_XPM_W32
8229 HANDLE hShape; /* Mask bitmap handle */
8230#endif
8231} signicon_t;
8232
8233 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008234gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235{
8236 signicon_t *sign;
8237 int x, y, w, h;
8238
8239 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8240 return;
8241
8242 x = TEXT_X(col);
8243 y = TEXT_Y(row);
8244 w = gui.char_width * 2;
8245 h = gui.char_height;
8246 switch (sign->uType)
8247 {
8248 case IMAGE_BITMAP:
8249 {
8250 HDC hdcMem;
8251 HBITMAP hbmpOld;
8252
8253 hdcMem = CreateCompatibleDC(s_hdc);
8254 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8255 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8256 SelectObject(hdcMem, hbmpOld);
8257 DeleteDC(hdcMem);
8258 }
8259 break;
8260 case IMAGE_ICON:
8261 case IMAGE_CURSOR:
8262 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8263 break;
8264#ifdef FEAT_XPM_W32
8265 case IMAGE_XPM:
8266 {
8267 HDC hdcMem;
8268 HBITMAP hbmpOld;
8269
8270 hdcMem = CreateCompatibleDC(s_hdc);
8271 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8272 /* Make hole */
8273 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8274
8275 SelectObject(hdcMem, sign->hImage);
8276 /* Paint sign */
8277 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8278 SelectObject(hdcMem, hbmpOld);
8279 DeleteDC(hdcMem);
8280 }
8281 break;
8282#endif
8283 }
8284}
8285
8286 static void
8287close_signicon_image(signicon_t *sign)
8288{
8289 if (sign)
8290 switch (sign->uType)
8291 {
8292 case IMAGE_BITMAP:
8293 DeleteObject((HGDIOBJ)sign->hImage);
8294 break;
8295 case IMAGE_CURSOR:
8296 DestroyCursor((HCURSOR)sign->hImage);
8297 break;
8298 case IMAGE_ICON:
8299 DestroyIcon((HICON)sign->hImage);
8300 break;
8301#ifdef FEAT_XPM_W32
8302 case IMAGE_XPM:
8303 DeleteObject((HBITMAP)sign->hImage);
8304 DeleteObject((HBITMAP)sign->hShape);
8305 break;
8306#endif
8307 }
8308}
8309
8310 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008311gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312{
8313 signicon_t sign, *psign;
8314 char_u *ext;
8315
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008317 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 if (ext > signfile)
8319 {
8320 int do_load = 1;
8321
8322 if (!STRICMP(ext, ".bmp"))
8323 sign.uType = IMAGE_BITMAP;
8324 else if (!STRICMP(ext, ".ico"))
8325 sign.uType = IMAGE_ICON;
8326 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8327 sign.uType = IMAGE_CURSOR;
8328 else
8329 do_load = 0;
8330
8331 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008332 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 gui.char_width * 2, gui.char_height,
8334 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8335#ifdef FEAT_XPM_W32
8336 if (!STRICMP(ext, ".xpm"))
8337 {
8338 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008339 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8340 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 }
8342#endif
8343 }
8344
8345 psign = NULL;
8346 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8347 != NULL)
8348 *psign = sign;
8349
8350 if (!psign)
8351 {
8352 if (sign.hImage)
8353 close_signicon_image(&sign);
8354 EMSG(_(e_signdata));
8355 }
8356 return (void *)psign;
8357
8358}
8359
8360 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008361gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362{
8363 if (sign)
8364 {
8365 close_signicon_image((signicon_t *)sign);
8366 vim_free(sign);
8367 }
8368}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370
8371#if defined(FEAT_BEVAL) || defined(PROTO)
8372
8373/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008374 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 *
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00008376 * The only reused thing is gui_beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8378 * to get current mouse position).
8379 *
8380 * Trying to use as more Windows services as possible, and as less
8381 * IE version as possible :)).
8382 *
8383 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8384 * BalloonEval struct.
8385 * 2) Enable/Disable simply create/kill BalloonEval Timer
8386 * 3) When there was enough inactivity, timer procedure posts
8387 * async request to debugger
8388 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8389 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008390 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 */
8392
Bram Moolenaar45360022005-07-21 21:08:21 +00008393/*
8394 * determine whether installed Common Controls support multiline tooltips
8395 * (i.e. their version is >= 4.70
8396 */
8397 int
8398multiline_balloon_available(void)
8399{
8400 HINSTANCE hDll;
8401 static char comctl_dll[] = "comctl32.dll";
8402 static int multiline_tip = MAYBE;
8403
8404 if (multiline_tip != MAYBE)
8405 return multiline_tip;
8406
8407 hDll = GetModuleHandle(comctl_dll);
8408 if (hDll != NULL)
8409 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008410 DLLGETVERSIONPROC pGetVer;
8411 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008412
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008413 if (pGetVer != NULL)
8414 {
8415 DLLVERSIONINFO dvi;
8416 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008417
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008418 ZeroMemory(&dvi, sizeof(dvi));
8419 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008420
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008421 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008422
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008423 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008424 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008425 || (dvi.dwMajorVersion == 4
8426 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008427 {
8428 multiline_tip = TRUE;
8429 return multiline_tip;
8430 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008431 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008432 else
8433 {
8434 /* there is chance we have ancient CommCtl 4.70
8435 which doesn't export DllGetVersion */
8436 DWORD dwHandle = 0;
8437 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8438 if (len > 0)
8439 {
8440 VS_FIXEDFILEINFO *ver;
8441 UINT vlen = 0;
8442 void *data = alloc(len);
8443
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008444 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008445 && GetFileVersionInfo(comctl_dll, 0, len, data)
8446 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8447 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008448 && HIWORD(ver->dwFileVersionMS) > 4)
8449 || ((HIWORD(ver->dwFileVersionMS) == 4
8450 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008451 {
8452 vim_free(data);
8453 multiline_tip = TRUE;
8454 return multiline_tip;
8455 }
8456 vim_free(data);
8457 }
8458 }
8459 }
8460 multiline_tip = FALSE;
8461 return multiline_tip;
8462}
8463
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008465make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466{
Bram Moolenaar45360022005-07-21 21:08:21 +00008467 TOOLINFO *pti;
8468 int ToolInfoSize;
8469
8470 if (multiline_balloon_available() == TRUE)
8471 ToolInfoSize = sizeof(TOOLINFO_NEW);
8472 else
8473 ToolInfoSize = sizeof(TOOLINFO);
8474
8475 pti = (TOOLINFO *)alloc(ToolInfoSize);
8476 if (pti == NULL)
8477 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478
8479 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
8480 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8481 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8482 beval->target, NULL, s_hinst, NULL);
8483
8484 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8485 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8486
Bram Moolenaar45360022005-07-21 21:08:21 +00008487 pti->cbSize = ToolInfoSize;
8488 pti->uFlags = TTF_SUBCLASS;
8489 pti->hwnd = beval->target;
8490 pti->hinst = 0; /* Don't use string resources */
8491 pti->uId = ID_BEVAL_TOOLTIP;
8492
8493 if (multiline_balloon_available() == TRUE)
8494 {
8495 RECT rect;
8496 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
8497 pti->lpszText = LPSTR_TEXTCALLBACK;
8498 ptin->lParam = (LPARAM)text;
8499 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
8500 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8501 (LPARAM)rect.right);
8502 }
8503 else
8504 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505
8506 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00008507 pti->rect.left = pt.x - 3;
8508 pti->rect.top = pt.y - 3;
8509 pti->rect.right = pt.x + 3;
8510 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511
Bram Moolenaar45360022005-07-21 21:08:21 +00008512 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 /* Make tooltip appear sooner */
8514 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008515 /* I've performed some tests and it seems the longest possible life time
8516 * of tooltip is 30 seconds */
8517 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 /*
8519 * HACK: force tooltip to appear, because it'll not appear until
8520 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008521 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008523 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00008525 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526}
8527
8528 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008529delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008531 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532}
8533
8534 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008535BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008536 HWND hwnd UNUSED,
8537 UINT uMsg UNUSED,
8538 UINT_PTR idEvent UNUSED,
8539 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540{
8541 POINT pt;
8542 RECT rect;
8543
8544 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8545 return;
8546
8547 GetCursorPos(&pt);
8548 if (WindowFromPoint(pt) != s_textArea)
8549 return;
8550
8551 ScreenToClient(s_textArea, &pt);
8552 GetClientRect(s_textArea, &rect);
8553 if (!PtInRect(&rect, pt))
8554 return;
8555
8556 if (LastActivity > 0
8557 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8558 && (cur_beval->showState != ShS_PENDING
8559 || abs(cur_beval->x - pt.x) > 3
8560 || abs(cur_beval->y - pt.y) > 3))
8561 {
8562 /* Pointer resting in one place long enough, it's time to show
8563 * the tooltip. */
8564 cur_beval->showState = ShS_PENDING;
8565 cur_beval->x = pt.x;
8566 cur_beval->y = pt.y;
8567
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008568 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569
8570 if (cur_beval->msgCB != NULL)
8571 (*cur_beval->msgCB)(cur_beval, 0);
8572 }
8573}
8574
8575 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008576gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008578 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008580 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581}
8582
8583 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008584gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008586 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 if (beval == NULL)
8588 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008589 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008590 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008591 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592}
8593
8594 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008595gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596{
8597 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008598
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008599 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 if (beval->showState == ShS_SHOWING)
8601 return;
8602 GetCursorPos(&pt);
8603 ScreenToClient(s_textArea, &pt);
8604
8605 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008607 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 gui_mch_disable_beval_area(cur_beval);
8609 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008610 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008612 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613}
8614
8615 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008616gui_mch_create_beval_area(
8617 void *target, /* ignored, always use s_textArea */
8618 char_u *mesg,
8619 void (*mesgCB)(BalloonEval *, int),
8620 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621{
8622 /* partially stolen from gui_beval.c */
8623 BalloonEval *beval;
8624
8625 if (mesg != NULL && mesgCB != NULL)
8626 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01008627 IEMSG(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 return NULL;
8629 }
8630
8631 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
8632 if (beval != NULL)
8633 {
8634 beval->target = s_textArea;
8635 beval->balloon = NULL;
8636
8637 beval->showState = ShS_NEUTRAL;
8638 beval->x = 0;
8639 beval->y = 0;
8640 beval->msg = mesg;
8641 beval->msgCB = mesgCB;
8642 beval->clientData = clientData;
8643
8644 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 cur_beval = beval;
8646
8647 if (p_beval)
8648 gui_mch_enable_beval_area(beval);
8649
8650 }
8651 return beval;
8652}
8653
8654 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008655Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656{
8657 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8658 return;
8659
8660 if (cur_beval != NULL)
8661 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008662 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008664 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008665 // TRACE0("TTN_SHOW {{{");
8666 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008667 break;
8668 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008669 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 delete_tooltip(cur_beval);
8671 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008672 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673
8674 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008675 break;
8676 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008677 {
8678 /* if you get there then we have new common controls */
8679 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8680 info->lpszText = (LPSTR)info->lParam;
8681 info->uFlags |= TTF_DI_SETITEM;
8682 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008683 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 }
8685 }
8686}
8687
8688 static void
8689TrackUserActivity(UINT uMsg)
8690{
8691 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8692 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8693 LastActivity = GetTickCount();
8694}
8695
8696 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008697gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698{
8699 vim_free(beval);
8700}
8701#endif /* FEAT_BEVAL */
8702
8703#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8704/*
8705 * We have multiple signs to draw at the same location. Draw the
8706 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8707 */
8708 void
8709netbeans_draw_multisign_indicator(int row)
8710{
8711 int i;
8712 int y;
8713 int x;
8714
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008715 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008716 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008717
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 x = 0;
8719 y = TEXT_Y(row);
8720
8721 for (i = 0; i < gui.char_height - 3; i++)
8722 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8723
8724 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8725 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8726 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8727 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8728 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8729 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8730 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8731}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008732#endif