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