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