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