blob: 3a64691493bfe345a0f238ca4133166aa9d3605b [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;
329#if !defined(FEAT_SNIFF) && !defined(FEAT_GUI)
330static
331#endif
332HWND s_hwnd = NULL;
333static HDC s_hdc = NULL;
334static HBRUSH s_brush = NULL;
335
336#ifdef FEAT_TOOLBAR
337static HWND s_toolbarhwnd = NULL;
338static WNDPROC s_toolbar_wndproc = NULL;
339#endif
340
341#ifdef FEAT_GUI_TABLINE
342static HWND s_tabhwnd = NULL;
343static WNDPROC s_tabline_wndproc = NULL;
344static int showing_tabline = 0;
345#endif
346
347static WPARAM s_wParam = 0;
348static LPARAM s_lParam = 0;
349
350static HWND s_textArea = NULL;
351static UINT s_uMsg = 0;
352
353static char_u *s_textfield; /* Used by dialogs to pass back strings */
354
355static int s_need_activate = FALSE;
356
357/* This variable is set when waiting for an event, which is the only moment
358 * scrollbar dragging can be done directly. It's not allowed while commands
359 * are executed, because it may move the cursor and that may cause unexpected
360 * problems (e.g., while ":s" is working).
361 */
362static int allow_scrollbar = FALSE;
363
364#ifdef GLOBAL_IME
365# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
366#else
367# define MyTranslateMessage(x) TranslateMessage(x)
368#endif
369
370#if (defined(WIN3264) && defined(FEAT_MBYTE)) || defined(GLOBAL_IME)
371 /* use of WindowProc depends on wide_WindowProc */
372# define MyWindowProc vim_WindowProc
373#else
374 /* use ordinary WindowProc */
375# define MyWindowProc DefWindowProc
376#endif
377
378extern int current_font_height; /* this is in os_mswin.c */
379
380static struct
381{
382 UINT key_sym;
383 char_u vim_code0;
384 char_u vim_code1;
385} special_keys[] =
386{
387 {VK_UP, 'k', 'u'},
388 {VK_DOWN, 'k', 'd'},
389 {VK_LEFT, 'k', 'l'},
390 {VK_RIGHT, 'k', 'r'},
391
392 {VK_F1, 'k', '1'},
393 {VK_F2, 'k', '2'},
394 {VK_F3, 'k', '3'},
395 {VK_F4, 'k', '4'},
396 {VK_F5, 'k', '5'},
397 {VK_F6, 'k', '6'},
398 {VK_F7, 'k', '7'},
399 {VK_F8, 'k', '8'},
400 {VK_F9, 'k', '9'},
401 {VK_F10, 'k', ';'},
402
403 {VK_F11, 'F', '1'},
404 {VK_F12, 'F', '2'},
405 {VK_F13, 'F', '3'},
406 {VK_F14, 'F', '4'},
407 {VK_F15, 'F', '5'},
408 {VK_F16, 'F', '6'},
409 {VK_F17, 'F', '7'},
410 {VK_F18, 'F', '8'},
411 {VK_F19, 'F', '9'},
412 {VK_F20, 'F', 'A'},
413
414 {VK_F21, 'F', 'B'},
415#ifdef FEAT_NETBEANS_INTG
416 {VK_PAUSE, 'F', 'B'}, /* Pause == F21 (see gui_gtk_x11.c) */
417#endif
418 {VK_F22, 'F', 'C'},
419 {VK_F23, 'F', 'D'},
420 {VK_F24, 'F', 'E'}, /* winuser.h defines up to F24 */
421
422 {VK_HELP, '%', '1'},
423 {VK_BACK, 'k', 'b'},
424 {VK_INSERT, 'k', 'I'},
425 {VK_DELETE, 'k', 'D'},
426 {VK_HOME, 'k', 'h'},
427 {VK_END, '@', '7'},
428 {VK_PRIOR, 'k', 'P'},
429 {VK_NEXT, 'k', 'N'},
430 {VK_PRINT, '%', '9'},
431 {VK_ADD, 'K', '6'},
432 {VK_SUBTRACT, 'K', '7'},
433 {VK_DIVIDE, 'K', '8'},
434 {VK_MULTIPLY, 'K', '9'},
435 {VK_SEPARATOR, 'K', 'A'}, /* Keypad Enter */
436 {VK_DECIMAL, 'K', 'B'},
437
438 {VK_NUMPAD0, 'K', 'C'},
439 {VK_NUMPAD1, 'K', 'D'},
440 {VK_NUMPAD2, 'K', 'E'},
441 {VK_NUMPAD3, 'K', 'F'},
442 {VK_NUMPAD4, 'K', 'G'},
443 {VK_NUMPAD5, 'K', 'H'},
444 {VK_NUMPAD6, 'K', 'I'},
445 {VK_NUMPAD7, 'K', 'J'},
446 {VK_NUMPAD8, 'K', 'K'},
447 {VK_NUMPAD9, 'K', 'L'},
448
449 /* Keys that we want to be able to use any modifier with: */
450 {VK_SPACE, ' ', NUL},
451 {VK_TAB, TAB, NUL},
452 {VK_ESCAPE, ESC, NUL},
453 {NL, NL, NUL},
454 {CAR, CAR, NUL},
455
456 /* End of list marker: */
457 {0, 0, 0}
458};
459
460/* Local variables */
461static int s_button_pending = -1;
462
463/* s_getting_focus is set when we got focus but didn't see mouse-up event yet,
464 * so don't reset s_button_pending. */
465static int s_getting_focus = FALSE;
466
467static int s_x_pending;
468static int s_y_pending;
469static UINT s_kFlags_pending;
470static UINT s_wait_timer = 0; /* Timer for get char from user */
471static int s_timed_out = FALSE;
472static int dead_key = 0; /* 0: no dead key, 1: dead key pressed */
473
474#ifdef WIN3264
475static OSVERSIONINFO os_version; /* like it says. Init in gui_mch_init() */
476#endif
477
478#ifdef FEAT_BEVAL
479/* balloon-eval WM_NOTIFY_HANDLER */
480static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
481static void TrackUserActivity(UINT uMsg);
482#endif
483
484/*
485 * For control IME.
486 *
487 * These LOGFONT used for IME.
488 */
489#ifdef FEAT_MBYTE
490# ifdef USE_IM_CONTROL
491/* holds LOGFONT for 'guifontwide' if available, otherwise 'guifont' */
492static LOGFONT norm_logfont;
493/* holds LOGFONT for 'guifont' always. */
494static LOGFONT sub_logfont;
495# endif
496#endif
497
498#ifdef FEAT_MBYTE_IME
499static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
500#endif
501
502#if defined(FEAT_BROWSE)
503static char_u *convert_filter(char_u *s);
504#endif
505
506#ifdef DEBUG_PRINT_ERROR
507/*
508 * Print out the last Windows error message
509 */
510 static void
511print_windows_error(void)
512{
513 LPVOID lpMsgBuf;
514
515 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
516 NULL, GetLastError(),
517 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
518 (LPTSTR) &lpMsgBuf, 0, NULL);
519 TRACE1("Error: %s\n", lpMsgBuf);
520 LocalFree(lpMsgBuf);
521}
522#endif
523
524/*
525 * Cursor blink functions.
526 *
527 * This is a simple state machine:
528 * BLINK_NONE not blinking at all
529 * BLINK_OFF blinking, cursor is not shown
530 * BLINK_ON blinking, cursor is shown
531 */
532
533#define BLINK_NONE 0
534#define BLINK_OFF 1
535#define BLINK_ON 2
536
537static int blink_state = BLINK_NONE;
538static long_u blink_waittime = 700;
539static long_u blink_ontime = 400;
540static long_u blink_offtime = 250;
541static UINT blink_timer = 0;
542
543 void
544gui_mch_set_blinking(long wait, long on, long off)
545{
546 blink_waittime = wait;
547 blink_ontime = on;
548 blink_offtime = off;
549}
550
551/* ARGSUSED */
552 static VOID CALLBACK
553_OnBlinkTimer(
554 HWND hwnd,
555 UINT uMsg,
556 UINT idEvent,
557 DWORD dwTime)
558{
559 MSG msg;
560
561 /*
562 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
563 */
564
565 KillTimer(NULL, idEvent);
566
567 /* Eat spurious WM_TIMER messages */
568 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
569 ;
570
571 if (blink_state == BLINK_ON)
572 {
573 gui_undraw_cursor();
574 blink_state = BLINK_OFF;
575 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
576 (TIMERPROC)_OnBlinkTimer);
577 }
578 else
579 {
580 gui_update_cursor(TRUE, FALSE);
581 blink_state = BLINK_ON;
582 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
583 (TIMERPROC)_OnBlinkTimer);
584 }
585}
586
587 static void
588gui_mswin_rm_blink_timer(void)
589{
590 MSG msg;
591
592 if (blink_timer != 0)
593 {
594 KillTimer(NULL, blink_timer);
595 /* Eat spurious WM_TIMER messages */
596 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
597 ;
598 blink_timer = 0;
599 }
600}
601
602/*
603 * Stop the cursor blinking. Show the cursor if it wasn't shown.
604 */
605 void
606gui_mch_stop_blink(void)
607{
608 gui_mswin_rm_blink_timer();
609 if (blink_state == BLINK_OFF)
610 gui_update_cursor(TRUE, FALSE);
611 blink_state = BLINK_NONE;
612}
613
614/*
615 * Start the cursor blinking. If it was already blinking, this restarts the
616 * waiting time and shows the cursor.
617 */
618 void
619gui_mch_start_blink(void)
620{
621 gui_mswin_rm_blink_timer();
622
623 /* Only switch blinking on if none of the times is zero */
624 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
625 {
626 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
627 (TIMERPROC)_OnBlinkTimer);
628 blink_state = BLINK_ON;
629 gui_update_cursor(TRUE, FALSE);
630 }
631}
632
633/*
634 * Call-back routines.
635 */
636
637/*ARGSUSED*/
638 static VOID CALLBACK
639_OnTimer(
640 HWND hwnd,
641 UINT uMsg,
642 UINT idEvent,
643 DWORD dwTime)
644{
645 MSG msg;
646
647 /*
648 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
649 */
650 KillTimer(NULL, idEvent);
651 s_timed_out = TRUE;
652
653 /* Eat spurious WM_TIMER messages */
654 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
655 ;
656 if (idEvent == s_wait_timer)
657 s_wait_timer = 0;
658}
659
660/*ARGSUSED*/
661 static void
662_OnDeadChar(
663 HWND hwnd,
664 UINT ch,
665 int cRepeat)
666{
667 dead_key = 1;
668}
669
670/*
671 * Convert Unicode character "ch" to bytes in "string[slen]".
672 * When "had_alt" is TRUE the ALT key was included in "ch".
673 * Return the length.
674 */
675 static int
676char_to_string(int ch, char_u *string, int slen, int had_alt)
677{
678 int len;
679 int i;
680#ifdef FEAT_MBYTE
681 WCHAR wstring[2];
682 char_u *ws = NULL;;
683
684 if (os_version.dwPlatformId != VER_PLATFORM_WIN32_NT)
685 {
686 /* On Windows 95/98 we apparently get the character in the active
687 * codepage, not in UCS-2. If conversion is needed convert it to
688 * UCS-2 first. */
689 if ((int)GetACP() == enc_codepage)
690 len = 0; /* no conversion required */
691 else
692 {
693 string[0] = ch;
694 len = MultiByteToWideChar(GetACP(), 0, (LPCSTR)string,
695 1, wstring, 2);
696 }
697 }
698 else
699 {
700 wstring[0] = ch;
701 len = 1;
702 }
703
704 if (len > 0)
705 {
706 /* "ch" is a UTF-16 character. Convert it to a string of bytes. When
707 * "enc_codepage" is non-zero use the standard Win32 function,
708 * otherwise use our own conversion function (e.g., for UTF-8). */
709 if (enc_codepage > 0)
710 {
711 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
712 (LPSTR)string, slen, 0, NULL);
713 /* If we had included the ALT key into the character but now the
714 * upper bit is no longer set, that probably means the conversion
715 * failed. Convert the original character and set the upper bit
716 * afterwards. */
717 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
718 {
719 wstring[0] = ch & 0x7f;
720 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
721 (LPSTR)string, slen, 0, NULL);
722 if (len == 1) /* safety check */
723 string[0] |= 0x80;
724 }
725 }
726 else
727 {
728 len = 1;
729 ws = utf16_to_enc(wstring, &len);
730 if (ws == NULL)
731 len = 0;
732 else
733 {
734 if (len > slen) /* just in case */
735 len = slen;
736 mch_memmove(string, ws, len);
737 vim_free(ws);
738 }
739 }
740 }
741
742 if (len == 0)
743#endif
744 {
745 string[0] = ch;
746 len = 1;
747 }
748
749 for (i = 0; i < len; ++i)
750 if (string[i] == CSI && len <= slen - 2)
751 {
752 /* Insert CSI as K_CSI. */
753 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
754 string[++i] = KS_EXTRA;
755 string[++i] = (int)KE_CSI;
756 len += 2;
757 }
758
759 return len;
760}
761
762/*
763 * Key hit, add it to the input buffer.
764 */
765/*ARGSUSED*/
766 static void
767_OnChar(
768 HWND hwnd,
769 UINT ch,
770 int cRepeat)
771{
772 char_u string[40];
773 int len = 0;
774
775 dead_key = 0;
776
777 len = char_to_string(ch, string, 40, FALSE);
778 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
779 {
780 trash_input_buf();
781 got_int = TRUE;
782 }
783
784 add_to_input_buf(string, len);
785}
786
787/*
788 * Alt-Key hit, add it to the input buffer.
789 */
790/*ARGSUSED*/
791 static void
792_OnSysChar(
793 HWND hwnd,
794 UINT cch,
795 int cRepeat)
796{
797 char_u string[40]; /* Enough for multibyte character */
798 int len;
799 int modifiers;
800 int ch = cch; /* special keys are negative */
801
802 dead_key = 0;
803
804 /* TRACE("OnSysChar(%d, %c)\n", ch, ch); */
805
806 /* OK, we have a character key (given by ch) which was entered with the
807 * ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
808 * that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
809 * CAPSLOCK is pressed) at this point.
810 */
811 modifiers = MOD_MASK_ALT;
812 if (GetKeyState(VK_SHIFT) & 0x8000)
813 modifiers |= MOD_MASK_SHIFT;
814 if (GetKeyState(VK_CONTROL) & 0x8000)
815 modifiers |= MOD_MASK_CTRL;
816
817 ch = simplify_key(ch, &modifiers);
818 /* remove the SHIFT modifier for keys where it's already included, e.g.,
819 * '(' and '*' */
820 if (ch < 0x100 && !isalpha(ch) && isprint(ch))
821 modifiers &= ~MOD_MASK_SHIFT;
822
823 /* Interpret the ALT key as making the key META, include SHIFT, etc. */
824 ch = extract_modifiers(ch, &modifiers);
825 if (ch == CSI)
826 ch = K_CSI;
827
828 len = 0;
829 if (modifiers)
830 {
831 string[len++] = CSI;
832 string[len++] = KS_MODIFIER;
833 string[len++] = modifiers;
834 }
835
836 if (IS_SPECIAL((int)ch))
837 {
838 string[len++] = CSI;
839 string[len++] = K_SECOND((int)ch);
840 string[len++] = K_THIRD((int)ch);
841 }
842 else
843 {
844 /* Although the documentation isn't clear about it, we assume "ch" is
845 * a Unicode character. */
846 len += char_to_string(ch, string + len, 40 - len, TRUE);
847 }
848
849 add_to_input_buf(string, len);
850}
851
852 static void
853_OnMouseEvent(
854 int button,
855 int x,
856 int y,
857 int repeated_click,
858 UINT keyFlags)
859{
860 int vim_modifiers = 0x0;
861
862 s_getting_focus = FALSE;
863
864 if (keyFlags & MK_SHIFT)
865 vim_modifiers |= MOUSE_SHIFT;
866 if (keyFlags & MK_CONTROL)
867 vim_modifiers |= MOUSE_CTRL;
868 if (GetKeyState(VK_MENU) & 0x8000)
869 vim_modifiers |= MOUSE_ALT;
870
871 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
872}
873
874/*ARGSUSED*/
875 static void
876_OnMouseButtonDown(
877 HWND hwnd,
878 BOOL fDoubleClick,
879 int x,
880 int y,
881 UINT keyFlags)
882{
883 static LONG s_prevTime = 0;
884
885 LONG currentTime = GetMessageTime();
886 int button = -1;
887 int repeated_click;
888
889 /* Give main window the focus: this is so the cursor isn't hollow. */
890 (void)SetFocus(s_hwnd);
891
892 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
893 button = MOUSE_LEFT;
894 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
895 button = MOUSE_MIDDLE;
896 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
897 button = MOUSE_RIGHT;
898 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
899 {
900#ifndef GET_XBUTTON_WPARAM
901# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
902#endif
903 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
904 }
905 else if (s_uMsg == WM_CAPTURECHANGED)
906 {
907 /* on W95/NT4, somehow you get in here with an odd Msg
908 * if you press one button while holding down the other..*/
909 if (s_button_pending == MOUSE_LEFT)
910 button = MOUSE_RIGHT;
911 else
912 button = MOUSE_LEFT;
913 }
914 if (button >= 0)
915 {
916 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
917
918 /*
919 * Holding down the left and right buttons simulates pushing the middle
920 * button.
921 */
922 if (repeated_click
923 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
924 || (button == MOUSE_RIGHT
925 && s_button_pending == MOUSE_LEFT)))
926 {
927 /*
928 * Hmm, gui.c will ignore more than one button down at a time, so
929 * pretend we let go of it first.
930 */
931 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
932 button = MOUSE_MIDDLE;
933 repeated_click = FALSE;
934 s_button_pending = -1;
935 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
936 }
937 else if ((repeated_click)
938 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
939 {
940 if (s_button_pending > -1)
941 {
942 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
943 s_button_pending = -1;
944 }
945 /* TRACE("Button down at x %d, y %d\n", x, y); */
946 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
947 }
948 else
949 {
950 /*
951 * If this is the first press (i.e. not a multiple click) don't
952 * action immediately, but store and wait for:
953 * i) button-up
954 * ii) mouse move
955 * iii) another button press
956 * before using it.
957 * This enables us to make left+right simulate middle button,
958 * without left or right being actioned first. The side-effect is
959 * that if you click and hold the mouse without dragging, the
960 * cursor doesn't move until you release the button. In practice
961 * this is hardly a problem.
962 */
963 s_button_pending = button;
964 s_x_pending = x;
965 s_y_pending = y;
966 s_kFlags_pending = keyFlags;
967 }
968
969 s_prevTime = currentTime;
970 }
971}
972
973/*ARGSUSED*/
974 static void
975_OnMouseMoveOrRelease(
976 HWND hwnd,
977 int x,
978 int y,
979 UINT keyFlags)
980{
981 int button;
982
983 s_getting_focus = FALSE;
984 if (s_button_pending > -1)
985 {
986 /* Delayed action for mouse down event */
987 _OnMouseEvent(s_button_pending, s_x_pending,
988 s_y_pending, FALSE, s_kFlags_pending);
989 s_button_pending = -1;
990 }
991 if (s_uMsg == WM_MOUSEMOVE)
992 {
993 /*
994 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
995 * down.
996 */
997 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
998 | MK_XBUTTON1 | MK_XBUTTON2)))
999 {
1000 gui_mouse_moved(x, y);
1001 return;
1002 }
1003
1004 /*
1005 * While button is down, keep grabbing mouse move events when
1006 * the mouse goes outside the window
1007 */
1008 SetCapture(s_textArea);
1009 button = MOUSE_DRAG;
1010 /* TRACE(" move at x %d, y %d\n", x, y); */
1011 }
1012 else
1013 {
1014 ReleaseCapture();
1015 button = MOUSE_RELEASE;
1016 /* TRACE(" up at x %d, y %d\n", x, y); */
1017 }
1018
1019 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1020}
1021
1022#ifdef FEAT_MENU
1023/*
1024 * Find the vimmenu_T with the given id
1025 */
1026 static vimmenu_T *
1027gui_mswin_find_menu(
1028 vimmenu_T *pMenu,
1029 int id)
1030{
1031 vimmenu_T *pChildMenu;
1032
1033 while (pMenu)
1034 {
1035 if (pMenu->id == (UINT)id)
1036 break;
1037 if (pMenu->children != NULL)
1038 {
1039 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1040 if (pChildMenu)
1041 {
1042 pMenu = pChildMenu;
1043 break;
1044 }
1045 }
1046 pMenu = pMenu->next;
1047 }
1048 return pMenu;
1049}
1050
1051/*ARGSUSED*/
1052 static void
1053_OnMenu(
1054 HWND hwnd,
1055 int id,
1056 HWND hwndCtl,
1057 UINT codeNotify)
1058{
1059 vimmenu_T *pMenu;
1060
1061 pMenu = gui_mswin_find_menu(root_menu, id);
1062 if (pMenu)
1063 gui_menu_cb(pMenu);
1064}
1065#endif
1066
1067#ifdef MSWIN_FIND_REPLACE
1068# if defined(FEAT_MBYTE) && defined(WIN3264)
1069/*
1070 * copy useful data from structure LPFINDREPLACE to structure LPFINDREPLACEW
1071 */
1072 static void
1073findrep_atow(LPFINDREPLACEW lpfrw, LPFINDREPLACE lpfr)
1074{
1075 WCHAR *wp;
1076
1077 lpfrw->hwndOwner = lpfr->hwndOwner;
1078 lpfrw->Flags = lpfr->Flags;
1079
1080 wp = enc_to_utf16((char_u *)lpfr->lpstrFindWhat, NULL);
1081 wcsncpy(lpfrw->lpstrFindWhat, wp, lpfrw->wFindWhatLen - 1);
1082 vim_free(wp);
1083
1084 /* the field "lpstrReplaceWith" doesn't need to be copied */
1085}
1086
1087/*
1088 * copy useful data from structure LPFINDREPLACEW to structure LPFINDREPLACE
1089 */
1090 static void
1091findrep_wtoa(LPFINDREPLACE lpfr, LPFINDREPLACEW lpfrw)
1092{
1093 char_u *p;
1094
1095 lpfr->Flags = lpfrw->Flags;
1096
1097 p = utf16_to_enc((short_u*)lpfrw->lpstrFindWhat, NULL);
1098 vim_strncpy((char_u *)lpfr->lpstrFindWhat, p, lpfr->wFindWhatLen - 1);
1099 vim_free(p);
1100
1101 p = utf16_to_enc((short_u*)lpfrw->lpstrReplaceWith, NULL);
1102 vim_strncpy((char_u *)lpfr->lpstrReplaceWith, p, lpfr->wReplaceWithLen - 1);
1103 vim_free(p);
1104}
1105# endif
1106
1107/*
1108 * Handle a Find/Replace window message.
1109 */
1110 static void
1111_OnFindRepl(void)
1112{
1113 int flags = 0;
1114 int down;
1115
1116# if defined(FEAT_MBYTE) && defined(WIN3264)
1117 /* If the OS is Windows NT, and 'encoding' differs from active codepage:
1118 * convert text from wide string. */
1119 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
1120 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1121 {
1122 findrep_wtoa(&s_findrep_struct, &s_findrep_struct_w);
1123 }
1124# endif
1125
1126 if (s_findrep_struct.Flags & FR_DIALOGTERM)
1127 /* Give main window the focus back. */
1128 (void)SetFocus(s_hwnd);
1129
1130 if (s_findrep_struct.Flags & FR_FINDNEXT)
1131 {
1132 flags = FRD_FINDNEXT;
1133
1134 /* Give main window the focus back: this is so the cursor isn't
1135 * hollow. */
1136 (void)SetFocus(s_hwnd);
1137 }
1138 else if (s_findrep_struct.Flags & FR_REPLACE)
1139 {
1140 flags = FRD_REPLACE;
1141
1142 /* Give main window the focus back: this is so the cursor isn't
1143 * hollow. */
1144 (void)SetFocus(s_hwnd);
1145 }
1146 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1147 {
1148 flags = FRD_REPLACEALL;
1149 }
1150
1151 if (flags != 0)
1152 {
1153 /* Call the generic GUI function to do the actual work. */
1154 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1155 flags |= FRD_WHOLE_WORD;
1156 if (s_findrep_struct.Flags & FR_MATCHCASE)
1157 flags |= FRD_MATCH_CASE;
1158 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
1159 gui_do_findrepl(flags, (char_u *)s_findrep_struct.lpstrFindWhat,
1160 (char_u *)s_findrep_struct.lpstrReplaceWith, down);
1161 }
1162}
1163#endif
1164
1165 static void
1166HandleMouseHide(UINT uMsg, LPARAM lParam)
1167{
1168 static LPARAM last_lParam = 0L;
1169
1170 /* We sometimes get a mousemove when the mouse didn't move... */
1171 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1172 {
1173 if (lParam == last_lParam)
1174 return;
1175 last_lParam = lParam;
1176 }
1177
1178 /* Handle specially, to centralise coding. We need to be sure we catch all
1179 * possible events which should cause us to restore the cursor (as it is a
1180 * shared resource, we take full responsibility for it).
1181 */
1182 switch (uMsg)
1183 {
1184 case WM_KEYUP:
1185 case WM_CHAR:
1186 /*
1187 * blank out the pointer if necessary
1188 */
1189 if (p_mh)
1190 gui_mch_mousehide(TRUE);
1191 break;
1192
1193 case WM_SYSKEYUP: /* show the pointer when a system-key is pressed */
1194 case WM_SYSCHAR:
1195 case WM_MOUSEMOVE: /* show the pointer on any mouse action */
1196 case WM_LBUTTONDOWN:
1197 case WM_LBUTTONUP:
1198 case WM_MBUTTONDOWN:
1199 case WM_MBUTTONUP:
1200 case WM_RBUTTONDOWN:
1201 case WM_RBUTTONUP:
1202 case WM_XBUTTONDOWN:
1203 case WM_XBUTTONUP:
1204 case WM_NCMOUSEMOVE:
1205 case WM_NCLBUTTONDOWN:
1206 case WM_NCLBUTTONUP:
1207 case WM_NCMBUTTONDOWN:
1208 case WM_NCMBUTTONUP:
1209 case WM_NCRBUTTONDOWN:
1210 case WM_NCRBUTTONUP:
1211 case WM_KILLFOCUS:
1212 /*
1213 * if the pointer is currently hidden, then we should show it.
1214 */
1215 gui_mch_mousehide(FALSE);
1216 break;
1217 }
1218}
1219
1220 static LRESULT CALLBACK
1221_TextAreaWndProc(
1222 HWND hwnd,
1223 UINT uMsg,
1224 WPARAM wParam,
1225 LPARAM lParam)
1226{
1227 /*
1228 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1229 hwnd, uMsg, wParam, lParam);
1230 */
1231
1232 HandleMouseHide(uMsg, lParam);
1233
1234 s_uMsg = uMsg;
1235 s_wParam = wParam;
1236 s_lParam = lParam;
1237
1238#ifdef FEAT_BEVAL
1239 TrackUserActivity(uMsg);
1240#endif
1241
1242 switch (uMsg)
1243 {
1244 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1245 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1246 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1247 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1248 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1249 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1250 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1251 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1252 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1253 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1254 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1255 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1256 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1257 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
1258
1259#ifdef FEAT_BEVAL
1260 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1261 return TRUE;
1262#endif
1263 default:
1264 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1265 }
1266}
1267
1268#if (defined(WIN3264) && defined(FEAT_MBYTE)) \
1269 || defined(GLOBAL_IME) \
1270 || defined(PROTO)
1271# ifdef PROTO
1272typedef int WINAPI;
1273# endif
1274
1275 LRESULT WINAPI
1276vim_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1277{
1278# ifdef GLOBAL_IME
1279 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
1280# else
1281 if (wide_WindowProc)
1282 return DefWindowProcW(hwnd, message, wParam, lParam);
1283 return DefWindowProc(hwnd, message, wParam, lParam);
1284#endif
1285}
1286#endif
1287
1288/*
1289 * Called when the foreground or background color has been changed.
1290 */
1291 void
1292gui_mch_new_colors(void)
1293{
1294 /* nothing to do? */
1295}
1296
1297/*
1298 * Set the colors to their default values.
1299 */
1300 void
1301gui_mch_def_colors(void)
1302{
1303 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1304 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1305 gui.def_norm_pixel = gui.norm_pixel;
1306 gui.def_back_pixel = gui.back_pixel;
1307}
1308
1309/*
1310 * Open the GUI window which was created by a call to gui_mch_init().
1311 */
1312 int
1313gui_mch_open(void)
1314{
1315#ifndef SW_SHOWDEFAULT
1316# define SW_SHOWDEFAULT 10 /* Borland 5.0 doesn't have it */
1317#endif
1318 /* Actually open the window, if not already visible
1319 * (may be done already in gui_mch_set_shellsize) */
1320 if (!IsWindowVisible(s_hwnd))
1321 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1322
1323#ifdef MSWIN_FIND_REPLACE
1324 /* Init replace string here, so that we keep it when re-opening the
1325 * dialog. */
1326 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1327#endif
1328
1329 return OK;
1330}
1331
1332/*
1333 * Get the position of the top left corner of the window.
1334 */
1335 int
1336gui_mch_get_winpos(int *x, int *y)
1337{
1338 RECT rect;
1339
1340 GetWindowRect(s_hwnd, &rect);
1341 *x = rect.left;
1342 *y = rect.top;
1343 return OK;
1344}
1345
1346/*
1347 * Set the position of the top left corner of the window to the given
1348 * coordinates.
1349 */
1350 void
1351gui_mch_set_winpos(int x, int y)
1352{
1353 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1354 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1355}
1356 void
1357gui_mch_set_text_area_pos(int x, int y, int w, int h)
1358{
1359 static int oldx = 0;
1360 static int oldy = 0;
1361
1362 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1363
1364#ifdef FEAT_TOOLBAR
1365 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1366 SendMessage(s_toolbarhwnd, WM_SIZE,
1367 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1368#endif
1369#if defined(FEAT_GUI_TABLINE)
1370 if (showing_tabline)
1371 {
1372 int top = 0;
1373 RECT rect;
1374
1375# ifdef FEAT_TOOLBAR
1376 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1377 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1378# endif
1379 GetClientRect(s_hwnd, &rect);
1380 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1381 }
1382#endif
1383
1384 /* When side scroll bar is unshown, the size of window will change.
1385 * then, the text area move left or right. thus client rect should be
1386 * forcedly redrawn. (Yasuhiro Matsumoto) */
1387 if (oldx != x || oldy != y)
1388 {
1389 InvalidateRect(s_hwnd, NULL, FALSE);
1390 oldx = x;
1391 oldy = y;
1392 }
1393}
1394
1395
1396/*
1397 * Scrollbar stuff:
1398 */
1399
1400 void
1401gui_mch_enable_scrollbar(
1402 scrollbar_T *sb,
1403 int flag)
1404{
1405 ShowScrollBar(sb->id, SB_CTL, flag);
1406
1407 /* TODO: When the window is maximized, the size of the window stays the
1408 * same, thus the size of the text area changes. On Win98 it's OK, on Win
1409 * NT 4.0 it's not... */
1410}
1411
1412 void
1413gui_mch_set_scrollbar_pos(
1414 scrollbar_T *sb,
1415 int x,
1416 int y,
1417 int w,
1418 int h)
1419{
1420 SetWindowPos(sb->id, NULL, x, y, w, h,
1421 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1422}
1423
1424 void
1425gui_mch_create_scrollbar(
1426 scrollbar_T *sb,
1427 int orient) /* SBAR_VERT or SBAR_HORIZ */
1428{
1429 sb->id = CreateWindow(
1430 "SCROLLBAR", "Scrollbar",
1431 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
1432 10, /* Any value will do for now */
1433 10, /* Any value will do for now */
1434 s_hwnd, NULL,
1435 s_hinst, NULL);
1436}
1437
1438/*
1439 * Find the scrollbar with the given hwnd.
1440 */
1441 static scrollbar_T *
1442gui_mswin_find_scrollbar(HWND hwnd)
1443{
1444 win_T *wp;
1445
1446 if (gui.bottom_sbar.id == hwnd)
1447 return &gui.bottom_sbar;
1448 FOR_ALL_WINDOWS(wp)
1449 {
1450 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1451 return &wp->w_scrollbars[SBAR_LEFT];
1452 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1453 return &wp->w_scrollbars[SBAR_RIGHT];
1454 }
1455 return NULL;
1456}
1457
1458/*
1459 * Get the character size of a font.
1460 */
1461 static void
1462GetFontSize(GuiFont font)
1463{
1464 HWND hwnd = GetDesktopWindow();
1465 HDC hdc = GetWindowDC(hwnd);
1466 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
1467 TEXTMETRIC tm;
1468
1469 GetTextMetrics(hdc, &tm);
1470 gui.char_width = tm.tmAveCharWidth + tm.tmOverhang;
1471
1472 gui.char_height = tm.tmHeight + p_linespace;
1473
1474 SelectFont(hdc, hfntOld);
1475
1476 ReleaseDC(hwnd, hdc);
1477}
1478
1479/*
1480 * Adjust gui.char_height (after 'linespace' was changed).
1481 */
1482 int
1483gui_mch_adjust_charheight(void)
1484{
1485 GetFontSize(gui.norm_font);
1486 return OK;
1487}
1488
1489 static GuiFont
1490get_font_handle(LOGFONT *lf)
1491{
1492 HFONT font = NULL;
1493
1494 /* Load the font */
1495 font = CreateFontIndirect(lf);
1496
1497 if (font == NULL)
1498 return NOFONT;
1499
1500 return (GuiFont)font;
1501}
1502
1503 static int
1504pixels_to_points(int pixels, int vertical)
1505{
1506 int points;
1507 HWND hwnd;
1508 HDC hdc;
1509
1510 hwnd = GetDesktopWindow();
1511 hdc = GetWindowDC(hwnd);
1512
1513 points = MulDiv(pixels, 72,
1514 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1515
1516 ReleaseDC(hwnd, hdc);
1517
1518 return points;
1519}
1520
1521 GuiFont
1522gui_mch_get_font(
1523 char_u *name,
1524 int giveErrorIfMissing)
1525{
1526 LOGFONT lf;
1527 GuiFont font = NOFONT;
1528
1529 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1530 font = get_font_handle(&lf);
1531 if (font == NOFONT && giveErrorIfMissing)
1532 EMSG2(_(e_font), name);
1533 return font;
1534}
1535
1536#if defined(FEAT_EVAL) || defined(PROTO)
1537/*
1538 * Return the name of font "font" in allocated memory.
1539 * Don't know how to get the actual name, thus use the provided name.
1540 */
1541/*ARGSUSED*/
1542 char_u *
1543gui_mch_get_fontname(GuiFont font, char_u *name)
1544{
1545 if (name == NULL)
1546 return NULL;
1547 return vim_strsave(name);
1548}
1549#endif
1550
1551 void
1552gui_mch_free_font(GuiFont font)
1553{
1554 if (font)
1555 DeleteObject((HFONT)font);
1556}
1557
1558 static int
1559hex_digit(int c)
1560{
1561 if (VIM_ISDIGIT(c))
1562 return c - '0';
1563 c = TOLOWER_ASC(c);
1564 if (c >= 'a' && c <= 'f')
1565 return c - 'a' + 10;
1566 return -1000;
1567}
1568/*
1569 * Return the Pixel value (color) for the given color name.
1570 * Return INVALCOLOR for error.
1571 */
1572 guicolor_T
1573gui_mch_get_color(char_u *name)
1574{
1575 typedef struct guicolor_tTable
1576 {
1577 char *name;
1578 COLORREF color;
1579 } guicolor_tTable;
1580
1581 static guicolor_tTable table[] =
1582 {
1583 {"Black", RGB(0x00, 0x00, 0x00)},
1584 {"DarkGray", RGB(0xA9, 0xA9, 0xA9)},
1585 {"DarkGrey", RGB(0xA9, 0xA9, 0xA9)},
1586 {"Gray", RGB(0xC0, 0xC0, 0xC0)},
1587 {"Grey", RGB(0xC0, 0xC0, 0xC0)},
1588 {"LightGray", RGB(0xD3, 0xD3, 0xD3)},
1589 {"LightGrey", RGB(0xD3, 0xD3, 0xD3)},
1590 {"Gray10", RGB(0x1A, 0x1A, 0x1A)},
1591 {"Grey10", RGB(0x1A, 0x1A, 0x1A)},
1592 {"Gray20", RGB(0x33, 0x33, 0x33)},
1593 {"Grey20", RGB(0x33, 0x33, 0x33)},
1594 {"Gray30", RGB(0x4D, 0x4D, 0x4D)},
1595 {"Grey30", RGB(0x4D, 0x4D, 0x4D)},
1596 {"Gray40", RGB(0x66, 0x66, 0x66)},
1597 {"Grey40", RGB(0x66, 0x66, 0x66)},
1598 {"Gray50", RGB(0x7F, 0x7F, 0x7F)},
1599 {"Grey50", RGB(0x7F, 0x7F, 0x7F)},
1600 {"Gray60", RGB(0x99, 0x99, 0x99)},
1601 {"Grey60", RGB(0x99, 0x99, 0x99)},
1602 {"Gray70", RGB(0xB3, 0xB3, 0xB3)},
1603 {"Grey70", RGB(0xB3, 0xB3, 0xB3)},
1604 {"Gray80", RGB(0xCC, 0xCC, 0xCC)},
1605 {"Grey80", RGB(0xCC, 0xCC, 0xCC)},
1606 {"Gray90", RGB(0xE5, 0xE5, 0xE5)},
1607 {"Grey90", RGB(0xE5, 0xE5, 0xE5)},
1608 {"White", RGB(0xFF, 0xFF, 0xFF)},
1609 {"DarkRed", RGB(0x80, 0x00, 0x00)},
1610 {"Red", RGB(0xFF, 0x00, 0x00)},
1611 {"LightRed", RGB(0xFF, 0xA0, 0xA0)},
1612 {"DarkBlue", RGB(0x00, 0x00, 0x80)},
1613 {"Blue", RGB(0x00, 0x00, 0xFF)},
1614 {"LightBlue", RGB(0xAD, 0xD8, 0xE6)},
1615 {"DarkGreen", RGB(0x00, 0x80, 0x00)},
1616 {"Green", RGB(0x00, 0xFF, 0x00)},
1617 {"LightGreen", RGB(0x90, 0xEE, 0x90)},
1618 {"DarkCyan", RGB(0x00, 0x80, 0x80)},
1619 {"Cyan", RGB(0x00, 0xFF, 0xFF)},
1620 {"LightCyan", RGB(0xE0, 0xFF, 0xFF)},
1621 {"DarkMagenta", RGB(0x80, 0x00, 0x80)},
1622 {"Magenta", RGB(0xFF, 0x00, 0xFF)},
1623 {"LightMagenta", RGB(0xFF, 0xA0, 0xFF)},
1624 {"Brown", RGB(0x80, 0x40, 0x40)},
1625 {"Yellow", RGB(0xFF, 0xFF, 0x00)},
1626 {"LightYellow", RGB(0xFF, 0xFF, 0xE0)},
1627 {"DarkYellow", RGB(0xBB, 0xBB, 0x00)},
1628 {"SeaGreen", RGB(0x2E, 0x8B, 0x57)},
1629 {"Orange", RGB(0xFF, 0xA5, 0x00)},
1630 {"Purple", RGB(0xA0, 0x20, 0xF0)},
1631 {"SlateBlue", RGB(0x6A, 0x5A, 0xCD)},
1632 {"Violet", RGB(0xEE, 0x82, 0xEE)},
1633 };
1634
1635 typedef struct SysColorTable
1636 {
1637 char *name;
1638 int color;
1639 } SysColorTable;
1640
1641 static SysColorTable sys_table[] =
1642 {
1643#ifdef WIN3264
1644 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1645 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
1646#ifndef __MINGW32__
1647 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1648#endif
1649 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1650 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1651 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1652 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1653 {"SYS_DESKTOP", COLOR_DESKTOP},
1654 {"SYS_INFOBK", COLOR_INFOBK},
1655 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1656 {"SYS_3DFACE", COLOR_3DFACE},
1657#endif
1658 {"SYS_BTNFACE", COLOR_BTNFACE},
1659 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1660 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1661 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1662 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1663 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1664 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1665 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1666 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1667 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1668 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1669 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1670 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1671 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1672 {"SYS_MENU", COLOR_MENU},
1673 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1674 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1675 {"SYS_WINDOW", COLOR_WINDOW},
1676 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1677 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1678 };
1679
1680 int r, g, b;
1681 int i;
1682
1683 if (name[0] == '#' && STRLEN(name) == 7)
1684 {
1685 /* Name is in "#rrggbb" format */
1686 r = hex_digit(name[1]) * 16 + hex_digit(name[2]);
1687 g = hex_digit(name[3]) * 16 + hex_digit(name[4]);
1688 b = hex_digit(name[5]) * 16 + hex_digit(name[6]);
1689 if (r < 0 || g < 0 || b < 0)
1690 return INVALCOLOR;
1691 return RGB(r, g, b);
1692 }
1693 else
1694 {
1695 /* Check if the name is one of the colors we know */
1696 for (i = 0; i < sizeof(table) / sizeof(table[0]); i++)
1697 if (STRICMP(name, table[i].name) == 0)
1698 return table[i].color;
1699 }
1700
1701 /*
1702 * Try to look up a system colour.
1703 */
1704 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1705 if (STRICMP(name, sys_table[i].name) == 0)
1706 return GetSysColor(sys_table[i].color);
1707
1708 /*
1709 * Last attempt. Look in the file "$VIMRUNTIME/rgb.txt".
1710 */
1711 {
1712#define LINE_LEN 100
1713 FILE *fd;
1714 char line[LINE_LEN];
1715 char_u *fname;
1716
1717 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
1718 if (fname == NULL)
1719 return INVALCOLOR;
1720
1721 fd = mch_fopen((char *)fname, "rt");
1722 vim_free(fname);
1723 if (fd == NULL)
1724 return INVALCOLOR;
1725
1726 while (!feof(fd))
1727 {
1728 int len;
1729 int pos;
1730 char *color;
1731
1732 fgets(line, LINE_LEN, fd);
1733 len = (int)STRLEN(line);
1734
1735 if (len <= 1 || line[len-1] != '\n')
1736 continue;
1737
1738 line[len-1] = '\0';
1739
1740 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
1741 if (i != 3)
1742 continue;
1743
1744 color = line + pos;
1745
1746 if (STRICMP(color, name) == 0)
1747 {
1748 fclose(fd);
1749 return (guicolor_T) RGB(r, g, b);
1750 }
1751 }
1752
1753 fclose(fd);
1754 }
1755
1756 return INVALCOLOR;
1757}
1758/*
1759 * Return OK if the key with the termcap name "name" is supported.
1760 */
1761 int
1762gui_mch_haskey(char_u *name)
1763{
1764 int i;
1765
1766 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1767 if (name[0] == special_keys[i].vim_code0 &&
1768 name[1] == special_keys[i].vim_code1)
1769 return OK;
1770 return FAIL;
1771}
1772
1773 void
1774gui_mch_beep(void)
1775{
1776 MessageBeep(MB_OK);
1777}
1778/*
1779 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1780 */
1781 void
1782gui_mch_invert_rectangle(
1783 int r,
1784 int c,
1785 int nr,
1786 int nc)
1787{
1788 RECT rc;
1789
1790 /*
1791 * Note: InvertRect() excludes right and bottom of rectangle.
1792 */
1793 rc.left = FILL_X(c);
1794 rc.top = FILL_Y(r);
1795 rc.right = rc.left + nc * gui.char_width;
1796 rc.bottom = rc.top + nr * gui.char_height;
1797 InvertRect(s_hdc, &rc);
1798}
1799
1800/*
1801 * Iconify the GUI window.
1802 */
1803 void
1804gui_mch_iconify(void)
1805{
1806 ShowWindow(s_hwnd, SW_MINIMIZE);
1807}
1808
1809/*
1810 * Draw a cursor without focus.
1811 */
1812 void
1813gui_mch_draw_hollow_cursor(guicolor_T color)
1814{
1815 HBRUSH hbr;
1816 RECT rc;
1817
1818 /*
1819 * Note: FrameRect() excludes right and bottom of rectangle.
1820 */
1821 rc.left = FILL_X(gui.col);
1822 rc.top = FILL_Y(gui.row);
1823 rc.right = rc.left + gui.char_width;
1824#ifdef FEAT_MBYTE
1825 if (mb_lefthalve(gui.row, gui.col))
1826 rc.right += gui.char_width;
1827#endif
1828 rc.bottom = rc.top + gui.char_height;
1829 hbr = CreateSolidBrush(color);
1830 FrameRect(s_hdc, &rc, hbr);
1831 DeleteBrush(hbr);
1832}
1833/*
1834 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1835 * color "color".
1836 */
1837 void
1838gui_mch_draw_part_cursor(
1839 int w,
1840 int h,
1841 guicolor_T color)
1842{
1843 HBRUSH hbr;
1844 RECT rc;
1845
1846 /*
1847 * Note: FillRect() excludes right and bottom of rectangle.
1848 */
1849 rc.left =
1850#ifdef FEAT_RIGHTLEFT
1851 /* vertical line should be on the right of current point */
1852 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1853#endif
1854 FILL_X(gui.col);
1855 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1856 rc.right = rc.left + w;
1857 rc.bottom = rc.top + h;
1858 hbr = CreateSolidBrush(color);
1859 FillRect(s_hdc, &rc, hbr);
1860 DeleteBrush(hbr);
1861}
1862
1863
1864/*
1865 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1866 * dead key's nominal character and re-post the original message.
1867 */
1868 static void
1869outputDeadKey_rePost(MSG originalMsg)
1870{
1871 static MSG deadCharExpel;
1872
1873 if (!dead_key)
1874 return;
1875
1876 dead_key = 0;
1877
1878 /* Make Windows generate the dead key's character */
1879 deadCharExpel.message = originalMsg.message;
1880 deadCharExpel.hwnd = originalMsg.hwnd;
1881 deadCharExpel.wParam = VK_SPACE;
1882
1883 MyTranslateMessage(&deadCharExpel);
1884
1885 /* re-generate the current character free of the dead char influence */
1886 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1887 originalMsg.lParam);
1888}
1889
1890
1891/*
1892 * Process a single Windows message.
1893 * If one is not available we hang until one is.
1894 */
1895 static void
1896process_message(void)
1897{
1898 MSG msg;
1899 UINT vk = 0; /* Virtual key */
1900 char_u string[40];
1901 int i;
1902 int modifiers = 0;
1903 int key;
1904#ifdef FEAT_MENU
1905 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1906#endif
1907
1908 pGetMessage(&msg, NULL, 0, 0);
1909
1910#ifdef FEAT_OLE
1911 /* Look after OLE Automation commands */
1912 if (msg.message == WM_OLE)
1913 {
1914 char_u *str = (char_u *)msg.lParam;
1915 if (str == NULL || *str == NUL)
1916 {
1917 /* Message can't be ours, forward it. Fixes problem with Ultramon
1918 * 3.0.4 */
1919 pDispatchMessage(&msg);
1920 }
1921 else
1922 {
1923 add_to_input_buf(str, (int)STRLEN(str));
1924 vim_free(str); /* was allocated in CVim::SendKeys() */
1925 }
1926 return;
1927 }
1928#endif
1929
1930#ifdef FEAT_CHANNEL
1931 if (msg.message == WM_NETBEANS)
1932 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001933 int part;
1934 channel_T *channel = channel_fd2channel((sock_T)msg.wParam, &part);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001935
1936 if (channel != NULL)
1937 {
1938 /* Disable error messages, they can mess up the display and throw
1939 * an exception. */
1940 ++emsg_off;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001941 channel_read(channel, part, "process_message");
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001942 --emsg_off;
1943 }
1944 return;
1945 }
1946#endif
1947
1948#ifdef FEAT_SNIFF
1949 if (sniff_request_waiting && want_sniff_request)
1950 {
1951 static char_u bytes[3] = {CSI, (char_u)KS_EXTRA, (char_u)KE_SNIFF};
1952 add_to_input_buf(bytes, 3); /* K_SNIFF */
1953 sniff_request_waiting = 0;
1954 want_sniff_request = 0;
1955 /* request is handled in normal.c */
1956 }
1957 if (msg.message == WM_USER)
1958 {
1959 MyTranslateMessage(&msg);
1960 pDispatchMessage(&msg);
1961 return;
1962 }
1963#endif
1964
1965#ifdef MSWIN_FIND_REPLACE
1966 /* Don't process messages used by the dialog */
1967 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1968 {
1969 HandleMouseHide(msg.message, msg.lParam);
1970 return;
1971 }
1972#endif
1973
1974 /*
1975 * Check if it's a special key that we recognise. If not, call
1976 * TranslateMessage().
1977 */
1978 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1979 {
1980 vk = (int) msg.wParam;
1981
1982 /*
1983 * Handle dead keys in special conditions in other cases we let Windows
1984 * handle them and do not interfere.
1985 *
1986 * The dead_key flag must be reset on several occasions:
1987 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1988 * consumed at that point (This is when we let Windows combine the
1989 * dead character on its own)
1990 *
1991 * - Before doing something special such as regenerating keypresses to
1992 * expel the dead character as this could trigger an infinite loop if
1993 * for some reason MyTranslateMessage() do not trigger a call
1994 * immediately to _OnChar() (or _OnSysChar()).
1995 */
1996 if (dead_key)
1997 {
1998 /*
1999 * If a dead key was pressed and the user presses VK_SPACE,
2000 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
2001 * with the dead char now, so do nothing special and let Windows
2002 * handle it.
2003 *
2004 * Note that VK_SPACE combines with the dead_key's character and
2005 * only one WM_CHAR will be generated by TranslateMessage(), in
2006 * the two other cases two WM_CHAR will be generated: the dead
2007 * char and VK_BACK or VK_ESCAPE. That is most likely what the
2008 * user expects.
2009 */
2010 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
2011 {
2012 dead_key = 0;
2013 MyTranslateMessage(&msg);
2014 return;
2015 }
2016 /* In modes where we are not typing, dead keys should behave
2017 * normally */
2018 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
2019 {
2020 outputDeadKey_rePost(msg);
2021 return;
2022 }
2023 }
2024
2025 /* Check for CTRL-BREAK */
2026 if (vk == VK_CANCEL)
2027 {
2028 trash_input_buf();
2029 got_int = TRUE;
2030 string[0] = Ctrl_C;
2031 add_to_input_buf(string, 1);
2032 }
2033
2034 for (i = 0; special_keys[i].key_sym != 0; i++)
2035 {
2036 /* ignore VK_SPACE when ALT key pressed: system menu */
2037 if (special_keys[i].key_sym == vk
2038 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
2039 {
2040 /*
2041 * Behave as exected if we have a dead key and the special key
2042 * is a key that would normally trigger the dead key nominal
2043 * character output (such as a NUMPAD printable character or
2044 * the TAB key, etc...).
2045 */
2046 if (dead_key && (special_keys[i].vim_code0 == 'K'
2047 || vk == VK_TAB || vk == CAR))
2048 {
2049 outputDeadKey_rePost(msg);
2050 return;
2051 }
2052
2053#ifdef FEAT_MENU
2054 /* Check for <F10>: Windows selects the menu. When <F10> is
2055 * mapped we want to use the mapping instead. */
2056 if (vk == VK_F10
2057 && gui.menu_is_active
2058 && check_map(k10, State, FALSE, TRUE, FALSE,
2059 NULL, NULL) == NULL)
2060 break;
2061#endif
2062 if (GetKeyState(VK_SHIFT) & 0x8000)
2063 modifiers |= MOD_MASK_SHIFT;
2064 /*
2065 * Don't use caps-lock as shift, because these are special keys
2066 * being considered here, and we only want letters to get
2067 * shifted -- webb
2068 */
2069 /*
2070 if (GetKeyState(VK_CAPITAL) & 0x0001)
2071 modifiers ^= MOD_MASK_SHIFT;
2072 */
2073 if (GetKeyState(VK_CONTROL) & 0x8000)
2074 modifiers |= MOD_MASK_CTRL;
2075 if (GetKeyState(VK_MENU) & 0x8000)
2076 modifiers |= MOD_MASK_ALT;
2077
2078 if (special_keys[i].vim_code1 == NUL)
2079 key = special_keys[i].vim_code0;
2080 else
2081 key = TO_SPECIAL(special_keys[i].vim_code0,
2082 special_keys[i].vim_code1);
2083 key = simplify_key(key, &modifiers);
2084 if (key == CSI)
2085 key = K_CSI;
2086
2087 if (modifiers)
2088 {
2089 string[0] = CSI;
2090 string[1] = KS_MODIFIER;
2091 string[2] = modifiers;
2092 add_to_input_buf(string, 3);
2093 }
2094
2095 if (IS_SPECIAL(key))
2096 {
2097 string[0] = CSI;
2098 string[1] = K_SECOND(key);
2099 string[2] = K_THIRD(key);
2100 add_to_input_buf(string, 3);
2101 }
2102 else
2103 {
2104 int len;
2105
2106 /* Handle "key" as a Unicode character. */
2107 len = char_to_string(key, string, 40, FALSE);
2108 add_to_input_buf(string, len);
2109 }
2110 break;
2111 }
2112 }
2113 if (special_keys[i].key_sym == 0)
2114 {
2115 /* Some keys need C-S- where they should only need C-.
2116 * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
2117 * system startup (Helmut Stiegler, 2003 Oct 3). */
2118 if (vk != 0xff
2119 && (GetKeyState(VK_CONTROL) & 0x8000)
2120 && !(GetKeyState(VK_SHIFT) & 0x8000)
2121 && !(GetKeyState(VK_MENU) & 0x8000))
2122 {
2123 /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */
2124 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
2125 {
2126 string[0] = Ctrl_HAT;
2127 add_to_input_buf(string, 1);
2128 }
2129 /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */
2130 else if (vk == 0xBD) /* QWERTY for CTRL-'-' */
2131 {
2132 string[0] = Ctrl__;
2133 add_to_input_buf(string, 1);
2134 }
2135 /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */
2136 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2137 {
2138 string[0] = Ctrl_AT;
2139 add_to_input_buf(string, 1);
2140 }
2141 else
2142 MyTranslateMessage(&msg);
2143 }
2144 else
2145 MyTranslateMessage(&msg);
2146 }
2147 }
2148#ifdef FEAT_MBYTE_IME
2149 else if (msg.message == WM_IME_NOTIFY)
2150 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2151 else if (msg.message == WM_KEYUP && im_get_status())
2152 /* added for non-MS IME (Yasuhiro Matsumoto) */
2153 MyTranslateMessage(&msg);
2154#endif
2155#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
2156/* GIME_TEST */
2157 else if (msg.message == WM_IME_STARTCOMPOSITION)
2158 {
2159 POINT point;
2160
2161 global_ime_set_font(&norm_logfont);
2162 point.x = FILL_X(gui.col);
2163 point.y = FILL_Y(gui.row);
2164 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
2165 global_ime_set_position(&point);
2166 }
2167#endif
2168
2169#ifdef FEAT_MENU
2170 /* Check for <F10>: Default effect is to select the menu. When <F10> is
2171 * mapped we need to stop it here to avoid strange effects (e.g., for the
2172 * key-up event) */
2173 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2174 NULL, NULL) == NULL)
2175#endif
2176 pDispatchMessage(&msg);
2177}
2178
2179/*
2180 * Catch up with any queued events. This may put keyboard input into the
2181 * input buffer, call resize call-backs, trigger timers etc. If there is
2182 * nothing in the event queue (& no timers pending), then we return
2183 * immediately.
2184 */
2185 void
2186gui_mch_update(void)
2187{
2188 MSG msg;
2189
2190 if (!s_busy_processing)
2191 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2192 && !vim_is_input_buf_full())
2193 process_message();
2194}
2195
2196/*
2197 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2198 * from the keyboard.
2199 * wtime == -1 Wait forever.
2200 * wtime == 0 This should never happen.
2201 * wtime > 0 Wait wtime milliseconds for a character.
2202 * Returns OK if a character was found to be available within the given time,
2203 * or FAIL otherwise.
2204 */
2205 int
2206gui_mch_wait_for_chars(int wtime)
2207{
2208 MSG msg;
2209 int focus;
2210
2211 s_timed_out = FALSE;
2212
2213 if (wtime > 0)
2214 {
2215 /* Don't do anything while processing a (scroll) message. */
2216 if (s_busy_processing)
2217 return FAIL;
2218 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)wtime,
2219 (TIMERPROC)_OnTimer);
2220 }
2221
2222 allow_scrollbar = TRUE;
2223
2224 focus = gui.in_focus;
2225 while (!s_timed_out)
2226 {
2227 /* Stop or start blinking when focus changes */
2228 if (gui.in_focus != focus)
2229 {
2230 if (gui.in_focus)
2231 gui_mch_start_blink();
2232 else
2233 gui_mch_stop_blink();
2234 focus = gui.in_focus;
2235 }
2236
2237 if (s_need_activate)
2238 {
2239#ifdef WIN32
2240 (void)SetForegroundWindow(s_hwnd);
2241#else
2242 (void)SetActiveWindow(s_hwnd);
2243#endif
2244 s_need_activate = FALSE;
2245 }
2246
2247#ifdef MESSAGE_QUEUE
2248 parse_queued_messages();
2249#endif
2250
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002251 /*
2252 * Don't use gui_mch_update() because then we will spin-lock until a
2253 * char arrives, instead we use GetMessage() to hang until an
2254 * event arrives. No need to check for input_buf_full because we are
2255 * returning as soon as it contains a single char -- webb
2256 */
2257 process_message();
2258
2259 if (input_available())
2260 {
2261 if (s_wait_timer != 0 && !s_timed_out)
2262 {
2263 KillTimer(NULL, s_wait_timer);
2264
2265 /* Eat spurious WM_TIMER messages */
2266 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2267 ;
2268 s_wait_timer = 0;
2269 }
2270 allow_scrollbar = FALSE;
2271
2272 /* Clear pending mouse button, the release event may have been
2273 * taken by the dialog window. But don't do this when getting
2274 * focus, we need the mouse-up event then. */
2275 if (!s_getting_focus)
2276 s_button_pending = -1;
2277
2278 return OK;
2279 }
2280 }
2281 allow_scrollbar = FALSE;
2282 return FAIL;
2283}
2284
2285/*
2286 * Clear a rectangular region of the screen from text pos (row1, col1) to
2287 * (row2, col2) inclusive.
2288 */
2289 void
2290gui_mch_clear_block(
2291 int row1,
2292 int col1,
2293 int row2,
2294 int col2)
2295{
2296 RECT rc;
2297
2298 /*
2299 * Clear one extra pixel at the far right, for when bold characters have
2300 * spilled over to the window border.
2301 * Note: FillRect() excludes right and bottom of rectangle.
2302 */
2303 rc.left = FILL_X(col1);
2304 rc.top = FILL_Y(row1);
2305 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2306 rc.bottom = FILL_Y(row2 + 1);
2307 clear_rect(&rc);
2308}
2309
2310/*
2311 * Clear the whole text window.
2312 */
2313 void
2314gui_mch_clear_all(void)
2315{
2316 RECT rc;
2317
2318 rc.left = 0;
2319 rc.top = 0;
2320 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2321 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2322 clear_rect(&rc);
2323}
2324/*
2325 * Menu stuff.
2326 */
2327
2328 void
2329gui_mch_enable_menu(int flag)
2330{
2331#ifdef FEAT_MENU
2332 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2333#endif
2334}
2335
2336/*ARGSUSED*/
2337 void
2338gui_mch_set_menu_pos(
2339 int x,
2340 int y,
2341 int w,
2342 int h)
2343{
2344 /* It will be in the right place anyway */
2345}
2346
2347#if defined(FEAT_MENU) || defined(PROTO)
2348/*
2349 * Make menu item hidden or not hidden
2350 */
2351 void
2352gui_mch_menu_hidden(
2353 vimmenu_T *menu,
2354 int hidden)
2355{
2356 /*
2357 * This doesn't do what we want. Hmm, just grey the menu items for now.
2358 */
2359 /*
2360 if (hidden)
2361 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2362 else
2363 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2364 */
2365 gui_mch_menu_grey(menu, hidden);
2366}
2367
2368/*
2369 * This is called after setting all the menus to grey/hidden or not.
2370 */
2371 void
2372gui_mch_draw_menubar(void)
2373{
2374 DrawMenuBar(s_hwnd);
2375}
2376#endif /*FEAT_MENU*/
2377
2378#ifndef PROTO
2379void
2380#ifdef VIMDLL
2381_export
2382#endif
2383_cdecl
2384SaveInst(HINSTANCE hInst)
2385{
2386 s_hinst = hInst;
2387}
2388#endif
2389
2390/*
2391 * Return the RGB value of a pixel as a long.
2392 */
2393 long_u
2394gui_mch_get_rgb(guicolor_T pixel)
2395{
2396 return (GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2397 + GetBValue(pixel);
2398}
2399
2400#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2401/* Convert pixels in X to dialog units */
2402 static WORD
2403PixelToDialogX(int numPixels)
2404{
2405 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2406}
2407
2408/* Convert pixels in Y to dialog units */
2409 static WORD
2410PixelToDialogY(int numPixels)
2411{
2412 return (WORD)((numPixels * 8) / s_dlgfntheight);
2413}
2414
2415/* Return the width in pixels of the given text in the given DC. */
2416 static int
2417GetTextWidth(HDC hdc, char_u *str, int len)
2418{
2419 SIZE size;
2420
2421 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2422 return size.cx;
2423}
2424
2425#ifdef FEAT_MBYTE
2426/*
2427 * Return the width in pixels of the given text in the given DC, taking care
2428 * of 'encoding' to active codepage conversion.
2429 */
2430 static int
2431GetTextWidthEnc(HDC hdc, char_u *str, int len)
2432{
2433 SIZE size;
2434 WCHAR *wstr;
2435 int n;
2436 int wlen = len;
2437
2438 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2439 {
2440 /* 'encoding' differs from active codepage: convert text and use wide
2441 * function */
2442 wstr = enc_to_utf16(str, &wlen);
2443 if (wstr != NULL)
2444 {
2445 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2446 vim_free(wstr);
2447 if (n)
2448 return size.cx;
2449 }
2450 }
2451
2452 return GetTextWidth(hdc, str, len);
2453}
2454#else
2455# define GetTextWidthEnc(h, s, l) GetTextWidth((h), (s), (l))
2456#endif
2457
2458/*
2459 * A quick little routine that will center one window over another, handy for
2460 * dialog boxes. Taken from the Win32SDK samples.
2461 */
2462 static BOOL
2463CenterWindow(
2464 HWND hwndChild,
2465 HWND hwndParent)
2466{
2467 RECT rChild, rParent;
2468 int wChild, hChild, wParent, hParent;
2469 int wScreen, hScreen, xNew, yNew;
2470 HDC hdc;
2471
2472 GetWindowRect(hwndChild, &rChild);
2473 wChild = rChild.right - rChild.left;
2474 hChild = rChild.bottom - rChild.top;
2475
2476 /* If Vim is minimized put the window in the middle of the screen. */
2477 if (hwndParent == NULL || IsMinimized(hwndParent))
2478 SystemParametersInfo(SPI_GETWORKAREA, 0, &rParent, 0);
2479 else
2480 GetWindowRect(hwndParent, &rParent);
2481 wParent = rParent.right - rParent.left;
2482 hParent = rParent.bottom - rParent.top;
2483
2484 hdc = GetDC(hwndChild);
2485 wScreen = GetDeviceCaps (hdc, HORZRES);
2486 hScreen = GetDeviceCaps (hdc, VERTRES);
2487 ReleaseDC(hwndChild, hdc);
2488
2489 xNew = rParent.left + ((wParent - wChild) /2);
2490 if (xNew < 0)
2491 {
2492 xNew = 0;
2493 }
2494 else if ((xNew+wChild) > wScreen)
2495 {
2496 xNew = wScreen - wChild;
2497 }
2498
2499 yNew = rParent.top + ((hParent - hChild) /2);
2500 if (yNew < 0)
2501 yNew = 0;
2502 else if ((yNew+hChild) > hScreen)
2503 yNew = hScreen - hChild;
2504
2505 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2506 SWP_NOSIZE | SWP_NOZORDER);
2507}
2508#endif /* FEAT_GUI_DIALOG */
2509
2510void
2511gui_mch_activate_window(void)
2512{
2513 (void)SetActiveWindow(s_hwnd);
2514}
2515
2516#if defined(FEAT_TOOLBAR) || defined(PROTO)
2517 void
2518gui_mch_show_toolbar(int showit)
2519{
2520 if (s_toolbarhwnd == NULL)
2521 return;
2522
2523 if (showit)
2524 {
2525# ifdef FEAT_MBYTE
2526# ifndef TB_SETUNICODEFORMAT
2527 /* For older compilers. We assume this never changes. */
2528# define TB_SETUNICODEFORMAT 0x2005
2529# endif
2530 /* Enable/disable unicode support */
2531 int uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2532 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2533# endif
2534 ShowWindow(s_toolbarhwnd, SW_SHOW);
2535 }
2536 else
2537 ShowWindow(s_toolbarhwnd, SW_HIDE);
2538}
2539
2540/* Then number of bitmaps is fixed. Exit is missing! */
2541#define TOOLBAR_BITMAP_COUNT 31
2542
2543#endif
2544
2545#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2546 static void
2547add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2548{
2549#ifdef FEAT_MBYTE
2550 WCHAR *wn = NULL;
2551 int n;
2552
2553 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2554 {
2555 /* 'encoding' differs from active codepage: convert menu name
2556 * and use wide function */
2557 wn = enc_to_utf16(item_text, NULL);
2558 if (wn != NULL)
2559 {
2560 MENUITEMINFOW infow;
2561
2562 infow.cbSize = sizeof(infow);
2563 infow.fMask = MIIM_TYPE | MIIM_ID;
2564 infow.wID = item_id;
2565 infow.fType = MFT_STRING;
2566 infow.dwTypeData = wn;
2567 infow.cch = (UINT)wcslen(wn);
2568 n = InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2569 vim_free(wn);
2570 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2571 /* Failed, try using non-wide function. */
2572 wn = NULL;
2573 }
2574 }
2575
2576 if (wn == NULL)
2577#endif
2578 {
2579 MENUITEMINFO info;
2580
2581 info.cbSize = sizeof(info);
2582 info.fMask = MIIM_TYPE | MIIM_ID;
2583 info.wID = item_id;
2584 info.fType = MFT_STRING;
2585 info.dwTypeData = (LPTSTR)item_text;
2586 info.cch = (UINT)STRLEN(item_text);
2587 InsertMenuItem(pmenu, item_id, FALSE, &info);
2588 }
2589}
2590
2591 static void
2592show_tabline_popup_menu(void)
2593{
2594 HMENU tab_pmenu;
2595 long rval;
2596 POINT pt;
2597
2598 /* When ignoring events don't show the menu. */
2599 if (hold_gui_events
2600# ifdef FEAT_CMDWIN
2601 || cmdwin_type != 0
2602# endif
2603 )
2604 return;
2605
2606 tab_pmenu = CreatePopupMenu();
2607 if (tab_pmenu == NULL)
2608 return;
2609
2610 if (first_tabpage->tp_next != NULL)
2611 add_tabline_popup_menu_entry(tab_pmenu,
2612 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2613 add_tabline_popup_menu_entry(tab_pmenu,
2614 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2615 add_tabline_popup_menu_entry(tab_pmenu,
2616 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2617
2618 GetCursorPos(&pt);
2619 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2620 NULL);
2621
2622 DestroyMenu(tab_pmenu);
2623
2624 /* Add the string cmd into input buffer */
2625 if (rval > 0)
2626 {
2627 TCHITTESTINFO htinfo;
2628 int idx;
2629
2630 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2631 return;
2632
2633 htinfo.pt.x = pt.x;
2634 htinfo.pt.y = pt.y;
2635 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2636 if (idx == -1)
2637 idx = 0;
2638 else
2639 idx += 1;
2640
2641 send_tabline_menu_event(idx, (int)rval);
2642 }
2643}
2644
2645/*
2646 * Show or hide the tabline.
2647 */
2648 void
2649gui_mch_show_tabline(int showit)
2650{
2651 if (s_tabhwnd == NULL)
2652 return;
2653
2654 if (!showit != !showing_tabline)
2655 {
2656 if (showit)
2657 ShowWindow(s_tabhwnd, SW_SHOW);
2658 else
2659 ShowWindow(s_tabhwnd, SW_HIDE);
2660 showing_tabline = showit;
2661 }
2662}
2663
2664/*
2665 * Return TRUE when tabline is displayed.
2666 */
2667 int
2668gui_mch_showing_tabline(void)
2669{
2670 return s_tabhwnd != NULL && showing_tabline;
2671}
2672
2673/*
2674 * Update the labels of the tabline.
2675 */
2676 void
2677gui_mch_update_tabline(void)
2678{
2679 tabpage_T *tp;
2680 TCITEM tie;
2681 int nr = 0;
2682 int curtabidx = 0;
2683 int tabadded = 0;
2684#ifdef FEAT_MBYTE
2685 static int use_unicode = FALSE;
2686 int uu;
2687 WCHAR *wstr = NULL;
2688#endif
2689
2690 if (s_tabhwnd == NULL)
2691 return;
2692
2693#if defined(FEAT_MBYTE)
2694# ifndef CCM_SETUNICODEFORMAT
2695 /* For older compilers. We assume this never changes. */
2696# define CCM_SETUNICODEFORMAT 0x2005
2697# endif
2698 uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2699 if (uu != use_unicode)
2700 {
2701 /* Enable/disable unicode support */
2702 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2703 use_unicode = uu;
2704 }
2705#endif
2706
2707 tie.mask = TCIF_TEXT;
2708 tie.iImage = -1;
2709
2710 /* Disable redraw for tab updates to eliminate O(N^2) draws. */
2711 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2712
2713 /* Add a label for each tab page. They all contain the same text area. */
2714 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2715 {
2716 if (tp == curtab)
2717 curtabidx = nr;
2718
2719 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2720 {
2721 /* Add the tab */
2722 tie.pszText = "-Empty-";
2723 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2724 tabadded = 1;
2725 }
2726
2727 get_tabline_label(tp, FALSE);
2728 tie.pszText = (LPSTR)NameBuff;
2729#ifdef FEAT_MBYTE
2730 wstr = NULL;
2731 if (use_unicode)
2732 {
2733 /* Need to go through Unicode. */
2734 wstr = enc_to_utf16(NameBuff, NULL);
2735 if (wstr != NULL)
2736 {
2737 TCITEMW tiw;
2738
2739 tiw.mask = TCIF_TEXT;
2740 tiw.iImage = -1;
2741 tiw.pszText = wstr;
2742 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2743 vim_free(wstr);
2744 }
2745 }
2746 if (wstr == NULL)
2747#endif
2748 {
2749 TabCtrl_SetItem(s_tabhwnd, nr, &tie);
2750 }
2751 }
2752
2753 /* Remove any old labels. */
2754 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2755 TabCtrl_DeleteItem(s_tabhwnd, nr);
2756
2757 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2758 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2759
2760 /* Re-enable redraw and redraw. */
2761 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2762 RedrawWindow(s_tabhwnd, NULL, NULL,
2763 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2764
2765 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2766 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2767}
2768
2769/*
2770 * Set the current tab to "nr". First tab is 1.
2771 */
2772 void
2773gui_mch_set_curtab(int nr)
2774{
2775 if (s_tabhwnd == NULL)
2776 return;
2777
2778 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2779 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2780}
2781
2782#endif
2783
2784/*
2785 * ":simalt" command.
2786 */
2787 void
2788ex_simalt(exarg_T *eap)
2789{
2790 char_u *keys = eap->arg;
2791
2792 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2793 while (*keys)
2794 {
2795 if (*keys == '~')
2796 *keys = ' '; /* for showing system menu */
2797 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2798 keys++;
2799 }
2800}
2801
2802/*
2803 * Create the find & replace dialogs.
2804 * You can't have both at once: ":find" when replace is showing, destroys
2805 * the replace dialog first, and the other way around.
2806 */
2807#ifdef MSWIN_FIND_REPLACE
2808 static void
2809initialise_findrep(char_u *initial_string)
2810{
2811 int wword = FALSE;
2812 int mcase = !p_ic;
2813 char_u *entry_text;
2814
2815 /* Get the search string to use. */
2816 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2817
2818 s_findrep_struct.hwndOwner = s_hwnd;
2819 s_findrep_struct.Flags = FR_DOWN;
2820 if (mcase)
2821 s_findrep_struct.Flags |= FR_MATCHCASE;
2822 if (wword)
2823 s_findrep_struct.Flags |= FR_WHOLEWORD;
2824 if (entry_text != NULL && *entry_text != NUL)
2825 vim_strncpy((char_u *)s_findrep_struct.lpstrFindWhat, entry_text,
2826 s_findrep_struct.wFindWhatLen - 1);
2827 vim_free(entry_text);
2828}
2829#endif
2830
2831 static void
2832set_window_title(HWND hwnd, char *title)
2833{
2834#ifdef FEAT_MBYTE
2835 if (title != NULL && enc_codepage >= 0 && enc_codepage != (int)GetACP())
2836 {
2837 WCHAR *wbuf;
2838 int n;
2839
2840 /* Convert the title from 'encoding' to UTF-16. */
2841 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2842 if (wbuf != NULL)
2843 {
2844 n = SetWindowTextW(hwnd, wbuf);
2845 vim_free(wbuf);
2846 if (n != 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
2847 return;
2848 /* Retry with non-wide function (for Windows 98). */
2849 }
2850 }
2851#endif
2852 (void)SetWindowText(hwnd, (LPCSTR)title);
2853}
2854
2855 void
2856gui_mch_find_dialog(exarg_T *eap)
2857{
2858#ifdef MSWIN_FIND_REPLACE
2859 if (s_findrep_msg != 0)
2860 {
2861 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2862 DestroyWindow(s_findrep_hwnd);
2863
2864 if (!IsWindow(s_findrep_hwnd))
2865 {
2866 initialise_findrep(eap->arg);
2867# if defined(FEAT_MBYTE) && defined(WIN3264)
2868 /* If the OS is Windows NT, and 'encoding' differs from active
2869 * codepage: convert text and use wide function. */
2870 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
2871 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2872 {
2873 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2874 s_findrep_hwnd = FindTextW(
2875 (LPFINDREPLACEW) &s_findrep_struct_w);
2876 }
2877 else
2878# endif
2879 s_findrep_hwnd = FindText((LPFINDREPLACE) &s_findrep_struct);
2880 }
2881
2882 set_window_title(s_findrep_hwnd,
2883 _("Find string (use '\\\\' to find a '\\')"));
2884 (void)SetFocus(s_findrep_hwnd);
2885
2886 s_findrep_is_find = TRUE;
2887 }
2888#endif
2889}
2890
2891
2892 void
2893gui_mch_replace_dialog(exarg_T *eap)
2894{
2895#ifdef MSWIN_FIND_REPLACE
2896 if (s_findrep_msg != 0)
2897 {
2898 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2899 DestroyWindow(s_findrep_hwnd);
2900
2901 if (!IsWindow(s_findrep_hwnd))
2902 {
2903 initialise_findrep(eap->arg);
2904# if defined(FEAT_MBYTE) && defined(WIN3264)
2905 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
2906 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2907 {
2908 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2909 s_findrep_hwnd = ReplaceTextW(
2910 (LPFINDREPLACEW) &s_findrep_struct_w);
2911 }
2912 else
2913# endif
2914 s_findrep_hwnd = ReplaceText(
2915 (LPFINDREPLACE) &s_findrep_struct);
2916 }
2917
2918 set_window_title(s_findrep_hwnd,
2919 _("Find & Replace (use '\\\\' to find a '\\')"));
2920 (void)SetFocus(s_findrep_hwnd);
2921
2922 s_findrep_is_find = FALSE;
2923 }
2924#endif
2925}
2926
2927
2928/*
2929 * Set visibility of the pointer.
2930 */
2931 void
2932gui_mch_mousehide(int hide)
2933{
2934 if (hide != gui.pointer_hidden)
2935 {
2936 ShowCursor(!hide);
2937 gui.pointer_hidden = hide;
2938 }
2939}
2940
2941#ifdef FEAT_MENU
2942 static void
2943gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2944{
2945 /* Unhide the mouse, we don't get move events here. */
2946 gui_mch_mousehide(FALSE);
2947
2948 (void)TrackPopupMenu(
2949 (HMENU)menu->submenu_id,
2950 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2951 x, y,
2952 (int)0, /*reserved param*/
2953 s_hwnd,
2954 NULL);
2955 /*
2956 * NOTE: The pop-up menu can eat the mouse up event.
2957 * We deal with this in normal.c.
2958 */
2959}
2960#endif
2961
2962/*
2963 * Got a message when the system will go down.
2964 */
2965 static void
2966_OnEndSession(void)
2967{
2968 getout_preserve_modified(1);
2969}
2970
2971/*
2972 * Get this message when the user clicks on the cross in the top right corner
2973 * of a Windows95 window.
2974 */
2975/*ARGSUSED*/
2976 static void
2977_OnClose(
2978 HWND hwnd)
2979{
2980 gui_shell_closed();
2981}
2982
2983/*
2984 * Get a message when the window is being destroyed.
2985 */
2986 static void
2987_OnDestroy(
2988 HWND hwnd)
2989{
2990 if (!destroying)
2991 _OnClose(hwnd);
2992}
2993
2994 static void
2995_OnPaint(
2996 HWND hwnd)
2997{
2998 if (!IsMinimized(hwnd))
2999 {
3000 PAINTSTRUCT ps;
3001
3002 out_flush(); /* make sure all output has been processed */
3003 (void)BeginPaint(hwnd, &ps);
3004#if defined(FEAT_DIRECTX)
3005 if (IS_ENABLE_DIRECTX())
3006 DWriteContext_BeginDraw(s_dwc);
3007#endif
3008
3009#ifdef FEAT_MBYTE
3010 /* prevent multi-byte characters from misprinting on an invalid
3011 * rectangle */
3012 if (has_mbyte)
3013 {
3014 RECT rect;
3015
3016 GetClientRect(hwnd, &rect);
3017 ps.rcPaint.left = rect.left;
3018 ps.rcPaint.right = rect.right;
3019 }
3020#endif
3021
3022 if (!IsRectEmpty(&ps.rcPaint))
3023 {
3024#if defined(FEAT_DIRECTX)
3025 if (IS_ENABLE_DIRECTX())
3026 DWriteContext_BindDC(s_dwc, s_hdc, &ps.rcPaint);
3027#endif
3028 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
3029 ps.rcPaint.right - ps.rcPaint.left + 1,
3030 ps.rcPaint.bottom - ps.rcPaint.top + 1);
3031 }
3032
3033#if defined(FEAT_DIRECTX)
3034 if (IS_ENABLE_DIRECTX())
3035 DWriteContext_EndDraw(s_dwc);
3036#endif
3037 EndPaint(hwnd, &ps);
3038 }
3039}
3040
3041/*ARGSUSED*/
3042 static void
3043_OnSize(
3044 HWND hwnd,
3045 UINT state,
3046 int cx,
3047 int cy)
3048{
3049 if (!IsMinimized(hwnd))
3050 {
3051 gui_resize_shell(cx, cy);
3052
3053#ifdef FEAT_MENU
3054 /* Menu bar may wrap differently now */
3055 gui_mswin_get_menu_height(TRUE);
3056#endif
3057 }
3058}
3059
3060 static void
3061_OnSetFocus(
3062 HWND hwnd,
3063 HWND hwndOldFocus)
3064{
3065 gui_focus_change(TRUE);
3066 s_getting_focus = TRUE;
3067 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
3068}
3069
3070 static void
3071_OnKillFocus(
3072 HWND hwnd,
3073 HWND hwndNewFocus)
3074{
3075 gui_focus_change(FALSE);
3076 s_getting_focus = FALSE;
3077 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
3078}
3079
3080/*
3081 * Get a message when the user switches back to vim
3082 */
3083 static LRESULT
3084_OnActivateApp(
3085 HWND hwnd,
3086 BOOL fActivate,
3087 DWORD dwThreadId)
3088{
3089 /* we call gui_focus_change() in _OnSetFocus() */
3090 /* gui_focus_change((int)fActivate); */
3091 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
3092}
3093
3094#if defined(FEAT_WINDOWS) || defined(PROTO)
3095 void
3096gui_mch_destroy_scrollbar(scrollbar_T *sb)
3097{
3098 DestroyWindow(sb->id);
3099}
3100#endif
3101
3102/*
3103 * Get current mouse coordinates in text window.
3104 */
3105 void
3106gui_mch_getmouse(int *x, int *y)
3107{
3108 RECT rct;
3109 POINT mp;
3110
3111 (void)GetWindowRect(s_textArea, &rct);
3112 (void)GetCursorPos((LPPOINT)&mp);
3113 *x = (int)(mp.x - rct.left);
3114 *y = (int)(mp.y - rct.top);
3115}
3116
3117/*
3118 * Move mouse pointer to character at (x, y).
3119 */
3120 void
3121gui_mch_setmouse(int x, int y)
3122{
3123 RECT rct;
3124
3125 (void)GetWindowRect(s_textArea, &rct);
3126 (void)SetCursorPos(x + gui.border_offset + rct.left,
3127 y + gui.border_offset + rct.top);
3128}
3129
3130 static void
3131gui_mswin_get_valid_dimensions(
3132 int w,
3133 int h,
3134 int *valid_w,
3135 int *valid_h)
3136{
3137 int base_width, base_height;
3138
3139 base_width = gui_get_base_width()
3140 + (GetSystemMetrics(SM_CXFRAME) +
3141 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
3142 base_height = gui_get_base_height()
3143 + (GetSystemMetrics(SM_CYFRAME) +
3144 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3145 + GetSystemMetrics(SM_CYCAPTION)
3146#ifdef FEAT_MENU
3147 + gui_mswin_get_menu_height(FALSE)
3148#endif
3149 ;
3150 *valid_w = base_width +
3151 ((w - base_width) / gui.char_width) * gui.char_width;
3152 *valid_h = base_height +
3153 ((h - base_height) / gui.char_height) * gui.char_height;
3154}
3155
3156 void
3157gui_mch_flash(int msec)
3158{
3159 RECT rc;
3160
3161 /*
3162 * Note: InvertRect() excludes right and bottom of rectangle.
3163 */
3164 rc.left = 0;
3165 rc.top = 0;
3166 rc.right = gui.num_cols * gui.char_width;
3167 rc.bottom = gui.num_rows * gui.char_height;
3168 InvertRect(s_hdc, &rc);
3169 gui_mch_flush(); /* make sure it's displayed */
3170
3171 ui_delay((long)msec, TRUE); /* wait for a few msec */
3172
3173 InvertRect(s_hdc, &rc);
3174}
3175
3176/*
3177 * Return flags used for scrolling.
3178 * The SW_INVALIDATE is required when part of the window is covered or
3179 * off-screen. Refer to MS KB Q75236.
3180 */
3181 static int
3182get_scroll_flags(void)
3183{
3184 HWND hwnd;
3185 RECT rcVim, rcOther, rcDest;
3186
3187 GetWindowRect(s_hwnd, &rcVim);
3188
3189 /* Check if the window is partly above or below the screen. We don't care
3190 * about partly left or right of the screen, it is not relevant when
3191 * scrolling up or down. */
3192 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
3193 return SW_INVALIDATE;
3194
3195 /* Check if there is an window (partly) on top of us. */
3196 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3197 if (IsWindowVisible(hwnd))
3198 {
3199 GetWindowRect(hwnd, &rcOther);
3200 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3201 return SW_INVALIDATE;
3202 }
3203 return 0;
3204}
3205
3206/*
3207 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3208 * may not be scrolled out properly.
3209 * For gVim, when _OnScroll() is repeated, the character at the
3210 * previous cursor position may be left drawn after scroll.
3211 * The problem can be avoided by calling GetPixel() to get a pixel in
3212 * the region before ScrollWindowEx().
3213 */
3214 static void
3215intel_gpu_workaround(void)
3216{
3217 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3218}
3219
3220/*
3221 * Delete the given number of lines from the given row, scrolling up any
3222 * text further down within the scroll region.
3223 */
3224 void
3225gui_mch_delete_lines(
3226 int row,
3227 int num_lines)
3228{
3229 RECT rc;
3230
3231 intel_gpu_workaround();
3232
3233 rc.left = FILL_X(gui.scroll_region_left);
3234 rc.right = FILL_X(gui.scroll_region_right + 1);
3235 rc.top = FILL_Y(row);
3236 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3237
3238 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
3239 &rc, &rc, NULL, NULL, get_scroll_flags());
3240
3241 UpdateWindow(s_textArea);
3242 /* This seems to be required to avoid the cursor disappearing when
3243 * scrolling such that the cursor ends up in the top-left character on
3244 * the screen... But why? (Webb) */
3245 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3246 /* gui.cursor_is_valid = FALSE; */
3247
3248 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3249 gui.scroll_region_left,
3250 gui.scroll_region_bot, gui.scroll_region_right);
3251}
3252
3253/*
3254 * Insert the given number of lines before the given row, scrolling down any
3255 * following text within the scroll region.
3256 */
3257 void
3258gui_mch_insert_lines(
3259 int row,
3260 int num_lines)
3261{
3262 RECT rc;
3263
3264 intel_gpu_workaround();
3265
3266 rc.left = FILL_X(gui.scroll_region_left);
3267 rc.right = FILL_X(gui.scroll_region_right + 1);
3268 rc.top = FILL_Y(row);
3269 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3270 /* The SW_INVALIDATE is required when part of the window is covered or
3271 * off-screen. How do we avoid it when it's not needed? */
3272 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
3273 &rc, &rc, NULL, NULL, get_scroll_flags());
3274
3275 UpdateWindow(s_textArea);
3276
3277 gui_clear_block(row, gui.scroll_region_left,
3278 row + num_lines - 1, gui.scroll_region_right);
3279}
3280
3281
3282/*ARGSUSED*/
3283 void
3284gui_mch_exit(int rc)
3285{
3286#if defined(FEAT_DIRECTX)
3287 DWriteContext_Close(s_dwc);
3288 DWrite_Final();
3289 s_dwc = NULL;
3290#endif
3291
3292 ReleaseDC(s_textArea, s_hdc);
3293 DeleteObject(s_brush);
3294
3295#ifdef FEAT_TEAROFF
3296 /* Unload the tearoff bitmap */
3297 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3298#endif
3299
3300 /* Destroy our window (if we have one). */
3301 if (s_hwnd != NULL)
3302 {
3303 destroying = TRUE; /* ignore WM_DESTROY message now */
3304 DestroyWindow(s_hwnd);
3305 }
3306
3307#ifdef GLOBAL_IME
3308 global_ime_end();
3309#endif
3310}
3311
3312 static char_u *
3313logfont2name(LOGFONT lf)
3314{
3315 char *p;
3316 char *res;
3317 char *charset_name;
3318 char *font_name = lf.lfFaceName;
3319
3320 charset_name = charset_id2name((int)lf.lfCharSet);
3321#ifdef FEAT_MBYTE
3322 /* Convert a font name from the current codepage to 'encoding'.
3323 * TODO: Use Wide APIs (including LOGFONTW) instead of ANSI APIs. */
3324 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
3325 {
3326 int len;
3327 acp_to_enc((char_u *)lf.lfFaceName, (int)strlen(lf.lfFaceName),
3328 (char_u **)&font_name, &len);
3329 }
3330#endif
3331 res = (char *)alloc((unsigned)(strlen(font_name) + 20
3332 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
3333 if (res != NULL)
3334 {
3335 p = res;
3336 /* make a normal font string out of the lf thing:*/
3337 sprintf((char *)p, "%s:h%d", font_name, pixels_to_points(
3338 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
3339 while (*p)
3340 {
3341 if (*p == ' ')
3342 *p = '_';
3343 ++p;
3344 }
3345 if (lf.lfItalic)
3346 STRCAT(p, ":i");
3347 if (lf.lfWeight >= FW_BOLD)
3348 STRCAT(p, ":b");
3349 if (lf.lfUnderline)
3350 STRCAT(p, ":u");
3351 if (lf.lfStrikeOut)
3352 STRCAT(p, ":s");
3353 if (charset_name != NULL)
3354 {
3355 STRCAT(p, ":c");
3356 STRCAT(p, charset_name);
3357 }
3358 }
3359
3360#ifdef FEAT_MBYTE
3361 if (font_name != lf.lfFaceName)
3362 vim_free(font_name);
3363#endif
3364 return (char_u *)res;
3365}
3366
3367
3368#ifdef FEAT_MBYTE_IME
3369/*
3370 * Set correct LOGFONT to IME. Use 'guifontwide' if available, otherwise use
3371 * 'guifont'
3372 */
3373 static void
3374update_im_font(void)
3375{
3376 LOGFONT lf_wide;
3377
3378 if (p_guifontwide != NULL && *p_guifontwide != NUL
3379 && gui.wide_font != NOFONT
3380 && GetObject((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
3381 norm_logfont = lf_wide;
3382 else
3383 norm_logfont = sub_logfont;
3384 im_set_font(&norm_logfont);
3385}
3386#endif
3387
3388#ifdef FEAT_MBYTE
3389/*
3390 * Handler of gui.wide_font (p_guifontwide) changed notification.
3391 */
3392 void
3393gui_mch_wide_font_changed(void)
3394{
3395 LOGFONT lf;
3396
3397# ifdef FEAT_MBYTE_IME
3398 update_im_font();
3399# endif
3400
3401 gui_mch_free_font(gui.wide_ital_font);
3402 gui.wide_ital_font = NOFONT;
3403 gui_mch_free_font(gui.wide_bold_font);
3404 gui.wide_bold_font = NOFONT;
3405 gui_mch_free_font(gui.wide_boldital_font);
3406 gui.wide_boldital_font = NOFONT;
3407
3408 if (gui.wide_font
3409 && GetObject((HFONT)gui.wide_font, sizeof(lf), &lf))
3410 {
3411 if (!lf.lfItalic)
3412 {
3413 lf.lfItalic = TRUE;
3414 gui.wide_ital_font = get_font_handle(&lf);
3415 lf.lfItalic = FALSE;
3416 }
3417 if (lf.lfWeight < FW_BOLD)
3418 {
3419 lf.lfWeight = FW_BOLD;
3420 gui.wide_bold_font = get_font_handle(&lf);
3421 if (!lf.lfItalic)
3422 {
3423 lf.lfItalic = TRUE;
3424 gui.wide_boldital_font = get_font_handle(&lf);
3425 }
3426 }
3427 }
3428}
3429#endif
3430
3431/*
3432 * Initialise vim to use the font with the given name.
3433 * Return FAIL if the font could not be loaded, OK otherwise.
3434 */
3435/*ARGSUSED*/
3436 int
3437gui_mch_init_font(char_u *font_name, int fontset)
3438{
3439 LOGFONT lf;
3440 GuiFont font = NOFONT;
3441 char_u *p;
3442
3443 /* Load the font */
3444 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3445 font = get_font_handle(&lf);
3446 if (font == NOFONT)
3447 return FAIL;
3448
3449 if (font_name == NULL)
3450 font_name = (char_u *)lf.lfFaceName;
3451#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3452 norm_logfont = lf;
3453 sub_logfont = lf;
3454#endif
3455#ifdef FEAT_MBYTE_IME
3456 update_im_font();
3457#endif
3458 gui_mch_free_font(gui.norm_font);
3459 gui.norm_font = font;
3460 current_font_height = lf.lfHeight;
3461 GetFontSize(font);
3462
3463 p = logfont2name(lf);
3464 if (p != NULL)
3465 {
3466 hl_set_font_name(p);
3467
3468 /* When setting 'guifont' to "*" replace it with the actual font name.
3469 * */
3470 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3471 {
3472 vim_free(p_guifont);
3473 p_guifont = p;
3474 }
3475 else
3476 vim_free(p);
3477 }
3478
3479 gui_mch_free_font(gui.ital_font);
3480 gui.ital_font = NOFONT;
3481 gui_mch_free_font(gui.bold_font);
3482 gui.bold_font = NOFONT;
3483 gui_mch_free_font(gui.boldital_font);
3484 gui.boldital_font = NOFONT;
3485
3486 if (!lf.lfItalic)
3487 {
3488 lf.lfItalic = TRUE;
3489 gui.ital_font = get_font_handle(&lf);
3490 lf.lfItalic = FALSE;
3491 }
3492 if (lf.lfWeight < FW_BOLD)
3493 {
3494 lf.lfWeight = FW_BOLD;
3495 gui.bold_font = get_font_handle(&lf);
3496 if (!lf.lfItalic)
3497 {
3498 lf.lfItalic = TRUE;
3499 gui.boldital_font = get_font_handle(&lf);
3500 }
3501 }
3502
3503 return OK;
3504}
3505
3506#ifndef WPF_RESTORETOMAXIMIZED
3507# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3508#endif
3509
3510/*
3511 * Return TRUE if the GUI window is maximized, filling the whole screen.
3512 */
3513 int
3514gui_mch_maximized(void)
3515{
3516 WINDOWPLACEMENT wp;
3517
3518 wp.length = sizeof(WINDOWPLACEMENT);
3519 if (GetWindowPlacement(s_hwnd, &wp))
3520 return wp.showCmd == SW_SHOWMAXIMIZED
3521 || (wp.showCmd == SW_SHOWMINIMIZED
3522 && wp.flags == WPF_RESTORETOMAXIMIZED);
3523
3524 return 0;
3525}
3526
3527/*
3528 * Called when the font changed while the window is maximized. Compute the
3529 * new Rows and Columns. This is like resizing the window.
3530 */
3531 void
3532gui_mch_newfont(void)
3533{
3534 RECT rect;
3535
3536 GetWindowRect(s_hwnd, &rect);
3537 if (win_socket_id == 0)
3538 {
3539 gui_resize_shell(rect.right - rect.left
3540 - (GetSystemMetrics(SM_CXFRAME) +
3541 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3542 rect.bottom - rect.top
3543 - (GetSystemMetrics(SM_CYFRAME) +
3544 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3545 - GetSystemMetrics(SM_CYCAPTION)
3546#ifdef FEAT_MENU
3547 - gui_mswin_get_menu_height(FALSE)
3548#endif
3549 );
3550 }
3551 else
3552 {
3553 /* Inside another window, don't use the frame and border. */
3554 gui_resize_shell(rect.right - rect.left,
3555 rect.bottom - rect.top
3556#ifdef FEAT_MENU
3557 - gui_mswin_get_menu_height(FALSE)
3558#endif
3559 );
3560 }
3561}
3562
3563/*
3564 * Set the window title
3565 */
3566/*ARGSUSED*/
3567 void
3568gui_mch_settitle(
3569 char_u *title,
3570 char_u *icon)
3571{
3572 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3573}
3574
3575#ifdef FEAT_MOUSESHAPE
3576/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3577 * misc2.c! */
3578static LPCSTR mshape_idcs[] =
3579{
3580 IDC_ARROW, /* arrow */
3581 MAKEINTRESOURCE(0), /* blank */
3582 IDC_IBEAM, /* beam */
3583 IDC_SIZENS, /* updown */
3584 IDC_SIZENS, /* udsizing */
3585 IDC_SIZEWE, /* leftright */
3586 IDC_SIZEWE, /* lrsizing */
3587 IDC_WAIT, /* busy */
3588#ifdef WIN3264
3589 IDC_NO, /* no */
3590#else
3591 IDC_ICON, /* no */
3592#endif
3593 IDC_ARROW, /* crosshair */
3594 IDC_ARROW, /* hand1 */
3595 IDC_ARROW, /* hand2 */
3596 IDC_ARROW, /* pencil */
3597 IDC_ARROW, /* question */
3598 IDC_ARROW, /* right-arrow */
3599 IDC_UPARROW, /* up-arrow */
3600 IDC_ARROW /* last one */
3601};
3602
3603 void
3604mch_set_mouse_shape(int shape)
3605{
3606 LPCSTR idc;
3607
3608 if (shape == MSHAPE_HIDE)
3609 ShowCursor(FALSE);
3610 else
3611 {
3612 if (shape >= MSHAPE_NUMBERED)
3613 idc = IDC_ARROW;
3614 else
3615 idc = mshape_idcs[shape];
3616#ifdef SetClassLongPtr
3617 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3618#else
3619# ifdef WIN32
3620 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
3621# else /* Win16 */
3622 SetClassWord(s_textArea, GCW_HCURSOR, (WORD)LoadCursor(NULL, idc));
3623# endif
3624#endif
3625 if (!p_mh)
3626 {
3627 POINT mp;
3628
3629 /* Set the position to make it redrawn with the new shape. */
3630 (void)GetCursorPos((LPPOINT)&mp);
3631 (void)SetCursorPos(mp.x, mp.y);
3632 ShowCursor(TRUE);
3633 }
3634 }
3635}
3636#endif
3637
3638#ifdef FEAT_BROWSE
3639/*
3640 * The file browser exists in two versions: with "W" uses wide characters,
3641 * without "W" the current codepage. When FEAT_MBYTE is defined and on
3642 * Windows NT/2000/XP the "W" functions are used.
3643 */
3644
3645# if defined(FEAT_MBYTE) && defined(WIN3264)
3646/*
3647 * Wide version of convert_filter().
3648 */
3649 static WCHAR *
3650convert_filterW(char_u *s)
3651{
3652 char_u *tmp;
3653 int len;
3654 WCHAR *res;
3655
3656 tmp = convert_filter(s);
3657 if (tmp == NULL)
3658 return NULL;
3659 len = (int)STRLEN(s) + 3;
3660 res = enc_to_utf16(tmp, &len);
3661 vim_free(tmp);
3662 return res;
3663}
3664
3665/*
3666 * Wide version of gui_mch_browse(). Keep in sync!
3667 */
3668 static char_u *
3669gui_mch_browseW(
3670 int saving,
3671 char_u *title,
3672 char_u *dflt,
3673 char_u *ext,
3674 char_u *initdir,
3675 char_u *filter)
3676{
3677 /* We always use the wide function. This means enc_to_utf16() must work,
3678 * otherwise it fails miserably! */
3679 OPENFILENAMEW fileStruct;
3680 WCHAR fileBuf[MAXPATHL];
3681 WCHAR *wp;
3682 int i;
3683 WCHAR *titlep = NULL;
3684 WCHAR *extp = NULL;
3685 WCHAR *initdirp = NULL;
3686 WCHAR *filterp;
3687 char_u *p;
3688
3689 if (dflt == NULL)
3690 fileBuf[0] = NUL;
3691 else
3692 {
3693 wp = enc_to_utf16(dflt, NULL);
3694 if (wp == NULL)
3695 fileBuf[0] = NUL;
3696 else
3697 {
3698 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3699 fileBuf[i] = wp[i];
3700 fileBuf[i] = NUL;
3701 vim_free(wp);
3702 }
3703 }
3704
3705 /* Convert the filter to Windows format. */
3706 filterp = convert_filterW(filter);
3707
3708 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
3709#ifdef OPENFILENAME_SIZE_VERSION_400
3710 /* be compatible with Windows NT 4.0 */
3711 /* TODO: what to use for OPENFILENAMEW??? */
3712 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400;
3713#else
3714 fileStruct.lStructSize = sizeof(fileStruct);
3715#endif
3716
3717 if (title != NULL)
3718 titlep = enc_to_utf16(title, NULL);
3719 fileStruct.lpstrTitle = titlep;
3720
3721 if (ext != NULL)
3722 extp = enc_to_utf16(ext, NULL);
3723 fileStruct.lpstrDefExt = extp;
3724
3725 fileStruct.lpstrFile = fileBuf;
3726 fileStruct.nMaxFile = MAXPATHL;
3727 fileStruct.lpstrFilter = filterp;
3728 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3729 /* has an initial dir been specified? */
3730 if (initdir != NULL && *initdir != NUL)
3731 {
3732 /* Must have backslashes here, no matter what 'shellslash' says */
3733 initdirp = enc_to_utf16(initdir, NULL);
3734 if (initdirp != NULL)
3735 {
3736 for (wp = initdirp; *wp != NUL; ++wp)
3737 if (*wp == '/')
3738 *wp = '\\';
3739 }
3740 fileStruct.lpstrInitialDir = initdirp;
3741 }
3742
3743 /*
3744 * TODO: Allow selection of multiple files. Needs another arg to this
3745 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3746 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3747 * files that don't exist yet, so I haven't put it in. What about
3748 * OFN_PATHMUSTEXIST?
3749 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3750 */
3751 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
3752#ifdef FEAT_SHORTCUT
3753 if (curbuf->b_p_bin)
3754 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
3755#endif
3756 if (saving)
3757 {
3758 if (!GetSaveFileNameW(&fileStruct))
3759 return NULL;
3760 }
3761 else
3762 {
3763 if (!GetOpenFileNameW(&fileStruct))
3764 return NULL;
3765 }
3766
3767 vim_free(filterp);
3768 vim_free(initdirp);
3769 vim_free(titlep);
3770 vim_free(extp);
3771
3772 /* Convert from UCS2 to 'encoding'. */
3773 p = utf16_to_enc(fileBuf, NULL);
3774 if (p != NULL)
3775 /* when out of memory we get garbage for non-ASCII chars */
3776 STRCPY(fileBuf, p);
3777 vim_free(p);
3778
3779 /* Give focus back to main window (when using MDI). */
3780 SetFocus(s_hwnd);
3781
3782 /* Shorten the file name if possible */
3783 return vim_strsave(shorten_fname1((char_u *)fileBuf));
3784}
3785# endif /* FEAT_MBYTE */
3786
3787
3788/*
3789 * Convert the string s to the proper format for a filter string by replacing
3790 * the \t and \n delimiters with \0.
3791 * Returns the converted string in allocated memory.
3792 *
3793 * Keep in sync with convert_filterW() above!
3794 */
3795 static char_u *
3796convert_filter(char_u *s)
3797{
3798 char_u *res;
3799 unsigned s_len = (unsigned)STRLEN(s);
3800 unsigned i;
3801
3802 res = alloc(s_len + 3);
3803 if (res != NULL)
3804 {
3805 for (i = 0; i < s_len; ++i)
3806 if (s[i] == '\t' || s[i] == '\n')
3807 res[i] = '\0';
3808 else
3809 res[i] = s[i];
3810 res[s_len] = NUL;
3811 /* Add two extra NULs to make sure it's properly terminated. */
3812 res[s_len + 1] = NUL;
3813 res[s_len + 2] = NUL;
3814 }
3815 return res;
3816}
3817
3818/*
3819 * Select a directory.
3820 */
3821 char_u *
3822gui_mch_browsedir(char_u *title, char_u *initdir)
3823{
3824 /* We fake this: Use a filter that doesn't select anything and a default
3825 * file name that won't be used. */
3826 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3827 initdir, (char_u *)_("Directory\t*.nothing\n"));
3828}
3829
3830/*
3831 * Pop open a file browser and return the file selected, in allocated memory,
3832 * or NULL if Cancel is hit.
3833 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3834 * title - Title message for the file browser dialog.
3835 * dflt - Default name of file.
3836 * ext - Default extension to be added to files without extensions.
3837 * initdir - directory in which to open the browser (NULL = current dir)
3838 * filter - Filter for matched files to choose from.
3839 *
3840 * Keep in sync with gui_mch_browseW() above!
3841 */
3842 char_u *
3843gui_mch_browse(
3844 int saving,
3845 char_u *title,
3846 char_u *dflt,
3847 char_u *ext,
3848 char_u *initdir,
3849 char_u *filter)
3850{
3851 OPENFILENAME fileStruct;
3852 char_u fileBuf[MAXPATHL];
3853 char_u *initdirp = NULL;
3854 char_u *filterp;
3855 char_u *p;
3856
3857# if defined(FEAT_MBYTE) && defined(WIN3264)
3858 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
3859 return gui_mch_browseW(saving, title, dflt, ext, initdir, filter);
3860# endif
3861
3862 if (dflt == NULL)
3863 fileBuf[0] = NUL;
3864 else
3865 vim_strncpy(fileBuf, dflt, MAXPATHL - 1);
3866
3867 /* Convert the filter to Windows format. */
3868 filterp = convert_filter(filter);
3869
3870 vim_memset(&fileStruct, 0, sizeof(OPENFILENAME));
3871#ifdef OPENFILENAME_SIZE_VERSION_400
3872 /* be compatible with Windows NT 4.0 */
3873 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400;
3874#else
3875 fileStruct.lStructSize = sizeof(fileStruct);
3876#endif
3877
3878 fileStruct.lpstrTitle = (LPSTR)title;
3879 fileStruct.lpstrDefExt = (LPSTR)ext;
3880
3881 fileStruct.lpstrFile = (LPSTR)fileBuf;
3882 fileStruct.nMaxFile = MAXPATHL;
3883 fileStruct.lpstrFilter = (LPSTR)filterp;
3884 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3885 /* has an initial dir been specified? */
3886 if (initdir != NULL && *initdir != NUL)
3887 {
3888 /* Must have backslashes here, no matter what 'shellslash' says */
3889 initdirp = vim_strsave(initdir);
3890 if (initdirp != NULL)
3891 for (p = initdirp; *p != NUL; ++p)
3892 if (*p == '/')
3893 *p = '\\';
3894 fileStruct.lpstrInitialDir = (LPSTR)initdirp;
3895 }
3896
3897 /*
3898 * TODO: Allow selection of multiple files. Needs another arg to this
3899 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3900 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3901 * files that don't exist yet, so I haven't put it in. What about
3902 * OFN_PATHMUSTEXIST?
3903 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3904 */
3905 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
3906#ifdef FEAT_SHORTCUT
3907 if (curbuf->b_p_bin)
3908 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
3909#endif
3910 if (saving)
3911 {
3912 if (!GetSaveFileName(&fileStruct))
3913 return NULL;
3914 }
3915 else
3916 {
3917 if (!GetOpenFileName(&fileStruct))
3918 return NULL;
3919 }
3920
3921 vim_free(filterp);
3922 vim_free(initdirp);
3923
3924 /* Give focus back to main window (when using MDI). */
3925 SetFocus(s_hwnd);
3926
3927 /* Shorten the file name if possible */
3928 return vim_strsave(shorten_fname1((char_u *)fileBuf));
3929}
3930#endif /* FEAT_BROWSE */
3931
3932/*ARGSUSED*/
3933 static void
3934_OnDropFiles(
3935 HWND hwnd,
3936 HDROP hDrop)
3937{
3938#ifdef FEAT_WINDOWS
3939#ifdef WIN3264
3940# define BUFPATHLEN _MAX_PATH
3941# define DRAGQVAL 0xFFFFFFFF
3942#else
3943# define BUFPATHLEN MAXPATHL
3944# define DRAGQVAL 0xFFFF
3945#endif
3946#ifdef FEAT_MBYTE
3947 WCHAR wszFile[BUFPATHLEN];
3948#endif
3949 char szFile[BUFPATHLEN];
3950 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3951 UINT i;
3952 char_u **fnames;
3953 POINT pt;
3954 int_u modifiers = 0;
3955
3956 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3957
3958 /* Obtain dropped position */
3959 DragQueryPoint(hDrop, &pt);
3960 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3961
3962 reset_VIsual();
3963
3964 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3965
3966 if (fnames != NULL)
3967 for (i = 0; i < cFiles; ++i)
3968 {
3969#ifdef FEAT_MBYTE
3970 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3971 fnames[i] = utf16_to_enc(wszFile, NULL);
3972 else
3973#endif
3974 {
3975 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3976 fnames[i] = vim_strsave((char_u *)szFile);
3977 }
3978 }
3979
3980 DragFinish(hDrop);
3981
3982 if (fnames != NULL)
3983 {
3984 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3985 modifiers |= MOUSE_SHIFT;
3986 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3987 modifiers |= MOUSE_CTRL;
3988 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3989 modifiers |= MOUSE_ALT;
3990
3991 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3992
3993 s_need_activate = TRUE;
3994 }
3995#endif
3996}
3997
3998/*ARGSUSED*/
3999 static int
4000_OnScroll(
4001 HWND hwnd,
4002 HWND hwndCtl,
4003 UINT code,
4004 int pos)
4005{
4006 static UINT prev_code = 0; /* code of previous call */
4007 scrollbar_T *sb, *sb_info;
4008 long val;
4009 int dragging = FALSE;
4010 int dont_scroll_save = dont_scroll;
4011#ifndef WIN3264
4012 int nPos;
4013#else
4014 SCROLLINFO si;
4015
4016 si.cbSize = sizeof(si);
4017 si.fMask = SIF_POS;
4018#endif
4019
4020 sb = gui_mswin_find_scrollbar(hwndCtl);
4021 if (sb == NULL)
4022 return 0;
4023
4024 if (sb->wp != NULL) /* Left or right scrollbar */
4025 {
4026 /*
4027 * Careful: need to get scrollbar info out of first (left) scrollbar
4028 * for window, but keep real scrollbar too because we must pass it to
4029 * gui_drag_scrollbar().
4030 */
4031 sb_info = &sb->wp->w_scrollbars[0];
4032 }
4033 else /* Bottom scrollbar */
4034 sb_info = sb;
4035 val = sb_info->value;
4036
4037 switch (code)
4038 {
4039 case SB_THUMBTRACK:
4040 val = pos;
4041 dragging = TRUE;
4042 if (sb->scroll_shift > 0)
4043 val <<= sb->scroll_shift;
4044 break;
4045 case SB_LINEDOWN:
4046 val++;
4047 break;
4048 case SB_LINEUP:
4049 val--;
4050 break;
4051 case SB_PAGEDOWN:
4052 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
4053 break;
4054 case SB_PAGEUP:
4055 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
4056 break;
4057 case SB_TOP:
4058 val = 0;
4059 break;
4060 case SB_BOTTOM:
4061 val = sb_info->max;
4062 break;
4063 case SB_ENDSCROLL:
4064 if (prev_code == SB_THUMBTRACK)
4065 {
4066 /*
4067 * "pos" only gives us 16-bit data. In case of large file,
4068 * use GetScrollPos() which returns 32-bit. Unfortunately it
4069 * is not valid while the scrollbar is being dragged.
4070 */
4071 val = GetScrollPos(hwndCtl, SB_CTL);
4072 if (sb->scroll_shift > 0)
4073 val <<= sb->scroll_shift;
4074 }
4075 break;
4076
4077 default:
4078 /* TRACE("Unknown scrollbar event %d\n", code); */
4079 return 0;
4080 }
4081 prev_code = code;
4082
4083#ifdef WIN3264
4084 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
4085 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
4086#else
4087 nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
4088 SetScrollPos(hwndCtl, SB_CTL, nPos, TRUE);
4089#endif
4090
4091 /*
4092 * When moving a vertical scrollbar, move the other vertical scrollbar too.
4093 */
4094 if (sb->wp != NULL)
4095 {
4096 scrollbar_T *sba = sb->wp->w_scrollbars;
4097 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
4098
4099#ifdef WIN3264
4100 SetScrollInfo(id, SB_CTL, &si, TRUE);
4101#else
4102 SetScrollPos(id, SB_CTL, nPos, TRUE);
4103#endif
4104 }
4105
4106 /* Don't let us be interrupted here by another message. */
4107 s_busy_processing = TRUE;
4108
4109 /* When "allow_scrollbar" is FALSE still need to remember the new
4110 * position, but don't actually scroll by setting "dont_scroll". */
4111 dont_scroll = !allow_scrollbar;
4112
4113 gui_drag_scrollbar(sb, val, dragging);
4114
4115 s_busy_processing = FALSE;
4116 dont_scroll = dont_scroll_save;
4117
4118 return 0;
4119}
4120
4121
4122/*
4123 * Get command line arguments.
4124 * Use "prog" as the name of the program and "cmdline" as the arguments.
4125 * Copy the arguments to allocated memory.
4126 * Return the number of arguments (including program name).
4127 * Return pointers to the arguments in "argvp". Memory is allocated with
4128 * malloc(), use free() instead of vim_free().
4129 * Return pointer to buffer in "tofree".
4130 * Returns zero when out of memory.
4131 */
4132/*ARGSUSED*/
4133 int
4134get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
4135{
4136 int i;
4137 char *p;
4138 char *progp;
4139 char *pnew = NULL;
4140 char *newcmdline;
4141 int inquote;
4142 int argc;
4143 char **argv = NULL;
4144 int round;
4145
4146 *tofree = NULL;
4147
4148#ifdef FEAT_MBYTE
4149 /* Try using the Unicode version first, it takes care of conversion when
4150 * 'encoding' is changed. */
4151 argc = get_cmd_argsW(&argv);
4152 if (argc != 0)
4153 goto done;
4154#endif
4155
4156 /* Handle the program name. Remove the ".exe" extension, and find the 1st
4157 * non-space. */
4158 p = strrchr(prog, '.');
4159 if (p != NULL)
4160 *p = NUL;
4161 for (progp = prog; *progp == ' '; ++progp)
4162 ;
4163
4164 /* The command line is copied to allocated memory, so that we can change
4165 * it. Add the size of the string, the separating NUL and a terminating
4166 * NUL. */
4167 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
4168 if (newcmdline == NULL)
4169 return 0;
4170
4171 /*
4172 * First round: count the number of arguments ("pnew" == NULL).
4173 * Second round: produce the arguments.
4174 */
4175 for (round = 1; round <= 2; ++round)
4176 {
4177 /* First argument is the program name. */
4178 if (pnew != NULL)
4179 {
4180 argv[0] = pnew;
4181 strcpy(pnew, progp);
4182 pnew += strlen(pnew);
4183 *pnew++ = NUL;
4184 }
4185
4186 /*
4187 * Isolate each argument and put it in argv[].
4188 */
4189 p = cmdline;
4190 argc = 1;
4191 while (*p != NUL)
4192 {
4193 inquote = FALSE;
4194 if (pnew != NULL)
4195 argv[argc] = pnew;
4196 ++argc;
4197 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
4198 {
4199 /* Backslashes are only special when followed by a double
4200 * quote. */
4201 i = (int)strspn(p, "\\");
4202 if (p[i] == '"')
4203 {
4204 /* Halve the number of backslashes. */
4205 if (i > 1 && pnew != NULL)
4206 {
4207 vim_memset(pnew, '\\', i / 2);
4208 pnew += i / 2;
4209 }
4210
4211 /* Even nr of backslashes toggles quoting, uneven copies
4212 * the double quote. */
4213 if ((i & 1) == 0)
4214 inquote = !inquote;
4215 else if (pnew != NULL)
4216 *pnew++ = '"';
4217 p += i + 1;
4218 }
4219 else if (i > 0)
4220 {
4221 /* Copy span of backslashes unmodified. */
4222 if (pnew != NULL)
4223 {
4224 vim_memset(pnew, '\\', i);
4225 pnew += i;
4226 }
4227 p += i;
4228 }
4229 else
4230 {
4231 if (pnew != NULL)
4232 *pnew++ = *p;
4233#ifdef FEAT_MBYTE
4234 /* Can't use mb_* functions, because 'encoding' is not
4235 * initialized yet here. */
4236 if (IsDBCSLeadByte(*p))
4237 {
4238 ++p;
4239 if (pnew != NULL)
4240 *pnew++ = *p;
4241 }
4242#endif
4243 ++p;
4244 }
4245 }
4246
4247 if (pnew != NULL)
4248 *pnew++ = NUL;
4249 while (*p == ' ' || *p == '\t')
4250 ++p; /* advance until a non-space */
4251 }
4252
4253 if (round == 1)
4254 {
4255 argv = (char **)malloc((argc + 1) * sizeof(char *));
4256 if (argv == NULL )
4257 {
4258 free(newcmdline);
4259 return 0; /* malloc error */
4260 }
4261 pnew = newcmdline;
4262 *tofree = newcmdline;
4263 }
4264 }
4265
4266#ifdef FEAT_MBYTE
4267done:
4268#endif
4269 argv[argc] = NULL; /* NULL-terminated list */
4270 *argvp = argv;
4271 return argc;
4272}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273
4274#ifdef FEAT_XPM_W32
4275# include "xpm_w32.h"
4276#endif
4277
4278#ifdef PROTO
4279# define WINAPI
4280#endif
4281
4282#ifdef __MINGW32__
4283/*
4284 * Add a lot of missing defines.
4285 * They are not always missing, we need the #ifndef's.
4286 */
4287# ifndef _cdecl
4288# define _cdecl
4289# endif
4290# ifndef IsMinimized
4291# define IsMinimized(hwnd) IsIconic(hwnd)
4292# endif
4293# ifndef IsMaximized
4294# define IsMaximized(hwnd) IsZoomed(hwnd)
4295# endif
4296# ifndef SelectFont
4297# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
4298# endif
4299# ifndef GetStockBrush
4300# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
4301# endif
4302# ifndef DeleteBrush
4303# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
4304# endif
4305
4306# ifndef HANDLE_WM_RBUTTONDBLCLK
4307# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4308 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4309# endif
4310# ifndef HANDLE_WM_MBUTTONUP
4311# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
4312 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4313# endif
4314# ifndef HANDLE_WM_MBUTTONDBLCLK
4315# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4316 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4317# endif
4318# ifndef HANDLE_WM_LBUTTONDBLCLK
4319# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4320 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4321# endif
4322# ifndef HANDLE_WM_RBUTTONDOWN
4323# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
4324 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4325# endif
4326# ifndef HANDLE_WM_MOUSEMOVE
4327# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
4328 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4329# endif
4330# ifndef HANDLE_WM_RBUTTONUP
4331# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
4332 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4333# endif
4334# ifndef HANDLE_WM_MBUTTONDOWN
4335# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
4336 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4337# endif
4338# ifndef HANDLE_WM_LBUTTONUP
4339# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
4340 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4341# endif
4342# ifndef HANDLE_WM_LBUTTONDOWN
4343# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
4344 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4345# endif
4346# ifndef HANDLE_WM_SYSCHAR
4347# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
4348 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4349# endif
4350# ifndef HANDLE_WM_ACTIVATEAPP
4351# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
4352 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
4353# endif
4354# ifndef HANDLE_WM_WINDOWPOSCHANGING
4355# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
4356 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
4357# endif
4358# ifndef HANDLE_WM_VSCROLL
4359# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
4360 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4361# endif
4362# ifndef HANDLE_WM_SETFOCUS
4363# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
4364 ((fn)((hwnd), (HWND)(wParam)), 0L)
4365# endif
4366# ifndef HANDLE_WM_KILLFOCUS
4367# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
4368 ((fn)((hwnd), (HWND)(wParam)), 0L)
4369# endif
4370# ifndef HANDLE_WM_HSCROLL
4371# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
4372 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4373# endif
4374# ifndef HANDLE_WM_DROPFILES
4375# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
4376 ((fn)((hwnd), (HDROP)(wParam)), 0L)
4377# endif
4378# ifndef HANDLE_WM_CHAR
4379# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
4380 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4381# endif
4382# ifndef HANDLE_WM_SYSDEADCHAR
4383# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
4384 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4385# endif
4386# ifndef HANDLE_WM_DEADCHAR
4387# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4388 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4389# endif
4390#endif /* __MINGW32__ */
4391
4392
4393/* Some parameters for tearoff menus. All in pixels. */
4394#define TEAROFF_PADDING_X 2
4395#define TEAROFF_BUTTON_PAD_X 8
4396#define TEAROFF_MIN_WIDTH 200
4397#define TEAROFF_SUBMENU_LABEL ">>"
4398#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4399
4400
4401/* For the Intellimouse: */
4402#ifndef WM_MOUSEWHEEL
4403#define WM_MOUSEWHEEL 0x20a
4404#endif
4405
4406
4407#ifdef FEAT_BEVAL
4408# define ID_BEVAL_TOOLTIP 200
4409# define BEVAL_TEXT_LEN MAXPATHL
4410
Bram Moolenaar167632f2010-05-26 21:42:54 +02004411#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004412/* Work around old versions of basetsd.h which wrongly declares
4413 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02004414# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00004415# define UINT_PTR UINT
4416#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004417
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004418static void make_tooltip(BalloonEval *beval, char *text, POINT pt);
4419static void delete_tooltip(BalloonEval *beval);
4420static VOID CALLBACK BevalTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004421
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004423static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004425
Bram Moolenaar82881492012-11-20 16:53:39 +01004426
4427/* cproto fails on missing include files */
4428#ifndef PROTO
4429
Bram Moolenaar45360022005-07-21 21:08:21 +00004430/*
4431 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004432 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004433 */
Bram Moolenaar82881492012-11-20 16:53:39 +01004434# include <pshpack1.h>
4435
4436#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004437
4438typedef struct _DllVersionInfo
4439{
4440 DWORD cbSize;
4441 DWORD dwMajorVersion;
4442 DWORD dwMinorVersion;
4443 DWORD dwBuildNumber;
4444 DWORD dwPlatformID;
4445} DLLVERSIONINFO;
4446
Bram Moolenaar82881492012-11-20 16:53:39 +01004447#ifndef PROTO
4448# include <poppack.h>
4449#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004450
Bram Moolenaar45360022005-07-21 21:08:21 +00004451typedef struct tagTOOLINFOA_NEW
4452{
4453 UINT cbSize;
4454 UINT uFlags;
4455 HWND hwnd;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004456 UINT_PTR uId;
Bram Moolenaar45360022005-07-21 21:08:21 +00004457 RECT rect;
4458 HINSTANCE hinst;
4459 LPSTR lpszText;
4460 LPARAM lParam;
4461} TOOLINFO_NEW;
4462
4463typedef struct tagNMTTDISPINFO_NEW
4464{
4465 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004466 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004467 char szText[80];
4468 HINSTANCE hinst;
4469 UINT uFlags;
4470 LPARAM lParam;
4471} NMTTDISPINFO_NEW;
4472
Bram Moolenaar45360022005-07-21 21:08:21 +00004473typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4474#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004475# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476#endif
4477
Bram Moolenaar45360022005-07-21 21:08:21 +00004478#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004479# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004480#endif
4481
4482#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004483# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004484#endif
4485
4486#endif /* defined(FEAT_BEVAL) */
4487
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004488#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4489/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4490 * it here if LPNMTTDISPINFO isn't defined.
4491 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4492 * _MSC_VER. */
4493# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4494typedef struct tagNMTTDISPINFOA {
4495 NMHDR hdr;
4496 LPSTR lpszText;
4497 char szText[80];
4498 HINSTANCE hinst;
4499 UINT uFlags;
4500 LPARAM lParam;
4501} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4502# define LPNMTTDISPINFO LPNMTTDISPINFOA
4503
4504# ifdef FEAT_MBYTE
4505typedef struct tagNMTTDISPINFOW {
4506 NMHDR hdr;
4507 LPWSTR lpszText;
4508 WCHAR szText[80];
4509 HINSTANCE hinst;
4510 UINT uFlags;
4511 LPARAM lParam;
4512} NMTTDISPINFOW, *LPNMTTDISPINFOW;
4513# endif
4514# endif
4515#endif
4516
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004517#ifndef TTN_GETDISPINFOW
4518# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4519#endif
4520
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521/* Local variables: */
4522
4523#ifdef FEAT_MENU
4524static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526
4527/*
4528 * Use the system font for dialogs and tear-off menus. Remove this line to
4529 * use DLG_FONT_NAME.
4530 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004531#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
4533#define VIM_NAME "vim"
4534#define VIM_CLASS "Vim"
4535#define VIM_CLASSW L"Vim"
4536
4537/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4538 * thus there should be room for every dialog. For tearoffs it's made bigger
4539 * when needed. */
4540#define DLG_ALLOC_SIZE 16 * 1024
4541
4542/*
4543 * stuff for dialogs, menus, tearoffs etc.
4544 */
4545static LRESULT APIENTRY dialog_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004546#ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547static LRESULT APIENTRY tearoff_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549static PWORD
4550add_dialog_element(
4551 PWORD p,
4552 DWORD lStyle,
4553 WORD x,
4554 WORD y,
4555 WORD w,
4556 WORD h,
4557 WORD Id,
4558 WORD clss,
4559 const char *caption);
4560static LPWORD lpwAlign(LPWORD);
4561static int nCopyAnsiToWideChar(LPWORD, LPSTR);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004562#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565static void get_dialog_font_metrics(void);
4566
4567static int dialog_default_button = -1;
4568
4569/* Intellimouse support */
4570static int mouse_scroll_lines = 0;
4571static UINT msh_msgmousewheel = 0;
4572
4573static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4574#ifdef FEAT_TOOLBAR
4575static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004576static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577static int get_toolbar_bitmap(vimmenu_T *menu);
4578#endif
4579
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004580#ifdef FEAT_GUI_TABLINE
4581static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004582static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004583#endif
4584
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585#ifdef FEAT_MBYTE_IME
4586static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4587static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4588#endif
4589#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4590# ifdef NOIME
4591typedef struct tagCOMPOSITIONFORM {
4592 DWORD dwStyle;
4593 POINT ptCurrentPos;
4594 RECT rcArea;
4595} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4596typedef HANDLE HIMC;
4597# endif
4598
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004599static HINSTANCE hLibImm = NULL;
4600static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4601static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4602static HIMC (WINAPI *pImmGetContext)(HWND);
4603static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4604static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4605static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4606static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
4607static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
4608static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
4609static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4610static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004611static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612static void dyn_imm_load(void);
4613#else
4614# define pImmGetCompositionStringA ImmGetCompositionStringA
4615# define pImmGetCompositionStringW ImmGetCompositionStringW
4616# define pImmGetContext ImmGetContext
4617# define pImmAssociateContext ImmAssociateContext
4618# define pImmReleaseContext ImmReleaseContext
4619# define pImmGetOpenStatus ImmGetOpenStatus
4620# define pImmSetOpenStatus ImmSetOpenStatus
4621# define pImmGetCompositionFont ImmGetCompositionFontA
4622# define pImmSetCompositionFont ImmSetCompositionFontA
4623# define pImmSetCompositionWindow ImmSetCompositionWindow
4624# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004625# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626#endif
4627
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628/* multi monitor support */
4629typedef struct _MONITORINFOstruct
4630{
4631 DWORD cbSize;
4632 RECT rcMonitor;
4633 RECT rcWork;
4634 DWORD dwFlags;
4635} _MONITORINFO;
4636
4637typedef HANDLE _HMONITOR;
4638typedef _HMONITOR (WINAPI *TMonitorFromWindow)(HWND, DWORD);
4639typedef BOOL (WINAPI *TGetMonitorInfo)(_HMONITOR, _MONITORINFO *);
4640
4641static TMonitorFromWindow pMonitorFromWindow = NULL;
4642static TGetMonitorInfo pGetMonitorInfo = NULL;
4643static HANDLE user32_lib = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644/*
4645 * Return TRUE when running under Windows NT 3.x or Win32s, both of which have
4646 * less fancy GUI APIs.
4647 */
4648 static int
4649is_winnt_3(void)
4650{
4651 return ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
4652 && os_version.dwMajorVersion == 3)
4653 || (os_version.dwPlatformId == VER_PLATFORM_WIN32s));
4654}
4655
4656/*
4657 * Return TRUE when running under Win32s.
4658 */
4659 int
4660gui_is_win32s(void)
4661{
4662 return (os_version.dwPlatformId == VER_PLATFORM_WIN32s);
4663}
4664
4665#ifdef FEAT_MENU
4666/*
4667 * Figure out how high the menu bar is at the moment.
4668 */
4669 static int
4670gui_mswin_get_menu_height(
4671 int fix_window) /* If TRUE, resize window if menu height changed */
4672{
4673 static int old_menu_height = -1;
4674
4675 RECT rc1, rc2;
4676 int num;
4677 int menu_height;
4678
4679 if (gui.menu_is_active)
4680 num = GetMenuItemCount(s_menuBar);
4681 else
4682 num = 0;
4683
4684 if (num == 0)
4685 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004686 else if (IsMinimized(s_hwnd))
4687 {
4688 /* The height of the menu cannot be determined while the window is
4689 * minimized. Take the previous height if the menu is changed in that
4690 * state, to avoid that Vim's vertical window size accidentally
4691 * increases due to the unaccounted-for menu height. */
4692 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 else
4695 {
4696 if (is_winnt_3()) /* for NT 3.xx */
4697 {
4698 if (gui.starting)
4699 menu_height = GetSystemMetrics(SM_CYMENU);
4700 else
4701 {
4702 RECT r1, r2;
4703 int frameht = GetSystemMetrics(SM_CYFRAME);
4704 int capht = GetSystemMetrics(SM_CYCAPTION);
4705
4706 /* get window rect of s_hwnd
4707 * get client rect of s_hwnd
4708 * get cap height
4709 * subtract from window rect, the sum of client height,
4710 * (if not maximized)frame thickness, and caption height.
4711 */
4712 GetWindowRect(s_hwnd, &r1);
4713 GetClientRect(s_hwnd, &r2);
4714 menu_height = r1.bottom - r1.top - (r2.bottom - r2.top
4715 + 2 * frameht * (!IsZoomed(s_hwnd)) + capht);
4716 }
4717 }
4718 else /* win95 and variants (NT 4.0, I guess) */
4719 {
4720 /*
4721 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4722 * seem to have been set yet, so menu wraps in default window
4723 * width which is very narrow. Instead just return height of a
4724 * single menu item. Will still be wrong when the menu really
4725 * should wrap over more than one line.
4726 */
4727 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4728 if (gui.starting)
4729 menu_height = rc1.bottom - rc1.top + 1;
4730 else
4731 {
4732 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4733 menu_height = rc2.bottom - rc1.top + 1;
4734 }
4735 }
4736 }
4737
4738 if (fix_window && menu_height != old_menu_height)
4739 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004740 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 }
Bram Moolenaar71371b12015-03-24 17:57:12 +01004742 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743
4744 return menu_height;
4745}
4746#endif /*FEAT_MENU*/
4747
4748
4749/*
4750 * Setup for the Intellimouse
4751 */
4752 static void
4753init_mouse_wheel(void)
4754{
4755
4756#ifndef SPI_GETWHEELSCROLLLINES
4757# define SPI_GETWHEELSCROLLLINES 104
4758#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004759#ifndef SPI_SETWHEELSCROLLLINES
4760# define SPI_SETWHEELSCROLLLINES 105
4761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
4763#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4764#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4765#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4766#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4767
4768 HWND hdl_mswheel;
4769 UINT msh_msgscrolllines;
4770
4771 msh_msgmousewheel = 0;
4772 mouse_scroll_lines = 3; /* reasonable default */
4773
4774 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
4775 && os_version.dwMajorVersion >= 4)
4776 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
4777 && ((os_version.dwMajorVersion == 4
4778 && os_version.dwMinorVersion >= 10)
4779 || os_version.dwMajorVersion >= 5)))
4780 {
4781 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4782 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4783 &mouse_scroll_lines, 0);
4784 }
4785 else if (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
4786 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
4787 && os_version.dwMajorVersion < 4))
4788 { /*
4789 * If Win95 or NT 3.51,
4790 * try to find the hidden point32 window.
4791 */
4792 hdl_mswheel = FindWindow(VMOUSEZ_CLASSNAME, VMOUSEZ_TITLE);
4793 if (hdl_mswheel)
4794 {
4795 msh_msgscrolllines = RegisterWindowMessage(VMSH_SCROLL_LINES);
4796 if (msh_msgscrolllines)
4797 {
4798 mouse_scroll_lines = (int)SendMessage(hdl_mswheel,
4799 msh_msgscrolllines, 0, 0);
4800 msh_msgmousewheel = RegisterWindowMessage(VMSH_MOUSEWHEEL);
4801 }
4802 }
4803 }
4804}
4805
4806
4807/* Intellimouse wheel handler */
4808 static void
4809_OnMouseWheel(
4810 HWND hwnd,
4811 short zDelta)
4812{
4813/* Treat a mouse wheel event as if it were a scroll request */
4814 int i;
4815 int size;
4816 HWND hwndCtl;
4817
4818 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4819 {
4820 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4821 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4822 }
4823 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4824 {
4825 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4826 size = curwin->w_scrollbars[SBAR_LEFT].size;
4827 }
4828 else
4829 return;
4830
4831 size = curwin->w_height;
4832 if (mouse_scroll_lines == 0)
4833 init_mouse_wheel();
4834
4835 if (mouse_scroll_lines > 0
4836 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4837 {
4838 for (i = mouse_scroll_lines; i > 0; --i)
4839 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4840 }
4841 else
4842 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
4843}
4844
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004845#ifdef USE_SYSMENU_FONT
4846/*
4847 * Get Menu Font.
4848 * Return OK or FAIL.
4849 */
4850 static int
4851gui_w32_get_menu_font(LOGFONT *lf)
4852{
4853 NONCLIENTMETRICS nm;
4854
4855 nm.cbSize = sizeof(NONCLIENTMETRICS);
4856 if (!SystemParametersInfo(
4857 SPI_GETNONCLIENTMETRICS,
4858 sizeof(NONCLIENTMETRICS),
4859 &nm,
4860 0))
4861 return FAIL;
4862 *lf = nm.lfMenuFont;
4863 return OK;
4864}
4865#endif
4866
4867
4868#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4869/*
4870 * Set the GUI tabline font to the system menu font
4871 */
4872 static void
4873set_tabline_font(void)
4874{
4875 LOGFONT lfSysmenu;
4876 HFONT font;
4877 HWND hwnd;
4878 HDC hdc;
4879 HFONT hfntOld;
4880 TEXTMETRIC tm;
4881
4882 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4883 return;
4884
4885 font = CreateFontIndirect(&lfSysmenu);
4886
4887 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4888
4889 /*
4890 * Compute the height of the font used for the tab text
4891 */
4892 hwnd = GetDesktopWindow();
4893 hdc = GetWindowDC(hwnd);
4894 hfntOld = SelectFont(hdc, font);
4895
4896 GetTextMetrics(hdc, &tm);
4897
4898 SelectFont(hdc, hfntOld);
4899 ReleaseDC(hwnd, hdc);
4900
4901 /*
4902 * The space used by the tab border and the space between the tab label
4903 * and the tab border is included as 7.
4904 */
4905 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4906}
4907#endif
4908
Bram Moolenaar520470a2005-06-16 21:59:56 +00004909/*
4910 * Invoked when a setting was changed.
4911 */
4912 static LRESULT CALLBACK
4913_OnSettingChange(UINT n)
4914{
4915 if (n == SPI_SETWHEELSCROLLLINES)
4916 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4917 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004918#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4919 if (n == SPI_SETNONCLIENTMETRICS)
4920 set_tabline_font();
4921#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004922 return 0;
4923}
4924
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925#ifdef FEAT_NETBEANS_INTG
4926 static void
4927_OnWindowPosChanged(
4928 HWND hwnd,
4929 const LPWINDOWPOS lpwpos)
4930{
4931 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004932 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933
4934 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4935 || lpwpos->cx != cx || lpwpos->cy != cy))
4936 {
4937 x = lpwpos->x;
4938 y = lpwpos->y;
4939 cx = lpwpos->cx;
4940 cy = lpwpos->cy;
4941 netbeans_frame_moved(x, y);
4942 }
4943 /* Allow to send WM_SIZE and WM_MOVE */
4944 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4945}
4946#endif
4947
4948 static int
4949_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 UINT fwSide,
4951 LPRECT lprc)
4952{
4953 int w, h;
4954 int valid_w, valid_h;
4955 int w_offset, h_offset;
4956
4957 w = lprc->right - lprc->left;
4958 h = lprc->bottom - lprc->top;
4959 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4960 w_offset = w - valid_w;
4961 h_offset = h - valid_h;
4962
4963 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4964 || fwSide == WMSZ_BOTTOMLEFT)
4965 lprc->left += w_offset;
4966 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4967 || fwSide == WMSZ_BOTTOMRIGHT)
4968 lprc->right -= w_offset;
4969
4970 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4971 || fwSide == WMSZ_TOPRIGHT)
4972 lprc->top += h_offset;
4973 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4974 || fwSide == WMSZ_BOTTOMRIGHT)
4975 lprc->bottom -= h_offset;
4976 return TRUE;
4977}
4978
4979
4980
4981 static LRESULT CALLBACK
4982_WndProc(
4983 HWND hwnd,
4984 UINT uMsg,
4985 WPARAM wParam,
4986 LPARAM lParam)
4987{
4988 /*
4989 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4990 hwnd, uMsg, wParam, lParam);
4991 */
4992
4993 HandleMouseHide(uMsg, lParam);
4994
4995 s_uMsg = uMsg;
4996 s_wParam = wParam;
4997 s_lParam = lParam;
4998
4999 switch (uMsg)
5000 {
5001 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
5002 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
5003 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
5004 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
5005 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
5006 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
5007 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
5008 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
5009 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
5010#ifdef FEAT_MENU
5011 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
5012#endif
5013 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
5014 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
5015 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
5016 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
5017 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
5018 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
5019 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
5020 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
5021 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
5022#ifdef FEAT_NETBEANS_INTG
5023 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
5024#endif
5025
Bram Moolenaarafa24992006-03-27 20:58:26 +00005026#ifdef FEAT_GUI_TABLINE
5027 case WM_RBUTTONUP:
5028 {
5029 if (gui_mch_showing_tabline())
5030 {
5031 POINT pt;
5032 RECT rect;
5033
5034 /*
5035 * If the cursor is on the tabline, display the tab menu
5036 */
5037 GetCursorPos((LPPOINT)&pt);
5038 GetWindowRect(s_textArea, &rect);
5039 if (pt.y < rect.top)
5040 {
5041 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005042 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00005043 }
5044 }
5045 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5046 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005047 case WM_LBUTTONDBLCLK:
5048 {
5049 /*
5050 * If the user double clicked the tabline, create a new tab
5051 */
5052 if (gui_mch_showing_tabline())
5053 {
5054 POINT pt;
5055 RECT rect;
5056
5057 GetCursorPos((LPPOINT)&pt);
5058 GetWindowRect(s_textArea, &rect);
5059 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005060 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005061 }
5062 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5063 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00005064#endif
5065
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 case WM_QUERYENDSESSION: /* System wants to go down. */
5067 gui_shell_closed(); /* Will exit when no changed buffers. */
5068 return FALSE; /* Do NOT allow system to go down. */
5069
5070 case WM_ENDSESSION:
5071 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01005072 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005074 return 0L;
5075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 break;
5077
5078 case WM_CHAR:
5079 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
5080 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005081 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 return 0L;
5083
5084 case WM_SYSCHAR:
5085 /*
5086 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
5087 * shortcut key, handle like a typed ALT key, otherwise call Windows
5088 * ALT key handling.
5089 */
5090#ifdef FEAT_MENU
5091 if ( !gui.menu_is_active
5092 || p_wak[0] == 'n'
5093 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
5094 )
5095#endif
5096 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005097 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 return 0L;
5099 }
5100#ifdef FEAT_MENU
5101 else
5102 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5103#endif
5104
5105 case WM_SYSKEYUP:
5106#ifdef FEAT_MENU
5107 /* This used to be done only when menu is active: ALT key is used for
5108 * that. But that caused problems when menu is disabled and using
5109 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
5110 * are received, mouse pointer remains hidden. */
5111 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5112#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01005113 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114#endif
5115
5116 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005117 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118
5119 case WM_MOUSEWHEEL:
5120 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01005121 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122
Bram Moolenaar520470a2005-06-16 21:59:56 +00005123 /* Notification for change in SystemParametersInfo() */
5124 case WM_SETTINGCHANGE:
5125 return _OnSettingChange((UINT)wParam);
5126
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005127#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 case WM_NOTIFY:
5129 switch (((LPNMHDR) lParam)->code)
5130 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005131# ifdef FEAT_MBYTE
5132 case TTN_GETDISPINFOW:
5133# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005134 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005136 LPNMHDR hdr = (LPNMHDR)lParam;
5137 char_u *str = NULL;
5138 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005139
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005140 vim_free(tt_text);
5141 tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005142
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005143# ifdef FEAT_GUI_TABLINE
5144 if (gui_mch_showing_tabline()
5145 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005146 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005147 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005148 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005149 * Mouse is over the GUI tabline. Display the
5150 * tooltip for the tab under the cursor
5151 *
5152 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005153 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005154 GetCursorPos(&pt);
5155 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005156 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005157 TCHITTESTINFO htinfo;
5158 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005159
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005160 /*
5161 * Get the tab under the cursor
5162 */
5163 htinfo.pt.x = pt.x;
5164 htinfo.pt.y = pt.y;
5165 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
5166 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005167 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005168 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005169
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005170 tp = find_tabpage(idx + 1);
5171 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005172 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005173 get_tabline_label(tp, TRUE);
5174 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005175 }
5176 }
5177 }
5178 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005179# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005180# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005181# ifdef FEAT_GUI_TABLINE
5182 else
5183# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005185 UINT idButton;
5186 vimmenu_T *pMenu;
5187
5188 idButton = (UINT) hdr->idFrom;
5189 pMenu = gui_mswin_find_menu(root_menu, idButton);
5190 if (pMenu)
5191 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005193# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005194 if (str != NULL)
5195 {
5196# ifdef FEAT_MBYTE
5197 if (hdr->code == TTN_GETDISPINFOW)
5198 {
5199 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
5200
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005201 /* Set the maximum width, this also enables using
5202 * \n for line break. */
5203 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5204 0, 500);
5205
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005206 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005207 lpdi->lpszText = tt_text;
5208 /* can't show tooltip if failed */
5209 }
5210 else
5211# endif
5212 {
5213 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
5214
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005215 /* Set the maximum width, this also enables using
5216 * \n for line break. */
5217 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5218 0, 500);
5219
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005220 if (STRLEN(str) < sizeof(lpdi->szText)
5221 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005222 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005223 sizeof(lpdi->szText) - 1);
5224 else
5225 lpdi->lpszText = tt_text;
5226 }
5227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
5229 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005230# ifdef FEAT_GUI_TABLINE
5231 case TCN_SELCHANGE:
5232 if (gui_mch_showing_tabline()
5233 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005234 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005235 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005236 return 0L;
5237 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005238 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00005239
5240 case NM_RCLICK:
5241 if (gui_mch_showing_tabline()
5242 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005243 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00005244 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005245 return 0L;
5246 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00005247 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005248# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005250# ifdef FEAT_GUI_TABLINE
5251 if (gui_mch_showing_tabline()
5252 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
5253 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5254# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 break;
5256 }
5257 break;
5258#endif
5259#if defined(MENUHINTS) && defined(FEAT_MENU)
5260 case WM_MENUSELECT:
5261 if (((UINT) HIWORD(wParam)
5262 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
5263 == MF_HILITE
5264 && (State & CMDLINE) == 0)
5265 {
5266 UINT idButton;
5267 vimmenu_T *pMenu;
5268 static int did_menu_tip = FALSE;
5269
5270 if (did_menu_tip)
5271 {
5272 msg_clr_cmdline();
5273 setcursor();
5274 out_flush();
5275 did_menu_tip = FALSE;
5276 }
5277
5278 idButton = (UINT)LOWORD(wParam);
5279 pMenu = gui_mswin_find_menu(root_menu, idButton);
5280 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
5281 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
5282 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005283 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005285 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 setcursor();
5287 out_flush();
5288 did_menu_tip = TRUE;
5289 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01005290 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
5292 break;
5293#endif
5294 case WM_NCHITTEST:
5295 {
5296 LRESULT result;
5297 int x, y;
5298 int xPos = GET_X_LPARAM(lParam);
5299
5300 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
5301 if (result == HTCLIENT)
5302 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005303#ifdef FEAT_GUI_TABLINE
5304 if (gui_mch_showing_tabline())
5305 {
5306 int yPos = GET_Y_LPARAM(lParam);
5307 RECT rct;
5308
5309 /* If the cursor is on the GUI tabline, don't process this
5310 * event */
5311 GetWindowRect(s_textArea, &rct);
5312 if (yPos < rct.top)
5313 return result;
5314 }
5315#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02005316 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 xPos -= x;
5318
5319 if (xPos < 48) /* <VN> TODO should use system metric? */
5320 return HTBOTTOMLEFT;
5321 else
5322 return HTBOTTOMRIGHT;
5323 }
5324 else
5325 return result;
5326 }
5327 /* break; notreached */
5328
5329#ifdef FEAT_MBYTE_IME
5330 case WM_IME_NOTIFY:
5331 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5332 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005333 return 1L;
5334
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 case WM_IME_COMPOSITION:
5336 if (!_OnImeComposition(hwnd, wParam, lParam))
5337 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005338 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339#endif
5340
5341 default:
5342 if (uMsg == msh_msgmousewheel && msh_msgmousewheel != 0)
5343 { /* handle MSH_MOUSEWHEEL messages for Intellimouse */
5344 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01005345 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 }
5347#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005348 else if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 {
5350 _OnFindRepl();
5351 }
5352#endif
5353 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5354 }
5355
Bram Moolenaar2787ab92011-12-14 15:23:59 +01005356 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357}
5358
5359/*
5360 * End of call-back routines
5361 */
5362
5363/* parent window, if specified with -P */
5364HWND vim_parent_hwnd = NULL;
5365
5366 static BOOL CALLBACK
5367FindWindowTitle(HWND hwnd, LPARAM lParam)
5368{
5369 char buf[2048];
5370 char *title = (char *)lParam;
5371
5372 if (GetWindowText(hwnd, buf, sizeof(buf)))
5373 {
5374 if (strstr(buf, title) != NULL)
5375 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005376 /* Found it. Store the window ref. and quit searching if MDI
5377 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005379 if (vim_parent_hwnd != NULL)
5380 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 }
5382 }
5383 return TRUE; /* continue searching */
5384}
5385
5386/*
5387 * Invoked for '-P "title"' argument: search for parent application to open
5388 * our window in.
5389 */
5390 void
5391gui_mch_set_parent(char *title)
5392{
5393 EnumWindows(FindWindowTitle, (LPARAM)title);
5394 if (vim_parent_hwnd == NULL)
5395 {
5396 EMSG2(_("E671: Cannot find window title \"%s\""), title);
5397 mch_exit(2);
5398 }
5399}
5400
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005401#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 static void
5403ole_error(char *arg)
5404{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005405 char buf[IOSIZE];
5406
5407 /* Can't use EMSG() here, we have not finished initialisation yet. */
5408 vim_snprintf(buf, IOSIZE,
5409 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
5410 arg);
5411 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414
5415/*
5416 * Parse the GUI related command-line arguments. Any arguments used are
5417 * deleted from argv, and *argc is decremented accordingly. This is called
5418 * when vim is started, whether or not the GUI has been started.
5419 */
5420 void
5421gui_mch_prepare(int *argc, char **argv)
5422{
5423 int silent = FALSE;
5424 int idx;
5425
5426 /* Check for special OLE command line parameters */
5427 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5428 {
5429 /* Check for a "-silent" argument first. */
5430 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5431 && (argv[2][0] == '-' || argv[2][0] == '/'))
5432 {
5433 silent = TRUE;
5434 idx = 2;
5435 }
5436 else
5437 idx = 1;
5438
5439 /* Register Vim as an OLE Automation server */
5440 if (STRICMP(argv[idx] + 1, "register") == 0)
5441 {
5442#ifdef FEAT_OLE
5443 RegisterMe(silent);
5444 mch_exit(0);
5445#else
5446 if (!silent)
5447 ole_error("register");
5448 mch_exit(2);
5449#endif
5450 }
5451
5452 /* Unregister Vim as an OLE Automation server */
5453 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5454 {
5455#ifdef FEAT_OLE
5456 UnregisterMe(!silent);
5457 mch_exit(0);
5458#else
5459 if (!silent)
5460 ole_error("unregister");
5461 mch_exit(2);
5462#endif
5463 }
5464
5465 /* Ignore an -embedding argument. It is only relevant if the
5466 * application wants to treat the case when it is started manually
5467 * differently from the case where it is started via automation (and
5468 * we don't).
5469 */
5470 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5471 {
5472#ifdef FEAT_OLE
5473 *argc = 1;
5474#else
5475 ole_error("embedding");
5476 mch_exit(2);
5477#endif
5478 }
5479 }
5480
5481#ifdef FEAT_OLE
5482 {
5483 int bDoRestart = FALSE;
5484
5485 InitOLE(&bDoRestart);
5486 /* automatically exit after registering */
5487 if (bDoRestart)
5488 mch_exit(0);
5489 }
5490#endif
5491
5492#ifdef FEAT_NETBEANS_INTG
5493 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005494 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 int arg;
5496
5497 for (arg = 1; arg < *argc; arg++)
5498 if (strncmp("-nb", argv[arg], 3) == 0)
5499 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 netbeansArg = argv[arg];
5501 mch_memmove(&argv[arg], &argv[arg + 1],
5502 (--*argc - arg) * sizeof(char *));
5503 argv[*argc] = NULL;
5504 break; /* enough? */
5505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 }
5507#endif
5508
5509 /* get the OS version info */
5510 os_version.dwOSVersionInfoSize = sizeof(os_version);
5511 GetVersionEx(&os_version); /* this call works on Win32s, Win95 and WinNT */
5512
5513 /* try and load the user32.dll library and get the entry points for
5514 * multi-monitor-support. */
Bram Moolenaarebbcb822010-10-23 14:02:54 +02005515 if ((user32_lib = vimLoadLib("User32.dll")) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 {
5517 pMonitorFromWindow = (TMonitorFromWindow)GetProcAddress(user32_lib,
5518 "MonitorFromWindow");
5519
5520 /* there are ...A and ...W version of GetMonitorInfo - looking at
5521 * winuser.h, they have exactly the same declaration. */
5522 pGetMonitorInfo = (TGetMonitorInfo)GetProcAddress(user32_lib,
5523 "GetMonitorInfoA");
5524 }
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02005525
5526#ifdef FEAT_MBYTE
5527 /* If the OS is Windows NT, use wide functions;
5528 * this enables common dialogs input unicode from IME. */
5529 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
5530 {
5531 pDispatchMessage = DispatchMessageW;
5532 pGetMessage = GetMessageW;
5533 pIsDialogMessage = IsDialogMessageW;
5534 pPeekMessage = PeekMessageW;
5535 }
5536 else
5537 {
5538 pDispatchMessage = DispatchMessageA;
5539 pGetMessage = GetMessageA;
5540 pIsDialogMessage = IsDialogMessageA;
5541 pPeekMessage = PeekMessageA;
5542 }
5543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544}
5545
5546/*
5547 * Initialise the GUI. Create all the windows, set up all the call-backs
5548 * etc.
5549 */
5550 int
5551gui_mch_init(void)
5552{
5553 const char szVimWndClass[] = VIM_CLASS;
5554 const char szTextAreaClass[] = "VimTextArea";
5555 WNDCLASS wndclass;
5556#ifdef FEAT_MBYTE
5557 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005558 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 WNDCLASSW wndclassw;
5560#endif
5561#ifdef GLOBAL_IME
5562 ATOM atom;
5563#endif
5564
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 /* Return here if the window was already opened (happens when
5566 * gui_mch_dialog() is called early). */
5567 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005568 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569
5570 /*
5571 * Load the tearoff bitmap
5572 */
5573#ifdef FEAT_TEAROFF
5574 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
5575#endif
5576
5577 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5578 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5579#ifdef FEAT_MENU
5580 gui.menu_height = 0; /* Windows takes care of this */
5581#endif
5582 gui.border_width = 0;
5583
5584 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5585
5586#ifdef FEAT_MBYTE
5587 /* First try using the wide version, so that we can use any title.
5588 * Otherwise only characters in the active codepage will work. */
5589 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
5590 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005591 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 wndclassw.lpfnWndProc = _WndProc;
5593 wndclassw.cbClsExtra = 0;
5594 wndclassw.cbWndExtra = 0;
5595 wndclassw.hInstance = s_hinst;
5596 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5597 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5598 wndclassw.hbrBackground = s_brush;
5599 wndclassw.lpszMenuName = NULL;
5600 wndclassw.lpszClassName = szVimWndClassW;
5601
5602 if ((
5603#ifdef GLOBAL_IME
5604 atom =
5605#endif
5606 RegisterClassW(&wndclassw)) == 0)
5607 {
5608 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
5609 return FAIL;
5610
5611 /* Must be Windows 98, fall back to non-wide function. */
5612 }
5613 else
5614 wide_WindowProc = TRUE;
5615 }
5616
5617 if (!wide_WindowProc)
5618#endif
5619
5620 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
5621 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005622 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 wndclass.lpfnWndProc = _WndProc;
5624 wndclass.cbClsExtra = 0;
5625 wndclass.cbWndExtra = 0;
5626 wndclass.hInstance = s_hinst;
5627 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
5628 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5629 wndclass.hbrBackground = s_brush;
5630 wndclass.lpszMenuName = NULL;
5631 wndclass.lpszClassName = szVimWndClass;
5632
5633 if ((
5634#ifdef GLOBAL_IME
5635 atom =
5636#endif
5637 RegisterClass(&wndclass)) == 0)
5638 return FAIL;
5639 }
5640
5641 if (vim_parent_hwnd != NULL)
5642 {
5643#ifdef HAVE_TRY_EXCEPT
5644 __try
5645 {
5646#endif
5647 /* Open inside the specified parent window.
5648 * TODO: last argument should point to a CLIENTCREATESTRUCT
5649 * structure. */
5650 s_hwnd = CreateWindowEx(
5651 WS_EX_MDICHILD,
5652 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005653 WS_OVERLAPPEDWINDOW | WS_CHILD
5654 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5656 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5657 100, /* Any value will do */
5658 100, /* Any value will do */
5659 vim_parent_hwnd, NULL,
5660 s_hinst, NULL);
5661#ifdef HAVE_TRY_EXCEPT
5662 }
5663 __except(EXCEPTION_EXECUTE_HANDLER)
5664 {
5665 /* NOP */
5666 }
5667#endif
5668 if (s_hwnd == NULL)
5669 {
5670 EMSG(_("E672: Unable to open window inside MDI application"));
5671 mch_exit(2);
5672 }
5673 }
5674 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005675 {
5676 /* If the provided windowid is not valid reset it to zero, so that it
5677 * is ignored and we open our own window. */
5678 if (IsWindow((HWND)win_socket_id) <= 0)
5679 win_socket_id = 0;
5680
5681 /* Create a window. If win_socket_id is not zero without border and
5682 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00005684 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005685 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5686 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005687 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5688 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5689 100, /* Any value will do */
5690 100, /* Any value will do */
5691 NULL, NULL,
5692 s_hinst, NULL);
5693 if (s_hwnd != NULL && win_socket_id != 0)
5694 {
5695 SetParent(s_hwnd, (HWND)win_socket_id);
5696 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5697 }
5698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699
5700 if (s_hwnd == NULL)
5701 return FAIL;
5702
5703#ifdef GLOBAL_IME
5704 global_ime_init(atom, s_hwnd);
5705#endif
5706#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5707 dyn_imm_load();
5708#endif
5709
5710 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005711#ifdef FEAT_MBYTE
5712 if (wide_WindowProc)
5713 {
5714 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
5715 {
5716 wndclassw.style = CS_OWNDC;
5717 wndclassw.lpfnWndProc = _TextAreaWndProc;
5718 wndclassw.cbClsExtra = 0;
5719 wndclassw.cbWndExtra = 0;
5720 wndclassw.hInstance = s_hinst;
5721 wndclassw.hIcon = NULL;
5722 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5723 wndclassw.hbrBackground = NULL;
5724 wndclassw.lpszMenuName = NULL;
5725 wndclassw.lpszClassName = szTextAreaClassW;
5726
5727 if (RegisterClassW(&wndclassw) == 0)
5728 return FAIL;
5729 }
5730 }
5731 else
5732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
5734 {
5735 wndclass.style = CS_OWNDC;
5736 wndclass.lpfnWndProc = _TextAreaWndProc;
5737 wndclass.cbClsExtra = 0;
5738 wndclass.cbWndExtra = 0;
5739 wndclass.hInstance = s_hinst;
5740 wndclass.hIcon = NULL;
5741 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5742 wndclass.hbrBackground = NULL;
5743 wndclass.lpszMenuName = NULL;
5744 wndclass.lpszClassName = szTextAreaClass;
5745
5746 if (RegisterClass(&wndclass) == 0)
5747 return FAIL;
5748 }
5749 s_textArea = CreateWindowEx(
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005750 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 szTextAreaClass, "Vim text area",
5752 WS_CHILD | WS_VISIBLE, 0, 0,
5753 100, /* Any value will do for now */
5754 100, /* Any value will do for now */
5755 s_hwnd, NULL,
5756 s_hinst, NULL);
5757
5758 if (s_textArea == NULL)
5759 return FAIL;
5760
Bram Moolenaar20321902016-02-17 12:30:17 +01005761#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005762 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5763 {
5764 HANDLE hIcon = NULL;
5765
5766 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005767 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005768 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005769#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005770
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771#ifdef FEAT_MENU
5772 s_menuBar = CreateMenu();
5773#endif
5774 s_hdc = GetDC(s_textArea);
5775
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776#ifdef FEAT_WINDOWS
5777 DragAcceptFiles(s_hwnd, TRUE);
5778#endif
5779
5780 /* Do we need to bother with this? */
5781 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5782
5783 /* Get background/foreground colors from the system */
5784 gui_mch_def_colors();
5785
5786 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5787 * file) */
5788 set_normal_colors();
5789
5790 /*
5791 * Check that none of the colors are the same as the background color.
5792 * Then store the current values as the defaults.
5793 */
5794 gui_check_colors();
5795 gui.def_norm_pixel = gui.norm_pixel;
5796 gui.def_back_pixel = gui.back_pixel;
5797
5798 /* Get the colors for the highlight groups (gui_check_colors() might have
5799 * changed them) */
5800 highlight_gui_started();
5801
5802 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005803 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005805 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806
5807 /*
5808 * Set up for Intellimouse processing
5809 */
5810 init_mouse_wheel();
5811
5812 /*
5813 * compute a couple of metrics used for the dialogs
5814 */
5815 get_dialog_font_metrics();
5816#ifdef FEAT_TOOLBAR
5817 /*
5818 * Create the toolbar
5819 */
5820 initialise_toolbar();
5821#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005822#ifdef FEAT_GUI_TABLINE
5823 /*
5824 * Create the tabline
5825 */
5826 initialise_tabline();
5827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828#ifdef MSWIN_FIND_REPLACE
5829 /*
5830 * Initialise the dialog box stuff
5831 */
5832 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5833
5834 /* Initialise the struct */
5835 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005836 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005838 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5840 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5841 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00005842# if defined(FEAT_MBYTE) && defined(WIN3264)
5843 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
5844 s_findrep_struct_w.lpstrFindWhat =
5845 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5846 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
5847 s_findrep_struct_w.lpstrReplaceWith =
5848 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5849 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
5850 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
5851 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005855#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005856# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5857/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5858# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005859# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005860# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005861# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005862 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005863 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005864#endif
5865
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005866#ifdef FEAT_RENDER_OPTIONS
5867 if (p_rop)
5868 (void)gui_mch_set_rendering_options(p_rop);
5869#endif
5870
Bram Moolenaar748bf032005-02-02 23:04:36 +00005871theend:
5872 /* Display any pending error messages */
5873 display_errors();
5874
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 return OK;
5876}
5877
5878/*
5879 * Get the size of the screen, taking position on multiple monitors into
5880 * account (if supported).
5881 */
5882 static void
5883get_work_area(RECT *spi_rect)
5884{
5885 _HMONITOR mon;
5886 _MONITORINFO moninfo;
5887
5888 /* use these functions only if available */
5889 if (pMonitorFromWindow != NULL && pGetMonitorInfo != NULL)
5890 {
5891 /* work out which monitor the window is on, and get *it's* work area */
5892 mon = pMonitorFromWindow(s_hwnd, 1 /*MONITOR_DEFAULTTOPRIMARY*/);
5893 if (mon != NULL)
5894 {
5895 moninfo.cbSize = sizeof(_MONITORINFO);
5896 if (pGetMonitorInfo(mon, &moninfo))
5897 {
5898 *spi_rect = moninfo.rcWork;
5899 return;
5900 }
5901 }
5902 }
5903 /* this is the old method... */
5904 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5905}
5906
5907/*
5908 * Set the size of the window to the given width and height in pixels.
5909 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005910/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 void
5912gui_mch_set_shellsize(int width, int height,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005913 int min_width, int min_height, int base_width, int base_height,
5914 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915{
5916 RECT workarea_rect;
5917 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 WINDOWPLACEMENT wndpl;
5919
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005920 /* Try to keep window completely on screen. */
5921 /* Get position of the screen work area. This is the part that is not
5922 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 get_work_area(&workarea_rect);
5924
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005925 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005926 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 wndpl.length = sizeof(WINDOWPLACEMENT);
5928 GetWindowPlacement(s_hwnd, &wndpl);
5929
5930 /* Resizing a maximized window looks very strange, unzoom it first.
5931 * But don't do it when still starting up, it may have been requested in
5932 * the shortcut. */
5933 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5934 {
5935 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5936 /* Need to get the settings of the normal window. */
5937 GetWindowPlacement(s_hwnd, &wndpl);
5938 }
5939
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005941 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005942 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005943 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005944 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 + GetSystemMetrics(SM_CYCAPTION)
5946#ifdef FEAT_MENU
5947 + gui_mswin_get_menu_height(FALSE)
5948#endif
5949 ;
5950
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005951 /* The following should take care of keeping Vim on the same monitor, no
5952 * matter if the secondary monitor is left or right of the primary
5953 * monitor. */
5954 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5955 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005956
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005957 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005958 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005959 && wndpl.rcNormalPosition.right > workarea_rect.right)
5960 OffsetRect(&wndpl.rcNormalPosition,
5961 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005963 if ((direction & RESIZE_HOR)
5964 && wndpl.rcNormalPosition.left < workarea_rect.left)
5965 OffsetRect(&wndpl.rcNormalPosition,
5966 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967
Bram Moolenaarafa24992006-03-27 20:58:26 +00005968 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005969 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5970 OffsetRect(&wndpl.rcNormalPosition,
5971 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005973 if ((direction & RESIZE_VERT)
5974 && wndpl.rcNormalPosition.top < workarea_rect.top)
5975 OffsetRect(&wndpl.rcNormalPosition,
5976 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977
5978 /* set window position - we should use SetWindowPlacement rather than
5979 * SetWindowPos as the MSDN docs say the coord systems returned by
5980 * these two are not compatible. */
5981 SetWindowPlacement(s_hwnd, &wndpl);
5982
5983 SetActiveWindow(s_hwnd);
5984 SetFocus(s_hwnd);
5985
5986#ifdef FEAT_MENU
5987 /* Menu may wrap differently now */
5988 gui_mswin_get_menu_height(!gui.starting);
5989#endif
5990}
5991
5992
5993 void
5994gui_mch_set_scrollbar_thumb(
5995 scrollbar_T *sb,
5996 long val,
5997 long size,
5998 long max)
5999{
6000 SCROLLINFO info;
6001
6002 sb->scroll_shift = 0;
6003 while (max > 32767)
6004 {
6005 max = (max + 1) >> 1;
6006 val >>= 1;
6007 size >>= 1;
6008 ++sb->scroll_shift;
6009 }
6010
6011 if (sb->scroll_shift > 0)
6012 ++size;
6013
6014 info.cbSize = sizeof(info);
6015 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
6016 info.nPos = val;
6017 info.nMin = 0;
6018 info.nMax = max;
6019 info.nPage = size;
6020 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
6021}
6022
6023
6024/*
6025 * Set the current text font.
6026 */
6027 void
6028gui_mch_set_font(GuiFont font)
6029{
6030 gui.currFont = font;
6031}
6032
6033
6034/*
6035 * Set the current text foreground color.
6036 */
6037 void
6038gui_mch_set_fg_color(guicolor_T color)
6039{
6040 gui.currFgColor = color;
6041}
6042
6043/*
6044 * Set the current text background color.
6045 */
6046 void
6047gui_mch_set_bg_color(guicolor_T color)
6048{
6049 gui.currBgColor = color;
6050}
6051
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006052/*
6053 * Set the current text special color.
6054 */
6055 void
6056gui_mch_set_sp_color(guicolor_T color)
6057{
6058 gui.currSpColor = color;
6059}
6060
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061#if defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)
6062/*
6063 * Multi-byte handling, originally by Sung-Hoon Baek.
6064 * First static functions (no prototypes generated).
6065 */
6066#ifdef _MSC_VER
6067# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
6068#endif
6069#include <imm.h>
6070
6071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 * handle WM_IME_NOTIFY message
6073 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006074/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 static LRESULT
6076_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData)
6077{
6078 LRESULT lResult = 0;
6079 HIMC hImc;
6080
6081 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
6082 return lResult;
6083 switch (dwCommand)
6084 {
6085 case IMN_SETOPENSTATUS:
6086 if (pImmGetOpenStatus(hImc))
6087 {
6088 pImmSetCompositionFont(hImc, &norm_logfont);
6089 im_set_position(gui.row, gui.col);
6090
6091 /* Disable langmap */
6092 State &= ~LANGMAP;
6093 if (State & INSERT)
6094 {
6095#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
6096 /* Unshown 'keymap' in status lines */
6097 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
6098 {
6099 /* Save cursor position */
6100 int old_row = gui.row;
6101 int old_col = gui.col;
6102
6103 // This must be called here before
6104 // status_redraw_curbuf(), otherwise the mode
6105 // message may appear in the wrong position.
6106 showmode();
6107 status_redraw_curbuf();
6108 update_screen(0);
6109 /* Restore cursor position */
6110 gui.row = old_row;
6111 gui.col = old_col;
6112 }
6113#endif
6114 }
6115 }
6116 gui_update_cursor(TRUE, FALSE);
6117 lResult = 0;
6118 break;
6119 }
6120 pImmReleaseContext(hWnd, hImc);
6121 return lResult;
6122}
6123
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006124/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 static LRESULT
6126_OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param)
6127{
6128 char_u *ret;
6129 int len;
6130
6131 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
6132 return 0;
6133
6134 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
6135 if (ret != NULL)
6136 {
6137 add_to_input_buf_csi(ret, len);
6138 vim_free(ret);
6139 return 1;
6140 }
6141 return 0;
6142}
6143
6144/*
6145 * get the current composition string, in UCS-2; *lenp is the number of
6146 * *lenp is the number of Unicode characters.
6147 */
6148 static short_u *
6149GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
6150{
6151 LONG ret;
6152 LPWSTR wbuf = NULL;
6153 char_u *buf;
6154
6155 if (!pImmGetContext)
6156 return NULL; /* no imm32.dll */
6157
6158 /* Try Unicode; this'll always work on NT regardless of codepage. */
6159 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
6160 if (ret == 0)
6161 return NULL; /* empty */
6162
6163 if (ret > 0)
6164 {
6165 /* Allocate the requested buffer plus space for the NUL character. */
6166 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
6167 if (wbuf != NULL)
6168 {
6169 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
6170 *lenp = ret / sizeof(WCHAR);
6171 }
6172 return (short_u *)wbuf;
6173 }
6174
6175 /* ret < 0; we got an error, so try the ANSI version. This'll work
6176 * on 9x/ME, but only if the codepage happens to be set to whatever
6177 * we're inputting. */
6178 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
6179 if (ret <= 0)
6180 return NULL; /* empty or error */
6181
6182 buf = alloc(ret);
6183 if (buf == NULL)
6184 return NULL;
6185 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
6186
6187 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006188 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 vim_free(buf);
6190
6191 return (short_u *)wbuf;
6192}
6193
6194/*
6195 * void GetResultStr()
6196 *
6197 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
6198 * get complete composition string
6199 */
6200 static char_u *
6201GetResultStr(HWND hwnd, int GCS, int *lenp)
6202{
6203 HIMC hIMC; /* Input context handle. */
6204 short_u *buf = NULL;
6205 char_u *convbuf = NULL;
6206
6207 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
6208 return NULL;
6209
6210 /* Reads in the composition string. */
6211 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
6212 if (buf == NULL)
6213 return NULL;
6214
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006215 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 pImmReleaseContext(hwnd, hIMC);
6217 vim_free(buf);
6218 return convbuf;
6219}
6220#endif
6221
6222/* For global functions we need prototypes. */
6223#if (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) || defined(PROTO)
6224
6225/*
6226 * set font to IM.
6227 */
6228 void
6229im_set_font(LOGFONT *lf)
6230{
6231 HIMC hImc;
6232
6233 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6234 {
6235 pImmSetCompositionFont(hImc, lf);
6236 pImmReleaseContext(s_hwnd, hImc);
6237 }
6238}
6239
6240/*
6241 * Notify cursor position to IM.
6242 */
6243 void
6244im_set_position(int row, int col)
6245{
6246 HIMC hImc;
6247
6248 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6249 {
6250 COMPOSITIONFORM cfs;
6251
6252 cfs.dwStyle = CFS_POINT;
6253 cfs.ptCurrentPos.x = FILL_X(col);
6254 cfs.ptCurrentPos.y = FILL_Y(row);
6255 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
6256 pImmSetCompositionWindow(hImc, &cfs);
6257
6258 pImmReleaseContext(s_hwnd, hImc);
6259 }
6260}
6261
6262/*
6263 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6264 */
6265 void
6266im_set_active(int active)
6267{
6268 HIMC hImc;
6269 static HIMC hImcOld = (HIMC)0;
6270
6271 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
6272 {
6273 if (p_imdisable)
6274 {
6275 if (hImcOld == (HIMC)0)
6276 {
6277 hImcOld = pImmGetContext(s_hwnd);
6278 if (hImcOld)
6279 pImmAssociateContext(s_hwnd, (HIMC)0);
6280 }
6281 active = FALSE;
6282 }
6283 else if (hImcOld != (HIMC)0)
6284 {
6285 pImmAssociateContext(s_hwnd, hImcOld);
6286 hImcOld = (HIMC)0;
6287 }
6288
6289 hImc = pImmGetContext(s_hwnd);
6290 if (hImc)
6291 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006292 /*
6293 * for Korean ime
6294 */
6295 HKL hKL = GetKeyboardLayout(0);
6296
6297 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6298 {
6299 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6300 static BOOL bSaved = FALSE;
6301
6302 if (active)
6303 {
6304 /* if we have a saved conversion status, restore it */
6305 if (bSaved)
6306 pImmSetConversionStatus(hImc, dwConversionSaved,
6307 dwSentenceSaved);
6308 bSaved = FALSE;
6309 }
6310 else
6311 {
6312 /* save conversion status and disable korean */
6313 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6314 &dwSentenceSaved))
6315 {
6316 bSaved = TRUE;
6317 pImmSetConversionStatus(hImc,
6318 dwConversionSaved & ~(IME_CMODE_NATIVE
6319 | IME_CMODE_FULLSHAPE),
6320 dwSentenceSaved);
6321 }
6322 }
6323 }
6324
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 pImmSetOpenStatus(hImc, active);
6326 pImmReleaseContext(s_hwnd, hImc);
6327 }
6328 }
6329}
6330
6331/*
6332 * Get IM status. When IM is on, return not 0. Else return 0.
6333 */
6334 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006335im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336{
6337 int status = 0;
6338 HIMC hImc;
6339
6340 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6341 {
6342 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6343 pImmReleaseContext(s_hwnd, hImc);
6344 }
6345 return status;
6346}
6347
6348#endif /* FEAT_MBYTE && FEAT_MBYTE_IME */
6349
6350#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
6351/* Win32 with GLOBAL IME */
6352
6353/*
6354 * Notify cursor position to IM.
6355 */
6356 void
6357im_set_position(int row, int col)
6358{
6359 /* Win32 with GLOBAL IME */
6360 POINT p;
6361
6362 p.x = FILL_X(col);
6363 p.y = FILL_Y(row);
6364 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
6365 global_ime_set_position(&p);
6366}
6367
6368/*
6369 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6370 */
6371 void
6372im_set_active(int active)
6373{
6374 global_ime_set_status(active);
6375}
6376
6377/*
6378 * Get IM status. When IM is on, return not 0. Else return 0.
6379 */
6380 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006381im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382{
6383 return global_ime_get_status();
6384}
6385#endif
6386
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006387#ifdef FEAT_MBYTE
6388/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006389 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006390 */
6391 static void
6392latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6393{
6394 int c;
6395
Bram Moolenaarca003e12006-03-17 23:19:38 +00006396 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006397 {
6398 c = *text++;
6399 switch (c)
6400 {
6401 case 0xa4: c = 0x20ac; break; /* euro */
6402 case 0xa6: c = 0x0160; break; /* S hat */
6403 case 0xa8: c = 0x0161; break; /* S -hat */
6404 case 0xb4: c = 0x017d; break; /* Z hat */
6405 case 0xb8: c = 0x017e; break; /* Z -hat */
6406 case 0xbc: c = 0x0152; break; /* OE */
6407 case 0xbd: c = 0x0153; break; /* oe */
6408 case 0xbe: c = 0x0178; break; /* Y */
6409 }
6410 *unicodebuf++ = c;
6411 }
6412}
6413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414
6415#ifdef FEAT_RIGHTLEFT
6416/*
6417 * What is this for? In the case where you are using Win98 or Win2K or later,
6418 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6419 * reverses the string sent to the TextOut... family. This sucks, because we
6420 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6421 * way to tell Windblows not to do this!
6422 *
6423 * The short of it is that this 'RevOut' only gets called if you are running
6424 * one of the new, "improved" MS OSes, and only if you are running in
6425 * 'rightleft' mode. It makes display take *slightly* longer, but not
6426 * noticeably so.
6427 */
6428 static void
6429RevOut( HDC s_hdc,
6430 int col,
6431 int row,
6432 UINT foptions,
6433 CONST RECT *pcliprect,
6434 LPCTSTR text,
6435 UINT len,
6436 CONST INT *padding)
6437{
6438 int ix;
6439 static int special = -1;
6440
6441 if (special == -1)
6442 {
6443 /* Check windows version: special treatment is needed if it is NT 5 or
6444 * Win98 or higher. */
6445 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
6446 && os_version.dwMajorVersion >= 5)
6447 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
6448 && (os_version.dwMajorVersion > 4
6449 || (os_version.dwMajorVersion == 4
6450 && os_version.dwMinorVersion > 0))))
6451 special = 1;
6452 else
6453 special = 0;
6454 }
6455
6456 if (special)
6457 for (ix = 0; ix < (int)len; ++ix)
6458 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6459 pcliprect, text + ix, 1, padding);
6460 else
6461 ExtTextOut(s_hdc, col, row, foptions, pcliprect, text, len, padding);
6462}
6463#endif
6464
6465 void
6466gui_mch_draw_string(
6467 int row,
6468 int col,
6469 char_u *text,
6470 int len,
6471 int flags)
6472{
6473 static int *padding = NULL;
6474 static int pad_size = 0;
6475 int i;
6476 const RECT *pcliprect = NULL;
6477 UINT foptions = 0;
6478#ifdef FEAT_MBYTE
6479 static WCHAR *unicodebuf = NULL;
6480 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006481 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 int n = 0;
6483#endif
6484 HPEN hpen, old_pen;
6485 int y;
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006486#ifdef FEAT_DIRECTX
6487 int font_is_ttf_or_vector = 0;
6488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 /*
6491 * Italic and bold text seems to have an extra row of pixels at the bottom
6492 * (below where the bottom of the character should be). If we draw the
6493 * characters with a solid background, the top row of pixels in the
6494 * character below will be overwritten. We can fix this by filling in the
6495 * background ourselves, to the correct character proportions, and then
6496 * writing the character in transparent mode. Still have a problem when
6497 * the character is "_", which gets written on to the character below.
6498 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6499 * pixel in their slots, which fixes the problem with the bottom row of
6500 * pixels. We still need this code because otherwise the top row of pixels
6501 * becomes a problem. - webb.
6502 */
6503 static HBRUSH hbr_cache[2] = {NULL, NULL};
6504 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6505 static int brush_lru = 0;
6506 HBRUSH hbr;
6507 RECT rc;
6508
6509 if (!(flags & DRAW_TRANSP))
6510 {
6511 /*
6512 * Clear background first.
6513 * Note: FillRect() excludes right and bottom of rectangle.
6514 */
6515 rc.left = FILL_X(col);
6516 rc.top = FILL_Y(row);
6517#ifdef FEAT_MBYTE
6518 if (has_mbyte)
6519 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006521 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 }
6523 else
6524#endif
6525 rc.right = FILL_X(col + len);
6526 rc.bottom = FILL_Y(row + 1);
6527
6528 /* Cache the created brush, that saves a lot of time. We need two:
6529 * one for cursor background and one for the normal background. */
6530 if (gui.currBgColor == brush_color[0])
6531 {
6532 hbr = hbr_cache[0];
6533 brush_lru = 1;
6534 }
6535 else if (gui.currBgColor == brush_color[1])
6536 {
6537 hbr = hbr_cache[1];
6538 brush_lru = 0;
6539 }
6540 else
6541 {
6542 if (hbr_cache[brush_lru] != NULL)
6543 DeleteBrush(hbr_cache[brush_lru]);
6544 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6545 brush_color[brush_lru] = gui.currBgColor;
6546 hbr = hbr_cache[brush_lru];
6547 brush_lru = !brush_lru;
6548 }
6549 FillRect(s_hdc, &rc, hbr);
6550
6551 SetBkMode(s_hdc, TRANSPARENT);
6552
6553 /*
6554 * When drawing block cursor, prevent inverted character spilling
6555 * over character cell (can happen with bold/italic)
6556 */
6557 if (flags & DRAW_CURSOR)
6558 {
6559 pcliprect = &rc;
6560 foptions = ETO_CLIPPED;
6561 }
6562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 SetTextColor(s_hdc, gui.currFgColor);
6564 SelectFont(s_hdc, gui.currFont);
6565
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006566#ifdef FEAT_DIRECTX
6567 if (IS_ENABLE_DIRECTX())
6568 {
6569 TEXTMETRIC tm;
6570
6571 GetTextMetrics(s_hdc, &tm);
6572 if (tm.tmPitchAndFamily & (TMPF_TRUETYPE | TMPF_VECTOR))
6573 {
6574 font_is_ttf_or_vector = 1;
6575 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
6576 }
6577 }
6578#endif
6579
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6581 {
6582 vim_free(padding);
6583 pad_size = Columns;
6584
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006585 /* Don't give an out-of-memory message here, it would call us
6586 * recursively. */
6587 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 if (padding != NULL)
6589 for (i = 0; i < pad_size; i++)
6590 padding[i] = gui.char_width;
6591 }
6592
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 /*
6594 * We have to provide the padding argument because italic and bold versions
6595 * of fixed-width fonts are often one pixel or so wider than their normal
6596 * versions.
6597 * No check for DRAW_BOLD, Windows will have done it already.
6598 */
6599
6600#ifdef FEAT_MBYTE
6601 /* Check if there are any UTF-8 characters. If not, use normal text
6602 * output to speed up output. */
6603 if (enc_utf8)
6604 for (n = 0; n < len; ++n)
6605 if (text[n] >= 0x80)
6606 break;
6607
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006608#if defined(FEAT_DIRECTX)
6609 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6610 * required that unicode drawing routine, currently. So this forces it
6611 * enabled. */
6612 if (enc_utf8 && IS_ENABLE_DIRECTX())
6613 n = 0; /* Keep n < len, to enter block for unicode. */
6614#endif
6615
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006617 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006619 if ((enc_utf8
6620 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6621 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 && (unicodebuf == NULL || len > unibuflen))
6623 {
6624 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006625 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626
6627 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006628 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629
6630 unibuflen = len;
6631 }
6632
6633 if (enc_utf8 && n < len && unicodebuf != NULL)
6634 {
6635 /* Output UTF-8 characters. Caller has already separated
6636 * composing characters. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006637 int i;
6638 int wlen; /* string length in words */
6639 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 int cells; /* cell width of string up to composing char */
6641 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006642 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006644 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006645 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006647 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006649 c = utf_ptr2char(text + i);
6650 if (c >= 0x10000)
6651 {
6652 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006653 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6654 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006655 }
6656 else
6657 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006658 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006659 }
6660 cw = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 if (cw > 2) /* don't use 4 for unprintable char */
6662 cw = 1;
6663 if (unicodepdy != NULL)
6664 {
6665 /* Use unicodepdy to make characters fit as we expect, even
6666 * when the font uses different widths (e.g., bold character
6667 * is wider). */
6668 unicodepdy[clen] = cw * gui.char_width;
6669 }
6670 cells += cw;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006671 i += utfc_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006672 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006674#if defined(FEAT_DIRECTX)
6675 if (IS_ENABLE_DIRECTX() && font_is_ttf_or_vector)
6676 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006677 /* Add one to "cells" for italics. */
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006678 DWriteContext_DrawText(s_dwc, s_hdc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006679 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006680 gui.char_width, gui.currFgColor);
6681 }
6682 else
6683#endif
6684 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6685 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 len = cells; /* used for underlining */
6687 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006688 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 {
6690 /* If we want to display codepage data, and the current CP is not the
6691 * ANSI one, we need to go via Unicode. */
6692 if (unicodebuf != NULL)
6693 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006694 if (enc_latin9)
6695 latin9_to_ucs(text, len, unicodebuf);
6696 else
6697 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 MB_PRECOMPOSED,
6699 (char *)text, len,
6700 (LPWSTR)unicodebuf, unibuflen);
6701 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006702 {
6703 /* Use unicodepdy to make characters fit as we expect, even
6704 * when the font uses different widths (e.g., bold character
6705 * is wider). */
6706 if (unicodepdy != NULL)
6707 {
6708 int i;
6709 int cw;
6710
6711 for (i = 0; i < len; ++i)
6712 {
6713 cw = utf_char2cells(unicodebuf[i]);
6714 if (cw > 2)
6715 cw = 1;
6716 unicodepdy[i] = cw * gui.char_width;
6717 }
6718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006720 foptions, pcliprect, unicodebuf, len, unicodepdy);
6721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 }
6723 }
6724 else
6725#endif
6726 {
6727#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006728 /* Windows will mess up RL text, so we have to draw it character by
6729 * character. Only do this if RL is on, since it's slow. */
6730 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6732 foptions, pcliprect, (char *)text, len, padding);
6733 else
6734#endif
6735 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6736 foptions, pcliprect, (char *)text, len, padding);
6737 }
6738
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006739 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 if (flags & DRAW_UNDERL)
6741 {
6742 hpen = CreatePen(PS_SOLID, 1, gui.currFgColor);
6743 old_pen = SelectObject(s_hdc, hpen);
6744 /* When p_linespace is 0, overwrite the bottom row of pixels.
6745 * Otherwise put the line just below the character. */
6746 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 if (p_linespace > 1)
6748 y -= p_linespace - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 MoveToEx(s_hdc, FILL_X(col), y, NULL);
6750 /* Note: LineTo() excludes the last pixel in the line. */
6751 LineTo(s_hdc, FILL_X(col + len), y);
6752 DeleteObject(SelectObject(s_hdc, old_pen));
6753 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006754
6755 /* Undercurl */
6756 if (flags & DRAW_UNDERC)
6757 {
6758 int x;
6759 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006760 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006761
6762 y = FILL_Y(row + 1) - 1;
6763 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6764 {
6765 offset = val[x % 8];
6766 SetPixel(s_hdc, x, y - offset, gui.currSpColor);
6767 }
6768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769}
6770
6771
6772/*
6773 * Output routines.
6774 */
6775
6776/* Flush any output to the screen */
6777 void
6778gui_mch_flush(void)
6779{
6780# if defined(__BORLANDC__)
6781 /*
6782 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6783 * prototype declaration.
6784 * The compiler complains if __stdcall is not used in both declarations.
6785 */
6786 BOOL __stdcall GdiFlush(void);
6787# endif
6788
6789 GdiFlush();
6790}
6791
6792 static void
6793clear_rect(RECT *rcp)
6794{
6795 HBRUSH hbr;
6796
6797 hbr = CreateSolidBrush(gui.back_pixel);
6798 FillRect(s_hdc, rcp, hbr);
6799 DeleteBrush(hbr);
6800}
6801
6802
Bram Moolenaarc716c302006-01-21 22:12:51 +00006803 void
6804gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6805{
6806 RECT workarea_rect;
6807
6808 get_work_area(&workarea_rect);
6809
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006810 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006811 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006812 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006813
6814 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6815 * the menubar for MSwin, we subtract it from the screen height, so that
6816 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006817 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006818 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006819 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006820 - GetSystemMetrics(SM_CYCAPTION)
6821#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006822 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006823#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006824 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006825}
6826
6827
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828#if defined(FEAT_MENU) || defined(PROTO)
6829/*
6830 * Add a sub menu to the menu bar.
6831 */
6832 void
6833gui_mch_add_menu(
6834 vimmenu_T *menu,
6835 int pos)
6836{
6837 vimmenu_T *parent = menu->parent;
6838
6839 menu->submenu_id = CreatePopupMenu();
6840 menu->id = s_menu_id++;
6841
6842 if (menu_is_menubar(menu->name))
6843 {
6844 if (is_winnt_3())
6845 {
6846 InsertMenu((parent == NULL) ? s_menuBar : parent->submenu_id,
6847 (UINT)pos, MF_POPUP | MF_STRING | MF_BYPOSITION,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006848 (long_u)menu->submenu_id, (LPCTSTR) menu->name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 }
6850 else
6851 {
6852#ifdef FEAT_MBYTE
6853 WCHAR *wn = NULL;
6854 int n;
6855
6856 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6857 {
6858 /* 'encoding' differs from active codepage: convert menu name
6859 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006860 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 if (wn != NULL)
6862 {
6863 MENUITEMINFOW infow;
6864
6865 infow.cbSize = sizeof(infow);
6866 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6867 | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006868 infow.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 infow.wID = menu->id;
6870 infow.fType = MFT_STRING;
6871 infow.dwTypeData = wn;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006872 infow.cch = (UINT)wcslen(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 infow.hSubMenu = menu->submenu_id;
6874 n = InsertMenuItemW((parent == NULL)
6875 ? s_menuBar : parent->submenu_id,
6876 (UINT)pos, TRUE, &infow);
6877 vim_free(wn);
6878 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
6879 /* Failed, try using non-wide function. */
6880 wn = NULL;
6881 }
6882 }
6883
6884 if (wn == NULL)
6885#endif
6886 {
6887 MENUITEMINFO info;
6888
6889 info.cbSize = sizeof(info);
6890 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006891 info.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 info.wID = menu->id;
6893 info.fType = MFT_STRING;
6894 info.dwTypeData = (LPTSTR)menu->name;
6895 info.cch = (UINT)STRLEN(menu->name);
6896 info.hSubMenu = menu->submenu_id;
6897 InsertMenuItem((parent == NULL)
6898 ? s_menuBar : parent->submenu_id,
6899 (UINT)pos, TRUE, &info);
6900 }
6901 }
6902 }
6903
6904 /* Fix window size if menu may have wrapped */
6905 if (parent == NULL)
6906 gui_mswin_get_menu_height(!gui.starting);
6907#ifdef FEAT_TEAROFF
6908 else if (IsWindow(parent->tearoff_handle))
6909 rebuild_tearoff(parent);
6910#endif
6911}
6912
6913 void
6914gui_mch_show_popupmenu(vimmenu_T *menu)
6915{
6916 POINT mp;
6917
6918 (void)GetCursorPos((LPPOINT)&mp);
6919 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6920}
6921
6922 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006923gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924{
6925 vimmenu_T *menu = gui_find_menu(path_name);
6926
6927 if (menu != NULL)
6928 {
6929 POINT p;
6930
6931 /* Find the position of the current cursor */
6932 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006933 if (mouse_pos)
6934 {
6935 int mx, my;
6936
6937 gui_mch_getmouse(&mx, &my);
6938 p.x += mx;
6939 p.y += my;
6940 }
6941 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 {
6943 p.x += TEXT_X(W_WINCOL(curwin) + curwin->w_wcol + 1);
6944 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6945 }
6946 msg_scroll = FALSE;
6947 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6948 }
6949}
6950
6951#if defined(FEAT_TEAROFF) || defined(PROTO)
6952/*
6953 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6954 * create it as a pseudo-"tearoff menu".
6955 */
6956 void
6957gui_make_tearoff(char_u *path_name)
6958{
6959 vimmenu_T *menu = gui_find_menu(path_name);
6960
6961 /* Found the menu, so tear it off. */
6962 if (menu != NULL)
6963 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6964}
6965#endif
6966
6967/*
6968 * Add a menu item to a menu
6969 */
6970 void
6971gui_mch_add_menu_item(
6972 vimmenu_T *menu,
6973 int idx)
6974{
6975 vimmenu_T *parent = menu->parent;
6976
6977 menu->id = s_menu_id++;
6978 menu->submenu_id = NULL;
6979
6980#ifdef FEAT_TEAROFF
6981 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6982 {
6983 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6984 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6985 }
6986 else
6987#endif
6988#ifdef FEAT_TOOLBAR
6989 if (menu_is_toolbar(parent->name))
6990 {
6991 TBBUTTON newtb;
6992
6993 vim_memset(&newtb, 0, sizeof(newtb));
6994 if (menu_is_separator(menu->name))
6995 {
6996 newtb.iBitmap = 0;
6997 newtb.fsStyle = TBSTYLE_SEP;
6998 }
6999 else
7000 {
7001 newtb.iBitmap = get_toolbar_bitmap(menu);
7002 newtb.fsStyle = TBSTYLE_BUTTON;
7003 }
7004 newtb.idCommand = menu->id;
7005 newtb.fsState = TBSTATE_ENABLED;
7006 newtb.iString = 0;
7007 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
7008 (LPARAM)&newtb);
7009 menu->submenu_id = (HMENU)-1;
7010 }
7011 else
7012#endif
7013 {
7014#ifdef FEAT_MBYTE
7015 WCHAR *wn = NULL;
7016 int n;
7017
7018 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
7019 {
7020 /* 'encoding' differs from active codepage: convert menu item name
7021 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007022 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 if (wn != NULL)
7024 {
7025 n = InsertMenuW(parent->submenu_id, (UINT)idx,
7026 (menu_is_separator(menu->name)
7027 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
7028 (UINT)menu->id, wn);
7029 vim_free(wn);
7030 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
7031 /* Failed, try using non-wide function. */
7032 wn = NULL;
7033 }
7034 }
7035 if (wn == NULL)
7036#endif
7037 InsertMenu(parent->submenu_id, (UINT)idx,
7038 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
7039 | MF_BYPOSITION,
7040 (UINT)menu->id, (LPCTSTR)menu->name);
7041#ifdef FEAT_TEAROFF
7042 if (IsWindow(parent->tearoff_handle))
7043 rebuild_tearoff(parent);
7044#endif
7045 }
7046}
7047
7048/*
7049 * Destroy the machine specific menu widget.
7050 */
7051 void
7052gui_mch_destroy_menu(vimmenu_T *menu)
7053{
7054#ifdef FEAT_TOOLBAR
7055 /*
7056 * is this a toolbar button?
7057 */
7058 if (menu->submenu_id == (HMENU)-1)
7059 {
7060 int iButton;
7061
7062 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
7063 (WPARAM)menu->id, 0);
7064 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
7065 }
7066 else
7067#endif
7068 {
7069 if (menu->parent != NULL
7070 && menu_is_popup(menu->parent->dname)
7071 && menu->parent->submenu_id != NULL)
7072 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
7073 else
7074 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
7075 if (menu->submenu_id != NULL)
7076 DestroyMenu(menu->submenu_id);
7077#ifdef FEAT_TEAROFF
7078 if (IsWindow(menu->tearoff_handle))
7079 DestroyWindow(menu->tearoff_handle);
7080 if (menu->parent != NULL
7081 && menu->parent->children != NULL
7082 && IsWindow(menu->parent->tearoff_handle))
7083 {
7084 /* This menu must not show up when rebuilding the tearoff window. */
7085 menu->modes = 0;
7086 rebuild_tearoff(menu->parent);
7087 }
7088#endif
7089 }
7090}
7091
7092#ifdef FEAT_TEAROFF
7093 static void
7094rebuild_tearoff(vimmenu_T *menu)
7095{
7096 /*hackish*/
7097 char_u tbuf[128];
7098 RECT trect;
7099 RECT rct;
7100 RECT roct;
7101 int x, y;
7102
7103 HWND thwnd = menu->tearoff_handle;
7104
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007105 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 if (GetWindowRect(thwnd, &trect)
7107 && GetWindowRect(s_hwnd, &rct)
7108 && GetClientRect(s_hwnd, &roct))
7109 {
7110 x = trect.left - rct.left;
7111 y = (trect.top - rct.bottom + roct.bottom);
7112 }
7113 else
7114 {
7115 x = y = 0xffffL;
7116 }
7117 DestroyWindow(thwnd);
7118 if (menu->children != NULL)
7119 {
7120 gui_mch_tearoff(tbuf, menu, x, y);
7121 if (IsWindow(menu->tearoff_handle))
7122 (void) SetWindowPos(menu->tearoff_handle,
7123 NULL,
7124 (int)trect.left,
7125 (int)trect.top,
7126 0, 0,
7127 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
7128 }
7129}
7130#endif /* FEAT_TEAROFF */
7131
7132/*
7133 * Make a menu either grey or not grey.
7134 */
7135 void
7136gui_mch_menu_grey(
7137 vimmenu_T *menu,
7138 int grey)
7139{
7140#ifdef FEAT_TOOLBAR
7141 /*
7142 * is this a toolbar button?
7143 */
7144 if (menu->submenu_id == (HMENU)-1)
7145 {
7146 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
7147 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
7148 }
7149 else
7150#endif
7151 if (grey)
7152 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_GRAYED);
7153 else
7154 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
7155
7156#ifdef FEAT_TEAROFF
7157 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
7158 {
7159 WORD menuID;
7160 HWND menuHandle;
7161
7162 /*
7163 * A tearoff button has changed state.
7164 */
7165 if (menu->children == NULL)
7166 menuID = (WORD)(menu->id);
7167 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007168 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
7170 if (menuHandle)
7171 EnableWindow(menuHandle, !grey);
7172
7173 }
7174#endif
7175}
7176
7177#endif /* FEAT_MENU */
7178
7179
7180/* define some macros used to make the dialogue creation more readable */
7181
7182#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
7183#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007184#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185
7186#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
7187/*
7188 * stuff for dialogs
7189 */
7190
7191/*
7192 * The callback routine used by all the dialogs. Very simple. First,
7193 * acknowledges the INITDIALOG message so that Windows knows to do standard
7194 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
7195 * pressed, return that button's ID - IDCANCEL (2), which is the button's
7196 * number.
7197 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007198/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 static LRESULT CALLBACK
7200dialog_callback(
7201 HWND hwnd,
7202 UINT message,
7203 WPARAM wParam,
7204 LPARAM lParam)
7205{
7206 if (message == WM_INITDIALOG)
7207 {
7208 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
7209 /* Set focus to the dialog. Set the default button, if specified. */
7210 (void)SetFocus(hwnd);
7211 if (dialog_default_button > IDCANCEL)
7212 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007213 else
7214 /* We don't have a default, set focus on another element of the
7215 * dialog window, probably the icon */
7216 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 return FALSE;
7218 }
7219
7220 if (message == WM_COMMAND)
7221 {
7222 int button = LOWORD(wParam);
7223
7224 /* Don't end the dialog if something was selected that was
7225 * not a button.
7226 */
7227 if (button >= DLG_NONBUTTON_CONTROL)
7228 return TRUE;
7229
7230 /* If the edit box exists, copy the string. */
7231 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007232 {
7233# if defined(FEAT_MBYTE) && defined(WIN3264)
7234 /* If the OS is Windows NT, and 'encoding' differs from active
7235 * codepage: use wide function and convert text. */
7236 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
7237 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007238 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007239 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
7240 char_u *p;
7241
7242 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
7243 p = utf16_to_enc(wp, NULL);
7244 vim_strncpy(s_textfield, p, IOSIZE);
7245 vim_free(p);
7246 vim_free(wp);
7247 }
7248 else
7249# endif
7250 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007251 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253
7254 /*
7255 * Need to check for IDOK because if the user just hits Return to
7256 * accept the default value, some reason this is what we get.
7257 */
7258 if (button == IDOK)
7259 {
7260 if (dialog_default_button > IDCANCEL)
7261 EndDialog(hwnd, dialog_default_button);
7262 }
7263 else
7264 EndDialog(hwnd, button - IDCANCEL);
7265 return TRUE;
7266 }
7267
7268 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7269 {
7270 EndDialog(hwnd, 0);
7271 return TRUE;
7272 }
7273 return FALSE;
7274}
7275
7276/*
7277 * Create a dialog dynamically from the parameter strings.
7278 * type = type of dialog (question, alert, etc.)
7279 * title = dialog title. may be NULL for default title.
7280 * message = text to display. Dialog sizes to accommodate it.
7281 * buttons = '\n' separated list of button captions, default first.
7282 * dfltbutton = number of default button.
7283 *
7284 * This routine returns 1 if the first button is pressed,
7285 * 2 for the second, etc.
7286 *
7287 * 0 indicates Esc was pressed.
7288 * -1 for unexpected error
7289 *
7290 * If stubbing out this fn, return 1.
7291 */
7292
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007293static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294{
7295 "IDR_VIM",
7296 "IDR_VIM_ERROR",
7297 "IDR_VIM_ALERT",
7298 "IDR_VIM_INFO",
7299 "IDR_VIM_QUESTION"
7300};
7301
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 int
7303gui_mch_dialog(
7304 int type,
7305 char_u *title,
7306 char_u *message,
7307 char_u *buttons,
7308 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007309 char_u *textfield,
7310 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311{
7312 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007313 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 int numButtons;
7315 int *buttonWidths, *buttonPositions;
7316 int buttonYpos;
7317 int nchar, i;
7318 DWORD lStyle;
7319 int dlgwidth = 0;
7320 int dlgheight;
7321 int editboxheight;
7322 int horizWidth = 0;
7323 int msgheight;
7324 char_u *pstart;
7325 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007326 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 char_u *tbuffer;
7328 RECT rect;
7329 HWND hwnd;
7330 HDC hdc;
7331 HFONT font, oldFont;
7332 TEXTMETRIC fontInfo;
7333 int fontHeight;
7334 int textWidth, minButtonWidth, messageWidth;
7335 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007336 int maxDialogHeight;
7337 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 int vertical;
7339 int dlgPaddingX;
7340 int dlgPaddingY;
7341#ifdef USE_SYSMENU_FONT
7342 LOGFONT lfSysmenu;
7343 int use_lfSysmenu = FALSE;
7344#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007345 garray_T ga;
7346 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347
7348#ifndef NO_CONSOLE
7349 /* Don't output anything in silent mode ("ex -s") */
7350 if (silent_mode)
7351 return dfltbutton; /* return default option */
7352#endif
7353
Bram Moolenaar748bf032005-02-02 23:04:36 +00007354 if (s_hwnd == NULL)
7355 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356
7357 if ((type < 0) || (type > VIM_LAST_TYPE))
7358 type = 0;
7359
7360 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007361 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007362 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007363 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364
7365 if (p == NULL)
7366 return -1;
7367
7368 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007369 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7371 * const.
7372 */
7373 tbuffer = vim_strsave(buttons);
7374 if (tbuffer == NULL)
7375 return -1;
7376
7377 --dfltbutton; /* Change from one-based to zero-based */
7378
7379 /* Count buttons */
7380 numButtons = 1;
7381 for (i = 0; tbuffer[i] != '\0'; i++)
7382 {
7383 if (tbuffer[i] == DLG_BUTTON_SEP)
7384 numButtons++;
7385 }
7386 if (dfltbutton >= numButtons)
7387 dfltbutton = -1;
7388
7389 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007390 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 if (buttonWidths == NULL)
7392 return -1;
7393
7394 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007395 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 if (buttonPositions == NULL)
7397 return -1;
7398
7399 /*
7400 * Calculate how big the dialog must be.
7401 */
7402 hwnd = GetDesktopWindow();
7403 hdc = GetWindowDC(hwnd);
7404#ifdef USE_SYSMENU_FONT
7405 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7406 {
7407 font = CreateFontIndirect(&lfSysmenu);
7408 use_lfSysmenu = TRUE;
7409 }
7410 else
7411#endif
7412 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7413 VARIABLE_PITCH , DLG_FONT_NAME);
7414 if (s_usenewlook)
7415 {
7416 oldFont = SelectFont(hdc, font);
7417 dlgPaddingX = DLG_PADDING_X;
7418 dlgPaddingY = DLG_PADDING_Y;
7419 }
7420 else
7421 {
7422 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7423 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7424 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7425 }
7426 GetTextMetrics(hdc, &fontInfo);
7427 fontHeight = fontInfo.tmHeight;
7428
7429 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007430 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431
7432 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007433 if (s_hwnd == NULL)
7434 {
7435 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436
Bram Moolenaarc716c302006-01-21 22:12:51 +00007437 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007438 get_work_area(&workarea_rect);
7439 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7440 if (maxDialogWidth > 600)
7441 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007442 /* Leave some room for the taskbar. */
7443 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007444 }
7445 else
7446 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02007447 /* Use our own window for the size, unless it's very small. */
7448 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007449 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02007450 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007451 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007452 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
7453 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007454
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007455 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007456 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007457 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02007458 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007459 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
7460 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
7461 }
7462
7463 /* Set dlgwidth to width of message.
7464 * Copy the message into "ga", changing NL to CR-NL and inserting line
7465 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 pstart = message;
7467 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007468 msgheight = 0;
7469 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 do
7471 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007472 msgheight += fontHeight; /* at least one line */
7473
7474 /* Need to figure out where to break the string. The system does it
7475 * at a word boundary, which would mean we can't compute the number of
7476 * wrapped lines. */
7477 textWidth = 0;
7478 last_white = NULL;
7479 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007480 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007481#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007482 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007483#else
7484 l = 1;
7485#endif
7486 if (l == 1 && vim_iswhite(*pend)
7487 && textWidth > maxDialogWidth * 3 / 4)
7488 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007489 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007490 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007491 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007492 /* Line will wrap. */
7493 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007494 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007495 textWidth = 0;
7496
7497 if (last_white != NULL)
7498 {
7499 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007500 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007501 pend = last_white + 1;
7502 last_white = NULL;
7503 }
7504 ga_append(&ga, '\r');
7505 ga_append(&ga, '\n');
7506 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007507 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007508
7509 while (--l >= 0)
7510 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007511 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007512 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007514
7515 ga_append(&ga, '\r');
7516 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 pstart = pend + 1;
7518 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007519
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007520 if (ga.ga_data != NULL)
7521 message = ga.ga_data;
7522
Bram Moolenaar748bf032005-02-02 23:04:36 +00007523 messageWidth += 10; /* roundoff space */
7524
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02007526 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7527 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528
7529 if (msgheight < DLG_ICON_HEIGHT)
7530 msgheight = DLG_ICON_HEIGHT;
7531
7532 /*
7533 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007534 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007536 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 if (!vertical)
7538 {
7539 // Place buttons horizontally if they fit.
7540 horizWidth = dlgPaddingX;
7541 pstart = tbuffer;
7542 i = 0;
7543 do
7544 {
7545 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7546 if (pend == NULL)
7547 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007548 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 if (textWidth < minButtonWidth)
7550 textWidth = minButtonWidth;
7551 textWidth += dlgPaddingX; /* Padding within button */
7552 buttonWidths[i] = textWidth;
7553 buttonPositions[i++] = horizWidth;
7554 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7555 pstart = pend + 1;
7556 } while (*pend != NUL);
7557
7558 if (horizWidth > maxDialogWidth)
7559 vertical = TRUE; // Too wide to fit on the screen.
7560 else if (horizWidth > dlgwidth)
7561 dlgwidth = horizWidth;
7562 }
7563
7564 if (vertical)
7565 {
7566 // Stack buttons vertically.
7567 pstart = tbuffer;
7568 do
7569 {
7570 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7571 if (pend == NULL)
7572 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007573 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 textWidth += dlgPaddingX; /* Padding within button */
7575 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7576 if (textWidth > dlgwidth)
7577 dlgwidth = textWidth;
7578 pstart = pend + 1;
7579 } while (*pend != NUL);
7580 }
7581
7582 if (dlgwidth < DLG_MIN_WIDTH)
7583 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7584
7585 /* start to fill in the dlgtemplate information. addressing by WORDs */
7586 if (s_usenewlook)
7587 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7588 else
7589 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7590
7591 add_long(lStyle);
7592 add_long(0); // (lExtendedStyle)
7593 pnumitems = p; /*save where the number of items must be stored*/
7594 add_word(0); // NumberOfItems(will change later)
7595 add_word(10); // x
7596 add_word(10); // y
7597 add_word(PixelToDialogX(dlgwidth)); // cx
7598
7599 // Dialog height.
7600 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007601 dlgheight = msgheight + 2 * dlgPaddingY
7602 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 else
7604 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7605
7606 // Dialog needs to be taller if contains an edit box.
7607 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7608 if (textfield != NULL)
7609 dlgheight += editboxheight;
7610
Bram Moolenaara95d8232013-08-07 15:27:11 +02007611 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7612 if (dlgheight > maxDialogHeight)
7613 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007614 msgheight = msgheight - (dlgheight - maxDialogHeight);
7615 dlgheight = maxDialogHeight;
7616 scroll_flag = WS_VSCROLL;
7617 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7618 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007619 }
7620
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 add_word(PixelToDialogY(dlgheight));
7622
7623 add_word(0); // Menu
7624 add_word(0); // Class
7625
7626 /* copy the title of the dialog */
7627 nchar = nCopyAnsiToWideChar(p, (title ?
7628 (LPSTR)title :
7629 (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
7630 p += nchar;
7631
7632 if (s_usenewlook)
7633 {
7634 /* do the font, since DS_3DLOOK doesn't work properly */
7635#ifdef USE_SYSMENU_FONT
7636 if (use_lfSysmenu)
7637 {
7638 /* point size */
7639 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7640 GetDeviceCaps(hdc, LOGPIXELSY));
7641 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
7642 }
7643 else
7644#endif
7645 {
7646 *p++ = DLG_FONT_POINT_SIZE; // point size
7647 nchar = nCopyAnsiToWideChar(p, TEXT(DLG_FONT_NAME));
7648 }
7649 p += nchar;
7650 }
7651
7652 buttonYpos = msgheight + 2 * dlgPaddingY;
7653
7654 if (textfield != NULL)
7655 buttonYpos += editboxheight;
7656
7657 pstart = tbuffer;
7658 if (!vertical)
7659 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7660 for (i = 0; i < numButtons; i++)
7661 {
7662 /* get end of this button. */
7663 for ( pend = pstart;
7664 *pend && (*pend != DLG_BUTTON_SEP);
7665 pend++)
7666 ;
7667
7668 if (*pend)
7669 *pend = '\0';
7670
7671 /*
7672 * old NOTE:
7673 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7674 * the focus to the first tab-able button and in so doing makes that
7675 * the default!! Grrr. Workaround: Make the default button the only
7676 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7677 * he/she can use arrow keys.
7678 *
7679 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007680 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 * dialog. Also needed for when the textfield is the default control.
7682 * It appears to work now (perhaps not on Win95?).
7683 */
7684 if (vertical)
7685 {
7686 p = add_dialog_element(p,
7687 (i == dfltbutton
7688 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7689 PixelToDialogX(DLG_VERT_PADDING_X),
7690 PixelToDialogY(buttonYpos /* TBK */
7691 + 2 * fontHeight * i),
7692 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7693 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007694 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 }
7696 else
7697 {
7698 p = add_dialog_element(p,
7699 (i == dfltbutton
7700 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7701 PixelToDialogX(horizWidth + buttonPositions[i]),
7702 PixelToDialogY(buttonYpos), /* TBK */
7703 PixelToDialogX(buttonWidths[i]),
7704 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007705 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 }
7707 pstart = pend + 1; /*next button*/
7708 }
7709 *pnumitems += numButtons;
7710
7711 /* Vim icon */
7712 p = add_dialog_element(p, SS_ICON,
7713 PixelToDialogX(dlgPaddingX),
7714 PixelToDialogY(dlgPaddingY),
7715 PixelToDialogX(DLG_ICON_WIDTH),
7716 PixelToDialogY(DLG_ICON_HEIGHT),
7717 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7718 dlg_icons[type]);
7719
Bram Moolenaar748bf032005-02-02 23:04:36 +00007720 /* Dialog message */
7721 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7722 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7723 PixelToDialogY(dlgPaddingY),
7724 (WORD)(PixelToDialogX(messageWidth) + 1),
7725 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007726 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727
7728 /* Edit box */
7729 if (textfield != NULL)
7730 {
7731 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7732 PixelToDialogX(2 * dlgPaddingX),
7733 PixelToDialogY(2 * dlgPaddingY + msgheight),
7734 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7735 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007736 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 *pnumitems += 1;
7738 }
7739
7740 *pnumitems += 2;
7741
7742 SelectFont(hdc, oldFont);
7743 DeleteObject(font);
7744 ReleaseDC(hwnd, hdc);
7745
7746 /* Let the dialog_callback() function know which button to make default
7747 * If we have an edit box, make that the default. We also need to tell
7748 * dialog_callback() if this dialog contains an edit box or not. We do
7749 * this by setting s_textfield if it does.
7750 */
7751 if (textfield != NULL)
7752 {
7753 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7754 s_textfield = textfield;
7755 }
7756 else
7757 {
7758 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7759 s_textfield = NULL;
7760 }
7761
7762 /* show the dialog box modally and get a return value */
7763 nchar = (int)DialogBoxIndirect(
7764 s_hinst,
7765 (LPDLGTEMPLATE)pdlgtemplate,
7766 s_hwnd,
7767 (DLGPROC)dialog_callback);
7768
7769 LocalFree(LocalHandle(pdlgtemplate));
7770 vim_free(tbuffer);
7771 vim_free(buttonWidths);
7772 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007773 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774
7775 /* Focus back to our window (for when MDI is used). */
7776 (void)SetFocus(s_hwnd);
7777
7778 return nchar;
7779}
7780
7781#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007782
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783/*
7784 * Put a simple element (basic class) onto a dialog template in memory.
7785 * return a pointer to where the next item should be added.
7786 *
7787 * parameters:
7788 * lStyle = additional style flags
7789 * (be careful, NT3.51 & Win32s will ignore the new ones)
7790 * x,y = x & y positions IN DIALOG UNITS
7791 * w,h = width and height IN DIALOG UNITS
7792 * Id = ID used in messages
7793 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7794 * caption = usually text or resource name
7795 *
7796 * TODO: use the length information noted here to enable the dialog creation
7797 * routines to work out more exactly how much memory they need to alloc.
7798 */
7799 static PWORD
7800add_dialog_element(
7801 PWORD p,
7802 DWORD lStyle,
7803 WORD x,
7804 WORD y,
7805 WORD w,
7806 WORD h,
7807 WORD Id,
7808 WORD clss,
7809 const char *caption)
7810{
7811 int nchar;
7812
7813 p = lpwAlign(p); /* Align to dword boundary*/
7814 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7815 *p++ = LOWORD(lStyle);
7816 *p++ = HIWORD(lStyle);
7817 *p++ = 0; // LOWORD (lExtendedStyle)
7818 *p++ = 0; // HIWORD (lExtendedStyle)
7819 *p++ = x;
7820 *p++ = y;
7821 *p++ = w;
7822 *p++ = h;
7823 *p++ = Id; //9 or 10 words in all
7824
7825 *p++ = (WORD)0xffff;
7826 *p++ = clss; //2 more here
7827
7828 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption); //strlen(caption)+1
7829 p += nchar;
7830
7831 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7832
7833 return p; //total = 15+ (strlen(caption)) words
7834 // = 30 + 2(strlen(caption) bytes reqd
7835}
7836
7837
7838/*
7839 * Helper routine. Take an input pointer, return closest pointer that is
7840 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7841 */
7842 static LPWORD
7843lpwAlign(
7844 LPWORD lpIn)
7845{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007846 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007848 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 ul += 3;
7850 ul >>= 2;
7851 ul <<= 2;
7852 return (LPWORD)ul;
7853}
7854
7855/*
7856 * Helper routine. Takes second parameter as Ansi string, copies it to first
7857 * parameter as wide character (16-bits / char) string, and returns integer
7858 * number of wide characters (words) in string (including the trailing wide
7859 * char NULL). Partly taken from the Win32SDK samples.
7860 */
7861 static int
7862nCopyAnsiToWideChar(
7863 LPWORD lpWCStr,
7864 LPSTR lpAnsiIn)
7865{
7866 int nChar = 0;
7867#ifdef FEAT_MBYTE
7868 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7869 int i;
7870 WCHAR *wn;
7871
7872 if (enc_codepage == 0 && (int)GetACP() != enc_codepage)
7873 {
7874 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007875 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 if (wn != NULL)
7877 {
7878 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007879 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 vim_free(wn);
7881 }
7882 }
7883 if (nChar == 0)
7884 /* Use Win32 conversion function. */
7885 nChar = MultiByteToWideChar(
7886 enc_codepage > 0 ? enc_codepage : CP_ACP,
7887 MB_PRECOMPOSED,
7888 lpAnsiIn, len,
7889 lpWCStr, len);
7890 for (i = 0; i < nChar; ++i)
7891 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7892 lpWCStr[i] = (WORD)' ';
7893#else
7894 do
7895 {
7896 if (*lpAnsiIn == '\t')
7897 *lpWCStr++ = (WORD)' ';
7898 else
7899 *lpWCStr++ = (WORD)*lpAnsiIn;
7900 nChar++;
7901 } while (*lpAnsiIn++);
7902#endif
7903
7904 return nChar;
7905}
7906
7907
7908#ifdef FEAT_TEAROFF
7909/*
7910 * The callback function for all the modeless dialogs that make up the
7911 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7912 * thinking its menus have been clicked), and go away when closed.
7913 */
7914 static LRESULT CALLBACK
7915tearoff_callback(
7916 HWND hwnd,
7917 UINT message,
7918 WPARAM wParam,
7919 LPARAM lParam)
7920{
7921 if (message == WM_INITDIALOG)
7922 return (TRUE);
7923
7924 /* May show the mouse pointer again. */
7925 HandleMouseHide(message, lParam);
7926
7927 if (message == WM_COMMAND)
7928 {
7929 if ((WORD)(LOWORD(wParam)) & 0x8000)
7930 {
7931 POINT mp;
7932 RECT rect;
7933
7934 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7935 {
7936 (void)TrackPopupMenu(
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007937 (HMENU)(long_u)(LOWORD(wParam) ^ 0x8000),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7939 (int)rect.right - 8,
7940 (int)mp.y,
7941 (int)0, /*reserved param*/
7942 s_hwnd,
7943 NULL);
7944 /*
7945 * NOTE: The pop-up menu can eat the mouse up event.
7946 * We deal with this in normal.c.
7947 */
7948 }
7949 }
7950 else
7951 /* Pass on messages to the main Vim window */
7952 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7953 /*
7954 * Give main window the focus back: this is so after
7955 * choosing a tearoff button you can start typing again
7956 * straight away.
7957 */
7958 (void)SetFocus(s_hwnd);
7959 return TRUE;
7960 }
7961 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7962 {
7963 DestroyWindow(hwnd);
7964 return TRUE;
7965 }
7966
7967 /* When moved around, give main window the focus back. */
7968 if (message == WM_EXITSIZEMOVE)
7969 (void)SetActiveWindow(s_hwnd);
7970
7971 return FALSE;
7972}
7973#endif
7974
7975
7976/*
7977 * Decide whether to use the "new look" (small, non-bold font) or the "old
7978 * look" (big, clanky font) for dialogs, and work out a few values for use
7979 * later accordingly.
7980 */
7981 static void
7982get_dialog_font_metrics(void)
7983{
7984 HDC hdc;
7985 HFONT hfontTools = 0;
7986 DWORD dlgFontSize;
7987 SIZE size;
7988#ifdef USE_SYSMENU_FONT
7989 LOGFONT lfSysmenu;
7990#endif
7991
7992 s_usenewlook = FALSE;
7993
7994 /*
7995 * For NT3.51 and Win32s, we stick with the old look
7996 * because it matches everything else.
7997 */
7998 if (!is_winnt_3())
7999 {
8000#ifdef USE_SYSMENU_FONT
8001 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
8002 hfontTools = CreateFontIndirect(&lfSysmenu);
8003 else
8004#endif
8005 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
8006 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
8007
8008 if (hfontTools)
8009 {
8010 hdc = GetDC(s_hwnd);
8011 SelectObject(hdc, hfontTools);
8012 /*
8013 * GetTextMetrics() doesn't return the right value in
8014 * tmAveCharWidth, so we have to figure out the dialog base units
8015 * ourselves.
8016 */
8017 GetTextExtentPoint(hdc,
8018 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
8019 52, &size);
8020 ReleaseDC(s_hwnd, hdc);
8021
8022 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
8023 s_dlgfntheight = (WORD)size.cy;
8024 s_usenewlook = TRUE;
8025 }
8026 }
8027
8028 if (!s_usenewlook)
8029 {
8030 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
8031 s_dlgfntwidth = LOWORD(dlgFontSize);
8032 s_dlgfntheight = HIWORD(dlgFontSize);
8033 }
8034}
8035
8036#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8037/*
8038 * Create a pseudo-"tearoff menu" based on the child
8039 * items of a given menu pointer.
8040 */
8041 static void
8042gui_mch_tearoff(
8043 char_u *title,
8044 vimmenu_T *menu,
8045 int initX,
8046 int initY)
8047{
8048 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
8049 int template_len;
8050 int nchar, textWidth, submenuWidth;
8051 DWORD lStyle;
8052 DWORD lExtendedStyle;
8053 WORD dlgwidth;
8054 WORD menuID;
8055 vimmenu_T *pmenu;
8056 vimmenu_T *the_menu = menu;
8057 HWND hwnd;
8058 HDC hdc;
8059 HFONT font, oldFont;
8060 int col, spaceWidth, len;
8061 int columnWidths[2];
8062 char_u *label, *text;
8063 int acLen = 0;
8064 int nameLen;
8065 int padding0, padding1, padding2 = 0;
8066 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008067 int x;
8068 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069#ifdef USE_SYSMENU_FONT
8070 LOGFONT lfSysmenu;
8071 int use_lfSysmenu = FALSE;
8072#endif
8073
8074 /*
8075 * If this menu is already torn off, move it to the mouse position.
8076 */
8077 if (IsWindow(menu->tearoff_handle))
8078 {
8079 POINT mp;
8080 if (GetCursorPos((LPPOINT)&mp))
8081 {
8082 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
8083 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
8084 }
8085 return;
8086 }
8087
8088 /*
8089 * Create a new tearoff.
8090 */
8091 if (*title == MNU_HIDDEN_CHAR)
8092 title++;
8093
8094 /* Allocate memory to store the dialog template. It's made bigger when
8095 * needed. */
8096 template_len = DLG_ALLOC_SIZE;
8097 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
8098 if (p == NULL)
8099 return;
8100
8101 hwnd = GetDesktopWindow();
8102 hdc = GetWindowDC(hwnd);
8103#ifdef USE_SYSMENU_FONT
8104 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
8105 {
8106 font = CreateFontIndirect(&lfSysmenu);
8107 use_lfSysmenu = TRUE;
8108 }
8109 else
8110#endif
8111 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8112 VARIABLE_PITCH , DLG_FONT_NAME);
8113 if (s_usenewlook)
8114 oldFont = SelectFont(hdc, font);
8115 else
8116 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
8117
8118 /* Calculate width of a single space. Used for padding columns to the
8119 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008120 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121
8122 /* Figure out max width of the text column, the accelerator column and the
8123 * optional submenu column. */
8124 submenuWidth = 0;
8125 for (col = 0; col < 2; col++)
8126 {
8127 columnWidths[col] = 0;
8128 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
8129 {
8130 /* Use "dname" here to compute the width of the visible text. */
8131 text = (col == 0) ? pmenu->dname : pmenu->actext;
8132 if (text != NULL && *text != NUL)
8133 {
8134 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
8135 if (textWidth > columnWidths[col])
8136 columnWidths[col] = textWidth;
8137 }
8138 if (pmenu->children != NULL)
8139 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
8140 }
8141 }
8142 if (columnWidths[1] == 0)
8143 {
8144 /* no accelerators */
8145 if (submenuWidth != 0)
8146 columnWidths[0] += submenuWidth;
8147 else
8148 columnWidths[0] += spaceWidth;
8149 }
8150 else
8151 {
8152 /* there is an accelerator column */
8153 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
8154 columnWidths[1] += submenuWidth;
8155 }
8156
8157 /*
8158 * Now find the total width of our 'menu'.
8159 */
8160 textWidth = columnWidths[0] + columnWidths[1];
8161 if (submenuWidth != 0)
8162 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008163 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
8165 textWidth += submenuWidth;
8166 }
8167 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
8168 if (textWidth > dlgwidth)
8169 dlgwidth = textWidth;
8170 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
8171
8172 /* W95 can't do thin dialogs, they look v. weird! */
8173 if (mch_windows95() && dlgwidth < TEAROFF_MIN_WIDTH)
8174 dlgwidth = TEAROFF_MIN_WIDTH;
8175
8176 /* start to fill in the dlgtemplate information. addressing by WORDs */
8177 if (s_usenewlook)
8178 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
8179 else
8180 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
8181
8182 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
8183 *p++ = LOWORD(lStyle);
8184 *p++ = HIWORD(lStyle);
8185 *p++ = LOWORD(lExtendedStyle);
8186 *p++ = HIWORD(lExtendedStyle);
8187 pnumitems = p; /* save where the number of items must be stored */
8188 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008189 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008191 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 else
8193 *p++ = PixelToDialogX(initX); // x
8194 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008195 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 else
8197 *p++ = PixelToDialogY(initY); // y
8198 *p++ = PixelToDialogX(dlgwidth); // cx
8199 ptrueheight = p;
8200 *p++ = 0; // dialog height: changed later anyway
8201 *p++ = 0; // Menu
8202 *p++ = 0; // Class
8203
8204 /* copy the title of the dialog */
8205 nchar = nCopyAnsiToWideChar(p, ((*title)
8206 ? (LPSTR)title
8207 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
8208 p += nchar;
8209
8210 if (s_usenewlook)
8211 {
8212 /* do the font, since DS_3DLOOK doesn't work properly */
8213#ifdef USE_SYSMENU_FONT
8214 if (use_lfSysmenu)
8215 {
8216 /* point size */
8217 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
8218 GetDeviceCaps(hdc, LOGPIXELSY));
8219 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
8220 }
8221 else
8222#endif
8223 {
8224 *p++ = DLG_FONT_POINT_SIZE; // point size
8225 nchar = nCopyAnsiToWideChar (p, TEXT(DLG_FONT_NAME));
8226 }
8227 p += nchar;
8228 }
8229
8230 /*
8231 * Loop over all the items in the menu.
8232 * But skip over the tearbar.
8233 */
8234 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
8235 menu = menu->children->next;
8236 else
8237 menu = menu->children;
8238 for ( ; menu != NULL; menu = menu->next)
8239 {
8240 if (menu->modes == 0) /* this menu has just been deleted */
8241 continue;
8242 if (menu_is_separator(menu->dname))
8243 {
8244 sepPadding += 3;
8245 continue;
8246 }
8247
8248 /* Check if there still is plenty of room in the template. Make it
8249 * larger when needed. */
8250 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8251 {
8252 WORD *newp;
8253
8254 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8255 if (newp != NULL)
8256 {
8257 template_len += 4096;
8258 mch_memmove(newp, pdlgtemplate,
8259 (char *)p - (char *)pdlgtemplate);
8260 p = newp + (p - pdlgtemplate);
8261 pnumitems = newp + (pnumitems - pdlgtemplate);
8262 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8263 LocalFree(LocalHandle(pdlgtemplate));
8264 pdlgtemplate = newp;
8265 }
8266 }
8267
8268 /* Figure out minimal length of this menu label. Use "name" for the
8269 * actual text, "dname" for estimating the displayed size. "name"
8270 * has "&a" for mnemonic and includes the accelerator. */
8271 len = nameLen = (int)STRLEN(menu->name);
8272 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8273 (int)STRLEN(menu->dname))) / spaceWidth;
8274 len += padding0;
8275
8276 if (menu->actext != NULL)
8277 {
8278 acLen = (int)STRLEN(menu->actext);
8279 len += acLen;
8280 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8281 }
8282 else
8283 textWidth = 0;
8284 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8285 len += padding1;
8286
8287 if (menu->children == NULL)
8288 {
8289 padding2 = submenuWidth / spaceWidth;
8290 len += padding2;
8291 menuID = (WORD)(menu->id);
8292 }
8293 else
8294 {
8295 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008296 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 }
8298
8299 /* Allocate menu label and fill it in */
8300 text = label = alloc((unsigned)len + 1);
8301 if (label == NULL)
8302 break;
8303
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008304 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 text = vim_strchr(text, TAB); /* stop at TAB before actext */
8306 if (text == NULL)
8307 text = label + nameLen; /* no actext, use whole name */
8308 while (padding0-- > 0)
8309 *text++ = ' ';
8310 if (menu->actext != NULL)
8311 {
8312 STRNCPY(text, menu->actext, acLen);
8313 text += acLen;
8314 }
8315 while (padding1-- > 0)
8316 *text++ = ' ';
8317 if (menu->children != NULL)
8318 {
8319 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8320 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8321 }
8322 else
8323 {
8324 while (padding2-- > 0)
8325 *text++ = ' ';
8326 }
8327 *text = NUL;
8328
8329 /*
8330 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8331 * W95/NT4 it makes the tear-off look more like a menu.
8332 */
8333 p = add_dialog_element(p,
8334 BS_PUSHBUTTON|BS_LEFT,
8335 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8336 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8337 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8338 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008339 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 vim_free(label);
8341 (*pnumitems)++;
8342 }
8343
8344 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8345
8346
8347 /* show modelessly */
8348 the_menu->tearoff_handle = CreateDialogIndirect(
8349 s_hinst,
8350 (LPDLGTEMPLATE)pdlgtemplate,
8351 s_hwnd,
8352 (DLGPROC)tearoff_callback);
8353
8354 LocalFree(LocalHandle(pdlgtemplate));
8355 SelectFont(hdc, oldFont);
8356 DeleteObject(font);
8357 ReleaseDC(hwnd, hdc);
8358
8359 /*
8360 * Reassert ourselves as the active window. This is so that after creating
8361 * a tearoff, the user doesn't have to click with the mouse just to start
8362 * typing again!
8363 */
8364 (void)SetActiveWindow(s_hwnd);
8365
8366 /* make sure the right buttons are enabled */
8367 force_menu_update = TRUE;
8368}
8369#endif
8370
8371#if defined(FEAT_TOOLBAR) || defined(PROTO)
8372#include "gui_w32_rc.h"
8373
8374/* This not defined in older SDKs */
8375# ifndef TBSTYLE_FLAT
8376# define TBSTYLE_FLAT 0x0800
8377# endif
8378
8379/*
8380 * Create the toolbar, initially unpopulated.
8381 * (just like the menu, there are no defaults, it's all
8382 * set up through menu.vim)
8383 */
8384 static void
8385initialise_toolbar(void)
8386{
8387 InitCommonControls();
8388 s_toolbarhwnd = CreateToolbarEx(
8389 s_hwnd,
8390 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8391 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008392 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 s_hinst,
8394 IDR_TOOLBAR1, // id of initial bitmap
8395 NULL,
8396 0, // initial number of buttons
8397 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8398 TOOLBAR_BUTTON_HEIGHT,
8399 TOOLBAR_BUTTON_WIDTH,
8400 TOOLBAR_BUTTON_HEIGHT,
8401 sizeof(TBBUTTON)
8402 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008403 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404
8405 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
8406}
8407
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008408 static LRESULT CALLBACK
8409toolbar_wndproc(
8410 HWND hwnd,
8411 UINT uMsg,
8412 WPARAM wParam,
8413 LPARAM lParam)
8414{
8415 HandleMouseHide(uMsg, lParam);
8416 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8417}
8418
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 static int
8420get_toolbar_bitmap(vimmenu_T *menu)
8421{
8422 int i = -1;
8423
8424 /*
8425 * Check user bitmaps first, unless builtin is specified.
8426 */
8427 if (!is_winnt_3() && !menu->icon_builtin)
8428 {
8429 char_u fname[MAXPATHL];
8430 HANDLE hbitmap = NULL;
8431
8432 if (menu->iconfile != NULL)
8433 {
8434 gui_find_iconfile(menu->iconfile, fname, "bmp");
8435 hbitmap = LoadImage(
8436 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008437 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 IMAGE_BITMAP,
8439 TOOLBAR_BUTTON_WIDTH,
8440 TOOLBAR_BUTTON_HEIGHT,
8441 LR_LOADFROMFILE |
8442 LR_LOADMAP3DCOLORS
8443 );
8444 }
8445
8446 /*
8447 * If the LoadImage call failed, or the "icon=" file
8448 * didn't exist or wasn't specified, try the menu name
8449 */
8450 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008451 && (gui_find_bitmap(
8452#ifdef FEAT_MULTI_LANG
8453 menu->en_dname != NULL ? menu->en_dname :
8454#endif
8455 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 hbitmap = LoadImage(
8457 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008458 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 IMAGE_BITMAP,
8460 TOOLBAR_BUTTON_WIDTH,
8461 TOOLBAR_BUTTON_HEIGHT,
8462 LR_LOADFROMFILE |
8463 LR_LOADMAP3DCOLORS
8464 );
8465
8466 if (hbitmap != NULL)
8467 {
8468 TBADDBITMAP tbAddBitmap;
8469
8470 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008471 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472
8473 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8474 (WPARAM)1, (LPARAM)&tbAddBitmap);
8475 /* i will be set to -1 if it fails */
8476 }
8477 }
8478 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8479 i = menu->iconidx;
8480
8481 return i;
8482}
8483#endif
8484
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008485#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8486 static void
8487initialise_tabline(void)
8488{
8489 InitCommonControls();
8490
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008491 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008492 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008493 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8494 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008495 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008496
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008497 gui.tabline_height = TABLINE_HEIGHT;
8498
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008499# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008500 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008501# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008502}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008503
8504 static LRESULT CALLBACK
8505tabline_wndproc(
8506 HWND hwnd,
8507 UINT uMsg,
8508 WPARAM wParam,
8509 LPARAM lParam)
8510{
8511 HandleMouseHide(uMsg, lParam);
8512 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8513}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008514#endif
8515
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8517/*
8518 * Make the GUI window come to the foreground.
8519 */
8520 void
8521gui_mch_set_foreground(void)
8522{
8523 if (IsIconic(s_hwnd))
8524 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8525 SetForegroundWindow(s_hwnd);
8526}
8527#endif
8528
8529#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8530 static void
8531dyn_imm_load(void)
8532{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008533 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 if (hLibImm == NULL)
8535 return;
8536
8537 pImmGetCompositionStringA
8538 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8539 pImmGetCompositionStringW
8540 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8541 pImmGetContext
8542 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8543 pImmAssociateContext
8544 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8545 pImmReleaseContext
8546 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8547 pImmGetOpenStatus
8548 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8549 pImmSetOpenStatus
8550 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
8551 pImmGetCompositionFont
8552 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
8553 pImmSetCompositionFont
8554 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
8555 pImmSetCompositionWindow
8556 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8557 pImmGetConversionStatus
8558 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008559 pImmSetConversionStatus
8560 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561
8562 if ( pImmGetCompositionStringA == NULL
8563 || pImmGetCompositionStringW == NULL
8564 || pImmGetContext == NULL
8565 || pImmAssociateContext == NULL
8566 || pImmReleaseContext == NULL
8567 || pImmGetOpenStatus == NULL
8568 || pImmSetOpenStatus == NULL
8569 || pImmGetCompositionFont == NULL
8570 || pImmSetCompositionFont == NULL
8571 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008572 || pImmGetConversionStatus == NULL
8573 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 {
8575 FreeLibrary(hLibImm);
8576 hLibImm = NULL;
8577 pImmGetContext = NULL;
8578 return;
8579 }
8580
8581 return;
8582}
8583
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584#endif
8585
8586#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8587
8588# ifdef FEAT_XPM_W32
8589# define IMAGE_XPM 100
8590# endif
8591
8592typedef struct _signicon_t
8593{
8594 HANDLE hImage;
8595 UINT uType;
8596#ifdef FEAT_XPM_W32
8597 HANDLE hShape; /* Mask bitmap handle */
8598#endif
8599} signicon_t;
8600
8601 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008602gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603{
8604 signicon_t *sign;
8605 int x, y, w, h;
8606
8607 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8608 return;
8609
8610 x = TEXT_X(col);
8611 y = TEXT_Y(row);
8612 w = gui.char_width * 2;
8613 h = gui.char_height;
8614 switch (sign->uType)
8615 {
8616 case IMAGE_BITMAP:
8617 {
8618 HDC hdcMem;
8619 HBITMAP hbmpOld;
8620
8621 hdcMem = CreateCompatibleDC(s_hdc);
8622 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8623 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8624 SelectObject(hdcMem, hbmpOld);
8625 DeleteDC(hdcMem);
8626 }
8627 break;
8628 case IMAGE_ICON:
8629 case IMAGE_CURSOR:
8630 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8631 break;
8632#ifdef FEAT_XPM_W32
8633 case IMAGE_XPM:
8634 {
8635 HDC hdcMem;
8636 HBITMAP hbmpOld;
8637
8638 hdcMem = CreateCompatibleDC(s_hdc);
8639 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8640 /* Make hole */
8641 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8642
8643 SelectObject(hdcMem, sign->hImage);
8644 /* Paint sign */
8645 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8646 SelectObject(hdcMem, hbmpOld);
8647 DeleteDC(hdcMem);
8648 }
8649 break;
8650#endif
8651 }
8652}
8653
8654 static void
8655close_signicon_image(signicon_t *sign)
8656{
8657 if (sign)
8658 switch (sign->uType)
8659 {
8660 case IMAGE_BITMAP:
8661 DeleteObject((HGDIOBJ)sign->hImage);
8662 break;
8663 case IMAGE_CURSOR:
8664 DestroyCursor((HCURSOR)sign->hImage);
8665 break;
8666 case IMAGE_ICON:
8667 DestroyIcon((HICON)sign->hImage);
8668 break;
8669#ifdef FEAT_XPM_W32
8670 case IMAGE_XPM:
8671 DeleteObject((HBITMAP)sign->hImage);
8672 DeleteObject((HBITMAP)sign->hShape);
8673 break;
8674#endif
8675 }
8676}
8677
8678 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008679gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680{
8681 signicon_t sign, *psign;
8682 char_u *ext;
8683
8684 if (is_winnt_3())
8685 {
8686 EMSG(_(e_signdata));
8687 return NULL;
8688 }
8689
8690 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008691 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 if (ext > signfile)
8693 {
8694 int do_load = 1;
8695
8696 if (!STRICMP(ext, ".bmp"))
8697 sign.uType = IMAGE_BITMAP;
8698 else if (!STRICMP(ext, ".ico"))
8699 sign.uType = IMAGE_ICON;
8700 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8701 sign.uType = IMAGE_CURSOR;
8702 else
8703 do_load = 0;
8704
8705 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008706 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 gui.char_width * 2, gui.char_height,
8708 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8709#ifdef FEAT_XPM_W32
8710 if (!STRICMP(ext, ".xpm"))
8711 {
8712 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008713 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8714 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 }
8716#endif
8717 }
8718
8719 psign = NULL;
8720 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8721 != NULL)
8722 *psign = sign;
8723
8724 if (!psign)
8725 {
8726 if (sign.hImage)
8727 close_signicon_image(&sign);
8728 EMSG(_(e_signdata));
8729 }
8730 return (void *)psign;
8731
8732}
8733
8734 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008735gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736{
8737 if (sign)
8738 {
8739 close_signicon_image((signicon_t *)sign);
8740 vim_free(sign);
8741 }
8742}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744
8745#if defined(FEAT_BEVAL) || defined(PROTO)
8746
8747/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008748 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 *
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00008750 * The only reused thing is gui_beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8752 * to get current mouse position).
8753 *
8754 * Trying to use as more Windows services as possible, and as less
8755 * IE version as possible :)).
8756 *
8757 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8758 * BalloonEval struct.
8759 * 2) Enable/Disable simply create/kill BalloonEval Timer
8760 * 3) When there was enough inactivity, timer procedure posts
8761 * async request to debugger
8762 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8763 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008764 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 */
8766
Bram Moolenaar45360022005-07-21 21:08:21 +00008767/*
8768 * determine whether installed Common Controls support multiline tooltips
8769 * (i.e. their version is >= 4.70
8770 */
8771 int
8772multiline_balloon_available(void)
8773{
8774 HINSTANCE hDll;
8775 static char comctl_dll[] = "comctl32.dll";
8776 static int multiline_tip = MAYBE;
8777
8778 if (multiline_tip != MAYBE)
8779 return multiline_tip;
8780
8781 hDll = GetModuleHandle(comctl_dll);
8782 if (hDll != NULL)
8783 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008784 DLLGETVERSIONPROC pGetVer;
8785 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008786
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008787 if (pGetVer != NULL)
8788 {
8789 DLLVERSIONINFO dvi;
8790 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008791
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008792 ZeroMemory(&dvi, sizeof(dvi));
8793 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008794
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008795 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008796
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008797 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008798 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008799 || (dvi.dwMajorVersion == 4
8800 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008801 {
8802 multiline_tip = TRUE;
8803 return multiline_tip;
8804 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008805 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008806 else
8807 {
8808 /* there is chance we have ancient CommCtl 4.70
8809 which doesn't export DllGetVersion */
8810 DWORD dwHandle = 0;
8811 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8812 if (len > 0)
8813 {
8814 VS_FIXEDFILEINFO *ver;
8815 UINT vlen = 0;
8816 void *data = alloc(len);
8817
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008818 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008819 && GetFileVersionInfo(comctl_dll, 0, len, data)
8820 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8821 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008822 && HIWORD(ver->dwFileVersionMS) > 4)
8823 || ((HIWORD(ver->dwFileVersionMS) == 4
8824 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008825 {
8826 vim_free(data);
8827 multiline_tip = TRUE;
8828 return multiline_tip;
8829 }
8830 vim_free(data);
8831 }
8832 }
8833 }
8834 multiline_tip = FALSE;
8835 return multiline_tip;
8836}
8837
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008839make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840{
Bram Moolenaar45360022005-07-21 21:08:21 +00008841 TOOLINFO *pti;
8842 int ToolInfoSize;
8843
8844 if (multiline_balloon_available() == TRUE)
8845 ToolInfoSize = sizeof(TOOLINFO_NEW);
8846 else
8847 ToolInfoSize = sizeof(TOOLINFO);
8848
8849 pti = (TOOLINFO *)alloc(ToolInfoSize);
8850 if (pti == NULL)
8851 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852
8853 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
8854 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8855 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8856 beval->target, NULL, s_hinst, NULL);
8857
8858 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8859 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8860
Bram Moolenaar45360022005-07-21 21:08:21 +00008861 pti->cbSize = ToolInfoSize;
8862 pti->uFlags = TTF_SUBCLASS;
8863 pti->hwnd = beval->target;
8864 pti->hinst = 0; /* Don't use string resources */
8865 pti->uId = ID_BEVAL_TOOLTIP;
8866
8867 if (multiline_balloon_available() == TRUE)
8868 {
8869 RECT rect;
8870 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
8871 pti->lpszText = LPSTR_TEXTCALLBACK;
8872 ptin->lParam = (LPARAM)text;
8873 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
8874 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8875 (LPARAM)rect.right);
8876 }
8877 else
8878 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879
8880 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00008881 pti->rect.left = pt.x - 3;
8882 pti->rect.top = pt.y - 3;
8883 pti->rect.right = pt.x + 3;
8884 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885
Bram Moolenaar45360022005-07-21 21:08:21 +00008886 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 /* Make tooltip appear sooner */
8888 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008889 /* I've performed some tests and it seems the longest possible life time
8890 * of tooltip is 30 seconds */
8891 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 /*
8893 * HACK: force tooltip to appear, because it'll not appear until
8894 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008895 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008897 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00008899 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900}
8901
8902 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008903delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008905 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906}
8907
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008908/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008910BevalTimerProc(
8911 HWND hwnd,
8912 UINT uMsg,
8913 UINT_PTR idEvent,
8914 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915{
8916 POINT pt;
8917 RECT rect;
8918
8919 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8920 return;
8921
8922 GetCursorPos(&pt);
8923 if (WindowFromPoint(pt) != s_textArea)
8924 return;
8925
8926 ScreenToClient(s_textArea, &pt);
8927 GetClientRect(s_textArea, &rect);
8928 if (!PtInRect(&rect, pt))
8929 return;
8930
8931 if (LastActivity > 0
8932 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8933 && (cur_beval->showState != ShS_PENDING
8934 || abs(cur_beval->x - pt.x) > 3
8935 || abs(cur_beval->y - pt.y) > 3))
8936 {
8937 /* Pointer resting in one place long enough, it's time to show
8938 * the tooltip. */
8939 cur_beval->showState = ShS_PENDING;
8940 cur_beval->x = pt.x;
8941 cur_beval->y = pt.y;
8942
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008943 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944
8945 if (cur_beval->msgCB != NULL)
8946 (*cur_beval->msgCB)(cur_beval, 0);
8947 }
8948}
8949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008950/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008952gui_mch_disable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008954 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008956 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957}
8958
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008959/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008961gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008963 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 if (beval == NULL)
8965 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008966 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008967 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008968 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969}
8970
8971 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008972gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973{
8974 POINT pt;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008975 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 if (beval->showState == ShS_SHOWING)
8977 return;
8978 GetCursorPos(&pt);
8979 ScreenToClient(s_textArea, &pt);
8980
8981 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
8982 /* cursor is still here */
8983 {
8984 gui_mch_disable_beval_area(cur_beval);
8985 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008986 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008988 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989}
8990
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008991/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008993gui_mch_create_beval_area(
8994 void *target, /* ignored, always use s_textArea */
8995 char_u *mesg,
8996 void (*mesgCB)(BalloonEval *, int),
8997 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998{
8999 /* partially stolen from gui_beval.c */
9000 BalloonEval *beval;
9001
9002 if (mesg != NULL && mesgCB != NULL)
9003 {
9004 EMSG(_("E232: Cannot create BalloonEval with both message and callback"));
9005 return NULL;
9006 }
9007
9008 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
9009 if (beval != NULL)
9010 {
9011 beval->target = s_textArea;
9012 beval->balloon = NULL;
9013
9014 beval->showState = ShS_NEUTRAL;
9015 beval->x = 0;
9016 beval->y = 0;
9017 beval->msg = mesg;
9018 beval->msgCB = mesgCB;
9019 beval->clientData = clientData;
9020
9021 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 cur_beval = beval;
9023
9024 if (p_beval)
9025 gui_mch_enable_beval_area(beval);
9026
9027 }
9028 return beval;
9029}
9030
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009031/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 static void
Bram Moolenaar442b4222010-05-24 21:34:22 +02009033Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034{
9035 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
9036 return;
9037
9038 if (cur_beval != NULL)
9039 {
Bram Moolenaar45360022005-07-21 21:08:21 +00009040 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 {
Bram Moolenaar45360022005-07-21 21:08:21 +00009042 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009043 // TRACE0("TTN_SHOW {{{");
9044 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00009045 break;
9046 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009047 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 delete_tooltip(cur_beval);
9049 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009050 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051
9052 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00009053 break;
9054 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00009055 {
9056 /* if you get there then we have new common controls */
9057 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
9058 info->lpszText = (LPSTR)info->lParam;
9059 info->uFlags |= TTF_DI_SETITEM;
9060 }
Bram Moolenaar45360022005-07-21 21:08:21 +00009061 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 }
9063 }
9064}
9065
9066 static void
9067TrackUserActivity(UINT uMsg)
9068{
9069 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
9070 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
9071 LastActivity = GetTickCount();
9072}
9073
9074 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01009075gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076{
9077 vim_free(beval);
9078}
9079#endif /* FEAT_BEVAL */
9080
9081#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
9082/*
9083 * We have multiple signs to draw at the same location. Draw the
9084 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
9085 */
9086 void
9087netbeans_draw_multisign_indicator(int row)
9088{
9089 int i;
9090 int y;
9091 int x;
9092
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009093 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009094 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009095
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 x = 0;
9097 y = TEXT_Y(row);
9098
9099 for (i = 0; i < gui.char_height - 3; i++)
9100 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
9101
9102 SetPixel(s_hdc, x+0, y, gui.currFgColor);
9103 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9104 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
9105 SetPixel(s_hdc, x+1, y, gui.currFgColor);
9106 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9107 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
9108 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9109}
Bram Moolenaare0874f82016-01-24 20:36:41 +01009110#endif