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