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