blob: 0979d677e05186c9b28321c05a5153a6cc5f2f06 [file] [log] [blame]
Bram Moolenaar060f1f02007-05-10 20:17:29 +00001/* vi:set ts=8 sts=4 sw=4:
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;
322# if defined(FEAT_MBYTE) && defined(WIN3264)
323static 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
372#if (defined(WIN3264) && defined(FEAT_MBYTE)) || defined(GLOBAL_IME)
373 /* 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
476#ifdef WIN3264
477static OSVERSIONINFO os_version; /* like it says. Init in gui_mch_init() */
478#endif
479
480#ifdef FEAT_BEVAL
481/* balloon-eval WM_NOTIFY_HANDLER */
482static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
483static void TrackUserActivity(UINT uMsg);
484#endif
485
486/*
487 * For control IME.
488 *
489 * These LOGFONT used for IME.
490 */
491#ifdef FEAT_MBYTE
492# ifdef USE_IM_CONTROL
493/* holds LOGFONT for 'guifontwide' if available, otherwise 'guifont' */
494static LOGFONT norm_logfont;
495/* holds LOGFONT for 'guifont' always. */
496static LOGFONT sub_logfont;
497# endif
498#endif
499
500#ifdef FEAT_MBYTE_IME
501static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
502#endif
503
504#if defined(FEAT_BROWSE)
505static char_u *convert_filter(char_u *s);
506#endif
507
508#ifdef DEBUG_PRINT_ERROR
509/*
510 * Print out the last Windows error message
511 */
512 static void
513print_windows_error(void)
514{
515 LPVOID lpMsgBuf;
516
517 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
518 NULL, GetLastError(),
519 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
520 (LPTSTR) &lpMsgBuf, 0, NULL);
521 TRACE1("Error: %s\n", lpMsgBuf);
522 LocalFree(lpMsgBuf);
523}
524#endif
525
526/*
527 * Cursor blink functions.
528 *
529 * This is a simple state machine:
530 * BLINK_NONE not blinking at all
531 * BLINK_OFF blinking, cursor is not shown
532 * BLINK_ON blinking, cursor is shown
533 */
534
535#define BLINK_NONE 0
536#define BLINK_OFF 1
537#define BLINK_ON 2
538
539static int blink_state = BLINK_NONE;
540static long_u blink_waittime = 700;
541static long_u blink_ontime = 400;
542static long_u blink_offtime = 250;
543static UINT blink_timer = 0;
544
Bram Moolenaar703a8042016-06-04 16:24:32 +0200545 int
546gui_mch_is_blinking(void)
547{
548 return blink_state != BLINK_NONE;
549}
550
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200551 int
552gui_mch_is_blink_off(void)
553{
554 return blink_state == BLINK_OFF;
555}
556
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100557 void
558gui_mch_set_blinking(long wait, long on, long off)
559{
560 blink_waittime = wait;
561 blink_ontime = on;
562 blink_offtime = off;
563}
564
565/* ARGSUSED */
566 static VOID CALLBACK
567_OnBlinkTimer(
568 HWND hwnd,
569 UINT uMsg,
570 UINT idEvent,
571 DWORD dwTime)
572{
573 MSG msg;
574
575 /*
576 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
577 */
578
579 KillTimer(NULL, idEvent);
580
581 /* Eat spurious WM_TIMER messages */
582 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
583 ;
584
585 if (blink_state == BLINK_ON)
586 {
587 gui_undraw_cursor();
588 blink_state = BLINK_OFF;
589 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
590 (TIMERPROC)_OnBlinkTimer);
591 }
592 else
593 {
594 gui_update_cursor(TRUE, FALSE);
595 blink_state = BLINK_ON;
596 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
597 (TIMERPROC)_OnBlinkTimer);
598 }
599}
600
601 static void
602gui_mswin_rm_blink_timer(void)
603{
604 MSG msg;
605
606 if (blink_timer != 0)
607 {
608 KillTimer(NULL, blink_timer);
609 /* Eat spurious WM_TIMER messages */
610 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
611 ;
612 blink_timer = 0;
613 }
614}
615
616/*
617 * Stop the cursor blinking. Show the cursor if it wasn't shown.
618 */
619 void
620gui_mch_stop_blink(void)
621{
622 gui_mswin_rm_blink_timer();
623 if (blink_state == BLINK_OFF)
624 gui_update_cursor(TRUE, FALSE);
625 blink_state = BLINK_NONE;
626}
627
628/*
629 * Start the cursor blinking. If it was already blinking, this restarts the
630 * waiting time and shows the cursor.
631 */
632 void
633gui_mch_start_blink(void)
634{
635 gui_mswin_rm_blink_timer();
636
637 /* Only switch blinking on if none of the times is zero */
638 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
639 {
640 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
641 (TIMERPROC)_OnBlinkTimer);
642 blink_state = BLINK_ON;
643 gui_update_cursor(TRUE, FALSE);
644 }
645}
646
647/*
648 * Call-back routines.
649 */
650
651/*ARGSUSED*/
652 static VOID CALLBACK
653_OnTimer(
654 HWND hwnd,
655 UINT uMsg,
656 UINT idEvent,
657 DWORD dwTime)
658{
659 MSG msg;
660
661 /*
662 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
663 */
664 KillTimer(NULL, idEvent);
665 s_timed_out = TRUE;
666
667 /* Eat spurious WM_TIMER messages */
668 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
669 ;
670 if (idEvent == s_wait_timer)
671 s_wait_timer = 0;
672}
673
674/*ARGSUSED*/
675 static void
676_OnDeadChar(
677 HWND hwnd,
678 UINT ch,
679 int cRepeat)
680{
681 dead_key = 1;
682}
683
684/*
685 * Convert Unicode character "ch" to bytes in "string[slen]".
686 * When "had_alt" is TRUE the ALT key was included in "ch".
687 * Return the length.
688 */
689 static int
690char_to_string(int ch, char_u *string, int slen, int had_alt)
691{
692 int len;
693 int i;
694#ifdef FEAT_MBYTE
695 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200696 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100697
698 if (os_version.dwPlatformId != VER_PLATFORM_WIN32_NT)
699 {
700 /* On Windows 95/98 we apparently get the character in the active
701 * codepage, not in UCS-2. If conversion is needed convert it to
702 * UCS-2 first. */
703 if ((int)GetACP() == enc_codepage)
704 len = 0; /* no conversion required */
705 else
706 {
707 string[0] = ch;
708 len = MultiByteToWideChar(GetACP(), 0, (LPCSTR)string,
709 1, wstring, 2);
710 }
711 }
712 else
713 {
714 wstring[0] = ch;
715 len = 1;
716 }
717
718 if (len > 0)
719 {
720 /* "ch" is a UTF-16 character. Convert it to a string of bytes. When
721 * "enc_codepage" is non-zero use the standard Win32 function,
722 * otherwise use our own conversion function (e.g., for UTF-8). */
723 if (enc_codepage > 0)
724 {
725 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
726 (LPSTR)string, slen, 0, NULL);
727 /* If we had included the ALT key into the character but now the
728 * upper bit is no longer set, that probably means the conversion
729 * failed. Convert the original character and set the upper bit
730 * afterwards. */
731 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
732 {
733 wstring[0] = ch & 0x7f;
734 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
735 (LPSTR)string, slen, 0, NULL);
736 if (len == 1) /* safety check */
737 string[0] |= 0x80;
738 }
739 }
740 else
741 {
742 len = 1;
743 ws = utf16_to_enc(wstring, &len);
744 if (ws == NULL)
745 len = 0;
746 else
747 {
748 if (len > slen) /* just in case */
749 len = slen;
750 mch_memmove(string, ws, len);
751 vim_free(ws);
752 }
753 }
754 }
755
756 if (len == 0)
757#endif
758 {
759 string[0] = ch;
760 len = 1;
761 }
762
763 for (i = 0; i < len; ++i)
764 if (string[i] == CSI && len <= slen - 2)
765 {
766 /* Insert CSI as K_CSI. */
767 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
768 string[++i] = KS_EXTRA;
769 string[++i] = (int)KE_CSI;
770 len += 2;
771 }
772
773 return len;
774}
775
776/*
777 * Key hit, add it to the input buffer.
778 */
779/*ARGSUSED*/
780 static void
781_OnChar(
782 HWND hwnd,
783 UINT ch,
784 int cRepeat)
785{
786 char_u string[40];
787 int len = 0;
788
789 dead_key = 0;
790
791 len = char_to_string(ch, string, 40, FALSE);
792 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
793 {
794 trash_input_buf();
795 got_int = TRUE;
796 }
797
798 add_to_input_buf(string, len);
799}
800
801/*
802 * Alt-Key hit, add it to the input buffer.
803 */
804/*ARGSUSED*/
805 static void
806_OnSysChar(
807 HWND hwnd,
808 UINT cch,
809 int cRepeat)
810{
811 char_u string[40]; /* Enough for multibyte character */
812 int len;
813 int modifiers;
814 int ch = cch; /* special keys are negative */
815
816 dead_key = 0;
817
818 /* TRACE("OnSysChar(%d, %c)\n", ch, ch); */
819
820 /* OK, we have a character key (given by ch) which was entered with the
821 * ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
822 * that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
823 * CAPSLOCK is pressed) at this point.
824 */
825 modifiers = MOD_MASK_ALT;
826 if (GetKeyState(VK_SHIFT) & 0x8000)
827 modifiers |= MOD_MASK_SHIFT;
828 if (GetKeyState(VK_CONTROL) & 0x8000)
829 modifiers |= MOD_MASK_CTRL;
830
831 ch = simplify_key(ch, &modifiers);
832 /* remove the SHIFT modifier for keys where it's already included, e.g.,
833 * '(' and '*' */
834 if (ch < 0x100 && !isalpha(ch) && isprint(ch))
835 modifiers &= ~MOD_MASK_SHIFT;
836
837 /* Interpret the ALT key as making the key META, include SHIFT, etc. */
838 ch = extract_modifiers(ch, &modifiers);
839 if (ch == CSI)
840 ch = K_CSI;
841
842 len = 0;
843 if (modifiers)
844 {
845 string[len++] = CSI;
846 string[len++] = KS_MODIFIER;
847 string[len++] = modifiers;
848 }
849
850 if (IS_SPECIAL((int)ch))
851 {
852 string[len++] = CSI;
853 string[len++] = K_SECOND((int)ch);
854 string[len++] = K_THIRD((int)ch);
855 }
856 else
857 {
858 /* Although the documentation isn't clear about it, we assume "ch" is
859 * a Unicode character. */
860 len += char_to_string(ch, string + len, 40 - len, TRUE);
861 }
862
863 add_to_input_buf(string, len);
864}
865
866 static void
867_OnMouseEvent(
868 int button,
869 int x,
870 int y,
871 int repeated_click,
872 UINT keyFlags)
873{
874 int vim_modifiers = 0x0;
875
876 s_getting_focus = FALSE;
877
878 if (keyFlags & MK_SHIFT)
879 vim_modifiers |= MOUSE_SHIFT;
880 if (keyFlags & MK_CONTROL)
881 vim_modifiers |= MOUSE_CTRL;
882 if (GetKeyState(VK_MENU) & 0x8000)
883 vim_modifiers |= MOUSE_ALT;
884
885 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
886}
887
888/*ARGSUSED*/
889 static void
890_OnMouseButtonDown(
891 HWND hwnd,
892 BOOL fDoubleClick,
893 int x,
894 int y,
895 UINT keyFlags)
896{
897 static LONG s_prevTime = 0;
898
899 LONG currentTime = GetMessageTime();
900 int button = -1;
901 int repeated_click;
902
903 /* Give main window the focus: this is so the cursor isn't hollow. */
904 (void)SetFocus(s_hwnd);
905
906 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
907 button = MOUSE_LEFT;
908 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
909 button = MOUSE_MIDDLE;
910 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
911 button = MOUSE_RIGHT;
912 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
913 {
914#ifndef GET_XBUTTON_WPARAM
915# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
916#endif
917 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
918 }
919 else if (s_uMsg == WM_CAPTURECHANGED)
920 {
921 /* on W95/NT4, somehow you get in here with an odd Msg
922 * if you press one button while holding down the other..*/
923 if (s_button_pending == MOUSE_LEFT)
924 button = MOUSE_RIGHT;
925 else
926 button = MOUSE_LEFT;
927 }
928 if (button >= 0)
929 {
930 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
931
932 /*
933 * Holding down the left and right buttons simulates pushing the middle
934 * button.
935 */
936 if (repeated_click
937 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
938 || (button == MOUSE_RIGHT
939 && s_button_pending == MOUSE_LEFT)))
940 {
941 /*
942 * Hmm, gui.c will ignore more than one button down at a time, so
943 * pretend we let go of it first.
944 */
945 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
946 button = MOUSE_MIDDLE;
947 repeated_click = FALSE;
948 s_button_pending = -1;
949 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
950 }
951 else if ((repeated_click)
952 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
953 {
954 if (s_button_pending > -1)
955 {
956 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
957 s_button_pending = -1;
958 }
959 /* TRACE("Button down at x %d, y %d\n", x, y); */
960 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
961 }
962 else
963 {
964 /*
965 * If this is the first press (i.e. not a multiple click) don't
966 * action immediately, but store and wait for:
967 * i) button-up
968 * ii) mouse move
969 * iii) another button press
970 * before using it.
971 * This enables us to make left+right simulate middle button,
972 * without left or right being actioned first. The side-effect is
973 * that if you click and hold the mouse without dragging, the
974 * cursor doesn't move until you release the button. In practice
975 * this is hardly a problem.
976 */
977 s_button_pending = button;
978 s_x_pending = x;
979 s_y_pending = y;
980 s_kFlags_pending = keyFlags;
981 }
982
983 s_prevTime = currentTime;
984 }
985}
986
987/*ARGSUSED*/
988 static void
989_OnMouseMoveOrRelease(
990 HWND hwnd,
991 int x,
992 int y,
993 UINT keyFlags)
994{
995 int button;
996
997 s_getting_focus = FALSE;
998 if (s_button_pending > -1)
999 {
1000 /* Delayed action for mouse down event */
1001 _OnMouseEvent(s_button_pending, s_x_pending,
1002 s_y_pending, FALSE, s_kFlags_pending);
1003 s_button_pending = -1;
1004 }
1005 if (s_uMsg == WM_MOUSEMOVE)
1006 {
1007 /*
1008 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1009 * down.
1010 */
1011 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1012 | MK_XBUTTON1 | MK_XBUTTON2)))
1013 {
1014 gui_mouse_moved(x, y);
1015 return;
1016 }
1017
1018 /*
1019 * While button is down, keep grabbing mouse move events when
1020 * the mouse goes outside the window
1021 */
1022 SetCapture(s_textArea);
1023 button = MOUSE_DRAG;
1024 /* TRACE(" move at x %d, y %d\n", x, y); */
1025 }
1026 else
1027 {
1028 ReleaseCapture();
1029 button = MOUSE_RELEASE;
1030 /* TRACE(" up at x %d, y %d\n", x, y); */
1031 }
1032
1033 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1034}
1035
1036#ifdef FEAT_MENU
1037/*
1038 * Find the vimmenu_T with the given id
1039 */
1040 static vimmenu_T *
1041gui_mswin_find_menu(
1042 vimmenu_T *pMenu,
1043 int id)
1044{
1045 vimmenu_T *pChildMenu;
1046
1047 while (pMenu)
1048 {
1049 if (pMenu->id == (UINT)id)
1050 break;
1051 if (pMenu->children != NULL)
1052 {
1053 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1054 if (pChildMenu)
1055 {
1056 pMenu = pChildMenu;
1057 break;
1058 }
1059 }
1060 pMenu = pMenu->next;
1061 }
1062 return pMenu;
1063}
1064
1065/*ARGSUSED*/
1066 static void
1067_OnMenu(
1068 HWND hwnd,
1069 int id,
1070 HWND hwndCtl,
1071 UINT codeNotify)
1072{
1073 vimmenu_T *pMenu;
1074
1075 pMenu = gui_mswin_find_menu(root_menu, id);
1076 if (pMenu)
1077 gui_menu_cb(pMenu);
1078}
1079#endif
1080
1081#ifdef MSWIN_FIND_REPLACE
1082# if defined(FEAT_MBYTE) && defined(WIN3264)
1083/*
1084 * copy useful data from structure LPFINDREPLACE to structure LPFINDREPLACEW
1085 */
1086 static void
1087findrep_atow(LPFINDREPLACEW lpfrw, LPFINDREPLACE lpfr)
1088{
1089 WCHAR *wp;
1090
1091 lpfrw->hwndOwner = lpfr->hwndOwner;
1092 lpfrw->Flags = lpfr->Flags;
1093
1094 wp = enc_to_utf16((char_u *)lpfr->lpstrFindWhat, NULL);
1095 wcsncpy(lpfrw->lpstrFindWhat, wp, lpfrw->wFindWhatLen - 1);
1096 vim_free(wp);
1097
1098 /* the field "lpstrReplaceWith" doesn't need to be copied */
1099}
1100
1101/*
1102 * copy useful data from structure LPFINDREPLACEW to structure LPFINDREPLACE
1103 */
1104 static void
1105findrep_wtoa(LPFINDREPLACE lpfr, LPFINDREPLACEW lpfrw)
1106{
1107 char_u *p;
1108
1109 lpfr->Flags = lpfrw->Flags;
1110
1111 p = utf16_to_enc((short_u*)lpfrw->lpstrFindWhat, NULL);
1112 vim_strncpy((char_u *)lpfr->lpstrFindWhat, p, lpfr->wFindWhatLen - 1);
1113 vim_free(p);
1114
1115 p = utf16_to_enc((short_u*)lpfrw->lpstrReplaceWith, NULL);
1116 vim_strncpy((char_u *)lpfr->lpstrReplaceWith, p, lpfr->wReplaceWithLen - 1);
1117 vim_free(p);
1118}
1119# endif
1120
1121/*
1122 * Handle a Find/Replace window message.
1123 */
1124 static void
1125_OnFindRepl(void)
1126{
1127 int flags = 0;
1128 int down;
1129
1130# if defined(FEAT_MBYTE) && defined(WIN3264)
1131 /* If the OS is Windows NT, and 'encoding' differs from active codepage:
1132 * convert text from wide string. */
1133 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
1134 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1135 {
1136 findrep_wtoa(&s_findrep_struct, &s_findrep_struct_w);
1137 }
1138# endif
1139
1140 if (s_findrep_struct.Flags & FR_DIALOGTERM)
1141 /* Give main window the focus back. */
1142 (void)SetFocus(s_hwnd);
1143
1144 if (s_findrep_struct.Flags & FR_FINDNEXT)
1145 {
1146 flags = FRD_FINDNEXT;
1147
1148 /* Give main window the focus back: this is so the cursor isn't
1149 * hollow. */
1150 (void)SetFocus(s_hwnd);
1151 }
1152 else if (s_findrep_struct.Flags & FR_REPLACE)
1153 {
1154 flags = FRD_REPLACE;
1155
1156 /* Give main window the focus back: this is so the cursor isn't
1157 * hollow. */
1158 (void)SetFocus(s_hwnd);
1159 }
1160 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1161 {
1162 flags = FRD_REPLACEALL;
1163 }
1164
1165 if (flags != 0)
1166 {
1167 /* Call the generic GUI function to do the actual work. */
1168 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1169 flags |= FRD_WHOLE_WORD;
1170 if (s_findrep_struct.Flags & FR_MATCHCASE)
1171 flags |= FRD_MATCH_CASE;
1172 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
1173 gui_do_findrepl(flags, (char_u *)s_findrep_struct.lpstrFindWhat,
1174 (char_u *)s_findrep_struct.lpstrReplaceWith, down);
1175 }
1176}
1177#endif
1178
1179 static void
1180HandleMouseHide(UINT uMsg, LPARAM lParam)
1181{
1182 static LPARAM last_lParam = 0L;
1183
1184 /* We sometimes get a mousemove when the mouse didn't move... */
1185 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1186 {
1187 if (lParam == last_lParam)
1188 return;
1189 last_lParam = lParam;
1190 }
1191
1192 /* Handle specially, to centralise coding. We need to be sure we catch all
1193 * possible events which should cause us to restore the cursor (as it is a
1194 * shared resource, we take full responsibility for it).
1195 */
1196 switch (uMsg)
1197 {
1198 case WM_KEYUP:
1199 case WM_CHAR:
1200 /*
1201 * blank out the pointer if necessary
1202 */
1203 if (p_mh)
1204 gui_mch_mousehide(TRUE);
1205 break;
1206
1207 case WM_SYSKEYUP: /* show the pointer when a system-key is pressed */
1208 case WM_SYSCHAR:
1209 case WM_MOUSEMOVE: /* show the pointer on any mouse action */
1210 case WM_LBUTTONDOWN:
1211 case WM_LBUTTONUP:
1212 case WM_MBUTTONDOWN:
1213 case WM_MBUTTONUP:
1214 case WM_RBUTTONDOWN:
1215 case WM_RBUTTONUP:
1216 case WM_XBUTTONDOWN:
1217 case WM_XBUTTONUP:
1218 case WM_NCMOUSEMOVE:
1219 case WM_NCLBUTTONDOWN:
1220 case WM_NCLBUTTONUP:
1221 case WM_NCMBUTTONDOWN:
1222 case WM_NCMBUTTONUP:
1223 case WM_NCRBUTTONDOWN:
1224 case WM_NCRBUTTONUP:
1225 case WM_KILLFOCUS:
1226 /*
1227 * if the pointer is currently hidden, then we should show it.
1228 */
1229 gui_mch_mousehide(FALSE);
1230 break;
1231 }
1232}
1233
1234 static LRESULT CALLBACK
1235_TextAreaWndProc(
1236 HWND hwnd,
1237 UINT uMsg,
1238 WPARAM wParam,
1239 LPARAM lParam)
1240{
1241 /*
1242 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1243 hwnd, uMsg, wParam, lParam);
1244 */
1245
1246 HandleMouseHide(uMsg, lParam);
1247
1248 s_uMsg = uMsg;
1249 s_wParam = wParam;
1250 s_lParam = lParam;
1251
1252#ifdef FEAT_BEVAL
1253 TrackUserActivity(uMsg);
1254#endif
1255
1256 switch (uMsg)
1257 {
1258 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1259 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1260 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1261 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1262 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1263 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1264 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1265 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1266 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1267 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1268 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1269 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1270 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1271 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
1272
1273#ifdef FEAT_BEVAL
1274 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1275 return TRUE;
1276#endif
1277 default:
1278 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1279 }
1280}
1281
1282#if (defined(WIN3264) && defined(FEAT_MBYTE)) \
1283 || defined(GLOBAL_IME) \
1284 || defined(PROTO)
1285# ifdef PROTO
1286typedef int WINAPI;
1287# endif
1288
1289 LRESULT WINAPI
1290vim_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1291{
1292# ifdef GLOBAL_IME
1293 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
1294# else
1295 if (wide_WindowProc)
1296 return DefWindowProcW(hwnd, message, wParam, lParam);
1297 return DefWindowProc(hwnd, message, wParam, lParam);
1298#endif
1299}
1300#endif
1301
1302/*
1303 * Called when the foreground or background color has been changed.
1304 */
1305 void
1306gui_mch_new_colors(void)
1307{
1308 /* nothing to do? */
1309}
1310
1311/*
1312 * Set the colors to their default values.
1313 */
1314 void
1315gui_mch_def_colors(void)
1316{
1317 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1318 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1319 gui.def_norm_pixel = gui.norm_pixel;
1320 gui.def_back_pixel = gui.back_pixel;
1321}
1322
1323/*
1324 * Open the GUI window which was created by a call to gui_mch_init().
1325 */
1326 int
1327gui_mch_open(void)
1328{
1329#ifndef SW_SHOWDEFAULT
1330# define SW_SHOWDEFAULT 10 /* Borland 5.0 doesn't have it */
1331#endif
1332 /* Actually open the window, if not already visible
1333 * (may be done already in gui_mch_set_shellsize) */
1334 if (!IsWindowVisible(s_hwnd))
1335 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1336
1337#ifdef MSWIN_FIND_REPLACE
1338 /* Init replace string here, so that we keep it when re-opening the
1339 * dialog. */
1340 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1341#endif
1342
1343 return OK;
1344}
1345
1346/*
1347 * Get the position of the top left corner of the window.
1348 */
1349 int
1350gui_mch_get_winpos(int *x, int *y)
1351{
1352 RECT rect;
1353
1354 GetWindowRect(s_hwnd, &rect);
1355 *x = rect.left;
1356 *y = rect.top;
1357 return OK;
1358}
1359
1360/*
1361 * Set the position of the top left corner of the window to the given
1362 * coordinates.
1363 */
1364 void
1365gui_mch_set_winpos(int x, int y)
1366{
1367 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1368 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1369}
1370 void
1371gui_mch_set_text_area_pos(int x, int y, int w, int h)
1372{
1373 static int oldx = 0;
1374 static int oldy = 0;
1375
1376 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1377
1378#ifdef FEAT_TOOLBAR
1379 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1380 SendMessage(s_toolbarhwnd, WM_SIZE,
1381 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1382#endif
1383#if defined(FEAT_GUI_TABLINE)
1384 if (showing_tabline)
1385 {
1386 int top = 0;
1387 RECT rect;
1388
1389# ifdef FEAT_TOOLBAR
1390 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1391 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1392# endif
1393 GetClientRect(s_hwnd, &rect);
1394 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1395 }
1396#endif
1397
1398 /* When side scroll bar is unshown, the size of window will change.
1399 * then, the text area move left or right. thus client rect should be
1400 * forcedly redrawn. (Yasuhiro Matsumoto) */
1401 if (oldx != x || oldy != y)
1402 {
1403 InvalidateRect(s_hwnd, NULL, FALSE);
1404 oldx = x;
1405 oldy = y;
1406 }
1407}
1408
1409
1410/*
1411 * Scrollbar stuff:
1412 */
1413
1414 void
1415gui_mch_enable_scrollbar(
1416 scrollbar_T *sb,
1417 int flag)
1418{
1419 ShowScrollBar(sb->id, SB_CTL, flag);
1420
1421 /* TODO: When the window is maximized, the size of the window stays the
1422 * same, thus the size of the text area changes. On Win98 it's OK, on Win
1423 * NT 4.0 it's not... */
1424}
1425
1426 void
1427gui_mch_set_scrollbar_pos(
1428 scrollbar_T *sb,
1429 int x,
1430 int y,
1431 int w,
1432 int h)
1433{
1434 SetWindowPos(sb->id, NULL, x, y, w, h,
1435 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1436}
1437
1438 void
1439gui_mch_create_scrollbar(
1440 scrollbar_T *sb,
1441 int orient) /* SBAR_VERT or SBAR_HORIZ */
1442{
1443 sb->id = CreateWindow(
1444 "SCROLLBAR", "Scrollbar",
1445 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
1446 10, /* Any value will do for now */
1447 10, /* Any value will do for now */
1448 s_hwnd, NULL,
1449 s_hinst, NULL);
1450}
1451
1452/*
1453 * Find the scrollbar with the given hwnd.
1454 */
1455 static scrollbar_T *
1456gui_mswin_find_scrollbar(HWND hwnd)
1457{
1458 win_T *wp;
1459
1460 if (gui.bottom_sbar.id == hwnd)
1461 return &gui.bottom_sbar;
1462 FOR_ALL_WINDOWS(wp)
1463 {
1464 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1465 return &wp->w_scrollbars[SBAR_LEFT];
1466 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1467 return &wp->w_scrollbars[SBAR_RIGHT];
1468 }
1469 return NULL;
1470}
1471
1472/*
1473 * Get the character size of a font.
1474 */
1475 static void
1476GetFontSize(GuiFont font)
1477{
1478 HWND hwnd = GetDesktopWindow();
1479 HDC hdc = GetWindowDC(hwnd);
1480 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
1481 TEXTMETRIC tm;
1482
1483 GetTextMetrics(hdc, &tm);
1484 gui.char_width = tm.tmAveCharWidth + tm.tmOverhang;
1485
1486 gui.char_height = tm.tmHeight + p_linespace;
1487
1488 SelectFont(hdc, hfntOld);
1489
1490 ReleaseDC(hwnd, hdc);
1491}
1492
1493/*
1494 * Adjust gui.char_height (after 'linespace' was changed).
1495 */
1496 int
1497gui_mch_adjust_charheight(void)
1498{
1499 GetFontSize(gui.norm_font);
1500 return OK;
1501}
1502
1503 static GuiFont
1504get_font_handle(LOGFONT *lf)
1505{
1506 HFONT font = NULL;
1507
1508 /* Load the font */
1509 font = CreateFontIndirect(lf);
1510
1511 if (font == NULL)
1512 return NOFONT;
1513
1514 return (GuiFont)font;
1515}
1516
1517 static int
1518pixels_to_points(int pixels, int vertical)
1519{
1520 int points;
1521 HWND hwnd;
1522 HDC hdc;
1523
1524 hwnd = GetDesktopWindow();
1525 hdc = GetWindowDC(hwnd);
1526
1527 points = MulDiv(pixels, 72,
1528 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1529
1530 ReleaseDC(hwnd, hdc);
1531
1532 return points;
1533}
1534
1535 GuiFont
1536gui_mch_get_font(
1537 char_u *name,
1538 int giveErrorIfMissing)
1539{
1540 LOGFONT lf;
1541 GuiFont font = NOFONT;
1542
1543 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1544 font = get_font_handle(&lf);
1545 if (font == NOFONT && giveErrorIfMissing)
1546 EMSG2(_(e_font), name);
1547 return font;
1548}
1549
1550#if defined(FEAT_EVAL) || defined(PROTO)
1551/*
1552 * Return the name of font "font" in allocated memory.
1553 * Don't know how to get the actual name, thus use the provided name.
1554 */
1555/*ARGSUSED*/
1556 char_u *
1557gui_mch_get_fontname(GuiFont font, char_u *name)
1558{
1559 if (name == NULL)
1560 return NULL;
1561 return vim_strsave(name);
1562}
1563#endif
1564
1565 void
1566gui_mch_free_font(GuiFont font)
1567{
1568 if (font)
1569 DeleteObject((HFONT)font);
1570}
1571
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001572/*
1573 * Return the Pixel value (color) for the given color name.
1574 * Return INVALCOLOR for error.
1575 */
1576 guicolor_T
1577gui_mch_get_color(char_u *name)
1578{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001579 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001580
1581 typedef struct SysColorTable
1582 {
1583 char *name;
1584 int color;
1585 } SysColorTable;
1586
1587 static SysColorTable sys_table[] =
1588 {
1589#ifdef WIN3264
1590 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1591 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
1592#ifndef __MINGW32__
1593 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1594#endif
1595 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1596 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1597 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1598 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1599 {"SYS_DESKTOP", COLOR_DESKTOP},
1600 {"SYS_INFOBK", COLOR_INFOBK},
1601 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1602 {"SYS_3DFACE", COLOR_3DFACE},
1603#endif
1604 {"SYS_BTNFACE", COLOR_BTNFACE},
1605 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1606 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1607 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1608 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1609 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1610 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1611 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1612 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1613 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1614 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1615 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1616 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1617 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1618 {"SYS_MENU", COLOR_MENU},
1619 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1620 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1621 {"SYS_WINDOW", COLOR_WINDOW},
1622 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1623 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1624 };
1625
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001626 /*
1627 * Try to look up a system colour.
1628 */
1629 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1630 if (STRICMP(name, sys_table[i].name) == 0)
1631 return GetSysColor(sys_table[i].color);
1632
Bram Moolenaarab302212016-04-26 20:59:29 +02001633 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001634}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001635
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001636/*
1637 * Return OK if the key with the termcap name "name" is supported.
1638 */
1639 int
1640gui_mch_haskey(char_u *name)
1641{
1642 int i;
1643
1644 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1645 if (name[0] == special_keys[i].vim_code0 &&
1646 name[1] == special_keys[i].vim_code1)
1647 return OK;
1648 return FAIL;
1649}
1650
1651 void
1652gui_mch_beep(void)
1653{
1654 MessageBeep(MB_OK);
1655}
1656/*
1657 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1658 */
1659 void
1660gui_mch_invert_rectangle(
1661 int r,
1662 int c,
1663 int nr,
1664 int nc)
1665{
1666 RECT rc;
1667
1668 /*
1669 * Note: InvertRect() excludes right and bottom of rectangle.
1670 */
1671 rc.left = FILL_X(c);
1672 rc.top = FILL_Y(r);
1673 rc.right = rc.left + nc * gui.char_width;
1674 rc.bottom = rc.top + nr * gui.char_height;
1675 InvertRect(s_hdc, &rc);
1676}
1677
1678/*
1679 * Iconify the GUI window.
1680 */
1681 void
1682gui_mch_iconify(void)
1683{
1684 ShowWindow(s_hwnd, SW_MINIMIZE);
1685}
1686
1687/*
1688 * Draw a cursor without focus.
1689 */
1690 void
1691gui_mch_draw_hollow_cursor(guicolor_T color)
1692{
1693 HBRUSH hbr;
1694 RECT rc;
1695
1696 /*
1697 * Note: FrameRect() excludes right and bottom of rectangle.
1698 */
1699 rc.left = FILL_X(gui.col);
1700 rc.top = FILL_Y(gui.row);
1701 rc.right = rc.left + gui.char_width;
1702#ifdef FEAT_MBYTE
1703 if (mb_lefthalve(gui.row, gui.col))
1704 rc.right += gui.char_width;
1705#endif
1706 rc.bottom = rc.top + gui.char_height;
1707 hbr = CreateSolidBrush(color);
1708 FrameRect(s_hdc, &rc, hbr);
1709 DeleteBrush(hbr);
1710}
1711/*
1712 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1713 * color "color".
1714 */
1715 void
1716gui_mch_draw_part_cursor(
1717 int w,
1718 int h,
1719 guicolor_T color)
1720{
1721 HBRUSH hbr;
1722 RECT rc;
1723
1724 /*
1725 * Note: FillRect() excludes right and bottom of rectangle.
1726 */
1727 rc.left =
1728#ifdef FEAT_RIGHTLEFT
1729 /* vertical line should be on the right of current point */
1730 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1731#endif
1732 FILL_X(gui.col);
1733 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1734 rc.right = rc.left + w;
1735 rc.bottom = rc.top + h;
1736 hbr = CreateSolidBrush(color);
1737 FillRect(s_hdc, &rc, hbr);
1738 DeleteBrush(hbr);
1739}
1740
1741
1742/*
1743 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1744 * dead key's nominal character and re-post the original message.
1745 */
1746 static void
1747outputDeadKey_rePost(MSG originalMsg)
1748{
1749 static MSG deadCharExpel;
1750
1751 if (!dead_key)
1752 return;
1753
1754 dead_key = 0;
1755
1756 /* Make Windows generate the dead key's character */
1757 deadCharExpel.message = originalMsg.message;
1758 deadCharExpel.hwnd = originalMsg.hwnd;
1759 deadCharExpel.wParam = VK_SPACE;
1760
1761 MyTranslateMessage(&deadCharExpel);
1762
1763 /* re-generate the current character free of the dead char influence */
1764 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1765 originalMsg.lParam);
1766}
1767
1768
1769/*
1770 * Process a single Windows message.
1771 * If one is not available we hang until one is.
1772 */
1773 static void
1774process_message(void)
1775{
1776 MSG msg;
1777 UINT vk = 0; /* Virtual key */
1778 char_u string[40];
1779 int i;
1780 int modifiers = 0;
1781 int key;
1782#ifdef FEAT_MENU
1783 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1784#endif
1785
1786 pGetMessage(&msg, NULL, 0, 0);
1787
1788#ifdef FEAT_OLE
1789 /* Look after OLE Automation commands */
1790 if (msg.message == WM_OLE)
1791 {
1792 char_u *str = (char_u *)msg.lParam;
1793 if (str == NULL || *str == NUL)
1794 {
1795 /* Message can't be ours, forward it. Fixes problem with Ultramon
1796 * 3.0.4 */
1797 pDispatchMessage(&msg);
1798 }
1799 else
1800 {
1801 add_to_input_buf(str, (int)STRLEN(str));
1802 vim_free(str); /* was allocated in CVim::SendKeys() */
1803 }
1804 return;
1805 }
1806#endif
1807
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001808#ifdef MSWIN_FIND_REPLACE
1809 /* Don't process messages used by the dialog */
1810 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1811 {
1812 HandleMouseHide(msg.message, msg.lParam);
1813 return;
1814 }
1815#endif
1816
1817 /*
1818 * Check if it's a special key that we recognise. If not, call
1819 * TranslateMessage().
1820 */
1821 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1822 {
1823 vk = (int) msg.wParam;
1824
1825 /*
1826 * Handle dead keys in special conditions in other cases we let Windows
1827 * handle them and do not interfere.
1828 *
1829 * The dead_key flag must be reset on several occasions:
1830 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1831 * consumed at that point (This is when we let Windows combine the
1832 * dead character on its own)
1833 *
1834 * - Before doing something special such as regenerating keypresses to
1835 * expel the dead character as this could trigger an infinite loop if
1836 * for some reason MyTranslateMessage() do not trigger a call
1837 * immediately to _OnChar() (or _OnSysChar()).
1838 */
1839 if (dead_key)
1840 {
1841 /*
1842 * If a dead key was pressed and the user presses VK_SPACE,
1843 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1844 * with the dead char now, so do nothing special and let Windows
1845 * handle it.
1846 *
1847 * Note that VK_SPACE combines with the dead_key's character and
1848 * only one WM_CHAR will be generated by TranslateMessage(), in
1849 * the two other cases two WM_CHAR will be generated: the dead
1850 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1851 * user expects.
1852 */
1853 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1854 {
1855 dead_key = 0;
1856 MyTranslateMessage(&msg);
1857 return;
1858 }
1859 /* In modes where we are not typing, dead keys should behave
1860 * normally */
1861 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1862 {
1863 outputDeadKey_rePost(msg);
1864 return;
1865 }
1866 }
1867
1868 /* Check for CTRL-BREAK */
1869 if (vk == VK_CANCEL)
1870 {
1871 trash_input_buf();
1872 got_int = TRUE;
1873 string[0] = Ctrl_C;
1874 add_to_input_buf(string, 1);
1875 }
1876
1877 for (i = 0; special_keys[i].key_sym != 0; i++)
1878 {
1879 /* ignore VK_SPACE when ALT key pressed: system menu */
1880 if (special_keys[i].key_sym == vk
1881 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1882 {
1883 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001884 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001885 * is a key that would normally trigger the dead key nominal
1886 * character output (such as a NUMPAD printable character or
1887 * the TAB key, etc...).
1888 */
1889 if (dead_key && (special_keys[i].vim_code0 == 'K'
1890 || vk == VK_TAB || vk == CAR))
1891 {
1892 outputDeadKey_rePost(msg);
1893 return;
1894 }
1895
1896#ifdef FEAT_MENU
1897 /* Check for <F10>: Windows selects the menu. When <F10> is
1898 * mapped we want to use the mapping instead. */
1899 if (vk == VK_F10
1900 && gui.menu_is_active
1901 && check_map(k10, State, FALSE, TRUE, FALSE,
1902 NULL, NULL) == NULL)
1903 break;
1904#endif
1905 if (GetKeyState(VK_SHIFT) & 0x8000)
1906 modifiers |= MOD_MASK_SHIFT;
1907 /*
1908 * Don't use caps-lock as shift, because these are special keys
1909 * being considered here, and we only want letters to get
1910 * shifted -- webb
1911 */
1912 /*
1913 if (GetKeyState(VK_CAPITAL) & 0x0001)
1914 modifiers ^= MOD_MASK_SHIFT;
1915 */
1916 if (GetKeyState(VK_CONTROL) & 0x8000)
1917 modifiers |= MOD_MASK_CTRL;
1918 if (GetKeyState(VK_MENU) & 0x8000)
1919 modifiers |= MOD_MASK_ALT;
1920
1921 if (special_keys[i].vim_code1 == NUL)
1922 key = special_keys[i].vim_code0;
1923 else
1924 key = TO_SPECIAL(special_keys[i].vim_code0,
1925 special_keys[i].vim_code1);
1926 key = simplify_key(key, &modifiers);
1927 if (key == CSI)
1928 key = K_CSI;
1929
1930 if (modifiers)
1931 {
1932 string[0] = CSI;
1933 string[1] = KS_MODIFIER;
1934 string[2] = modifiers;
1935 add_to_input_buf(string, 3);
1936 }
1937
1938 if (IS_SPECIAL(key))
1939 {
1940 string[0] = CSI;
1941 string[1] = K_SECOND(key);
1942 string[2] = K_THIRD(key);
1943 add_to_input_buf(string, 3);
1944 }
1945 else
1946 {
1947 int len;
1948
1949 /* Handle "key" as a Unicode character. */
1950 len = char_to_string(key, string, 40, FALSE);
1951 add_to_input_buf(string, len);
1952 }
1953 break;
1954 }
1955 }
1956 if (special_keys[i].key_sym == 0)
1957 {
1958 /* Some keys need C-S- where they should only need C-.
1959 * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1960 * system startup (Helmut Stiegler, 2003 Oct 3). */
1961 if (vk != 0xff
1962 && (GetKeyState(VK_CONTROL) & 0x8000)
1963 && !(GetKeyState(VK_SHIFT) & 0x8000)
1964 && !(GetKeyState(VK_MENU) & 0x8000))
1965 {
1966 /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */
1967 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1968 {
1969 string[0] = Ctrl_HAT;
1970 add_to_input_buf(string, 1);
1971 }
1972 /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */
1973 else if (vk == 0xBD) /* QWERTY for CTRL-'-' */
1974 {
1975 string[0] = Ctrl__;
1976 add_to_input_buf(string, 1);
1977 }
1978 /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */
1979 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
1980 {
1981 string[0] = Ctrl_AT;
1982 add_to_input_buf(string, 1);
1983 }
1984 else
1985 MyTranslateMessage(&msg);
1986 }
1987 else
1988 MyTranslateMessage(&msg);
1989 }
1990 }
1991#ifdef FEAT_MBYTE_IME
1992 else if (msg.message == WM_IME_NOTIFY)
1993 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
1994 else if (msg.message == WM_KEYUP && im_get_status())
1995 /* added for non-MS IME (Yasuhiro Matsumoto) */
1996 MyTranslateMessage(&msg);
1997#endif
1998#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
1999/* GIME_TEST */
2000 else if (msg.message == WM_IME_STARTCOMPOSITION)
2001 {
2002 POINT point;
2003
2004 global_ime_set_font(&norm_logfont);
2005 point.x = FILL_X(gui.col);
2006 point.y = FILL_Y(gui.row);
2007 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
2008 global_ime_set_position(&point);
2009 }
2010#endif
2011
2012#ifdef FEAT_MENU
2013 /* Check for <F10>: Default effect is to select the menu. When <F10> is
2014 * mapped we need to stop it here to avoid strange effects (e.g., for the
2015 * key-up event) */
2016 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2017 NULL, NULL) == NULL)
2018#endif
2019 pDispatchMessage(&msg);
2020}
2021
2022/*
2023 * Catch up with any queued events. This may put keyboard input into the
2024 * input buffer, call resize call-backs, trigger timers etc. If there is
2025 * nothing in the event queue (& no timers pending), then we return
2026 * immediately.
2027 */
2028 void
2029gui_mch_update(void)
2030{
2031 MSG msg;
2032
2033 if (!s_busy_processing)
2034 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2035 && !vim_is_input_buf_full())
2036 process_message();
2037}
2038
Bram Moolenaar4231da42016-06-02 14:30:04 +02002039 static void
2040remove_any_timer(void)
2041{
2042 MSG msg;
2043
2044 if (s_wait_timer != 0 && !s_timed_out)
2045 {
2046 KillTimer(NULL, s_wait_timer);
2047
2048 /* Eat spurious WM_TIMER messages */
2049 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2050 ;
2051 s_wait_timer = 0;
2052 }
2053}
2054
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002055/*
2056 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2057 * from the keyboard.
2058 * wtime == -1 Wait forever.
2059 * wtime == 0 This should never happen.
2060 * wtime > 0 Wait wtime milliseconds for a character.
2061 * Returns OK if a character was found to be available within the given time,
2062 * or FAIL otherwise.
2063 */
2064 int
2065gui_mch_wait_for_chars(int wtime)
2066{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002067 int focus;
2068
2069 s_timed_out = FALSE;
2070
2071 if (wtime > 0)
2072 {
2073 /* Don't do anything while processing a (scroll) message. */
2074 if (s_busy_processing)
2075 return FAIL;
2076 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)wtime,
2077 (TIMERPROC)_OnTimer);
2078 }
2079
2080 allow_scrollbar = TRUE;
2081
2082 focus = gui.in_focus;
2083 while (!s_timed_out)
2084 {
2085 /* Stop or start blinking when focus changes */
2086 if (gui.in_focus != focus)
2087 {
2088 if (gui.in_focus)
2089 gui_mch_start_blink();
2090 else
2091 gui_mch_stop_blink();
2092 focus = gui.in_focus;
2093 }
2094
2095 if (s_need_activate)
2096 {
2097#ifdef WIN32
2098 (void)SetForegroundWindow(s_hwnd);
2099#else
2100 (void)SetActiveWindow(s_hwnd);
2101#endif
2102 s_need_activate = FALSE;
2103 }
2104
Bram Moolenaar4231da42016-06-02 14:30:04 +02002105#ifdef FEAT_TIMERS
2106 did_add_timer = FALSE;
2107#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002108#ifdef MESSAGE_QUEUE
Bram Moolenaar9186a272016-02-23 19:34:01 +01002109 /* Check channel while waiting message. */
2110 for (;;)
2111 {
2112 MSG msg;
2113
2114 parse_queued_messages();
2115
2116 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarf28d8712016-04-02 15:59:40 +02002117 || MsgWaitForMultipleObjects(0, NULL, FALSE, 100, QS_ALLINPUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002118 != WAIT_TIMEOUT)
2119 break;
2120 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002121#endif
2122
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002123 /*
2124 * Don't use gui_mch_update() because then we will spin-lock until a
2125 * char arrives, instead we use GetMessage() to hang until an
2126 * event arrives. No need to check for input_buf_full because we are
2127 * returning as soon as it contains a single char -- webb
2128 */
2129 process_message();
2130
2131 if (input_available())
2132 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002133 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002134 allow_scrollbar = FALSE;
2135
2136 /* Clear pending mouse button, the release event may have been
2137 * taken by the dialog window. But don't do this when getting
2138 * focus, we need the mouse-up event then. */
2139 if (!s_getting_focus)
2140 s_button_pending = -1;
2141
2142 return OK;
2143 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002144
2145#ifdef FEAT_TIMERS
2146 if (did_add_timer)
2147 {
2148 /* Need to recompute the waiting time. */
2149 remove_any_timer();
2150 break;
2151 }
2152#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002153 }
2154 allow_scrollbar = FALSE;
2155 return FAIL;
2156}
2157
2158/*
2159 * Clear a rectangular region of the screen from text pos (row1, col1) to
2160 * (row2, col2) inclusive.
2161 */
2162 void
2163gui_mch_clear_block(
2164 int row1,
2165 int col1,
2166 int row2,
2167 int col2)
2168{
2169 RECT rc;
2170
2171 /*
2172 * Clear one extra pixel at the far right, for when bold characters have
2173 * spilled over to the window border.
2174 * Note: FillRect() excludes right and bottom of rectangle.
2175 */
2176 rc.left = FILL_X(col1);
2177 rc.top = FILL_Y(row1);
2178 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2179 rc.bottom = FILL_Y(row2 + 1);
2180 clear_rect(&rc);
2181}
2182
2183/*
2184 * Clear the whole text window.
2185 */
2186 void
2187gui_mch_clear_all(void)
2188{
2189 RECT rc;
2190
2191 rc.left = 0;
2192 rc.top = 0;
2193 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2194 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2195 clear_rect(&rc);
2196}
2197/*
2198 * Menu stuff.
2199 */
2200
2201 void
2202gui_mch_enable_menu(int flag)
2203{
2204#ifdef FEAT_MENU
2205 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2206#endif
2207}
2208
2209/*ARGSUSED*/
2210 void
2211gui_mch_set_menu_pos(
2212 int x,
2213 int y,
2214 int w,
2215 int h)
2216{
2217 /* It will be in the right place anyway */
2218}
2219
2220#if defined(FEAT_MENU) || defined(PROTO)
2221/*
2222 * Make menu item hidden or not hidden
2223 */
2224 void
2225gui_mch_menu_hidden(
2226 vimmenu_T *menu,
2227 int hidden)
2228{
2229 /*
2230 * This doesn't do what we want. Hmm, just grey the menu items for now.
2231 */
2232 /*
2233 if (hidden)
2234 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2235 else
2236 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2237 */
2238 gui_mch_menu_grey(menu, hidden);
2239}
2240
2241/*
2242 * This is called after setting all the menus to grey/hidden or not.
2243 */
2244 void
2245gui_mch_draw_menubar(void)
2246{
2247 DrawMenuBar(s_hwnd);
2248}
2249#endif /*FEAT_MENU*/
2250
2251#ifndef PROTO
2252void
2253#ifdef VIMDLL
2254_export
2255#endif
2256_cdecl
2257SaveInst(HINSTANCE hInst)
2258{
2259 s_hinst = hInst;
2260}
2261#endif
2262
2263/*
2264 * Return the RGB value of a pixel as a long.
2265 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002266 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002267gui_mch_get_rgb(guicolor_T pixel)
2268{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002269 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2270 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002271}
2272
2273#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2274/* Convert pixels in X to dialog units */
2275 static WORD
2276PixelToDialogX(int numPixels)
2277{
2278 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2279}
2280
2281/* Convert pixels in Y to dialog units */
2282 static WORD
2283PixelToDialogY(int numPixels)
2284{
2285 return (WORD)((numPixels * 8) / s_dlgfntheight);
2286}
2287
2288/* Return the width in pixels of the given text in the given DC. */
2289 static int
2290GetTextWidth(HDC hdc, char_u *str, int len)
2291{
2292 SIZE size;
2293
2294 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2295 return size.cx;
2296}
2297
2298#ifdef FEAT_MBYTE
2299/*
2300 * Return the width in pixels of the given text in the given DC, taking care
2301 * of 'encoding' to active codepage conversion.
2302 */
2303 static int
2304GetTextWidthEnc(HDC hdc, char_u *str, int len)
2305{
2306 SIZE size;
2307 WCHAR *wstr;
2308 int n;
2309 int wlen = len;
2310
2311 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2312 {
2313 /* 'encoding' differs from active codepage: convert text and use wide
2314 * function */
2315 wstr = enc_to_utf16(str, &wlen);
2316 if (wstr != NULL)
2317 {
2318 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2319 vim_free(wstr);
2320 if (n)
2321 return size.cx;
2322 }
2323 }
2324
2325 return GetTextWidth(hdc, str, len);
2326}
2327#else
2328# define GetTextWidthEnc(h, s, l) GetTextWidth((h), (s), (l))
2329#endif
2330
2331/*
2332 * A quick little routine that will center one window over another, handy for
2333 * dialog boxes. Taken from the Win32SDK samples.
2334 */
2335 static BOOL
2336CenterWindow(
2337 HWND hwndChild,
2338 HWND hwndParent)
2339{
2340 RECT rChild, rParent;
2341 int wChild, hChild, wParent, hParent;
2342 int wScreen, hScreen, xNew, yNew;
2343 HDC hdc;
2344
2345 GetWindowRect(hwndChild, &rChild);
2346 wChild = rChild.right - rChild.left;
2347 hChild = rChild.bottom - rChild.top;
2348
2349 /* If Vim is minimized put the window in the middle of the screen. */
2350 if (hwndParent == NULL || IsMinimized(hwndParent))
2351 SystemParametersInfo(SPI_GETWORKAREA, 0, &rParent, 0);
2352 else
2353 GetWindowRect(hwndParent, &rParent);
2354 wParent = rParent.right - rParent.left;
2355 hParent = rParent.bottom - rParent.top;
2356
2357 hdc = GetDC(hwndChild);
2358 wScreen = GetDeviceCaps (hdc, HORZRES);
2359 hScreen = GetDeviceCaps (hdc, VERTRES);
2360 ReleaseDC(hwndChild, hdc);
2361
2362 xNew = rParent.left + ((wParent - wChild) /2);
2363 if (xNew < 0)
2364 {
2365 xNew = 0;
2366 }
2367 else if ((xNew+wChild) > wScreen)
2368 {
2369 xNew = wScreen - wChild;
2370 }
2371
2372 yNew = rParent.top + ((hParent - hChild) /2);
2373 if (yNew < 0)
2374 yNew = 0;
2375 else if ((yNew+hChild) > hScreen)
2376 yNew = hScreen - hChild;
2377
2378 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2379 SWP_NOSIZE | SWP_NOZORDER);
2380}
2381#endif /* FEAT_GUI_DIALOG */
2382
2383void
2384gui_mch_activate_window(void)
2385{
2386 (void)SetActiveWindow(s_hwnd);
2387}
2388
2389#if defined(FEAT_TOOLBAR) || defined(PROTO)
2390 void
2391gui_mch_show_toolbar(int showit)
2392{
2393 if (s_toolbarhwnd == NULL)
2394 return;
2395
2396 if (showit)
2397 {
2398# ifdef FEAT_MBYTE
2399# ifndef TB_SETUNICODEFORMAT
2400 /* For older compilers. We assume this never changes. */
2401# define TB_SETUNICODEFORMAT 0x2005
2402# endif
2403 /* Enable/disable unicode support */
2404 int uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2405 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2406# endif
2407 ShowWindow(s_toolbarhwnd, SW_SHOW);
2408 }
2409 else
2410 ShowWindow(s_toolbarhwnd, SW_HIDE);
2411}
2412
2413/* Then number of bitmaps is fixed. Exit is missing! */
2414#define TOOLBAR_BITMAP_COUNT 31
2415
2416#endif
2417
2418#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2419 static void
2420add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2421{
2422#ifdef FEAT_MBYTE
2423 WCHAR *wn = NULL;
2424 int n;
2425
2426 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2427 {
2428 /* 'encoding' differs from active codepage: convert menu name
2429 * and use wide function */
2430 wn = enc_to_utf16(item_text, NULL);
2431 if (wn != NULL)
2432 {
2433 MENUITEMINFOW infow;
2434
2435 infow.cbSize = sizeof(infow);
2436 infow.fMask = MIIM_TYPE | MIIM_ID;
2437 infow.wID = item_id;
2438 infow.fType = MFT_STRING;
2439 infow.dwTypeData = wn;
2440 infow.cch = (UINT)wcslen(wn);
2441 n = InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2442 vim_free(wn);
2443 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2444 /* Failed, try using non-wide function. */
2445 wn = NULL;
2446 }
2447 }
2448
2449 if (wn == NULL)
2450#endif
2451 {
2452 MENUITEMINFO info;
2453
2454 info.cbSize = sizeof(info);
2455 info.fMask = MIIM_TYPE | MIIM_ID;
2456 info.wID = item_id;
2457 info.fType = MFT_STRING;
2458 info.dwTypeData = (LPTSTR)item_text;
2459 info.cch = (UINT)STRLEN(item_text);
2460 InsertMenuItem(pmenu, item_id, FALSE, &info);
2461 }
2462}
2463
2464 static void
2465show_tabline_popup_menu(void)
2466{
2467 HMENU tab_pmenu;
2468 long rval;
2469 POINT pt;
2470
2471 /* When ignoring events don't show the menu. */
2472 if (hold_gui_events
2473# ifdef FEAT_CMDWIN
2474 || cmdwin_type != 0
2475# endif
2476 )
2477 return;
2478
2479 tab_pmenu = CreatePopupMenu();
2480 if (tab_pmenu == NULL)
2481 return;
2482
2483 if (first_tabpage->tp_next != NULL)
2484 add_tabline_popup_menu_entry(tab_pmenu,
2485 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2486 add_tabline_popup_menu_entry(tab_pmenu,
2487 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2488 add_tabline_popup_menu_entry(tab_pmenu,
2489 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2490
2491 GetCursorPos(&pt);
2492 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2493 NULL);
2494
2495 DestroyMenu(tab_pmenu);
2496
2497 /* Add the string cmd into input buffer */
2498 if (rval > 0)
2499 {
2500 TCHITTESTINFO htinfo;
2501 int idx;
2502
2503 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2504 return;
2505
2506 htinfo.pt.x = pt.x;
2507 htinfo.pt.y = pt.y;
2508 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2509 if (idx == -1)
2510 idx = 0;
2511 else
2512 idx += 1;
2513
2514 send_tabline_menu_event(idx, (int)rval);
2515 }
2516}
2517
2518/*
2519 * Show or hide the tabline.
2520 */
2521 void
2522gui_mch_show_tabline(int showit)
2523{
2524 if (s_tabhwnd == NULL)
2525 return;
2526
2527 if (!showit != !showing_tabline)
2528 {
2529 if (showit)
2530 ShowWindow(s_tabhwnd, SW_SHOW);
2531 else
2532 ShowWindow(s_tabhwnd, SW_HIDE);
2533 showing_tabline = showit;
2534 }
2535}
2536
2537/*
2538 * Return TRUE when tabline is displayed.
2539 */
2540 int
2541gui_mch_showing_tabline(void)
2542{
2543 return s_tabhwnd != NULL && showing_tabline;
2544}
2545
2546/*
2547 * Update the labels of the tabline.
2548 */
2549 void
2550gui_mch_update_tabline(void)
2551{
2552 tabpage_T *tp;
2553 TCITEM tie;
2554 int nr = 0;
2555 int curtabidx = 0;
2556 int tabadded = 0;
2557#ifdef FEAT_MBYTE
2558 static int use_unicode = FALSE;
2559 int uu;
2560 WCHAR *wstr = NULL;
2561#endif
2562
2563 if (s_tabhwnd == NULL)
2564 return;
2565
2566#if defined(FEAT_MBYTE)
2567# ifndef CCM_SETUNICODEFORMAT
2568 /* For older compilers. We assume this never changes. */
2569# define CCM_SETUNICODEFORMAT 0x2005
2570# endif
2571 uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2572 if (uu != use_unicode)
2573 {
2574 /* Enable/disable unicode support */
2575 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2576 use_unicode = uu;
2577 }
2578#endif
2579
2580 tie.mask = TCIF_TEXT;
2581 tie.iImage = -1;
2582
2583 /* Disable redraw for tab updates to eliminate O(N^2) draws. */
2584 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2585
2586 /* Add a label for each tab page. They all contain the same text area. */
2587 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2588 {
2589 if (tp == curtab)
2590 curtabidx = nr;
2591
2592 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2593 {
2594 /* Add the tab */
2595 tie.pszText = "-Empty-";
2596 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2597 tabadded = 1;
2598 }
2599
2600 get_tabline_label(tp, FALSE);
2601 tie.pszText = (LPSTR)NameBuff;
2602#ifdef FEAT_MBYTE
2603 wstr = NULL;
2604 if (use_unicode)
2605 {
2606 /* Need to go through Unicode. */
2607 wstr = enc_to_utf16(NameBuff, NULL);
2608 if (wstr != NULL)
2609 {
2610 TCITEMW tiw;
2611
2612 tiw.mask = TCIF_TEXT;
2613 tiw.iImage = -1;
2614 tiw.pszText = wstr;
2615 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2616 vim_free(wstr);
2617 }
2618 }
2619 if (wstr == NULL)
2620#endif
2621 {
2622 TabCtrl_SetItem(s_tabhwnd, nr, &tie);
2623 }
2624 }
2625
2626 /* Remove any old labels. */
2627 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2628 TabCtrl_DeleteItem(s_tabhwnd, nr);
2629
2630 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2631 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2632
2633 /* Re-enable redraw and redraw. */
2634 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2635 RedrawWindow(s_tabhwnd, NULL, NULL,
2636 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2637
2638 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2639 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2640}
2641
2642/*
2643 * Set the current tab to "nr". First tab is 1.
2644 */
2645 void
2646gui_mch_set_curtab(int nr)
2647{
2648 if (s_tabhwnd == NULL)
2649 return;
2650
2651 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2652 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2653}
2654
2655#endif
2656
2657/*
2658 * ":simalt" command.
2659 */
2660 void
2661ex_simalt(exarg_T *eap)
2662{
2663 char_u *keys = eap->arg;
2664
2665 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2666 while (*keys)
2667 {
2668 if (*keys == '~')
2669 *keys = ' '; /* for showing system menu */
2670 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2671 keys++;
2672 }
2673}
2674
2675/*
2676 * Create the find & replace dialogs.
2677 * You can't have both at once: ":find" when replace is showing, destroys
2678 * the replace dialog first, and the other way around.
2679 */
2680#ifdef MSWIN_FIND_REPLACE
2681 static void
2682initialise_findrep(char_u *initial_string)
2683{
2684 int wword = FALSE;
2685 int mcase = !p_ic;
2686 char_u *entry_text;
2687
2688 /* Get the search string to use. */
2689 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2690
2691 s_findrep_struct.hwndOwner = s_hwnd;
2692 s_findrep_struct.Flags = FR_DOWN;
2693 if (mcase)
2694 s_findrep_struct.Flags |= FR_MATCHCASE;
2695 if (wword)
2696 s_findrep_struct.Flags |= FR_WHOLEWORD;
2697 if (entry_text != NULL && *entry_text != NUL)
2698 vim_strncpy((char_u *)s_findrep_struct.lpstrFindWhat, entry_text,
2699 s_findrep_struct.wFindWhatLen - 1);
2700 vim_free(entry_text);
2701}
2702#endif
2703
2704 static void
2705set_window_title(HWND hwnd, char *title)
2706{
2707#ifdef FEAT_MBYTE
2708 if (title != NULL && enc_codepage >= 0 && enc_codepage != (int)GetACP())
2709 {
2710 WCHAR *wbuf;
2711 int n;
2712
2713 /* Convert the title from 'encoding' to UTF-16. */
2714 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2715 if (wbuf != NULL)
2716 {
2717 n = SetWindowTextW(hwnd, wbuf);
2718 vim_free(wbuf);
2719 if (n != 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
2720 return;
2721 /* Retry with non-wide function (for Windows 98). */
2722 }
2723 }
2724#endif
2725 (void)SetWindowText(hwnd, (LPCSTR)title);
2726}
2727
2728 void
2729gui_mch_find_dialog(exarg_T *eap)
2730{
2731#ifdef MSWIN_FIND_REPLACE
2732 if (s_findrep_msg != 0)
2733 {
2734 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2735 DestroyWindow(s_findrep_hwnd);
2736
2737 if (!IsWindow(s_findrep_hwnd))
2738 {
2739 initialise_findrep(eap->arg);
2740# if defined(FEAT_MBYTE) && defined(WIN3264)
2741 /* If the OS is Windows NT, and 'encoding' differs from active
2742 * codepage: convert text and use wide function. */
2743 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
2744 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2745 {
2746 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2747 s_findrep_hwnd = FindTextW(
2748 (LPFINDREPLACEW) &s_findrep_struct_w);
2749 }
2750 else
2751# endif
2752 s_findrep_hwnd = FindText((LPFINDREPLACE) &s_findrep_struct);
2753 }
2754
2755 set_window_title(s_findrep_hwnd,
2756 _("Find string (use '\\\\' to find a '\\')"));
2757 (void)SetFocus(s_findrep_hwnd);
2758
2759 s_findrep_is_find = TRUE;
2760 }
2761#endif
2762}
2763
2764
2765 void
2766gui_mch_replace_dialog(exarg_T *eap)
2767{
2768#ifdef MSWIN_FIND_REPLACE
2769 if (s_findrep_msg != 0)
2770 {
2771 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2772 DestroyWindow(s_findrep_hwnd);
2773
2774 if (!IsWindow(s_findrep_hwnd))
2775 {
2776 initialise_findrep(eap->arg);
2777# if defined(FEAT_MBYTE) && defined(WIN3264)
2778 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
2779 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2780 {
2781 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2782 s_findrep_hwnd = ReplaceTextW(
2783 (LPFINDREPLACEW) &s_findrep_struct_w);
2784 }
2785 else
2786# endif
2787 s_findrep_hwnd = ReplaceText(
2788 (LPFINDREPLACE) &s_findrep_struct);
2789 }
2790
2791 set_window_title(s_findrep_hwnd,
2792 _("Find & Replace (use '\\\\' to find a '\\')"));
2793 (void)SetFocus(s_findrep_hwnd);
2794
2795 s_findrep_is_find = FALSE;
2796 }
2797#endif
2798}
2799
2800
2801/*
2802 * Set visibility of the pointer.
2803 */
2804 void
2805gui_mch_mousehide(int hide)
2806{
2807 if (hide != gui.pointer_hidden)
2808 {
2809 ShowCursor(!hide);
2810 gui.pointer_hidden = hide;
2811 }
2812}
2813
2814#ifdef FEAT_MENU
2815 static void
2816gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2817{
2818 /* Unhide the mouse, we don't get move events here. */
2819 gui_mch_mousehide(FALSE);
2820
2821 (void)TrackPopupMenu(
2822 (HMENU)menu->submenu_id,
2823 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2824 x, y,
2825 (int)0, /*reserved param*/
2826 s_hwnd,
2827 NULL);
2828 /*
2829 * NOTE: The pop-up menu can eat the mouse up event.
2830 * We deal with this in normal.c.
2831 */
2832}
2833#endif
2834
2835/*
2836 * Got a message when the system will go down.
2837 */
2838 static void
2839_OnEndSession(void)
2840{
2841 getout_preserve_modified(1);
2842}
2843
2844/*
2845 * Get this message when the user clicks on the cross in the top right corner
2846 * of a Windows95 window.
2847 */
2848/*ARGSUSED*/
2849 static void
2850_OnClose(
2851 HWND hwnd)
2852{
2853 gui_shell_closed();
2854}
2855
2856/*
2857 * Get a message when the window is being destroyed.
2858 */
2859 static void
2860_OnDestroy(
2861 HWND hwnd)
2862{
2863 if (!destroying)
2864 _OnClose(hwnd);
2865}
2866
2867 static void
2868_OnPaint(
2869 HWND hwnd)
2870{
2871 if (!IsMinimized(hwnd))
2872 {
2873 PAINTSTRUCT ps;
2874
2875 out_flush(); /* make sure all output has been processed */
2876 (void)BeginPaint(hwnd, &ps);
2877#if defined(FEAT_DIRECTX)
2878 if (IS_ENABLE_DIRECTX())
2879 DWriteContext_BeginDraw(s_dwc);
2880#endif
2881
2882#ifdef FEAT_MBYTE
2883 /* prevent multi-byte characters from misprinting on an invalid
2884 * rectangle */
2885 if (has_mbyte)
2886 {
2887 RECT rect;
2888
2889 GetClientRect(hwnd, &rect);
2890 ps.rcPaint.left = rect.left;
2891 ps.rcPaint.right = rect.right;
2892 }
2893#endif
2894
2895 if (!IsRectEmpty(&ps.rcPaint))
2896 {
2897#if defined(FEAT_DIRECTX)
2898 if (IS_ENABLE_DIRECTX())
2899 DWriteContext_BindDC(s_dwc, s_hdc, &ps.rcPaint);
2900#endif
2901 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2902 ps.rcPaint.right - ps.rcPaint.left + 1,
2903 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2904 }
2905
2906#if defined(FEAT_DIRECTX)
2907 if (IS_ENABLE_DIRECTX())
2908 DWriteContext_EndDraw(s_dwc);
2909#endif
2910 EndPaint(hwnd, &ps);
2911 }
2912}
2913
2914/*ARGSUSED*/
2915 static void
2916_OnSize(
2917 HWND hwnd,
2918 UINT state,
2919 int cx,
2920 int cy)
2921{
2922 if (!IsMinimized(hwnd))
2923 {
2924 gui_resize_shell(cx, cy);
2925
2926#ifdef FEAT_MENU
2927 /* Menu bar may wrap differently now */
2928 gui_mswin_get_menu_height(TRUE);
2929#endif
2930 }
2931}
2932
2933 static void
2934_OnSetFocus(
2935 HWND hwnd,
2936 HWND hwndOldFocus)
2937{
2938 gui_focus_change(TRUE);
2939 s_getting_focus = TRUE;
2940 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2941}
2942
2943 static void
2944_OnKillFocus(
2945 HWND hwnd,
2946 HWND hwndNewFocus)
2947{
2948 gui_focus_change(FALSE);
2949 s_getting_focus = FALSE;
2950 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2951}
2952
2953/*
2954 * Get a message when the user switches back to vim
2955 */
2956 static LRESULT
2957_OnActivateApp(
2958 HWND hwnd,
2959 BOOL fActivate,
2960 DWORD dwThreadId)
2961{
2962 /* we call gui_focus_change() in _OnSetFocus() */
2963 /* gui_focus_change((int)fActivate); */
2964 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2965}
2966
2967#if defined(FEAT_WINDOWS) || defined(PROTO)
2968 void
2969gui_mch_destroy_scrollbar(scrollbar_T *sb)
2970{
2971 DestroyWindow(sb->id);
2972}
2973#endif
2974
2975/*
2976 * Get current mouse coordinates in text window.
2977 */
2978 void
2979gui_mch_getmouse(int *x, int *y)
2980{
2981 RECT rct;
2982 POINT mp;
2983
2984 (void)GetWindowRect(s_textArea, &rct);
2985 (void)GetCursorPos((LPPOINT)&mp);
2986 *x = (int)(mp.x - rct.left);
2987 *y = (int)(mp.y - rct.top);
2988}
2989
2990/*
2991 * Move mouse pointer to character at (x, y).
2992 */
2993 void
2994gui_mch_setmouse(int x, int y)
2995{
2996 RECT rct;
2997
2998 (void)GetWindowRect(s_textArea, &rct);
2999 (void)SetCursorPos(x + gui.border_offset + rct.left,
3000 y + gui.border_offset + rct.top);
3001}
3002
3003 static void
3004gui_mswin_get_valid_dimensions(
3005 int w,
3006 int h,
3007 int *valid_w,
3008 int *valid_h)
3009{
3010 int base_width, base_height;
3011
3012 base_width = gui_get_base_width()
3013 + (GetSystemMetrics(SM_CXFRAME) +
3014 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
3015 base_height = gui_get_base_height()
3016 + (GetSystemMetrics(SM_CYFRAME) +
3017 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3018 + GetSystemMetrics(SM_CYCAPTION)
3019#ifdef FEAT_MENU
3020 + gui_mswin_get_menu_height(FALSE)
3021#endif
3022 ;
3023 *valid_w = base_width +
3024 ((w - base_width) / gui.char_width) * gui.char_width;
3025 *valid_h = base_height +
3026 ((h - base_height) / gui.char_height) * gui.char_height;
3027}
3028
3029 void
3030gui_mch_flash(int msec)
3031{
3032 RECT rc;
3033
3034 /*
3035 * Note: InvertRect() excludes right and bottom of rectangle.
3036 */
3037 rc.left = 0;
3038 rc.top = 0;
3039 rc.right = gui.num_cols * gui.char_width;
3040 rc.bottom = gui.num_rows * gui.char_height;
3041 InvertRect(s_hdc, &rc);
3042 gui_mch_flush(); /* make sure it's displayed */
3043
3044 ui_delay((long)msec, TRUE); /* wait for a few msec */
3045
3046 InvertRect(s_hdc, &rc);
3047}
3048
3049/*
3050 * Return flags used for scrolling.
3051 * The SW_INVALIDATE is required when part of the window is covered or
3052 * off-screen. Refer to MS KB Q75236.
3053 */
3054 static int
3055get_scroll_flags(void)
3056{
3057 HWND hwnd;
3058 RECT rcVim, rcOther, rcDest;
3059
3060 GetWindowRect(s_hwnd, &rcVim);
3061
3062 /* Check if the window is partly above or below the screen. We don't care
3063 * about partly left or right of the screen, it is not relevant when
3064 * scrolling up or down. */
3065 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
3066 return SW_INVALIDATE;
3067
3068 /* Check if there is an window (partly) on top of us. */
3069 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3070 if (IsWindowVisible(hwnd))
3071 {
3072 GetWindowRect(hwnd, &rcOther);
3073 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3074 return SW_INVALIDATE;
3075 }
3076 return 0;
3077}
3078
3079/*
3080 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3081 * may not be scrolled out properly.
3082 * For gVim, when _OnScroll() is repeated, the character at the
3083 * previous cursor position may be left drawn after scroll.
3084 * The problem can be avoided by calling GetPixel() to get a pixel in
3085 * the region before ScrollWindowEx().
3086 */
3087 static void
3088intel_gpu_workaround(void)
3089{
3090 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3091}
3092
3093/*
3094 * Delete the given number of lines from the given row, scrolling up any
3095 * text further down within the scroll region.
3096 */
3097 void
3098gui_mch_delete_lines(
3099 int row,
3100 int num_lines)
3101{
3102 RECT rc;
3103
3104 intel_gpu_workaround();
3105
3106 rc.left = FILL_X(gui.scroll_region_left);
3107 rc.right = FILL_X(gui.scroll_region_right + 1);
3108 rc.top = FILL_Y(row);
3109 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3110
3111 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
3112 &rc, &rc, NULL, NULL, get_scroll_flags());
3113
3114 UpdateWindow(s_textArea);
3115 /* This seems to be required to avoid the cursor disappearing when
3116 * scrolling such that the cursor ends up in the top-left character on
3117 * the screen... But why? (Webb) */
3118 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3119 /* gui.cursor_is_valid = FALSE; */
3120
3121 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3122 gui.scroll_region_left,
3123 gui.scroll_region_bot, gui.scroll_region_right);
3124}
3125
3126/*
3127 * Insert the given number of lines before the given row, scrolling down any
3128 * following text within the scroll region.
3129 */
3130 void
3131gui_mch_insert_lines(
3132 int row,
3133 int num_lines)
3134{
3135 RECT rc;
3136
3137 intel_gpu_workaround();
3138
3139 rc.left = FILL_X(gui.scroll_region_left);
3140 rc.right = FILL_X(gui.scroll_region_right + 1);
3141 rc.top = FILL_Y(row);
3142 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3143 /* The SW_INVALIDATE is required when part of the window is covered or
3144 * off-screen. How do we avoid it when it's not needed? */
3145 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
3146 &rc, &rc, NULL, NULL, get_scroll_flags());
3147
3148 UpdateWindow(s_textArea);
3149
3150 gui_clear_block(row, gui.scroll_region_left,
3151 row + num_lines - 1, gui.scroll_region_right);
3152}
3153
3154
3155/*ARGSUSED*/
3156 void
3157gui_mch_exit(int rc)
3158{
3159#if defined(FEAT_DIRECTX)
3160 DWriteContext_Close(s_dwc);
3161 DWrite_Final();
3162 s_dwc = NULL;
3163#endif
3164
3165 ReleaseDC(s_textArea, s_hdc);
3166 DeleteObject(s_brush);
3167
3168#ifdef FEAT_TEAROFF
3169 /* Unload the tearoff bitmap */
3170 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3171#endif
3172
3173 /* Destroy our window (if we have one). */
3174 if (s_hwnd != NULL)
3175 {
3176 destroying = TRUE; /* ignore WM_DESTROY message now */
3177 DestroyWindow(s_hwnd);
3178 }
3179
3180#ifdef GLOBAL_IME
3181 global_ime_end();
3182#endif
3183}
3184
3185 static char_u *
3186logfont2name(LOGFONT lf)
3187{
3188 char *p;
3189 char *res;
3190 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003191 char *quality_name;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003192 char *font_name = lf.lfFaceName;
3193
3194 charset_name = charset_id2name((int)lf.lfCharSet);
3195#ifdef FEAT_MBYTE
3196 /* Convert a font name from the current codepage to 'encoding'.
3197 * TODO: Use Wide APIs (including LOGFONTW) instead of ANSI APIs. */
3198 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
3199 {
3200 int len;
3201 acp_to_enc((char_u *)lf.lfFaceName, (int)strlen(lf.lfFaceName),
3202 (char_u **)&font_name, &len);
3203 }
3204#endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003205 quality_name = quality_id2name((int)lf.lfQuality);
3206
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003207 res = (char *)alloc((unsigned)(strlen(font_name) + 20
3208 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
3209 if (res != NULL)
3210 {
3211 p = res;
3212 /* make a normal font string out of the lf thing:*/
3213 sprintf((char *)p, "%s:h%d", font_name, pixels_to_points(
3214 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
3215 while (*p)
3216 {
3217 if (*p == ' ')
3218 *p = '_';
3219 ++p;
3220 }
3221 if (lf.lfItalic)
3222 STRCAT(p, ":i");
3223 if (lf.lfWeight >= FW_BOLD)
3224 STRCAT(p, ":b");
3225 if (lf.lfUnderline)
3226 STRCAT(p, ":u");
3227 if (lf.lfStrikeOut)
3228 STRCAT(p, ":s");
3229 if (charset_name != NULL)
3230 {
3231 STRCAT(p, ":c");
3232 STRCAT(p, charset_name);
3233 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003234 if (quality_name != NULL)
3235 {
3236 STRCAT(p, ":q");
3237 STRCAT(p, quality_name);
3238 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003239 }
3240
3241#ifdef FEAT_MBYTE
3242 if (font_name != lf.lfFaceName)
3243 vim_free(font_name);
3244#endif
3245 return (char_u *)res;
3246}
3247
3248
3249#ifdef FEAT_MBYTE_IME
3250/*
3251 * Set correct LOGFONT to IME. Use 'guifontwide' if available, otherwise use
3252 * 'guifont'
3253 */
3254 static void
3255update_im_font(void)
3256{
3257 LOGFONT lf_wide;
3258
3259 if (p_guifontwide != NULL && *p_guifontwide != NUL
3260 && gui.wide_font != NOFONT
3261 && GetObject((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
3262 norm_logfont = lf_wide;
3263 else
3264 norm_logfont = sub_logfont;
3265 im_set_font(&norm_logfont);
3266}
3267#endif
3268
3269#ifdef FEAT_MBYTE
3270/*
3271 * Handler of gui.wide_font (p_guifontwide) changed notification.
3272 */
3273 void
3274gui_mch_wide_font_changed(void)
3275{
3276 LOGFONT lf;
3277
3278# ifdef FEAT_MBYTE_IME
3279 update_im_font();
3280# endif
3281
3282 gui_mch_free_font(gui.wide_ital_font);
3283 gui.wide_ital_font = NOFONT;
3284 gui_mch_free_font(gui.wide_bold_font);
3285 gui.wide_bold_font = NOFONT;
3286 gui_mch_free_font(gui.wide_boldital_font);
3287 gui.wide_boldital_font = NOFONT;
3288
3289 if (gui.wide_font
3290 && GetObject((HFONT)gui.wide_font, sizeof(lf), &lf))
3291 {
3292 if (!lf.lfItalic)
3293 {
3294 lf.lfItalic = TRUE;
3295 gui.wide_ital_font = get_font_handle(&lf);
3296 lf.lfItalic = FALSE;
3297 }
3298 if (lf.lfWeight < FW_BOLD)
3299 {
3300 lf.lfWeight = FW_BOLD;
3301 gui.wide_bold_font = get_font_handle(&lf);
3302 if (!lf.lfItalic)
3303 {
3304 lf.lfItalic = TRUE;
3305 gui.wide_boldital_font = get_font_handle(&lf);
3306 }
3307 }
3308 }
3309}
3310#endif
3311
3312/*
3313 * Initialise vim to use the font with the given name.
3314 * Return FAIL if the font could not be loaded, OK otherwise.
3315 */
3316/*ARGSUSED*/
3317 int
3318gui_mch_init_font(char_u *font_name, int fontset)
3319{
3320 LOGFONT lf;
3321 GuiFont font = NOFONT;
3322 char_u *p;
3323
3324 /* Load the font */
3325 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3326 font = get_font_handle(&lf);
3327 if (font == NOFONT)
3328 return FAIL;
3329
3330 if (font_name == NULL)
3331 font_name = (char_u *)lf.lfFaceName;
3332#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3333 norm_logfont = lf;
3334 sub_logfont = lf;
3335#endif
3336#ifdef FEAT_MBYTE_IME
3337 update_im_font();
3338#endif
3339 gui_mch_free_font(gui.norm_font);
3340 gui.norm_font = font;
3341 current_font_height = lf.lfHeight;
3342 GetFontSize(font);
3343
3344 p = logfont2name(lf);
3345 if (p != NULL)
3346 {
3347 hl_set_font_name(p);
3348
3349 /* When setting 'guifont' to "*" replace it with the actual font name.
3350 * */
3351 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3352 {
3353 vim_free(p_guifont);
3354 p_guifont = p;
3355 }
3356 else
3357 vim_free(p);
3358 }
3359
3360 gui_mch_free_font(gui.ital_font);
3361 gui.ital_font = NOFONT;
3362 gui_mch_free_font(gui.bold_font);
3363 gui.bold_font = NOFONT;
3364 gui_mch_free_font(gui.boldital_font);
3365 gui.boldital_font = NOFONT;
3366
3367 if (!lf.lfItalic)
3368 {
3369 lf.lfItalic = TRUE;
3370 gui.ital_font = get_font_handle(&lf);
3371 lf.lfItalic = FALSE;
3372 }
3373 if (lf.lfWeight < FW_BOLD)
3374 {
3375 lf.lfWeight = FW_BOLD;
3376 gui.bold_font = get_font_handle(&lf);
3377 if (!lf.lfItalic)
3378 {
3379 lf.lfItalic = TRUE;
3380 gui.boldital_font = get_font_handle(&lf);
3381 }
3382 }
3383
3384 return OK;
3385}
3386
3387#ifndef WPF_RESTORETOMAXIMIZED
3388# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3389#endif
3390
3391/*
3392 * Return TRUE if the GUI window is maximized, filling the whole screen.
3393 */
3394 int
3395gui_mch_maximized(void)
3396{
3397 WINDOWPLACEMENT wp;
3398
3399 wp.length = sizeof(WINDOWPLACEMENT);
3400 if (GetWindowPlacement(s_hwnd, &wp))
3401 return wp.showCmd == SW_SHOWMAXIMIZED
3402 || (wp.showCmd == SW_SHOWMINIMIZED
3403 && wp.flags == WPF_RESTORETOMAXIMIZED);
3404
3405 return 0;
3406}
3407
3408/*
3409 * Called when the font changed while the window is maximized. Compute the
3410 * new Rows and Columns. This is like resizing the window.
3411 */
3412 void
3413gui_mch_newfont(void)
3414{
3415 RECT rect;
3416
3417 GetWindowRect(s_hwnd, &rect);
3418 if (win_socket_id == 0)
3419 {
3420 gui_resize_shell(rect.right - rect.left
3421 - (GetSystemMetrics(SM_CXFRAME) +
3422 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3423 rect.bottom - rect.top
3424 - (GetSystemMetrics(SM_CYFRAME) +
3425 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3426 - GetSystemMetrics(SM_CYCAPTION)
3427#ifdef FEAT_MENU
3428 - gui_mswin_get_menu_height(FALSE)
3429#endif
3430 );
3431 }
3432 else
3433 {
3434 /* Inside another window, don't use the frame and border. */
3435 gui_resize_shell(rect.right - rect.left,
3436 rect.bottom - rect.top
3437#ifdef FEAT_MENU
3438 - gui_mswin_get_menu_height(FALSE)
3439#endif
3440 );
3441 }
3442}
3443
3444/*
3445 * Set the window title
3446 */
3447/*ARGSUSED*/
3448 void
3449gui_mch_settitle(
3450 char_u *title,
3451 char_u *icon)
3452{
3453 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3454}
3455
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003456#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003457/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3458 * misc2.c! */
3459static LPCSTR mshape_idcs[] =
3460{
3461 IDC_ARROW, /* arrow */
3462 MAKEINTRESOURCE(0), /* blank */
3463 IDC_IBEAM, /* beam */
3464 IDC_SIZENS, /* updown */
3465 IDC_SIZENS, /* udsizing */
3466 IDC_SIZEWE, /* leftright */
3467 IDC_SIZEWE, /* lrsizing */
3468 IDC_WAIT, /* busy */
3469#ifdef WIN3264
3470 IDC_NO, /* no */
3471#else
3472 IDC_ICON, /* no */
3473#endif
3474 IDC_ARROW, /* crosshair */
3475 IDC_ARROW, /* hand1 */
3476 IDC_ARROW, /* hand2 */
3477 IDC_ARROW, /* pencil */
3478 IDC_ARROW, /* question */
3479 IDC_ARROW, /* right-arrow */
3480 IDC_UPARROW, /* up-arrow */
3481 IDC_ARROW /* last one */
3482};
3483
3484 void
3485mch_set_mouse_shape(int shape)
3486{
3487 LPCSTR idc;
3488
3489 if (shape == MSHAPE_HIDE)
3490 ShowCursor(FALSE);
3491 else
3492 {
3493 if (shape >= MSHAPE_NUMBERED)
3494 idc = IDC_ARROW;
3495 else
3496 idc = mshape_idcs[shape];
3497#ifdef SetClassLongPtr
3498 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3499#else
3500# ifdef WIN32
3501 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
3502# else /* Win16 */
3503 SetClassWord(s_textArea, GCW_HCURSOR, (WORD)LoadCursor(NULL, idc));
3504# endif
3505#endif
3506 if (!p_mh)
3507 {
3508 POINT mp;
3509
3510 /* Set the position to make it redrawn with the new shape. */
3511 (void)GetCursorPos((LPPOINT)&mp);
3512 (void)SetCursorPos(mp.x, mp.y);
3513 ShowCursor(TRUE);
3514 }
3515 }
3516}
3517#endif
3518
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003519#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003520/*
3521 * The file browser exists in two versions: with "W" uses wide characters,
3522 * without "W" the current codepage. When FEAT_MBYTE is defined and on
3523 * Windows NT/2000/XP the "W" functions are used.
3524 */
3525
3526# if defined(FEAT_MBYTE) && defined(WIN3264)
3527/*
3528 * Wide version of convert_filter().
3529 */
3530 static WCHAR *
3531convert_filterW(char_u *s)
3532{
3533 char_u *tmp;
3534 int len;
3535 WCHAR *res;
3536
3537 tmp = convert_filter(s);
3538 if (tmp == NULL)
3539 return NULL;
3540 len = (int)STRLEN(s) + 3;
3541 res = enc_to_utf16(tmp, &len);
3542 vim_free(tmp);
3543 return res;
3544}
3545
3546/*
3547 * Wide version of gui_mch_browse(). Keep in sync!
3548 */
3549 static char_u *
3550gui_mch_browseW(
3551 int saving,
3552 char_u *title,
3553 char_u *dflt,
3554 char_u *ext,
3555 char_u *initdir,
3556 char_u *filter)
3557{
3558 /* We always use the wide function. This means enc_to_utf16() must work,
3559 * otherwise it fails miserably! */
3560 OPENFILENAMEW fileStruct;
3561 WCHAR fileBuf[MAXPATHL];
3562 WCHAR *wp;
3563 int i;
3564 WCHAR *titlep = NULL;
3565 WCHAR *extp = NULL;
3566 WCHAR *initdirp = NULL;
3567 WCHAR *filterp;
3568 char_u *p;
3569
3570 if (dflt == NULL)
3571 fileBuf[0] = NUL;
3572 else
3573 {
3574 wp = enc_to_utf16(dflt, NULL);
3575 if (wp == NULL)
3576 fileBuf[0] = NUL;
3577 else
3578 {
3579 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3580 fileBuf[i] = wp[i];
3581 fileBuf[i] = NUL;
3582 vim_free(wp);
3583 }
3584 }
3585
3586 /* Convert the filter to Windows format. */
3587 filterp = convert_filterW(filter);
3588
3589 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003590#ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003591 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003592 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003593#else
3594 fileStruct.lStructSize = sizeof(fileStruct);
3595#endif
3596
3597 if (title != NULL)
3598 titlep = enc_to_utf16(title, NULL);
3599 fileStruct.lpstrTitle = titlep;
3600
3601 if (ext != NULL)
3602 extp = enc_to_utf16(ext, NULL);
3603 fileStruct.lpstrDefExt = extp;
3604
3605 fileStruct.lpstrFile = fileBuf;
3606 fileStruct.nMaxFile = MAXPATHL;
3607 fileStruct.lpstrFilter = filterp;
3608 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3609 /* has an initial dir been specified? */
3610 if (initdir != NULL && *initdir != NUL)
3611 {
3612 /* Must have backslashes here, no matter what 'shellslash' says */
3613 initdirp = enc_to_utf16(initdir, NULL);
3614 if (initdirp != NULL)
3615 {
3616 for (wp = initdirp; *wp != NUL; ++wp)
3617 if (*wp == '/')
3618 *wp = '\\';
3619 }
3620 fileStruct.lpstrInitialDir = initdirp;
3621 }
3622
3623 /*
3624 * TODO: Allow selection of multiple files. Needs another arg to this
3625 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3626 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3627 * files that don't exist yet, so I haven't put it in. What about
3628 * OFN_PATHMUSTEXIST?
3629 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3630 */
3631 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
3632#ifdef FEAT_SHORTCUT
3633 if (curbuf->b_p_bin)
3634 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
3635#endif
3636 if (saving)
3637 {
3638 if (!GetSaveFileNameW(&fileStruct))
3639 return NULL;
3640 }
3641 else
3642 {
3643 if (!GetOpenFileNameW(&fileStruct))
3644 return NULL;
3645 }
3646
3647 vim_free(filterp);
3648 vim_free(initdirp);
3649 vim_free(titlep);
3650 vim_free(extp);
3651
3652 /* Convert from UCS2 to 'encoding'. */
3653 p = utf16_to_enc(fileBuf, NULL);
3654 if (p != NULL)
3655 /* when out of memory we get garbage for non-ASCII chars */
3656 STRCPY(fileBuf, p);
3657 vim_free(p);
3658
3659 /* Give focus back to main window (when using MDI). */
3660 SetFocus(s_hwnd);
3661
3662 /* Shorten the file name if possible */
3663 return vim_strsave(shorten_fname1((char_u *)fileBuf));
3664}
3665# endif /* FEAT_MBYTE */
3666
3667
3668/*
3669 * Convert the string s to the proper format for a filter string by replacing
3670 * the \t and \n delimiters with \0.
3671 * Returns the converted string in allocated memory.
3672 *
3673 * Keep in sync with convert_filterW() above!
3674 */
3675 static char_u *
3676convert_filter(char_u *s)
3677{
3678 char_u *res;
3679 unsigned s_len = (unsigned)STRLEN(s);
3680 unsigned i;
3681
3682 res = alloc(s_len + 3);
3683 if (res != NULL)
3684 {
3685 for (i = 0; i < s_len; ++i)
3686 if (s[i] == '\t' || s[i] == '\n')
3687 res[i] = '\0';
3688 else
3689 res[i] = s[i];
3690 res[s_len] = NUL;
3691 /* Add two extra NULs to make sure it's properly terminated. */
3692 res[s_len + 1] = NUL;
3693 res[s_len + 2] = NUL;
3694 }
3695 return res;
3696}
3697
3698/*
3699 * Select a directory.
3700 */
3701 char_u *
3702gui_mch_browsedir(char_u *title, char_u *initdir)
3703{
3704 /* We fake this: Use a filter that doesn't select anything and a default
3705 * file name that won't be used. */
3706 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3707 initdir, (char_u *)_("Directory\t*.nothing\n"));
3708}
3709
3710/*
3711 * Pop open a file browser and return the file selected, in allocated memory,
3712 * or NULL if Cancel is hit.
3713 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3714 * title - Title message for the file browser dialog.
3715 * dflt - Default name of file.
3716 * ext - Default extension to be added to files without extensions.
3717 * initdir - directory in which to open the browser (NULL = current dir)
3718 * filter - Filter for matched files to choose from.
3719 *
3720 * Keep in sync with gui_mch_browseW() above!
3721 */
3722 char_u *
3723gui_mch_browse(
3724 int saving,
3725 char_u *title,
3726 char_u *dflt,
3727 char_u *ext,
3728 char_u *initdir,
3729 char_u *filter)
3730{
3731 OPENFILENAME fileStruct;
3732 char_u fileBuf[MAXPATHL];
3733 char_u *initdirp = NULL;
3734 char_u *filterp;
3735 char_u *p;
3736
3737# if defined(FEAT_MBYTE) && defined(WIN3264)
3738 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
3739 return gui_mch_browseW(saving, title, dflt, ext, initdir, filter);
3740# endif
3741
3742 if (dflt == NULL)
3743 fileBuf[0] = NUL;
3744 else
3745 vim_strncpy(fileBuf, dflt, MAXPATHL - 1);
3746
3747 /* Convert the filter to Windows format. */
3748 filterp = convert_filter(filter);
3749
3750 vim_memset(&fileStruct, 0, sizeof(OPENFILENAME));
3751#ifdef OPENFILENAME_SIZE_VERSION_400
3752 /* be compatible with Windows NT 4.0 */
3753 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400;
3754#else
3755 fileStruct.lStructSize = sizeof(fileStruct);
3756#endif
3757
3758 fileStruct.lpstrTitle = (LPSTR)title;
3759 fileStruct.lpstrDefExt = (LPSTR)ext;
3760
3761 fileStruct.lpstrFile = (LPSTR)fileBuf;
3762 fileStruct.nMaxFile = MAXPATHL;
3763 fileStruct.lpstrFilter = (LPSTR)filterp;
3764 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3765 /* has an initial dir been specified? */
3766 if (initdir != NULL && *initdir != NUL)
3767 {
3768 /* Must have backslashes here, no matter what 'shellslash' says */
3769 initdirp = vim_strsave(initdir);
3770 if (initdirp != NULL)
3771 for (p = initdirp; *p != NUL; ++p)
3772 if (*p == '/')
3773 *p = '\\';
3774 fileStruct.lpstrInitialDir = (LPSTR)initdirp;
3775 }
3776
3777 /*
3778 * TODO: Allow selection of multiple files. Needs another arg to this
3779 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3780 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3781 * files that don't exist yet, so I haven't put it in. What about
3782 * OFN_PATHMUSTEXIST?
3783 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3784 */
3785 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
3786#ifdef FEAT_SHORTCUT
3787 if (curbuf->b_p_bin)
3788 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
3789#endif
3790 if (saving)
3791 {
3792 if (!GetSaveFileName(&fileStruct))
3793 return NULL;
3794 }
3795 else
3796 {
3797 if (!GetOpenFileName(&fileStruct))
3798 return NULL;
3799 }
3800
3801 vim_free(filterp);
3802 vim_free(initdirp);
3803
3804 /* Give focus back to main window (when using MDI). */
3805 SetFocus(s_hwnd);
3806
3807 /* Shorten the file name if possible */
3808 return vim_strsave(shorten_fname1((char_u *)fileBuf));
3809}
3810#endif /* FEAT_BROWSE */
3811
3812/*ARGSUSED*/
3813 static void
3814_OnDropFiles(
3815 HWND hwnd,
3816 HDROP hDrop)
3817{
3818#ifdef FEAT_WINDOWS
3819#ifdef WIN3264
3820# define BUFPATHLEN _MAX_PATH
3821# define DRAGQVAL 0xFFFFFFFF
3822#else
3823# define BUFPATHLEN MAXPATHL
3824# define DRAGQVAL 0xFFFF
3825#endif
3826#ifdef FEAT_MBYTE
3827 WCHAR wszFile[BUFPATHLEN];
3828#endif
3829 char szFile[BUFPATHLEN];
3830 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3831 UINT i;
3832 char_u **fnames;
3833 POINT pt;
3834 int_u modifiers = 0;
3835
3836 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3837
3838 /* Obtain dropped position */
3839 DragQueryPoint(hDrop, &pt);
3840 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3841
3842 reset_VIsual();
3843
3844 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3845
3846 if (fnames != NULL)
3847 for (i = 0; i < cFiles; ++i)
3848 {
3849#ifdef FEAT_MBYTE
3850 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3851 fnames[i] = utf16_to_enc(wszFile, NULL);
3852 else
3853#endif
3854 {
3855 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3856 fnames[i] = vim_strsave((char_u *)szFile);
3857 }
3858 }
3859
3860 DragFinish(hDrop);
3861
3862 if (fnames != NULL)
3863 {
3864 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3865 modifiers |= MOUSE_SHIFT;
3866 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3867 modifiers |= MOUSE_CTRL;
3868 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3869 modifiers |= MOUSE_ALT;
3870
3871 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3872
3873 s_need_activate = TRUE;
3874 }
3875#endif
3876}
3877
3878/*ARGSUSED*/
3879 static int
3880_OnScroll(
3881 HWND hwnd,
3882 HWND hwndCtl,
3883 UINT code,
3884 int pos)
3885{
3886 static UINT prev_code = 0; /* code of previous call */
3887 scrollbar_T *sb, *sb_info;
3888 long val;
3889 int dragging = FALSE;
3890 int dont_scroll_save = dont_scroll;
3891#ifndef WIN3264
3892 int nPos;
3893#else
3894 SCROLLINFO si;
3895
3896 si.cbSize = sizeof(si);
3897 si.fMask = SIF_POS;
3898#endif
3899
3900 sb = gui_mswin_find_scrollbar(hwndCtl);
3901 if (sb == NULL)
3902 return 0;
3903
3904 if (sb->wp != NULL) /* Left or right scrollbar */
3905 {
3906 /*
3907 * Careful: need to get scrollbar info out of first (left) scrollbar
3908 * for window, but keep real scrollbar too because we must pass it to
3909 * gui_drag_scrollbar().
3910 */
3911 sb_info = &sb->wp->w_scrollbars[0];
3912 }
3913 else /* Bottom scrollbar */
3914 sb_info = sb;
3915 val = sb_info->value;
3916
3917 switch (code)
3918 {
3919 case SB_THUMBTRACK:
3920 val = pos;
3921 dragging = TRUE;
3922 if (sb->scroll_shift > 0)
3923 val <<= sb->scroll_shift;
3924 break;
3925 case SB_LINEDOWN:
3926 val++;
3927 break;
3928 case SB_LINEUP:
3929 val--;
3930 break;
3931 case SB_PAGEDOWN:
3932 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3933 break;
3934 case SB_PAGEUP:
3935 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3936 break;
3937 case SB_TOP:
3938 val = 0;
3939 break;
3940 case SB_BOTTOM:
3941 val = sb_info->max;
3942 break;
3943 case SB_ENDSCROLL:
3944 if (prev_code == SB_THUMBTRACK)
3945 {
3946 /*
3947 * "pos" only gives us 16-bit data. In case of large file,
3948 * use GetScrollPos() which returns 32-bit. Unfortunately it
3949 * is not valid while the scrollbar is being dragged.
3950 */
3951 val = GetScrollPos(hwndCtl, SB_CTL);
3952 if (sb->scroll_shift > 0)
3953 val <<= sb->scroll_shift;
3954 }
3955 break;
3956
3957 default:
3958 /* TRACE("Unknown scrollbar event %d\n", code); */
3959 return 0;
3960 }
3961 prev_code = code;
3962
3963#ifdef WIN3264
3964 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3965 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
3966#else
3967 nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3968 SetScrollPos(hwndCtl, SB_CTL, nPos, TRUE);
3969#endif
3970
3971 /*
3972 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3973 */
3974 if (sb->wp != NULL)
3975 {
3976 scrollbar_T *sba = sb->wp->w_scrollbars;
3977 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3978
3979#ifdef WIN3264
3980 SetScrollInfo(id, SB_CTL, &si, TRUE);
3981#else
3982 SetScrollPos(id, SB_CTL, nPos, TRUE);
3983#endif
3984 }
3985
3986 /* Don't let us be interrupted here by another message. */
3987 s_busy_processing = TRUE;
3988
3989 /* When "allow_scrollbar" is FALSE still need to remember the new
3990 * position, but don't actually scroll by setting "dont_scroll". */
3991 dont_scroll = !allow_scrollbar;
3992
3993 gui_drag_scrollbar(sb, val, dragging);
3994
3995 s_busy_processing = FALSE;
3996 dont_scroll = dont_scroll_save;
3997
3998 return 0;
3999}
4000
4001
4002/*
4003 * Get command line arguments.
4004 * Use "prog" as the name of the program and "cmdline" as the arguments.
4005 * Copy the arguments to allocated memory.
4006 * Return the number of arguments (including program name).
4007 * Return pointers to the arguments in "argvp". Memory is allocated with
4008 * malloc(), use free() instead of vim_free().
4009 * Return pointer to buffer in "tofree".
4010 * Returns zero when out of memory.
4011 */
4012/*ARGSUSED*/
4013 int
4014get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
4015{
4016 int i;
4017 char *p;
4018 char *progp;
4019 char *pnew = NULL;
4020 char *newcmdline;
4021 int inquote;
4022 int argc;
4023 char **argv = NULL;
4024 int round;
4025
4026 *tofree = NULL;
4027
4028#ifdef FEAT_MBYTE
4029 /* Try using the Unicode version first, it takes care of conversion when
4030 * 'encoding' is changed. */
4031 argc = get_cmd_argsW(&argv);
4032 if (argc != 0)
4033 goto done;
4034#endif
4035
4036 /* Handle the program name. Remove the ".exe" extension, and find the 1st
4037 * non-space. */
4038 p = strrchr(prog, '.');
4039 if (p != NULL)
4040 *p = NUL;
4041 for (progp = prog; *progp == ' '; ++progp)
4042 ;
4043
4044 /* The command line is copied to allocated memory, so that we can change
4045 * it. Add the size of the string, the separating NUL and a terminating
4046 * NUL. */
4047 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
4048 if (newcmdline == NULL)
4049 return 0;
4050
4051 /*
4052 * First round: count the number of arguments ("pnew" == NULL).
4053 * Second round: produce the arguments.
4054 */
4055 for (round = 1; round <= 2; ++round)
4056 {
4057 /* First argument is the program name. */
4058 if (pnew != NULL)
4059 {
4060 argv[0] = pnew;
4061 strcpy(pnew, progp);
4062 pnew += strlen(pnew);
4063 *pnew++ = NUL;
4064 }
4065
4066 /*
4067 * Isolate each argument and put it in argv[].
4068 */
4069 p = cmdline;
4070 argc = 1;
4071 while (*p != NUL)
4072 {
4073 inquote = FALSE;
4074 if (pnew != NULL)
4075 argv[argc] = pnew;
4076 ++argc;
4077 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
4078 {
4079 /* Backslashes are only special when followed by a double
4080 * quote. */
4081 i = (int)strspn(p, "\\");
4082 if (p[i] == '"')
4083 {
4084 /* Halve the number of backslashes. */
4085 if (i > 1 && pnew != NULL)
4086 {
4087 vim_memset(pnew, '\\', i / 2);
4088 pnew += i / 2;
4089 }
4090
4091 /* Even nr of backslashes toggles quoting, uneven copies
4092 * the double quote. */
4093 if ((i & 1) == 0)
4094 inquote = !inquote;
4095 else if (pnew != NULL)
4096 *pnew++ = '"';
4097 p += i + 1;
4098 }
4099 else if (i > 0)
4100 {
4101 /* Copy span of backslashes unmodified. */
4102 if (pnew != NULL)
4103 {
4104 vim_memset(pnew, '\\', i);
4105 pnew += i;
4106 }
4107 p += i;
4108 }
4109 else
4110 {
4111 if (pnew != NULL)
4112 *pnew++ = *p;
4113#ifdef FEAT_MBYTE
4114 /* Can't use mb_* functions, because 'encoding' is not
4115 * initialized yet here. */
4116 if (IsDBCSLeadByte(*p))
4117 {
4118 ++p;
4119 if (pnew != NULL)
4120 *pnew++ = *p;
4121 }
4122#endif
4123 ++p;
4124 }
4125 }
4126
4127 if (pnew != NULL)
4128 *pnew++ = NUL;
4129 while (*p == ' ' || *p == '\t')
4130 ++p; /* advance until a non-space */
4131 }
4132
4133 if (round == 1)
4134 {
4135 argv = (char **)malloc((argc + 1) * sizeof(char *));
4136 if (argv == NULL )
4137 {
4138 free(newcmdline);
4139 return 0; /* malloc error */
4140 }
4141 pnew = newcmdline;
4142 *tofree = newcmdline;
4143 }
4144 }
4145
4146#ifdef FEAT_MBYTE
4147done:
4148#endif
4149 argv[argc] = NULL; /* NULL-terminated list */
4150 *argvp = argv;
4151 return argc;
4152}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153
4154#ifdef FEAT_XPM_W32
4155# include "xpm_w32.h"
4156#endif
4157
4158#ifdef PROTO
4159# define WINAPI
4160#endif
4161
4162#ifdef __MINGW32__
4163/*
4164 * Add a lot of missing defines.
4165 * They are not always missing, we need the #ifndef's.
4166 */
4167# ifndef _cdecl
4168# define _cdecl
4169# endif
4170# ifndef IsMinimized
4171# define IsMinimized(hwnd) IsIconic(hwnd)
4172# endif
4173# ifndef IsMaximized
4174# define IsMaximized(hwnd) IsZoomed(hwnd)
4175# endif
4176# ifndef SelectFont
4177# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
4178# endif
4179# ifndef GetStockBrush
4180# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
4181# endif
4182# ifndef DeleteBrush
4183# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
4184# endif
4185
4186# ifndef HANDLE_WM_RBUTTONDBLCLK
4187# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4188 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4189# endif
4190# ifndef HANDLE_WM_MBUTTONUP
4191# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
4192 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4193# endif
4194# ifndef HANDLE_WM_MBUTTONDBLCLK
4195# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4196 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4197# endif
4198# ifndef HANDLE_WM_LBUTTONDBLCLK
4199# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4200 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4201# endif
4202# ifndef HANDLE_WM_RBUTTONDOWN
4203# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
4204 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4205# endif
4206# ifndef HANDLE_WM_MOUSEMOVE
4207# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
4208 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4209# endif
4210# ifndef HANDLE_WM_RBUTTONUP
4211# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
4212 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4213# endif
4214# ifndef HANDLE_WM_MBUTTONDOWN
4215# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
4216 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4217# endif
4218# ifndef HANDLE_WM_LBUTTONUP
4219# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
4220 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4221# endif
4222# ifndef HANDLE_WM_LBUTTONDOWN
4223# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
4224 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4225# endif
4226# ifndef HANDLE_WM_SYSCHAR
4227# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
4228 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4229# endif
4230# ifndef HANDLE_WM_ACTIVATEAPP
4231# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
4232 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
4233# endif
4234# ifndef HANDLE_WM_WINDOWPOSCHANGING
4235# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
4236 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
4237# endif
4238# ifndef HANDLE_WM_VSCROLL
4239# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
4240 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4241# endif
4242# ifndef HANDLE_WM_SETFOCUS
4243# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
4244 ((fn)((hwnd), (HWND)(wParam)), 0L)
4245# endif
4246# ifndef HANDLE_WM_KILLFOCUS
4247# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
4248 ((fn)((hwnd), (HWND)(wParam)), 0L)
4249# endif
4250# ifndef HANDLE_WM_HSCROLL
4251# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
4252 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4253# endif
4254# ifndef HANDLE_WM_DROPFILES
4255# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
4256 ((fn)((hwnd), (HDROP)(wParam)), 0L)
4257# endif
4258# ifndef HANDLE_WM_CHAR
4259# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
4260 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4261# endif
4262# ifndef HANDLE_WM_SYSDEADCHAR
4263# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
4264 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4265# endif
4266# ifndef HANDLE_WM_DEADCHAR
4267# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4268 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4269# endif
4270#endif /* __MINGW32__ */
4271
4272
4273/* Some parameters for tearoff menus. All in pixels. */
4274#define TEAROFF_PADDING_X 2
4275#define TEAROFF_BUTTON_PAD_X 8
4276#define TEAROFF_MIN_WIDTH 200
4277#define TEAROFF_SUBMENU_LABEL ">>"
4278#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4279
4280
4281/* For the Intellimouse: */
4282#ifndef WM_MOUSEWHEEL
4283#define WM_MOUSEWHEEL 0x20a
4284#endif
4285
4286
4287#ifdef FEAT_BEVAL
4288# define ID_BEVAL_TOOLTIP 200
4289# define BEVAL_TEXT_LEN MAXPATHL
4290
Bram Moolenaar167632f2010-05-26 21:42:54 +02004291#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004292/* Work around old versions of basetsd.h which wrongly declares
4293 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02004294# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00004295# define UINT_PTR UINT
4296#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004297
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004298static void make_tooltip(BalloonEval *beval, char *text, POINT pt);
4299static void delete_tooltip(BalloonEval *beval);
4300static VOID CALLBACK BevalTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004301
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004303static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004305
Bram Moolenaar82881492012-11-20 16:53:39 +01004306
4307/* cproto fails on missing include files */
4308#ifndef PROTO
4309
Bram Moolenaar45360022005-07-21 21:08:21 +00004310/*
4311 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004312 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004313 */
Bram Moolenaar82881492012-11-20 16:53:39 +01004314# include <pshpack1.h>
4315
4316#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004317
4318typedef struct _DllVersionInfo
4319{
4320 DWORD cbSize;
4321 DWORD dwMajorVersion;
4322 DWORD dwMinorVersion;
4323 DWORD dwBuildNumber;
4324 DWORD dwPlatformID;
4325} DLLVERSIONINFO;
4326
Bram Moolenaar82881492012-11-20 16:53:39 +01004327#ifndef PROTO
4328# include <poppack.h>
4329#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004330
Bram Moolenaar45360022005-07-21 21:08:21 +00004331typedef struct tagTOOLINFOA_NEW
4332{
4333 UINT cbSize;
4334 UINT uFlags;
4335 HWND hwnd;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004336 UINT_PTR uId;
Bram Moolenaar45360022005-07-21 21:08:21 +00004337 RECT rect;
4338 HINSTANCE hinst;
4339 LPSTR lpszText;
4340 LPARAM lParam;
4341} TOOLINFO_NEW;
4342
4343typedef struct tagNMTTDISPINFO_NEW
4344{
4345 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004346 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004347 char szText[80];
4348 HINSTANCE hinst;
4349 UINT uFlags;
4350 LPARAM lParam;
4351} NMTTDISPINFO_NEW;
4352
Bram Moolenaar45360022005-07-21 21:08:21 +00004353typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4354#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004355# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356#endif
4357
Bram Moolenaar45360022005-07-21 21:08:21 +00004358#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004359# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004360#endif
4361
4362#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004363# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004364#endif
4365
4366#endif /* defined(FEAT_BEVAL) */
4367
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004368#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4369/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4370 * it here if LPNMTTDISPINFO isn't defined.
4371 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4372 * _MSC_VER. */
4373# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4374typedef struct tagNMTTDISPINFOA {
4375 NMHDR hdr;
4376 LPSTR lpszText;
4377 char szText[80];
4378 HINSTANCE hinst;
4379 UINT uFlags;
4380 LPARAM lParam;
4381} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4382# define LPNMTTDISPINFO LPNMTTDISPINFOA
4383
4384# ifdef FEAT_MBYTE
4385typedef struct tagNMTTDISPINFOW {
4386 NMHDR hdr;
4387 LPWSTR lpszText;
4388 WCHAR szText[80];
4389 HINSTANCE hinst;
4390 UINT uFlags;
4391 LPARAM lParam;
4392} NMTTDISPINFOW, *LPNMTTDISPINFOW;
4393# endif
4394# endif
4395#endif
4396
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004397#ifndef TTN_GETDISPINFOW
4398# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4399#endif
4400
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401/* Local variables: */
4402
4403#ifdef FEAT_MENU
4404static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406
4407/*
4408 * Use the system font for dialogs and tear-off menus. Remove this line to
4409 * use DLG_FONT_NAME.
4410 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004411#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412
4413#define VIM_NAME "vim"
4414#define VIM_CLASS "Vim"
4415#define VIM_CLASSW L"Vim"
4416
4417/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4418 * thus there should be room for every dialog. For tearoffs it's made bigger
4419 * when needed. */
4420#define DLG_ALLOC_SIZE 16 * 1024
4421
4422/*
4423 * stuff for dialogs, menus, tearoffs etc.
4424 */
4425static LRESULT APIENTRY dialog_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004426#ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427static LRESULT APIENTRY tearoff_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429static PWORD
4430add_dialog_element(
4431 PWORD p,
4432 DWORD lStyle,
4433 WORD x,
4434 WORD y,
4435 WORD w,
4436 WORD h,
4437 WORD Id,
4438 WORD clss,
4439 const char *caption);
4440static LPWORD lpwAlign(LPWORD);
4441static int nCopyAnsiToWideChar(LPWORD, LPSTR);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004442#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445static void get_dialog_font_metrics(void);
4446
4447static int dialog_default_button = -1;
4448
4449/* Intellimouse support */
4450static int mouse_scroll_lines = 0;
4451static UINT msh_msgmousewheel = 0;
4452
4453static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4454#ifdef FEAT_TOOLBAR
4455static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004456static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457static int get_toolbar_bitmap(vimmenu_T *menu);
4458#endif
4459
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004460#ifdef FEAT_GUI_TABLINE
4461static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004462static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004463#endif
4464
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465#ifdef FEAT_MBYTE_IME
4466static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4467static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4468#endif
4469#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4470# ifdef NOIME
4471typedef struct tagCOMPOSITIONFORM {
4472 DWORD dwStyle;
4473 POINT ptCurrentPos;
4474 RECT rcArea;
4475} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4476typedef HANDLE HIMC;
4477# endif
4478
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004479static HINSTANCE hLibImm = NULL;
4480static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4481static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4482static HIMC (WINAPI *pImmGetContext)(HWND);
4483static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4484static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4485static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4486static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
4487static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
4488static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
4489static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4490static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004491static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492static void dyn_imm_load(void);
4493#else
4494# define pImmGetCompositionStringA ImmGetCompositionStringA
4495# define pImmGetCompositionStringW ImmGetCompositionStringW
4496# define pImmGetContext ImmGetContext
4497# define pImmAssociateContext ImmAssociateContext
4498# define pImmReleaseContext ImmReleaseContext
4499# define pImmGetOpenStatus ImmGetOpenStatus
4500# define pImmSetOpenStatus ImmSetOpenStatus
4501# define pImmGetCompositionFont ImmGetCompositionFontA
4502# define pImmSetCompositionFont ImmSetCompositionFontA
4503# define pImmSetCompositionWindow ImmSetCompositionWindow
4504# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004505# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506#endif
4507
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508/* multi monitor support */
4509typedef struct _MONITORINFOstruct
4510{
4511 DWORD cbSize;
4512 RECT rcMonitor;
4513 RECT rcWork;
4514 DWORD dwFlags;
4515} _MONITORINFO;
4516
4517typedef HANDLE _HMONITOR;
4518typedef _HMONITOR (WINAPI *TMonitorFromWindow)(HWND, DWORD);
4519typedef BOOL (WINAPI *TGetMonitorInfo)(_HMONITOR, _MONITORINFO *);
4520
4521static TMonitorFromWindow pMonitorFromWindow = NULL;
4522static TGetMonitorInfo pGetMonitorInfo = NULL;
4523static HANDLE user32_lib = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524/*
4525 * Return TRUE when running under Windows NT 3.x or Win32s, both of which have
4526 * less fancy GUI APIs.
4527 */
4528 static int
4529is_winnt_3(void)
4530{
4531 return ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
4532 && os_version.dwMajorVersion == 3)
4533 || (os_version.dwPlatformId == VER_PLATFORM_WIN32s));
4534}
4535
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536#ifdef FEAT_MENU
4537/*
4538 * Figure out how high the menu bar is at the moment.
4539 */
4540 static int
4541gui_mswin_get_menu_height(
4542 int fix_window) /* If TRUE, resize window if menu height changed */
4543{
4544 static int old_menu_height = -1;
4545
4546 RECT rc1, rc2;
4547 int num;
4548 int menu_height;
4549
4550 if (gui.menu_is_active)
4551 num = GetMenuItemCount(s_menuBar);
4552 else
4553 num = 0;
4554
4555 if (num == 0)
4556 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004557 else if (IsMinimized(s_hwnd))
4558 {
4559 /* The height of the menu cannot be determined while the window is
4560 * minimized. Take the previous height if the menu is changed in that
4561 * state, to avoid that Vim's vertical window size accidentally
4562 * increases due to the unaccounted-for menu height. */
4563 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 else
4566 {
4567 if (is_winnt_3()) /* for NT 3.xx */
4568 {
4569 if (gui.starting)
4570 menu_height = GetSystemMetrics(SM_CYMENU);
4571 else
4572 {
4573 RECT r1, r2;
4574 int frameht = GetSystemMetrics(SM_CYFRAME);
4575 int capht = GetSystemMetrics(SM_CYCAPTION);
4576
4577 /* get window rect of s_hwnd
4578 * get client rect of s_hwnd
4579 * get cap height
4580 * subtract from window rect, the sum of client height,
4581 * (if not maximized)frame thickness, and caption height.
4582 */
4583 GetWindowRect(s_hwnd, &r1);
4584 GetClientRect(s_hwnd, &r2);
4585 menu_height = r1.bottom - r1.top - (r2.bottom - r2.top
4586 + 2 * frameht * (!IsZoomed(s_hwnd)) + capht);
4587 }
4588 }
4589 else /* win95 and variants (NT 4.0, I guess) */
4590 {
4591 /*
4592 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4593 * seem to have been set yet, so menu wraps in default window
4594 * width which is very narrow. Instead just return height of a
4595 * single menu item. Will still be wrong when the menu really
4596 * should wrap over more than one line.
4597 */
4598 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4599 if (gui.starting)
4600 menu_height = rc1.bottom - rc1.top + 1;
4601 else
4602 {
4603 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4604 menu_height = rc2.bottom - rc1.top + 1;
4605 }
4606 }
4607 }
4608
4609 if (fix_window && menu_height != old_menu_height)
4610 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004611 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 }
Bram Moolenaar71371b12015-03-24 17:57:12 +01004613 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614
4615 return menu_height;
4616}
4617#endif /*FEAT_MENU*/
4618
4619
4620/*
4621 * Setup for the Intellimouse
4622 */
4623 static void
4624init_mouse_wheel(void)
4625{
4626
4627#ifndef SPI_GETWHEELSCROLLLINES
4628# define SPI_GETWHEELSCROLLLINES 104
4629#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004630#ifndef SPI_SETWHEELSCROLLLINES
4631# define SPI_SETWHEELSCROLLLINES 105
4632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633
4634#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4635#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4636#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4637#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4638
4639 HWND hdl_mswheel;
4640 UINT msh_msgscrolllines;
4641
4642 msh_msgmousewheel = 0;
4643 mouse_scroll_lines = 3; /* reasonable default */
4644
4645 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
4646 && os_version.dwMajorVersion >= 4)
4647 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
4648 && ((os_version.dwMajorVersion == 4
4649 && os_version.dwMinorVersion >= 10)
4650 || os_version.dwMajorVersion >= 5)))
4651 {
4652 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4653 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4654 &mouse_scroll_lines, 0);
4655 }
4656 else if (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
4657 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
4658 && os_version.dwMajorVersion < 4))
4659 { /*
4660 * If Win95 or NT 3.51,
4661 * try to find the hidden point32 window.
4662 */
4663 hdl_mswheel = FindWindow(VMOUSEZ_CLASSNAME, VMOUSEZ_TITLE);
4664 if (hdl_mswheel)
4665 {
4666 msh_msgscrolllines = RegisterWindowMessage(VMSH_SCROLL_LINES);
4667 if (msh_msgscrolllines)
4668 {
4669 mouse_scroll_lines = (int)SendMessage(hdl_mswheel,
4670 msh_msgscrolllines, 0, 0);
4671 msh_msgmousewheel = RegisterWindowMessage(VMSH_MOUSEWHEEL);
4672 }
4673 }
4674 }
4675}
4676
4677
4678/* Intellimouse wheel handler */
4679 static void
4680_OnMouseWheel(
4681 HWND hwnd,
4682 short zDelta)
4683{
4684/* Treat a mouse wheel event as if it were a scroll request */
4685 int i;
4686 int size;
4687 HWND hwndCtl;
4688
4689 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4690 {
4691 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4692 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4693 }
4694 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4695 {
4696 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4697 size = curwin->w_scrollbars[SBAR_LEFT].size;
4698 }
4699 else
4700 return;
4701
4702 size = curwin->w_height;
4703 if (mouse_scroll_lines == 0)
4704 init_mouse_wheel();
4705
4706 if (mouse_scroll_lines > 0
4707 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4708 {
4709 for (i = mouse_scroll_lines; i > 0; --i)
4710 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4711 }
4712 else
4713 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
4714}
4715
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004716#ifdef USE_SYSMENU_FONT
4717/*
4718 * Get Menu Font.
4719 * Return OK or FAIL.
4720 */
4721 static int
4722gui_w32_get_menu_font(LOGFONT *lf)
4723{
4724 NONCLIENTMETRICS nm;
4725
4726 nm.cbSize = sizeof(NONCLIENTMETRICS);
4727 if (!SystemParametersInfo(
4728 SPI_GETNONCLIENTMETRICS,
4729 sizeof(NONCLIENTMETRICS),
4730 &nm,
4731 0))
4732 return FAIL;
4733 *lf = nm.lfMenuFont;
4734 return OK;
4735}
4736#endif
4737
4738
4739#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4740/*
4741 * Set the GUI tabline font to the system menu font
4742 */
4743 static void
4744set_tabline_font(void)
4745{
4746 LOGFONT lfSysmenu;
4747 HFONT font;
4748 HWND hwnd;
4749 HDC hdc;
4750 HFONT hfntOld;
4751 TEXTMETRIC tm;
4752
4753 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4754 return;
4755
4756 font = CreateFontIndirect(&lfSysmenu);
4757
4758 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4759
4760 /*
4761 * Compute the height of the font used for the tab text
4762 */
4763 hwnd = GetDesktopWindow();
4764 hdc = GetWindowDC(hwnd);
4765 hfntOld = SelectFont(hdc, font);
4766
4767 GetTextMetrics(hdc, &tm);
4768
4769 SelectFont(hdc, hfntOld);
4770 ReleaseDC(hwnd, hdc);
4771
4772 /*
4773 * The space used by the tab border and the space between the tab label
4774 * and the tab border is included as 7.
4775 */
4776 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4777}
4778#endif
4779
Bram Moolenaar520470a2005-06-16 21:59:56 +00004780/*
4781 * Invoked when a setting was changed.
4782 */
4783 static LRESULT CALLBACK
4784_OnSettingChange(UINT n)
4785{
4786 if (n == SPI_SETWHEELSCROLLLINES)
4787 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4788 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004789#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4790 if (n == SPI_SETNONCLIENTMETRICS)
4791 set_tabline_font();
4792#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004793 return 0;
4794}
4795
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796#ifdef FEAT_NETBEANS_INTG
4797 static void
4798_OnWindowPosChanged(
4799 HWND hwnd,
4800 const LPWINDOWPOS lpwpos)
4801{
4802 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004803 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804
4805 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4806 || lpwpos->cx != cx || lpwpos->cy != cy))
4807 {
4808 x = lpwpos->x;
4809 y = lpwpos->y;
4810 cx = lpwpos->cx;
4811 cy = lpwpos->cy;
4812 netbeans_frame_moved(x, y);
4813 }
4814 /* Allow to send WM_SIZE and WM_MOVE */
4815 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4816}
4817#endif
4818
4819 static int
4820_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 UINT fwSide,
4822 LPRECT lprc)
4823{
4824 int w, h;
4825 int valid_w, valid_h;
4826 int w_offset, h_offset;
4827
4828 w = lprc->right - lprc->left;
4829 h = lprc->bottom - lprc->top;
4830 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4831 w_offset = w - valid_w;
4832 h_offset = h - valid_h;
4833
4834 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4835 || fwSide == WMSZ_BOTTOMLEFT)
4836 lprc->left += w_offset;
4837 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4838 || fwSide == WMSZ_BOTTOMRIGHT)
4839 lprc->right -= w_offset;
4840
4841 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4842 || fwSide == WMSZ_TOPRIGHT)
4843 lprc->top += h_offset;
4844 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4845 || fwSide == WMSZ_BOTTOMRIGHT)
4846 lprc->bottom -= h_offset;
4847 return TRUE;
4848}
4849
4850
4851
4852 static LRESULT CALLBACK
4853_WndProc(
4854 HWND hwnd,
4855 UINT uMsg,
4856 WPARAM wParam,
4857 LPARAM lParam)
4858{
4859 /*
4860 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4861 hwnd, uMsg, wParam, lParam);
4862 */
4863
4864 HandleMouseHide(uMsg, lParam);
4865
4866 s_uMsg = uMsg;
4867 s_wParam = wParam;
4868 s_lParam = lParam;
4869
4870 switch (uMsg)
4871 {
4872 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4873 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4874 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4875 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4876 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4877 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4878 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4879 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4880 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4881#ifdef FEAT_MENU
4882 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4883#endif
4884 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4885 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4886 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4887 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4888 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4889 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4890 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4891 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4892 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4893#ifdef FEAT_NETBEANS_INTG
4894 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4895#endif
4896
Bram Moolenaarafa24992006-03-27 20:58:26 +00004897#ifdef FEAT_GUI_TABLINE
4898 case WM_RBUTTONUP:
4899 {
4900 if (gui_mch_showing_tabline())
4901 {
4902 POINT pt;
4903 RECT rect;
4904
4905 /*
4906 * If the cursor is on the tabline, display the tab menu
4907 */
4908 GetCursorPos((LPPOINT)&pt);
4909 GetWindowRect(s_textArea, &rect);
4910 if (pt.y < rect.top)
4911 {
4912 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004913 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004914 }
4915 }
4916 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4917 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004918 case WM_LBUTTONDBLCLK:
4919 {
4920 /*
4921 * If the user double clicked the tabline, create a new tab
4922 */
4923 if (gui_mch_showing_tabline())
4924 {
4925 POINT pt;
4926 RECT rect;
4927
4928 GetCursorPos((LPPOINT)&pt);
4929 GetWindowRect(s_textArea, &rect);
4930 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004931 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004932 }
4933 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4934 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004935#endif
4936
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 case WM_QUERYENDSESSION: /* System wants to go down. */
4938 gui_shell_closed(); /* Will exit when no changed buffers. */
4939 return FALSE; /* Do NOT allow system to go down. */
4940
4941 case WM_ENDSESSION:
4942 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004943 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004945 return 0L;
4946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 break;
4948
4949 case WM_CHAR:
4950 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4951 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004952 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 return 0L;
4954
4955 case WM_SYSCHAR:
4956 /*
4957 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4958 * shortcut key, handle like a typed ALT key, otherwise call Windows
4959 * ALT key handling.
4960 */
4961#ifdef FEAT_MENU
4962 if ( !gui.menu_is_active
4963 || p_wak[0] == 'n'
4964 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4965 )
4966#endif
4967 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004968 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 return 0L;
4970 }
4971#ifdef FEAT_MENU
4972 else
4973 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4974#endif
4975
4976 case WM_SYSKEYUP:
4977#ifdef FEAT_MENU
4978 /* This used to be done only when menu is active: ALT key is used for
4979 * that. But that caused problems when menu is disabled and using
4980 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4981 * are received, mouse pointer remains hidden. */
4982 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4983#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004984 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985#endif
4986
4987 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004988 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989
4990 case WM_MOUSEWHEEL:
4991 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004992 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993
Bram Moolenaar520470a2005-06-16 21:59:56 +00004994 /* Notification for change in SystemParametersInfo() */
4995 case WM_SETTINGCHANGE:
4996 return _OnSettingChange((UINT)wParam);
4997
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004998#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 case WM_NOTIFY:
5000 switch (((LPNMHDR) lParam)->code)
5001 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005002# ifdef FEAT_MBYTE
5003 case TTN_GETDISPINFOW:
5004# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005005 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005007 LPNMHDR hdr = (LPNMHDR)lParam;
5008 char_u *str = NULL;
5009 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005010
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005011 vim_free(tt_text);
5012 tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005013
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005014# ifdef FEAT_GUI_TABLINE
5015 if (gui_mch_showing_tabline()
5016 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005017 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005018 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005019 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005020 * Mouse is over the GUI tabline. Display the
5021 * tooltip for the tab under the cursor
5022 *
5023 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005024 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005025 GetCursorPos(&pt);
5026 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005027 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005028 TCHITTESTINFO htinfo;
5029 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005030
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005031 /*
5032 * Get the tab under the cursor
5033 */
5034 htinfo.pt.x = pt.x;
5035 htinfo.pt.y = pt.y;
5036 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
5037 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005038 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005039 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005040
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005041 tp = find_tabpage(idx + 1);
5042 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005043 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005044 get_tabline_label(tp, TRUE);
5045 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005046 }
5047 }
5048 }
5049 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005050# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005051# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005052# ifdef FEAT_GUI_TABLINE
5053 else
5054# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005056 UINT idButton;
5057 vimmenu_T *pMenu;
5058
5059 idButton = (UINT) hdr->idFrom;
5060 pMenu = gui_mswin_find_menu(root_menu, idButton);
5061 if (pMenu)
5062 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005064# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005065 if (str != NULL)
5066 {
5067# ifdef FEAT_MBYTE
5068 if (hdr->code == TTN_GETDISPINFOW)
5069 {
5070 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
5071
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005072 /* Set the maximum width, this also enables using
5073 * \n for line break. */
5074 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5075 0, 500);
5076
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005077 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005078 lpdi->lpszText = tt_text;
5079 /* can't show tooltip if failed */
5080 }
5081 else
5082# endif
5083 {
5084 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
5085
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005086 /* Set the maximum width, this also enables using
5087 * \n for line break. */
5088 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5089 0, 500);
5090
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005091 if (STRLEN(str) < sizeof(lpdi->szText)
5092 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005093 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005094 sizeof(lpdi->szText) - 1);
5095 else
5096 lpdi->lpszText = tt_text;
5097 }
5098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 }
5100 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005101# ifdef FEAT_GUI_TABLINE
5102 case TCN_SELCHANGE:
5103 if (gui_mch_showing_tabline()
5104 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005105 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005106 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005107 return 0L;
5108 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005109 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00005110
5111 case NM_RCLICK:
5112 if (gui_mch_showing_tabline()
5113 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005114 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00005115 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005116 return 0L;
5117 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00005118 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005119# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005121# ifdef FEAT_GUI_TABLINE
5122 if (gui_mch_showing_tabline()
5123 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
5124 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 break;
5127 }
5128 break;
5129#endif
5130#if defined(MENUHINTS) && defined(FEAT_MENU)
5131 case WM_MENUSELECT:
5132 if (((UINT) HIWORD(wParam)
5133 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
5134 == MF_HILITE
5135 && (State & CMDLINE) == 0)
5136 {
5137 UINT idButton;
5138 vimmenu_T *pMenu;
5139 static int did_menu_tip = FALSE;
5140
5141 if (did_menu_tip)
5142 {
5143 msg_clr_cmdline();
5144 setcursor();
5145 out_flush();
5146 did_menu_tip = FALSE;
5147 }
5148
5149 idButton = (UINT)LOWORD(wParam);
5150 pMenu = gui_mswin_find_menu(root_menu, idButton);
5151 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
5152 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
5153 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005154 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005156 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 setcursor();
5158 out_flush();
5159 did_menu_tip = TRUE;
5160 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01005161 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
5163 break;
5164#endif
5165 case WM_NCHITTEST:
5166 {
5167 LRESULT result;
5168 int x, y;
5169 int xPos = GET_X_LPARAM(lParam);
5170
5171 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
5172 if (result == HTCLIENT)
5173 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005174#ifdef FEAT_GUI_TABLINE
5175 if (gui_mch_showing_tabline())
5176 {
5177 int yPos = GET_Y_LPARAM(lParam);
5178 RECT rct;
5179
5180 /* If the cursor is on the GUI tabline, don't process this
5181 * event */
5182 GetWindowRect(s_textArea, &rct);
5183 if (yPos < rct.top)
5184 return result;
5185 }
5186#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02005187 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 xPos -= x;
5189
5190 if (xPos < 48) /* <VN> TODO should use system metric? */
5191 return HTBOTTOMLEFT;
5192 else
5193 return HTBOTTOMRIGHT;
5194 }
5195 else
5196 return result;
5197 }
5198 /* break; notreached */
5199
5200#ifdef FEAT_MBYTE_IME
5201 case WM_IME_NOTIFY:
5202 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5203 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005204 return 1L;
5205
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 case WM_IME_COMPOSITION:
5207 if (!_OnImeComposition(hwnd, wParam, lParam))
5208 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005209 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210#endif
5211
5212 default:
5213 if (uMsg == msh_msgmousewheel && msh_msgmousewheel != 0)
5214 { /* handle MSH_MOUSEWHEEL messages for Intellimouse */
5215 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01005216 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 }
5218#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005219 else if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 {
5221 _OnFindRepl();
5222 }
5223#endif
5224 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5225 }
5226
Bram Moolenaar2787ab92011-12-14 15:23:59 +01005227 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228}
5229
5230/*
5231 * End of call-back routines
5232 */
5233
5234/* parent window, if specified with -P */
5235HWND vim_parent_hwnd = NULL;
5236
5237 static BOOL CALLBACK
5238FindWindowTitle(HWND hwnd, LPARAM lParam)
5239{
5240 char buf[2048];
5241 char *title = (char *)lParam;
5242
5243 if (GetWindowText(hwnd, buf, sizeof(buf)))
5244 {
5245 if (strstr(buf, title) != NULL)
5246 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005247 /* Found it. Store the window ref. and quit searching if MDI
5248 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005250 if (vim_parent_hwnd != NULL)
5251 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 }
5253 }
5254 return TRUE; /* continue searching */
5255}
5256
5257/*
5258 * Invoked for '-P "title"' argument: search for parent application to open
5259 * our window in.
5260 */
5261 void
5262gui_mch_set_parent(char *title)
5263{
5264 EnumWindows(FindWindowTitle, (LPARAM)title);
5265 if (vim_parent_hwnd == NULL)
5266 {
5267 EMSG2(_("E671: Cannot find window title \"%s\""), title);
5268 mch_exit(2);
5269 }
5270}
5271
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005272#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 static void
5274ole_error(char *arg)
5275{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005276 char buf[IOSIZE];
5277
5278 /* Can't use EMSG() here, we have not finished initialisation yet. */
5279 vim_snprintf(buf, IOSIZE,
5280 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
5281 arg);
5282 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
5286/*
5287 * Parse the GUI related command-line arguments. Any arguments used are
5288 * deleted from argv, and *argc is decremented accordingly. This is called
5289 * when vim is started, whether or not the GUI has been started.
5290 */
5291 void
5292gui_mch_prepare(int *argc, char **argv)
5293{
5294 int silent = FALSE;
5295 int idx;
5296
5297 /* Check for special OLE command line parameters */
5298 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5299 {
5300 /* Check for a "-silent" argument first. */
5301 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5302 && (argv[2][0] == '-' || argv[2][0] == '/'))
5303 {
5304 silent = TRUE;
5305 idx = 2;
5306 }
5307 else
5308 idx = 1;
5309
5310 /* Register Vim as an OLE Automation server */
5311 if (STRICMP(argv[idx] + 1, "register") == 0)
5312 {
5313#ifdef FEAT_OLE
5314 RegisterMe(silent);
5315 mch_exit(0);
5316#else
5317 if (!silent)
5318 ole_error("register");
5319 mch_exit(2);
5320#endif
5321 }
5322
5323 /* Unregister Vim as an OLE Automation server */
5324 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5325 {
5326#ifdef FEAT_OLE
5327 UnregisterMe(!silent);
5328 mch_exit(0);
5329#else
5330 if (!silent)
5331 ole_error("unregister");
5332 mch_exit(2);
5333#endif
5334 }
5335
5336 /* Ignore an -embedding argument. It is only relevant if the
5337 * application wants to treat the case when it is started manually
5338 * differently from the case where it is started via automation (and
5339 * we don't).
5340 */
5341 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5342 {
5343#ifdef FEAT_OLE
5344 *argc = 1;
5345#else
5346 ole_error("embedding");
5347 mch_exit(2);
5348#endif
5349 }
5350 }
5351
5352#ifdef FEAT_OLE
5353 {
5354 int bDoRestart = FALSE;
5355
5356 InitOLE(&bDoRestart);
5357 /* automatically exit after registering */
5358 if (bDoRestart)
5359 mch_exit(0);
5360 }
5361#endif
5362
5363#ifdef FEAT_NETBEANS_INTG
5364 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005365 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 int arg;
5367
5368 for (arg = 1; arg < *argc; arg++)
5369 if (strncmp("-nb", argv[arg], 3) == 0)
5370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 netbeansArg = argv[arg];
5372 mch_memmove(&argv[arg], &argv[arg + 1],
5373 (--*argc - arg) * sizeof(char *));
5374 argv[*argc] = NULL;
5375 break; /* enough? */
5376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 }
5378#endif
5379
5380 /* get the OS version info */
5381 os_version.dwOSVersionInfoSize = sizeof(os_version);
5382 GetVersionEx(&os_version); /* this call works on Win32s, Win95 and WinNT */
5383
5384 /* try and load the user32.dll library and get the entry points for
5385 * multi-monitor-support. */
Bram Moolenaarebbcb822010-10-23 14:02:54 +02005386 if ((user32_lib = vimLoadLib("User32.dll")) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 {
5388 pMonitorFromWindow = (TMonitorFromWindow)GetProcAddress(user32_lib,
5389 "MonitorFromWindow");
5390
5391 /* there are ...A and ...W version of GetMonitorInfo - looking at
5392 * winuser.h, they have exactly the same declaration. */
5393 pGetMonitorInfo = (TGetMonitorInfo)GetProcAddress(user32_lib,
5394 "GetMonitorInfoA");
5395 }
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02005396
5397#ifdef FEAT_MBYTE
5398 /* If the OS is Windows NT, use wide functions;
5399 * this enables common dialogs input unicode from IME. */
5400 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
5401 {
5402 pDispatchMessage = DispatchMessageW;
5403 pGetMessage = GetMessageW;
5404 pIsDialogMessage = IsDialogMessageW;
5405 pPeekMessage = PeekMessageW;
5406 }
5407 else
5408 {
5409 pDispatchMessage = DispatchMessageA;
5410 pGetMessage = GetMessageA;
5411 pIsDialogMessage = IsDialogMessageA;
5412 pPeekMessage = PeekMessageA;
5413 }
5414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415}
5416
5417/*
5418 * Initialise the GUI. Create all the windows, set up all the call-backs
5419 * etc.
5420 */
5421 int
5422gui_mch_init(void)
5423{
5424 const char szVimWndClass[] = VIM_CLASS;
5425 const char szTextAreaClass[] = "VimTextArea";
5426 WNDCLASS wndclass;
5427#ifdef FEAT_MBYTE
5428 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005429 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 WNDCLASSW wndclassw;
5431#endif
5432#ifdef GLOBAL_IME
5433 ATOM atom;
5434#endif
5435
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 /* Return here if the window was already opened (happens when
5437 * gui_mch_dialog() is called early). */
5438 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005439 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440
5441 /*
5442 * Load the tearoff bitmap
5443 */
5444#ifdef FEAT_TEAROFF
5445 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
5446#endif
5447
5448 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5449 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5450#ifdef FEAT_MENU
5451 gui.menu_height = 0; /* Windows takes care of this */
5452#endif
5453 gui.border_width = 0;
5454
5455 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5456
5457#ifdef FEAT_MBYTE
5458 /* First try using the wide version, so that we can use any title.
5459 * Otherwise only characters in the active codepage will work. */
5460 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
5461 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005462 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 wndclassw.lpfnWndProc = _WndProc;
5464 wndclassw.cbClsExtra = 0;
5465 wndclassw.cbWndExtra = 0;
5466 wndclassw.hInstance = s_hinst;
5467 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5468 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5469 wndclassw.hbrBackground = s_brush;
5470 wndclassw.lpszMenuName = NULL;
5471 wndclassw.lpszClassName = szVimWndClassW;
5472
5473 if ((
5474#ifdef GLOBAL_IME
5475 atom =
5476#endif
5477 RegisterClassW(&wndclassw)) == 0)
5478 {
5479 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
5480 return FAIL;
5481
5482 /* Must be Windows 98, fall back to non-wide function. */
5483 }
5484 else
5485 wide_WindowProc = TRUE;
5486 }
5487
5488 if (!wide_WindowProc)
5489#endif
5490
5491 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
5492 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005493 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 wndclass.lpfnWndProc = _WndProc;
5495 wndclass.cbClsExtra = 0;
5496 wndclass.cbWndExtra = 0;
5497 wndclass.hInstance = s_hinst;
5498 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
5499 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5500 wndclass.hbrBackground = s_brush;
5501 wndclass.lpszMenuName = NULL;
5502 wndclass.lpszClassName = szVimWndClass;
5503
5504 if ((
5505#ifdef GLOBAL_IME
5506 atom =
5507#endif
5508 RegisterClass(&wndclass)) == 0)
5509 return FAIL;
5510 }
5511
5512 if (vim_parent_hwnd != NULL)
5513 {
5514#ifdef HAVE_TRY_EXCEPT
5515 __try
5516 {
5517#endif
5518 /* Open inside the specified parent window.
5519 * TODO: last argument should point to a CLIENTCREATESTRUCT
5520 * structure. */
5521 s_hwnd = CreateWindowEx(
5522 WS_EX_MDICHILD,
5523 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005524 WS_OVERLAPPEDWINDOW | WS_CHILD
5525 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5527 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5528 100, /* Any value will do */
5529 100, /* Any value will do */
5530 vim_parent_hwnd, NULL,
5531 s_hinst, NULL);
5532#ifdef HAVE_TRY_EXCEPT
5533 }
5534 __except(EXCEPTION_EXECUTE_HANDLER)
5535 {
5536 /* NOP */
5537 }
5538#endif
5539 if (s_hwnd == NULL)
5540 {
5541 EMSG(_("E672: Unable to open window inside MDI application"));
5542 mch_exit(2);
5543 }
5544 }
5545 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005546 {
5547 /* If the provided windowid is not valid reset it to zero, so that it
5548 * is ignored and we open our own window. */
5549 if (IsWindow((HWND)win_socket_id) <= 0)
5550 win_socket_id = 0;
5551
5552 /* Create a window. If win_socket_id is not zero without border and
5553 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00005555 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005556 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5557 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005558 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5559 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5560 100, /* Any value will do */
5561 100, /* Any value will do */
5562 NULL, NULL,
5563 s_hinst, NULL);
5564 if (s_hwnd != NULL && win_socket_id != 0)
5565 {
5566 SetParent(s_hwnd, (HWND)win_socket_id);
5567 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5568 }
5569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570
5571 if (s_hwnd == NULL)
5572 return FAIL;
5573
5574#ifdef GLOBAL_IME
5575 global_ime_init(atom, s_hwnd);
5576#endif
5577#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5578 dyn_imm_load();
5579#endif
5580
5581 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005582#ifdef FEAT_MBYTE
5583 if (wide_WindowProc)
5584 {
5585 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
5586 {
5587 wndclassw.style = CS_OWNDC;
5588 wndclassw.lpfnWndProc = _TextAreaWndProc;
5589 wndclassw.cbClsExtra = 0;
5590 wndclassw.cbWndExtra = 0;
5591 wndclassw.hInstance = s_hinst;
5592 wndclassw.hIcon = NULL;
5593 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5594 wndclassw.hbrBackground = NULL;
5595 wndclassw.lpszMenuName = NULL;
5596 wndclassw.lpszClassName = szTextAreaClassW;
5597
5598 if (RegisterClassW(&wndclassw) == 0)
5599 return FAIL;
5600 }
5601 }
5602 else
5603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
5605 {
5606 wndclass.style = CS_OWNDC;
5607 wndclass.lpfnWndProc = _TextAreaWndProc;
5608 wndclass.cbClsExtra = 0;
5609 wndclass.cbWndExtra = 0;
5610 wndclass.hInstance = s_hinst;
5611 wndclass.hIcon = NULL;
5612 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5613 wndclass.hbrBackground = NULL;
5614 wndclass.lpszMenuName = NULL;
5615 wndclass.lpszClassName = szTextAreaClass;
5616
5617 if (RegisterClass(&wndclass) == 0)
5618 return FAIL;
5619 }
5620 s_textArea = CreateWindowEx(
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005621 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 szTextAreaClass, "Vim text area",
5623 WS_CHILD | WS_VISIBLE, 0, 0,
5624 100, /* Any value will do for now */
5625 100, /* Any value will do for now */
5626 s_hwnd, NULL,
5627 s_hinst, NULL);
5628
5629 if (s_textArea == NULL)
5630 return FAIL;
5631
Bram Moolenaar20321902016-02-17 12:30:17 +01005632#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005633 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5634 {
5635 HANDLE hIcon = NULL;
5636
5637 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005638 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005639 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005640#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005641
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642#ifdef FEAT_MENU
5643 s_menuBar = CreateMenu();
5644#endif
5645 s_hdc = GetDC(s_textArea);
5646
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647#ifdef FEAT_WINDOWS
5648 DragAcceptFiles(s_hwnd, TRUE);
5649#endif
5650
5651 /* Do we need to bother with this? */
5652 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5653
5654 /* Get background/foreground colors from the system */
5655 gui_mch_def_colors();
5656
5657 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5658 * file) */
5659 set_normal_colors();
5660
5661 /*
5662 * Check that none of the colors are the same as the background color.
5663 * Then store the current values as the defaults.
5664 */
5665 gui_check_colors();
5666 gui.def_norm_pixel = gui.norm_pixel;
5667 gui.def_back_pixel = gui.back_pixel;
5668
5669 /* Get the colors for the highlight groups (gui_check_colors() might have
5670 * changed them) */
5671 highlight_gui_started();
5672
5673 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005674 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005676 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677
5678 /*
5679 * Set up for Intellimouse processing
5680 */
5681 init_mouse_wheel();
5682
5683 /*
5684 * compute a couple of metrics used for the dialogs
5685 */
5686 get_dialog_font_metrics();
5687#ifdef FEAT_TOOLBAR
5688 /*
5689 * Create the toolbar
5690 */
5691 initialise_toolbar();
5692#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005693#ifdef FEAT_GUI_TABLINE
5694 /*
5695 * Create the tabline
5696 */
5697 initialise_tabline();
5698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699#ifdef MSWIN_FIND_REPLACE
5700 /*
5701 * Initialise the dialog box stuff
5702 */
5703 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5704
5705 /* Initialise the struct */
5706 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005707 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005709 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5711 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5712 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00005713# if defined(FEAT_MBYTE) && defined(WIN3264)
5714 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
5715 s_findrep_struct_w.lpstrFindWhat =
5716 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5717 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
5718 s_findrep_struct_w.lpstrReplaceWith =
5719 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5720 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
5721 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
5722 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5723# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005726#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005727# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5728/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5729# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005730# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005731# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005732# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005733 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005734 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005735#endif
5736
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005737#ifdef FEAT_RENDER_OPTIONS
5738 if (p_rop)
5739 (void)gui_mch_set_rendering_options(p_rop);
5740#endif
5741
Bram Moolenaar748bf032005-02-02 23:04:36 +00005742theend:
5743 /* Display any pending error messages */
5744 display_errors();
5745
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 return OK;
5747}
5748
5749/*
5750 * Get the size of the screen, taking position on multiple monitors into
5751 * account (if supported).
5752 */
5753 static void
5754get_work_area(RECT *spi_rect)
5755{
5756 _HMONITOR mon;
5757 _MONITORINFO moninfo;
5758
5759 /* use these functions only if available */
5760 if (pMonitorFromWindow != NULL && pGetMonitorInfo != NULL)
5761 {
5762 /* work out which monitor the window is on, and get *it's* work area */
5763 mon = pMonitorFromWindow(s_hwnd, 1 /*MONITOR_DEFAULTTOPRIMARY*/);
5764 if (mon != NULL)
5765 {
5766 moninfo.cbSize = sizeof(_MONITORINFO);
5767 if (pGetMonitorInfo(mon, &moninfo))
5768 {
5769 *spi_rect = moninfo.rcWork;
5770 return;
5771 }
5772 }
5773 }
5774 /* this is the old method... */
5775 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5776}
5777
5778/*
5779 * Set the size of the window to the given width and height in pixels.
5780 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005781/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 void
5783gui_mch_set_shellsize(int width, int height,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005784 int min_width, int min_height, int base_width, int base_height,
5785 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786{
5787 RECT workarea_rect;
5788 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 WINDOWPLACEMENT wndpl;
5790
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005791 /* Try to keep window completely on screen. */
5792 /* Get position of the screen work area. This is the part that is not
5793 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 get_work_area(&workarea_rect);
5795
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005796 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005797 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 wndpl.length = sizeof(WINDOWPLACEMENT);
5799 GetWindowPlacement(s_hwnd, &wndpl);
5800
5801 /* Resizing a maximized window looks very strange, unzoom it first.
5802 * But don't do it when still starting up, it may have been requested in
5803 * the shortcut. */
5804 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5805 {
5806 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5807 /* Need to get the settings of the normal window. */
5808 GetWindowPlacement(s_hwnd, &wndpl);
5809 }
5810
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005812 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005813 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005814 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005815 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 + GetSystemMetrics(SM_CYCAPTION)
5817#ifdef FEAT_MENU
5818 + gui_mswin_get_menu_height(FALSE)
5819#endif
5820 ;
5821
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005822 /* The following should take care of keeping Vim on the same monitor, no
5823 * matter if the secondary monitor is left or right of the primary
5824 * monitor. */
5825 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5826 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005827
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005828 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005829 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005830 && wndpl.rcNormalPosition.right > workarea_rect.right)
5831 OffsetRect(&wndpl.rcNormalPosition,
5832 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005834 if ((direction & RESIZE_HOR)
5835 && wndpl.rcNormalPosition.left < workarea_rect.left)
5836 OffsetRect(&wndpl.rcNormalPosition,
5837 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838
Bram Moolenaarafa24992006-03-27 20:58:26 +00005839 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005840 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5841 OffsetRect(&wndpl.rcNormalPosition,
5842 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005844 if ((direction & RESIZE_VERT)
5845 && wndpl.rcNormalPosition.top < workarea_rect.top)
5846 OffsetRect(&wndpl.rcNormalPosition,
5847 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848
5849 /* set window position - we should use SetWindowPlacement rather than
5850 * SetWindowPos as the MSDN docs say the coord systems returned by
5851 * these two are not compatible. */
5852 SetWindowPlacement(s_hwnd, &wndpl);
5853
5854 SetActiveWindow(s_hwnd);
5855 SetFocus(s_hwnd);
5856
5857#ifdef FEAT_MENU
5858 /* Menu may wrap differently now */
5859 gui_mswin_get_menu_height(!gui.starting);
5860#endif
5861}
5862
5863
5864 void
5865gui_mch_set_scrollbar_thumb(
5866 scrollbar_T *sb,
5867 long val,
5868 long size,
5869 long max)
5870{
5871 SCROLLINFO info;
5872
5873 sb->scroll_shift = 0;
5874 while (max > 32767)
5875 {
5876 max = (max + 1) >> 1;
5877 val >>= 1;
5878 size >>= 1;
5879 ++sb->scroll_shift;
5880 }
5881
5882 if (sb->scroll_shift > 0)
5883 ++size;
5884
5885 info.cbSize = sizeof(info);
5886 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5887 info.nPos = val;
5888 info.nMin = 0;
5889 info.nMax = max;
5890 info.nPage = size;
5891 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5892}
5893
5894
5895/*
5896 * Set the current text font.
5897 */
5898 void
5899gui_mch_set_font(GuiFont font)
5900{
5901 gui.currFont = font;
5902}
5903
5904
5905/*
5906 * Set the current text foreground color.
5907 */
5908 void
5909gui_mch_set_fg_color(guicolor_T color)
5910{
5911 gui.currFgColor = color;
5912}
5913
5914/*
5915 * Set the current text background color.
5916 */
5917 void
5918gui_mch_set_bg_color(guicolor_T color)
5919{
5920 gui.currBgColor = color;
5921}
5922
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005923/*
5924 * Set the current text special color.
5925 */
5926 void
5927gui_mch_set_sp_color(guicolor_T color)
5928{
5929 gui.currSpColor = color;
5930}
5931
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932#if defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)
5933/*
5934 * Multi-byte handling, originally by Sung-Hoon Baek.
5935 * First static functions (no prototypes generated).
5936 */
5937#ifdef _MSC_VER
5938# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5939#endif
5940#include <imm.h>
5941
5942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 * handle WM_IME_NOTIFY message
5944 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005945/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 static LRESULT
5947_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData)
5948{
5949 LRESULT lResult = 0;
5950 HIMC hImc;
5951
5952 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5953 return lResult;
5954 switch (dwCommand)
5955 {
5956 case IMN_SETOPENSTATUS:
5957 if (pImmGetOpenStatus(hImc))
5958 {
5959 pImmSetCompositionFont(hImc, &norm_logfont);
5960 im_set_position(gui.row, gui.col);
5961
5962 /* Disable langmap */
5963 State &= ~LANGMAP;
5964 if (State & INSERT)
5965 {
5966#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
5967 /* Unshown 'keymap' in status lines */
5968 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5969 {
5970 /* Save cursor position */
5971 int old_row = gui.row;
5972 int old_col = gui.col;
5973
5974 // This must be called here before
5975 // status_redraw_curbuf(), otherwise the mode
5976 // message may appear in the wrong position.
5977 showmode();
5978 status_redraw_curbuf();
5979 update_screen(0);
5980 /* Restore cursor position */
5981 gui.row = old_row;
5982 gui.col = old_col;
5983 }
5984#endif
5985 }
5986 }
5987 gui_update_cursor(TRUE, FALSE);
5988 lResult = 0;
5989 break;
5990 }
5991 pImmReleaseContext(hWnd, hImc);
5992 return lResult;
5993}
5994
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005995/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 static LRESULT
5997_OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param)
5998{
5999 char_u *ret;
6000 int len;
6001
6002 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
6003 return 0;
6004
6005 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
6006 if (ret != NULL)
6007 {
6008 add_to_input_buf_csi(ret, len);
6009 vim_free(ret);
6010 return 1;
6011 }
6012 return 0;
6013}
6014
6015/*
6016 * get the current composition string, in UCS-2; *lenp is the number of
6017 * *lenp is the number of Unicode characters.
6018 */
6019 static short_u *
6020GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
6021{
6022 LONG ret;
6023 LPWSTR wbuf = NULL;
6024 char_u *buf;
6025
6026 if (!pImmGetContext)
6027 return NULL; /* no imm32.dll */
6028
6029 /* Try Unicode; this'll always work on NT regardless of codepage. */
6030 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
6031 if (ret == 0)
6032 return NULL; /* empty */
6033
6034 if (ret > 0)
6035 {
6036 /* Allocate the requested buffer plus space for the NUL character. */
6037 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
6038 if (wbuf != NULL)
6039 {
6040 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
6041 *lenp = ret / sizeof(WCHAR);
6042 }
6043 return (short_u *)wbuf;
6044 }
6045
6046 /* ret < 0; we got an error, so try the ANSI version. This'll work
6047 * on 9x/ME, but only if the codepage happens to be set to whatever
6048 * we're inputting. */
6049 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
6050 if (ret <= 0)
6051 return NULL; /* empty or error */
6052
6053 buf = alloc(ret);
6054 if (buf == NULL)
6055 return NULL;
6056 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
6057
6058 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006059 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 vim_free(buf);
6061
6062 return (short_u *)wbuf;
6063}
6064
6065/*
6066 * void GetResultStr()
6067 *
6068 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
6069 * get complete composition string
6070 */
6071 static char_u *
6072GetResultStr(HWND hwnd, int GCS, int *lenp)
6073{
6074 HIMC hIMC; /* Input context handle. */
6075 short_u *buf = NULL;
6076 char_u *convbuf = NULL;
6077
6078 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
6079 return NULL;
6080
6081 /* Reads in the composition string. */
6082 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
6083 if (buf == NULL)
6084 return NULL;
6085
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006086 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 pImmReleaseContext(hwnd, hIMC);
6088 vim_free(buf);
6089 return convbuf;
6090}
6091#endif
6092
6093/* For global functions we need prototypes. */
6094#if (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) || defined(PROTO)
6095
6096/*
6097 * set font to IM.
6098 */
6099 void
6100im_set_font(LOGFONT *lf)
6101{
6102 HIMC hImc;
6103
6104 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6105 {
6106 pImmSetCompositionFont(hImc, lf);
6107 pImmReleaseContext(s_hwnd, hImc);
6108 }
6109}
6110
6111/*
6112 * Notify cursor position to IM.
6113 */
6114 void
6115im_set_position(int row, int col)
6116{
6117 HIMC hImc;
6118
6119 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6120 {
6121 COMPOSITIONFORM cfs;
6122
6123 cfs.dwStyle = CFS_POINT;
6124 cfs.ptCurrentPos.x = FILL_X(col);
6125 cfs.ptCurrentPos.y = FILL_Y(row);
6126 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
6127 pImmSetCompositionWindow(hImc, &cfs);
6128
6129 pImmReleaseContext(s_hwnd, hImc);
6130 }
6131}
6132
6133/*
6134 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6135 */
6136 void
6137im_set_active(int active)
6138{
6139 HIMC hImc;
6140 static HIMC hImcOld = (HIMC)0;
6141
6142 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
6143 {
6144 if (p_imdisable)
6145 {
6146 if (hImcOld == (HIMC)0)
6147 {
6148 hImcOld = pImmGetContext(s_hwnd);
6149 if (hImcOld)
6150 pImmAssociateContext(s_hwnd, (HIMC)0);
6151 }
6152 active = FALSE;
6153 }
6154 else if (hImcOld != (HIMC)0)
6155 {
6156 pImmAssociateContext(s_hwnd, hImcOld);
6157 hImcOld = (HIMC)0;
6158 }
6159
6160 hImc = pImmGetContext(s_hwnd);
6161 if (hImc)
6162 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006163 /*
6164 * for Korean ime
6165 */
6166 HKL hKL = GetKeyboardLayout(0);
6167
6168 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6169 {
6170 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6171 static BOOL bSaved = FALSE;
6172
6173 if (active)
6174 {
6175 /* if we have a saved conversion status, restore it */
6176 if (bSaved)
6177 pImmSetConversionStatus(hImc, dwConversionSaved,
6178 dwSentenceSaved);
6179 bSaved = FALSE;
6180 }
6181 else
6182 {
6183 /* save conversion status and disable korean */
6184 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6185 &dwSentenceSaved))
6186 {
6187 bSaved = TRUE;
6188 pImmSetConversionStatus(hImc,
6189 dwConversionSaved & ~(IME_CMODE_NATIVE
6190 | IME_CMODE_FULLSHAPE),
6191 dwSentenceSaved);
6192 }
6193 }
6194 }
6195
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 pImmSetOpenStatus(hImc, active);
6197 pImmReleaseContext(s_hwnd, hImc);
6198 }
6199 }
6200}
6201
6202/*
6203 * Get IM status. When IM is on, return not 0. Else return 0.
6204 */
6205 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006206im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207{
6208 int status = 0;
6209 HIMC hImc;
6210
6211 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6212 {
6213 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6214 pImmReleaseContext(s_hwnd, hImc);
6215 }
6216 return status;
6217}
6218
6219#endif /* FEAT_MBYTE && FEAT_MBYTE_IME */
6220
6221#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
6222/* Win32 with GLOBAL IME */
6223
6224/*
6225 * Notify cursor position to IM.
6226 */
6227 void
6228im_set_position(int row, int col)
6229{
6230 /* Win32 with GLOBAL IME */
6231 POINT p;
6232
6233 p.x = FILL_X(col);
6234 p.y = FILL_Y(row);
6235 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
6236 global_ime_set_position(&p);
6237}
6238
6239/*
6240 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6241 */
6242 void
6243im_set_active(int active)
6244{
6245 global_ime_set_status(active);
6246}
6247
6248/*
6249 * Get IM status. When IM is on, return not 0. Else return 0.
6250 */
6251 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006252im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253{
6254 return global_ime_get_status();
6255}
6256#endif
6257
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006258#ifdef FEAT_MBYTE
6259/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006260 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006261 */
6262 static void
6263latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6264{
6265 int c;
6266
Bram Moolenaarca003e12006-03-17 23:19:38 +00006267 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006268 {
6269 c = *text++;
6270 switch (c)
6271 {
6272 case 0xa4: c = 0x20ac; break; /* euro */
6273 case 0xa6: c = 0x0160; break; /* S hat */
6274 case 0xa8: c = 0x0161; break; /* S -hat */
6275 case 0xb4: c = 0x017d; break; /* Z hat */
6276 case 0xb8: c = 0x017e; break; /* Z -hat */
6277 case 0xbc: c = 0x0152; break; /* OE */
6278 case 0xbd: c = 0x0153; break; /* oe */
6279 case 0xbe: c = 0x0178; break; /* Y */
6280 }
6281 *unicodebuf++ = c;
6282 }
6283}
6284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285
6286#ifdef FEAT_RIGHTLEFT
6287/*
6288 * What is this for? In the case where you are using Win98 or Win2K or later,
6289 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6290 * reverses the string sent to the TextOut... family. This sucks, because we
6291 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6292 * way to tell Windblows not to do this!
6293 *
6294 * The short of it is that this 'RevOut' only gets called if you are running
6295 * one of the new, "improved" MS OSes, and only if you are running in
6296 * 'rightleft' mode. It makes display take *slightly* longer, but not
6297 * noticeably so.
6298 */
6299 static void
6300RevOut( HDC s_hdc,
6301 int col,
6302 int row,
6303 UINT foptions,
6304 CONST RECT *pcliprect,
6305 LPCTSTR text,
6306 UINT len,
6307 CONST INT *padding)
6308{
6309 int ix;
6310 static int special = -1;
6311
6312 if (special == -1)
6313 {
6314 /* Check windows version: special treatment is needed if it is NT 5 or
6315 * Win98 or higher. */
6316 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
6317 && os_version.dwMajorVersion >= 5)
6318 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
6319 && (os_version.dwMajorVersion > 4
6320 || (os_version.dwMajorVersion == 4
6321 && os_version.dwMinorVersion > 0))))
6322 special = 1;
6323 else
6324 special = 0;
6325 }
6326
6327 if (special)
6328 for (ix = 0; ix < (int)len; ++ix)
6329 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6330 pcliprect, text + ix, 1, padding);
6331 else
6332 ExtTextOut(s_hdc, col, row, foptions, pcliprect, text, len, padding);
6333}
6334#endif
6335
6336 void
6337gui_mch_draw_string(
6338 int row,
6339 int col,
6340 char_u *text,
6341 int len,
6342 int flags)
6343{
6344 static int *padding = NULL;
6345 static int pad_size = 0;
6346 int i;
6347 const RECT *pcliprect = NULL;
6348 UINT foptions = 0;
6349#ifdef FEAT_MBYTE
6350 static WCHAR *unicodebuf = NULL;
6351 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006352 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 int n = 0;
6354#endif
6355 HPEN hpen, old_pen;
6356 int y;
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006357#ifdef FEAT_DIRECTX
6358 int font_is_ttf_or_vector = 0;
6359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 /*
6362 * Italic and bold text seems to have an extra row of pixels at the bottom
6363 * (below where the bottom of the character should be). If we draw the
6364 * characters with a solid background, the top row of pixels in the
6365 * character below will be overwritten. We can fix this by filling in the
6366 * background ourselves, to the correct character proportions, and then
6367 * writing the character in transparent mode. Still have a problem when
6368 * the character is "_", which gets written on to the character below.
6369 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6370 * pixel in their slots, which fixes the problem with the bottom row of
6371 * pixels. We still need this code because otherwise the top row of pixels
6372 * becomes a problem. - webb.
6373 */
6374 static HBRUSH hbr_cache[2] = {NULL, NULL};
6375 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6376 static int brush_lru = 0;
6377 HBRUSH hbr;
6378 RECT rc;
6379
6380 if (!(flags & DRAW_TRANSP))
6381 {
6382 /*
6383 * Clear background first.
6384 * Note: FillRect() excludes right and bottom of rectangle.
6385 */
6386 rc.left = FILL_X(col);
6387 rc.top = FILL_Y(row);
6388#ifdef FEAT_MBYTE
6389 if (has_mbyte)
6390 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006392 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 }
6394 else
6395#endif
6396 rc.right = FILL_X(col + len);
6397 rc.bottom = FILL_Y(row + 1);
6398
6399 /* Cache the created brush, that saves a lot of time. We need two:
6400 * one for cursor background and one for the normal background. */
6401 if (gui.currBgColor == brush_color[0])
6402 {
6403 hbr = hbr_cache[0];
6404 brush_lru = 1;
6405 }
6406 else if (gui.currBgColor == brush_color[1])
6407 {
6408 hbr = hbr_cache[1];
6409 brush_lru = 0;
6410 }
6411 else
6412 {
6413 if (hbr_cache[brush_lru] != NULL)
6414 DeleteBrush(hbr_cache[brush_lru]);
6415 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6416 brush_color[brush_lru] = gui.currBgColor;
6417 hbr = hbr_cache[brush_lru];
6418 brush_lru = !brush_lru;
6419 }
6420 FillRect(s_hdc, &rc, hbr);
6421
6422 SetBkMode(s_hdc, TRANSPARENT);
6423
6424 /*
6425 * When drawing block cursor, prevent inverted character spilling
6426 * over character cell (can happen with bold/italic)
6427 */
6428 if (flags & DRAW_CURSOR)
6429 {
6430 pcliprect = &rc;
6431 foptions = ETO_CLIPPED;
6432 }
6433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 SetTextColor(s_hdc, gui.currFgColor);
6435 SelectFont(s_hdc, gui.currFont);
6436
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006437#ifdef FEAT_DIRECTX
6438 if (IS_ENABLE_DIRECTX())
6439 {
6440 TEXTMETRIC tm;
6441
6442 GetTextMetrics(s_hdc, &tm);
6443 if (tm.tmPitchAndFamily & (TMPF_TRUETYPE | TMPF_VECTOR))
6444 {
6445 font_is_ttf_or_vector = 1;
6446 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
6447 }
6448 }
6449#endif
6450
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6452 {
6453 vim_free(padding);
6454 pad_size = Columns;
6455
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006456 /* Don't give an out-of-memory message here, it would call us
6457 * recursively. */
6458 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 if (padding != NULL)
6460 for (i = 0; i < pad_size; i++)
6461 padding[i] = gui.char_width;
6462 }
6463
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 /*
6465 * We have to provide the padding argument because italic and bold versions
6466 * of fixed-width fonts are often one pixel or so wider than their normal
6467 * versions.
6468 * No check for DRAW_BOLD, Windows will have done it already.
6469 */
6470
6471#ifdef FEAT_MBYTE
6472 /* Check if there are any UTF-8 characters. If not, use normal text
6473 * output to speed up output. */
6474 if (enc_utf8)
6475 for (n = 0; n < len; ++n)
6476 if (text[n] >= 0x80)
6477 break;
6478
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006479#if defined(FEAT_DIRECTX)
6480 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6481 * required that unicode drawing routine, currently. So this forces it
6482 * enabled. */
6483 if (enc_utf8 && IS_ENABLE_DIRECTX())
6484 n = 0; /* Keep n < len, to enter block for unicode. */
6485#endif
6486
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006488 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006490 if ((enc_utf8
6491 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6492 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 && (unicodebuf == NULL || len > unibuflen))
6494 {
6495 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006496 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497
6498 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006499 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500
6501 unibuflen = len;
6502 }
6503
6504 if (enc_utf8 && n < len && unicodebuf != NULL)
6505 {
6506 /* Output UTF-8 characters. Caller has already separated
6507 * composing characters. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006508 int i;
6509 int wlen; /* string length in words */
6510 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 int cells; /* cell width of string up to composing char */
6512 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006513 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006515 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006516 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006518 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006520 c = utf_ptr2char(text + i);
6521 if (c >= 0x10000)
6522 {
6523 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006524 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6525 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006526 }
6527 else
6528 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006529 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006530 }
6531 cw = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 if (cw > 2) /* don't use 4 for unprintable char */
6533 cw = 1;
6534 if (unicodepdy != NULL)
6535 {
6536 /* Use unicodepdy to make characters fit as we expect, even
6537 * when the font uses different widths (e.g., bold character
6538 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006539 if (c >= 0x10000)
6540 {
6541 unicodepdy[wlen - 2] = cw * gui.char_width;
6542 unicodepdy[wlen - 1] = 0;
6543 }
6544 else
6545 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 }
6547 cells += cw;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006548 i += utfc_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006549 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006551#if defined(FEAT_DIRECTX)
6552 if (IS_ENABLE_DIRECTX() && font_is_ttf_or_vector)
6553 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006554 /* Add one to "cells" for italics. */
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006555 DWriteContext_DrawText(s_dwc, s_hdc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006556 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006557 gui.char_width, gui.currFgColor);
6558 }
6559 else
6560#endif
6561 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6562 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 len = cells; /* used for underlining */
6564 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006565 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 {
6567 /* If we want to display codepage data, and the current CP is not the
6568 * ANSI one, we need to go via Unicode. */
6569 if (unicodebuf != NULL)
6570 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006571 if (enc_latin9)
6572 latin9_to_ucs(text, len, unicodebuf);
6573 else
6574 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 MB_PRECOMPOSED,
6576 (char *)text, len,
6577 (LPWSTR)unicodebuf, unibuflen);
6578 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006579 {
6580 /* Use unicodepdy to make characters fit as we expect, even
6581 * when the font uses different widths (e.g., bold character
6582 * is wider). */
6583 if (unicodepdy != NULL)
6584 {
6585 int i;
6586 int cw;
6587
6588 for (i = 0; i < len; ++i)
6589 {
6590 cw = utf_char2cells(unicodebuf[i]);
6591 if (cw > 2)
6592 cw = 1;
6593 unicodepdy[i] = cw * gui.char_width;
6594 }
6595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006597 foptions, pcliprect, unicodebuf, len, unicodepdy);
6598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 }
6600 }
6601 else
6602#endif
6603 {
6604#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006605 /* Windows will mess up RL text, so we have to draw it character by
6606 * character. Only do this if RL is on, since it's slow. */
6607 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6609 foptions, pcliprect, (char *)text, len, padding);
6610 else
6611#endif
6612 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6613 foptions, pcliprect, (char *)text, len, padding);
6614 }
6615
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006616 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 if (flags & DRAW_UNDERL)
6618 {
6619 hpen = CreatePen(PS_SOLID, 1, gui.currFgColor);
6620 old_pen = SelectObject(s_hdc, hpen);
6621 /* When p_linespace is 0, overwrite the bottom row of pixels.
6622 * Otherwise put the line just below the character. */
6623 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 if (p_linespace > 1)
6625 y -= p_linespace - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 MoveToEx(s_hdc, FILL_X(col), y, NULL);
6627 /* Note: LineTo() excludes the last pixel in the line. */
6628 LineTo(s_hdc, FILL_X(col + len), y);
6629 DeleteObject(SelectObject(s_hdc, old_pen));
6630 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006631
6632 /* Undercurl */
6633 if (flags & DRAW_UNDERC)
6634 {
6635 int x;
6636 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006637 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006638
6639 y = FILL_Y(row + 1) - 1;
6640 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6641 {
6642 offset = val[x % 8];
6643 SetPixel(s_hdc, x, y - offset, gui.currSpColor);
6644 }
6645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646}
6647
6648
6649/*
6650 * Output routines.
6651 */
6652
6653/* Flush any output to the screen */
6654 void
6655gui_mch_flush(void)
6656{
6657# if defined(__BORLANDC__)
6658 /*
6659 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6660 * prototype declaration.
6661 * The compiler complains if __stdcall is not used in both declarations.
6662 */
6663 BOOL __stdcall GdiFlush(void);
6664# endif
6665
6666 GdiFlush();
6667}
6668
6669 static void
6670clear_rect(RECT *rcp)
6671{
6672 HBRUSH hbr;
6673
6674 hbr = CreateSolidBrush(gui.back_pixel);
6675 FillRect(s_hdc, rcp, hbr);
6676 DeleteBrush(hbr);
6677}
6678
6679
Bram Moolenaarc716c302006-01-21 22:12:51 +00006680 void
6681gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6682{
6683 RECT workarea_rect;
6684
6685 get_work_area(&workarea_rect);
6686
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006687 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006688 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006689 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006690
6691 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6692 * the menubar for MSwin, we subtract it from the screen height, so that
6693 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006694 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006695 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006696 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006697 - GetSystemMetrics(SM_CYCAPTION)
6698#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006699 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006700#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006701 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006702}
6703
6704
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705#if defined(FEAT_MENU) || defined(PROTO)
6706/*
6707 * Add a sub menu to the menu bar.
6708 */
6709 void
6710gui_mch_add_menu(
6711 vimmenu_T *menu,
6712 int pos)
6713{
6714 vimmenu_T *parent = menu->parent;
6715
6716 menu->submenu_id = CreatePopupMenu();
6717 menu->id = s_menu_id++;
6718
6719 if (menu_is_menubar(menu->name))
6720 {
6721 if (is_winnt_3())
6722 {
6723 InsertMenu((parent == NULL) ? s_menuBar : parent->submenu_id,
6724 (UINT)pos, MF_POPUP | MF_STRING | MF_BYPOSITION,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006725 (long_u)menu->submenu_id, (LPCTSTR) menu->name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 }
6727 else
6728 {
6729#ifdef FEAT_MBYTE
6730 WCHAR *wn = NULL;
6731 int n;
6732
6733 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6734 {
6735 /* 'encoding' differs from active codepage: convert menu name
6736 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006737 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 if (wn != NULL)
6739 {
6740 MENUITEMINFOW infow;
6741
6742 infow.cbSize = sizeof(infow);
6743 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6744 | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006745 infow.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 infow.wID = menu->id;
6747 infow.fType = MFT_STRING;
6748 infow.dwTypeData = wn;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006749 infow.cch = (UINT)wcslen(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 infow.hSubMenu = menu->submenu_id;
6751 n = InsertMenuItemW((parent == NULL)
6752 ? s_menuBar : parent->submenu_id,
6753 (UINT)pos, TRUE, &infow);
6754 vim_free(wn);
6755 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
6756 /* Failed, try using non-wide function. */
6757 wn = NULL;
6758 }
6759 }
6760
6761 if (wn == NULL)
6762#endif
6763 {
6764 MENUITEMINFO info;
6765
6766 info.cbSize = sizeof(info);
6767 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006768 info.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 info.wID = menu->id;
6770 info.fType = MFT_STRING;
6771 info.dwTypeData = (LPTSTR)menu->name;
6772 info.cch = (UINT)STRLEN(menu->name);
6773 info.hSubMenu = menu->submenu_id;
6774 InsertMenuItem((parent == NULL)
6775 ? s_menuBar : parent->submenu_id,
6776 (UINT)pos, TRUE, &info);
6777 }
6778 }
6779 }
6780
6781 /* Fix window size if menu may have wrapped */
6782 if (parent == NULL)
6783 gui_mswin_get_menu_height(!gui.starting);
6784#ifdef FEAT_TEAROFF
6785 else if (IsWindow(parent->tearoff_handle))
6786 rebuild_tearoff(parent);
6787#endif
6788}
6789
6790 void
6791gui_mch_show_popupmenu(vimmenu_T *menu)
6792{
6793 POINT mp;
6794
6795 (void)GetCursorPos((LPPOINT)&mp);
6796 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6797}
6798
6799 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006800gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801{
6802 vimmenu_T *menu = gui_find_menu(path_name);
6803
6804 if (menu != NULL)
6805 {
6806 POINT p;
6807
6808 /* Find the position of the current cursor */
6809 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006810 if (mouse_pos)
6811 {
6812 int mx, my;
6813
6814 gui_mch_getmouse(&mx, &my);
6815 p.x += mx;
6816 p.y += my;
6817 }
6818 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 {
6820 p.x += TEXT_X(W_WINCOL(curwin) + curwin->w_wcol + 1);
6821 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6822 }
6823 msg_scroll = FALSE;
6824 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6825 }
6826}
6827
6828#if defined(FEAT_TEAROFF) || defined(PROTO)
6829/*
6830 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6831 * create it as a pseudo-"tearoff menu".
6832 */
6833 void
6834gui_make_tearoff(char_u *path_name)
6835{
6836 vimmenu_T *menu = gui_find_menu(path_name);
6837
6838 /* Found the menu, so tear it off. */
6839 if (menu != NULL)
6840 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6841}
6842#endif
6843
6844/*
6845 * Add a menu item to a menu
6846 */
6847 void
6848gui_mch_add_menu_item(
6849 vimmenu_T *menu,
6850 int idx)
6851{
6852 vimmenu_T *parent = menu->parent;
6853
6854 menu->id = s_menu_id++;
6855 menu->submenu_id = NULL;
6856
6857#ifdef FEAT_TEAROFF
6858 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6859 {
6860 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6861 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6862 }
6863 else
6864#endif
6865#ifdef FEAT_TOOLBAR
6866 if (menu_is_toolbar(parent->name))
6867 {
6868 TBBUTTON newtb;
6869
6870 vim_memset(&newtb, 0, sizeof(newtb));
6871 if (menu_is_separator(menu->name))
6872 {
6873 newtb.iBitmap = 0;
6874 newtb.fsStyle = TBSTYLE_SEP;
6875 }
6876 else
6877 {
6878 newtb.iBitmap = get_toolbar_bitmap(menu);
6879 newtb.fsStyle = TBSTYLE_BUTTON;
6880 }
6881 newtb.idCommand = menu->id;
6882 newtb.fsState = TBSTATE_ENABLED;
6883 newtb.iString = 0;
6884 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6885 (LPARAM)&newtb);
6886 menu->submenu_id = (HMENU)-1;
6887 }
6888 else
6889#endif
6890 {
6891#ifdef FEAT_MBYTE
6892 WCHAR *wn = NULL;
6893 int n;
6894
6895 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6896 {
6897 /* 'encoding' differs from active codepage: convert menu item name
6898 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006899 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 if (wn != NULL)
6901 {
6902 n = InsertMenuW(parent->submenu_id, (UINT)idx,
6903 (menu_is_separator(menu->name)
6904 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6905 (UINT)menu->id, wn);
6906 vim_free(wn);
6907 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
6908 /* Failed, try using non-wide function. */
6909 wn = NULL;
6910 }
6911 }
6912 if (wn == NULL)
6913#endif
6914 InsertMenu(parent->submenu_id, (UINT)idx,
6915 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
6916 | MF_BYPOSITION,
6917 (UINT)menu->id, (LPCTSTR)menu->name);
6918#ifdef FEAT_TEAROFF
6919 if (IsWindow(parent->tearoff_handle))
6920 rebuild_tearoff(parent);
6921#endif
6922 }
6923}
6924
6925/*
6926 * Destroy the machine specific menu widget.
6927 */
6928 void
6929gui_mch_destroy_menu(vimmenu_T *menu)
6930{
6931#ifdef FEAT_TOOLBAR
6932 /*
6933 * is this a toolbar button?
6934 */
6935 if (menu->submenu_id == (HMENU)-1)
6936 {
6937 int iButton;
6938
6939 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6940 (WPARAM)menu->id, 0);
6941 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6942 }
6943 else
6944#endif
6945 {
6946 if (menu->parent != NULL
6947 && menu_is_popup(menu->parent->dname)
6948 && menu->parent->submenu_id != NULL)
6949 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6950 else
6951 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6952 if (menu->submenu_id != NULL)
6953 DestroyMenu(menu->submenu_id);
6954#ifdef FEAT_TEAROFF
6955 if (IsWindow(menu->tearoff_handle))
6956 DestroyWindow(menu->tearoff_handle);
6957 if (menu->parent != NULL
6958 && menu->parent->children != NULL
6959 && IsWindow(menu->parent->tearoff_handle))
6960 {
6961 /* This menu must not show up when rebuilding the tearoff window. */
6962 menu->modes = 0;
6963 rebuild_tearoff(menu->parent);
6964 }
6965#endif
6966 }
6967}
6968
6969#ifdef FEAT_TEAROFF
6970 static void
6971rebuild_tearoff(vimmenu_T *menu)
6972{
6973 /*hackish*/
6974 char_u tbuf[128];
6975 RECT trect;
6976 RECT rct;
6977 RECT roct;
6978 int x, y;
6979
6980 HWND thwnd = menu->tearoff_handle;
6981
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006982 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 if (GetWindowRect(thwnd, &trect)
6984 && GetWindowRect(s_hwnd, &rct)
6985 && GetClientRect(s_hwnd, &roct))
6986 {
6987 x = trect.left - rct.left;
6988 y = (trect.top - rct.bottom + roct.bottom);
6989 }
6990 else
6991 {
6992 x = y = 0xffffL;
6993 }
6994 DestroyWindow(thwnd);
6995 if (menu->children != NULL)
6996 {
6997 gui_mch_tearoff(tbuf, menu, x, y);
6998 if (IsWindow(menu->tearoff_handle))
6999 (void) SetWindowPos(menu->tearoff_handle,
7000 NULL,
7001 (int)trect.left,
7002 (int)trect.top,
7003 0, 0,
7004 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
7005 }
7006}
7007#endif /* FEAT_TEAROFF */
7008
7009/*
7010 * Make a menu either grey or not grey.
7011 */
7012 void
7013gui_mch_menu_grey(
7014 vimmenu_T *menu,
7015 int grey)
7016{
7017#ifdef FEAT_TOOLBAR
7018 /*
7019 * is this a toolbar button?
7020 */
7021 if (menu->submenu_id == (HMENU)-1)
7022 {
7023 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
7024 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
7025 }
7026 else
7027#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02007028 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
7029 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030
7031#ifdef FEAT_TEAROFF
7032 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
7033 {
7034 WORD menuID;
7035 HWND menuHandle;
7036
7037 /*
7038 * A tearoff button has changed state.
7039 */
7040 if (menu->children == NULL)
7041 menuID = (WORD)(menu->id);
7042 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007043 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
7045 if (menuHandle)
7046 EnableWindow(menuHandle, !grey);
7047
7048 }
7049#endif
7050}
7051
7052#endif /* FEAT_MENU */
7053
7054
7055/* define some macros used to make the dialogue creation more readable */
7056
7057#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
7058#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007059#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060
7061#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
7062/*
7063 * stuff for dialogs
7064 */
7065
7066/*
7067 * The callback routine used by all the dialogs. Very simple. First,
7068 * acknowledges the INITDIALOG message so that Windows knows to do standard
7069 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
7070 * pressed, return that button's ID - IDCANCEL (2), which is the button's
7071 * number.
7072 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007073/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 static LRESULT CALLBACK
7075dialog_callback(
7076 HWND hwnd,
7077 UINT message,
7078 WPARAM wParam,
7079 LPARAM lParam)
7080{
7081 if (message == WM_INITDIALOG)
7082 {
7083 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
7084 /* Set focus to the dialog. Set the default button, if specified. */
7085 (void)SetFocus(hwnd);
7086 if (dialog_default_button > IDCANCEL)
7087 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007088 else
7089 /* We don't have a default, set focus on another element of the
7090 * dialog window, probably the icon */
7091 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 return FALSE;
7093 }
7094
7095 if (message == WM_COMMAND)
7096 {
7097 int button = LOWORD(wParam);
7098
7099 /* Don't end the dialog if something was selected that was
7100 * not a button.
7101 */
7102 if (button >= DLG_NONBUTTON_CONTROL)
7103 return TRUE;
7104
7105 /* If the edit box exists, copy the string. */
7106 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007107 {
7108# if defined(FEAT_MBYTE) && defined(WIN3264)
7109 /* If the OS is Windows NT, and 'encoding' differs from active
7110 * codepage: use wide function and convert text. */
7111 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
7112 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007113 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007114 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
7115 char_u *p;
7116
7117 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
7118 p = utf16_to_enc(wp, NULL);
7119 vim_strncpy(s_textfield, p, IOSIZE);
7120 vim_free(p);
7121 vim_free(wp);
7122 }
7123 else
7124# endif
7125 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007126 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128
7129 /*
7130 * Need to check for IDOK because if the user just hits Return to
7131 * accept the default value, some reason this is what we get.
7132 */
7133 if (button == IDOK)
7134 {
7135 if (dialog_default_button > IDCANCEL)
7136 EndDialog(hwnd, dialog_default_button);
7137 }
7138 else
7139 EndDialog(hwnd, button - IDCANCEL);
7140 return TRUE;
7141 }
7142
7143 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7144 {
7145 EndDialog(hwnd, 0);
7146 return TRUE;
7147 }
7148 return FALSE;
7149}
7150
7151/*
7152 * Create a dialog dynamically from the parameter strings.
7153 * type = type of dialog (question, alert, etc.)
7154 * title = dialog title. may be NULL for default title.
7155 * message = text to display. Dialog sizes to accommodate it.
7156 * buttons = '\n' separated list of button captions, default first.
7157 * dfltbutton = number of default button.
7158 *
7159 * This routine returns 1 if the first button is pressed,
7160 * 2 for the second, etc.
7161 *
7162 * 0 indicates Esc was pressed.
7163 * -1 for unexpected error
7164 *
7165 * If stubbing out this fn, return 1.
7166 */
7167
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007168static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169{
7170 "IDR_VIM",
7171 "IDR_VIM_ERROR",
7172 "IDR_VIM_ALERT",
7173 "IDR_VIM_INFO",
7174 "IDR_VIM_QUESTION"
7175};
7176
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 int
7178gui_mch_dialog(
7179 int type,
7180 char_u *title,
7181 char_u *message,
7182 char_u *buttons,
7183 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007184 char_u *textfield,
7185 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186{
7187 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007188 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 int numButtons;
7190 int *buttonWidths, *buttonPositions;
7191 int buttonYpos;
7192 int nchar, i;
7193 DWORD lStyle;
7194 int dlgwidth = 0;
7195 int dlgheight;
7196 int editboxheight;
7197 int horizWidth = 0;
7198 int msgheight;
7199 char_u *pstart;
7200 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007201 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 char_u *tbuffer;
7203 RECT rect;
7204 HWND hwnd;
7205 HDC hdc;
7206 HFONT font, oldFont;
7207 TEXTMETRIC fontInfo;
7208 int fontHeight;
7209 int textWidth, minButtonWidth, messageWidth;
7210 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007211 int maxDialogHeight;
7212 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 int vertical;
7214 int dlgPaddingX;
7215 int dlgPaddingY;
7216#ifdef USE_SYSMENU_FONT
7217 LOGFONT lfSysmenu;
7218 int use_lfSysmenu = FALSE;
7219#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007220 garray_T ga;
7221 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222
7223#ifndef NO_CONSOLE
7224 /* Don't output anything in silent mode ("ex -s") */
7225 if (silent_mode)
7226 return dfltbutton; /* return default option */
7227#endif
7228
Bram Moolenaar748bf032005-02-02 23:04:36 +00007229 if (s_hwnd == NULL)
7230 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231
7232 if ((type < 0) || (type > VIM_LAST_TYPE))
7233 type = 0;
7234
7235 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007236 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007237 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007238 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239
7240 if (p == NULL)
7241 return -1;
7242
7243 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007244 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7246 * const.
7247 */
7248 tbuffer = vim_strsave(buttons);
7249 if (tbuffer == NULL)
7250 return -1;
7251
7252 --dfltbutton; /* Change from one-based to zero-based */
7253
7254 /* Count buttons */
7255 numButtons = 1;
7256 for (i = 0; tbuffer[i] != '\0'; i++)
7257 {
7258 if (tbuffer[i] == DLG_BUTTON_SEP)
7259 numButtons++;
7260 }
7261 if (dfltbutton >= numButtons)
7262 dfltbutton = -1;
7263
7264 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007265 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 if (buttonWidths == NULL)
7267 return -1;
7268
7269 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007270 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 if (buttonPositions == NULL)
7272 return -1;
7273
7274 /*
7275 * Calculate how big the dialog must be.
7276 */
7277 hwnd = GetDesktopWindow();
7278 hdc = GetWindowDC(hwnd);
7279#ifdef USE_SYSMENU_FONT
7280 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7281 {
7282 font = CreateFontIndirect(&lfSysmenu);
7283 use_lfSysmenu = TRUE;
7284 }
7285 else
7286#endif
7287 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7288 VARIABLE_PITCH , DLG_FONT_NAME);
7289 if (s_usenewlook)
7290 {
7291 oldFont = SelectFont(hdc, font);
7292 dlgPaddingX = DLG_PADDING_X;
7293 dlgPaddingY = DLG_PADDING_Y;
7294 }
7295 else
7296 {
7297 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7298 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7299 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7300 }
7301 GetTextMetrics(hdc, &fontInfo);
7302 fontHeight = fontInfo.tmHeight;
7303
7304 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007305 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306
7307 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007308 if (s_hwnd == NULL)
7309 {
7310 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311
Bram Moolenaarc716c302006-01-21 22:12:51 +00007312 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007313 get_work_area(&workarea_rect);
7314 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7315 if (maxDialogWidth > 600)
7316 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007317 /* Leave some room for the taskbar. */
7318 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007319 }
7320 else
7321 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02007322 /* Use our own window for the size, unless it's very small. */
7323 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007324 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02007325 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007326 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007327 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
7328 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007329
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007330 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007331 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007332 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02007333 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007334 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
7335 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
7336 }
7337
7338 /* Set dlgwidth to width of message.
7339 * Copy the message into "ga", changing NL to CR-NL and inserting line
7340 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 pstart = message;
7342 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007343 msgheight = 0;
7344 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 do
7346 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007347 msgheight += fontHeight; /* at least one line */
7348
7349 /* Need to figure out where to break the string. The system does it
7350 * at a word boundary, which would mean we can't compute the number of
7351 * wrapped lines. */
7352 textWidth = 0;
7353 last_white = NULL;
7354 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007355 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007356#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007357 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007358#else
7359 l = 1;
7360#endif
7361 if (l == 1 && vim_iswhite(*pend)
7362 && textWidth > maxDialogWidth * 3 / 4)
7363 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007364 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007365 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007366 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007367 /* Line will wrap. */
7368 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007369 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007370 textWidth = 0;
7371
7372 if (last_white != NULL)
7373 {
7374 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007375 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007376 pend = last_white + 1;
7377 last_white = NULL;
7378 }
7379 ga_append(&ga, '\r');
7380 ga_append(&ga, '\n');
7381 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007382 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007383
7384 while (--l >= 0)
7385 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007386 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007387 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007389
7390 ga_append(&ga, '\r');
7391 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 pstart = pend + 1;
7393 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007394
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007395 if (ga.ga_data != NULL)
7396 message = ga.ga_data;
7397
Bram Moolenaar748bf032005-02-02 23:04:36 +00007398 messageWidth += 10; /* roundoff space */
7399
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02007401 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7402 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403
7404 if (msgheight < DLG_ICON_HEIGHT)
7405 msgheight = DLG_ICON_HEIGHT;
7406
7407 /*
7408 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007409 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007411 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 if (!vertical)
7413 {
7414 // Place buttons horizontally if they fit.
7415 horizWidth = dlgPaddingX;
7416 pstart = tbuffer;
7417 i = 0;
7418 do
7419 {
7420 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7421 if (pend == NULL)
7422 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007423 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 if (textWidth < minButtonWidth)
7425 textWidth = minButtonWidth;
7426 textWidth += dlgPaddingX; /* Padding within button */
7427 buttonWidths[i] = textWidth;
7428 buttonPositions[i++] = horizWidth;
7429 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7430 pstart = pend + 1;
7431 } while (*pend != NUL);
7432
7433 if (horizWidth > maxDialogWidth)
7434 vertical = TRUE; // Too wide to fit on the screen.
7435 else if (horizWidth > dlgwidth)
7436 dlgwidth = horizWidth;
7437 }
7438
7439 if (vertical)
7440 {
7441 // Stack buttons vertically.
7442 pstart = tbuffer;
7443 do
7444 {
7445 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7446 if (pend == NULL)
7447 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007448 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 textWidth += dlgPaddingX; /* Padding within button */
7450 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7451 if (textWidth > dlgwidth)
7452 dlgwidth = textWidth;
7453 pstart = pend + 1;
7454 } while (*pend != NUL);
7455 }
7456
7457 if (dlgwidth < DLG_MIN_WIDTH)
7458 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7459
7460 /* start to fill in the dlgtemplate information. addressing by WORDs */
7461 if (s_usenewlook)
7462 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7463 else
7464 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7465
7466 add_long(lStyle);
7467 add_long(0); // (lExtendedStyle)
7468 pnumitems = p; /*save where the number of items must be stored*/
7469 add_word(0); // NumberOfItems(will change later)
7470 add_word(10); // x
7471 add_word(10); // y
7472 add_word(PixelToDialogX(dlgwidth)); // cx
7473
7474 // Dialog height.
7475 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007476 dlgheight = msgheight + 2 * dlgPaddingY
7477 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 else
7479 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7480
7481 // Dialog needs to be taller if contains an edit box.
7482 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7483 if (textfield != NULL)
7484 dlgheight += editboxheight;
7485
Bram Moolenaara95d8232013-08-07 15:27:11 +02007486 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7487 if (dlgheight > maxDialogHeight)
7488 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007489 msgheight = msgheight - (dlgheight - maxDialogHeight);
7490 dlgheight = maxDialogHeight;
7491 scroll_flag = WS_VSCROLL;
7492 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7493 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007494 }
7495
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 add_word(PixelToDialogY(dlgheight));
7497
7498 add_word(0); // Menu
7499 add_word(0); // Class
7500
7501 /* copy the title of the dialog */
7502 nchar = nCopyAnsiToWideChar(p, (title ?
7503 (LPSTR)title :
7504 (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
7505 p += nchar;
7506
7507 if (s_usenewlook)
7508 {
7509 /* do the font, since DS_3DLOOK doesn't work properly */
7510#ifdef USE_SYSMENU_FONT
7511 if (use_lfSysmenu)
7512 {
7513 /* point size */
7514 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7515 GetDeviceCaps(hdc, LOGPIXELSY));
7516 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
7517 }
7518 else
7519#endif
7520 {
7521 *p++ = DLG_FONT_POINT_SIZE; // point size
7522 nchar = nCopyAnsiToWideChar(p, TEXT(DLG_FONT_NAME));
7523 }
7524 p += nchar;
7525 }
7526
7527 buttonYpos = msgheight + 2 * dlgPaddingY;
7528
7529 if (textfield != NULL)
7530 buttonYpos += editboxheight;
7531
7532 pstart = tbuffer;
7533 if (!vertical)
7534 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7535 for (i = 0; i < numButtons; i++)
7536 {
7537 /* get end of this button. */
7538 for ( pend = pstart;
7539 *pend && (*pend != DLG_BUTTON_SEP);
7540 pend++)
7541 ;
7542
7543 if (*pend)
7544 *pend = '\0';
7545
7546 /*
7547 * old NOTE:
7548 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7549 * the focus to the first tab-able button and in so doing makes that
7550 * the default!! Grrr. Workaround: Make the default button the only
7551 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7552 * he/she can use arrow keys.
7553 *
7554 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007555 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 * dialog. Also needed for when the textfield is the default control.
7557 * It appears to work now (perhaps not on Win95?).
7558 */
7559 if (vertical)
7560 {
7561 p = add_dialog_element(p,
7562 (i == dfltbutton
7563 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7564 PixelToDialogX(DLG_VERT_PADDING_X),
7565 PixelToDialogY(buttonYpos /* TBK */
7566 + 2 * fontHeight * i),
7567 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7568 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007569 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 }
7571 else
7572 {
7573 p = add_dialog_element(p,
7574 (i == dfltbutton
7575 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7576 PixelToDialogX(horizWidth + buttonPositions[i]),
7577 PixelToDialogY(buttonYpos), /* TBK */
7578 PixelToDialogX(buttonWidths[i]),
7579 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007580 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 }
7582 pstart = pend + 1; /*next button*/
7583 }
7584 *pnumitems += numButtons;
7585
7586 /* Vim icon */
7587 p = add_dialog_element(p, SS_ICON,
7588 PixelToDialogX(dlgPaddingX),
7589 PixelToDialogY(dlgPaddingY),
7590 PixelToDialogX(DLG_ICON_WIDTH),
7591 PixelToDialogY(DLG_ICON_HEIGHT),
7592 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7593 dlg_icons[type]);
7594
Bram Moolenaar748bf032005-02-02 23:04:36 +00007595 /* Dialog message */
7596 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7597 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7598 PixelToDialogY(dlgPaddingY),
7599 (WORD)(PixelToDialogX(messageWidth) + 1),
7600 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007601 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602
7603 /* Edit box */
7604 if (textfield != NULL)
7605 {
7606 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7607 PixelToDialogX(2 * dlgPaddingX),
7608 PixelToDialogY(2 * dlgPaddingY + msgheight),
7609 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7610 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007611 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 *pnumitems += 1;
7613 }
7614
7615 *pnumitems += 2;
7616
7617 SelectFont(hdc, oldFont);
7618 DeleteObject(font);
7619 ReleaseDC(hwnd, hdc);
7620
7621 /* Let the dialog_callback() function know which button to make default
7622 * If we have an edit box, make that the default. We also need to tell
7623 * dialog_callback() if this dialog contains an edit box or not. We do
7624 * this by setting s_textfield if it does.
7625 */
7626 if (textfield != NULL)
7627 {
7628 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7629 s_textfield = textfield;
7630 }
7631 else
7632 {
7633 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7634 s_textfield = NULL;
7635 }
7636
7637 /* show the dialog box modally and get a return value */
7638 nchar = (int)DialogBoxIndirect(
7639 s_hinst,
7640 (LPDLGTEMPLATE)pdlgtemplate,
7641 s_hwnd,
7642 (DLGPROC)dialog_callback);
7643
7644 LocalFree(LocalHandle(pdlgtemplate));
7645 vim_free(tbuffer);
7646 vim_free(buttonWidths);
7647 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007648 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649
7650 /* Focus back to our window (for when MDI is used). */
7651 (void)SetFocus(s_hwnd);
7652
7653 return nchar;
7654}
7655
7656#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007657
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658/*
7659 * Put a simple element (basic class) onto a dialog template in memory.
7660 * return a pointer to where the next item should be added.
7661 *
7662 * parameters:
7663 * lStyle = additional style flags
7664 * (be careful, NT3.51 & Win32s will ignore the new ones)
7665 * x,y = x & y positions IN DIALOG UNITS
7666 * w,h = width and height IN DIALOG UNITS
7667 * Id = ID used in messages
7668 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7669 * caption = usually text or resource name
7670 *
7671 * TODO: use the length information noted here to enable the dialog creation
7672 * routines to work out more exactly how much memory they need to alloc.
7673 */
7674 static PWORD
7675add_dialog_element(
7676 PWORD p,
7677 DWORD lStyle,
7678 WORD x,
7679 WORD y,
7680 WORD w,
7681 WORD h,
7682 WORD Id,
7683 WORD clss,
7684 const char *caption)
7685{
7686 int nchar;
7687
7688 p = lpwAlign(p); /* Align to dword boundary*/
7689 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7690 *p++ = LOWORD(lStyle);
7691 *p++ = HIWORD(lStyle);
7692 *p++ = 0; // LOWORD (lExtendedStyle)
7693 *p++ = 0; // HIWORD (lExtendedStyle)
7694 *p++ = x;
7695 *p++ = y;
7696 *p++ = w;
7697 *p++ = h;
7698 *p++ = Id; //9 or 10 words in all
7699
7700 *p++ = (WORD)0xffff;
7701 *p++ = clss; //2 more here
7702
7703 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption); //strlen(caption)+1
7704 p += nchar;
7705
7706 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7707
7708 return p; //total = 15+ (strlen(caption)) words
7709 // = 30 + 2(strlen(caption) bytes reqd
7710}
7711
7712
7713/*
7714 * Helper routine. Take an input pointer, return closest pointer that is
7715 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7716 */
7717 static LPWORD
7718lpwAlign(
7719 LPWORD lpIn)
7720{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007721 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007723 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 ul += 3;
7725 ul >>= 2;
7726 ul <<= 2;
7727 return (LPWORD)ul;
7728}
7729
7730/*
7731 * Helper routine. Takes second parameter as Ansi string, copies it to first
7732 * parameter as wide character (16-bits / char) string, and returns integer
7733 * number of wide characters (words) in string (including the trailing wide
7734 * char NULL). Partly taken from the Win32SDK samples.
7735 */
7736 static int
7737nCopyAnsiToWideChar(
7738 LPWORD lpWCStr,
7739 LPSTR lpAnsiIn)
7740{
7741 int nChar = 0;
7742#ifdef FEAT_MBYTE
7743 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7744 int i;
7745 WCHAR *wn;
7746
7747 if (enc_codepage == 0 && (int)GetACP() != enc_codepage)
7748 {
7749 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007750 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 if (wn != NULL)
7752 {
7753 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007754 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 vim_free(wn);
7756 }
7757 }
7758 if (nChar == 0)
7759 /* Use Win32 conversion function. */
7760 nChar = MultiByteToWideChar(
7761 enc_codepage > 0 ? enc_codepage : CP_ACP,
7762 MB_PRECOMPOSED,
7763 lpAnsiIn, len,
7764 lpWCStr, len);
7765 for (i = 0; i < nChar; ++i)
7766 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7767 lpWCStr[i] = (WORD)' ';
7768#else
7769 do
7770 {
7771 if (*lpAnsiIn == '\t')
7772 *lpWCStr++ = (WORD)' ';
7773 else
7774 *lpWCStr++ = (WORD)*lpAnsiIn;
7775 nChar++;
7776 } while (*lpAnsiIn++);
7777#endif
7778
7779 return nChar;
7780}
7781
7782
7783#ifdef FEAT_TEAROFF
7784/*
7785 * The callback function for all the modeless dialogs that make up the
7786 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7787 * thinking its menus have been clicked), and go away when closed.
7788 */
7789 static LRESULT CALLBACK
7790tearoff_callback(
7791 HWND hwnd,
7792 UINT message,
7793 WPARAM wParam,
7794 LPARAM lParam)
7795{
7796 if (message == WM_INITDIALOG)
7797 return (TRUE);
7798
7799 /* May show the mouse pointer again. */
7800 HandleMouseHide(message, lParam);
7801
7802 if (message == WM_COMMAND)
7803 {
7804 if ((WORD)(LOWORD(wParam)) & 0x8000)
7805 {
7806 POINT mp;
7807 RECT rect;
7808
7809 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7810 {
7811 (void)TrackPopupMenu(
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007812 (HMENU)(long_u)(LOWORD(wParam) ^ 0x8000),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7814 (int)rect.right - 8,
7815 (int)mp.y,
7816 (int)0, /*reserved param*/
7817 s_hwnd,
7818 NULL);
7819 /*
7820 * NOTE: The pop-up menu can eat the mouse up event.
7821 * We deal with this in normal.c.
7822 */
7823 }
7824 }
7825 else
7826 /* Pass on messages to the main Vim window */
7827 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7828 /*
7829 * Give main window the focus back: this is so after
7830 * choosing a tearoff button you can start typing again
7831 * straight away.
7832 */
7833 (void)SetFocus(s_hwnd);
7834 return TRUE;
7835 }
7836 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7837 {
7838 DestroyWindow(hwnd);
7839 return TRUE;
7840 }
7841
7842 /* When moved around, give main window the focus back. */
7843 if (message == WM_EXITSIZEMOVE)
7844 (void)SetActiveWindow(s_hwnd);
7845
7846 return FALSE;
7847}
7848#endif
7849
7850
7851/*
7852 * Decide whether to use the "new look" (small, non-bold font) or the "old
7853 * look" (big, clanky font) for dialogs, and work out a few values for use
7854 * later accordingly.
7855 */
7856 static void
7857get_dialog_font_metrics(void)
7858{
7859 HDC hdc;
7860 HFONT hfontTools = 0;
7861 DWORD dlgFontSize;
7862 SIZE size;
7863#ifdef USE_SYSMENU_FONT
7864 LOGFONT lfSysmenu;
7865#endif
7866
7867 s_usenewlook = FALSE;
7868
7869 /*
7870 * For NT3.51 and Win32s, we stick with the old look
7871 * because it matches everything else.
7872 */
7873 if (!is_winnt_3())
7874 {
7875#ifdef USE_SYSMENU_FONT
7876 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7877 hfontTools = CreateFontIndirect(&lfSysmenu);
7878 else
7879#endif
7880 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7881 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7882
7883 if (hfontTools)
7884 {
7885 hdc = GetDC(s_hwnd);
7886 SelectObject(hdc, hfontTools);
7887 /*
7888 * GetTextMetrics() doesn't return the right value in
7889 * tmAveCharWidth, so we have to figure out the dialog base units
7890 * ourselves.
7891 */
7892 GetTextExtentPoint(hdc,
7893 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7894 52, &size);
7895 ReleaseDC(s_hwnd, hdc);
7896
7897 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7898 s_dlgfntheight = (WORD)size.cy;
7899 s_usenewlook = TRUE;
7900 }
7901 }
7902
7903 if (!s_usenewlook)
7904 {
7905 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7906 s_dlgfntwidth = LOWORD(dlgFontSize);
7907 s_dlgfntheight = HIWORD(dlgFontSize);
7908 }
7909}
7910
7911#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7912/*
7913 * Create a pseudo-"tearoff menu" based on the child
7914 * items of a given menu pointer.
7915 */
7916 static void
7917gui_mch_tearoff(
7918 char_u *title,
7919 vimmenu_T *menu,
7920 int initX,
7921 int initY)
7922{
7923 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7924 int template_len;
7925 int nchar, textWidth, submenuWidth;
7926 DWORD lStyle;
7927 DWORD lExtendedStyle;
7928 WORD dlgwidth;
7929 WORD menuID;
7930 vimmenu_T *pmenu;
7931 vimmenu_T *the_menu = menu;
7932 HWND hwnd;
7933 HDC hdc;
7934 HFONT font, oldFont;
7935 int col, spaceWidth, len;
7936 int columnWidths[2];
7937 char_u *label, *text;
7938 int acLen = 0;
7939 int nameLen;
7940 int padding0, padding1, padding2 = 0;
7941 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007942 int x;
7943 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944#ifdef USE_SYSMENU_FONT
7945 LOGFONT lfSysmenu;
7946 int use_lfSysmenu = FALSE;
7947#endif
7948
7949 /*
7950 * If this menu is already torn off, move it to the mouse position.
7951 */
7952 if (IsWindow(menu->tearoff_handle))
7953 {
7954 POINT mp;
7955 if (GetCursorPos((LPPOINT)&mp))
7956 {
7957 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7958 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7959 }
7960 return;
7961 }
7962
7963 /*
7964 * Create a new tearoff.
7965 */
7966 if (*title == MNU_HIDDEN_CHAR)
7967 title++;
7968
7969 /* Allocate memory to store the dialog template. It's made bigger when
7970 * needed. */
7971 template_len = DLG_ALLOC_SIZE;
7972 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7973 if (p == NULL)
7974 return;
7975
7976 hwnd = GetDesktopWindow();
7977 hdc = GetWindowDC(hwnd);
7978#ifdef USE_SYSMENU_FONT
7979 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7980 {
7981 font = CreateFontIndirect(&lfSysmenu);
7982 use_lfSysmenu = TRUE;
7983 }
7984 else
7985#endif
7986 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7987 VARIABLE_PITCH , DLG_FONT_NAME);
7988 if (s_usenewlook)
7989 oldFont = SelectFont(hdc, font);
7990 else
7991 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7992
7993 /* Calculate width of a single space. Used for padding columns to the
7994 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007995 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996
7997 /* Figure out max width of the text column, the accelerator column and the
7998 * optional submenu column. */
7999 submenuWidth = 0;
8000 for (col = 0; col < 2; col++)
8001 {
8002 columnWidths[col] = 0;
8003 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
8004 {
8005 /* Use "dname" here to compute the width of the visible text. */
8006 text = (col == 0) ? pmenu->dname : pmenu->actext;
8007 if (text != NULL && *text != NUL)
8008 {
8009 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
8010 if (textWidth > columnWidths[col])
8011 columnWidths[col] = textWidth;
8012 }
8013 if (pmenu->children != NULL)
8014 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
8015 }
8016 }
8017 if (columnWidths[1] == 0)
8018 {
8019 /* no accelerators */
8020 if (submenuWidth != 0)
8021 columnWidths[0] += submenuWidth;
8022 else
8023 columnWidths[0] += spaceWidth;
8024 }
8025 else
8026 {
8027 /* there is an accelerator column */
8028 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
8029 columnWidths[1] += submenuWidth;
8030 }
8031
8032 /*
8033 * Now find the total width of our 'menu'.
8034 */
8035 textWidth = columnWidths[0] + columnWidths[1];
8036 if (submenuWidth != 0)
8037 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008038 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
8040 textWidth += submenuWidth;
8041 }
8042 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
8043 if (textWidth > dlgwidth)
8044 dlgwidth = textWidth;
8045 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
8046
8047 /* W95 can't do thin dialogs, they look v. weird! */
8048 if (mch_windows95() && dlgwidth < TEAROFF_MIN_WIDTH)
8049 dlgwidth = TEAROFF_MIN_WIDTH;
8050
8051 /* start to fill in the dlgtemplate information. addressing by WORDs */
8052 if (s_usenewlook)
8053 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
8054 else
8055 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
8056
8057 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
8058 *p++ = LOWORD(lStyle);
8059 *p++ = HIWORD(lStyle);
8060 *p++ = LOWORD(lExtendedStyle);
8061 *p++ = HIWORD(lExtendedStyle);
8062 pnumitems = p; /* save where the number of items must be stored */
8063 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008064 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008066 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 else
8068 *p++ = PixelToDialogX(initX); // x
8069 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008070 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 else
8072 *p++ = PixelToDialogY(initY); // y
8073 *p++ = PixelToDialogX(dlgwidth); // cx
8074 ptrueheight = p;
8075 *p++ = 0; // dialog height: changed later anyway
8076 *p++ = 0; // Menu
8077 *p++ = 0; // Class
8078
8079 /* copy the title of the dialog */
8080 nchar = nCopyAnsiToWideChar(p, ((*title)
8081 ? (LPSTR)title
8082 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
8083 p += nchar;
8084
8085 if (s_usenewlook)
8086 {
8087 /* do the font, since DS_3DLOOK doesn't work properly */
8088#ifdef USE_SYSMENU_FONT
8089 if (use_lfSysmenu)
8090 {
8091 /* point size */
8092 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
8093 GetDeviceCaps(hdc, LOGPIXELSY));
8094 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
8095 }
8096 else
8097#endif
8098 {
8099 *p++ = DLG_FONT_POINT_SIZE; // point size
8100 nchar = nCopyAnsiToWideChar (p, TEXT(DLG_FONT_NAME));
8101 }
8102 p += nchar;
8103 }
8104
8105 /*
8106 * Loop over all the items in the menu.
8107 * But skip over the tearbar.
8108 */
8109 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
8110 menu = menu->children->next;
8111 else
8112 menu = menu->children;
8113 for ( ; menu != NULL; menu = menu->next)
8114 {
8115 if (menu->modes == 0) /* this menu has just been deleted */
8116 continue;
8117 if (menu_is_separator(menu->dname))
8118 {
8119 sepPadding += 3;
8120 continue;
8121 }
8122
8123 /* Check if there still is plenty of room in the template. Make it
8124 * larger when needed. */
8125 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8126 {
8127 WORD *newp;
8128
8129 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8130 if (newp != NULL)
8131 {
8132 template_len += 4096;
8133 mch_memmove(newp, pdlgtemplate,
8134 (char *)p - (char *)pdlgtemplate);
8135 p = newp + (p - pdlgtemplate);
8136 pnumitems = newp + (pnumitems - pdlgtemplate);
8137 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8138 LocalFree(LocalHandle(pdlgtemplate));
8139 pdlgtemplate = newp;
8140 }
8141 }
8142
8143 /* Figure out minimal length of this menu label. Use "name" for the
8144 * actual text, "dname" for estimating the displayed size. "name"
8145 * has "&a" for mnemonic and includes the accelerator. */
8146 len = nameLen = (int)STRLEN(menu->name);
8147 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8148 (int)STRLEN(menu->dname))) / spaceWidth;
8149 len += padding0;
8150
8151 if (menu->actext != NULL)
8152 {
8153 acLen = (int)STRLEN(menu->actext);
8154 len += acLen;
8155 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8156 }
8157 else
8158 textWidth = 0;
8159 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8160 len += padding1;
8161
8162 if (menu->children == NULL)
8163 {
8164 padding2 = submenuWidth / spaceWidth;
8165 len += padding2;
8166 menuID = (WORD)(menu->id);
8167 }
8168 else
8169 {
8170 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008171 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 }
8173
8174 /* Allocate menu label and fill it in */
8175 text = label = alloc((unsigned)len + 1);
8176 if (label == NULL)
8177 break;
8178
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008179 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 text = vim_strchr(text, TAB); /* stop at TAB before actext */
8181 if (text == NULL)
8182 text = label + nameLen; /* no actext, use whole name */
8183 while (padding0-- > 0)
8184 *text++ = ' ';
8185 if (menu->actext != NULL)
8186 {
8187 STRNCPY(text, menu->actext, acLen);
8188 text += acLen;
8189 }
8190 while (padding1-- > 0)
8191 *text++ = ' ';
8192 if (menu->children != NULL)
8193 {
8194 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8195 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8196 }
8197 else
8198 {
8199 while (padding2-- > 0)
8200 *text++ = ' ';
8201 }
8202 *text = NUL;
8203
8204 /*
8205 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8206 * W95/NT4 it makes the tear-off look more like a menu.
8207 */
8208 p = add_dialog_element(p,
8209 BS_PUSHBUTTON|BS_LEFT,
8210 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8211 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8212 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8213 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008214 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 vim_free(label);
8216 (*pnumitems)++;
8217 }
8218
8219 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8220
8221
8222 /* show modelessly */
8223 the_menu->tearoff_handle = CreateDialogIndirect(
8224 s_hinst,
8225 (LPDLGTEMPLATE)pdlgtemplate,
8226 s_hwnd,
8227 (DLGPROC)tearoff_callback);
8228
8229 LocalFree(LocalHandle(pdlgtemplate));
8230 SelectFont(hdc, oldFont);
8231 DeleteObject(font);
8232 ReleaseDC(hwnd, hdc);
8233
8234 /*
8235 * Reassert ourselves as the active window. This is so that after creating
8236 * a tearoff, the user doesn't have to click with the mouse just to start
8237 * typing again!
8238 */
8239 (void)SetActiveWindow(s_hwnd);
8240
8241 /* make sure the right buttons are enabled */
8242 force_menu_update = TRUE;
8243}
8244#endif
8245
8246#if defined(FEAT_TOOLBAR) || defined(PROTO)
8247#include "gui_w32_rc.h"
8248
8249/* This not defined in older SDKs */
8250# ifndef TBSTYLE_FLAT
8251# define TBSTYLE_FLAT 0x0800
8252# endif
8253
8254/*
8255 * Create the toolbar, initially unpopulated.
8256 * (just like the menu, there are no defaults, it's all
8257 * set up through menu.vim)
8258 */
8259 static void
8260initialise_toolbar(void)
8261{
8262 InitCommonControls();
8263 s_toolbarhwnd = CreateToolbarEx(
8264 s_hwnd,
8265 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8266 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008267 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 s_hinst,
8269 IDR_TOOLBAR1, // id of initial bitmap
8270 NULL,
8271 0, // initial number of buttons
8272 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8273 TOOLBAR_BUTTON_HEIGHT,
8274 TOOLBAR_BUTTON_WIDTH,
8275 TOOLBAR_BUTTON_HEIGHT,
8276 sizeof(TBBUTTON)
8277 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008278 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279
8280 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
8281}
8282
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008283 static LRESULT CALLBACK
8284toolbar_wndproc(
8285 HWND hwnd,
8286 UINT uMsg,
8287 WPARAM wParam,
8288 LPARAM lParam)
8289{
8290 HandleMouseHide(uMsg, lParam);
8291 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8292}
8293
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 static int
8295get_toolbar_bitmap(vimmenu_T *menu)
8296{
8297 int i = -1;
8298
8299 /*
8300 * Check user bitmaps first, unless builtin is specified.
8301 */
8302 if (!is_winnt_3() && !menu->icon_builtin)
8303 {
8304 char_u fname[MAXPATHL];
8305 HANDLE hbitmap = NULL;
8306
8307 if (menu->iconfile != NULL)
8308 {
8309 gui_find_iconfile(menu->iconfile, fname, "bmp");
8310 hbitmap = LoadImage(
8311 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008312 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 IMAGE_BITMAP,
8314 TOOLBAR_BUTTON_WIDTH,
8315 TOOLBAR_BUTTON_HEIGHT,
8316 LR_LOADFROMFILE |
8317 LR_LOADMAP3DCOLORS
8318 );
8319 }
8320
8321 /*
8322 * If the LoadImage call failed, or the "icon=" file
8323 * didn't exist or wasn't specified, try the menu name
8324 */
8325 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008326 && (gui_find_bitmap(
8327#ifdef FEAT_MULTI_LANG
8328 menu->en_dname != NULL ? menu->en_dname :
8329#endif
8330 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 hbitmap = LoadImage(
8332 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008333 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 IMAGE_BITMAP,
8335 TOOLBAR_BUTTON_WIDTH,
8336 TOOLBAR_BUTTON_HEIGHT,
8337 LR_LOADFROMFILE |
8338 LR_LOADMAP3DCOLORS
8339 );
8340
8341 if (hbitmap != NULL)
8342 {
8343 TBADDBITMAP tbAddBitmap;
8344
8345 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008346 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347
8348 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8349 (WPARAM)1, (LPARAM)&tbAddBitmap);
8350 /* i will be set to -1 if it fails */
8351 }
8352 }
8353 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8354 i = menu->iconidx;
8355
8356 return i;
8357}
8358#endif
8359
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008360#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8361 static void
8362initialise_tabline(void)
8363{
8364 InitCommonControls();
8365
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008366 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008367 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008368 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8369 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008370 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008371
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008372 gui.tabline_height = TABLINE_HEIGHT;
8373
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008374# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008375 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008376# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008377}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008378
8379 static LRESULT CALLBACK
8380tabline_wndproc(
8381 HWND hwnd,
8382 UINT uMsg,
8383 WPARAM wParam,
8384 LPARAM lParam)
8385{
8386 HandleMouseHide(uMsg, lParam);
8387 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8388}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008389#endif
8390
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8392/*
8393 * Make the GUI window come to the foreground.
8394 */
8395 void
8396gui_mch_set_foreground(void)
8397{
8398 if (IsIconic(s_hwnd))
8399 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8400 SetForegroundWindow(s_hwnd);
8401}
8402#endif
8403
8404#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8405 static void
8406dyn_imm_load(void)
8407{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008408 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 if (hLibImm == NULL)
8410 return;
8411
8412 pImmGetCompositionStringA
8413 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8414 pImmGetCompositionStringW
8415 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8416 pImmGetContext
8417 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8418 pImmAssociateContext
8419 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8420 pImmReleaseContext
8421 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8422 pImmGetOpenStatus
8423 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8424 pImmSetOpenStatus
8425 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
8426 pImmGetCompositionFont
8427 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
8428 pImmSetCompositionFont
8429 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
8430 pImmSetCompositionWindow
8431 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8432 pImmGetConversionStatus
8433 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008434 pImmSetConversionStatus
8435 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436
8437 if ( pImmGetCompositionStringA == NULL
8438 || pImmGetCompositionStringW == NULL
8439 || pImmGetContext == NULL
8440 || pImmAssociateContext == NULL
8441 || pImmReleaseContext == NULL
8442 || pImmGetOpenStatus == NULL
8443 || pImmSetOpenStatus == NULL
8444 || pImmGetCompositionFont == NULL
8445 || pImmSetCompositionFont == NULL
8446 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008447 || pImmGetConversionStatus == NULL
8448 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 {
8450 FreeLibrary(hLibImm);
8451 hLibImm = NULL;
8452 pImmGetContext = NULL;
8453 return;
8454 }
8455
8456 return;
8457}
8458
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459#endif
8460
8461#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8462
8463# ifdef FEAT_XPM_W32
8464# define IMAGE_XPM 100
8465# endif
8466
8467typedef struct _signicon_t
8468{
8469 HANDLE hImage;
8470 UINT uType;
8471#ifdef FEAT_XPM_W32
8472 HANDLE hShape; /* Mask bitmap handle */
8473#endif
8474} signicon_t;
8475
8476 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008477gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478{
8479 signicon_t *sign;
8480 int x, y, w, h;
8481
8482 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8483 return;
8484
8485 x = TEXT_X(col);
8486 y = TEXT_Y(row);
8487 w = gui.char_width * 2;
8488 h = gui.char_height;
8489 switch (sign->uType)
8490 {
8491 case IMAGE_BITMAP:
8492 {
8493 HDC hdcMem;
8494 HBITMAP hbmpOld;
8495
8496 hdcMem = CreateCompatibleDC(s_hdc);
8497 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8498 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8499 SelectObject(hdcMem, hbmpOld);
8500 DeleteDC(hdcMem);
8501 }
8502 break;
8503 case IMAGE_ICON:
8504 case IMAGE_CURSOR:
8505 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8506 break;
8507#ifdef FEAT_XPM_W32
8508 case IMAGE_XPM:
8509 {
8510 HDC hdcMem;
8511 HBITMAP hbmpOld;
8512
8513 hdcMem = CreateCompatibleDC(s_hdc);
8514 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8515 /* Make hole */
8516 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8517
8518 SelectObject(hdcMem, sign->hImage);
8519 /* Paint sign */
8520 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8521 SelectObject(hdcMem, hbmpOld);
8522 DeleteDC(hdcMem);
8523 }
8524 break;
8525#endif
8526 }
8527}
8528
8529 static void
8530close_signicon_image(signicon_t *sign)
8531{
8532 if (sign)
8533 switch (sign->uType)
8534 {
8535 case IMAGE_BITMAP:
8536 DeleteObject((HGDIOBJ)sign->hImage);
8537 break;
8538 case IMAGE_CURSOR:
8539 DestroyCursor((HCURSOR)sign->hImage);
8540 break;
8541 case IMAGE_ICON:
8542 DestroyIcon((HICON)sign->hImage);
8543 break;
8544#ifdef FEAT_XPM_W32
8545 case IMAGE_XPM:
8546 DeleteObject((HBITMAP)sign->hImage);
8547 DeleteObject((HBITMAP)sign->hShape);
8548 break;
8549#endif
8550 }
8551}
8552
8553 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008554gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555{
8556 signicon_t sign, *psign;
8557 char_u *ext;
8558
8559 if (is_winnt_3())
8560 {
8561 EMSG(_(e_signdata));
8562 return NULL;
8563 }
8564
8565 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008566 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 if (ext > signfile)
8568 {
8569 int do_load = 1;
8570
8571 if (!STRICMP(ext, ".bmp"))
8572 sign.uType = IMAGE_BITMAP;
8573 else if (!STRICMP(ext, ".ico"))
8574 sign.uType = IMAGE_ICON;
8575 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8576 sign.uType = IMAGE_CURSOR;
8577 else
8578 do_load = 0;
8579
8580 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008581 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 gui.char_width * 2, gui.char_height,
8583 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8584#ifdef FEAT_XPM_W32
8585 if (!STRICMP(ext, ".xpm"))
8586 {
8587 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008588 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8589 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 }
8591#endif
8592 }
8593
8594 psign = NULL;
8595 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8596 != NULL)
8597 *psign = sign;
8598
8599 if (!psign)
8600 {
8601 if (sign.hImage)
8602 close_signicon_image(&sign);
8603 EMSG(_(e_signdata));
8604 }
8605 return (void *)psign;
8606
8607}
8608
8609 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008610gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611{
8612 if (sign)
8613 {
8614 close_signicon_image((signicon_t *)sign);
8615 vim_free(sign);
8616 }
8617}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619
8620#if defined(FEAT_BEVAL) || defined(PROTO)
8621
8622/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008623 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 *
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00008625 * The only reused thing is gui_beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8627 * to get current mouse position).
8628 *
8629 * Trying to use as more Windows services as possible, and as less
8630 * IE version as possible :)).
8631 *
8632 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8633 * BalloonEval struct.
8634 * 2) Enable/Disable simply create/kill BalloonEval Timer
8635 * 3) When there was enough inactivity, timer procedure posts
8636 * async request to debugger
8637 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8638 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008639 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 */
8641
Bram Moolenaar45360022005-07-21 21:08:21 +00008642/*
8643 * determine whether installed Common Controls support multiline tooltips
8644 * (i.e. their version is >= 4.70
8645 */
8646 int
8647multiline_balloon_available(void)
8648{
8649 HINSTANCE hDll;
8650 static char comctl_dll[] = "comctl32.dll";
8651 static int multiline_tip = MAYBE;
8652
8653 if (multiline_tip != MAYBE)
8654 return multiline_tip;
8655
8656 hDll = GetModuleHandle(comctl_dll);
8657 if (hDll != NULL)
8658 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008659 DLLGETVERSIONPROC pGetVer;
8660 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008661
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008662 if (pGetVer != NULL)
8663 {
8664 DLLVERSIONINFO dvi;
8665 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008666
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008667 ZeroMemory(&dvi, sizeof(dvi));
8668 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008669
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008670 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008671
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008672 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008673 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008674 || (dvi.dwMajorVersion == 4
8675 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008676 {
8677 multiline_tip = TRUE;
8678 return multiline_tip;
8679 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008680 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008681 else
8682 {
8683 /* there is chance we have ancient CommCtl 4.70
8684 which doesn't export DllGetVersion */
8685 DWORD dwHandle = 0;
8686 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8687 if (len > 0)
8688 {
8689 VS_FIXEDFILEINFO *ver;
8690 UINT vlen = 0;
8691 void *data = alloc(len);
8692
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008693 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008694 && GetFileVersionInfo(comctl_dll, 0, len, data)
8695 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8696 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008697 && HIWORD(ver->dwFileVersionMS) > 4)
8698 || ((HIWORD(ver->dwFileVersionMS) == 4
8699 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008700 {
8701 vim_free(data);
8702 multiline_tip = TRUE;
8703 return multiline_tip;
8704 }
8705 vim_free(data);
8706 }
8707 }
8708 }
8709 multiline_tip = FALSE;
8710 return multiline_tip;
8711}
8712
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008714make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715{
Bram Moolenaar45360022005-07-21 21:08:21 +00008716 TOOLINFO *pti;
8717 int ToolInfoSize;
8718
8719 if (multiline_balloon_available() == TRUE)
8720 ToolInfoSize = sizeof(TOOLINFO_NEW);
8721 else
8722 ToolInfoSize = sizeof(TOOLINFO);
8723
8724 pti = (TOOLINFO *)alloc(ToolInfoSize);
8725 if (pti == NULL)
8726 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727
8728 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
8729 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8730 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8731 beval->target, NULL, s_hinst, NULL);
8732
8733 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8734 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8735
Bram Moolenaar45360022005-07-21 21:08:21 +00008736 pti->cbSize = ToolInfoSize;
8737 pti->uFlags = TTF_SUBCLASS;
8738 pti->hwnd = beval->target;
8739 pti->hinst = 0; /* Don't use string resources */
8740 pti->uId = ID_BEVAL_TOOLTIP;
8741
8742 if (multiline_balloon_available() == TRUE)
8743 {
8744 RECT rect;
8745 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
8746 pti->lpszText = LPSTR_TEXTCALLBACK;
8747 ptin->lParam = (LPARAM)text;
8748 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
8749 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8750 (LPARAM)rect.right);
8751 }
8752 else
8753 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754
8755 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00008756 pti->rect.left = pt.x - 3;
8757 pti->rect.top = pt.y - 3;
8758 pti->rect.right = pt.x + 3;
8759 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760
Bram Moolenaar45360022005-07-21 21:08:21 +00008761 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 /* Make tooltip appear sooner */
8763 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008764 /* I've performed some tests and it seems the longest possible life time
8765 * of tooltip is 30 seconds */
8766 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 /*
8768 * HACK: force tooltip to appear, because it'll not appear until
8769 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008770 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008772 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00008774 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775}
8776
8777 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008778delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008780 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781}
8782
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008783/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008785BevalTimerProc(
8786 HWND hwnd,
8787 UINT uMsg,
8788 UINT_PTR idEvent,
8789 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790{
8791 POINT pt;
8792 RECT rect;
8793
8794 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8795 return;
8796
8797 GetCursorPos(&pt);
8798 if (WindowFromPoint(pt) != s_textArea)
8799 return;
8800
8801 ScreenToClient(s_textArea, &pt);
8802 GetClientRect(s_textArea, &rect);
8803 if (!PtInRect(&rect, pt))
8804 return;
8805
8806 if (LastActivity > 0
8807 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8808 && (cur_beval->showState != ShS_PENDING
8809 || abs(cur_beval->x - pt.x) > 3
8810 || abs(cur_beval->y - pt.y) > 3))
8811 {
8812 /* Pointer resting in one place long enough, it's time to show
8813 * the tooltip. */
8814 cur_beval->showState = ShS_PENDING;
8815 cur_beval->x = pt.x;
8816 cur_beval->y = pt.y;
8817
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008818 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819
8820 if (cur_beval->msgCB != NULL)
8821 (*cur_beval->msgCB)(cur_beval, 0);
8822 }
8823}
8824
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008825/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008827gui_mch_disable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008829 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008831 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832}
8833
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008834/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008836gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008838 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 if (beval == NULL)
8840 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008841 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008842 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008843 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844}
8845
8846 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008847gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848{
8849 POINT pt;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008850 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 if (beval->showState == ShS_SHOWING)
8852 return;
8853 GetCursorPos(&pt);
8854 ScreenToClient(s_textArea, &pt);
8855
8856 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
8857 /* cursor is still here */
8858 {
8859 gui_mch_disable_beval_area(cur_beval);
8860 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008861 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008863 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864}
8865
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008866/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008868gui_mch_create_beval_area(
8869 void *target, /* ignored, always use s_textArea */
8870 char_u *mesg,
8871 void (*mesgCB)(BalloonEval *, int),
8872 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873{
8874 /* partially stolen from gui_beval.c */
8875 BalloonEval *beval;
8876
8877 if (mesg != NULL && mesgCB != NULL)
8878 {
8879 EMSG(_("E232: Cannot create BalloonEval with both message and callback"));
8880 return NULL;
8881 }
8882
8883 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
8884 if (beval != NULL)
8885 {
8886 beval->target = s_textArea;
8887 beval->balloon = NULL;
8888
8889 beval->showState = ShS_NEUTRAL;
8890 beval->x = 0;
8891 beval->y = 0;
8892 beval->msg = mesg;
8893 beval->msgCB = mesgCB;
8894 beval->clientData = clientData;
8895
8896 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 cur_beval = beval;
8898
8899 if (p_beval)
8900 gui_mch_enable_beval_area(beval);
8901
8902 }
8903 return beval;
8904}
8905
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008906/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 static void
Bram Moolenaar442b4222010-05-24 21:34:22 +02008908Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909{
8910 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8911 return;
8912
8913 if (cur_beval != NULL)
8914 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008915 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008917 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008918 // TRACE0("TTN_SHOW {{{");
8919 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008920 break;
8921 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008922 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 delete_tooltip(cur_beval);
8924 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008925 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926
8927 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008928 break;
8929 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008930 {
8931 /* if you get there then we have new common controls */
8932 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8933 info->lpszText = (LPSTR)info->lParam;
8934 info->uFlags |= TTF_DI_SETITEM;
8935 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008936 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 }
8938 }
8939}
8940
8941 static void
8942TrackUserActivity(UINT uMsg)
8943{
8944 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8945 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8946 LastActivity = GetTickCount();
8947}
8948
8949 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008950gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951{
8952 vim_free(beval);
8953}
8954#endif /* FEAT_BEVAL */
8955
8956#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8957/*
8958 * We have multiple signs to draw at the same location. Draw the
8959 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8960 */
8961 void
8962netbeans_draw_multisign_indicator(int row)
8963{
8964 int i;
8965 int y;
8966 int x;
8967
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008968 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008969 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008970
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 x = 0;
8972 y = TEXT_Y(row);
8973
8974 for (i = 0; i < gui.char_height - 3; i++)
8975 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8976
8977 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8978 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8979 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8980 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8981 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8982 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8983 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8984}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008985#endif