blob: 543d5d5a12ec40acc41789ae4e941ae3073ec72b [file] [log] [blame]
Bram Moolenaar060f1f02007-05-10 20:17:29 +00001/* vi:set ts=8 sts=4 sw=4:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10/*
11 * Windows GUI.
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * GUI support for Microsoft Windows, aka Win32. Also for Win64.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * George V. Reilly <george@reilly.org> wrote the original Win32 GUI.
16 * Robert Webb reworked it to use the existing GUI stuff and added menu,
17 * scrollbars, etc.
18 *
19 * Note: Clipboard stuff, for cutting and pasting text to other windows, is in
Bram Moolenaarcde88542015-08-11 19:14:00 +020020 * winclip.c. (It can also be done from the terminal version).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 *
22 * TODO: Some of the function signatures ought to be updated for Win64;
23 * e.g., replace LONG with LONG_PTR, etc.
24 */
25
Bram Moolenaar78e17622007-08-30 10:26:19 +000026#include "vim.h"
27
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020028#if defined(FEAT_DIRECTX)
29# include "gui_dwrite.h"
30#endif
31
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010032#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020033static DWriteContext *s_dwc = NULL;
34static int s_directx_enabled = 0;
35static int s_directx_load_attempted = 0;
36# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL)
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010037#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020038
Bram Moolenaar065bbac2016-02-20 13:08:46 +010039#ifdef FEAT_MENU
40static int gui_mswin_get_menu_height(int fix_window);
41#endif
42
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010043#if defined(FEAT_DIRECTX) || defined(PROTO)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020044 int
45directx_enabled(void)
46{
47 if (s_dwc != NULL)
48 return 1;
49 else if (s_directx_load_attempted)
50 return 0;
51 /* load DirectX */
52 DWrite_Init();
53 s_directx_load_attempted = 1;
54 s_dwc = DWriteContext_Open();
55 return s_dwc != NULL ? 1 : 0;
56}
57#endif
58
59#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
60 int
61gui_mch_set_rendering_options(char_u *s)
62{
63#ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020064 char_u *p, *q;
65
66 int dx_enable = 0;
67 int dx_flags = 0;
68 float dx_gamma = 0.0f;
69 float dx_contrast = 0.0f;
70 float dx_level = 0.0f;
71 int dx_geom = 0;
72 int dx_renmode = 0;
73 int dx_taamode = 0;
74
75 /* parse string as rendering options. */
76 for (p = s; p != NULL && *p != NUL; )
77 {
78 char_u item[256];
79 char_u name[128];
80 char_u value[128];
81
Bram Moolenaarcde88542015-08-11 19:14:00 +020082 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020083 if (p == NULL)
84 break;
85 q = &item[0];
86 copy_option_part(&q, name, sizeof(name), ":");
87 if (q == NULL)
88 return FAIL;
89 copy_option_part(&q, value, sizeof(value), ":");
90
91 if (STRCMP(name, "type") == 0)
92 {
93 if (STRCMP(value, "directx") == 0)
94 dx_enable = 1;
95 else
96 return FAIL;
97 }
98 else if (STRCMP(name, "gamma") == 0)
99 {
100 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100101 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200102 }
103 else if (STRCMP(name, "contrast") == 0)
104 {
105 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100106 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200107 }
108 else if (STRCMP(name, "level") == 0)
109 {
110 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100111 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200112 }
113 else if (STRCMP(name, "geom") == 0)
114 {
115 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100116 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200117 if (dx_geom < 0 || dx_geom > 2)
118 return FAIL;
119 }
120 else if (STRCMP(name, "renmode") == 0)
121 {
122 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100123 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200124 if (dx_renmode < 0 || dx_renmode > 6)
125 return FAIL;
126 }
127 else if (STRCMP(name, "taamode") == 0)
128 {
129 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100130 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200131 if (dx_taamode < 0 || dx_taamode > 3)
132 return FAIL;
133 }
134 else
135 return FAIL;
136 }
137
138 /* Enable DirectX/DirectWrite */
139 if (dx_enable)
140 {
141 if (!directx_enabled())
142 return FAIL;
143 DWriteContext_SetRenderingParams(s_dwc, NULL);
144 if (dx_flags)
145 {
146 DWriteRenderingParams param;
147 DWriteContext_GetRenderingParams(s_dwc, &param);
148 if (dx_flags & (1 << 0))
149 param.gamma = dx_gamma;
150 if (dx_flags & (1 << 1))
151 param.enhancedContrast = dx_contrast;
152 if (dx_flags & (1 << 2))
153 param.clearTypeLevel = dx_level;
154 if (dx_flags & (1 << 3))
155 param.pixelGeometry = dx_geom;
156 if (dx_flags & (1 << 4))
157 param.renderingMode = dx_renmode;
158 if (dx_flags & (1 << 5))
159 param.textAntialiasMode = dx_taamode;
160 DWriteContext_SetRenderingParams(s_dwc, &param);
161 }
162 }
163 s_directx_enabled = dx_enable;
164
165 return OK;
166#else
167 return FAIL;
168#endif
169}
170#endif
171
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172/*
173 * These are new in Windows ME/XP, only defined in recent compilers.
174 */
175#ifndef HANDLE_WM_XBUTTONUP
176# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
177 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
178#endif
179#ifndef HANDLE_WM_XBUTTONDOWN
180# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
181 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
182#endif
183#ifndef HANDLE_WM_XBUTTONDBLCLK
184# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
185 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
186#endif
187
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100188
189#include "version.h" /* used by dialog box routine for default title */
190#ifdef DEBUG
191# include <tchar.h>
192#endif
193
194/* cproto fails on missing include files */
195#ifndef PROTO
196
197#ifndef __MINGW32__
198# include <shellapi.h>
199#endif
200#if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL) || defined(FEAT_GUI_TABLINE)
201# include <commctrl.h>
202#endif
203#include <windowsx.h>
204
205#ifdef GLOBAL_IME
206# include "glbl_ime.h"
207#endif
208
209#endif /* PROTO */
210
211#ifdef FEAT_MENU
212# define MENUHINTS /* show menu hints in command line */
213#endif
214
215/* Some parameters for dialog boxes. All in pixels. */
216#define DLG_PADDING_X 10
217#define DLG_PADDING_Y 10
218#define DLG_OLD_STYLE_PADDING_X 5
219#define DLG_OLD_STYLE_PADDING_Y 5
220#define DLG_VERT_PADDING_X 4 /* For vertical buttons */
221#define DLG_VERT_PADDING_Y 4
222#define DLG_ICON_WIDTH 34
223#define DLG_ICON_HEIGHT 34
224#define DLG_MIN_WIDTH 150
225#define DLG_FONT_NAME "MS Sans Serif"
226#define DLG_FONT_POINT_SIZE 8
227#define DLG_MIN_MAX_WIDTH 400
228#define DLG_MIN_MAX_HEIGHT 400
229
230#define DLG_NONBUTTON_CONTROL 5000 /* First ID of non-button controls */
231
232#ifndef WM_XBUTTONDOWN /* For Win2K / winME ONLY */
233# define WM_XBUTTONDOWN 0x020B
234# define WM_XBUTTONUP 0x020C
235# define WM_XBUTTONDBLCLK 0x020D
236# define MK_XBUTTON1 0x0020
237# define MK_XBUTTON2 0x0040
238#endif
239
240#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100242 * Define a few things for generating prototypes. This is just to avoid
243 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100245# define APIENTRY
246# define CALLBACK
247# define CONST
248# define FAR
249# define NEAR
250# define _cdecl
251typedef int BOOL;
252typedef int BYTE;
253typedef int DWORD;
254typedef int WCHAR;
255typedef int ENUMLOGFONT;
256typedef int FINDREPLACE;
257typedef int HANDLE;
258typedef int HBITMAP;
259typedef int HBRUSH;
260typedef int HDROP;
261typedef int INT;
262typedef int LOGFONT[];
263typedef int LPARAM;
264typedef int LPCREATESTRUCT;
265typedef int LPCSTR;
266typedef int LPCTSTR;
267typedef int LPRECT;
268typedef int LPSTR;
269typedef int LPWINDOWPOS;
270typedef int LPWORD;
271typedef int LRESULT;
272typedef int HRESULT;
273# undef MSG
274typedef int MSG;
275typedef int NEWTEXTMETRIC;
276typedef int OSVERSIONINFO;
277typedef int PWORD;
278typedef int RECT;
279typedef int UINT;
280typedef int WORD;
281typedef int WPARAM;
282typedef int POINT;
283typedef void *HINSTANCE;
284typedef void *HMENU;
285typedef void *HWND;
286typedef void *HDC;
287typedef void VOID;
288typedef int LPNMHDR;
289typedef int LONG;
290typedef int WNDPROC;
291#endif
292
293#ifndef GET_X_LPARAM
294# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
295#endif
296
297static void _OnPaint( HWND hwnd);
298static void clear_rect(RECT *rcp);
299
300static WORD s_dlgfntheight; /* height of the dialog font */
301static WORD s_dlgfntwidth; /* width of the dialog font */
302
303#ifdef FEAT_MENU
304static HMENU s_menuBar = NULL;
305#endif
306#ifdef FEAT_TEAROFF
307static void rebuild_tearoff(vimmenu_T *menu);
308static HBITMAP s_htearbitmap; /* bitmap used to indicate tearoff */
309#endif
310
311/* Flag that is set while processing a message that must not be interrupted by
312 * processing another message. */
313static int s_busy_processing = FALSE;
314
315static int destroying = FALSE; /* call DestroyWindow() ourselves */
316
317#ifdef MSWIN_FIND_REPLACE
318static UINT s_findrep_msg = 0; /* set in gui_w[16/32].c */
319static FINDREPLACE s_findrep_struct;
320# if defined(FEAT_MBYTE) && defined(WIN3264)
321static FINDREPLACEW s_findrep_struct_w;
322# endif
323static HWND s_findrep_hwnd = NULL;
324static int s_findrep_is_find; /* TRUE for find dialog, FALSE
325 for find/replace dialog */
326#endif
327
328static HINSTANCE s_hinst = NULL;
Bram Moolenaar85b11762016-02-27 18:13:23 +0100329#if !defined(FEAT_GUI)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100330static
331#endif
332HWND s_hwnd = NULL;
333static HDC s_hdc = NULL;
334static HBRUSH s_brush = NULL;
335
336#ifdef FEAT_TOOLBAR
337static HWND s_toolbarhwnd = NULL;
338static WNDPROC s_toolbar_wndproc = NULL;
339#endif
340
341#ifdef FEAT_GUI_TABLINE
342static HWND s_tabhwnd = NULL;
343static WNDPROC s_tabline_wndproc = NULL;
344static int showing_tabline = 0;
345#endif
346
347static WPARAM s_wParam = 0;
348static LPARAM s_lParam = 0;
349
350static HWND s_textArea = NULL;
351static UINT s_uMsg = 0;
352
353static char_u *s_textfield; /* Used by dialogs to pass back strings */
354
355static int s_need_activate = FALSE;
356
357/* This variable is set when waiting for an event, which is the only moment
358 * scrollbar dragging can be done directly. It's not allowed while commands
359 * are executed, because it may move the cursor and that may cause unexpected
360 * problems (e.g., while ":s" is working).
361 */
362static int allow_scrollbar = FALSE;
363
364#ifdef GLOBAL_IME
365# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
366#else
367# define MyTranslateMessage(x) TranslateMessage(x)
368#endif
369
370#if (defined(WIN3264) && defined(FEAT_MBYTE)) || defined(GLOBAL_IME)
371 /* use of WindowProc depends on wide_WindowProc */
372# define MyWindowProc vim_WindowProc
373#else
374 /* use ordinary WindowProc */
375# define MyWindowProc DefWindowProc
376#endif
377
378extern int current_font_height; /* this is in os_mswin.c */
379
380static struct
381{
382 UINT key_sym;
383 char_u vim_code0;
384 char_u vim_code1;
385} special_keys[] =
386{
387 {VK_UP, 'k', 'u'},
388 {VK_DOWN, 'k', 'd'},
389 {VK_LEFT, 'k', 'l'},
390 {VK_RIGHT, 'k', 'r'},
391
392 {VK_F1, 'k', '1'},
393 {VK_F2, 'k', '2'},
394 {VK_F3, 'k', '3'},
395 {VK_F4, 'k', '4'},
396 {VK_F5, 'k', '5'},
397 {VK_F6, 'k', '6'},
398 {VK_F7, 'k', '7'},
399 {VK_F8, 'k', '8'},
400 {VK_F9, 'k', '9'},
401 {VK_F10, 'k', ';'},
402
403 {VK_F11, 'F', '1'},
404 {VK_F12, 'F', '2'},
405 {VK_F13, 'F', '3'},
406 {VK_F14, 'F', '4'},
407 {VK_F15, 'F', '5'},
408 {VK_F16, 'F', '6'},
409 {VK_F17, 'F', '7'},
410 {VK_F18, 'F', '8'},
411 {VK_F19, 'F', '9'},
412 {VK_F20, 'F', 'A'},
413
414 {VK_F21, 'F', 'B'},
415#ifdef FEAT_NETBEANS_INTG
416 {VK_PAUSE, 'F', 'B'}, /* Pause == F21 (see gui_gtk_x11.c) */
417#endif
418 {VK_F22, 'F', 'C'},
419 {VK_F23, 'F', 'D'},
420 {VK_F24, 'F', 'E'}, /* winuser.h defines up to F24 */
421
422 {VK_HELP, '%', '1'},
423 {VK_BACK, 'k', 'b'},
424 {VK_INSERT, 'k', 'I'},
425 {VK_DELETE, 'k', 'D'},
426 {VK_HOME, 'k', 'h'},
427 {VK_END, '@', '7'},
428 {VK_PRIOR, 'k', 'P'},
429 {VK_NEXT, 'k', 'N'},
430 {VK_PRINT, '%', '9'},
431 {VK_ADD, 'K', '6'},
432 {VK_SUBTRACT, 'K', '7'},
433 {VK_DIVIDE, 'K', '8'},
434 {VK_MULTIPLY, 'K', '9'},
435 {VK_SEPARATOR, 'K', 'A'}, /* Keypad Enter */
436 {VK_DECIMAL, 'K', 'B'},
437
438 {VK_NUMPAD0, 'K', 'C'},
439 {VK_NUMPAD1, 'K', 'D'},
440 {VK_NUMPAD2, 'K', 'E'},
441 {VK_NUMPAD3, 'K', 'F'},
442 {VK_NUMPAD4, 'K', 'G'},
443 {VK_NUMPAD5, 'K', 'H'},
444 {VK_NUMPAD6, 'K', 'I'},
445 {VK_NUMPAD7, 'K', 'J'},
446 {VK_NUMPAD8, 'K', 'K'},
447 {VK_NUMPAD9, 'K', 'L'},
448
449 /* Keys that we want to be able to use any modifier with: */
450 {VK_SPACE, ' ', NUL},
451 {VK_TAB, TAB, NUL},
452 {VK_ESCAPE, ESC, NUL},
453 {NL, NL, NUL},
454 {CAR, CAR, NUL},
455
456 /* End of list marker: */
457 {0, 0, 0}
458};
459
460/* Local variables */
461static int s_button_pending = -1;
462
463/* s_getting_focus is set when we got focus but didn't see mouse-up event yet,
464 * so don't reset s_button_pending. */
465static int s_getting_focus = FALSE;
466
467static int s_x_pending;
468static int s_y_pending;
469static UINT s_kFlags_pending;
470static UINT s_wait_timer = 0; /* Timer for get char from user */
471static int s_timed_out = FALSE;
472static int dead_key = 0; /* 0: no dead key, 1: dead key pressed */
473
474#ifdef WIN3264
475static OSVERSIONINFO os_version; /* like it says. Init in gui_mch_init() */
476#endif
477
478#ifdef FEAT_BEVAL
479/* balloon-eval WM_NOTIFY_HANDLER */
480static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
481static void TrackUserActivity(UINT uMsg);
482#endif
483
484/*
485 * For control IME.
486 *
487 * These LOGFONT used for IME.
488 */
489#ifdef FEAT_MBYTE
490# ifdef USE_IM_CONTROL
491/* holds LOGFONT for 'guifontwide' if available, otherwise 'guifont' */
492static LOGFONT norm_logfont;
493/* holds LOGFONT for 'guifont' always. */
494static LOGFONT sub_logfont;
495# endif
496#endif
497
498#ifdef FEAT_MBYTE_IME
499static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
500#endif
501
502#if defined(FEAT_BROWSE)
503static char_u *convert_filter(char_u *s);
504#endif
505
506#ifdef DEBUG_PRINT_ERROR
507/*
508 * Print out the last Windows error message
509 */
510 static void
511print_windows_error(void)
512{
513 LPVOID lpMsgBuf;
514
515 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
516 NULL, GetLastError(),
517 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
518 (LPTSTR) &lpMsgBuf, 0, NULL);
519 TRACE1("Error: %s\n", lpMsgBuf);
520 LocalFree(lpMsgBuf);
521}
522#endif
523
524/*
525 * Cursor blink functions.
526 *
527 * This is a simple state machine:
528 * BLINK_NONE not blinking at all
529 * BLINK_OFF blinking, cursor is not shown
530 * BLINK_ON blinking, cursor is shown
531 */
532
533#define BLINK_NONE 0
534#define BLINK_OFF 1
535#define BLINK_ON 2
536
537static int blink_state = BLINK_NONE;
538static long_u blink_waittime = 700;
539static long_u blink_ontime = 400;
540static long_u blink_offtime = 250;
541static UINT blink_timer = 0;
542
543 void
544gui_mch_set_blinking(long wait, long on, long off)
545{
546 blink_waittime = wait;
547 blink_ontime = on;
548 blink_offtime = off;
549}
550
551/* ARGSUSED */
552 static VOID CALLBACK
553_OnBlinkTimer(
554 HWND hwnd,
555 UINT uMsg,
556 UINT idEvent,
557 DWORD dwTime)
558{
559 MSG msg;
560
561 /*
562 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
563 */
564
565 KillTimer(NULL, idEvent);
566
567 /* Eat spurious WM_TIMER messages */
568 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
569 ;
570
571 if (blink_state == BLINK_ON)
572 {
573 gui_undraw_cursor();
574 blink_state = BLINK_OFF;
575 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
576 (TIMERPROC)_OnBlinkTimer);
577 }
578 else
579 {
580 gui_update_cursor(TRUE, FALSE);
581 blink_state = BLINK_ON;
582 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
583 (TIMERPROC)_OnBlinkTimer);
584 }
585}
586
587 static void
588gui_mswin_rm_blink_timer(void)
589{
590 MSG msg;
591
592 if (blink_timer != 0)
593 {
594 KillTimer(NULL, blink_timer);
595 /* Eat spurious WM_TIMER messages */
596 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
597 ;
598 blink_timer = 0;
599 }
600}
601
602/*
603 * Stop the cursor blinking. Show the cursor if it wasn't shown.
604 */
605 void
606gui_mch_stop_blink(void)
607{
608 gui_mswin_rm_blink_timer();
609 if (blink_state == BLINK_OFF)
610 gui_update_cursor(TRUE, FALSE);
611 blink_state = BLINK_NONE;
612}
613
614/*
615 * Start the cursor blinking. If it was already blinking, this restarts the
616 * waiting time and shows the cursor.
617 */
618 void
619gui_mch_start_blink(void)
620{
621 gui_mswin_rm_blink_timer();
622
623 /* Only switch blinking on if none of the times is zero */
624 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
625 {
626 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
627 (TIMERPROC)_OnBlinkTimer);
628 blink_state = BLINK_ON;
629 gui_update_cursor(TRUE, FALSE);
630 }
631}
632
633/*
634 * Call-back routines.
635 */
636
637/*ARGSUSED*/
638 static VOID CALLBACK
639_OnTimer(
640 HWND hwnd,
641 UINT uMsg,
642 UINT idEvent,
643 DWORD dwTime)
644{
645 MSG msg;
646
647 /*
648 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
649 */
650 KillTimer(NULL, idEvent);
651 s_timed_out = TRUE;
652
653 /* Eat spurious WM_TIMER messages */
654 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
655 ;
656 if (idEvent == s_wait_timer)
657 s_wait_timer = 0;
658}
659
660/*ARGSUSED*/
661 static void
662_OnDeadChar(
663 HWND hwnd,
664 UINT ch,
665 int cRepeat)
666{
667 dead_key = 1;
668}
669
670/*
671 * Convert Unicode character "ch" to bytes in "string[slen]".
672 * When "had_alt" is TRUE the ALT key was included in "ch".
673 * Return the length.
674 */
675 static int
676char_to_string(int ch, char_u *string, int slen, int had_alt)
677{
678 int len;
679 int i;
680#ifdef FEAT_MBYTE
681 WCHAR wstring[2];
682 char_u *ws = NULL;;
683
684 if (os_version.dwPlatformId != VER_PLATFORM_WIN32_NT)
685 {
686 /* On Windows 95/98 we apparently get the character in the active
687 * codepage, not in UCS-2. If conversion is needed convert it to
688 * UCS-2 first. */
689 if ((int)GetACP() == enc_codepage)
690 len = 0; /* no conversion required */
691 else
692 {
693 string[0] = ch;
694 len = MultiByteToWideChar(GetACP(), 0, (LPCSTR)string,
695 1, wstring, 2);
696 }
697 }
698 else
699 {
700 wstring[0] = ch;
701 len = 1;
702 }
703
704 if (len > 0)
705 {
706 /* "ch" is a UTF-16 character. Convert it to a string of bytes. When
707 * "enc_codepage" is non-zero use the standard Win32 function,
708 * otherwise use our own conversion function (e.g., for UTF-8). */
709 if (enc_codepage > 0)
710 {
711 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
712 (LPSTR)string, slen, 0, NULL);
713 /* If we had included the ALT key into the character but now the
714 * upper bit is no longer set, that probably means the conversion
715 * failed. Convert the original character and set the upper bit
716 * afterwards. */
717 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
718 {
719 wstring[0] = ch & 0x7f;
720 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
721 (LPSTR)string, slen, 0, NULL);
722 if (len == 1) /* safety check */
723 string[0] |= 0x80;
724 }
725 }
726 else
727 {
728 len = 1;
729 ws = utf16_to_enc(wstring, &len);
730 if (ws == NULL)
731 len = 0;
732 else
733 {
734 if (len > slen) /* just in case */
735 len = slen;
736 mch_memmove(string, ws, len);
737 vim_free(ws);
738 }
739 }
740 }
741
742 if (len == 0)
743#endif
744 {
745 string[0] = ch;
746 len = 1;
747 }
748
749 for (i = 0; i < len; ++i)
750 if (string[i] == CSI && len <= slen - 2)
751 {
752 /* Insert CSI as K_CSI. */
753 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
754 string[++i] = KS_EXTRA;
755 string[++i] = (int)KE_CSI;
756 len += 2;
757 }
758
759 return len;
760}
761
762/*
763 * Key hit, add it to the input buffer.
764 */
765/*ARGSUSED*/
766 static void
767_OnChar(
768 HWND hwnd,
769 UINT ch,
770 int cRepeat)
771{
772 char_u string[40];
773 int len = 0;
774
775 dead_key = 0;
776
777 len = char_to_string(ch, string, 40, FALSE);
778 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
779 {
780 trash_input_buf();
781 got_int = TRUE;
782 }
783
784 add_to_input_buf(string, len);
785}
786
787/*
788 * Alt-Key hit, add it to the input buffer.
789 */
790/*ARGSUSED*/
791 static void
792_OnSysChar(
793 HWND hwnd,
794 UINT cch,
795 int cRepeat)
796{
797 char_u string[40]; /* Enough for multibyte character */
798 int len;
799 int modifiers;
800 int ch = cch; /* special keys are negative */
801
802 dead_key = 0;
803
804 /* TRACE("OnSysChar(%d, %c)\n", ch, ch); */
805
806 /* OK, we have a character key (given by ch) which was entered with the
807 * ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
808 * that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
809 * CAPSLOCK is pressed) at this point.
810 */
811 modifiers = MOD_MASK_ALT;
812 if (GetKeyState(VK_SHIFT) & 0x8000)
813 modifiers |= MOD_MASK_SHIFT;
814 if (GetKeyState(VK_CONTROL) & 0x8000)
815 modifiers |= MOD_MASK_CTRL;
816
817 ch = simplify_key(ch, &modifiers);
818 /* remove the SHIFT modifier for keys where it's already included, e.g.,
819 * '(' and '*' */
820 if (ch < 0x100 && !isalpha(ch) && isprint(ch))
821 modifiers &= ~MOD_MASK_SHIFT;
822
823 /* Interpret the ALT key as making the key META, include SHIFT, etc. */
824 ch = extract_modifiers(ch, &modifiers);
825 if (ch == CSI)
826 ch = K_CSI;
827
828 len = 0;
829 if (modifiers)
830 {
831 string[len++] = CSI;
832 string[len++] = KS_MODIFIER;
833 string[len++] = modifiers;
834 }
835
836 if (IS_SPECIAL((int)ch))
837 {
838 string[len++] = CSI;
839 string[len++] = K_SECOND((int)ch);
840 string[len++] = K_THIRD((int)ch);
841 }
842 else
843 {
844 /* Although the documentation isn't clear about it, we assume "ch" is
845 * a Unicode character. */
846 len += char_to_string(ch, string + len, 40 - len, TRUE);
847 }
848
849 add_to_input_buf(string, len);
850}
851
852 static void
853_OnMouseEvent(
854 int button,
855 int x,
856 int y,
857 int repeated_click,
858 UINT keyFlags)
859{
860 int vim_modifiers = 0x0;
861
862 s_getting_focus = FALSE;
863
864 if (keyFlags & MK_SHIFT)
865 vim_modifiers |= MOUSE_SHIFT;
866 if (keyFlags & MK_CONTROL)
867 vim_modifiers |= MOUSE_CTRL;
868 if (GetKeyState(VK_MENU) & 0x8000)
869 vim_modifiers |= MOUSE_ALT;
870
871 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
872}
873
874/*ARGSUSED*/
875 static void
876_OnMouseButtonDown(
877 HWND hwnd,
878 BOOL fDoubleClick,
879 int x,
880 int y,
881 UINT keyFlags)
882{
883 static LONG s_prevTime = 0;
884
885 LONG currentTime = GetMessageTime();
886 int button = -1;
887 int repeated_click;
888
889 /* Give main window the focus: this is so the cursor isn't hollow. */
890 (void)SetFocus(s_hwnd);
891
892 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
893 button = MOUSE_LEFT;
894 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
895 button = MOUSE_MIDDLE;
896 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
897 button = MOUSE_RIGHT;
898 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
899 {
900#ifndef GET_XBUTTON_WPARAM
901# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
902#endif
903 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
904 }
905 else if (s_uMsg == WM_CAPTURECHANGED)
906 {
907 /* on W95/NT4, somehow you get in here with an odd Msg
908 * if you press one button while holding down the other..*/
909 if (s_button_pending == MOUSE_LEFT)
910 button = MOUSE_RIGHT;
911 else
912 button = MOUSE_LEFT;
913 }
914 if (button >= 0)
915 {
916 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
917
918 /*
919 * Holding down the left and right buttons simulates pushing the middle
920 * button.
921 */
922 if (repeated_click
923 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
924 || (button == MOUSE_RIGHT
925 && s_button_pending == MOUSE_LEFT)))
926 {
927 /*
928 * Hmm, gui.c will ignore more than one button down at a time, so
929 * pretend we let go of it first.
930 */
931 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
932 button = MOUSE_MIDDLE;
933 repeated_click = FALSE;
934 s_button_pending = -1;
935 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
936 }
937 else if ((repeated_click)
938 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
939 {
940 if (s_button_pending > -1)
941 {
942 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
943 s_button_pending = -1;
944 }
945 /* TRACE("Button down at x %d, y %d\n", x, y); */
946 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
947 }
948 else
949 {
950 /*
951 * If this is the first press (i.e. not a multiple click) don't
952 * action immediately, but store and wait for:
953 * i) button-up
954 * ii) mouse move
955 * iii) another button press
956 * before using it.
957 * This enables us to make left+right simulate middle button,
958 * without left or right being actioned first. The side-effect is
959 * that if you click and hold the mouse without dragging, the
960 * cursor doesn't move until you release the button. In practice
961 * this is hardly a problem.
962 */
963 s_button_pending = button;
964 s_x_pending = x;
965 s_y_pending = y;
966 s_kFlags_pending = keyFlags;
967 }
968
969 s_prevTime = currentTime;
970 }
971}
972
973/*ARGSUSED*/
974 static void
975_OnMouseMoveOrRelease(
976 HWND hwnd,
977 int x,
978 int y,
979 UINT keyFlags)
980{
981 int button;
982
983 s_getting_focus = FALSE;
984 if (s_button_pending > -1)
985 {
986 /* Delayed action for mouse down event */
987 _OnMouseEvent(s_button_pending, s_x_pending,
988 s_y_pending, FALSE, s_kFlags_pending);
989 s_button_pending = -1;
990 }
991 if (s_uMsg == WM_MOUSEMOVE)
992 {
993 /*
994 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
995 * down.
996 */
997 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
998 | MK_XBUTTON1 | MK_XBUTTON2)))
999 {
1000 gui_mouse_moved(x, y);
1001 return;
1002 }
1003
1004 /*
1005 * While button is down, keep grabbing mouse move events when
1006 * the mouse goes outside the window
1007 */
1008 SetCapture(s_textArea);
1009 button = MOUSE_DRAG;
1010 /* TRACE(" move at x %d, y %d\n", x, y); */
1011 }
1012 else
1013 {
1014 ReleaseCapture();
1015 button = MOUSE_RELEASE;
1016 /* TRACE(" up at x %d, y %d\n", x, y); */
1017 }
1018
1019 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1020}
1021
1022#ifdef FEAT_MENU
1023/*
1024 * Find the vimmenu_T with the given id
1025 */
1026 static vimmenu_T *
1027gui_mswin_find_menu(
1028 vimmenu_T *pMenu,
1029 int id)
1030{
1031 vimmenu_T *pChildMenu;
1032
1033 while (pMenu)
1034 {
1035 if (pMenu->id == (UINT)id)
1036 break;
1037 if (pMenu->children != NULL)
1038 {
1039 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1040 if (pChildMenu)
1041 {
1042 pMenu = pChildMenu;
1043 break;
1044 }
1045 }
1046 pMenu = pMenu->next;
1047 }
1048 return pMenu;
1049}
1050
1051/*ARGSUSED*/
1052 static void
1053_OnMenu(
1054 HWND hwnd,
1055 int id,
1056 HWND hwndCtl,
1057 UINT codeNotify)
1058{
1059 vimmenu_T *pMenu;
1060
1061 pMenu = gui_mswin_find_menu(root_menu, id);
1062 if (pMenu)
1063 gui_menu_cb(pMenu);
1064}
1065#endif
1066
1067#ifdef MSWIN_FIND_REPLACE
1068# if defined(FEAT_MBYTE) && defined(WIN3264)
1069/*
1070 * copy useful data from structure LPFINDREPLACE to structure LPFINDREPLACEW
1071 */
1072 static void
1073findrep_atow(LPFINDREPLACEW lpfrw, LPFINDREPLACE lpfr)
1074{
1075 WCHAR *wp;
1076
1077 lpfrw->hwndOwner = lpfr->hwndOwner;
1078 lpfrw->Flags = lpfr->Flags;
1079
1080 wp = enc_to_utf16((char_u *)lpfr->lpstrFindWhat, NULL);
1081 wcsncpy(lpfrw->lpstrFindWhat, wp, lpfrw->wFindWhatLen - 1);
1082 vim_free(wp);
1083
1084 /* the field "lpstrReplaceWith" doesn't need to be copied */
1085}
1086
1087/*
1088 * copy useful data from structure LPFINDREPLACEW to structure LPFINDREPLACE
1089 */
1090 static void
1091findrep_wtoa(LPFINDREPLACE lpfr, LPFINDREPLACEW lpfrw)
1092{
1093 char_u *p;
1094
1095 lpfr->Flags = lpfrw->Flags;
1096
1097 p = utf16_to_enc((short_u*)lpfrw->lpstrFindWhat, NULL);
1098 vim_strncpy((char_u *)lpfr->lpstrFindWhat, p, lpfr->wFindWhatLen - 1);
1099 vim_free(p);
1100
1101 p = utf16_to_enc((short_u*)lpfrw->lpstrReplaceWith, NULL);
1102 vim_strncpy((char_u *)lpfr->lpstrReplaceWith, p, lpfr->wReplaceWithLen - 1);
1103 vim_free(p);
1104}
1105# endif
1106
1107/*
1108 * Handle a Find/Replace window message.
1109 */
1110 static void
1111_OnFindRepl(void)
1112{
1113 int flags = 0;
1114 int down;
1115
1116# if defined(FEAT_MBYTE) && defined(WIN3264)
1117 /* If the OS is Windows NT, and 'encoding' differs from active codepage:
1118 * convert text from wide string. */
1119 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
1120 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1121 {
1122 findrep_wtoa(&s_findrep_struct, &s_findrep_struct_w);
1123 }
1124# endif
1125
1126 if (s_findrep_struct.Flags & FR_DIALOGTERM)
1127 /* Give main window the focus back. */
1128 (void)SetFocus(s_hwnd);
1129
1130 if (s_findrep_struct.Flags & FR_FINDNEXT)
1131 {
1132 flags = FRD_FINDNEXT;
1133
1134 /* Give main window the focus back: this is so the cursor isn't
1135 * hollow. */
1136 (void)SetFocus(s_hwnd);
1137 }
1138 else if (s_findrep_struct.Flags & FR_REPLACE)
1139 {
1140 flags = FRD_REPLACE;
1141
1142 /* Give main window the focus back: this is so the cursor isn't
1143 * hollow. */
1144 (void)SetFocus(s_hwnd);
1145 }
1146 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1147 {
1148 flags = FRD_REPLACEALL;
1149 }
1150
1151 if (flags != 0)
1152 {
1153 /* Call the generic GUI function to do the actual work. */
1154 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1155 flags |= FRD_WHOLE_WORD;
1156 if (s_findrep_struct.Flags & FR_MATCHCASE)
1157 flags |= FRD_MATCH_CASE;
1158 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
1159 gui_do_findrepl(flags, (char_u *)s_findrep_struct.lpstrFindWhat,
1160 (char_u *)s_findrep_struct.lpstrReplaceWith, down);
1161 }
1162}
1163#endif
1164
1165 static void
1166HandleMouseHide(UINT uMsg, LPARAM lParam)
1167{
1168 static LPARAM last_lParam = 0L;
1169
1170 /* We sometimes get a mousemove when the mouse didn't move... */
1171 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1172 {
1173 if (lParam == last_lParam)
1174 return;
1175 last_lParam = lParam;
1176 }
1177
1178 /* Handle specially, to centralise coding. We need to be sure we catch all
1179 * possible events which should cause us to restore the cursor (as it is a
1180 * shared resource, we take full responsibility for it).
1181 */
1182 switch (uMsg)
1183 {
1184 case WM_KEYUP:
1185 case WM_CHAR:
1186 /*
1187 * blank out the pointer if necessary
1188 */
1189 if (p_mh)
1190 gui_mch_mousehide(TRUE);
1191 break;
1192
1193 case WM_SYSKEYUP: /* show the pointer when a system-key is pressed */
1194 case WM_SYSCHAR:
1195 case WM_MOUSEMOVE: /* show the pointer on any mouse action */
1196 case WM_LBUTTONDOWN:
1197 case WM_LBUTTONUP:
1198 case WM_MBUTTONDOWN:
1199 case WM_MBUTTONUP:
1200 case WM_RBUTTONDOWN:
1201 case WM_RBUTTONUP:
1202 case WM_XBUTTONDOWN:
1203 case WM_XBUTTONUP:
1204 case WM_NCMOUSEMOVE:
1205 case WM_NCLBUTTONDOWN:
1206 case WM_NCLBUTTONUP:
1207 case WM_NCMBUTTONDOWN:
1208 case WM_NCMBUTTONUP:
1209 case WM_NCRBUTTONDOWN:
1210 case WM_NCRBUTTONUP:
1211 case WM_KILLFOCUS:
1212 /*
1213 * if the pointer is currently hidden, then we should show it.
1214 */
1215 gui_mch_mousehide(FALSE);
1216 break;
1217 }
1218}
1219
1220 static LRESULT CALLBACK
1221_TextAreaWndProc(
1222 HWND hwnd,
1223 UINT uMsg,
1224 WPARAM wParam,
1225 LPARAM lParam)
1226{
1227 /*
1228 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1229 hwnd, uMsg, wParam, lParam);
1230 */
1231
1232 HandleMouseHide(uMsg, lParam);
1233
1234 s_uMsg = uMsg;
1235 s_wParam = wParam;
1236 s_lParam = lParam;
1237
1238#ifdef FEAT_BEVAL
1239 TrackUserActivity(uMsg);
1240#endif
1241
1242 switch (uMsg)
1243 {
1244 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1245 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1246 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1247 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1248 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1249 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1250 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1251 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1252 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1253 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1254 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1255 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1256 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1257 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
1258
1259#ifdef FEAT_BEVAL
1260 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1261 return TRUE;
1262#endif
1263 default:
1264 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1265 }
1266}
1267
1268#if (defined(WIN3264) && defined(FEAT_MBYTE)) \
1269 || defined(GLOBAL_IME) \
1270 || defined(PROTO)
1271# ifdef PROTO
1272typedef int WINAPI;
1273# endif
1274
1275 LRESULT WINAPI
1276vim_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1277{
1278# ifdef GLOBAL_IME
1279 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
1280# else
1281 if (wide_WindowProc)
1282 return DefWindowProcW(hwnd, message, wParam, lParam);
1283 return DefWindowProc(hwnd, message, wParam, lParam);
1284#endif
1285}
1286#endif
1287
1288/*
1289 * Called when the foreground or background color has been changed.
1290 */
1291 void
1292gui_mch_new_colors(void)
1293{
1294 /* nothing to do? */
1295}
1296
1297/*
1298 * Set the colors to their default values.
1299 */
1300 void
1301gui_mch_def_colors(void)
1302{
1303 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1304 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1305 gui.def_norm_pixel = gui.norm_pixel;
1306 gui.def_back_pixel = gui.back_pixel;
1307}
1308
1309/*
1310 * Open the GUI window which was created by a call to gui_mch_init().
1311 */
1312 int
1313gui_mch_open(void)
1314{
1315#ifndef SW_SHOWDEFAULT
1316# define SW_SHOWDEFAULT 10 /* Borland 5.0 doesn't have it */
1317#endif
1318 /* Actually open the window, if not already visible
1319 * (may be done already in gui_mch_set_shellsize) */
1320 if (!IsWindowVisible(s_hwnd))
1321 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1322
1323#ifdef MSWIN_FIND_REPLACE
1324 /* Init replace string here, so that we keep it when re-opening the
1325 * dialog. */
1326 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1327#endif
1328
1329 return OK;
1330}
1331
1332/*
1333 * Get the position of the top left corner of the window.
1334 */
1335 int
1336gui_mch_get_winpos(int *x, int *y)
1337{
1338 RECT rect;
1339
1340 GetWindowRect(s_hwnd, &rect);
1341 *x = rect.left;
1342 *y = rect.top;
1343 return OK;
1344}
1345
1346/*
1347 * Set the position of the top left corner of the window to the given
1348 * coordinates.
1349 */
1350 void
1351gui_mch_set_winpos(int x, int y)
1352{
1353 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1354 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1355}
1356 void
1357gui_mch_set_text_area_pos(int x, int y, int w, int h)
1358{
1359 static int oldx = 0;
1360 static int oldy = 0;
1361
1362 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1363
1364#ifdef FEAT_TOOLBAR
1365 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1366 SendMessage(s_toolbarhwnd, WM_SIZE,
1367 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1368#endif
1369#if defined(FEAT_GUI_TABLINE)
1370 if (showing_tabline)
1371 {
1372 int top = 0;
1373 RECT rect;
1374
1375# ifdef FEAT_TOOLBAR
1376 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1377 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1378# endif
1379 GetClientRect(s_hwnd, &rect);
1380 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1381 }
1382#endif
1383
1384 /* When side scroll bar is unshown, the size of window will change.
1385 * then, the text area move left or right. thus client rect should be
1386 * forcedly redrawn. (Yasuhiro Matsumoto) */
1387 if (oldx != x || oldy != y)
1388 {
1389 InvalidateRect(s_hwnd, NULL, FALSE);
1390 oldx = x;
1391 oldy = y;
1392 }
1393}
1394
1395
1396/*
1397 * Scrollbar stuff:
1398 */
1399
1400 void
1401gui_mch_enable_scrollbar(
1402 scrollbar_T *sb,
1403 int flag)
1404{
1405 ShowScrollBar(sb->id, SB_CTL, flag);
1406
1407 /* TODO: When the window is maximized, the size of the window stays the
1408 * same, thus the size of the text area changes. On Win98 it's OK, on Win
1409 * NT 4.0 it's not... */
1410}
1411
1412 void
1413gui_mch_set_scrollbar_pos(
1414 scrollbar_T *sb,
1415 int x,
1416 int y,
1417 int w,
1418 int h)
1419{
1420 SetWindowPos(sb->id, NULL, x, y, w, h,
1421 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1422}
1423
1424 void
1425gui_mch_create_scrollbar(
1426 scrollbar_T *sb,
1427 int orient) /* SBAR_VERT or SBAR_HORIZ */
1428{
1429 sb->id = CreateWindow(
1430 "SCROLLBAR", "Scrollbar",
1431 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
1432 10, /* Any value will do for now */
1433 10, /* Any value will do for now */
1434 s_hwnd, NULL,
1435 s_hinst, NULL);
1436}
1437
1438/*
1439 * Find the scrollbar with the given hwnd.
1440 */
1441 static scrollbar_T *
1442gui_mswin_find_scrollbar(HWND hwnd)
1443{
1444 win_T *wp;
1445
1446 if (gui.bottom_sbar.id == hwnd)
1447 return &gui.bottom_sbar;
1448 FOR_ALL_WINDOWS(wp)
1449 {
1450 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1451 return &wp->w_scrollbars[SBAR_LEFT];
1452 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1453 return &wp->w_scrollbars[SBAR_RIGHT];
1454 }
1455 return NULL;
1456}
1457
1458/*
1459 * Get the character size of a font.
1460 */
1461 static void
1462GetFontSize(GuiFont font)
1463{
1464 HWND hwnd = GetDesktopWindow();
1465 HDC hdc = GetWindowDC(hwnd);
1466 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
1467 TEXTMETRIC tm;
1468
1469 GetTextMetrics(hdc, &tm);
1470 gui.char_width = tm.tmAveCharWidth + tm.tmOverhang;
1471
1472 gui.char_height = tm.tmHeight + p_linespace;
1473
1474 SelectFont(hdc, hfntOld);
1475
1476 ReleaseDC(hwnd, hdc);
1477}
1478
1479/*
1480 * Adjust gui.char_height (after 'linespace' was changed).
1481 */
1482 int
1483gui_mch_adjust_charheight(void)
1484{
1485 GetFontSize(gui.norm_font);
1486 return OK;
1487}
1488
1489 static GuiFont
1490get_font_handle(LOGFONT *lf)
1491{
1492 HFONT font = NULL;
1493
1494 /* Load the font */
1495 font = CreateFontIndirect(lf);
1496
1497 if (font == NULL)
1498 return NOFONT;
1499
1500 return (GuiFont)font;
1501}
1502
1503 static int
1504pixels_to_points(int pixels, int vertical)
1505{
1506 int points;
1507 HWND hwnd;
1508 HDC hdc;
1509
1510 hwnd = GetDesktopWindow();
1511 hdc = GetWindowDC(hwnd);
1512
1513 points = MulDiv(pixels, 72,
1514 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1515
1516 ReleaseDC(hwnd, hdc);
1517
1518 return points;
1519}
1520
1521 GuiFont
1522gui_mch_get_font(
1523 char_u *name,
1524 int giveErrorIfMissing)
1525{
1526 LOGFONT lf;
1527 GuiFont font = NOFONT;
1528
1529 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1530 font = get_font_handle(&lf);
1531 if (font == NOFONT && giveErrorIfMissing)
1532 EMSG2(_(e_font), name);
1533 return font;
1534}
1535
1536#if defined(FEAT_EVAL) || defined(PROTO)
1537/*
1538 * Return the name of font "font" in allocated memory.
1539 * Don't know how to get the actual name, thus use the provided name.
1540 */
1541/*ARGSUSED*/
1542 char_u *
1543gui_mch_get_fontname(GuiFont font, char_u *name)
1544{
1545 if (name == NULL)
1546 return NULL;
1547 return vim_strsave(name);
1548}
1549#endif
1550
1551 void
1552gui_mch_free_font(GuiFont font)
1553{
1554 if (font)
1555 DeleteObject((HFONT)font);
1556}
1557
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
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001930#ifdef MSWIN_FIND_REPLACE
1931 /* Don't process messages used by the dialog */
1932 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1933 {
1934 HandleMouseHide(msg.message, msg.lParam);
1935 return;
1936 }
1937#endif
1938
1939 /*
1940 * Check if it's a special key that we recognise. If not, call
1941 * TranslateMessage().
1942 */
1943 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1944 {
1945 vk = (int) msg.wParam;
1946
1947 /*
1948 * Handle dead keys in special conditions in other cases we let Windows
1949 * handle them and do not interfere.
1950 *
1951 * The dead_key flag must be reset on several occasions:
1952 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1953 * consumed at that point (This is when we let Windows combine the
1954 * dead character on its own)
1955 *
1956 * - Before doing something special such as regenerating keypresses to
1957 * expel the dead character as this could trigger an infinite loop if
1958 * for some reason MyTranslateMessage() do not trigger a call
1959 * immediately to _OnChar() (or _OnSysChar()).
1960 */
1961 if (dead_key)
1962 {
1963 /*
1964 * If a dead key was pressed and the user presses VK_SPACE,
1965 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1966 * with the dead char now, so do nothing special and let Windows
1967 * handle it.
1968 *
1969 * Note that VK_SPACE combines with the dead_key's character and
1970 * only one WM_CHAR will be generated by TranslateMessage(), in
1971 * the two other cases two WM_CHAR will be generated: the dead
1972 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1973 * user expects.
1974 */
1975 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1976 {
1977 dead_key = 0;
1978 MyTranslateMessage(&msg);
1979 return;
1980 }
1981 /* In modes where we are not typing, dead keys should behave
1982 * normally */
1983 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1984 {
1985 outputDeadKey_rePost(msg);
1986 return;
1987 }
1988 }
1989
1990 /* Check for CTRL-BREAK */
1991 if (vk == VK_CANCEL)
1992 {
1993 trash_input_buf();
1994 got_int = TRUE;
1995 string[0] = Ctrl_C;
1996 add_to_input_buf(string, 1);
1997 }
1998
1999 for (i = 0; special_keys[i].key_sym != 0; i++)
2000 {
2001 /* ignore VK_SPACE when ALT key pressed: system menu */
2002 if (special_keys[i].key_sym == vk
2003 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
2004 {
2005 /*
2006 * Behave as exected if we have a dead key and the special key
2007 * is a key that would normally trigger the dead key nominal
2008 * character output (such as a NUMPAD printable character or
2009 * the TAB key, etc...).
2010 */
2011 if (dead_key && (special_keys[i].vim_code0 == 'K'
2012 || vk == VK_TAB || vk == CAR))
2013 {
2014 outputDeadKey_rePost(msg);
2015 return;
2016 }
2017
2018#ifdef FEAT_MENU
2019 /* Check for <F10>: Windows selects the menu. When <F10> is
2020 * mapped we want to use the mapping instead. */
2021 if (vk == VK_F10
2022 && gui.menu_is_active
2023 && check_map(k10, State, FALSE, TRUE, FALSE,
2024 NULL, NULL) == NULL)
2025 break;
2026#endif
2027 if (GetKeyState(VK_SHIFT) & 0x8000)
2028 modifiers |= MOD_MASK_SHIFT;
2029 /*
2030 * Don't use caps-lock as shift, because these are special keys
2031 * being considered here, and we only want letters to get
2032 * shifted -- webb
2033 */
2034 /*
2035 if (GetKeyState(VK_CAPITAL) & 0x0001)
2036 modifiers ^= MOD_MASK_SHIFT;
2037 */
2038 if (GetKeyState(VK_CONTROL) & 0x8000)
2039 modifiers |= MOD_MASK_CTRL;
2040 if (GetKeyState(VK_MENU) & 0x8000)
2041 modifiers |= MOD_MASK_ALT;
2042
2043 if (special_keys[i].vim_code1 == NUL)
2044 key = special_keys[i].vim_code0;
2045 else
2046 key = TO_SPECIAL(special_keys[i].vim_code0,
2047 special_keys[i].vim_code1);
2048 key = simplify_key(key, &modifiers);
2049 if (key == CSI)
2050 key = K_CSI;
2051
2052 if (modifiers)
2053 {
2054 string[0] = CSI;
2055 string[1] = KS_MODIFIER;
2056 string[2] = modifiers;
2057 add_to_input_buf(string, 3);
2058 }
2059
2060 if (IS_SPECIAL(key))
2061 {
2062 string[0] = CSI;
2063 string[1] = K_SECOND(key);
2064 string[2] = K_THIRD(key);
2065 add_to_input_buf(string, 3);
2066 }
2067 else
2068 {
2069 int len;
2070
2071 /* Handle "key" as a Unicode character. */
2072 len = char_to_string(key, string, 40, FALSE);
2073 add_to_input_buf(string, len);
2074 }
2075 break;
2076 }
2077 }
2078 if (special_keys[i].key_sym == 0)
2079 {
2080 /* Some keys need C-S- where they should only need C-.
2081 * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
2082 * system startup (Helmut Stiegler, 2003 Oct 3). */
2083 if (vk != 0xff
2084 && (GetKeyState(VK_CONTROL) & 0x8000)
2085 && !(GetKeyState(VK_SHIFT) & 0x8000)
2086 && !(GetKeyState(VK_MENU) & 0x8000))
2087 {
2088 /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */
2089 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
2090 {
2091 string[0] = Ctrl_HAT;
2092 add_to_input_buf(string, 1);
2093 }
2094 /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */
2095 else if (vk == 0xBD) /* QWERTY for CTRL-'-' */
2096 {
2097 string[0] = Ctrl__;
2098 add_to_input_buf(string, 1);
2099 }
2100 /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */
2101 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2102 {
2103 string[0] = Ctrl_AT;
2104 add_to_input_buf(string, 1);
2105 }
2106 else
2107 MyTranslateMessage(&msg);
2108 }
2109 else
2110 MyTranslateMessage(&msg);
2111 }
2112 }
2113#ifdef FEAT_MBYTE_IME
2114 else if (msg.message == WM_IME_NOTIFY)
2115 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2116 else if (msg.message == WM_KEYUP && im_get_status())
2117 /* added for non-MS IME (Yasuhiro Matsumoto) */
2118 MyTranslateMessage(&msg);
2119#endif
2120#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
2121/* GIME_TEST */
2122 else if (msg.message == WM_IME_STARTCOMPOSITION)
2123 {
2124 POINT point;
2125
2126 global_ime_set_font(&norm_logfont);
2127 point.x = FILL_X(gui.col);
2128 point.y = FILL_Y(gui.row);
2129 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
2130 global_ime_set_position(&point);
2131 }
2132#endif
2133
2134#ifdef FEAT_MENU
2135 /* Check for <F10>: Default effect is to select the menu. When <F10> is
2136 * mapped we need to stop it here to avoid strange effects (e.g., for the
2137 * key-up event) */
2138 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2139 NULL, NULL) == NULL)
2140#endif
2141 pDispatchMessage(&msg);
2142}
2143
2144/*
2145 * Catch up with any queued events. This may put keyboard input into the
2146 * input buffer, call resize call-backs, trigger timers etc. If there is
2147 * nothing in the event queue (& no timers pending), then we return
2148 * immediately.
2149 */
2150 void
2151gui_mch_update(void)
2152{
2153 MSG msg;
2154
2155 if (!s_busy_processing)
2156 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2157 && !vim_is_input_buf_full())
2158 process_message();
2159}
2160
2161/*
2162 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2163 * from the keyboard.
2164 * wtime == -1 Wait forever.
2165 * wtime == 0 This should never happen.
2166 * wtime > 0 Wait wtime milliseconds for a character.
2167 * Returns OK if a character was found to be available within the given time,
2168 * or FAIL otherwise.
2169 */
2170 int
2171gui_mch_wait_for_chars(int wtime)
2172{
2173 MSG msg;
2174 int focus;
2175
2176 s_timed_out = FALSE;
2177
2178 if (wtime > 0)
2179 {
2180 /* Don't do anything while processing a (scroll) message. */
2181 if (s_busy_processing)
2182 return FAIL;
2183 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)wtime,
2184 (TIMERPROC)_OnTimer);
2185 }
2186
2187 allow_scrollbar = TRUE;
2188
2189 focus = gui.in_focus;
2190 while (!s_timed_out)
2191 {
2192 /* Stop or start blinking when focus changes */
2193 if (gui.in_focus != focus)
2194 {
2195 if (gui.in_focus)
2196 gui_mch_start_blink();
2197 else
2198 gui_mch_stop_blink();
2199 focus = gui.in_focus;
2200 }
2201
2202 if (s_need_activate)
2203 {
2204#ifdef WIN32
2205 (void)SetForegroundWindow(s_hwnd);
2206#else
2207 (void)SetActiveWindow(s_hwnd);
2208#endif
2209 s_need_activate = FALSE;
2210 }
2211
2212#ifdef MESSAGE_QUEUE
Bram Moolenaar9186a272016-02-23 19:34:01 +01002213 /* Check channel while waiting message. */
2214 for (;;)
2215 {
2216 MSG msg;
2217
2218 parse_queued_messages();
2219
2220 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarf28d8712016-04-02 15:59:40 +02002221 || MsgWaitForMultipleObjects(0, NULL, FALSE, 100, QS_ALLINPUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002222 != WAIT_TIMEOUT)
2223 break;
2224 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002225#endif
2226
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002227 /*
2228 * Don't use gui_mch_update() because then we will spin-lock until a
2229 * char arrives, instead we use GetMessage() to hang until an
2230 * event arrives. No need to check for input_buf_full because we are
2231 * returning as soon as it contains a single char -- webb
2232 */
2233 process_message();
2234
2235 if (input_available())
2236 {
2237 if (s_wait_timer != 0 && !s_timed_out)
2238 {
2239 KillTimer(NULL, s_wait_timer);
2240
2241 /* Eat spurious WM_TIMER messages */
2242 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2243 ;
2244 s_wait_timer = 0;
2245 }
2246 allow_scrollbar = FALSE;
2247
2248 /* Clear pending mouse button, the release event may have been
2249 * taken by the dialog window. But don't do this when getting
2250 * focus, we need the mouse-up event then. */
2251 if (!s_getting_focus)
2252 s_button_pending = -1;
2253
2254 return OK;
2255 }
2256 }
2257 allow_scrollbar = FALSE;
2258 return FAIL;
2259}
2260
2261/*
2262 * Clear a rectangular region of the screen from text pos (row1, col1) to
2263 * (row2, col2) inclusive.
2264 */
2265 void
2266gui_mch_clear_block(
2267 int row1,
2268 int col1,
2269 int row2,
2270 int col2)
2271{
2272 RECT rc;
2273
2274 /*
2275 * Clear one extra pixel at the far right, for when bold characters have
2276 * spilled over to the window border.
2277 * Note: FillRect() excludes right and bottom of rectangle.
2278 */
2279 rc.left = FILL_X(col1);
2280 rc.top = FILL_Y(row1);
2281 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2282 rc.bottom = FILL_Y(row2 + 1);
2283 clear_rect(&rc);
2284}
2285
2286/*
2287 * Clear the whole text window.
2288 */
2289 void
2290gui_mch_clear_all(void)
2291{
2292 RECT rc;
2293
2294 rc.left = 0;
2295 rc.top = 0;
2296 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2297 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2298 clear_rect(&rc);
2299}
2300/*
2301 * Menu stuff.
2302 */
2303
2304 void
2305gui_mch_enable_menu(int flag)
2306{
2307#ifdef FEAT_MENU
2308 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2309#endif
2310}
2311
2312/*ARGSUSED*/
2313 void
2314gui_mch_set_menu_pos(
2315 int x,
2316 int y,
2317 int w,
2318 int h)
2319{
2320 /* It will be in the right place anyway */
2321}
2322
2323#if defined(FEAT_MENU) || defined(PROTO)
2324/*
2325 * Make menu item hidden or not hidden
2326 */
2327 void
2328gui_mch_menu_hidden(
2329 vimmenu_T *menu,
2330 int hidden)
2331{
2332 /*
2333 * This doesn't do what we want. Hmm, just grey the menu items for now.
2334 */
2335 /*
2336 if (hidden)
2337 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2338 else
2339 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2340 */
2341 gui_mch_menu_grey(menu, hidden);
2342}
2343
2344/*
2345 * This is called after setting all the menus to grey/hidden or not.
2346 */
2347 void
2348gui_mch_draw_menubar(void)
2349{
2350 DrawMenuBar(s_hwnd);
2351}
2352#endif /*FEAT_MENU*/
2353
2354#ifndef PROTO
2355void
2356#ifdef VIMDLL
2357_export
2358#endif
2359_cdecl
2360SaveInst(HINSTANCE hInst)
2361{
2362 s_hinst = hInst;
2363}
2364#endif
2365
2366/*
2367 * Return the RGB value of a pixel as a long.
2368 */
2369 long_u
2370gui_mch_get_rgb(guicolor_T pixel)
2371{
2372 return (GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2373 + GetBValue(pixel);
2374}
2375
2376#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2377/* Convert pixels in X to dialog units */
2378 static WORD
2379PixelToDialogX(int numPixels)
2380{
2381 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2382}
2383
2384/* Convert pixels in Y to dialog units */
2385 static WORD
2386PixelToDialogY(int numPixels)
2387{
2388 return (WORD)((numPixels * 8) / s_dlgfntheight);
2389}
2390
2391/* Return the width in pixels of the given text in the given DC. */
2392 static int
2393GetTextWidth(HDC hdc, char_u *str, int len)
2394{
2395 SIZE size;
2396
2397 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2398 return size.cx;
2399}
2400
2401#ifdef FEAT_MBYTE
2402/*
2403 * Return the width in pixels of the given text in the given DC, taking care
2404 * of 'encoding' to active codepage conversion.
2405 */
2406 static int
2407GetTextWidthEnc(HDC hdc, char_u *str, int len)
2408{
2409 SIZE size;
2410 WCHAR *wstr;
2411 int n;
2412 int wlen = len;
2413
2414 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2415 {
2416 /* 'encoding' differs from active codepage: convert text and use wide
2417 * function */
2418 wstr = enc_to_utf16(str, &wlen);
2419 if (wstr != NULL)
2420 {
2421 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2422 vim_free(wstr);
2423 if (n)
2424 return size.cx;
2425 }
2426 }
2427
2428 return GetTextWidth(hdc, str, len);
2429}
2430#else
2431# define GetTextWidthEnc(h, s, l) GetTextWidth((h), (s), (l))
2432#endif
2433
2434/*
2435 * A quick little routine that will center one window over another, handy for
2436 * dialog boxes. Taken from the Win32SDK samples.
2437 */
2438 static BOOL
2439CenterWindow(
2440 HWND hwndChild,
2441 HWND hwndParent)
2442{
2443 RECT rChild, rParent;
2444 int wChild, hChild, wParent, hParent;
2445 int wScreen, hScreen, xNew, yNew;
2446 HDC hdc;
2447
2448 GetWindowRect(hwndChild, &rChild);
2449 wChild = rChild.right - rChild.left;
2450 hChild = rChild.bottom - rChild.top;
2451
2452 /* If Vim is minimized put the window in the middle of the screen. */
2453 if (hwndParent == NULL || IsMinimized(hwndParent))
2454 SystemParametersInfo(SPI_GETWORKAREA, 0, &rParent, 0);
2455 else
2456 GetWindowRect(hwndParent, &rParent);
2457 wParent = rParent.right - rParent.left;
2458 hParent = rParent.bottom - rParent.top;
2459
2460 hdc = GetDC(hwndChild);
2461 wScreen = GetDeviceCaps (hdc, HORZRES);
2462 hScreen = GetDeviceCaps (hdc, VERTRES);
2463 ReleaseDC(hwndChild, hdc);
2464
2465 xNew = rParent.left + ((wParent - wChild) /2);
2466 if (xNew < 0)
2467 {
2468 xNew = 0;
2469 }
2470 else if ((xNew+wChild) > wScreen)
2471 {
2472 xNew = wScreen - wChild;
2473 }
2474
2475 yNew = rParent.top + ((hParent - hChild) /2);
2476 if (yNew < 0)
2477 yNew = 0;
2478 else if ((yNew+hChild) > hScreen)
2479 yNew = hScreen - hChild;
2480
2481 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2482 SWP_NOSIZE | SWP_NOZORDER);
2483}
2484#endif /* FEAT_GUI_DIALOG */
2485
2486void
2487gui_mch_activate_window(void)
2488{
2489 (void)SetActiveWindow(s_hwnd);
2490}
2491
2492#if defined(FEAT_TOOLBAR) || defined(PROTO)
2493 void
2494gui_mch_show_toolbar(int showit)
2495{
2496 if (s_toolbarhwnd == NULL)
2497 return;
2498
2499 if (showit)
2500 {
2501# ifdef FEAT_MBYTE
2502# ifndef TB_SETUNICODEFORMAT
2503 /* For older compilers. We assume this never changes. */
2504# define TB_SETUNICODEFORMAT 0x2005
2505# endif
2506 /* Enable/disable unicode support */
2507 int uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2508 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2509# endif
2510 ShowWindow(s_toolbarhwnd, SW_SHOW);
2511 }
2512 else
2513 ShowWindow(s_toolbarhwnd, SW_HIDE);
2514}
2515
2516/* Then number of bitmaps is fixed. Exit is missing! */
2517#define TOOLBAR_BITMAP_COUNT 31
2518
2519#endif
2520
2521#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2522 static void
2523add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2524{
2525#ifdef FEAT_MBYTE
2526 WCHAR *wn = NULL;
2527 int n;
2528
2529 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2530 {
2531 /* 'encoding' differs from active codepage: convert menu name
2532 * and use wide function */
2533 wn = enc_to_utf16(item_text, NULL);
2534 if (wn != NULL)
2535 {
2536 MENUITEMINFOW infow;
2537
2538 infow.cbSize = sizeof(infow);
2539 infow.fMask = MIIM_TYPE | MIIM_ID;
2540 infow.wID = item_id;
2541 infow.fType = MFT_STRING;
2542 infow.dwTypeData = wn;
2543 infow.cch = (UINT)wcslen(wn);
2544 n = InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2545 vim_free(wn);
2546 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2547 /* Failed, try using non-wide function. */
2548 wn = NULL;
2549 }
2550 }
2551
2552 if (wn == NULL)
2553#endif
2554 {
2555 MENUITEMINFO info;
2556
2557 info.cbSize = sizeof(info);
2558 info.fMask = MIIM_TYPE | MIIM_ID;
2559 info.wID = item_id;
2560 info.fType = MFT_STRING;
2561 info.dwTypeData = (LPTSTR)item_text;
2562 info.cch = (UINT)STRLEN(item_text);
2563 InsertMenuItem(pmenu, item_id, FALSE, &info);
2564 }
2565}
2566
2567 static void
2568show_tabline_popup_menu(void)
2569{
2570 HMENU tab_pmenu;
2571 long rval;
2572 POINT pt;
2573
2574 /* When ignoring events don't show the menu. */
2575 if (hold_gui_events
2576# ifdef FEAT_CMDWIN
2577 || cmdwin_type != 0
2578# endif
2579 )
2580 return;
2581
2582 tab_pmenu = CreatePopupMenu();
2583 if (tab_pmenu == NULL)
2584 return;
2585
2586 if (first_tabpage->tp_next != NULL)
2587 add_tabline_popup_menu_entry(tab_pmenu,
2588 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2589 add_tabline_popup_menu_entry(tab_pmenu,
2590 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2591 add_tabline_popup_menu_entry(tab_pmenu,
2592 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2593
2594 GetCursorPos(&pt);
2595 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2596 NULL);
2597
2598 DestroyMenu(tab_pmenu);
2599
2600 /* Add the string cmd into input buffer */
2601 if (rval > 0)
2602 {
2603 TCHITTESTINFO htinfo;
2604 int idx;
2605
2606 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2607 return;
2608
2609 htinfo.pt.x = pt.x;
2610 htinfo.pt.y = pt.y;
2611 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2612 if (idx == -1)
2613 idx = 0;
2614 else
2615 idx += 1;
2616
2617 send_tabline_menu_event(idx, (int)rval);
2618 }
2619}
2620
2621/*
2622 * Show or hide the tabline.
2623 */
2624 void
2625gui_mch_show_tabline(int showit)
2626{
2627 if (s_tabhwnd == NULL)
2628 return;
2629
2630 if (!showit != !showing_tabline)
2631 {
2632 if (showit)
2633 ShowWindow(s_tabhwnd, SW_SHOW);
2634 else
2635 ShowWindow(s_tabhwnd, SW_HIDE);
2636 showing_tabline = showit;
2637 }
2638}
2639
2640/*
2641 * Return TRUE when tabline is displayed.
2642 */
2643 int
2644gui_mch_showing_tabline(void)
2645{
2646 return s_tabhwnd != NULL && showing_tabline;
2647}
2648
2649/*
2650 * Update the labels of the tabline.
2651 */
2652 void
2653gui_mch_update_tabline(void)
2654{
2655 tabpage_T *tp;
2656 TCITEM tie;
2657 int nr = 0;
2658 int curtabidx = 0;
2659 int tabadded = 0;
2660#ifdef FEAT_MBYTE
2661 static int use_unicode = FALSE;
2662 int uu;
2663 WCHAR *wstr = NULL;
2664#endif
2665
2666 if (s_tabhwnd == NULL)
2667 return;
2668
2669#if defined(FEAT_MBYTE)
2670# ifndef CCM_SETUNICODEFORMAT
2671 /* For older compilers. We assume this never changes. */
2672# define CCM_SETUNICODEFORMAT 0x2005
2673# endif
2674 uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2675 if (uu != use_unicode)
2676 {
2677 /* Enable/disable unicode support */
2678 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2679 use_unicode = uu;
2680 }
2681#endif
2682
2683 tie.mask = TCIF_TEXT;
2684 tie.iImage = -1;
2685
2686 /* Disable redraw for tab updates to eliminate O(N^2) draws. */
2687 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2688
2689 /* Add a label for each tab page. They all contain the same text area. */
2690 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2691 {
2692 if (tp == curtab)
2693 curtabidx = nr;
2694
2695 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2696 {
2697 /* Add the tab */
2698 tie.pszText = "-Empty-";
2699 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2700 tabadded = 1;
2701 }
2702
2703 get_tabline_label(tp, FALSE);
2704 tie.pszText = (LPSTR)NameBuff;
2705#ifdef FEAT_MBYTE
2706 wstr = NULL;
2707 if (use_unicode)
2708 {
2709 /* Need to go through Unicode. */
2710 wstr = enc_to_utf16(NameBuff, NULL);
2711 if (wstr != NULL)
2712 {
2713 TCITEMW tiw;
2714
2715 tiw.mask = TCIF_TEXT;
2716 tiw.iImage = -1;
2717 tiw.pszText = wstr;
2718 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2719 vim_free(wstr);
2720 }
2721 }
2722 if (wstr == NULL)
2723#endif
2724 {
2725 TabCtrl_SetItem(s_tabhwnd, nr, &tie);
2726 }
2727 }
2728
2729 /* Remove any old labels. */
2730 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2731 TabCtrl_DeleteItem(s_tabhwnd, nr);
2732
2733 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2734 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2735
2736 /* Re-enable redraw and redraw. */
2737 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2738 RedrawWindow(s_tabhwnd, NULL, NULL,
2739 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2740
2741 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2742 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2743}
2744
2745/*
2746 * Set the current tab to "nr". First tab is 1.
2747 */
2748 void
2749gui_mch_set_curtab(int nr)
2750{
2751 if (s_tabhwnd == NULL)
2752 return;
2753
2754 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2755 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2756}
2757
2758#endif
2759
2760/*
2761 * ":simalt" command.
2762 */
2763 void
2764ex_simalt(exarg_T *eap)
2765{
2766 char_u *keys = eap->arg;
2767
2768 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2769 while (*keys)
2770 {
2771 if (*keys == '~')
2772 *keys = ' '; /* for showing system menu */
2773 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2774 keys++;
2775 }
2776}
2777
2778/*
2779 * Create the find & replace dialogs.
2780 * You can't have both at once: ":find" when replace is showing, destroys
2781 * the replace dialog first, and the other way around.
2782 */
2783#ifdef MSWIN_FIND_REPLACE
2784 static void
2785initialise_findrep(char_u *initial_string)
2786{
2787 int wword = FALSE;
2788 int mcase = !p_ic;
2789 char_u *entry_text;
2790
2791 /* Get the search string to use. */
2792 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2793
2794 s_findrep_struct.hwndOwner = s_hwnd;
2795 s_findrep_struct.Flags = FR_DOWN;
2796 if (mcase)
2797 s_findrep_struct.Flags |= FR_MATCHCASE;
2798 if (wword)
2799 s_findrep_struct.Flags |= FR_WHOLEWORD;
2800 if (entry_text != NULL && *entry_text != NUL)
2801 vim_strncpy((char_u *)s_findrep_struct.lpstrFindWhat, entry_text,
2802 s_findrep_struct.wFindWhatLen - 1);
2803 vim_free(entry_text);
2804}
2805#endif
2806
2807 static void
2808set_window_title(HWND hwnd, char *title)
2809{
2810#ifdef FEAT_MBYTE
2811 if (title != NULL && enc_codepage >= 0 && enc_codepage != (int)GetACP())
2812 {
2813 WCHAR *wbuf;
2814 int n;
2815
2816 /* Convert the title from 'encoding' to UTF-16. */
2817 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2818 if (wbuf != NULL)
2819 {
2820 n = SetWindowTextW(hwnd, wbuf);
2821 vim_free(wbuf);
2822 if (n != 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
2823 return;
2824 /* Retry with non-wide function (for Windows 98). */
2825 }
2826 }
2827#endif
2828 (void)SetWindowText(hwnd, (LPCSTR)title);
2829}
2830
2831 void
2832gui_mch_find_dialog(exarg_T *eap)
2833{
2834#ifdef MSWIN_FIND_REPLACE
2835 if (s_findrep_msg != 0)
2836 {
2837 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2838 DestroyWindow(s_findrep_hwnd);
2839
2840 if (!IsWindow(s_findrep_hwnd))
2841 {
2842 initialise_findrep(eap->arg);
2843# if defined(FEAT_MBYTE) && defined(WIN3264)
2844 /* If the OS is Windows NT, and 'encoding' differs from active
2845 * codepage: convert text and use wide function. */
2846 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
2847 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2848 {
2849 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2850 s_findrep_hwnd = FindTextW(
2851 (LPFINDREPLACEW) &s_findrep_struct_w);
2852 }
2853 else
2854# endif
2855 s_findrep_hwnd = FindText((LPFINDREPLACE) &s_findrep_struct);
2856 }
2857
2858 set_window_title(s_findrep_hwnd,
2859 _("Find string (use '\\\\' to find a '\\')"));
2860 (void)SetFocus(s_findrep_hwnd);
2861
2862 s_findrep_is_find = TRUE;
2863 }
2864#endif
2865}
2866
2867
2868 void
2869gui_mch_replace_dialog(exarg_T *eap)
2870{
2871#ifdef MSWIN_FIND_REPLACE
2872 if (s_findrep_msg != 0)
2873 {
2874 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2875 DestroyWindow(s_findrep_hwnd);
2876
2877 if (!IsWindow(s_findrep_hwnd))
2878 {
2879 initialise_findrep(eap->arg);
2880# if defined(FEAT_MBYTE) && defined(WIN3264)
2881 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
2882 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2883 {
2884 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2885 s_findrep_hwnd = ReplaceTextW(
2886 (LPFINDREPLACEW) &s_findrep_struct_w);
2887 }
2888 else
2889# endif
2890 s_findrep_hwnd = ReplaceText(
2891 (LPFINDREPLACE) &s_findrep_struct);
2892 }
2893
2894 set_window_title(s_findrep_hwnd,
2895 _("Find & Replace (use '\\\\' to find a '\\')"));
2896 (void)SetFocus(s_findrep_hwnd);
2897
2898 s_findrep_is_find = FALSE;
2899 }
2900#endif
2901}
2902
2903
2904/*
2905 * Set visibility of the pointer.
2906 */
2907 void
2908gui_mch_mousehide(int hide)
2909{
2910 if (hide != gui.pointer_hidden)
2911 {
2912 ShowCursor(!hide);
2913 gui.pointer_hidden = hide;
2914 }
2915}
2916
2917#ifdef FEAT_MENU
2918 static void
2919gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2920{
2921 /* Unhide the mouse, we don't get move events here. */
2922 gui_mch_mousehide(FALSE);
2923
2924 (void)TrackPopupMenu(
2925 (HMENU)menu->submenu_id,
2926 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2927 x, y,
2928 (int)0, /*reserved param*/
2929 s_hwnd,
2930 NULL);
2931 /*
2932 * NOTE: The pop-up menu can eat the mouse up event.
2933 * We deal with this in normal.c.
2934 */
2935}
2936#endif
2937
2938/*
2939 * Got a message when the system will go down.
2940 */
2941 static void
2942_OnEndSession(void)
2943{
2944 getout_preserve_modified(1);
2945}
2946
2947/*
2948 * Get this message when the user clicks on the cross in the top right corner
2949 * of a Windows95 window.
2950 */
2951/*ARGSUSED*/
2952 static void
2953_OnClose(
2954 HWND hwnd)
2955{
2956 gui_shell_closed();
2957}
2958
2959/*
2960 * Get a message when the window is being destroyed.
2961 */
2962 static void
2963_OnDestroy(
2964 HWND hwnd)
2965{
2966 if (!destroying)
2967 _OnClose(hwnd);
2968}
2969
2970 static void
2971_OnPaint(
2972 HWND hwnd)
2973{
2974 if (!IsMinimized(hwnd))
2975 {
2976 PAINTSTRUCT ps;
2977
2978 out_flush(); /* make sure all output has been processed */
2979 (void)BeginPaint(hwnd, &ps);
2980#if defined(FEAT_DIRECTX)
2981 if (IS_ENABLE_DIRECTX())
2982 DWriteContext_BeginDraw(s_dwc);
2983#endif
2984
2985#ifdef FEAT_MBYTE
2986 /* prevent multi-byte characters from misprinting on an invalid
2987 * rectangle */
2988 if (has_mbyte)
2989 {
2990 RECT rect;
2991
2992 GetClientRect(hwnd, &rect);
2993 ps.rcPaint.left = rect.left;
2994 ps.rcPaint.right = rect.right;
2995 }
2996#endif
2997
2998 if (!IsRectEmpty(&ps.rcPaint))
2999 {
3000#if defined(FEAT_DIRECTX)
3001 if (IS_ENABLE_DIRECTX())
3002 DWriteContext_BindDC(s_dwc, s_hdc, &ps.rcPaint);
3003#endif
3004 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
3005 ps.rcPaint.right - ps.rcPaint.left + 1,
3006 ps.rcPaint.bottom - ps.rcPaint.top + 1);
3007 }
3008
3009#if defined(FEAT_DIRECTX)
3010 if (IS_ENABLE_DIRECTX())
3011 DWriteContext_EndDraw(s_dwc);
3012#endif
3013 EndPaint(hwnd, &ps);
3014 }
3015}
3016
3017/*ARGSUSED*/
3018 static void
3019_OnSize(
3020 HWND hwnd,
3021 UINT state,
3022 int cx,
3023 int cy)
3024{
3025 if (!IsMinimized(hwnd))
3026 {
3027 gui_resize_shell(cx, cy);
3028
3029#ifdef FEAT_MENU
3030 /* Menu bar may wrap differently now */
3031 gui_mswin_get_menu_height(TRUE);
3032#endif
3033 }
3034}
3035
3036 static void
3037_OnSetFocus(
3038 HWND hwnd,
3039 HWND hwndOldFocus)
3040{
3041 gui_focus_change(TRUE);
3042 s_getting_focus = TRUE;
3043 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
3044}
3045
3046 static void
3047_OnKillFocus(
3048 HWND hwnd,
3049 HWND hwndNewFocus)
3050{
3051 gui_focus_change(FALSE);
3052 s_getting_focus = FALSE;
3053 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
3054}
3055
3056/*
3057 * Get a message when the user switches back to vim
3058 */
3059 static LRESULT
3060_OnActivateApp(
3061 HWND hwnd,
3062 BOOL fActivate,
3063 DWORD dwThreadId)
3064{
3065 /* we call gui_focus_change() in _OnSetFocus() */
3066 /* gui_focus_change((int)fActivate); */
3067 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
3068}
3069
3070#if defined(FEAT_WINDOWS) || defined(PROTO)
3071 void
3072gui_mch_destroy_scrollbar(scrollbar_T *sb)
3073{
3074 DestroyWindow(sb->id);
3075}
3076#endif
3077
3078/*
3079 * Get current mouse coordinates in text window.
3080 */
3081 void
3082gui_mch_getmouse(int *x, int *y)
3083{
3084 RECT rct;
3085 POINT mp;
3086
3087 (void)GetWindowRect(s_textArea, &rct);
3088 (void)GetCursorPos((LPPOINT)&mp);
3089 *x = (int)(mp.x - rct.left);
3090 *y = (int)(mp.y - rct.top);
3091}
3092
3093/*
3094 * Move mouse pointer to character at (x, y).
3095 */
3096 void
3097gui_mch_setmouse(int x, int y)
3098{
3099 RECT rct;
3100
3101 (void)GetWindowRect(s_textArea, &rct);
3102 (void)SetCursorPos(x + gui.border_offset + rct.left,
3103 y + gui.border_offset + rct.top);
3104}
3105
3106 static void
3107gui_mswin_get_valid_dimensions(
3108 int w,
3109 int h,
3110 int *valid_w,
3111 int *valid_h)
3112{
3113 int base_width, base_height;
3114
3115 base_width = gui_get_base_width()
3116 + (GetSystemMetrics(SM_CXFRAME) +
3117 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
3118 base_height = gui_get_base_height()
3119 + (GetSystemMetrics(SM_CYFRAME) +
3120 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3121 + GetSystemMetrics(SM_CYCAPTION)
3122#ifdef FEAT_MENU
3123 + gui_mswin_get_menu_height(FALSE)
3124#endif
3125 ;
3126 *valid_w = base_width +
3127 ((w - base_width) / gui.char_width) * gui.char_width;
3128 *valid_h = base_height +
3129 ((h - base_height) / gui.char_height) * gui.char_height;
3130}
3131
3132 void
3133gui_mch_flash(int msec)
3134{
3135 RECT rc;
3136
3137 /*
3138 * Note: InvertRect() excludes right and bottom of rectangle.
3139 */
3140 rc.left = 0;
3141 rc.top = 0;
3142 rc.right = gui.num_cols * gui.char_width;
3143 rc.bottom = gui.num_rows * gui.char_height;
3144 InvertRect(s_hdc, &rc);
3145 gui_mch_flush(); /* make sure it's displayed */
3146
3147 ui_delay((long)msec, TRUE); /* wait for a few msec */
3148
3149 InvertRect(s_hdc, &rc);
3150}
3151
3152/*
3153 * Return flags used for scrolling.
3154 * The SW_INVALIDATE is required when part of the window is covered or
3155 * off-screen. Refer to MS KB Q75236.
3156 */
3157 static int
3158get_scroll_flags(void)
3159{
3160 HWND hwnd;
3161 RECT rcVim, rcOther, rcDest;
3162
3163 GetWindowRect(s_hwnd, &rcVim);
3164
3165 /* Check if the window is partly above or below the screen. We don't care
3166 * about partly left or right of the screen, it is not relevant when
3167 * scrolling up or down. */
3168 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
3169 return SW_INVALIDATE;
3170
3171 /* Check if there is an window (partly) on top of us. */
3172 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3173 if (IsWindowVisible(hwnd))
3174 {
3175 GetWindowRect(hwnd, &rcOther);
3176 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3177 return SW_INVALIDATE;
3178 }
3179 return 0;
3180}
3181
3182/*
3183 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3184 * may not be scrolled out properly.
3185 * For gVim, when _OnScroll() is repeated, the character at the
3186 * previous cursor position may be left drawn after scroll.
3187 * The problem can be avoided by calling GetPixel() to get a pixel in
3188 * the region before ScrollWindowEx().
3189 */
3190 static void
3191intel_gpu_workaround(void)
3192{
3193 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3194}
3195
3196/*
3197 * Delete the given number of lines from the given row, scrolling up any
3198 * text further down within the scroll region.
3199 */
3200 void
3201gui_mch_delete_lines(
3202 int row,
3203 int num_lines)
3204{
3205 RECT rc;
3206
3207 intel_gpu_workaround();
3208
3209 rc.left = FILL_X(gui.scroll_region_left);
3210 rc.right = FILL_X(gui.scroll_region_right + 1);
3211 rc.top = FILL_Y(row);
3212 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3213
3214 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
3215 &rc, &rc, NULL, NULL, get_scroll_flags());
3216
3217 UpdateWindow(s_textArea);
3218 /* This seems to be required to avoid the cursor disappearing when
3219 * scrolling such that the cursor ends up in the top-left character on
3220 * the screen... But why? (Webb) */
3221 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3222 /* gui.cursor_is_valid = FALSE; */
3223
3224 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3225 gui.scroll_region_left,
3226 gui.scroll_region_bot, gui.scroll_region_right);
3227}
3228
3229/*
3230 * Insert the given number of lines before the given row, scrolling down any
3231 * following text within the scroll region.
3232 */
3233 void
3234gui_mch_insert_lines(
3235 int row,
3236 int num_lines)
3237{
3238 RECT rc;
3239
3240 intel_gpu_workaround();
3241
3242 rc.left = FILL_X(gui.scroll_region_left);
3243 rc.right = FILL_X(gui.scroll_region_right + 1);
3244 rc.top = FILL_Y(row);
3245 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3246 /* The SW_INVALIDATE is required when part of the window is covered or
3247 * off-screen. How do we avoid it when it's not needed? */
3248 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
3249 &rc, &rc, NULL, NULL, get_scroll_flags());
3250
3251 UpdateWindow(s_textArea);
3252
3253 gui_clear_block(row, gui.scroll_region_left,
3254 row + num_lines - 1, gui.scroll_region_right);
3255}
3256
3257
3258/*ARGSUSED*/
3259 void
3260gui_mch_exit(int rc)
3261{
3262#if defined(FEAT_DIRECTX)
3263 DWriteContext_Close(s_dwc);
3264 DWrite_Final();
3265 s_dwc = NULL;
3266#endif
3267
3268 ReleaseDC(s_textArea, s_hdc);
3269 DeleteObject(s_brush);
3270
3271#ifdef FEAT_TEAROFF
3272 /* Unload the tearoff bitmap */
3273 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3274#endif
3275
3276 /* Destroy our window (if we have one). */
3277 if (s_hwnd != NULL)
3278 {
3279 destroying = TRUE; /* ignore WM_DESTROY message now */
3280 DestroyWindow(s_hwnd);
3281 }
3282
3283#ifdef GLOBAL_IME
3284 global_ime_end();
3285#endif
3286}
3287
3288 static char_u *
3289logfont2name(LOGFONT lf)
3290{
3291 char *p;
3292 char *res;
3293 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003294 char *quality_name;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003295 char *font_name = lf.lfFaceName;
3296
3297 charset_name = charset_id2name((int)lf.lfCharSet);
3298#ifdef FEAT_MBYTE
3299 /* Convert a font name from the current codepage to 'encoding'.
3300 * TODO: Use Wide APIs (including LOGFONTW) instead of ANSI APIs. */
3301 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
3302 {
3303 int len;
3304 acp_to_enc((char_u *)lf.lfFaceName, (int)strlen(lf.lfFaceName),
3305 (char_u **)&font_name, &len);
3306 }
3307#endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003308 quality_name = quality_id2name((int)lf.lfQuality);
3309
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003310 res = (char *)alloc((unsigned)(strlen(font_name) + 20
3311 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
3312 if (res != NULL)
3313 {
3314 p = res;
3315 /* make a normal font string out of the lf thing:*/
3316 sprintf((char *)p, "%s:h%d", font_name, pixels_to_points(
3317 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
3318 while (*p)
3319 {
3320 if (*p == ' ')
3321 *p = '_';
3322 ++p;
3323 }
3324 if (lf.lfItalic)
3325 STRCAT(p, ":i");
3326 if (lf.lfWeight >= FW_BOLD)
3327 STRCAT(p, ":b");
3328 if (lf.lfUnderline)
3329 STRCAT(p, ":u");
3330 if (lf.lfStrikeOut)
3331 STRCAT(p, ":s");
3332 if (charset_name != NULL)
3333 {
3334 STRCAT(p, ":c");
3335 STRCAT(p, charset_name);
3336 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003337 if (quality_name != NULL)
3338 {
3339 STRCAT(p, ":q");
3340 STRCAT(p, quality_name);
3341 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003342 }
3343
3344#ifdef FEAT_MBYTE
3345 if (font_name != lf.lfFaceName)
3346 vim_free(font_name);
3347#endif
3348 return (char_u *)res;
3349}
3350
3351
3352#ifdef FEAT_MBYTE_IME
3353/*
3354 * Set correct LOGFONT to IME. Use 'guifontwide' if available, otherwise use
3355 * 'guifont'
3356 */
3357 static void
3358update_im_font(void)
3359{
3360 LOGFONT lf_wide;
3361
3362 if (p_guifontwide != NULL && *p_guifontwide != NUL
3363 && gui.wide_font != NOFONT
3364 && GetObject((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
3365 norm_logfont = lf_wide;
3366 else
3367 norm_logfont = sub_logfont;
3368 im_set_font(&norm_logfont);
3369}
3370#endif
3371
3372#ifdef FEAT_MBYTE
3373/*
3374 * Handler of gui.wide_font (p_guifontwide) changed notification.
3375 */
3376 void
3377gui_mch_wide_font_changed(void)
3378{
3379 LOGFONT lf;
3380
3381# ifdef FEAT_MBYTE_IME
3382 update_im_font();
3383# endif
3384
3385 gui_mch_free_font(gui.wide_ital_font);
3386 gui.wide_ital_font = NOFONT;
3387 gui_mch_free_font(gui.wide_bold_font);
3388 gui.wide_bold_font = NOFONT;
3389 gui_mch_free_font(gui.wide_boldital_font);
3390 gui.wide_boldital_font = NOFONT;
3391
3392 if (gui.wide_font
3393 && GetObject((HFONT)gui.wide_font, sizeof(lf), &lf))
3394 {
3395 if (!lf.lfItalic)
3396 {
3397 lf.lfItalic = TRUE;
3398 gui.wide_ital_font = get_font_handle(&lf);
3399 lf.lfItalic = FALSE;
3400 }
3401 if (lf.lfWeight < FW_BOLD)
3402 {
3403 lf.lfWeight = FW_BOLD;
3404 gui.wide_bold_font = get_font_handle(&lf);
3405 if (!lf.lfItalic)
3406 {
3407 lf.lfItalic = TRUE;
3408 gui.wide_boldital_font = get_font_handle(&lf);
3409 }
3410 }
3411 }
3412}
3413#endif
3414
3415/*
3416 * Initialise vim to use the font with the given name.
3417 * Return FAIL if the font could not be loaded, OK otherwise.
3418 */
3419/*ARGSUSED*/
3420 int
3421gui_mch_init_font(char_u *font_name, int fontset)
3422{
3423 LOGFONT lf;
3424 GuiFont font = NOFONT;
3425 char_u *p;
3426
3427 /* Load the font */
3428 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3429 font = get_font_handle(&lf);
3430 if (font == NOFONT)
3431 return FAIL;
3432
3433 if (font_name == NULL)
3434 font_name = (char_u *)lf.lfFaceName;
3435#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3436 norm_logfont = lf;
3437 sub_logfont = lf;
3438#endif
3439#ifdef FEAT_MBYTE_IME
3440 update_im_font();
3441#endif
3442 gui_mch_free_font(gui.norm_font);
3443 gui.norm_font = font;
3444 current_font_height = lf.lfHeight;
3445 GetFontSize(font);
3446
3447 p = logfont2name(lf);
3448 if (p != NULL)
3449 {
3450 hl_set_font_name(p);
3451
3452 /* When setting 'guifont' to "*" replace it with the actual font name.
3453 * */
3454 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3455 {
3456 vim_free(p_guifont);
3457 p_guifont = p;
3458 }
3459 else
3460 vim_free(p);
3461 }
3462
3463 gui_mch_free_font(gui.ital_font);
3464 gui.ital_font = NOFONT;
3465 gui_mch_free_font(gui.bold_font);
3466 gui.bold_font = NOFONT;
3467 gui_mch_free_font(gui.boldital_font);
3468 gui.boldital_font = NOFONT;
3469
3470 if (!lf.lfItalic)
3471 {
3472 lf.lfItalic = TRUE;
3473 gui.ital_font = get_font_handle(&lf);
3474 lf.lfItalic = FALSE;
3475 }
3476 if (lf.lfWeight < FW_BOLD)
3477 {
3478 lf.lfWeight = FW_BOLD;
3479 gui.bold_font = get_font_handle(&lf);
3480 if (!lf.lfItalic)
3481 {
3482 lf.lfItalic = TRUE;
3483 gui.boldital_font = get_font_handle(&lf);
3484 }
3485 }
3486
3487 return OK;
3488}
3489
3490#ifndef WPF_RESTORETOMAXIMIZED
3491# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3492#endif
3493
3494/*
3495 * Return TRUE if the GUI window is maximized, filling the whole screen.
3496 */
3497 int
3498gui_mch_maximized(void)
3499{
3500 WINDOWPLACEMENT wp;
3501
3502 wp.length = sizeof(WINDOWPLACEMENT);
3503 if (GetWindowPlacement(s_hwnd, &wp))
3504 return wp.showCmd == SW_SHOWMAXIMIZED
3505 || (wp.showCmd == SW_SHOWMINIMIZED
3506 && wp.flags == WPF_RESTORETOMAXIMIZED);
3507
3508 return 0;
3509}
3510
3511/*
3512 * Called when the font changed while the window is maximized. Compute the
3513 * new Rows and Columns. This is like resizing the window.
3514 */
3515 void
3516gui_mch_newfont(void)
3517{
3518 RECT rect;
3519
3520 GetWindowRect(s_hwnd, &rect);
3521 if (win_socket_id == 0)
3522 {
3523 gui_resize_shell(rect.right - rect.left
3524 - (GetSystemMetrics(SM_CXFRAME) +
3525 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3526 rect.bottom - rect.top
3527 - (GetSystemMetrics(SM_CYFRAME) +
3528 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3529 - GetSystemMetrics(SM_CYCAPTION)
3530#ifdef FEAT_MENU
3531 - gui_mswin_get_menu_height(FALSE)
3532#endif
3533 );
3534 }
3535 else
3536 {
3537 /* Inside another window, don't use the frame and border. */
3538 gui_resize_shell(rect.right - rect.left,
3539 rect.bottom - rect.top
3540#ifdef FEAT_MENU
3541 - gui_mswin_get_menu_height(FALSE)
3542#endif
3543 );
3544 }
3545}
3546
3547/*
3548 * Set the window title
3549 */
3550/*ARGSUSED*/
3551 void
3552gui_mch_settitle(
3553 char_u *title,
3554 char_u *icon)
3555{
3556 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3557}
3558
3559#ifdef FEAT_MOUSESHAPE
3560/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3561 * misc2.c! */
3562static LPCSTR mshape_idcs[] =
3563{
3564 IDC_ARROW, /* arrow */
3565 MAKEINTRESOURCE(0), /* blank */
3566 IDC_IBEAM, /* beam */
3567 IDC_SIZENS, /* updown */
3568 IDC_SIZENS, /* udsizing */
3569 IDC_SIZEWE, /* leftright */
3570 IDC_SIZEWE, /* lrsizing */
3571 IDC_WAIT, /* busy */
3572#ifdef WIN3264
3573 IDC_NO, /* no */
3574#else
3575 IDC_ICON, /* no */
3576#endif
3577 IDC_ARROW, /* crosshair */
3578 IDC_ARROW, /* hand1 */
3579 IDC_ARROW, /* hand2 */
3580 IDC_ARROW, /* pencil */
3581 IDC_ARROW, /* question */
3582 IDC_ARROW, /* right-arrow */
3583 IDC_UPARROW, /* up-arrow */
3584 IDC_ARROW /* last one */
3585};
3586
3587 void
3588mch_set_mouse_shape(int shape)
3589{
3590 LPCSTR idc;
3591
3592 if (shape == MSHAPE_HIDE)
3593 ShowCursor(FALSE);
3594 else
3595 {
3596 if (shape >= MSHAPE_NUMBERED)
3597 idc = IDC_ARROW;
3598 else
3599 idc = mshape_idcs[shape];
3600#ifdef SetClassLongPtr
3601 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3602#else
3603# ifdef WIN32
3604 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
3605# else /* Win16 */
3606 SetClassWord(s_textArea, GCW_HCURSOR, (WORD)LoadCursor(NULL, idc));
3607# endif
3608#endif
3609 if (!p_mh)
3610 {
3611 POINT mp;
3612
3613 /* Set the position to make it redrawn with the new shape. */
3614 (void)GetCursorPos((LPPOINT)&mp);
3615 (void)SetCursorPos(mp.x, mp.y);
3616 ShowCursor(TRUE);
3617 }
3618 }
3619}
3620#endif
3621
3622#ifdef FEAT_BROWSE
3623/*
3624 * The file browser exists in two versions: with "W" uses wide characters,
3625 * without "W" the current codepage. When FEAT_MBYTE is defined and on
3626 * Windows NT/2000/XP the "W" functions are used.
3627 */
3628
3629# if defined(FEAT_MBYTE) && defined(WIN3264)
3630/*
3631 * Wide version of convert_filter().
3632 */
3633 static WCHAR *
3634convert_filterW(char_u *s)
3635{
3636 char_u *tmp;
3637 int len;
3638 WCHAR *res;
3639
3640 tmp = convert_filter(s);
3641 if (tmp == NULL)
3642 return NULL;
3643 len = (int)STRLEN(s) + 3;
3644 res = enc_to_utf16(tmp, &len);
3645 vim_free(tmp);
3646 return res;
3647}
3648
3649/*
3650 * Wide version of gui_mch_browse(). Keep in sync!
3651 */
3652 static char_u *
3653gui_mch_browseW(
3654 int saving,
3655 char_u *title,
3656 char_u *dflt,
3657 char_u *ext,
3658 char_u *initdir,
3659 char_u *filter)
3660{
3661 /* We always use the wide function. This means enc_to_utf16() must work,
3662 * otherwise it fails miserably! */
3663 OPENFILENAMEW fileStruct;
3664 WCHAR fileBuf[MAXPATHL];
3665 WCHAR *wp;
3666 int i;
3667 WCHAR *titlep = NULL;
3668 WCHAR *extp = NULL;
3669 WCHAR *initdirp = NULL;
3670 WCHAR *filterp;
3671 char_u *p;
3672
3673 if (dflt == NULL)
3674 fileBuf[0] = NUL;
3675 else
3676 {
3677 wp = enc_to_utf16(dflt, NULL);
3678 if (wp == NULL)
3679 fileBuf[0] = NUL;
3680 else
3681 {
3682 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3683 fileBuf[i] = wp[i];
3684 fileBuf[i] = NUL;
3685 vim_free(wp);
3686 }
3687 }
3688
3689 /* Convert the filter to Windows format. */
3690 filterp = convert_filterW(filter);
3691
3692 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003693#ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003694 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003695 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003696#else
3697 fileStruct.lStructSize = sizeof(fileStruct);
3698#endif
3699
3700 if (title != NULL)
3701 titlep = enc_to_utf16(title, NULL);
3702 fileStruct.lpstrTitle = titlep;
3703
3704 if (ext != NULL)
3705 extp = enc_to_utf16(ext, NULL);
3706 fileStruct.lpstrDefExt = extp;
3707
3708 fileStruct.lpstrFile = fileBuf;
3709 fileStruct.nMaxFile = MAXPATHL;
3710 fileStruct.lpstrFilter = filterp;
3711 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3712 /* has an initial dir been specified? */
3713 if (initdir != NULL && *initdir != NUL)
3714 {
3715 /* Must have backslashes here, no matter what 'shellslash' says */
3716 initdirp = enc_to_utf16(initdir, NULL);
3717 if (initdirp != NULL)
3718 {
3719 for (wp = initdirp; *wp != NUL; ++wp)
3720 if (*wp == '/')
3721 *wp = '\\';
3722 }
3723 fileStruct.lpstrInitialDir = initdirp;
3724 }
3725
3726 /*
3727 * TODO: Allow selection of multiple files. Needs another arg to this
3728 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3729 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3730 * files that don't exist yet, so I haven't put it in. What about
3731 * OFN_PATHMUSTEXIST?
3732 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3733 */
3734 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
3735#ifdef FEAT_SHORTCUT
3736 if (curbuf->b_p_bin)
3737 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
3738#endif
3739 if (saving)
3740 {
3741 if (!GetSaveFileNameW(&fileStruct))
3742 return NULL;
3743 }
3744 else
3745 {
3746 if (!GetOpenFileNameW(&fileStruct))
3747 return NULL;
3748 }
3749
3750 vim_free(filterp);
3751 vim_free(initdirp);
3752 vim_free(titlep);
3753 vim_free(extp);
3754
3755 /* Convert from UCS2 to 'encoding'. */
3756 p = utf16_to_enc(fileBuf, NULL);
3757 if (p != NULL)
3758 /* when out of memory we get garbage for non-ASCII chars */
3759 STRCPY(fileBuf, p);
3760 vim_free(p);
3761
3762 /* Give focus back to main window (when using MDI). */
3763 SetFocus(s_hwnd);
3764
3765 /* Shorten the file name if possible */
3766 return vim_strsave(shorten_fname1((char_u *)fileBuf));
3767}
3768# endif /* FEAT_MBYTE */
3769
3770
3771/*
3772 * Convert the string s to the proper format for a filter string by replacing
3773 * the \t and \n delimiters with \0.
3774 * Returns the converted string in allocated memory.
3775 *
3776 * Keep in sync with convert_filterW() above!
3777 */
3778 static char_u *
3779convert_filter(char_u *s)
3780{
3781 char_u *res;
3782 unsigned s_len = (unsigned)STRLEN(s);
3783 unsigned i;
3784
3785 res = alloc(s_len + 3);
3786 if (res != NULL)
3787 {
3788 for (i = 0; i < s_len; ++i)
3789 if (s[i] == '\t' || s[i] == '\n')
3790 res[i] = '\0';
3791 else
3792 res[i] = s[i];
3793 res[s_len] = NUL;
3794 /* Add two extra NULs to make sure it's properly terminated. */
3795 res[s_len + 1] = NUL;
3796 res[s_len + 2] = NUL;
3797 }
3798 return res;
3799}
3800
3801/*
3802 * Select a directory.
3803 */
3804 char_u *
3805gui_mch_browsedir(char_u *title, char_u *initdir)
3806{
3807 /* We fake this: Use a filter that doesn't select anything and a default
3808 * file name that won't be used. */
3809 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3810 initdir, (char_u *)_("Directory\t*.nothing\n"));
3811}
3812
3813/*
3814 * Pop open a file browser and return the file selected, in allocated memory,
3815 * or NULL if Cancel is hit.
3816 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3817 * title - Title message for the file browser dialog.
3818 * dflt - Default name of file.
3819 * ext - Default extension to be added to files without extensions.
3820 * initdir - directory in which to open the browser (NULL = current dir)
3821 * filter - Filter for matched files to choose from.
3822 *
3823 * Keep in sync with gui_mch_browseW() above!
3824 */
3825 char_u *
3826gui_mch_browse(
3827 int saving,
3828 char_u *title,
3829 char_u *dflt,
3830 char_u *ext,
3831 char_u *initdir,
3832 char_u *filter)
3833{
3834 OPENFILENAME fileStruct;
3835 char_u fileBuf[MAXPATHL];
3836 char_u *initdirp = NULL;
3837 char_u *filterp;
3838 char_u *p;
3839
3840# if defined(FEAT_MBYTE) && defined(WIN3264)
3841 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
3842 return gui_mch_browseW(saving, title, dflt, ext, initdir, filter);
3843# endif
3844
3845 if (dflt == NULL)
3846 fileBuf[0] = NUL;
3847 else
3848 vim_strncpy(fileBuf, dflt, MAXPATHL - 1);
3849
3850 /* Convert the filter to Windows format. */
3851 filterp = convert_filter(filter);
3852
3853 vim_memset(&fileStruct, 0, sizeof(OPENFILENAME));
3854#ifdef OPENFILENAME_SIZE_VERSION_400
3855 /* be compatible with Windows NT 4.0 */
3856 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400;
3857#else
3858 fileStruct.lStructSize = sizeof(fileStruct);
3859#endif
3860
3861 fileStruct.lpstrTitle = (LPSTR)title;
3862 fileStruct.lpstrDefExt = (LPSTR)ext;
3863
3864 fileStruct.lpstrFile = (LPSTR)fileBuf;
3865 fileStruct.nMaxFile = MAXPATHL;
3866 fileStruct.lpstrFilter = (LPSTR)filterp;
3867 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3868 /* has an initial dir been specified? */
3869 if (initdir != NULL && *initdir != NUL)
3870 {
3871 /* Must have backslashes here, no matter what 'shellslash' says */
3872 initdirp = vim_strsave(initdir);
3873 if (initdirp != NULL)
3874 for (p = initdirp; *p != NUL; ++p)
3875 if (*p == '/')
3876 *p = '\\';
3877 fileStruct.lpstrInitialDir = (LPSTR)initdirp;
3878 }
3879
3880 /*
3881 * TODO: Allow selection of multiple files. Needs another arg to this
3882 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3883 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3884 * files that don't exist yet, so I haven't put it in. What about
3885 * OFN_PATHMUSTEXIST?
3886 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3887 */
3888 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
3889#ifdef FEAT_SHORTCUT
3890 if (curbuf->b_p_bin)
3891 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
3892#endif
3893 if (saving)
3894 {
3895 if (!GetSaveFileName(&fileStruct))
3896 return NULL;
3897 }
3898 else
3899 {
3900 if (!GetOpenFileName(&fileStruct))
3901 return NULL;
3902 }
3903
3904 vim_free(filterp);
3905 vim_free(initdirp);
3906
3907 /* Give focus back to main window (when using MDI). */
3908 SetFocus(s_hwnd);
3909
3910 /* Shorten the file name if possible */
3911 return vim_strsave(shorten_fname1((char_u *)fileBuf));
3912}
3913#endif /* FEAT_BROWSE */
3914
3915/*ARGSUSED*/
3916 static void
3917_OnDropFiles(
3918 HWND hwnd,
3919 HDROP hDrop)
3920{
3921#ifdef FEAT_WINDOWS
3922#ifdef WIN3264
3923# define BUFPATHLEN _MAX_PATH
3924# define DRAGQVAL 0xFFFFFFFF
3925#else
3926# define BUFPATHLEN MAXPATHL
3927# define DRAGQVAL 0xFFFF
3928#endif
3929#ifdef FEAT_MBYTE
3930 WCHAR wszFile[BUFPATHLEN];
3931#endif
3932 char szFile[BUFPATHLEN];
3933 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3934 UINT i;
3935 char_u **fnames;
3936 POINT pt;
3937 int_u modifiers = 0;
3938
3939 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3940
3941 /* Obtain dropped position */
3942 DragQueryPoint(hDrop, &pt);
3943 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3944
3945 reset_VIsual();
3946
3947 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3948
3949 if (fnames != NULL)
3950 for (i = 0; i < cFiles; ++i)
3951 {
3952#ifdef FEAT_MBYTE
3953 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3954 fnames[i] = utf16_to_enc(wszFile, NULL);
3955 else
3956#endif
3957 {
3958 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3959 fnames[i] = vim_strsave((char_u *)szFile);
3960 }
3961 }
3962
3963 DragFinish(hDrop);
3964
3965 if (fnames != NULL)
3966 {
3967 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3968 modifiers |= MOUSE_SHIFT;
3969 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3970 modifiers |= MOUSE_CTRL;
3971 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3972 modifiers |= MOUSE_ALT;
3973
3974 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3975
3976 s_need_activate = TRUE;
3977 }
3978#endif
3979}
3980
3981/*ARGSUSED*/
3982 static int
3983_OnScroll(
3984 HWND hwnd,
3985 HWND hwndCtl,
3986 UINT code,
3987 int pos)
3988{
3989 static UINT prev_code = 0; /* code of previous call */
3990 scrollbar_T *sb, *sb_info;
3991 long val;
3992 int dragging = FALSE;
3993 int dont_scroll_save = dont_scroll;
3994#ifndef WIN3264
3995 int nPos;
3996#else
3997 SCROLLINFO si;
3998
3999 si.cbSize = sizeof(si);
4000 si.fMask = SIF_POS;
4001#endif
4002
4003 sb = gui_mswin_find_scrollbar(hwndCtl);
4004 if (sb == NULL)
4005 return 0;
4006
4007 if (sb->wp != NULL) /* Left or right scrollbar */
4008 {
4009 /*
4010 * Careful: need to get scrollbar info out of first (left) scrollbar
4011 * for window, but keep real scrollbar too because we must pass it to
4012 * gui_drag_scrollbar().
4013 */
4014 sb_info = &sb->wp->w_scrollbars[0];
4015 }
4016 else /* Bottom scrollbar */
4017 sb_info = sb;
4018 val = sb_info->value;
4019
4020 switch (code)
4021 {
4022 case SB_THUMBTRACK:
4023 val = pos;
4024 dragging = TRUE;
4025 if (sb->scroll_shift > 0)
4026 val <<= sb->scroll_shift;
4027 break;
4028 case SB_LINEDOWN:
4029 val++;
4030 break;
4031 case SB_LINEUP:
4032 val--;
4033 break;
4034 case SB_PAGEDOWN:
4035 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
4036 break;
4037 case SB_PAGEUP:
4038 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
4039 break;
4040 case SB_TOP:
4041 val = 0;
4042 break;
4043 case SB_BOTTOM:
4044 val = sb_info->max;
4045 break;
4046 case SB_ENDSCROLL:
4047 if (prev_code == SB_THUMBTRACK)
4048 {
4049 /*
4050 * "pos" only gives us 16-bit data. In case of large file,
4051 * use GetScrollPos() which returns 32-bit. Unfortunately it
4052 * is not valid while the scrollbar is being dragged.
4053 */
4054 val = GetScrollPos(hwndCtl, SB_CTL);
4055 if (sb->scroll_shift > 0)
4056 val <<= sb->scroll_shift;
4057 }
4058 break;
4059
4060 default:
4061 /* TRACE("Unknown scrollbar event %d\n", code); */
4062 return 0;
4063 }
4064 prev_code = code;
4065
4066#ifdef WIN3264
4067 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
4068 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
4069#else
4070 nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
4071 SetScrollPos(hwndCtl, SB_CTL, nPos, TRUE);
4072#endif
4073
4074 /*
4075 * When moving a vertical scrollbar, move the other vertical scrollbar too.
4076 */
4077 if (sb->wp != NULL)
4078 {
4079 scrollbar_T *sba = sb->wp->w_scrollbars;
4080 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
4081
4082#ifdef WIN3264
4083 SetScrollInfo(id, SB_CTL, &si, TRUE);
4084#else
4085 SetScrollPos(id, SB_CTL, nPos, TRUE);
4086#endif
4087 }
4088
4089 /* Don't let us be interrupted here by another message. */
4090 s_busy_processing = TRUE;
4091
4092 /* When "allow_scrollbar" is FALSE still need to remember the new
4093 * position, but don't actually scroll by setting "dont_scroll". */
4094 dont_scroll = !allow_scrollbar;
4095
4096 gui_drag_scrollbar(sb, val, dragging);
4097
4098 s_busy_processing = FALSE;
4099 dont_scroll = dont_scroll_save;
4100
4101 return 0;
4102}
4103
4104
4105/*
4106 * Get command line arguments.
4107 * Use "prog" as the name of the program and "cmdline" as the arguments.
4108 * Copy the arguments to allocated memory.
4109 * Return the number of arguments (including program name).
4110 * Return pointers to the arguments in "argvp". Memory is allocated with
4111 * malloc(), use free() instead of vim_free().
4112 * Return pointer to buffer in "tofree".
4113 * Returns zero when out of memory.
4114 */
4115/*ARGSUSED*/
4116 int
4117get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
4118{
4119 int i;
4120 char *p;
4121 char *progp;
4122 char *pnew = NULL;
4123 char *newcmdline;
4124 int inquote;
4125 int argc;
4126 char **argv = NULL;
4127 int round;
4128
4129 *tofree = NULL;
4130
4131#ifdef FEAT_MBYTE
4132 /* Try using the Unicode version first, it takes care of conversion when
4133 * 'encoding' is changed. */
4134 argc = get_cmd_argsW(&argv);
4135 if (argc != 0)
4136 goto done;
4137#endif
4138
4139 /* Handle the program name. Remove the ".exe" extension, and find the 1st
4140 * non-space. */
4141 p = strrchr(prog, '.');
4142 if (p != NULL)
4143 *p = NUL;
4144 for (progp = prog; *progp == ' '; ++progp)
4145 ;
4146
4147 /* The command line is copied to allocated memory, so that we can change
4148 * it. Add the size of the string, the separating NUL and a terminating
4149 * NUL. */
4150 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
4151 if (newcmdline == NULL)
4152 return 0;
4153
4154 /*
4155 * First round: count the number of arguments ("pnew" == NULL).
4156 * Second round: produce the arguments.
4157 */
4158 for (round = 1; round <= 2; ++round)
4159 {
4160 /* First argument is the program name. */
4161 if (pnew != NULL)
4162 {
4163 argv[0] = pnew;
4164 strcpy(pnew, progp);
4165 pnew += strlen(pnew);
4166 *pnew++ = NUL;
4167 }
4168
4169 /*
4170 * Isolate each argument and put it in argv[].
4171 */
4172 p = cmdline;
4173 argc = 1;
4174 while (*p != NUL)
4175 {
4176 inquote = FALSE;
4177 if (pnew != NULL)
4178 argv[argc] = pnew;
4179 ++argc;
4180 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
4181 {
4182 /* Backslashes are only special when followed by a double
4183 * quote. */
4184 i = (int)strspn(p, "\\");
4185 if (p[i] == '"')
4186 {
4187 /* Halve the number of backslashes. */
4188 if (i > 1 && pnew != NULL)
4189 {
4190 vim_memset(pnew, '\\', i / 2);
4191 pnew += i / 2;
4192 }
4193
4194 /* Even nr of backslashes toggles quoting, uneven copies
4195 * the double quote. */
4196 if ((i & 1) == 0)
4197 inquote = !inquote;
4198 else if (pnew != NULL)
4199 *pnew++ = '"';
4200 p += i + 1;
4201 }
4202 else if (i > 0)
4203 {
4204 /* Copy span of backslashes unmodified. */
4205 if (pnew != NULL)
4206 {
4207 vim_memset(pnew, '\\', i);
4208 pnew += i;
4209 }
4210 p += i;
4211 }
4212 else
4213 {
4214 if (pnew != NULL)
4215 *pnew++ = *p;
4216#ifdef FEAT_MBYTE
4217 /* Can't use mb_* functions, because 'encoding' is not
4218 * initialized yet here. */
4219 if (IsDBCSLeadByte(*p))
4220 {
4221 ++p;
4222 if (pnew != NULL)
4223 *pnew++ = *p;
4224 }
4225#endif
4226 ++p;
4227 }
4228 }
4229
4230 if (pnew != NULL)
4231 *pnew++ = NUL;
4232 while (*p == ' ' || *p == '\t')
4233 ++p; /* advance until a non-space */
4234 }
4235
4236 if (round == 1)
4237 {
4238 argv = (char **)malloc((argc + 1) * sizeof(char *));
4239 if (argv == NULL )
4240 {
4241 free(newcmdline);
4242 return 0; /* malloc error */
4243 }
4244 pnew = newcmdline;
4245 *tofree = newcmdline;
4246 }
4247 }
4248
4249#ifdef FEAT_MBYTE
4250done:
4251#endif
4252 argv[argc] = NULL; /* NULL-terminated list */
4253 *argvp = argv;
4254 return argc;
4255}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257#ifdef FEAT_XPM_W32
4258# include "xpm_w32.h"
4259#endif
4260
4261#ifdef PROTO
4262# define WINAPI
4263#endif
4264
4265#ifdef __MINGW32__
4266/*
4267 * Add a lot of missing defines.
4268 * They are not always missing, we need the #ifndef's.
4269 */
4270# ifndef _cdecl
4271# define _cdecl
4272# endif
4273# ifndef IsMinimized
4274# define IsMinimized(hwnd) IsIconic(hwnd)
4275# endif
4276# ifndef IsMaximized
4277# define IsMaximized(hwnd) IsZoomed(hwnd)
4278# endif
4279# ifndef SelectFont
4280# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
4281# endif
4282# ifndef GetStockBrush
4283# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
4284# endif
4285# ifndef DeleteBrush
4286# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
4287# endif
4288
4289# ifndef HANDLE_WM_RBUTTONDBLCLK
4290# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4291 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4292# endif
4293# ifndef HANDLE_WM_MBUTTONUP
4294# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
4295 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4296# endif
4297# ifndef HANDLE_WM_MBUTTONDBLCLK
4298# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4299 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4300# endif
4301# ifndef HANDLE_WM_LBUTTONDBLCLK
4302# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4303 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4304# endif
4305# ifndef HANDLE_WM_RBUTTONDOWN
4306# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
4307 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4308# endif
4309# ifndef HANDLE_WM_MOUSEMOVE
4310# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
4311 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4312# endif
4313# ifndef HANDLE_WM_RBUTTONUP
4314# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
4315 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4316# endif
4317# ifndef HANDLE_WM_MBUTTONDOWN
4318# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
4319 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4320# endif
4321# ifndef HANDLE_WM_LBUTTONUP
4322# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
4323 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4324# endif
4325# ifndef HANDLE_WM_LBUTTONDOWN
4326# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
4327 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4328# endif
4329# ifndef HANDLE_WM_SYSCHAR
4330# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
4331 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4332# endif
4333# ifndef HANDLE_WM_ACTIVATEAPP
4334# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
4335 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
4336# endif
4337# ifndef HANDLE_WM_WINDOWPOSCHANGING
4338# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
4339 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
4340# endif
4341# ifndef HANDLE_WM_VSCROLL
4342# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
4343 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4344# endif
4345# ifndef HANDLE_WM_SETFOCUS
4346# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
4347 ((fn)((hwnd), (HWND)(wParam)), 0L)
4348# endif
4349# ifndef HANDLE_WM_KILLFOCUS
4350# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
4351 ((fn)((hwnd), (HWND)(wParam)), 0L)
4352# endif
4353# ifndef HANDLE_WM_HSCROLL
4354# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
4355 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4356# endif
4357# ifndef HANDLE_WM_DROPFILES
4358# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
4359 ((fn)((hwnd), (HDROP)(wParam)), 0L)
4360# endif
4361# ifndef HANDLE_WM_CHAR
4362# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
4363 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4364# endif
4365# ifndef HANDLE_WM_SYSDEADCHAR
4366# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
4367 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4368# endif
4369# ifndef HANDLE_WM_DEADCHAR
4370# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4371 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4372# endif
4373#endif /* __MINGW32__ */
4374
4375
4376/* Some parameters for tearoff menus. All in pixels. */
4377#define TEAROFF_PADDING_X 2
4378#define TEAROFF_BUTTON_PAD_X 8
4379#define TEAROFF_MIN_WIDTH 200
4380#define TEAROFF_SUBMENU_LABEL ">>"
4381#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4382
4383
4384/* For the Intellimouse: */
4385#ifndef WM_MOUSEWHEEL
4386#define WM_MOUSEWHEEL 0x20a
4387#endif
4388
4389
4390#ifdef FEAT_BEVAL
4391# define ID_BEVAL_TOOLTIP 200
4392# define BEVAL_TEXT_LEN MAXPATHL
4393
Bram Moolenaar167632f2010-05-26 21:42:54 +02004394#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004395/* Work around old versions of basetsd.h which wrongly declares
4396 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02004397# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00004398# define UINT_PTR UINT
4399#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004400
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004401static void make_tooltip(BalloonEval *beval, char *text, POINT pt);
4402static void delete_tooltip(BalloonEval *beval);
4403static VOID CALLBACK BevalTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004404
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004406static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004408
Bram Moolenaar82881492012-11-20 16:53:39 +01004409
4410/* cproto fails on missing include files */
4411#ifndef PROTO
4412
Bram Moolenaar45360022005-07-21 21:08:21 +00004413/*
4414 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004415 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004416 */
Bram Moolenaar82881492012-11-20 16:53:39 +01004417# include <pshpack1.h>
4418
4419#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004420
4421typedef struct _DllVersionInfo
4422{
4423 DWORD cbSize;
4424 DWORD dwMajorVersion;
4425 DWORD dwMinorVersion;
4426 DWORD dwBuildNumber;
4427 DWORD dwPlatformID;
4428} DLLVERSIONINFO;
4429
Bram Moolenaar82881492012-11-20 16:53:39 +01004430#ifndef PROTO
4431# include <poppack.h>
4432#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004433
Bram Moolenaar45360022005-07-21 21:08:21 +00004434typedef struct tagTOOLINFOA_NEW
4435{
4436 UINT cbSize;
4437 UINT uFlags;
4438 HWND hwnd;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004439 UINT_PTR uId;
Bram Moolenaar45360022005-07-21 21:08:21 +00004440 RECT rect;
4441 HINSTANCE hinst;
4442 LPSTR lpszText;
4443 LPARAM lParam;
4444} TOOLINFO_NEW;
4445
4446typedef struct tagNMTTDISPINFO_NEW
4447{
4448 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004449 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004450 char szText[80];
4451 HINSTANCE hinst;
4452 UINT uFlags;
4453 LPARAM lParam;
4454} NMTTDISPINFO_NEW;
4455
Bram Moolenaar45360022005-07-21 21:08:21 +00004456typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4457#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004458# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459#endif
4460
Bram Moolenaar45360022005-07-21 21:08:21 +00004461#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004462# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004463#endif
4464
4465#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004466# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004467#endif
4468
4469#endif /* defined(FEAT_BEVAL) */
4470
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004471#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4472/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4473 * it here if LPNMTTDISPINFO isn't defined.
4474 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4475 * _MSC_VER. */
4476# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4477typedef struct tagNMTTDISPINFOA {
4478 NMHDR hdr;
4479 LPSTR lpszText;
4480 char szText[80];
4481 HINSTANCE hinst;
4482 UINT uFlags;
4483 LPARAM lParam;
4484} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4485# define LPNMTTDISPINFO LPNMTTDISPINFOA
4486
4487# ifdef FEAT_MBYTE
4488typedef struct tagNMTTDISPINFOW {
4489 NMHDR hdr;
4490 LPWSTR lpszText;
4491 WCHAR szText[80];
4492 HINSTANCE hinst;
4493 UINT uFlags;
4494 LPARAM lParam;
4495} NMTTDISPINFOW, *LPNMTTDISPINFOW;
4496# endif
4497# endif
4498#endif
4499
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004500#ifndef TTN_GETDISPINFOW
4501# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4502#endif
4503
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504/* Local variables: */
4505
4506#ifdef FEAT_MENU
4507static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509
4510/*
4511 * Use the system font for dialogs and tear-off menus. Remove this line to
4512 * use DLG_FONT_NAME.
4513 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004514#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515
4516#define VIM_NAME "vim"
4517#define VIM_CLASS "Vim"
4518#define VIM_CLASSW L"Vim"
4519
4520/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4521 * thus there should be room for every dialog. For tearoffs it's made bigger
4522 * when needed. */
4523#define DLG_ALLOC_SIZE 16 * 1024
4524
4525/*
4526 * stuff for dialogs, menus, tearoffs etc.
4527 */
4528static LRESULT APIENTRY dialog_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004529#ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530static LRESULT APIENTRY tearoff_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532static PWORD
4533add_dialog_element(
4534 PWORD p,
4535 DWORD lStyle,
4536 WORD x,
4537 WORD y,
4538 WORD w,
4539 WORD h,
4540 WORD Id,
4541 WORD clss,
4542 const char *caption);
4543static LPWORD lpwAlign(LPWORD);
4544static int nCopyAnsiToWideChar(LPWORD, LPSTR);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004545#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548static void get_dialog_font_metrics(void);
4549
4550static int dialog_default_button = -1;
4551
4552/* Intellimouse support */
4553static int mouse_scroll_lines = 0;
4554static UINT msh_msgmousewheel = 0;
4555
4556static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4557#ifdef FEAT_TOOLBAR
4558static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004559static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560static int get_toolbar_bitmap(vimmenu_T *menu);
4561#endif
4562
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004563#ifdef FEAT_GUI_TABLINE
4564static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004565static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004566#endif
4567
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568#ifdef FEAT_MBYTE_IME
4569static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4570static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4571#endif
4572#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4573# ifdef NOIME
4574typedef struct tagCOMPOSITIONFORM {
4575 DWORD dwStyle;
4576 POINT ptCurrentPos;
4577 RECT rcArea;
4578} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4579typedef HANDLE HIMC;
4580# endif
4581
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004582static HINSTANCE hLibImm = NULL;
4583static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4584static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4585static HIMC (WINAPI *pImmGetContext)(HWND);
4586static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4587static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4588static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4589static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
4590static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
4591static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
4592static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4593static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004594static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595static void dyn_imm_load(void);
4596#else
4597# define pImmGetCompositionStringA ImmGetCompositionStringA
4598# define pImmGetCompositionStringW ImmGetCompositionStringW
4599# define pImmGetContext ImmGetContext
4600# define pImmAssociateContext ImmAssociateContext
4601# define pImmReleaseContext ImmReleaseContext
4602# define pImmGetOpenStatus ImmGetOpenStatus
4603# define pImmSetOpenStatus ImmSetOpenStatus
4604# define pImmGetCompositionFont ImmGetCompositionFontA
4605# define pImmSetCompositionFont ImmSetCompositionFontA
4606# define pImmSetCompositionWindow ImmSetCompositionWindow
4607# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004608# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609#endif
4610
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611/* multi monitor support */
4612typedef struct _MONITORINFOstruct
4613{
4614 DWORD cbSize;
4615 RECT rcMonitor;
4616 RECT rcWork;
4617 DWORD dwFlags;
4618} _MONITORINFO;
4619
4620typedef HANDLE _HMONITOR;
4621typedef _HMONITOR (WINAPI *TMonitorFromWindow)(HWND, DWORD);
4622typedef BOOL (WINAPI *TGetMonitorInfo)(_HMONITOR, _MONITORINFO *);
4623
4624static TMonitorFromWindow pMonitorFromWindow = NULL;
4625static TGetMonitorInfo pGetMonitorInfo = NULL;
4626static HANDLE user32_lib = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627/*
4628 * Return TRUE when running under Windows NT 3.x or Win32s, both of which have
4629 * less fancy GUI APIs.
4630 */
4631 static int
4632is_winnt_3(void)
4633{
4634 return ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
4635 && os_version.dwMajorVersion == 3)
4636 || (os_version.dwPlatformId == VER_PLATFORM_WIN32s));
4637}
4638
4639/*
4640 * Return TRUE when running under Win32s.
4641 */
4642 int
4643gui_is_win32s(void)
4644{
4645 return (os_version.dwPlatformId == VER_PLATFORM_WIN32s);
4646}
4647
4648#ifdef FEAT_MENU
4649/*
4650 * Figure out how high the menu bar is at the moment.
4651 */
4652 static int
4653gui_mswin_get_menu_height(
4654 int fix_window) /* If TRUE, resize window if menu height changed */
4655{
4656 static int old_menu_height = -1;
4657
4658 RECT rc1, rc2;
4659 int num;
4660 int menu_height;
4661
4662 if (gui.menu_is_active)
4663 num = GetMenuItemCount(s_menuBar);
4664 else
4665 num = 0;
4666
4667 if (num == 0)
4668 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004669 else if (IsMinimized(s_hwnd))
4670 {
4671 /* The height of the menu cannot be determined while the window is
4672 * minimized. Take the previous height if the menu is changed in that
4673 * state, to avoid that Vim's vertical window size accidentally
4674 * increases due to the unaccounted-for menu height. */
4675 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 else
4678 {
4679 if (is_winnt_3()) /* for NT 3.xx */
4680 {
4681 if (gui.starting)
4682 menu_height = GetSystemMetrics(SM_CYMENU);
4683 else
4684 {
4685 RECT r1, r2;
4686 int frameht = GetSystemMetrics(SM_CYFRAME);
4687 int capht = GetSystemMetrics(SM_CYCAPTION);
4688
4689 /* get window rect of s_hwnd
4690 * get client rect of s_hwnd
4691 * get cap height
4692 * subtract from window rect, the sum of client height,
4693 * (if not maximized)frame thickness, and caption height.
4694 */
4695 GetWindowRect(s_hwnd, &r1);
4696 GetClientRect(s_hwnd, &r2);
4697 menu_height = r1.bottom - r1.top - (r2.bottom - r2.top
4698 + 2 * frameht * (!IsZoomed(s_hwnd)) + capht);
4699 }
4700 }
4701 else /* win95 and variants (NT 4.0, I guess) */
4702 {
4703 /*
4704 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4705 * seem to have been set yet, so menu wraps in default window
4706 * width which is very narrow. Instead just return height of a
4707 * single menu item. Will still be wrong when the menu really
4708 * should wrap over more than one line.
4709 */
4710 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4711 if (gui.starting)
4712 menu_height = rc1.bottom - rc1.top + 1;
4713 else
4714 {
4715 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4716 menu_height = rc2.bottom - rc1.top + 1;
4717 }
4718 }
4719 }
4720
4721 if (fix_window && menu_height != old_menu_height)
4722 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004723 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 }
Bram Moolenaar71371b12015-03-24 17:57:12 +01004725 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726
4727 return menu_height;
4728}
4729#endif /*FEAT_MENU*/
4730
4731
4732/*
4733 * Setup for the Intellimouse
4734 */
4735 static void
4736init_mouse_wheel(void)
4737{
4738
4739#ifndef SPI_GETWHEELSCROLLLINES
4740# define SPI_GETWHEELSCROLLLINES 104
4741#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004742#ifndef SPI_SETWHEELSCROLLLINES
4743# define SPI_SETWHEELSCROLLLINES 105
4744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745
4746#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4747#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4748#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4749#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4750
4751 HWND hdl_mswheel;
4752 UINT msh_msgscrolllines;
4753
4754 msh_msgmousewheel = 0;
4755 mouse_scroll_lines = 3; /* reasonable default */
4756
4757 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
4758 && os_version.dwMajorVersion >= 4)
4759 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
4760 && ((os_version.dwMajorVersion == 4
4761 && os_version.dwMinorVersion >= 10)
4762 || os_version.dwMajorVersion >= 5)))
4763 {
4764 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4765 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4766 &mouse_scroll_lines, 0);
4767 }
4768 else if (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
4769 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
4770 && os_version.dwMajorVersion < 4))
4771 { /*
4772 * If Win95 or NT 3.51,
4773 * try to find the hidden point32 window.
4774 */
4775 hdl_mswheel = FindWindow(VMOUSEZ_CLASSNAME, VMOUSEZ_TITLE);
4776 if (hdl_mswheel)
4777 {
4778 msh_msgscrolllines = RegisterWindowMessage(VMSH_SCROLL_LINES);
4779 if (msh_msgscrolllines)
4780 {
4781 mouse_scroll_lines = (int)SendMessage(hdl_mswheel,
4782 msh_msgscrolllines, 0, 0);
4783 msh_msgmousewheel = RegisterWindowMessage(VMSH_MOUSEWHEEL);
4784 }
4785 }
4786 }
4787}
4788
4789
4790/* Intellimouse wheel handler */
4791 static void
4792_OnMouseWheel(
4793 HWND hwnd,
4794 short zDelta)
4795{
4796/* Treat a mouse wheel event as if it were a scroll request */
4797 int i;
4798 int size;
4799 HWND hwndCtl;
4800
4801 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4802 {
4803 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4804 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4805 }
4806 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4807 {
4808 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4809 size = curwin->w_scrollbars[SBAR_LEFT].size;
4810 }
4811 else
4812 return;
4813
4814 size = curwin->w_height;
4815 if (mouse_scroll_lines == 0)
4816 init_mouse_wheel();
4817
4818 if (mouse_scroll_lines > 0
4819 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4820 {
4821 for (i = mouse_scroll_lines; i > 0; --i)
4822 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4823 }
4824 else
4825 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
4826}
4827
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004828#ifdef USE_SYSMENU_FONT
4829/*
4830 * Get Menu Font.
4831 * Return OK or FAIL.
4832 */
4833 static int
4834gui_w32_get_menu_font(LOGFONT *lf)
4835{
4836 NONCLIENTMETRICS nm;
4837
4838 nm.cbSize = sizeof(NONCLIENTMETRICS);
4839 if (!SystemParametersInfo(
4840 SPI_GETNONCLIENTMETRICS,
4841 sizeof(NONCLIENTMETRICS),
4842 &nm,
4843 0))
4844 return FAIL;
4845 *lf = nm.lfMenuFont;
4846 return OK;
4847}
4848#endif
4849
4850
4851#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4852/*
4853 * Set the GUI tabline font to the system menu font
4854 */
4855 static void
4856set_tabline_font(void)
4857{
4858 LOGFONT lfSysmenu;
4859 HFONT font;
4860 HWND hwnd;
4861 HDC hdc;
4862 HFONT hfntOld;
4863 TEXTMETRIC tm;
4864
4865 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4866 return;
4867
4868 font = CreateFontIndirect(&lfSysmenu);
4869
4870 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4871
4872 /*
4873 * Compute the height of the font used for the tab text
4874 */
4875 hwnd = GetDesktopWindow();
4876 hdc = GetWindowDC(hwnd);
4877 hfntOld = SelectFont(hdc, font);
4878
4879 GetTextMetrics(hdc, &tm);
4880
4881 SelectFont(hdc, hfntOld);
4882 ReleaseDC(hwnd, hdc);
4883
4884 /*
4885 * The space used by the tab border and the space between the tab label
4886 * and the tab border is included as 7.
4887 */
4888 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4889}
4890#endif
4891
Bram Moolenaar520470a2005-06-16 21:59:56 +00004892/*
4893 * Invoked when a setting was changed.
4894 */
4895 static LRESULT CALLBACK
4896_OnSettingChange(UINT n)
4897{
4898 if (n == SPI_SETWHEELSCROLLLINES)
4899 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4900 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004901#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4902 if (n == SPI_SETNONCLIENTMETRICS)
4903 set_tabline_font();
4904#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004905 return 0;
4906}
4907
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908#ifdef FEAT_NETBEANS_INTG
4909 static void
4910_OnWindowPosChanged(
4911 HWND hwnd,
4912 const LPWINDOWPOS lpwpos)
4913{
4914 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004915 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916
4917 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4918 || lpwpos->cx != cx || lpwpos->cy != cy))
4919 {
4920 x = lpwpos->x;
4921 y = lpwpos->y;
4922 cx = lpwpos->cx;
4923 cy = lpwpos->cy;
4924 netbeans_frame_moved(x, y);
4925 }
4926 /* Allow to send WM_SIZE and WM_MOVE */
4927 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4928}
4929#endif
4930
4931 static int
4932_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 UINT fwSide,
4934 LPRECT lprc)
4935{
4936 int w, h;
4937 int valid_w, valid_h;
4938 int w_offset, h_offset;
4939
4940 w = lprc->right - lprc->left;
4941 h = lprc->bottom - lprc->top;
4942 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4943 w_offset = w - valid_w;
4944 h_offset = h - valid_h;
4945
4946 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4947 || fwSide == WMSZ_BOTTOMLEFT)
4948 lprc->left += w_offset;
4949 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4950 || fwSide == WMSZ_BOTTOMRIGHT)
4951 lprc->right -= w_offset;
4952
4953 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4954 || fwSide == WMSZ_TOPRIGHT)
4955 lprc->top += h_offset;
4956 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4957 || fwSide == WMSZ_BOTTOMRIGHT)
4958 lprc->bottom -= h_offset;
4959 return TRUE;
4960}
4961
4962
4963
4964 static LRESULT CALLBACK
4965_WndProc(
4966 HWND hwnd,
4967 UINT uMsg,
4968 WPARAM wParam,
4969 LPARAM lParam)
4970{
4971 /*
4972 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4973 hwnd, uMsg, wParam, lParam);
4974 */
4975
4976 HandleMouseHide(uMsg, lParam);
4977
4978 s_uMsg = uMsg;
4979 s_wParam = wParam;
4980 s_lParam = lParam;
4981
4982 switch (uMsg)
4983 {
4984 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4985 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4986 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4987 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4988 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4989 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4990 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4991 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4992 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4993#ifdef FEAT_MENU
4994 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4995#endif
4996 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4997 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4998 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4999 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
5000 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
5001 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
5002 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
5003 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
5004 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
5005#ifdef FEAT_NETBEANS_INTG
5006 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
5007#endif
5008
Bram Moolenaarafa24992006-03-27 20:58:26 +00005009#ifdef FEAT_GUI_TABLINE
5010 case WM_RBUTTONUP:
5011 {
5012 if (gui_mch_showing_tabline())
5013 {
5014 POINT pt;
5015 RECT rect;
5016
5017 /*
5018 * If the cursor is on the tabline, display the tab menu
5019 */
5020 GetCursorPos((LPPOINT)&pt);
5021 GetWindowRect(s_textArea, &rect);
5022 if (pt.y < rect.top)
5023 {
5024 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005025 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00005026 }
5027 }
5028 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5029 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005030 case WM_LBUTTONDBLCLK:
5031 {
5032 /*
5033 * If the user double clicked the tabline, create a new tab
5034 */
5035 if (gui_mch_showing_tabline())
5036 {
5037 POINT pt;
5038 RECT rect;
5039
5040 GetCursorPos((LPPOINT)&pt);
5041 GetWindowRect(s_textArea, &rect);
5042 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005043 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005044 }
5045 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5046 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00005047#endif
5048
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 case WM_QUERYENDSESSION: /* System wants to go down. */
5050 gui_shell_closed(); /* Will exit when no changed buffers. */
5051 return FALSE; /* Do NOT allow system to go down. */
5052
5053 case WM_ENDSESSION:
5054 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01005055 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005057 return 0L;
5058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 break;
5060
5061 case WM_CHAR:
5062 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
5063 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005064 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 return 0L;
5066
5067 case WM_SYSCHAR:
5068 /*
5069 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
5070 * shortcut key, handle like a typed ALT key, otherwise call Windows
5071 * ALT key handling.
5072 */
5073#ifdef FEAT_MENU
5074 if ( !gui.menu_is_active
5075 || p_wak[0] == 'n'
5076 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
5077 )
5078#endif
5079 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005080 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 return 0L;
5082 }
5083#ifdef FEAT_MENU
5084 else
5085 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5086#endif
5087
5088 case WM_SYSKEYUP:
5089#ifdef FEAT_MENU
5090 /* This used to be done only when menu is active: ALT key is used for
5091 * that. But that caused problems when menu is disabled and using
5092 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
5093 * are received, mouse pointer remains hidden. */
5094 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5095#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01005096 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097#endif
5098
5099 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005100 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101
5102 case WM_MOUSEWHEEL:
5103 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01005104 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105
Bram Moolenaar520470a2005-06-16 21:59:56 +00005106 /* Notification for change in SystemParametersInfo() */
5107 case WM_SETTINGCHANGE:
5108 return _OnSettingChange((UINT)wParam);
5109
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005110#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 case WM_NOTIFY:
5112 switch (((LPNMHDR) lParam)->code)
5113 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005114# ifdef FEAT_MBYTE
5115 case TTN_GETDISPINFOW:
5116# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005117 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005119 LPNMHDR hdr = (LPNMHDR)lParam;
5120 char_u *str = NULL;
5121 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005122
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005123 vim_free(tt_text);
5124 tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005125
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005126# ifdef FEAT_GUI_TABLINE
5127 if (gui_mch_showing_tabline()
5128 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005129 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005130 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005131 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005132 * Mouse is over the GUI tabline. Display the
5133 * tooltip for the tab under the cursor
5134 *
5135 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005136 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005137 GetCursorPos(&pt);
5138 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005139 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005140 TCHITTESTINFO htinfo;
5141 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005142
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005143 /*
5144 * Get the tab under the cursor
5145 */
5146 htinfo.pt.x = pt.x;
5147 htinfo.pt.y = pt.y;
5148 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
5149 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005150 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005151 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005152
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005153 tp = find_tabpage(idx + 1);
5154 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005155 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005156 get_tabline_label(tp, TRUE);
5157 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005158 }
5159 }
5160 }
5161 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005162# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005163# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005164# ifdef FEAT_GUI_TABLINE
5165 else
5166# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005168 UINT idButton;
5169 vimmenu_T *pMenu;
5170
5171 idButton = (UINT) hdr->idFrom;
5172 pMenu = gui_mswin_find_menu(root_menu, idButton);
5173 if (pMenu)
5174 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005176# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005177 if (str != NULL)
5178 {
5179# ifdef FEAT_MBYTE
5180 if (hdr->code == TTN_GETDISPINFOW)
5181 {
5182 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
5183
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005184 /* Set the maximum width, this also enables using
5185 * \n for line break. */
5186 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5187 0, 500);
5188
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005189 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005190 lpdi->lpszText = tt_text;
5191 /* can't show tooltip if failed */
5192 }
5193 else
5194# endif
5195 {
5196 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
5197
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005198 /* Set the maximum width, this also enables using
5199 * \n for line break. */
5200 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5201 0, 500);
5202
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005203 if (STRLEN(str) < sizeof(lpdi->szText)
5204 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005205 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005206 sizeof(lpdi->szText) - 1);
5207 else
5208 lpdi->lpszText = tt_text;
5209 }
5210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 }
5212 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005213# ifdef FEAT_GUI_TABLINE
5214 case TCN_SELCHANGE:
5215 if (gui_mch_showing_tabline()
5216 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005217 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005218 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005219 return 0L;
5220 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005221 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00005222
5223 case NM_RCLICK:
5224 if (gui_mch_showing_tabline()
5225 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005226 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00005227 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005228 return 0L;
5229 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00005230 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005231# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005233# ifdef FEAT_GUI_TABLINE
5234 if (gui_mch_showing_tabline()
5235 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
5236 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5237# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 break;
5239 }
5240 break;
5241#endif
5242#if defined(MENUHINTS) && defined(FEAT_MENU)
5243 case WM_MENUSELECT:
5244 if (((UINT) HIWORD(wParam)
5245 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
5246 == MF_HILITE
5247 && (State & CMDLINE) == 0)
5248 {
5249 UINT idButton;
5250 vimmenu_T *pMenu;
5251 static int did_menu_tip = FALSE;
5252
5253 if (did_menu_tip)
5254 {
5255 msg_clr_cmdline();
5256 setcursor();
5257 out_flush();
5258 did_menu_tip = FALSE;
5259 }
5260
5261 idButton = (UINT)LOWORD(wParam);
5262 pMenu = gui_mswin_find_menu(root_menu, idButton);
5263 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
5264 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
5265 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005266 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005268 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 setcursor();
5270 out_flush();
5271 did_menu_tip = TRUE;
5272 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01005273 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 }
5275 break;
5276#endif
5277 case WM_NCHITTEST:
5278 {
5279 LRESULT result;
5280 int x, y;
5281 int xPos = GET_X_LPARAM(lParam);
5282
5283 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
5284 if (result == HTCLIENT)
5285 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005286#ifdef FEAT_GUI_TABLINE
5287 if (gui_mch_showing_tabline())
5288 {
5289 int yPos = GET_Y_LPARAM(lParam);
5290 RECT rct;
5291
5292 /* If the cursor is on the GUI tabline, don't process this
5293 * event */
5294 GetWindowRect(s_textArea, &rct);
5295 if (yPos < rct.top)
5296 return result;
5297 }
5298#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02005299 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 xPos -= x;
5301
5302 if (xPos < 48) /* <VN> TODO should use system metric? */
5303 return HTBOTTOMLEFT;
5304 else
5305 return HTBOTTOMRIGHT;
5306 }
5307 else
5308 return result;
5309 }
5310 /* break; notreached */
5311
5312#ifdef FEAT_MBYTE_IME
5313 case WM_IME_NOTIFY:
5314 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5315 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005316 return 1L;
5317
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 case WM_IME_COMPOSITION:
5319 if (!_OnImeComposition(hwnd, wParam, lParam))
5320 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005321 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322#endif
5323
5324 default:
5325 if (uMsg == msh_msgmousewheel && msh_msgmousewheel != 0)
5326 { /* handle MSH_MOUSEWHEEL messages for Intellimouse */
5327 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01005328 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 }
5330#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005331 else if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 {
5333 _OnFindRepl();
5334 }
5335#endif
5336 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5337 }
5338
Bram Moolenaar2787ab92011-12-14 15:23:59 +01005339 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340}
5341
5342/*
5343 * End of call-back routines
5344 */
5345
5346/* parent window, if specified with -P */
5347HWND vim_parent_hwnd = NULL;
5348
5349 static BOOL CALLBACK
5350FindWindowTitle(HWND hwnd, LPARAM lParam)
5351{
5352 char buf[2048];
5353 char *title = (char *)lParam;
5354
5355 if (GetWindowText(hwnd, buf, sizeof(buf)))
5356 {
5357 if (strstr(buf, title) != NULL)
5358 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005359 /* Found it. Store the window ref. and quit searching if MDI
5360 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005362 if (vim_parent_hwnd != NULL)
5363 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 }
5365 }
5366 return TRUE; /* continue searching */
5367}
5368
5369/*
5370 * Invoked for '-P "title"' argument: search for parent application to open
5371 * our window in.
5372 */
5373 void
5374gui_mch_set_parent(char *title)
5375{
5376 EnumWindows(FindWindowTitle, (LPARAM)title);
5377 if (vim_parent_hwnd == NULL)
5378 {
5379 EMSG2(_("E671: Cannot find window title \"%s\""), title);
5380 mch_exit(2);
5381 }
5382}
5383
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005384#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 static void
5386ole_error(char *arg)
5387{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005388 char buf[IOSIZE];
5389
5390 /* Can't use EMSG() here, we have not finished initialisation yet. */
5391 vim_snprintf(buf, IOSIZE,
5392 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
5393 arg);
5394 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397
5398/*
5399 * Parse the GUI related command-line arguments. Any arguments used are
5400 * deleted from argv, and *argc is decremented accordingly. This is called
5401 * when vim is started, whether or not the GUI has been started.
5402 */
5403 void
5404gui_mch_prepare(int *argc, char **argv)
5405{
5406 int silent = FALSE;
5407 int idx;
5408
5409 /* Check for special OLE command line parameters */
5410 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5411 {
5412 /* Check for a "-silent" argument first. */
5413 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5414 && (argv[2][0] == '-' || argv[2][0] == '/'))
5415 {
5416 silent = TRUE;
5417 idx = 2;
5418 }
5419 else
5420 idx = 1;
5421
5422 /* Register Vim as an OLE Automation server */
5423 if (STRICMP(argv[idx] + 1, "register") == 0)
5424 {
5425#ifdef FEAT_OLE
5426 RegisterMe(silent);
5427 mch_exit(0);
5428#else
5429 if (!silent)
5430 ole_error("register");
5431 mch_exit(2);
5432#endif
5433 }
5434
5435 /* Unregister Vim as an OLE Automation server */
5436 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5437 {
5438#ifdef FEAT_OLE
5439 UnregisterMe(!silent);
5440 mch_exit(0);
5441#else
5442 if (!silent)
5443 ole_error("unregister");
5444 mch_exit(2);
5445#endif
5446 }
5447
5448 /* Ignore an -embedding argument. It is only relevant if the
5449 * application wants to treat the case when it is started manually
5450 * differently from the case where it is started via automation (and
5451 * we don't).
5452 */
5453 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5454 {
5455#ifdef FEAT_OLE
5456 *argc = 1;
5457#else
5458 ole_error("embedding");
5459 mch_exit(2);
5460#endif
5461 }
5462 }
5463
5464#ifdef FEAT_OLE
5465 {
5466 int bDoRestart = FALSE;
5467
5468 InitOLE(&bDoRestart);
5469 /* automatically exit after registering */
5470 if (bDoRestart)
5471 mch_exit(0);
5472 }
5473#endif
5474
5475#ifdef FEAT_NETBEANS_INTG
5476 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005477 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 int arg;
5479
5480 for (arg = 1; arg < *argc; arg++)
5481 if (strncmp("-nb", argv[arg], 3) == 0)
5482 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 netbeansArg = argv[arg];
5484 mch_memmove(&argv[arg], &argv[arg + 1],
5485 (--*argc - arg) * sizeof(char *));
5486 argv[*argc] = NULL;
5487 break; /* enough? */
5488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 }
5490#endif
5491
5492 /* get the OS version info */
5493 os_version.dwOSVersionInfoSize = sizeof(os_version);
5494 GetVersionEx(&os_version); /* this call works on Win32s, Win95 and WinNT */
5495
5496 /* try and load the user32.dll library and get the entry points for
5497 * multi-monitor-support. */
Bram Moolenaarebbcb822010-10-23 14:02:54 +02005498 if ((user32_lib = vimLoadLib("User32.dll")) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 {
5500 pMonitorFromWindow = (TMonitorFromWindow)GetProcAddress(user32_lib,
5501 "MonitorFromWindow");
5502
5503 /* there are ...A and ...W version of GetMonitorInfo - looking at
5504 * winuser.h, they have exactly the same declaration. */
5505 pGetMonitorInfo = (TGetMonitorInfo)GetProcAddress(user32_lib,
5506 "GetMonitorInfoA");
5507 }
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02005508
5509#ifdef FEAT_MBYTE
5510 /* If the OS is Windows NT, use wide functions;
5511 * this enables common dialogs input unicode from IME. */
5512 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
5513 {
5514 pDispatchMessage = DispatchMessageW;
5515 pGetMessage = GetMessageW;
5516 pIsDialogMessage = IsDialogMessageW;
5517 pPeekMessage = PeekMessageW;
5518 }
5519 else
5520 {
5521 pDispatchMessage = DispatchMessageA;
5522 pGetMessage = GetMessageA;
5523 pIsDialogMessage = IsDialogMessageA;
5524 pPeekMessage = PeekMessageA;
5525 }
5526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527}
5528
5529/*
5530 * Initialise the GUI. Create all the windows, set up all the call-backs
5531 * etc.
5532 */
5533 int
5534gui_mch_init(void)
5535{
5536 const char szVimWndClass[] = VIM_CLASS;
5537 const char szTextAreaClass[] = "VimTextArea";
5538 WNDCLASS wndclass;
5539#ifdef FEAT_MBYTE
5540 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005541 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 WNDCLASSW wndclassw;
5543#endif
5544#ifdef GLOBAL_IME
5545 ATOM atom;
5546#endif
5547
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 /* Return here if the window was already opened (happens when
5549 * gui_mch_dialog() is called early). */
5550 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005551 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552
5553 /*
5554 * Load the tearoff bitmap
5555 */
5556#ifdef FEAT_TEAROFF
5557 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
5558#endif
5559
5560 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5561 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5562#ifdef FEAT_MENU
5563 gui.menu_height = 0; /* Windows takes care of this */
5564#endif
5565 gui.border_width = 0;
5566
5567 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5568
5569#ifdef FEAT_MBYTE
5570 /* First try using the wide version, so that we can use any title.
5571 * Otherwise only characters in the active codepage will work. */
5572 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
5573 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005574 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 wndclassw.lpfnWndProc = _WndProc;
5576 wndclassw.cbClsExtra = 0;
5577 wndclassw.cbWndExtra = 0;
5578 wndclassw.hInstance = s_hinst;
5579 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5580 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5581 wndclassw.hbrBackground = s_brush;
5582 wndclassw.lpszMenuName = NULL;
5583 wndclassw.lpszClassName = szVimWndClassW;
5584
5585 if ((
5586#ifdef GLOBAL_IME
5587 atom =
5588#endif
5589 RegisterClassW(&wndclassw)) == 0)
5590 {
5591 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
5592 return FAIL;
5593
5594 /* Must be Windows 98, fall back to non-wide function. */
5595 }
5596 else
5597 wide_WindowProc = TRUE;
5598 }
5599
5600 if (!wide_WindowProc)
5601#endif
5602
5603 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
5604 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005605 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 wndclass.lpfnWndProc = _WndProc;
5607 wndclass.cbClsExtra = 0;
5608 wndclass.cbWndExtra = 0;
5609 wndclass.hInstance = s_hinst;
5610 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
5611 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5612 wndclass.hbrBackground = s_brush;
5613 wndclass.lpszMenuName = NULL;
5614 wndclass.lpszClassName = szVimWndClass;
5615
5616 if ((
5617#ifdef GLOBAL_IME
5618 atom =
5619#endif
5620 RegisterClass(&wndclass)) == 0)
5621 return FAIL;
5622 }
5623
5624 if (vim_parent_hwnd != NULL)
5625 {
5626#ifdef HAVE_TRY_EXCEPT
5627 __try
5628 {
5629#endif
5630 /* Open inside the specified parent window.
5631 * TODO: last argument should point to a CLIENTCREATESTRUCT
5632 * structure. */
5633 s_hwnd = CreateWindowEx(
5634 WS_EX_MDICHILD,
5635 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005636 WS_OVERLAPPEDWINDOW | WS_CHILD
5637 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5639 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5640 100, /* Any value will do */
5641 100, /* Any value will do */
5642 vim_parent_hwnd, NULL,
5643 s_hinst, NULL);
5644#ifdef HAVE_TRY_EXCEPT
5645 }
5646 __except(EXCEPTION_EXECUTE_HANDLER)
5647 {
5648 /* NOP */
5649 }
5650#endif
5651 if (s_hwnd == NULL)
5652 {
5653 EMSG(_("E672: Unable to open window inside MDI application"));
5654 mch_exit(2);
5655 }
5656 }
5657 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005658 {
5659 /* If the provided windowid is not valid reset it to zero, so that it
5660 * is ignored and we open our own window. */
5661 if (IsWindow((HWND)win_socket_id) <= 0)
5662 win_socket_id = 0;
5663
5664 /* Create a window. If win_socket_id is not zero without border and
5665 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00005667 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005668 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5669 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005670 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5671 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5672 100, /* Any value will do */
5673 100, /* Any value will do */
5674 NULL, NULL,
5675 s_hinst, NULL);
5676 if (s_hwnd != NULL && win_socket_id != 0)
5677 {
5678 SetParent(s_hwnd, (HWND)win_socket_id);
5679 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5680 }
5681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682
5683 if (s_hwnd == NULL)
5684 return FAIL;
5685
5686#ifdef GLOBAL_IME
5687 global_ime_init(atom, s_hwnd);
5688#endif
5689#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5690 dyn_imm_load();
5691#endif
5692
5693 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005694#ifdef FEAT_MBYTE
5695 if (wide_WindowProc)
5696 {
5697 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
5698 {
5699 wndclassw.style = CS_OWNDC;
5700 wndclassw.lpfnWndProc = _TextAreaWndProc;
5701 wndclassw.cbClsExtra = 0;
5702 wndclassw.cbWndExtra = 0;
5703 wndclassw.hInstance = s_hinst;
5704 wndclassw.hIcon = NULL;
5705 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5706 wndclassw.hbrBackground = NULL;
5707 wndclassw.lpszMenuName = NULL;
5708 wndclassw.lpszClassName = szTextAreaClassW;
5709
5710 if (RegisterClassW(&wndclassw) == 0)
5711 return FAIL;
5712 }
5713 }
5714 else
5715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
5717 {
5718 wndclass.style = CS_OWNDC;
5719 wndclass.lpfnWndProc = _TextAreaWndProc;
5720 wndclass.cbClsExtra = 0;
5721 wndclass.cbWndExtra = 0;
5722 wndclass.hInstance = s_hinst;
5723 wndclass.hIcon = NULL;
5724 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5725 wndclass.hbrBackground = NULL;
5726 wndclass.lpszMenuName = NULL;
5727 wndclass.lpszClassName = szTextAreaClass;
5728
5729 if (RegisterClass(&wndclass) == 0)
5730 return FAIL;
5731 }
5732 s_textArea = CreateWindowEx(
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005733 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 szTextAreaClass, "Vim text area",
5735 WS_CHILD | WS_VISIBLE, 0, 0,
5736 100, /* Any value will do for now */
5737 100, /* Any value will do for now */
5738 s_hwnd, NULL,
5739 s_hinst, NULL);
5740
5741 if (s_textArea == NULL)
5742 return FAIL;
5743
Bram Moolenaar20321902016-02-17 12:30:17 +01005744#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005745 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5746 {
5747 HANDLE hIcon = NULL;
5748
5749 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005750 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005751 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005752#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005753
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754#ifdef FEAT_MENU
5755 s_menuBar = CreateMenu();
5756#endif
5757 s_hdc = GetDC(s_textArea);
5758
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759#ifdef FEAT_WINDOWS
5760 DragAcceptFiles(s_hwnd, TRUE);
5761#endif
5762
5763 /* Do we need to bother with this? */
5764 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5765
5766 /* Get background/foreground colors from the system */
5767 gui_mch_def_colors();
5768
5769 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5770 * file) */
5771 set_normal_colors();
5772
5773 /*
5774 * Check that none of the colors are the same as the background color.
5775 * Then store the current values as the defaults.
5776 */
5777 gui_check_colors();
5778 gui.def_norm_pixel = gui.norm_pixel;
5779 gui.def_back_pixel = gui.back_pixel;
5780
5781 /* Get the colors for the highlight groups (gui_check_colors() might have
5782 * changed them) */
5783 highlight_gui_started();
5784
5785 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005786 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005788 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789
5790 /*
5791 * Set up for Intellimouse processing
5792 */
5793 init_mouse_wheel();
5794
5795 /*
5796 * compute a couple of metrics used for the dialogs
5797 */
5798 get_dialog_font_metrics();
5799#ifdef FEAT_TOOLBAR
5800 /*
5801 * Create the toolbar
5802 */
5803 initialise_toolbar();
5804#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005805#ifdef FEAT_GUI_TABLINE
5806 /*
5807 * Create the tabline
5808 */
5809 initialise_tabline();
5810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811#ifdef MSWIN_FIND_REPLACE
5812 /*
5813 * Initialise the dialog box stuff
5814 */
5815 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5816
5817 /* Initialise the struct */
5818 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005819 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005821 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5823 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5824 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00005825# if defined(FEAT_MBYTE) && defined(WIN3264)
5826 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
5827 s_findrep_struct_w.lpstrFindWhat =
5828 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5829 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
5830 s_findrep_struct_w.lpstrReplaceWith =
5831 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5832 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
5833 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
5834 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5835# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005838#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005839# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5840/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5841# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005842# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005843# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005844# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005845 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005846 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005847#endif
5848
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005849#ifdef FEAT_RENDER_OPTIONS
5850 if (p_rop)
5851 (void)gui_mch_set_rendering_options(p_rop);
5852#endif
5853
Bram Moolenaar748bf032005-02-02 23:04:36 +00005854theend:
5855 /* Display any pending error messages */
5856 display_errors();
5857
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 return OK;
5859}
5860
5861/*
5862 * Get the size of the screen, taking position on multiple monitors into
5863 * account (if supported).
5864 */
5865 static void
5866get_work_area(RECT *spi_rect)
5867{
5868 _HMONITOR mon;
5869 _MONITORINFO moninfo;
5870
5871 /* use these functions only if available */
5872 if (pMonitorFromWindow != NULL && pGetMonitorInfo != NULL)
5873 {
5874 /* work out which monitor the window is on, and get *it's* work area */
5875 mon = pMonitorFromWindow(s_hwnd, 1 /*MONITOR_DEFAULTTOPRIMARY*/);
5876 if (mon != NULL)
5877 {
5878 moninfo.cbSize = sizeof(_MONITORINFO);
5879 if (pGetMonitorInfo(mon, &moninfo))
5880 {
5881 *spi_rect = moninfo.rcWork;
5882 return;
5883 }
5884 }
5885 }
5886 /* this is the old method... */
5887 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5888}
5889
5890/*
5891 * Set the size of the window to the given width and height in pixels.
5892 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005893/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 void
5895gui_mch_set_shellsize(int width, int height,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005896 int min_width, int min_height, int base_width, int base_height,
5897 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898{
5899 RECT workarea_rect;
5900 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 WINDOWPLACEMENT wndpl;
5902
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005903 /* Try to keep window completely on screen. */
5904 /* Get position of the screen work area. This is the part that is not
5905 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 get_work_area(&workarea_rect);
5907
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005908 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005909 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 wndpl.length = sizeof(WINDOWPLACEMENT);
5911 GetWindowPlacement(s_hwnd, &wndpl);
5912
5913 /* Resizing a maximized window looks very strange, unzoom it first.
5914 * But don't do it when still starting up, it may have been requested in
5915 * the shortcut. */
5916 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5917 {
5918 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5919 /* Need to get the settings of the normal window. */
5920 GetWindowPlacement(s_hwnd, &wndpl);
5921 }
5922
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005924 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005925 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005926 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005927 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 + GetSystemMetrics(SM_CYCAPTION)
5929#ifdef FEAT_MENU
5930 + gui_mswin_get_menu_height(FALSE)
5931#endif
5932 ;
5933
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005934 /* The following should take care of keeping Vim on the same monitor, no
5935 * matter if the secondary monitor is left or right of the primary
5936 * monitor. */
5937 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5938 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005939
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005940 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005941 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005942 && wndpl.rcNormalPosition.right > workarea_rect.right)
5943 OffsetRect(&wndpl.rcNormalPosition,
5944 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005946 if ((direction & RESIZE_HOR)
5947 && wndpl.rcNormalPosition.left < workarea_rect.left)
5948 OffsetRect(&wndpl.rcNormalPosition,
5949 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950
Bram Moolenaarafa24992006-03-27 20:58:26 +00005951 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005952 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5953 OffsetRect(&wndpl.rcNormalPosition,
5954 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005956 if ((direction & RESIZE_VERT)
5957 && wndpl.rcNormalPosition.top < workarea_rect.top)
5958 OffsetRect(&wndpl.rcNormalPosition,
5959 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960
5961 /* set window position - we should use SetWindowPlacement rather than
5962 * SetWindowPos as the MSDN docs say the coord systems returned by
5963 * these two are not compatible. */
5964 SetWindowPlacement(s_hwnd, &wndpl);
5965
5966 SetActiveWindow(s_hwnd);
5967 SetFocus(s_hwnd);
5968
5969#ifdef FEAT_MENU
5970 /* Menu may wrap differently now */
5971 gui_mswin_get_menu_height(!gui.starting);
5972#endif
5973}
5974
5975
5976 void
5977gui_mch_set_scrollbar_thumb(
5978 scrollbar_T *sb,
5979 long val,
5980 long size,
5981 long max)
5982{
5983 SCROLLINFO info;
5984
5985 sb->scroll_shift = 0;
5986 while (max > 32767)
5987 {
5988 max = (max + 1) >> 1;
5989 val >>= 1;
5990 size >>= 1;
5991 ++sb->scroll_shift;
5992 }
5993
5994 if (sb->scroll_shift > 0)
5995 ++size;
5996
5997 info.cbSize = sizeof(info);
5998 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5999 info.nPos = val;
6000 info.nMin = 0;
6001 info.nMax = max;
6002 info.nPage = size;
6003 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
6004}
6005
6006
6007/*
6008 * Set the current text font.
6009 */
6010 void
6011gui_mch_set_font(GuiFont font)
6012{
6013 gui.currFont = font;
6014}
6015
6016
6017/*
6018 * Set the current text foreground color.
6019 */
6020 void
6021gui_mch_set_fg_color(guicolor_T color)
6022{
6023 gui.currFgColor = color;
6024}
6025
6026/*
6027 * Set the current text background color.
6028 */
6029 void
6030gui_mch_set_bg_color(guicolor_T color)
6031{
6032 gui.currBgColor = color;
6033}
6034
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006035/*
6036 * Set the current text special color.
6037 */
6038 void
6039gui_mch_set_sp_color(guicolor_T color)
6040{
6041 gui.currSpColor = color;
6042}
6043
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044#if defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)
6045/*
6046 * Multi-byte handling, originally by Sung-Hoon Baek.
6047 * First static functions (no prototypes generated).
6048 */
6049#ifdef _MSC_VER
6050# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
6051#endif
6052#include <imm.h>
6053
6054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 * handle WM_IME_NOTIFY message
6056 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006057/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 static LRESULT
6059_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData)
6060{
6061 LRESULT lResult = 0;
6062 HIMC hImc;
6063
6064 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
6065 return lResult;
6066 switch (dwCommand)
6067 {
6068 case IMN_SETOPENSTATUS:
6069 if (pImmGetOpenStatus(hImc))
6070 {
6071 pImmSetCompositionFont(hImc, &norm_logfont);
6072 im_set_position(gui.row, gui.col);
6073
6074 /* Disable langmap */
6075 State &= ~LANGMAP;
6076 if (State & INSERT)
6077 {
6078#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
6079 /* Unshown 'keymap' in status lines */
6080 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
6081 {
6082 /* Save cursor position */
6083 int old_row = gui.row;
6084 int old_col = gui.col;
6085
6086 // This must be called here before
6087 // status_redraw_curbuf(), otherwise the mode
6088 // message may appear in the wrong position.
6089 showmode();
6090 status_redraw_curbuf();
6091 update_screen(0);
6092 /* Restore cursor position */
6093 gui.row = old_row;
6094 gui.col = old_col;
6095 }
6096#endif
6097 }
6098 }
6099 gui_update_cursor(TRUE, FALSE);
6100 lResult = 0;
6101 break;
6102 }
6103 pImmReleaseContext(hWnd, hImc);
6104 return lResult;
6105}
6106
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006107/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 static LRESULT
6109_OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param)
6110{
6111 char_u *ret;
6112 int len;
6113
6114 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
6115 return 0;
6116
6117 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
6118 if (ret != NULL)
6119 {
6120 add_to_input_buf_csi(ret, len);
6121 vim_free(ret);
6122 return 1;
6123 }
6124 return 0;
6125}
6126
6127/*
6128 * get the current composition string, in UCS-2; *lenp is the number of
6129 * *lenp is the number of Unicode characters.
6130 */
6131 static short_u *
6132GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
6133{
6134 LONG ret;
6135 LPWSTR wbuf = NULL;
6136 char_u *buf;
6137
6138 if (!pImmGetContext)
6139 return NULL; /* no imm32.dll */
6140
6141 /* Try Unicode; this'll always work on NT regardless of codepage. */
6142 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
6143 if (ret == 0)
6144 return NULL; /* empty */
6145
6146 if (ret > 0)
6147 {
6148 /* Allocate the requested buffer plus space for the NUL character. */
6149 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
6150 if (wbuf != NULL)
6151 {
6152 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
6153 *lenp = ret / sizeof(WCHAR);
6154 }
6155 return (short_u *)wbuf;
6156 }
6157
6158 /* ret < 0; we got an error, so try the ANSI version. This'll work
6159 * on 9x/ME, but only if the codepage happens to be set to whatever
6160 * we're inputting. */
6161 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
6162 if (ret <= 0)
6163 return NULL; /* empty or error */
6164
6165 buf = alloc(ret);
6166 if (buf == NULL)
6167 return NULL;
6168 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
6169
6170 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006171 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 vim_free(buf);
6173
6174 return (short_u *)wbuf;
6175}
6176
6177/*
6178 * void GetResultStr()
6179 *
6180 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
6181 * get complete composition string
6182 */
6183 static char_u *
6184GetResultStr(HWND hwnd, int GCS, int *lenp)
6185{
6186 HIMC hIMC; /* Input context handle. */
6187 short_u *buf = NULL;
6188 char_u *convbuf = NULL;
6189
6190 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
6191 return NULL;
6192
6193 /* Reads in the composition string. */
6194 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
6195 if (buf == NULL)
6196 return NULL;
6197
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006198 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 pImmReleaseContext(hwnd, hIMC);
6200 vim_free(buf);
6201 return convbuf;
6202}
6203#endif
6204
6205/* For global functions we need prototypes. */
6206#if (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) || defined(PROTO)
6207
6208/*
6209 * set font to IM.
6210 */
6211 void
6212im_set_font(LOGFONT *lf)
6213{
6214 HIMC hImc;
6215
6216 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6217 {
6218 pImmSetCompositionFont(hImc, lf);
6219 pImmReleaseContext(s_hwnd, hImc);
6220 }
6221}
6222
6223/*
6224 * Notify cursor position to IM.
6225 */
6226 void
6227im_set_position(int row, int col)
6228{
6229 HIMC hImc;
6230
6231 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6232 {
6233 COMPOSITIONFORM cfs;
6234
6235 cfs.dwStyle = CFS_POINT;
6236 cfs.ptCurrentPos.x = FILL_X(col);
6237 cfs.ptCurrentPos.y = FILL_Y(row);
6238 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
6239 pImmSetCompositionWindow(hImc, &cfs);
6240
6241 pImmReleaseContext(s_hwnd, hImc);
6242 }
6243}
6244
6245/*
6246 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6247 */
6248 void
6249im_set_active(int active)
6250{
6251 HIMC hImc;
6252 static HIMC hImcOld = (HIMC)0;
6253
6254 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
6255 {
6256 if (p_imdisable)
6257 {
6258 if (hImcOld == (HIMC)0)
6259 {
6260 hImcOld = pImmGetContext(s_hwnd);
6261 if (hImcOld)
6262 pImmAssociateContext(s_hwnd, (HIMC)0);
6263 }
6264 active = FALSE;
6265 }
6266 else if (hImcOld != (HIMC)0)
6267 {
6268 pImmAssociateContext(s_hwnd, hImcOld);
6269 hImcOld = (HIMC)0;
6270 }
6271
6272 hImc = pImmGetContext(s_hwnd);
6273 if (hImc)
6274 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006275 /*
6276 * for Korean ime
6277 */
6278 HKL hKL = GetKeyboardLayout(0);
6279
6280 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6281 {
6282 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6283 static BOOL bSaved = FALSE;
6284
6285 if (active)
6286 {
6287 /* if we have a saved conversion status, restore it */
6288 if (bSaved)
6289 pImmSetConversionStatus(hImc, dwConversionSaved,
6290 dwSentenceSaved);
6291 bSaved = FALSE;
6292 }
6293 else
6294 {
6295 /* save conversion status and disable korean */
6296 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6297 &dwSentenceSaved))
6298 {
6299 bSaved = TRUE;
6300 pImmSetConversionStatus(hImc,
6301 dwConversionSaved & ~(IME_CMODE_NATIVE
6302 | IME_CMODE_FULLSHAPE),
6303 dwSentenceSaved);
6304 }
6305 }
6306 }
6307
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 pImmSetOpenStatus(hImc, active);
6309 pImmReleaseContext(s_hwnd, hImc);
6310 }
6311 }
6312}
6313
6314/*
6315 * Get IM status. When IM is on, return not 0. Else return 0.
6316 */
6317 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006318im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319{
6320 int status = 0;
6321 HIMC hImc;
6322
6323 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6324 {
6325 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6326 pImmReleaseContext(s_hwnd, hImc);
6327 }
6328 return status;
6329}
6330
6331#endif /* FEAT_MBYTE && FEAT_MBYTE_IME */
6332
6333#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
6334/* Win32 with GLOBAL IME */
6335
6336/*
6337 * Notify cursor position to IM.
6338 */
6339 void
6340im_set_position(int row, int col)
6341{
6342 /* Win32 with GLOBAL IME */
6343 POINT p;
6344
6345 p.x = FILL_X(col);
6346 p.y = FILL_Y(row);
6347 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
6348 global_ime_set_position(&p);
6349}
6350
6351/*
6352 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6353 */
6354 void
6355im_set_active(int active)
6356{
6357 global_ime_set_status(active);
6358}
6359
6360/*
6361 * Get IM status. When IM is on, return not 0. Else return 0.
6362 */
6363 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006364im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365{
6366 return global_ime_get_status();
6367}
6368#endif
6369
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006370#ifdef FEAT_MBYTE
6371/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006372 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006373 */
6374 static void
6375latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6376{
6377 int c;
6378
Bram Moolenaarca003e12006-03-17 23:19:38 +00006379 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006380 {
6381 c = *text++;
6382 switch (c)
6383 {
6384 case 0xa4: c = 0x20ac; break; /* euro */
6385 case 0xa6: c = 0x0160; break; /* S hat */
6386 case 0xa8: c = 0x0161; break; /* S -hat */
6387 case 0xb4: c = 0x017d; break; /* Z hat */
6388 case 0xb8: c = 0x017e; break; /* Z -hat */
6389 case 0xbc: c = 0x0152; break; /* OE */
6390 case 0xbd: c = 0x0153; break; /* oe */
6391 case 0xbe: c = 0x0178; break; /* Y */
6392 }
6393 *unicodebuf++ = c;
6394 }
6395}
6396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397
6398#ifdef FEAT_RIGHTLEFT
6399/*
6400 * What is this for? In the case where you are using Win98 or Win2K or later,
6401 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6402 * reverses the string sent to the TextOut... family. This sucks, because we
6403 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6404 * way to tell Windblows not to do this!
6405 *
6406 * The short of it is that this 'RevOut' only gets called if you are running
6407 * one of the new, "improved" MS OSes, and only if you are running in
6408 * 'rightleft' mode. It makes display take *slightly* longer, but not
6409 * noticeably so.
6410 */
6411 static void
6412RevOut( HDC s_hdc,
6413 int col,
6414 int row,
6415 UINT foptions,
6416 CONST RECT *pcliprect,
6417 LPCTSTR text,
6418 UINT len,
6419 CONST INT *padding)
6420{
6421 int ix;
6422 static int special = -1;
6423
6424 if (special == -1)
6425 {
6426 /* Check windows version: special treatment is needed if it is NT 5 or
6427 * Win98 or higher. */
6428 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
6429 && os_version.dwMajorVersion >= 5)
6430 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
6431 && (os_version.dwMajorVersion > 4
6432 || (os_version.dwMajorVersion == 4
6433 && os_version.dwMinorVersion > 0))))
6434 special = 1;
6435 else
6436 special = 0;
6437 }
6438
6439 if (special)
6440 for (ix = 0; ix < (int)len; ++ix)
6441 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6442 pcliprect, text + ix, 1, padding);
6443 else
6444 ExtTextOut(s_hdc, col, row, foptions, pcliprect, text, len, padding);
6445}
6446#endif
6447
6448 void
6449gui_mch_draw_string(
6450 int row,
6451 int col,
6452 char_u *text,
6453 int len,
6454 int flags)
6455{
6456 static int *padding = NULL;
6457 static int pad_size = 0;
6458 int i;
6459 const RECT *pcliprect = NULL;
6460 UINT foptions = 0;
6461#ifdef FEAT_MBYTE
6462 static WCHAR *unicodebuf = NULL;
6463 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006464 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 int n = 0;
6466#endif
6467 HPEN hpen, old_pen;
6468 int y;
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006469#ifdef FEAT_DIRECTX
6470 int font_is_ttf_or_vector = 0;
6471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 /*
6474 * Italic and bold text seems to have an extra row of pixels at the bottom
6475 * (below where the bottom of the character should be). If we draw the
6476 * characters with a solid background, the top row of pixels in the
6477 * character below will be overwritten. We can fix this by filling in the
6478 * background ourselves, to the correct character proportions, and then
6479 * writing the character in transparent mode. Still have a problem when
6480 * the character is "_", which gets written on to the character below.
6481 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6482 * pixel in their slots, which fixes the problem with the bottom row of
6483 * pixels. We still need this code because otherwise the top row of pixels
6484 * becomes a problem. - webb.
6485 */
6486 static HBRUSH hbr_cache[2] = {NULL, NULL};
6487 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6488 static int brush_lru = 0;
6489 HBRUSH hbr;
6490 RECT rc;
6491
6492 if (!(flags & DRAW_TRANSP))
6493 {
6494 /*
6495 * Clear background first.
6496 * Note: FillRect() excludes right and bottom of rectangle.
6497 */
6498 rc.left = FILL_X(col);
6499 rc.top = FILL_Y(row);
6500#ifdef FEAT_MBYTE
6501 if (has_mbyte)
6502 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006504 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 }
6506 else
6507#endif
6508 rc.right = FILL_X(col + len);
6509 rc.bottom = FILL_Y(row + 1);
6510
6511 /* Cache the created brush, that saves a lot of time. We need two:
6512 * one for cursor background and one for the normal background. */
6513 if (gui.currBgColor == brush_color[0])
6514 {
6515 hbr = hbr_cache[0];
6516 brush_lru = 1;
6517 }
6518 else if (gui.currBgColor == brush_color[1])
6519 {
6520 hbr = hbr_cache[1];
6521 brush_lru = 0;
6522 }
6523 else
6524 {
6525 if (hbr_cache[brush_lru] != NULL)
6526 DeleteBrush(hbr_cache[brush_lru]);
6527 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6528 brush_color[brush_lru] = gui.currBgColor;
6529 hbr = hbr_cache[brush_lru];
6530 brush_lru = !brush_lru;
6531 }
6532 FillRect(s_hdc, &rc, hbr);
6533
6534 SetBkMode(s_hdc, TRANSPARENT);
6535
6536 /*
6537 * When drawing block cursor, prevent inverted character spilling
6538 * over character cell (can happen with bold/italic)
6539 */
6540 if (flags & DRAW_CURSOR)
6541 {
6542 pcliprect = &rc;
6543 foptions = ETO_CLIPPED;
6544 }
6545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 SetTextColor(s_hdc, gui.currFgColor);
6547 SelectFont(s_hdc, gui.currFont);
6548
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006549#ifdef FEAT_DIRECTX
6550 if (IS_ENABLE_DIRECTX())
6551 {
6552 TEXTMETRIC tm;
6553
6554 GetTextMetrics(s_hdc, &tm);
6555 if (tm.tmPitchAndFamily & (TMPF_TRUETYPE | TMPF_VECTOR))
6556 {
6557 font_is_ttf_or_vector = 1;
6558 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
6559 }
6560 }
6561#endif
6562
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6564 {
6565 vim_free(padding);
6566 pad_size = Columns;
6567
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006568 /* Don't give an out-of-memory message here, it would call us
6569 * recursively. */
6570 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 if (padding != NULL)
6572 for (i = 0; i < pad_size; i++)
6573 padding[i] = gui.char_width;
6574 }
6575
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 /*
6577 * We have to provide the padding argument because italic and bold versions
6578 * of fixed-width fonts are often one pixel or so wider than their normal
6579 * versions.
6580 * No check for DRAW_BOLD, Windows will have done it already.
6581 */
6582
6583#ifdef FEAT_MBYTE
6584 /* Check if there are any UTF-8 characters. If not, use normal text
6585 * output to speed up output. */
6586 if (enc_utf8)
6587 for (n = 0; n < len; ++n)
6588 if (text[n] >= 0x80)
6589 break;
6590
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006591#if defined(FEAT_DIRECTX)
6592 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6593 * required that unicode drawing routine, currently. So this forces it
6594 * enabled. */
6595 if (enc_utf8 && IS_ENABLE_DIRECTX())
6596 n = 0; /* Keep n < len, to enter block for unicode. */
6597#endif
6598
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006600 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006602 if ((enc_utf8
6603 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6604 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 && (unicodebuf == NULL || len > unibuflen))
6606 {
6607 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006608 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609
6610 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006611 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612
6613 unibuflen = len;
6614 }
6615
6616 if (enc_utf8 && n < len && unicodebuf != NULL)
6617 {
6618 /* Output UTF-8 characters. Caller has already separated
6619 * composing characters. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006620 int i;
6621 int wlen; /* string length in words */
6622 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 int cells; /* cell width of string up to composing char */
6624 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006625 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006627 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006628 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006630 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006632 c = utf_ptr2char(text + i);
6633 if (c >= 0x10000)
6634 {
6635 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006636 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6637 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006638 }
6639 else
6640 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006641 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006642 }
6643 cw = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 if (cw > 2) /* don't use 4 for unprintable char */
6645 cw = 1;
6646 if (unicodepdy != NULL)
6647 {
6648 /* Use unicodepdy to make characters fit as we expect, even
6649 * when the font uses different widths (e.g., bold character
6650 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006651 if (c >= 0x10000)
6652 {
6653 unicodepdy[wlen - 2] = cw * gui.char_width;
6654 unicodepdy[wlen - 1] = 0;
6655 }
6656 else
6657 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 }
6659 cells += cw;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006660 i += utfc_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006661 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006663#if defined(FEAT_DIRECTX)
6664 if (IS_ENABLE_DIRECTX() && font_is_ttf_or_vector)
6665 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006666 /* Add one to "cells" for italics. */
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006667 DWriteContext_DrawText(s_dwc, s_hdc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006668 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006669 gui.char_width, gui.currFgColor);
6670 }
6671 else
6672#endif
6673 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6674 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 len = cells; /* used for underlining */
6676 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006677 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 {
6679 /* If we want to display codepage data, and the current CP is not the
6680 * ANSI one, we need to go via Unicode. */
6681 if (unicodebuf != NULL)
6682 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006683 if (enc_latin9)
6684 latin9_to_ucs(text, len, unicodebuf);
6685 else
6686 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 MB_PRECOMPOSED,
6688 (char *)text, len,
6689 (LPWSTR)unicodebuf, unibuflen);
6690 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006691 {
6692 /* Use unicodepdy to make characters fit as we expect, even
6693 * when the font uses different widths (e.g., bold character
6694 * is wider). */
6695 if (unicodepdy != NULL)
6696 {
6697 int i;
6698 int cw;
6699
6700 for (i = 0; i < len; ++i)
6701 {
6702 cw = utf_char2cells(unicodebuf[i]);
6703 if (cw > 2)
6704 cw = 1;
6705 unicodepdy[i] = cw * gui.char_width;
6706 }
6707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006709 foptions, pcliprect, unicodebuf, len, unicodepdy);
6710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 }
6712 }
6713 else
6714#endif
6715 {
6716#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006717 /* Windows will mess up RL text, so we have to draw it character by
6718 * character. Only do this if RL is on, since it's slow. */
6719 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6721 foptions, pcliprect, (char *)text, len, padding);
6722 else
6723#endif
6724 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6725 foptions, pcliprect, (char *)text, len, padding);
6726 }
6727
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006728 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 if (flags & DRAW_UNDERL)
6730 {
6731 hpen = CreatePen(PS_SOLID, 1, gui.currFgColor);
6732 old_pen = SelectObject(s_hdc, hpen);
6733 /* When p_linespace is 0, overwrite the bottom row of pixels.
6734 * Otherwise put the line just below the character. */
6735 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 if (p_linespace > 1)
6737 y -= p_linespace - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 MoveToEx(s_hdc, FILL_X(col), y, NULL);
6739 /* Note: LineTo() excludes the last pixel in the line. */
6740 LineTo(s_hdc, FILL_X(col + len), y);
6741 DeleteObject(SelectObject(s_hdc, old_pen));
6742 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006743
6744 /* Undercurl */
6745 if (flags & DRAW_UNDERC)
6746 {
6747 int x;
6748 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006749 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006750
6751 y = FILL_Y(row + 1) - 1;
6752 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6753 {
6754 offset = val[x % 8];
6755 SetPixel(s_hdc, x, y - offset, gui.currSpColor);
6756 }
6757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758}
6759
6760
6761/*
6762 * Output routines.
6763 */
6764
6765/* Flush any output to the screen */
6766 void
6767gui_mch_flush(void)
6768{
6769# if defined(__BORLANDC__)
6770 /*
6771 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6772 * prototype declaration.
6773 * The compiler complains if __stdcall is not used in both declarations.
6774 */
6775 BOOL __stdcall GdiFlush(void);
6776# endif
6777
6778 GdiFlush();
6779}
6780
6781 static void
6782clear_rect(RECT *rcp)
6783{
6784 HBRUSH hbr;
6785
6786 hbr = CreateSolidBrush(gui.back_pixel);
6787 FillRect(s_hdc, rcp, hbr);
6788 DeleteBrush(hbr);
6789}
6790
6791
Bram Moolenaarc716c302006-01-21 22:12:51 +00006792 void
6793gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6794{
6795 RECT workarea_rect;
6796
6797 get_work_area(&workarea_rect);
6798
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006799 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006800 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006801 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006802
6803 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6804 * the menubar for MSwin, we subtract it from the screen height, so that
6805 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006806 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006807 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006808 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006809 - GetSystemMetrics(SM_CYCAPTION)
6810#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006811 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006812#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006813 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006814}
6815
6816
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817#if defined(FEAT_MENU) || defined(PROTO)
6818/*
6819 * Add a sub menu to the menu bar.
6820 */
6821 void
6822gui_mch_add_menu(
6823 vimmenu_T *menu,
6824 int pos)
6825{
6826 vimmenu_T *parent = menu->parent;
6827
6828 menu->submenu_id = CreatePopupMenu();
6829 menu->id = s_menu_id++;
6830
6831 if (menu_is_menubar(menu->name))
6832 {
6833 if (is_winnt_3())
6834 {
6835 InsertMenu((parent == NULL) ? s_menuBar : parent->submenu_id,
6836 (UINT)pos, MF_POPUP | MF_STRING | MF_BYPOSITION,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006837 (long_u)menu->submenu_id, (LPCTSTR) menu->name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 }
6839 else
6840 {
6841#ifdef FEAT_MBYTE
6842 WCHAR *wn = NULL;
6843 int n;
6844
6845 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6846 {
6847 /* 'encoding' differs from active codepage: convert menu name
6848 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006849 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 if (wn != NULL)
6851 {
6852 MENUITEMINFOW infow;
6853
6854 infow.cbSize = sizeof(infow);
6855 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6856 | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006857 infow.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 infow.wID = menu->id;
6859 infow.fType = MFT_STRING;
6860 infow.dwTypeData = wn;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006861 infow.cch = (UINT)wcslen(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 infow.hSubMenu = menu->submenu_id;
6863 n = InsertMenuItemW((parent == NULL)
6864 ? s_menuBar : parent->submenu_id,
6865 (UINT)pos, TRUE, &infow);
6866 vim_free(wn);
6867 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
6868 /* Failed, try using non-wide function. */
6869 wn = NULL;
6870 }
6871 }
6872
6873 if (wn == NULL)
6874#endif
6875 {
6876 MENUITEMINFO info;
6877
6878 info.cbSize = sizeof(info);
6879 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006880 info.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 info.wID = menu->id;
6882 info.fType = MFT_STRING;
6883 info.dwTypeData = (LPTSTR)menu->name;
6884 info.cch = (UINT)STRLEN(menu->name);
6885 info.hSubMenu = menu->submenu_id;
6886 InsertMenuItem((parent == NULL)
6887 ? s_menuBar : parent->submenu_id,
6888 (UINT)pos, TRUE, &info);
6889 }
6890 }
6891 }
6892
6893 /* Fix window size if menu may have wrapped */
6894 if (parent == NULL)
6895 gui_mswin_get_menu_height(!gui.starting);
6896#ifdef FEAT_TEAROFF
6897 else if (IsWindow(parent->tearoff_handle))
6898 rebuild_tearoff(parent);
6899#endif
6900}
6901
6902 void
6903gui_mch_show_popupmenu(vimmenu_T *menu)
6904{
6905 POINT mp;
6906
6907 (void)GetCursorPos((LPPOINT)&mp);
6908 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6909}
6910
6911 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006912gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913{
6914 vimmenu_T *menu = gui_find_menu(path_name);
6915
6916 if (menu != NULL)
6917 {
6918 POINT p;
6919
6920 /* Find the position of the current cursor */
6921 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006922 if (mouse_pos)
6923 {
6924 int mx, my;
6925
6926 gui_mch_getmouse(&mx, &my);
6927 p.x += mx;
6928 p.y += my;
6929 }
6930 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 {
6932 p.x += TEXT_X(W_WINCOL(curwin) + curwin->w_wcol + 1);
6933 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6934 }
6935 msg_scroll = FALSE;
6936 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6937 }
6938}
6939
6940#if defined(FEAT_TEAROFF) || defined(PROTO)
6941/*
6942 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6943 * create it as a pseudo-"tearoff menu".
6944 */
6945 void
6946gui_make_tearoff(char_u *path_name)
6947{
6948 vimmenu_T *menu = gui_find_menu(path_name);
6949
6950 /* Found the menu, so tear it off. */
6951 if (menu != NULL)
6952 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6953}
6954#endif
6955
6956/*
6957 * Add a menu item to a menu
6958 */
6959 void
6960gui_mch_add_menu_item(
6961 vimmenu_T *menu,
6962 int idx)
6963{
6964 vimmenu_T *parent = menu->parent;
6965
6966 menu->id = s_menu_id++;
6967 menu->submenu_id = NULL;
6968
6969#ifdef FEAT_TEAROFF
6970 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6971 {
6972 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6973 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6974 }
6975 else
6976#endif
6977#ifdef FEAT_TOOLBAR
6978 if (menu_is_toolbar(parent->name))
6979 {
6980 TBBUTTON newtb;
6981
6982 vim_memset(&newtb, 0, sizeof(newtb));
6983 if (menu_is_separator(menu->name))
6984 {
6985 newtb.iBitmap = 0;
6986 newtb.fsStyle = TBSTYLE_SEP;
6987 }
6988 else
6989 {
6990 newtb.iBitmap = get_toolbar_bitmap(menu);
6991 newtb.fsStyle = TBSTYLE_BUTTON;
6992 }
6993 newtb.idCommand = menu->id;
6994 newtb.fsState = TBSTATE_ENABLED;
6995 newtb.iString = 0;
6996 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6997 (LPARAM)&newtb);
6998 menu->submenu_id = (HMENU)-1;
6999 }
7000 else
7001#endif
7002 {
7003#ifdef FEAT_MBYTE
7004 WCHAR *wn = NULL;
7005 int n;
7006
7007 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
7008 {
7009 /* 'encoding' differs from active codepage: convert menu item name
7010 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007011 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 if (wn != NULL)
7013 {
7014 n = InsertMenuW(parent->submenu_id, (UINT)idx,
7015 (menu_is_separator(menu->name)
7016 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
7017 (UINT)menu->id, wn);
7018 vim_free(wn);
7019 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
7020 /* Failed, try using non-wide function. */
7021 wn = NULL;
7022 }
7023 }
7024 if (wn == NULL)
7025#endif
7026 InsertMenu(parent->submenu_id, (UINT)idx,
7027 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
7028 | MF_BYPOSITION,
7029 (UINT)menu->id, (LPCTSTR)menu->name);
7030#ifdef FEAT_TEAROFF
7031 if (IsWindow(parent->tearoff_handle))
7032 rebuild_tearoff(parent);
7033#endif
7034 }
7035}
7036
7037/*
7038 * Destroy the machine specific menu widget.
7039 */
7040 void
7041gui_mch_destroy_menu(vimmenu_T *menu)
7042{
7043#ifdef FEAT_TOOLBAR
7044 /*
7045 * is this a toolbar button?
7046 */
7047 if (menu->submenu_id == (HMENU)-1)
7048 {
7049 int iButton;
7050
7051 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
7052 (WPARAM)menu->id, 0);
7053 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
7054 }
7055 else
7056#endif
7057 {
7058 if (menu->parent != NULL
7059 && menu_is_popup(menu->parent->dname)
7060 && menu->parent->submenu_id != NULL)
7061 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
7062 else
7063 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
7064 if (menu->submenu_id != NULL)
7065 DestroyMenu(menu->submenu_id);
7066#ifdef FEAT_TEAROFF
7067 if (IsWindow(menu->tearoff_handle))
7068 DestroyWindow(menu->tearoff_handle);
7069 if (menu->parent != NULL
7070 && menu->parent->children != NULL
7071 && IsWindow(menu->parent->tearoff_handle))
7072 {
7073 /* This menu must not show up when rebuilding the tearoff window. */
7074 menu->modes = 0;
7075 rebuild_tearoff(menu->parent);
7076 }
7077#endif
7078 }
7079}
7080
7081#ifdef FEAT_TEAROFF
7082 static void
7083rebuild_tearoff(vimmenu_T *menu)
7084{
7085 /*hackish*/
7086 char_u tbuf[128];
7087 RECT trect;
7088 RECT rct;
7089 RECT roct;
7090 int x, y;
7091
7092 HWND thwnd = menu->tearoff_handle;
7093
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007094 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 if (GetWindowRect(thwnd, &trect)
7096 && GetWindowRect(s_hwnd, &rct)
7097 && GetClientRect(s_hwnd, &roct))
7098 {
7099 x = trect.left - rct.left;
7100 y = (trect.top - rct.bottom + roct.bottom);
7101 }
7102 else
7103 {
7104 x = y = 0xffffL;
7105 }
7106 DestroyWindow(thwnd);
7107 if (menu->children != NULL)
7108 {
7109 gui_mch_tearoff(tbuf, menu, x, y);
7110 if (IsWindow(menu->tearoff_handle))
7111 (void) SetWindowPos(menu->tearoff_handle,
7112 NULL,
7113 (int)trect.left,
7114 (int)trect.top,
7115 0, 0,
7116 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
7117 }
7118}
7119#endif /* FEAT_TEAROFF */
7120
7121/*
7122 * Make a menu either grey or not grey.
7123 */
7124 void
7125gui_mch_menu_grey(
7126 vimmenu_T *menu,
7127 int grey)
7128{
7129#ifdef FEAT_TOOLBAR
7130 /*
7131 * is this a toolbar button?
7132 */
7133 if (menu->submenu_id == (HMENU)-1)
7134 {
7135 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
7136 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
7137 }
7138 else
7139#endif
7140 if (grey)
7141 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_GRAYED);
7142 else
7143 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
7144
7145#ifdef FEAT_TEAROFF
7146 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
7147 {
7148 WORD menuID;
7149 HWND menuHandle;
7150
7151 /*
7152 * A tearoff button has changed state.
7153 */
7154 if (menu->children == NULL)
7155 menuID = (WORD)(menu->id);
7156 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007157 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
7159 if (menuHandle)
7160 EnableWindow(menuHandle, !grey);
7161
7162 }
7163#endif
7164}
7165
7166#endif /* FEAT_MENU */
7167
7168
7169/* define some macros used to make the dialogue creation more readable */
7170
7171#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
7172#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007173#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174
7175#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
7176/*
7177 * stuff for dialogs
7178 */
7179
7180/*
7181 * The callback routine used by all the dialogs. Very simple. First,
7182 * acknowledges the INITDIALOG message so that Windows knows to do standard
7183 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
7184 * pressed, return that button's ID - IDCANCEL (2), which is the button's
7185 * number.
7186 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007187/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 static LRESULT CALLBACK
7189dialog_callback(
7190 HWND hwnd,
7191 UINT message,
7192 WPARAM wParam,
7193 LPARAM lParam)
7194{
7195 if (message == WM_INITDIALOG)
7196 {
7197 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
7198 /* Set focus to the dialog. Set the default button, if specified. */
7199 (void)SetFocus(hwnd);
7200 if (dialog_default_button > IDCANCEL)
7201 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007202 else
7203 /* We don't have a default, set focus on another element of the
7204 * dialog window, probably the icon */
7205 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 return FALSE;
7207 }
7208
7209 if (message == WM_COMMAND)
7210 {
7211 int button = LOWORD(wParam);
7212
7213 /* Don't end the dialog if something was selected that was
7214 * not a button.
7215 */
7216 if (button >= DLG_NONBUTTON_CONTROL)
7217 return TRUE;
7218
7219 /* If the edit box exists, copy the string. */
7220 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007221 {
7222# if defined(FEAT_MBYTE) && defined(WIN3264)
7223 /* If the OS is Windows NT, and 'encoding' differs from active
7224 * codepage: use wide function and convert text. */
7225 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
7226 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007227 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007228 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
7229 char_u *p;
7230
7231 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
7232 p = utf16_to_enc(wp, NULL);
7233 vim_strncpy(s_textfield, p, IOSIZE);
7234 vim_free(p);
7235 vim_free(wp);
7236 }
7237 else
7238# endif
7239 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007240 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242
7243 /*
7244 * Need to check for IDOK because if the user just hits Return to
7245 * accept the default value, some reason this is what we get.
7246 */
7247 if (button == IDOK)
7248 {
7249 if (dialog_default_button > IDCANCEL)
7250 EndDialog(hwnd, dialog_default_button);
7251 }
7252 else
7253 EndDialog(hwnd, button - IDCANCEL);
7254 return TRUE;
7255 }
7256
7257 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7258 {
7259 EndDialog(hwnd, 0);
7260 return TRUE;
7261 }
7262 return FALSE;
7263}
7264
7265/*
7266 * Create a dialog dynamically from the parameter strings.
7267 * type = type of dialog (question, alert, etc.)
7268 * title = dialog title. may be NULL for default title.
7269 * message = text to display. Dialog sizes to accommodate it.
7270 * buttons = '\n' separated list of button captions, default first.
7271 * dfltbutton = number of default button.
7272 *
7273 * This routine returns 1 if the first button is pressed,
7274 * 2 for the second, etc.
7275 *
7276 * 0 indicates Esc was pressed.
7277 * -1 for unexpected error
7278 *
7279 * If stubbing out this fn, return 1.
7280 */
7281
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007282static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283{
7284 "IDR_VIM",
7285 "IDR_VIM_ERROR",
7286 "IDR_VIM_ALERT",
7287 "IDR_VIM_INFO",
7288 "IDR_VIM_QUESTION"
7289};
7290
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 int
7292gui_mch_dialog(
7293 int type,
7294 char_u *title,
7295 char_u *message,
7296 char_u *buttons,
7297 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007298 char_u *textfield,
7299 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300{
7301 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007302 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 int numButtons;
7304 int *buttonWidths, *buttonPositions;
7305 int buttonYpos;
7306 int nchar, i;
7307 DWORD lStyle;
7308 int dlgwidth = 0;
7309 int dlgheight;
7310 int editboxheight;
7311 int horizWidth = 0;
7312 int msgheight;
7313 char_u *pstart;
7314 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007315 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 char_u *tbuffer;
7317 RECT rect;
7318 HWND hwnd;
7319 HDC hdc;
7320 HFONT font, oldFont;
7321 TEXTMETRIC fontInfo;
7322 int fontHeight;
7323 int textWidth, minButtonWidth, messageWidth;
7324 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007325 int maxDialogHeight;
7326 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 int vertical;
7328 int dlgPaddingX;
7329 int dlgPaddingY;
7330#ifdef USE_SYSMENU_FONT
7331 LOGFONT lfSysmenu;
7332 int use_lfSysmenu = FALSE;
7333#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007334 garray_T ga;
7335 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336
7337#ifndef NO_CONSOLE
7338 /* Don't output anything in silent mode ("ex -s") */
7339 if (silent_mode)
7340 return dfltbutton; /* return default option */
7341#endif
7342
Bram Moolenaar748bf032005-02-02 23:04:36 +00007343 if (s_hwnd == NULL)
7344 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345
7346 if ((type < 0) || (type > VIM_LAST_TYPE))
7347 type = 0;
7348
7349 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007350 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007351 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007352 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353
7354 if (p == NULL)
7355 return -1;
7356
7357 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007358 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7360 * const.
7361 */
7362 tbuffer = vim_strsave(buttons);
7363 if (tbuffer == NULL)
7364 return -1;
7365
7366 --dfltbutton; /* Change from one-based to zero-based */
7367
7368 /* Count buttons */
7369 numButtons = 1;
7370 for (i = 0; tbuffer[i] != '\0'; i++)
7371 {
7372 if (tbuffer[i] == DLG_BUTTON_SEP)
7373 numButtons++;
7374 }
7375 if (dfltbutton >= numButtons)
7376 dfltbutton = -1;
7377
7378 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007379 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 if (buttonWidths == NULL)
7381 return -1;
7382
7383 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007384 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 if (buttonPositions == NULL)
7386 return -1;
7387
7388 /*
7389 * Calculate how big the dialog must be.
7390 */
7391 hwnd = GetDesktopWindow();
7392 hdc = GetWindowDC(hwnd);
7393#ifdef USE_SYSMENU_FONT
7394 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7395 {
7396 font = CreateFontIndirect(&lfSysmenu);
7397 use_lfSysmenu = TRUE;
7398 }
7399 else
7400#endif
7401 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7402 VARIABLE_PITCH , DLG_FONT_NAME);
7403 if (s_usenewlook)
7404 {
7405 oldFont = SelectFont(hdc, font);
7406 dlgPaddingX = DLG_PADDING_X;
7407 dlgPaddingY = DLG_PADDING_Y;
7408 }
7409 else
7410 {
7411 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7412 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7413 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7414 }
7415 GetTextMetrics(hdc, &fontInfo);
7416 fontHeight = fontInfo.tmHeight;
7417
7418 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007419 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420
7421 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007422 if (s_hwnd == NULL)
7423 {
7424 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425
Bram Moolenaarc716c302006-01-21 22:12:51 +00007426 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007427 get_work_area(&workarea_rect);
7428 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7429 if (maxDialogWidth > 600)
7430 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007431 /* Leave some room for the taskbar. */
7432 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007433 }
7434 else
7435 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02007436 /* Use our own window for the size, unless it's very small. */
7437 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007438 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02007439 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007440 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007441 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
7442 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007443
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007444 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007445 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007446 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02007447 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007448 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
7449 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
7450 }
7451
7452 /* Set dlgwidth to width of message.
7453 * Copy the message into "ga", changing NL to CR-NL and inserting line
7454 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 pstart = message;
7456 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007457 msgheight = 0;
7458 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 do
7460 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007461 msgheight += fontHeight; /* at least one line */
7462
7463 /* Need to figure out where to break the string. The system does it
7464 * at a word boundary, which would mean we can't compute the number of
7465 * wrapped lines. */
7466 textWidth = 0;
7467 last_white = NULL;
7468 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007469 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007470#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007471 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007472#else
7473 l = 1;
7474#endif
7475 if (l == 1 && vim_iswhite(*pend)
7476 && textWidth > maxDialogWidth * 3 / 4)
7477 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007478 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007479 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007480 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007481 /* Line will wrap. */
7482 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007483 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007484 textWidth = 0;
7485
7486 if (last_white != NULL)
7487 {
7488 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007489 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007490 pend = last_white + 1;
7491 last_white = NULL;
7492 }
7493 ga_append(&ga, '\r');
7494 ga_append(&ga, '\n');
7495 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007496 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007497
7498 while (--l >= 0)
7499 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007500 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007501 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007503
7504 ga_append(&ga, '\r');
7505 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 pstart = pend + 1;
7507 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007508
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007509 if (ga.ga_data != NULL)
7510 message = ga.ga_data;
7511
Bram Moolenaar748bf032005-02-02 23:04:36 +00007512 messageWidth += 10; /* roundoff space */
7513
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02007515 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7516 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517
7518 if (msgheight < DLG_ICON_HEIGHT)
7519 msgheight = DLG_ICON_HEIGHT;
7520
7521 /*
7522 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007523 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007525 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 if (!vertical)
7527 {
7528 // Place buttons horizontally if they fit.
7529 horizWidth = dlgPaddingX;
7530 pstart = tbuffer;
7531 i = 0;
7532 do
7533 {
7534 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7535 if (pend == NULL)
7536 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007537 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 if (textWidth < minButtonWidth)
7539 textWidth = minButtonWidth;
7540 textWidth += dlgPaddingX; /* Padding within button */
7541 buttonWidths[i] = textWidth;
7542 buttonPositions[i++] = horizWidth;
7543 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7544 pstart = pend + 1;
7545 } while (*pend != NUL);
7546
7547 if (horizWidth > maxDialogWidth)
7548 vertical = TRUE; // Too wide to fit on the screen.
7549 else if (horizWidth > dlgwidth)
7550 dlgwidth = horizWidth;
7551 }
7552
7553 if (vertical)
7554 {
7555 // Stack buttons vertically.
7556 pstart = tbuffer;
7557 do
7558 {
7559 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7560 if (pend == NULL)
7561 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007562 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 textWidth += dlgPaddingX; /* Padding within button */
7564 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7565 if (textWidth > dlgwidth)
7566 dlgwidth = textWidth;
7567 pstart = pend + 1;
7568 } while (*pend != NUL);
7569 }
7570
7571 if (dlgwidth < DLG_MIN_WIDTH)
7572 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7573
7574 /* start to fill in the dlgtemplate information. addressing by WORDs */
7575 if (s_usenewlook)
7576 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7577 else
7578 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7579
7580 add_long(lStyle);
7581 add_long(0); // (lExtendedStyle)
7582 pnumitems = p; /*save where the number of items must be stored*/
7583 add_word(0); // NumberOfItems(will change later)
7584 add_word(10); // x
7585 add_word(10); // y
7586 add_word(PixelToDialogX(dlgwidth)); // cx
7587
7588 // Dialog height.
7589 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007590 dlgheight = msgheight + 2 * dlgPaddingY
7591 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 else
7593 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7594
7595 // Dialog needs to be taller if contains an edit box.
7596 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7597 if (textfield != NULL)
7598 dlgheight += editboxheight;
7599
Bram Moolenaara95d8232013-08-07 15:27:11 +02007600 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7601 if (dlgheight > maxDialogHeight)
7602 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007603 msgheight = msgheight - (dlgheight - maxDialogHeight);
7604 dlgheight = maxDialogHeight;
7605 scroll_flag = WS_VSCROLL;
7606 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7607 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007608 }
7609
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 add_word(PixelToDialogY(dlgheight));
7611
7612 add_word(0); // Menu
7613 add_word(0); // Class
7614
7615 /* copy the title of the dialog */
7616 nchar = nCopyAnsiToWideChar(p, (title ?
7617 (LPSTR)title :
7618 (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
7619 p += nchar;
7620
7621 if (s_usenewlook)
7622 {
7623 /* do the font, since DS_3DLOOK doesn't work properly */
7624#ifdef USE_SYSMENU_FONT
7625 if (use_lfSysmenu)
7626 {
7627 /* point size */
7628 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7629 GetDeviceCaps(hdc, LOGPIXELSY));
7630 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
7631 }
7632 else
7633#endif
7634 {
7635 *p++ = DLG_FONT_POINT_SIZE; // point size
7636 nchar = nCopyAnsiToWideChar(p, TEXT(DLG_FONT_NAME));
7637 }
7638 p += nchar;
7639 }
7640
7641 buttonYpos = msgheight + 2 * dlgPaddingY;
7642
7643 if (textfield != NULL)
7644 buttonYpos += editboxheight;
7645
7646 pstart = tbuffer;
7647 if (!vertical)
7648 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7649 for (i = 0; i < numButtons; i++)
7650 {
7651 /* get end of this button. */
7652 for ( pend = pstart;
7653 *pend && (*pend != DLG_BUTTON_SEP);
7654 pend++)
7655 ;
7656
7657 if (*pend)
7658 *pend = '\0';
7659
7660 /*
7661 * old NOTE:
7662 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7663 * the focus to the first tab-able button and in so doing makes that
7664 * the default!! Grrr. Workaround: Make the default button the only
7665 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7666 * he/she can use arrow keys.
7667 *
7668 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007669 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 * dialog. Also needed for when the textfield is the default control.
7671 * It appears to work now (perhaps not on Win95?).
7672 */
7673 if (vertical)
7674 {
7675 p = add_dialog_element(p,
7676 (i == dfltbutton
7677 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7678 PixelToDialogX(DLG_VERT_PADDING_X),
7679 PixelToDialogY(buttonYpos /* TBK */
7680 + 2 * fontHeight * i),
7681 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7682 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007683 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 }
7685 else
7686 {
7687 p = add_dialog_element(p,
7688 (i == dfltbutton
7689 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7690 PixelToDialogX(horizWidth + buttonPositions[i]),
7691 PixelToDialogY(buttonYpos), /* TBK */
7692 PixelToDialogX(buttonWidths[i]),
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 pstart = pend + 1; /*next button*/
7697 }
7698 *pnumitems += numButtons;
7699
7700 /* Vim icon */
7701 p = add_dialog_element(p, SS_ICON,
7702 PixelToDialogX(dlgPaddingX),
7703 PixelToDialogY(dlgPaddingY),
7704 PixelToDialogX(DLG_ICON_WIDTH),
7705 PixelToDialogY(DLG_ICON_HEIGHT),
7706 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7707 dlg_icons[type]);
7708
Bram Moolenaar748bf032005-02-02 23:04:36 +00007709 /* Dialog message */
7710 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7711 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7712 PixelToDialogY(dlgPaddingY),
7713 (WORD)(PixelToDialogX(messageWidth) + 1),
7714 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007715 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716
7717 /* Edit box */
7718 if (textfield != NULL)
7719 {
7720 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7721 PixelToDialogX(2 * dlgPaddingX),
7722 PixelToDialogY(2 * dlgPaddingY + msgheight),
7723 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7724 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007725 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 *pnumitems += 1;
7727 }
7728
7729 *pnumitems += 2;
7730
7731 SelectFont(hdc, oldFont);
7732 DeleteObject(font);
7733 ReleaseDC(hwnd, hdc);
7734
7735 /* Let the dialog_callback() function know which button to make default
7736 * If we have an edit box, make that the default. We also need to tell
7737 * dialog_callback() if this dialog contains an edit box or not. We do
7738 * this by setting s_textfield if it does.
7739 */
7740 if (textfield != NULL)
7741 {
7742 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7743 s_textfield = textfield;
7744 }
7745 else
7746 {
7747 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7748 s_textfield = NULL;
7749 }
7750
7751 /* show the dialog box modally and get a return value */
7752 nchar = (int)DialogBoxIndirect(
7753 s_hinst,
7754 (LPDLGTEMPLATE)pdlgtemplate,
7755 s_hwnd,
7756 (DLGPROC)dialog_callback);
7757
7758 LocalFree(LocalHandle(pdlgtemplate));
7759 vim_free(tbuffer);
7760 vim_free(buttonWidths);
7761 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007762 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763
7764 /* Focus back to our window (for when MDI is used). */
7765 (void)SetFocus(s_hwnd);
7766
7767 return nchar;
7768}
7769
7770#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007771
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772/*
7773 * Put a simple element (basic class) onto a dialog template in memory.
7774 * return a pointer to where the next item should be added.
7775 *
7776 * parameters:
7777 * lStyle = additional style flags
7778 * (be careful, NT3.51 & Win32s will ignore the new ones)
7779 * x,y = x & y positions IN DIALOG UNITS
7780 * w,h = width and height IN DIALOG UNITS
7781 * Id = ID used in messages
7782 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7783 * caption = usually text or resource name
7784 *
7785 * TODO: use the length information noted here to enable the dialog creation
7786 * routines to work out more exactly how much memory they need to alloc.
7787 */
7788 static PWORD
7789add_dialog_element(
7790 PWORD p,
7791 DWORD lStyle,
7792 WORD x,
7793 WORD y,
7794 WORD w,
7795 WORD h,
7796 WORD Id,
7797 WORD clss,
7798 const char *caption)
7799{
7800 int nchar;
7801
7802 p = lpwAlign(p); /* Align to dword boundary*/
7803 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7804 *p++ = LOWORD(lStyle);
7805 *p++ = HIWORD(lStyle);
7806 *p++ = 0; // LOWORD (lExtendedStyle)
7807 *p++ = 0; // HIWORD (lExtendedStyle)
7808 *p++ = x;
7809 *p++ = y;
7810 *p++ = w;
7811 *p++ = h;
7812 *p++ = Id; //9 or 10 words in all
7813
7814 *p++ = (WORD)0xffff;
7815 *p++ = clss; //2 more here
7816
7817 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption); //strlen(caption)+1
7818 p += nchar;
7819
7820 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7821
7822 return p; //total = 15+ (strlen(caption)) words
7823 // = 30 + 2(strlen(caption) bytes reqd
7824}
7825
7826
7827/*
7828 * Helper routine. Take an input pointer, return closest pointer that is
7829 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7830 */
7831 static LPWORD
7832lpwAlign(
7833 LPWORD lpIn)
7834{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007835 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007837 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 ul += 3;
7839 ul >>= 2;
7840 ul <<= 2;
7841 return (LPWORD)ul;
7842}
7843
7844/*
7845 * Helper routine. Takes second parameter as Ansi string, copies it to first
7846 * parameter as wide character (16-bits / char) string, and returns integer
7847 * number of wide characters (words) in string (including the trailing wide
7848 * char NULL). Partly taken from the Win32SDK samples.
7849 */
7850 static int
7851nCopyAnsiToWideChar(
7852 LPWORD lpWCStr,
7853 LPSTR lpAnsiIn)
7854{
7855 int nChar = 0;
7856#ifdef FEAT_MBYTE
7857 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7858 int i;
7859 WCHAR *wn;
7860
7861 if (enc_codepage == 0 && (int)GetACP() != enc_codepage)
7862 {
7863 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007864 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 if (wn != NULL)
7866 {
7867 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007868 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 vim_free(wn);
7870 }
7871 }
7872 if (nChar == 0)
7873 /* Use Win32 conversion function. */
7874 nChar = MultiByteToWideChar(
7875 enc_codepage > 0 ? enc_codepage : CP_ACP,
7876 MB_PRECOMPOSED,
7877 lpAnsiIn, len,
7878 lpWCStr, len);
7879 for (i = 0; i < nChar; ++i)
7880 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7881 lpWCStr[i] = (WORD)' ';
7882#else
7883 do
7884 {
7885 if (*lpAnsiIn == '\t')
7886 *lpWCStr++ = (WORD)' ';
7887 else
7888 *lpWCStr++ = (WORD)*lpAnsiIn;
7889 nChar++;
7890 } while (*lpAnsiIn++);
7891#endif
7892
7893 return nChar;
7894}
7895
7896
7897#ifdef FEAT_TEAROFF
7898/*
7899 * The callback function for all the modeless dialogs that make up the
7900 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7901 * thinking its menus have been clicked), and go away when closed.
7902 */
7903 static LRESULT CALLBACK
7904tearoff_callback(
7905 HWND hwnd,
7906 UINT message,
7907 WPARAM wParam,
7908 LPARAM lParam)
7909{
7910 if (message == WM_INITDIALOG)
7911 return (TRUE);
7912
7913 /* May show the mouse pointer again. */
7914 HandleMouseHide(message, lParam);
7915
7916 if (message == WM_COMMAND)
7917 {
7918 if ((WORD)(LOWORD(wParam)) & 0x8000)
7919 {
7920 POINT mp;
7921 RECT rect;
7922
7923 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7924 {
7925 (void)TrackPopupMenu(
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007926 (HMENU)(long_u)(LOWORD(wParam) ^ 0x8000),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7928 (int)rect.right - 8,
7929 (int)mp.y,
7930 (int)0, /*reserved param*/
7931 s_hwnd,
7932 NULL);
7933 /*
7934 * NOTE: The pop-up menu can eat the mouse up event.
7935 * We deal with this in normal.c.
7936 */
7937 }
7938 }
7939 else
7940 /* Pass on messages to the main Vim window */
7941 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7942 /*
7943 * Give main window the focus back: this is so after
7944 * choosing a tearoff button you can start typing again
7945 * straight away.
7946 */
7947 (void)SetFocus(s_hwnd);
7948 return TRUE;
7949 }
7950 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7951 {
7952 DestroyWindow(hwnd);
7953 return TRUE;
7954 }
7955
7956 /* When moved around, give main window the focus back. */
7957 if (message == WM_EXITSIZEMOVE)
7958 (void)SetActiveWindow(s_hwnd);
7959
7960 return FALSE;
7961}
7962#endif
7963
7964
7965/*
7966 * Decide whether to use the "new look" (small, non-bold font) or the "old
7967 * look" (big, clanky font) for dialogs, and work out a few values for use
7968 * later accordingly.
7969 */
7970 static void
7971get_dialog_font_metrics(void)
7972{
7973 HDC hdc;
7974 HFONT hfontTools = 0;
7975 DWORD dlgFontSize;
7976 SIZE size;
7977#ifdef USE_SYSMENU_FONT
7978 LOGFONT lfSysmenu;
7979#endif
7980
7981 s_usenewlook = FALSE;
7982
7983 /*
7984 * For NT3.51 and Win32s, we stick with the old look
7985 * because it matches everything else.
7986 */
7987 if (!is_winnt_3())
7988 {
7989#ifdef USE_SYSMENU_FONT
7990 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7991 hfontTools = CreateFontIndirect(&lfSysmenu);
7992 else
7993#endif
7994 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7995 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7996
7997 if (hfontTools)
7998 {
7999 hdc = GetDC(s_hwnd);
8000 SelectObject(hdc, hfontTools);
8001 /*
8002 * GetTextMetrics() doesn't return the right value in
8003 * tmAveCharWidth, so we have to figure out the dialog base units
8004 * ourselves.
8005 */
8006 GetTextExtentPoint(hdc,
8007 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
8008 52, &size);
8009 ReleaseDC(s_hwnd, hdc);
8010
8011 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
8012 s_dlgfntheight = (WORD)size.cy;
8013 s_usenewlook = TRUE;
8014 }
8015 }
8016
8017 if (!s_usenewlook)
8018 {
8019 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
8020 s_dlgfntwidth = LOWORD(dlgFontSize);
8021 s_dlgfntheight = HIWORD(dlgFontSize);
8022 }
8023}
8024
8025#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8026/*
8027 * Create a pseudo-"tearoff menu" based on the child
8028 * items of a given menu pointer.
8029 */
8030 static void
8031gui_mch_tearoff(
8032 char_u *title,
8033 vimmenu_T *menu,
8034 int initX,
8035 int initY)
8036{
8037 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
8038 int template_len;
8039 int nchar, textWidth, submenuWidth;
8040 DWORD lStyle;
8041 DWORD lExtendedStyle;
8042 WORD dlgwidth;
8043 WORD menuID;
8044 vimmenu_T *pmenu;
8045 vimmenu_T *the_menu = menu;
8046 HWND hwnd;
8047 HDC hdc;
8048 HFONT font, oldFont;
8049 int col, spaceWidth, len;
8050 int columnWidths[2];
8051 char_u *label, *text;
8052 int acLen = 0;
8053 int nameLen;
8054 int padding0, padding1, padding2 = 0;
8055 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008056 int x;
8057 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058#ifdef USE_SYSMENU_FONT
8059 LOGFONT lfSysmenu;
8060 int use_lfSysmenu = FALSE;
8061#endif
8062
8063 /*
8064 * If this menu is already torn off, move it to the mouse position.
8065 */
8066 if (IsWindow(menu->tearoff_handle))
8067 {
8068 POINT mp;
8069 if (GetCursorPos((LPPOINT)&mp))
8070 {
8071 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
8072 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
8073 }
8074 return;
8075 }
8076
8077 /*
8078 * Create a new tearoff.
8079 */
8080 if (*title == MNU_HIDDEN_CHAR)
8081 title++;
8082
8083 /* Allocate memory to store the dialog template. It's made bigger when
8084 * needed. */
8085 template_len = DLG_ALLOC_SIZE;
8086 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
8087 if (p == NULL)
8088 return;
8089
8090 hwnd = GetDesktopWindow();
8091 hdc = GetWindowDC(hwnd);
8092#ifdef USE_SYSMENU_FONT
8093 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
8094 {
8095 font = CreateFontIndirect(&lfSysmenu);
8096 use_lfSysmenu = TRUE;
8097 }
8098 else
8099#endif
8100 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8101 VARIABLE_PITCH , DLG_FONT_NAME);
8102 if (s_usenewlook)
8103 oldFont = SelectFont(hdc, font);
8104 else
8105 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
8106
8107 /* Calculate width of a single space. Used for padding columns to the
8108 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008109 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110
8111 /* Figure out max width of the text column, the accelerator column and the
8112 * optional submenu column. */
8113 submenuWidth = 0;
8114 for (col = 0; col < 2; col++)
8115 {
8116 columnWidths[col] = 0;
8117 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
8118 {
8119 /* Use "dname" here to compute the width of the visible text. */
8120 text = (col == 0) ? pmenu->dname : pmenu->actext;
8121 if (text != NULL && *text != NUL)
8122 {
8123 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
8124 if (textWidth > columnWidths[col])
8125 columnWidths[col] = textWidth;
8126 }
8127 if (pmenu->children != NULL)
8128 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
8129 }
8130 }
8131 if (columnWidths[1] == 0)
8132 {
8133 /* no accelerators */
8134 if (submenuWidth != 0)
8135 columnWidths[0] += submenuWidth;
8136 else
8137 columnWidths[0] += spaceWidth;
8138 }
8139 else
8140 {
8141 /* there is an accelerator column */
8142 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
8143 columnWidths[1] += submenuWidth;
8144 }
8145
8146 /*
8147 * Now find the total width of our 'menu'.
8148 */
8149 textWidth = columnWidths[0] + columnWidths[1];
8150 if (submenuWidth != 0)
8151 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008152 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
8154 textWidth += submenuWidth;
8155 }
8156 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
8157 if (textWidth > dlgwidth)
8158 dlgwidth = textWidth;
8159 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
8160
8161 /* W95 can't do thin dialogs, they look v. weird! */
8162 if (mch_windows95() && dlgwidth < TEAROFF_MIN_WIDTH)
8163 dlgwidth = TEAROFF_MIN_WIDTH;
8164
8165 /* start to fill in the dlgtemplate information. addressing by WORDs */
8166 if (s_usenewlook)
8167 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
8168 else
8169 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
8170
8171 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
8172 *p++ = LOWORD(lStyle);
8173 *p++ = HIWORD(lStyle);
8174 *p++ = LOWORD(lExtendedStyle);
8175 *p++ = HIWORD(lExtendedStyle);
8176 pnumitems = p; /* save where the number of items must be stored */
8177 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008178 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008180 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 else
8182 *p++ = PixelToDialogX(initX); // x
8183 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008184 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 else
8186 *p++ = PixelToDialogY(initY); // y
8187 *p++ = PixelToDialogX(dlgwidth); // cx
8188 ptrueheight = p;
8189 *p++ = 0; // dialog height: changed later anyway
8190 *p++ = 0; // Menu
8191 *p++ = 0; // Class
8192
8193 /* copy the title of the dialog */
8194 nchar = nCopyAnsiToWideChar(p, ((*title)
8195 ? (LPSTR)title
8196 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
8197 p += nchar;
8198
8199 if (s_usenewlook)
8200 {
8201 /* do the font, since DS_3DLOOK doesn't work properly */
8202#ifdef USE_SYSMENU_FONT
8203 if (use_lfSysmenu)
8204 {
8205 /* point size */
8206 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
8207 GetDeviceCaps(hdc, LOGPIXELSY));
8208 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
8209 }
8210 else
8211#endif
8212 {
8213 *p++ = DLG_FONT_POINT_SIZE; // point size
8214 nchar = nCopyAnsiToWideChar (p, TEXT(DLG_FONT_NAME));
8215 }
8216 p += nchar;
8217 }
8218
8219 /*
8220 * Loop over all the items in the menu.
8221 * But skip over the tearbar.
8222 */
8223 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
8224 menu = menu->children->next;
8225 else
8226 menu = menu->children;
8227 for ( ; menu != NULL; menu = menu->next)
8228 {
8229 if (menu->modes == 0) /* this menu has just been deleted */
8230 continue;
8231 if (menu_is_separator(menu->dname))
8232 {
8233 sepPadding += 3;
8234 continue;
8235 }
8236
8237 /* Check if there still is plenty of room in the template. Make it
8238 * larger when needed. */
8239 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8240 {
8241 WORD *newp;
8242
8243 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8244 if (newp != NULL)
8245 {
8246 template_len += 4096;
8247 mch_memmove(newp, pdlgtemplate,
8248 (char *)p - (char *)pdlgtemplate);
8249 p = newp + (p - pdlgtemplate);
8250 pnumitems = newp + (pnumitems - pdlgtemplate);
8251 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8252 LocalFree(LocalHandle(pdlgtemplate));
8253 pdlgtemplate = newp;
8254 }
8255 }
8256
8257 /* Figure out minimal length of this menu label. Use "name" for the
8258 * actual text, "dname" for estimating the displayed size. "name"
8259 * has "&a" for mnemonic and includes the accelerator. */
8260 len = nameLen = (int)STRLEN(menu->name);
8261 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8262 (int)STRLEN(menu->dname))) / spaceWidth;
8263 len += padding0;
8264
8265 if (menu->actext != NULL)
8266 {
8267 acLen = (int)STRLEN(menu->actext);
8268 len += acLen;
8269 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8270 }
8271 else
8272 textWidth = 0;
8273 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8274 len += padding1;
8275
8276 if (menu->children == NULL)
8277 {
8278 padding2 = submenuWidth / spaceWidth;
8279 len += padding2;
8280 menuID = (WORD)(menu->id);
8281 }
8282 else
8283 {
8284 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008285 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 }
8287
8288 /* Allocate menu label and fill it in */
8289 text = label = alloc((unsigned)len + 1);
8290 if (label == NULL)
8291 break;
8292
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008293 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 text = vim_strchr(text, TAB); /* stop at TAB before actext */
8295 if (text == NULL)
8296 text = label + nameLen; /* no actext, use whole name */
8297 while (padding0-- > 0)
8298 *text++ = ' ';
8299 if (menu->actext != NULL)
8300 {
8301 STRNCPY(text, menu->actext, acLen);
8302 text += acLen;
8303 }
8304 while (padding1-- > 0)
8305 *text++ = ' ';
8306 if (menu->children != NULL)
8307 {
8308 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8309 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8310 }
8311 else
8312 {
8313 while (padding2-- > 0)
8314 *text++ = ' ';
8315 }
8316 *text = NUL;
8317
8318 /*
8319 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8320 * W95/NT4 it makes the tear-off look more like a menu.
8321 */
8322 p = add_dialog_element(p,
8323 BS_PUSHBUTTON|BS_LEFT,
8324 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8325 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8326 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8327 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008328 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 vim_free(label);
8330 (*pnumitems)++;
8331 }
8332
8333 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8334
8335
8336 /* show modelessly */
8337 the_menu->tearoff_handle = CreateDialogIndirect(
8338 s_hinst,
8339 (LPDLGTEMPLATE)pdlgtemplate,
8340 s_hwnd,
8341 (DLGPROC)tearoff_callback);
8342
8343 LocalFree(LocalHandle(pdlgtemplate));
8344 SelectFont(hdc, oldFont);
8345 DeleteObject(font);
8346 ReleaseDC(hwnd, hdc);
8347
8348 /*
8349 * Reassert ourselves as the active window. This is so that after creating
8350 * a tearoff, the user doesn't have to click with the mouse just to start
8351 * typing again!
8352 */
8353 (void)SetActiveWindow(s_hwnd);
8354
8355 /* make sure the right buttons are enabled */
8356 force_menu_update = TRUE;
8357}
8358#endif
8359
8360#if defined(FEAT_TOOLBAR) || defined(PROTO)
8361#include "gui_w32_rc.h"
8362
8363/* This not defined in older SDKs */
8364# ifndef TBSTYLE_FLAT
8365# define TBSTYLE_FLAT 0x0800
8366# endif
8367
8368/*
8369 * Create the toolbar, initially unpopulated.
8370 * (just like the menu, there are no defaults, it's all
8371 * set up through menu.vim)
8372 */
8373 static void
8374initialise_toolbar(void)
8375{
8376 InitCommonControls();
8377 s_toolbarhwnd = CreateToolbarEx(
8378 s_hwnd,
8379 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8380 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008381 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 s_hinst,
8383 IDR_TOOLBAR1, // id of initial bitmap
8384 NULL,
8385 0, // initial number of buttons
8386 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8387 TOOLBAR_BUTTON_HEIGHT,
8388 TOOLBAR_BUTTON_WIDTH,
8389 TOOLBAR_BUTTON_HEIGHT,
8390 sizeof(TBBUTTON)
8391 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008392 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393
8394 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
8395}
8396
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008397 static LRESULT CALLBACK
8398toolbar_wndproc(
8399 HWND hwnd,
8400 UINT uMsg,
8401 WPARAM wParam,
8402 LPARAM lParam)
8403{
8404 HandleMouseHide(uMsg, lParam);
8405 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8406}
8407
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 static int
8409get_toolbar_bitmap(vimmenu_T *menu)
8410{
8411 int i = -1;
8412
8413 /*
8414 * Check user bitmaps first, unless builtin is specified.
8415 */
8416 if (!is_winnt_3() && !menu->icon_builtin)
8417 {
8418 char_u fname[MAXPATHL];
8419 HANDLE hbitmap = NULL;
8420
8421 if (menu->iconfile != NULL)
8422 {
8423 gui_find_iconfile(menu->iconfile, fname, "bmp");
8424 hbitmap = LoadImage(
8425 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008426 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 IMAGE_BITMAP,
8428 TOOLBAR_BUTTON_WIDTH,
8429 TOOLBAR_BUTTON_HEIGHT,
8430 LR_LOADFROMFILE |
8431 LR_LOADMAP3DCOLORS
8432 );
8433 }
8434
8435 /*
8436 * If the LoadImage call failed, or the "icon=" file
8437 * didn't exist or wasn't specified, try the menu name
8438 */
8439 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008440 && (gui_find_bitmap(
8441#ifdef FEAT_MULTI_LANG
8442 menu->en_dname != NULL ? menu->en_dname :
8443#endif
8444 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 hbitmap = LoadImage(
8446 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008447 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 IMAGE_BITMAP,
8449 TOOLBAR_BUTTON_WIDTH,
8450 TOOLBAR_BUTTON_HEIGHT,
8451 LR_LOADFROMFILE |
8452 LR_LOADMAP3DCOLORS
8453 );
8454
8455 if (hbitmap != NULL)
8456 {
8457 TBADDBITMAP tbAddBitmap;
8458
8459 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008460 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461
8462 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8463 (WPARAM)1, (LPARAM)&tbAddBitmap);
8464 /* i will be set to -1 if it fails */
8465 }
8466 }
8467 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8468 i = menu->iconidx;
8469
8470 return i;
8471}
8472#endif
8473
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008474#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8475 static void
8476initialise_tabline(void)
8477{
8478 InitCommonControls();
8479
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008480 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008481 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008482 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8483 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008484 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008485
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008486 gui.tabline_height = TABLINE_HEIGHT;
8487
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008488# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008489 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008490# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008491}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008492
8493 static LRESULT CALLBACK
8494tabline_wndproc(
8495 HWND hwnd,
8496 UINT uMsg,
8497 WPARAM wParam,
8498 LPARAM lParam)
8499{
8500 HandleMouseHide(uMsg, lParam);
8501 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8502}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008503#endif
8504
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8506/*
8507 * Make the GUI window come to the foreground.
8508 */
8509 void
8510gui_mch_set_foreground(void)
8511{
8512 if (IsIconic(s_hwnd))
8513 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8514 SetForegroundWindow(s_hwnd);
8515}
8516#endif
8517
8518#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8519 static void
8520dyn_imm_load(void)
8521{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008522 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 if (hLibImm == NULL)
8524 return;
8525
8526 pImmGetCompositionStringA
8527 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8528 pImmGetCompositionStringW
8529 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8530 pImmGetContext
8531 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8532 pImmAssociateContext
8533 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8534 pImmReleaseContext
8535 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8536 pImmGetOpenStatus
8537 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8538 pImmSetOpenStatus
8539 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
8540 pImmGetCompositionFont
8541 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
8542 pImmSetCompositionFont
8543 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
8544 pImmSetCompositionWindow
8545 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8546 pImmGetConversionStatus
8547 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008548 pImmSetConversionStatus
8549 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550
8551 if ( pImmGetCompositionStringA == NULL
8552 || pImmGetCompositionStringW == NULL
8553 || pImmGetContext == NULL
8554 || pImmAssociateContext == NULL
8555 || pImmReleaseContext == NULL
8556 || pImmGetOpenStatus == NULL
8557 || pImmSetOpenStatus == NULL
8558 || pImmGetCompositionFont == NULL
8559 || pImmSetCompositionFont == NULL
8560 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008561 || pImmGetConversionStatus == NULL
8562 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 {
8564 FreeLibrary(hLibImm);
8565 hLibImm = NULL;
8566 pImmGetContext = NULL;
8567 return;
8568 }
8569
8570 return;
8571}
8572
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573#endif
8574
8575#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8576
8577# ifdef FEAT_XPM_W32
8578# define IMAGE_XPM 100
8579# endif
8580
8581typedef struct _signicon_t
8582{
8583 HANDLE hImage;
8584 UINT uType;
8585#ifdef FEAT_XPM_W32
8586 HANDLE hShape; /* Mask bitmap handle */
8587#endif
8588} signicon_t;
8589
8590 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008591gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592{
8593 signicon_t *sign;
8594 int x, y, w, h;
8595
8596 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8597 return;
8598
8599 x = TEXT_X(col);
8600 y = TEXT_Y(row);
8601 w = gui.char_width * 2;
8602 h = gui.char_height;
8603 switch (sign->uType)
8604 {
8605 case IMAGE_BITMAP:
8606 {
8607 HDC hdcMem;
8608 HBITMAP hbmpOld;
8609
8610 hdcMem = CreateCompatibleDC(s_hdc);
8611 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8612 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8613 SelectObject(hdcMem, hbmpOld);
8614 DeleteDC(hdcMem);
8615 }
8616 break;
8617 case IMAGE_ICON:
8618 case IMAGE_CURSOR:
8619 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8620 break;
8621#ifdef FEAT_XPM_W32
8622 case IMAGE_XPM:
8623 {
8624 HDC hdcMem;
8625 HBITMAP hbmpOld;
8626
8627 hdcMem = CreateCompatibleDC(s_hdc);
8628 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8629 /* Make hole */
8630 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8631
8632 SelectObject(hdcMem, sign->hImage);
8633 /* Paint sign */
8634 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8635 SelectObject(hdcMem, hbmpOld);
8636 DeleteDC(hdcMem);
8637 }
8638 break;
8639#endif
8640 }
8641}
8642
8643 static void
8644close_signicon_image(signicon_t *sign)
8645{
8646 if (sign)
8647 switch (sign->uType)
8648 {
8649 case IMAGE_BITMAP:
8650 DeleteObject((HGDIOBJ)sign->hImage);
8651 break;
8652 case IMAGE_CURSOR:
8653 DestroyCursor((HCURSOR)sign->hImage);
8654 break;
8655 case IMAGE_ICON:
8656 DestroyIcon((HICON)sign->hImage);
8657 break;
8658#ifdef FEAT_XPM_W32
8659 case IMAGE_XPM:
8660 DeleteObject((HBITMAP)sign->hImage);
8661 DeleteObject((HBITMAP)sign->hShape);
8662 break;
8663#endif
8664 }
8665}
8666
8667 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008668gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669{
8670 signicon_t sign, *psign;
8671 char_u *ext;
8672
8673 if (is_winnt_3())
8674 {
8675 EMSG(_(e_signdata));
8676 return NULL;
8677 }
8678
8679 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008680 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 if (ext > signfile)
8682 {
8683 int do_load = 1;
8684
8685 if (!STRICMP(ext, ".bmp"))
8686 sign.uType = IMAGE_BITMAP;
8687 else if (!STRICMP(ext, ".ico"))
8688 sign.uType = IMAGE_ICON;
8689 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8690 sign.uType = IMAGE_CURSOR;
8691 else
8692 do_load = 0;
8693
8694 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008695 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 gui.char_width * 2, gui.char_height,
8697 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8698#ifdef FEAT_XPM_W32
8699 if (!STRICMP(ext, ".xpm"))
8700 {
8701 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008702 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8703 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 }
8705#endif
8706 }
8707
8708 psign = NULL;
8709 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8710 != NULL)
8711 *psign = sign;
8712
8713 if (!psign)
8714 {
8715 if (sign.hImage)
8716 close_signicon_image(&sign);
8717 EMSG(_(e_signdata));
8718 }
8719 return (void *)psign;
8720
8721}
8722
8723 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008724gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725{
8726 if (sign)
8727 {
8728 close_signicon_image((signicon_t *)sign);
8729 vim_free(sign);
8730 }
8731}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733
8734#if defined(FEAT_BEVAL) || defined(PROTO)
8735
8736/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008737 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 *
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00008739 * The only reused thing is gui_beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8741 * to get current mouse position).
8742 *
8743 * Trying to use as more Windows services as possible, and as less
8744 * IE version as possible :)).
8745 *
8746 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8747 * BalloonEval struct.
8748 * 2) Enable/Disable simply create/kill BalloonEval Timer
8749 * 3) When there was enough inactivity, timer procedure posts
8750 * async request to debugger
8751 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8752 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008753 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 */
8755
Bram Moolenaar45360022005-07-21 21:08:21 +00008756/*
8757 * determine whether installed Common Controls support multiline tooltips
8758 * (i.e. their version is >= 4.70
8759 */
8760 int
8761multiline_balloon_available(void)
8762{
8763 HINSTANCE hDll;
8764 static char comctl_dll[] = "comctl32.dll";
8765 static int multiline_tip = MAYBE;
8766
8767 if (multiline_tip != MAYBE)
8768 return multiline_tip;
8769
8770 hDll = GetModuleHandle(comctl_dll);
8771 if (hDll != NULL)
8772 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008773 DLLGETVERSIONPROC pGetVer;
8774 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008775
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008776 if (pGetVer != NULL)
8777 {
8778 DLLVERSIONINFO dvi;
8779 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008780
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008781 ZeroMemory(&dvi, sizeof(dvi));
8782 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008783
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008784 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008785
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008786 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008787 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008788 || (dvi.dwMajorVersion == 4
8789 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008790 {
8791 multiline_tip = TRUE;
8792 return multiline_tip;
8793 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008794 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008795 else
8796 {
8797 /* there is chance we have ancient CommCtl 4.70
8798 which doesn't export DllGetVersion */
8799 DWORD dwHandle = 0;
8800 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8801 if (len > 0)
8802 {
8803 VS_FIXEDFILEINFO *ver;
8804 UINT vlen = 0;
8805 void *data = alloc(len);
8806
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008807 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008808 && GetFileVersionInfo(comctl_dll, 0, len, data)
8809 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8810 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008811 && HIWORD(ver->dwFileVersionMS) > 4)
8812 || ((HIWORD(ver->dwFileVersionMS) == 4
8813 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008814 {
8815 vim_free(data);
8816 multiline_tip = TRUE;
8817 return multiline_tip;
8818 }
8819 vim_free(data);
8820 }
8821 }
8822 }
8823 multiline_tip = FALSE;
8824 return multiline_tip;
8825}
8826
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008828make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829{
Bram Moolenaar45360022005-07-21 21:08:21 +00008830 TOOLINFO *pti;
8831 int ToolInfoSize;
8832
8833 if (multiline_balloon_available() == TRUE)
8834 ToolInfoSize = sizeof(TOOLINFO_NEW);
8835 else
8836 ToolInfoSize = sizeof(TOOLINFO);
8837
8838 pti = (TOOLINFO *)alloc(ToolInfoSize);
8839 if (pti == NULL)
8840 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841
8842 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
8843 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8844 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8845 beval->target, NULL, s_hinst, NULL);
8846
8847 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8848 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8849
Bram Moolenaar45360022005-07-21 21:08:21 +00008850 pti->cbSize = ToolInfoSize;
8851 pti->uFlags = TTF_SUBCLASS;
8852 pti->hwnd = beval->target;
8853 pti->hinst = 0; /* Don't use string resources */
8854 pti->uId = ID_BEVAL_TOOLTIP;
8855
8856 if (multiline_balloon_available() == TRUE)
8857 {
8858 RECT rect;
8859 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
8860 pti->lpszText = LPSTR_TEXTCALLBACK;
8861 ptin->lParam = (LPARAM)text;
8862 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
8863 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8864 (LPARAM)rect.right);
8865 }
8866 else
8867 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868
8869 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00008870 pti->rect.left = pt.x - 3;
8871 pti->rect.top = pt.y - 3;
8872 pti->rect.right = pt.x + 3;
8873 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874
Bram Moolenaar45360022005-07-21 21:08:21 +00008875 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 /* Make tooltip appear sooner */
8877 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008878 /* I've performed some tests and it seems the longest possible life time
8879 * of tooltip is 30 seconds */
8880 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 /*
8882 * HACK: force tooltip to appear, because it'll not appear until
8883 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008884 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008886 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00008888 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889}
8890
8891 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008892delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008894 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895}
8896
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008897/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008899BevalTimerProc(
8900 HWND hwnd,
8901 UINT uMsg,
8902 UINT_PTR idEvent,
8903 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904{
8905 POINT pt;
8906 RECT rect;
8907
8908 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8909 return;
8910
8911 GetCursorPos(&pt);
8912 if (WindowFromPoint(pt) != s_textArea)
8913 return;
8914
8915 ScreenToClient(s_textArea, &pt);
8916 GetClientRect(s_textArea, &rect);
8917 if (!PtInRect(&rect, pt))
8918 return;
8919
8920 if (LastActivity > 0
8921 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8922 && (cur_beval->showState != ShS_PENDING
8923 || abs(cur_beval->x - pt.x) > 3
8924 || abs(cur_beval->y - pt.y) > 3))
8925 {
8926 /* Pointer resting in one place long enough, it's time to show
8927 * the tooltip. */
8928 cur_beval->showState = ShS_PENDING;
8929 cur_beval->x = pt.x;
8930 cur_beval->y = pt.y;
8931
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008932 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933
8934 if (cur_beval->msgCB != NULL)
8935 (*cur_beval->msgCB)(cur_beval, 0);
8936 }
8937}
8938
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008939/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008941gui_mch_disable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008943 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008945 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946}
8947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008948/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008950gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008952 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 if (beval == NULL)
8954 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008955 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008956 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008957 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958}
8959
8960 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008961gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962{
8963 POINT pt;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008964 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 if (beval->showState == ShS_SHOWING)
8966 return;
8967 GetCursorPos(&pt);
8968 ScreenToClient(s_textArea, &pt);
8969
8970 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
8971 /* cursor is still here */
8972 {
8973 gui_mch_disable_beval_area(cur_beval);
8974 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008975 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008977 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978}
8979
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008980/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008982gui_mch_create_beval_area(
8983 void *target, /* ignored, always use s_textArea */
8984 char_u *mesg,
8985 void (*mesgCB)(BalloonEval *, int),
8986 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987{
8988 /* partially stolen from gui_beval.c */
8989 BalloonEval *beval;
8990
8991 if (mesg != NULL && mesgCB != NULL)
8992 {
8993 EMSG(_("E232: Cannot create BalloonEval with both message and callback"));
8994 return NULL;
8995 }
8996
8997 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
8998 if (beval != NULL)
8999 {
9000 beval->target = s_textArea;
9001 beval->balloon = NULL;
9002
9003 beval->showState = ShS_NEUTRAL;
9004 beval->x = 0;
9005 beval->y = 0;
9006 beval->msg = mesg;
9007 beval->msgCB = mesgCB;
9008 beval->clientData = clientData;
9009
9010 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 cur_beval = beval;
9012
9013 if (p_beval)
9014 gui_mch_enable_beval_area(beval);
9015
9016 }
9017 return beval;
9018}
9019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009020/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 static void
Bram Moolenaar442b4222010-05-24 21:34:22 +02009022Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023{
9024 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
9025 return;
9026
9027 if (cur_beval != NULL)
9028 {
Bram Moolenaar45360022005-07-21 21:08:21 +00009029 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 {
Bram Moolenaar45360022005-07-21 21:08:21 +00009031 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009032 // TRACE0("TTN_SHOW {{{");
9033 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00009034 break;
9035 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009036 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 delete_tooltip(cur_beval);
9038 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009039 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040
9041 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00009042 break;
9043 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00009044 {
9045 /* if you get there then we have new common controls */
9046 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
9047 info->lpszText = (LPSTR)info->lParam;
9048 info->uFlags |= TTF_DI_SETITEM;
9049 }
Bram Moolenaar45360022005-07-21 21:08:21 +00009050 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 }
9052 }
9053}
9054
9055 static void
9056TrackUserActivity(UINT uMsg)
9057{
9058 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
9059 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
9060 LastActivity = GetTickCount();
9061}
9062
9063 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01009064gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065{
9066 vim_free(beval);
9067}
9068#endif /* FEAT_BEVAL */
9069
9070#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
9071/*
9072 * We have multiple signs to draw at the same location. Draw the
9073 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
9074 */
9075 void
9076netbeans_draw_multisign_indicator(int row)
9077{
9078 int i;
9079 int y;
9080 int x;
9081
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009082 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009083 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009084
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 x = 0;
9086 y = TEXT_Y(row);
9087
9088 for (i = 0; i < gui.char_height - 3; i++)
9089 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
9090
9091 SetPixel(s_hdc, x+0, y, gui.currFgColor);
9092 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9093 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
9094 SetPixel(s_hdc, x+1, y, gui.currFgColor);
9095 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9096 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
9097 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9098}
Bram Moolenaare0874f82016-01-24 20:36:41 +01009099#endif