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