blob: 78db43e6b3ead5805ec1776ff79542d05af9a19a [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 */
2266 long_u
2267gui_mch_get_rgb(guicolor_T pixel)
2268{
2269 return (GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2270 + GetBValue(pixel);
2271}
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
4536/*
4537 * Return TRUE when running under Win32s.
4538 */
4539 int
4540gui_is_win32s(void)
4541{
4542 return (os_version.dwPlatformId == VER_PLATFORM_WIN32s);
4543}
4544
4545#ifdef FEAT_MENU
4546/*
4547 * Figure out how high the menu bar is at the moment.
4548 */
4549 static int
4550gui_mswin_get_menu_height(
4551 int fix_window) /* If TRUE, resize window if menu height changed */
4552{
4553 static int old_menu_height = -1;
4554
4555 RECT rc1, rc2;
4556 int num;
4557 int menu_height;
4558
4559 if (gui.menu_is_active)
4560 num = GetMenuItemCount(s_menuBar);
4561 else
4562 num = 0;
4563
4564 if (num == 0)
4565 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004566 else if (IsMinimized(s_hwnd))
4567 {
4568 /* The height of the menu cannot be determined while the window is
4569 * minimized. Take the previous height if the menu is changed in that
4570 * state, to avoid that Vim's vertical window size accidentally
4571 * increases due to the unaccounted-for menu height. */
4572 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 else
4575 {
4576 if (is_winnt_3()) /* for NT 3.xx */
4577 {
4578 if (gui.starting)
4579 menu_height = GetSystemMetrics(SM_CYMENU);
4580 else
4581 {
4582 RECT r1, r2;
4583 int frameht = GetSystemMetrics(SM_CYFRAME);
4584 int capht = GetSystemMetrics(SM_CYCAPTION);
4585
4586 /* get window rect of s_hwnd
4587 * get client rect of s_hwnd
4588 * get cap height
4589 * subtract from window rect, the sum of client height,
4590 * (if not maximized)frame thickness, and caption height.
4591 */
4592 GetWindowRect(s_hwnd, &r1);
4593 GetClientRect(s_hwnd, &r2);
4594 menu_height = r1.bottom - r1.top - (r2.bottom - r2.top
4595 + 2 * frameht * (!IsZoomed(s_hwnd)) + capht);
4596 }
4597 }
4598 else /* win95 and variants (NT 4.0, I guess) */
4599 {
4600 /*
4601 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4602 * seem to have been set yet, so menu wraps in default window
4603 * width which is very narrow. Instead just return height of a
4604 * single menu item. Will still be wrong when the menu really
4605 * should wrap over more than one line.
4606 */
4607 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4608 if (gui.starting)
4609 menu_height = rc1.bottom - rc1.top + 1;
4610 else
4611 {
4612 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4613 menu_height = rc2.bottom - rc1.top + 1;
4614 }
4615 }
4616 }
4617
4618 if (fix_window && menu_height != old_menu_height)
4619 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004620 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 }
Bram Moolenaar71371b12015-03-24 17:57:12 +01004622 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623
4624 return menu_height;
4625}
4626#endif /*FEAT_MENU*/
4627
4628
4629/*
4630 * Setup for the Intellimouse
4631 */
4632 static void
4633init_mouse_wheel(void)
4634{
4635
4636#ifndef SPI_GETWHEELSCROLLLINES
4637# define SPI_GETWHEELSCROLLLINES 104
4638#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004639#ifndef SPI_SETWHEELSCROLLLINES
4640# define SPI_SETWHEELSCROLLLINES 105
4641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642
4643#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4644#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4645#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4646#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4647
4648 HWND hdl_mswheel;
4649 UINT msh_msgscrolllines;
4650
4651 msh_msgmousewheel = 0;
4652 mouse_scroll_lines = 3; /* reasonable default */
4653
4654 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
4655 && os_version.dwMajorVersion >= 4)
4656 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
4657 && ((os_version.dwMajorVersion == 4
4658 && os_version.dwMinorVersion >= 10)
4659 || os_version.dwMajorVersion >= 5)))
4660 {
4661 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4662 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4663 &mouse_scroll_lines, 0);
4664 }
4665 else if (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
4666 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
4667 && os_version.dwMajorVersion < 4))
4668 { /*
4669 * If Win95 or NT 3.51,
4670 * try to find the hidden point32 window.
4671 */
4672 hdl_mswheel = FindWindow(VMOUSEZ_CLASSNAME, VMOUSEZ_TITLE);
4673 if (hdl_mswheel)
4674 {
4675 msh_msgscrolllines = RegisterWindowMessage(VMSH_SCROLL_LINES);
4676 if (msh_msgscrolllines)
4677 {
4678 mouse_scroll_lines = (int)SendMessage(hdl_mswheel,
4679 msh_msgscrolllines, 0, 0);
4680 msh_msgmousewheel = RegisterWindowMessage(VMSH_MOUSEWHEEL);
4681 }
4682 }
4683 }
4684}
4685
4686
4687/* Intellimouse wheel handler */
4688 static void
4689_OnMouseWheel(
4690 HWND hwnd,
4691 short zDelta)
4692{
4693/* Treat a mouse wheel event as if it were a scroll request */
4694 int i;
4695 int size;
4696 HWND hwndCtl;
4697
4698 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4699 {
4700 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4701 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4702 }
4703 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4704 {
4705 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4706 size = curwin->w_scrollbars[SBAR_LEFT].size;
4707 }
4708 else
4709 return;
4710
4711 size = curwin->w_height;
4712 if (mouse_scroll_lines == 0)
4713 init_mouse_wheel();
4714
4715 if (mouse_scroll_lines > 0
4716 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4717 {
4718 for (i = mouse_scroll_lines; i > 0; --i)
4719 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4720 }
4721 else
4722 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
4723}
4724
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004725#ifdef USE_SYSMENU_FONT
4726/*
4727 * Get Menu Font.
4728 * Return OK or FAIL.
4729 */
4730 static int
4731gui_w32_get_menu_font(LOGFONT *lf)
4732{
4733 NONCLIENTMETRICS nm;
4734
4735 nm.cbSize = sizeof(NONCLIENTMETRICS);
4736 if (!SystemParametersInfo(
4737 SPI_GETNONCLIENTMETRICS,
4738 sizeof(NONCLIENTMETRICS),
4739 &nm,
4740 0))
4741 return FAIL;
4742 *lf = nm.lfMenuFont;
4743 return OK;
4744}
4745#endif
4746
4747
4748#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4749/*
4750 * Set the GUI tabline font to the system menu font
4751 */
4752 static void
4753set_tabline_font(void)
4754{
4755 LOGFONT lfSysmenu;
4756 HFONT font;
4757 HWND hwnd;
4758 HDC hdc;
4759 HFONT hfntOld;
4760 TEXTMETRIC tm;
4761
4762 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4763 return;
4764
4765 font = CreateFontIndirect(&lfSysmenu);
4766
4767 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4768
4769 /*
4770 * Compute the height of the font used for the tab text
4771 */
4772 hwnd = GetDesktopWindow();
4773 hdc = GetWindowDC(hwnd);
4774 hfntOld = SelectFont(hdc, font);
4775
4776 GetTextMetrics(hdc, &tm);
4777
4778 SelectFont(hdc, hfntOld);
4779 ReleaseDC(hwnd, hdc);
4780
4781 /*
4782 * The space used by the tab border and the space between the tab label
4783 * and the tab border is included as 7.
4784 */
4785 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4786}
4787#endif
4788
Bram Moolenaar520470a2005-06-16 21:59:56 +00004789/*
4790 * Invoked when a setting was changed.
4791 */
4792 static LRESULT CALLBACK
4793_OnSettingChange(UINT n)
4794{
4795 if (n == SPI_SETWHEELSCROLLLINES)
4796 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4797 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004798#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4799 if (n == SPI_SETNONCLIENTMETRICS)
4800 set_tabline_font();
4801#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004802 return 0;
4803}
4804
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805#ifdef FEAT_NETBEANS_INTG
4806 static void
4807_OnWindowPosChanged(
4808 HWND hwnd,
4809 const LPWINDOWPOS lpwpos)
4810{
4811 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004812 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
4814 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4815 || lpwpos->cx != cx || lpwpos->cy != cy))
4816 {
4817 x = lpwpos->x;
4818 y = lpwpos->y;
4819 cx = lpwpos->cx;
4820 cy = lpwpos->cy;
4821 netbeans_frame_moved(x, y);
4822 }
4823 /* Allow to send WM_SIZE and WM_MOVE */
4824 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4825}
4826#endif
4827
4828 static int
4829_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 UINT fwSide,
4831 LPRECT lprc)
4832{
4833 int w, h;
4834 int valid_w, valid_h;
4835 int w_offset, h_offset;
4836
4837 w = lprc->right - lprc->left;
4838 h = lprc->bottom - lprc->top;
4839 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4840 w_offset = w - valid_w;
4841 h_offset = h - valid_h;
4842
4843 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4844 || fwSide == WMSZ_BOTTOMLEFT)
4845 lprc->left += w_offset;
4846 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4847 || fwSide == WMSZ_BOTTOMRIGHT)
4848 lprc->right -= w_offset;
4849
4850 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4851 || fwSide == WMSZ_TOPRIGHT)
4852 lprc->top += h_offset;
4853 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4854 || fwSide == WMSZ_BOTTOMRIGHT)
4855 lprc->bottom -= h_offset;
4856 return TRUE;
4857}
4858
4859
4860
4861 static LRESULT CALLBACK
4862_WndProc(
4863 HWND hwnd,
4864 UINT uMsg,
4865 WPARAM wParam,
4866 LPARAM lParam)
4867{
4868 /*
4869 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4870 hwnd, uMsg, wParam, lParam);
4871 */
4872
4873 HandleMouseHide(uMsg, lParam);
4874
4875 s_uMsg = uMsg;
4876 s_wParam = wParam;
4877 s_lParam = lParam;
4878
4879 switch (uMsg)
4880 {
4881 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4882 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4883 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4884 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4885 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4886 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4887 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4888 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4889 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4890#ifdef FEAT_MENU
4891 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4892#endif
4893 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4894 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4895 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4896 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4897 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4898 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4899 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4900 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4901 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4902#ifdef FEAT_NETBEANS_INTG
4903 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4904#endif
4905
Bram Moolenaarafa24992006-03-27 20:58:26 +00004906#ifdef FEAT_GUI_TABLINE
4907 case WM_RBUTTONUP:
4908 {
4909 if (gui_mch_showing_tabline())
4910 {
4911 POINT pt;
4912 RECT rect;
4913
4914 /*
4915 * If the cursor is on the tabline, display the tab menu
4916 */
4917 GetCursorPos((LPPOINT)&pt);
4918 GetWindowRect(s_textArea, &rect);
4919 if (pt.y < rect.top)
4920 {
4921 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004922 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004923 }
4924 }
4925 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4926 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004927 case WM_LBUTTONDBLCLK:
4928 {
4929 /*
4930 * If the user double clicked the tabline, create a new tab
4931 */
4932 if (gui_mch_showing_tabline())
4933 {
4934 POINT pt;
4935 RECT rect;
4936
4937 GetCursorPos((LPPOINT)&pt);
4938 GetWindowRect(s_textArea, &rect);
4939 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004940 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004941 }
4942 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4943 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004944#endif
4945
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 case WM_QUERYENDSESSION: /* System wants to go down. */
4947 gui_shell_closed(); /* Will exit when no changed buffers. */
4948 return FALSE; /* Do NOT allow system to go down. */
4949
4950 case WM_ENDSESSION:
4951 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004952 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004954 return 0L;
4955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 break;
4957
4958 case WM_CHAR:
4959 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4960 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004961 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 return 0L;
4963
4964 case WM_SYSCHAR:
4965 /*
4966 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4967 * shortcut key, handle like a typed ALT key, otherwise call Windows
4968 * ALT key handling.
4969 */
4970#ifdef FEAT_MENU
4971 if ( !gui.menu_is_active
4972 || p_wak[0] == 'n'
4973 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4974 )
4975#endif
4976 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004977 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 return 0L;
4979 }
4980#ifdef FEAT_MENU
4981 else
4982 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4983#endif
4984
4985 case WM_SYSKEYUP:
4986#ifdef FEAT_MENU
4987 /* This used to be done only when menu is active: ALT key is used for
4988 * that. But that caused problems when menu is disabled and using
4989 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4990 * are received, mouse pointer remains hidden. */
4991 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4992#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004993 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994#endif
4995
4996 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004997 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998
4999 case WM_MOUSEWHEEL:
5000 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01005001 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002
Bram Moolenaar520470a2005-06-16 21:59:56 +00005003 /* Notification for change in SystemParametersInfo() */
5004 case WM_SETTINGCHANGE:
5005 return _OnSettingChange((UINT)wParam);
5006
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005007#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 case WM_NOTIFY:
5009 switch (((LPNMHDR) lParam)->code)
5010 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005011# ifdef FEAT_MBYTE
5012 case TTN_GETDISPINFOW:
5013# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005014 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005016 LPNMHDR hdr = (LPNMHDR)lParam;
5017 char_u *str = NULL;
5018 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005019
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005020 vim_free(tt_text);
5021 tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005022
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005023# ifdef FEAT_GUI_TABLINE
5024 if (gui_mch_showing_tabline()
5025 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005026 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005027 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005028 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005029 * Mouse is over the GUI tabline. Display the
5030 * tooltip for the tab under the cursor
5031 *
5032 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005033 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005034 GetCursorPos(&pt);
5035 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005036 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005037 TCHITTESTINFO htinfo;
5038 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005039
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005040 /*
5041 * Get the tab under the cursor
5042 */
5043 htinfo.pt.x = pt.x;
5044 htinfo.pt.y = pt.y;
5045 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
5046 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005047 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005048 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005049
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005050 tp = find_tabpage(idx + 1);
5051 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005052 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005053 get_tabline_label(tp, TRUE);
5054 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005055 }
5056 }
5057 }
5058 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005059# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005060# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005061# ifdef FEAT_GUI_TABLINE
5062 else
5063# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005065 UINT idButton;
5066 vimmenu_T *pMenu;
5067
5068 idButton = (UINT) hdr->idFrom;
5069 pMenu = gui_mswin_find_menu(root_menu, idButton);
5070 if (pMenu)
5071 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005073# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005074 if (str != NULL)
5075 {
5076# ifdef FEAT_MBYTE
5077 if (hdr->code == TTN_GETDISPINFOW)
5078 {
5079 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
5080
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005081 /* Set the maximum width, this also enables using
5082 * \n for line break. */
5083 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5084 0, 500);
5085
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005086 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005087 lpdi->lpszText = tt_text;
5088 /* can't show tooltip if failed */
5089 }
5090 else
5091# endif
5092 {
5093 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
5094
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005095 /* Set the maximum width, this also enables using
5096 * \n for line break. */
5097 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5098 0, 500);
5099
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005100 if (STRLEN(str) < sizeof(lpdi->szText)
5101 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005102 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005103 sizeof(lpdi->szText) - 1);
5104 else
5105 lpdi->lpszText = tt_text;
5106 }
5107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
5109 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005110# ifdef FEAT_GUI_TABLINE
5111 case TCN_SELCHANGE:
5112 if (gui_mch_showing_tabline()
5113 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005114 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005115 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005116 return 0L;
5117 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005118 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00005119
5120 case NM_RCLICK:
5121 if (gui_mch_showing_tabline()
5122 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005123 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00005124 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005125 return 0L;
5126 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00005127 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005130# ifdef FEAT_GUI_TABLINE
5131 if (gui_mch_showing_tabline()
5132 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
5133 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 break;
5136 }
5137 break;
5138#endif
5139#if defined(MENUHINTS) && defined(FEAT_MENU)
5140 case WM_MENUSELECT:
5141 if (((UINT) HIWORD(wParam)
5142 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
5143 == MF_HILITE
5144 && (State & CMDLINE) == 0)
5145 {
5146 UINT idButton;
5147 vimmenu_T *pMenu;
5148 static int did_menu_tip = FALSE;
5149
5150 if (did_menu_tip)
5151 {
5152 msg_clr_cmdline();
5153 setcursor();
5154 out_flush();
5155 did_menu_tip = FALSE;
5156 }
5157
5158 idButton = (UINT)LOWORD(wParam);
5159 pMenu = gui_mswin_find_menu(root_menu, idButton);
5160 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
5161 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
5162 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005163 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005165 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 setcursor();
5167 out_flush();
5168 did_menu_tip = TRUE;
5169 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01005170 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 }
5172 break;
5173#endif
5174 case WM_NCHITTEST:
5175 {
5176 LRESULT result;
5177 int x, y;
5178 int xPos = GET_X_LPARAM(lParam);
5179
5180 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
5181 if (result == HTCLIENT)
5182 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005183#ifdef FEAT_GUI_TABLINE
5184 if (gui_mch_showing_tabline())
5185 {
5186 int yPos = GET_Y_LPARAM(lParam);
5187 RECT rct;
5188
5189 /* If the cursor is on the GUI tabline, don't process this
5190 * event */
5191 GetWindowRect(s_textArea, &rct);
5192 if (yPos < rct.top)
5193 return result;
5194 }
5195#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02005196 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 xPos -= x;
5198
5199 if (xPos < 48) /* <VN> TODO should use system metric? */
5200 return HTBOTTOMLEFT;
5201 else
5202 return HTBOTTOMRIGHT;
5203 }
5204 else
5205 return result;
5206 }
5207 /* break; notreached */
5208
5209#ifdef FEAT_MBYTE_IME
5210 case WM_IME_NOTIFY:
5211 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5212 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005213 return 1L;
5214
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 case WM_IME_COMPOSITION:
5216 if (!_OnImeComposition(hwnd, wParam, lParam))
5217 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005218 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219#endif
5220
5221 default:
5222 if (uMsg == msh_msgmousewheel && msh_msgmousewheel != 0)
5223 { /* handle MSH_MOUSEWHEEL messages for Intellimouse */
5224 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01005225 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 }
5227#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005228 else if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 {
5230 _OnFindRepl();
5231 }
5232#endif
5233 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5234 }
5235
Bram Moolenaar2787ab92011-12-14 15:23:59 +01005236 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237}
5238
5239/*
5240 * End of call-back routines
5241 */
5242
5243/* parent window, if specified with -P */
5244HWND vim_parent_hwnd = NULL;
5245
5246 static BOOL CALLBACK
5247FindWindowTitle(HWND hwnd, LPARAM lParam)
5248{
5249 char buf[2048];
5250 char *title = (char *)lParam;
5251
5252 if (GetWindowText(hwnd, buf, sizeof(buf)))
5253 {
5254 if (strstr(buf, title) != NULL)
5255 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005256 /* Found it. Store the window ref. and quit searching if MDI
5257 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005259 if (vim_parent_hwnd != NULL)
5260 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 }
5262 }
5263 return TRUE; /* continue searching */
5264}
5265
5266/*
5267 * Invoked for '-P "title"' argument: search for parent application to open
5268 * our window in.
5269 */
5270 void
5271gui_mch_set_parent(char *title)
5272{
5273 EnumWindows(FindWindowTitle, (LPARAM)title);
5274 if (vim_parent_hwnd == NULL)
5275 {
5276 EMSG2(_("E671: Cannot find window title \"%s\""), title);
5277 mch_exit(2);
5278 }
5279}
5280
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005281#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 static void
5283ole_error(char *arg)
5284{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005285 char buf[IOSIZE];
5286
5287 /* Can't use EMSG() here, we have not finished initialisation yet. */
5288 vim_snprintf(buf, IOSIZE,
5289 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
5290 arg);
5291 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294
5295/*
5296 * Parse the GUI related command-line arguments. Any arguments used are
5297 * deleted from argv, and *argc is decremented accordingly. This is called
5298 * when vim is started, whether or not the GUI has been started.
5299 */
5300 void
5301gui_mch_prepare(int *argc, char **argv)
5302{
5303 int silent = FALSE;
5304 int idx;
5305
5306 /* Check for special OLE command line parameters */
5307 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5308 {
5309 /* Check for a "-silent" argument first. */
5310 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5311 && (argv[2][0] == '-' || argv[2][0] == '/'))
5312 {
5313 silent = TRUE;
5314 idx = 2;
5315 }
5316 else
5317 idx = 1;
5318
5319 /* Register Vim as an OLE Automation server */
5320 if (STRICMP(argv[idx] + 1, "register") == 0)
5321 {
5322#ifdef FEAT_OLE
5323 RegisterMe(silent);
5324 mch_exit(0);
5325#else
5326 if (!silent)
5327 ole_error("register");
5328 mch_exit(2);
5329#endif
5330 }
5331
5332 /* Unregister Vim as an OLE Automation server */
5333 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5334 {
5335#ifdef FEAT_OLE
5336 UnregisterMe(!silent);
5337 mch_exit(0);
5338#else
5339 if (!silent)
5340 ole_error("unregister");
5341 mch_exit(2);
5342#endif
5343 }
5344
5345 /* Ignore an -embedding argument. It is only relevant if the
5346 * application wants to treat the case when it is started manually
5347 * differently from the case where it is started via automation (and
5348 * we don't).
5349 */
5350 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5351 {
5352#ifdef FEAT_OLE
5353 *argc = 1;
5354#else
5355 ole_error("embedding");
5356 mch_exit(2);
5357#endif
5358 }
5359 }
5360
5361#ifdef FEAT_OLE
5362 {
5363 int bDoRestart = FALSE;
5364
5365 InitOLE(&bDoRestart);
5366 /* automatically exit after registering */
5367 if (bDoRestart)
5368 mch_exit(0);
5369 }
5370#endif
5371
5372#ifdef FEAT_NETBEANS_INTG
5373 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005374 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 int arg;
5376
5377 for (arg = 1; arg < *argc; arg++)
5378 if (strncmp("-nb", argv[arg], 3) == 0)
5379 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 netbeansArg = argv[arg];
5381 mch_memmove(&argv[arg], &argv[arg + 1],
5382 (--*argc - arg) * sizeof(char *));
5383 argv[*argc] = NULL;
5384 break; /* enough? */
5385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 }
5387#endif
5388
5389 /* get the OS version info */
5390 os_version.dwOSVersionInfoSize = sizeof(os_version);
5391 GetVersionEx(&os_version); /* this call works on Win32s, Win95 and WinNT */
5392
5393 /* try and load the user32.dll library and get the entry points for
5394 * multi-monitor-support. */
Bram Moolenaarebbcb822010-10-23 14:02:54 +02005395 if ((user32_lib = vimLoadLib("User32.dll")) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 {
5397 pMonitorFromWindow = (TMonitorFromWindow)GetProcAddress(user32_lib,
5398 "MonitorFromWindow");
5399
5400 /* there are ...A and ...W version of GetMonitorInfo - looking at
5401 * winuser.h, they have exactly the same declaration. */
5402 pGetMonitorInfo = (TGetMonitorInfo)GetProcAddress(user32_lib,
5403 "GetMonitorInfoA");
5404 }
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02005405
5406#ifdef FEAT_MBYTE
5407 /* If the OS is Windows NT, use wide functions;
5408 * this enables common dialogs input unicode from IME. */
5409 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
5410 {
5411 pDispatchMessage = DispatchMessageW;
5412 pGetMessage = GetMessageW;
5413 pIsDialogMessage = IsDialogMessageW;
5414 pPeekMessage = PeekMessageW;
5415 }
5416 else
5417 {
5418 pDispatchMessage = DispatchMessageA;
5419 pGetMessage = GetMessageA;
5420 pIsDialogMessage = IsDialogMessageA;
5421 pPeekMessage = PeekMessageA;
5422 }
5423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424}
5425
5426/*
5427 * Initialise the GUI. Create all the windows, set up all the call-backs
5428 * etc.
5429 */
5430 int
5431gui_mch_init(void)
5432{
5433 const char szVimWndClass[] = VIM_CLASS;
5434 const char szTextAreaClass[] = "VimTextArea";
5435 WNDCLASS wndclass;
5436#ifdef FEAT_MBYTE
5437 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005438 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 WNDCLASSW wndclassw;
5440#endif
5441#ifdef GLOBAL_IME
5442 ATOM atom;
5443#endif
5444
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 /* Return here if the window was already opened (happens when
5446 * gui_mch_dialog() is called early). */
5447 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005448 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449
5450 /*
5451 * Load the tearoff bitmap
5452 */
5453#ifdef FEAT_TEAROFF
5454 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
5455#endif
5456
5457 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5458 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5459#ifdef FEAT_MENU
5460 gui.menu_height = 0; /* Windows takes care of this */
5461#endif
5462 gui.border_width = 0;
5463
5464 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5465
5466#ifdef FEAT_MBYTE
5467 /* First try using the wide version, so that we can use any title.
5468 * Otherwise only characters in the active codepage will work. */
5469 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
5470 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005471 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 wndclassw.lpfnWndProc = _WndProc;
5473 wndclassw.cbClsExtra = 0;
5474 wndclassw.cbWndExtra = 0;
5475 wndclassw.hInstance = s_hinst;
5476 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5477 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5478 wndclassw.hbrBackground = s_brush;
5479 wndclassw.lpszMenuName = NULL;
5480 wndclassw.lpszClassName = szVimWndClassW;
5481
5482 if ((
5483#ifdef GLOBAL_IME
5484 atom =
5485#endif
5486 RegisterClassW(&wndclassw)) == 0)
5487 {
5488 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
5489 return FAIL;
5490
5491 /* Must be Windows 98, fall back to non-wide function. */
5492 }
5493 else
5494 wide_WindowProc = TRUE;
5495 }
5496
5497 if (!wide_WindowProc)
5498#endif
5499
5500 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
5501 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005502 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 wndclass.lpfnWndProc = _WndProc;
5504 wndclass.cbClsExtra = 0;
5505 wndclass.cbWndExtra = 0;
5506 wndclass.hInstance = s_hinst;
5507 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
5508 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5509 wndclass.hbrBackground = s_brush;
5510 wndclass.lpszMenuName = NULL;
5511 wndclass.lpszClassName = szVimWndClass;
5512
5513 if ((
5514#ifdef GLOBAL_IME
5515 atom =
5516#endif
5517 RegisterClass(&wndclass)) == 0)
5518 return FAIL;
5519 }
5520
5521 if (vim_parent_hwnd != NULL)
5522 {
5523#ifdef HAVE_TRY_EXCEPT
5524 __try
5525 {
5526#endif
5527 /* Open inside the specified parent window.
5528 * TODO: last argument should point to a CLIENTCREATESTRUCT
5529 * structure. */
5530 s_hwnd = CreateWindowEx(
5531 WS_EX_MDICHILD,
5532 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005533 WS_OVERLAPPEDWINDOW | WS_CHILD
5534 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5536 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5537 100, /* Any value will do */
5538 100, /* Any value will do */
5539 vim_parent_hwnd, NULL,
5540 s_hinst, NULL);
5541#ifdef HAVE_TRY_EXCEPT
5542 }
5543 __except(EXCEPTION_EXECUTE_HANDLER)
5544 {
5545 /* NOP */
5546 }
5547#endif
5548 if (s_hwnd == NULL)
5549 {
5550 EMSG(_("E672: Unable to open window inside MDI application"));
5551 mch_exit(2);
5552 }
5553 }
5554 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005555 {
5556 /* If the provided windowid is not valid reset it to zero, so that it
5557 * is ignored and we open our own window. */
5558 if (IsWindow((HWND)win_socket_id) <= 0)
5559 win_socket_id = 0;
5560
5561 /* Create a window. If win_socket_id is not zero without border and
5562 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00005564 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005565 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5566 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005567 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5568 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5569 100, /* Any value will do */
5570 100, /* Any value will do */
5571 NULL, NULL,
5572 s_hinst, NULL);
5573 if (s_hwnd != NULL && win_socket_id != 0)
5574 {
5575 SetParent(s_hwnd, (HWND)win_socket_id);
5576 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5577 }
5578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579
5580 if (s_hwnd == NULL)
5581 return FAIL;
5582
5583#ifdef GLOBAL_IME
5584 global_ime_init(atom, s_hwnd);
5585#endif
5586#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5587 dyn_imm_load();
5588#endif
5589
5590 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005591#ifdef FEAT_MBYTE
5592 if (wide_WindowProc)
5593 {
5594 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
5595 {
5596 wndclassw.style = CS_OWNDC;
5597 wndclassw.lpfnWndProc = _TextAreaWndProc;
5598 wndclassw.cbClsExtra = 0;
5599 wndclassw.cbWndExtra = 0;
5600 wndclassw.hInstance = s_hinst;
5601 wndclassw.hIcon = NULL;
5602 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5603 wndclassw.hbrBackground = NULL;
5604 wndclassw.lpszMenuName = NULL;
5605 wndclassw.lpszClassName = szTextAreaClassW;
5606
5607 if (RegisterClassW(&wndclassw) == 0)
5608 return FAIL;
5609 }
5610 }
5611 else
5612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
5614 {
5615 wndclass.style = CS_OWNDC;
5616 wndclass.lpfnWndProc = _TextAreaWndProc;
5617 wndclass.cbClsExtra = 0;
5618 wndclass.cbWndExtra = 0;
5619 wndclass.hInstance = s_hinst;
5620 wndclass.hIcon = NULL;
5621 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5622 wndclass.hbrBackground = NULL;
5623 wndclass.lpszMenuName = NULL;
5624 wndclass.lpszClassName = szTextAreaClass;
5625
5626 if (RegisterClass(&wndclass) == 0)
5627 return FAIL;
5628 }
5629 s_textArea = CreateWindowEx(
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005630 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 szTextAreaClass, "Vim text area",
5632 WS_CHILD | WS_VISIBLE, 0, 0,
5633 100, /* Any value will do for now */
5634 100, /* Any value will do for now */
5635 s_hwnd, NULL,
5636 s_hinst, NULL);
5637
5638 if (s_textArea == NULL)
5639 return FAIL;
5640
Bram Moolenaar20321902016-02-17 12:30:17 +01005641#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005642 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5643 {
5644 HANDLE hIcon = NULL;
5645
5646 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005647 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005648 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005649#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005650
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651#ifdef FEAT_MENU
5652 s_menuBar = CreateMenu();
5653#endif
5654 s_hdc = GetDC(s_textArea);
5655
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656#ifdef FEAT_WINDOWS
5657 DragAcceptFiles(s_hwnd, TRUE);
5658#endif
5659
5660 /* Do we need to bother with this? */
5661 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5662
5663 /* Get background/foreground colors from the system */
5664 gui_mch_def_colors();
5665
5666 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5667 * file) */
5668 set_normal_colors();
5669
5670 /*
5671 * Check that none of the colors are the same as the background color.
5672 * Then store the current values as the defaults.
5673 */
5674 gui_check_colors();
5675 gui.def_norm_pixel = gui.norm_pixel;
5676 gui.def_back_pixel = gui.back_pixel;
5677
5678 /* Get the colors for the highlight groups (gui_check_colors() might have
5679 * changed them) */
5680 highlight_gui_started();
5681
5682 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005683 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005685 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686
5687 /*
5688 * Set up for Intellimouse processing
5689 */
5690 init_mouse_wheel();
5691
5692 /*
5693 * compute a couple of metrics used for the dialogs
5694 */
5695 get_dialog_font_metrics();
5696#ifdef FEAT_TOOLBAR
5697 /*
5698 * Create the toolbar
5699 */
5700 initialise_toolbar();
5701#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005702#ifdef FEAT_GUI_TABLINE
5703 /*
5704 * Create the tabline
5705 */
5706 initialise_tabline();
5707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708#ifdef MSWIN_FIND_REPLACE
5709 /*
5710 * Initialise the dialog box stuff
5711 */
5712 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5713
5714 /* Initialise the struct */
5715 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005716 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005718 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5720 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5721 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00005722# if defined(FEAT_MBYTE) && defined(WIN3264)
5723 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
5724 s_findrep_struct_w.lpstrFindWhat =
5725 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5726 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
5727 s_findrep_struct_w.lpstrReplaceWith =
5728 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5729 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
5730 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
5731 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5732# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005735#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005736# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5737/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5738# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005739# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005740# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005741# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005742 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005743 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005744#endif
5745
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005746#ifdef FEAT_RENDER_OPTIONS
5747 if (p_rop)
5748 (void)gui_mch_set_rendering_options(p_rop);
5749#endif
5750
Bram Moolenaar748bf032005-02-02 23:04:36 +00005751theend:
5752 /* Display any pending error messages */
5753 display_errors();
5754
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 return OK;
5756}
5757
5758/*
5759 * Get the size of the screen, taking position on multiple monitors into
5760 * account (if supported).
5761 */
5762 static void
5763get_work_area(RECT *spi_rect)
5764{
5765 _HMONITOR mon;
5766 _MONITORINFO moninfo;
5767
5768 /* use these functions only if available */
5769 if (pMonitorFromWindow != NULL && pGetMonitorInfo != NULL)
5770 {
5771 /* work out which monitor the window is on, and get *it's* work area */
5772 mon = pMonitorFromWindow(s_hwnd, 1 /*MONITOR_DEFAULTTOPRIMARY*/);
5773 if (mon != NULL)
5774 {
5775 moninfo.cbSize = sizeof(_MONITORINFO);
5776 if (pGetMonitorInfo(mon, &moninfo))
5777 {
5778 *spi_rect = moninfo.rcWork;
5779 return;
5780 }
5781 }
5782 }
5783 /* this is the old method... */
5784 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5785}
5786
5787/*
5788 * Set the size of the window to the given width and height in pixels.
5789 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005790/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 void
5792gui_mch_set_shellsize(int width, int height,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005793 int min_width, int min_height, int base_width, int base_height,
5794 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795{
5796 RECT workarea_rect;
5797 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 WINDOWPLACEMENT wndpl;
5799
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005800 /* Try to keep window completely on screen. */
5801 /* Get position of the screen work area. This is the part that is not
5802 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 get_work_area(&workarea_rect);
5804
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005805 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005806 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 wndpl.length = sizeof(WINDOWPLACEMENT);
5808 GetWindowPlacement(s_hwnd, &wndpl);
5809
5810 /* Resizing a maximized window looks very strange, unzoom it first.
5811 * But don't do it when still starting up, it may have been requested in
5812 * the shortcut. */
5813 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5814 {
5815 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5816 /* Need to get the settings of the normal window. */
5817 GetWindowPlacement(s_hwnd, &wndpl);
5818 }
5819
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005821 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005822 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005823 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005824 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 + GetSystemMetrics(SM_CYCAPTION)
5826#ifdef FEAT_MENU
5827 + gui_mswin_get_menu_height(FALSE)
5828#endif
5829 ;
5830
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005831 /* The following should take care of keeping Vim on the same monitor, no
5832 * matter if the secondary monitor is left or right of the primary
5833 * monitor. */
5834 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5835 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005836
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005837 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005838 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005839 && wndpl.rcNormalPosition.right > workarea_rect.right)
5840 OffsetRect(&wndpl.rcNormalPosition,
5841 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005843 if ((direction & RESIZE_HOR)
5844 && wndpl.rcNormalPosition.left < workarea_rect.left)
5845 OffsetRect(&wndpl.rcNormalPosition,
5846 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847
Bram Moolenaarafa24992006-03-27 20:58:26 +00005848 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005849 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5850 OffsetRect(&wndpl.rcNormalPosition,
5851 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005853 if ((direction & RESIZE_VERT)
5854 && wndpl.rcNormalPosition.top < workarea_rect.top)
5855 OffsetRect(&wndpl.rcNormalPosition,
5856 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857
5858 /* set window position - we should use SetWindowPlacement rather than
5859 * SetWindowPos as the MSDN docs say the coord systems returned by
5860 * these two are not compatible. */
5861 SetWindowPlacement(s_hwnd, &wndpl);
5862
5863 SetActiveWindow(s_hwnd);
5864 SetFocus(s_hwnd);
5865
5866#ifdef FEAT_MENU
5867 /* Menu may wrap differently now */
5868 gui_mswin_get_menu_height(!gui.starting);
5869#endif
5870}
5871
5872
5873 void
5874gui_mch_set_scrollbar_thumb(
5875 scrollbar_T *sb,
5876 long val,
5877 long size,
5878 long max)
5879{
5880 SCROLLINFO info;
5881
5882 sb->scroll_shift = 0;
5883 while (max > 32767)
5884 {
5885 max = (max + 1) >> 1;
5886 val >>= 1;
5887 size >>= 1;
5888 ++sb->scroll_shift;
5889 }
5890
5891 if (sb->scroll_shift > 0)
5892 ++size;
5893
5894 info.cbSize = sizeof(info);
5895 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5896 info.nPos = val;
5897 info.nMin = 0;
5898 info.nMax = max;
5899 info.nPage = size;
5900 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5901}
5902
5903
5904/*
5905 * Set the current text font.
5906 */
5907 void
5908gui_mch_set_font(GuiFont font)
5909{
5910 gui.currFont = font;
5911}
5912
5913
5914/*
5915 * Set the current text foreground color.
5916 */
5917 void
5918gui_mch_set_fg_color(guicolor_T color)
5919{
5920 gui.currFgColor = color;
5921}
5922
5923/*
5924 * Set the current text background color.
5925 */
5926 void
5927gui_mch_set_bg_color(guicolor_T color)
5928{
5929 gui.currBgColor = color;
5930}
5931
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005932/*
5933 * Set the current text special color.
5934 */
5935 void
5936gui_mch_set_sp_color(guicolor_T color)
5937{
5938 gui.currSpColor = color;
5939}
5940
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941#if defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)
5942/*
5943 * Multi-byte handling, originally by Sung-Hoon Baek.
5944 * First static functions (no prototypes generated).
5945 */
5946#ifdef _MSC_VER
5947# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5948#endif
5949#include <imm.h>
5950
5951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 * handle WM_IME_NOTIFY message
5953 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005954/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 static LRESULT
5956_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData)
5957{
5958 LRESULT lResult = 0;
5959 HIMC hImc;
5960
5961 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5962 return lResult;
5963 switch (dwCommand)
5964 {
5965 case IMN_SETOPENSTATUS:
5966 if (pImmGetOpenStatus(hImc))
5967 {
5968 pImmSetCompositionFont(hImc, &norm_logfont);
5969 im_set_position(gui.row, gui.col);
5970
5971 /* Disable langmap */
5972 State &= ~LANGMAP;
5973 if (State & INSERT)
5974 {
5975#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
5976 /* Unshown 'keymap' in status lines */
5977 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5978 {
5979 /* Save cursor position */
5980 int old_row = gui.row;
5981 int old_col = gui.col;
5982
5983 // This must be called here before
5984 // status_redraw_curbuf(), otherwise the mode
5985 // message may appear in the wrong position.
5986 showmode();
5987 status_redraw_curbuf();
5988 update_screen(0);
5989 /* Restore cursor position */
5990 gui.row = old_row;
5991 gui.col = old_col;
5992 }
5993#endif
5994 }
5995 }
5996 gui_update_cursor(TRUE, FALSE);
5997 lResult = 0;
5998 break;
5999 }
6000 pImmReleaseContext(hWnd, hImc);
6001 return lResult;
6002}
6003
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006004/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 static LRESULT
6006_OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param)
6007{
6008 char_u *ret;
6009 int len;
6010
6011 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
6012 return 0;
6013
6014 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
6015 if (ret != NULL)
6016 {
6017 add_to_input_buf_csi(ret, len);
6018 vim_free(ret);
6019 return 1;
6020 }
6021 return 0;
6022}
6023
6024/*
6025 * get the current composition string, in UCS-2; *lenp is the number of
6026 * *lenp is the number of Unicode characters.
6027 */
6028 static short_u *
6029GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
6030{
6031 LONG ret;
6032 LPWSTR wbuf = NULL;
6033 char_u *buf;
6034
6035 if (!pImmGetContext)
6036 return NULL; /* no imm32.dll */
6037
6038 /* Try Unicode; this'll always work on NT regardless of codepage. */
6039 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
6040 if (ret == 0)
6041 return NULL; /* empty */
6042
6043 if (ret > 0)
6044 {
6045 /* Allocate the requested buffer plus space for the NUL character. */
6046 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
6047 if (wbuf != NULL)
6048 {
6049 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
6050 *lenp = ret / sizeof(WCHAR);
6051 }
6052 return (short_u *)wbuf;
6053 }
6054
6055 /* ret < 0; we got an error, so try the ANSI version. This'll work
6056 * on 9x/ME, but only if the codepage happens to be set to whatever
6057 * we're inputting. */
6058 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
6059 if (ret <= 0)
6060 return NULL; /* empty or error */
6061
6062 buf = alloc(ret);
6063 if (buf == NULL)
6064 return NULL;
6065 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
6066
6067 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006068 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 vim_free(buf);
6070
6071 return (short_u *)wbuf;
6072}
6073
6074/*
6075 * void GetResultStr()
6076 *
6077 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
6078 * get complete composition string
6079 */
6080 static char_u *
6081GetResultStr(HWND hwnd, int GCS, int *lenp)
6082{
6083 HIMC hIMC; /* Input context handle. */
6084 short_u *buf = NULL;
6085 char_u *convbuf = NULL;
6086
6087 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
6088 return NULL;
6089
6090 /* Reads in the composition string. */
6091 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
6092 if (buf == NULL)
6093 return NULL;
6094
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006095 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 pImmReleaseContext(hwnd, hIMC);
6097 vim_free(buf);
6098 return convbuf;
6099}
6100#endif
6101
6102/* For global functions we need prototypes. */
6103#if (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) || defined(PROTO)
6104
6105/*
6106 * set font to IM.
6107 */
6108 void
6109im_set_font(LOGFONT *lf)
6110{
6111 HIMC hImc;
6112
6113 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6114 {
6115 pImmSetCompositionFont(hImc, lf);
6116 pImmReleaseContext(s_hwnd, hImc);
6117 }
6118}
6119
6120/*
6121 * Notify cursor position to IM.
6122 */
6123 void
6124im_set_position(int row, int col)
6125{
6126 HIMC hImc;
6127
6128 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6129 {
6130 COMPOSITIONFORM cfs;
6131
6132 cfs.dwStyle = CFS_POINT;
6133 cfs.ptCurrentPos.x = FILL_X(col);
6134 cfs.ptCurrentPos.y = FILL_Y(row);
6135 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
6136 pImmSetCompositionWindow(hImc, &cfs);
6137
6138 pImmReleaseContext(s_hwnd, hImc);
6139 }
6140}
6141
6142/*
6143 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6144 */
6145 void
6146im_set_active(int active)
6147{
6148 HIMC hImc;
6149 static HIMC hImcOld = (HIMC)0;
6150
6151 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
6152 {
6153 if (p_imdisable)
6154 {
6155 if (hImcOld == (HIMC)0)
6156 {
6157 hImcOld = pImmGetContext(s_hwnd);
6158 if (hImcOld)
6159 pImmAssociateContext(s_hwnd, (HIMC)0);
6160 }
6161 active = FALSE;
6162 }
6163 else if (hImcOld != (HIMC)0)
6164 {
6165 pImmAssociateContext(s_hwnd, hImcOld);
6166 hImcOld = (HIMC)0;
6167 }
6168
6169 hImc = pImmGetContext(s_hwnd);
6170 if (hImc)
6171 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006172 /*
6173 * for Korean ime
6174 */
6175 HKL hKL = GetKeyboardLayout(0);
6176
6177 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6178 {
6179 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6180 static BOOL bSaved = FALSE;
6181
6182 if (active)
6183 {
6184 /* if we have a saved conversion status, restore it */
6185 if (bSaved)
6186 pImmSetConversionStatus(hImc, dwConversionSaved,
6187 dwSentenceSaved);
6188 bSaved = FALSE;
6189 }
6190 else
6191 {
6192 /* save conversion status and disable korean */
6193 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6194 &dwSentenceSaved))
6195 {
6196 bSaved = TRUE;
6197 pImmSetConversionStatus(hImc,
6198 dwConversionSaved & ~(IME_CMODE_NATIVE
6199 | IME_CMODE_FULLSHAPE),
6200 dwSentenceSaved);
6201 }
6202 }
6203 }
6204
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 pImmSetOpenStatus(hImc, active);
6206 pImmReleaseContext(s_hwnd, hImc);
6207 }
6208 }
6209}
6210
6211/*
6212 * Get IM status. When IM is on, return not 0. Else return 0.
6213 */
6214 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006215im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216{
6217 int status = 0;
6218 HIMC hImc;
6219
6220 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6221 {
6222 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6223 pImmReleaseContext(s_hwnd, hImc);
6224 }
6225 return status;
6226}
6227
6228#endif /* FEAT_MBYTE && FEAT_MBYTE_IME */
6229
6230#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
6231/* Win32 with GLOBAL IME */
6232
6233/*
6234 * Notify cursor position to IM.
6235 */
6236 void
6237im_set_position(int row, int col)
6238{
6239 /* Win32 with GLOBAL IME */
6240 POINT p;
6241
6242 p.x = FILL_X(col);
6243 p.y = FILL_Y(row);
6244 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
6245 global_ime_set_position(&p);
6246}
6247
6248/*
6249 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6250 */
6251 void
6252im_set_active(int active)
6253{
6254 global_ime_set_status(active);
6255}
6256
6257/*
6258 * Get IM status. When IM is on, return not 0. Else return 0.
6259 */
6260 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006261im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262{
6263 return global_ime_get_status();
6264}
6265#endif
6266
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006267#ifdef FEAT_MBYTE
6268/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006269 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006270 */
6271 static void
6272latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6273{
6274 int c;
6275
Bram Moolenaarca003e12006-03-17 23:19:38 +00006276 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006277 {
6278 c = *text++;
6279 switch (c)
6280 {
6281 case 0xa4: c = 0x20ac; break; /* euro */
6282 case 0xa6: c = 0x0160; break; /* S hat */
6283 case 0xa8: c = 0x0161; break; /* S -hat */
6284 case 0xb4: c = 0x017d; break; /* Z hat */
6285 case 0xb8: c = 0x017e; break; /* Z -hat */
6286 case 0xbc: c = 0x0152; break; /* OE */
6287 case 0xbd: c = 0x0153; break; /* oe */
6288 case 0xbe: c = 0x0178; break; /* Y */
6289 }
6290 *unicodebuf++ = c;
6291 }
6292}
6293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294
6295#ifdef FEAT_RIGHTLEFT
6296/*
6297 * What is this for? In the case where you are using Win98 or Win2K or later,
6298 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6299 * reverses the string sent to the TextOut... family. This sucks, because we
6300 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6301 * way to tell Windblows not to do this!
6302 *
6303 * The short of it is that this 'RevOut' only gets called if you are running
6304 * one of the new, "improved" MS OSes, and only if you are running in
6305 * 'rightleft' mode. It makes display take *slightly* longer, but not
6306 * noticeably so.
6307 */
6308 static void
6309RevOut( HDC s_hdc,
6310 int col,
6311 int row,
6312 UINT foptions,
6313 CONST RECT *pcliprect,
6314 LPCTSTR text,
6315 UINT len,
6316 CONST INT *padding)
6317{
6318 int ix;
6319 static int special = -1;
6320
6321 if (special == -1)
6322 {
6323 /* Check windows version: special treatment is needed if it is NT 5 or
6324 * Win98 or higher. */
6325 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
6326 && os_version.dwMajorVersion >= 5)
6327 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
6328 && (os_version.dwMajorVersion > 4
6329 || (os_version.dwMajorVersion == 4
6330 && os_version.dwMinorVersion > 0))))
6331 special = 1;
6332 else
6333 special = 0;
6334 }
6335
6336 if (special)
6337 for (ix = 0; ix < (int)len; ++ix)
6338 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6339 pcliprect, text + ix, 1, padding);
6340 else
6341 ExtTextOut(s_hdc, col, row, foptions, pcliprect, text, len, padding);
6342}
6343#endif
6344
6345 void
6346gui_mch_draw_string(
6347 int row,
6348 int col,
6349 char_u *text,
6350 int len,
6351 int flags)
6352{
6353 static int *padding = NULL;
6354 static int pad_size = 0;
6355 int i;
6356 const RECT *pcliprect = NULL;
6357 UINT foptions = 0;
6358#ifdef FEAT_MBYTE
6359 static WCHAR *unicodebuf = NULL;
6360 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006361 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 int n = 0;
6363#endif
6364 HPEN hpen, old_pen;
6365 int y;
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006366#ifdef FEAT_DIRECTX
6367 int font_is_ttf_or_vector = 0;
6368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 /*
6371 * Italic and bold text seems to have an extra row of pixels at the bottom
6372 * (below where the bottom of the character should be). If we draw the
6373 * characters with a solid background, the top row of pixels in the
6374 * character below will be overwritten. We can fix this by filling in the
6375 * background ourselves, to the correct character proportions, and then
6376 * writing the character in transparent mode. Still have a problem when
6377 * the character is "_", which gets written on to the character below.
6378 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6379 * pixel in their slots, which fixes the problem with the bottom row of
6380 * pixels. We still need this code because otherwise the top row of pixels
6381 * becomes a problem. - webb.
6382 */
6383 static HBRUSH hbr_cache[2] = {NULL, NULL};
6384 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6385 static int brush_lru = 0;
6386 HBRUSH hbr;
6387 RECT rc;
6388
6389 if (!(flags & DRAW_TRANSP))
6390 {
6391 /*
6392 * Clear background first.
6393 * Note: FillRect() excludes right and bottom of rectangle.
6394 */
6395 rc.left = FILL_X(col);
6396 rc.top = FILL_Y(row);
6397#ifdef FEAT_MBYTE
6398 if (has_mbyte)
6399 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006401 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 }
6403 else
6404#endif
6405 rc.right = FILL_X(col + len);
6406 rc.bottom = FILL_Y(row + 1);
6407
6408 /* Cache the created brush, that saves a lot of time. We need two:
6409 * one for cursor background and one for the normal background. */
6410 if (gui.currBgColor == brush_color[0])
6411 {
6412 hbr = hbr_cache[0];
6413 brush_lru = 1;
6414 }
6415 else if (gui.currBgColor == brush_color[1])
6416 {
6417 hbr = hbr_cache[1];
6418 brush_lru = 0;
6419 }
6420 else
6421 {
6422 if (hbr_cache[brush_lru] != NULL)
6423 DeleteBrush(hbr_cache[brush_lru]);
6424 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6425 brush_color[brush_lru] = gui.currBgColor;
6426 hbr = hbr_cache[brush_lru];
6427 brush_lru = !brush_lru;
6428 }
6429 FillRect(s_hdc, &rc, hbr);
6430
6431 SetBkMode(s_hdc, TRANSPARENT);
6432
6433 /*
6434 * When drawing block cursor, prevent inverted character spilling
6435 * over character cell (can happen with bold/italic)
6436 */
6437 if (flags & DRAW_CURSOR)
6438 {
6439 pcliprect = &rc;
6440 foptions = ETO_CLIPPED;
6441 }
6442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 SetTextColor(s_hdc, gui.currFgColor);
6444 SelectFont(s_hdc, gui.currFont);
6445
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006446#ifdef FEAT_DIRECTX
6447 if (IS_ENABLE_DIRECTX())
6448 {
6449 TEXTMETRIC tm;
6450
6451 GetTextMetrics(s_hdc, &tm);
6452 if (tm.tmPitchAndFamily & (TMPF_TRUETYPE | TMPF_VECTOR))
6453 {
6454 font_is_ttf_or_vector = 1;
6455 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
6456 }
6457 }
6458#endif
6459
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6461 {
6462 vim_free(padding);
6463 pad_size = Columns;
6464
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006465 /* Don't give an out-of-memory message here, it would call us
6466 * recursively. */
6467 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 if (padding != NULL)
6469 for (i = 0; i < pad_size; i++)
6470 padding[i] = gui.char_width;
6471 }
6472
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 /*
6474 * We have to provide the padding argument because italic and bold versions
6475 * of fixed-width fonts are often one pixel or so wider than their normal
6476 * versions.
6477 * No check for DRAW_BOLD, Windows will have done it already.
6478 */
6479
6480#ifdef FEAT_MBYTE
6481 /* Check if there are any UTF-8 characters. If not, use normal text
6482 * output to speed up output. */
6483 if (enc_utf8)
6484 for (n = 0; n < len; ++n)
6485 if (text[n] >= 0x80)
6486 break;
6487
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006488#if defined(FEAT_DIRECTX)
6489 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6490 * required that unicode drawing routine, currently. So this forces it
6491 * enabled. */
6492 if (enc_utf8 && IS_ENABLE_DIRECTX())
6493 n = 0; /* Keep n < len, to enter block for unicode. */
6494#endif
6495
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006497 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006499 if ((enc_utf8
6500 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6501 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 && (unicodebuf == NULL || len > unibuflen))
6503 {
6504 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006505 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506
6507 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006508 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509
6510 unibuflen = len;
6511 }
6512
6513 if (enc_utf8 && n < len && unicodebuf != NULL)
6514 {
6515 /* Output UTF-8 characters. Caller has already separated
6516 * composing characters. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006517 int i;
6518 int wlen; /* string length in words */
6519 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 int cells; /* cell width of string up to composing char */
6521 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006522 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006524 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006525 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006527 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006529 c = utf_ptr2char(text + i);
6530 if (c >= 0x10000)
6531 {
6532 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006533 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6534 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006535 }
6536 else
6537 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006538 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006539 }
6540 cw = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 if (cw > 2) /* don't use 4 for unprintable char */
6542 cw = 1;
6543 if (unicodepdy != NULL)
6544 {
6545 /* Use unicodepdy to make characters fit as we expect, even
6546 * when the font uses different widths (e.g., bold character
6547 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006548 if (c >= 0x10000)
6549 {
6550 unicodepdy[wlen - 2] = cw * gui.char_width;
6551 unicodepdy[wlen - 1] = 0;
6552 }
6553 else
6554 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 }
6556 cells += cw;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006557 i += utfc_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006558 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006560#if defined(FEAT_DIRECTX)
6561 if (IS_ENABLE_DIRECTX() && font_is_ttf_or_vector)
6562 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006563 /* Add one to "cells" for italics. */
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006564 DWriteContext_DrawText(s_dwc, s_hdc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006565 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006566 gui.char_width, gui.currFgColor);
6567 }
6568 else
6569#endif
6570 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6571 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 len = cells; /* used for underlining */
6573 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006574 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 {
6576 /* If we want to display codepage data, and the current CP is not the
6577 * ANSI one, we need to go via Unicode. */
6578 if (unicodebuf != NULL)
6579 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006580 if (enc_latin9)
6581 latin9_to_ucs(text, len, unicodebuf);
6582 else
6583 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 MB_PRECOMPOSED,
6585 (char *)text, len,
6586 (LPWSTR)unicodebuf, unibuflen);
6587 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006588 {
6589 /* Use unicodepdy to make characters fit as we expect, even
6590 * when the font uses different widths (e.g., bold character
6591 * is wider). */
6592 if (unicodepdy != NULL)
6593 {
6594 int i;
6595 int cw;
6596
6597 for (i = 0; i < len; ++i)
6598 {
6599 cw = utf_char2cells(unicodebuf[i]);
6600 if (cw > 2)
6601 cw = 1;
6602 unicodepdy[i] = cw * gui.char_width;
6603 }
6604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006606 foptions, pcliprect, unicodebuf, len, unicodepdy);
6607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 }
6609 }
6610 else
6611#endif
6612 {
6613#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006614 /* Windows will mess up RL text, so we have to draw it character by
6615 * character. Only do this if RL is on, since it's slow. */
6616 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6618 foptions, pcliprect, (char *)text, len, padding);
6619 else
6620#endif
6621 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6622 foptions, pcliprect, (char *)text, len, padding);
6623 }
6624
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006625 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 if (flags & DRAW_UNDERL)
6627 {
6628 hpen = CreatePen(PS_SOLID, 1, gui.currFgColor);
6629 old_pen = SelectObject(s_hdc, hpen);
6630 /* When p_linespace is 0, overwrite the bottom row of pixels.
6631 * Otherwise put the line just below the character. */
6632 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 if (p_linespace > 1)
6634 y -= p_linespace - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 MoveToEx(s_hdc, FILL_X(col), y, NULL);
6636 /* Note: LineTo() excludes the last pixel in the line. */
6637 LineTo(s_hdc, FILL_X(col + len), y);
6638 DeleteObject(SelectObject(s_hdc, old_pen));
6639 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006640
6641 /* Undercurl */
6642 if (flags & DRAW_UNDERC)
6643 {
6644 int x;
6645 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006646 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006647
6648 y = FILL_Y(row + 1) - 1;
6649 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6650 {
6651 offset = val[x % 8];
6652 SetPixel(s_hdc, x, y - offset, gui.currSpColor);
6653 }
6654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655}
6656
6657
6658/*
6659 * Output routines.
6660 */
6661
6662/* Flush any output to the screen */
6663 void
6664gui_mch_flush(void)
6665{
6666# if defined(__BORLANDC__)
6667 /*
6668 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6669 * prototype declaration.
6670 * The compiler complains if __stdcall is not used in both declarations.
6671 */
6672 BOOL __stdcall GdiFlush(void);
6673# endif
6674
6675 GdiFlush();
6676}
6677
6678 static void
6679clear_rect(RECT *rcp)
6680{
6681 HBRUSH hbr;
6682
6683 hbr = CreateSolidBrush(gui.back_pixel);
6684 FillRect(s_hdc, rcp, hbr);
6685 DeleteBrush(hbr);
6686}
6687
6688
Bram Moolenaarc716c302006-01-21 22:12:51 +00006689 void
6690gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6691{
6692 RECT workarea_rect;
6693
6694 get_work_area(&workarea_rect);
6695
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006696 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006697 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006698 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006699
6700 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6701 * the menubar for MSwin, we subtract it from the screen height, so that
6702 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006703 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006704 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006705 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006706 - GetSystemMetrics(SM_CYCAPTION)
6707#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006708 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006709#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006710 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006711}
6712
6713
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714#if defined(FEAT_MENU) || defined(PROTO)
6715/*
6716 * Add a sub menu to the menu bar.
6717 */
6718 void
6719gui_mch_add_menu(
6720 vimmenu_T *menu,
6721 int pos)
6722{
6723 vimmenu_T *parent = menu->parent;
6724
6725 menu->submenu_id = CreatePopupMenu();
6726 menu->id = s_menu_id++;
6727
6728 if (menu_is_menubar(menu->name))
6729 {
6730 if (is_winnt_3())
6731 {
6732 InsertMenu((parent == NULL) ? s_menuBar : parent->submenu_id,
6733 (UINT)pos, MF_POPUP | MF_STRING | MF_BYPOSITION,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006734 (long_u)menu->submenu_id, (LPCTSTR) menu->name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 }
6736 else
6737 {
6738#ifdef FEAT_MBYTE
6739 WCHAR *wn = NULL;
6740 int n;
6741
6742 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6743 {
6744 /* 'encoding' differs from active codepage: convert menu name
6745 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006746 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 if (wn != NULL)
6748 {
6749 MENUITEMINFOW infow;
6750
6751 infow.cbSize = sizeof(infow);
6752 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6753 | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006754 infow.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 infow.wID = menu->id;
6756 infow.fType = MFT_STRING;
6757 infow.dwTypeData = wn;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006758 infow.cch = (UINT)wcslen(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 infow.hSubMenu = menu->submenu_id;
6760 n = InsertMenuItemW((parent == NULL)
6761 ? s_menuBar : parent->submenu_id,
6762 (UINT)pos, TRUE, &infow);
6763 vim_free(wn);
6764 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
6765 /* Failed, try using non-wide function. */
6766 wn = NULL;
6767 }
6768 }
6769
6770 if (wn == NULL)
6771#endif
6772 {
6773 MENUITEMINFO info;
6774
6775 info.cbSize = sizeof(info);
6776 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006777 info.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 info.wID = menu->id;
6779 info.fType = MFT_STRING;
6780 info.dwTypeData = (LPTSTR)menu->name;
6781 info.cch = (UINT)STRLEN(menu->name);
6782 info.hSubMenu = menu->submenu_id;
6783 InsertMenuItem((parent == NULL)
6784 ? s_menuBar : parent->submenu_id,
6785 (UINT)pos, TRUE, &info);
6786 }
6787 }
6788 }
6789
6790 /* Fix window size if menu may have wrapped */
6791 if (parent == NULL)
6792 gui_mswin_get_menu_height(!gui.starting);
6793#ifdef FEAT_TEAROFF
6794 else if (IsWindow(parent->tearoff_handle))
6795 rebuild_tearoff(parent);
6796#endif
6797}
6798
6799 void
6800gui_mch_show_popupmenu(vimmenu_T *menu)
6801{
6802 POINT mp;
6803
6804 (void)GetCursorPos((LPPOINT)&mp);
6805 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6806}
6807
6808 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006809gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810{
6811 vimmenu_T *menu = gui_find_menu(path_name);
6812
6813 if (menu != NULL)
6814 {
6815 POINT p;
6816
6817 /* Find the position of the current cursor */
6818 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006819 if (mouse_pos)
6820 {
6821 int mx, my;
6822
6823 gui_mch_getmouse(&mx, &my);
6824 p.x += mx;
6825 p.y += my;
6826 }
6827 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 {
6829 p.x += TEXT_X(W_WINCOL(curwin) + curwin->w_wcol + 1);
6830 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6831 }
6832 msg_scroll = FALSE;
6833 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6834 }
6835}
6836
6837#if defined(FEAT_TEAROFF) || defined(PROTO)
6838/*
6839 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6840 * create it as a pseudo-"tearoff menu".
6841 */
6842 void
6843gui_make_tearoff(char_u *path_name)
6844{
6845 vimmenu_T *menu = gui_find_menu(path_name);
6846
6847 /* Found the menu, so tear it off. */
6848 if (menu != NULL)
6849 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6850}
6851#endif
6852
6853/*
6854 * Add a menu item to a menu
6855 */
6856 void
6857gui_mch_add_menu_item(
6858 vimmenu_T *menu,
6859 int idx)
6860{
6861 vimmenu_T *parent = menu->parent;
6862
6863 menu->id = s_menu_id++;
6864 menu->submenu_id = NULL;
6865
6866#ifdef FEAT_TEAROFF
6867 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6868 {
6869 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6870 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6871 }
6872 else
6873#endif
6874#ifdef FEAT_TOOLBAR
6875 if (menu_is_toolbar(parent->name))
6876 {
6877 TBBUTTON newtb;
6878
6879 vim_memset(&newtb, 0, sizeof(newtb));
6880 if (menu_is_separator(menu->name))
6881 {
6882 newtb.iBitmap = 0;
6883 newtb.fsStyle = TBSTYLE_SEP;
6884 }
6885 else
6886 {
6887 newtb.iBitmap = get_toolbar_bitmap(menu);
6888 newtb.fsStyle = TBSTYLE_BUTTON;
6889 }
6890 newtb.idCommand = menu->id;
6891 newtb.fsState = TBSTATE_ENABLED;
6892 newtb.iString = 0;
6893 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6894 (LPARAM)&newtb);
6895 menu->submenu_id = (HMENU)-1;
6896 }
6897 else
6898#endif
6899 {
6900#ifdef FEAT_MBYTE
6901 WCHAR *wn = NULL;
6902 int n;
6903
6904 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6905 {
6906 /* 'encoding' differs from active codepage: convert menu item name
6907 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006908 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 if (wn != NULL)
6910 {
6911 n = InsertMenuW(parent->submenu_id, (UINT)idx,
6912 (menu_is_separator(menu->name)
6913 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6914 (UINT)menu->id, wn);
6915 vim_free(wn);
6916 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
6917 /* Failed, try using non-wide function. */
6918 wn = NULL;
6919 }
6920 }
6921 if (wn == NULL)
6922#endif
6923 InsertMenu(parent->submenu_id, (UINT)idx,
6924 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
6925 | MF_BYPOSITION,
6926 (UINT)menu->id, (LPCTSTR)menu->name);
6927#ifdef FEAT_TEAROFF
6928 if (IsWindow(parent->tearoff_handle))
6929 rebuild_tearoff(parent);
6930#endif
6931 }
6932}
6933
6934/*
6935 * Destroy the machine specific menu widget.
6936 */
6937 void
6938gui_mch_destroy_menu(vimmenu_T *menu)
6939{
6940#ifdef FEAT_TOOLBAR
6941 /*
6942 * is this a toolbar button?
6943 */
6944 if (menu->submenu_id == (HMENU)-1)
6945 {
6946 int iButton;
6947
6948 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6949 (WPARAM)menu->id, 0);
6950 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6951 }
6952 else
6953#endif
6954 {
6955 if (menu->parent != NULL
6956 && menu_is_popup(menu->parent->dname)
6957 && menu->parent->submenu_id != NULL)
6958 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6959 else
6960 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6961 if (menu->submenu_id != NULL)
6962 DestroyMenu(menu->submenu_id);
6963#ifdef FEAT_TEAROFF
6964 if (IsWindow(menu->tearoff_handle))
6965 DestroyWindow(menu->tearoff_handle);
6966 if (menu->parent != NULL
6967 && menu->parent->children != NULL
6968 && IsWindow(menu->parent->tearoff_handle))
6969 {
6970 /* This menu must not show up when rebuilding the tearoff window. */
6971 menu->modes = 0;
6972 rebuild_tearoff(menu->parent);
6973 }
6974#endif
6975 }
6976}
6977
6978#ifdef FEAT_TEAROFF
6979 static void
6980rebuild_tearoff(vimmenu_T *menu)
6981{
6982 /*hackish*/
6983 char_u tbuf[128];
6984 RECT trect;
6985 RECT rct;
6986 RECT roct;
6987 int x, y;
6988
6989 HWND thwnd = menu->tearoff_handle;
6990
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006991 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 if (GetWindowRect(thwnd, &trect)
6993 && GetWindowRect(s_hwnd, &rct)
6994 && GetClientRect(s_hwnd, &roct))
6995 {
6996 x = trect.left - rct.left;
6997 y = (trect.top - rct.bottom + roct.bottom);
6998 }
6999 else
7000 {
7001 x = y = 0xffffL;
7002 }
7003 DestroyWindow(thwnd);
7004 if (menu->children != NULL)
7005 {
7006 gui_mch_tearoff(tbuf, menu, x, y);
7007 if (IsWindow(menu->tearoff_handle))
7008 (void) SetWindowPos(menu->tearoff_handle,
7009 NULL,
7010 (int)trect.left,
7011 (int)trect.top,
7012 0, 0,
7013 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
7014 }
7015}
7016#endif /* FEAT_TEAROFF */
7017
7018/*
7019 * Make a menu either grey or not grey.
7020 */
7021 void
7022gui_mch_menu_grey(
7023 vimmenu_T *menu,
7024 int grey)
7025{
7026#ifdef FEAT_TOOLBAR
7027 /*
7028 * is this a toolbar button?
7029 */
7030 if (menu->submenu_id == (HMENU)-1)
7031 {
7032 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
7033 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
7034 }
7035 else
7036#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02007037 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
7038 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039
7040#ifdef FEAT_TEAROFF
7041 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
7042 {
7043 WORD menuID;
7044 HWND menuHandle;
7045
7046 /*
7047 * A tearoff button has changed state.
7048 */
7049 if (menu->children == NULL)
7050 menuID = (WORD)(menu->id);
7051 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007052 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
7054 if (menuHandle)
7055 EnableWindow(menuHandle, !grey);
7056
7057 }
7058#endif
7059}
7060
7061#endif /* FEAT_MENU */
7062
7063
7064/* define some macros used to make the dialogue creation more readable */
7065
7066#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
7067#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007068#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069
7070#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
7071/*
7072 * stuff for dialogs
7073 */
7074
7075/*
7076 * The callback routine used by all the dialogs. Very simple. First,
7077 * acknowledges the INITDIALOG message so that Windows knows to do standard
7078 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
7079 * pressed, return that button's ID - IDCANCEL (2), which is the button's
7080 * number.
7081 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007082/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 static LRESULT CALLBACK
7084dialog_callback(
7085 HWND hwnd,
7086 UINT message,
7087 WPARAM wParam,
7088 LPARAM lParam)
7089{
7090 if (message == WM_INITDIALOG)
7091 {
7092 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
7093 /* Set focus to the dialog. Set the default button, if specified. */
7094 (void)SetFocus(hwnd);
7095 if (dialog_default_button > IDCANCEL)
7096 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007097 else
7098 /* We don't have a default, set focus on another element of the
7099 * dialog window, probably the icon */
7100 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 return FALSE;
7102 }
7103
7104 if (message == WM_COMMAND)
7105 {
7106 int button = LOWORD(wParam);
7107
7108 /* Don't end the dialog if something was selected that was
7109 * not a button.
7110 */
7111 if (button >= DLG_NONBUTTON_CONTROL)
7112 return TRUE;
7113
7114 /* If the edit box exists, copy the string. */
7115 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007116 {
7117# if defined(FEAT_MBYTE) && defined(WIN3264)
7118 /* If the OS is Windows NT, and 'encoding' differs from active
7119 * codepage: use wide function and convert text. */
7120 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
7121 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007122 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007123 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
7124 char_u *p;
7125
7126 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
7127 p = utf16_to_enc(wp, NULL);
7128 vim_strncpy(s_textfield, p, IOSIZE);
7129 vim_free(p);
7130 vim_free(wp);
7131 }
7132 else
7133# endif
7134 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007135 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137
7138 /*
7139 * Need to check for IDOK because if the user just hits Return to
7140 * accept the default value, some reason this is what we get.
7141 */
7142 if (button == IDOK)
7143 {
7144 if (dialog_default_button > IDCANCEL)
7145 EndDialog(hwnd, dialog_default_button);
7146 }
7147 else
7148 EndDialog(hwnd, button - IDCANCEL);
7149 return TRUE;
7150 }
7151
7152 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7153 {
7154 EndDialog(hwnd, 0);
7155 return TRUE;
7156 }
7157 return FALSE;
7158}
7159
7160/*
7161 * Create a dialog dynamically from the parameter strings.
7162 * type = type of dialog (question, alert, etc.)
7163 * title = dialog title. may be NULL for default title.
7164 * message = text to display. Dialog sizes to accommodate it.
7165 * buttons = '\n' separated list of button captions, default first.
7166 * dfltbutton = number of default button.
7167 *
7168 * This routine returns 1 if the first button is pressed,
7169 * 2 for the second, etc.
7170 *
7171 * 0 indicates Esc was pressed.
7172 * -1 for unexpected error
7173 *
7174 * If stubbing out this fn, return 1.
7175 */
7176
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007177static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178{
7179 "IDR_VIM",
7180 "IDR_VIM_ERROR",
7181 "IDR_VIM_ALERT",
7182 "IDR_VIM_INFO",
7183 "IDR_VIM_QUESTION"
7184};
7185
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 int
7187gui_mch_dialog(
7188 int type,
7189 char_u *title,
7190 char_u *message,
7191 char_u *buttons,
7192 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007193 char_u *textfield,
7194 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195{
7196 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007197 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 int numButtons;
7199 int *buttonWidths, *buttonPositions;
7200 int buttonYpos;
7201 int nchar, i;
7202 DWORD lStyle;
7203 int dlgwidth = 0;
7204 int dlgheight;
7205 int editboxheight;
7206 int horizWidth = 0;
7207 int msgheight;
7208 char_u *pstart;
7209 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007210 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 char_u *tbuffer;
7212 RECT rect;
7213 HWND hwnd;
7214 HDC hdc;
7215 HFONT font, oldFont;
7216 TEXTMETRIC fontInfo;
7217 int fontHeight;
7218 int textWidth, minButtonWidth, messageWidth;
7219 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007220 int maxDialogHeight;
7221 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 int vertical;
7223 int dlgPaddingX;
7224 int dlgPaddingY;
7225#ifdef USE_SYSMENU_FONT
7226 LOGFONT lfSysmenu;
7227 int use_lfSysmenu = FALSE;
7228#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007229 garray_T ga;
7230 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231
7232#ifndef NO_CONSOLE
7233 /* Don't output anything in silent mode ("ex -s") */
7234 if (silent_mode)
7235 return dfltbutton; /* return default option */
7236#endif
7237
Bram Moolenaar748bf032005-02-02 23:04:36 +00007238 if (s_hwnd == NULL)
7239 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240
7241 if ((type < 0) || (type > VIM_LAST_TYPE))
7242 type = 0;
7243
7244 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007245 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007246 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007247 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248
7249 if (p == NULL)
7250 return -1;
7251
7252 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007253 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7255 * const.
7256 */
7257 tbuffer = vim_strsave(buttons);
7258 if (tbuffer == NULL)
7259 return -1;
7260
7261 --dfltbutton; /* Change from one-based to zero-based */
7262
7263 /* Count buttons */
7264 numButtons = 1;
7265 for (i = 0; tbuffer[i] != '\0'; i++)
7266 {
7267 if (tbuffer[i] == DLG_BUTTON_SEP)
7268 numButtons++;
7269 }
7270 if (dfltbutton >= numButtons)
7271 dfltbutton = -1;
7272
7273 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007274 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 if (buttonWidths == NULL)
7276 return -1;
7277
7278 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007279 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 if (buttonPositions == NULL)
7281 return -1;
7282
7283 /*
7284 * Calculate how big the dialog must be.
7285 */
7286 hwnd = GetDesktopWindow();
7287 hdc = GetWindowDC(hwnd);
7288#ifdef USE_SYSMENU_FONT
7289 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7290 {
7291 font = CreateFontIndirect(&lfSysmenu);
7292 use_lfSysmenu = TRUE;
7293 }
7294 else
7295#endif
7296 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7297 VARIABLE_PITCH , DLG_FONT_NAME);
7298 if (s_usenewlook)
7299 {
7300 oldFont = SelectFont(hdc, font);
7301 dlgPaddingX = DLG_PADDING_X;
7302 dlgPaddingY = DLG_PADDING_Y;
7303 }
7304 else
7305 {
7306 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7307 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7308 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7309 }
7310 GetTextMetrics(hdc, &fontInfo);
7311 fontHeight = fontInfo.tmHeight;
7312
7313 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007314 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315
7316 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007317 if (s_hwnd == NULL)
7318 {
7319 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320
Bram Moolenaarc716c302006-01-21 22:12:51 +00007321 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007322 get_work_area(&workarea_rect);
7323 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7324 if (maxDialogWidth > 600)
7325 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007326 /* Leave some room for the taskbar. */
7327 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007328 }
7329 else
7330 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02007331 /* Use our own window for the size, unless it's very small. */
7332 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007333 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02007334 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007335 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007336 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
7337 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007338
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007339 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007340 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007341 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02007342 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007343 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
7344 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
7345 }
7346
7347 /* Set dlgwidth to width of message.
7348 * Copy the message into "ga", changing NL to CR-NL and inserting line
7349 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 pstart = message;
7351 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007352 msgheight = 0;
7353 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 do
7355 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007356 msgheight += fontHeight; /* at least one line */
7357
7358 /* Need to figure out where to break the string. The system does it
7359 * at a word boundary, which would mean we can't compute the number of
7360 * wrapped lines. */
7361 textWidth = 0;
7362 last_white = NULL;
7363 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007364 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007365#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007366 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007367#else
7368 l = 1;
7369#endif
7370 if (l == 1 && vim_iswhite(*pend)
7371 && textWidth > maxDialogWidth * 3 / 4)
7372 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007373 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007374 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007375 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007376 /* Line will wrap. */
7377 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007378 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007379 textWidth = 0;
7380
7381 if (last_white != NULL)
7382 {
7383 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007384 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007385 pend = last_white + 1;
7386 last_white = NULL;
7387 }
7388 ga_append(&ga, '\r');
7389 ga_append(&ga, '\n');
7390 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007391 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007392
7393 while (--l >= 0)
7394 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007395 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007396 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007398
7399 ga_append(&ga, '\r');
7400 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 pstart = pend + 1;
7402 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007403
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007404 if (ga.ga_data != NULL)
7405 message = ga.ga_data;
7406
Bram Moolenaar748bf032005-02-02 23:04:36 +00007407 messageWidth += 10; /* roundoff space */
7408
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02007410 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7411 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412
7413 if (msgheight < DLG_ICON_HEIGHT)
7414 msgheight = DLG_ICON_HEIGHT;
7415
7416 /*
7417 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007418 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007420 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 if (!vertical)
7422 {
7423 // Place buttons horizontally if they fit.
7424 horizWidth = dlgPaddingX;
7425 pstart = tbuffer;
7426 i = 0;
7427 do
7428 {
7429 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7430 if (pend == NULL)
7431 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007432 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 if (textWidth < minButtonWidth)
7434 textWidth = minButtonWidth;
7435 textWidth += dlgPaddingX; /* Padding within button */
7436 buttonWidths[i] = textWidth;
7437 buttonPositions[i++] = horizWidth;
7438 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7439 pstart = pend + 1;
7440 } while (*pend != NUL);
7441
7442 if (horizWidth > maxDialogWidth)
7443 vertical = TRUE; // Too wide to fit on the screen.
7444 else if (horizWidth > dlgwidth)
7445 dlgwidth = horizWidth;
7446 }
7447
7448 if (vertical)
7449 {
7450 // Stack buttons vertically.
7451 pstart = tbuffer;
7452 do
7453 {
7454 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7455 if (pend == NULL)
7456 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007457 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 textWidth += dlgPaddingX; /* Padding within button */
7459 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7460 if (textWidth > dlgwidth)
7461 dlgwidth = textWidth;
7462 pstart = pend + 1;
7463 } while (*pend != NUL);
7464 }
7465
7466 if (dlgwidth < DLG_MIN_WIDTH)
7467 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7468
7469 /* start to fill in the dlgtemplate information. addressing by WORDs */
7470 if (s_usenewlook)
7471 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7472 else
7473 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7474
7475 add_long(lStyle);
7476 add_long(0); // (lExtendedStyle)
7477 pnumitems = p; /*save where the number of items must be stored*/
7478 add_word(0); // NumberOfItems(will change later)
7479 add_word(10); // x
7480 add_word(10); // y
7481 add_word(PixelToDialogX(dlgwidth)); // cx
7482
7483 // Dialog height.
7484 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007485 dlgheight = msgheight + 2 * dlgPaddingY
7486 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 else
7488 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7489
7490 // Dialog needs to be taller if contains an edit box.
7491 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7492 if (textfield != NULL)
7493 dlgheight += editboxheight;
7494
Bram Moolenaara95d8232013-08-07 15:27:11 +02007495 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7496 if (dlgheight > maxDialogHeight)
7497 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007498 msgheight = msgheight - (dlgheight - maxDialogHeight);
7499 dlgheight = maxDialogHeight;
7500 scroll_flag = WS_VSCROLL;
7501 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7502 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007503 }
7504
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 add_word(PixelToDialogY(dlgheight));
7506
7507 add_word(0); // Menu
7508 add_word(0); // Class
7509
7510 /* copy the title of the dialog */
7511 nchar = nCopyAnsiToWideChar(p, (title ?
7512 (LPSTR)title :
7513 (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
7514 p += nchar;
7515
7516 if (s_usenewlook)
7517 {
7518 /* do the font, since DS_3DLOOK doesn't work properly */
7519#ifdef USE_SYSMENU_FONT
7520 if (use_lfSysmenu)
7521 {
7522 /* point size */
7523 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7524 GetDeviceCaps(hdc, LOGPIXELSY));
7525 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
7526 }
7527 else
7528#endif
7529 {
7530 *p++ = DLG_FONT_POINT_SIZE; // point size
7531 nchar = nCopyAnsiToWideChar(p, TEXT(DLG_FONT_NAME));
7532 }
7533 p += nchar;
7534 }
7535
7536 buttonYpos = msgheight + 2 * dlgPaddingY;
7537
7538 if (textfield != NULL)
7539 buttonYpos += editboxheight;
7540
7541 pstart = tbuffer;
7542 if (!vertical)
7543 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7544 for (i = 0; i < numButtons; i++)
7545 {
7546 /* get end of this button. */
7547 for ( pend = pstart;
7548 *pend && (*pend != DLG_BUTTON_SEP);
7549 pend++)
7550 ;
7551
7552 if (*pend)
7553 *pend = '\0';
7554
7555 /*
7556 * old NOTE:
7557 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7558 * the focus to the first tab-able button and in so doing makes that
7559 * the default!! Grrr. Workaround: Make the default button the only
7560 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7561 * he/she can use arrow keys.
7562 *
7563 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007564 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 * dialog. Also needed for when the textfield is the default control.
7566 * It appears to work now (perhaps not on Win95?).
7567 */
7568 if (vertical)
7569 {
7570 p = add_dialog_element(p,
7571 (i == dfltbutton
7572 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7573 PixelToDialogX(DLG_VERT_PADDING_X),
7574 PixelToDialogY(buttonYpos /* TBK */
7575 + 2 * fontHeight * i),
7576 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7577 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007578 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 }
7580 else
7581 {
7582 p = add_dialog_element(p,
7583 (i == dfltbutton
7584 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7585 PixelToDialogX(horizWidth + buttonPositions[i]),
7586 PixelToDialogY(buttonYpos), /* TBK */
7587 PixelToDialogX(buttonWidths[i]),
7588 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007589 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 }
7591 pstart = pend + 1; /*next button*/
7592 }
7593 *pnumitems += numButtons;
7594
7595 /* Vim icon */
7596 p = add_dialog_element(p, SS_ICON,
7597 PixelToDialogX(dlgPaddingX),
7598 PixelToDialogY(dlgPaddingY),
7599 PixelToDialogX(DLG_ICON_WIDTH),
7600 PixelToDialogY(DLG_ICON_HEIGHT),
7601 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7602 dlg_icons[type]);
7603
Bram Moolenaar748bf032005-02-02 23:04:36 +00007604 /* Dialog message */
7605 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7606 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7607 PixelToDialogY(dlgPaddingY),
7608 (WORD)(PixelToDialogX(messageWidth) + 1),
7609 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007610 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611
7612 /* Edit box */
7613 if (textfield != NULL)
7614 {
7615 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7616 PixelToDialogX(2 * dlgPaddingX),
7617 PixelToDialogY(2 * dlgPaddingY + msgheight),
7618 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7619 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007620 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 *pnumitems += 1;
7622 }
7623
7624 *pnumitems += 2;
7625
7626 SelectFont(hdc, oldFont);
7627 DeleteObject(font);
7628 ReleaseDC(hwnd, hdc);
7629
7630 /* Let the dialog_callback() function know which button to make default
7631 * If we have an edit box, make that the default. We also need to tell
7632 * dialog_callback() if this dialog contains an edit box or not. We do
7633 * this by setting s_textfield if it does.
7634 */
7635 if (textfield != NULL)
7636 {
7637 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7638 s_textfield = textfield;
7639 }
7640 else
7641 {
7642 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7643 s_textfield = NULL;
7644 }
7645
7646 /* show the dialog box modally and get a return value */
7647 nchar = (int)DialogBoxIndirect(
7648 s_hinst,
7649 (LPDLGTEMPLATE)pdlgtemplate,
7650 s_hwnd,
7651 (DLGPROC)dialog_callback);
7652
7653 LocalFree(LocalHandle(pdlgtemplate));
7654 vim_free(tbuffer);
7655 vim_free(buttonWidths);
7656 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007657 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658
7659 /* Focus back to our window (for when MDI is used). */
7660 (void)SetFocus(s_hwnd);
7661
7662 return nchar;
7663}
7664
7665#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007666
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667/*
7668 * Put a simple element (basic class) onto a dialog template in memory.
7669 * return a pointer to where the next item should be added.
7670 *
7671 * parameters:
7672 * lStyle = additional style flags
7673 * (be careful, NT3.51 & Win32s will ignore the new ones)
7674 * x,y = x & y positions IN DIALOG UNITS
7675 * w,h = width and height IN DIALOG UNITS
7676 * Id = ID used in messages
7677 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7678 * caption = usually text or resource name
7679 *
7680 * TODO: use the length information noted here to enable the dialog creation
7681 * routines to work out more exactly how much memory they need to alloc.
7682 */
7683 static PWORD
7684add_dialog_element(
7685 PWORD p,
7686 DWORD lStyle,
7687 WORD x,
7688 WORD y,
7689 WORD w,
7690 WORD h,
7691 WORD Id,
7692 WORD clss,
7693 const char *caption)
7694{
7695 int nchar;
7696
7697 p = lpwAlign(p); /* Align to dword boundary*/
7698 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7699 *p++ = LOWORD(lStyle);
7700 *p++ = HIWORD(lStyle);
7701 *p++ = 0; // LOWORD (lExtendedStyle)
7702 *p++ = 0; // HIWORD (lExtendedStyle)
7703 *p++ = x;
7704 *p++ = y;
7705 *p++ = w;
7706 *p++ = h;
7707 *p++ = Id; //9 or 10 words in all
7708
7709 *p++ = (WORD)0xffff;
7710 *p++ = clss; //2 more here
7711
7712 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption); //strlen(caption)+1
7713 p += nchar;
7714
7715 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7716
7717 return p; //total = 15+ (strlen(caption)) words
7718 // = 30 + 2(strlen(caption) bytes reqd
7719}
7720
7721
7722/*
7723 * Helper routine. Take an input pointer, return closest pointer that is
7724 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7725 */
7726 static LPWORD
7727lpwAlign(
7728 LPWORD lpIn)
7729{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007730 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007732 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 ul += 3;
7734 ul >>= 2;
7735 ul <<= 2;
7736 return (LPWORD)ul;
7737}
7738
7739/*
7740 * Helper routine. Takes second parameter as Ansi string, copies it to first
7741 * parameter as wide character (16-bits / char) string, and returns integer
7742 * number of wide characters (words) in string (including the trailing wide
7743 * char NULL). Partly taken from the Win32SDK samples.
7744 */
7745 static int
7746nCopyAnsiToWideChar(
7747 LPWORD lpWCStr,
7748 LPSTR lpAnsiIn)
7749{
7750 int nChar = 0;
7751#ifdef FEAT_MBYTE
7752 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7753 int i;
7754 WCHAR *wn;
7755
7756 if (enc_codepage == 0 && (int)GetACP() != enc_codepage)
7757 {
7758 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007759 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 if (wn != NULL)
7761 {
7762 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007763 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 vim_free(wn);
7765 }
7766 }
7767 if (nChar == 0)
7768 /* Use Win32 conversion function. */
7769 nChar = MultiByteToWideChar(
7770 enc_codepage > 0 ? enc_codepage : CP_ACP,
7771 MB_PRECOMPOSED,
7772 lpAnsiIn, len,
7773 lpWCStr, len);
7774 for (i = 0; i < nChar; ++i)
7775 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7776 lpWCStr[i] = (WORD)' ';
7777#else
7778 do
7779 {
7780 if (*lpAnsiIn == '\t')
7781 *lpWCStr++ = (WORD)' ';
7782 else
7783 *lpWCStr++ = (WORD)*lpAnsiIn;
7784 nChar++;
7785 } while (*lpAnsiIn++);
7786#endif
7787
7788 return nChar;
7789}
7790
7791
7792#ifdef FEAT_TEAROFF
7793/*
7794 * The callback function for all the modeless dialogs that make up the
7795 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7796 * thinking its menus have been clicked), and go away when closed.
7797 */
7798 static LRESULT CALLBACK
7799tearoff_callback(
7800 HWND hwnd,
7801 UINT message,
7802 WPARAM wParam,
7803 LPARAM lParam)
7804{
7805 if (message == WM_INITDIALOG)
7806 return (TRUE);
7807
7808 /* May show the mouse pointer again. */
7809 HandleMouseHide(message, lParam);
7810
7811 if (message == WM_COMMAND)
7812 {
7813 if ((WORD)(LOWORD(wParam)) & 0x8000)
7814 {
7815 POINT mp;
7816 RECT rect;
7817
7818 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7819 {
7820 (void)TrackPopupMenu(
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007821 (HMENU)(long_u)(LOWORD(wParam) ^ 0x8000),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7823 (int)rect.right - 8,
7824 (int)mp.y,
7825 (int)0, /*reserved param*/
7826 s_hwnd,
7827 NULL);
7828 /*
7829 * NOTE: The pop-up menu can eat the mouse up event.
7830 * We deal with this in normal.c.
7831 */
7832 }
7833 }
7834 else
7835 /* Pass on messages to the main Vim window */
7836 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7837 /*
7838 * Give main window the focus back: this is so after
7839 * choosing a tearoff button you can start typing again
7840 * straight away.
7841 */
7842 (void)SetFocus(s_hwnd);
7843 return TRUE;
7844 }
7845 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7846 {
7847 DestroyWindow(hwnd);
7848 return TRUE;
7849 }
7850
7851 /* When moved around, give main window the focus back. */
7852 if (message == WM_EXITSIZEMOVE)
7853 (void)SetActiveWindow(s_hwnd);
7854
7855 return FALSE;
7856}
7857#endif
7858
7859
7860/*
7861 * Decide whether to use the "new look" (small, non-bold font) or the "old
7862 * look" (big, clanky font) for dialogs, and work out a few values for use
7863 * later accordingly.
7864 */
7865 static void
7866get_dialog_font_metrics(void)
7867{
7868 HDC hdc;
7869 HFONT hfontTools = 0;
7870 DWORD dlgFontSize;
7871 SIZE size;
7872#ifdef USE_SYSMENU_FONT
7873 LOGFONT lfSysmenu;
7874#endif
7875
7876 s_usenewlook = FALSE;
7877
7878 /*
7879 * For NT3.51 and Win32s, we stick with the old look
7880 * because it matches everything else.
7881 */
7882 if (!is_winnt_3())
7883 {
7884#ifdef USE_SYSMENU_FONT
7885 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7886 hfontTools = CreateFontIndirect(&lfSysmenu);
7887 else
7888#endif
7889 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7890 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7891
7892 if (hfontTools)
7893 {
7894 hdc = GetDC(s_hwnd);
7895 SelectObject(hdc, hfontTools);
7896 /*
7897 * GetTextMetrics() doesn't return the right value in
7898 * tmAveCharWidth, so we have to figure out the dialog base units
7899 * ourselves.
7900 */
7901 GetTextExtentPoint(hdc,
7902 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7903 52, &size);
7904 ReleaseDC(s_hwnd, hdc);
7905
7906 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7907 s_dlgfntheight = (WORD)size.cy;
7908 s_usenewlook = TRUE;
7909 }
7910 }
7911
7912 if (!s_usenewlook)
7913 {
7914 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7915 s_dlgfntwidth = LOWORD(dlgFontSize);
7916 s_dlgfntheight = HIWORD(dlgFontSize);
7917 }
7918}
7919
7920#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7921/*
7922 * Create a pseudo-"tearoff menu" based on the child
7923 * items of a given menu pointer.
7924 */
7925 static void
7926gui_mch_tearoff(
7927 char_u *title,
7928 vimmenu_T *menu,
7929 int initX,
7930 int initY)
7931{
7932 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7933 int template_len;
7934 int nchar, textWidth, submenuWidth;
7935 DWORD lStyle;
7936 DWORD lExtendedStyle;
7937 WORD dlgwidth;
7938 WORD menuID;
7939 vimmenu_T *pmenu;
7940 vimmenu_T *the_menu = menu;
7941 HWND hwnd;
7942 HDC hdc;
7943 HFONT font, oldFont;
7944 int col, spaceWidth, len;
7945 int columnWidths[2];
7946 char_u *label, *text;
7947 int acLen = 0;
7948 int nameLen;
7949 int padding0, padding1, padding2 = 0;
7950 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007951 int x;
7952 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953#ifdef USE_SYSMENU_FONT
7954 LOGFONT lfSysmenu;
7955 int use_lfSysmenu = FALSE;
7956#endif
7957
7958 /*
7959 * If this menu is already torn off, move it to the mouse position.
7960 */
7961 if (IsWindow(menu->tearoff_handle))
7962 {
7963 POINT mp;
7964 if (GetCursorPos((LPPOINT)&mp))
7965 {
7966 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7967 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7968 }
7969 return;
7970 }
7971
7972 /*
7973 * Create a new tearoff.
7974 */
7975 if (*title == MNU_HIDDEN_CHAR)
7976 title++;
7977
7978 /* Allocate memory to store the dialog template. It's made bigger when
7979 * needed. */
7980 template_len = DLG_ALLOC_SIZE;
7981 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7982 if (p == NULL)
7983 return;
7984
7985 hwnd = GetDesktopWindow();
7986 hdc = GetWindowDC(hwnd);
7987#ifdef USE_SYSMENU_FONT
7988 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7989 {
7990 font = CreateFontIndirect(&lfSysmenu);
7991 use_lfSysmenu = TRUE;
7992 }
7993 else
7994#endif
7995 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7996 VARIABLE_PITCH , DLG_FONT_NAME);
7997 if (s_usenewlook)
7998 oldFont = SelectFont(hdc, font);
7999 else
8000 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
8001
8002 /* Calculate width of a single space. Used for padding columns to the
8003 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008004 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005
8006 /* Figure out max width of the text column, the accelerator column and the
8007 * optional submenu column. */
8008 submenuWidth = 0;
8009 for (col = 0; col < 2; col++)
8010 {
8011 columnWidths[col] = 0;
8012 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
8013 {
8014 /* Use "dname" here to compute the width of the visible text. */
8015 text = (col == 0) ? pmenu->dname : pmenu->actext;
8016 if (text != NULL && *text != NUL)
8017 {
8018 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
8019 if (textWidth > columnWidths[col])
8020 columnWidths[col] = textWidth;
8021 }
8022 if (pmenu->children != NULL)
8023 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
8024 }
8025 }
8026 if (columnWidths[1] == 0)
8027 {
8028 /* no accelerators */
8029 if (submenuWidth != 0)
8030 columnWidths[0] += submenuWidth;
8031 else
8032 columnWidths[0] += spaceWidth;
8033 }
8034 else
8035 {
8036 /* there is an accelerator column */
8037 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
8038 columnWidths[1] += submenuWidth;
8039 }
8040
8041 /*
8042 * Now find the total width of our 'menu'.
8043 */
8044 textWidth = columnWidths[0] + columnWidths[1];
8045 if (submenuWidth != 0)
8046 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008047 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
8049 textWidth += submenuWidth;
8050 }
8051 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
8052 if (textWidth > dlgwidth)
8053 dlgwidth = textWidth;
8054 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
8055
8056 /* W95 can't do thin dialogs, they look v. weird! */
8057 if (mch_windows95() && dlgwidth < TEAROFF_MIN_WIDTH)
8058 dlgwidth = TEAROFF_MIN_WIDTH;
8059
8060 /* start to fill in the dlgtemplate information. addressing by WORDs */
8061 if (s_usenewlook)
8062 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
8063 else
8064 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
8065
8066 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
8067 *p++ = LOWORD(lStyle);
8068 *p++ = HIWORD(lStyle);
8069 *p++ = LOWORD(lExtendedStyle);
8070 *p++ = HIWORD(lExtendedStyle);
8071 pnumitems = p; /* save where the number of items must be stored */
8072 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008073 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008075 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 else
8077 *p++ = PixelToDialogX(initX); // x
8078 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008079 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 else
8081 *p++ = PixelToDialogY(initY); // y
8082 *p++ = PixelToDialogX(dlgwidth); // cx
8083 ptrueheight = p;
8084 *p++ = 0; // dialog height: changed later anyway
8085 *p++ = 0; // Menu
8086 *p++ = 0; // Class
8087
8088 /* copy the title of the dialog */
8089 nchar = nCopyAnsiToWideChar(p, ((*title)
8090 ? (LPSTR)title
8091 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
8092 p += nchar;
8093
8094 if (s_usenewlook)
8095 {
8096 /* do the font, since DS_3DLOOK doesn't work properly */
8097#ifdef USE_SYSMENU_FONT
8098 if (use_lfSysmenu)
8099 {
8100 /* point size */
8101 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
8102 GetDeviceCaps(hdc, LOGPIXELSY));
8103 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
8104 }
8105 else
8106#endif
8107 {
8108 *p++ = DLG_FONT_POINT_SIZE; // point size
8109 nchar = nCopyAnsiToWideChar (p, TEXT(DLG_FONT_NAME));
8110 }
8111 p += nchar;
8112 }
8113
8114 /*
8115 * Loop over all the items in the menu.
8116 * But skip over the tearbar.
8117 */
8118 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
8119 menu = menu->children->next;
8120 else
8121 menu = menu->children;
8122 for ( ; menu != NULL; menu = menu->next)
8123 {
8124 if (menu->modes == 0) /* this menu has just been deleted */
8125 continue;
8126 if (menu_is_separator(menu->dname))
8127 {
8128 sepPadding += 3;
8129 continue;
8130 }
8131
8132 /* Check if there still is plenty of room in the template. Make it
8133 * larger when needed. */
8134 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8135 {
8136 WORD *newp;
8137
8138 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8139 if (newp != NULL)
8140 {
8141 template_len += 4096;
8142 mch_memmove(newp, pdlgtemplate,
8143 (char *)p - (char *)pdlgtemplate);
8144 p = newp + (p - pdlgtemplate);
8145 pnumitems = newp + (pnumitems - pdlgtemplate);
8146 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8147 LocalFree(LocalHandle(pdlgtemplate));
8148 pdlgtemplate = newp;
8149 }
8150 }
8151
8152 /* Figure out minimal length of this menu label. Use "name" for the
8153 * actual text, "dname" for estimating the displayed size. "name"
8154 * has "&a" for mnemonic and includes the accelerator. */
8155 len = nameLen = (int)STRLEN(menu->name);
8156 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8157 (int)STRLEN(menu->dname))) / spaceWidth;
8158 len += padding0;
8159
8160 if (menu->actext != NULL)
8161 {
8162 acLen = (int)STRLEN(menu->actext);
8163 len += acLen;
8164 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8165 }
8166 else
8167 textWidth = 0;
8168 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8169 len += padding1;
8170
8171 if (menu->children == NULL)
8172 {
8173 padding2 = submenuWidth / spaceWidth;
8174 len += padding2;
8175 menuID = (WORD)(menu->id);
8176 }
8177 else
8178 {
8179 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008180 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 }
8182
8183 /* Allocate menu label and fill it in */
8184 text = label = alloc((unsigned)len + 1);
8185 if (label == NULL)
8186 break;
8187
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008188 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 text = vim_strchr(text, TAB); /* stop at TAB before actext */
8190 if (text == NULL)
8191 text = label + nameLen; /* no actext, use whole name */
8192 while (padding0-- > 0)
8193 *text++ = ' ';
8194 if (menu->actext != NULL)
8195 {
8196 STRNCPY(text, menu->actext, acLen);
8197 text += acLen;
8198 }
8199 while (padding1-- > 0)
8200 *text++ = ' ';
8201 if (menu->children != NULL)
8202 {
8203 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8204 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8205 }
8206 else
8207 {
8208 while (padding2-- > 0)
8209 *text++ = ' ';
8210 }
8211 *text = NUL;
8212
8213 /*
8214 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8215 * W95/NT4 it makes the tear-off look more like a menu.
8216 */
8217 p = add_dialog_element(p,
8218 BS_PUSHBUTTON|BS_LEFT,
8219 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8220 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8221 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8222 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008223 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 vim_free(label);
8225 (*pnumitems)++;
8226 }
8227
8228 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8229
8230
8231 /* show modelessly */
8232 the_menu->tearoff_handle = CreateDialogIndirect(
8233 s_hinst,
8234 (LPDLGTEMPLATE)pdlgtemplate,
8235 s_hwnd,
8236 (DLGPROC)tearoff_callback);
8237
8238 LocalFree(LocalHandle(pdlgtemplate));
8239 SelectFont(hdc, oldFont);
8240 DeleteObject(font);
8241 ReleaseDC(hwnd, hdc);
8242
8243 /*
8244 * Reassert ourselves as the active window. This is so that after creating
8245 * a tearoff, the user doesn't have to click with the mouse just to start
8246 * typing again!
8247 */
8248 (void)SetActiveWindow(s_hwnd);
8249
8250 /* make sure the right buttons are enabled */
8251 force_menu_update = TRUE;
8252}
8253#endif
8254
8255#if defined(FEAT_TOOLBAR) || defined(PROTO)
8256#include "gui_w32_rc.h"
8257
8258/* This not defined in older SDKs */
8259# ifndef TBSTYLE_FLAT
8260# define TBSTYLE_FLAT 0x0800
8261# endif
8262
8263/*
8264 * Create the toolbar, initially unpopulated.
8265 * (just like the menu, there are no defaults, it's all
8266 * set up through menu.vim)
8267 */
8268 static void
8269initialise_toolbar(void)
8270{
8271 InitCommonControls();
8272 s_toolbarhwnd = CreateToolbarEx(
8273 s_hwnd,
8274 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8275 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008276 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 s_hinst,
8278 IDR_TOOLBAR1, // id of initial bitmap
8279 NULL,
8280 0, // initial number of buttons
8281 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8282 TOOLBAR_BUTTON_HEIGHT,
8283 TOOLBAR_BUTTON_WIDTH,
8284 TOOLBAR_BUTTON_HEIGHT,
8285 sizeof(TBBUTTON)
8286 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008287 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288
8289 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
8290}
8291
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008292 static LRESULT CALLBACK
8293toolbar_wndproc(
8294 HWND hwnd,
8295 UINT uMsg,
8296 WPARAM wParam,
8297 LPARAM lParam)
8298{
8299 HandleMouseHide(uMsg, lParam);
8300 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8301}
8302
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 static int
8304get_toolbar_bitmap(vimmenu_T *menu)
8305{
8306 int i = -1;
8307
8308 /*
8309 * Check user bitmaps first, unless builtin is specified.
8310 */
8311 if (!is_winnt_3() && !menu->icon_builtin)
8312 {
8313 char_u fname[MAXPATHL];
8314 HANDLE hbitmap = NULL;
8315
8316 if (menu->iconfile != NULL)
8317 {
8318 gui_find_iconfile(menu->iconfile, fname, "bmp");
8319 hbitmap = LoadImage(
8320 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008321 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 IMAGE_BITMAP,
8323 TOOLBAR_BUTTON_WIDTH,
8324 TOOLBAR_BUTTON_HEIGHT,
8325 LR_LOADFROMFILE |
8326 LR_LOADMAP3DCOLORS
8327 );
8328 }
8329
8330 /*
8331 * If the LoadImage call failed, or the "icon=" file
8332 * didn't exist or wasn't specified, try the menu name
8333 */
8334 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008335 && (gui_find_bitmap(
8336#ifdef FEAT_MULTI_LANG
8337 menu->en_dname != NULL ? menu->en_dname :
8338#endif
8339 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 hbitmap = LoadImage(
8341 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008342 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 IMAGE_BITMAP,
8344 TOOLBAR_BUTTON_WIDTH,
8345 TOOLBAR_BUTTON_HEIGHT,
8346 LR_LOADFROMFILE |
8347 LR_LOADMAP3DCOLORS
8348 );
8349
8350 if (hbitmap != NULL)
8351 {
8352 TBADDBITMAP tbAddBitmap;
8353
8354 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008355 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356
8357 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8358 (WPARAM)1, (LPARAM)&tbAddBitmap);
8359 /* i will be set to -1 if it fails */
8360 }
8361 }
8362 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8363 i = menu->iconidx;
8364
8365 return i;
8366}
8367#endif
8368
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008369#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8370 static void
8371initialise_tabline(void)
8372{
8373 InitCommonControls();
8374
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008375 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008376 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008377 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8378 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008379 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008380
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008381 gui.tabline_height = TABLINE_HEIGHT;
8382
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008383# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008384 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008385# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008386}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008387
8388 static LRESULT CALLBACK
8389tabline_wndproc(
8390 HWND hwnd,
8391 UINT uMsg,
8392 WPARAM wParam,
8393 LPARAM lParam)
8394{
8395 HandleMouseHide(uMsg, lParam);
8396 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8397}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008398#endif
8399
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8401/*
8402 * Make the GUI window come to the foreground.
8403 */
8404 void
8405gui_mch_set_foreground(void)
8406{
8407 if (IsIconic(s_hwnd))
8408 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8409 SetForegroundWindow(s_hwnd);
8410}
8411#endif
8412
8413#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8414 static void
8415dyn_imm_load(void)
8416{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008417 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 if (hLibImm == NULL)
8419 return;
8420
8421 pImmGetCompositionStringA
8422 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8423 pImmGetCompositionStringW
8424 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8425 pImmGetContext
8426 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8427 pImmAssociateContext
8428 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8429 pImmReleaseContext
8430 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8431 pImmGetOpenStatus
8432 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8433 pImmSetOpenStatus
8434 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
8435 pImmGetCompositionFont
8436 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
8437 pImmSetCompositionFont
8438 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
8439 pImmSetCompositionWindow
8440 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8441 pImmGetConversionStatus
8442 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008443 pImmSetConversionStatus
8444 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445
8446 if ( pImmGetCompositionStringA == NULL
8447 || pImmGetCompositionStringW == NULL
8448 || pImmGetContext == NULL
8449 || pImmAssociateContext == NULL
8450 || pImmReleaseContext == NULL
8451 || pImmGetOpenStatus == NULL
8452 || pImmSetOpenStatus == NULL
8453 || pImmGetCompositionFont == NULL
8454 || pImmSetCompositionFont == NULL
8455 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008456 || pImmGetConversionStatus == NULL
8457 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 {
8459 FreeLibrary(hLibImm);
8460 hLibImm = NULL;
8461 pImmGetContext = NULL;
8462 return;
8463 }
8464
8465 return;
8466}
8467
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468#endif
8469
8470#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8471
8472# ifdef FEAT_XPM_W32
8473# define IMAGE_XPM 100
8474# endif
8475
8476typedef struct _signicon_t
8477{
8478 HANDLE hImage;
8479 UINT uType;
8480#ifdef FEAT_XPM_W32
8481 HANDLE hShape; /* Mask bitmap handle */
8482#endif
8483} signicon_t;
8484
8485 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008486gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487{
8488 signicon_t *sign;
8489 int x, y, w, h;
8490
8491 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8492 return;
8493
8494 x = TEXT_X(col);
8495 y = TEXT_Y(row);
8496 w = gui.char_width * 2;
8497 h = gui.char_height;
8498 switch (sign->uType)
8499 {
8500 case IMAGE_BITMAP:
8501 {
8502 HDC hdcMem;
8503 HBITMAP hbmpOld;
8504
8505 hdcMem = CreateCompatibleDC(s_hdc);
8506 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8507 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8508 SelectObject(hdcMem, hbmpOld);
8509 DeleteDC(hdcMem);
8510 }
8511 break;
8512 case IMAGE_ICON:
8513 case IMAGE_CURSOR:
8514 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8515 break;
8516#ifdef FEAT_XPM_W32
8517 case IMAGE_XPM:
8518 {
8519 HDC hdcMem;
8520 HBITMAP hbmpOld;
8521
8522 hdcMem = CreateCompatibleDC(s_hdc);
8523 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8524 /* Make hole */
8525 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8526
8527 SelectObject(hdcMem, sign->hImage);
8528 /* Paint sign */
8529 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8530 SelectObject(hdcMem, hbmpOld);
8531 DeleteDC(hdcMem);
8532 }
8533 break;
8534#endif
8535 }
8536}
8537
8538 static void
8539close_signicon_image(signicon_t *sign)
8540{
8541 if (sign)
8542 switch (sign->uType)
8543 {
8544 case IMAGE_BITMAP:
8545 DeleteObject((HGDIOBJ)sign->hImage);
8546 break;
8547 case IMAGE_CURSOR:
8548 DestroyCursor((HCURSOR)sign->hImage);
8549 break;
8550 case IMAGE_ICON:
8551 DestroyIcon((HICON)sign->hImage);
8552 break;
8553#ifdef FEAT_XPM_W32
8554 case IMAGE_XPM:
8555 DeleteObject((HBITMAP)sign->hImage);
8556 DeleteObject((HBITMAP)sign->hShape);
8557 break;
8558#endif
8559 }
8560}
8561
8562 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008563gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564{
8565 signicon_t sign, *psign;
8566 char_u *ext;
8567
8568 if (is_winnt_3())
8569 {
8570 EMSG(_(e_signdata));
8571 return NULL;
8572 }
8573
8574 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008575 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 if (ext > signfile)
8577 {
8578 int do_load = 1;
8579
8580 if (!STRICMP(ext, ".bmp"))
8581 sign.uType = IMAGE_BITMAP;
8582 else if (!STRICMP(ext, ".ico"))
8583 sign.uType = IMAGE_ICON;
8584 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8585 sign.uType = IMAGE_CURSOR;
8586 else
8587 do_load = 0;
8588
8589 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008590 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 gui.char_width * 2, gui.char_height,
8592 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8593#ifdef FEAT_XPM_W32
8594 if (!STRICMP(ext, ".xpm"))
8595 {
8596 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008597 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8598 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 }
8600#endif
8601 }
8602
8603 psign = NULL;
8604 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8605 != NULL)
8606 *psign = sign;
8607
8608 if (!psign)
8609 {
8610 if (sign.hImage)
8611 close_signicon_image(&sign);
8612 EMSG(_(e_signdata));
8613 }
8614 return (void *)psign;
8615
8616}
8617
8618 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008619gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620{
8621 if (sign)
8622 {
8623 close_signicon_image((signicon_t *)sign);
8624 vim_free(sign);
8625 }
8626}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628
8629#if defined(FEAT_BEVAL) || defined(PROTO)
8630
8631/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008632 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 *
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00008634 * The only reused thing is gui_beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8636 * to get current mouse position).
8637 *
8638 * Trying to use as more Windows services as possible, and as less
8639 * IE version as possible :)).
8640 *
8641 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8642 * BalloonEval struct.
8643 * 2) Enable/Disable simply create/kill BalloonEval Timer
8644 * 3) When there was enough inactivity, timer procedure posts
8645 * async request to debugger
8646 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8647 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008648 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 */
8650
Bram Moolenaar45360022005-07-21 21:08:21 +00008651/*
8652 * determine whether installed Common Controls support multiline tooltips
8653 * (i.e. their version is >= 4.70
8654 */
8655 int
8656multiline_balloon_available(void)
8657{
8658 HINSTANCE hDll;
8659 static char comctl_dll[] = "comctl32.dll";
8660 static int multiline_tip = MAYBE;
8661
8662 if (multiline_tip != MAYBE)
8663 return multiline_tip;
8664
8665 hDll = GetModuleHandle(comctl_dll);
8666 if (hDll != NULL)
8667 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008668 DLLGETVERSIONPROC pGetVer;
8669 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008670
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008671 if (pGetVer != NULL)
8672 {
8673 DLLVERSIONINFO dvi;
8674 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008675
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008676 ZeroMemory(&dvi, sizeof(dvi));
8677 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008678
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008679 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008680
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008681 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008682 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008683 || (dvi.dwMajorVersion == 4
8684 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008685 {
8686 multiline_tip = TRUE;
8687 return multiline_tip;
8688 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008689 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008690 else
8691 {
8692 /* there is chance we have ancient CommCtl 4.70
8693 which doesn't export DllGetVersion */
8694 DWORD dwHandle = 0;
8695 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8696 if (len > 0)
8697 {
8698 VS_FIXEDFILEINFO *ver;
8699 UINT vlen = 0;
8700 void *data = alloc(len);
8701
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008702 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008703 && GetFileVersionInfo(comctl_dll, 0, len, data)
8704 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8705 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008706 && HIWORD(ver->dwFileVersionMS) > 4)
8707 || ((HIWORD(ver->dwFileVersionMS) == 4
8708 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008709 {
8710 vim_free(data);
8711 multiline_tip = TRUE;
8712 return multiline_tip;
8713 }
8714 vim_free(data);
8715 }
8716 }
8717 }
8718 multiline_tip = FALSE;
8719 return multiline_tip;
8720}
8721
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008723make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724{
Bram Moolenaar45360022005-07-21 21:08:21 +00008725 TOOLINFO *pti;
8726 int ToolInfoSize;
8727
8728 if (multiline_balloon_available() == TRUE)
8729 ToolInfoSize = sizeof(TOOLINFO_NEW);
8730 else
8731 ToolInfoSize = sizeof(TOOLINFO);
8732
8733 pti = (TOOLINFO *)alloc(ToolInfoSize);
8734 if (pti == NULL)
8735 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736
8737 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
8738 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8739 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8740 beval->target, NULL, s_hinst, NULL);
8741
8742 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8743 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8744
Bram Moolenaar45360022005-07-21 21:08:21 +00008745 pti->cbSize = ToolInfoSize;
8746 pti->uFlags = TTF_SUBCLASS;
8747 pti->hwnd = beval->target;
8748 pti->hinst = 0; /* Don't use string resources */
8749 pti->uId = ID_BEVAL_TOOLTIP;
8750
8751 if (multiline_balloon_available() == TRUE)
8752 {
8753 RECT rect;
8754 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
8755 pti->lpszText = LPSTR_TEXTCALLBACK;
8756 ptin->lParam = (LPARAM)text;
8757 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
8758 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8759 (LPARAM)rect.right);
8760 }
8761 else
8762 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763
8764 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00008765 pti->rect.left = pt.x - 3;
8766 pti->rect.top = pt.y - 3;
8767 pti->rect.right = pt.x + 3;
8768 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769
Bram Moolenaar45360022005-07-21 21:08:21 +00008770 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 /* Make tooltip appear sooner */
8772 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008773 /* I've performed some tests and it seems the longest possible life time
8774 * of tooltip is 30 seconds */
8775 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 /*
8777 * HACK: force tooltip to appear, because it'll not appear until
8778 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008779 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008781 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00008783 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784}
8785
8786 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008787delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008789 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790}
8791
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008792/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008794BevalTimerProc(
8795 HWND hwnd,
8796 UINT uMsg,
8797 UINT_PTR idEvent,
8798 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799{
8800 POINT pt;
8801 RECT rect;
8802
8803 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8804 return;
8805
8806 GetCursorPos(&pt);
8807 if (WindowFromPoint(pt) != s_textArea)
8808 return;
8809
8810 ScreenToClient(s_textArea, &pt);
8811 GetClientRect(s_textArea, &rect);
8812 if (!PtInRect(&rect, pt))
8813 return;
8814
8815 if (LastActivity > 0
8816 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8817 && (cur_beval->showState != ShS_PENDING
8818 || abs(cur_beval->x - pt.x) > 3
8819 || abs(cur_beval->y - pt.y) > 3))
8820 {
8821 /* Pointer resting in one place long enough, it's time to show
8822 * the tooltip. */
8823 cur_beval->showState = ShS_PENDING;
8824 cur_beval->x = pt.x;
8825 cur_beval->y = pt.y;
8826
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008827 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828
8829 if (cur_beval->msgCB != NULL)
8830 (*cur_beval->msgCB)(cur_beval, 0);
8831 }
8832}
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_disable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008838 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008840 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841}
8842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008843/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008845gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008847 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 if (beval == NULL)
8849 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008850 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008851 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008852 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853}
8854
8855 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008856gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857{
8858 POINT pt;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008859 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 if (beval->showState == ShS_SHOWING)
8861 return;
8862 GetCursorPos(&pt);
8863 ScreenToClient(s_textArea, &pt);
8864
8865 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
8866 /* cursor is still here */
8867 {
8868 gui_mch_disable_beval_area(cur_beval);
8869 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008870 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008872 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873}
8874
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008875/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008877gui_mch_create_beval_area(
8878 void *target, /* ignored, always use s_textArea */
8879 char_u *mesg,
8880 void (*mesgCB)(BalloonEval *, int),
8881 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882{
8883 /* partially stolen from gui_beval.c */
8884 BalloonEval *beval;
8885
8886 if (mesg != NULL && mesgCB != NULL)
8887 {
8888 EMSG(_("E232: Cannot create BalloonEval with both message and callback"));
8889 return NULL;
8890 }
8891
8892 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
8893 if (beval != NULL)
8894 {
8895 beval->target = s_textArea;
8896 beval->balloon = NULL;
8897
8898 beval->showState = ShS_NEUTRAL;
8899 beval->x = 0;
8900 beval->y = 0;
8901 beval->msg = mesg;
8902 beval->msgCB = mesgCB;
8903 beval->clientData = clientData;
8904
8905 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 cur_beval = beval;
8907
8908 if (p_beval)
8909 gui_mch_enable_beval_area(beval);
8910
8911 }
8912 return beval;
8913}
8914
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008915/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 static void
Bram Moolenaar442b4222010-05-24 21:34:22 +02008917Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918{
8919 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8920 return;
8921
8922 if (cur_beval != NULL)
8923 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008924 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008926 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008927 // TRACE0("TTN_SHOW {{{");
8928 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008929 break;
8930 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008931 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 delete_tooltip(cur_beval);
8933 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008934 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935
8936 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008937 break;
8938 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008939 {
8940 /* if you get there then we have new common controls */
8941 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8942 info->lpszText = (LPSTR)info->lParam;
8943 info->uFlags |= TTF_DI_SETITEM;
8944 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008945 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 }
8947 }
8948}
8949
8950 static void
8951TrackUserActivity(UINT uMsg)
8952{
8953 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8954 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8955 LastActivity = GetTickCount();
8956}
8957
8958 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008959gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960{
8961 vim_free(beval);
8962}
8963#endif /* FEAT_BEVAL */
8964
8965#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8966/*
8967 * We have multiple signs to draw at the same location. Draw the
8968 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8969 */
8970 void
8971netbeans_draw_multisign_indicator(int row)
8972{
8973 int i;
8974 int y;
8975 int x;
8976
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008977 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008978 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008979
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 x = 0;
8981 y = TEXT_Y(row);
8982
8983 for (i = 0; i < gui.char_height - 3; i++)
8984 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8985
8986 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8987 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8988 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8989 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8990 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8991 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8992 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8993}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008994#endif