blob: edfe24e56016a6c2c519f602f877e1495f0901fc [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). */
6661 unicodepdy[clen] = cw * gui.char_width;
6662 }
6663 cells += cw;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006664 i += utfc_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006665 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006667#if defined(FEAT_DIRECTX)
6668 if (IS_ENABLE_DIRECTX() && font_is_ttf_or_vector)
6669 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006670 /* Add one to "cells" for italics. */
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006671 DWriteContext_DrawText(s_dwc, s_hdc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006672 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006673 gui.char_width, gui.currFgColor);
6674 }
6675 else
6676#endif
6677 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6678 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 len = cells; /* used for underlining */
6680 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006681 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 {
6683 /* If we want to display codepage data, and the current CP is not the
6684 * ANSI one, we need to go via Unicode. */
6685 if (unicodebuf != NULL)
6686 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006687 if (enc_latin9)
6688 latin9_to_ucs(text, len, unicodebuf);
6689 else
6690 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 MB_PRECOMPOSED,
6692 (char *)text, len,
6693 (LPWSTR)unicodebuf, unibuflen);
6694 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006695 {
6696 /* Use unicodepdy to make characters fit as we expect, even
6697 * when the font uses different widths (e.g., bold character
6698 * is wider). */
6699 if (unicodepdy != NULL)
6700 {
6701 int i;
6702 int cw;
6703
6704 for (i = 0; i < len; ++i)
6705 {
6706 cw = utf_char2cells(unicodebuf[i]);
6707 if (cw > 2)
6708 cw = 1;
6709 unicodepdy[i] = cw * gui.char_width;
6710 }
6711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006713 foptions, pcliprect, unicodebuf, len, unicodepdy);
6714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 }
6716 }
6717 else
6718#endif
6719 {
6720#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006721 /* Windows will mess up RL text, so we have to draw it character by
6722 * character. Only do this if RL is on, since it's slow. */
6723 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6725 foptions, pcliprect, (char *)text, len, padding);
6726 else
6727#endif
6728 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6729 foptions, pcliprect, (char *)text, len, padding);
6730 }
6731
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006732 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 if (flags & DRAW_UNDERL)
6734 {
6735 hpen = CreatePen(PS_SOLID, 1, gui.currFgColor);
6736 old_pen = SelectObject(s_hdc, hpen);
6737 /* When p_linespace is 0, overwrite the bottom row of pixels.
6738 * Otherwise put the line just below the character. */
6739 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 if (p_linespace > 1)
6741 y -= p_linespace - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 MoveToEx(s_hdc, FILL_X(col), y, NULL);
6743 /* Note: LineTo() excludes the last pixel in the line. */
6744 LineTo(s_hdc, FILL_X(col + len), y);
6745 DeleteObject(SelectObject(s_hdc, old_pen));
6746 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006747
6748 /* Undercurl */
6749 if (flags & DRAW_UNDERC)
6750 {
6751 int x;
6752 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006753 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006754
6755 y = FILL_Y(row + 1) - 1;
6756 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6757 {
6758 offset = val[x % 8];
6759 SetPixel(s_hdc, x, y - offset, gui.currSpColor);
6760 }
6761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762}
6763
6764
6765/*
6766 * Output routines.
6767 */
6768
6769/* Flush any output to the screen */
6770 void
6771gui_mch_flush(void)
6772{
6773# if defined(__BORLANDC__)
6774 /*
6775 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6776 * prototype declaration.
6777 * The compiler complains if __stdcall is not used in both declarations.
6778 */
6779 BOOL __stdcall GdiFlush(void);
6780# endif
6781
6782 GdiFlush();
6783}
6784
6785 static void
6786clear_rect(RECT *rcp)
6787{
6788 HBRUSH hbr;
6789
6790 hbr = CreateSolidBrush(gui.back_pixel);
6791 FillRect(s_hdc, rcp, hbr);
6792 DeleteBrush(hbr);
6793}
6794
6795
Bram Moolenaarc716c302006-01-21 22:12:51 +00006796 void
6797gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6798{
6799 RECT workarea_rect;
6800
6801 get_work_area(&workarea_rect);
6802
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006803 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006804 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006805 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006806
6807 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6808 * the menubar for MSwin, we subtract it from the screen height, so that
6809 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006810 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006811 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006812 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006813 - GetSystemMetrics(SM_CYCAPTION)
6814#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006815 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006816#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006817 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006818}
6819
6820
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821#if defined(FEAT_MENU) || defined(PROTO)
6822/*
6823 * Add a sub menu to the menu bar.
6824 */
6825 void
6826gui_mch_add_menu(
6827 vimmenu_T *menu,
6828 int pos)
6829{
6830 vimmenu_T *parent = menu->parent;
6831
6832 menu->submenu_id = CreatePopupMenu();
6833 menu->id = s_menu_id++;
6834
6835 if (menu_is_menubar(menu->name))
6836 {
6837 if (is_winnt_3())
6838 {
6839 InsertMenu((parent == NULL) ? s_menuBar : parent->submenu_id,
6840 (UINT)pos, MF_POPUP | MF_STRING | MF_BYPOSITION,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006841 (long_u)menu->submenu_id, (LPCTSTR) menu->name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 }
6843 else
6844 {
6845#ifdef FEAT_MBYTE
6846 WCHAR *wn = NULL;
6847 int n;
6848
6849 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6850 {
6851 /* 'encoding' differs from active codepage: convert menu name
6852 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006853 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 if (wn != NULL)
6855 {
6856 MENUITEMINFOW infow;
6857
6858 infow.cbSize = sizeof(infow);
6859 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6860 | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006861 infow.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 infow.wID = menu->id;
6863 infow.fType = MFT_STRING;
6864 infow.dwTypeData = wn;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006865 infow.cch = (UINT)wcslen(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 infow.hSubMenu = menu->submenu_id;
6867 n = InsertMenuItemW((parent == NULL)
6868 ? s_menuBar : parent->submenu_id,
6869 (UINT)pos, TRUE, &infow);
6870 vim_free(wn);
6871 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
6872 /* Failed, try using non-wide function. */
6873 wn = NULL;
6874 }
6875 }
6876
6877 if (wn == NULL)
6878#endif
6879 {
6880 MENUITEMINFO info;
6881
6882 info.cbSize = sizeof(info);
6883 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006884 info.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 info.wID = menu->id;
6886 info.fType = MFT_STRING;
6887 info.dwTypeData = (LPTSTR)menu->name;
6888 info.cch = (UINT)STRLEN(menu->name);
6889 info.hSubMenu = menu->submenu_id;
6890 InsertMenuItem((parent == NULL)
6891 ? s_menuBar : parent->submenu_id,
6892 (UINT)pos, TRUE, &info);
6893 }
6894 }
6895 }
6896
6897 /* Fix window size if menu may have wrapped */
6898 if (parent == NULL)
6899 gui_mswin_get_menu_height(!gui.starting);
6900#ifdef FEAT_TEAROFF
6901 else if (IsWindow(parent->tearoff_handle))
6902 rebuild_tearoff(parent);
6903#endif
6904}
6905
6906 void
6907gui_mch_show_popupmenu(vimmenu_T *menu)
6908{
6909 POINT mp;
6910
6911 (void)GetCursorPos((LPPOINT)&mp);
6912 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6913}
6914
6915 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006916gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917{
6918 vimmenu_T *menu = gui_find_menu(path_name);
6919
6920 if (menu != NULL)
6921 {
6922 POINT p;
6923
6924 /* Find the position of the current cursor */
6925 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006926 if (mouse_pos)
6927 {
6928 int mx, my;
6929
6930 gui_mch_getmouse(&mx, &my);
6931 p.x += mx;
6932 p.y += my;
6933 }
6934 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 {
6936 p.x += TEXT_X(W_WINCOL(curwin) + curwin->w_wcol + 1);
6937 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6938 }
6939 msg_scroll = FALSE;
6940 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6941 }
6942}
6943
6944#if defined(FEAT_TEAROFF) || defined(PROTO)
6945/*
6946 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6947 * create it as a pseudo-"tearoff menu".
6948 */
6949 void
6950gui_make_tearoff(char_u *path_name)
6951{
6952 vimmenu_T *menu = gui_find_menu(path_name);
6953
6954 /* Found the menu, so tear it off. */
6955 if (menu != NULL)
6956 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6957}
6958#endif
6959
6960/*
6961 * Add a menu item to a menu
6962 */
6963 void
6964gui_mch_add_menu_item(
6965 vimmenu_T *menu,
6966 int idx)
6967{
6968 vimmenu_T *parent = menu->parent;
6969
6970 menu->id = s_menu_id++;
6971 menu->submenu_id = NULL;
6972
6973#ifdef FEAT_TEAROFF
6974 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6975 {
6976 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6977 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6978 }
6979 else
6980#endif
6981#ifdef FEAT_TOOLBAR
6982 if (menu_is_toolbar(parent->name))
6983 {
6984 TBBUTTON newtb;
6985
6986 vim_memset(&newtb, 0, sizeof(newtb));
6987 if (menu_is_separator(menu->name))
6988 {
6989 newtb.iBitmap = 0;
6990 newtb.fsStyle = TBSTYLE_SEP;
6991 }
6992 else
6993 {
6994 newtb.iBitmap = get_toolbar_bitmap(menu);
6995 newtb.fsStyle = TBSTYLE_BUTTON;
6996 }
6997 newtb.idCommand = menu->id;
6998 newtb.fsState = TBSTATE_ENABLED;
6999 newtb.iString = 0;
7000 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
7001 (LPARAM)&newtb);
7002 menu->submenu_id = (HMENU)-1;
7003 }
7004 else
7005#endif
7006 {
7007#ifdef FEAT_MBYTE
7008 WCHAR *wn = NULL;
7009 int n;
7010
7011 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
7012 {
7013 /* 'encoding' differs from active codepage: convert menu item name
7014 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007015 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 if (wn != NULL)
7017 {
7018 n = InsertMenuW(parent->submenu_id, (UINT)idx,
7019 (menu_is_separator(menu->name)
7020 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
7021 (UINT)menu->id, wn);
7022 vim_free(wn);
7023 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
7024 /* Failed, try using non-wide function. */
7025 wn = NULL;
7026 }
7027 }
7028 if (wn == NULL)
7029#endif
7030 InsertMenu(parent->submenu_id, (UINT)idx,
7031 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
7032 | MF_BYPOSITION,
7033 (UINT)menu->id, (LPCTSTR)menu->name);
7034#ifdef FEAT_TEAROFF
7035 if (IsWindow(parent->tearoff_handle))
7036 rebuild_tearoff(parent);
7037#endif
7038 }
7039}
7040
7041/*
7042 * Destroy the machine specific menu widget.
7043 */
7044 void
7045gui_mch_destroy_menu(vimmenu_T *menu)
7046{
7047#ifdef FEAT_TOOLBAR
7048 /*
7049 * is this a toolbar button?
7050 */
7051 if (menu->submenu_id == (HMENU)-1)
7052 {
7053 int iButton;
7054
7055 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
7056 (WPARAM)menu->id, 0);
7057 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
7058 }
7059 else
7060#endif
7061 {
7062 if (menu->parent != NULL
7063 && menu_is_popup(menu->parent->dname)
7064 && menu->parent->submenu_id != NULL)
7065 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
7066 else
7067 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
7068 if (menu->submenu_id != NULL)
7069 DestroyMenu(menu->submenu_id);
7070#ifdef FEAT_TEAROFF
7071 if (IsWindow(menu->tearoff_handle))
7072 DestroyWindow(menu->tearoff_handle);
7073 if (menu->parent != NULL
7074 && menu->parent->children != NULL
7075 && IsWindow(menu->parent->tearoff_handle))
7076 {
7077 /* This menu must not show up when rebuilding the tearoff window. */
7078 menu->modes = 0;
7079 rebuild_tearoff(menu->parent);
7080 }
7081#endif
7082 }
7083}
7084
7085#ifdef FEAT_TEAROFF
7086 static void
7087rebuild_tearoff(vimmenu_T *menu)
7088{
7089 /*hackish*/
7090 char_u tbuf[128];
7091 RECT trect;
7092 RECT rct;
7093 RECT roct;
7094 int x, y;
7095
7096 HWND thwnd = menu->tearoff_handle;
7097
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007098 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 if (GetWindowRect(thwnd, &trect)
7100 && GetWindowRect(s_hwnd, &rct)
7101 && GetClientRect(s_hwnd, &roct))
7102 {
7103 x = trect.left - rct.left;
7104 y = (trect.top - rct.bottom + roct.bottom);
7105 }
7106 else
7107 {
7108 x = y = 0xffffL;
7109 }
7110 DestroyWindow(thwnd);
7111 if (menu->children != NULL)
7112 {
7113 gui_mch_tearoff(tbuf, menu, x, y);
7114 if (IsWindow(menu->tearoff_handle))
7115 (void) SetWindowPos(menu->tearoff_handle,
7116 NULL,
7117 (int)trect.left,
7118 (int)trect.top,
7119 0, 0,
7120 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
7121 }
7122}
7123#endif /* FEAT_TEAROFF */
7124
7125/*
7126 * Make a menu either grey or not grey.
7127 */
7128 void
7129gui_mch_menu_grey(
7130 vimmenu_T *menu,
7131 int grey)
7132{
7133#ifdef FEAT_TOOLBAR
7134 /*
7135 * is this a toolbar button?
7136 */
7137 if (menu->submenu_id == (HMENU)-1)
7138 {
7139 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
7140 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
7141 }
7142 else
7143#endif
7144 if (grey)
7145 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_GRAYED);
7146 else
7147 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
7148
7149#ifdef FEAT_TEAROFF
7150 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
7151 {
7152 WORD menuID;
7153 HWND menuHandle;
7154
7155 /*
7156 * A tearoff button has changed state.
7157 */
7158 if (menu->children == NULL)
7159 menuID = (WORD)(menu->id);
7160 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007161 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
7163 if (menuHandle)
7164 EnableWindow(menuHandle, !grey);
7165
7166 }
7167#endif
7168}
7169
7170#endif /* FEAT_MENU */
7171
7172
7173/* define some macros used to make the dialogue creation more readable */
7174
7175#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
7176#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007177#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178
7179#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
7180/*
7181 * stuff for dialogs
7182 */
7183
7184/*
7185 * The callback routine used by all the dialogs. Very simple. First,
7186 * acknowledges the INITDIALOG message so that Windows knows to do standard
7187 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
7188 * pressed, return that button's ID - IDCANCEL (2), which is the button's
7189 * number.
7190 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007191/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 static LRESULT CALLBACK
7193dialog_callback(
7194 HWND hwnd,
7195 UINT message,
7196 WPARAM wParam,
7197 LPARAM lParam)
7198{
7199 if (message == WM_INITDIALOG)
7200 {
7201 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
7202 /* Set focus to the dialog. Set the default button, if specified. */
7203 (void)SetFocus(hwnd);
7204 if (dialog_default_button > IDCANCEL)
7205 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007206 else
7207 /* We don't have a default, set focus on another element of the
7208 * dialog window, probably the icon */
7209 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 return FALSE;
7211 }
7212
7213 if (message == WM_COMMAND)
7214 {
7215 int button = LOWORD(wParam);
7216
7217 /* Don't end the dialog if something was selected that was
7218 * not a button.
7219 */
7220 if (button >= DLG_NONBUTTON_CONTROL)
7221 return TRUE;
7222
7223 /* If the edit box exists, copy the string. */
7224 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007225 {
7226# if defined(FEAT_MBYTE) && defined(WIN3264)
7227 /* If the OS is Windows NT, and 'encoding' differs from active
7228 * codepage: use wide function and convert text. */
7229 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
7230 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007231 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007232 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
7233 char_u *p;
7234
7235 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
7236 p = utf16_to_enc(wp, NULL);
7237 vim_strncpy(s_textfield, p, IOSIZE);
7238 vim_free(p);
7239 vim_free(wp);
7240 }
7241 else
7242# endif
7243 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007244 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246
7247 /*
7248 * Need to check for IDOK because if the user just hits Return to
7249 * accept the default value, some reason this is what we get.
7250 */
7251 if (button == IDOK)
7252 {
7253 if (dialog_default_button > IDCANCEL)
7254 EndDialog(hwnd, dialog_default_button);
7255 }
7256 else
7257 EndDialog(hwnd, button - IDCANCEL);
7258 return TRUE;
7259 }
7260
7261 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7262 {
7263 EndDialog(hwnd, 0);
7264 return TRUE;
7265 }
7266 return FALSE;
7267}
7268
7269/*
7270 * Create a dialog dynamically from the parameter strings.
7271 * type = type of dialog (question, alert, etc.)
7272 * title = dialog title. may be NULL for default title.
7273 * message = text to display. Dialog sizes to accommodate it.
7274 * buttons = '\n' separated list of button captions, default first.
7275 * dfltbutton = number of default button.
7276 *
7277 * This routine returns 1 if the first button is pressed,
7278 * 2 for the second, etc.
7279 *
7280 * 0 indicates Esc was pressed.
7281 * -1 for unexpected error
7282 *
7283 * If stubbing out this fn, return 1.
7284 */
7285
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007286static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287{
7288 "IDR_VIM",
7289 "IDR_VIM_ERROR",
7290 "IDR_VIM_ALERT",
7291 "IDR_VIM_INFO",
7292 "IDR_VIM_QUESTION"
7293};
7294
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 int
7296gui_mch_dialog(
7297 int type,
7298 char_u *title,
7299 char_u *message,
7300 char_u *buttons,
7301 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007302 char_u *textfield,
7303 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304{
7305 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007306 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 int numButtons;
7308 int *buttonWidths, *buttonPositions;
7309 int buttonYpos;
7310 int nchar, i;
7311 DWORD lStyle;
7312 int dlgwidth = 0;
7313 int dlgheight;
7314 int editboxheight;
7315 int horizWidth = 0;
7316 int msgheight;
7317 char_u *pstart;
7318 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007319 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 char_u *tbuffer;
7321 RECT rect;
7322 HWND hwnd;
7323 HDC hdc;
7324 HFONT font, oldFont;
7325 TEXTMETRIC fontInfo;
7326 int fontHeight;
7327 int textWidth, minButtonWidth, messageWidth;
7328 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007329 int maxDialogHeight;
7330 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 int vertical;
7332 int dlgPaddingX;
7333 int dlgPaddingY;
7334#ifdef USE_SYSMENU_FONT
7335 LOGFONT lfSysmenu;
7336 int use_lfSysmenu = FALSE;
7337#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007338 garray_T ga;
7339 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340
7341#ifndef NO_CONSOLE
7342 /* Don't output anything in silent mode ("ex -s") */
7343 if (silent_mode)
7344 return dfltbutton; /* return default option */
7345#endif
7346
Bram Moolenaar748bf032005-02-02 23:04:36 +00007347 if (s_hwnd == NULL)
7348 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349
7350 if ((type < 0) || (type > VIM_LAST_TYPE))
7351 type = 0;
7352
7353 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007354 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007355 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007356 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357
7358 if (p == NULL)
7359 return -1;
7360
7361 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007362 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7364 * const.
7365 */
7366 tbuffer = vim_strsave(buttons);
7367 if (tbuffer == NULL)
7368 return -1;
7369
7370 --dfltbutton; /* Change from one-based to zero-based */
7371
7372 /* Count buttons */
7373 numButtons = 1;
7374 for (i = 0; tbuffer[i] != '\0'; i++)
7375 {
7376 if (tbuffer[i] == DLG_BUTTON_SEP)
7377 numButtons++;
7378 }
7379 if (dfltbutton >= numButtons)
7380 dfltbutton = -1;
7381
7382 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007383 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 if (buttonWidths == NULL)
7385 return -1;
7386
7387 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007388 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 if (buttonPositions == NULL)
7390 return -1;
7391
7392 /*
7393 * Calculate how big the dialog must be.
7394 */
7395 hwnd = GetDesktopWindow();
7396 hdc = GetWindowDC(hwnd);
7397#ifdef USE_SYSMENU_FONT
7398 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7399 {
7400 font = CreateFontIndirect(&lfSysmenu);
7401 use_lfSysmenu = TRUE;
7402 }
7403 else
7404#endif
7405 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7406 VARIABLE_PITCH , DLG_FONT_NAME);
7407 if (s_usenewlook)
7408 {
7409 oldFont = SelectFont(hdc, font);
7410 dlgPaddingX = DLG_PADDING_X;
7411 dlgPaddingY = DLG_PADDING_Y;
7412 }
7413 else
7414 {
7415 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7416 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7417 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7418 }
7419 GetTextMetrics(hdc, &fontInfo);
7420 fontHeight = fontInfo.tmHeight;
7421
7422 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007423 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424
7425 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007426 if (s_hwnd == NULL)
7427 {
7428 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429
Bram Moolenaarc716c302006-01-21 22:12:51 +00007430 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007431 get_work_area(&workarea_rect);
7432 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7433 if (maxDialogWidth > 600)
7434 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007435 /* Leave some room for the taskbar. */
7436 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007437 }
7438 else
7439 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02007440 /* Use our own window for the size, unless it's very small. */
7441 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007442 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02007443 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007444 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007445 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
7446 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007447
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007448 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007449 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007450 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02007451 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007452 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
7453 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
7454 }
7455
7456 /* Set dlgwidth to width of message.
7457 * Copy the message into "ga", changing NL to CR-NL and inserting line
7458 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 pstart = message;
7460 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007461 msgheight = 0;
7462 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 do
7464 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007465 msgheight += fontHeight; /* at least one line */
7466
7467 /* Need to figure out where to break the string. The system does it
7468 * at a word boundary, which would mean we can't compute the number of
7469 * wrapped lines. */
7470 textWidth = 0;
7471 last_white = NULL;
7472 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007473 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007474#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007475 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007476#else
7477 l = 1;
7478#endif
7479 if (l == 1 && vim_iswhite(*pend)
7480 && textWidth > maxDialogWidth * 3 / 4)
7481 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007482 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007483 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007484 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007485 /* Line will wrap. */
7486 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007487 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007488 textWidth = 0;
7489
7490 if (last_white != NULL)
7491 {
7492 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007493 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007494 pend = last_white + 1;
7495 last_white = NULL;
7496 }
7497 ga_append(&ga, '\r');
7498 ga_append(&ga, '\n');
7499 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007500 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007501
7502 while (--l >= 0)
7503 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007504 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007505 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007507
7508 ga_append(&ga, '\r');
7509 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 pstart = pend + 1;
7511 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007512
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007513 if (ga.ga_data != NULL)
7514 message = ga.ga_data;
7515
Bram Moolenaar748bf032005-02-02 23:04:36 +00007516 messageWidth += 10; /* roundoff space */
7517
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02007519 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7520 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521
7522 if (msgheight < DLG_ICON_HEIGHT)
7523 msgheight = DLG_ICON_HEIGHT;
7524
7525 /*
7526 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007527 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007529 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 if (!vertical)
7531 {
7532 // Place buttons horizontally if they fit.
7533 horizWidth = dlgPaddingX;
7534 pstart = tbuffer;
7535 i = 0;
7536 do
7537 {
7538 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7539 if (pend == NULL)
7540 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007541 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 if (textWidth < minButtonWidth)
7543 textWidth = minButtonWidth;
7544 textWidth += dlgPaddingX; /* Padding within button */
7545 buttonWidths[i] = textWidth;
7546 buttonPositions[i++] = horizWidth;
7547 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7548 pstart = pend + 1;
7549 } while (*pend != NUL);
7550
7551 if (horizWidth > maxDialogWidth)
7552 vertical = TRUE; // Too wide to fit on the screen.
7553 else if (horizWidth > dlgwidth)
7554 dlgwidth = horizWidth;
7555 }
7556
7557 if (vertical)
7558 {
7559 // Stack buttons vertically.
7560 pstart = tbuffer;
7561 do
7562 {
7563 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7564 if (pend == NULL)
7565 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007566 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 textWidth += dlgPaddingX; /* Padding within button */
7568 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7569 if (textWidth > dlgwidth)
7570 dlgwidth = textWidth;
7571 pstart = pend + 1;
7572 } while (*pend != NUL);
7573 }
7574
7575 if (dlgwidth < DLG_MIN_WIDTH)
7576 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7577
7578 /* start to fill in the dlgtemplate information. addressing by WORDs */
7579 if (s_usenewlook)
7580 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7581 else
7582 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7583
7584 add_long(lStyle);
7585 add_long(0); // (lExtendedStyle)
7586 pnumitems = p; /*save where the number of items must be stored*/
7587 add_word(0); // NumberOfItems(will change later)
7588 add_word(10); // x
7589 add_word(10); // y
7590 add_word(PixelToDialogX(dlgwidth)); // cx
7591
7592 // Dialog height.
7593 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007594 dlgheight = msgheight + 2 * dlgPaddingY
7595 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 else
7597 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7598
7599 // Dialog needs to be taller if contains an edit box.
7600 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7601 if (textfield != NULL)
7602 dlgheight += editboxheight;
7603
Bram Moolenaara95d8232013-08-07 15:27:11 +02007604 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7605 if (dlgheight > maxDialogHeight)
7606 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007607 msgheight = msgheight - (dlgheight - maxDialogHeight);
7608 dlgheight = maxDialogHeight;
7609 scroll_flag = WS_VSCROLL;
7610 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7611 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007612 }
7613
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 add_word(PixelToDialogY(dlgheight));
7615
7616 add_word(0); // Menu
7617 add_word(0); // Class
7618
7619 /* copy the title of the dialog */
7620 nchar = nCopyAnsiToWideChar(p, (title ?
7621 (LPSTR)title :
7622 (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
7623 p += nchar;
7624
7625 if (s_usenewlook)
7626 {
7627 /* do the font, since DS_3DLOOK doesn't work properly */
7628#ifdef USE_SYSMENU_FONT
7629 if (use_lfSysmenu)
7630 {
7631 /* point size */
7632 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7633 GetDeviceCaps(hdc, LOGPIXELSY));
7634 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
7635 }
7636 else
7637#endif
7638 {
7639 *p++ = DLG_FONT_POINT_SIZE; // point size
7640 nchar = nCopyAnsiToWideChar(p, TEXT(DLG_FONT_NAME));
7641 }
7642 p += nchar;
7643 }
7644
7645 buttonYpos = msgheight + 2 * dlgPaddingY;
7646
7647 if (textfield != NULL)
7648 buttonYpos += editboxheight;
7649
7650 pstart = tbuffer;
7651 if (!vertical)
7652 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7653 for (i = 0; i < numButtons; i++)
7654 {
7655 /* get end of this button. */
7656 for ( pend = pstart;
7657 *pend && (*pend != DLG_BUTTON_SEP);
7658 pend++)
7659 ;
7660
7661 if (*pend)
7662 *pend = '\0';
7663
7664 /*
7665 * old NOTE:
7666 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7667 * the focus to the first tab-able button and in so doing makes that
7668 * the default!! Grrr. Workaround: Make the default button the only
7669 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7670 * he/she can use arrow keys.
7671 *
7672 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007673 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 * dialog. Also needed for when the textfield is the default control.
7675 * It appears to work now (perhaps not on Win95?).
7676 */
7677 if (vertical)
7678 {
7679 p = add_dialog_element(p,
7680 (i == dfltbutton
7681 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7682 PixelToDialogX(DLG_VERT_PADDING_X),
7683 PixelToDialogY(buttonYpos /* TBK */
7684 + 2 * fontHeight * i),
7685 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7686 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007687 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 }
7689 else
7690 {
7691 p = add_dialog_element(p,
7692 (i == dfltbutton
7693 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7694 PixelToDialogX(horizWidth + buttonPositions[i]),
7695 PixelToDialogY(buttonYpos), /* TBK */
7696 PixelToDialogX(buttonWidths[i]),
7697 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007698 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 }
7700 pstart = pend + 1; /*next button*/
7701 }
7702 *pnumitems += numButtons;
7703
7704 /* Vim icon */
7705 p = add_dialog_element(p, SS_ICON,
7706 PixelToDialogX(dlgPaddingX),
7707 PixelToDialogY(dlgPaddingY),
7708 PixelToDialogX(DLG_ICON_WIDTH),
7709 PixelToDialogY(DLG_ICON_HEIGHT),
7710 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7711 dlg_icons[type]);
7712
Bram Moolenaar748bf032005-02-02 23:04:36 +00007713 /* Dialog message */
7714 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7715 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7716 PixelToDialogY(dlgPaddingY),
7717 (WORD)(PixelToDialogX(messageWidth) + 1),
7718 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007719 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720
7721 /* Edit box */
7722 if (textfield != NULL)
7723 {
7724 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7725 PixelToDialogX(2 * dlgPaddingX),
7726 PixelToDialogY(2 * dlgPaddingY + msgheight),
7727 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7728 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007729 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 *pnumitems += 1;
7731 }
7732
7733 *pnumitems += 2;
7734
7735 SelectFont(hdc, oldFont);
7736 DeleteObject(font);
7737 ReleaseDC(hwnd, hdc);
7738
7739 /* Let the dialog_callback() function know which button to make default
7740 * If we have an edit box, make that the default. We also need to tell
7741 * dialog_callback() if this dialog contains an edit box or not. We do
7742 * this by setting s_textfield if it does.
7743 */
7744 if (textfield != NULL)
7745 {
7746 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7747 s_textfield = textfield;
7748 }
7749 else
7750 {
7751 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7752 s_textfield = NULL;
7753 }
7754
7755 /* show the dialog box modally and get a return value */
7756 nchar = (int)DialogBoxIndirect(
7757 s_hinst,
7758 (LPDLGTEMPLATE)pdlgtemplate,
7759 s_hwnd,
7760 (DLGPROC)dialog_callback);
7761
7762 LocalFree(LocalHandle(pdlgtemplate));
7763 vim_free(tbuffer);
7764 vim_free(buttonWidths);
7765 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007766 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767
7768 /* Focus back to our window (for when MDI is used). */
7769 (void)SetFocus(s_hwnd);
7770
7771 return nchar;
7772}
7773
7774#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007775
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776/*
7777 * Put a simple element (basic class) onto a dialog template in memory.
7778 * return a pointer to where the next item should be added.
7779 *
7780 * parameters:
7781 * lStyle = additional style flags
7782 * (be careful, NT3.51 & Win32s will ignore the new ones)
7783 * x,y = x & y positions IN DIALOG UNITS
7784 * w,h = width and height IN DIALOG UNITS
7785 * Id = ID used in messages
7786 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7787 * caption = usually text or resource name
7788 *
7789 * TODO: use the length information noted here to enable the dialog creation
7790 * routines to work out more exactly how much memory they need to alloc.
7791 */
7792 static PWORD
7793add_dialog_element(
7794 PWORD p,
7795 DWORD lStyle,
7796 WORD x,
7797 WORD y,
7798 WORD w,
7799 WORD h,
7800 WORD Id,
7801 WORD clss,
7802 const char *caption)
7803{
7804 int nchar;
7805
7806 p = lpwAlign(p); /* Align to dword boundary*/
7807 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7808 *p++ = LOWORD(lStyle);
7809 *p++ = HIWORD(lStyle);
7810 *p++ = 0; // LOWORD (lExtendedStyle)
7811 *p++ = 0; // HIWORD (lExtendedStyle)
7812 *p++ = x;
7813 *p++ = y;
7814 *p++ = w;
7815 *p++ = h;
7816 *p++ = Id; //9 or 10 words in all
7817
7818 *p++ = (WORD)0xffff;
7819 *p++ = clss; //2 more here
7820
7821 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption); //strlen(caption)+1
7822 p += nchar;
7823
7824 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7825
7826 return p; //total = 15+ (strlen(caption)) words
7827 // = 30 + 2(strlen(caption) bytes reqd
7828}
7829
7830
7831/*
7832 * Helper routine. Take an input pointer, return closest pointer that is
7833 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7834 */
7835 static LPWORD
7836lpwAlign(
7837 LPWORD lpIn)
7838{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007839 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007841 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 ul += 3;
7843 ul >>= 2;
7844 ul <<= 2;
7845 return (LPWORD)ul;
7846}
7847
7848/*
7849 * Helper routine. Takes second parameter as Ansi string, copies it to first
7850 * parameter as wide character (16-bits / char) string, and returns integer
7851 * number of wide characters (words) in string (including the trailing wide
7852 * char NULL). Partly taken from the Win32SDK samples.
7853 */
7854 static int
7855nCopyAnsiToWideChar(
7856 LPWORD lpWCStr,
7857 LPSTR lpAnsiIn)
7858{
7859 int nChar = 0;
7860#ifdef FEAT_MBYTE
7861 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7862 int i;
7863 WCHAR *wn;
7864
7865 if (enc_codepage == 0 && (int)GetACP() != enc_codepage)
7866 {
7867 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007868 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 if (wn != NULL)
7870 {
7871 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007872 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 vim_free(wn);
7874 }
7875 }
7876 if (nChar == 0)
7877 /* Use Win32 conversion function. */
7878 nChar = MultiByteToWideChar(
7879 enc_codepage > 0 ? enc_codepage : CP_ACP,
7880 MB_PRECOMPOSED,
7881 lpAnsiIn, len,
7882 lpWCStr, len);
7883 for (i = 0; i < nChar; ++i)
7884 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7885 lpWCStr[i] = (WORD)' ';
7886#else
7887 do
7888 {
7889 if (*lpAnsiIn == '\t')
7890 *lpWCStr++ = (WORD)' ';
7891 else
7892 *lpWCStr++ = (WORD)*lpAnsiIn;
7893 nChar++;
7894 } while (*lpAnsiIn++);
7895#endif
7896
7897 return nChar;
7898}
7899
7900
7901#ifdef FEAT_TEAROFF
7902/*
7903 * The callback function for all the modeless dialogs that make up the
7904 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7905 * thinking its menus have been clicked), and go away when closed.
7906 */
7907 static LRESULT CALLBACK
7908tearoff_callback(
7909 HWND hwnd,
7910 UINT message,
7911 WPARAM wParam,
7912 LPARAM lParam)
7913{
7914 if (message == WM_INITDIALOG)
7915 return (TRUE);
7916
7917 /* May show the mouse pointer again. */
7918 HandleMouseHide(message, lParam);
7919
7920 if (message == WM_COMMAND)
7921 {
7922 if ((WORD)(LOWORD(wParam)) & 0x8000)
7923 {
7924 POINT mp;
7925 RECT rect;
7926
7927 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7928 {
7929 (void)TrackPopupMenu(
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007930 (HMENU)(long_u)(LOWORD(wParam) ^ 0x8000),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7932 (int)rect.right - 8,
7933 (int)mp.y,
7934 (int)0, /*reserved param*/
7935 s_hwnd,
7936 NULL);
7937 /*
7938 * NOTE: The pop-up menu can eat the mouse up event.
7939 * We deal with this in normal.c.
7940 */
7941 }
7942 }
7943 else
7944 /* Pass on messages to the main Vim window */
7945 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7946 /*
7947 * Give main window the focus back: this is so after
7948 * choosing a tearoff button you can start typing again
7949 * straight away.
7950 */
7951 (void)SetFocus(s_hwnd);
7952 return TRUE;
7953 }
7954 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7955 {
7956 DestroyWindow(hwnd);
7957 return TRUE;
7958 }
7959
7960 /* When moved around, give main window the focus back. */
7961 if (message == WM_EXITSIZEMOVE)
7962 (void)SetActiveWindow(s_hwnd);
7963
7964 return FALSE;
7965}
7966#endif
7967
7968
7969/*
7970 * Decide whether to use the "new look" (small, non-bold font) or the "old
7971 * look" (big, clanky font) for dialogs, and work out a few values for use
7972 * later accordingly.
7973 */
7974 static void
7975get_dialog_font_metrics(void)
7976{
7977 HDC hdc;
7978 HFONT hfontTools = 0;
7979 DWORD dlgFontSize;
7980 SIZE size;
7981#ifdef USE_SYSMENU_FONT
7982 LOGFONT lfSysmenu;
7983#endif
7984
7985 s_usenewlook = FALSE;
7986
7987 /*
7988 * For NT3.51 and Win32s, we stick with the old look
7989 * because it matches everything else.
7990 */
7991 if (!is_winnt_3())
7992 {
7993#ifdef USE_SYSMENU_FONT
7994 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7995 hfontTools = CreateFontIndirect(&lfSysmenu);
7996 else
7997#endif
7998 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7999 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
8000
8001 if (hfontTools)
8002 {
8003 hdc = GetDC(s_hwnd);
8004 SelectObject(hdc, hfontTools);
8005 /*
8006 * GetTextMetrics() doesn't return the right value in
8007 * tmAveCharWidth, so we have to figure out the dialog base units
8008 * ourselves.
8009 */
8010 GetTextExtentPoint(hdc,
8011 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
8012 52, &size);
8013 ReleaseDC(s_hwnd, hdc);
8014
8015 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
8016 s_dlgfntheight = (WORD)size.cy;
8017 s_usenewlook = TRUE;
8018 }
8019 }
8020
8021 if (!s_usenewlook)
8022 {
8023 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
8024 s_dlgfntwidth = LOWORD(dlgFontSize);
8025 s_dlgfntheight = HIWORD(dlgFontSize);
8026 }
8027}
8028
8029#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8030/*
8031 * Create a pseudo-"tearoff menu" based on the child
8032 * items of a given menu pointer.
8033 */
8034 static void
8035gui_mch_tearoff(
8036 char_u *title,
8037 vimmenu_T *menu,
8038 int initX,
8039 int initY)
8040{
8041 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
8042 int template_len;
8043 int nchar, textWidth, submenuWidth;
8044 DWORD lStyle;
8045 DWORD lExtendedStyle;
8046 WORD dlgwidth;
8047 WORD menuID;
8048 vimmenu_T *pmenu;
8049 vimmenu_T *the_menu = menu;
8050 HWND hwnd;
8051 HDC hdc;
8052 HFONT font, oldFont;
8053 int col, spaceWidth, len;
8054 int columnWidths[2];
8055 char_u *label, *text;
8056 int acLen = 0;
8057 int nameLen;
8058 int padding0, padding1, padding2 = 0;
8059 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008060 int x;
8061 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062#ifdef USE_SYSMENU_FONT
8063 LOGFONT lfSysmenu;
8064 int use_lfSysmenu = FALSE;
8065#endif
8066
8067 /*
8068 * If this menu is already torn off, move it to the mouse position.
8069 */
8070 if (IsWindow(menu->tearoff_handle))
8071 {
8072 POINT mp;
8073 if (GetCursorPos((LPPOINT)&mp))
8074 {
8075 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
8076 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
8077 }
8078 return;
8079 }
8080
8081 /*
8082 * Create a new tearoff.
8083 */
8084 if (*title == MNU_HIDDEN_CHAR)
8085 title++;
8086
8087 /* Allocate memory to store the dialog template. It's made bigger when
8088 * needed. */
8089 template_len = DLG_ALLOC_SIZE;
8090 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
8091 if (p == NULL)
8092 return;
8093
8094 hwnd = GetDesktopWindow();
8095 hdc = GetWindowDC(hwnd);
8096#ifdef USE_SYSMENU_FONT
8097 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
8098 {
8099 font = CreateFontIndirect(&lfSysmenu);
8100 use_lfSysmenu = TRUE;
8101 }
8102 else
8103#endif
8104 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8105 VARIABLE_PITCH , DLG_FONT_NAME);
8106 if (s_usenewlook)
8107 oldFont = SelectFont(hdc, font);
8108 else
8109 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
8110
8111 /* Calculate width of a single space. Used for padding columns to the
8112 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008113 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114
8115 /* Figure out max width of the text column, the accelerator column and the
8116 * optional submenu column. */
8117 submenuWidth = 0;
8118 for (col = 0; col < 2; col++)
8119 {
8120 columnWidths[col] = 0;
8121 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
8122 {
8123 /* Use "dname" here to compute the width of the visible text. */
8124 text = (col == 0) ? pmenu->dname : pmenu->actext;
8125 if (text != NULL && *text != NUL)
8126 {
8127 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
8128 if (textWidth > columnWidths[col])
8129 columnWidths[col] = textWidth;
8130 }
8131 if (pmenu->children != NULL)
8132 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
8133 }
8134 }
8135 if (columnWidths[1] == 0)
8136 {
8137 /* no accelerators */
8138 if (submenuWidth != 0)
8139 columnWidths[0] += submenuWidth;
8140 else
8141 columnWidths[0] += spaceWidth;
8142 }
8143 else
8144 {
8145 /* there is an accelerator column */
8146 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
8147 columnWidths[1] += submenuWidth;
8148 }
8149
8150 /*
8151 * Now find the total width of our 'menu'.
8152 */
8153 textWidth = columnWidths[0] + columnWidths[1];
8154 if (submenuWidth != 0)
8155 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008156 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
8158 textWidth += submenuWidth;
8159 }
8160 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
8161 if (textWidth > dlgwidth)
8162 dlgwidth = textWidth;
8163 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
8164
8165 /* W95 can't do thin dialogs, they look v. weird! */
8166 if (mch_windows95() && dlgwidth < TEAROFF_MIN_WIDTH)
8167 dlgwidth = TEAROFF_MIN_WIDTH;
8168
8169 /* start to fill in the dlgtemplate information. addressing by WORDs */
8170 if (s_usenewlook)
8171 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
8172 else
8173 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
8174
8175 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
8176 *p++ = LOWORD(lStyle);
8177 *p++ = HIWORD(lStyle);
8178 *p++ = LOWORD(lExtendedStyle);
8179 *p++ = HIWORD(lExtendedStyle);
8180 pnumitems = p; /* save where the number of items must be stored */
8181 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008182 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008184 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 else
8186 *p++ = PixelToDialogX(initX); // x
8187 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008188 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 else
8190 *p++ = PixelToDialogY(initY); // y
8191 *p++ = PixelToDialogX(dlgwidth); // cx
8192 ptrueheight = p;
8193 *p++ = 0; // dialog height: changed later anyway
8194 *p++ = 0; // Menu
8195 *p++ = 0; // Class
8196
8197 /* copy the title of the dialog */
8198 nchar = nCopyAnsiToWideChar(p, ((*title)
8199 ? (LPSTR)title
8200 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
8201 p += nchar;
8202
8203 if (s_usenewlook)
8204 {
8205 /* do the font, since DS_3DLOOK doesn't work properly */
8206#ifdef USE_SYSMENU_FONT
8207 if (use_lfSysmenu)
8208 {
8209 /* point size */
8210 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
8211 GetDeviceCaps(hdc, LOGPIXELSY));
8212 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
8213 }
8214 else
8215#endif
8216 {
8217 *p++ = DLG_FONT_POINT_SIZE; // point size
8218 nchar = nCopyAnsiToWideChar (p, TEXT(DLG_FONT_NAME));
8219 }
8220 p += nchar;
8221 }
8222
8223 /*
8224 * Loop over all the items in the menu.
8225 * But skip over the tearbar.
8226 */
8227 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
8228 menu = menu->children->next;
8229 else
8230 menu = menu->children;
8231 for ( ; menu != NULL; menu = menu->next)
8232 {
8233 if (menu->modes == 0) /* this menu has just been deleted */
8234 continue;
8235 if (menu_is_separator(menu->dname))
8236 {
8237 sepPadding += 3;
8238 continue;
8239 }
8240
8241 /* Check if there still is plenty of room in the template. Make it
8242 * larger when needed. */
8243 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8244 {
8245 WORD *newp;
8246
8247 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8248 if (newp != NULL)
8249 {
8250 template_len += 4096;
8251 mch_memmove(newp, pdlgtemplate,
8252 (char *)p - (char *)pdlgtemplate);
8253 p = newp + (p - pdlgtemplate);
8254 pnumitems = newp + (pnumitems - pdlgtemplate);
8255 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8256 LocalFree(LocalHandle(pdlgtemplate));
8257 pdlgtemplate = newp;
8258 }
8259 }
8260
8261 /* Figure out minimal length of this menu label. Use "name" for the
8262 * actual text, "dname" for estimating the displayed size. "name"
8263 * has "&a" for mnemonic and includes the accelerator. */
8264 len = nameLen = (int)STRLEN(menu->name);
8265 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8266 (int)STRLEN(menu->dname))) / spaceWidth;
8267 len += padding0;
8268
8269 if (menu->actext != NULL)
8270 {
8271 acLen = (int)STRLEN(menu->actext);
8272 len += acLen;
8273 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8274 }
8275 else
8276 textWidth = 0;
8277 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8278 len += padding1;
8279
8280 if (menu->children == NULL)
8281 {
8282 padding2 = submenuWidth / spaceWidth;
8283 len += padding2;
8284 menuID = (WORD)(menu->id);
8285 }
8286 else
8287 {
8288 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008289 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 }
8291
8292 /* Allocate menu label and fill it in */
8293 text = label = alloc((unsigned)len + 1);
8294 if (label == NULL)
8295 break;
8296
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008297 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 text = vim_strchr(text, TAB); /* stop at TAB before actext */
8299 if (text == NULL)
8300 text = label + nameLen; /* no actext, use whole name */
8301 while (padding0-- > 0)
8302 *text++ = ' ';
8303 if (menu->actext != NULL)
8304 {
8305 STRNCPY(text, menu->actext, acLen);
8306 text += acLen;
8307 }
8308 while (padding1-- > 0)
8309 *text++ = ' ';
8310 if (menu->children != NULL)
8311 {
8312 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8313 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8314 }
8315 else
8316 {
8317 while (padding2-- > 0)
8318 *text++ = ' ';
8319 }
8320 *text = NUL;
8321
8322 /*
8323 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8324 * W95/NT4 it makes the tear-off look more like a menu.
8325 */
8326 p = add_dialog_element(p,
8327 BS_PUSHBUTTON|BS_LEFT,
8328 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8329 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8330 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8331 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008332 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 vim_free(label);
8334 (*pnumitems)++;
8335 }
8336
8337 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8338
8339
8340 /* show modelessly */
8341 the_menu->tearoff_handle = CreateDialogIndirect(
8342 s_hinst,
8343 (LPDLGTEMPLATE)pdlgtemplate,
8344 s_hwnd,
8345 (DLGPROC)tearoff_callback);
8346
8347 LocalFree(LocalHandle(pdlgtemplate));
8348 SelectFont(hdc, oldFont);
8349 DeleteObject(font);
8350 ReleaseDC(hwnd, hdc);
8351
8352 /*
8353 * Reassert ourselves as the active window. This is so that after creating
8354 * a tearoff, the user doesn't have to click with the mouse just to start
8355 * typing again!
8356 */
8357 (void)SetActiveWindow(s_hwnd);
8358
8359 /* make sure the right buttons are enabled */
8360 force_menu_update = TRUE;
8361}
8362#endif
8363
8364#if defined(FEAT_TOOLBAR) || defined(PROTO)
8365#include "gui_w32_rc.h"
8366
8367/* This not defined in older SDKs */
8368# ifndef TBSTYLE_FLAT
8369# define TBSTYLE_FLAT 0x0800
8370# endif
8371
8372/*
8373 * Create the toolbar, initially unpopulated.
8374 * (just like the menu, there are no defaults, it's all
8375 * set up through menu.vim)
8376 */
8377 static void
8378initialise_toolbar(void)
8379{
8380 InitCommonControls();
8381 s_toolbarhwnd = CreateToolbarEx(
8382 s_hwnd,
8383 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8384 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008385 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 s_hinst,
8387 IDR_TOOLBAR1, // id of initial bitmap
8388 NULL,
8389 0, // initial number of buttons
8390 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8391 TOOLBAR_BUTTON_HEIGHT,
8392 TOOLBAR_BUTTON_WIDTH,
8393 TOOLBAR_BUTTON_HEIGHT,
8394 sizeof(TBBUTTON)
8395 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008396 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397
8398 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
8399}
8400
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008401 static LRESULT CALLBACK
8402toolbar_wndproc(
8403 HWND hwnd,
8404 UINT uMsg,
8405 WPARAM wParam,
8406 LPARAM lParam)
8407{
8408 HandleMouseHide(uMsg, lParam);
8409 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8410}
8411
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 static int
8413get_toolbar_bitmap(vimmenu_T *menu)
8414{
8415 int i = -1;
8416
8417 /*
8418 * Check user bitmaps first, unless builtin is specified.
8419 */
8420 if (!is_winnt_3() && !menu->icon_builtin)
8421 {
8422 char_u fname[MAXPATHL];
8423 HANDLE hbitmap = NULL;
8424
8425 if (menu->iconfile != NULL)
8426 {
8427 gui_find_iconfile(menu->iconfile, fname, "bmp");
8428 hbitmap = LoadImage(
8429 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008430 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 IMAGE_BITMAP,
8432 TOOLBAR_BUTTON_WIDTH,
8433 TOOLBAR_BUTTON_HEIGHT,
8434 LR_LOADFROMFILE |
8435 LR_LOADMAP3DCOLORS
8436 );
8437 }
8438
8439 /*
8440 * If the LoadImage call failed, or the "icon=" file
8441 * didn't exist or wasn't specified, try the menu name
8442 */
8443 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008444 && (gui_find_bitmap(
8445#ifdef FEAT_MULTI_LANG
8446 menu->en_dname != NULL ? menu->en_dname :
8447#endif
8448 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 hbitmap = LoadImage(
8450 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008451 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 IMAGE_BITMAP,
8453 TOOLBAR_BUTTON_WIDTH,
8454 TOOLBAR_BUTTON_HEIGHT,
8455 LR_LOADFROMFILE |
8456 LR_LOADMAP3DCOLORS
8457 );
8458
8459 if (hbitmap != NULL)
8460 {
8461 TBADDBITMAP tbAddBitmap;
8462
8463 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008464 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465
8466 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8467 (WPARAM)1, (LPARAM)&tbAddBitmap);
8468 /* i will be set to -1 if it fails */
8469 }
8470 }
8471 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8472 i = menu->iconidx;
8473
8474 return i;
8475}
8476#endif
8477
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008478#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8479 static void
8480initialise_tabline(void)
8481{
8482 InitCommonControls();
8483
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008484 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008485 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008486 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8487 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008488 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008489
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008490 gui.tabline_height = TABLINE_HEIGHT;
8491
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008492# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008493 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008494# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008495}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008496
8497 static LRESULT CALLBACK
8498tabline_wndproc(
8499 HWND hwnd,
8500 UINT uMsg,
8501 WPARAM wParam,
8502 LPARAM lParam)
8503{
8504 HandleMouseHide(uMsg, lParam);
8505 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8506}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008507#endif
8508
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8510/*
8511 * Make the GUI window come to the foreground.
8512 */
8513 void
8514gui_mch_set_foreground(void)
8515{
8516 if (IsIconic(s_hwnd))
8517 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8518 SetForegroundWindow(s_hwnd);
8519}
8520#endif
8521
8522#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8523 static void
8524dyn_imm_load(void)
8525{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008526 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 if (hLibImm == NULL)
8528 return;
8529
8530 pImmGetCompositionStringA
8531 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8532 pImmGetCompositionStringW
8533 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8534 pImmGetContext
8535 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8536 pImmAssociateContext
8537 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8538 pImmReleaseContext
8539 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8540 pImmGetOpenStatus
8541 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8542 pImmSetOpenStatus
8543 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
8544 pImmGetCompositionFont
8545 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
8546 pImmSetCompositionFont
8547 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
8548 pImmSetCompositionWindow
8549 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8550 pImmGetConversionStatus
8551 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008552 pImmSetConversionStatus
8553 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554
8555 if ( pImmGetCompositionStringA == NULL
8556 || pImmGetCompositionStringW == NULL
8557 || pImmGetContext == NULL
8558 || pImmAssociateContext == NULL
8559 || pImmReleaseContext == NULL
8560 || pImmGetOpenStatus == NULL
8561 || pImmSetOpenStatus == NULL
8562 || pImmGetCompositionFont == NULL
8563 || pImmSetCompositionFont == NULL
8564 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008565 || pImmGetConversionStatus == NULL
8566 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 {
8568 FreeLibrary(hLibImm);
8569 hLibImm = NULL;
8570 pImmGetContext = NULL;
8571 return;
8572 }
8573
8574 return;
8575}
8576
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577#endif
8578
8579#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8580
8581# ifdef FEAT_XPM_W32
8582# define IMAGE_XPM 100
8583# endif
8584
8585typedef struct _signicon_t
8586{
8587 HANDLE hImage;
8588 UINT uType;
8589#ifdef FEAT_XPM_W32
8590 HANDLE hShape; /* Mask bitmap handle */
8591#endif
8592} signicon_t;
8593
8594 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008595gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596{
8597 signicon_t *sign;
8598 int x, y, w, h;
8599
8600 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8601 return;
8602
8603 x = TEXT_X(col);
8604 y = TEXT_Y(row);
8605 w = gui.char_width * 2;
8606 h = gui.char_height;
8607 switch (sign->uType)
8608 {
8609 case IMAGE_BITMAP:
8610 {
8611 HDC hdcMem;
8612 HBITMAP hbmpOld;
8613
8614 hdcMem = CreateCompatibleDC(s_hdc);
8615 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8616 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8617 SelectObject(hdcMem, hbmpOld);
8618 DeleteDC(hdcMem);
8619 }
8620 break;
8621 case IMAGE_ICON:
8622 case IMAGE_CURSOR:
8623 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8624 break;
8625#ifdef FEAT_XPM_W32
8626 case IMAGE_XPM:
8627 {
8628 HDC hdcMem;
8629 HBITMAP hbmpOld;
8630
8631 hdcMem = CreateCompatibleDC(s_hdc);
8632 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8633 /* Make hole */
8634 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8635
8636 SelectObject(hdcMem, sign->hImage);
8637 /* Paint sign */
8638 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8639 SelectObject(hdcMem, hbmpOld);
8640 DeleteDC(hdcMem);
8641 }
8642 break;
8643#endif
8644 }
8645}
8646
8647 static void
8648close_signicon_image(signicon_t *sign)
8649{
8650 if (sign)
8651 switch (sign->uType)
8652 {
8653 case IMAGE_BITMAP:
8654 DeleteObject((HGDIOBJ)sign->hImage);
8655 break;
8656 case IMAGE_CURSOR:
8657 DestroyCursor((HCURSOR)sign->hImage);
8658 break;
8659 case IMAGE_ICON:
8660 DestroyIcon((HICON)sign->hImage);
8661 break;
8662#ifdef FEAT_XPM_W32
8663 case IMAGE_XPM:
8664 DeleteObject((HBITMAP)sign->hImage);
8665 DeleteObject((HBITMAP)sign->hShape);
8666 break;
8667#endif
8668 }
8669}
8670
8671 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008672gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673{
8674 signicon_t sign, *psign;
8675 char_u *ext;
8676
8677 if (is_winnt_3())
8678 {
8679 EMSG(_(e_signdata));
8680 return NULL;
8681 }
8682
8683 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008684 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 if (ext > signfile)
8686 {
8687 int do_load = 1;
8688
8689 if (!STRICMP(ext, ".bmp"))
8690 sign.uType = IMAGE_BITMAP;
8691 else if (!STRICMP(ext, ".ico"))
8692 sign.uType = IMAGE_ICON;
8693 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8694 sign.uType = IMAGE_CURSOR;
8695 else
8696 do_load = 0;
8697
8698 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008699 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 gui.char_width * 2, gui.char_height,
8701 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8702#ifdef FEAT_XPM_W32
8703 if (!STRICMP(ext, ".xpm"))
8704 {
8705 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008706 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8707 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 }
8709#endif
8710 }
8711
8712 psign = NULL;
8713 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8714 != NULL)
8715 *psign = sign;
8716
8717 if (!psign)
8718 {
8719 if (sign.hImage)
8720 close_signicon_image(&sign);
8721 EMSG(_(e_signdata));
8722 }
8723 return (void *)psign;
8724
8725}
8726
8727 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008728gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729{
8730 if (sign)
8731 {
8732 close_signicon_image((signicon_t *)sign);
8733 vim_free(sign);
8734 }
8735}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737
8738#if defined(FEAT_BEVAL) || defined(PROTO)
8739
8740/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008741 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 *
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00008743 * The only reused thing is gui_beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8745 * to get current mouse position).
8746 *
8747 * Trying to use as more Windows services as possible, and as less
8748 * IE version as possible :)).
8749 *
8750 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8751 * BalloonEval struct.
8752 * 2) Enable/Disable simply create/kill BalloonEval Timer
8753 * 3) When there was enough inactivity, timer procedure posts
8754 * async request to debugger
8755 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8756 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008757 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 */
8759
Bram Moolenaar45360022005-07-21 21:08:21 +00008760/*
8761 * determine whether installed Common Controls support multiline tooltips
8762 * (i.e. their version is >= 4.70
8763 */
8764 int
8765multiline_balloon_available(void)
8766{
8767 HINSTANCE hDll;
8768 static char comctl_dll[] = "comctl32.dll";
8769 static int multiline_tip = MAYBE;
8770
8771 if (multiline_tip != MAYBE)
8772 return multiline_tip;
8773
8774 hDll = GetModuleHandle(comctl_dll);
8775 if (hDll != NULL)
8776 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008777 DLLGETVERSIONPROC pGetVer;
8778 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008779
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008780 if (pGetVer != NULL)
8781 {
8782 DLLVERSIONINFO dvi;
8783 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008784
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008785 ZeroMemory(&dvi, sizeof(dvi));
8786 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008787
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008788 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008789
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008790 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008791 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008792 || (dvi.dwMajorVersion == 4
8793 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008794 {
8795 multiline_tip = TRUE;
8796 return multiline_tip;
8797 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008798 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008799 else
8800 {
8801 /* there is chance we have ancient CommCtl 4.70
8802 which doesn't export DllGetVersion */
8803 DWORD dwHandle = 0;
8804 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8805 if (len > 0)
8806 {
8807 VS_FIXEDFILEINFO *ver;
8808 UINT vlen = 0;
8809 void *data = alloc(len);
8810
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008811 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008812 && GetFileVersionInfo(comctl_dll, 0, len, data)
8813 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8814 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008815 && HIWORD(ver->dwFileVersionMS) > 4)
8816 || ((HIWORD(ver->dwFileVersionMS) == 4
8817 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008818 {
8819 vim_free(data);
8820 multiline_tip = TRUE;
8821 return multiline_tip;
8822 }
8823 vim_free(data);
8824 }
8825 }
8826 }
8827 multiline_tip = FALSE;
8828 return multiline_tip;
8829}
8830
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008832make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833{
Bram Moolenaar45360022005-07-21 21:08:21 +00008834 TOOLINFO *pti;
8835 int ToolInfoSize;
8836
8837 if (multiline_balloon_available() == TRUE)
8838 ToolInfoSize = sizeof(TOOLINFO_NEW);
8839 else
8840 ToolInfoSize = sizeof(TOOLINFO);
8841
8842 pti = (TOOLINFO *)alloc(ToolInfoSize);
8843 if (pti == NULL)
8844 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845
8846 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
8847 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8848 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8849 beval->target, NULL, s_hinst, NULL);
8850
8851 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8852 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8853
Bram Moolenaar45360022005-07-21 21:08:21 +00008854 pti->cbSize = ToolInfoSize;
8855 pti->uFlags = TTF_SUBCLASS;
8856 pti->hwnd = beval->target;
8857 pti->hinst = 0; /* Don't use string resources */
8858 pti->uId = ID_BEVAL_TOOLTIP;
8859
8860 if (multiline_balloon_available() == TRUE)
8861 {
8862 RECT rect;
8863 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
8864 pti->lpszText = LPSTR_TEXTCALLBACK;
8865 ptin->lParam = (LPARAM)text;
8866 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
8867 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8868 (LPARAM)rect.right);
8869 }
8870 else
8871 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872
8873 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00008874 pti->rect.left = pt.x - 3;
8875 pti->rect.top = pt.y - 3;
8876 pti->rect.right = pt.x + 3;
8877 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878
Bram Moolenaar45360022005-07-21 21:08:21 +00008879 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 /* Make tooltip appear sooner */
8881 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008882 /* I've performed some tests and it seems the longest possible life time
8883 * of tooltip is 30 seconds */
8884 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 /*
8886 * HACK: force tooltip to appear, because it'll not appear until
8887 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008888 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008890 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00008892 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893}
8894
8895 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008896delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008898 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899}
8900
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008901/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008903BevalTimerProc(
8904 HWND hwnd,
8905 UINT uMsg,
8906 UINT_PTR idEvent,
8907 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908{
8909 POINT pt;
8910 RECT rect;
8911
8912 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8913 return;
8914
8915 GetCursorPos(&pt);
8916 if (WindowFromPoint(pt) != s_textArea)
8917 return;
8918
8919 ScreenToClient(s_textArea, &pt);
8920 GetClientRect(s_textArea, &rect);
8921 if (!PtInRect(&rect, pt))
8922 return;
8923
8924 if (LastActivity > 0
8925 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8926 && (cur_beval->showState != ShS_PENDING
8927 || abs(cur_beval->x - pt.x) > 3
8928 || abs(cur_beval->y - pt.y) > 3))
8929 {
8930 /* Pointer resting in one place long enough, it's time to show
8931 * the tooltip. */
8932 cur_beval->showState = ShS_PENDING;
8933 cur_beval->x = pt.x;
8934 cur_beval->y = pt.y;
8935
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008936 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937
8938 if (cur_beval->msgCB != NULL)
8939 (*cur_beval->msgCB)(cur_beval, 0);
8940 }
8941}
8942
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008943/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008945gui_mch_disable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008947 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008949 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950}
8951
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008952/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008954gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008956 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 if (beval == NULL)
8958 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008959 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008960 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008961 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962}
8963
8964 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008965gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966{
8967 POINT pt;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008968 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 if (beval->showState == ShS_SHOWING)
8970 return;
8971 GetCursorPos(&pt);
8972 ScreenToClient(s_textArea, &pt);
8973
8974 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
8975 /* cursor is still here */
8976 {
8977 gui_mch_disable_beval_area(cur_beval);
8978 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008979 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008981 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982}
8983
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008984/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008986gui_mch_create_beval_area(
8987 void *target, /* ignored, always use s_textArea */
8988 char_u *mesg,
8989 void (*mesgCB)(BalloonEval *, int),
8990 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991{
8992 /* partially stolen from gui_beval.c */
8993 BalloonEval *beval;
8994
8995 if (mesg != NULL && mesgCB != NULL)
8996 {
8997 EMSG(_("E232: Cannot create BalloonEval with both message and callback"));
8998 return NULL;
8999 }
9000
9001 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
9002 if (beval != NULL)
9003 {
9004 beval->target = s_textArea;
9005 beval->balloon = NULL;
9006
9007 beval->showState = ShS_NEUTRAL;
9008 beval->x = 0;
9009 beval->y = 0;
9010 beval->msg = mesg;
9011 beval->msgCB = mesgCB;
9012 beval->clientData = clientData;
9013
9014 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 cur_beval = beval;
9016
9017 if (p_beval)
9018 gui_mch_enable_beval_area(beval);
9019
9020 }
9021 return beval;
9022}
9023
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009024/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 static void
Bram Moolenaar442b4222010-05-24 21:34:22 +02009026Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027{
9028 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
9029 return;
9030
9031 if (cur_beval != NULL)
9032 {
Bram Moolenaar45360022005-07-21 21:08:21 +00009033 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 {
Bram Moolenaar45360022005-07-21 21:08:21 +00009035 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009036 // TRACE0("TTN_SHOW {{{");
9037 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00009038 break;
9039 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009040 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 delete_tooltip(cur_beval);
9042 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009043 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044
9045 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00009046 break;
9047 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00009048 {
9049 /* if you get there then we have new common controls */
9050 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
9051 info->lpszText = (LPSTR)info->lParam;
9052 info->uFlags |= TTF_DI_SETITEM;
9053 }
Bram Moolenaar45360022005-07-21 21:08:21 +00009054 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 }
9056 }
9057}
9058
9059 static void
9060TrackUserActivity(UINT uMsg)
9061{
9062 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
9063 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
9064 LastActivity = GetTickCount();
9065}
9066
9067 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01009068gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069{
9070 vim_free(beval);
9071}
9072#endif /* FEAT_BEVAL */
9073
9074#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
9075/*
9076 * We have multiple signs to draw at the same location. Draw the
9077 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
9078 */
9079 void
9080netbeans_draw_multisign_indicator(int row)
9081{
9082 int i;
9083 int y;
9084 int x;
9085
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009086 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009087 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009088
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 x = 0;
9090 y = TEXT_Y(row);
9091
9092 for (i = 0; i < gui.char_height - 3; i++)
9093 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
9094
9095 SetPixel(s_hdc, x+0, y, gui.currFgColor);
9096 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9097 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
9098 SetPixel(s_hdc, x+1, y, gui.currFgColor);
9099 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9100 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
9101 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9102}
Bram Moolenaare0874f82016-01-24 20:36:41 +01009103#endif