blob: 2c665cb46d60337d82e06529c51683796d6c8959 [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{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002053 int focus;
2054
2055 s_timed_out = FALSE;
2056
2057 if (wtime > 0)
2058 {
2059 /* Don't do anything while processing a (scroll) message. */
2060 if (s_busy_processing)
2061 return FAIL;
2062 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)wtime,
2063 (TIMERPROC)_OnTimer);
2064 }
2065
2066 allow_scrollbar = TRUE;
2067
2068 focus = gui.in_focus;
2069 while (!s_timed_out)
2070 {
2071 /* Stop or start blinking when focus changes */
2072 if (gui.in_focus != focus)
2073 {
2074 if (gui.in_focus)
2075 gui_mch_start_blink();
2076 else
2077 gui_mch_stop_blink();
2078 focus = gui.in_focus;
2079 }
2080
2081 if (s_need_activate)
2082 {
2083#ifdef WIN32
2084 (void)SetForegroundWindow(s_hwnd);
2085#else
2086 (void)SetActiveWindow(s_hwnd);
2087#endif
2088 s_need_activate = FALSE;
2089 }
2090
Bram Moolenaar4231da42016-06-02 14:30:04 +02002091#ifdef FEAT_TIMERS
2092 did_add_timer = FALSE;
2093#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002094#ifdef MESSAGE_QUEUE
Bram Moolenaar9186a272016-02-23 19:34:01 +01002095 /* Check channel while waiting message. */
2096 for (;;)
2097 {
2098 MSG msg;
2099
2100 parse_queued_messages();
2101
2102 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarf28d8712016-04-02 15:59:40 +02002103 || MsgWaitForMultipleObjects(0, NULL, FALSE, 100, QS_ALLINPUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002104 != WAIT_TIMEOUT)
2105 break;
2106 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002107#endif
2108
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002109 /*
2110 * Don't use gui_mch_update() because then we will spin-lock until a
2111 * char arrives, instead we use GetMessage() to hang until an
2112 * event arrives. No need to check for input_buf_full because we are
2113 * returning as soon as it contains a single char -- webb
2114 */
2115 process_message();
2116
2117 if (input_available())
2118 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002119 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002120 allow_scrollbar = FALSE;
2121
2122 /* Clear pending mouse button, the release event may have been
2123 * taken by the dialog window. But don't do this when getting
2124 * focus, we need the mouse-up event then. */
2125 if (!s_getting_focus)
2126 s_button_pending = -1;
2127
2128 return OK;
2129 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002130
2131#ifdef FEAT_TIMERS
2132 if (did_add_timer)
2133 {
2134 /* Need to recompute the waiting time. */
2135 remove_any_timer();
2136 break;
2137 }
2138#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002139 }
2140 allow_scrollbar = FALSE;
2141 return FAIL;
2142}
2143
2144/*
2145 * Clear a rectangular region of the screen from text pos (row1, col1) to
2146 * (row2, col2) inclusive.
2147 */
2148 void
2149gui_mch_clear_block(
2150 int row1,
2151 int col1,
2152 int row2,
2153 int col2)
2154{
2155 RECT rc;
2156
2157 /*
2158 * Clear one extra pixel at the far right, for when bold characters have
2159 * spilled over to the window border.
2160 * Note: FillRect() excludes right and bottom of rectangle.
2161 */
2162 rc.left = FILL_X(col1);
2163 rc.top = FILL_Y(row1);
2164 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2165 rc.bottom = FILL_Y(row2 + 1);
2166 clear_rect(&rc);
2167}
2168
2169/*
2170 * Clear the whole text window.
2171 */
2172 void
2173gui_mch_clear_all(void)
2174{
2175 RECT rc;
2176
2177 rc.left = 0;
2178 rc.top = 0;
2179 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2180 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2181 clear_rect(&rc);
2182}
2183/*
2184 * Menu stuff.
2185 */
2186
2187 void
2188gui_mch_enable_menu(int flag)
2189{
2190#ifdef FEAT_MENU
2191 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2192#endif
2193}
2194
2195/*ARGSUSED*/
2196 void
2197gui_mch_set_menu_pos(
2198 int x,
2199 int y,
2200 int w,
2201 int h)
2202{
2203 /* It will be in the right place anyway */
2204}
2205
2206#if defined(FEAT_MENU) || defined(PROTO)
2207/*
2208 * Make menu item hidden or not hidden
2209 */
2210 void
2211gui_mch_menu_hidden(
2212 vimmenu_T *menu,
2213 int hidden)
2214{
2215 /*
2216 * This doesn't do what we want. Hmm, just grey the menu items for now.
2217 */
2218 /*
2219 if (hidden)
2220 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2221 else
2222 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2223 */
2224 gui_mch_menu_grey(menu, hidden);
2225}
2226
2227/*
2228 * This is called after setting all the menus to grey/hidden or not.
2229 */
2230 void
2231gui_mch_draw_menubar(void)
2232{
2233 DrawMenuBar(s_hwnd);
2234}
2235#endif /*FEAT_MENU*/
2236
2237#ifndef PROTO
2238void
2239#ifdef VIMDLL
2240_export
2241#endif
2242_cdecl
2243SaveInst(HINSTANCE hInst)
2244{
2245 s_hinst = hInst;
2246}
2247#endif
2248
2249/*
2250 * Return the RGB value of a pixel as a long.
2251 */
2252 long_u
2253gui_mch_get_rgb(guicolor_T pixel)
2254{
2255 return (GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2256 + GetBValue(pixel);
2257}
2258
2259#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2260/* Convert pixels in X to dialog units */
2261 static WORD
2262PixelToDialogX(int numPixels)
2263{
2264 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2265}
2266
2267/* Convert pixels in Y to dialog units */
2268 static WORD
2269PixelToDialogY(int numPixels)
2270{
2271 return (WORD)((numPixels * 8) / s_dlgfntheight);
2272}
2273
2274/* Return the width in pixels of the given text in the given DC. */
2275 static int
2276GetTextWidth(HDC hdc, char_u *str, int len)
2277{
2278 SIZE size;
2279
2280 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2281 return size.cx;
2282}
2283
2284#ifdef FEAT_MBYTE
2285/*
2286 * Return the width in pixels of the given text in the given DC, taking care
2287 * of 'encoding' to active codepage conversion.
2288 */
2289 static int
2290GetTextWidthEnc(HDC hdc, char_u *str, int len)
2291{
2292 SIZE size;
2293 WCHAR *wstr;
2294 int n;
2295 int wlen = len;
2296
2297 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2298 {
2299 /* 'encoding' differs from active codepage: convert text and use wide
2300 * function */
2301 wstr = enc_to_utf16(str, &wlen);
2302 if (wstr != NULL)
2303 {
2304 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2305 vim_free(wstr);
2306 if (n)
2307 return size.cx;
2308 }
2309 }
2310
2311 return GetTextWidth(hdc, str, len);
2312}
2313#else
2314# define GetTextWidthEnc(h, s, l) GetTextWidth((h), (s), (l))
2315#endif
2316
2317/*
2318 * A quick little routine that will center one window over another, handy for
2319 * dialog boxes. Taken from the Win32SDK samples.
2320 */
2321 static BOOL
2322CenterWindow(
2323 HWND hwndChild,
2324 HWND hwndParent)
2325{
2326 RECT rChild, rParent;
2327 int wChild, hChild, wParent, hParent;
2328 int wScreen, hScreen, xNew, yNew;
2329 HDC hdc;
2330
2331 GetWindowRect(hwndChild, &rChild);
2332 wChild = rChild.right - rChild.left;
2333 hChild = rChild.bottom - rChild.top;
2334
2335 /* If Vim is minimized put the window in the middle of the screen. */
2336 if (hwndParent == NULL || IsMinimized(hwndParent))
2337 SystemParametersInfo(SPI_GETWORKAREA, 0, &rParent, 0);
2338 else
2339 GetWindowRect(hwndParent, &rParent);
2340 wParent = rParent.right - rParent.left;
2341 hParent = rParent.bottom - rParent.top;
2342
2343 hdc = GetDC(hwndChild);
2344 wScreen = GetDeviceCaps (hdc, HORZRES);
2345 hScreen = GetDeviceCaps (hdc, VERTRES);
2346 ReleaseDC(hwndChild, hdc);
2347
2348 xNew = rParent.left + ((wParent - wChild) /2);
2349 if (xNew < 0)
2350 {
2351 xNew = 0;
2352 }
2353 else if ((xNew+wChild) > wScreen)
2354 {
2355 xNew = wScreen - wChild;
2356 }
2357
2358 yNew = rParent.top + ((hParent - hChild) /2);
2359 if (yNew < 0)
2360 yNew = 0;
2361 else if ((yNew+hChild) > hScreen)
2362 yNew = hScreen - hChild;
2363
2364 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2365 SWP_NOSIZE | SWP_NOZORDER);
2366}
2367#endif /* FEAT_GUI_DIALOG */
2368
2369void
2370gui_mch_activate_window(void)
2371{
2372 (void)SetActiveWindow(s_hwnd);
2373}
2374
2375#if defined(FEAT_TOOLBAR) || defined(PROTO)
2376 void
2377gui_mch_show_toolbar(int showit)
2378{
2379 if (s_toolbarhwnd == NULL)
2380 return;
2381
2382 if (showit)
2383 {
2384# ifdef FEAT_MBYTE
2385# ifndef TB_SETUNICODEFORMAT
2386 /* For older compilers. We assume this never changes. */
2387# define TB_SETUNICODEFORMAT 0x2005
2388# endif
2389 /* Enable/disable unicode support */
2390 int uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2391 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2392# endif
2393 ShowWindow(s_toolbarhwnd, SW_SHOW);
2394 }
2395 else
2396 ShowWindow(s_toolbarhwnd, SW_HIDE);
2397}
2398
2399/* Then number of bitmaps is fixed. Exit is missing! */
2400#define TOOLBAR_BITMAP_COUNT 31
2401
2402#endif
2403
2404#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2405 static void
2406add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2407{
2408#ifdef FEAT_MBYTE
2409 WCHAR *wn = NULL;
2410 int n;
2411
2412 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2413 {
2414 /* 'encoding' differs from active codepage: convert menu name
2415 * and use wide function */
2416 wn = enc_to_utf16(item_text, NULL);
2417 if (wn != NULL)
2418 {
2419 MENUITEMINFOW infow;
2420
2421 infow.cbSize = sizeof(infow);
2422 infow.fMask = MIIM_TYPE | MIIM_ID;
2423 infow.wID = item_id;
2424 infow.fType = MFT_STRING;
2425 infow.dwTypeData = wn;
2426 infow.cch = (UINT)wcslen(wn);
2427 n = InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2428 vim_free(wn);
2429 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2430 /* Failed, try using non-wide function. */
2431 wn = NULL;
2432 }
2433 }
2434
2435 if (wn == NULL)
2436#endif
2437 {
2438 MENUITEMINFO info;
2439
2440 info.cbSize = sizeof(info);
2441 info.fMask = MIIM_TYPE | MIIM_ID;
2442 info.wID = item_id;
2443 info.fType = MFT_STRING;
2444 info.dwTypeData = (LPTSTR)item_text;
2445 info.cch = (UINT)STRLEN(item_text);
2446 InsertMenuItem(pmenu, item_id, FALSE, &info);
2447 }
2448}
2449
2450 static void
2451show_tabline_popup_menu(void)
2452{
2453 HMENU tab_pmenu;
2454 long rval;
2455 POINT pt;
2456
2457 /* When ignoring events don't show the menu. */
2458 if (hold_gui_events
2459# ifdef FEAT_CMDWIN
2460 || cmdwin_type != 0
2461# endif
2462 )
2463 return;
2464
2465 tab_pmenu = CreatePopupMenu();
2466 if (tab_pmenu == NULL)
2467 return;
2468
2469 if (first_tabpage->tp_next != NULL)
2470 add_tabline_popup_menu_entry(tab_pmenu,
2471 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2472 add_tabline_popup_menu_entry(tab_pmenu,
2473 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2474 add_tabline_popup_menu_entry(tab_pmenu,
2475 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2476
2477 GetCursorPos(&pt);
2478 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2479 NULL);
2480
2481 DestroyMenu(tab_pmenu);
2482
2483 /* Add the string cmd into input buffer */
2484 if (rval > 0)
2485 {
2486 TCHITTESTINFO htinfo;
2487 int idx;
2488
2489 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2490 return;
2491
2492 htinfo.pt.x = pt.x;
2493 htinfo.pt.y = pt.y;
2494 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2495 if (idx == -1)
2496 idx = 0;
2497 else
2498 idx += 1;
2499
2500 send_tabline_menu_event(idx, (int)rval);
2501 }
2502}
2503
2504/*
2505 * Show or hide the tabline.
2506 */
2507 void
2508gui_mch_show_tabline(int showit)
2509{
2510 if (s_tabhwnd == NULL)
2511 return;
2512
2513 if (!showit != !showing_tabline)
2514 {
2515 if (showit)
2516 ShowWindow(s_tabhwnd, SW_SHOW);
2517 else
2518 ShowWindow(s_tabhwnd, SW_HIDE);
2519 showing_tabline = showit;
2520 }
2521}
2522
2523/*
2524 * Return TRUE when tabline is displayed.
2525 */
2526 int
2527gui_mch_showing_tabline(void)
2528{
2529 return s_tabhwnd != NULL && showing_tabline;
2530}
2531
2532/*
2533 * Update the labels of the tabline.
2534 */
2535 void
2536gui_mch_update_tabline(void)
2537{
2538 tabpage_T *tp;
2539 TCITEM tie;
2540 int nr = 0;
2541 int curtabidx = 0;
2542 int tabadded = 0;
2543#ifdef FEAT_MBYTE
2544 static int use_unicode = FALSE;
2545 int uu;
2546 WCHAR *wstr = NULL;
2547#endif
2548
2549 if (s_tabhwnd == NULL)
2550 return;
2551
2552#if defined(FEAT_MBYTE)
2553# ifndef CCM_SETUNICODEFORMAT
2554 /* For older compilers. We assume this never changes. */
2555# define CCM_SETUNICODEFORMAT 0x2005
2556# endif
2557 uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2558 if (uu != use_unicode)
2559 {
2560 /* Enable/disable unicode support */
2561 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2562 use_unicode = uu;
2563 }
2564#endif
2565
2566 tie.mask = TCIF_TEXT;
2567 tie.iImage = -1;
2568
2569 /* Disable redraw for tab updates to eliminate O(N^2) draws. */
2570 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2571
2572 /* Add a label for each tab page. They all contain the same text area. */
2573 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2574 {
2575 if (tp == curtab)
2576 curtabidx = nr;
2577
2578 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2579 {
2580 /* Add the tab */
2581 tie.pszText = "-Empty-";
2582 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2583 tabadded = 1;
2584 }
2585
2586 get_tabline_label(tp, FALSE);
2587 tie.pszText = (LPSTR)NameBuff;
2588#ifdef FEAT_MBYTE
2589 wstr = NULL;
2590 if (use_unicode)
2591 {
2592 /* Need to go through Unicode. */
2593 wstr = enc_to_utf16(NameBuff, NULL);
2594 if (wstr != NULL)
2595 {
2596 TCITEMW tiw;
2597
2598 tiw.mask = TCIF_TEXT;
2599 tiw.iImage = -1;
2600 tiw.pszText = wstr;
2601 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2602 vim_free(wstr);
2603 }
2604 }
2605 if (wstr == NULL)
2606#endif
2607 {
2608 TabCtrl_SetItem(s_tabhwnd, nr, &tie);
2609 }
2610 }
2611
2612 /* Remove any old labels. */
2613 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2614 TabCtrl_DeleteItem(s_tabhwnd, nr);
2615
2616 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2617 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2618
2619 /* Re-enable redraw and redraw. */
2620 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2621 RedrawWindow(s_tabhwnd, NULL, NULL,
2622 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2623
2624 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2625 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2626}
2627
2628/*
2629 * Set the current tab to "nr". First tab is 1.
2630 */
2631 void
2632gui_mch_set_curtab(int nr)
2633{
2634 if (s_tabhwnd == NULL)
2635 return;
2636
2637 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2638 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2639}
2640
2641#endif
2642
2643/*
2644 * ":simalt" command.
2645 */
2646 void
2647ex_simalt(exarg_T *eap)
2648{
2649 char_u *keys = eap->arg;
2650
2651 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2652 while (*keys)
2653 {
2654 if (*keys == '~')
2655 *keys = ' '; /* for showing system menu */
2656 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2657 keys++;
2658 }
2659}
2660
2661/*
2662 * Create the find & replace dialogs.
2663 * You can't have both at once: ":find" when replace is showing, destroys
2664 * the replace dialog first, and the other way around.
2665 */
2666#ifdef MSWIN_FIND_REPLACE
2667 static void
2668initialise_findrep(char_u *initial_string)
2669{
2670 int wword = FALSE;
2671 int mcase = !p_ic;
2672 char_u *entry_text;
2673
2674 /* Get the search string to use. */
2675 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2676
2677 s_findrep_struct.hwndOwner = s_hwnd;
2678 s_findrep_struct.Flags = FR_DOWN;
2679 if (mcase)
2680 s_findrep_struct.Flags |= FR_MATCHCASE;
2681 if (wword)
2682 s_findrep_struct.Flags |= FR_WHOLEWORD;
2683 if (entry_text != NULL && *entry_text != NUL)
2684 vim_strncpy((char_u *)s_findrep_struct.lpstrFindWhat, entry_text,
2685 s_findrep_struct.wFindWhatLen - 1);
2686 vim_free(entry_text);
2687}
2688#endif
2689
2690 static void
2691set_window_title(HWND hwnd, char *title)
2692{
2693#ifdef FEAT_MBYTE
2694 if (title != NULL && enc_codepage >= 0 && enc_codepage != (int)GetACP())
2695 {
2696 WCHAR *wbuf;
2697 int n;
2698
2699 /* Convert the title from 'encoding' to UTF-16. */
2700 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2701 if (wbuf != NULL)
2702 {
2703 n = SetWindowTextW(hwnd, wbuf);
2704 vim_free(wbuf);
2705 if (n != 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
2706 return;
2707 /* Retry with non-wide function (for Windows 98). */
2708 }
2709 }
2710#endif
2711 (void)SetWindowText(hwnd, (LPCSTR)title);
2712}
2713
2714 void
2715gui_mch_find_dialog(exarg_T *eap)
2716{
2717#ifdef MSWIN_FIND_REPLACE
2718 if (s_findrep_msg != 0)
2719 {
2720 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2721 DestroyWindow(s_findrep_hwnd);
2722
2723 if (!IsWindow(s_findrep_hwnd))
2724 {
2725 initialise_findrep(eap->arg);
2726# if defined(FEAT_MBYTE) && defined(WIN3264)
2727 /* If the OS is Windows NT, and 'encoding' differs from active
2728 * codepage: convert text and use wide function. */
2729 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
2730 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2731 {
2732 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2733 s_findrep_hwnd = FindTextW(
2734 (LPFINDREPLACEW) &s_findrep_struct_w);
2735 }
2736 else
2737# endif
2738 s_findrep_hwnd = FindText((LPFINDREPLACE) &s_findrep_struct);
2739 }
2740
2741 set_window_title(s_findrep_hwnd,
2742 _("Find string (use '\\\\' to find a '\\')"));
2743 (void)SetFocus(s_findrep_hwnd);
2744
2745 s_findrep_is_find = TRUE;
2746 }
2747#endif
2748}
2749
2750
2751 void
2752gui_mch_replace_dialog(exarg_T *eap)
2753{
2754#ifdef MSWIN_FIND_REPLACE
2755 if (s_findrep_msg != 0)
2756 {
2757 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2758 DestroyWindow(s_findrep_hwnd);
2759
2760 if (!IsWindow(s_findrep_hwnd))
2761 {
2762 initialise_findrep(eap->arg);
2763# if defined(FEAT_MBYTE) && defined(WIN3264)
2764 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
2765 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2766 {
2767 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2768 s_findrep_hwnd = ReplaceTextW(
2769 (LPFINDREPLACEW) &s_findrep_struct_w);
2770 }
2771 else
2772# endif
2773 s_findrep_hwnd = ReplaceText(
2774 (LPFINDREPLACE) &s_findrep_struct);
2775 }
2776
2777 set_window_title(s_findrep_hwnd,
2778 _("Find & Replace (use '\\\\' to find a '\\')"));
2779 (void)SetFocus(s_findrep_hwnd);
2780
2781 s_findrep_is_find = FALSE;
2782 }
2783#endif
2784}
2785
2786
2787/*
2788 * Set visibility of the pointer.
2789 */
2790 void
2791gui_mch_mousehide(int hide)
2792{
2793 if (hide != gui.pointer_hidden)
2794 {
2795 ShowCursor(!hide);
2796 gui.pointer_hidden = hide;
2797 }
2798}
2799
2800#ifdef FEAT_MENU
2801 static void
2802gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2803{
2804 /* Unhide the mouse, we don't get move events here. */
2805 gui_mch_mousehide(FALSE);
2806
2807 (void)TrackPopupMenu(
2808 (HMENU)menu->submenu_id,
2809 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2810 x, y,
2811 (int)0, /*reserved param*/
2812 s_hwnd,
2813 NULL);
2814 /*
2815 * NOTE: The pop-up menu can eat the mouse up event.
2816 * We deal with this in normal.c.
2817 */
2818}
2819#endif
2820
2821/*
2822 * Got a message when the system will go down.
2823 */
2824 static void
2825_OnEndSession(void)
2826{
2827 getout_preserve_modified(1);
2828}
2829
2830/*
2831 * Get this message when the user clicks on the cross in the top right corner
2832 * of a Windows95 window.
2833 */
2834/*ARGSUSED*/
2835 static void
2836_OnClose(
2837 HWND hwnd)
2838{
2839 gui_shell_closed();
2840}
2841
2842/*
2843 * Get a message when the window is being destroyed.
2844 */
2845 static void
2846_OnDestroy(
2847 HWND hwnd)
2848{
2849 if (!destroying)
2850 _OnClose(hwnd);
2851}
2852
2853 static void
2854_OnPaint(
2855 HWND hwnd)
2856{
2857 if (!IsMinimized(hwnd))
2858 {
2859 PAINTSTRUCT ps;
2860
2861 out_flush(); /* make sure all output has been processed */
2862 (void)BeginPaint(hwnd, &ps);
2863#if defined(FEAT_DIRECTX)
2864 if (IS_ENABLE_DIRECTX())
2865 DWriteContext_BeginDraw(s_dwc);
2866#endif
2867
2868#ifdef FEAT_MBYTE
2869 /* prevent multi-byte characters from misprinting on an invalid
2870 * rectangle */
2871 if (has_mbyte)
2872 {
2873 RECT rect;
2874
2875 GetClientRect(hwnd, &rect);
2876 ps.rcPaint.left = rect.left;
2877 ps.rcPaint.right = rect.right;
2878 }
2879#endif
2880
2881 if (!IsRectEmpty(&ps.rcPaint))
2882 {
2883#if defined(FEAT_DIRECTX)
2884 if (IS_ENABLE_DIRECTX())
2885 DWriteContext_BindDC(s_dwc, s_hdc, &ps.rcPaint);
2886#endif
2887 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2888 ps.rcPaint.right - ps.rcPaint.left + 1,
2889 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2890 }
2891
2892#if defined(FEAT_DIRECTX)
2893 if (IS_ENABLE_DIRECTX())
2894 DWriteContext_EndDraw(s_dwc);
2895#endif
2896 EndPaint(hwnd, &ps);
2897 }
2898}
2899
2900/*ARGSUSED*/
2901 static void
2902_OnSize(
2903 HWND hwnd,
2904 UINT state,
2905 int cx,
2906 int cy)
2907{
2908 if (!IsMinimized(hwnd))
2909 {
2910 gui_resize_shell(cx, cy);
2911
2912#ifdef FEAT_MENU
2913 /* Menu bar may wrap differently now */
2914 gui_mswin_get_menu_height(TRUE);
2915#endif
2916 }
2917}
2918
2919 static void
2920_OnSetFocus(
2921 HWND hwnd,
2922 HWND hwndOldFocus)
2923{
2924 gui_focus_change(TRUE);
2925 s_getting_focus = TRUE;
2926 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2927}
2928
2929 static void
2930_OnKillFocus(
2931 HWND hwnd,
2932 HWND hwndNewFocus)
2933{
2934 gui_focus_change(FALSE);
2935 s_getting_focus = FALSE;
2936 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2937}
2938
2939/*
2940 * Get a message when the user switches back to vim
2941 */
2942 static LRESULT
2943_OnActivateApp(
2944 HWND hwnd,
2945 BOOL fActivate,
2946 DWORD dwThreadId)
2947{
2948 /* we call gui_focus_change() in _OnSetFocus() */
2949 /* gui_focus_change((int)fActivate); */
2950 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2951}
2952
2953#if defined(FEAT_WINDOWS) || defined(PROTO)
2954 void
2955gui_mch_destroy_scrollbar(scrollbar_T *sb)
2956{
2957 DestroyWindow(sb->id);
2958}
2959#endif
2960
2961/*
2962 * Get current mouse coordinates in text window.
2963 */
2964 void
2965gui_mch_getmouse(int *x, int *y)
2966{
2967 RECT rct;
2968 POINT mp;
2969
2970 (void)GetWindowRect(s_textArea, &rct);
2971 (void)GetCursorPos((LPPOINT)&mp);
2972 *x = (int)(mp.x - rct.left);
2973 *y = (int)(mp.y - rct.top);
2974}
2975
2976/*
2977 * Move mouse pointer to character at (x, y).
2978 */
2979 void
2980gui_mch_setmouse(int x, int y)
2981{
2982 RECT rct;
2983
2984 (void)GetWindowRect(s_textArea, &rct);
2985 (void)SetCursorPos(x + gui.border_offset + rct.left,
2986 y + gui.border_offset + rct.top);
2987}
2988
2989 static void
2990gui_mswin_get_valid_dimensions(
2991 int w,
2992 int h,
2993 int *valid_w,
2994 int *valid_h)
2995{
2996 int base_width, base_height;
2997
2998 base_width = gui_get_base_width()
2999 + (GetSystemMetrics(SM_CXFRAME) +
3000 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
3001 base_height = gui_get_base_height()
3002 + (GetSystemMetrics(SM_CYFRAME) +
3003 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3004 + GetSystemMetrics(SM_CYCAPTION)
3005#ifdef FEAT_MENU
3006 + gui_mswin_get_menu_height(FALSE)
3007#endif
3008 ;
3009 *valid_w = base_width +
3010 ((w - base_width) / gui.char_width) * gui.char_width;
3011 *valid_h = base_height +
3012 ((h - base_height) / gui.char_height) * gui.char_height;
3013}
3014
3015 void
3016gui_mch_flash(int msec)
3017{
3018 RECT rc;
3019
3020 /*
3021 * Note: InvertRect() excludes right and bottom of rectangle.
3022 */
3023 rc.left = 0;
3024 rc.top = 0;
3025 rc.right = gui.num_cols * gui.char_width;
3026 rc.bottom = gui.num_rows * gui.char_height;
3027 InvertRect(s_hdc, &rc);
3028 gui_mch_flush(); /* make sure it's displayed */
3029
3030 ui_delay((long)msec, TRUE); /* wait for a few msec */
3031
3032 InvertRect(s_hdc, &rc);
3033}
3034
3035/*
3036 * Return flags used for scrolling.
3037 * The SW_INVALIDATE is required when part of the window is covered or
3038 * off-screen. Refer to MS KB Q75236.
3039 */
3040 static int
3041get_scroll_flags(void)
3042{
3043 HWND hwnd;
3044 RECT rcVim, rcOther, rcDest;
3045
3046 GetWindowRect(s_hwnd, &rcVim);
3047
3048 /* Check if the window is partly above or below the screen. We don't care
3049 * about partly left or right of the screen, it is not relevant when
3050 * scrolling up or down. */
3051 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
3052 return SW_INVALIDATE;
3053
3054 /* Check if there is an window (partly) on top of us. */
3055 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3056 if (IsWindowVisible(hwnd))
3057 {
3058 GetWindowRect(hwnd, &rcOther);
3059 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3060 return SW_INVALIDATE;
3061 }
3062 return 0;
3063}
3064
3065/*
3066 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3067 * may not be scrolled out properly.
3068 * For gVim, when _OnScroll() is repeated, the character at the
3069 * previous cursor position may be left drawn after scroll.
3070 * The problem can be avoided by calling GetPixel() to get a pixel in
3071 * the region before ScrollWindowEx().
3072 */
3073 static void
3074intel_gpu_workaround(void)
3075{
3076 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3077}
3078
3079/*
3080 * Delete the given number of lines from the given row, scrolling up any
3081 * text further down within the scroll region.
3082 */
3083 void
3084gui_mch_delete_lines(
3085 int row,
3086 int num_lines)
3087{
3088 RECT rc;
3089
3090 intel_gpu_workaround();
3091
3092 rc.left = FILL_X(gui.scroll_region_left);
3093 rc.right = FILL_X(gui.scroll_region_right + 1);
3094 rc.top = FILL_Y(row);
3095 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3096
3097 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
3098 &rc, &rc, NULL, NULL, get_scroll_flags());
3099
3100 UpdateWindow(s_textArea);
3101 /* This seems to be required to avoid the cursor disappearing when
3102 * scrolling such that the cursor ends up in the top-left character on
3103 * the screen... But why? (Webb) */
3104 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3105 /* gui.cursor_is_valid = FALSE; */
3106
3107 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3108 gui.scroll_region_left,
3109 gui.scroll_region_bot, gui.scroll_region_right);
3110}
3111
3112/*
3113 * Insert the given number of lines before the given row, scrolling down any
3114 * following text within the scroll region.
3115 */
3116 void
3117gui_mch_insert_lines(
3118 int row,
3119 int num_lines)
3120{
3121 RECT rc;
3122
3123 intel_gpu_workaround();
3124
3125 rc.left = FILL_X(gui.scroll_region_left);
3126 rc.right = FILL_X(gui.scroll_region_right + 1);
3127 rc.top = FILL_Y(row);
3128 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3129 /* The SW_INVALIDATE is required when part of the window is covered or
3130 * off-screen. How do we avoid it when it's not needed? */
3131 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
3132 &rc, &rc, NULL, NULL, get_scroll_flags());
3133
3134 UpdateWindow(s_textArea);
3135
3136 gui_clear_block(row, gui.scroll_region_left,
3137 row + num_lines - 1, gui.scroll_region_right);
3138}
3139
3140
3141/*ARGSUSED*/
3142 void
3143gui_mch_exit(int rc)
3144{
3145#if defined(FEAT_DIRECTX)
3146 DWriteContext_Close(s_dwc);
3147 DWrite_Final();
3148 s_dwc = NULL;
3149#endif
3150
3151 ReleaseDC(s_textArea, s_hdc);
3152 DeleteObject(s_brush);
3153
3154#ifdef FEAT_TEAROFF
3155 /* Unload the tearoff bitmap */
3156 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3157#endif
3158
3159 /* Destroy our window (if we have one). */
3160 if (s_hwnd != NULL)
3161 {
3162 destroying = TRUE; /* ignore WM_DESTROY message now */
3163 DestroyWindow(s_hwnd);
3164 }
3165
3166#ifdef GLOBAL_IME
3167 global_ime_end();
3168#endif
3169}
3170
3171 static char_u *
3172logfont2name(LOGFONT lf)
3173{
3174 char *p;
3175 char *res;
3176 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003177 char *quality_name;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003178 char *font_name = lf.lfFaceName;
3179
3180 charset_name = charset_id2name((int)lf.lfCharSet);
3181#ifdef FEAT_MBYTE
3182 /* Convert a font name from the current codepage to 'encoding'.
3183 * TODO: Use Wide APIs (including LOGFONTW) instead of ANSI APIs. */
3184 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
3185 {
3186 int len;
3187 acp_to_enc((char_u *)lf.lfFaceName, (int)strlen(lf.lfFaceName),
3188 (char_u **)&font_name, &len);
3189 }
3190#endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003191 quality_name = quality_id2name((int)lf.lfQuality);
3192
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003193 res = (char *)alloc((unsigned)(strlen(font_name) + 20
3194 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
3195 if (res != NULL)
3196 {
3197 p = res;
3198 /* make a normal font string out of the lf thing:*/
3199 sprintf((char *)p, "%s:h%d", font_name, pixels_to_points(
3200 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
3201 while (*p)
3202 {
3203 if (*p == ' ')
3204 *p = '_';
3205 ++p;
3206 }
3207 if (lf.lfItalic)
3208 STRCAT(p, ":i");
3209 if (lf.lfWeight >= FW_BOLD)
3210 STRCAT(p, ":b");
3211 if (lf.lfUnderline)
3212 STRCAT(p, ":u");
3213 if (lf.lfStrikeOut)
3214 STRCAT(p, ":s");
3215 if (charset_name != NULL)
3216 {
3217 STRCAT(p, ":c");
3218 STRCAT(p, charset_name);
3219 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003220 if (quality_name != NULL)
3221 {
3222 STRCAT(p, ":q");
3223 STRCAT(p, quality_name);
3224 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003225 }
3226
3227#ifdef FEAT_MBYTE
3228 if (font_name != lf.lfFaceName)
3229 vim_free(font_name);
3230#endif
3231 return (char_u *)res;
3232}
3233
3234
3235#ifdef FEAT_MBYTE_IME
3236/*
3237 * Set correct LOGFONT to IME. Use 'guifontwide' if available, otherwise use
3238 * 'guifont'
3239 */
3240 static void
3241update_im_font(void)
3242{
3243 LOGFONT lf_wide;
3244
3245 if (p_guifontwide != NULL && *p_guifontwide != NUL
3246 && gui.wide_font != NOFONT
3247 && GetObject((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
3248 norm_logfont = lf_wide;
3249 else
3250 norm_logfont = sub_logfont;
3251 im_set_font(&norm_logfont);
3252}
3253#endif
3254
3255#ifdef FEAT_MBYTE
3256/*
3257 * Handler of gui.wide_font (p_guifontwide) changed notification.
3258 */
3259 void
3260gui_mch_wide_font_changed(void)
3261{
3262 LOGFONT lf;
3263
3264# ifdef FEAT_MBYTE_IME
3265 update_im_font();
3266# endif
3267
3268 gui_mch_free_font(gui.wide_ital_font);
3269 gui.wide_ital_font = NOFONT;
3270 gui_mch_free_font(gui.wide_bold_font);
3271 gui.wide_bold_font = NOFONT;
3272 gui_mch_free_font(gui.wide_boldital_font);
3273 gui.wide_boldital_font = NOFONT;
3274
3275 if (gui.wide_font
3276 && GetObject((HFONT)gui.wide_font, sizeof(lf), &lf))
3277 {
3278 if (!lf.lfItalic)
3279 {
3280 lf.lfItalic = TRUE;
3281 gui.wide_ital_font = get_font_handle(&lf);
3282 lf.lfItalic = FALSE;
3283 }
3284 if (lf.lfWeight < FW_BOLD)
3285 {
3286 lf.lfWeight = FW_BOLD;
3287 gui.wide_bold_font = get_font_handle(&lf);
3288 if (!lf.lfItalic)
3289 {
3290 lf.lfItalic = TRUE;
3291 gui.wide_boldital_font = get_font_handle(&lf);
3292 }
3293 }
3294 }
3295}
3296#endif
3297
3298/*
3299 * Initialise vim to use the font with the given name.
3300 * Return FAIL if the font could not be loaded, OK otherwise.
3301 */
3302/*ARGSUSED*/
3303 int
3304gui_mch_init_font(char_u *font_name, int fontset)
3305{
3306 LOGFONT lf;
3307 GuiFont font = NOFONT;
3308 char_u *p;
3309
3310 /* Load the font */
3311 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3312 font = get_font_handle(&lf);
3313 if (font == NOFONT)
3314 return FAIL;
3315
3316 if (font_name == NULL)
3317 font_name = (char_u *)lf.lfFaceName;
3318#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3319 norm_logfont = lf;
3320 sub_logfont = lf;
3321#endif
3322#ifdef FEAT_MBYTE_IME
3323 update_im_font();
3324#endif
3325 gui_mch_free_font(gui.norm_font);
3326 gui.norm_font = font;
3327 current_font_height = lf.lfHeight;
3328 GetFontSize(font);
3329
3330 p = logfont2name(lf);
3331 if (p != NULL)
3332 {
3333 hl_set_font_name(p);
3334
3335 /* When setting 'guifont' to "*" replace it with the actual font name.
3336 * */
3337 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3338 {
3339 vim_free(p_guifont);
3340 p_guifont = p;
3341 }
3342 else
3343 vim_free(p);
3344 }
3345
3346 gui_mch_free_font(gui.ital_font);
3347 gui.ital_font = NOFONT;
3348 gui_mch_free_font(gui.bold_font);
3349 gui.bold_font = NOFONT;
3350 gui_mch_free_font(gui.boldital_font);
3351 gui.boldital_font = NOFONT;
3352
3353 if (!lf.lfItalic)
3354 {
3355 lf.lfItalic = TRUE;
3356 gui.ital_font = get_font_handle(&lf);
3357 lf.lfItalic = FALSE;
3358 }
3359 if (lf.lfWeight < FW_BOLD)
3360 {
3361 lf.lfWeight = FW_BOLD;
3362 gui.bold_font = get_font_handle(&lf);
3363 if (!lf.lfItalic)
3364 {
3365 lf.lfItalic = TRUE;
3366 gui.boldital_font = get_font_handle(&lf);
3367 }
3368 }
3369
3370 return OK;
3371}
3372
3373#ifndef WPF_RESTORETOMAXIMIZED
3374# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3375#endif
3376
3377/*
3378 * Return TRUE if the GUI window is maximized, filling the whole screen.
3379 */
3380 int
3381gui_mch_maximized(void)
3382{
3383 WINDOWPLACEMENT wp;
3384
3385 wp.length = sizeof(WINDOWPLACEMENT);
3386 if (GetWindowPlacement(s_hwnd, &wp))
3387 return wp.showCmd == SW_SHOWMAXIMIZED
3388 || (wp.showCmd == SW_SHOWMINIMIZED
3389 && wp.flags == WPF_RESTORETOMAXIMIZED);
3390
3391 return 0;
3392}
3393
3394/*
3395 * Called when the font changed while the window is maximized. Compute the
3396 * new Rows and Columns. This is like resizing the window.
3397 */
3398 void
3399gui_mch_newfont(void)
3400{
3401 RECT rect;
3402
3403 GetWindowRect(s_hwnd, &rect);
3404 if (win_socket_id == 0)
3405 {
3406 gui_resize_shell(rect.right - rect.left
3407 - (GetSystemMetrics(SM_CXFRAME) +
3408 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3409 rect.bottom - rect.top
3410 - (GetSystemMetrics(SM_CYFRAME) +
3411 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3412 - GetSystemMetrics(SM_CYCAPTION)
3413#ifdef FEAT_MENU
3414 - gui_mswin_get_menu_height(FALSE)
3415#endif
3416 );
3417 }
3418 else
3419 {
3420 /* Inside another window, don't use the frame and border. */
3421 gui_resize_shell(rect.right - rect.left,
3422 rect.bottom - rect.top
3423#ifdef FEAT_MENU
3424 - gui_mswin_get_menu_height(FALSE)
3425#endif
3426 );
3427 }
3428}
3429
3430/*
3431 * Set the window title
3432 */
3433/*ARGSUSED*/
3434 void
3435gui_mch_settitle(
3436 char_u *title,
3437 char_u *icon)
3438{
3439 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3440}
3441
3442#ifdef FEAT_MOUSESHAPE
3443/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3444 * misc2.c! */
3445static LPCSTR mshape_idcs[] =
3446{
3447 IDC_ARROW, /* arrow */
3448 MAKEINTRESOURCE(0), /* blank */
3449 IDC_IBEAM, /* beam */
3450 IDC_SIZENS, /* updown */
3451 IDC_SIZENS, /* udsizing */
3452 IDC_SIZEWE, /* leftright */
3453 IDC_SIZEWE, /* lrsizing */
3454 IDC_WAIT, /* busy */
3455#ifdef WIN3264
3456 IDC_NO, /* no */
3457#else
3458 IDC_ICON, /* no */
3459#endif
3460 IDC_ARROW, /* crosshair */
3461 IDC_ARROW, /* hand1 */
3462 IDC_ARROW, /* hand2 */
3463 IDC_ARROW, /* pencil */
3464 IDC_ARROW, /* question */
3465 IDC_ARROW, /* right-arrow */
3466 IDC_UPARROW, /* up-arrow */
3467 IDC_ARROW /* last one */
3468};
3469
3470 void
3471mch_set_mouse_shape(int shape)
3472{
3473 LPCSTR idc;
3474
3475 if (shape == MSHAPE_HIDE)
3476 ShowCursor(FALSE);
3477 else
3478 {
3479 if (shape >= MSHAPE_NUMBERED)
3480 idc = IDC_ARROW;
3481 else
3482 idc = mshape_idcs[shape];
3483#ifdef SetClassLongPtr
3484 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3485#else
3486# ifdef WIN32
3487 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
3488# else /* Win16 */
3489 SetClassWord(s_textArea, GCW_HCURSOR, (WORD)LoadCursor(NULL, idc));
3490# endif
3491#endif
3492 if (!p_mh)
3493 {
3494 POINT mp;
3495
3496 /* Set the position to make it redrawn with the new shape. */
3497 (void)GetCursorPos((LPPOINT)&mp);
3498 (void)SetCursorPos(mp.x, mp.y);
3499 ShowCursor(TRUE);
3500 }
3501 }
3502}
3503#endif
3504
3505#ifdef FEAT_BROWSE
3506/*
3507 * The file browser exists in two versions: with "W" uses wide characters,
3508 * without "W" the current codepage. When FEAT_MBYTE is defined and on
3509 * Windows NT/2000/XP the "W" functions are used.
3510 */
3511
3512# if defined(FEAT_MBYTE) && defined(WIN3264)
3513/*
3514 * Wide version of convert_filter().
3515 */
3516 static WCHAR *
3517convert_filterW(char_u *s)
3518{
3519 char_u *tmp;
3520 int len;
3521 WCHAR *res;
3522
3523 tmp = convert_filter(s);
3524 if (tmp == NULL)
3525 return NULL;
3526 len = (int)STRLEN(s) + 3;
3527 res = enc_to_utf16(tmp, &len);
3528 vim_free(tmp);
3529 return res;
3530}
3531
3532/*
3533 * Wide version of gui_mch_browse(). Keep in sync!
3534 */
3535 static char_u *
3536gui_mch_browseW(
3537 int saving,
3538 char_u *title,
3539 char_u *dflt,
3540 char_u *ext,
3541 char_u *initdir,
3542 char_u *filter)
3543{
3544 /* We always use the wide function. This means enc_to_utf16() must work,
3545 * otherwise it fails miserably! */
3546 OPENFILENAMEW fileStruct;
3547 WCHAR fileBuf[MAXPATHL];
3548 WCHAR *wp;
3549 int i;
3550 WCHAR *titlep = NULL;
3551 WCHAR *extp = NULL;
3552 WCHAR *initdirp = NULL;
3553 WCHAR *filterp;
3554 char_u *p;
3555
3556 if (dflt == NULL)
3557 fileBuf[0] = NUL;
3558 else
3559 {
3560 wp = enc_to_utf16(dflt, NULL);
3561 if (wp == NULL)
3562 fileBuf[0] = NUL;
3563 else
3564 {
3565 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3566 fileBuf[i] = wp[i];
3567 fileBuf[i] = NUL;
3568 vim_free(wp);
3569 }
3570 }
3571
3572 /* Convert the filter to Windows format. */
3573 filterp = convert_filterW(filter);
3574
3575 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003576#ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003577 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003578 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003579#else
3580 fileStruct.lStructSize = sizeof(fileStruct);
3581#endif
3582
3583 if (title != NULL)
3584 titlep = enc_to_utf16(title, NULL);
3585 fileStruct.lpstrTitle = titlep;
3586
3587 if (ext != NULL)
3588 extp = enc_to_utf16(ext, NULL);
3589 fileStruct.lpstrDefExt = extp;
3590
3591 fileStruct.lpstrFile = fileBuf;
3592 fileStruct.nMaxFile = MAXPATHL;
3593 fileStruct.lpstrFilter = filterp;
3594 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3595 /* has an initial dir been specified? */
3596 if (initdir != NULL && *initdir != NUL)
3597 {
3598 /* Must have backslashes here, no matter what 'shellslash' says */
3599 initdirp = enc_to_utf16(initdir, NULL);
3600 if (initdirp != NULL)
3601 {
3602 for (wp = initdirp; *wp != NUL; ++wp)
3603 if (*wp == '/')
3604 *wp = '\\';
3605 }
3606 fileStruct.lpstrInitialDir = initdirp;
3607 }
3608
3609 /*
3610 * TODO: Allow selection of multiple files. Needs another arg to this
3611 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3612 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3613 * files that don't exist yet, so I haven't put it in. What about
3614 * OFN_PATHMUSTEXIST?
3615 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3616 */
3617 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
3618#ifdef FEAT_SHORTCUT
3619 if (curbuf->b_p_bin)
3620 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
3621#endif
3622 if (saving)
3623 {
3624 if (!GetSaveFileNameW(&fileStruct))
3625 return NULL;
3626 }
3627 else
3628 {
3629 if (!GetOpenFileNameW(&fileStruct))
3630 return NULL;
3631 }
3632
3633 vim_free(filterp);
3634 vim_free(initdirp);
3635 vim_free(titlep);
3636 vim_free(extp);
3637
3638 /* Convert from UCS2 to 'encoding'. */
3639 p = utf16_to_enc(fileBuf, NULL);
3640 if (p != NULL)
3641 /* when out of memory we get garbage for non-ASCII chars */
3642 STRCPY(fileBuf, p);
3643 vim_free(p);
3644
3645 /* Give focus back to main window (when using MDI). */
3646 SetFocus(s_hwnd);
3647
3648 /* Shorten the file name if possible */
3649 return vim_strsave(shorten_fname1((char_u *)fileBuf));
3650}
3651# endif /* FEAT_MBYTE */
3652
3653
3654/*
3655 * Convert the string s to the proper format for a filter string by replacing
3656 * the \t and \n delimiters with \0.
3657 * Returns the converted string in allocated memory.
3658 *
3659 * Keep in sync with convert_filterW() above!
3660 */
3661 static char_u *
3662convert_filter(char_u *s)
3663{
3664 char_u *res;
3665 unsigned s_len = (unsigned)STRLEN(s);
3666 unsigned i;
3667
3668 res = alloc(s_len + 3);
3669 if (res != NULL)
3670 {
3671 for (i = 0; i < s_len; ++i)
3672 if (s[i] == '\t' || s[i] == '\n')
3673 res[i] = '\0';
3674 else
3675 res[i] = s[i];
3676 res[s_len] = NUL;
3677 /* Add two extra NULs to make sure it's properly terminated. */
3678 res[s_len + 1] = NUL;
3679 res[s_len + 2] = NUL;
3680 }
3681 return res;
3682}
3683
3684/*
3685 * Select a directory.
3686 */
3687 char_u *
3688gui_mch_browsedir(char_u *title, char_u *initdir)
3689{
3690 /* We fake this: Use a filter that doesn't select anything and a default
3691 * file name that won't be used. */
3692 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3693 initdir, (char_u *)_("Directory\t*.nothing\n"));
3694}
3695
3696/*
3697 * Pop open a file browser and return the file selected, in allocated memory,
3698 * or NULL if Cancel is hit.
3699 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3700 * title - Title message for the file browser dialog.
3701 * dflt - Default name of file.
3702 * ext - Default extension to be added to files without extensions.
3703 * initdir - directory in which to open the browser (NULL = current dir)
3704 * filter - Filter for matched files to choose from.
3705 *
3706 * Keep in sync with gui_mch_browseW() above!
3707 */
3708 char_u *
3709gui_mch_browse(
3710 int saving,
3711 char_u *title,
3712 char_u *dflt,
3713 char_u *ext,
3714 char_u *initdir,
3715 char_u *filter)
3716{
3717 OPENFILENAME fileStruct;
3718 char_u fileBuf[MAXPATHL];
3719 char_u *initdirp = NULL;
3720 char_u *filterp;
3721 char_u *p;
3722
3723# if defined(FEAT_MBYTE) && defined(WIN3264)
3724 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
3725 return gui_mch_browseW(saving, title, dflt, ext, initdir, filter);
3726# endif
3727
3728 if (dflt == NULL)
3729 fileBuf[0] = NUL;
3730 else
3731 vim_strncpy(fileBuf, dflt, MAXPATHL - 1);
3732
3733 /* Convert the filter to Windows format. */
3734 filterp = convert_filter(filter);
3735
3736 vim_memset(&fileStruct, 0, sizeof(OPENFILENAME));
3737#ifdef OPENFILENAME_SIZE_VERSION_400
3738 /* be compatible with Windows NT 4.0 */
3739 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400;
3740#else
3741 fileStruct.lStructSize = sizeof(fileStruct);
3742#endif
3743
3744 fileStruct.lpstrTitle = (LPSTR)title;
3745 fileStruct.lpstrDefExt = (LPSTR)ext;
3746
3747 fileStruct.lpstrFile = (LPSTR)fileBuf;
3748 fileStruct.nMaxFile = MAXPATHL;
3749 fileStruct.lpstrFilter = (LPSTR)filterp;
3750 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3751 /* has an initial dir been specified? */
3752 if (initdir != NULL && *initdir != NUL)
3753 {
3754 /* Must have backslashes here, no matter what 'shellslash' says */
3755 initdirp = vim_strsave(initdir);
3756 if (initdirp != NULL)
3757 for (p = initdirp; *p != NUL; ++p)
3758 if (*p == '/')
3759 *p = '\\';
3760 fileStruct.lpstrInitialDir = (LPSTR)initdirp;
3761 }
3762
3763 /*
3764 * TODO: Allow selection of multiple files. Needs another arg to this
3765 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3766 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3767 * files that don't exist yet, so I haven't put it in. What about
3768 * OFN_PATHMUSTEXIST?
3769 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3770 */
3771 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
3772#ifdef FEAT_SHORTCUT
3773 if (curbuf->b_p_bin)
3774 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
3775#endif
3776 if (saving)
3777 {
3778 if (!GetSaveFileName(&fileStruct))
3779 return NULL;
3780 }
3781 else
3782 {
3783 if (!GetOpenFileName(&fileStruct))
3784 return NULL;
3785 }
3786
3787 vim_free(filterp);
3788 vim_free(initdirp);
3789
3790 /* Give focus back to main window (when using MDI). */
3791 SetFocus(s_hwnd);
3792
3793 /* Shorten the file name if possible */
3794 return vim_strsave(shorten_fname1((char_u *)fileBuf));
3795}
3796#endif /* FEAT_BROWSE */
3797
3798/*ARGSUSED*/
3799 static void
3800_OnDropFiles(
3801 HWND hwnd,
3802 HDROP hDrop)
3803{
3804#ifdef FEAT_WINDOWS
3805#ifdef WIN3264
3806# define BUFPATHLEN _MAX_PATH
3807# define DRAGQVAL 0xFFFFFFFF
3808#else
3809# define BUFPATHLEN MAXPATHL
3810# define DRAGQVAL 0xFFFF
3811#endif
3812#ifdef FEAT_MBYTE
3813 WCHAR wszFile[BUFPATHLEN];
3814#endif
3815 char szFile[BUFPATHLEN];
3816 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3817 UINT i;
3818 char_u **fnames;
3819 POINT pt;
3820 int_u modifiers = 0;
3821
3822 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3823
3824 /* Obtain dropped position */
3825 DragQueryPoint(hDrop, &pt);
3826 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3827
3828 reset_VIsual();
3829
3830 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3831
3832 if (fnames != NULL)
3833 for (i = 0; i < cFiles; ++i)
3834 {
3835#ifdef FEAT_MBYTE
3836 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3837 fnames[i] = utf16_to_enc(wszFile, NULL);
3838 else
3839#endif
3840 {
3841 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3842 fnames[i] = vim_strsave((char_u *)szFile);
3843 }
3844 }
3845
3846 DragFinish(hDrop);
3847
3848 if (fnames != NULL)
3849 {
3850 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3851 modifiers |= MOUSE_SHIFT;
3852 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3853 modifiers |= MOUSE_CTRL;
3854 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3855 modifiers |= MOUSE_ALT;
3856
3857 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3858
3859 s_need_activate = TRUE;
3860 }
3861#endif
3862}
3863
3864/*ARGSUSED*/
3865 static int
3866_OnScroll(
3867 HWND hwnd,
3868 HWND hwndCtl,
3869 UINT code,
3870 int pos)
3871{
3872 static UINT prev_code = 0; /* code of previous call */
3873 scrollbar_T *sb, *sb_info;
3874 long val;
3875 int dragging = FALSE;
3876 int dont_scroll_save = dont_scroll;
3877#ifndef WIN3264
3878 int nPos;
3879#else
3880 SCROLLINFO si;
3881
3882 si.cbSize = sizeof(si);
3883 si.fMask = SIF_POS;
3884#endif
3885
3886 sb = gui_mswin_find_scrollbar(hwndCtl);
3887 if (sb == NULL)
3888 return 0;
3889
3890 if (sb->wp != NULL) /* Left or right scrollbar */
3891 {
3892 /*
3893 * Careful: need to get scrollbar info out of first (left) scrollbar
3894 * for window, but keep real scrollbar too because we must pass it to
3895 * gui_drag_scrollbar().
3896 */
3897 sb_info = &sb->wp->w_scrollbars[0];
3898 }
3899 else /* Bottom scrollbar */
3900 sb_info = sb;
3901 val = sb_info->value;
3902
3903 switch (code)
3904 {
3905 case SB_THUMBTRACK:
3906 val = pos;
3907 dragging = TRUE;
3908 if (sb->scroll_shift > 0)
3909 val <<= sb->scroll_shift;
3910 break;
3911 case SB_LINEDOWN:
3912 val++;
3913 break;
3914 case SB_LINEUP:
3915 val--;
3916 break;
3917 case SB_PAGEDOWN:
3918 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3919 break;
3920 case SB_PAGEUP:
3921 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3922 break;
3923 case SB_TOP:
3924 val = 0;
3925 break;
3926 case SB_BOTTOM:
3927 val = sb_info->max;
3928 break;
3929 case SB_ENDSCROLL:
3930 if (prev_code == SB_THUMBTRACK)
3931 {
3932 /*
3933 * "pos" only gives us 16-bit data. In case of large file,
3934 * use GetScrollPos() which returns 32-bit. Unfortunately it
3935 * is not valid while the scrollbar is being dragged.
3936 */
3937 val = GetScrollPos(hwndCtl, SB_CTL);
3938 if (sb->scroll_shift > 0)
3939 val <<= sb->scroll_shift;
3940 }
3941 break;
3942
3943 default:
3944 /* TRACE("Unknown scrollbar event %d\n", code); */
3945 return 0;
3946 }
3947 prev_code = code;
3948
3949#ifdef WIN3264
3950 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3951 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
3952#else
3953 nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3954 SetScrollPos(hwndCtl, SB_CTL, nPos, TRUE);
3955#endif
3956
3957 /*
3958 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3959 */
3960 if (sb->wp != NULL)
3961 {
3962 scrollbar_T *sba = sb->wp->w_scrollbars;
3963 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3964
3965#ifdef WIN3264
3966 SetScrollInfo(id, SB_CTL, &si, TRUE);
3967#else
3968 SetScrollPos(id, SB_CTL, nPos, TRUE);
3969#endif
3970 }
3971
3972 /* Don't let us be interrupted here by another message. */
3973 s_busy_processing = TRUE;
3974
3975 /* When "allow_scrollbar" is FALSE still need to remember the new
3976 * position, but don't actually scroll by setting "dont_scroll". */
3977 dont_scroll = !allow_scrollbar;
3978
3979 gui_drag_scrollbar(sb, val, dragging);
3980
3981 s_busy_processing = FALSE;
3982 dont_scroll = dont_scroll_save;
3983
3984 return 0;
3985}
3986
3987
3988/*
3989 * Get command line arguments.
3990 * Use "prog" as the name of the program and "cmdline" as the arguments.
3991 * Copy the arguments to allocated memory.
3992 * Return the number of arguments (including program name).
3993 * Return pointers to the arguments in "argvp". Memory is allocated with
3994 * malloc(), use free() instead of vim_free().
3995 * Return pointer to buffer in "tofree".
3996 * Returns zero when out of memory.
3997 */
3998/*ARGSUSED*/
3999 int
4000get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
4001{
4002 int i;
4003 char *p;
4004 char *progp;
4005 char *pnew = NULL;
4006 char *newcmdline;
4007 int inquote;
4008 int argc;
4009 char **argv = NULL;
4010 int round;
4011
4012 *tofree = NULL;
4013
4014#ifdef FEAT_MBYTE
4015 /* Try using the Unicode version first, it takes care of conversion when
4016 * 'encoding' is changed. */
4017 argc = get_cmd_argsW(&argv);
4018 if (argc != 0)
4019 goto done;
4020#endif
4021
4022 /* Handle the program name. Remove the ".exe" extension, and find the 1st
4023 * non-space. */
4024 p = strrchr(prog, '.');
4025 if (p != NULL)
4026 *p = NUL;
4027 for (progp = prog; *progp == ' '; ++progp)
4028 ;
4029
4030 /* The command line is copied to allocated memory, so that we can change
4031 * it. Add the size of the string, the separating NUL and a terminating
4032 * NUL. */
4033 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
4034 if (newcmdline == NULL)
4035 return 0;
4036
4037 /*
4038 * First round: count the number of arguments ("pnew" == NULL).
4039 * Second round: produce the arguments.
4040 */
4041 for (round = 1; round <= 2; ++round)
4042 {
4043 /* First argument is the program name. */
4044 if (pnew != NULL)
4045 {
4046 argv[0] = pnew;
4047 strcpy(pnew, progp);
4048 pnew += strlen(pnew);
4049 *pnew++ = NUL;
4050 }
4051
4052 /*
4053 * Isolate each argument and put it in argv[].
4054 */
4055 p = cmdline;
4056 argc = 1;
4057 while (*p != NUL)
4058 {
4059 inquote = FALSE;
4060 if (pnew != NULL)
4061 argv[argc] = pnew;
4062 ++argc;
4063 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
4064 {
4065 /* Backslashes are only special when followed by a double
4066 * quote. */
4067 i = (int)strspn(p, "\\");
4068 if (p[i] == '"')
4069 {
4070 /* Halve the number of backslashes. */
4071 if (i > 1 && pnew != NULL)
4072 {
4073 vim_memset(pnew, '\\', i / 2);
4074 pnew += i / 2;
4075 }
4076
4077 /* Even nr of backslashes toggles quoting, uneven copies
4078 * the double quote. */
4079 if ((i & 1) == 0)
4080 inquote = !inquote;
4081 else if (pnew != NULL)
4082 *pnew++ = '"';
4083 p += i + 1;
4084 }
4085 else if (i > 0)
4086 {
4087 /* Copy span of backslashes unmodified. */
4088 if (pnew != NULL)
4089 {
4090 vim_memset(pnew, '\\', i);
4091 pnew += i;
4092 }
4093 p += i;
4094 }
4095 else
4096 {
4097 if (pnew != NULL)
4098 *pnew++ = *p;
4099#ifdef FEAT_MBYTE
4100 /* Can't use mb_* functions, because 'encoding' is not
4101 * initialized yet here. */
4102 if (IsDBCSLeadByte(*p))
4103 {
4104 ++p;
4105 if (pnew != NULL)
4106 *pnew++ = *p;
4107 }
4108#endif
4109 ++p;
4110 }
4111 }
4112
4113 if (pnew != NULL)
4114 *pnew++ = NUL;
4115 while (*p == ' ' || *p == '\t')
4116 ++p; /* advance until a non-space */
4117 }
4118
4119 if (round == 1)
4120 {
4121 argv = (char **)malloc((argc + 1) * sizeof(char *));
4122 if (argv == NULL )
4123 {
4124 free(newcmdline);
4125 return 0; /* malloc error */
4126 }
4127 pnew = newcmdline;
4128 *tofree = newcmdline;
4129 }
4130 }
4131
4132#ifdef FEAT_MBYTE
4133done:
4134#endif
4135 argv[argc] = NULL; /* NULL-terminated list */
4136 *argvp = argv;
4137 return argc;
4138}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139
4140#ifdef FEAT_XPM_W32
4141# include "xpm_w32.h"
4142#endif
4143
4144#ifdef PROTO
4145# define WINAPI
4146#endif
4147
4148#ifdef __MINGW32__
4149/*
4150 * Add a lot of missing defines.
4151 * They are not always missing, we need the #ifndef's.
4152 */
4153# ifndef _cdecl
4154# define _cdecl
4155# endif
4156# ifndef IsMinimized
4157# define IsMinimized(hwnd) IsIconic(hwnd)
4158# endif
4159# ifndef IsMaximized
4160# define IsMaximized(hwnd) IsZoomed(hwnd)
4161# endif
4162# ifndef SelectFont
4163# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
4164# endif
4165# ifndef GetStockBrush
4166# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
4167# endif
4168# ifndef DeleteBrush
4169# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
4170# endif
4171
4172# ifndef HANDLE_WM_RBUTTONDBLCLK
4173# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4174 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4175# endif
4176# ifndef HANDLE_WM_MBUTTONUP
4177# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
4178 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4179# endif
4180# ifndef HANDLE_WM_MBUTTONDBLCLK
4181# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4182 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4183# endif
4184# ifndef HANDLE_WM_LBUTTONDBLCLK
4185# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4186 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4187# endif
4188# ifndef HANDLE_WM_RBUTTONDOWN
4189# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
4190 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4191# endif
4192# ifndef HANDLE_WM_MOUSEMOVE
4193# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
4194 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4195# endif
4196# ifndef HANDLE_WM_RBUTTONUP
4197# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
4198 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4199# endif
4200# ifndef HANDLE_WM_MBUTTONDOWN
4201# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
4202 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4203# endif
4204# ifndef HANDLE_WM_LBUTTONUP
4205# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
4206 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4207# endif
4208# ifndef HANDLE_WM_LBUTTONDOWN
4209# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
4210 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4211# endif
4212# ifndef HANDLE_WM_SYSCHAR
4213# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
4214 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4215# endif
4216# ifndef HANDLE_WM_ACTIVATEAPP
4217# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
4218 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
4219# endif
4220# ifndef HANDLE_WM_WINDOWPOSCHANGING
4221# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
4222 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
4223# endif
4224# ifndef HANDLE_WM_VSCROLL
4225# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
4226 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4227# endif
4228# ifndef HANDLE_WM_SETFOCUS
4229# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
4230 ((fn)((hwnd), (HWND)(wParam)), 0L)
4231# endif
4232# ifndef HANDLE_WM_KILLFOCUS
4233# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
4234 ((fn)((hwnd), (HWND)(wParam)), 0L)
4235# endif
4236# ifndef HANDLE_WM_HSCROLL
4237# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
4238 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4239# endif
4240# ifndef HANDLE_WM_DROPFILES
4241# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
4242 ((fn)((hwnd), (HDROP)(wParam)), 0L)
4243# endif
4244# ifndef HANDLE_WM_CHAR
4245# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
4246 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4247# endif
4248# ifndef HANDLE_WM_SYSDEADCHAR
4249# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
4250 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4251# endif
4252# ifndef HANDLE_WM_DEADCHAR
4253# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4254 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4255# endif
4256#endif /* __MINGW32__ */
4257
4258
4259/* Some parameters for tearoff menus. All in pixels. */
4260#define TEAROFF_PADDING_X 2
4261#define TEAROFF_BUTTON_PAD_X 8
4262#define TEAROFF_MIN_WIDTH 200
4263#define TEAROFF_SUBMENU_LABEL ">>"
4264#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4265
4266
4267/* For the Intellimouse: */
4268#ifndef WM_MOUSEWHEEL
4269#define WM_MOUSEWHEEL 0x20a
4270#endif
4271
4272
4273#ifdef FEAT_BEVAL
4274# define ID_BEVAL_TOOLTIP 200
4275# define BEVAL_TEXT_LEN MAXPATHL
4276
Bram Moolenaar167632f2010-05-26 21:42:54 +02004277#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004278/* Work around old versions of basetsd.h which wrongly declares
4279 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02004280# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00004281# define UINT_PTR UINT
4282#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004283
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004284static void make_tooltip(BalloonEval *beval, char *text, POINT pt);
4285static void delete_tooltip(BalloonEval *beval);
4286static VOID CALLBACK BevalTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004287
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004289static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004291
Bram Moolenaar82881492012-11-20 16:53:39 +01004292
4293/* cproto fails on missing include files */
4294#ifndef PROTO
4295
Bram Moolenaar45360022005-07-21 21:08:21 +00004296/*
4297 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004298 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004299 */
Bram Moolenaar82881492012-11-20 16:53:39 +01004300# include <pshpack1.h>
4301
4302#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004303
4304typedef struct _DllVersionInfo
4305{
4306 DWORD cbSize;
4307 DWORD dwMajorVersion;
4308 DWORD dwMinorVersion;
4309 DWORD dwBuildNumber;
4310 DWORD dwPlatformID;
4311} DLLVERSIONINFO;
4312
Bram Moolenaar82881492012-11-20 16:53:39 +01004313#ifndef PROTO
4314# include <poppack.h>
4315#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004316
Bram Moolenaar45360022005-07-21 21:08:21 +00004317typedef struct tagTOOLINFOA_NEW
4318{
4319 UINT cbSize;
4320 UINT uFlags;
4321 HWND hwnd;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004322 UINT_PTR uId;
Bram Moolenaar45360022005-07-21 21:08:21 +00004323 RECT rect;
4324 HINSTANCE hinst;
4325 LPSTR lpszText;
4326 LPARAM lParam;
4327} TOOLINFO_NEW;
4328
4329typedef struct tagNMTTDISPINFO_NEW
4330{
4331 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004332 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004333 char szText[80];
4334 HINSTANCE hinst;
4335 UINT uFlags;
4336 LPARAM lParam;
4337} NMTTDISPINFO_NEW;
4338
Bram Moolenaar45360022005-07-21 21:08:21 +00004339typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4340#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004341# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342#endif
4343
Bram Moolenaar45360022005-07-21 21:08:21 +00004344#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004345# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004346#endif
4347
4348#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004349# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004350#endif
4351
4352#endif /* defined(FEAT_BEVAL) */
4353
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004354#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4355/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4356 * it here if LPNMTTDISPINFO isn't defined.
4357 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4358 * _MSC_VER. */
4359# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4360typedef struct tagNMTTDISPINFOA {
4361 NMHDR hdr;
4362 LPSTR lpszText;
4363 char szText[80];
4364 HINSTANCE hinst;
4365 UINT uFlags;
4366 LPARAM lParam;
4367} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4368# define LPNMTTDISPINFO LPNMTTDISPINFOA
4369
4370# ifdef FEAT_MBYTE
4371typedef struct tagNMTTDISPINFOW {
4372 NMHDR hdr;
4373 LPWSTR lpszText;
4374 WCHAR szText[80];
4375 HINSTANCE hinst;
4376 UINT uFlags;
4377 LPARAM lParam;
4378} NMTTDISPINFOW, *LPNMTTDISPINFOW;
4379# endif
4380# endif
4381#endif
4382
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004383#ifndef TTN_GETDISPINFOW
4384# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4385#endif
4386
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387/* Local variables: */
4388
4389#ifdef FEAT_MENU
4390static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
4393/*
4394 * Use the system font for dialogs and tear-off menus. Remove this line to
4395 * use DLG_FONT_NAME.
4396 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004397#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
4399#define VIM_NAME "vim"
4400#define VIM_CLASS "Vim"
4401#define VIM_CLASSW L"Vim"
4402
4403/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4404 * thus there should be room for every dialog. For tearoffs it's made bigger
4405 * when needed. */
4406#define DLG_ALLOC_SIZE 16 * 1024
4407
4408/*
4409 * stuff for dialogs, menus, tearoffs etc.
4410 */
4411static LRESULT APIENTRY dialog_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004412#ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413static LRESULT APIENTRY tearoff_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415static PWORD
4416add_dialog_element(
4417 PWORD p,
4418 DWORD lStyle,
4419 WORD x,
4420 WORD y,
4421 WORD w,
4422 WORD h,
4423 WORD Id,
4424 WORD clss,
4425 const char *caption);
4426static LPWORD lpwAlign(LPWORD);
4427static int nCopyAnsiToWideChar(LPWORD, LPSTR);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004428#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431static void get_dialog_font_metrics(void);
4432
4433static int dialog_default_button = -1;
4434
4435/* Intellimouse support */
4436static int mouse_scroll_lines = 0;
4437static UINT msh_msgmousewheel = 0;
4438
4439static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4440#ifdef FEAT_TOOLBAR
4441static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004442static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443static int get_toolbar_bitmap(vimmenu_T *menu);
4444#endif
4445
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004446#ifdef FEAT_GUI_TABLINE
4447static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004448static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004449#endif
4450
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451#ifdef FEAT_MBYTE_IME
4452static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4453static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4454#endif
4455#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4456# ifdef NOIME
4457typedef struct tagCOMPOSITIONFORM {
4458 DWORD dwStyle;
4459 POINT ptCurrentPos;
4460 RECT rcArea;
4461} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4462typedef HANDLE HIMC;
4463# endif
4464
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004465static HINSTANCE hLibImm = NULL;
4466static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4467static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4468static HIMC (WINAPI *pImmGetContext)(HWND);
4469static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4470static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4471static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4472static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
4473static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
4474static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
4475static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4476static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004477static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478static void dyn_imm_load(void);
4479#else
4480# define pImmGetCompositionStringA ImmGetCompositionStringA
4481# define pImmGetCompositionStringW ImmGetCompositionStringW
4482# define pImmGetContext ImmGetContext
4483# define pImmAssociateContext ImmAssociateContext
4484# define pImmReleaseContext ImmReleaseContext
4485# define pImmGetOpenStatus ImmGetOpenStatus
4486# define pImmSetOpenStatus ImmSetOpenStatus
4487# define pImmGetCompositionFont ImmGetCompositionFontA
4488# define pImmSetCompositionFont ImmSetCompositionFontA
4489# define pImmSetCompositionWindow ImmSetCompositionWindow
4490# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004491# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492#endif
4493
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494/* multi monitor support */
4495typedef struct _MONITORINFOstruct
4496{
4497 DWORD cbSize;
4498 RECT rcMonitor;
4499 RECT rcWork;
4500 DWORD dwFlags;
4501} _MONITORINFO;
4502
4503typedef HANDLE _HMONITOR;
4504typedef _HMONITOR (WINAPI *TMonitorFromWindow)(HWND, DWORD);
4505typedef BOOL (WINAPI *TGetMonitorInfo)(_HMONITOR, _MONITORINFO *);
4506
4507static TMonitorFromWindow pMonitorFromWindow = NULL;
4508static TGetMonitorInfo pGetMonitorInfo = NULL;
4509static HANDLE user32_lib = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510/*
4511 * Return TRUE when running under Windows NT 3.x or Win32s, both of which have
4512 * less fancy GUI APIs.
4513 */
4514 static int
4515is_winnt_3(void)
4516{
4517 return ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
4518 && os_version.dwMajorVersion == 3)
4519 || (os_version.dwPlatformId == VER_PLATFORM_WIN32s));
4520}
4521
4522/*
4523 * Return TRUE when running under Win32s.
4524 */
4525 int
4526gui_is_win32s(void)
4527{
4528 return (os_version.dwPlatformId == VER_PLATFORM_WIN32s);
4529}
4530
4531#ifdef FEAT_MENU
4532/*
4533 * Figure out how high the menu bar is at the moment.
4534 */
4535 static int
4536gui_mswin_get_menu_height(
4537 int fix_window) /* If TRUE, resize window if menu height changed */
4538{
4539 static int old_menu_height = -1;
4540
4541 RECT rc1, rc2;
4542 int num;
4543 int menu_height;
4544
4545 if (gui.menu_is_active)
4546 num = GetMenuItemCount(s_menuBar);
4547 else
4548 num = 0;
4549
4550 if (num == 0)
4551 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004552 else if (IsMinimized(s_hwnd))
4553 {
4554 /* The height of the menu cannot be determined while the window is
4555 * minimized. Take the previous height if the menu is changed in that
4556 * state, to avoid that Vim's vertical window size accidentally
4557 * increases due to the unaccounted-for menu height. */
4558 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 else
4561 {
4562 if (is_winnt_3()) /* for NT 3.xx */
4563 {
4564 if (gui.starting)
4565 menu_height = GetSystemMetrics(SM_CYMENU);
4566 else
4567 {
4568 RECT r1, r2;
4569 int frameht = GetSystemMetrics(SM_CYFRAME);
4570 int capht = GetSystemMetrics(SM_CYCAPTION);
4571
4572 /* get window rect of s_hwnd
4573 * get client rect of s_hwnd
4574 * get cap height
4575 * subtract from window rect, the sum of client height,
4576 * (if not maximized)frame thickness, and caption height.
4577 */
4578 GetWindowRect(s_hwnd, &r1);
4579 GetClientRect(s_hwnd, &r2);
4580 menu_height = r1.bottom - r1.top - (r2.bottom - r2.top
4581 + 2 * frameht * (!IsZoomed(s_hwnd)) + capht);
4582 }
4583 }
4584 else /* win95 and variants (NT 4.0, I guess) */
4585 {
4586 /*
4587 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4588 * seem to have been set yet, so menu wraps in default window
4589 * width which is very narrow. Instead just return height of a
4590 * single menu item. Will still be wrong when the menu really
4591 * should wrap over more than one line.
4592 */
4593 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4594 if (gui.starting)
4595 menu_height = rc1.bottom - rc1.top + 1;
4596 else
4597 {
4598 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4599 menu_height = rc2.bottom - rc1.top + 1;
4600 }
4601 }
4602 }
4603
4604 if (fix_window && menu_height != old_menu_height)
4605 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004606 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
Bram Moolenaar71371b12015-03-24 17:57:12 +01004608 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609
4610 return menu_height;
4611}
4612#endif /*FEAT_MENU*/
4613
4614
4615/*
4616 * Setup for the Intellimouse
4617 */
4618 static void
4619init_mouse_wheel(void)
4620{
4621
4622#ifndef SPI_GETWHEELSCROLLLINES
4623# define SPI_GETWHEELSCROLLLINES 104
4624#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004625#ifndef SPI_SETWHEELSCROLLLINES
4626# define SPI_SETWHEELSCROLLLINES 105
4627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628
4629#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4630#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4631#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4632#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4633
4634 HWND hdl_mswheel;
4635 UINT msh_msgscrolllines;
4636
4637 msh_msgmousewheel = 0;
4638 mouse_scroll_lines = 3; /* reasonable default */
4639
4640 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
4641 && os_version.dwMajorVersion >= 4)
4642 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
4643 && ((os_version.dwMajorVersion == 4
4644 && os_version.dwMinorVersion >= 10)
4645 || os_version.dwMajorVersion >= 5)))
4646 {
4647 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4648 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4649 &mouse_scroll_lines, 0);
4650 }
4651 else if (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
4652 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
4653 && os_version.dwMajorVersion < 4))
4654 { /*
4655 * If Win95 or NT 3.51,
4656 * try to find the hidden point32 window.
4657 */
4658 hdl_mswheel = FindWindow(VMOUSEZ_CLASSNAME, VMOUSEZ_TITLE);
4659 if (hdl_mswheel)
4660 {
4661 msh_msgscrolllines = RegisterWindowMessage(VMSH_SCROLL_LINES);
4662 if (msh_msgscrolllines)
4663 {
4664 mouse_scroll_lines = (int)SendMessage(hdl_mswheel,
4665 msh_msgscrolllines, 0, 0);
4666 msh_msgmousewheel = RegisterWindowMessage(VMSH_MOUSEWHEEL);
4667 }
4668 }
4669 }
4670}
4671
4672
4673/* Intellimouse wheel handler */
4674 static void
4675_OnMouseWheel(
4676 HWND hwnd,
4677 short zDelta)
4678{
4679/* Treat a mouse wheel event as if it were a scroll request */
4680 int i;
4681 int size;
4682 HWND hwndCtl;
4683
4684 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4685 {
4686 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4687 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4688 }
4689 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4690 {
4691 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4692 size = curwin->w_scrollbars[SBAR_LEFT].size;
4693 }
4694 else
4695 return;
4696
4697 size = curwin->w_height;
4698 if (mouse_scroll_lines == 0)
4699 init_mouse_wheel();
4700
4701 if (mouse_scroll_lines > 0
4702 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4703 {
4704 for (i = mouse_scroll_lines; i > 0; --i)
4705 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4706 }
4707 else
4708 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
4709}
4710
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004711#ifdef USE_SYSMENU_FONT
4712/*
4713 * Get Menu Font.
4714 * Return OK or FAIL.
4715 */
4716 static int
4717gui_w32_get_menu_font(LOGFONT *lf)
4718{
4719 NONCLIENTMETRICS nm;
4720
4721 nm.cbSize = sizeof(NONCLIENTMETRICS);
4722 if (!SystemParametersInfo(
4723 SPI_GETNONCLIENTMETRICS,
4724 sizeof(NONCLIENTMETRICS),
4725 &nm,
4726 0))
4727 return FAIL;
4728 *lf = nm.lfMenuFont;
4729 return OK;
4730}
4731#endif
4732
4733
4734#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4735/*
4736 * Set the GUI tabline font to the system menu font
4737 */
4738 static void
4739set_tabline_font(void)
4740{
4741 LOGFONT lfSysmenu;
4742 HFONT font;
4743 HWND hwnd;
4744 HDC hdc;
4745 HFONT hfntOld;
4746 TEXTMETRIC tm;
4747
4748 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4749 return;
4750
4751 font = CreateFontIndirect(&lfSysmenu);
4752
4753 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4754
4755 /*
4756 * Compute the height of the font used for the tab text
4757 */
4758 hwnd = GetDesktopWindow();
4759 hdc = GetWindowDC(hwnd);
4760 hfntOld = SelectFont(hdc, font);
4761
4762 GetTextMetrics(hdc, &tm);
4763
4764 SelectFont(hdc, hfntOld);
4765 ReleaseDC(hwnd, hdc);
4766
4767 /*
4768 * The space used by the tab border and the space between the tab label
4769 * and the tab border is included as 7.
4770 */
4771 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4772}
4773#endif
4774
Bram Moolenaar520470a2005-06-16 21:59:56 +00004775/*
4776 * Invoked when a setting was changed.
4777 */
4778 static LRESULT CALLBACK
4779_OnSettingChange(UINT n)
4780{
4781 if (n == SPI_SETWHEELSCROLLLINES)
4782 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4783 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004784#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4785 if (n == SPI_SETNONCLIENTMETRICS)
4786 set_tabline_font();
4787#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004788 return 0;
4789}
4790
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791#ifdef FEAT_NETBEANS_INTG
4792 static void
4793_OnWindowPosChanged(
4794 HWND hwnd,
4795 const LPWINDOWPOS lpwpos)
4796{
4797 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004798 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799
4800 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4801 || lpwpos->cx != cx || lpwpos->cy != cy))
4802 {
4803 x = lpwpos->x;
4804 y = lpwpos->y;
4805 cx = lpwpos->cx;
4806 cy = lpwpos->cy;
4807 netbeans_frame_moved(x, y);
4808 }
4809 /* Allow to send WM_SIZE and WM_MOVE */
4810 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4811}
4812#endif
4813
4814 static int
4815_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 UINT fwSide,
4817 LPRECT lprc)
4818{
4819 int w, h;
4820 int valid_w, valid_h;
4821 int w_offset, h_offset;
4822
4823 w = lprc->right - lprc->left;
4824 h = lprc->bottom - lprc->top;
4825 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4826 w_offset = w - valid_w;
4827 h_offset = h - valid_h;
4828
4829 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4830 || fwSide == WMSZ_BOTTOMLEFT)
4831 lprc->left += w_offset;
4832 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4833 || fwSide == WMSZ_BOTTOMRIGHT)
4834 lprc->right -= w_offset;
4835
4836 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4837 || fwSide == WMSZ_TOPRIGHT)
4838 lprc->top += h_offset;
4839 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4840 || fwSide == WMSZ_BOTTOMRIGHT)
4841 lprc->bottom -= h_offset;
4842 return TRUE;
4843}
4844
4845
4846
4847 static LRESULT CALLBACK
4848_WndProc(
4849 HWND hwnd,
4850 UINT uMsg,
4851 WPARAM wParam,
4852 LPARAM lParam)
4853{
4854 /*
4855 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4856 hwnd, uMsg, wParam, lParam);
4857 */
4858
4859 HandleMouseHide(uMsg, lParam);
4860
4861 s_uMsg = uMsg;
4862 s_wParam = wParam;
4863 s_lParam = lParam;
4864
4865 switch (uMsg)
4866 {
4867 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4868 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4869 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4870 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4871 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4872 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4873 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4874 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4875 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4876#ifdef FEAT_MENU
4877 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4878#endif
4879 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4880 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4881 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4882 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4883 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4884 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4885 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4886 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4887 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4888#ifdef FEAT_NETBEANS_INTG
4889 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4890#endif
4891
Bram Moolenaarafa24992006-03-27 20:58:26 +00004892#ifdef FEAT_GUI_TABLINE
4893 case WM_RBUTTONUP:
4894 {
4895 if (gui_mch_showing_tabline())
4896 {
4897 POINT pt;
4898 RECT rect;
4899
4900 /*
4901 * If the cursor is on the tabline, display the tab menu
4902 */
4903 GetCursorPos((LPPOINT)&pt);
4904 GetWindowRect(s_textArea, &rect);
4905 if (pt.y < rect.top)
4906 {
4907 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004908 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004909 }
4910 }
4911 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4912 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004913 case WM_LBUTTONDBLCLK:
4914 {
4915 /*
4916 * If the user double clicked the tabline, create a new tab
4917 */
4918 if (gui_mch_showing_tabline())
4919 {
4920 POINT pt;
4921 RECT rect;
4922
4923 GetCursorPos((LPPOINT)&pt);
4924 GetWindowRect(s_textArea, &rect);
4925 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004926 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004927 }
4928 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4929 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004930#endif
4931
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 case WM_QUERYENDSESSION: /* System wants to go down. */
4933 gui_shell_closed(); /* Will exit when no changed buffers. */
4934 return FALSE; /* Do NOT allow system to go down. */
4935
4936 case WM_ENDSESSION:
4937 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004938 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004940 return 0L;
4941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 break;
4943
4944 case WM_CHAR:
4945 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4946 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004947 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 return 0L;
4949
4950 case WM_SYSCHAR:
4951 /*
4952 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4953 * shortcut key, handle like a typed ALT key, otherwise call Windows
4954 * ALT key handling.
4955 */
4956#ifdef FEAT_MENU
4957 if ( !gui.menu_is_active
4958 || p_wak[0] == 'n'
4959 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4960 )
4961#endif
4962 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004963 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 return 0L;
4965 }
4966#ifdef FEAT_MENU
4967 else
4968 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4969#endif
4970
4971 case WM_SYSKEYUP:
4972#ifdef FEAT_MENU
4973 /* This used to be done only when menu is active: ALT key is used for
4974 * that. But that caused problems when menu is disabled and using
4975 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4976 * are received, mouse pointer remains hidden. */
4977 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4978#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004979 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980#endif
4981
4982 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004983 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984
4985 case WM_MOUSEWHEEL:
4986 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004987 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988
Bram Moolenaar520470a2005-06-16 21:59:56 +00004989 /* Notification for change in SystemParametersInfo() */
4990 case WM_SETTINGCHANGE:
4991 return _OnSettingChange((UINT)wParam);
4992
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004993#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 case WM_NOTIFY:
4995 switch (((LPNMHDR) lParam)->code)
4996 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004997# ifdef FEAT_MBYTE
4998 case TTN_GETDISPINFOW:
4999# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005000 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005002 LPNMHDR hdr = (LPNMHDR)lParam;
5003 char_u *str = NULL;
5004 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005005
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005006 vim_free(tt_text);
5007 tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005008
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005009# ifdef FEAT_GUI_TABLINE
5010 if (gui_mch_showing_tabline()
5011 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005012 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005013 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005014 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005015 * Mouse is over the GUI tabline. Display the
5016 * tooltip for the tab under the cursor
5017 *
5018 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005019 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005020 GetCursorPos(&pt);
5021 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005022 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005023 TCHITTESTINFO htinfo;
5024 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005025
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005026 /*
5027 * Get the tab under the cursor
5028 */
5029 htinfo.pt.x = pt.x;
5030 htinfo.pt.y = pt.y;
5031 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
5032 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005033 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005034 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005035
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005036 tp = find_tabpage(idx + 1);
5037 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005038 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005039 get_tabline_label(tp, TRUE);
5040 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005041 }
5042 }
5043 }
5044 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005045# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005046# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005047# ifdef FEAT_GUI_TABLINE
5048 else
5049# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005051 UINT idButton;
5052 vimmenu_T *pMenu;
5053
5054 idButton = (UINT) hdr->idFrom;
5055 pMenu = gui_mswin_find_menu(root_menu, idButton);
5056 if (pMenu)
5057 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005059# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005060 if (str != NULL)
5061 {
5062# ifdef FEAT_MBYTE
5063 if (hdr->code == TTN_GETDISPINFOW)
5064 {
5065 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
5066
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005067 /* Set the maximum width, this also enables using
5068 * \n for line break. */
5069 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5070 0, 500);
5071
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005072 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005073 lpdi->lpszText = tt_text;
5074 /* can't show tooltip if failed */
5075 }
5076 else
5077# endif
5078 {
5079 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
5080
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005081 /* Set the maximum width, this also enables using
5082 * \n for line break. */
5083 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5084 0, 500);
5085
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005086 if (STRLEN(str) < sizeof(lpdi->szText)
5087 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005088 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005089 sizeof(lpdi->szText) - 1);
5090 else
5091 lpdi->lpszText = tt_text;
5092 }
5093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 }
5095 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005096# ifdef FEAT_GUI_TABLINE
5097 case TCN_SELCHANGE:
5098 if (gui_mch_showing_tabline()
5099 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005100 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005101 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005102 return 0L;
5103 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005104 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00005105
5106 case NM_RCLICK:
5107 if (gui_mch_showing_tabline()
5108 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005109 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00005110 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005111 return 0L;
5112 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00005113 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005114# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005116# ifdef FEAT_GUI_TABLINE
5117 if (gui_mch_showing_tabline()
5118 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
5119 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5120# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 break;
5122 }
5123 break;
5124#endif
5125#if defined(MENUHINTS) && defined(FEAT_MENU)
5126 case WM_MENUSELECT:
5127 if (((UINT) HIWORD(wParam)
5128 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
5129 == MF_HILITE
5130 && (State & CMDLINE) == 0)
5131 {
5132 UINT idButton;
5133 vimmenu_T *pMenu;
5134 static int did_menu_tip = FALSE;
5135
5136 if (did_menu_tip)
5137 {
5138 msg_clr_cmdline();
5139 setcursor();
5140 out_flush();
5141 did_menu_tip = FALSE;
5142 }
5143
5144 idButton = (UINT)LOWORD(wParam);
5145 pMenu = gui_mswin_find_menu(root_menu, idButton);
5146 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
5147 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
5148 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005149 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005151 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 setcursor();
5153 out_flush();
5154 did_menu_tip = TRUE;
5155 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01005156 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 }
5158 break;
5159#endif
5160 case WM_NCHITTEST:
5161 {
5162 LRESULT result;
5163 int x, y;
5164 int xPos = GET_X_LPARAM(lParam);
5165
5166 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
5167 if (result == HTCLIENT)
5168 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005169#ifdef FEAT_GUI_TABLINE
5170 if (gui_mch_showing_tabline())
5171 {
5172 int yPos = GET_Y_LPARAM(lParam);
5173 RECT rct;
5174
5175 /* If the cursor is on the GUI tabline, don't process this
5176 * event */
5177 GetWindowRect(s_textArea, &rct);
5178 if (yPos < rct.top)
5179 return result;
5180 }
5181#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02005182 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 xPos -= x;
5184
5185 if (xPos < 48) /* <VN> TODO should use system metric? */
5186 return HTBOTTOMLEFT;
5187 else
5188 return HTBOTTOMRIGHT;
5189 }
5190 else
5191 return result;
5192 }
5193 /* break; notreached */
5194
5195#ifdef FEAT_MBYTE_IME
5196 case WM_IME_NOTIFY:
5197 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5198 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005199 return 1L;
5200
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 case WM_IME_COMPOSITION:
5202 if (!_OnImeComposition(hwnd, wParam, lParam))
5203 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005204 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205#endif
5206
5207 default:
5208 if (uMsg == msh_msgmousewheel && msh_msgmousewheel != 0)
5209 { /* handle MSH_MOUSEWHEEL messages for Intellimouse */
5210 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01005211 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 }
5213#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005214 else if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 {
5216 _OnFindRepl();
5217 }
5218#endif
5219 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5220 }
5221
Bram Moolenaar2787ab92011-12-14 15:23:59 +01005222 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223}
5224
5225/*
5226 * End of call-back routines
5227 */
5228
5229/* parent window, if specified with -P */
5230HWND vim_parent_hwnd = NULL;
5231
5232 static BOOL CALLBACK
5233FindWindowTitle(HWND hwnd, LPARAM lParam)
5234{
5235 char buf[2048];
5236 char *title = (char *)lParam;
5237
5238 if (GetWindowText(hwnd, buf, sizeof(buf)))
5239 {
5240 if (strstr(buf, title) != NULL)
5241 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005242 /* Found it. Store the window ref. and quit searching if MDI
5243 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005245 if (vim_parent_hwnd != NULL)
5246 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 }
5248 }
5249 return TRUE; /* continue searching */
5250}
5251
5252/*
5253 * Invoked for '-P "title"' argument: search for parent application to open
5254 * our window in.
5255 */
5256 void
5257gui_mch_set_parent(char *title)
5258{
5259 EnumWindows(FindWindowTitle, (LPARAM)title);
5260 if (vim_parent_hwnd == NULL)
5261 {
5262 EMSG2(_("E671: Cannot find window title \"%s\""), title);
5263 mch_exit(2);
5264 }
5265}
5266
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005267#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 static void
5269ole_error(char *arg)
5270{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005271 char buf[IOSIZE];
5272
5273 /* Can't use EMSG() here, we have not finished initialisation yet. */
5274 vim_snprintf(buf, IOSIZE,
5275 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
5276 arg);
5277 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280
5281/*
5282 * Parse the GUI related command-line arguments. Any arguments used are
5283 * deleted from argv, and *argc is decremented accordingly. This is called
5284 * when vim is started, whether or not the GUI has been started.
5285 */
5286 void
5287gui_mch_prepare(int *argc, char **argv)
5288{
5289 int silent = FALSE;
5290 int idx;
5291
5292 /* Check for special OLE command line parameters */
5293 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5294 {
5295 /* Check for a "-silent" argument first. */
5296 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5297 && (argv[2][0] == '-' || argv[2][0] == '/'))
5298 {
5299 silent = TRUE;
5300 idx = 2;
5301 }
5302 else
5303 idx = 1;
5304
5305 /* Register Vim as an OLE Automation server */
5306 if (STRICMP(argv[idx] + 1, "register") == 0)
5307 {
5308#ifdef FEAT_OLE
5309 RegisterMe(silent);
5310 mch_exit(0);
5311#else
5312 if (!silent)
5313 ole_error("register");
5314 mch_exit(2);
5315#endif
5316 }
5317
5318 /* Unregister Vim as an OLE Automation server */
5319 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5320 {
5321#ifdef FEAT_OLE
5322 UnregisterMe(!silent);
5323 mch_exit(0);
5324#else
5325 if (!silent)
5326 ole_error("unregister");
5327 mch_exit(2);
5328#endif
5329 }
5330
5331 /* Ignore an -embedding argument. It is only relevant if the
5332 * application wants to treat the case when it is started manually
5333 * differently from the case where it is started via automation (and
5334 * we don't).
5335 */
5336 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5337 {
5338#ifdef FEAT_OLE
5339 *argc = 1;
5340#else
5341 ole_error("embedding");
5342 mch_exit(2);
5343#endif
5344 }
5345 }
5346
5347#ifdef FEAT_OLE
5348 {
5349 int bDoRestart = FALSE;
5350
5351 InitOLE(&bDoRestart);
5352 /* automatically exit after registering */
5353 if (bDoRestart)
5354 mch_exit(0);
5355 }
5356#endif
5357
5358#ifdef FEAT_NETBEANS_INTG
5359 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005360 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 int arg;
5362
5363 for (arg = 1; arg < *argc; arg++)
5364 if (strncmp("-nb", argv[arg], 3) == 0)
5365 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 netbeansArg = argv[arg];
5367 mch_memmove(&argv[arg], &argv[arg + 1],
5368 (--*argc - arg) * sizeof(char *));
5369 argv[*argc] = NULL;
5370 break; /* enough? */
5371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 }
5373#endif
5374
5375 /* get the OS version info */
5376 os_version.dwOSVersionInfoSize = sizeof(os_version);
5377 GetVersionEx(&os_version); /* this call works on Win32s, Win95 and WinNT */
5378
5379 /* try and load the user32.dll library and get the entry points for
5380 * multi-monitor-support. */
Bram Moolenaarebbcb822010-10-23 14:02:54 +02005381 if ((user32_lib = vimLoadLib("User32.dll")) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 {
5383 pMonitorFromWindow = (TMonitorFromWindow)GetProcAddress(user32_lib,
5384 "MonitorFromWindow");
5385
5386 /* there are ...A and ...W version of GetMonitorInfo - looking at
5387 * winuser.h, they have exactly the same declaration. */
5388 pGetMonitorInfo = (TGetMonitorInfo)GetProcAddress(user32_lib,
5389 "GetMonitorInfoA");
5390 }
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02005391
5392#ifdef FEAT_MBYTE
5393 /* If the OS is Windows NT, use wide functions;
5394 * this enables common dialogs input unicode from IME. */
5395 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
5396 {
5397 pDispatchMessage = DispatchMessageW;
5398 pGetMessage = GetMessageW;
5399 pIsDialogMessage = IsDialogMessageW;
5400 pPeekMessage = PeekMessageW;
5401 }
5402 else
5403 {
5404 pDispatchMessage = DispatchMessageA;
5405 pGetMessage = GetMessageA;
5406 pIsDialogMessage = IsDialogMessageA;
5407 pPeekMessage = PeekMessageA;
5408 }
5409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410}
5411
5412/*
5413 * Initialise the GUI. Create all the windows, set up all the call-backs
5414 * etc.
5415 */
5416 int
5417gui_mch_init(void)
5418{
5419 const char szVimWndClass[] = VIM_CLASS;
5420 const char szTextAreaClass[] = "VimTextArea";
5421 WNDCLASS wndclass;
5422#ifdef FEAT_MBYTE
5423 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005424 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 WNDCLASSW wndclassw;
5426#endif
5427#ifdef GLOBAL_IME
5428 ATOM atom;
5429#endif
5430
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 /* Return here if the window was already opened (happens when
5432 * gui_mch_dialog() is called early). */
5433 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005434 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435
5436 /*
5437 * Load the tearoff bitmap
5438 */
5439#ifdef FEAT_TEAROFF
5440 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
5441#endif
5442
5443 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5444 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5445#ifdef FEAT_MENU
5446 gui.menu_height = 0; /* Windows takes care of this */
5447#endif
5448 gui.border_width = 0;
5449
5450 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5451
5452#ifdef FEAT_MBYTE
5453 /* First try using the wide version, so that we can use any title.
5454 * Otherwise only characters in the active codepage will work. */
5455 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
5456 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005457 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 wndclassw.lpfnWndProc = _WndProc;
5459 wndclassw.cbClsExtra = 0;
5460 wndclassw.cbWndExtra = 0;
5461 wndclassw.hInstance = s_hinst;
5462 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5463 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5464 wndclassw.hbrBackground = s_brush;
5465 wndclassw.lpszMenuName = NULL;
5466 wndclassw.lpszClassName = szVimWndClassW;
5467
5468 if ((
5469#ifdef GLOBAL_IME
5470 atom =
5471#endif
5472 RegisterClassW(&wndclassw)) == 0)
5473 {
5474 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
5475 return FAIL;
5476
5477 /* Must be Windows 98, fall back to non-wide function. */
5478 }
5479 else
5480 wide_WindowProc = TRUE;
5481 }
5482
5483 if (!wide_WindowProc)
5484#endif
5485
5486 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
5487 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005488 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 wndclass.lpfnWndProc = _WndProc;
5490 wndclass.cbClsExtra = 0;
5491 wndclass.cbWndExtra = 0;
5492 wndclass.hInstance = s_hinst;
5493 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
5494 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5495 wndclass.hbrBackground = s_brush;
5496 wndclass.lpszMenuName = NULL;
5497 wndclass.lpszClassName = szVimWndClass;
5498
5499 if ((
5500#ifdef GLOBAL_IME
5501 atom =
5502#endif
5503 RegisterClass(&wndclass)) == 0)
5504 return FAIL;
5505 }
5506
5507 if (vim_parent_hwnd != NULL)
5508 {
5509#ifdef HAVE_TRY_EXCEPT
5510 __try
5511 {
5512#endif
5513 /* Open inside the specified parent window.
5514 * TODO: last argument should point to a CLIENTCREATESTRUCT
5515 * structure. */
5516 s_hwnd = CreateWindowEx(
5517 WS_EX_MDICHILD,
5518 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005519 WS_OVERLAPPEDWINDOW | WS_CHILD
5520 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5522 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5523 100, /* Any value will do */
5524 100, /* Any value will do */
5525 vim_parent_hwnd, NULL,
5526 s_hinst, NULL);
5527#ifdef HAVE_TRY_EXCEPT
5528 }
5529 __except(EXCEPTION_EXECUTE_HANDLER)
5530 {
5531 /* NOP */
5532 }
5533#endif
5534 if (s_hwnd == NULL)
5535 {
5536 EMSG(_("E672: Unable to open window inside MDI application"));
5537 mch_exit(2);
5538 }
5539 }
5540 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005541 {
5542 /* If the provided windowid is not valid reset it to zero, so that it
5543 * is ignored and we open our own window. */
5544 if (IsWindow((HWND)win_socket_id) <= 0)
5545 win_socket_id = 0;
5546
5547 /* Create a window. If win_socket_id is not zero without border and
5548 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00005550 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005551 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5552 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005553 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5554 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5555 100, /* Any value will do */
5556 100, /* Any value will do */
5557 NULL, NULL,
5558 s_hinst, NULL);
5559 if (s_hwnd != NULL && win_socket_id != 0)
5560 {
5561 SetParent(s_hwnd, (HWND)win_socket_id);
5562 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5563 }
5564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565
5566 if (s_hwnd == NULL)
5567 return FAIL;
5568
5569#ifdef GLOBAL_IME
5570 global_ime_init(atom, s_hwnd);
5571#endif
5572#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5573 dyn_imm_load();
5574#endif
5575
5576 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005577#ifdef FEAT_MBYTE
5578 if (wide_WindowProc)
5579 {
5580 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
5581 {
5582 wndclassw.style = CS_OWNDC;
5583 wndclassw.lpfnWndProc = _TextAreaWndProc;
5584 wndclassw.cbClsExtra = 0;
5585 wndclassw.cbWndExtra = 0;
5586 wndclassw.hInstance = s_hinst;
5587 wndclassw.hIcon = NULL;
5588 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5589 wndclassw.hbrBackground = NULL;
5590 wndclassw.lpszMenuName = NULL;
5591 wndclassw.lpszClassName = szTextAreaClassW;
5592
5593 if (RegisterClassW(&wndclassw) == 0)
5594 return FAIL;
5595 }
5596 }
5597 else
5598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
5600 {
5601 wndclass.style = CS_OWNDC;
5602 wndclass.lpfnWndProc = _TextAreaWndProc;
5603 wndclass.cbClsExtra = 0;
5604 wndclass.cbWndExtra = 0;
5605 wndclass.hInstance = s_hinst;
5606 wndclass.hIcon = NULL;
5607 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5608 wndclass.hbrBackground = NULL;
5609 wndclass.lpszMenuName = NULL;
5610 wndclass.lpszClassName = szTextAreaClass;
5611
5612 if (RegisterClass(&wndclass) == 0)
5613 return FAIL;
5614 }
5615 s_textArea = CreateWindowEx(
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005616 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 szTextAreaClass, "Vim text area",
5618 WS_CHILD | WS_VISIBLE, 0, 0,
5619 100, /* Any value will do for now */
5620 100, /* Any value will do for now */
5621 s_hwnd, NULL,
5622 s_hinst, NULL);
5623
5624 if (s_textArea == NULL)
5625 return FAIL;
5626
Bram Moolenaar20321902016-02-17 12:30:17 +01005627#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005628 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5629 {
5630 HANDLE hIcon = NULL;
5631
5632 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005633 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005634 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005635#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005636
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637#ifdef FEAT_MENU
5638 s_menuBar = CreateMenu();
5639#endif
5640 s_hdc = GetDC(s_textArea);
5641
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642#ifdef FEAT_WINDOWS
5643 DragAcceptFiles(s_hwnd, TRUE);
5644#endif
5645
5646 /* Do we need to bother with this? */
5647 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5648
5649 /* Get background/foreground colors from the system */
5650 gui_mch_def_colors();
5651
5652 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5653 * file) */
5654 set_normal_colors();
5655
5656 /*
5657 * Check that none of the colors are the same as the background color.
5658 * Then store the current values as the defaults.
5659 */
5660 gui_check_colors();
5661 gui.def_norm_pixel = gui.norm_pixel;
5662 gui.def_back_pixel = gui.back_pixel;
5663
5664 /* Get the colors for the highlight groups (gui_check_colors() might have
5665 * changed them) */
5666 highlight_gui_started();
5667
5668 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005669 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005671 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672
5673 /*
5674 * Set up for Intellimouse processing
5675 */
5676 init_mouse_wheel();
5677
5678 /*
5679 * compute a couple of metrics used for the dialogs
5680 */
5681 get_dialog_font_metrics();
5682#ifdef FEAT_TOOLBAR
5683 /*
5684 * Create the toolbar
5685 */
5686 initialise_toolbar();
5687#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005688#ifdef FEAT_GUI_TABLINE
5689 /*
5690 * Create the tabline
5691 */
5692 initialise_tabline();
5693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694#ifdef MSWIN_FIND_REPLACE
5695 /*
5696 * Initialise the dialog box stuff
5697 */
5698 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5699
5700 /* Initialise the struct */
5701 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005702 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005704 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5706 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5707 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00005708# if defined(FEAT_MBYTE) && defined(WIN3264)
5709 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
5710 s_findrep_struct_w.lpstrFindWhat =
5711 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5712 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
5713 s_findrep_struct_w.lpstrReplaceWith =
5714 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5715 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
5716 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
5717 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5718# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005721#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005722# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5723/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5724# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005725# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005726# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005727# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005728 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005729 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005730#endif
5731
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005732#ifdef FEAT_RENDER_OPTIONS
5733 if (p_rop)
5734 (void)gui_mch_set_rendering_options(p_rop);
5735#endif
5736
Bram Moolenaar748bf032005-02-02 23:04:36 +00005737theend:
5738 /* Display any pending error messages */
5739 display_errors();
5740
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 return OK;
5742}
5743
5744/*
5745 * Get the size of the screen, taking position on multiple monitors into
5746 * account (if supported).
5747 */
5748 static void
5749get_work_area(RECT *spi_rect)
5750{
5751 _HMONITOR mon;
5752 _MONITORINFO moninfo;
5753
5754 /* use these functions only if available */
5755 if (pMonitorFromWindow != NULL && pGetMonitorInfo != NULL)
5756 {
5757 /* work out which monitor the window is on, and get *it's* work area */
5758 mon = pMonitorFromWindow(s_hwnd, 1 /*MONITOR_DEFAULTTOPRIMARY*/);
5759 if (mon != NULL)
5760 {
5761 moninfo.cbSize = sizeof(_MONITORINFO);
5762 if (pGetMonitorInfo(mon, &moninfo))
5763 {
5764 *spi_rect = moninfo.rcWork;
5765 return;
5766 }
5767 }
5768 }
5769 /* this is the old method... */
5770 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5771}
5772
5773/*
5774 * Set the size of the window to the given width and height in pixels.
5775 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005776/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 void
5778gui_mch_set_shellsize(int width, int height,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005779 int min_width, int min_height, int base_width, int base_height,
5780 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781{
5782 RECT workarea_rect;
5783 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 WINDOWPLACEMENT wndpl;
5785
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005786 /* Try to keep window completely on screen. */
5787 /* Get position of the screen work area. This is the part that is not
5788 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 get_work_area(&workarea_rect);
5790
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005791 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005792 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 wndpl.length = sizeof(WINDOWPLACEMENT);
5794 GetWindowPlacement(s_hwnd, &wndpl);
5795
5796 /* Resizing a maximized window looks very strange, unzoom it first.
5797 * But don't do it when still starting up, it may have been requested in
5798 * the shortcut. */
5799 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5800 {
5801 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5802 /* Need to get the settings of the normal window. */
5803 GetWindowPlacement(s_hwnd, &wndpl);
5804 }
5805
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005807 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005808 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005809 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005810 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 + GetSystemMetrics(SM_CYCAPTION)
5812#ifdef FEAT_MENU
5813 + gui_mswin_get_menu_height(FALSE)
5814#endif
5815 ;
5816
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005817 /* The following should take care of keeping Vim on the same monitor, no
5818 * matter if the secondary monitor is left or right of the primary
5819 * monitor. */
5820 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5821 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005822
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005823 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005824 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005825 && wndpl.rcNormalPosition.right > workarea_rect.right)
5826 OffsetRect(&wndpl.rcNormalPosition,
5827 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005829 if ((direction & RESIZE_HOR)
5830 && wndpl.rcNormalPosition.left < workarea_rect.left)
5831 OffsetRect(&wndpl.rcNormalPosition,
5832 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833
Bram Moolenaarafa24992006-03-27 20:58:26 +00005834 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005835 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5836 OffsetRect(&wndpl.rcNormalPosition,
5837 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005839 if ((direction & RESIZE_VERT)
5840 && wndpl.rcNormalPosition.top < workarea_rect.top)
5841 OffsetRect(&wndpl.rcNormalPosition,
5842 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843
5844 /* set window position - we should use SetWindowPlacement rather than
5845 * SetWindowPos as the MSDN docs say the coord systems returned by
5846 * these two are not compatible. */
5847 SetWindowPlacement(s_hwnd, &wndpl);
5848
5849 SetActiveWindow(s_hwnd);
5850 SetFocus(s_hwnd);
5851
5852#ifdef FEAT_MENU
5853 /* Menu may wrap differently now */
5854 gui_mswin_get_menu_height(!gui.starting);
5855#endif
5856}
5857
5858
5859 void
5860gui_mch_set_scrollbar_thumb(
5861 scrollbar_T *sb,
5862 long val,
5863 long size,
5864 long max)
5865{
5866 SCROLLINFO info;
5867
5868 sb->scroll_shift = 0;
5869 while (max > 32767)
5870 {
5871 max = (max + 1) >> 1;
5872 val >>= 1;
5873 size >>= 1;
5874 ++sb->scroll_shift;
5875 }
5876
5877 if (sb->scroll_shift > 0)
5878 ++size;
5879
5880 info.cbSize = sizeof(info);
5881 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5882 info.nPos = val;
5883 info.nMin = 0;
5884 info.nMax = max;
5885 info.nPage = size;
5886 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5887}
5888
5889
5890/*
5891 * Set the current text font.
5892 */
5893 void
5894gui_mch_set_font(GuiFont font)
5895{
5896 gui.currFont = font;
5897}
5898
5899
5900/*
5901 * Set the current text foreground color.
5902 */
5903 void
5904gui_mch_set_fg_color(guicolor_T color)
5905{
5906 gui.currFgColor = color;
5907}
5908
5909/*
5910 * Set the current text background color.
5911 */
5912 void
5913gui_mch_set_bg_color(guicolor_T color)
5914{
5915 gui.currBgColor = color;
5916}
5917
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005918/*
5919 * Set the current text special color.
5920 */
5921 void
5922gui_mch_set_sp_color(guicolor_T color)
5923{
5924 gui.currSpColor = color;
5925}
5926
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927#if defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)
5928/*
5929 * Multi-byte handling, originally by Sung-Hoon Baek.
5930 * First static functions (no prototypes generated).
5931 */
5932#ifdef _MSC_VER
5933# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5934#endif
5935#include <imm.h>
5936
5937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 * handle WM_IME_NOTIFY message
5939 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005940/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 static LRESULT
5942_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData)
5943{
5944 LRESULT lResult = 0;
5945 HIMC hImc;
5946
5947 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5948 return lResult;
5949 switch (dwCommand)
5950 {
5951 case IMN_SETOPENSTATUS:
5952 if (pImmGetOpenStatus(hImc))
5953 {
5954 pImmSetCompositionFont(hImc, &norm_logfont);
5955 im_set_position(gui.row, gui.col);
5956
5957 /* Disable langmap */
5958 State &= ~LANGMAP;
5959 if (State & INSERT)
5960 {
5961#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
5962 /* Unshown 'keymap' in status lines */
5963 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5964 {
5965 /* Save cursor position */
5966 int old_row = gui.row;
5967 int old_col = gui.col;
5968
5969 // This must be called here before
5970 // status_redraw_curbuf(), otherwise the mode
5971 // message may appear in the wrong position.
5972 showmode();
5973 status_redraw_curbuf();
5974 update_screen(0);
5975 /* Restore cursor position */
5976 gui.row = old_row;
5977 gui.col = old_col;
5978 }
5979#endif
5980 }
5981 }
5982 gui_update_cursor(TRUE, FALSE);
5983 lResult = 0;
5984 break;
5985 }
5986 pImmReleaseContext(hWnd, hImc);
5987 return lResult;
5988}
5989
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005990/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 static LRESULT
5992_OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param)
5993{
5994 char_u *ret;
5995 int len;
5996
5997 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5998 return 0;
5999
6000 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
6001 if (ret != NULL)
6002 {
6003 add_to_input_buf_csi(ret, len);
6004 vim_free(ret);
6005 return 1;
6006 }
6007 return 0;
6008}
6009
6010/*
6011 * get the current composition string, in UCS-2; *lenp is the number of
6012 * *lenp is the number of Unicode characters.
6013 */
6014 static short_u *
6015GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
6016{
6017 LONG ret;
6018 LPWSTR wbuf = NULL;
6019 char_u *buf;
6020
6021 if (!pImmGetContext)
6022 return NULL; /* no imm32.dll */
6023
6024 /* Try Unicode; this'll always work on NT regardless of codepage. */
6025 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
6026 if (ret == 0)
6027 return NULL; /* empty */
6028
6029 if (ret > 0)
6030 {
6031 /* Allocate the requested buffer plus space for the NUL character. */
6032 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
6033 if (wbuf != NULL)
6034 {
6035 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
6036 *lenp = ret / sizeof(WCHAR);
6037 }
6038 return (short_u *)wbuf;
6039 }
6040
6041 /* ret < 0; we got an error, so try the ANSI version. This'll work
6042 * on 9x/ME, but only if the codepage happens to be set to whatever
6043 * we're inputting. */
6044 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
6045 if (ret <= 0)
6046 return NULL; /* empty or error */
6047
6048 buf = alloc(ret);
6049 if (buf == NULL)
6050 return NULL;
6051 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
6052
6053 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006054 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 vim_free(buf);
6056
6057 return (short_u *)wbuf;
6058}
6059
6060/*
6061 * void GetResultStr()
6062 *
6063 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
6064 * get complete composition string
6065 */
6066 static char_u *
6067GetResultStr(HWND hwnd, int GCS, int *lenp)
6068{
6069 HIMC hIMC; /* Input context handle. */
6070 short_u *buf = NULL;
6071 char_u *convbuf = NULL;
6072
6073 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
6074 return NULL;
6075
6076 /* Reads in the composition string. */
6077 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
6078 if (buf == NULL)
6079 return NULL;
6080
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006081 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 pImmReleaseContext(hwnd, hIMC);
6083 vim_free(buf);
6084 return convbuf;
6085}
6086#endif
6087
6088/* For global functions we need prototypes. */
6089#if (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) || defined(PROTO)
6090
6091/*
6092 * set font to IM.
6093 */
6094 void
6095im_set_font(LOGFONT *lf)
6096{
6097 HIMC hImc;
6098
6099 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6100 {
6101 pImmSetCompositionFont(hImc, lf);
6102 pImmReleaseContext(s_hwnd, hImc);
6103 }
6104}
6105
6106/*
6107 * Notify cursor position to IM.
6108 */
6109 void
6110im_set_position(int row, int col)
6111{
6112 HIMC hImc;
6113
6114 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6115 {
6116 COMPOSITIONFORM cfs;
6117
6118 cfs.dwStyle = CFS_POINT;
6119 cfs.ptCurrentPos.x = FILL_X(col);
6120 cfs.ptCurrentPos.y = FILL_Y(row);
6121 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
6122 pImmSetCompositionWindow(hImc, &cfs);
6123
6124 pImmReleaseContext(s_hwnd, hImc);
6125 }
6126}
6127
6128/*
6129 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6130 */
6131 void
6132im_set_active(int active)
6133{
6134 HIMC hImc;
6135 static HIMC hImcOld = (HIMC)0;
6136
6137 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
6138 {
6139 if (p_imdisable)
6140 {
6141 if (hImcOld == (HIMC)0)
6142 {
6143 hImcOld = pImmGetContext(s_hwnd);
6144 if (hImcOld)
6145 pImmAssociateContext(s_hwnd, (HIMC)0);
6146 }
6147 active = FALSE;
6148 }
6149 else if (hImcOld != (HIMC)0)
6150 {
6151 pImmAssociateContext(s_hwnd, hImcOld);
6152 hImcOld = (HIMC)0;
6153 }
6154
6155 hImc = pImmGetContext(s_hwnd);
6156 if (hImc)
6157 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006158 /*
6159 * for Korean ime
6160 */
6161 HKL hKL = GetKeyboardLayout(0);
6162
6163 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6164 {
6165 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6166 static BOOL bSaved = FALSE;
6167
6168 if (active)
6169 {
6170 /* if we have a saved conversion status, restore it */
6171 if (bSaved)
6172 pImmSetConversionStatus(hImc, dwConversionSaved,
6173 dwSentenceSaved);
6174 bSaved = FALSE;
6175 }
6176 else
6177 {
6178 /* save conversion status and disable korean */
6179 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6180 &dwSentenceSaved))
6181 {
6182 bSaved = TRUE;
6183 pImmSetConversionStatus(hImc,
6184 dwConversionSaved & ~(IME_CMODE_NATIVE
6185 | IME_CMODE_FULLSHAPE),
6186 dwSentenceSaved);
6187 }
6188 }
6189 }
6190
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 pImmSetOpenStatus(hImc, active);
6192 pImmReleaseContext(s_hwnd, hImc);
6193 }
6194 }
6195}
6196
6197/*
6198 * Get IM status. When IM is on, return not 0. Else return 0.
6199 */
6200 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006201im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202{
6203 int status = 0;
6204 HIMC hImc;
6205
6206 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6207 {
6208 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6209 pImmReleaseContext(s_hwnd, hImc);
6210 }
6211 return status;
6212}
6213
6214#endif /* FEAT_MBYTE && FEAT_MBYTE_IME */
6215
6216#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
6217/* Win32 with GLOBAL IME */
6218
6219/*
6220 * Notify cursor position to IM.
6221 */
6222 void
6223im_set_position(int row, int col)
6224{
6225 /* Win32 with GLOBAL IME */
6226 POINT p;
6227
6228 p.x = FILL_X(col);
6229 p.y = FILL_Y(row);
6230 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
6231 global_ime_set_position(&p);
6232}
6233
6234/*
6235 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6236 */
6237 void
6238im_set_active(int active)
6239{
6240 global_ime_set_status(active);
6241}
6242
6243/*
6244 * Get IM status. When IM is on, return not 0. Else return 0.
6245 */
6246 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006247im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248{
6249 return global_ime_get_status();
6250}
6251#endif
6252
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006253#ifdef FEAT_MBYTE
6254/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006255 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006256 */
6257 static void
6258latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6259{
6260 int c;
6261
Bram Moolenaarca003e12006-03-17 23:19:38 +00006262 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006263 {
6264 c = *text++;
6265 switch (c)
6266 {
6267 case 0xa4: c = 0x20ac; break; /* euro */
6268 case 0xa6: c = 0x0160; break; /* S hat */
6269 case 0xa8: c = 0x0161; break; /* S -hat */
6270 case 0xb4: c = 0x017d; break; /* Z hat */
6271 case 0xb8: c = 0x017e; break; /* Z -hat */
6272 case 0xbc: c = 0x0152; break; /* OE */
6273 case 0xbd: c = 0x0153; break; /* oe */
6274 case 0xbe: c = 0x0178; break; /* Y */
6275 }
6276 *unicodebuf++ = c;
6277 }
6278}
6279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280
6281#ifdef FEAT_RIGHTLEFT
6282/*
6283 * What is this for? In the case where you are using Win98 or Win2K or later,
6284 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6285 * reverses the string sent to the TextOut... family. This sucks, because we
6286 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6287 * way to tell Windblows not to do this!
6288 *
6289 * The short of it is that this 'RevOut' only gets called if you are running
6290 * one of the new, "improved" MS OSes, and only if you are running in
6291 * 'rightleft' mode. It makes display take *slightly* longer, but not
6292 * noticeably so.
6293 */
6294 static void
6295RevOut( HDC s_hdc,
6296 int col,
6297 int row,
6298 UINT foptions,
6299 CONST RECT *pcliprect,
6300 LPCTSTR text,
6301 UINT len,
6302 CONST INT *padding)
6303{
6304 int ix;
6305 static int special = -1;
6306
6307 if (special == -1)
6308 {
6309 /* Check windows version: special treatment is needed if it is NT 5 or
6310 * Win98 or higher. */
6311 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
6312 && os_version.dwMajorVersion >= 5)
6313 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
6314 && (os_version.dwMajorVersion > 4
6315 || (os_version.dwMajorVersion == 4
6316 && os_version.dwMinorVersion > 0))))
6317 special = 1;
6318 else
6319 special = 0;
6320 }
6321
6322 if (special)
6323 for (ix = 0; ix < (int)len; ++ix)
6324 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6325 pcliprect, text + ix, 1, padding);
6326 else
6327 ExtTextOut(s_hdc, col, row, foptions, pcliprect, text, len, padding);
6328}
6329#endif
6330
6331 void
6332gui_mch_draw_string(
6333 int row,
6334 int col,
6335 char_u *text,
6336 int len,
6337 int flags)
6338{
6339 static int *padding = NULL;
6340 static int pad_size = 0;
6341 int i;
6342 const RECT *pcliprect = NULL;
6343 UINT foptions = 0;
6344#ifdef FEAT_MBYTE
6345 static WCHAR *unicodebuf = NULL;
6346 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006347 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 int n = 0;
6349#endif
6350 HPEN hpen, old_pen;
6351 int y;
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006352#ifdef FEAT_DIRECTX
6353 int font_is_ttf_or_vector = 0;
6354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 /*
6357 * Italic and bold text seems to have an extra row of pixels at the bottom
6358 * (below where the bottom of the character should be). If we draw the
6359 * characters with a solid background, the top row of pixels in the
6360 * character below will be overwritten. We can fix this by filling in the
6361 * background ourselves, to the correct character proportions, and then
6362 * writing the character in transparent mode. Still have a problem when
6363 * the character is "_", which gets written on to the character below.
6364 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6365 * pixel in their slots, which fixes the problem with the bottom row of
6366 * pixels. We still need this code because otherwise the top row of pixels
6367 * becomes a problem. - webb.
6368 */
6369 static HBRUSH hbr_cache[2] = {NULL, NULL};
6370 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6371 static int brush_lru = 0;
6372 HBRUSH hbr;
6373 RECT rc;
6374
6375 if (!(flags & DRAW_TRANSP))
6376 {
6377 /*
6378 * Clear background first.
6379 * Note: FillRect() excludes right and bottom of rectangle.
6380 */
6381 rc.left = FILL_X(col);
6382 rc.top = FILL_Y(row);
6383#ifdef FEAT_MBYTE
6384 if (has_mbyte)
6385 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006387 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 }
6389 else
6390#endif
6391 rc.right = FILL_X(col + len);
6392 rc.bottom = FILL_Y(row + 1);
6393
6394 /* Cache the created brush, that saves a lot of time. We need two:
6395 * one for cursor background and one for the normal background. */
6396 if (gui.currBgColor == brush_color[0])
6397 {
6398 hbr = hbr_cache[0];
6399 brush_lru = 1;
6400 }
6401 else if (gui.currBgColor == brush_color[1])
6402 {
6403 hbr = hbr_cache[1];
6404 brush_lru = 0;
6405 }
6406 else
6407 {
6408 if (hbr_cache[brush_lru] != NULL)
6409 DeleteBrush(hbr_cache[brush_lru]);
6410 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6411 brush_color[brush_lru] = gui.currBgColor;
6412 hbr = hbr_cache[brush_lru];
6413 brush_lru = !brush_lru;
6414 }
6415 FillRect(s_hdc, &rc, hbr);
6416
6417 SetBkMode(s_hdc, TRANSPARENT);
6418
6419 /*
6420 * When drawing block cursor, prevent inverted character spilling
6421 * over character cell (can happen with bold/italic)
6422 */
6423 if (flags & DRAW_CURSOR)
6424 {
6425 pcliprect = &rc;
6426 foptions = ETO_CLIPPED;
6427 }
6428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 SetTextColor(s_hdc, gui.currFgColor);
6430 SelectFont(s_hdc, gui.currFont);
6431
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006432#ifdef FEAT_DIRECTX
6433 if (IS_ENABLE_DIRECTX())
6434 {
6435 TEXTMETRIC tm;
6436
6437 GetTextMetrics(s_hdc, &tm);
6438 if (tm.tmPitchAndFamily & (TMPF_TRUETYPE | TMPF_VECTOR))
6439 {
6440 font_is_ttf_or_vector = 1;
6441 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
6442 }
6443 }
6444#endif
6445
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6447 {
6448 vim_free(padding);
6449 pad_size = Columns;
6450
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006451 /* Don't give an out-of-memory message here, it would call us
6452 * recursively. */
6453 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 if (padding != NULL)
6455 for (i = 0; i < pad_size; i++)
6456 padding[i] = gui.char_width;
6457 }
6458
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 /*
6460 * We have to provide the padding argument because italic and bold versions
6461 * of fixed-width fonts are often one pixel or so wider than their normal
6462 * versions.
6463 * No check for DRAW_BOLD, Windows will have done it already.
6464 */
6465
6466#ifdef FEAT_MBYTE
6467 /* Check if there are any UTF-8 characters. If not, use normal text
6468 * output to speed up output. */
6469 if (enc_utf8)
6470 for (n = 0; n < len; ++n)
6471 if (text[n] >= 0x80)
6472 break;
6473
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006474#if defined(FEAT_DIRECTX)
6475 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6476 * required that unicode drawing routine, currently. So this forces it
6477 * enabled. */
6478 if (enc_utf8 && IS_ENABLE_DIRECTX())
6479 n = 0; /* Keep n < len, to enter block for unicode. */
6480#endif
6481
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006483 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006485 if ((enc_utf8
6486 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6487 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 && (unicodebuf == NULL || len > unibuflen))
6489 {
6490 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006491 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492
6493 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006494 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495
6496 unibuflen = len;
6497 }
6498
6499 if (enc_utf8 && n < len && unicodebuf != NULL)
6500 {
6501 /* Output UTF-8 characters. Caller has already separated
6502 * composing characters. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006503 int i;
6504 int wlen; /* string length in words */
6505 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 int cells; /* cell width of string up to composing char */
6507 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006508 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006510 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006511 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006513 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006515 c = utf_ptr2char(text + i);
6516 if (c >= 0x10000)
6517 {
6518 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006519 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6520 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006521 }
6522 else
6523 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006524 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006525 }
6526 cw = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 if (cw > 2) /* don't use 4 for unprintable char */
6528 cw = 1;
6529 if (unicodepdy != NULL)
6530 {
6531 /* Use unicodepdy to make characters fit as we expect, even
6532 * when the font uses different widths (e.g., bold character
6533 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006534 if (c >= 0x10000)
6535 {
6536 unicodepdy[wlen - 2] = cw * gui.char_width;
6537 unicodepdy[wlen - 1] = 0;
6538 }
6539 else
6540 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 }
6542 cells += cw;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006543 i += utfc_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006544 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006546#if defined(FEAT_DIRECTX)
6547 if (IS_ENABLE_DIRECTX() && font_is_ttf_or_vector)
6548 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006549 /* Add one to "cells" for italics. */
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006550 DWriteContext_DrawText(s_dwc, s_hdc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006551 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006552 gui.char_width, gui.currFgColor);
6553 }
6554 else
6555#endif
6556 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6557 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 len = cells; /* used for underlining */
6559 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006560 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 {
6562 /* If we want to display codepage data, and the current CP is not the
6563 * ANSI one, we need to go via Unicode. */
6564 if (unicodebuf != NULL)
6565 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006566 if (enc_latin9)
6567 latin9_to_ucs(text, len, unicodebuf);
6568 else
6569 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 MB_PRECOMPOSED,
6571 (char *)text, len,
6572 (LPWSTR)unicodebuf, unibuflen);
6573 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006574 {
6575 /* Use unicodepdy to make characters fit as we expect, even
6576 * when the font uses different widths (e.g., bold character
6577 * is wider). */
6578 if (unicodepdy != NULL)
6579 {
6580 int i;
6581 int cw;
6582
6583 for (i = 0; i < len; ++i)
6584 {
6585 cw = utf_char2cells(unicodebuf[i]);
6586 if (cw > 2)
6587 cw = 1;
6588 unicodepdy[i] = cw * gui.char_width;
6589 }
6590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006592 foptions, pcliprect, unicodebuf, len, unicodepdy);
6593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 }
6595 }
6596 else
6597#endif
6598 {
6599#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006600 /* Windows will mess up RL text, so we have to draw it character by
6601 * character. Only do this if RL is on, since it's slow. */
6602 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6604 foptions, pcliprect, (char *)text, len, padding);
6605 else
6606#endif
6607 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6608 foptions, pcliprect, (char *)text, len, padding);
6609 }
6610
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006611 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 if (flags & DRAW_UNDERL)
6613 {
6614 hpen = CreatePen(PS_SOLID, 1, gui.currFgColor);
6615 old_pen = SelectObject(s_hdc, hpen);
6616 /* When p_linespace is 0, overwrite the bottom row of pixels.
6617 * Otherwise put the line just below the character. */
6618 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 if (p_linespace > 1)
6620 y -= p_linespace - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 MoveToEx(s_hdc, FILL_X(col), y, NULL);
6622 /* Note: LineTo() excludes the last pixel in the line. */
6623 LineTo(s_hdc, FILL_X(col + len), y);
6624 DeleteObject(SelectObject(s_hdc, old_pen));
6625 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006626
6627 /* Undercurl */
6628 if (flags & DRAW_UNDERC)
6629 {
6630 int x;
6631 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006632 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006633
6634 y = FILL_Y(row + 1) - 1;
6635 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6636 {
6637 offset = val[x % 8];
6638 SetPixel(s_hdc, x, y - offset, gui.currSpColor);
6639 }
6640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641}
6642
6643
6644/*
6645 * Output routines.
6646 */
6647
6648/* Flush any output to the screen */
6649 void
6650gui_mch_flush(void)
6651{
6652# if defined(__BORLANDC__)
6653 /*
6654 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6655 * prototype declaration.
6656 * The compiler complains if __stdcall is not used in both declarations.
6657 */
6658 BOOL __stdcall GdiFlush(void);
6659# endif
6660
6661 GdiFlush();
6662}
6663
6664 static void
6665clear_rect(RECT *rcp)
6666{
6667 HBRUSH hbr;
6668
6669 hbr = CreateSolidBrush(gui.back_pixel);
6670 FillRect(s_hdc, rcp, hbr);
6671 DeleteBrush(hbr);
6672}
6673
6674
Bram Moolenaarc716c302006-01-21 22:12:51 +00006675 void
6676gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6677{
6678 RECT workarea_rect;
6679
6680 get_work_area(&workarea_rect);
6681
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006682 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006683 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006684 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006685
6686 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6687 * the menubar for MSwin, we subtract it from the screen height, so that
6688 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006689 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006690 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006691 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006692 - GetSystemMetrics(SM_CYCAPTION)
6693#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006694 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006695#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006696 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006697}
6698
6699
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700#if defined(FEAT_MENU) || defined(PROTO)
6701/*
6702 * Add a sub menu to the menu bar.
6703 */
6704 void
6705gui_mch_add_menu(
6706 vimmenu_T *menu,
6707 int pos)
6708{
6709 vimmenu_T *parent = menu->parent;
6710
6711 menu->submenu_id = CreatePopupMenu();
6712 menu->id = s_menu_id++;
6713
6714 if (menu_is_menubar(menu->name))
6715 {
6716 if (is_winnt_3())
6717 {
6718 InsertMenu((parent == NULL) ? s_menuBar : parent->submenu_id,
6719 (UINT)pos, MF_POPUP | MF_STRING | MF_BYPOSITION,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006720 (long_u)menu->submenu_id, (LPCTSTR) menu->name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 }
6722 else
6723 {
6724#ifdef FEAT_MBYTE
6725 WCHAR *wn = NULL;
6726 int n;
6727
6728 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6729 {
6730 /* 'encoding' differs from active codepage: convert menu name
6731 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006732 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 if (wn != NULL)
6734 {
6735 MENUITEMINFOW infow;
6736
6737 infow.cbSize = sizeof(infow);
6738 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6739 | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006740 infow.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 infow.wID = menu->id;
6742 infow.fType = MFT_STRING;
6743 infow.dwTypeData = wn;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006744 infow.cch = (UINT)wcslen(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 infow.hSubMenu = menu->submenu_id;
6746 n = InsertMenuItemW((parent == NULL)
6747 ? s_menuBar : parent->submenu_id,
6748 (UINT)pos, TRUE, &infow);
6749 vim_free(wn);
6750 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
6751 /* Failed, try using non-wide function. */
6752 wn = NULL;
6753 }
6754 }
6755
6756 if (wn == NULL)
6757#endif
6758 {
6759 MENUITEMINFO info;
6760
6761 info.cbSize = sizeof(info);
6762 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006763 info.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 info.wID = menu->id;
6765 info.fType = MFT_STRING;
6766 info.dwTypeData = (LPTSTR)menu->name;
6767 info.cch = (UINT)STRLEN(menu->name);
6768 info.hSubMenu = menu->submenu_id;
6769 InsertMenuItem((parent == NULL)
6770 ? s_menuBar : parent->submenu_id,
6771 (UINT)pos, TRUE, &info);
6772 }
6773 }
6774 }
6775
6776 /* Fix window size if menu may have wrapped */
6777 if (parent == NULL)
6778 gui_mswin_get_menu_height(!gui.starting);
6779#ifdef FEAT_TEAROFF
6780 else if (IsWindow(parent->tearoff_handle))
6781 rebuild_tearoff(parent);
6782#endif
6783}
6784
6785 void
6786gui_mch_show_popupmenu(vimmenu_T *menu)
6787{
6788 POINT mp;
6789
6790 (void)GetCursorPos((LPPOINT)&mp);
6791 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6792}
6793
6794 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006795gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796{
6797 vimmenu_T *menu = gui_find_menu(path_name);
6798
6799 if (menu != NULL)
6800 {
6801 POINT p;
6802
6803 /* Find the position of the current cursor */
6804 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006805 if (mouse_pos)
6806 {
6807 int mx, my;
6808
6809 gui_mch_getmouse(&mx, &my);
6810 p.x += mx;
6811 p.y += my;
6812 }
6813 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 {
6815 p.x += TEXT_X(W_WINCOL(curwin) + curwin->w_wcol + 1);
6816 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6817 }
6818 msg_scroll = FALSE;
6819 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6820 }
6821}
6822
6823#if defined(FEAT_TEAROFF) || defined(PROTO)
6824/*
6825 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6826 * create it as a pseudo-"tearoff menu".
6827 */
6828 void
6829gui_make_tearoff(char_u *path_name)
6830{
6831 vimmenu_T *menu = gui_find_menu(path_name);
6832
6833 /* Found the menu, so tear it off. */
6834 if (menu != NULL)
6835 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6836}
6837#endif
6838
6839/*
6840 * Add a menu item to a menu
6841 */
6842 void
6843gui_mch_add_menu_item(
6844 vimmenu_T *menu,
6845 int idx)
6846{
6847 vimmenu_T *parent = menu->parent;
6848
6849 menu->id = s_menu_id++;
6850 menu->submenu_id = NULL;
6851
6852#ifdef FEAT_TEAROFF
6853 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6854 {
6855 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6856 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6857 }
6858 else
6859#endif
6860#ifdef FEAT_TOOLBAR
6861 if (menu_is_toolbar(parent->name))
6862 {
6863 TBBUTTON newtb;
6864
6865 vim_memset(&newtb, 0, sizeof(newtb));
6866 if (menu_is_separator(menu->name))
6867 {
6868 newtb.iBitmap = 0;
6869 newtb.fsStyle = TBSTYLE_SEP;
6870 }
6871 else
6872 {
6873 newtb.iBitmap = get_toolbar_bitmap(menu);
6874 newtb.fsStyle = TBSTYLE_BUTTON;
6875 }
6876 newtb.idCommand = menu->id;
6877 newtb.fsState = TBSTATE_ENABLED;
6878 newtb.iString = 0;
6879 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6880 (LPARAM)&newtb);
6881 menu->submenu_id = (HMENU)-1;
6882 }
6883 else
6884#endif
6885 {
6886#ifdef FEAT_MBYTE
6887 WCHAR *wn = NULL;
6888 int n;
6889
6890 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6891 {
6892 /* 'encoding' differs from active codepage: convert menu item name
6893 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006894 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 if (wn != NULL)
6896 {
6897 n = InsertMenuW(parent->submenu_id, (UINT)idx,
6898 (menu_is_separator(menu->name)
6899 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6900 (UINT)menu->id, wn);
6901 vim_free(wn);
6902 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
6903 /* Failed, try using non-wide function. */
6904 wn = NULL;
6905 }
6906 }
6907 if (wn == NULL)
6908#endif
6909 InsertMenu(parent->submenu_id, (UINT)idx,
6910 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
6911 | MF_BYPOSITION,
6912 (UINT)menu->id, (LPCTSTR)menu->name);
6913#ifdef FEAT_TEAROFF
6914 if (IsWindow(parent->tearoff_handle))
6915 rebuild_tearoff(parent);
6916#endif
6917 }
6918}
6919
6920/*
6921 * Destroy the machine specific menu widget.
6922 */
6923 void
6924gui_mch_destroy_menu(vimmenu_T *menu)
6925{
6926#ifdef FEAT_TOOLBAR
6927 /*
6928 * is this a toolbar button?
6929 */
6930 if (menu->submenu_id == (HMENU)-1)
6931 {
6932 int iButton;
6933
6934 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6935 (WPARAM)menu->id, 0);
6936 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6937 }
6938 else
6939#endif
6940 {
6941 if (menu->parent != NULL
6942 && menu_is_popup(menu->parent->dname)
6943 && menu->parent->submenu_id != NULL)
6944 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6945 else
6946 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6947 if (menu->submenu_id != NULL)
6948 DestroyMenu(menu->submenu_id);
6949#ifdef FEAT_TEAROFF
6950 if (IsWindow(menu->tearoff_handle))
6951 DestroyWindow(menu->tearoff_handle);
6952 if (menu->parent != NULL
6953 && menu->parent->children != NULL
6954 && IsWindow(menu->parent->tearoff_handle))
6955 {
6956 /* This menu must not show up when rebuilding the tearoff window. */
6957 menu->modes = 0;
6958 rebuild_tearoff(menu->parent);
6959 }
6960#endif
6961 }
6962}
6963
6964#ifdef FEAT_TEAROFF
6965 static void
6966rebuild_tearoff(vimmenu_T *menu)
6967{
6968 /*hackish*/
6969 char_u tbuf[128];
6970 RECT trect;
6971 RECT rct;
6972 RECT roct;
6973 int x, y;
6974
6975 HWND thwnd = menu->tearoff_handle;
6976
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006977 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 if (GetWindowRect(thwnd, &trect)
6979 && GetWindowRect(s_hwnd, &rct)
6980 && GetClientRect(s_hwnd, &roct))
6981 {
6982 x = trect.left - rct.left;
6983 y = (trect.top - rct.bottom + roct.bottom);
6984 }
6985 else
6986 {
6987 x = y = 0xffffL;
6988 }
6989 DestroyWindow(thwnd);
6990 if (menu->children != NULL)
6991 {
6992 gui_mch_tearoff(tbuf, menu, x, y);
6993 if (IsWindow(menu->tearoff_handle))
6994 (void) SetWindowPos(menu->tearoff_handle,
6995 NULL,
6996 (int)trect.left,
6997 (int)trect.top,
6998 0, 0,
6999 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
7000 }
7001}
7002#endif /* FEAT_TEAROFF */
7003
7004/*
7005 * Make a menu either grey or not grey.
7006 */
7007 void
7008gui_mch_menu_grey(
7009 vimmenu_T *menu,
7010 int grey)
7011{
7012#ifdef FEAT_TOOLBAR
7013 /*
7014 * is this a toolbar button?
7015 */
7016 if (menu->submenu_id == (HMENU)-1)
7017 {
7018 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
7019 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
7020 }
7021 else
7022#endif
7023 if (grey)
7024 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_GRAYED);
7025 else
7026 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
7027
7028#ifdef FEAT_TEAROFF
7029 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
7030 {
7031 WORD menuID;
7032 HWND menuHandle;
7033
7034 /*
7035 * A tearoff button has changed state.
7036 */
7037 if (menu->children == NULL)
7038 menuID = (WORD)(menu->id);
7039 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007040 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
7042 if (menuHandle)
7043 EnableWindow(menuHandle, !grey);
7044
7045 }
7046#endif
7047}
7048
7049#endif /* FEAT_MENU */
7050
7051
7052/* define some macros used to make the dialogue creation more readable */
7053
7054#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
7055#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007056#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057
7058#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
7059/*
7060 * stuff for dialogs
7061 */
7062
7063/*
7064 * The callback routine used by all the dialogs. Very simple. First,
7065 * acknowledges the INITDIALOG message so that Windows knows to do standard
7066 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
7067 * pressed, return that button's ID - IDCANCEL (2), which is the button's
7068 * number.
7069 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007070/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 static LRESULT CALLBACK
7072dialog_callback(
7073 HWND hwnd,
7074 UINT message,
7075 WPARAM wParam,
7076 LPARAM lParam)
7077{
7078 if (message == WM_INITDIALOG)
7079 {
7080 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
7081 /* Set focus to the dialog. Set the default button, if specified. */
7082 (void)SetFocus(hwnd);
7083 if (dialog_default_button > IDCANCEL)
7084 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007085 else
7086 /* We don't have a default, set focus on another element of the
7087 * dialog window, probably the icon */
7088 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 return FALSE;
7090 }
7091
7092 if (message == WM_COMMAND)
7093 {
7094 int button = LOWORD(wParam);
7095
7096 /* Don't end the dialog if something was selected that was
7097 * not a button.
7098 */
7099 if (button >= DLG_NONBUTTON_CONTROL)
7100 return TRUE;
7101
7102 /* If the edit box exists, copy the string. */
7103 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007104 {
7105# if defined(FEAT_MBYTE) && defined(WIN3264)
7106 /* If the OS is Windows NT, and 'encoding' differs from active
7107 * codepage: use wide function and convert text. */
7108 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
7109 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007110 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007111 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
7112 char_u *p;
7113
7114 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
7115 p = utf16_to_enc(wp, NULL);
7116 vim_strncpy(s_textfield, p, IOSIZE);
7117 vim_free(p);
7118 vim_free(wp);
7119 }
7120 else
7121# endif
7122 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007123 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125
7126 /*
7127 * Need to check for IDOK because if the user just hits Return to
7128 * accept the default value, some reason this is what we get.
7129 */
7130 if (button == IDOK)
7131 {
7132 if (dialog_default_button > IDCANCEL)
7133 EndDialog(hwnd, dialog_default_button);
7134 }
7135 else
7136 EndDialog(hwnd, button - IDCANCEL);
7137 return TRUE;
7138 }
7139
7140 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7141 {
7142 EndDialog(hwnd, 0);
7143 return TRUE;
7144 }
7145 return FALSE;
7146}
7147
7148/*
7149 * Create a dialog dynamically from the parameter strings.
7150 * type = type of dialog (question, alert, etc.)
7151 * title = dialog title. may be NULL for default title.
7152 * message = text to display. Dialog sizes to accommodate it.
7153 * buttons = '\n' separated list of button captions, default first.
7154 * dfltbutton = number of default button.
7155 *
7156 * This routine returns 1 if the first button is pressed,
7157 * 2 for the second, etc.
7158 *
7159 * 0 indicates Esc was pressed.
7160 * -1 for unexpected error
7161 *
7162 * If stubbing out this fn, return 1.
7163 */
7164
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007165static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166{
7167 "IDR_VIM",
7168 "IDR_VIM_ERROR",
7169 "IDR_VIM_ALERT",
7170 "IDR_VIM_INFO",
7171 "IDR_VIM_QUESTION"
7172};
7173
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 int
7175gui_mch_dialog(
7176 int type,
7177 char_u *title,
7178 char_u *message,
7179 char_u *buttons,
7180 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007181 char_u *textfield,
7182 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183{
7184 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007185 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 int numButtons;
7187 int *buttonWidths, *buttonPositions;
7188 int buttonYpos;
7189 int nchar, i;
7190 DWORD lStyle;
7191 int dlgwidth = 0;
7192 int dlgheight;
7193 int editboxheight;
7194 int horizWidth = 0;
7195 int msgheight;
7196 char_u *pstart;
7197 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007198 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 char_u *tbuffer;
7200 RECT rect;
7201 HWND hwnd;
7202 HDC hdc;
7203 HFONT font, oldFont;
7204 TEXTMETRIC fontInfo;
7205 int fontHeight;
7206 int textWidth, minButtonWidth, messageWidth;
7207 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007208 int maxDialogHeight;
7209 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 int vertical;
7211 int dlgPaddingX;
7212 int dlgPaddingY;
7213#ifdef USE_SYSMENU_FONT
7214 LOGFONT lfSysmenu;
7215 int use_lfSysmenu = FALSE;
7216#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007217 garray_T ga;
7218 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219
7220#ifndef NO_CONSOLE
7221 /* Don't output anything in silent mode ("ex -s") */
7222 if (silent_mode)
7223 return dfltbutton; /* return default option */
7224#endif
7225
Bram Moolenaar748bf032005-02-02 23:04:36 +00007226 if (s_hwnd == NULL)
7227 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228
7229 if ((type < 0) || (type > VIM_LAST_TYPE))
7230 type = 0;
7231
7232 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007233 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007234 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007235 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236
7237 if (p == NULL)
7238 return -1;
7239
7240 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007241 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7243 * const.
7244 */
7245 tbuffer = vim_strsave(buttons);
7246 if (tbuffer == NULL)
7247 return -1;
7248
7249 --dfltbutton; /* Change from one-based to zero-based */
7250
7251 /* Count buttons */
7252 numButtons = 1;
7253 for (i = 0; tbuffer[i] != '\0'; i++)
7254 {
7255 if (tbuffer[i] == DLG_BUTTON_SEP)
7256 numButtons++;
7257 }
7258 if (dfltbutton >= numButtons)
7259 dfltbutton = -1;
7260
7261 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007262 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 if (buttonWidths == NULL)
7264 return -1;
7265
7266 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007267 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 if (buttonPositions == NULL)
7269 return -1;
7270
7271 /*
7272 * Calculate how big the dialog must be.
7273 */
7274 hwnd = GetDesktopWindow();
7275 hdc = GetWindowDC(hwnd);
7276#ifdef USE_SYSMENU_FONT
7277 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7278 {
7279 font = CreateFontIndirect(&lfSysmenu);
7280 use_lfSysmenu = TRUE;
7281 }
7282 else
7283#endif
7284 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7285 VARIABLE_PITCH , DLG_FONT_NAME);
7286 if (s_usenewlook)
7287 {
7288 oldFont = SelectFont(hdc, font);
7289 dlgPaddingX = DLG_PADDING_X;
7290 dlgPaddingY = DLG_PADDING_Y;
7291 }
7292 else
7293 {
7294 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7295 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7296 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7297 }
7298 GetTextMetrics(hdc, &fontInfo);
7299 fontHeight = fontInfo.tmHeight;
7300
7301 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007302 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303
7304 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007305 if (s_hwnd == NULL)
7306 {
7307 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308
Bram Moolenaarc716c302006-01-21 22:12:51 +00007309 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007310 get_work_area(&workarea_rect);
7311 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7312 if (maxDialogWidth > 600)
7313 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007314 /* Leave some room for the taskbar. */
7315 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007316 }
7317 else
7318 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02007319 /* Use our own window for the size, unless it's very small. */
7320 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007321 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02007322 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007323 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007324 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
7325 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007326
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007327 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007328 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007329 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02007330 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007331 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
7332 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
7333 }
7334
7335 /* Set dlgwidth to width of message.
7336 * Copy the message into "ga", changing NL to CR-NL and inserting line
7337 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 pstart = message;
7339 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007340 msgheight = 0;
7341 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 do
7343 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007344 msgheight += fontHeight; /* at least one line */
7345
7346 /* Need to figure out where to break the string. The system does it
7347 * at a word boundary, which would mean we can't compute the number of
7348 * wrapped lines. */
7349 textWidth = 0;
7350 last_white = NULL;
7351 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007352 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007353#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007354 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007355#else
7356 l = 1;
7357#endif
7358 if (l == 1 && vim_iswhite(*pend)
7359 && textWidth > maxDialogWidth * 3 / 4)
7360 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007361 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007362 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007363 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007364 /* Line will wrap. */
7365 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007366 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007367 textWidth = 0;
7368
7369 if (last_white != NULL)
7370 {
7371 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007372 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007373 pend = last_white + 1;
7374 last_white = NULL;
7375 }
7376 ga_append(&ga, '\r');
7377 ga_append(&ga, '\n');
7378 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007379 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007380
7381 while (--l >= 0)
7382 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007383 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007384 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007386
7387 ga_append(&ga, '\r');
7388 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 pstart = pend + 1;
7390 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007391
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007392 if (ga.ga_data != NULL)
7393 message = ga.ga_data;
7394
Bram Moolenaar748bf032005-02-02 23:04:36 +00007395 messageWidth += 10; /* roundoff space */
7396
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02007398 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7399 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400
7401 if (msgheight < DLG_ICON_HEIGHT)
7402 msgheight = DLG_ICON_HEIGHT;
7403
7404 /*
7405 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007406 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007408 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 if (!vertical)
7410 {
7411 // Place buttons horizontally if they fit.
7412 horizWidth = dlgPaddingX;
7413 pstart = tbuffer;
7414 i = 0;
7415 do
7416 {
7417 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7418 if (pend == NULL)
7419 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007420 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 if (textWidth < minButtonWidth)
7422 textWidth = minButtonWidth;
7423 textWidth += dlgPaddingX; /* Padding within button */
7424 buttonWidths[i] = textWidth;
7425 buttonPositions[i++] = horizWidth;
7426 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7427 pstart = pend + 1;
7428 } while (*pend != NUL);
7429
7430 if (horizWidth > maxDialogWidth)
7431 vertical = TRUE; // Too wide to fit on the screen.
7432 else if (horizWidth > dlgwidth)
7433 dlgwidth = horizWidth;
7434 }
7435
7436 if (vertical)
7437 {
7438 // Stack buttons vertically.
7439 pstart = tbuffer;
7440 do
7441 {
7442 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7443 if (pend == NULL)
7444 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007445 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 textWidth += dlgPaddingX; /* Padding within button */
7447 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7448 if (textWidth > dlgwidth)
7449 dlgwidth = textWidth;
7450 pstart = pend + 1;
7451 } while (*pend != NUL);
7452 }
7453
7454 if (dlgwidth < DLG_MIN_WIDTH)
7455 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7456
7457 /* start to fill in the dlgtemplate information. addressing by WORDs */
7458 if (s_usenewlook)
7459 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7460 else
7461 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7462
7463 add_long(lStyle);
7464 add_long(0); // (lExtendedStyle)
7465 pnumitems = p; /*save where the number of items must be stored*/
7466 add_word(0); // NumberOfItems(will change later)
7467 add_word(10); // x
7468 add_word(10); // y
7469 add_word(PixelToDialogX(dlgwidth)); // cx
7470
7471 // Dialog height.
7472 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007473 dlgheight = msgheight + 2 * dlgPaddingY
7474 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 else
7476 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7477
7478 // Dialog needs to be taller if contains an edit box.
7479 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7480 if (textfield != NULL)
7481 dlgheight += editboxheight;
7482
Bram Moolenaara95d8232013-08-07 15:27:11 +02007483 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7484 if (dlgheight > maxDialogHeight)
7485 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007486 msgheight = msgheight - (dlgheight - maxDialogHeight);
7487 dlgheight = maxDialogHeight;
7488 scroll_flag = WS_VSCROLL;
7489 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7490 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007491 }
7492
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 add_word(PixelToDialogY(dlgheight));
7494
7495 add_word(0); // Menu
7496 add_word(0); // Class
7497
7498 /* copy the title of the dialog */
7499 nchar = nCopyAnsiToWideChar(p, (title ?
7500 (LPSTR)title :
7501 (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
7502 p += nchar;
7503
7504 if (s_usenewlook)
7505 {
7506 /* do the font, since DS_3DLOOK doesn't work properly */
7507#ifdef USE_SYSMENU_FONT
7508 if (use_lfSysmenu)
7509 {
7510 /* point size */
7511 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7512 GetDeviceCaps(hdc, LOGPIXELSY));
7513 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
7514 }
7515 else
7516#endif
7517 {
7518 *p++ = DLG_FONT_POINT_SIZE; // point size
7519 nchar = nCopyAnsiToWideChar(p, TEXT(DLG_FONT_NAME));
7520 }
7521 p += nchar;
7522 }
7523
7524 buttonYpos = msgheight + 2 * dlgPaddingY;
7525
7526 if (textfield != NULL)
7527 buttonYpos += editboxheight;
7528
7529 pstart = tbuffer;
7530 if (!vertical)
7531 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7532 for (i = 0; i < numButtons; i++)
7533 {
7534 /* get end of this button. */
7535 for ( pend = pstart;
7536 *pend && (*pend != DLG_BUTTON_SEP);
7537 pend++)
7538 ;
7539
7540 if (*pend)
7541 *pend = '\0';
7542
7543 /*
7544 * old NOTE:
7545 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7546 * the focus to the first tab-able button and in so doing makes that
7547 * the default!! Grrr. Workaround: Make the default button the only
7548 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7549 * he/she can use arrow keys.
7550 *
7551 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007552 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 * dialog. Also needed for when the textfield is the default control.
7554 * It appears to work now (perhaps not on Win95?).
7555 */
7556 if (vertical)
7557 {
7558 p = add_dialog_element(p,
7559 (i == dfltbutton
7560 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7561 PixelToDialogX(DLG_VERT_PADDING_X),
7562 PixelToDialogY(buttonYpos /* TBK */
7563 + 2 * fontHeight * i),
7564 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7565 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007566 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 }
7568 else
7569 {
7570 p = add_dialog_element(p,
7571 (i == dfltbutton
7572 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7573 PixelToDialogX(horizWidth + buttonPositions[i]),
7574 PixelToDialogY(buttonYpos), /* TBK */
7575 PixelToDialogX(buttonWidths[i]),
7576 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007577 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 }
7579 pstart = pend + 1; /*next button*/
7580 }
7581 *pnumitems += numButtons;
7582
7583 /* Vim icon */
7584 p = add_dialog_element(p, SS_ICON,
7585 PixelToDialogX(dlgPaddingX),
7586 PixelToDialogY(dlgPaddingY),
7587 PixelToDialogX(DLG_ICON_WIDTH),
7588 PixelToDialogY(DLG_ICON_HEIGHT),
7589 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7590 dlg_icons[type]);
7591
Bram Moolenaar748bf032005-02-02 23:04:36 +00007592 /* Dialog message */
7593 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7594 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7595 PixelToDialogY(dlgPaddingY),
7596 (WORD)(PixelToDialogX(messageWidth) + 1),
7597 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007598 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599
7600 /* Edit box */
7601 if (textfield != NULL)
7602 {
7603 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7604 PixelToDialogX(2 * dlgPaddingX),
7605 PixelToDialogY(2 * dlgPaddingY + msgheight),
7606 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7607 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007608 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 *pnumitems += 1;
7610 }
7611
7612 *pnumitems += 2;
7613
7614 SelectFont(hdc, oldFont);
7615 DeleteObject(font);
7616 ReleaseDC(hwnd, hdc);
7617
7618 /* Let the dialog_callback() function know which button to make default
7619 * If we have an edit box, make that the default. We also need to tell
7620 * dialog_callback() if this dialog contains an edit box or not. We do
7621 * this by setting s_textfield if it does.
7622 */
7623 if (textfield != NULL)
7624 {
7625 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7626 s_textfield = textfield;
7627 }
7628 else
7629 {
7630 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7631 s_textfield = NULL;
7632 }
7633
7634 /* show the dialog box modally and get a return value */
7635 nchar = (int)DialogBoxIndirect(
7636 s_hinst,
7637 (LPDLGTEMPLATE)pdlgtemplate,
7638 s_hwnd,
7639 (DLGPROC)dialog_callback);
7640
7641 LocalFree(LocalHandle(pdlgtemplate));
7642 vim_free(tbuffer);
7643 vim_free(buttonWidths);
7644 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007645 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646
7647 /* Focus back to our window (for when MDI is used). */
7648 (void)SetFocus(s_hwnd);
7649
7650 return nchar;
7651}
7652
7653#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007654
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655/*
7656 * Put a simple element (basic class) onto a dialog template in memory.
7657 * return a pointer to where the next item should be added.
7658 *
7659 * parameters:
7660 * lStyle = additional style flags
7661 * (be careful, NT3.51 & Win32s will ignore the new ones)
7662 * x,y = x & y positions IN DIALOG UNITS
7663 * w,h = width and height IN DIALOG UNITS
7664 * Id = ID used in messages
7665 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7666 * caption = usually text or resource name
7667 *
7668 * TODO: use the length information noted here to enable the dialog creation
7669 * routines to work out more exactly how much memory they need to alloc.
7670 */
7671 static PWORD
7672add_dialog_element(
7673 PWORD p,
7674 DWORD lStyle,
7675 WORD x,
7676 WORD y,
7677 WORD w,
7678 WORD h,
7679 WORD Id,
7680 WORD clss,
7681 const char *caption)
7682{
7683 int nchar;
7684
7685 p = lpwAlign(p); /* Align to dword boundary*/
7686 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7687 *p++ = LOWORD(lStyle);
7688 *p++ = HIWORD(lStyle);
7689 *p++ = 0; // LOWORD (lExtendedStyle)
7690 *p++ = 0; // HIWORD (lExtendedStyle)
7691 *p++ = x;
7692 *p++ = y;
7693 *p++ = w;
7694 *p++ = h;
7695 *p++ = Id; //9 or 10 words in all
7696
7697 *p++ = (WORD)0xffff;
7698 *p++ = clss; //2 more here
7699
7700 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption); //strlen(caption)+1
7701 p += nchar;
7702
7703 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7704
7705 return p; //total = 15+ (strlen(caption)) words
7706 // = 30 + 2(strlen(caption) bytes reqd
7707}
7708
7709
7710/*
7711 * Helper routine. Take an input pointer, return closest pointer that is
7712 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7713 */
7714 static LPWORD
7715lpwAlign(
7716 LPWORD lpIn)
7717{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007718 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007720 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 ul += 3;
7722 ul >>= 2;
7723 ul <<= 2;
7724 return (LPWORD)ul;
7725}
7726
7727/*
7728 * Helper routine. Takes second parameter as Ansi string, copies it to first
7729 * parameter as wide character (16-bits / char) string, and returns integer
7730 * number of wide characters (words) in string (including the trailing wide
7731 * char NULL). Partly taken from the Win32SDK samples.
7732 */
7733 static int
7734nCopyAnsiToWideChar(
7735 LPWORD lpWCStr,
7736 LPSTR lpAnsiIn)
7737{
7738 int nChar = 0;
7739#ifdef FEAT_MBYTE
7740 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7741 int i;
7742 WCHAR *wn;
7743
7744 if (enc_codepage == 0 && (int)GetACP() != enc_codepage)
7745 {
7746 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007747 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 if (wn != NULL)
7749 {
7750 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007751 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 vim_free(wn);
7753 }
7754 }
7755 if (nChar == 0)
7756 /* Use Win32 conversion function. */
7757 nChar = MultiByteToWideChar(
7758 enc_codepage > 0 ? enc_codepage : CP_ACP,
7759 MB_PRECOMPOSED,
7760 lpAnsiIn, len,
7761 lpWCStr, len);
7762 for (i = 0; i < nChar; ++i)
7763 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7764 lpWCStr[i] = (WORD)' ';
7765#else
7766 do
7767 {
7768 if (*lpAnsiIn == '\t')
7769 *lpWCStr++ = (WORD)' ';
7770 else
7771 *lpWCStr++ = (WORD)*lpAnsiIn;
7772 nChar++;
7773 } while (*lpAnsiIn++);
7774#endif
7775
7776 return nChar;
7777}
7778
7779
7780#ifdef FEAT_TEAROFF
7781/*
7782 * The callback function for all the modeless dialogs that make up the
7783 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7784 * thinking its menus have been clicked), and go away when closed.
7785 */
7786 static LRESULT CALLBACK
7787tearoff_callback(
7788 HWND hwnd,
7789 UINT message,
7790 WPARAM wParam,
7791 LPARAM lParam)
7792{
7793 if (message == WM_INITDIALOG)
7794 return (TRUE);
7795
7796 /* May show the mouse pointer again. */
7797 HandleMouseHide(message, lParam);
7798
7799 if (message == WM_COMMAND)
7800 {
7801 if ((WORD)(LOWORD(wParam)) & 0x8000)
7802 {
7803 POINT mp;
7804 RECT rect;
7805
7806 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7807 {
7808 (void)TrackPopupMenu(
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007809 (HMENU)(long_u)(LOWORD(wParam) ^ 0x8000),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7811 (int)rect.right - 8,
7812 (int)mp.y,
7813 (int)0, /*reserved param*/
7814 s_hwnd,
7815 NULL);
7816 /*
7817 * NOTE: The pop-up menu can eat the mouse up event.
7818 * We deal with this in normal.c.
7819 */
7820 }
7821 }
7822 else
7823 /* Pass on messages to the main Vim window */
7824 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7825 /*
7826 * Give main window the focus back: this is so after
7827 * choosing a tearoff button you can start typing again
7828 * straight away.
7829 */
7830 (void)SetFocus(s_hwnd);
7831 return TRUE;
7832 }
7833 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7834 {
7835 DestroyWindow(hwnd);
7836 return TRUE;
7837 }
7838
7839 /* When moved around, give main window the focus back. */
7840 if (message == WM_EXITSIZEMOVE)
7841 (void)SetActiveWindow(s_hwnd);
7842
7843 return FALSE;
7844}
7845#endif
7846
7847
7848/*
7849 * Decide whether to use the "new look" (small, non-bold font) or the "old
7850 * look" (big, clanky font) for dialogs, and work out a few values for use
7851 * later accordingly.
7852 */
7853 static void
7854get_dialog_font_metrics(void)
7855{
7856 HDC hdc;
7857 HFONT hfontTools = 0;
7858 DWORD dlgFontSize;
7859 SIZE size;
7860#ifdef USE_SYSMENU_FONT
7861 LOGFONT lfSysmenu;
7862#endif
7863
7864 s_usenewlook = FALSE;
7865
7866 /*
7867 * For NT3.51 and Win32s, we stick with the old look
7868 * because it matches everything else.
7869 */
7870 if (!is_winnt_3())
7871 {
7872#ifdef USE_SYSMENU_FONT
7873 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7874 hfontTools = CreateFontIndirect(&lfSysmenu);
7875 else
7876#endif
7877 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7878 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7879
7880 if (hfontTools)
7881 {
7882 hdc = GetDC(s_hwnd);
7883 SelectObject(hdc, hfontTools);
7884 /*
7885 * GetTextMetrics() doesn't return the right value in
7886 * tmAveCharWidth, so we have to figure out the dialog base units
7887 * ourselves.
7888 */
7889 GetTextExtentPoint(hdc,
7890 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7891 52, &size);
7892 ReleaseDC(s_hwnd, hdc);
7893
7894 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7895 s_dlgfntheight = (WORD)size.cy;
7896 s_usenewlook = TRUE;
7897 }
7898 }
7899
7900 if (!s_usenewlook)
7901 {
7902 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7903 s_dlgfntwidth = LOWORD(dlgFontSize);
7904 s_dlgfntheight = HIWORD(dlgFontSize);
7905 }
7906}
7907
7908#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7909/*
7910 * Create a pseudo-"tearoff menu" based on the child
7911 * items of a given menu pointer.
7912 */
7913 static void
7914gui_mch_tearoff(
7915 char_u *title,
7916 vimmenu_T *menu,
7917 int initX,
7918 int initY)
7919{
7920 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7921 int template_len;
7922 int nchar, textWidth, submenuWidth;
7923 DWORD lStyle;
7924 DWORD lExtendedStyle;
7925 WORD dlgwidth;
7926 WORD menuID;
7927 vimmenu_T *pmenu;
7928 vimmenu_T *the_menu = menu;
7929 HWND hwnd;
7930 HDC hdc;
7931 HFONT font, oldFont;
7932 int col, spaceWidth, len;
7933 int columnWidths[2];
7934 char_u *label, *text;
7935 int acLen = 0;
7936 int nameLen;
7937 int padding0, padding1, padding2 = 0;
7938 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007939 int x;
7940 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941#ifdef USE_SYSMENU_FONT
7942 LOGFONT lfSysmenu;
7943 int use_lfSysmenu = FALSE;
7944#endif
7945
7946 /*
7947 * If this menu is already torn off, move it to the mouse position.
7948 */
7949 if (IsWindow(menu->tearoff_handle))
7950 {
7951 POINT mp;
7952 if (GetCursorPos((LPPOINT)&mp))
7953 {
7954 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7955 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7956 }
7957 return;
7958 }
7959
7960 /*
7961 * Create a new tearoff.
7962 */
7963 if (*title == MNU_HIDDEN_CHAR)
7964 title++;
7965
7966 /* Allocate memory to store the dialog template. It's made bigger when
7967 * needed. */
7968 template_len = DLG_ALLOC_SIZE;
7969 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7970 if (p == NULL)
7971 return;
7972
7973 hwnd = GetDesktopWindow();
7974 hdc = GetWindowDC(hwnd);
7975#ifdef USE_SYSMENU_FONT
7976 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7977 {
7978 font = CreateFontIndirect(&lfSysmenu);
7979 use_lfSysmenu = TRUE;
7980 }
7981 else
7982#endif
7983 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7984 VARIABLE_PITCH , DLG_FONT_NAME);
7985 if (s_usenewlook)
7986 oldFont = SelectFont(hdc, font);
7987 else
7988 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7989
7990 /* Calculate width of a single space. Used for padding columns to the
7991 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007992 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993
7994 /* Figure out max width of the text column, the accelerator column and the
7995 * optional submenu column. */
7996 submenuWidth = 0;
7997 for (col = 0; col < 2; col++)
7998 {
7999 columnWidths[col] = 0;
8000 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
8001 {
8002 /* Use "dname" here to compute the width of the visible text. */
8003 text = (col == 0) ? pmenu->dname : pmenu->actext;
8004 if (text != NULL && *text != NUL)
8005 {
8006 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
8007 if (textWidth > columnWidths[col])
8008 columnWidths[col] = textWidth;
8009 }
8010 if (pmenu->children != NULL)
8011 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
8012 }
8013 }
8014 if (columnWidths[1] == 0)
8015 {
8016 /* no accelerators */
8017 if (submenuWidth != 0)
8018 columnWidths[0] += submenuWidth;
8019 else
8020 columnWidths[0] += spaceWidth;
8021 }
8022 else
8023 {
8024 /* there is an accelerator column */
8025 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
8026 columnWidths[1] += submenuWidth;
8027 }
8028
8029 /*
8030 * Now find the total width of our 'menu'.
8031 */
8032 textWidth = columnWidths[0] + columnWidths[1];
8033 if (submenuWidth != 0)
8034 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008035 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
8037 textWidth += submenuWidth;
8038 }
8039 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
8040 if (textWidth > dlgwidth)
8041 dlgwidth = textWidth;
8042 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
8043
8044 /* W95 can't do thin dialogs, they look v. weird! */
8045 if (mch_windows95() && dlgwidth < TEAROFF_MIN_WIDTH)
8046 dlgwidth = TEAROFF_MIN_WIDTH;
8047
8048 /* start to fill in the dlgtemplate information. addressing by WORDs */
8049 if (s_usenewlook)
8050 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
8051 else
8052 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
8053
8054 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
8055 *p++ = LOWORD(lStyle);
8056 *p++ = HIWORD(lStyle);
8057 *p++ = LOWORD(lExtendedStyle);
8058 *p++ = HIWORD(lExtendedStyle);
8059 pnumitems = p; /* save where the number of items must be stored */
8060 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008061 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008063 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 else
8065 *p++ = PixelToDialogX(initX); // x
8066 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008067 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 else
8069 *p++ = PixelToDialogY(initY); // y
8070 *p++ = PixelToDialogX(dlgwidth); // cx
8071 ptrueheight = p;
8072 *p++ = 0; // dialog height: changed later anyway
8073 *p++ = 0; // Menu
8074 *p++ = 0; // Class
8075
8076 /* copy the title of the dialog */
8077 nchar = nCopyAnsiToWideChar(p, ((*title)
8078 ? (LPSTR)title
8079 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
8080 p += nchar;
8081
8082 if (s_usenewlook)
8083 {
8084 /* do the font, since DS_3DLOOK doesn't work properly */
8085#ifdef USE_SYSMENU_FONT
8086 if (use_lfSysmenu)
8087 {
8088 /* point size */
8089 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
8090 GetDeviceCaps(hdc, LOGPIXELSY));
8091 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
8092 }
8093 else
8094#endif
8095 {
8096 *p++ = DLG_FONT_POINT_SIZE; // point size
8097 nchar = nCopyAnsiToWideChar (p, TEXT(DLG_FONT_NAME));
8098 }
8099 p += nchar;
8100 }
8101
8102 /*
8103 * Loop over all the items in the menu.
8104 * But skip over the tearbar.
8105 */
8106 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
8107 menu = menu->children->next;
8108 else
8109 menu = menu->children;
8110 for ( ; menu != NULL; menu = menu->next)
8111 {
8112 if (menu->modes == 0) /* this menu has just been deleted */
8113 continue;
8114 if (menu_is_separator(menu->dname))
8115 {
8116 sepPadding += 3;
8117 continue;
8118 }
8119
8120 /* Check if there still is plenty of room in the template. Make it
8121 * larger when needed. */
8122 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8123 {
8124 WORD *newp;
8125
8126 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8127 if (newp != NULL)
8128 {
8129 template_len += 4096;
8130 mch_memmove(newp, pdlgtemplate,
8131 (char *)p - (char *)pdlgtemplate);
8132 p = newp + (p - pdlgtemplate);
8133 pnumitems = newp + (pnumitems - pdlgtemplate);
8134 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8135 LocalFree(LocalHandle(pdlgtemplate));
8136 pdlgtemplate = newp;
8137 }
8138 }
8139
8140 /* Figure out minimal length of this menu label. Use "name" for the
8141 * actual text, "dname" for estimating the displayed size. "name"
8142 * has "&a" for mnemonic and includes the accelerator. */
8143 len = nameLen = (int)STRLEN(menu->name);
8144 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8145 (int)STRLEN(menu->dname))) / spaceWidth;
8146 len += padding0;
8147
8148 if (menu->actext != NULL)
8149 {
8150 acLen = (int)STRLEN(menu->actext);
8151 len += acLen;
8152 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8153 }
8154 else
8155 textWidth = 0;
8156 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8157 len += padding1;
8158
8159 if (menu->children == NULL)
8160 {
8161 padding2 = submenuWidth / spaceWidth;
8162 len += padding2;
8163 menuID = (WORD)(menu->id);
8164 }
8165 else
8166 {
8167 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008168 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 }
8170
8171 /* Allocate menu label and fill it in */
8172 text = label = alloc((unsigned)len + 1);
8173 if (label == NULL)
8174 break;
8175
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008176 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 text = vim_strchr(text, TAB); /* stop at TAB before actext */
8178 if (text == NULL)
8179 text = label + nameLen; /* no actext, use whole name */
8180 while (padding0-- > 0)
8181 *text++ = ' ';
8182 if (menu->actext != NULL)
8183 {
8184 STRNCPY(text, menu->actext, acLen);
8185 text += acLen;
8186 }
8187 while (padding1-- > 0)
8188 *text++ = ' ';
8189 if (menu->children != NULL)
8190 {
8191 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8192 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8193 }
8194 else
8195 {
8196 while (padding2-- > 0)
8197 *text++ = ' ';
8198 }
8199 *text = NUL;
8200
8201 /*
8202 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8203 * W95/NT4 it makes the tear-off look more like a menu.
8204 */
8205 p = add_dialog_element(p,
8206 BS_PUSHBUTTON|BS_LEFT,
8207 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8208 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8209 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8210 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008211 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 vim_free(label);
8213 (*pnumitems)++;
8214 }
8215
8216 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8217
8218
8219 /* show modelessly */
8220 the_menu->tearoff_handle = CreateDialogIndirect(
8221 s_hinst,
8222 (LPDLGTEMPLATE)pdlgtemplate,
8223 s_hwnd,
8224 (DLGPROC)tearoff_callback);
8225
8226 LocalFree(LocalHandle(pdlgtemplate));
8227 SelectFont(hdc, oldFont);
8228 DeleteObject(font);
8229 ReleaseDC(hwnd, hdc);
8230
8231 /*
8232 * Reassert ourselves as the active window. This is so that after creating
8233 * a tearoff, the user doesn't have to click with the mouse just to start
8234 * typing again!
8235 */
8236 (void)SetActiveWindow(s_hwnd);
8237
8238 /* make sure the right buttons are enabled */
8239 force_menu_update = TRUE;
8240}
8241#endif
8242
8243#if defined(FEAT_TOOLBAR) || defined(PROTO)
8244#include "gui_w32_rc.h"
8245
8246/* This not defined in older SDKs */
8247# ifndef TBSTYLE_FLAT
8248# define TBSTYLE_FLAT 0x0800
8249# endif
8250
8251/*
8252 * Create the toolbar, initially unpopulated.
8253 * (just like the menu, there are no defaults, it's all
8254 * set up through menu.vim)
8255 */
8256 static void
8257initialise_toolbar(void)
8258{
8259 InitCommonControls();
8260 s_toolbarhwnd = CreateToolbarEx(
8261 s_hwnd,
8262 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8263 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008264 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 s_hinst,
8266 IDR_TOOLBAR1, // id of initial bitmap
8267 NULL,
8268 0, // initial number of buttons
8269 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8270 TOOLBAR_BUTTON_HEIGHT,
8271 TOOLBAR_BUTTON_WIDTH,
8272 TOOLBAR_BUTTON_HEIGHT,
8273 sizeof(TBBUTTON)
8274 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008275 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276
8277 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
8278}
8279
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008280 static LRESULT CALLBACK
8281toolbar_wndproc(
8282 HWND hwnd,
8283 UINT uMsg,
8284 WPARAM wParam,
8285 LPARAM lParam)
8286{
8287 HandleMouseHide(uMsg, lParam);
8288 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8289}
8290
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 static int
8292get_toolbar_bitmap(vimmenu_T *menu)
8293{
8294 int i = -1;
8295
8296 /*
8297 * Check user bitmaps first, unless builtin is specified.
8298 */
8299 if (!is_winnt_3() && !menu->icon_builtin)
8300 {
8301 char_u fname[MAXPATHL];
8302 HANDLE hbitmap = NULL;
8303
8304 if (menu->iconfile != NULL)
8305 {
8306 gui_find_iconfile(menu->iconfile, fname, "bmp");
8307 hbitmap = LoadImage(
8308 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008309 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 IMAGE_BITMAP,
8311 TOOLBAR_BUTTON_WIDTH,
8312 TOOLBAR_BUTTON_HEIGHT,
8313 LR_LOADFROMFILE |
8314 LR_LOADMAP3DCOLORS
8315 );
8316 }
8317
8318 /*
8319 * If the LoadImage call failed, or the "icon=" file
8320 * didn't exist or wasn't specified, try the menu name
8321 */
8322 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008323 && (gui_find_bitmap(
8324#ifdef FEAT_MULTI_LANG
8325 menu->en_dname != NULL ? menu->en_dname :
8326#endif
8327 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 hbitmap = LoadImage(
8329 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008330 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 IMAGE_BITMAP,
8332 TOOLBAR_BUTTON_WIDTH,
8333 TOOLBAR_BUTTON_HEIGHT,
8334 LR_LOADFROMFILE |
8335 LR_LOADMAP3DCOLORS
8336 );
8337
8338 if (hbitmap != NULL)
8339 {
8340 TBADDBITMAP tbAddBitmap;
8341
8342 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008343 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344
8345 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8346 (WPARAM)1, (LPARAM)&tbAddBitmap);
8347 /* i will be set to -1 if it fails */
8348 }
8349 }
8350 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8351 i = menu->iconidx;
8352
8353 return i;
8354}
8355#endif
8356
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008357#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8358 static void
8359initialise_tabline(void)
8360{
8361 InitCommonControls();
8362
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008363 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008364 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008365 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8366 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008367 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008368
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008369 gui.tabline_height = TABLINE_HEIGHT;
8370
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008371# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008372 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008373# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008374}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008375
8376 static LRESULT CALLBACK
8377tabline_wndproc(
8378 HWND hwnd,
8379 UINT uMsg,
8380 WPARAM wParam,
8381 LPARAM lParam)
8382{
8383 HandleMouseHide(uMsg, lParam);
8384 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8385}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008386#endif
8387
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8389/*
8390 * Make the GUI window come to the foreground.
8391 */
8392 void
8393gui_mch_set_foreground(void)
8394{
8395 if (IsIconic(s_hwnd))
8396 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8397 SetForegroundWindow(s_hwnd);
8398}
8399#endif
8400
8401#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8402 static void
8403dyn_imm_load(void)
8404{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008405 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 if (hLibImm == NULL)
8407 return;
8408
8409 pImmGetCompositionStringA
8410 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8411 pImmGetCompositionStringW
8412 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8413 pImmGetContext
8414 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8415 pImmAssociateContext
8416 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8417 pImmReleaseContext
8418 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8419 pImmGetOpenStatus
8420 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8421 pImmSetOpenStatus
8422 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
8423 pImmGetCompositionFont
8424 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
8425 pImmSetCompositionFont
8426 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
8427 pImmSetCompositionWindow
8428 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8429 pImmGetConversionStatus
8430 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008431 pImmSetConversionStatus
8432 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433
8434 if ( pImmGetCompositionStringA == NULL
8435 || pImmGetCompositionStringW == NULL
8436 || pImmGetContext == NULL
8437 || pImmAssociateContext == NULL
8438 || pImmReleaseContext == NULL
8439 || pImmGetOpenStatus == NULL
8440 || pImmSetOpenStatus == NULL
8441 || pImmGetCompositionFont == NULL
8442 || pImmSetCompositionFont == NULL
8443 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008444 || pImmGetConversionStatus == NULL
8445 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 {
8447 FreeLibrary(hLibImm);
8448 hLibImm = NULL;
8449 pImmGetContext = NULL;
8450 return;
8451 }
8452
8453 return;
8454}
8455
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456#endif
8457
8458#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8459
8460# ifdef FEAT_XPM_W32
8461# define IMAGE_XPM 100
8462# endif
8463
8464typedef struct _signicon_t
8465{
8466 HANDLE hImage;
8467 UINT uType;
8468#ifdef FEAT_XPM_W32
8469 HANDLE hShape; /* Mask bitmap handle */
8470#endif
8471} signicon_t;
8472
8473 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008474gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475{
8476 signicon_t *sign;
8477 int x, y, w, h;
8478
8479 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8480 return;
8481
8482 x = TEXT_X(col);
8483 y = TEXT_Y(row);
8484 w = gui.char_width * 2;
8485 h = gui.char_height;
8486 switch (sign->uType)
8487 {
8488 case IMAGE_BITMAP:
8489 {
8490 HDC hdcMem;
8491 HBITMAP hbmpOld;
8492
8493 hdcMem = CreateCompatibleDC(s_hdc);
8494 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8495 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8496 SelectObject(hdcMem, hbmpOld);
8497 DeleteDC(hdcMem);
8498 }
8499 break;
8500 case IMAGE_ICON:
8501 case IMAGE_CURSOR:
8502 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8503 break;
8504#ifdef FEAT_XPM_W32
8505 case IMAGE_XPM:
8506 {
8507 HDC hdcMem;
8508 HBITMAP hbmpOld;
8509
8510 hdcMem = CreateCompatibleDC(s_hdc);
8511 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8512 /* Make hole */
8513 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8514
8515 SelectObject(hdcMem, sign->hImage);
8516 /* Paint sign */
8517 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8518 SelectObject(hdcMem, hbmpOld);
8519 DeleteDC(hdcMem);
8520 }
8521 break;
8522#endif
8523 }
8524}
8525
8526 static void
8527close_signicon_image(signicon_t *sign)
8528{
8529 if (sign)
8530 switch (sign->uType)
8531 {
8532 case IMAGE_BITMAP:
8533 DeleteObject((HGDIOBJ)sign->hImage);
8534 break;
8535 case IMAGE_CURSOR:
8536 DestroyCursor((HCURSOR)sign->hImage);
8537 break;
8538 case IMAGE_ICON:
8539 DestroyIcon((HICON)sign->hImage);
8540 break;
8541#ifdef FEAT_XPM_W32
8542 case IMAGE_XPM:
8543 DeleteObject((HBITMAP)sign->hImage);
8544 DeleteObject((HBITMAP)sign->hShape);
8545 break;
8546#endif
8547 }
8548}
8549
8550 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008551gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552{
8553 signicon_t sign, *psign;
8554 char_u *ext;
8555
8556 if (is_winnt_3())
8557 {
8558 EMSG(_(e_signdata));
8559 return NULL;
8560 }
8561
8562 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008563 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 if (ext > signfile)
8565 {
8566 int do_load = 1;
8567
8568 if (!STRICMP(ext, ".bmp"))
8569 sign.uType = IMAGE_BITMAP;
8570 else if (!STRICMP(ext, ".ico"))
8571 sign.uType = IMAGE_ICON;
8572 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8573 sign.uType = IMAGE_CURSOR;
8574 else
8575 do_load = 0;
8576
8577 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008578 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 gui.char_width * 2, gui.char_height,
8580 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8581#ifdef FEAT_XPM_W32
8582 if (!STRICMP(ext, ".xpm"))
8583 {
8584 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008585 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8586 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 }
8588#endif
8589 }
8590
8591 psign = NULL;
8592 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8593 != NULL)
8594 *psign = sign;
8595
8596 if (!psign)
8597 {
8598 if (sign.hImage)
8599 close_signicon_image(&sign);
8600 EMSG(_(e_signdata));
8601 }
8602 return (void *)psign;
8603
8604}
8605
8606 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008607gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608{
8609 if (sign)
8610 {
8611 close_signicon_image((signicon_t *)sign);
8612 vim_free(sign);
8613 }
8614}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616
8617#if defined(FEAT_BEVAL) || defined(PROTO)
8618
8619/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008620 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 *
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00008622 * The only reused thing is gui_beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8624 * to get current mouse position).
8625 *
8626 * Trying to use as more Windows services as possible, and as less
8627 * IE version as possible :)).
8628 *
8629 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8630 * BalloonEval struct.
8631 * 2) Enable/Disable simply create/kill BalloonEval Timer
8632 * 3) When there was enough inactivity, timer procedure posts
8633 * async request to debugger
8634 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8635 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008636 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 */
8638
Bram Moolenaar45360022005-07-21 21:08:21 +00008639/*
8640 * determine whether installed Common Controls support multiline tooltips
8641 * (i.e. their version is >= 4.70
8642 */
8643 int
8644multiline_balloon_available(void)
8645{
8646 HINSTANCE hDll;
8647 static char comctl_dll[] = "comctl32.dll";
8648 static int multiline_tip = MAYBE;
8649
8650 if (multiline_tip != MAYBE)
8651 return multiline_tip;
8652
8653 hDll = GetModuleHandle(comctl_dll);
8654 if (hDll != NULL)
8655 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008656 DLLGETVERSIONPROC pGetVer;
8657 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008658
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008659 if (pGetVer != NULL)
8660 {
8661 DLLVERSIONINFO dvi;
8662 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008663
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008664 ZeroMemory(&dvi, sizeof(dvi));
8665 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008666
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008667 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008668
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008669 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008670 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008671 || (dvi.dwMajorVersion == 4
8672 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008673 {
8674 multiline_tip = TRUE;
8675 return multiline_tip;
8676 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008677 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008678 else
8679 {
8680 /* there is chance we have ancient CommCtl 4.70
8681 which doesn't export DllGetVersion */
8682 DWORD dwHandle = 0;
8683 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8684 if (len > 0)
8685 {
8686 VS_FIXEDFILEINFO *ver;
8687 UINT vlen = 0;
8688 void *data = alloc(len);
8689
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008690 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008691 && GetFileVersionInfo(comctl_dll, 0, len, data)
8692 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8693 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008694 && HIWORD(ver->dwFileVersionMS) > 4)
8695 || ((HIWORD(ver->dwFileVersionMS) == 4
8696 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008697 {
8698 vim_free(data);
8699 multiline_tip = TRUE;
8700 return multiline_tip;
8701 }
8702 vim_free(data);
8703 }
8704 }
8705 }
8706 multiline_tip = FALSE;
8707 return multiline_tip;
8708}
8709
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008711make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712{
Bram Moolenaar45360022005-07-21 21:08:21 +00008713 TOOLINFO *pti;
8714 int ToolInfoSize;
8715
8716 if (multiline_balloon_available() == TRUE)
8717 ToolInfoSize = sizeof(TOOLINFO_NEW);
8718 else
8719 ToolInfoSize = sizeof(TOOLINFO);
8720
8721 pti = (TOOLINFO *)alloc(ToolInfoSize);
8722 if (pti == NULL)
8723 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724
8725 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
8726 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8727 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8728 beval->target, NULL, s_hinst, NULL);
8729
8730 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8731 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8732
Bram Moolenaar45360022005-07-21 21:08:21 +00008733 pti->cbSize = ToolInfoSize;
8734 pti->uFlags = TTF_SUBCLASS;
8735 pti->hwnd = beval->target;
8736 pti->hinst = 0; /* Don't use string resources */
8737 pti->uId = ID_BEVAL_TOOLTIP;
8738
8739 if (multiline_balloon_available() == TRUE)
8740 {
8741 RECT rect;
8742 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
8743 pti->lpszText = LPSTR_TEXTCALLBACK;
8744 ptin->lParam = (LPARAM)text;
8745 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
8746 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8747 (LPARAM)rect.right);
8748 }
8749 else
8750 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751
8752 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00008753 pti->rect.left = pt.x - 3;
8754 pti->rect.top = pt.y - 3;
8755 pti->rect.right = pt.x + 3;
8756 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757
Bram Moolenaar45360022005-07-21 21:08:21 +00008758 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 /* Make tooltip appear sooner */
8760 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008761 /* I've performed some tests and it seems the longest possible life time
8762 * of tooltip is 30 seconds */
8763 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 /*
8765 * HACK: force tooltip to appear, because it'll not appear until
8766 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008767 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008769 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00008771 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772}
8773
8774 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008775delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008777 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778}
8779
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008780/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008782BevalTimerProc(
8783 HWND hwnd,
8784 UINT uMsg,
8785 UINT_PTR idEvent,
8786 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787{
8788 POINT pt;
8789 RECT rect;
8790
8791 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8792 return;
8793
8794 GetCursorPos(&pt);
8795 if (WindowFromPoint(pt) != s_textArea)
8796 return;
8797
8798 ScreenToClient(s_textArea, &pt);
8799 GetClientRect(s_textArea, &rect);
8800 if (!PtInRect(&rect, pt))
8801 return;
8802
8803 if (LastActivity > 0
8804 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8805 && (cur_beval->showState != ShS_PENDING
8806 || abs(cur_beval->x - pt.x) > 3
8807 || abs(cur_beval->y - pt.y) > 3))
8808 {
8809 /* Pointer resting in one place long enough, it's time to show
8810 * the tooltip. */
8811 cur_beval->showState = ShS_PENDING;
8812 cur_beval->x = pt.x;
8813 cur_beval->y = pt.y;
8814
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008815 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816
8817 if (cur_beval->msgCB != NULL)
8818 (*cur_beval->msgCB)(cur_beval, 0);
8819 }
8820}
8821
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008822/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008824gui_mch_disable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008826 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008828 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829}
8830
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008831/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008833gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008835 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 if (beval == NULL)
8837 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008838 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008839 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008840 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841}
8842
8843 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008844gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845{
8846 POINT pt;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008847 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 if (beval->showState == ShS_SHOWING)
8849 return;
8850 GetCursorPos(&pt);
8851 ScreenToClient(s_textArea, &pt);
8852
8853 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
8854 /* cursor is still here */
8855 {
8856 gui_mch_disable_beval_area(cur_beval);
8857 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008858 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008860 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861}
8862
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008863/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008865gui_mch_create_beval_area(
8866 void *target, /* ignored, always use s_textArea */
8867 char_u *mesg,
8868 void (*mesgCB)(BalloonEval *, int),
8869 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870{
8871 /* partially stolen from gui_beval.c */
8872 BalloonEval *beval;
8873
8874 if (mesg != NULL && mesgCB != NULL)
8875 {
8876 EMSG(_("E232: Cannot create BalloonEval with both message and callback"));
8877 return NULL;
8878 }
8879
8880 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
8881 if (beval != NULL)
8882 {
8883 beval->target = s_textArea;
8884 beval->balloon = NULL;
8885
8886 beval->showState = ShS_NEUTRAL;
8887 beval->x = 0;
8888 beval->y = 0;
8889 beval->msg = mesg;
8890 beval->msgCB = mesgCB;
8891 beval->clientData = clientData;
8892
8893 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 cur_beval = beval;
8895
8896 if (p_beval)
8897 gui_mch_enable_beval_area(beval);
8898
8899 }
8900 return beval;
8901}
8902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008903/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 static void
Bram Moolenaar442b4222010-05-24 21:34:22 +02008905Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906{
8907 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8908 return;
8909
8910 if (cur_beval != NULL)
8911 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008912 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008914 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008915 // TRACE0("TTN_SHOW {{{");
8916 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008917 break;
8918 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008919 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 delete_tooltip(cur_beval);
8921 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008922 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923
8924 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008925 break;
8926 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008927 {
8928 /* if you get there then we have new common controls */
8929 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8930 info->lpszText = (LPSTR)info->lParam;
8931 info->uFlags |= TTF_DI_SETITEM;
8932 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008933 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 }
8935 }
8936}
8937
8938 static void
8939TrackUserActivity(UINT uMsg)
8940{
8941 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8942 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8943 LastActivity = GetTickCount();
8944}
8945
8946 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008947gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948{
8949 vim_free(beval);
8950}
8951#endif /* FEAT_BEVAL */
8952
8953#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8954/*
8955 * We have multiple signs to draw at the same location. Draw the
8956 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8957 */
8958 void
8959netbeans_draw_multisign_indicator(int row)
8960{
8961 int i;
8962 int y;
8963 int x;
8964
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008965 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008966 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008967
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 x = 0;
8969 y = TEXT_Y(row);
8970
8971 for (i = 0; i < gui.char_height - 3; i++)
8972 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8973
8974 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8975 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8976 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8977 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8978 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8979 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8980 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8981}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008982#endif