blob: 67895cca3e4451b0201c958762842ce2080fc751 [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 Moolenaar26af85d2017-07-23 16:45:10 +02001600 guicolor_T
1601gui_mch_get_rgb_color(int r, int g, int b)
1602{
1603 return gui_get_rgb_color_cmn(r, g, b);
1604}
1605
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001606/*
1607 * Return OK if the key with the termcap name "name" is supported.
1608 */
1609 int
1610gui_mch_haskey(char_u *name)
1611{
1612 int i;
1613
1614 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1615 if (name[0] == special_keys[i].vim_code0 &&
1616 name[1] == special_keys[i].vim_code1)
1617 return OK;
1618 return FAIL;
1619}
1620
1621 void
1622gui_mch_beep(void)
1623{
1624 MessageBeep(MB_OK);
1625}
1626/*
1627 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1628 */
1629 void
1630gui_mch_invert_rectangle(
1631 int r,
1632 int c,
1633 int nr,
1634 int nc)
1635{
1636 RECT rc;
1637
1638 /*
1639 * Note: InvertRect() excludes right and bottom of rectangle.
1640 */
1641 rc.left = FILL_X(c);
1642 rc.top = FILL_Y(r);
1643 rc.right = rc.left + nc * gui.char_width;
1644 rc.bottom = rc.top + nr * gui.char_height;
1645 InvertRect(s_hdc, &rc);
1646}
1647
1648/*
1649 * Iconify the GUI window.
1650 */
1651 void
1652gui_mch_iconify(void)
1653{
1654 ShowWindow(s_hwnd, SW_MINIMIZE);
1655}
1656
1657/*
1658 * Draw a cursor without focus.
1659 */
1660 void
1661gui_mch_draw_hollow_cursor(guicolor_T color)
1662{
1663 HBRUSH hbr;
1664 RECT rc;
1665
1666 /*
1667 * Note: FrameRect() excludes right and bottom of rectangle.
1668 */
1669 rc.left = FILL_X(gui.col);
1670 rc.top = FILL_Y(gui.row);
1671 rc.right = rc.left + gui.char_width;
1672#ifdef FEAT_MBYTE
1673 if (mb_lefthalve(gui.row, gui.col))
1674 rc.right += gui.char_width;
1675#endif
1676 rc.bottom = rc.top + gui.char_height;
1677 hbr = CreateSolidBrush(color);
1678 FrameRect(s_hdc, &rc, hbr);
1679 DeleteBrush(hbr);
1680}
1681/*
1682 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1683 * color "color".
1684 */
1685 void
1686gui_mch_draw_part_cursor(
1687 int w,
1688 int h,
1689 guicolor_T color)
1690{
1691 HBRUSH hbr;
1692 RECT rc;
1693
1694 /*
1695 * Note: FillRect() excludes right and bottom of rectangle.
1696 */
1697 rc.left =
1698#ifdef FEAT_RIGHTLEFT
1699 /* vertical line should be on the right of current point */
1700 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1701#endif
1702 FILL_X(gui.col);
1703 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1704 rc.right = rc.left + w;
1705 rc.bottom = rc.top + h;
1706 hbr = CreateSolidBrush(color);
1707 FillRect(s_hdc, &rc, hbr);
1708 DeleteBrush(hbr);
1709}
1710
1711
1712/*
1713 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1714 * dead key's nominal character and re-post the original message.
1715 */
1716 static void
1717outputDeadKey_rePost(MSG originalMsg)
1718{
1719 static MSG deadCharExpel;
1720
1721 if (!dead_key)
1722 return;
1723
1724 dead_key = 0;
1725
1726 /* Make Windows generate the dead key's character */
1727 deadCharExpel.message = originalMsg.message;
1728 deadCharExpel.hwnd = originalMsg.hwnd;
1729 deadCharExpel.wParam = VK_SPACE;
1730
1731 MyTranslateMessage(&deadCharExpel);
1732
1733 /* re-generate the current character free of the dead char influence */
1734 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1735 originalMsg.lParam);
1736}
1737
1738
1739/*
1740 * Process a single Windows message.
1741 * If one is not available we hang until one is.
1742 */
1743 static void
1744process_message(void)
1745{
1746 MSG msg;
1747 UINT vk = 0; /* Virtual key */
1748 char_u string[40];
1749 int i;
1750 int modifiers = 0;
1751 int key;
1752#ifdef FEAT_MENU
1753 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1754#endif
1755
1756 pGetMessage(&msg, NULL, 0, 0);
1757
1758#ifdef FEAT_OLE
1759 /* Look after OLE Automation commands */
1760 if (msg.message == WM_OLE)
1761 {
1762 char_u *str = (char_u *)msg.lParam;
1763 if (str == NULL || *str == NUL)
1764 {
1765 /* Message can't be ours, forward it. Fixes problem with Ultramon
1766 * 3.0.4 */
1767 pDispatchMessage(&msg);
1768 }
1769 else
1770 {
1771 add_to_input_buf(str, (int)STRLEN(str));
1772 vim_free(str); /* was allocated in CVim::SendKeys() */
1773 }
1774 return;
1775 }
1776#endif
1777
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001778#ifdef MSWIN_FIND_REPLACE
1779 /* Don't process messages used by the dialog */
1780 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1781 {
1782 HandleMouseHide(msg.message, msg.lParam);
1783 return;
1784 }
1785#endif
1786
1787 /*
1788 * Check if it's a special key that we recognise. If not, call
1789 * TranslateMessage().
1790 */
1791 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1792 {
1793 vk = (int) msg.wParam;
1794
1795 /*
1796 * Handle dead keys in special conditions in other cases we let Windows
1797 * handle them and do not interfere.
1798 *
1799 * The dead_key flag must be reset on several occasions:
1800 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1801 * consumed at that point (This is when we let Windows combine the
1802 * dead character on its own)
1803 *
1804 * - Before doing something special such as regenerating keypresses to
1805 * expel the dead character as this could trigger an infinite loop if
1806 * for some reason MyTranslateMessage() do not trigger a call
1807 * immediately to _OnChar() (or _OnSysChar()).
1808 */
1809 if (dead_key)
1810 {
1811 /*
1812 * If a dead key was pressed and the user presses VK_SPACE,
1813 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1814 * with the dead char now, so do nothing special and let Windows
1815 * handle it.
1816 *
1817 * Note that VK_SPACE combines with the dead_key's character and
1818 * only one WM_CHAR will be generated by TranslateMessage(), in
1819 * the two other cases two WM_CHAR will be generated: the dead
1820 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1821 * user expects.
1822 */
1823 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1824 {
1825 dead_key = 0;
1826 MyTranslateMessage(&msg);
1827 return;
1828 }
1829 /* In modes where we are not typing, dead keys should behave
1830 * normally */
1831 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1832 {
1833 outputDeadKey_rePost(msg);
1834 return;
1835 }
1836 }
1837
1838 /* Check for CTRL-BREAK */
1839 if (vk == VK_CANCEL)
1840 {
1841 trash_input_buf();
1842 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001843 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001844 string[0] = Ctrl_C;
1845 add_to_input_buf(string, 1);
1846 }
1847
1848 for (i = 0; special_keys[i].key_sym != 0; i++)
1849 {
1850 /* ignore VK_SPACE when ALT key pressed: system menu */
1851 if (special_keys[i].key_sym == vk
1852 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1853 {
1854 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001855 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001856 * is a key that would normally trigger the dead key nominal
1857 * character output (such as a NUMPAD printable character or
1858 * the TAB key, etc...).
1859 */
1860 if (dead_key && (special_keys[i].vim_code0 == 'K'
1861 || vk == VK_TAB || vk == CAR))
1862 {
1863 outputDeadKey_rePost(msg);
1864 return;
1865 }
1866
1867#ifdef FEAT_MENU
1868 /* Check for <F10>: Windows selects the menu. When <F10> is
1869 * mapped we want to use the mapping instead. */
1870 if (vk == VK_F10
1871 && gui.menu_is_active
1872 && check_map(k10, State, FALSE, TRUE, FALSE,
1873 NULL, NULL) == NULL)
1874 break;
1875#endif
1876 if (GetKeyState(VK_SHIFT) & 0x8000)
1877 modifiers |= MOD_MASK_SHIFT;
1878 /*
1879 * Don't use caps-lock as shift, because these are special keys
1880 * being considered here, and we only want letters to get
1881 * shifted -- webb
1882 */
1883 /*
1884 if (GetKeyState(VK_CAPITAL) & 0x0001)
1885 modifiers ^= MOD_MASK_SHIFT;
1886 */
1887 if (GetKeyState(VK_CONTROL) & 0x8000)
1888 modifiers |= MOD_MASK_CTRL;
1889 if (GetKeyState(VK_MENU) & 0x8000)
1890 modifiers |= MOD_MASK_ALT;
1891
1892 if (special_keys[i].vim_code1 == NUL)
1893 key = special_keys[i].vim_code0;
1894 else
1895 key = TO_SPECIAL(special_keys[i].vim_code0,
1896 special_keys[i].vim_code1);
1897 key = simplify_key(key, &modifiers);
1898 if (key == CSI)
1899 key = K_CSI;
1900
1901 if (modifiers)
1902 {
1903 string[0] = CSI;
1904 string[1] = KS_MODIFIER;
1905 string[2] = modifiers;
1906 add_to_input_buf(string, 3);
1907 }
1908
1909 if (IS_SPECIAL(key))
1910 {
1911 string[0] = CSI;
1912 string[1] = K_SECOND(key);
1913 string[2] = K_THIRD(key);
1914 add_to_input_buf(string, 3);
1915 }
1916 else
1917 {
1918 int len;
1919
1920 /* Handle "key" as a Unicode character. */
1921 len = char_to_string(key, string, 40, FALSE);
1922 add_to_input_buf(string, len);
1923 }
1924 break;
1925 }
1926 }
1927 if (special_keys[i].key_sym == 0)
1928 {
1929 /* Some keys need C-S- where they should only need C-.
1930 * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1931 * system startup (Helmut Stiegler, 2003 Oct 3). */
1932 if (vk != 0xff
1933 && (GetKeyState(VK_CONTROL) & 0x8000)
1934 && !(GetKeyState(VK_SHIFT) & 0x8000)
1935 && !(GetKeyState(VK_MENU) & 0x8000))
1936 {
1937 /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */
1938 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1939 {
1940 string[0] = Ctrl_HAT;
1941 add_to_input_buf(string, 1);
1942 }
1943 /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */
1944 else if (vk == 0xBD) /* QWERTY for CTRL-'-' */
1945 {
1946 string[0] = Ctrl__;
1947 add_to_input_buf(string, 1);
1948 }
1949 /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */
1950 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
1951 {
1952 string[0] = Ctrl_AT;
1953 add_to_input_buf(string, 1);
1954 }
1955 else
1956 MyTranslateMessage(&msg);
1957 }
1958 else
1959 MyTranslateMessage(&msg);
1960 }
1961 }
1962#ifdef FEAT_MBYTE_IME
1963 else if (msg.message == WM_IME_NOTIFY)
1964 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
1965 else if (msg.message == WM_KEYUP && im_get_status())
1966 /* added for non-MS IME (Yasuhiro Matsumoto) */
1967 MyTranslateMessage(&msg);
1968#endif
1969#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
1970/* GIME_TEST */
1971 else if (msg.message == WM_IME_STARTCOMPOSITION)
1972 {
1973 POINT point;
1974
1975 global_ime_set_font(&norm_logfont);
1976 point.x = FILL_X(gui.col);
1977 point.y = FILL_Y(gui.row);
1978 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
1979 global_ime_set_position(&point);
1980 }
1981#endif
1982
1983#ifdef FEAT_MENU
1984 /* Check for <F10>: Default effect is to select the menu. When <F10> is
1985 * mapped we need to stop it here to avoid strange effects (e.g., for the
1986 * key-up event) */
1987 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
1988 NULL, NULL) == NULL)
1989#endif
1990 pDispatchMessage(&msg);
1991}
1992
1993/*
1994 * Catch up with any queued events. This may put keyboard input into the
1995 * input buffer, call resize call-backs, trigger timers etc. If there is
1996 * nothing in the event queue (& no timers pending), then we return
1997 * immediately.
1998 */
1999 void
2000gui_mch_update(void)
2001{
2002 MSG msg;
2003
2004 if (!s_busy_processing)
2005 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2006 && !vim_is_input_buf_full())
2007 process_message();
2008}
2009
Bram Moolenaar4231da42016-06-02 14:30:04 +02002010 static void
2011remove_any_timer(void)
2012{
2013 MSG msg;
2014
2015 if (s_wait_timer != 0 && !s_timed_out)
2016 {
2017 KillTimer(NULL, s_wait_timer);
2018
2019 /* Eat spurious WM_TIMER messages */
2020 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2021 ;
2022 s_wait_timer = 0;
2023 }
2024}
2025
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002026/*
2027 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2028 * from the keyboard.
2029 * wtime == -1 Wait forever.
2030 * wtime == 0 This should never happen.
2031 * wtime > 0 Wait wtime milliseconds for a character.
2032 * Returns OK if a character was found to be available within the given time,
2033 * or FAIL otherwise.
2034 */
2035 int
2036gui_mch_wait_for_chars(int wtime)
2037{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002038 int focus;
2039
2040 s_timed_out = FALSE;
2041
2042 if (wtime > 0)
2043 {
2044 /* Don't do anything while processing a (scroll) message. */
2045 if (s_busy_processing)
2046 return FAIL;
2047 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)wtime,
2048 (TIMERPROC)_OnTimer);
2049 }
2050
2051 allow_scrollbar = TRUE;
2052
2053 focus = gui.in_focus;
2054 while (!s_timed_out)
2055 {
2056 /* Stop or start blinking when focus changes */
2057 if (gui.in_focus != focus)
2058 {
2059 if (gui.in_focus)
2060 gui_mch_start_blink();
2061 else
2062 gui_mch_stop_blink();
2063 focus = gui.in_focus;
2064 }
2065
2066 if (s_need_activate)
2067 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002068 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002069 s_need_activate = FALSE;
2070 }
2071
Bram Moolenaar4231da42016-06-02 14:30:04 +02002072#ifdef FEAT_TIMERS
2073 did_add_timer = FALSE;
2074#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002075#ifdef MESSAGE_QUEUE
Bram Moolenaar62426e12017-08-13 15:37:58 +02002076 /* Check channel I/O while waiting for a message. */
Bram Moolenaar9186a272016-02-23 19:34:01 +01002077 for (;;)
2078 {
2079 MSG msg;
2080
2081 parse_queued_messages();
2082
Bram Moolenaar62426e12017-08-13 15:37:58 +02002083 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2084 {
2085 process_message();
2086 break;
2087 }
2088 else if (MsgWaitForMultipleObjects(0, NULL, FALSE, 100, QS_ALLINPUT)
2089 != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002090 break;
2091 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002092#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002093 /*
2094 * Don't use gui_mch_update() because then we will spin-lock until a
2095 * char arrives, instead we use GetMessage() to hang until an
2096 * event arrives. No need to check for input_buf_full because we are
2097 * returning as soon as it contains a single char -- webb
2098 */
2099 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002100#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002101
2102 if (input_available())
2103 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002104 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002105 allow_scrollbar = FALSE;
2106
2107 /* Clear pending mouse button, the release event may have been
2108 * taken by the dialog window. But don't do this when getting
2109 * focus, we need the mouse-up event then. */
2110 if (!s_getting_focus)
2111 s_button_pending = -1;
2112
2113 return OK;
2114 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002115
2116#ifdef FEAT_TIMERS
2117 if (did_add_timer)
2118 {
2119 /* Need to recompute the waiting time. */
2120 remove_any_timer();
2121 break;
2122 }
2123#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002124 }
2125 allow_scrollbar = FALSE;
2126 return FAIL;
2127}
2128
2129/*
2130 * Clear a rectangular region of the screen from text pos (row1, col1) to
2131 * (row2, col2) inclusive.
2132 */
2133 void
2134gui_mch_clear_block(
2135 int row1,
2136 int col1,
2137 int row2,
2138 int col2)
2139{
2140 RECT rc;
2141
2142 /*
2143 * Clear one extra pixel at the far right, for when bold characters have
2144 * spilled over to the window border.
2145 * Note: FillRect() excludes right and bottom of rectangle.
2146 */
2147 rc.left = FILL_X(col1);
2148 rc.top = FILL_Y(row1);
2149 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2150 rc.bottom = FILL_Y(row2 + 1);
2151 clear_rect(&rc);
2152}
2153
2154/*
2155 * Clear the whole text window.
2156 */
2157 void
2158gui_mch_clear_all(void)
2159{
2160 RECT rc;
2161
2162 rc.left = 0;
2163 rc.top = 0;
2164 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2165 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2166 clear_rect(&rc);
2167}
2168/*
2169 * Menu stuff.
2170 */
2171
2172 void
2173gui_mch_enable_menu(int flag)
2174{
2175#ifdef FEAT_MENU
2176 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2177#endif
2178}
2179
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002180 void
2181gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002182 int x UNUSED,
2183 int y UNUSED,
2184 int w UNUSED,
2185 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002186{
2187 /* It will be in the right place anyway */
2188}
2189
2190#if defined(FEAT_MENU) || defined(PROTO)
2191/*
2192 * Make menu item hidden or not hidden
2193 */
2194 void
2195gui_mch_menu_hidden(
2196 vimmenu_T *menu,
2197 int hidden)
2198{
2199 /*
2200 * This doesn't do what we want. Hmm, just grey the menu items for now.
2201 */
2202 /*
2203 if (hidden)
2204 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2205 else
2206 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2207 */
2208 gui_mch_menu_grey(menu, hidden);
2209}
2210
2211/*
2212 * This is called after setting all the menus to grey/hidden or not.
2213 */
2214 void
2215gui_mch_draw_menubar(void)
2216{
2217 DrawMenuBar(s_hwnd);
2218}
2219#endif /*FEAT_MENU*/
2220
2221#ifndef PROTO
2222void
2223#ifdef VIMDLL
2224_export
2225#endif
2226_cdecl
2227SaveInst(HINSTANCE hInst)
2228{
2229 s_hinst = hInst;
2230}
2231#endif
2232
2233/*
2234 * Return the RGB value of a pixel as a long.
2235 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002236 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002237gui_mch_get_rgb(guicolor_T pixel)
2238{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002239 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2240 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002241}
2242
2243#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2244/* Convert pixels in X to dialog units */
2245 static WORD
2246PixelToDialogX(int numPixels)
2247{
2248 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2249}
2250
2251/* Convert pixels in Y to dialog units */
2252 static WORD
2253PixelToDialogY(int numPixels)
2254{
2255 return (WORD)((numPixels * 8) / s_dlgfntheight);
2256}
2257
2258/* Return the width in pixels of the given text in the given DC. */
2259 static int
2260GetTextWidth(HDC hdc, char_u *str, int len)
2261{
2262 SIZE size;
2263
2264 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2265 return size.cx;
2266}
2267
2268#ifdef FEAT_MBYTE
2269/*
2270 * Return the width in pixels of the given text in the given DC, taking care
2271 * of 'encoding' to active codepage conversion.
2272 */
2273 static int
2274GetTextWidthEnc(HDC hdc, char_u *str, int len)
2275{
2276 SIZE size;
2277 WCHAR *wstr;
2278 int n;
2279 int wlen = len;
2280
2281 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2282 {
2283 /* 'encoding' differs from active codepage: convert text and use wide
2284 * function */
2285 wstr = enc_to_utf16(str, &wlen);
2286 if (wstr != NULL)
2287 {
2288 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2289 vim_free(wstr);
2290 if (n)
2291 return size.cx;
2292 }
2293 }
2294
2295 return GetTextWidth(hdc, str, len);
2296}
2297#else
2298# define GetTextWidthEnc(h, s, l) GetTextWidth((h), (s), (l))
2299#endif
2300
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002301static void get_work_area(RECT *spi_rect);
2302
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002303/*
2304 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002305 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2306 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002307 */
2308 static BOOL
2309CenterWindow(
2310 HWND hwndChild,
2311 HWND hwndParent)
2312{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002313 HMONITOR mon;
2314 MONITORINFO moninfo;
2315 RECT rChild, rParent, rScreen;
2316 int wChild, hChild, wParent, hParent;
2317 int xNew, yNew;
2318 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002319
2320 GetWindowRect(hwndChild, &rChild);
2321 wChild = rChild.right - rChild.left;
2322 hChild = rChild.bottom - rChild.top;
2323
2324 /* If Vim is minimized put the window in the middle of the screen. */
2325 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002326 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002327 else
2328 GetWindowRect(hwndParent, &rParent);
2329 wParent = rParent.right - rParent.left;
2330 hParent = rParent.bottom - rParent.top;
2331
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002332 moninfo.cbSize = sizeof(MONITORINFO);
2333 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2334 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002335 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002336 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002337 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002338 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002339 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002340 hdc = GetDC(hwndChild);
2341 rScreen.left = 0;
2342 rScreen.top = 0;
2343 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2344 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2345 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002346 }
2347
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002348 xNew = rParent.left + ((wParent - wChild) / 2);
2349 if (xNew < rScreen.left)
2350 xNew = rScreen.left;
2351 else if ((xNew + wChild) > rScreen.right)
2352 xNew = rScreen.right - wChild;
2353
2354 yNew = rParent.top + ((hParent - hChild) / 2);
2355 if (yNew < rScreen.top)
2356 yNew = rScreen.top;
2357 else if ((yNew + hChild) > rScreen.bottom)
2358 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002359
2360 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2361 SWP_NOSIZE | SWP_NOZORDER);
2362}
2363#endif /* FEAT_GUI_DIALOG */
2364
2365void
2366gui_mch_activate_window(void)
2367{
2368 (void)SetActiveWindow(s_hwnd);
2369}
2370
2371#if defined(FEAT_TOOLBAR) || defined(PROTO)
2372 void
2373gui_mch_show_toolbar(int showit)
2374{
2375 if (s_toolbarhwnd == NULL)
2376 return;
2377
2378 if (showit)
2379 {
2380# ifdef FEAT_MBYTE
2381# ifndef TB_SETUNICODEFORMAT
2382 /* For older compilers. We assume this never changes. */
2383# define TB_SETUNICODEFORMAT 0x2005
2384# endif
2385 /* Enable/disable unicode support */
2386 int uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2387 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2388# endif
2389 ShowWindow(s_toolbarhwnd, SW_SHOW);
2390 }
2391 else
2392 ShowWindow(s_toolbarhwnd, SW_HIDE);
2393}
2394
2395/* Then number of bitmaps is fixed. Exit is missing! */
2396#define TOOLBAR_BITMAP_COUNT 31
2397
2398#endif
2399
2400#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2401 static void
2402add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2403{
2404#ifdef FEAT_MBYTE
2405 WCHAR *wn = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002406
2407 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2408 {
2409 /* 'encoding' differs from active codepage: convert menu name
2410 * and use wide function */
2411 wn = enc_to_utf16(item_text, NULL);
2412 if (wn != NULL)
2413 {
2414 MENUITEMINFOW infow;
2415
2416 infow.cbSize = sizeof(infow);
2417 infow.fMask = MIIM_TYPE | MIIM_ID;
2418 infow.wID = item_id;
2419 infow.fType = MFT_STRING;
2420 infow.dwTypeData = wn;
2421 infow.cch = (UINT)wcslen(wn);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002422 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002423 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002424 }
2425 }
2426
2427 if (wn == NULL)
2428#endif
2429 {
2430 MENUITEMINFO info;
2431
2432 info.cbSize = sizeof(info);
2433 info.fMask = MIIM_TYPE | MIIM_ID;
2434 info.wID = item_id;
2435 info.fType = MFT_STRING;
2436 info.dwTypeData = (LPTSTR)item_text;
2437 info.cch = (UINT)STRLEN(item_text);
2438 InsertMenuItem(pmenu, item_id, FALSE, &info);
2439 }
2440}
2441
2442 static void
2443show_tabline_popup_menu(void)
2444{
2445 HMENU tab_pmenu;
2446 long rval;
2447 POINT pt;
2448
2449 /* When ignoring events don't show the menu. */
2450 if (hold_gui_events
2451# ifdef FEAT_CMDWIN
2452 || cmdwin_type != 0
2453# endif
2454 )
2455 return;
2456
2457 tab_pmenu = CreatePopupMenu();
2458 if (tab_pmenu == NULL)
2459 return;
2460
2461 if (first_tabpage->tp_next != NULL)
2462 add_tabline_popup_menu_entry(tab_pmenu,
2463 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2464 add_tabline_popup_menu_entry(tab_pmenu,
2465 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2466 add_tabline_popup_menu_entry(tab_pmenu,
2467 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2468
2469 GetCursorPos(&pt);
2470 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2471 NULL);
2472
2473 DestroyMenu(tab_pmenu);
2474
2475 /* Add the string cmd into input buffer */
2476 if (rval > 0)
2477 {
2478 TCHITTESTINFO htinfo;
2479 int idx;
2480
2481 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2482 return;
2483
2484 htinfo.pt.x = pt.x;
2485 htinfo.pt.y = pt.y;
2486 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2487 if (idx == -1)
2488 idx = 0;
2489 else
2490 idx += 1;
2491
2492 send_tabline_menu_event(idx, (int)rval);
2493 }
2494}
2495
2496/*
2497 * Show or hide the tabline.
2498 */
2499 void
2500gui_mch_show_tabline(int showit)
2501{
2502 if (s_tabhwnd == NULL)
2503 return;
2504
2505 if (!showit != !showing_tabline)
2506 {
2507 if (showit)
2508 ShowWindow(s_tabhwnd, SW_SHOW);
2509 else
2510 ShowWindow(s_tabhwnd, SW_HIDE);
2511 showing_tabline = showit;
2512 }
2513}
2514
2515/*
2516 * Return TRUE when tabline is displayed.
2517 */
2518 int
2519gui_mch_showing_tabline(void)
2520{
2521 return s_tabhwnd != NULL && showing_tabline;
2522}
2523
2524/*
2525 * Update the labels of the tabline.
2526 */
2527 void
2528gui_mch_update_tabline(void)
2529{
2530 tabpage_T *tp;
2531 TCITEM tie;
2532 int nr = 0;
2533 int curtabidx = 0;
2534 int tabadded = 0;
2535#ifdef FEAT_MBYTE
2536 static int use_unicode = FALSE;
2537 int uu;
2538 WCHAR *wstr = NULL;
2539#endif
2540
2541 if (s_tabhwnd == NULL)
2542 return;
2543
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002544#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002545# ifndef CCM_SETUNICODEFORMAT
2546 /* For older compilers. We assume this never changes. */
2547# define CCM_SETUNICODEFORMAT 0x2005
2548# endif
2549 uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2550 if (uu != use_unicode)
2551 {
2552 /* Enable/disable unicode support */
2553 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2554 use_unicode = uu;
2555 }
2556#endif
2557
2558 tie.mask = TCIF_TEXT;
2559 tie.iImage = -1;
2560
2561 /* Disable redraw for tab updates to eliminate O(N^2) draws. */
2562 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2563
2564 /* Add a label for each tab page. They all contain the same text area. */
2565 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2566 {
2567 if (tp == curtab)
2568 curtabidx = nr;
2569
2570 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2571 {
2572 /* Add the tab */
2573 tie.pszText = "-Empty-";
2574 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2575 tabadded = 1;
2576 }
2577
2578 get_tabline_label(tp, FALSE);
2579 tie.pszText = (LPSTR)NameBuff;
2580#ifdef FEAT_MBYTE
2581 wstr = NULL;
2582 if (use_unicode)
2583 {
2584 /* Need to go through Unicode. */
2585 wstr = enc_to_utf16(NameBuff, NULL);
2586 if (wstr != NULL)
2587 {
2588 TCITEMW tiw;
2589
2590 tiw.mask = TCIF_TEXT;
2591 tiw.iImage = -1;
2592 tiw.pszText = wstr;
2593 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2594 vim_free(wstr);
2595 }
2596 }
2597 if (wstr == NULL)
2598#endif
2599 {
2600 TabCtrl_SetItem(s_tabhwnd, nr, &tie);
2601 }
2602 }
2603
2604 /* Remove any old labels. */
2605 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2606 TabCtrl_DeleteItem(s_tabhwnd, nr);
2607
2608 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2609 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2610
2611 /* Re-enable redraw and redraw. */
2612 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2613 RedrawWindow(s_tabhwnd, NULL, NULL,
2614 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2615
2616 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2617 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2618}
2619
2620/*
2621 * Set the current tab to "nr". First tab is 1.
2622 */
2623 void
2624gui_mch_set_curtab(int nr)
2625{
2626 if (s_tabhwnd == NULL)
2627 return;
2628
2629 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2630 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2631}
2632
2633#endif
2634
2635/*
2636 * ":simalt" command.
2637 */
2638 void
2639ex_simalt(exarg_T *eap)
2640{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002641 char_u *keys = eap->arg;
2642 int fill_typebuf = FALSE;
2643 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002644
2645 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2646 while (*keys)
2647 {
2648 if (*keys == '~')
2649 *keys = ' '; /* for showing system menu */
2650 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2651 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002652 fill_typebuf = TRUE;
2653 }
2654 if (fill_typebuf)
2655 {
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002656 /* Put a NOP in the typeahead buffer so that the message will get
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002657 * processed. */
2658 key_name[0] = K_SPECIAL;
2659 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002660 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002661 key_name[3] = NUL;
2662 typebuf_was_filled = TRUE;
2663 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002664 }
2665}
2666
2667/*
2668 * Create the find & replace dialogs.
2669 * You can't have both at once: ":find" when replace is showing, destroys
2670 * the replace dialog first, and the other way around.
2671 */
2672#ifdef MSWIN_FIND_REPLACE
2673 static void
2674initialise_findrep(char_u *initial_string)
2675{
2676 int wword = FALSE;
2677 int mcase = !p_ic;
2678 char_u *entry_text;
2679
2680 /* Get the search string to use. */
2681 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2682
2683 s_findrep_struct.hwndOwner = s_hwnd;
2684 s_findrep_struct.Flags = FR_DOWN;
2685 if (mcase)
2686 s_findrep_struct.Flags |= FR_MATCHCASE;
2687 if (wword)
2688 s_findrep_struct.Flags |= FR_WHOLEWORD;
2689 if (entry_text != NULL && *entry_text != NUL)
2690 vim_strncpy((char_u *)s_findrep_struct.lpstrFindWhat, entry_text,
2691 s_findrep_struct.wFindWhatLen - 1);
2692 vim_free(entry_text);
2693}
2694#endif
2695
2696 static void
2697set_window_title(HWND hwnd, char *title)
2698{
2699#ifdef FEAT_MBYTE
2700 if (title != NULL && enc_codepage >= 0 && enc_codepage != (int)GetACP())
2701 {
2702 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002703
2704 /* Convert the title from 'encoding' to UTF-16. */
2705 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2706 if (wbuf != NULL)
2707 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002708 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002709 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002710 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002711 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002712 }
2713#endif
2714 (void)SetWindowText(hwnd, (LPCSTR)title);
2715}
2716
2717 void
2718gui_mch_find_dialog(exarg_T *eap)
2719{
2720#ifdef MSWIN_FIND_REPLACE
2721 if (s_findrep_msg != 0)
2722 {
2723 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2724 DestroyWindow(s_findrep_hwnd);
2725
2726 if (!IsWindow(s_findrep_hwnd))
2727 {
2728 initialise_findrep(eap->arg);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002729# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002730 /* If the OS is Windows NT, and 'encoding' differs from active
2731 * codepage: convert text and use wide function. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002732 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002733 {
2734 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2735 s_findrep_hwnd = FindTextW(
2736 (LPFINDREPLACEW) &s_findrep_struct_w);
2737 }
2738 else
2739# endif
2740 s_findrep_hwnd = FindText((LPFINDREPLACE) &s_findrep_struct);
2741 }
2742
2743 set_window_title(s_findrep_hwnd,
2744 _("Find string (use '\\\\' to find a '\\')"));
2745 (void)SetFocus(s_findrep_hwnd);
2746
2747 s_findrep_is_find = TRUE;
2748 }
2749#endif
2750}
2751
2752
2753 void
2754gui_mch_replace_dialog(exarg_T *eap)
2755{
2756#ifdef MSWIN_FIND_REPLACE
2757 if (s_findrep_msg != 0)
2758 {
2759 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2760 DestroyWindow(s_findrep_hwnd);
2761
2762 if (!IsWindow(s_findrep_hwnd))
2763 {
2764 initialise_findrep(eap->arg);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002765# ifdef FEAT_MBYTE
2766 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002767 {
2768 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2769 s_findrep_hwnd = ReplaceTextW(
2770 (LPFINDREPLACEW) &s_findrep_struct_w);
2771 }
2772 else
2773# endif
2774 s_findrep_hwnd = ReplaceText(
2775 (LPFINDREPLACE) &s_findrep_struct);
2776 }
2777
2778 set_window_title(s_findrep_hwnd,
2779 _("Find & Replace (use '\\\\' to find a '\\')"));
2780 (void)SetFocus(s_findrep_hwnd);
2781
2782 s_findrep_is_find = FALSE;
2783 }
2784#endif
2785}
2786
2787
2788/*
2789 * Set visibility of the pointer.
2790 */
2791 void
2792gui_mch_mousehide(int hide)
2793{
2794 if (hide != gui.pointer_hidden)
2795 {
2796 ShowCursor(!hide);
2797 gui.pointer_hidden = hide;
2798 }
2799}
2800
2801#ifdef FEAT_MENU
2802 static void
2803gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2804{
2805 /* Unhide the mouse, we don't get move events here. */
2806 gui_mch_mousehide(FALSE);
2807
2808 (void)TrackPopupMenu(
2809 (HMENU)menu->submenu_id,
2810 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2811 x, y,
2812 (int)0, /*reserved param*/
2813 s_hwnd,
2814 NULL);
2815 /*
2816 * NOTE: The pop-up menu can eat the mouse up event.
2817 * We deal with this in normal.c.
2818 */
2819}
2820#endif
2821
2822/*
2823 * Got a message when the system will go down.
2824 */
2825 static void
2826_OnEndSession(void)
2827{
2828 getout_preserve_modified(1);
2829}
2830
2831/*
2832 * Get this message when the user clicks on the cross in the top right corner
2833 * of a Windows95 window.
2834 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002835 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002836_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002837{
2838 gui_shell_closed();
2839}
2840
2841/*
2842 * Get a message when the window is being destroyed.
2843 */
2844 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002845_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002846{
2847 if (!destroying)
2848 _OnClose(hwnd);
2849}
2850
2851 static void
2852_OnPaint(
2853 HWND hwnd)
2854{
2855 if (!IsMinimized(hwnd))
2856 {
2857 PAINTSTRUCT ps;
2858
2859 out_flush(); /* make sure all output has been processed */
2860 (void)BeginPaint(hwnd, &ps);
2861#if defined(FEAT_DIRECTX)
2862 if (IS_ENABLE_DIRECTX())
2863 DWriteContext_BeginDraw(s_dwc);
2864#endif
2865
2866#ifdef FEAT_MBYTE
2867 /* prevent multi-byte characters from misprinting on an invalid
2868 * rectangle */
2869 if (has_mbyte)
2870 {
2871 RECT rect;
2872
2873 GetClientRect(hwnd, &rect);
2874 ps.rcPaint.left = rect.left;
2875 ps.rcPaint.right = rect.right;
2876 }
2877#endif
2878
2879 if (!IsRectEmpty(&ps.rcPaint))
2880 {
2881#if defined(FEAT_DIRECTX)
2882 if (IS_ENABLE_DIRECTX())
2883 DWriteContext_BindDC(s_dwc, s_hdc, &ps.rcPaint);
2884#endif
2885 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2886 ps.rcPaint.right - ps.rcPaint.left + 1,
2887 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2888 }
2889
2890#if defined(FEAT_DIRECTX)
2891 if (IS_ENABLE_DIRECTX())
2892 DWriteContext_EndDraw(s_dwc);
2893#endif
2894 EndPaint(hwnd, &ps);
2895 }
2896}
2897
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002898 static void
2899_OnSize(
2900 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002901 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002902 int cx,
2903 int cy)
2904{
2905 if (!IsMinimized(hwnd))
2906 {
2907 gui_resize_shell(cx, cy);
2908
2909#ifdef FEAT_MENU
2910 /* Menu bar may wrap differently now */
2911 gui_mswin_get_menu_height(TRUE);
2912#endif
2913 }
2914}
2915
2916 static void
2917_OnSetFocus(
2918 HWND hwnd,
2919 HWND hwndOldFocus)
2920{
2921 gui_focus_change(TRUE);
2922 s_getting_focus = TRUE;
2923 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2924}
2925
2926 static void
2927_OnKillFocus(
2928 HWND hwnd,
2929 HWND hwndNewFocus)
2930{
2931 gui_focus_change(FALSE);
2932 s_getting_focus = FALSE;
2933 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2934}
2935
2936/*
2937 * Get a message when the user switches back to vim
2938 */
2939 static LRESULT
2940_OnActivateApp(
2941 HWND hwnd,
2942 BOOL fActivate,
2943 DWORD dwThreadId)
2944{
2945 /* we call gui_focus_change() in _OnSetFocus() */
2946 /* gui_focus_change((int)fActivate); */
2947 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2948}
2949
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002950 void
2951gui_mch_destroy_scrollbar(scrollbar_T *sb)
2952{
2953 DestroyWindow(sb->id);
2954}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002955
2956/*
2957 * Get current mouse coordinates in text window.
2958 */
2959 void
2960gui_mch_getmouse(int *x, int *y)
2961{
2962 RECT rct;
2963 POINT mp;
2964
2965 (void)GetWindowRect(s_textArea, &rct);
2966 (void)GetCursorPos((LPPOINT)&mp);
2967 *x = (int)(mp.x - rct.left);
2968 *y = (int)(mp.y - rct.top);
2969}
2970
2971/*
2972 * Move mouse pointer to character at (x, y).
2973 */
2974 void
2975gui_mch_setmouse(int x, int y)
2976{
2977 RECT rct;
2978
2979 (void)GetWindowRect(s_textArea, &rct);
2980 (void)SetCursorPos(x + gui.border_offset + rct.left,
2981 y + gui.border_offset + rct.top);
2982}
2983
2984 static void
2985gui_mswin_get_valid_dimensions(
2986 int w,
2987 int h,
2988 int *valid_w,
2989 int *valid_h)
2990{
2991 int base_width, base_height;
2992
2993 base_width = gui_get_base_width()
2994 + (GetSystemMetrics(SM_CXFRAME) +
2995 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
2996 base_height = gui_get_base_height()
2997 + (GetSystemMetrics(SM_CYFRAME) +
2998 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
2999 + GetSystemMetrics(SM_CYCAPTION)
3000#ifdef FEAT_MENU
3001 + gui_mswin_get_menu_height(FALSE)
3002#endif
3003 ;
3004 *valid_w = base_width +
3005 ((w - base_width) / gui.char_width) * gui.char_width;
3006 *valid_h = base_height +
3007 ((h - base_height) / gui.char_height) * gui.char_height;
3008}
3009
3010 void
3011gui_mch_flash(int msec)
3012{
3013 RECT rc;
3014
3015 /*
3016 * Note: InvertRect() excludes right and bottom of rectangle.
3017 */
3018 rc.left = 0;
3019 rc.top = 0;
3020 rc.right = gui.num_cols * gui.char_width;
3021 rc.bottom = gui.num_rows * gui.char_height;
3022 InvertRect(s_hdc, &rc);
3023 gui_mch_flush(); /* make sure it's displayed */
3024
3025 ui_delay((long)msec, TRUE); /* wait for a few msec */
3026
3027 InvertRect(s_hdc, &rc);
3028}
3029
3030/*
3031 * Return flags used for scrolling.
3032 * The SW_INVALIDATE is required when part of the window is covered or
3033 * off-screen. Refer to MS KB Q75236.
3034 */
3035 static int
3036get_scroll_flags(void)
3037{
3038 HWND hwnd;
3039 RECT rcVim, rcOther, rcDest;
3040
3041 GetWindowRect(s_hwnd, &rcVim);
3042
3043 /* Check if the window is partly above or below the screen. We don't care
3044 * about partly left or right of the screen, it is not relevant when
3045 * scrolling up or down. */
3046 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
3047 return SW_INVALIDATE;
3048
3049 /* Check if there is an window (partly) on top of us. */
3050 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3051 if (IsWindowVisible(hwnd))
3052 {
3053 GetWindowRect(hwnd, &rcOther);
3054 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3055 return SW_INVALIDATE;
3056 }
3057 return 0;
3058}
3059
3060/*
3061 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3062 * may not be scrolled out properly.
3063 * For gVim, when _OnScroll() is repeated, the character at the
3064 * previous cursor position may be left drawn after scroll.
3065 * The problem can be avoided by calling GetPixel() to get a pixel in
3066 * the region before ScrollWindowEx().
3067 */
3068 static void
3069intel_gpu_workaround(void)
3070{
3071 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3072}
3073
3074/*
3075 * Delete the given number of lines from the given row, scrolling up any
3076 * text further down within the scroll region.
3077 */
3078 void
3079gui_mch_delete_lines(
3080 int row,
3081 int num_lines)
3082{
3083 RECT rc;
3084
3085 intel_gpu_workaround();
3086
3087 rc.left = FILL_X(gui.scroll_region_left);
3088 rc.right = FILL_X(gui.scroll_region_right + 1);
3089 rc.top = FILL_Y(row);
3090 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3091
3092 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
3093 &rc, &rc, NULL, NULL, get_scroll_flags());
3094
3095 UpdateWindow(s_textArea);
3096 /* This seems to be required to avoid the cursor disappearing when
3097 * scrolling such that the cursor ends up in the top-left character on
3098 * the screen... But why? (Webb) */
3099 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3100 /* gui.cursor_is_valid = FALSE; */
3101
3102 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3103 gui.scroll_region_left,
3104 gui.scroll_region_bot, gui.scroll_region_right);
3105}
3106
3107/*
3108 * Insert the given number of lines before the given row, scrolling down any
3109 * following text within the scroll region.
3110 */
3111 void
3112gui_mch_insert_lines(
3113 int row,
3114 int num_lines)
3115{
3116 RECT rc;
3117
3118 intel_gpu_workaround();
3119
3120 rc.left = FILL_X(gui.scroll_region_left);
3121 rc.right = FILL_X(gui.scroll_region_right + 1);
3122 rc.top = FILL_Y(row);
3123 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3124 /* The SW_INVALIDATE is required when part of the window is covered or
3125 * off-screen. How do we avoid it when it's not needed? */
3126 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
3127 &rc, &rc, NULL, NULL, get_scroll_flags());
3128
3129 UpdateWindow(s_textArea);
3130
3131 gui_clear_block(row, gui.scroll_region_left,
3132 row + num_lines - 1, gui.scroll_region_right);
3133}
3134
3135
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003136 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003137gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003138{
3139#if defined(FEAT_DIRECTX)
3140 DWriteContext_Close(s_dwc);
3141 DWrite_Final();
3142 s_dwc = NULL;
3143#endif
3144
3145 ReleaseDC(s_textArea, s_hdc);
3146 DeleteObject(s_brush);
3147
3148#ifdef FEAT_TEAROFF
3149 /* Unload the tearoff bitmap */
3150 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3151#endif
3152
3153 /* Destroy our window (if we have one). */
3154 if (s_hwnd != NULL)
3155 {
3156 destroying = TRUE; /* ignore WM_DESTROY message now */
3157 DestroyWindow(s_hwnd);
3158 }
3159
3160#ifdef GLOBAL_IME
3161 global_ime_end();
3162#endif
3163}
3164
3165 static char_u *
3166logfont2name(LOGFONT lf)
3167{
3168 char *p;
3169 char *res;
3170 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003171 char *quality_name;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003172 char *font_name = lf.lfFaceName;
3173
3174 charset_name = charset_id2name((int)lf.lfCharSet);
3175#ifdef FEAT_MBYTE
3176 /* Convert a font name from the current codepage to 'encoding'.
3177 * TODO: Use Wide APIs (including LOGFONTW) instead of ANSI APIs. */
3178 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
3179 {
3180 int len;
3181 acp_to_enc((char_u *)lf.lfFaceName, (int)strlen(lf.lfFaceName),
3182 (char_u **)&font_name, &len);
3183 }
3184#endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003185 quality_name = quality_id2name((int)lf.lfQuality);
3186
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003187 res = (char *)alloc((unsigned)(strlen(font_name) + 20
3188 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
3189 if (res != NULL)
3190 {
3191 p = res;
3192 /* make a normal font string out of the lf thing:*/
3193 sprintf((char *)p, "%s:h%d", font_name, pixels_to_points(
3194 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
3195 while (*p)
3196 {
3197 if (*p == ' ')
3198 *p = '_';
3199 ++p;
3200 }
3201 if (lf.lfItalic)
3202 STRCAT(p, ":i");
3203 if (lf.lfWeight >= FW_BOLD)
3204 STRCAT(p, ":b");
3205 if (lf.lfUnderline)
3206 STRCAT(p, ":u");
3207 if (lf.lfStrikeOut)
3208 STRCAT(p, ":s");
3209 if (charset_name != NULL)
3210 {
3211 STRCAT(p, ":c");
3212 STRCAT(p, charset_name);
3213 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003214 if (quality_name != NULL)
3215 {
3216 STRCAT(p, ":q");
3217 STRCAT(p, quality_name);
3218 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003219 }
3220
3221#ifdef FEAT_MBYTE
3222 if (font_name != lf.lfFaceName)
3223 vim_free(font_name);
3224#endif
3225 return (char_u *)res;
3226}
3227
3228
3229#ifdef FEAT_MBYTE_IME
3230/*
3231 * Set correct LOGFONT to IME. Use 'guifontwide' if available, otherwise use
3232 * 'guifont'
3233 */
3234 static void
3235update_im_font(void)
3236{
3237 LOGFONT lf_wide;
3238
3239 if (p_guifontwide != NULL && *p_guifontwide != NUL
3240 && gui.wide_font != NOFONT
3241 && GetObject((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
3242 norm_logfont = lf_wide;
3243 else
3244 norm_logfont = sub_logfont;
3245 im_set_font(&norm_logfont);
3246}
3247#endif
3248
3249#ifdef FEAT_MBYTE
3250/*
3251 * Handler of gui.wide_font (p_guifontwide) changed notification.
3252 */
3253 void
3254gui_mch_wide_font_changed(void)
3255{
3256 LOGFONT lf;
3257
3258# ifdef FEAT_MBYTE_IME
3259 update_im_font();
3260# endif
3261
3262 gui_mch_free_font(gui.wide_ital_font);
3263 gui.wide_ital_font = NOFONT;
3264 gui_mch_free_font(gui.wide_bold_font);
3265 gui.wide_bold_font = NOFONT;
3266 gui_mch_free_font(gui.wide_boldital_font);
3267 gui.wide_boldital_font = NOFONT;
3268
3269 if (gui.wide_font
3270 && GetObject((HFONT)gui.wide_font, sizeof(lf), &lf))
3271 {
3272 if (!lf.lfItalic)
3273 {
3274 lf.lfItalic = TRUE;
3275 gui.wide_ital_font = get_font_handle(&lf);
3276 lf.lfItalic = FALSE;
3277 }
3278 if (lf.lfWeight < FW_BOLD)
3279 {
3280 lf.lfWeight = FW_BOLD;
3281 gui.wide_bold_font = get_font_handle(&lf);
3282 if (!lf.lfItalic)
3283 {
3284 lf.lfItalic = TRUE;
3285 gui.wide_boldital_font = get_font_handle(&lf);
3286 }
3287 }
3288 }
3289}
3290#endif
3291
3292/*
3293 * Initialise vim to use the font with the given name.
3294 * Return FAIL if the font could not be loaded, OK otherwise.
3295 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003296 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003297gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003298{
3299 LOGFONT lf;
3300 GuiFont font = NOFONT;
3301 char_u *p;
3302
3303 /* Load the font */
3304 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3305 font = get_font_handle(&lf);
3306 if (font == NOFONT)
3307 return FAIL;
3308
3309 if (font_name == NULL)
3310 font_name = (char_u *)lf.lfFaceName;
3311#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3312 norm_logfont = lf;
3313 sub_logfont = lf;
3314#endif
3315#ifdef FEAT_MBYTE_IME
3316 update_im_font();
3317#endif
3318 gui_mch_free_font(gui.norm_font);
3319 gui.norm_font = font;
3320 current_font_height = lf.lfHeight;
3321 GetFontSize(font);
3322
3323 p = logfont2name(lf);
3324 if (p != NULL)
3325 {
3326 hl_set_font_name(p);
3327
3328 /* When setting 'guifont' to "*" replace it with the actual font name.
3329 * */
3330 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3331 {
3332 vim_free(p_guifont);
3333 p_guifont = p;
3334 }
3335 else
3336 vim_free(p);
3337 }
3338
3339 gui_mch_free_font(gui.ital_font);
3340 gui.ital_font = NOFONT;
3341 gui_mch_free_font(gui.bold_font);
3342 gui.bold_font = NOFONT;
3343 gui_mch_free_font(gui.boldital_font);
3344 gui.boldital_font = NOFONT;
3345
3346 if (!lf.lfItalic)
3347 {
3348 lf.lfItalic = TRUE;
3349 gui.ital_font = get_font_handle(&lf);
3350 lf.lfItalic = FALSE;
3351 }
3352 if (lf.lfWeight < FW_BOLD)
3353 {
3354 lf.lfWeight = FW_BOLD;
3355 gui.bold_font = get_font_handle(&lf);
3356 if (!lf.lfItalic)
3357 {
3358 lf.lfItalic = TRUE;
3359 gui.boldital_font = get_font_handle(&lf);
3360 }
3361 }
3362
3363 return OK;
3364}
3365
3366#ifndef WPF_RESTORETOMAXIMIZED
3367# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3368#endif
3369
3370/*
3371 * Return TRUE if the GUI window is maximized, filling the whole screen.
3372 */
3373 int
3374gui_mch_maximized(void)
3375{
3376 WINDOWPLACEMENT wp;
3377
3378 wp.length = sizeof(WINDOWPLACEMENT);
3379 if (GetWindowPlacement(s_hwnd, &wp))
3380 return wp.showCmd == SW_SHOWMAXIMIZED
3381 || (wp.showCmd == SW_SHOWMINIMIZED
3382 && wp.flags == WPF_RESTORETOMAXIMIZED);
3383
3384 return 0;
3385}
3386
3387/*
3388 * Called when the font changed while the window is maximized. Compute the
3389 * new Rows and Columns. This is like resizing the window.
3390 */
3391 void
3392gui_mch_newfont(void)
3393{
3394 RECT rect;
3395
3396 GetWindowRect(s_hwnd, &rect);
3397 if (win_socket_id == 0)
3398 {
3399 gui_resize_shell(rect.right - rect.left
3400 - (GetSystemMetrics(SM_CXFRAME) +
3401 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3402 rect.bottom - rect.top
3403 - (GetSystemMetrics(SM_CYFRAME) +
3404 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3405 - GetSystemMetrics(SM_CYCAPTION)
3406#ifdef FEAT_MENU
3407 - gui_mswin_get_menu_height(FALSE)
3408#endif
3409 );
3410 }
3411 else
3412 {
3413 /* Inside another window, don't use the frame and border. */
3414 gui_resize_shell(rect.right - rect.left,
3415 rect.bottom - rect.top
3416#ifdef FEAT_MENU
3417 - gui_mswin_get_menu_height(FALSE)
3418#endif
3419 );
3420 }
3421}
3422
3423/*
3424 * Set the window title
3425 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003426 void
3427gui_mch_settitle(
3428 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003429 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003430{
3431 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3432}
3433
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003434#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003435/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3436 * misc2.c! */
3437static LPCSTR mshape_idcs[] =
3438{
3439 IDC_ARROW, /* arrow */
3440 MAKEINTRESOURCE(0), /* blank */
3441 IDC_IBEAM, /* beam */
3442 IDC_SIZENS, /* updown */
3443 IDC_SIZENS, /* udsizing */
3444 IDC_SIZEWE, /* leftright */
3445 IDC_SIZEWE, /* lrsizing */
3446 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003447 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003448 IDC_ARROW, /* crosshair */
3449 IDC_ARROW, /* hand1 */
3450 IDC_ARROW, /* hand2 */
3451 IDC_ARROW, /* pencil */
3452 IDC_ARROW, /* question */
3453 IDC_ARROW, /* right-arrow */
3454 IDC_UPARROW, /* up-arrow */
3455 IDC_ARROW /* last one */
3456};
3457
3458 void
3459mch_set_mouse_shape(int shape)
3460{
3461 LPCSTR idc;
3462
3463 if (shape == MSHAPE_HIDE)
3464 ShowCursor(FALSE);
3465 else
3466 {
3467 if (shape >= MSHAPE_NUMBERED)
3468 idc = IDC_ARROW;
3469 else
3470 idc = mshape_idcs[shape];
3471#ifdef SetClassLongPtr
3472 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3473#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003474 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003475#endif
3476 if (!p_mh)
3477 {
3478 POINT mp;
3479
3480 /* Set the position to make it redrawn with the new shape. */
3481 (void)GetCursorPos((LPPOINT)&mp);
3482 (void)SetCursorPos(mp.x, mp.y);
3483 ShowCursor(TRUE);
3484 }
3485 }
3486}
3487#endif
3488
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003489#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003490/*
3491 * The file browser exists in two versions: with "W" uses wide characters,
3492 * without "W" the current codepage. When FEAT_MBYTE is defined and on
3493 * Windows NT/2000/XP the "W" functions are used.
3494 */
3495
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003496# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003497/*
3498 * Wide version of convert_filter().
3499 */
3500 static WCHAR *
3501convert_filterW(char_u *s)
3502{
3503 char_u *tmp;
3504 int len;
3505 WCHAR *res;
3506
3507 tmp = convert_filter(s);
3508 if (tmp == NULL)
3509 return NULL;
3510 len = (int)STRLEN(s) + 3;
3511 res = enc_to_utf16(tmp, &len);
3512 vim_free(tmp);
3513 return res;
3514}
3515
3516/*
3517 * Wide version of gui_mch_browse(). Keep in sync!
3518 */
3519 static char_u *
3520gui_mch_browseW(
3521 int saving,
3522 char_u *title,
3523 char_u *dflt,
3524 char_u *ext,
3525 char_u *initdir,
3526 char_u *filter)
3527{
3528 /* We always use the wide function. This means enc_to_utf16() must work,
3529 * otherwise it fails miserably! */
3530 OPENFILENAMEW fileStruct;
3531 WCHAR fileBuf[MAXPATHL];
3532 WCHAR *wp;
3533 int i;
3534 WCHAR *titlep = NULL;
3535 WCHAR *extp = NULL;
3536 WCHAR *initdirp = NULL;
3537 WCHAR *filterp;
3538 char_u *p;
3539
3540 if (dflt == NULL)
3541 fileBuf[0] = NUL;
3542 else
3543 {
3544 wp = enc_to_utf16(dflt, NULL);
3545 if (wp == NULL)
3546 fileBuf[0] = NUL;
3547 else
3548 {
3549 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3550 fileBuf[i] = wp[i];
3551 fileBuf[i] = NUL;
3552 vim_free(wp);
3553 }
3554 }
3555
3556 /* Convert the filter to Windows format. */
3557 filterp = convert_filterW(filter);
3558
3559 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003560# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003561 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003562 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003563# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003564 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003565# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003566
3567 if (title != NULL)
3568 titlep = enc_to_utf16(title, NULL);
3569 fileStruct.lpstrTitle = titlep;
3570
3571 if (ext != NULL)
3572 extp = enc_to_utf16(ext, NULL);
3573 fileStruct.lpstrDefExt = extp;
3574
3575 fileStruct.lpstrFile = fileBuf;
3576 fileStruct.nMaxFile = MAXPATHL;
3577 fileStruct.lpstrFilter = filterp;
3578 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3579 /* has an initial dir been specified? */
3580 if (initdir != NULL && *initdir != NUL)
3581 {
3582 /* Must have backslashes here, no matter what 'shellslash' says */
3583 initdirp = enc_to_utf16(initdir, NULL);
3584 if (initdirp != NULL)
3585 {
3586 for (wp = initdirp; *wp != NUL; ++wp)
3587 if (*wp == '/')
3588 *wp = '\\';
3589 }
3590 fileStruct.lpstrInitialDir = initdirp;
3591 }
3592
3593 /*
3594 * TODO: Allow selection of multiple files. Needs another arg to this
3595 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3596 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3597 * files that don't exist yet, so I haven't put it in. What about
3598 * OFN_PATHMUSTEXIST?
3599 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3600 */
3601 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003602# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003603 if (curbuf->b_p_bin)
3604 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003605# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003606 if (saving)
3607 {
3608 if (!GetSaveFileNameW(&fileStruct))
3609 return NULL;
3610 }
3611 else
3612 {
3613 if (!GetOpenFileNameW(&fileStruct))
3614 return NULL;
3615 }
3616
3617 vim_free(filterp);
3618 vim_free(initdirp);
3619 vim_free(titlep);
3620 vim_free(extp);
3621
3622 /* Convert from UCS2 to 'encoding'. */
3623 p = utf16_to_enc(fileBuf, NULL);
3624 if (p != NULL)
3625 /* when out of memory we get garbage for non-ASCII chars */
3626 STRCPY(fileBuf, p);
3627 vim_free(p);
3628
3629 /* Give focus back to main window (when using MDI). */
3630 SetFocus(s_hwnd);
3631
3632 /* Shorten the file name if possible */
3633 return vim_strsave(shorten_fname1((char_u *)fileBuf));
3634}
3635# endif /* FEAT_MBYTE */
3636
3637
3638/*
3639 * Convert the string s to the proper format for a filter string by replacing
3640 * the \t and \n delimiters with \0.
3641 * Returns the converted string in allocated memory.
3642 *
3643 * Keep in sync with convert_filterW() above!
3644 */
3645 static char_u *
3646convert_filter(char_u *s)
3647{
3648 char_u *res;
3649 unsigned s_len = (unsigned)STRLEN(s);
3650 unsigned i;
3651
3652 res = alloc(s_len + 3);
3653 if (res != NULL)
3654 {
3655 for (i = 0; i < s_len; ++i)
3656 if (s[i] == '\t' || s[i] == '\n')
3657 res[i] = '\0';
3658 else
3659 res[i] = s[i];
3660 res[s_len] = NUL;
3661 /* Add two extra NULs to make sure it's properly terminated. */
3662 res[s_len + 1] = NUL;
3663 res[s_len + 2] = NUL;
3664 }
3665 return res;
3666}
3667
3668/*
3669 * Select a directory.
3670 */
3671 char_u *
3672gui_mch_browsedir(char_u *title, char_u *initdir)
3673{
3674 /* We fake this: Use a filter that doesn't select anything and a default
3675 * file name that won't be used. */
3676 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3677 initdir, (char_u *)_("Directory\t*.nothing\n"));
3678}
3679
3680/*
3681 * Pop open a file browser and return the file selected, in allocated memory,
3682 * or NULL if Cancel is hit.
3683 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3684 * title - Title message for the file browser dialog.
3685 * dflt - Default name of file.
3686 * ext - Default extension to be added to files without extensions.
3687 * initdir - directory in which to open the browser (NULL = current dir)
3688 * filter - Filter for matched files to choose from.
3689 *
3690 * Keep in sync with gui_mch_browseW() above!
3691 */
3692 char_u *
3693gui_mch_browse(
3694 int saving,
3695 char_u *title,
3696 char_u *dflt,
3697 char_u *ext,
3698 char_u *initdir,
3699 char_u *filter)
3700{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003701# ifdef FEAT_MBYTE
3702 return gui_mch_browseW(saving, title, dflt, ext, initdir, filter);
3703# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003704 OPENFILENAME fileStruct;
3705 char_u fileBuf[MAXPATHL];
3706 char_u *initdirp = NULL;
3707 char_u *filterp;
3708 char_u *p;
3709
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003710 if (dflt == NULL)
3711 fileBuf[0] = NUL;
3712 else
3713 vim_strncpy(fileBuf, dflt, MAXPATHL - 1);
3714
3715 /* Convert the filter to Windows format. */
3716 filterp = convert_filter(filter);
3717
3718 vim_memset(&fileStruct, 0, sizeof(OPENFILENAME));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003719# ifdef OPENFILENAME_SIZE_VERSION_400
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003720 /* be compatible with Windows NT 4.0 */
3721 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003722# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003723 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003724# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003725
3726 fileStruct.lpstrTitle = (LPSTR)title;
3727 fileStruct.lpstrDefExt = (LPSTR)ext;
3728
3729 fileStruct.lpstrFile = (LPSTR)fileBuf;
3730 fileStruct.nMaxFile = MAXPATHL;
3731 fileStruct.lpstrFilter = (LPSTR)filterp;
3732 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3733 /* has an initial dir been specified? */
3734 if (initdir != NULL && *initdir != NUL)
3735 {
3736 /* Must have backslashes here, no matter what 'shellslash' says */
3737 initdirp = vim_strsave(initdir);
3738 if (initdirp != NULL)
3739 for (p = initdirp; *p != NUL; ++p)
3740 if (*p == '/')
3741 *p = '\\';
3742 fileStruct.lpstrInitialDir = (LPSTR)initdirp;
3743 }
3744
3745 /*
3746 * TODO: Allow selection of multiple files. Needs another arg to this
3747 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3748 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3749 * files that don't exist yet, so I haven't put it in. What about
3750 * OFN_PATHMUSTEXIST?
3751 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3752 */
3753 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003754# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003755 if (curbuf->b_p_bin)
3756 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003757# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003758 if (saving)
3759 {
3760 if (!GetSaveFileName(&fileStruct))
3761 return NULL;
3762 }
3763 else
3764 {
3765 if (!GetOpenFileName(&fileStruct))
3766 return NULL;
3767 }
3768
3769 vim_free(filterp);
3770 vim_free(initdirp);
3771
3772 /* Give focus back to main window (when using MDI). */
3773 SetFocus(s_hwnd);
3774
3775 /* Shorten the file name if possible */
3776 return vim_strsave(shorten_fname1((char_u *)fileBuf));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003777# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003778}
3779#endif /* FEAT_BROWSE */
3780
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003781 static void
3782_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003783 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003784 HDROP hDrop)
3785{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003786#define BUFPATHLEN _MAX_PATH
3787#define DRAGQVAL 0xFFFFFFFF
3788#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003789 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaar4033c552017-09-16 20:54:51 +02003790#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003791 char szFile[BUFPATHLEN];
3792 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3793 UINT i;
3794 char_u **fnames;
3795 POINT pt;
3796 int_u modifiers = 0;
3797
3798 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3799
3800 /* Obtain dropped position */
3801 DragQueryPoint(hDrop, &pt);
3802 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3803
3804 reset_VIsual();
3805
3806 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3807
3808 if (fnames != NULL)
3809 for (i = 0; i < cFiles; ++i)
3810 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003811#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003812 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3813 fnames[i] = utf16_to_enc(wszFile, NULL);
3814 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02003815#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003816 {
3817 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3818 fnames[i] = vim_strsave((char_u *)szFile);
3819 }
3820 }
3821
3822 DragFinish(hDrop);
3823
3824 if (fnames != NULL)
3825 {
3826 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3827 modifiers |= MOUSE_SHIFT;
3828 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3829 modifiers |= MOUSE_CTRL;
3830 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3831 modifiers |= MOUSE_ALT;
3832
3833 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3834
3835 s_need_activate = TRUE;
3836 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003837}
3838
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003839 static int
3840_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003841 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003842 HWND hwndCtl,
3843 UINT code,
3844 int pos)
3845{
3846 static UINT prev_code = 0; /* code of previous call */
3847 scrollbar_T *sb, *sb_info;
3848 long val;
3849 int dragging = FALSE;
3850 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003851 SCROLLINFO si;
3852
3853 si.cbSize = sizeof(si);
3854 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003855
3856 sb = gui_mswin_find_scrollbar(hwndCtl);
3857 if (sb == NULL)
3858 return 0;
3859
3860 if (sb->wp != NULL) /* Left or right scrollbar */
3861 {
3862 /*
3863 * Careful: need to get scrollbar info out of first (left) scrollbar
3864 * for window, but keep real scrollbar too because we must pass it to
3865 * gui_drag_scrollbar().
3866 */
3867 sb_info = &sb->wp->w_scrollbars[0];
3868 }
3869 else /* Bottom scrollbar */
3870 sb_info = sb;
3871 val = sb_info->value;
3872
3873 switch (code)
3874 {
3875 case SB_THUMBTRACK:
3876 val = pos;
3877 dragging = TRUE;
3878 if (sb->scroll_shift > 0)
3879 val <<= sb->scroll_shift;
3880 break;
3881 case SB_LINEDOWN:
3882 val++;
3883 break;
3884 case SB_LINEUP:
3885 val--;
3886 break;
3887 case SB_PAGEDOWN:
3888 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3889 break;
3890 case SB_PAGEUP:
3891 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3892 break;
3893 case SB_TOP:
3894 val = 0;
3895 break;
3896 case SB_BOTTOM:
3897 val = sb_info->max;
3898 break;
3899 case SB_ENDSCROLL:
3900 if (prev_code == SB_THUMBTRACK)
3901 {
3902 /*
3903 * "pos" only gives us 16-bit data. In case of large file,
3904 * use GetScrollPos() which returns 32-bit. Unfortunately it
3905 * is not valid while the scrollbar is being dragged.
3906 */
3907 val = GetScrollPos(hwndCtl, SB_CTL);
3908 if (sb->scroll_shift > 0)
3909 val <<= sb->scroll_shift;
3910 }
3911 break;
3912
3913 default:
3914 /* TRACE("Unknown scrollbar event %d\n", code); */
3915 return 0;
3916 }
3917 prev_code = code;
3918
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003919 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3920 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003921
3922 /*
3923 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3924 */
3925 if (sb->wp != NULL)
3926 {
3927 scrollbar_T *sba = sb->wp->w_scrollbars;
3928 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3929
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003930 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003931 }
3932
3933 /* Don't let us be interrupted here by another message. */
3934 s_busy_processing = TRUE;
3935
3936 /* When "allow_scrollbar" is FALSE still need to remember the new
3937 * position, but don't actually scroll by setting "dont_scroll". */
3938 dont_scroll = !allow_scrollbar;
3939
3940 gui_drag_scrollbar(sb, val, dragging);
3941
3942 s_busy_processing = FALSE;
3943 dont_scroll = dont_scroll_save;
3944
3945 return 0;
3946}
3947
3948
3949/*
3950 * Get command line arguments.
3951 * Use "prog" as the name of the program and "cmdline" as the arguments.
3952 * Copy the arguments to allocated memory.
3953 * Return the number of arguments (including program name).
3954 * Return pointers to the arguments in "argvp". Memory is allocated with
3955 * malloc(), use free() instead of vim_free().
3956 * Return pointer to buffer in "tofree".
3957 * Returns zero when out of memory.
3958 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003959 int
3960get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
3961{
3962 int i;
3963 char *p;
3964 char *progp;
3965 char *pnew = NULL;
3966 char *newcmdline;
3967 int inquote;
3968 int argc;
3969 char **argv = NULL;
3970 int round;
3971
3972 *tofree = NULL;
3973
3974#ifdef FEAT_MBYTE
3975 /* Try using the Unicode version first, it takes care of conversion when
3976 * 'encoding' is changed. */
3977 argc = get_cmd_argsW(&argv);
3978 if (argc != 0)
3979 goto done;
3980#endif
3981
3982 /* Handle the program name. Remove the ".exe" extension, and find the 1st
3983 * non-space. */
3984 p = strrchr(prog, '.');
3985 if (p != NULL)
3986 *p = NUL;
3987 for (progp = prog; *progp == ' '; ++progp)
3988 ;
3989
3990 /* The command line is copied to allocated memory, so that we can change
3991 * it. Add the size of the string, the separating NUL and a terminating
3992 * NUL. */
3993 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
3994 if (newcmdline == NULL)
3995 return 0;
3996
3997 /*
3998 * First round: count the number of arguments ("pnew" == NULL).
3999 * Second round: produce the arguments.
4000 */
4001 for (round = 1; round <= 2; ++round)
4002 {
4003 /* First argument is the program name. */
4004 if (pnew != NULL)
4005 {
4006 argv[0] = pnew;
4007 strcpy(pnew, progp);
4008 pnew += strlen(pnew);
4009 *pnew++ = NUL;
4010 }
4011
4012 /*
4013 * Isolate each argument and put it in argv[].
4014 */
4015 p = cmdline;
4016 argc = 1;
4017 while (*p != NUL)
4018 {
4019 inquote = FALSE;
4020 if (pnew != NULL)
4021 argv[argc] = pnew;
4022 ++argc;
4023 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
4024 {
4025 /* Backslashes are only special when followed by a double
4026 * quote. */
4027 i = (int)strspn(p, "\\");
4028 if (p[i] == '"')
4029 {
4030 /* Halve the number of backslashes. */
4031 if (i > 1 && pnew != NULL)
4032 {
4033 vim_memset(pnew, '\\', i / 2);
4034 pnew += i / 2;
4035 }
4036
4037 /* Even nr of backslashes toggles quoting, uneven copies
4038 * the double quote. */
4039 if ((i & 1) == 0)
4040 inquote = !inquote;
4041 else if (pnew != NULL)
4042 *pnew++ = '"';
4043 p += i + 1;
4044 }
4045 else if (i > 0)
4046 {
4047 /* Copy span of backslashes unmodified. */
4048 if (pnew != NULL)
4049 {
4050 vim_memset(pnew, '\\', i);
4051 pnew += i;
4052 }
4053 p += i;
4054 }
4055 else
4056 {
4057 if (pnew != NULL)
4058 *pnew++ = *p;
4059#ifdef FEAT_MBYTE
4060 /* Can't use mb_* functions, because 'encoding' is not
4061 * initialized yet here. */
4062 if (IsDBCSLeadByte(*p))
4063 {
4064 ++p;
4065 if (pnew != NULL)
4066 *pnew++ = *p;
4067 }
4068#endif
4069 ++p;
4070 }
4071 }
4072
4073 if (pnew != NULL)
4074 *pnew++ = NUL;
4075 while (*p == ' ' || *p == '\t')
4076 ++p; /* advance until a non-space */
4077 }
4078
4079 if (round == 1)
4080 {
4081 argv = (char **)malloc((argc + 1) * sizeof(char *));
4082 if (argv == NULL )
4083 {
4084 free(newcmdline);
4085 return 0; /* malloc error */
4086 }
4087 pnew = newcmdline;
4088 *tofree = newcmdline;
4089 }
4090 }
4091
4092#ifdef FEAT_MBYTE
4093done:
4094#endif
4095 argv[argc] = NULL; /* NULL-terminated list */
4096 *argvp = argv;
4097 return argc;
4098}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099
4100#ifdef FEAT_XPM_W32
4101# include "xpm_w32.h"
4102#endif
4103
4104#ifdef PROTO
4105# define WINAPI
4106#endif
4107
4108#ifdef __MINGW32__
4109/*
4110 * Add a lot of missing defines.
4111 * They are not always missing, we need the #ifndef's.
4112 */
4113# ifndef _cdecl
4114# define _cdecl
4115# endif
4116# ifndef IsMinimized
4117# define IsMinimized(hwnd) IsIconic(hwnd)
4118# endif
4119# ifndef IsMaximized
4120# define IsMaximized(hwnd) IsZoomed(hwnd)
4121# endif
4122# ifndef SelectFont
4123# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
4124# endif
4125# ifndef GetStockBrush
4126# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
4127# endif
4128# ifndef DeleteBrush
4129# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
4130# endif
4131
4132# ifndef HANDLE_WM_RBUTTONDBLCLK
4133# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4134 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4135# endif
4136# ifndef HANDLE_WM_MBUTTONUP
4137# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
4138 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4139# endif
4140# ifndef HANDLE_WM_MBUTTONDBLCLK
4141# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4142 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4143# endif
4144# ifndef HANDLE_WM_LBUTTONDBLCLK
4145# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4146 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4147# endif
4148# ifndef HANDLE_WM_RBUTTONDOWN
4149# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
4150 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4151# endif
4152# ifndef HANDLE_WM_MOUSEMOVE
4153# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
4154 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4155# endif
4156# ifndef HANDLE_WM_RBUTTONUP
4157# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
4158 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4159# endif
4160# ifndef HANDLE_WM_MBUTTONDOWN
4161# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
4162 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4163# endif
4164# ifndef HANDLE_WM_LBUTTONUP
4165# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
4166 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4167# endif
4168# ifndef HANDLE_WM_LBUTTONDOWN
4169# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
4170 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4171# endif
4172# ifndef HANDLE_WM_SYSCHAR
4173# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
4174 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4175# endif
4176# ifndef HANDLE_WM_ACTIVATEAPP
4177# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
4178 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
4179# endif
4180# ifndef HANDLE_WM_WINDOWPOSCHANGING
4181# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
4182 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
4183# endif
4184# ifndef HANDLE_WM_VSCROLL
4185# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
4186 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4187# endif
4188# ifndef HANDLE_WM_SETFOCUS
4189# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
4190 ((fn)((hwnd), (HWND)(wParam)), 0L)
4191# endif
4192# ifndef HANDLE_WM_KILLFOCUS
4193# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
4194 ((fn)((hwnd), (HWND)(wParam)), 0L)
4195# endif
4196# ifndef HANDLE_WM_HSCROLL
4197# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
4198 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4199# endif
4200# ifndef HANDLE_WM_DROPFILES
4201# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
4202 ((fn)((hwnd), (HDROP)(wParam)), 0L)
4203# endif
4204# ifndef HANDLE_WM_CHAR
4205# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
4206 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4207# endif
4208# ifndef HANDLE_WM_SYSDEADCHAR
4209# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
4210 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4211# endif
4212# ifndef HANDLE_WM_DEADCHAR
4213# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4214 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4215# endif
4216#endif /* __MINGW32__ */
4217
4218
4219/* Some parameters for tearoff menus. All in pixels. */
4220#define TEAROFF_PADDING_X 2
4221#define TEAROFF_BUTTON_PAD_X 8
4222#define TEAROFF_MIN_WIDTH 200
4223#define TEAROFF_SUBMENU_LABEL ">>"
4224#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4225
4226
4227/* For the Intellimouse: */
4228#ifndef WM_MOUSEWHEEL
4229#define WM_MOUSEWHEEL 0x20a
4230#endif
4231
4232
4233#ifdef FEAT_BEVAL
4234# define ID_BEVAL_TOOLTIP 200
4235# define BEVAL_TEXT_LEN MAXPATHL
4236
Bram Moolenaar167632f2010-05-26 21:42:54 +02004237#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004238/* Work around old versions of basetsd.h which wrongly declares
4239 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02004240# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00004241# define UINT_PTR UINT
4242#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004243
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004244static void make_tooltip(BalloonEval *beval, char *text, POINT pt);
4245static void delete_tooltip(BalloonEval *beval);
4246static VOID CALLBACK BevalTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004247
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004249static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004251
Bram Moolenaar82881492012-11-20 16:53:39 +01004252
4253/* cproto fails on missing include files */
4254#ifndef PROTO
4255
Bram Moolenaar45360022005-07-21 21:08:21 +00004256/*
4257 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004258 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004259 */
Bram Moolenaar82881492012-11-20 16:53:39 +01004260# include <pshpack1.h>
4261
4262#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004263
4264typedef struct _DllVersionInfo
4265{
4266 DWORD cbSize;
4267 DWORD dwMajorVersion;
4268 DWORD dwMinorVersion;
4269 DWORD dwBuildNumber;
4270 DWORD dwPlatformID;
4271} DLLVERSIONINFO;
4272
Bram Moolenaar82881492012-11-20 16:53:39 +01004273#ifndef PROTO
4274# include <poppack.h>
4275#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004276
Bram Moolenaar45360022005-07-21 21:08:21 +00004277typedef struct tagTOOLINFOA_NEW
4278{
4279 UINT cbSize;
4280 UINT uFlags;
4281 HWND hwnd;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004282 UINT_PTR uId;
Bram Moolenaar45360022005-07-21 21:08:21 +00004283 RECT rect;
4284 HINSTANCE hinst;
4285 LPSTR lpszText;
4286 LPARAM lParam;
4287} TOOLINFO_NEW;
4288
4289typedef struct tagNMTTDISPINFO_NEW
4290{
4291 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004292 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004293 char szText[80];
4294 HINSTANCE hinst;
4295 UINT uFlags;
4296 LPARAM lParam;
4297} NMTTDISPINFO_NEW;
4298
Bram Moolenaar45360022005-07-21 21:08:21 +00004299typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4300#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004301# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302#endif
4303
Bram Moolenaar45360022005-07-21 21:08:21 +00004304#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004305# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004306#endif
4307
4308#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004309# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004310#endif
4311
4312#endif /* defined(FEAT_BEVAL) */
4313
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004314#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4315/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4316 * it here if LPNMTTDISPINFO isn't defined.
4317 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4318 * _MSC_VER. */
4319# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4320typedef struct tagNMTTDISPINFOA {
4321 NMHDR hdr;
4322 LPSTR lpszText;
4323 char szText[80];
4324 HINSTANCE hinst;
4325 UINT uFlags;
4326 LPARAM lParam;
4327} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4328# define LPNMTTDISPINFO LPNMTTDISPINFOA
4329
4330# ifdef FEAT_MBYTE
4331typedef struct tagNMTTDISPINFOW {
4332 NMHDR hdr;
4333 LPWSTR lpszText;
4334 WCHAR szText[80];
4335 HINSTANCE hinst;
4336 UINT uFlags;
4337 LPARAM lParam;
4338} NMTTDISPINFOW, *LPNMTTDISPINFOW;
4339# endif
4340# endif
4341#endif
4342
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004343#ifndef TTN_GETDISPINFOW
4344# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4345#endif
4346
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347/* Local variables: */
4348
4349#ifdef FEAT_MENU
4350static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352
4353/*
4354 * Use the system font for dialogs and tear-off menus. Remove this line to
4355 * use DLG_FONT_NAME.
4356 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004357#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358
4359#define VIM_NAME "vim"
4360#define VIM_CLASS "Vim"
4361#define VIM_CLASSW L"Vim"
4362
4363/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4364 * thus there should be room for every dialog. For tearoffs it's made bigger
4365 * when needed. */
4366#define DLG_ALLOC_SIZE 16 * 1024
4367
4368/*
4369 * stuff for dialogs, menus, tearoffs etc.
4370 */
4371static LRESULT APIENTRY dialog_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004372#ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373static LRESULT APIENTRY tearoff_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375static PWORD
4376add_dialog_element(
4377 PWORD p,
4378 DWORD lStyle,
4379 WORD x,
4380 WORD y,
4381 WORD w,
4382 WORD h,
4383 WORD Id,
4384 WORD clss,
4385 const char *caption);
4386static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004387static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004388#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391static void get_dialog_font_metrics(void);
4392
4393static int dialog_default_button = -1;
4394
4395/* Intellimouse support */
4396static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397
4398static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4399#ifdef FEAT_TOOLBAR
4400static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004401static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402static int get_toolbar_bitmap(vimmenu_T *menu);
4403#endif
4404
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004405#ifdef FEAT_GUI_TABLINE
4406static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004407static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004408#endif
4409
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410#ifdef FEAT_MBYTE_IME
4411static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4412static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4413#endif
4414#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4415# ifdef NOIME
4416typedef struct tagCOMPOSITIONFORM {
4417 DWORD dwStyle;
4418 POINT ptCurrentPos;
4419 RECT rcArea;
4420} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4421typedef HANDLE HIMC;
4422# endif
4423
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004424static HINSTANCE hLibImm = NULL;
4425static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4426static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4427static HIMC (WINAPI *pImmGetContext)(HWND);
4428static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4429static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4430static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4431static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
4432static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
4433static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
4434static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4435static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004436static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437static void dyn_imm_load(void);
4438#else
4439# define pImmGetCompositionStringA ImmGetCompositionStringA
4440# define pImmGetCompositionStringW ImmGetCompositionStringW
4441# define pImmGetContext ImmGetContext
4442# define pImmAssociateContext ImmAssociateContext
4443# define pImmReleaseContext ImmReleaseContext
4444# define pImmGetOpenStatus ImmGetOpenStatus
4445# define pImmSetOpenStatus ImmSetOpenStatus
4446# define pImmGetCompositionFont ImmGetCompositionFontA
4447# define pImmSetCompositionFont ImmSetCompositionFontA
4448# define pImmSetCompositionWindow ImmSetCompositionWindow
4449# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004450# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451#endif
4452
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453#ifdef FEAT_MENU
4454/*
4455 * Figure out how high the menu bar is at the moment.
4456 */
4457 static int
4458gui_mswin_get_menu_height(
4459 int fix_window) /* If TRUE, resize window if menu height changed */
4460{
4461 static int old_menu_height = -1;
4462
4463 RECT rc1, rc2;
4464 int num;
4465 int menu_height;
4466
4467 if (gui.menu_is_active)
4468 num = GetMenuItemCount(s_menuBar);
4469 else
4470 num = 0;
4471
4472 if (num == 0)
4473 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004474 else if (IsMinimized(s_hwnd))
4475 {
4476 /* The height of the menu cannot be determined while the window is
4477 * minimized. Take the previous height if the menu is changed in that
4478 * state, to avoid that Vim's vertical window size accidentally
4479 * increases due to the unaccounted-for menu height. */
4480 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 else
4483 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004484 /*
4485 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4486 * seem to have been set yet, so menu wraps in default window
4487 * width which is very narrow. Instead just return height of a
4488 * single menu item. Will still be wrong when the menu really
4489 * should wrap over more than one line.
4490 */
4491 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4492 if (gui.starting)
4493 menu_height = rc1.bottom - rc1.top + 1;
4494 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004496 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4497 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 }
4499 }
4500
4501 if (fix_window && menu_height != old_menu_height)
4502 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004503 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 }
Bram Moolenaar71371b12015-03-24 17:57:12 +01004505 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506
4507 return menu_height;
4508}
4509#endif /*FEAT_MENU*/
4510
4511
4512/*
4513 * Setup for the Intellimouse
4514 */
4515 static void
4516init_mouse_wheel(void)
4517{
4518
4519#ifndef SPI_GETWHEELSCROLLLINES
4520# define SPI_GETWHEELSCROLLLINES 104
4521#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004522#ifndef SPI_SETWHEELSCROLLLINES
4523# define SPI_SETWHEELSCROLLLINES 105
4524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525
4526#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4527#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4528#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4529#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4530
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 mouse_scroll_lines = 3; /* reasonable default */
4532
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004533 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4534 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4535 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536}
4537
4538
4539/* Intellimouse wheel handler */
4540 static void
4541_OnMouseWheel(
4542 HWND hwnd,
4543 short zDelta)
4544{
4545/* Treat a mouse wheel event as if it were a scroll request */
4546 int i;
4547 int size;
4548 HWND hwndCtl;
4549
4550 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4551 {
4552 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4553 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4554 }
4555 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4556 {
4557 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4558 size = curwin->w_scrollbars[SBAR_LEFT].size;
4559 }
4560 else
4561 return;
4562
4563 size = curwin->w_height;
4564 if (mouse_scroll_lines == 0)
4565 init_mouse_wheel();
4566
4567 if (mouse_scroll_lines > 0
4568 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4569 {
4570 for (i = mouse_scroll_lines; i > 0; --i)
4571 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4572 }
4573 else
4574 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
4575}
4576
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004577#ifdef USE_SYSMENU_FONT
4578/*
4579 * Get Menu Font.
4580 * Return OK or FAIL.
4581 */
4582 static int
4583gui_w32_get_menu_font(LOGFONT *lf)
4584{
4585 NONCLIENTMETRICS nm;
4586
4587 nm.cbSize = sizeof(NONCLIENTMETRICS);
4588 if (!SystemParametersInfo(
4589 SPI_GETNONCLIENTMETRICS,
4590 sizeof(NONCLIENTMETRICS),
4591 &nm,
4592 0))
4593 return FAIL;
4594 *lf = nm.lfMenuFont;
4595 return OK;
4596}
4597#endif
4598
4599
4600#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4601/*
4602 * Set the GUI tabline font to the system menu font
4603 */
4604 static void
4605set_tabline_font(void)
4606{
4607 LOGFONT lfSysmenu;
4608 HFONT font;
4609 HWND hwnd;
4610 HDC hdc;
4611 HFONT hfntOld;
4612 TEXTMETRIC tm;
4613
4614 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4615 return;
4616
4617 font = CreateFontIndirect(&lfSysmenu);
4618
4619 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4620
4621 /*
4622 * Compute the height of the font used for the tab text
4623 */
4624 hwnd = GetDesktopWindow();
4625 hdc = GetWindowDC(hwnd);
4626 hfntOld = SelectFont(hdc, font);
4627
4628 GetTextMetrics(hdc, &tm);
4629
4630 SelectFont(hdc, hfntOld);
4631 ReleaseDC(hwnd, hdc);
4632
4633 /*
4634 * The space used by the tab border and the space between the tab label
4635 * and the tab border is included as 7.
4636 */
4637 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4638}
4639#endif
4640
Bram Moolenaar520470a2005-06-16 21:59:56 +00004641/*
4642 * Invoked when a setting was changed.
4643 */
4644 static LRESULT CALLBACK
4645_OnSettingChange(UINT n)
4646{
4647 if (n == SPI_SETWHEELSCROLLLINES)
4648 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4649 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004650#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4651 if (n == SPI_SETNONCLIENTMETRICS)
4652 set_tabline_font();
4653#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004654 return 0;
4655}
4656
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657#ifdef FEAT_NETBEANS_INTG
4658 static void
4659_OnWindowPosChanged(
4660 HWND hwnd,
4661 const LPWINDOWPOS lpwpos)
4662{
4663 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004664 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665
4666 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4667 || lpwpos->cx != cx || lpwpos->cy != cy))
4668 {
4669 x = lpwpos->x;
4670 y = lpwpos->y;
4671 cx = lpwpos->cx;
4672 cy = lpwpos->cy;
4673 netbeans_frame_moved(x, y);
4674 }
4675 /* Allow to send WM_SIZE and WM_MOVE */
4676 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4677}
4678#endif
4679
4680 static int
4681_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 UINT fwSide,
4683 LPRECT lprc)
4684{
4685 int w, h;
4686 int valid_w, valid_h;
4687 int w_offset, h_offset;
4688
4689 w = lprc->right - lprc->left;
4690 h = lprc->bottom - lprc->top;
4691 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4692 w_offset = w - valid_w;
4693 h_offset = h - valid_h;
4694
4695 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4696 || fwSide == WMSZ_BOTTOMLEFT)
4697 lprc->left += w_offset;
4698 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4699 || fwSide == WMSZ_BOTTOMRIGHT)
4700 lprc->right -= w_offset;
4701
4702 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4703 || fwSide == WMSZ_TOPRIGHT)
4704 lprc->top += h_offset;
4705 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4706 || fwSide == WMSZ_BOTTOMRIGHT)
4707 lprc->bottom -= h_offset;
4708 return TRUE;
4709}
4710
4711
4712
4713 static LRESULT CALLBACK
4714_WndProc(
4715 HWND hwnd,
4716 UINT uMsg,
4717 WPARAM wParam,
4718 LPARAM lParam)
4719{
4720 /*
4721 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4722 hwnd, uMsg, wParam, lParam);
4723 */
4724
4725 HandleMouseHide(uMsg, lParam);
4726
4727 s_uMsg = uMsg;
4728 s_wParam = wParam;
4729 s_lParam = lParam;
4730
4731 switch (uMsg)
4732 {
4733 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4734 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4735 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4736 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4737 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4738 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4739 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4740 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4741 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4742#ifdef FEAT_MENU
4743 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4744#endif
4745 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4746 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4747 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4748 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4749 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4750 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4751 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4752 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4753 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4754#ifdef FEAT_NETBEANS_INTG
4755 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4756#endif
4757
Bram Moolenaarafa24992006-03-27 20:58:26 +00004758#ifdef FEAT_GUI_TABLINE
4759 case WM_RBUTTONUP:
4760 {
4761 if (gui_mch_showing_tabline())
4762 {
4763 POINT pt;
4764 RECT rect;
4765
4766 /*
4767 * If the cursor is on the tabline, display the tab menu
4768 */
4769 GetCursorPos((LPPOINT)&pt);
4770 GetWindowRect(s_textArea, &rect);
4771 if (pt.y < rect.top)
4772 {
4773 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004774 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004775 }
4776 }
4777 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4778 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004779 case WM_LBUTTONDBLCLK:
4780 {
4781 /*
4782 * If the user double clicked the tabline, create a new tab
4783 */
4784 if (gui_mch_showing_tabline())
4785 {
4786 POINT pt;
4787 RECT rect;
4788
4789 GetCursorPos((LPPOINT)&pt);
4790 GetWindowRect(s_textArea, &rect);
4791 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004792 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004793 }
4794 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4795 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004796#endif
4797
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 case WM_QUERYENDSESSION: /* System wants to go down. */
4799 gui_shell_closed(); /* Will exit when no changed buffers. */
4800 return FALSE; /* Do NOT allow system to go down. */
4801
4802 case WM_ENDSESSION:
4803 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004804 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004806 return 0L;
4807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 break;
4809
4810 case WM_CHAR:
4811 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4812 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004813 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 return 0L;
4815
4816 case WM_SYSCHAR:
4817 /*
4818 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4819 * shortcut key, handle like a typed ALT key, otherwise call Windows
4820 * ALT key handling.
4821 */
4822#ifdef FEAT_MENU
4823 if ( !gui.menu_is_active
4824 || p_wak[0] == 'n'
4825 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4826 )
4827#endif
4828 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004829 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 return 0L;
4831 }
4832#ifdef FEAT_MENU
4833 else
4834 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4835#endif
4836
4837 case WM_SYSKEYUP:
4838#ifdef FEAT_MENU
4839 /* This used to be done only when menu is active: ALT key is used for
4840 * that. But that caused problems when menu is disabled and using
4841 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4842 * are received, mouse pointer remains hidden. */
4843 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4844#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004845 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846#endif
4847
4848 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004849 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850
4851 case WM_MOUSEWHEEL:
4852 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004853 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854
Bram Moolenaar520470a2005-06-16 21:59:56 +00004855 /* Notification for change in SystemParametersInfo() */
4856 case WM_SETTINGCHANGE:
4857 return _OnSettingChange((UINT)wParam);
4858
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004859#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 case WM_NOTIFY:
4861 switch (((LPNMHDR) lParam)->code)
4862 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004863# ifdef FEAT_MBYTE
4864 case TTN_GETDISPINFOW:
4865# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004866 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004868 LPNMHDR hdr = (LPNMHDR)lParam;
4869 char_u *str = NULL;
4870 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004871
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004872 vim_free(tt_text);
4873 tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004874
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004875# ifdef FEAT_GUI_TABLINE
4876 if (gui_mch_showing_tabline()
4877 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004878 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004879 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004880 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004881 * Mouse is over the GUI tabline. Display the
4882 * tooltip for the tab under the cursor
4883 *
4884 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004885 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004886 GetCursorPos(&pt);
4887 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004888 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004889 TCHITTESTINFO htinfo;
4890 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004891
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004892 /*
4893 * Get the tab under the cursor
4894 */
4895 htinfo.pt.x = pt.x;
4896 htinfo.pt.y = pt.y;
4897 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4898 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004899 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004900 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004901
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004902 tp = find_tabpage(idx + 1);
4903 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004904 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004905 get_tabline_label(tp, TRUE);
4906 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004907 }
4908 }
4909 }
4910 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004911# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004912# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004913# ifdef FEAT_GUI_TABLINE
4914 else
4915# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004917 UINT idButton;
4918 vimmenu_T *pMenu;
4919
4920 idButton = (UINT) hdr->idFrom;
4921 pMenu = gui_mswin_find_menu(root_menu, idButton);
4922 if (pMenu)
4923 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004925# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004926 if (str != NULL)
4927 {
4928# ifdef FEAT_MBYTE
4929 if (hdr->code == TTN_GETDISPINFOW)
4930 {
4931 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4932
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004933 /* Set the maximum width, this also enables using
4934 * \n for line break. */
4935 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4936 0, 500);
4937
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004938 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004939 lpdi->lpszText = tt_text;
4940 /* can't show tooltip if failed */
4941 }
4942 else
4943# endif
4944 {
4945 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4946
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004947 /* Set the maximum width, this also enables using
4948 * \n for line break. */
4949 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4950 0, 500);
4951
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004952 if (STRLEN(str) < sizeof(lpdi->szText)
4953 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004954 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004955 sizeof(lpdi->szText) - 1);
4956 else
4957 lpdi->lpszText = tt_text;
4958 }
4959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 }
4961 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004962# ifdef FEAT_GUI_TABLINE
4963 case TCN_SELCHANGE:
4964 if (gui_mch_showing_tabline()
4965 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004966 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004967 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004968 return 0L;
4969 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004970 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004971
4972 case NM_RCLICK:
4973 if (gui_mch_showing_tabline()
4974 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004975 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004976 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004977 return 0L;
4978 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004979 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004980# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004982# ifdef FEAT_GUI_TABLINE
4983 if (gui_mch_showing_tabline()
4984 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4985 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4986# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 break;
4988 }
4989 break;
4990#endif
4991#if defined(MENUHINTS) && defined(FEAT_MENU)
4992 case WM_MENUSELECT:
4993 if (((UINT) HIWORD(wParam)
4994 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4995 == MF_HILITE
4996 && (State & CMDLINE) == 0)
4997 {
4998 UINT idButton;
4999 vimmenu_T *pMenu;
5000 static int did_menu_tip = FALSE;
5001
5002 if (did_menu_tip)
5003 {
5004 msg_clr_cmdline();
5005 setcursor();
5006 out_flush();
5007 did_menu_tip = FALSE;
5008 }
5009
5010 idButton = (UINT)LOWORD(wParam);
5011 pMenu = gui_mswin_find_menu(root_menu, idButton);
5012 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
5013 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
5014 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005015 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005017 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 setcursor();
5019 out_flush();
5020 did_menu_tip = TRUE;
5021 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01005022 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
5024 break;
5025#endif
5026 case WM_NCHITTEST:
5027 {
5028 LRESULT result;
5029 int x, y;
5030 int xPos = GET_X_LPARAM(lParam);
5031
5032 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
5033 if (result == HTCLIENT)
5034 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005035#ifdef FEAT_GUI_TABLINE
5036 if (gui_mch_showing_tabline())
5037 {
5038 int yPos = GET_Y_LPARAM(lParam);
5039 RECT rct;
5040
5041 /* If the cursor is on the GUI tabline, don't process this
5042 * event */
5043 GetWindowRect(s_textArea, &rct);
5044 if (yPos < rct.top)
5045 return result;
5046 }
5047#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02005048 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 xPos -= x;
5050
5051 if (xPos < 48) /* <VN> TODO should use system metric? */
5052 return HTBOTTOMLEFT;
5053 else
5054 return HTBOTTOMRIGHT;
5055 }
5056 else
5057 return result;
5058 }
5059 /* break; notreached */
5060
5061#ifdef FEAT_MBYTE_IME
5062 case WM_IME_NOTIFY:
5063 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5064 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005065 return 1L;
5066
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 case WM_IME_COMPOSITION:
5068 if (!_OnImeComposition(hwnd, wParam, lParam))
5069 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005070 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071#endif
5072
5073 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005075 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 {
5077 _OnFindRepl();
5078 }
5079#endif
5080 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5081 }
5082
Bram Moolenaar2787ab92011-12-14 15:23:59 +01005083 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084}
5085
5086/*
5087 * End of call-back routines
5088 */
5089
5090/* parent window, if specified with -P */
5091HWND vim_parent_hwnd = NULL;
5092
5093 static BOOL CALLBACK
5094FindWindowTitle(HWND hwnd, LPARAM lParam)
5095{
5096 char buf[2048];
5097 char *title = (char *)lParam;
5098
5099 if (GetWindowText(hwnd, buf, sizeof(buf)))
5100 {
5101 if (strstr(buf, title) != NULL)
5102 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005103 /* Found it. Store the window ref. and quit searching if MDI
5104 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005106 if (vim_parent_hwnd != NULL)
5107 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
5109 }
5110 return TRUE; /* continue searching */
5111}
5112
5113/*
5114 * Invoked for '-P "title"' argument: search for parent application to open
5115 * our window in.
5116 */
5117 void
5118gui_mch_set_parent(char *title)
5119{
5120 EnumWindows(FindWindowTitle, (LPARAM)title);
5121 if (vim_parent_hwnd == NULL)
5122 {
5123 EMSG2(_("E671: Cannot find window title \"%s\""), title);
5124 mch_exit(2);
5125 }
5126}
5127
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005128#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 static void
5130ole_error(char *arg)
5131{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005132 char buf[IOSIZE];
5133
5134 /* Can't use EMSG() here, we have not finished initialisation yet. */
5135 vim_snprintf(buf, IOSIZE,
5136 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
5137 arg);
5138 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141
5142/*
5143 * Parse the GUI related command-line arguments. Any arguments used are
5144 * deleted from argv, and *argc is decremented accordingly. This is called
5145 * when vim is started, whether or not the GUI has been started.
5146 */
5147 void
5148gui_mch_prepare(int *argc, char **argv)
5149{
5150 int silent = FALSE;
5151 int idx;
5152
5153 /* Check for special OLE command line parameters */
5154 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5155 {
5156 /* Check for a "-silent" argument first. */
5157 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5158 && (argv[2][0] == '-' || argv[2][0] == '/'))
5159 {
5160 silent = TRUE;
5161 idx = 2;
5162 }
5163 else
5164 idx = 1;
5165
5166 /* Register Vim as an OLE Automation server */
5167 if (STRICMP(argv[idx] + 1, "register") == 0)
5168 {
5169#ifdef FEAT_OLE
5170 RegisterMe(silent);
5171 mch_exit(0);
5172#else
5173 if (!silent)
5174 ole_error("register");
5175 mch_exit(2);
5176#endif
5177 }
5178
5179 /* Unregister Vim as an OLE Automation server */
5180 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5181 {
5182#ifdef FEAT_OLE
5183 UnregisterMe(!silent);
5184 mch_exit(0);
5185#else
5186 if (!silent)
5187 ole_error("unregister");
5188 mch_exit(2);
5189#endif
5190 }
5191
5192 /* Ignore an -embedding argument. It is only relevant if the
5193 * application wants to treat the case when it is started manually
5194 * differently from the case where it is started via automation (and
5195 * we don't).
5196 */
5197 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5198 {
5199#ifdef FEAT_OLE
5200 *argc = 1;
5201#else
5202 ole_error("embedding");
5203 mch_exit(2);
5204#endif
5205 }
5206 }
5207
5208#ifdef FEAT_OLE
5209 {
5210 int bDoRestart = FALSE;
5211
5212 InitOLE(&bDoRestart);
5213 /* automatically exit after registering */
5214 if (bDoRestart)
5215 mch_exit(0);
5216 }
5217#endif
5218
5219#ifdef FEAT_NETBEANS_INTG
5220 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005221 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 int arg;
5223
5224 for (arg = 1; arg < *argc; arg++)
5225 if (strncmp("-nb", argv[arg], 3) == 0)
5226 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 netbeansArg = argv[arg];
5228 mch_memmove(&argv[arg], &argv[arg + 1],
5229 (--*argc - arg) * sizeof(char *));
5230 argv[*argc] = NULL;
5231 break; /* enough? */
5232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 }
5234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235}
5236
5237/*
5238 * Initialise the GUI. Create all the windows, set up all the call-backs
5239 * etc.
5240 */
5241 int
5242gui_mch_init(void)
5243{
5244 const char szVimWndClass[] = VIM_CLASS;
5245 const char szTextAreaClass[] = "VimTextArea";
5246 WNDCLASS wndclass;
5247#ifdef FEAT_MBYTE
5248 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005249 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 WNDCLASSW wndclassw;
5251#endif
5252#ifdef GLOBAL_IME
5253 ATOM atom;
5254#endif
5255
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 /* Return here if the window was already opened (happens when
5257 * gui_mch_dialog() is called early). */
5258 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005259 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260
5261 /*
5262 * Load the tearoff bitmap
5263 */
5264#ifdef FEAT_TEAROFF
5265 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
5266#endif
5267
5268 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5269 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5270#ifdef FEAT_MENU
5271 gui.menu_height = 0; /* Windows takes care of this */
5272#endif
5273 gui.border_width = 0;
5274
5275 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5276
5277#ifdef FEAT_MBYTE
5278 /* First try using the wide version, so that we can use any title.
5279 * Otherwise only characters in the active codepage will work. */
5280 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
5281 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005282 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 wndclassw.lpfnWndProc = _WndProc;
5284 wndclassw.cbClsExtra = 0;
5285 wndclassw.cbWndExtra = 0;
5286 wndclassw.hInstance = s_hinst;
5287 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5288 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5289 wndclassw.hbrBackground = s_brush;
5290 wndclassw.lpszMenuName = NULL;
5291 wndclassw.lpszClassName = szVimWndClassW;
5292
5293 if ((
5294#ifdef GLOBAL_IME
5295 atom =
5296#endif
5297 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005298 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 else
5300 wide_WindowProc = TRUE;
5301 }
5302
5303 if (!wide_WindowProc)
5304#endif
5305
5306 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
5307 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005308 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 wndclass.lpfnWndProc = _WndProc;
5310 wndclass.cbClsExtra = 0;
5311 wndclass.cbWndExtra = 0;
5312 wndclass.hInstance = s_hinst;
5313 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
5314 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5315 wndclass.hbrBackground = s_brush;
5316 wndclass.lpszMenuName = NULL;
5317 wndclass.lpszClassName = szVimWndClass;
5318
5319 if ((
5320#ifdef GLOBAL_IME
5321 atom =
5322#endif
5323 RegisterClass(&wndclass)) == 0)
5324 return FAIL;
5325 }
5326
5327 if (vim_parent_hwnd != NULL)
5328 {
5329#ifdef HAVE_TRY_EXCEPT
5330 __try
5331 {
5332#endif
5333 /* Open inside the specified parent window.
5334 * TODO: last argument should point to a CLIENTCREATESTRUCT
5335 * structure. */
5336 s_hwnd = CreateWindowEx(
5337 WS_EX_MDICHILD,
5338 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005339 WS_OVERLAPPEDWINDOW | WS_CHILD
5340 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5342 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5343 100, /* Any value will do */
5344 100, /* Any value will do */
5345 vim_parent_hwnd, NULL,
5346 s_hinst, NULL);
5347#ifdef HAVE_TRY_EXCEPT
5348 }
5349 __except(EXCEPTION_EXECUTE_HANDLER)
5350 {
5351 /* NOP */
5352 }
5353#endif
5354 if (s_hwnd == NULL)
5355 {
5356 EMSG(_("E672: Unable to open window inside MDI application"));
5357 mch_exit(2);
5358 }
5359 }
5360 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005361 {
5362 /* If the provided windowid is not valid reset it to zero, so that it
5363 * is ignored and we open our own window. */
5364 if (IsWindow((HWND)win_socket_id) <= 0)
5365 win_socket_id = 0;
5366
5367 /* Create a window. If win_socket_id is not zero without border and
5368 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00005370 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005371 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5372 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005373 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5374 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5375 100, /* Any value will do */
5376 100, /* Any value will do */
5377 NULL, NULL,
5378 s_hinst, NULL);
5379 if (s_hwnd != NULL && win_socket_id != 0)
5380 {
5381 SetParent(s_hwnd, (HWND)win_socket_id);
5382 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5383 }
5384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385
5386 if (s_hwnd == NULL)
5387 return FAIL;
5388
5389#ifdef GLOBAL_IME
5390 global_ime_init(atom, s_hwnd);
5391#endif
5392#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5393 dyn_imm_load();
5394#endif
5395
5396 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005397#ifdef FEAT_MBYTE
5398 if (wide_WindowProc)
5399 {
5400 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
5401 {
5402 wndclassw.style = CS_OWNDC;
5403 wndclassw.lpfnWndProc = _TextAreaWndProc;
5404 wndclassw.cbClsExtra = 0;
5405 wndclassw.cbWndExtra = 0;
5406 wndclassw.hInstance = s_hinst;
5407 wndclassw.hIcon = NULL;
5408 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5409 wndclassw.hbrBackground = NULL;
5410 wndclassw.lpszMenuName = NULL;
5411 wndclassw.lpszClassName = szTextAreaClassW;
5412
5413 if (RegisterClassW(&wndclassw) == 0)
5414 return FAIL;
5415 }
5416 }
5417 else
5418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
5420 {
5421 wndclass.style = CS_OWNDC;
5422 wndclass.lpfnWndProc = _TextAreaWndProc;
5423 wndclass.cbClsExtra = 0;
5424 wndclass.cbWndExtra = 0;
5425 wndclass.hInstance = s_hinst;
5426 wndclass.hIcon = NULL;
5427 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5428 wndclass.hbrBackground = NULL;
5429 wndclass.lpszMenuName = NULL;
5430 wndclass.lpszClassName = szTextAreaClass;
5431
5432 if (RegisterClass(&wndclass) == 0)
5433 return FAIL;
5434 }
5435 s_textArea = CreateWindowEx(
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005436 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 szTextAreaClass, "Vim text area",
5438 WS_CHILD | WS_VISIBLE, 0, 0,
5439 100, /* Any value will do for now */
5440 100, /* Any value will do for now */
5441 s_hwnd, NULL,
5442 s_hinst, NULL);
5443
5444 if (s_textArea == NULL)
5445 return FAIL;
5446
Bram Moolenaar20321902016-02-17 12:30:17 +01005447#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005448 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5449 {
5450 HANDLE hIcon = NULL;
5451
5452 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005453 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005454 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005455#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005456
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457#ifdef FEAT_MENU
5458 s_menuBar = CreateMenu();
5459#endif
5460 s_hdc = GetDC(s_textArea);
5461
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463
5464 /* Do we need to bother with this? */
5465 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5466
5467 /* Get background/foreground colors from the system */
5468 gui_mch_def_colors();
5469
5470 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5471 * file) */
5472 set_normal_colors();
5473
5474 /*
5475 * Check that none of the colors are the same as the background color.
5476 * Then store the current values as the defaults.
5477 */
5478 gui_check_colors();
5479 gui.def_norm_pixel = gui.norm_pixel;
5480 gui.def_back_pixel = gui.back_pixel;
5481
5482 /* Get the colors for the highlight groups (gui_check_colors() might have
5483 * changed them) */
5484 highlight_gui_started();
5485
5486 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005487 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005489 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490
5491 /*
5492 * Set up for Intellimouse processing
5493 */
5494 init_mouse_wheel();
5495
5496 /*
5497 * compute a couple of metrics used for the dialogs
5498 */
5499 get_dialog_font_metrics();
5500#ifdef FEAT_TOOLBAR
5501 /*
5502 * Create the toolbar
5503 */
5504 initialise_toolbar();
5505#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005506#ifdef FEAT_GUI_TABLINE
5507 /*
5508 * Create the tabline
5509 */
5510 initialise_tabline();
5511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512#ifdef MSWIN_FIND_REPLACE
5513 /*
5514 * Initialise the dialog box stuff
5515 */
5516 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5517
5518 /* Initialise the struct */
5519 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005520 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005522 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5524 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5525 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005526# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00005527 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
5528 s_findrep_struct_w.lpstrFindWhat =
5529 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5530 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
5531 s_findrep_struct_w.lpstrReplaceWith =
5532 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5533 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
5534 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
5535 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5536# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005539#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005540# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5541/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5542# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005543# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005544# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005545# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005546 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005547 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005548#endif
5549
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005550#ifdef FEAT_RENDER_OPTIONS
5551 if (p_rop)
5552 (void)gui_mch_set_rendering_options(p_rop);
5553#endif
5554
Bram Moolenaar748bf032005-02-02 23:04:36 +00005555theend:
5556 /* Display any pending error messages */
5557 display_errors();
5558
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 return OK;
5560}
5561
5562/*
5563 * Get the size of the screen, taking position on multiple monitors into
5564 * account (if supported).
5565 */
5566 static void
5567get_work_area(RECT *spi_rect)
5568{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005569 HMONITOR mon;
5570 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005572 /* work out which monitor the window is on, and get *it's* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005573 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005574 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005576 moninfo.cbSize = sizeof(MONITORINFO);
5577 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005579 *spi_rect = moninfo.rcWork;
5580 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
5582 }
5583 /* this is the old method... */
5584 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5585}
5586
5587/*
5588 * Set the size of the window to the given width and height in pixels.
5589 */
5590 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005591gui_mch_set_shellsize(
5592 int width,
5593 int height,
5594 int min_width UNUSED,
5595 int min_height UNUSED,
5596 int base_width UNUSED,
5597 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005598 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599{
5600 RECT workarea_rect;
5601 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 WINDOWPLACEMENT wndpl;
5603
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005604 /* Try to keep window completely on screen. */
5605 /* Get position of the screen work area. This is the part that is not
5606 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 get_work_area(&workarea_rect);
5608
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005609 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005610 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 wndpl.length = sizeof(WINDOWPLACEMENT);
5612 GetWindowPlacement(s_hwnd, &wndpl);
5613
5614 /* Resizing a maximized window looks very strange, unzoom it first.
5615 * But don't do it when still starting up, it may have been requested in
5616 * the shortcut. */
5617 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5618 {
5619 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5620 /* Need to get the settings of the normal window. */
5621 GetWindowPlacement(s_hwnd, &wndpl);
5622 }
5623
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005625 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005626 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005627 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005628 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 + GetSystemMetrics(SM_CYCAPTION)
5630#ifdef FEAT_MENU
5631 + gui_mswin_get_menu_height(FALSE)
5632#endif
5633 ;
5634
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005635 /* The following should take care of keeping Vim on the same monitor, no
5636 * matter if the secondary monitor is left or right of the primary
5637 * monitor. */
5638 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5639 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005640
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005641 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005642 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005643 && wndpl.rcNormalPosition.right > workarea_rect.right)
5644 OffsetRect(&wndpl.rcNormalPosition,
5645 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005647 if ((direction & RESIZE_HOR)
5648 && wndpl.rcNormalPosition.left < workarea_rect.left)
5649 OffsetRect(&wndpl.rcNormalPosition,
5650 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651
Bram Moolenaarafa24992006-03-27 20:58:26 +00005652 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005653 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5654 OffsetRect(&wndpl.rcNormalPosition,
5655 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005657 if ((direction & RESIZE_VERT)
5658 && wndpl.rcNormalPosition.top < workarea_rect.top)
5659 OffsetRect(&wndpl.rcNormalPosition,
5660 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661
5662 /* set window position - we should use SetWindowPlacement rather than
5663 * SetWindowPos as the MSDN docs say the coord systems returned by
5664 * these two are not compatible. */
5665 SetWindowPlacement(s_hwnd, &wndpl);
5666
5667 SetActiveWindow(s_hwnd);
5668 SetFocus(s_hwnd);
5669
5670#ifdef FEAT_MENU
5671 /* Menu may wrap differently now */
5672 gui_mswin_get_menu_height(!gui.starting);
5673#endif
5674}
5675
5676
5677 void
5678gui_mch_set_scrollbar_thumb(
5679 scrollbar_T *sb,
5680 long val,
5681 long size,
5682 long max)
5683{
5684 SCROLLINFO info;
5685
5686 sb->scroll_shift = 0;
5687 while (max > 32767)
5688 {
5689 max = (max + 1) >> 1;
5690 val >>= 1;
5691 size >>= 1;
5692 ++sb->scroll_shift;
5693 }
5694
5695 if (sb->scroll_shift > 0)
5696 ++size;
5697
5698 info.cbSize = sizeof(info);
5699 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5700 info.nPos = val;
5701 info.nMin = 0;
5702 info.nMax = max;
5703 info.nPage = size;
5704 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5705}
5706
5707
5708/*
5709 * Set the current text font.
5710 */
5711 void
5712gui_mch_set_font(GuiFont font)
5713{
5714 gui.currFont = font;
5715}
5716
5717
5718/*
5719 * Set the current text foreground color.
5720 */
5721 void
5722gui_mch_set_fg_color(guicolor_T color)
5723{
5724 gui.currFgColor = color;
5725}
5726
5727/*
5728 * Set the current text background color.
5729 */
5730 void
5731gui_mch_set_bg_color(guicolor_T color)
5732{
5733 gui.currBgColor = color;
5734}
5735
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005736/*
5737 * Set the current text special color.
5738 */
5739 void
5740gui_mch_set_sp_color(guicolor_T color)
5741{
5742 gui.currSpColor = color;
5743}
5744
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745#if defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)
5746/*
5747 * Multi-byte handling, originally by Sung-Hoon Baek.
5748 * First static functions (no prototypes generated).
5749 */
5750#ifdef _MSC_VER
5751# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5752#endif
5753#include <imm.h>
5754
5755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 * handle WM_IME_NOTIFY message
5757 */
5758 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005759_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760{
5761 LRESULT lResult = 0;
5762 HIMC hImc;
5763
5764 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5765 return lResult;
5766 switch (dwCommand)
5767 {
5768 case IMN_SETOPENSTATUS:
5769 if (pImmGetOpenStatus(hImc))
5770 {
5771 pImmSetCompositionFont(hImc, &norm_logfont);
5772 im_set_position(gui.row, gui.col);
5773
5774 /* Disable langmap */
5775 State &= ~LANGMAP;
5776 if (State & INSERT)
5777 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005778#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 /* Unshown 'keymap' in status lines */
5780 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5781 {
5782 /* Save cursor position */
5783 int old_row = gui.row;
5784 int old_col = gui.col;
5785
5786 // This must be called here before
5787 // status_redraw_curbuf(), otherwise the mode
5788 // message may appear in the wrong position.
5789 showmode();
5790 status_redraw_curbuf();
5791 update_screen(0);
5792 /* Restore cursor position */
5793 gui.row = old_row;
5794 gui.col = old_col;
5795 }
5796#endif
5797 }
5798 }
5799 gui_update_cursor(TRUE, FALSE);
5800 lResult = 0;
5801 break;
5802 }
5803 pImmReleaseContext(hWnd, hImc);
5804 return lResult;
5805}
5806
5807 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005808_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809{
5810 char_u *ret;
5811 int len;
5812
5813 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5814 return 0;
5815
5816 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5817 if (ret != NULL)
5818 {
5819 add_to_input_buf_csi(ret, len);
5820 vim_free(ret);
5821 return 1;
5822 }
5823 return 0;
5824}
5825
5826/*
5827 * get the current composition string, in UCS-2; *lenp is the number of
5828 * *lenp is the number of Unicode characters.
5829 */
5830 static short_u *
5831GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5832{
5833 LONG ret;
5834 LPWSTR wbuf = NULL;
5835 char_u *buf;
5836
5837 if (!pImmGetContext)
5838 return NULL; /* no imm32.dll */
5839
5840 /* Try Unicode; this'll always work on NT regardless of codepage. */
5841 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5842 if (ret == 0)
5843 return NULL; /* empty */
5844
5845 if (ret > 0)
5846 {
5847 /* Allocate the requested buffer plus space for the NUL character. */
5848 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
5849 if (wbuf != NULL)
5850 {
5851 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5852 *lenp = ret / sizeof(WCHAR);
5853 }
5854 return (short_u *)wbuf;
5855 }
5856
5857 /* ret < 0; we got an error, so try the ANSI version. This'll work
5858 * on 9x/ME, but only if the codepage happens to be set to whatever
5859 * we're inputting. */
5860 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5861 if (ret <= 0)
5862 return NULL; /* empty or error */
5863
5864 buf = alloc(ret);
5865 if (buf == NULL)
5866 return NULL;
5867 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5868
5869 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005870 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 vim_free(buf);
5872
5873 return (short_u *)wbuf;
5874}
5875
5876/*
5877 * void GetResultStr()
5878 *
5879 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5880 * get complete composition string
5881 */
5882 static char_u *
5883GetResultStr(HWND hwnd, int GCS, int *lenp)
5884{
5885 HIMC hIMC; /* Input context handle. */
5886 short_u *buf = NULL;
5887 char_u *convbuf = NULL;
5888
5889 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5890 return NULL;
5891
5892 /* Reads in the composition string. */
5893 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5894 if (buf == NULL)
5895 return NULL;
5896
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005897 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 pImmReleaseContext(hwnd, hIMC);
5899 vim_free(buf);
5900 return convbuf;
5901}
5902#endif
5903
5904/* For global functions we need prototypes. */
5905#if (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) || defined(PROTO)
5906
5907/*
5908 * set font to IM.
5909 */
5910 void
5911im_set_font(LOGFONT *lf)
5912{
5913 HIMC hImc;
5914
5915 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5916 {
5917 pImmSetCompositionFont(hImc, lf);
5918 pImmReleaseContext(s_hwnd, hImc);
5919 }
5920}
5921
5922/*
5923 * Notify cursor position to IM.
5924 */
5925 void
5926im_set_position(int row, int col)
5927{
5928 HIMC hImc;
5929
5930 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5931 {
5932 COMPOSITIONFORM cfs;
5933
5934 cfs.dwStyle = CFS_POINT;
5935 cfs.ptCurrentPos.x = FILL_X(col);
5936 cfs.ptCurrentPos.y = FILL_Y(row);
5937 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
5938 pImmSetCompositionWindow(hImc, &cfs);
5939
5940 pImmReleaseContext(s_hwnd, hImc);
5941 }
5942}
5943
5944/*
5945 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5946 */
5947 void
5948im_set_active(int active)
5949{
5950 HIMC hImc;
5951 static HIMC hImcOld = (HIMC)0;
5952
5953 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
5954 {
5955 if (p_imdisable)
5956 {
5957 if (hImcOld == (HIMC)0)
5958 {
5959 hImcOld = pImmGetContext(s_hwnd);
5960 if (hImcOld)
5961 pImmAssociateContext(s_hwnd, (HIMC)0);
5962 }
5963 active = FALSE;
5964 }
5965 else if (hImcOld != (HIMC)0)
5966 {
5967 pImmAssociateContext(s_hwnd, hImcOld);
5968 hImcOld = (HIMC)0;
5969 }
5970
5971 hImc = pImmGetContext(s_hwnd);
5972 if (hImc)
5973 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005974 /*
5975 * for Korean ime
5976 */
5977 HKL hKL = GetKeyboardLayout(0);
5978
5979 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5980 {
5981 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5982 static BOOL bSaved = FALSE;
5983
5984 if (active)
5985 {
5986 /* if we have a saved conversion status, restore it */
5987 if (bSaved)
5988 pImmSetConversionStatus(hImc, dwConversionSaved,
5989 dwSentenceSaved);
5990 bSaved = FALSE;
5991 }
5992 else
5993 {
5994 /* save conversion status and disable korean */
5995 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5996 &dwSentenceSaved))
5997 {
5998 bSaved = TRUE;
5999 pImmSetConversionStatus(hImc,
6000 dwConversionSaved & ~(IME_CMODE_NATIVE
6001 | IME_CMODE_FULLSHAPE),
6002 dwSentenceSaved);
6003 }
6004 }
6005 }
6006
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 pImmSetOpenStatus(hImc, active);
6008 pImmReleaseContext(s_hwnd, hImc);
6009 }
6010 }
6011}
6012
6013/*
6014 * Get IM status. When IM is on, return not 0. Else return 0.
6015 */
6016 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006017im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018{
6019 int status = 0;
6020 HIMC hImc;
6021
6022 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6023 {
6024 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6025 pImmReleaseContext(s_hwnd, hImc);
6026 }
6027 return status;
6028}
6029
6030#endif /* FEAT_MBYTE && FEAT_MBYTE_IME */
6031
6032#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
6033/* Win32 with GLOBAL IME */
6034
6035/*
6036 * Notify cursor position to IM.
6037 */
6038 void
6039im_set_position(int row, int col)
6040{
6041 /* Win32 with GLOBAL IME */
6042 POINT p;
6043
6044 p.x = FILL_X(col);
6045 p.y = FILL_Y(row);
6046 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
6047 global_ime_set_position(&p);
6048}
6049
6050/*
6051 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6052 */
6053 void
6054im_set_active(int active)
6055{
6056 global_ime_set_status(active);
6057}
6058
6059/*
6060 * Get IM status. When IM is on, return not 0. Else return 0.
6061 */
6062 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006063im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064{
6065 return global_ime_get_status();
6066}
6067#endif
6068
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006069#ifdef FEAT_MBYTE
6070/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006071 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006072 */
6073 static void
6074latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6075{
6076 int c;
6077
Bram Moolenaarca003e12006-03-17 23:19:38 +00006078 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006079 {
6080 c = *text++;
6081 switch (c)
6082 {
6083 case 0xa4: c = 0x20ac; break; /* euro */
6084 case 0xa6: c = 0x0160; break; /* S hat */
6085 case 0xa8: c = 0x0161; break; /* S -hat */
6086 case 0xb4: c = 0x017d; break; /* Z hat */
6087 case 0xb8: c = 0x017e; break; /* Z -hat */
6088 case 0xbc: c = 0x0152; break; /* OE */
6089 case 0xbd: c = 0x0153; break; /* oe */
6090 case 0xbe: c = 0x0178; break; /* Y */
6091 }
6092 *unicodebuf++ = c;
6093 }
6094}
6095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096
6097#ifdef FEAT_RIGHTLEFT
6098/*
6099 * What is this for? In the case where you are using Win98 or Win2K or later,
6100 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6101 * reverses the string sent to the TextOut... family. This sucks, because we
6102 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6103 * way to tell Windblows not to do this!
6104 *
6105 * The short of it is that this 'RevOut' only gets called if you are running
6106 * one of the new, "improved" MS OSes, and only if you are running in
6107 * 'rightleft' mode. It makes display take *slightly* longer, but not
6108 * noticeably so.
6109 */
6110 static void
6111RevOut( HDC s_hdc,
6112 int col,
6113 int row,
6114 UINT foptions,
6115 CONST RECT *pcliprect,
6116 LPCTSTR text,
6117 UINT len,
6118 CONST INT *padding)
6119{
6120 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006122 for (ix = 0; ix < (int)len; ++ix)
6123 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6124 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125}
6126#endif
6127
6128 void
6129gui_mch_draw_string(
6130 int row,
6131 int col,
6132 char_u *text,
6133 int len,
6134 int flags)
6135{
6136 static int *padding = NULL;
6137 static int pad_size = 0;
6138 int i;
6139 const RECT *pcliprect = NULL;
6140 UINT foptions = 0;
6141#ifdef FEAT_MBYTE
6142 static WCHAR *unicodebuf = NULL;
6143 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006144 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 int n = 0;
6146#endif
6147 HPEN hpen, old_pen;
6148 int y;
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006149#ifdef FEAT_DIRECTX
6150 int font_is_ttf_or_vector = 0;
6151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 /*
6154 * Italic and bold text seems to have an extra row of pixels at the bottom
6155 * (below where the bottom of the character should be). If we draw the
6156 * characters with a solid background, the top row of pixels in the
6157 * character below will be overwritten. We can fix this by filling in the
6158 * background ourselves, to the correct character proportions, and then
6159 * writing the character in transparent mode. Still have a problem when
6160 * the character is "_", which gets written on to the character below.
6161 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6162 * pixel in their slots, which fixes the problem with the bottom row of
6163 * pixels. We still need this code because otherwise the top row of pixels
6164 * becomes a problem. - webb.
6165 */
6166 static HBRUSH hbr_cache[2] = {NULL, NULL};
6167 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6168 static int brush_lru = 0;
6169 HBRUSH hbr;
6170 RECT rc;
6171
6172 if (!(flags & DRAW_TRANSP))
6173 {
6174 /*
6175 * Clear background first.
6176 * Note: FillRect() excludes right and bottom of rectangle.
6177 */
6178 rc.left = FILL_X(col);
6179 rc.top = FILL_Y(row);
6180#ifdef FEAT_MBYTE
6181 if (has_mbyte)
6182 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006184 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 }
6186 else
6187#endif
6188 rc.right = FILL_X(col + len);
6189 rc.bottom = FILL_Y(row + 1);
6190
6191 /* Cache the created brush, that saves a lot of time. We need two:
6192 * one for cursor background and one for the normal background. */
6193 if (gui.currBgColor == brush_color[0])
6194 {
6195 hbr = hbr_cache[0];
6196 brush_lru = 1;
6197 }
6198 else if (gui.currBgColor == brush_color[1])
6199 {
6200 hbr = hbr_cache[1];
6201 brush_lru = 0;
6202 }
6203 else
6204 {
6205 if (hbr_cache[brush_lru] != NULL)
6206 DeleteBrush(hbr_cache[brush_lru]);
6207 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6208 brush_color[brush_lru] = gui.currBgColor;
6209 hbr = hbr_cache[brush_lru];
6210 brush_lru = !brush_lru;
6211 }
6212 FillRect(s_hdc, &rc, hbr);
6213
6214 SetBkMode(s_hdc, TRANSPARENT);
6215
6216 /*
6217 * When drawing block cursor, prevent inverted character spilling
6218 * over character cell (can happen with bold/italic)
6219 */
6220 if (flags & DRAW_CURSOR)
6221 {
6222 pcliprect = &rc;
6223 foptions = ETO_CLIPPED;
6224 }
6225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 SetTextColor(s_hdc, gui.currFgColor);
6227 SelectFont(s_hdc, gui.currFont);
6228
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006229#ifdef FEAT_DIRECTX
6230 if (IS_ENABLE_DIRECTX())
6231 {
6232 TEXTMETRIC tm;
6233
6234 GetTextMetrics(s_hdc, &tm);
6235 if (tm.tmPitchAndFamily & (TMPF_TRUETYPE | TMPF_VECTOR))
6236 {
6237 font_is_ttf_or_vector = 1;
6238 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
6239 }
6240 }
6241#endif
6242
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6244 {
6245 vim_free(padding);
6246 pad_size = Columns;
6247
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006248 /* Don't give an out-of-memory message here, it would call us
6249 * recursively. */
6250 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 if (padding != NULL)
6252 for (i = 0; i < pad_size; i++)
6253 padding[i] = gui.char_width;
6254 }
6255
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 /*
6257 * We have to provide the padding argument because italic and bold versions
6258 * of fixed-width fonts are often one pixel or so wider than their normal
6259 * versions.
6260 * No check for DRAW_BOLD, Windows will have done it already.
6261 */
6262
6263#ifdef FEAT_MBYTE
6264 /* Check if there are any UTF-8 characters. If not, use normal text
6265 * output to speed up output. */
6266 if (enc_utf8)
6267 for (n = 0; n < len; ++n)
6268 if (text[n] >= 0x80)
6269 break;
6270
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006271#if defined(FEAT_DIRECTX)
6272 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6273 * required that unicode drawing routine, currently. So this forces it
6274 * enabled. */
6275 if (enc_utf8 && IS_ENABLE_DIRECTX())
6276 n = 0; /* Keep n < len, to enter block for unicode. */
6277#endif
6278
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006280 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006282 if ((enc_utf8
6283 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6284 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 && (unicodebuf == NULL || len > unibuflen))
6286 {
6287 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006288 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289
6290 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006291 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292
6293 unibuflen = len;
6294 }
6295
6296 if (enc_utf8 && n < len && unicodebuf != NULL)
6297 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006298 /* Output UTF-8 characters. Composing characters should be
6299 * handled here. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006300 int i;
6301 int wlen; /* string length in words */
6302 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 int cells; /* cell width of string up to composing char */
6304 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006305 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006307 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006308 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006310 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006312 c = utf_ptr2char(text + i);
6313 if (c >= 0x10000)
6314 {
6315 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006316 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6317 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006318 }
6319 else
6320 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006321 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006322 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006323
6324 if (utf_iscomposing(c))
6325 cw = 0;
6326 else
6327 {
6328 cw = utf_char2cells(c);
6329 if (cw > 2) /* don't use 4 for unprintable char */
6330 cw = 1;
6331 }
6332
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 if (unicodepdy != NULL)
6334 {
6335 /* Use unicodepdy to make characters fit as we expect, even
6336 * when the font uses different widths (e.g., bold character
6337 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006338 if (c >= 0x10000)
6339 {
6340 unicodepdy[wlen - 2] = cw * gui.char_width;
6341 unicodepdy[wlen - 1] = 0;
6342 }
6343 else
6344 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 }
6346 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006347 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006348 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006350#if defined(FEAT_DIRECTX)
6351 if (IS_ENABLE_DIRECTX() && font_is_ttf_or_vector)
6352 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006353 /* Add one to "cells" for italics. */
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006354 DWriteContext_DrawText(s_dwc, s_hdc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006355 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006356 gui.char_width, gui.currFgColor);
6357 }
6358 else
6359#endif
6360 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6361 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 len = cells; /* used for underlining */
6363 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006364 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 {
6366 /* If we want to display codepage data, and the current CP is not the
6367 * ANSI one, we need to go via Unicode. */
6368 if (unicodebuf != NULL)
6369 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006370 if (enc_latin9)
6371 latin9_to_ucs(text, len, unicodebuf);
6372 else
6373 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 MB_PRECOMPOSED,
6375 (char *)text, len,
6376 (LPWSTR)unicodebuf, unibuflen);
6377 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006378 {
6379 /* Use unicodepdy to make characters fit as we expect, even
6380 * when the font uses different widths (e.g., bold character
6381 * is wider). */
6382 if (unicodepdy != NULL)
6383 {
6384 int i;
6385 int cw;
6386
6387 for (i = 0; i < len; ++i)
6388 {
6389 cw = utf_char2cells(unicodebuf[i]);
6390 if (cw > 2)
6391 cw = 1;
6392 unicodepdy[i] = cw * gui.char_width;
6393 }
6394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006396 foptions, pcliprect, unicodebuf, len, unicodepdy);
6397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 }
6399 }
6400 else
6401#endif
6402 {
6403#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006404 /* Windows will mess up RL text, so we have to draw it character by
6405 * character. Only do this if RL is on, since it's slow. */
6406 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6408 foptions, pcliprect, (char *)text, len, padding);
6409 else
6410#endif
6411 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6412 foptions, pcliprect, (char *)text, len, padding);
6413 }
6414
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006415 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 if (flags & DRAW_UNDERL)
6417 {
6418 hpen = CreatePen(PS_SOLID, 1, gui.currFgColor);
6419 old_pen = SelectObject(s_hdc, hpen);
6420 /* When p_linespace is 0, overwrite the bottom row of pixels.
6421 * Otherwise put the line just below the character. */
6422 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 if (p_linespace > 1)
6424 y -= p_linespace - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 MoveToEx(s_hdc, FILL_X(col), y, NULL);
6426 /* Note: LineTo() excludes the last pixel in the line. */
6427 LineTo(s_hdc, FILL_X(col + len), y);
6428 DeleteObject(SelectObject(s_hdc, old_pen));
6429 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006430
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006431 /* Strikethrough */
6432 if (flags & DRAW_STRIKE)
6433 {
6434 hpen = CreatePen(PS_SOLID, 1, gui.currSpColor);
6435 old_pen = SelectObject(s_hdc, hpen);
6436 y = FILL_Y(row + 1) - gui.char_height/2;
6437 MoveToEx(s_hdc, FILL_X(col), y, NULL);
6438 /* Note: LineTo() excludes the last pixel in the line. */
6439 LineTo(s_hdc, FILL_X(col + len), y);
6440 DeleteObject(SelectObject(s_hdc, old_pen));
6441 }
6442
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006443 /* Undercurl */
6444 if (flags & DRAW_UNDERC)
6445 {
6446 int x;
6447 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006448 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006449
6450 y = FILL_Y(row + 1) - 1;
6451 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6452 {
6453 offset = val[x % 8];
6454 SetPixel(s_hdc, x, y - offset, gui.currSpColor);
6455 }
6456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457}
6458
6459
6460/*
6461 * Output routines.
6462 */
6463
6464/* Flush any output to the screen */
6465 void
6466gui_mch_flush(void)
6467{
6468# if defined(__BORLANDC__)
6469 /*
6470 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6471 * prototype declaration.
6472 * The compiler complains if __stdcall is not used in both declarations.
6473 */
6474 BOOL __stdcall GdiFlush(void);
6475# endif
6476
6477 GdiFlush();
6478}
6479
6480 static void
6481clear_rect(RECT *rcp)
6482{
6483 HBRUSH hbr;
6484
6485 hbr = CreateSolidBrush(gui.back_pixel);
6486 FillRect(s_hdc, rcp, hbr);
6487 DeleteBrush(hbr);
6488}
6489
6490
Bram Moolenaarc716c302006-01-21 22:12:51 +00006491 void
6492gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6493{
6494 RECT workarea_rect;
6495
6496 get_work_area(&workarea_rect);
6497
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006498 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006499 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006500 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006501
6502 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6503 * the menubar for MSwin, we subtract it from the screen height, so that
6504 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006505 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006506 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006507 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006508 - GetSystemMetrics(SM_CYCAPTION)
6509#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006510 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006511#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006512 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006513}
6514
6515
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516#if defined(FEAT_MENU) || defined(PROTO)
6517/*
6518 * Add a sub menu to the menu bar.
6519 */
6520 void
6521gui_mch_add_menu(
6522 vimmenu_T *menu,
6523 int pos)
6524{
6525 vimmenu_T *parent = menu->parent;
6526
6527 menu->submenu_id = CreatePopupMenu();
6528 menu->id = s_menu_id++;
6529
6530 if (menu_is_menubar(menu->name))
6531 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532#ifdef FEAT_MBYTE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006533 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006535 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6536 {
6537 /* 'encoding' differs from active codepage: convert menu name
6538 * and use wide function */
6539 wn = enc_to_utf16(menu->name, NULL);
6540 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006542 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006544 infow.cbSize = sizeof(infow);
6545 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6546 | MIIM_SUBMENU;
6547 infow.dwItemData = (long_u)menu;
6548 infow.wID = menu->id;
6549 infow.fType = MFT_STRING;
6550 infow.dwTypeData = wn;
6551 infow.cch = (UINT)wcslen(wn);
6552 infow.hSubMenu = menu->submenu_id;
6553 InsertMenuItemW((parent == NULL)
6554 ? s_menuBar : parent->submenu_id,
6555 (UINT)pos, TRUE, &infow);
6556 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006560 if (wn == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561#endif
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006562 {
6563 MENUITEMINFO info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006565 info.cbSize = sizeof(info);
6566 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
6567 info.dwItemData = (long_u)menu;
6568 info.wID = menu->id;
6569 info.fType = MFT_STRING;
6570 info.dwTypeData = (LPTSTR)menu->name;
6571 info.cch = (UINT)STRLEN(menu->name);
6572 info.hSubMenu = menu->submenu_id;
6573 InsertMenuItem((parent == NULL)
6574 ? s_menuBar : parent->submenu_id,
6575 (UINT)pos, TRUE, &info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 }
6577 }
6578
6579 /* Fix window size if menu may have wrapped */
6580 if (parent == NULL)
6581 gui_mswin_get_menu_height(!gui.starting);
6582#ifdef FEAT_TEAROFF
6583 else if (IsWindow(parent->tearoff_handle))
6584 rebuild_tearoff(parent);
6585#endif
6586}
6587
6588 void
6589gui_mch_show_popupmenu(vimmenu_T *menu)
6590{
6591 POINT mp;
6592
6593 (void)GetCursorPos((LPPOINT)&mp);
6594 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6595}
6596
6597 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006598gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599{
6600 vimmenu_T *menu = gui_find_menu(path_name);
6601
6602 if (menu != NULL)
6603 {
6604 POINT p;
6605
6606 /* Find the position of the current cursor */
6607 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006608 if (mouse_pos)
6609 {
6610 int mx, my;
6611
6612 gui_mch_getmouse(&mx, &my);
6613 p.x += mx;
6614 p.y += my;
6615 }
6616 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006618 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6620 }
6621 msg_scroll = FALSE;
6622 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6623 }
6624}
6625
6626#if defined(FEAT_TEAROFF) || defined(PROTO)
6627/*
6628 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6629 * create it as a pseudo-"tearoff menu".
6630 */
6631 void
6632gui_make_tearoff(char_u *path_name)
6633{
6634 vimmenu_T *menu = gui_find_menu(path_name);
6635
6636 /* Found the menu, so tear it off. */
6637 if (menu != NULL)
6638 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6639}
6640#endif
6641
6642/*
6643 * Add a menu item to a menu
6644 */
6645 void
6646gui_mch_add_menu_item(
6647 vimmenu_T *menu,
6648 int idx)
6649{
6650 vimmenu_T *parent = menu->parent;
6651
6652 menu->id = s_menu_id++;
6653 menu->submenu_id = NULL;
6654
6655#ifdef FEAT_TEAROFF
6656 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6657 {
6658 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6659 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6660 }
6661 else
6662#endif
6663#ifdef FEAT_TOOLBAR
6664 if (menu_is_toolbar(parent->name))
6665 {
6666 TBBUTTON newtb;
6667
6668 vim_memset(&newtb, 0, sizeof(newtb));
6669 if (menu_is_separator(menu->name))
6670 {
6671 newtb.iBitmap = 0;
6672 newtb.fsStyle = TBSTYLE_SEP;
6673 }
6674 else
6675 {
6676 newtb.iBitmap = get_toolbar_bitmap(menu);
6677 newtb.fsStyle = TBSTYLE_BUTTON;
6678 }
6679 newtb.idCommand = menu->id;
6680 newtb.fsState = TBSTATE_ENABLED;
6681 newtb.iString = 0;
6682 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6683 (LPARAM)&newtb);
6684 menu->submenu_id = (HMENU)-1;
6685 }
6686 else
6687#endif
6688 {
6689#ifdef FEAT_MBYTE
6690 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691
6692 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6693 {
6694 /* 'encoding' differs from active codepage: convert menu item name
6695 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006696 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 if (wn != NULL)
6698 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006699 InsertMenuW(parent->submenu_id, (UINT)idx,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 (menu_is_separator(menu->name)
6701 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6702 (UINT)menu->id, wn);
6703 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 }
6705 }
6706 if (wn == NULL)
6707#endif
6708 InsertMenu(parent->submenu_id, (UINT)idx,
6709 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
6710 | MF_BYPOSITION,
6711 (UINT)menu->id, (LPCTSTR)menu->name);
6712#ifdef FEAT_TEAROFF
6713 if (IsWindow(parent->tearoff_handle))
6714 rebuild_tearoff(parent);
6715#endif
6716 }
6717}
6718
6719/*
6720 * Destroy the machine specific menu widget.
6721 */
6722 void
6723gui_mch_destroy_menu(vimmenu_T *menu)
6724{
6725#ifdef FEAT_TOOLBAR
6726 /*
6727 * is this a toolbar button?
6728 */
6729 if (menu->submenu_id == (HMENU)-1)
6730 {
6731 int iButton;
6732
6733 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6734 (WPARAM)menu->id, 0);
6735 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6736 }
6737 else
6738#endif
6739 {
6740 if (menu->parent != NULL
6741 && menu_is_popup(menu->parent->dname)
6742 && menu->parent->submenu_id != NULL)
6743 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6744 else
6745 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6746 if (menu->submenu_id != NULL)
6747 DestroyMenu(menu->submenu_id);
6748#ifdef FEAT_TEAROFF
6749 if (IsWindow(menu->tearoff_handle))
6750 DestroyWindow(menu->tearoff_handle);
6751 if (menu->parent != NULL
6752 && menu->parent->children != NULL
6753 && IsWindow(menu->parent->tearoff_handle))
6754 {
6755 /* This menu must not show up when rebuilding the tearoff window. */
6756 menu->modes = 0;
6757 rebuild_tearoff(menu->parent);
6758 }
6759#endif
6760 }
6761}
6762
6763#ifdef FEAT_TEAROFF
6764 static void
6765rebuild_tearoff(vimmenu_T *menu)
6766{
6767 /*hackish*/
6768 char_u tbuf[128];
6769 RECT trect;
6770 RECT rct;
6771 RECT roct;
6772 int x, y;
6773
6774 HWND thwnd = menu->tearoff_handle;
6775
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006776 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 if (GetWindowRect(thwnd, &trect)
6778 && GetWindowRect(s_hwnd, &rct)
6779 && GetClientRect(s_hwnd, &roct))
6780 {
6781 x = trect.left - rct.left;
6782 y = (trect.top - rct.bottom + roct.bottom);
6783 }
6784 else
6785 {
6786 x = y = 0xffffL;
6787 }
6788 DestroyWindow(thwnd);
6789 if (menu->children != NULL)
6790 {
6791 gui_mch_tearoff(tbuf, menu, x, y);
6792 if (IsWindow(menu->tearoff_handle))
6793 (void) SetWindowPos(menu->tearoff_handle,
6794 NULL,
6795 (int)trect.left,
6796 (int)trect.top,
6797 0, 0,
6798 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6799 }
6800}
6801#endif /* FEAT_TEAROFF */
6802
6803/*
6804 * Make a menu either grey or not grey.
6805 */
6806 void
6807gui_mch_menu_grey(
6808 vimmenu_T *menu,
6809 int grey)
6810{
6811#ifdef FEAT_TOOLBAR
6812 /*
6813 * is this a toolbar button?
6814 */
6815 if (menu->submenu_id == (HMENU)-1)
6816 {
6817 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6818 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6819 }
6820 else
6821#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006822 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6823 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824
6825#ifdef FEAT_TEAROFF
6826 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6827 {
6828 WORD menuID;
6829 HWND menuHandle;
6830
6831 /*
6832 * A tearoff button has changed state.
6833 */
6834 if (menu->children == NULL)
6835 menuID = (WORD)(menu->id);
6836 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006837 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6839 if (menuHandle)
6840 EnableWindow(menuHandle, !grey);
6841
6842 }
6843#endif
6844}
6845
6846#endif /* FEAT_MENU */
6847
6848
6849/* define some macros used to make the dialogue creation more readable */
6850
6851#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6852#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006853#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854
6855#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6856/*
6857 * stuff for dialogs
6858 */
6859
6860/*
6861 * The callback routine used by all the dialogs. Very simple. First,
6862 * acknowledges the INITDIALOG message so that Windows knows to do standard
6863 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6864 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6865 * number.
6866 */
6867 static LRESULT CALLBACK
6868dialog_callback(
6869 HWND hwnd,
6870 UINT message,
6871 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006872 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873{
6874 if (message == WM_INITDIALOG)
6875 {
6876 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
6877 /* Set focus to the dialog. Set the default button, if specified. */
6878 (void)SetFocus(hwnd);
6879 if (dialog_default_button > IDCANCEL)
6880 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006881 else
6882 /* We don't have a default, set focus on another element of the
6883 * dialog window, probably the icon */
6884 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 return FALSE;
6886 }
6887
6888 if (message == WM_COMMAND)
6889 {
6890 int button = LOWORD(wParam);
6891
6892 /* Don't end the dialog if something was selected that was
6893 * not a button.
6894 */
6895 if (button >= DLG_NONBUTTON_CONTROL)
6896 return TRUE;
6897
6898 /* If the edit box exists, copy the string. */
6899 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006900 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006901# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006902 /* If the OS is Windows NT, and 'encoding' differs from active
6903 * codepage: use wide function and convert text. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006904 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02006905 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006906 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
6907 char_u *p;
6908
6909 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6910 p = utf16_to_enc(wp, NULL);
6911 vim_strncpy(s_textfield, p, IOSIZE);
6912 vim_free(p);
6913 vim_free(wp);
6914 }
6915 else
6916# endif
6917 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006918 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920
6921 /*
6922 * Need to check for IDOK because if the user just hits Return to
6923 * accept the default value, some reason this is what we get.
6924 */
6925 if (button == IDOK)
6926 {
6927 if (dialog_default_button > IDCANCEL)
6928 EndDialog(hwnd, dialog_default_button);
6929 }
6930 else
6931 EndDialog(hwnd, button - IDCANCEL);
6932 return TRUE;
6933 }
6934
6935 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6936 {
6937 EndDialog(hwnd, 0);
6938 return TRUE;
6939 }
6940 return FALSE;
6941}
6942
6943/*
6944 * Create a dialog dynamically from the parameter strings.
6945 * type = type of dialog (question, alert, etc.)
6946 * title = dialog title. may be NULL for default title.
6947 * message = text to display. Dialog sizes to accommodate it.
6948 * buttons = '\n' separated list of button captions, default first.
6949 * dfltbutton = number of default button.
6950 *
6951 * This routine returns 1 if the first button is pressed,
6952 * 2 for the second, etc.
6953 *
6954 * 0 indicates Esc was pressed.
6955 * -1 for unexpected error
6956 *
6957 * If stubbing out this fn, return 1.
6958 */
6959
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006960static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961{
6962 "IDR_VIM",
6963 "IDR_VIM_ERROR",
6964 "IDR_VIM_ALERT",
6965 "IDR_VIM_INFO",
6966 "IDR_VIM_QUESTION"
6967};
6968
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 int
6970gui_mch_dialog(
6971 int type,
6972 char_u *title,
6973 char_u *message,
6974 char_u *buttons,
6975 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006976 char_u *textfield,
6977 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978{
6979 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006980 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 int numButtons;
6982 int *buttonWidths, *buttonPositions;
6983 int buttonYpos;
6984 int nchar, i;
6985 DWORD lStyle;
6986 int dlgwidth = 0;
6987 int dlgheight;
6988 int editboxheight;
6989 int horizWidth = 0;
6990 int msgheight;
6991 char_u *pstart;
6992 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006993 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 char_u *tbuffer;
6995 RECT rect;
6996 HWND hwnd;
6997 HDC hdc;
6998 HFONT font, oldFont;
6999 TEXTMETRIC fontInfo;
7000 int fontHeight;
7001 int textWidth, minButtonWidth, messageWidth;
7002 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007003 int maxDialogHeight;
7004 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 int vertical;
7006 int dlgPaddingX;
7007 int dlgPaddingY;
7008#ifdef USE_SYSMENU_FONT
7009 LOGFONT lfSysmenu;
7010 int use_lfSysmenu = FALSE;
7011#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007012 garray_T ga;
7013 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014
7015#ifndef NO_CONSOLE
7016 /* Don't output anything in silent mode ("ex -s") */
7017 if (silent_mode)
7018 return dfltbutton; /* return default option */
7019#endif
7020
Bram Moolenaar748bf032005-02-02 23:04:36 +00007021 if (s_hwnd == NULL)
7022 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023
7024 if ((type < 0) || (type > VIM_LAST_TYPE))
7025 type = 0;
7026
7027 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007028 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007029 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007030 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031
7032 if (p == NULL)
7033 return -1;
7034
7035 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007036 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7038 * const.
7039 */
7040 tbuffer = vim_strsave(buttons);
7041 if (tbuffer == NULL)
7042 return -1;
7043
7044 --dfltbutton; /* Change from one-based to zero-based */
7045
7046 /* Count buttons */
7047 numButtons = 1;
7048 for (i = 0; tbuffer[i] != '\0'; i++)
7049 {
7050 if (tbuffer[i] == DLG_BUTTON_SEP)
7051 numButtons++;
7052 }
7053 if (dfltbutton >= numButtons)
7054 dfltbutton = -1;
7055
7056 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007057 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 if (buttonWidths == NULL)
7059 return -1;
7060
7061 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007062 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 if (buttonPositions == NULL)
7064 return -1;
7065
7066 /*
7067 * Calculate how big the dialog must be.
7068 */
7069 hwnd = GetDesktopWindow();
7070 hdc = GetWindowDC(hwnd);
7071#ifdef USE_SYSMENU_FONT
7072 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7073 {
7074 font = CreateFontIndirect(&lfSysmenu);
7075 use_lfSysmenu = TRUE;
7076 }
7077 else
7078#endif
7079 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7080 VARIABLE_PITCH , DLG_FONT_NAME);
7081 if (s_usenewlook)
7082 {
7083 oldFont = SelectFont(hdc, font);
7084 dlgPaddingX = DLG_PADDING_X;
7085 dlgPaddingY = DLG_PADDING_Y;
7086 }
7087 else
7088 {
7089 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7090 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7091 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7092 }
7093 GetTextMetrics(hdc, &fontInfo);
7094 fontHeight = fontInfo.tmHeight;
7095
7096 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007097 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098
7099 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007100 if (s_hwnd == NULL)
7101 {
7102 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103
Bram Moolenaarc716c302006-01-21 22:12:51 +00007104 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007105 get_work_area(&workarea_rect);
7106 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7107 if (maxDialogWidth > 600)
7108 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007109 /* Leave some room for the taskbar. */
7110 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007111 }
7112 else
7113 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02007114 /* Use our own window for the size, unless it's very small. */
7115 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007116 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02007117 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007118 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007119 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
7120 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007121
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007122 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007123 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007124 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02007125 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007126 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
7127 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
7128 }
7129
7130 /* Set dlgwidth to width of message.
7131 * Copy the message into "ga", changing NL to CR-NL and inserting line
7132 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 pstart = message;
7134 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007135 msgheight = 0;
7136 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 do
7138 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007139 msgheight += fontHeight; /* at least one line */
7140
7141 /* Need to figure out where to break the string. The system does it
7142 * at a word boundary, which would mean we can't compute the number of
7143 * wrapped lines. */
7144 textWidth = 0;
7145 last_white = NULL;
7146 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007147 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007148#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007149 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007150#else
7151 l = 1;
7152#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01007153 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007154 && textWidth > maxDialogWidth * 3 / 4)
7155 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007156 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007157 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007158 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007159 /* Line will wrap. */
7160 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007161 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007162 textWidth = 0;
7163
7164 if (last_white != NULL)
7165 {
7166 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007167 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007168 pend = last_white + 1;
7169 last_white = NULL;
7170 }
7171 ga_append(&ga, '\r');
7172 ga_append(&ga, '\n');
7173 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007174 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007175
7176 while (--l >= 0)
7177 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007178 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007179 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007181
7182 ga_append(&ga, '\r');
7183 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 pstart = pend + 1;
7185 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007186
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007187 if (ga.ga_data != NULL)
7188 message = ga.ga_data;
7189
Bram Moolenaar748bf032005-02-02 23:04:36 +00007190 messageWidth += 10; /* roundoff space */
7191
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02007193 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7194 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195
7196 if (msgheight < DLG_ICON_HEIGHT)
7197 msgheight = DLG_ICON_HEIGHT;
7198
7199 /*
7200 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007201 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007203 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 if (!vertical)
7205 {
7206 // Place buttons horizontally if they fit.
7207 horizWidth = dlgPaddingX;
7208 pstart = tbuffer;
7209 i = 0;
7210 do
7211 {
7212 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7213 if (pend == NULL)
7214 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007215 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 if (textWidth < minButtonWidth)
7217 textWidth = minButtonWidth;
7218 textWidth += dlgPaddingX; /* Padding within button */
7219 buttonWidths[i] = textWidth;
7220 buttonPositions[i++] = horizWidth;
7221 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7222 pstart = pend + 1;
7223 } while (*pend != NUL);
7224
7225 if (horizWidth > maxDialogWidth)
7226 vertical = TRUE; // Too wide to fit on the screen.
7227 else if (horizWidth > dlgwidth)
7228 dlgwidth = horizWidth;
7229 }
7230
7231 if (vertical)
7232 {
7233 // Stack buttons vertically.
7234 pstart = tbuffer;
7235 do
7236 {
7237 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7238 if (pend == NULL)
7239 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007240 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 textWidth += dlgPaddingX; /* Padding within button */
7242 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7243 if (textWidth > dlgwidth)
7244 dlgwidth = textWidth;
7245 pstart = pend + 1;
7246 } while (*pend != NUL);
7247 }
7248
7249 if (dlgwidth < DLG_MIN_WIDTH)
7250 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7251
7252 /* start to fill in the dlgtemplate information. addressing by WORDs */
7253 if (s_usenewlook)
7254 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7255 else
7256 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7257
7258 add_long(lStyle);
7259 add_long(0); // (lExtendedStyle)
7260 pnumitems = p; /*save where the number of items must be stored*/
7261 add_word(0); // NumberOfItems(will change later)
7262 add_word(10); // x
7263 add_word(10); // y
7264 add_word(PixelToDialogX(dlgwidth)); // cx
7265
7266 // Dialog height.
7267 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007268 dlgheight = msgheight + 2 * dlgPaddingY
7269 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 else
7271 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7272
7273 // Dialog needs to be taller if contains an edit box.
7274 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7275 if (textfield != NULL)
7276 dlgheight += editboxheight;
7277
Bram Moolenaara95d8232013-08-07 15:27:11 +02007278 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7279 if (dlgheight > maxDialogHeight)
7280 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007281 msgheight = msgheight - (dlgheight - maxDialogHeight);
7282 dlgheight = maxDialogHeight;
7283 scroll_flag = WS_VSCROLL;
7284 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7285 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007286 }
7287
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 add_word(PixelToDialogY(dlgheight));
7289
7290 add_word(0); // Menu
7291 add_word(0); // Class
7292
7293 /* copy the title of the dialog */
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007294 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7295 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 p += nchar;
7297
7298 if (s_usenewlook)
7299 {
7300 /* do the font, since DS_3DLOOK doesn't work properly */
7301#ifdef USE_SYSMENU_FONT
7302 if (use_lfSysmenu)
7303 {
7304 /* point size */
7305 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7306 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007307 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 }
7309 else
7310#endif
7311 {
7312 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007313 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 }
7315 p += nchar;
7316 }
7317
7318 buttonYpos = msgheight + 2 * dlgPaddingY;
7319
7320 if (textfield != NULL)
7321 buttonYpos += editboxheight;
7322
7323 pstart = tbuffer;
7324 if (!vertical)
7325 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7326 for (i = 0; i < numButtons; i++)
7327 {
7328 /* get end of this button. */
7329 for ( pend = pstart;
7330 *pend && (*pend != DLG_BUTTON_SEP);
7331 pend++)
7332 ;
7333
7334 if (*pend)
7335 *pend = '\0';
7336
7337 /*
7338 * old NOTE:
7339 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7340 * the focus to the first tab-able button and in so doing makes that
7341 * the default!! Grrr. Workaround: Make the default button the only
7342 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7343 * he/she can use arrow keys.
7344 *
7345 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007346 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 * dialog. Also needed for when the textfield is the default control.
7348 * It appears to work now (perhaps not on Win95?).
7349 */
7350 if (vertical)
7351 {
7352 p = add_dialog_element(p,
7353 (i == dfltbutton
7354 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7355 PixelToDialogX(DLG_VERT_PADDING_X),
7356 PixelToDialogY(buttonYpos /* TBK */
7357 + 2 * fontHeight * i),
7358 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7359 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007360 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 }
7362 else
7363 {
7364 p = add_dialog_element(p,
7365 (i == dfltbutton
7366 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7367 PixelToDialogX(horizWidth + buttonPositions[i]),
7368 PixelToDialogY(buttonYpos), /* TBK */
7369 PixelToDialogX(buttonWidths[i]),
7370 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007371 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 }
7373 pstart = pend + 1; /*next button*/
7374 }
7375 *pnumitems += numButtons;
7376
7377 /* Vim icon */
7378 p = add_dialog_element(p, SS_ICON,
7379 PixelToDialogX(dlgPaddingX),
7380 PixelToDialogY(dlgPaddingY),
7381 PixelToDialogX(DLG_ICON_WIDTH),
7382 PixelToDialogY(DLG_ICON_HEIGHT),
7383 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7384 dlg_icons[type]);
7385
Bram Moolenaar748bf032005-02-02 23:04:36 +00007386 /* Dialog message */
7387 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7388 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7389 PixelToDialogY(dlgPaddingY),
7390 (WORD)(PixelToDialogX(messageWidth) + 1),
7391 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007392 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393
7394 /* Edit box */
7395 if (textfield != NULL)
7396 {
7397 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7398 PixelToDialogX(2 * dlgPaddingX),
7399 PixelToDialogY(2 * dlgPaddingY + msgheight),
7400 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7401 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007402 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 *pnumitems += 1;
7404 }
7405
7406 *pnumitems += 2;
7407
7408 SelectFont(hdc, oldFont);
7409 DeleteObject(font);
7410 ReleaseDC(hwnd, hdc);
7411
7412 /* Let the dialog_callback() function know which button to make default
7413 * If we have an edit box, make that the default. We also need to tell
7414 * dialog_callback() if this dialog contains an edit box or not. We do
7415 * this by setting s_textfield if it does.
7416 */
7417 if (textfield != NULL)
7418 {
7419 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7420 s_textfield = textfield;
7421 }
7422 else
7423 {
7424 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7425 s_textfield = NULL;
7426 }
7427
7428 /* show the dialog box modally and get a return value */
7429 nchar = (int)DialogBoxIndirect(
7430 s_hinst,
7431 (LPDLGTEMPLATE)pdlgtemplate,
7432 s_hwnd,
7433 (DLGPROC)dialog_callback);
7434
7435 LocalFree(LocalHandle(pdlgtemplate));
7436 vim_free(tbuffer);
7437 vim_free(buttonWidths);
7438 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007439 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440
7441 /* Focus back to our window (for when MDI is used). */
7442 (void)SetFocus(s_hwnd);
7443
7444 return nchar;
7445}
7446
7447#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007448
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449/*
7450 * Put a simple element (basic class) onto a dialog template in memory.
7451 * return a pointer to where the next item should be added.
7452 *
7453 * parameters:
7454 * lStyle = additional style flags
7455 * (be careful, NT3.51 & Win32s will ignore the new ones)
7456 * x,y = x & y positions IN DIALOG UNITS
7457 * w,h = width and height IN DIALOG UNITS
7458 * Id = ID used in messages
7459 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7460 * caption = usually text or resource name
7461 *
7462 * TODO: use the length information noted here to enable the dialog creation
7463 * routines to work out more exactly how much memory they need to alloc.
7464 */
7465 static PWORD
7466add_dialog_element(
7467 PWORD p,
7468 DWORD lStyle,
7469 WORD x,
7470 WORD y,
7471 WORD w,
7472 WORD h,
7473 WORD Id,
7474 WORD clss,
7475 const char *caption)
7476{
7477 int nchar;
7478
7479 p = lpwAlign(p); /* Align to dword boundary*/
7480 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7481 *p++ = LOWORD(lStyle);
7482 *p++ = HIWORD(lStyle);
7483 *p++ = 0; // LOWORD (lExtendedStyle)
7484 *p++ = 0; // HIWORD (lExtendedStyle)
7485 *p++ = x;
7486 *p++ = y;
7487 *p++ = w;
7488 *p++ = h;
7489 *p++ = Id; //9 or 10 words in all
7490
7491 *p++ = (WORD)0xffff;
7492 *p++ = clss; //2 more here
7493
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007494 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 p += nchar;
7496
7497 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7498
7499 return p; //total = 15+ (strlen(caption)) words
7500 // = 30 + 2(strlen(caption) bytes reqd
7501}
7502
7503
7504/*
7505 * Helper routine. Take an input pointer, return closest pointer that is
7506 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7507 */
7508 static LPWORD
7509lpwAlign(
7510 LPWORD lpIn)
7511{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007512 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007514 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 ul += 3;
7516 ul >>= 2;
7517 ul <<= 2;
7518 return (LPWORD)ul;
7519}
7520
7521/*
7522 * Helper routine. Takes second parameter as Ansi string, copies it to first
7523 * parameter as wide character (16-bits / char) string, and returns integer
7524 * number of wide characters (words) in string (including the trailing wide
7525 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007526 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7527 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 static int
7529nCopyAnsiToWideChar(
7530 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007531 LPSTR lpAnsiIn,
7532 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533{
7534 int nChar = 0;
7535#ifdef FEAT_MBYTE
7536 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7537 int i;
7538 WCHAR *wn;
7539
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007540 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 {
7542 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007543 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 if (wn != NULL)
7545 {
7546 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007547 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 vim_free(wn);
7549 }
7550 }
7551 if (nChar == 0)
7552 /* Use Win32 conversion function. */
7553 nChar = MultiByteToWideChar(
7554 enc_codepage > 0 ? enc_codepage : CP_ACP,
7555 MB_PRECOMPOSED,
7556 lpAnsiIn, len,
7557 lpWCStr, len);
7558 for (i = 0; i < nChar; ++i)
7559 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7560 lpWCStr[i] = (WORD)' ';
7561#else
7562 do
7563 {
7564 if (*lpAnsiIn == '\t')
7565 *lpWCStr++ = (WORD)' ';
7566 else
7567 *lpWCStr++ = (WORD)*lpAnsiIn;
7568 nChar++;
7569 } while (*lpAnsiIn++);
7570#endif
7571
7572 return nChar;
7573}
7574
7575
7576#ifdef FEAT_TEAROFF
7577/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007578 * Lookup menu handle from "menu_id".
7579 */
7580 static HMENU
7581tearoff_lookup_menuhandle(
7582 vimmenu_T *menu,
7583 WORD menu_id)
7584{
7585 for ( ; menu != NULL; menu = menu->next)
7586 {
7587 if (menu->modes == 0) /* this menu has just been deleted */
7588 continue;
7589 if (menu_is_separator(menu->dname))
7590 continue;
7591 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7592 return menu->submenu_id;
7593 }
7594 return NULL;
7595}
7596
7597/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 * The callback function for all the modeless dialogs that make up the
7599 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7600 * thinking its menus have been clicked), and go away when closed.
7601 */
7602 static LRESULT CALLBACK
7603tearoff_callback(
7604 HWND hwnd,
7605 UINT message,
7606 WPARAM wParam,
7607 LPARAM lParam)
7608{
7609 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007610 {
7611 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614
7615 /* May show the mouse pointer again. */
7616 HandleMouseHide(message, lParam);
7617
7618 if (message == WM_COMMAND)
7619 {
7620 if ((WORD)(LOWORD(wParam)) & 0x8000)
7621 {
7622 POINT mp;
7623 RECT rect;
7624
7625 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7626 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007627 vimmenu_T *menu;
7628
7629 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007631 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7633 (int)rect.right - 8,
7634 (int)mp.y,
7635 (int)0, /*reserved param*/
7636 s_hwnd,
7637 NULL);
7638 /*
7639 * NOTE: The pop-up menu can eat the mouse up event.
7640 * We deal with this in normal.c.
7641 */
7642 }
7643 }
7644 else
7645 /* Pass on messages to the main Vim window */
7646 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7647 /*
7648 * Give main window the focus back: this is so after
7649 * choosing a tearoff button you can start typing again
7650 * straight away.
7651 */
7652 (void)SetFocus(s_hwnd);
7653 return TRUE;
7654 }
7655 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7656 {
7657 DestroyWindow(hwnd);
7658 return TRUE;
7659 }
7660
7661 /* When moved around, give main window the focus back. */
7662 if (message == WM_EXITSIZEMOVE)
7663 (void)SetActiveWindow(s_hwnd);
7664
7665 return FALSE;
7666}
7667#endif
7668
7669
7670/*
7671 * Decide whether to use the "new look" (small, non-bold font) or the "old
7672 * look" (big, clanky font) for dialogs, and work out a few values for use
7673 * later accordingly.
7674 */
7675 static void
7676get_dialog_font_metrics(void)
7677{
7678 HDC hdc;
7679 HFONT hfontTools = 0;
7680 DWORD dlgFontSize;
7681 SIZE size;
7682#ifdef USE_SYSMENU_FONT
7683 LOGFONT lfSysmenu;
7684#endif
7685
7686 s_usenewlook = FALSE;
7687
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007689 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7690 hfontTools = CreateFontIndirect(&lfSysmenu);
7691 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692#endif
7693 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7694 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7695
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007696 if (hfontTools)
7697 {
7698 hdc = GetDC(s_hwnd);
7699 SelectObject(hdc, hfontTools);
7700 /*
7701 * GetTextMetrics() doesn't return the right value in
7702 * tmAveCharWidth, so we have to figure out the dialog base units
7703 * ourselves.
7704 */
7705 GetTextExtentPoint(hdc,
7706 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7707 52, &size);
7708 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007710 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7711 s_dlgfntheight = (WORD)size.cy;
7712 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 }
7714
7715 if (!s_usenewlook)
7716 {
7717 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7718 s_dlgfntwidth = LOWORD(dlgFontSize);
7719 s_dlgfntheight = HIWORD(dlgFontSize);
7720 }
7721}
7722
7723#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7724/*
7725 * Create a pseudo-"tearoff menu" based on the child
7726 * items of a given menu pointer.
7727 */
7728 static void
7729gui_mch_tearoff(
7730 char_u *title,
7731 vimmenu_T *menu,
7732 int initX,
7733 int initY)
7734{
7735 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7736 int template_len;
7737 int nchar, textWidth, submenuWidth;
7738 DWORD lStyle;
7739 DWORD lExtendedStyle;
7740 WORD dlgwidth;
7741 WORD menuID;
7742 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007743 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 vimmenu_T *the_menu = menu;
7745 HWND hwnd;
7746 HDC hdc;
7747 HFONT font, oldFont;
7748 int col, spaceWidth, len;
7749 int columnWidths[2];
7750 char_u *label, *text;
7751 int acLen = 0;
7752 int nameLen;
7753 int padding0, padding1, padding2 = 0;
7754 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007755 int x;
7756 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757#ifdef USE_SYSMENU_FONT
7758 LOGFONT lfSysmenu;
7759 int use_lfSysmenu = FALSE;
7760#endif
7761
7762 /*
7763 * If this menu is already torn off, move it to the mouse position.
7764 */
7765 if (IsWindow(menu->tearoff_handle))
7766 {
7767 POINT mp;
7768 if (GetCursorPos((LPPOINT)&mp))
7769 {
7770 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7771 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7772 }
7773 return;
7774 }
7775
7776 /*
7777 * Create a new tearoff.
7778 */
7779 if (*title == MNU_HIDDEN_CHAR)
7780 title++;
7781
7782 /* Allocate memory to store the dialog template. It's made bigger when
7783 * needed. */
7784 template_len = DLG_ALLOC_SIZE;
7785 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7786 if (p == NULL)
7787 return;
7788
7789 hwnd = GetDesktopWindow();
7790 hdc = GetWindowDC(hwnd);
7791#ifdef USE_SYSMENU_FONT
7792 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7793 {
7794 font = CreateFontIndirect(&lfSysmenu);
7795 use_lfSysmenu = TRUE;
7796 }
7797 else
7798#endif
7799 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7800 VARIABLE_PITCH , DLG_FONT_NAME);
7801 if (s_usenewlook)
7802 oldFont = SelectFont(hdc, font);
7803 else
7804 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7805
7806 /* Calculate width of a single space. Used for padding columns to the
7807 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007808 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809
7810 /* Figure out max width of the text column, the accelerator column and the
7811 * optional submenu column. */
7812 submenuWidth = 0;
7813 for (col = 0; col < 2; col++)
7814 {
7815 columnWidths[col] = 0;
7816 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7817 {
7818 /* Use "dname" here to compute the width of the visible text. */
7819 text = (col == 0) ? pmenu->dname : pmenu->actext;
7820 if (text != NULL && *text != NUL)
7821 {
7822 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7823 if (textWidth > columnWidths[col])
7824 columnWidths[col] = textWidth;
7825 }
7826 if (pmenu->children != NULL)
7827 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7828 }
7829 }
7830 if (columnWidths[1] == 0)
7831 {
7832 /* no accelerators */
7833 if (submenuWidth != 0)
7834 columnWidths[0] += submenuWidth;
7835 else
7836 columnWidths[0] += spaceWidth;
7837 }
7838 else
7839 {
7840 /* there is an accelerator column */
7841 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7842 columnWidths[1] += submenuWidth;
7843 }
7844
7845 /*
7846 * Now find the total width of our 'menu'.
7847 */
7848 textWidth = columnWidths[0] + columnWidths[1];
7849 if (submenuWidth != 0)
7850 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007851 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7853 textWidth += submenuWidth;
7854 }
7855 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7856 if (textWidth > dlgwidth)
7857 dlgwidth = textWidth;
7858 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7859
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 /* start to fill in the dlgtemplate information. addressing by WORDs */
7861 if (s_usenewlook)
7862 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7863 else
7864 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7865
7866 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7867 *p++ = LOWORD(lStyle);
7868 *p++ = HIWORD(lStyle);
7869 *p++ = LOWORD(lExtendedStyle);
7870 *p++ = HIWORD(lExtendedStyle);
7871 pnumitems = p; /* save where the number of items must be stored */
7872 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007873 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007875 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 else
7877 *p++ = PixelToDialogX(initX); // x
7878 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007879 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 else
7881 *p++ = PixelToDialogY(initY); // y
7882 *p++ = PixelToDialogX(dlgwidth); // cx
7883 ptrueheight = p;
7884 *p++ = 0; // dialog height: changed later anyway
7885 *p++ = 0; // Menu
7886 *p++ = 0; // Class
7887
7888 /* copy the title of the dialog */
7889 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007890 ? (LPSTR)title
7891 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 p += nchar;
7893
7894 if (s_usenewlook)
7895 {
7896 /* do the font, since DS_3DLOOK doesn't work properly */
7897#ifdef USE_SYSMENU_FONT
7898 if (use_lfSysmenu)
7899 {
7900 /* point size */
7901 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7902 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007903 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 }
7905 else
7906#endif
7907 {
7908 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007909 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 }
7911 p += nchar;
7912 }
7913
7914 /*
7915 * Loop over all the items in the menu.
7916 * But skip over the tearbar.
7917 */
7918 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7919 menu = menu->children->next;
7920 else
7921 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007922 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 for ( ; menu != NULL; menu = menu->next)
7924 {
7925 if (menu->modes == 0) /* this menu has just been deleted */
7926 continue;
7927 if (menu_is_separator(menu->dname))
7928 {
7929 sepPadding += 3;
7930 continue;
7931 }
7932
7933 /* Check if there still is plenty of room in the template. Make it
7934 * larger when needed. */
7935 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7936 {
7937 WORD *newp;
7938
7939 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7940 if (newp != NULL)
7941 {
7942 template_len += 4096;
7943 mch_memmove(newp, pdlgtemplate,
7944 (char *)p - (char *)pdlgtemplate);
7945 p = newp + (p - pdlgtemplate);
7946 pnumitems = newp + (pnumitems - pdlgtemplate);
7947 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7948 LocalFree(LocalHandle(pdlgtemplate));
7949 pdlgtemplate = newp;
7950 }
7951 }
7952
7953 /* Figure out minimal length of this menu label. Use "name" for the
7954 * actual text, "dname" for estimating the displayed size. "name"
7955 * has "&a" for mnemonic and includes the accelerator. */
7956 len = nameLen = (int)STRLEN(menu->name);
7957 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7958 (int)STRLEN(menu->dname))) / spaceWidth;
7959 len += padding0;
7960
7961 if (menu->actext != NULL)
7962 {
7963 acLen = (int)STRLEN(menu->actext);
7964 len += acLen;
7965 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7966 }
7967 else
7968 textWidth = 0;
7969 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7970 len += padding1;
7971
7972 if (menu->children == NULL)
7973 {
7974 padding2 = submenuWidth / spaceWidth;
7975 len += padding2;
7976 menuID = (WORD)(menu->id);
7977 }
7978 else
7979 {
7980 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007981 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 }
7983
7984 /* Allocate menu label and fill it in */
7985 text = label = alloc((unsigned)len + 1);
7986 if (label == NULL)
7987 break;
7988
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007989 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 text = vim_strchr(text, TAB); /* stop at TAB before actext */
7991 if (text == NULL)
7992 text = label + nameLen; /* no actext, use whole name */
7993 while (padding0-- > 0)
7994 *text++ = ' ';
7995 if (menu->actext != NULL)
7996 {
7997 STRNCPY(text, menu->actext, acLen);
7998 text += acLen;
7999 }
8000 while (padding1-- > 0)
8001 *text++ = ' ';
8002 if (menu->children != NULL)
8003 {
8004 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8005 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8006 }
8007 else
8008 {
8009 while (padding2-- > 0)
8010 *text++ = ' ';
8011 }
8012 *text = NUL;
8013
8014 /*
8015 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8016 * W95/NT4 it makes the tear-off look more like a menu.
8017 */
8018 p = add_dialog_element(p,
8019 BS_PUSHBUTTON|BS_LEFT,
8020 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8021 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8022 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8023 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008024 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 vim_free(label);
8026 (*pnumitems)++;
8027 }
8028
8029 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8030
8031
8032 /* show modelessly */
Bram Moolenaar66857f42017-10-22 16:43:20 +02008033 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 s_hinst,
8035 (LPDLGTEMPLATE)pdlgtemplate,
8036 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02008037 (DLGPROC)tearoff_callback,
8038 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039
8040 LocalFree(LocalHandle(pdlgtemplate));
8041 SelectFont(hdc, oldFont);
8042 DeleteObject(font);
8043 ReleaseDC(hwnd, hdc);
8044
8045 /*
8046 * Reassert ourselves as the active window. This is so that after creating
8047 * a tearoff, the user doesn't have to click with the mouse just to start
8048 * typing again!
8049 */
8050 (void)SetActiveWindow(s_hwnd);
8051
8052 /* make sure the right buttons are enabled */
8053 force_menu_update = TRUE;
8054}
8055#endif
8056
8057#if defined(FEAT_TOOLBAR) || defined(PROTO)
8058#include "gui_w32_rc.h"
8059
8060/* This not defined in older SDKs */
8061# ifndef TBSTYLE_FLAT
8062# define TBSTYLE_FLAT 0x0800
8063# endif
8064
8065/*
8066 * Create the toolbar, initially unpopulated.
8067 * (just like the menu, there are no defaults, it's all
8068 * set up through menu.vim)
8069 */
8070 static void
8071initialise_toolbar(void)
8072{
8073 InitCommonControls();
8074 s_toolbarhwnd = CreateToolbarEx(
8075 s_hwnd,
8076 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8077 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008078 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 s_hinst,
8080 IDR_TOOLBAR1, // id of initial bitmap
8081 NULL,
8082 0, // initial number of buttons
8083 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8084 TOOLBAR_BUTTON_HEIGHT,
8085 TOOLBAR_BUTTON_WIDTH,
8086 TOOLBAR_BUTTON_HEIGHT,
8087 sizeof(TBBUTTON)
8088 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008089 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090
8091 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
8092}
8093
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008094 static LRESULT CALLBACK
8095toolbar_wndproc(
8096 HWND hwnd,
8097 UINT uMsg,
8098 WPARAM wParam,
8099 LPARAM lParam)
8100{
8101 HandleMouseHide(uMsg, lParam);
8102 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8103}
8104
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 static int
8106get_toolbar_bitmap(vimmenu_T *menu)
8107{
8108 int i = -1;
8109
8110 /*
8111 * Check user bitmaps first, unless builtin is specified.
8112 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008113 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 {
8115 char_u fname[MAXPATHL];
8116 HANDLE hbitmap = NULL;
8117
8118 if (menu->iconfile != NULL)
8119 {
8120 gui_find_iconfile(menu->iconfile, fname, "bmp");
8121 hbitmap = LoadImage(
8122 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008123 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 IMAGE_BITMAP,
8125 TOOLBAR_BUTTON_WIDTH,
8126 TOOLBAR_BUTTON_HEIGHT,
8127 LR_LOADFROMFILE |
8128 LR_LOADMAP3DCOLORS
8129 );
8130 }
8131
8132 /*
8133 * If the LoadImage call failed, or the "icon=" file
8134 * didn't exist or wasn't specified, try the menu name
8135 */
8136 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008137 && (gui_find_bitmap(
8138#ifdef FEAT_MULTI_LANG
8139 menu->en_dname != NULL ? menu->en_dname :
8140#endif
8141 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 hbitmap = LoadImage(
8143 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008144 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 IMAGE_BITMAP,
8146 TOOLBAR_BUTTON_WIDTH,
8147 TOOLBAR_BUTTON_HEIGHT,
8148 LR_LOADFROMFILE |
8149 LR_LOADMAP3DCOLORS
8150 );
8151
8152 if (hbitmap != NULL)
8153 {
8154 TBADDBITMAP tbAddBitmap;
8155
8156 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008157 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158
8159 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8160 (WPARAM)1, (LPARAM)&tbAddBitmap);
8161 /* i will be set to -1 if it fails */
8162 }
8163 }
8164 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8165 i = menu->iconidx;
8166
8167 return i;
8168}
8169#endif
8170
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008171#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8172 static void
8173initialise_tabline(void)
8174{
8175 InitCommonControls();
8176
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008177 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008178 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008179 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8180 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008181 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008182
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008183 gui.tabline_height = TABLINE_HEIGHT;
8184
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008185# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008186 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008187# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008188}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008189
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008190/*
8191 * Get tabpage_T from POINT.
8192 */
8193 static tabpage_T *
8194GetTabFromPoint(
8195 HWND hWnd,
8196 POINT pt)
8197{
8198 tabpage_T *ptp = NULL;
8199
8200 if (gui_mch_showing_tabline())
8201 {
8202 TCHITTESTINFO htinfo;
8203 htinfo.pt = pt;
8204 /* ignore if a window under cusor is not tabcontrol. */
8205 if (s_tabhwnd == hWnd)
8206 {
8207 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8208 if (idx != -1)
8209 ptp = find_tabpage(idx + 1);
8210 }
8211 }
8212 return ptp;
8213}
8214
8215static POINT s_pt = {0, 0};
8216static HCURSOR s_hCursor = NULL;
8217
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008218 static LRESULT CALLBACK
8219tabline_wndproc(
8220 HWND hwnd,
8221 UINT uMsg,
8222 WPARAM wParam,
8223 LPARAM lParam)
8224{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008225 POINT pt;
8226 tabpage_T *tp;
8227 RECT rect;
8228 int nCenter;
8229 int idx0;
8230 int idx1;
8231
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008232 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008233
8234 switch (uMsg)
8235 {
8236 case WM_LBUTTONDOWN:
8237 {
8238 s_pt.x = GET_X_LPARAM(lParam);
8239 s_pt.y = GET_Y_LPARAM(lParam);
8240 SetCapture(hwnd);
8241 s_hCursor = GetCursor(); /* backup default cursor */
8242 break;
8243 }
8244 case WM_MOUSEMOVE:
8245 if (GetCapture() == hwnd
8246 && ((wParam & MK_LBUTTON)) != 0)
8247 {
8248 pt.x = GET_X_LPARAM(lParam);
8249 pt.y = s_pt.y;
8250 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8251 {
8252 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8253
8254 tp = GetTabFromPoint(hwnd, pt);
8255 if (tp != NULL)
8256 {
8257 idx0 = tabpage_index(curtab) - 1;
8258 idx1 = tabpage_index(tp) - 1;
8259
8260 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8261 nCenter = rect.left + (rect.right - rect.left) / 2;
8262
8263 /* Check if the mouse cursor goes over the center of
8264 * the next tab to prevent "flickering". */
8265 if ((idx0 < idx1) && (nCenter < pt.x))
8266 {
8267 tabpage_move(idx1 + 1);
8268 update_screen(0);
8269 }
8270 else if ((idx1 < idx0) && (pt.x < nCenter))
8271 {
8272 tabpage_move(idx1);
8273 update_screen(0);
8274 }
8275 }
8276 }
8277 }
8278 break;
8279 case WM_LBUTTONUP:
8280 {
8281 if (GetCapture() == hwnd)
8282 {
8283 SetCursor(s_hCursor);
8284 ReleaseCapture();
8285 }
8286 break;
8287 }
8288 default:
8289 break;
8290 }
8291
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008292 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8293}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008294#endif
8295
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8297/*
8298 * Make the GUI window come to the foreground.
8299 */
8300 void
8301gui_mch_set_foreground(void)
8302{
8303 if (IsIconic(s_hwnd))
8304 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8305 SetForegroundWindow(s_hwnd);
8306}
8307#endif
8308
8309#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8310 static void
8311dyn_imm_load(void)
8312{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008313 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 if (hLibImm == NULL)
8315 return;
8316
8317 pImmGetCompositionStringA
8318 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8319 pImmGetCompositionStringW
8320 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8321 pImmGetContext
8322 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8323 pImmAssociateContext
8324 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8325 pImmReleaseContext
8326 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8327 pImmGetOpenStatus
8328 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8329 pImmSetOpenStatus
8330 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
8331 pImmGetCompositionFont
8332 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
8333 pImmSetCompositionFont
8334 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
8335 pImmSetCompositionWindow
8336 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8337 pImmGetConversionStatus
8338 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008339 pImmSetConversionStatus
8340 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341
8342 if ( pImmGetCompositionStringA == NULL
8343 || pImmGetCompositionStringW == NULL
8344 || pImmGetContext == NULL
8345 || pImmAssociateContext == NULL
8346 || pImmReleaseContext == NULL
8347 || pImmGetOpenStatus == NULL
8348 || pImmSetOpenStatus == NULL
8349 || pImmGetCompositionFont == NULL
8350 || pImmSetCompositionFont == NULL
8351 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008352 || pImmGetConversionStatus == NULL
8353 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 {
8355 FreeLibrary(hLibImm);
8356 hLibImm = NULL;
8357 pImmGetContext = NULL;
8358 return;
8359 }
8360
8361 return;
8362}
8363
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364#endif
8365
8366#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8367
8368# ifdef FEAT_XPM_W32
8369# define IMAGE_XPM 100
8370# endif
8371
8372typedef struct _signicon_t
8373{
8374 HANDLE hImage;
8375 UINT uType;
8376#ifdef FEAT_XPM_W32
8377 HANDLE hShape; /* Mask bitmap handle */
8378#endif
8379} signicon_t;
8380
8381 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008382gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383{
8384 signicon_t *sign;
8385 int x, y, w, h;
8386
8387 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8388 return;
8389
8390 x = TEXT_X(col);
8391 y = TEXT_Y(row);
8392 w = gui.char_width * 2;
8393 h = gui.char_height;
8394 switch (sign->uType)
8395 {
8396 case IMAGE_BITMAP:
8397 {
8398 HDC hdcMem;
8399 HBITMAP hbmpOld;
8400
8401 hdcMem = CreateCompatibleDC(s_hdc);
8402 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8403 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8404 SelectObject(hdcMem, hbmpOld);
8405 DeleteDC(hdcMem);
8406 }
8407 break;
8408 case IMAGE_ICON:
8409 case IMAGE_CURSOR:
8410 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8411 break;
8412#ifdef FEAT_XPM_W32
8413 case IMAGE_XPM:
8414 {
8415 HDC hdcMem;
8416 HBITMAP hbmpOld;
8417
8418 hdcMem = CreateCompatibleDC(s_hdc);
8419 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8420 /* Make hole */
8421 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8422
8423 SelectObject(hdcMem, sign->hImage);
8424 /* Paint sign */
8425 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8426 SelectObject(hdcMem, hbmpOld);
8427 DeleteDC(hdcMem);
8428 }
8429 break;
8430#endif
8431 }
8432}
8433
8434 static void
8435close_signicon_image(signicon_t *sign)
8436{
8437 if (sign)
8438 switch (sign->uType)
8439 {
8440 case IMAGE_BITMAP:
8441 DeleteObject((HGDIOBJ)sign->hImage);
8442 break;
8443 case IMAGE_CURSOR:
8444 DestroyCursor((HCURSOR)sign->hImage);
8445 break;
8446 case IMAGE_ICON:
8447 DestroyIcon((HICON)sign->hImage);
8448 break;
8449#ifdef FEAT_XPM_W32
8450 case IMAGE_XPM:
8451 DeleteObject((HBITMAP)sign->hImage);
8452 DeleteObject((HBITMAP)sign->hShape);
8453 break;
8454#endif
8455 }
8456}
8457
8458 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008459gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460{
8461 signicon_t sign, *psign;
8462 char_u *ext;
8463
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008465 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 if (ext > signfile)
8467 {
8468 int do_load = 1;
8469
8470 if (!STRICMP(ext, ".bmp"))
8471 sign.uType = IMAGE_BITMAP;
8472 else if (!STRICMP(ext, ".ico"))
8473 sign.uType = IMAGE_ICON;
8474 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8475 sign.uType = IMAGE_CURSOR;
8476 else
8477 do_load = 0;
8478
8479 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008480 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 gui.char_width * 2, gui.char_height,
8482 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8483#ifdef FEAT_XPM_W32
8484 if (!STRICMP(ext, ".xpm"))
8485 {
8486 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008487 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8488 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 }
8490#endif
8491 }
8492
8493 psign = NULL;
8494 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8495 != NULL)
8496 *psign = sign;
8497
8498 if (!psign)
8499 {
8500 if (sign.hImage)
8501 close_signicon_image(&sign);
8502 EMSG(_(e_signdata));
8503 }
8504 return (void *)psign;
8505
8506}
8507
8508 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008509gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510{
8511 if (sign)
8512 {
8513 close_signicon_image((signicon_t *)sign);
8514 vim_free(sign);
8515 }
8516}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518
8519#if defined(FEAT_BEVAL) || defined(PROTO)
8520
8521/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008522 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 *
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00008524 * The only reused thing is gui_beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8526 * to get current mouse position).
8527 *
8528 * Trying to use as more Windows services as possible, and as less
8529 * IE version as possible :)).
8530 *
8531 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8532 * BalloonEval struct.
8533 * 2) Enable/Disable simply create/kill BalloonEval Timer
8534 * 3) When there was enough inactivity, timer procedure posts
8535 * async request to debugger
8536 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8537 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008538 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 */
8540
Bram Moolenaar45360022005-07-21 21:08:21 +00008541/*
8542 * determine whether installed Common Controls support multiline tooltips
8543 * (i.e. their version is >= 4.70
8544 */
8545 int
8546multiline_balloon_available(void)
8547{
8548 HINSTANCE hDll;
8549 static char comctl_dll[] = "comctl32.dll";
8550 static int multiline_tip = MAYBE;
8551
8552 if (multiline_tip != MAYBE)
8553 return multiline_tip;
8554
8555 hDll = GetModuleHandle(comctl_dll);
8556 if (hDll != NULL)
8557 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008558 DLLGETVERSIONPROC pGetVer;
8559 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008560
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008561 if (pGetVer != NULL)
8562 {
8563 DLLVERSIONINFO dvi;
8564 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008565
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008566 ZeroMemory(&dvi, sizeof(dvi));
8567 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008568
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008569 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008570
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008571 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008572 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008573 || (dvi.dwMajorVersion == 4
8574 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008575 {
8576 multiline_tip = TRUE;
8577 return multiline_tip;
8578 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008579 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008580 else
8581 {
8582 /* there is chance we have ancient CommCtl 4.70
8583 which doesn't export DllGetVersion */
8584 DWORD dwHandle = 0;
8585 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8586 if (len > 0)
8587 {
8588 VS_FIXEDFILEINFO *ver;
8589 UINT vlen = 0;
8590 void *data = alloc(len);
8591
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008592 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008593 && GetFileVersionInfo(comctl_dll, 0, len, data)
8594 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8595 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008596 && HIWORD(ver->dwFileVersionMS) > 4)
8597 || ((HIWORD(ver->dwFileVersionMS) == 4
8598 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008599 {
8600 vim_free(data);
8601 multiline_tip = TRUE;
8602 return multiline_tip;
8603 }
8604 vim_free(data);
8605 }
8606 }
8607 }
8608 multiline_tip = FALSE;
8609 return multiline_tip;
8610}
8611
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008613make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614{
Bram Moolenaar45360022005-07-21 21:08:21 +00008615 TOOLINFO *pti;
8616 int ToolInfoSize;
8617
8618 if (multiline_balloon_available() == TRUE)
8619 ToolInfoSize = sizeof(TOOLINFO_NEW);
8620 else
8621 ToolInfoSize = sizeof(TOOLINFO);
8622
8623 pti = (TOOLINFO *)alloc(ToolInfoSize);
8624 if (pti == NULL)
8625 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626
8627 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
8628 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8629 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8630 beval->target, NULL, s_hinst, NULL);
8631
8632 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8633 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8634
Bram Moolenaar45360022005-07-21 21:08:21 +00008635 pti->cbSize = ToolInfoSize;
8636 pti->uFlags = TTF_SUBCLASS;
8637 pti->hwnd = beval->target;
8638 pti->hinst = 0; /* Don't use string resources */
8639 pti->uId = ID_BEVAL_TOOLTIP;
8640
8641 if (multiline_balloon_available() == TRUE)
8642 {
8643 RECT rect;
8644 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
8645 pti->lpszText = LPSTR_TEXTCALLBACK;
8646 ptin->lParam = (LPARAM)text;
8647 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
8648 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8649 (LPARAM)rect.right);
8650 }
8651 else
8652 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653
8654 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00008655 pti->rect.left = pt.x - 3;
8656 pti->rect.top = pt.y - 3;
8657 pti->rect.right = pt.x + 3;
8658 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659
Bram Moolenaar45360022005-07-21 21:08:21 +00008660 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 /* Make tooltip appear sooner */
8662 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008663 /* I've performed some tests and it seems the longest possible life time
8664 * of tooltip is 30 seconds */
8665 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 /*
8667 * HACK: force tooltip to appear, because it'll not appear until
8668 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008669 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008671 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00008673 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674}
8675
8676 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008677delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008679 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680}
8681
8682 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008683BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008684 HWND hwnd UNUSED,
8685 UINT uMsg UNUSED,
8686 UINT_PTR idEvent UNUSED,
8687 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688{
8689 POINT pt;
8690 RECT rect;
8691
8692 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8693 return;
8694
8695 GetCursorPos(&pt);
8696 if (WindowFromPoint(pt) != s_textArea)
8697 return;
8698
8699 ScreenToClient(s_textArea, &pt);
8700 GetClientRect(s_textArea, &rect);
8701 if (!PtInRect(&rect, pt))
8702 return;
8703
8704 if (LastActivity > 0
8705 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8706 && (cur_beval->showState != ShS_PENDING
8707 || abs(cur_beval->x - pt.x) > 3
8708 || abs(cur_beval->y - pt.y) > 3))
8709 {
8710 /* Pointer resting in one place long enough, it's time to show
8711 * the tooltip. */
8712 cur_beval->showState = ShS_PENDING;
8713 cur_beval->x = pt.x;
8714 cur_beval->y = pt.y;
8715
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008716 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717
8718 if (cur_beval->msgCB != NULL)
8719 (*cur_beval->msgCB)(cur_beval, 0);
8720 }
8721}
8722
8723 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008724gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008726 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008728 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729}
8730
8731 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008732gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008734 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 if (beval == NULL)
8736 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008737 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008738 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008739 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740}
8741
8742 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008743gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744{
8745 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008746
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008747 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 if (beval->showState == ShS_SHOWING)
8749 return;
8750 GetCursorPos(&pt);
8751 ScreenToClient(s_textArea, &pt);
8752
8753 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008755 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 gui_mch_disable_beval_area(cur_beval);
8757 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008758 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008760 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761}
8762
8763 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008764gui_mch_create_beval_area(
8765 void *target, /* ignored, always use s_textArea */
8766 char_u *mesg,
8767 void (*mesgCB)(BalloonEval *, int),
8768 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769{
8770 /* partially stolen from gui_beval.c */
8771 BalloonEval *beval;
8772
8773 if (mesg != NULL && mesgCB != NULL)
8774 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01008775 IEMSG(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 return NULL;
8777 }
8778
8779 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
8780 if (beval != NULL)
8781 {
8782 beval->target = s_textArea;
8783 beval->balloon = NULL;
8784
8785 beval->showState = ShS_NEUTRAL;
8786 beval->x = 0;
8787 beval->y = 0;
8788 beval->msg = mesg;
8789 beval->msgCB = mesgCB;
8790 beval->clientData = clientData;
8791
8792 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 cur_beval = beval;
8794
8795 if (p_beval)
8796 gui_mch_enable_beval_area(beval);
8797
8798 }
8799 return beval;
8800}
8801
8802 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008803Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804{
8805 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8806 return;
8807
8808 if (cur_beval != NULL)
8809 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008810 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008812 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008813 // TRACE0("TTN_SHOW {{{");
8814 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008815 break;
8816 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008817 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 delete_tooltip(cur_beval);
8819 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008820 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821
8822 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008823 break;
8824 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008825 {
8826 /* if you get there then we have new common controls */
8827 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8828 info->lpszText = (LPSTR)info->lParam;
8829 info->uFlags |= TTF_DI_SETITEM;
8830 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008831 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 }
8833 }
8834}
8835
8836 static void
8837TrackUserActivity(UINT uMsg)
8838{
8839 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8840 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8841 LastActivity = GetTickCount();
8842}
8843
8844 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008845gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846{
8847 vim_free(beval);
8848}
8849#endif /* FEAT_BEVAL */
8850
8851#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8852/*
8853 * We have multiple signs to draw at the same location. Draw the
8854 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8855 */
8856 void
8857netbeans_draw_multisign_indicator(int row)
8858{
8859 int i;
8860 int y;
8861 int x;
8862
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008863 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008864 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008865
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 x = 0;
8867 y = TEXT_Y(row);
8868
8869 for (i = 0; i < gui.char_height - 3; i++)
8870 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8871
8872 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8873 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8874 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8875 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8876 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8877 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8878 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8879}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008880#endif