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