blob: ac0fb11f690e7e9bbba4c2f7c8139e0139a41416 [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 *
13 * GUI support for Microsoft Windows. Win32 initially; maybe Win16 later
14 *
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 Moolenaarb8e0bdb2014-11-12 16:10:48 +010039#if defined(FEAT_DIRECTX) || defined(PROTO)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020040 int
41directx_enabled(void)
42{
43 if (s_dwc != NULL)
44 return 1;
45 else if (s_directx_load_attempted)
46 return 0;
47 /* load DirectX */
48 DWrite_Init();
49 s_directx_load_attempted = 1;
50 s_dwc = DWriteContext_Open();
51 return s_dwc != NULL ? 1 : 0;
52}
53#endif
54
55#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
56 int
57gui_mch_set_rendering_options(char_u *s)
58{
59#ifdef FEAT_DIRECTX
60 int retval = FAIL;
61 char_u *p, *q;
62
63 int dx_enable = 0;
64 int dx_flags = 0;
65 float dx_gamma = 0.0f;
66 float dx_contrast = 0.0f;
67 float dx_level = 0.0f;
68 int dx_geom = 0;
69 int dx_renmode = 0;
70 int dx_taamode = 0;
71
72 /* parse string as rendering options. */
73 for (p = s; p != NULL && *p != NUL; )
74 {
75 char_u item[256];
76 char_u name[128];
77 char_u value[128];
78
Bram Moolenaarcde88542015-08-11 19:14:00 +020079 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020080 if (p == NULL)
81 break;
82 q = &item[0];
83 copy_option_part(&q, name, sizeof(name), ":");
84 if (q == NULL)
85 return FAIL;
86 copy_option_part(&q, value, sizeof(value), ":");
87
88 if (STRCMP(name, "type") == 0)
89 {
90 if (STRCMP(value, "directx") == 0)
91 dx_enable = 1;
92 else
93 return FAIL;
94 }
95 else if (STRCMP(name, "gamma") == 0)
96 {
97 dx_flags |= 1 << 0;
98 dx_gamma = (float)atof(value);
99 }
100 else if (STRCMP(name, "contrast") == 0)
101 {
102 dx_flags |= 1 << 1;
103 dx_contrast = (float)atof(value);
104 }
105 else if (STRCMP(name, "level") == 0)
106 {
107 dx_flags |= 1 << 2;
108 dx_level = (float)atof(value);
109 }
110 else if (STRCMP(name, "geom") == 0)
111 {
112 dx_flags |= 1 << 3;
113 dx_geom = atoi(value);
114 if (dx_geom < 0 || dx_geom > 2)
115 return FAIL;
116 }
117 else if (STRCMP(name, "renmode") == 0)
118 {
119 dx_flags |= 1 << 4;
120 dx_renmode = atoi(value);
121 if (dx_renmode < 0 || dx_renmode > 6)
122 return FAIL;
123 }
124 else if (STRCMP(name, "taamode") == 0)
125 {
126 dx_flags |= 1 << 5;
127 dx_taamode = atoi(value);
128 if (dx_taamode < 0 || dx_taamode > 3)
129 return FAIL;
130 }
131 else
132 return FAIL;
133 }
134
135 /* Enable DirectX/DirectWrite */
136 if (dx_enable)
137 {
138 if (!directx_enabled())
139 return FAIL;
140 DWriteContext_SetRenderingParams(s_dwc, NULL);
141 if (dx_flags)
142 {
143 DWriteRenderingParams param;
144 DWriteContext_GetRenderingParams(s_dwc, &param);
145 if (dx_flags & (1 << 0))
146 param.gamma = dx_gamma;
147 if (dx_flags & (1 << 1))
148 param.enhancedContrast = dx_contrast;
149 if (dx_flags & (1 << 2))
150 param.clearTypeLevel = dx_level;
151 if (dx_flags & (1 << 3))
152 param.pixelGeometry = dx_geom;
153 if (dx_flags & (1 << 4))
154 param.renderingMode = dx_renmode;
155 if (dx_flags & (1 << 5))
156 param.textAntialiasMode = dx_taamode;
157 DWriteContext_SetRenderingParams(s_dwc, &param);
158 }
159 }
160 s_directx_enabled = dx_enable;
161
162 return OK;
163#else
164 return FAIL;
165#endif
166}
167#endif
168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169/*
170 * These are new in Windows ME/XP, only defined in recent compilers.
171 */
172#ifndef HANDLE_WM_XBUTTONUP
173# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
174 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
175#endif
176#ifndef HANDLE_WM_XBUTTONDOWN
177# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
178 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
179#endif
180#ifndef HANDLE_WM_XBUTTONDBLCLK
181# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
182 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
183#endif
184
185/*
186 * Include the common stuff for MS-Windows GUI.
187 */
188#include "gui_w48.c"
189
190#ifdef FEAT_XPM_W32
191# include "xpm_w32.h"
192#endif
193
194#ifdef PROTO
195# define WINAPI
196#endif
197
198#ifdef __MINGW32__
199/*
200 * Add a lot of missing defines.
201 * They are not always missing, we need the #ifndef's.
202 */
203# ifndef _cdecl
204# define _cdecl
205# endif
206# ifndef IsMinimized
207# define IsMinimized(hwnd) IsIconic(hwnd)
208# endif
209# ifndef IsMaximized
210# define IsMaximized(hwnd) IsZoomed(hwnd)
211# endif
212# ifndef SelectFont
213# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
214# endif
215# ifndef GetStockBrush
216# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
217# endif
218# ifndef DeleteBrush
219# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
220# endif
221
222# ifndef HANDLE_WM_RBUTTONDBLCLK
223# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
224 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
225# endif
226# ifndef HANDLE_WM_MBUTTONUP
227# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
228 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
229# endif
230# ifndef HANDLE_WM_MBUTTONDBLCLK
231# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
232 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
233# endif
234# ifndef HANDLE_WM_LBUTTONDBLCLK
235# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
236 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
237# endif
238# ifndef HANDLE_WM_RBUTTONDOWN
239# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
240 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
241# endif
242# ifndef HANDLE_WM_MOUSEMOVE
243# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
244 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
245# endif
246# ifndef HANDLE_WM_RBUTTONUP
247# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
248 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
249# endif
250# ifndef HANDLE_WM_MBUTTONDOWN
251# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
252 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
253# endif
254# ifndef HANDLE_WM_LBUTTONUP
255# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
256 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
257# endif
258# ifndef HANDLE_WM_LBUTTONDOWN
259# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
260 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
261# endif
262# ifndef HANDLE_WM_SYSCHAR
263# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
264 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
265# endif
266# ifndef HANDLE_WM_ACTIVATEAPP
267# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
268 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
269# endif
270# ifndef HANDLE_WM_WINDOWPOSCHANGING
271# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
272 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
273# endif
274# ifndef HANDLE_WM_VSCROLL
275# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
276 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
277# endif
278# ifndef HANDLE_WM_SETFOCUS
279# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
280 ((fn)((hwnd), (HWND)(wParam)), 0L)
281# endif
282# ifndef HANDLE_WM_KILLFOCUS
283# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
284 ((fn)((hwnd), (HWND)(wParam)), 0L)
285# endif
286# ifndef HANDLE_WM_HSCROLL
287# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
288 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
289# endif
290# ifndef HANDLE_WM_DROPFILES
291# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
292 ((fn)((hwnd), (HDROP)(wParam)), 0L)
293# endif
294# ifndef HANDLE_WM_CHAR
295# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
296 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
297# endif
298# ifndef HANDLE_WM_SYSDEADCHAR
299# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
300 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
301# endif
302# ifndef HANDLE_WM_DEADCHAR
303# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
304 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
305# endif
306#endif /* __MINGW32__ */
307
308
309/* Some parameters for tearoff menus. All in pixels. */
310#define TEAROFF_PADDING_X 2
311#define TEAROFF_BUTTON_PAD_X 8
312#define TEAROFF_MIN_WIDTH 200
313#define TEAROFF_SUBMENU_LABEL ">>"
314#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
315
316
317/* For the Intellimouse: */
318#ifndef WM_MOUSEWHEEL
319#define WM_MOUSEWHEEL 0x20a
320#endif
321
322
323#ifdef FEAT_BEVAL
324# define ID_BEVAL_TOOLTIP 200
325# define BEVAL_TEXT_LEN MAXPATHL
326
Bram Moolenaar167632f2010-05-26 21:42:54 +0200327#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +0000328/* Work around old versions of basetsd.h which wrongly declares
329 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +0200330# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +0000331# define UINT_PTR UINT
332#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000333
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100334static void make_tooltip(BalloonEval *beval, char *text, POINT pt);
335static void delete_tooltip(BalloonEval *beval);
336static VOID CALLBACK BevalTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000337
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000339static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +0000341
Bram Moolenaar82881492012-11-20 16:53:39 +0100342
343/* cproto fails on missing include files */
344#ifndef PROTO
345
Bram Moolenaar45360022005-07-21 21:08:21 +0000346/*
347 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000348 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +0000349 */
Bram Moolenaar82881492012-11-20 16:53:39 +0100350# include <pshpack1.h>
351
352#endif
Bram Moolenaar45360022005-07-21 21:08:21 +0000353
354typedef struct _DllVersionInfo
355{
356 DWORD cbSize;
357 DWORD dwMajorVersion;
358 DWORD dwMinorVersion;
359 DWORD dwBuildNumber;
360 DWORD dwPlatformID;
361} DLLVERSIONINFO;
362
Bram Moolenaar82881492012-11-20 16:53:39 +0100363#ifndef PROTO
364# include <poppack.h>
365#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +0000366
Bram Moolenaar45360022005-07-21 21:08:21 +0000367typedef struct tagTOOLINFOA_NEW
368{
369 UINT cbSize;
370 UINT uFlags;
371 HWND hwnd;
Bram Moolenaar281daf62009-12-24 15:11:40 +0000372 UINT_PTR uId;
Bram Moolenaar45360022005-07-21 21:08:21 +0000373 RECT rect;
374 HINSTANCE hinst;
375 LPSTR lpszText;
376 LPARAM lParam;
377} TOOLINFO_NEW;
378
379typedef struct tagNMTTDISPINFO_NEW
380{
381 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +0000382 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +0000383 char szText[80];
384 HINSTANCE hinst;
385 UINT uFlags;
386 LPARAM lParam;
387} NMTTDISPINFO_NEW;
388
Bram Moolenaar45360022005-07-21 21:08:21 +0000389typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
390#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000391# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#endif
393
Bram Moolenaar45360022005-07-21 21:08:21 +0000394#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000395# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +0000396#endif
397
398#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000399# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +0000400#endif
401
402#endif /* defined(FEAT_BEVAL) */
403
Bram Moolenaar2c7a7632007-05-10 18:19:11 +0000404#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
405/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
406 * it here if LPNMTTDISPINFO isn't defined.
407 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
408 * _MSC_VER. */
409# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
410typedef struct tagNMTTDISPINFOA {
411 NMHDR hdr;
412 LPSTR lpszText;
413 char szText[80];
414 HINSTANCE hinst;
415 UINT uFlags;
416 LPARAM lParam;
417} NMTTDISPINFOA, *LPNMTTDISPINFOA;
418# define LPNMTTDISPINFO LPNMTTDISPINFOA
419
420# ifdef FEAT_MBYTE
421typedef struct tagNMTTDISPINFOW {
422 NMHDR hdr;
423 LPWSTR lpszText;
424 WCHAR szText[80];
425 HINSTANCE hinst;
426 UINT uFlags;
427 LPARAM lParam;
428} NMTTDISPINFOW, *LPNMTTDISPINFOW;
429# endif
430# endif
431#endif
432
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000433#ifndef TTN_GETDISPINFOW
434# define TTN_GETDISPINFOW (TTN_FIRST - 10)
435#endif
436
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437/* Local variables: */
438
439#ifdef FEAT_MENU
440static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +0200441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442
443/*
444 * Use the system font for dialogs and tear-off menus. Remove this line to
445 * use DLG_FONT_NAME.
446 */
Bram Moolenaar786989b2010-10-27 12:15:33 +0200447#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448
449#define VIM_NAME "vim"
450#define VIM_CLASS "Vim"
451#define VIM_CLASSW L"Vim"
452
453/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
454 * thus there should be room for every dialog. For tearoffs it's made bigger
455 * when needed. */
456#define DLG_ALLOC_SIZE 16 * 1024
457
458/*
459 * stuff for dialogs, menus, tearoffs etc.
460 */
461static LRESULT APIENTRY dialog_callback(HWND, UINT, WPARAM, LPARAM);
462static LRESULT APIENTRY tearoff_callback(HWND, UINT, WPARAM, LPARAM);
463static PWORD
464add_dialog_element(
465 PWORD p,
466 DWORD lStyle,
467 WORD x,
468 WORD y,
469 WORD w,
470 WORD h,
471 WORD Id,
472 WORD clss,
473 const char *caption);
474static LPWORD lpwAlign(LPWORD);
475static int nCopyAnsiToWideChar(LPWORD, LPSTR);
476static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
477static void get_dialog_font_metrics(void);
478
479static int dialog_default_button = -1;
480
481/* Intellimouse support */
482static int mouse_scroll_lines = 0;
483static UINT msh_msgmousewheel = 0;
484
485static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
486#ifdef FEAT_TOOLBAR
487static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +0200488static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489static int get_toolbar_bitmap(vimmenu_T *menu);
490#endif
491
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000492#ifdef FEAT_GUI_TABLINE
493static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +0200494static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000495#endif
496
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497#ifdef FEAT_MBYTE_IME
498static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
499static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
500#endif
501#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
502# ifdef NOIME
503typedef struct tagCOMPOSITIONFORM {
504 DWORD dwStyle;
505 POINT ptCurrentPos;
506 RECT rcArea;
507} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
508typedef HANDLE HIMC;
509# endif
510
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000511static HINSTANCE hLibImm = NULL;
512static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
513static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
514static HIMC (WINAPI *pImmGetContext)(HWND);
515static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
516static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
517static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
518static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
519static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
520static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
521static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
522static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +0000523static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524static void dyn_imm_load(void);
525#else
526# define pImmGetCompositionStringA ImmGetCompositionStringA
527# define pImmGetCompositionStringW ImmGetCompositionStringW
528# define pImmGetContext ImmGetContext
529# define pImmAssociateContext ImmAssociateContext
530# define pImmReleaseContext ImmReleaseContext
531# define pImmGetOpenStatus ImmGetOpenStatus
532# define pImmSetOpenStatus ImmSetOpenStatus
533# define pImmGetCompositionFont ImmGetCompositionFontA
534# define pImmSetCompositionFont ImmSetCompositionFontA
535# define pImmSetCompositionWindow ImmSetCompositionWindow
536# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +0000537# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538#endif
539
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540/* multi monitor support */
541typedef struct _MONITORINFOstruct
542{
543 DWORD cbSize;
544 RECT rcMonitor;
545 RECT rcWork;
546 DWORD dwFlags;
547} _MONITORINFO;
548
549typedef HANDLE _HMONITOR;
550typedef _HMONITOR (WINAPI *TMonitorFromWindow)(HWND, DWORD);
551typedef BOOL (WINAPI *TGetMonitorInfo)(_HMONITOR, _MONITORINFO *);
552
553static TMonitorFromWindow pMonitorFromWindow = NULL;
554static TGetMonitorInfo pGetMonitorInfo = NULL;
555static HANDLE user32_lib = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556/*
557 * Return TRUE when running under Windows NT 3.x or Win32s, both of which have
558 * less fancy GUI APIs.
559 */
560 static int
561is_winnt_3(void)
562{
563 return ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
564 && os_version.dwMajorVersion == 3)
565 || (os_version.dwPlatformId == VER_PLATFORM_WIN32s));
566}
567
568/*
569 * Return TRUE when running under Win32s.
570 */
571 int
572gui_is_win32s(void)
573{
574 return (os_version.dwPlatformId == VER_PLATFORM_WIN32s);
575}
576
577#ifdef FEAT_MENU
578/*
579 * Figure out how high the menu bar is at the moment.
580 */
581 static int
582gui_mswin_get_menu_height(
583 int fix_window) /* If TRUE, resize window if menu height changed */
584{
585 static int old_menu_height = -1;
586
587 RECT rc1, rc2;
588 int num;
589 int menu_height;
590
591 if (gui.menu_is_active)
592 num = GetMenuItemCount(s_menuBar);
593 else
594 num = 0;
595
596 if (num == 0)
597 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +0100598 else if (IsMinimized(s_hwnd))
599 {
600 /* The height of the menu cannot be determined while the window is
601 * minimized. Take the previous height if the menu is changed in that
602 * state, to avoid that Vim's vertical window size accidentally
603 * increases due to the unaccounted-for menu height. */
604 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 else
607 {
608 if (is_winnt_3()) /* for NT 3.xx */
609 {
610 if (gui.starting)
611 menu_height = GetSystemMetrics(SM_CYMENU);
612 else
613 {
614 RECT r1, r2;
615 int frameht = GetSystemMetrics(SM_CYFRAME);
616 int capht = GetSystemMetrics(SM_CYCAPTION);
617
618 /* get window rect of s_hwnd
619 * get client rect of s_hwnd
620 * get cap height
621 * subtract from window rect, the sum of client height,
622 * (if not maximized)frame thickness, and caption height.
623 */
624 GetWindowRect(s_hwnd, &r1);
625 GetClientRect(s_hwnd, &r2);
626 menu_height = r1.bottom - r1.top - (r2.bottom - r2.top
627 + 2 * frameht * (!IsZoomed(s_hwnd)) + capht);
628 }
629 }
630 else /* win95 and variants (NT 4.0, I guess) */
631 {
632 /*
633 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
634 * seem to have been set yet, so menu wraps in default window
635 * width which is very narrow. Instead just return height of a
636 * single menu item. Will still be wrong when the menu really
637 * should wrap over more than one line.
638 */
639 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
640 if (gui.starting)
641 menu_height = rc1.bottom - rc1.top + 1;
642 else
643 {
644 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
645 menu_height = rc2.bottom - rc1.top + 1;
646 }
647 }
648 }
649
650 if (fix_window && menu_height != old_menu_height)
651 {
Bram Moolenaarafa24992006-03-27 20:58:26 +0000652 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 }
Bram Moolenaar71371b12015-03-24 17:57:12 +0100654 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
656 return menu_height;
657}
658#endif /*FEAT_MENU*/
659
660
661/*
662 * Setup for the Intellimouse
663 */
664 static void
665init_mouse_wheel(void)
666{
667
668#ifndef SPI_GETWHEELSCROLLLINES
669# define SPI_GETWHEELSCROLLLINES 104
670#endif
Bram Moolenaare7566042005-06-17 22:00:15 +0000671#ifndef SPI_SETWHEELSCROLLLINES
672# define SPI_SETWHEELSCROLLLINES 105
673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674
675#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
676#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
677#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
678#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
679
680 HWND hdl_mswheel;
681 UINT msh_msgscrolllines;
682
683 msh_msgmousewheel = 0;
684 mouse_scroll_lines = 3; /* reasonable default */
685
686 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
687 && os_version.dwMajorVersion >= 4)
688 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
689 && ((os_version.dwMajorVersion == 4
690 && os_version.dwMinorVersion >= 10)
691 || os_version.dwMajorVersion >= 5)))
692 {
693 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
694 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
695 &mouse_scroll_lines, 0);
696 }
697 else if (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
698 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
699 && os_version.dwMajorVersion < 4))
700 { /*
701 * If Win95 or NT 3.51,
702 * try to find the hidden point32 window.
703 */
704 hdl_mswheel = FindWindow(VMOUSEZ_CLASSNAME, VMOUSEZ_TITLE);
705 if (hdl_mswheel)
706 {
707 msh_msgscrolllines = RegisterWindowMessage(VMSH_SCROLL_LINES);
708 if (msh_msgscrolllines)
709 {
710 mouse_scroll_lines = (int)SendMessage(hdl_mswheel,
711 msh_msgscrolllines, 0, 0);
712 msh_msgmousewheel = RegisterWindowMessage(VMSH_MOUSEWHEEL);
713 }
714 }
715 }
716}
717
718
719/* Intellimouse wheel handler */
720 static void
721_OnMouseWheel(
722 HWND hwnd,
723 short zDelta)
724{
725/* Treat a mouse wheel event as if it were a scroll request */
726 int i;
727 int size;
728 HWND hwndCtl;
729
730 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
731 {
732 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
733 size = curwin->w_scrollbars[SBAR_RIGHT].size;
734 }
735 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
736 {
737 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
738 size = curwin->w_scrollbars[SBAR_LEFT].size;
739 }
740 else
741 return;
742
743 size = curwin->w_height;
744 if (mouse_scroll_lines == 0)
745 init_mouse_wheel();
746
747 if (mouse_scroll_lines > 0
748 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
749 {
750 for (i = mouse_scroll_lines; i > 0; --i)
751 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
752 }
753 else
754 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
755}
756
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000757#ifdef USE_SYSMENU_FONT
758/*
759 * Get Menu Font.
760 * Return OK or FAIL.
761 */
762 static int
763gui_w32_get_menu_font(LOGFONT *lf)
764{
765 NONCLIENTMETRICS nm;
766
767 nm.cbSize = sizeof(NONCLIENTMETRICS);
768 if (!SystemParametersInfo(
769 SPI_GETNONCLIENTMETRICS,
770 sizeof(NONCLIENTMETRICS),
771 &nm,
772 0))
773 return FAIL;
774 *lf = nm.lfMenuFont;
775 return OK;
776}
777#endif
778
779
780#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
781/*
782 * Set the GUI tabline font to the system menu font
783 */
784 static void
785set_tabline_font(void)
786{
787 LOGFONT lfSysmenu;
788 HFONT font;
789 HWND hwnd;
790 HDC hdc;
791 HFONT hfntOld;
792 TEXTMETRIC tm;
793
794 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
795 return;
796
797 font = CreateFontIndirect(&lfSysmenu);
798
799 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
800
801 /*
802 * Compute the height of the font used for the tab text
803 */
804 hwnd = GetDesktopWindow();
805 hdc = GetWindowDC(hwnd);
806 hfntOld = SelectFont(hdc, font);
807
808 GetTextMetrics(hdc, &tm);
809
810 SelectFont(hdc, hfntOld);
811 ReleaseDC(hwnd, hdc);
812
813 /*
814 * The space used by the tab border and the space between the tab label
815 * and the tab border is included as 7.
816 */
817 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
818}
819#endif
820
Bram Moolenaar520470a2005-06-16 21:59:56 +0000821/*
822 * Invoked when a setting was changed.
823 */
824 static LRESULT CALLBACK
825_OnSettingChange(UINT n)
826{
827 if (n == SPI_SETWHEELSCROLLLINES)
828 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
829 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000830#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
831 if (n == SPI_SETNONCLIENTMETRICS)
832 set_tabline_font();
833#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +0000834 return 0;
835}
836
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837#ifdef FEAT_NETBEANS_INTG
838 static void
839_OnWindowPosChanged(
840 HWND hwnd,
841 const LPWINDOWPOS lpwpos)
842{
843 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100844 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845
846 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
847 || lpwpos->cx != cx || lpwpos->cy != cy))
848 {
849 x = lpwpos->x;
850 y = lpwpos->y;
851 cx = lpwpos->cx;
852 cy = lpwpos->cy;
853 netbeans_frame_moved(x, y);
854 }
855 /* Allow to send WM_SIZE and WM_MOVE */
856 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
857}
858#endif
859
860 static int
861_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 UINT fwSide,
863 LPRECT lprc)
864{
865 int w, h;
866 int valid_w, valid_h;
867 int w_offset, h_offset;
868
869 w = lprc->right - lprc->left;
870 h = lprc->bottom - lprc->top;
871 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
872 w_offset = w - valid_w;
873 h_offset = h - valid_h;
874
875 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
876 || fwSide == WMSZ_BOTTOMLEFT)
877 lprc->left += w_offset;
878 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
879 || fwSide == WMSZ_BOTTOMRIGHT)
880 lprc->right -= w_offset;
881
882 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
883 || fwSide == WMSZ_TOPRIGHT)
884 lprc->top += h_offset;
885 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
886 || fwSide == WMSZ_BOTTOMRIGHT)
887 lprc->bottom -= h_offset;
888 return TRUE;
889}
890
891
892
893 static LRESULT CALLBACK
894_WndProc(
895 HWND hwnd,
896 UINT uMsg,
897 WPARAM wParam,
898 LPARAM lParam)
899{
900 /*
901 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
902 hwnd, uMsg, wParam, lParam);
903 */
904
905 HandleMouseHide(uMsg, lParam);
906
907 s_uMsg = uMsg;
908 s_wParam = wParam;
909 s_lParam = lParam;
910
911 switch (uMsg)
912 {
913 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
914 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
915 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
916 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
917 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
918 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
919 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
920 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
921 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
922#ifdef FEAT_MENU
923 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
924#endif
925 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
926 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
927 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
928 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
929 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
930 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
931 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
932 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
933 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
934#ifdef FEAT_NETBEANS_INTG
935 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
936#endif
937
Bram Moolenaarafa24992006-03-27 20:58:26 +0000938#ifdef FEAT_GUI_TABLINE
939 case WM_RBUTTONUP:
940 {
941 if (gui_mch_showing_tabline())
942 {
943 POINT pt;
944 RECT rect;
945
946 /*
947 * If the cursor is on the tabline, display the tab menu
948 */
949 GetCursorPos((LPPOINT)&pt);
950 GetWindowRect(s_textArea, &rect);
951 if (pt.y < rect.top)
952 {
953 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +0100954 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +0000955 }
956 }
957 return MyWindowProc(hwnd, uMsg, wParam, lParam);
958 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000959 case WM_LBUTTONDBLCLK:
960 {
961 /*
962 * If the user double clicked the tabline, create a new tab
963 */
964 if (gui_mch_showing_tabline())
965 {
966 POINT pt;
967 RECT rect;
968
969 GetCursorPos((LPPOINT)&pt);
970 GetWindowRect(s_textArea, &rect);
971 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000972 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000973 }
974 return MyWindowProc(hwnd, uMsg, wParam, lParam);
975 }
Bram Moolenaarafa24992006-03-27 20:58:26 +0000976#endif
977
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 case WM_QUERYENDSESSION: /* System wants to go down. */
979 gui_shell_closed(); /* Will exit when no changed buffers. */
980 return FALSE; /* Do NOT allow system to go down. */
981
982 case WM_ENDSESSION:
983 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +0100984 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +0100986 return 0L;
987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 break;
989
990 case WM_CHAR:
991 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
992 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000993 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 return 0L;
995
996 case WM_SYSCHAR:
997 /*
998 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
999 * shortcut key, handle like a typed ALT key, otherwise call Windows
1000 * ALT key handling.
1001 */
1002#ifdef FEAT_MENU
1003 if ( !gui.menu_is_active
1004 || p_wak[0] == 'n'
1005 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
1006 )
1007#endif
1008 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001009 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 return 0L;
1011 }
1012#ifdef FEAT_MENU
1013 else
1014 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1015#endif
1016
1017 case WM_SYSKEYUP:
1018#ifdef FEAT_MENU
1019 /* This used to be done only when menu is active: ALT key is used for
1020 * that. But that caused problems when menu is disabled and using
1021 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
1022 * are received, mouse pointer remains hidden. */
1023 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1024#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01001025 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026#endif
1027
1028 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001029 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030
1031 case WM_MOUSEWHEEL:
1032 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01001033 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
Bram Moolenaar520470a2005-06-16 21:59:56 +00001035 /* Notification for change in SystemParametersInfo() */
1036 case WM_SETTINGCHANGE:
1037 return _OnSettingChange((UINT)wParam);
1038
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001039#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 case WM_NOTIFY:
1041 switch (((LPNMHDR) lParam)->code)
1042 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001043# ifdef FEAT_MBYTE
1044 case TTN_GETDISPINFOW:
1045# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001046 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001048 LPNMHDR hdr = (LPNMHDR)lParam;
1049 char_u *str = NULL;
1050 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001051
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001052 vim_free(tt_text);
1053 tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001054
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001055# ifdef FEAT_GUI_TABLINE
1056 if (gui_mch_showing_tabline()
1057 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001058 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001059 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001060 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001061 * Mouse is over the GUI tabline. Display the
1062 * tooltip for the tab under the cursor
1063 *
1064 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001065 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001066 GetCursorPos(&pt);
1067 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001068 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001069 TCHITTESTINFO htinfo;
1070 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001071
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001072 /*
1073 * Get the tab under the cursor
1074 */
1075 htinfo.pt.x = pt.x;
1076 htinfo.pt.y = pt.y;
1077 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
1078 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001079 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001080 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001081
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001082 tp = find_tabpage(idx + 1);
1083 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001084 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001085 get_tabline_label(tp, TRUE);
1086 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001087 }
1088 }
1089 }
1090 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001091# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001092# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001093# ifdef FEAT_GUI_TABLINE
1094 else
1095# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001097 UINT idButton;
1098 vimmenu_T *pMenu;
1099
1100 idButton = (UINT) hdr->idFrom;
1101 pMenu = gui_mswin_find_menu(root_menu, idButton);
1102 if (pMenu)
1103 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001105# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001106 if (str != NULL)
1107 {
1108# ifdef FEAT_MBYTE
1109 if (hdr->code == TTN_GETDISPINFOW)
1110 {
1111 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
1112
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00001113 /* Set the maximum width, this also enables using
1114 * \n for line break. */
1115 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
1116 0, 500);
1117
Bram Moolenaar36f692d2008-11-20 16:10:17 +00001118 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001119 lpdi->lpszText = tt_text;
1120 /* can't show tooltip if failed */
1121 }
1122 else
1123# endif
1124 {
1125 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
1126
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00001127 /* Set the maximum width, this also enables using
1128 * \n for line break. */
1129 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
1130 0, 500);
1131
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001132 if (STRLEN(str) < sizeof(lpdi->szText)
1133 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01001134 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001135 sizeof(lpdi->szText) - 1);
1136 else
1137 lpdi->lpszText = tt_text;
1138 }
1139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 }
1141 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001142# ifdef FEAT_GUI_TABLINE
1143 case TCN_SELCHANGE:
1144 if (gui_mch_showing_tabline()
1145 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01001146 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001147 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01001148 return 0L;
1149 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001150 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00001151
1152 case NM_RCLICK:
1153 if (gui_mch_showing_tabline()
1154 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01001155 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00001156 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01001157 return 0L;
1158 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00001159 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001162# ifdef FEAT_GUI_TABLINE
1163 if (gui_mch_showing_tabline()
1164 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
1165 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1166# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 break;
1168 }
1169 break;
1170#endif
1171#if defined(MENUHINTS) && defined(FEAT_MENU)
1172 case WM_MENUSELECT:
1173 if (((UINT) HIWORD(wParam)
1174 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
1175 == MF_HILITE
1176 && (State & CMDLINE) == 0)
1177 {
1178 UINT idButton;
1179 vimmenu_T *pMenu;
1180 static int did_menu_tip = FALSE;
1181
1182 if (did_menu_tip)
1183 {
1184 msg_clr_cmdline();
1185 setcursor();
1186 out_flush();
1187 did_menu_tip = FALSE;
1188 }
1189
1190 idButton = (UINT)LOWORD(wParam);
1191 pMenu = gui_mswin_find_menu(root_menu, idButton);
1192 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
1193 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
1194 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00001195 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00001197 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 setcursor();
1199 out_flush();
1200 did_menu_tip = TRUE;
1201 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01001202 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 }
1204 break;
1205#endif
1206 case WM_NCHITTEST:
1207 {
1208 LRESULT result;
1209 int x, y;
1210 int xPos = GET_X_LPARAM(lParam);
1211
1212 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
1213 if (result == HTCLIENT)
1214 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001215#ifdef FEAT_GUI_TABLINE
1216 if (gui_mch_showing_tabline())
1217 {
1218 int yPos = GET_Y_LPARAM(lParam);
1219 RECT rct;
1220
1221 /* If the cursor is on the GUI tabline, don't process this
1222 * event */
1223 GetWindowRect(s_textArea, &rct);
1224 if (yPos < rct.top)
1225 return result;
1226 }
1227#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02001228 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 xPos -= x;
1230
1231 if (xPos < 48) /* <VN> TODO should use system metric? */
1232 return HTBOTTOMLEFT;
1233 else
1234 return HTBOTTOMRIGHT;
1235 }
1236 else
1237 return result;
1238 }
1239 /* break; notreached */
1240
1241#ifdef FEAT_MBYTE_IME
1242 case WM_IME_NOTIFY:
1243 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
1244 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01001245 return 1L;
1246
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 case WM_IME_COMPOSITION:
1248 if (!_OnImeComposition(hwnd, wParam, lParam))
1249 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01001250 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251#endif
1252
1253 default:
1254 if (uMsg == msh_msgmousewheel && msh_msgmousewheel != 0)
1255 { /* handle MSH_MOUSEWHEEL messages for Intellimouse */
1256 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01001257 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 }
1259#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001260 else if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {
1262 _OnFindRepl();
1263 }
1264#endif
1265 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1266 }
1267
Bram Moolenaar2787ab92011-12-14 15:23:59 +01001268 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269}
1270
1271/*
1272 * End of call-back routines
1273 */
1274
1275/* parent window, if specified with -P */
1276HWND vim_parent_hwnd = NULL;
1277
1278 static BOOL CALLBACK
1279FindWindowTitle(HWND hwnd, LPARAM lParam)
1280{
1281 char buf[2048];
1282 char *title = (char *)lParam;
1283
1284 if (GetWindowText(hwnd, buf, sizeof(buf)))
1285 {
1286 if (strstr(buf, title) != NULL)
1287 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001288 /* Found it. Store the window ref. and quit searching if MDI
1289 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001291 if (vim_parent_hwnd != NULL)
1292 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 }
1294 }
1295 return TRUE; /* continue searching */
1296}
1297
1298/*
1299 * Invoked for '-P "title"' argument: search for parent application to open
1300 * our window in.
1301 */
1302 void
1303gui_mch_set_parent(char *title)
1304{
1305 EnumWindows(FindWindowTitle, (LPARAM)title);
1306 if (vim_parent_hwnd == NULL)
1307 {
1308 EMSG2(_("E671: Cannot find window title \"%s\""), title);
1309 mch_exit(2);
1310 }
1311}
1312
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001313#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 static void
1315ole_error(char *arg)
1316{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00001317 char buf[IOSIZE];
1318
1319 /* Can't use EMSG() here, we have not finished initialisation yet. */
1320 vim_snprintf(buf, IOSIZE,
1321 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
1322 arg);
1323 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326
1327/*
1328 * Parse the GUI related command-line arguments. Any arguments used are
1329 * deleted from argv, and *argc is decremented accordingly. This is called
1330 * when vim is started, whether or not the GUI has been started.
1331 */
1332 void
1333gui_mch_prepare(int *argc, char **argv)
1334{
1335 int silent = FALSE;
1336 int idx;
1337
1338 /* Check for special OLE command line parameters */
1339 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
1340 {
1341 /* Check for a "-silent" argument first. */
1342 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
1343 && (argv[2][0] == '-' || argv[2][0] == '/'))
1344 {
1345 silent = TRUE;
1346 idx = 2;
1347 }
1348 else
1349 idx = 1;
1350
1351 /* Register Vim as an OLE Automation server */
1352 if (STRICMP(argv[idx] + 1, "register") == 0)
1353 {
1354#ifdef FEAT_OLE
1355 RegisterMe(silent);
1356 mch_exit(0);
1357#else
1358 if (!silent)
1359 ole_error("register");
1360 mch_exit(2);
1361#endif
1362 }
1363
1364 /* Unregister Vim as an OLE Automation server */
1365 if (STRICMP(argv[idx] + 1, "unregister") == 0)
1366 {
1367#ifdef FEAT_OLE
1368 UnregisterMe(!silent);
1369 mch_exit(0);
1370#else
1371 if (!silent)
1372 ole_error("unregister");
1373 mch_exit(2);
1374#endif
1375 }
1376
1377 /* Ignore an -embedding argument. It is only relevant if the
1378 * application wants to treat the case when it is started manually
1379 * differently from the case where it is started via automation (and
1380 * we don't).
1381 */
1382 if (STRICMP(argv[idx] + 1, "embedding") == 0)
1383 {
1384#ifdef FEAT_OLE
1385 *argc = 1;
1386#else
1387 ole_error("embedding");
1388 mch_exit(2);
1389#endif
1390 }
1391 }
1392
1393#ifdef FEAT_OLE
1394 {
1395 int bDoRestart = FALSE;
1396
1397 InitOLE(&bDoRestart);
1398 /* automatically exit after registering */
1399 if (bDoRestart)
1400 mch_exit(0);
1401 }
1402#endif
1403
1404#ifdef FEAT_NETBEANS_INTG
1405 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001406 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 int arg;
1408
1409 for (arg = 1; arg < *argc; arg++)
1410 if (strncmp("-nb", argv[arg], 3) == 0)
1411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 netbeansArg = argv[arg];
1413 mch_memmove(&argv[arg], &argv[arg + 1],
1414 (--*argc - arg) * sizeof(char *));
1415 argv[*argc] = NULL;
1416 break; /* enough? */
1417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 }
1419#endif
1420
1421 /* get the OS version info */
1422 os_version.dwOSVersionInfoSize = sizeof(os_version);
1423 GetVersionEx(&os_version); /* this call works on Win32s, Win95 and WinNT */
1424
1425 /* try and load the user32.dll library and get the entry points for
1426 * multi-monitor-support. */
Bram Moolenaarebbcb822010-10-23 14:02:54 +02001427 if ((user32_lib = vimLoadLib("User32.dll")) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 {
1429 pMonitorFromWindow = (TMonitorFromWindow)GetProcAddress(user32_lib,
1430 "MonitorFromWindow");
1431
1432 /* there are ...A and ...W version of GetMonitorInfo - looking at
1433 * winuser.h, they have exactly the same declaration. */
1434 pGetMonitorInfo = (TGetMonitorInfo)GetProcAddress(user32_lib,
1435 "GetMonitorInfoA");
1436 }
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001437
1438#ifdef FEAT_MBYTE
1439 /* If the OS is Windows NT, use wide functions;
1440 * this enables common dialogs input unicode from IME. */
1441 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
1442 {
1443 pDispatchMessage = DispatchMessageW;
1444 pGetMessage = GetMessageW;
1445 pIsDialogMessage = IsDialogMessageW;
1446 pPeekMessage = PeekMessageW;
1447 }
1448 else
1449 {
1450 pDispatchMessage = DispatchMessageA;
1451 pGetMessage = GetMessageA;
1452 pIsDialogMessage = IsDialogMessageA;
1453 pPeekMessage = PeekMessageA;
1454 }
1455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456}
1457
1458/*
1459 * Initialise the GUI. Create all the windows, set up all the call-backs
1460 * etc.
1461 */
1462 int
1463gui_mch_init(void)
1464{
1465 const char szVimWndClass[] = VIM_CLASS;
1466 const char szTextAreaClass[] = "VimTextArea";
1467 WNDCLASS wndclass;
1468#ifdef FEAT_MBYTE
1469 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01001470 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 WNDCLASSW wndclassw;
1472#endif
1473#ifdef GLOBAL_IME
1474 ATOM atom;
1475#endif
1476
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 /* Return here if the window was already opened (happens when
1478 * gui_mch_dialog() is called early). */
1479 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00001480 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481
1482 /*
1483 * Load the tearoff bitmap
1484 */
1485#ifdef FEAT_TEAROFF
1486 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
1487#endif
1488
1489 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
1490 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
1491#ifdef FEAT_MENU
1492 gui.menu_height = 0; /* Windows takes care of this */
1493#endif
1494 gui.border_width = 0;
1495
1496 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
1497
1498#ifdef FEAT_MBYTE
1499 /* First try using the wide version, so that we can use any title.
1500 * Otherwise only characters in the active codepage will work. */
1501 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
1502 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001503 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 wndclassw.lpfnWndProc = _WndProc;
1505 wndclassw.cbClsExtra = 0;
1506 wndclassw.cbWndExtra = 0;
1507 wndclassw.hInstance = s_hinst;
1508 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
1509 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
1510 wndclassw.hbrBackground = s_brush;
1511 wndclassw.lpszMenuName = NULL;
1512 wndclassw.lpszClassName = szVimWndClassW;
1513
1514 if ((
1515#ifdef GLOBAL_IME
1516 atom =
1517#endif
1518 RegisterClassW(&wndclassw)) == 0)
1519 {
1520 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1521 return FAIL;
1522
1523 /* Must be Windows 98, fall back to non-wide function. */
1524 }
1525 else
1526 wide_WindowProc = TRUE;
1527 }
1528
1529 if (!wide_WindowProc)
1530#endif
1531
1532 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
1533 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001534 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 wndclass.lpfnWndProc = _WndProc;
1536 wndclass.cbClsExtra = 0;
1537 wndclass.cbWndExtra = 0;
1538 wndclass.hInstance = s_hinst;
1539 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
1540 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1541 wndclass.hbrBackground = s_brush;
1542 wndclass.lpszMenuName = NULL;
1543 wndclass.lpszClassName = szVimWndClass;
1544
1545 if ((
1546#ifdef GLOBAL_IME
1547 atom =
1548#endif
1549 RegisterClass(&wndclass)) == 0)
1550 return FAIL;
1551 }
1552
1553 if (vim_parent_hwnd != NULL)
1554 {
1555#ifdef HAVE_TRY_EXCEPT
1556 __try
1557 {
1558#endif
1559 /* Open inside the specified parent window.
1560 * TODO: last argument should point to a CLIENTCREATESTRUCT
1561 * structure. */
1562 s_hwnd = CreateWindowEx(
1563 WS_EX_MDICHILD,
1564 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02001565 WS_OVERLAPPEDWINDOW | WS_CHILD
1566 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
1568 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
1569 100, /* Any value will do */
1570 100, /* Any value will do */
1571 vim_parent_hwnd, NULL,
1572 s_hinst, NULL);
1573#ifdef HAVE_TRY_EXCEPT
1574 }
1575 __except(EXCEPTION_EXECUTE_HANDLER)
1576 {
1577 /* NOP */
1578 }
1579#endif
1580 if (s_hwnd == NULL)
1581 {
1582 EMSG(_("E672: Unable to open window inside MDI application"));
1583 mch_exit(2);
1584 }
1585 }
1586 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001587 {
1588 /* If the provided windowid is not valid reset it to zero, so that it
1589 * is ignored and we open our own window. */
1590 if (IsWindow((HWND)win_socket_id) <= 0)
1591 win_socket_id = 0;
1592
1593 /* Create a window. If win_socket_id is not zero without border and
1594 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00001596 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02001597 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
1598 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00001599 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
1600 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
1601 100, /* Any value will do */
1602 100, /* Any value will do */
1603 NULL, NULL,
1604 s_hinst, NULL);
1605 if (s_hwnd != NULL && win_socket_id != 0)
1606 {
1607 SetParent(s_hwnd, (HWND)win_socket_id);
1608 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
1609 }
1610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
1612 if (s_hwnd == NULL)
1613 return FAIL;
1614
1615#ifdef GLOBAL_IME
1616 global_ime_init(atom, s_hwnd);
1617#endif
1618#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
1619 dyn_imm_load();
1620#endif
1621
1622 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01001623#ifdef FEAT_MBYTE
1624 if (wide_WindowProc)
1625 {
1626 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
1627 {
1628 wndclassw.style = CS_OWNDC;
1629 wndclassw.lpfnWndProc = _TextAreaWndProc;
1630 wndclassw.cbClsExtra = 0;
1631 wndclassw.cbWndExtra = 0;
1632 wndclassw.hInstance = s_hinst;
1633 wndclassw.hIcon = NULL;
1634 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
1635 wndclassw.hbrBackground = NULL;
1636 wndclassw.lpszMenuName = NULL;
1637 wndclassw.lpszClassName = szTextAreaClassW;
1638
1639 if (RegisterClassW(&wndclassw) == 0)
1640 return FAIL;
1641 }
1642 }
1643 else
1644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
1646 {
1647 wndclass.style = CS_OWNDC;
1648 wndclass.lpfnWndProc = _TextAreaWndProc;
1649 wndclass.cbClsExtra = 0;
1650 wndclass.cbWndExtra = 0;
1651 wndclass.hInstance = s_hinst;
1652 wndclass.hIcon = NULL;
1653 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1654 wndclass.hbrBackground = NULL;
1655 wndclass.lpszMenuName = NULL;
1656 wndclass.lpszClassName = szTextAreaClass;
1657
1658 if (RegisterClass(&wndclass) == 0)
1659 return FAIL;
1660 }
1661 s_textArea = CreateWindowEx(
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01001662 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 szTextAreaClass, "Vim text area",
1664 WS_CHILD | WS_VISIBLE, 0, 0,
1665 100, /* Any value will do for now */
1666 100, /* Any value will do for now */
1667 s_hwnd, NULL,
1668 s_hinst, NULL);
1669
1670 if (s_textArea == NULL)
1671 return FAIL;
1672
Bram Moolenaar20321902016-02-17 12:30:17 +01001673#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02001674 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
1675 {
1676 HANDLE hIcon = NULL;
1677
1678 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02001679 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02001680 }
Bram Moolenaar20321902016-02-17 12:30:17 +01001681#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02001682
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683#ifdef FEAT_MENU
1684 s_menuBar = CreateMenu();
1685#endif
1686 s_hdc = GetDC(s_textArea);
1687
1688#ifdef MSWIN16_FASTTEXT
1689 SetBkMode(s_hdc, OPAQUE);
1690#endif
1691
1692#ifdef FEAT_WINDOWS
1693 DragAcceptFiles(s_hwnd, TRUE);
1694#endif
1695
1696 /* Do we need to bother with this? */
1697 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
1698
1699 /* Get background/foreground colors from the system */
1700 gui_mch_def_colors();
1701
1702 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
1703 * file) */
1704 set_normal_colors();
1705
1706 /*
1707 * Check that none of the colors are the same as the background color.
1708 * Then store the current values as the defaults.
1709 */
1710 gui_check_colors();
1711 gui.def_norm_pixel = gui.norm_pixel;
1712 gui.def_back_pixel = gui.back_pixel;
1713
1714 /* Get the colors for the highlight groups (gui_check_colors() might have
1715 * changed them) */
1716 highlight_gui_started();
1717
1718 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01001719 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01001721 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722
1723 /*
1724 * Set up for Intellimouse processing
1725 */
1726 init_mouse_wheel();
1727
1728 /*
1729 * compute a couple of metrics used for the dialogs
1730 */
1731 get_dialog_font_metrics();
1732#ifdef FEAT_TOOLBAR
1733 /*
1734 * Create the toolbar
1735 */
1736 initialise_toolbar();
1737#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001738#ifdef FEAT_GUI_TABLINE
1739 /*
1740 * Create the tabline
1741 */
1742 initialise_tabline();
1743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744#ifdef MSWIN_FIND_REPLACE
1745 /*
1746 * Initialise the dialog box stuff
1747 */
1748 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
1749
1750 /* Initialise the struct */
1751 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01001752 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01001754 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1756 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
1757 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00001758# if defined(FEAT_MBYTE) && defined(WIN3264)
1759 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
1760 s_findrep_struct_w.lpstrFindWhat =
1761 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
1762 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
1763 s_findrep_struct_w.lpstrReplaceWith =
1764 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
1765 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
1766 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
1767 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
1768# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02001771#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01001772# if !defined(_MSC_VER) || (_MSC_VER < 1400)
1773/* Define HandleToLong for old MS and non-MS compilers if not defined. */
1774# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01001775# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01001776# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02001777# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02001778 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02001779 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02001780#endif
1781
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02001782#ifdef FEAT_RENDER_OPTIONS
1783 if (p_rop)
1784 (void)gui_mch_set_rendering_options(p_rop);
1785#endif
1786
Bram Moolenaar748bf032005-02-02 23:04:36 +00001787theend:
1788 /* Display any pending error messages */
1789 display_errors();
1790
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 return OK;
1792}
1793
1794/*
1795 * Get the size of the screen, taking position on multiple monitors into
1796 * account (if supported).
1797 */
1798 static void
1799get_work_area(RECT *spi_rect)
1800{
1801 _HMONITOR mon;
1802 _MONITORINFO moninfo;
1803
1804 /* use these functions only if available */
1805 if (pMonitorFromWindow != NULL && pGetMonitorInfo != NULL)
1806 {
1807 /* work out which monitor the window is on, and get *it's* work area */
1808 mon = pMonitorFromWindow(s_hwnd, 1 /*MONITOR_DEFAULTTOPRIMARY*/);
1809 if (mon != NULL)
1810 {
1811 moninfo.cbSize = sizeof(_MONITORINFO);
1812 if (pGetMonitorInfo(mon, &moninfo))
1813 {
1814 *spi_rect = moninfo.rcWork;
1815 return;
1816 }
1817 }
1818 }
1819 /* this is the old method... */
1820 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
1821}
1822
1823/*
1824 * Set the size of the window to the given width and height in pixels.
1825 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001826/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 void
1828gui_mch_set_shellsize(int width, int height,
Bram Moolenaarafa24992006-03-27 20:58:26 +00001829 int min_width, int min_height, int base_width, int base_height,
1830 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831{
1832 RECT workarea_rect;
1833 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 WINDOWPLACEMENT wndpl;
1835
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001836 /* Try to keep window completely on screen. */
1837 /* Get position of the screen work area. This is the part that is not
1838 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 get_work_area(&workarea_rect);
1840
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001841 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001842 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 wndpl.length = sizeof(WINDOWPLACEMENT);
1844 GetWindowPlacement(s_hwnd, &wndpl);
1845
1846 /* Resizing a maximized window looks very strange, unzoom it first.
1847 * But don't do it when still starting up, it may have been requested in
1848 * the shortcut. */
1849 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
1850 {
1851 ShowWindow(s_hwnd, SW_SHOWNORMAL);
1852 /* Need to get the settings of the normal window. */
1853 GetWindowPlacement(s_hwnd, &wndpl);
1854 }
1855
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02001857 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02001858 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02001859 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02001860 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 + GetSystemMetrics(SM_CYCAPTION)
1862#ifdef FEAT_MENU
1863 + gui_mswin_get_menu_height(FALSE)
1864#endif
1865 ;
1866
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001867 /* The following should take care of keeping Vim on the same monitor, no
1868 * matter if the secondary monitor is left or right of the primary
1869 * monitor. */
1870 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
1871 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00001872
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001873 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001874 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001875 && wndpl.rcNormalPosition.right > workarea_rect.right)
1876 OffsetRect(&wndpl.rcNormalPosition,
1877 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001879 if ((direction & RESIZE_HOR)
1880 && wndpl.rcNormalPosition.left < workarea_rect.left)
1881 OffsetRect(&wndpl.rcNormalPosition,
1882 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883
Bram Moolenaarafa24992006-03-27 20:58:26 +00001884 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001885 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
1886 OffsetRect(&wndpl.rcNormalPosition,
1887 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001889 if ((direction & RESIZE_VERT)
1890 && wndpl.rcNormalPosition.top < workarea_rect.top)
1891 OffsetRect(&wndpl.rcNormalPosition,
1892 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893
1894 /* set window position - we should use SetWindowPlacement rather than
1895 * SetWindowPos as the MSDN docs say the coord systems returned by
1896 * these two are not compatible. */
1897 SetWindowPlacement(s_hwnd, &wndpl);
1898
1899 SetActiveWindow(s_hwnd);
1900 SetFocus(s_hwnd);
1901
1902#ifdef FEAT_MENU
1903 /* Menu may wrap differently now */
1904 gui_mswin_get_menu_height(!gui.starting);
1905#endif
1906}
1907
1908
1909 void
1910gui_mch_set_scrollbar_thumb(
1911 scrollbar_T *sb,
1912 long val,
1913 long size,
1914 long max)
1915{
1916 SCROLLINFO info;
1917
1918 sb->scroll_shift = 0;
1919 while (max > 32767)
1920 {
1921 max = (max + 1) >> 1;
1922 val >>= 1;
1923 size >>= 1;
1924 ++sb->scroll_shift;
1925 }
1926
1927 if (sb->scroll_shift > 0)
1928 ++size;
1929
1930 info.cbSize = sizeof(info);
1931 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
1932 info.nPos = val;
1933 info.nMin = 0;
1934 info.nMax = max;
1935 info.nPage = size;
1936 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
1937}
1938
1939
1940/*
1941 * Set the current text font.
1942 */
1943 void
1944gui_mch_set_font(GuiFont font)
1945{
1946 gui.currFont = font;
1947}
1948
1949
1950/*
1951 * Set the current text foreground color.
1952 */
1953 void
1954gui_mch_set_fg_color(guicolor_T color)
1955{
1956 gui.currFgColor = color;
1957}
1958
1959/*
1960 * Set the current text background color.
1961 */
1962 void
1963gui_mch_set_bg_color(guicolor_T color)
1964{
1965 gui.currBgColor = color;
1966}
1967
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001968/*
1969 * Set the current text special color.
1970 */
1971 void
1972gui_mch_set_sp_color(guicolor_T color)
1973{
1974 gui.currSpColor = color;
1975}
1976
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977#if defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)
1978/*
1979 * Multi-byte handling, originally by Sung-Hoon Baek.
1980 * First static functions (no prototypes generated).
1981 */
1982#ifdef _MSC_VER
1983# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
1984#endif
1985#include <imm.h>
1986
1987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 * handle WM_IME_NOTIFY message
1989 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001990/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 static LRESULT
1992_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData)
1993{
1994 LRESULT lResult = 0;
1995 HIMC hImc;
1996
1997 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
1998 return lResult;
1999 switch (dwCommand)
2000 {
2001 case IMN_SETOPENSTATUS:
2002 if (pImmGetOpenStatus(hImc))
2003 {
2004 pImmSetCompositionFont(hImc, &norm_logfont);
2005 im_set_position(gui.row, gui.col);
2006
2007 /* Disable langmap */
2008 State &= ~LANGMAP;
2009 if (State & INSERT)
2010 {
2011#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
2012 /* Unshown 'keymap' in status lines */
2013 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2014 {
2015 /* Save cursor position */
2016 int old_row = gui.row;
2017 int old_col = gui.col;
2018
2019 // This must be called here before
2020 // status_redraw_curbuf(), otherwise the mode
2021 // message may appear in the wrong position.
2022 showmode();
2023 status_redraw_curbuf();
2024 update_screen(0);
2025 /* Restore cursor position */
2026 gui.row = old_row;
2027 gui.col = old_col;
2028 }
2029#endif
2030 }
2031 }
2032 gui_update_cursor(TRUE, FALSE);
2033 lResult = 0;
2034 break;
2035 }
2036 pImmReleaseContext(hWnd, hImc);
2037 return lResult;
2038}
2039
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002040/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 static LRESULT
2042_OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param)
2043{
2044 char_u *ret;
2045 int len;
2046
2047 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
2048 return 0;
2049
2050 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
2051 if (ret != NULL)
2052 {
2053 add_to_input_buf_csi(ret, len);
2054 vim_free(ret);
2055 return 1;
2056 }
2057 return 0;
2058}
2059
2060/*
2061 * get the current composition string, in UCS-2; *lenp is the number of
2062 * *lenp is the number of Unicode characters.
2063 */
2064 static short_u *
2065GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
2066{
2067 LONG ret;
2068 LPWSTR wbuf = NULL;
2069 char_u *buf;
2070
2071 if (!pImmGetContext)
2072 return NULL; /* no imm32.dll */
2073
2074 /* Try Unicode; this'll always work on NT regardless of codepage. */
2075 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
2076 if (ret == 0)
2077 return NULL; /* empty */
2078
2079 if (ret > 0)
2080 {
2081 /* Allocate the requested buffer plus space for the NUL character. */
2082 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
2083 if (wbuf != NULL)
2084 {
2085 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
2086 *lenp = ret / sizeof(WCHAR);
2087 }
2088 return (short_u *)wbuf;
2089 }
2090
2091 /* ret < 0; we got an error, so try the ANSI version. This'll work
2092 * on 9x/ME, but only if the codepage happens to be set to whatever
2093 * we're inputting. */
2094 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
2095 if (ret <= 0)
2096 return NULL; /* empty or error */
2097
2098 buf = alloc(ret);
2099 if (buf == NULL)
2100 return NULL;
2101 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
2102
2103 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01002104 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 vim_free(buf);
2106
2107 return (short_u *)wbuf;
2108}
2109
2110/*
2111 * void GetResultStr()
2112 *
2113 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
2114 * get complete composition string
2115 */
2116 static char_u *
2117GetResultStr(HWND hwnd, int GCS, int *lenp)
2118{
2119 HIMC hIMC; /* Input context handle. */
2120 short_u *buf = NULL;
2121 char_u *convbuf = NULL;
2122
2123 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
2124 return NULL;
2125
2126 /* Reads in the composition string. */
2127 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
2128 if (buf == NULL)
2129 return NULL;
2130
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002131 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 pImmReleaseContext(hwnd, hIMC);
2133 vim_free(buf);
2134 return convbuf;
2135}
2136#endif
2137
2138/* For global functions we need prototypes. */
2139#if (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) || defined(PROTO)
2140
2141/*
2142 * set font to IM.
2143 */
2144 void
2145im_set_font(LOGFONT *lf)
2146{
2147 HIMC hImc;
2148
2149 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
2150 {
2151 pImmSetCompositionFont(hImc, lf);
2152 pImmReleaseContext(s_hwnd, hImc);
2153 }
2154}
2155
2156/*
2157 * Notify cursor position to IM.
2158 */
2159 void
2160im_set_position(int row, int col)
2161{
2162 HIMC hImc;
2163
2164 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
2165 {
2166 COMPOSITIONFORM cfs;
2167
2168 cfs.dwStyle = CFS_POINT;
2169 cfs.ptCurrentPos.x = FILL_X(col);
2170 cfs.ptCurrentPos.y = FILL_Y(row);
2171 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
2172 pImmSetCompositionWindow(hImc, &cfs);
2173
2174 pImmReleaseContext(s_hwnd, hImc);
2175 }
2176}
2177
2178/*
2179 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
2180 */
2181 void
2182im_set_active(int active)
2183{
2184 HIMC hImc;
2185 static HIMC hImcOld = (HIMC)0;
2186
2187 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
2188 {
2189 if (p_imdisable)
2190 {
2191 if (hImcOld == (HIMC)0)
2192 {
2193 hImcOld = pImmGetContext(s_hwnd);
2194 if (hImcOld)
2195 pImmAssociateContext(s_hwnd, (HIMC)0);
2196 }
2197 active = FALSE;
2198 }
2199 else if (hImcOld != (HIMC)0)
2200 {
2201 pImmAssociateContext(s_hwnd, hImcOld);
2202 hImcOld = (HIMC)0;
2203 }
2204
2205 hImc = pImmGetContext(s_hwnd);
2206 if (hImc)
2207 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002208 /*
2209 * for Korean ime
2210 */
2211 HKL hKL = GetKeyboardLayout(0);
2212
2213 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
2214 {
2215 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
2216 static BOOL bSaved = FALSE;
2217
2218 if (active)
2219 {
2220 /* if we have a saved conversion status, restore it */
2221 if (bSaved)
2222 pImmSetConversionStatus(hImc, dwConversionSaved,
2223 dwSentenceSaved);
2224 bSaved = FALSE;
2225 }
2226 else
2227 {
2228 /* save conversion status and disable korean */
2229 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
2230 &dwSentenceSaved))
2231 {
2232 bSaved = TRUE;
2233 pImmSetConversionStatus(hImc,
2234 dwConversionSaved & ~(IME_CMODE_NATIVE
2235 | IME_CMODE_FULLSHAPE),
2236 dwSentenceSaved);
2237 }
2238 }
2239 }
2240
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 pImmSetOpenStatus(hImc, active);
2242 pImmReleaseContext(s_hwnd, hImc);
2243 }
2244 }
2245}
2246
2247/*
2248 * Get IM status. When IM is on, return not 0. Else return 0.
2249 */
2250 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002251im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252{
2253 int status = 0;
2254 HIMC hImc;
2255
2256 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
2257 {
2258 status = pImmGetOpenStatus(hImc) ? 1 : 0;
2259 pImmReleaseContext(s_hwnd, hImc);
2260 }
2261 return status;
2262}
2263
2264#endif /* FEAT_MBYTE && FEAT_MBYTE_IME */
2265
2266#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
2267/* Win32 with GLOBAL IME */
2268
2269/*
2270 * Notify cursor position to IM.
2271 */
2272 void
2273im_set_position(int row, int col)
2274{
2275 /* Win32 with GLOBAL IME */
2276 POINT p;
2277
2278 p.x = FILL_X(col);
2279 p.y = FILL_Y(row);
2280 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
2281 global_ime_set_position(&p);
2282}
2283
2284/*
2285 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
2286 */
2287 void
2288im_set_active(int active)
2289{
2290 global_ime_set_status(active);
2291}
2292
2293/*
2294 * Get IM status. When IM is on, return not 0. Else return 0.
2295 */
2296 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002297im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298{
2299 return global_ime_get_status();
2300}
2301#endif
2302
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002303#ifdef FEAT_MBYTE
2304/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00002305 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002306 */
2307 static void
2308latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
2309{
2310 int c;
2311
Bram Moolenaarca003e12006-03-17 23:19:38 +00002312 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002313 {
2314 c = *text++;
2315 switch (c)
2316 {
2317 case 0xa4: c = 0x20ac; break; /* euro */
2318 case 0xa6: c = 0x0160; break; /* S hat */
2319 case 0xa8: c = 0x0161; break; /* S -hat */
2320 case 0xb4: c = 0x017d; break; /* Z hat */
2321 case 0xb8: c = 0x017e; break; /* Z -hat */
2322 case 0xbc: c = 0x0152; break; /* OE */
2323 case 0xbd: c = 0x0153; break; /* oe */
2324 case 0xbe: c = 0x0178; break; /* Y */
2325 }
2326 *unicodebuf++ = c;
2327 }
2328}
2329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330
2331#ifdef FEAT_RIGHTLEFT
2332/*
2333 * What is this for? In the case where you are using Win98 or Win2K or later,
2334 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
2335 * reverses the string sent to the TextOut... family. This sucks, because we
2336 * go to a lot of effort to do the right thing, and there doesn't seem to be a
2337 * way to tell Windblows not to do this!
2338 *
2339 * The short of it is that this 'RevOut' only gets called if you are running
2340 * one of the new, "improved" MS OSes, and only if you are running in
2341 * 'rightleft' mode. It makes display take *slightly* longer, but not
2342 * noticeably so.
2343 */
2344 static void
2345RevOut( HDC s_hdc,
2346 int col,
2347 int row,
2348 UINT foptions,
2349 CONST RECT *pcliprect,
2350 LPCTSTR text,
2351 UINT len,
2352 CONST INT *padding)
2353{
2354 int ix;
2355 static int special = -1;
2356
2357 if (special == -1)
2358 {
2359 /* Check windows version: special treatment is needed if it is NT 5 or
2360 * Win98 or higher. */
2361 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
2362 && os_version.dwMajorVersion >= 5)
2363 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
2364 && (os_version.dwMajorVersion > 4
2365 || (os_version.dwMajorVersion == 4
2366 && os_version.dwMinorVersion > 0))))
2367 special = 1;
2368 else
2369 special = 0;
2370 }
2371
2372 if (special)
2373 for (ix = 0; ix < (int)len; ++ix)
2374 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
2375 pcliprect, text + ix, 1, padding);
2376 else
2377 ExtTextOut(s_hdc, col, row, foptions, pcliprect, text, len, padding);
2378}
2379#endif
2380
2381 void
2382gui_mch_draw_string(
2383 int row,
2384 int col,
2385 char_u *text,
2386 int len,
2387 int flags)
2388{
2389 static int *padding = NULL;
2390 static int pad_size = 0;
2391 int i;
2392 const RECT *pcliprect = NULL;
2393 UINT foptions = 0;
2394#ifdef FEAT_MBYTE
2395 static WCHAR *unicodebuf = NULL;
2396 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002397 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 int n = 0;
2399#endif
2400 HPEN hpen, old_pen;
2401 int y;
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002402#ifdef FEAT_DIRECTX
2403 int font_is_ttf_or_vector = 0;
2404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406#ifndef MSWIN16_FASTTEXT
2407 /*
2408 * Italic and bold text seems to have an extra row of pixels at the bottom
2409 * (below where the bottom of the character should be). If we draw the
2410 * characters with a solid background, the top row of pixels in the
2411 * character below will be overwritten. We can fix this by filling in the
2412 * background ourselves, to the correct character proportions, and then
2413 * writing the character in transparent mode. Still have a problem when
2414 * the character is "_", which gets written on to the character below.
2415 * New fix: set gui.char_ascent to -1. This shifts all characters up one
2416 * pixel in their slots, which fixes the problem with the bottom row of
2417 * pixels. We still need this code because otherwise the top row of pixels
2418 * becomes a problem. - webb.
2419 */
2420 static HBRUSH hbr_cache[2] = {NULL, NULL};
2421 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
2422 static int brush_lru = 0;
2423 HBRUSH hbr;
2424 RECT rc;
2425
2426 if (!(flags & DRAW_TRANSP))
2427 {
2428 /*
2429 * Clear background first.
2430 * Note: FillRect() excludes right and bottom of rectangle.
2431 */
2432 rc.left = FILL_X(col);
2433 rc.top = FILL_Y(row);
2434#ifdef FEAT_MBYTE
2435 if (has_mbyte)
2436 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002438 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 }
2440 else
2441#endif
2442 rc.right = FILL_X(col + len);
2443 rc.bottom = FILL_Y(row + 1);
2444
2445 /* Cache the created brush, that saves a lot of time. We need two:
2446 * one for cursor background and one for the normal background. */
2447 if (gui.currBgColor == brush_color[0])
2448 {
2449 hbr = hbr_cache[0];
2450 brush_lru = 1;
2451 }
2452 else if (gui.currBgColor == brush_color[1])
2453 {
2454 hbr = hbr_cache[1];
2455 brush_lru = 0;
2456 }
2457 else
2458 {
2459 if (hbr_cache[brush_lru] != NULL)
2460 DeleteBrush(hbr_cache[brush_lru]);
2461 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
2462 brush_color[brush_lru] = gui.currBgColor;
2463 hbr = hbr_cache[brush_lru];
2464 brush_lru = !brush_lru;
2465 }
2466 FillRect(s_hdc, &rc, hbr);
2467
2468 SetBkMode(s_hdc, TRANSPARENT);
2469
2470 /*
2471 * When drawing block cursor, prevent inverted character spilling
2472 * over character cell (can happen with bold/italic)
2473 */
2474 if (flags & DRAW_CURSOR)
2475 {
2476 pcliprect = &rc;
2477 foptions = ETO_CLIPPED;
2478 }
2479 }
2480#else
2481 /*
2482 * The alternative would be to write the characters in opaque mode, but
2483 * when the text is not exactly the same proportions as normal text, too
2484 * big or too little a rectangle gets drawn for the background.
2485 */
2486 SetBkMode(s_hdc, OPAQUE);
2487 SetBkColor(s_hdc, gui.currBgColor);
2488#endif
2489 SetTextColor(s_hdc, gui.currFgColor);
2490 SelectFont(s_hdc, gui.currFont);
2491
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002492#ifdef FEAT_DIRECTX
2493 if (IS_ENABLE_DIRECTX())
2494 {
2495 TEXTMETRIC tm;
2496
2497 GetTextMetrics(s_hdc, &tm);
2498 if (tm.tmPitchAndFamily & (TMPF_TRUETYPE | TMPF_VECTOR))
2499 {
2500 font_is_ttf_or_vector = 1;
2501 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
2502 }
2503 }
2504#endif
2505
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
2507 {
2508 vim_free(padding);
2509 pad_size = Columns;
2510
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002511 /* Don't give an out-of-memory message here, it would call us
2512 * recursively. */
2513 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 if (padding != NULL)
2515 for (i = 0; i < pad_size; i++)
2516 padding[i] = gui.char_width;
2517 }
2518
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 /*
2520 * We have to provide the padding argument because italic and bold versions
2521 * of fixed-width fonts are often one pixel or so wider than their normal
2522 * versions.
2523 * No check for DRAW_BOLD, Windows will have done it already.
2524 */
2525
2526#ifdef FEAT_MBYTE
2527 /* Check if there are any UTF-8 characters. If not, use normal text
2528 * output to speed up output. */
2529 if (enc_utf8)
2530 for (n = 0; n < len; ++n)
2531 if (text[n] >= 0x80)
2532 break;
2533
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002534#if defined(FEAT_DIRECTX)
2535 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
2536 * required that unicode drawing routine, currently. So this forces it
2537 * enabled. */
2538 if (enc_utf8 && IS_ENABLE_DIRECTX())
2539 n = 0; /* Keep n < len, to enter block for unicode. */
2540#endif
2541
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002543 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002545 if ((enc_utf8
2546 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
2547 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 && (unicodebuf == NULL || len > unibuflen))
2549 {
2550 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002551 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552
2553 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002554 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555
2556 unibuflen = len;
2557 }
2558
2559 if (enc_utf8 && n < len && unicodebuf != NULL)
2560 {
2561 /* Output UTF-8 characters. Caller has already separated
2562 * composing characters. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002563 int i;
2564 int wlen; /* string length in words */
2565 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 int cells; /* cell width of string up to composing char */
2567 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002568 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002570 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002571 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002573 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002575 c = utf_ptr2char(text + i);
2576 if (c >= 0x10000)
2577 {
2578 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002579 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
2580 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002581 }
2582 else
2583 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002584 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002585 }
2586 cw = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 if (cw > 2) /* don't use 4 for unprintable char */
2588 cw = 1;
2589 if (unicodepdy != NULL)
2590 {
2591 /* Use unicodepdy to make characters fit as we expect, even
2592 * when the font uses different widths (e.g., bold character
2593 * is wider). */
2594 unicodepdy[clen] = cw * gui.char_width;
2595 }
2596 cells += cw;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002597 i += utfc_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00002598 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002600#if defined(FEAT_DIRECTX)
2601 if (IS_ENABLE_DIRECTX() && font_is_ttf_or_vector)
2602 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02002603 /* Add one to "cells" for italics. */
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002604 DWriteContext_DrawText(s_dwc, s_hdc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02002605 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002606 gui.char_width, gui.currFgColor);
2607 }
2608 else
2609#endif
2610 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
2611 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 len = cells; /* used for underlining */
2613 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002614 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 {
2616 /* If we want to display codepage data, and the current CP is not the
2617 * ANSI one, we need to go via Unicode. */
2618 if (unicodebuf != NULL)
2619 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002620 if (enc_latin9)
2621 latin9_to_ucs(text, len, unicodebuf);
2622 else
2623 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 MB_PRECOMPOSED,
2625 (char *)text, len,
2626 (LPWSTR)unicodebuf, unibuflen);
2627 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002628 {
2629 /* Use unicodepdy to make characters fit as we expect, even
2630 * when the font uses different widths (e.g., bold character
2631 * is wider). */
2632 if (unicodepdy != NULL)
2633 {
2634 int i;
2635 int cw;
2636
2637 for (i = 0; i < len; ++i)
2638 {
2639 cw = utf_char2cells(unicodebuf[i]);
2640 if (cw > 2)
2641 cw = 1;
2642 unicodepdy[i] = cw * gui.char_width;
2643 }
2644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002646 foptions, pcliprect, unicodebuf, len, unicodepdy);
2647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 }
2649 }
2650 else
2651#endif
2652 {
2653#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02002654 /* Windows will mess up RL text, so we have to draw it character by
2655 * character. Only do this if RL is on, since it's slow. */
2656 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
2658 foptions, pcliprect, (char *)text, len, padding);
2659 else
2660#endif
2661 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
2662 foptions, pcliprect, (char *)text, len, padding);
2663 }
2664
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002665 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 if (flags & DRAW_UNDERL)
2667 {
2668 hpen = CreatePen(PS_SOLID, 1, gui.currFgColor);
2669 old_pen = SelectObject(s_hdc, hpen);
2670 /* When p_linespace is 0, overwrite the bottom row of pixels.
2671 * Otherwise put the line just below the character. */
2672 y = FILL_Y(row + 1) - 1;
2673#ifndef MSWIN16_FASTTEXT
2674 if (p_linespace > 1)
2675 y -= p_linespace - 1;
2676#endif
2677 MoveToEx(s_hdc, FILL_X(col), y, NULL);
2678 /* Note: LineTo() excludes the last pixel in the line. */
2679 LineTo(s_hdc, FILL_X(col + len), y);
2680 DeleteObject(SelectObject(s_hdc, old_pen));
2681 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002682
2683 /* Undercurl */
2684 if (flags & DRAW_UNDERC)
2685 {
2686 int x;
2687 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002688 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002689
2690 y = FILL_Y(row + 1) - 1;
2691 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
2692 {
2693 offset = val[x % 8];
2694 SetPixel(s_hdc, x, y - offset, gui.currSpColor);
2695 }
2696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697}
2698
2699
2700/*
2701 * Output routines.
2702 */
2703
2704/* Flush any output to the screen */
2705 void
2706gui_mch_flush(void)
2707{
2708# if defined(__BORLANDC__)
2709 /*
2710 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
2711 * prototype declaration.
2712 * The compiler complains if __stdcall is not used in both declarations.
2713 */
2714 BOOL __stdcall GdiFlush(void);
2715# endif
2716
2717 GdiFlush();
2718}
2719
2720 static void
2721clear_rect(RECT *rcp)
2722{
2723 HBRUSH hbr;
2724
2725 hbr = CreateSolidBrush(gui.back_pixel);
2726 FillRect(s_hdc, rcp, hbr);
2727 DeleteBrush(hbr);
2728}
2729
2730
Bram Moolenaarc716c302006-01-21 22:12:51 +00002731 void
2732gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
2733{
2734 RECT workarea_rect;
2735
2736 get_work_area(&workarea_rect);
2737
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002738 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02002739 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002740 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00002741
2742 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
2743 * the menubar for MSwin, we subtract it from the screen height, so that
2744 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002745 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02002746 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002747 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00002748 - GetSystemMetrics(SM_CYCAPTION)
2749#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002750 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00002751#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002752 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00002753}
2754
2755
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756#if defined(FEAT_MENU) || defined(PROTO)
2757/*
2758 * Add a sub menu to the menu bar.
2759 */
2760 void
2761gui_mch_add_menu(
2762 vimmenu_T *menu,
2763 int pos)
2764{
2765 vimmenu_T *parent = menu->parent;
2766
2767 menu->submenu_id = CreatePopupMenu();
2768 menu->id = s_menu_id++;
2769
2770 if (menu_is_menubar(menu->name))
2771 {
2772 if (is_winnt_3())
2773 {
2774 InsertMenu((parent == NULL) ? s_menuBar : parent->submenu_id,
2775 (UINT)pos, MF_POPUP | MF_STRING | MF_BYPOSITION,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002776 (long_u)menu->submenu_id, (LPCTSTR) menu->name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 }
2778 else
2779 {
2780#ifdef FEAT_MBYTE
2781 WCHAR *wn = NULL;
2782 int n;
2783
2784 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2785 {
2786 /* 'encoding' differs from active codepage: convert menu name
2787 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002788 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 if (wn != NULL)
2790 {
2791 MENUITEMINFOW infow;
2792
2793 infow.cbSize = sizeof(infow);
2794 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
2795 | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002796 infow.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 infow.wID = menu->id;
2798 infow.fType = MFT_STRING;
2799 infow.dwTypeData = wn;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002800 infow.cch = (UINT)wcslen(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 infow.hSubMenu = menu->submenu_id;
2802 n = InsertMenuItemW((parent == NULL)
2803 ? s_menuBar : parent->submenu_id,
2804 (UINT)pos, TRUE, &infow);
2805 vim_free(wn);
2806 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2807 /* Failed, try using non-wide function. */
2808 wn = NULL;
2809 }
2810 }
2811
2812 if (wn == NULL)
2813#endif
2814 {
2815 MENUITEMINFO info;
2816
2817 info.cbSize = sizeof(info);
2818 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002819 info.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 info.wID = menu->id;
2821 info.fType = MFT_STRING;
2822 info.dwTypeData = (LPTSTR)menu->name;
2823 info.cch = (UINT)STRLEN(menu->name);
2824 info.hSubMenu = menu->submenu_id;
2825 InsertMenuItem((parent == NULL)
2826 ? s_menuBar : parent->submenu_id,
2827 (UINT)pos, TRUE, &info);
2828 }
2829 }
2830 }
2831
2832 /* Fix window size if menu may have wrapped */
2833 if (parent == NULL)
2834 gui_mswin_get_menu_height(!gui.starting);
2835#ifdef FEAT_TEAROFF
2836 else if (IsWindow(parent->tearoff_handle))
2837 rebuild_tearoff(parent);
2838#endif
2839}
2840
2841 void
2842gui_mch_show_popupmenu(vimmenu_T *menu)
2843{
2844 POINT mp;
2845
2846 (void)GetCursorPos((LPPOINT)&mp);
2847 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
2848}
2849
2850 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00002851gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
2853 vimmenu_T *menu = gui_find_menu(path_name);
2854
2855 if (menu != NULL)
2856 {
2857 POINT p;
2858
2859 /* Find the position of the current cursor */
2860 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00002861 if (mouse_pos)
2862 {
2863 int mx, my;
2864
2865 gui_mch_getmouse(&mx, &my);
2866 p.x += mx;
2867 p.y += my;
2868 }
2869 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 {
2871 p.x += TEXT_X(W_WINCOL(curwin) + curwin->w_wcol + 1);
2872 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
2873 }
2874 msg_scroll = FALSE;
2875 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
2876 }
2877}
2878
2879#if defined(FEAT_TEAROFF) || defined(PROTO)
2880/*
2881 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
2882 * create it as a pseudo-"tearoff menu".
2883 */
2884 void
2885gui_make_tearoff(char_u *path_name)
2886{
2887 vimmenu_T *menu = gui_find_menu(path_name);
2888
2889 /* Found the menu, so tear it off. */
2890 if (menu != NULL)
2891 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
2892}
2893#endif
2894
2895/*
2896 * Add a menu item to a menu
2897 */
2898 void
2899gui_mch_add_menu_item(
2900 vimmenu_T *menu,
2901 int idx)
2902{
2903 vimmenu_T *parent = menu->parent;
2904
2905 menu->id = s_menu_id++;
2906 menu->submenu_id = NULL;
2907
2908#ifdef FEAT_TEAROFF
2909 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
2910 {
2911 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
2912 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
2913 }
2914 else
2915#endif
2916#ifdef FEAT_TOOLBAR
2917 if (menu_is_toolbar(parent->name))
2918 {
2919 TBBUTTON newtb;
2920
2921 vim_memset(&newtb, 0, sizeof(newtb));
2922 if (menu_is_separator(menu->name))
2923 {
2924 newtb.iBitmap = 0;
2925 newtb.fsStyle = TBSTYLE_SEP;
2926 }
2927 else
2928 {
2929 newtb.iBitmap = get_toolbar_bitmap(menu);
2930 newtb.fsStyle = TBSTYLE_BUTTON;
2931 }
2932 newtb.idCommand = menu->id;
2933 newtb.fsState = TBSTATE_ENABLED;
2934 newtb.iString = 0;
2935 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
2936 (LPARAM)&newtb);
2937 menu->submenu_id = (HMENU)-1;
2938 }
2939 else
2940#endif
2941 {
2942#ifdef FEAT_MBYTE
2943 WCHAR *wn = NULL;
2944 int n;
2945
2946 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2947 {
2948 /* 'encoding' differs from active codepage: convert menu item name
2949 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002950 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 if (wn != NULL)
2952 {
2953 n = InsertMenuW(parent->submenu_id, (UINT)idx,
2954 (menu_is_separator(menu->name)
2955 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
2956 (UINT)menu->id, wn);
2957 vim_free(wn);
2958 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2959 /* Failed, try using non-wide function. */
2960 wn = NULL;
2961 }
2962 }
2963 if (wn == NULL)
2964#endif
2965 InsertMenu(parent->submenu_id, (UINT)idx,
2966 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
2967 | MF_BYPOSITION,
2968 (UINT)menu->id, (LPCTSTR)menu->name);
2969#ifdef FEAT_TEAROFF
2970 if (IsWindow(parent->tearoff_handle))
2971 rebuild_tearoff(parent);
2972#endif
2973 }
2974}
2975
2976/*
2977 * Destroy the machine specific menu widget.
2978 */
2979 void
2980gui_mch_destroy_menu(vimmenu_T *menu)
2981{
2982#ifdef FEAT_TOOLBAR
2983 /*
2984 * is this a toolbar button?
2985 */
2986 if (menu->submenu_id == (HMENU)-1)
2987 {
2988 int iButton;
2989
2990 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
2991 (WPARAM)menu->id, 0);
2992 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
2993 }
2994 else
2995#endif
2996 {
2997 if (menu->parent != NULL
2998 && menu_is_popup(menu->parent->dname)
2999 && menu->parent->submenu_id != NULL)
3000 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
3001 else
3002 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
3003 if (menu->submenu_id != NULL)
3004 DestroyMenu(menu->submenu_id);
3005#ifdef FEAT_TEAROFF
3006 if (IsWindow(menu->tearoff_handle))
3007 DestroyWindow(menu->tearoff_handle);
3008 if (menu->parent != NULL
3009 && menu->parent->children != NULL
3010 && IsWindow(menu->parent->tearoff_handle))
3011 {
3012 /* This menu must not show up when rebuilding the tearoff window. */
3013 menu->modes = 0;
3014 rebuild_tearoff(menu->parent);
3015 }
3016#endif
3017 }
3018}
3019
3020#ifdef FEAT_TEAROFF
3021 static void
3022rebuild_tearoff(vimmenu_T *menu)
3023{
3024 /*hackish*/
3025 char_u tbuf[128];
3026 RECT trect;
3027 RECT rct;
3028 RECT roct;
3029 int x, y;
3030
3031 HWND thwnd = menu->tearoff_handle;
3032
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003033 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 if (GetWindowRect(thwnd, &trect)
3035 && GetWindowRect(s_hwnd, &rct)
3036 && GetClientRect(s_hwnd, &roct))
3037 {
3038 x = trect.left - rct.left;
3039 y = (trect.top - rct.bottom + roct.bottom);
3040 }
3041 else
3042 {
3043 x = y = 0xffffL;
3044 }
3045 DestroyWindow(thwnd);
3046 if (menu->children != NULL)
3047 {
3048 gui_mch_tearoff(tbuf, menu, x, y);
3049 if (IsWindow(menu->tearoff_handle))
3050 (void) SetWindowPos(menu->tearoff_handle,
3051 NULL,
3052 (int)trect.left,
3053 (int)trect.top,
3054 0, 0,
3055 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
3056 }
3057}
3058#endif /* FEAT_TEAROFF */
3059
3060/*
3061 * Make a menu either grey or not grey.
3062 */
3063 void
3064gui_mch_menu_grey(
3065 vimmenu_T *menu,
3066 int grey)
3067{
3068#ifdef FEAT_TOOLBAR
3069 /*
3070 * is this a toolbar button?
3071 */
3072 if (menu->submenu_id == (HMENU)-1)
3073 {
3074 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
3075 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
3076 }
3077 else
3078#endif
3079 if (grey)
3080 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_GRAYED);
3081 else
3082 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
3083
3084#ifdef FEAT_TEAROFF
3085 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
3086 {
3087 WORD menuID;
3088 HWND menuHandle;
3089
3090 /*
3091 * A tearoff button has changed state.
3092 */
3093 if (menu->children == NULL)
3094 menuID = (WORD)(menu->id);
3095 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003096 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
3098 if (menuHandle)
3099 EnableWindow(menuHandle, !grey);
3100
3101 }
3102#endif
3103}
3104
3105#endif /* FEAT_MENU */
3106
3107
3108/* define some macros used to make the dialogue creation more readable */
3109
3110#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
3111#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00003112#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113
3114#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
3115/*
3116 * stuff for dialogs
3117 */
3118
3119/*
3120 * The callback routine used by all the dialogs. Very simple. First,
3121 * acknowledges the INITDIALOG message so that Windows knows to do standard
3122 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
3123 * pressed, return that button's ID - IDCANCEL (2), which is the button's
3124 * number.
3125 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003126/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 static LRESULT CALLBACK
3128dialog_callback(
3129 HWND hwnd,
3130 UINT message,
3131 WPARAM wParam,
3132 LPARAM lParam)
3133{
3134 if (message == WM_INITDIALOG)
3135 {
3136 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
3137 /* Set focus to the dialog. Set the default button, if specified. */
3138 (void)SetFocus(hwnd);
3139 if (dialog_default_button > IDCANCEL)
3140 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00003141 else
3142 /* We don't have a default, set focus on another element of the
3143 * dialog window, probably the icon */
3144 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 return FALSE;
3146 }
3147
3148 if (message == WM_COMMAND)
3149 {
3150 int button = LOWORD(wParam);
3151
3152 /* Don't end the dialog if something was selected that was
3153 * not a button.
3154 */
3155 if (button >= DLG_NONBUTTON_CONTROL)
3156 return TRUE;
3157
3158 /* If the edit box exists, copy the string. */
3159 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00003160 {
3161# if defined(FEAT_MBYTE) && defined(WIN3264)
3162 /* If the OS is Windows NT, and 'encoding' differs from active
3163 * codepage: use wide function and convert text. */
3164 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
3165 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003166 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00003167 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
3168 char_u *p;
3169
3170 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
3171 p = utf16_to_enc(wp, NULL);
3172 vim_strncpy(s_textfield, p, IOSIZE);
3173 vim_free(p);
3174 vim_free(wp);
3175 }
3176 else
3177# endif
3178 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003179 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00003180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181
3182 /*
3183 * Need to check for IDOK because if the user just hits Return to
3184 * accept the default value, some reason this is what we get.
3185 */
3186 if (button == IDOK)
3187 {
3188 if (dialog_default_button > IDCANCEL)
3189 EndDialog(hwnd, dialog_default_button);
3190 }
3191 else
3192 EndDialog(hwnd, button - IDCANCEL);
3193 return TRUE;
3194 }
3195
3196 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
3197 {
3198 EndDialog(hwnd, 0);
3199 return TRUE;
3200 }
3201 return FALSE;
3202}
3203
3204/*
3205 * Create a dialog dynamically from the parameter strings.
3206 * type = type of dialog (question, alert, etc.)
3207 * title = dialog title. may be NULL for default title.
3208 * message = text to display. Dialog sizes to accommodate it.
3209 * buttons = '\n' separated list of button captions, default first.
3210 * dfltbutton = number of default button.
3211 *
3212 * This routine returns 1 if the first button is pressed,
3213 * 2 for the second, etc.
3214 *
3215 * 0 indicates Esc was pressed.
3216 * -1 for unexpected error
3217 *
3218 * If stubbing out this fn, return 1.
3219 */
3220
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003221static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
3223 "IDR_VIM",
3224 "IDR_VIM_ERROR",
3225 "IDR_VIM_ALERT",
3226 "IDR_VIM_INFO",
3227 "IDR_VIM_QUESTION"
3228};
3229
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 int
3231gui_mch_dialog(
3232 int type,
3233 char_u *title,
3234 char_u *message,
3235 char_u *buttons,
3236 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003237 char_u *textfield,
3238 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239{
3240 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00003241 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 int numButtons;
3243 int *buttonWidths, *buttonPositions;
3244 int buttonYpos;
3245 int nchar, i;
3246 DWORD lStyle;
3247 int dlgwidth = 0;
3248 int dlgheight;
3249 int editboxheight;
3250 int horizWidth = 0;
3251 int msgheight;
3252 char_u *pstart;
3253 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003254 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 char_u *tbuffer;
3256 RECT rect;
3257 HWND hwnd;
3258 HDC hdc;
3259 HFONT font, oldFont;
3260 TEXTMETRIC fontInfo;
3261 int fontHeight;
3262 int textWidth, minButtonWidth, messageWidth;
3263 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003264 int maxDialogHeight;
3265 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 int vertical;
3267 int dlgPaddingX;
3268 int dlgPaddingY;
3269#ifdef USE_SYSMENU_FONT
3270 LOGFONT lfSysmenu;
3271 int use_lfSysmenu = FALSE;
3272#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003273 garray_T ga;
3274 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
3276#ifndef NO_CONSOLE
3277 /* Don't output anything in silent mode ("ex -s") */
3278 if (silent_mode)
3279 return dfltbutton; /* return default option */
3280#endif
3281
Bram Moolenaar748bf032005-02-02 23:04:36 +00003282 if (s_hwnd == NULL)
3283 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284
3285 if ((type < 0) || (type > VIM_LAST_TYPE))
3286 type = 0;
3287
3288 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003289 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003290 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003291 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292
3293 if (p == NULL)
3294 return -1;
3295
3296 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003297 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 * vim_strsave() doesn't take a const arg (why not?), so cast away the
3299 * const.
3300 */
3301 tbuffer = vim_strsave(buttons);
3302 if (tbuffer == NULL)
3303 return -1;
3304
3305 --dfltbutton; /* Change from one-based to zero-based */
3306
3307 /* Count buttons */
3308 numButtons = 1;
3309 for (i = 0; tbuffer[i] != '\0'; i++)
3310 {
3311 if (tbuffer[i] == DLG_BUTTON_SEP)
3312 numButtons++;
3313 }
3314 if (dfltbutton >= numButtons)
3315 dfltbutton = -1;
3316
3317 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003318 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 if (buttonWidths == NULL)
3320 return -1;
3321
3322 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003323 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 if (buttonPositions == NULL)
3325 return -1;
3326
3327 /*
3328 * Calculate how big the dialog must be.
3329 */
3330 hwnd = GetDesktopWindow();
3331 hdc = GetWindowDC(hwnd);
3332#ifdef USE_SYSMENU_FONT
3333 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
3334 {
3335 font = CreateFontIndirect(&lfSysmenu);
3336 use_lfSysmenu = TRUE;
3337 }
3338 else
3339#endif
3340 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3341 VARIABLE_PITCH , DLG_FONT_NAME);
3342 if (s_usenewlook)
3343 {
3344 oldFont = SelectFont(hdc, font);
3345 dlgPaddingX = DLG_PADDING_X;
3346 dlgPaddingY = DLG_PADDING_Y;
3347 }
3348 else
3349 {
3350 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
3351 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
3352 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
3353 }
3354 GetTextMetrics(hdc, &fontInfo);
3355 fontHeight = fontInfo.tmHeight;
3356
3357 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003358 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
3360 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003361 if (s_hwnd == NULL)
3362 {
3363 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364
Bram Moolenaarc716c302006-01-21 22:12:51 +00003365 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003366 get_work_area(&workarea_rect);
3367 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
3368 if (maxDialogWidth > 600)
3369 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02003370 /* Leave some room for the taskbar. */
3371 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003372 }
3373 else
3374 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02003375 /* Use our own window for the size, unless it's very small. */
3376 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003377 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02003378 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02003379 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003380 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
3381 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003382
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003383 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02003384 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02003385 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02003386 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003387 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
3388 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
3389 }
3390
3391 /* Set dlgwidth to width of message.
3392 * Copy the message into "ga", changing NL to CR-NL and inserting line
3393 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 pstart = message;
3395 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003396 msgheight = 0;
3397 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 do
3399 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003400 msgheight += fontHeight; /* at least one line */
3401
3402 /* Need to figure out where to break the string. The system does it
3403 * at a word boundary, which would mean we can't compute the number of
3404 * wrapped lines. */
3405 textWidth = 0;
3406 last_white = NULL;
3407 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00003408 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003409#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003410 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003411#else
3412 l = 1;
3413#endif
3414 if (l == 1 && vim_iswhite(*pend)
3415 && textWidth > maxDialogWidth * 3 / 4)
3416 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02003417 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003418 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003419 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003420 /* Line will wrap. */
3421 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003422 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003423 textWidth = 0;
3424
3425 if (last_white != NULL)
3426 {
3427 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003428 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003429 pend = last_white + 1;
3430 last_white = NULL;
3431 }
3432 ga_append(&ga, '\r');
3433 ga_append(&ga, '\n');
3434 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003435 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003436
3437 while (--l >= 0)
3438 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003439 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003440 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003442
3443 ga_append(&ga, '\r');
3444 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 pstart = pend + 1;
3446 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003447
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003448 if (ga.ga_data != NULL)
3449 message = ga.ga_data;
3450
Bram Moolenaar748bf032005-02-02 23:04:36 +00003451 messageWidth += 10; /* roundoff space */
3452
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02003454 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
3455 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
3457 if (msgheight < DLG_ICON_HEIGHT)
3458 msgheight = DLG_ICON_HEIGHT;
3459
3460 /*
3461 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00003462 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00003464 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 if (!vertical)
3466 {
3467 // Place buttons horizontally if they fit.
3468 horizWidth = dlgPaddingX;
3469 pstart = tbuffer;
3470 i = 0;
3471 do
3472 {
3473 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
3474 if (pend == NULL)
3475 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02003476 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 if (textWidth < minButtonWidth)
3478 textWidth = minButtonWidth;
3479 textWidth += dlgPaddingX; /* Padding within button */
3480 buttonWidths[i] = textWidth;
3481 buttonPositions[i++] = horizWidth;
3482 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
3483 pstart = pend + 1;
3484 } while (*pend != NUL);
3485
3486 if (horizWidth > maxDialogWidth)
3487 vertical = TRUE; // Too wide to fit on the screen.
3488 else if (horizWidth > dlgwidth)
3489 dlgwidth = horizWidth;
3490 }
3491
3492 if (vertical)
3493 {
3494 // Stack buttons vertically.
3495 pstart = tbuffer;
3496 do
3497 {
3498 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
3499 if (pend == NULL)
3500 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02003501 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 textWidth += dlgPaddingX; /* Padding within button */
3503 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
3504 if (textWidth > dlgwidth)
3505 dlgwidth = textWidth;
3506 pstart = pend + 1;
3507 } while (*pend != NUL);
3508 }
3509
3510 if (dlgwidth < DLG_MIN_WIDTH)
3511 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
3512
3513 /* start to fill in the dlgtemplate information. addressing by WORDs */
3514 if (s_usenewlook)
3515 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
3516 else
3517 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
3518
3519 add_long(lStyle);
3520 add_long(0); // (lExtendedStyle)
3521 pnumitems = p; /*save where the number of items must be stored*/
3522 add_word(0); // NumberOfItems(will change later)
3523 add_word(10); // x
3524 add_word(10); // y
3525 add_word(PixelToDialogX(dlgwidth)); // cx
3526
3527 // Dialog height.
3528 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02003529 dlgheight = msgheight + 2 * dlgPaddingY
3530 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 else
3532 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
3533
3534 // Dialog needs to be taller if contains an edit box.
3535 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
3536 if (textfield != NULL)
3537 dlgheight += editboxheight;
3538
Bram Moolenaara95d8232013-08-07 15:27:11 +02003539 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
3540 if (dlgheight > maxDialogHeight)
3541 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02003542 msgheight = msgheight - (dlgheight - maxDialogHeight);
3543 dlgheight = maxDialogHeight;
3544 scroll_flag = WS_VSCROLL;
3545 /* Make sure scrollbar doesn't appear in the middle of the dialog */
3546 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02003547 }
3548
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 add_word(PixelToDialogY(dlgheight));
3550
3551 add_word(0); // Menu
3552 add_word(0); // Class
3553
3554 /* copy the title of the dialog */
3555 nchar = nCopyAnsiToWideChar(p, (title ?
3556 (LPSTR)title :
3557 (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
3558 p += nchar;
3559
3560 if (s_usenewlook)
3561 {
3562 /* do the font, since DS_3DLOOK doesn't work properly */
3563#ifdef USE_SYSMENU_FONT
3564 if (use_lfSysmenu)
3565 {
3566 /* point size */
3567 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
3568 GetDeviceCaps(hdc, LOGPIXELSY));
3569 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
3570 }
3571 else
3572#endif
3573 {
3574 *p++ = DLG_FONT_POINT_SIZE; // point size
3575 nchar = nCopyAnsiToWideChar(p, TEXT(DLG_FONT_NAME));
3576 }
3577 p += nchar;
3578 }
3579
3580 buttonYpos = msgheight + 2 * dlgPaddingY;
3581
3582 if (textfield != NULL)
3583 buttonYpos += editboxheight;
3584
3585 pstart = tbuffer;
3586 if (!vertical)
3587 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
3588 for (i = 0; i < numButtons; i++)
3589 {
3590 /* get end of this button. */
3591 for ( pend = pstart;
3592 *pend && (*pend != DLG_BUTTON_SEP);
3593 pend++)
3594 ;
3595
3596 if (*pend)
3597 *pend = '\0';
3598
3599 /*
3600 * old NOTE:
3601 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
3602 * the focus to the first tab-able button and in so doing makes that
3603 * the default!! Grrr. Workaround: Make the default button the only
3604 * one with WS_TABSTOP style. Means user can't tab between buttons, but
3605 * he/she can use arrow keys.
3606 *
3607 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00003608 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 * dialog. Also needed for when the textfield is the default control.
3610 * It appears to work now (perhaps not on Win95?).
3611 */
3612 if (vertical)
3613 {
3614 p = add_dialog_element(p,
3615 (i == dfltbutton
3616 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
3617 PixelToDialogX(DLG_VERT_PADDING_X),
3618 PixelToDialogY(buttonYpos /* TBK */
3619 + 2 * fontHeight * i),
3620 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
3621 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003622 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 }
3624 else
3625 {
3626 p = add_dialog_element(p,
3627 (i == dfltbutton
3628 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
3629 PixelToDialogX(horizWidth + buttonPositions[i]),
3630 PixelToDialogY(buttonYpos), /* TBK */
3631 PixelToDialogX(buttonWidths[i]),
3632 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003633 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 }
3635 pstart = pend + 1; /*next button*/
3636 }
3637 *pnumitems += numButtons;
3638
3639 /* Vim icon */
3640 p = add_dialog_element(p, SS_ICON,
3641 PixelToDialogX(dlgPaddingX),
3642 PixelToDialogY(dlgPaddingY),
3643 PixelToDialogX(DLG_ICON_WIDTH),
3644 PixelToDialogY(DLG_ICON_HEIGHT),
3645 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
3646 dlg_icons[type]);
3647
Bram Moolenaar748bf032005-02-02 23:04:36 +00003648 /* Dialog message */
3649 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
3650 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
3651 PixelToDialogY(dlgPaddingY),
3652 (WORD)(PixelToDialogX(messageWidth) + 1),
3653 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003654 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655
3656 /* Edit box */
3657 if (textfield != NULL)
3658 {
3659 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
3660 PixelToDialogX(2 * dlgPaddingX),
3661 PixelToDialogY(2 * dlgPaddingY + msgheight),
3662 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
3663 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003664 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 *pnumitems += 1;
3666 }
3667
3668 *pnumitems += 2;
3669
3670 SelectFont(hdc, oldFont);
3671 DeleteObject(font);
3672 ReleaseDC(hwnd, hdc);
3673
3674 /* Let the dialog_callback() function know which button to make default
3675 * If we have an edit box, make that the default. We also need to tell
3676 * dialog_callback() if this dialog contains an edit box or not. We do
3677 * this by setting s_textfield if it does.
3678 */
3679 if (textfield != NULL)
3680 {
3681 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
3682 s_textfield = textfield;
3683 }
3684 else
3685 {
3686 dialog_default_button = IDCANCEL + 1 + dfltbutton;
3687 s_textfield = NULL;
3688 }
3689
3690 /* show the dialog box modally and get a return value */
3691 nchar = (int)DialogBoxIndirect(
3692 s_hinst,
3693 (LPDLGTEMPLATE)pdlgtemplate,
3694 s_hwnd,
3695 (DLGPROC)dialog_callback);
3696
3697 LocalFree(LocalHandle(pdlgtemplate));
3698 vim_free(tbuffer);
3699 vim_free(buttonWidths);
3700 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003701 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702
3703 /* Focus back to our window (for when MDI is used). */
3704 (void)SetFocus(s_hwnd);
3705
3706 return nchar;
3707}
3708
3709#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003710
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711/*
3712 * Put a simple element (basic class) onto a dialog template in memory.
3713 * return a pointer to where the next item should be added.
3714 *
3715 * parameters:
3716 * lStyle = additional style flags
3717 * (be careful, NT3.51 & Win32s will ignore the new ones)
3718 * x,y = x & y positions IN DIALOG UNITS
3719 * w,h = width and height IN DIALOG UNITS
3720 * Id = ID used in messages
3721 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
3722 * caption = usually text or resource name
3723 *
3724 * TODO: use the length information noted here to enable the dialog creation
3725 * routines to work out more exactly how much memory they need to alloc.
3726 */
3727 static PWORD
3728add_dialog_element(
3729 PWORD p,
3730 DWORD lStyle,
3731 WORD x,
3732 WORD y,
3733 WORD w,
3734 WORD h,
3735 WORD Id,
3736 WORD clss,
3737 const char *caption)
3738{
3739 int nchar;
3740
3741 p = lpwAlign(p); /* Align to dword boundary*/
3742 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
3743 *p++ = LOWORD(lStyle);
3744 *p++ = HIWORD(lStyle);
3745 *p++ = 0; // LOWORD (lExtendedStyle)
3746 *p++ = 0; // HIWORD (lExtendedStyle)
3747 *p++ = x;
3748 *p++ = y;
3749 *p++ = w;
3750 *p++ = h;
3751 *p++ = Id; //9 or 10 words in all
3752
3753 *p++ = (WORD)0xffff;
3754 *p++ = clss; //2 more here
3755
3756 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption); //strlen(caption)+1
3757 p += nchar;
3758
3759 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
3760
3761 return p; //total = 15+ (strlen(caption)) words
3762 // = 30 + 2(strlen(caption) bytes reqd
3763}
3764
3765
3766/*
3767 * Helper routine. Take an input pointer, return closest pointer that is
3768 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
3769 */
3770 static LPWORD
3771lpwAlign(
3772 LPWORD lpIn)
3773{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003774 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003776 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 ul += 3;
3778 ul >>= 2;
3779 ul <<= 2;
3780 return (LPWORD)ul;
3781}
3782
3783/*
3784 * Helper routine. Takes second parameter as Ansi string, copies it to first
3785 * parameter as wide character (16-bits / char) string, and returns integer
3786 * number of wide characters (words) in string (including the trailing wide
3787 * char NULL). Partly taken from the Win32SDK samples.
3788 */
3789 static int
3790nCopyAnsiToWideChar(
3791 LPWORD lpWCStr,
3792 LPSTR lpAnsiIn)
3793{
3794 int nChar = 0;
3795#ifdef FEAT_MBYTE
3796 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
3797 int i;
3798 WCHAR *wn;
3799
3800 if (enc_codepage == 0 && (int)GetACP() != enc_codepage)
3801 {
3802 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003803 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 if (wn != NULL)
3805 {
3806 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003807 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 vim_free(wn);
3809 }
3810 }
3811 if (nChar == 0)
3812 /* Use Win32 conversion function. */
3813 nChar = MultiByteToWideChar(
3814 enc_codepage > 0 ? enc_codepage : CP_ACP,
3815 MB_PRECOMPOSED,
3816 lpAnsiIn, len,
3817 lpWCStr, len);
3818 for (i = 0; i < nChar; ++i)
3819 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
3820 lpWCStr[i] = (WORD)' ';
3821#else
3822 do
3823 {
3824 if (*lpAnsiIn == '\t')
3825 *lpWCStr++ = (WORD)' ';
3826 else
3827 *lpWCStr++ = (WORD)*lpAnsiIn;
3828 nChar++;
3829 } while (*lpAnsiIn++);
3830#endif
3831
3832 return nChar;
3833}
3834
3835
3836#ifdef FEAT_TEAROFF
3837/*
3838 * The callback function for all the modeless dialogs that make up the
3839 * "tearoff menus" Very simple - forward button presses (to fool Vim into
3840 * thinking its menus have been clicked), and go away when closed.
3841 */
3842 static LRESULT CALLBACK
3843tearoff_callback(
3844 HWND hwnd,
3845 UINT message,
3846 WPARAM wParam,
3847 LPARAM lParam)
3848{
3849 if (message == WM_INITDIALOG)
3850 return (TRUE);
3851
3852 /* May show the mouse pointer again. */
3853 HandleMouseHide(message, lParam);
3854
3855 if (message == WM_COMMAND)
3856 {
3857 if ((WORD)(LOWORD(wParam)) & 0x8000)
3858 {
3859 POINT mp;
3860 RECT rect;
3861
3862 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
3863 {
3864 (void)TrackPopupMenu(
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003865 (HMENU)(long_u)(LOWORD(wParam) ^ 0x8000),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 TPM_LEFTALIGN | TPM_LEFTBUTTON,
3867 (int)rect.right - 8,
3868 (int)mp.y,
3869 (int)0, /*reserved param*/
3870 s_hwnd,
3871 NULL);
3872 /*
3873 * NOTE: The pop-up menu can eat the mouse up event.
3874 * We deal with this in normal.c.
3875 */
3876 }
3877 }
3878 else
3879 /* Pass on messages to the main Vim window */
3880 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
3881 /*
3882 * Give main window the focus back: this is so after
3883 * choosing a tearoff button you can start typing again
3884 * straight away.
3885 */
3886 (void)SetFocus(s_hwnd);
3887 return TRUE;
3888 }
3889 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
3890 {
3891 DestroyWindow(hwnd);
3892 return TRUE;
3893 }
3894
3895 /* When moved around, give main window the focus back. */
3896 if (message == WM_EXITSIZEMOVE)
3897 (void)SetActiveWindow(s_hwnd);
3898
3899 return FALSE;
3900}
3901#endif
3902
3903
3904/*
3905 * Decide whether to use the "new look" (small, non-bold font) or the "old
3906 * look" (big, clanky font) for dialogs, and work out a few values for use
3907 * later accordingly.
3908 */
3909 static void
3910get_dialog_font_metrics(void)
3911{
3912 HDC hdc;
3913 HFONT hfontTools = 0;
3914 DWORD dlgFontSize;
3915 SIZE size;
3916#ifdef USE_SYSMENU_FONT
3917 LOGFONT lfSysmenu;
3918#endif
3919
3920 s_usenewlook = FALSE;
3921
3922 /*
3923 * For NT3.51 and Win32s, we stick with the old look
3924 * because it matches everything else.
3925 */
3926 if (!is_winnt_3())
3927 {
3928#ifdef USE_SYSMENU_FONT
3929 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
3930 hfontTools = CreateFontIndirect(&lfSysmenu);
3931 else
3932#endif
3933 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
3934 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
3935
3936 if (hfontTools)
3937 {
3938 hdc = GetDC(s_hwnd);
3939 SelectObject(hdc, hfontTools);
3940 /*
3941 * GetTextMetrics() doesn't return the right value in
3942 * tmAveCharWidth, so we have to figure out the dialog base units
3943 * ourselves.
3944 */
3945 GetTextExtentPoint(hdc,
3946 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
3947 52, &size);
3948 ReleaseDC(s_hwnd, hdc);
3949
3950 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
3951 s_dlgfntheight = (WORD)size.cy;
3952 s_usenewlook = TRUE;
3953 }
3954 }
3955
3956 if (!s_usenewlook)
3957 {
3958 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
3959 s_dlgfntwidth = LOWORD(dlgFontSize);
3960 s_dlgfntheight = HIWORD(dlgFontSize);
3961 }
3962}
3963
3964#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
3965/*
3966 * Create a pseudo-"tearoff menu" based on the child
3967 * items of a given menu pointer.
3968 */
3969 static void
3970gui_mch_tearoff(
3971 char_u *title,
3972 vimmenu_T *menu,
3973 int initX,
3974 int initY)
3975{
3976 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
3977 int template_len;
3978 int nchar, textWidth, submenuWidth;
3979 DWORD lStyle;
3980 DWORD lExtendedStyle;
3981 WORD dlgwidth;
3982 WORD menuID;
3983 vimmenu_T *pmenu;
3984 vimmenu_T *the_menu = menu;
3985 HWND hwnd;
3986 HDC hdc;
3987 HFONT font, oldFont;
3988 int col, spaceWidth, len;
3989 int columnWidths[2];
3990 char_u *label, *text;
3991 int acLen = 0;
3992 int nameLen;
3993 int padding0, padding1, padding2 = 0;
3994 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003995 int x;
3996 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997#ifdef USE_SYSMENU_FONT
3998 LOGFONT lfSysmenu;
3999 int use_lfSysmenu = FALSE;
4000#endif
4001
4002 /*
4003 * If this menu is already torn off, move it to the mouse position.
4004 */
4005 if (IsWindow(menu->tearoff_handle))
4006 {
4007 POINT mp;
4008 if (GetCursorPos((LPPOINT)&mp))
4009 {
4010 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
4011 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
4012 }
4013 return;
4014 }
4015
4016 /*
4017 * Create a new tearoff.
4018 */
4019 if (*title == MNU_HIDDEN_CHAR)
4020 title++;
4021
4022 /* Allocate memory to store the dialog template. It's made bigger when
4023 * needed. */
4024 template_len = DLG_ALLOC_SIZE;
4025 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
4026 if (p == NULL)
4027 return;
4028
4029 hwnd = GetDesktopWindow();
4030 hdc = GetWindowDC(hwnd);
4031#ifdef USE_SYSMENU_FONT
4032 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
4033 {
4034 font = CreateFontIndirect(&lfSysmenu);
4035 use_lfSysmenu = TRUE;
4036 }
4037 else
4038#endif
4039 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4040 VARIABLE_PITCH , DLG_FONT_NAME);
4041 if (s_usenewlook)
4042 oldFont = SelectFont(hdc, font);
4043 else
4044 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
4045
4046 /* Calculate width of a single space. Used for padding columns to the
4047 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004048 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049
4050 /* Figure out max width of the text column, the accelerator column and the
4051 * optional submenu column. */
4052 submenuWidth = 0;
4053 for (col = 0; col < 2; col++)
4054 {
4055 columnWidths[col] = 0;
4056 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
4057 {
4058 /* Use "dname" here to compute the width of the visible text. */
4059 text = (col == 0) ? pmenu->dname : pmenu->actext;
4060 if (text != NULL && *text != NUL)
4061 {
4062 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
4063 if (textWidth > columnWidths[col])
4064 columnWidths[col] = textWidth;
4065 }
4066 if (pmenu->children != NULL)
4067 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
4068 }
4069 }
4070 if (columnWidths[1] == 0)
4071 {
4072 /* no accelerators */
4073 if (submenuWidth != 0)
4074 columnWidths[0] += submenuWidth;
4075 else
4076 columnWidths[0] += spaceWidth;
4077 }
4078 else
4079 {
4080 /* there is an accelerator column */
4081 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
4082 columnWidths[1] += submenuWidth;
4083 }
4084
4085 /*
4086 * Now find the total width of our 'menu'.
4087 */
4088 textWidth = columnWidths[0] + columnWidths[1];
4089 if (submenuWidth != 0)
4090 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004091 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
4093 textWidth += submenuWidth;
4094 }
4095 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
4096 if (textWidth > dlgwidth)
4097 dlgwidth = textWidth;
4098 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
4099
4100 /* W95 can't do thin dialogs, they look v. weird! */
4101 if (mch_windows95() && dlgwidth < TEAROFF_MIN_WIDTH)
4102 dlgwidth = TEAROFF_MIN_WIDTH;
4103
4104 /* start to fill in the dlgtemplate information. addressing by WORDs */
4105 if (s_usenewlook)
4106 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
4107 else
4108 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
4109
4110 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
4111 *p++ = LOWORD(lStyle);
4112 *p++ = HIWORD(lStyle);
4113 *p++ = LOWORD(lExtendedStyle);
4114 *p++ = HIWORD(lExtendedStyle);
4115 pnumitems = p; /* save where the number of items must be stored */
4116 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004117 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004119 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 else
4121 *p++ = PixelToDialogX(initX); // x
4122 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004123 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 else
4125 *p++ = PixelToDialogY(initY); // y
4126 *p++ = PixelToDialogX(dlgwidth); // cx
4127 ptrueheight = p;
4128 *p++ = 0; // dialog height: changed later anyway
4129 *p++ = 0; // Menu
4130 *p++ = 0; // Class
4131
4132 /* copy the title of the dialog */
4133 nchar = nCopyAnsiToWideChar(p, ((*title)
4134 ? (LPSTR)title
4135 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
4136 p += nchar;
4137
4138 if (s_usenewlook)
4139 {
4140 /* do the font, since DS_3DLOOK doesn't work properly */
4141#ifdef USE_SYSMENU_FONT
4142 if (use_lfSysmenu)
4143 {
4144 /* point size */
4145 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
4146 GetDeviceCaps(hdc, LOGPIXELSY));
4147 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
4148 }
4149 else
4150#endif
4151 {
4152 *p++ = DLG_FONT_POINT_SIZE; // point size
4153 nchar = nCopyAnsiToWideChar (p, TEXT(DLG_FONT_NAME));
4154 }
4155 p += nchar;
4156 }
4157
4158 /*
4159 * Loop over all the items in the menu.
4160 * But skip over the tearbar.
4161 */
4162 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
4163 menu = menu->children->next;
4164 else
4165 menu = menu->children;
4166 for ( ; menu != NULL; menu = menu->next)
4167 {
4168 if (menu->modes == 0) /* this menu has just been deleted */
4169 continue;
4170 if (menu_is_separator(menu->dname))
4171 {
4172 sepPadding += 3;
4173 continue;
4174 }
4175
4176 /* Check if there still is plenty of room in the template. Make it
4177 * larger when needed. */
4178 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
4179 {
4180 WORD *newp;
4181
4182 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
4183 if (newp != NULL)
4184 {
4185 template_len += 4096;
4186 mch_memmove(newp, pdlgtemplate,
4187 (char *)p - (char *)pdlgtemplate);
4188 p = newp + (p - pdlgtemplate);
4189 pnumitems = newp + (pnumitems - pdlgtemplate);
4190 ptrueheight = newp + (ptrueheight - pdlgtemplate);
4191 LocalFree(LocalHandle(pdlgtemplate));
4192 pdlgtemplate = newp;
4193 }
4194 }
4195
4196 /* Figure out minimal length of this menu label. Use "name" for the
4197 * actual text, "dname" for estimating the displayed size. "name"
4198 * has "&a" for mnemonic and includes the accelerator. */
4199 len = nameLen = (int)STRLEN(menu->name);
4200 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
4201 (int)STRLEN(menu->dname))) / spaceWidth;
4202 len += padding0;
4203
4204 if (menu->actext != NULL)
4205 {
4206 acLen = (int)STRLEN(menu->actext);
4207 len += acLen;
4208 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
4209 }
4210 else
4211 textWidth = 0;
4212 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
4213 len += padding1;
4214
4215 if (menu->children == NULL)
4216 {
4217 padding2 = submenuWidth / spaceWidth;
4218 len += padding2;
4219 menuID = (WORD)(menu->id);
4220 }
4221 else
4222 {
4223 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004224 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 }
4226
4227 /* Allocate menu label and fill it in */
4228 text = label = alloc((unsigned)len + 1);
4229 if (label == NULL)
4230 break;
4231
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004232 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 text = vim_strchr(text, TAB); /* stop at TAB before actext */
4234 if (text == NULL)
4235 text = label + nameLen; /* no actext, use whole name */
4236 while (padding0-- > 0)
4237 *text++ = ' ';
4238 if (menu->actext != NULL)
4239 {
4240 STRNCPY(text, menu->actext, acLen);
4241 text += acLen;
4242 }
4243 while (padding1-- > 0)
4244 *text++ = ' ';
4245 if (menu->children != NULL)
4246 {
4247 STRCPY(text, TEAROFF_SUBMENU_LABEL);
4248 text += STRLEN(TEAROFF_SUBMENU_LABEL);
4249 }
4250 else
4251 {
4252 while (padding2-- > 0)
4253 *text++ = ' ';
4254 }
4255 *text = NUL;
4256
4257 /*
4258 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
4259 * W95/NT4 it makes the tear-off look more like a menu.
4260 */
4261 p = add_dialog_element(p,
4262 BS_PUSHBUTTON|BS_LEFT,
4263 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
4264 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
4265 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
4266 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004267 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 vim_free(label);
4269 (*pnumitems)++;
4270 }
4271
4272 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
4273
4274
4275 /* show modelessly */
4276 the_menu->tearoff_handle = CreateDialogIndirect(
4277 s_hinst,
4278 (LPDLGTEMPLATE)pdlgtemplate,
4279 s_hwnd,
4280 (DLGPROC)tearoff_callback);
4281
4282 LocalFree(LocalHandle(pdlgtemplate));
4283 SelectFont(hdc, oldFont);
4284 DeleteObject(font);
4285 ReleaseDC(hwnd, hdc);
4286
4287 /*
4288 * Reassert ourselves as the active window. This is so that after creating
4289 * a tearoff, the user doesn't have to click with the mouse just to start
4290 * typing again!
4291 */
4292 (void)SetActiveWindow(s_hwnd);
4293
4294 /* make sure the right buttons are enabled */
4295 force_menu_update = TRUE;
4296}
4297#endif
4298
4299#if defined(FEAT_TOOLBAR) || defined(PROTO)
4300#include "gui_w32_rc.h"
4301
4302/* This not defined in older SDKs */
4303# ifndef TBSTYLE_FLAT
4304# define TBSTYLE_FLAT 0x0800
4305# endif
4306
4307/*
4308 * Create the toolbar, initially unpopulated.
4309 * (just like the menu, there are no defaults, it's all
4310 * set up through menu.vim)
4311 */
4312 static void
4313initialise_toolbar(void)
4314{
4315 InitCommonControls();
4316 s_toolbarhwnd = CreateToolbarEx(
4317 s_hwnd,
4318 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
4319 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004320 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 s_hinst,
4322 IDR_TOOLBAR1, // id of initial bitmap
4323 NULL,
4324 0, // initial number of buttons
4325 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
4326 TOOLBAR_BUTTON_HEIGHT,
4327 TOOLBAR_BUTTON_WIDTH,
4328 TOOLBAR_BUTTON_HEIGHT,
4329 sizeof(TBBUTTON)
4330 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004331 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332
4333 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
4334}
4335
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004336 static LRESULT CALLBACK
4337toolbar_wndproc(
4338 HWND hwnd,
4339 UINT uMsg,
4340 WPARAM wParam,
4341 LPARAM lParam)
4342{
4343 HandleMouseHide(uMsg, lParam);
4344 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
4345}
4346
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 static int
4348get_toolbar_bitmap(vimmenu_T *menu)
4349{
4350 int i = -1;
4351
4352 /*
4353 * Check user bitmaps first, unless builtin is specified.
4354 */
4355 if (!is_winnt_3() && !menu->icon_builtin)
4356 {
4357 char_u fname[MAXPATHL];
4358 HANDLE hbitmap = NULL;
4359
4360 if (menu->iconfile != NULL)
4361 {
4362 gui_find_iconfile(menu->iconfile, fname, "bmp");
4363 hbitmap = LoadImage(
4364 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004365 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 IMAGE_BITMAP,
4367 TOOLBAR_BUTTON_WIDTH,
4368 TOOLBAR_BUTTON_HEIGHT,
4369 LR_LOADFROMFILE |
4370 LR_LOADMAP3DCOLORS
4371 );
4372 }
4373
4374 /*
4375 * If the LoadImage call failed, or the "icon=" file
4376 * didn't exist or wasn't specified, try the menu name
4377 */
4378 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02004379 && (gui_find_bitmap(
4380#ifdef FEAT_MULTI_LANG
4381 menu->en_dname != NULL ? menu->en_dname :
4382#endif
4383 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 hbitmap = LoadImage(
4385 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004386 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 IMAGE_BITMAP,
4388 TOOLBAR_BUTTON_WIDTH,
4389 TOOLBAR_BUTTON_HEIGHT,
4390 LR_LOADFROMFILE |
4391 LR_LOADMAP3DCOLORS
4392 );
4393
4394 if (hbitmap != NULL)
4395 {
4396 TBADDBITMAP tbAddBitmap;
4397
4398 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004399 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400
4401 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
4402 (WPARAM)1, (LPARAM)&tbAddBitmap);
4403 /* i will be set to -1 if it fails */
4404 }
4405 }
4406 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
4407 i = menu->iconidx;
4408
4409 return i;
4410}
4411#endif
4412
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004413#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
4414 static void
4415initialise_tabline(void)
4416{
4417 InitCommonControls();
4418
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004419 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004420 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004421 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4422 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004423 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004424
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004425 gui.tabline_height = TABLINE_HEIGHT;
4426
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004427# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004428 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004429# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004430}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004431
4432 static LRESULT CALLBACK
4433tabline_wndproc(
4434 HWND hwnd,
4435 UINT uMsg,
4436 WPARAM wParam,
4437 LPARAM lParam)
4438{
4439 HandleMouseHide(uMsg, lParam);
4440 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
4441}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004442#endif
4443
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
4445/*
4446 * Make the GUI window come to the foreground.
4447 */
4448 void
4449gui_mch_set_foreground(void)
4450{
4451 if (IsIconic(s_hwnd))
4452 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
4453 SetForegroundWindow(s_hwnd);
4454}
4455#endif
4456
4457#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4458 static void
4459dyn_imm_load(void)
4460{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02004461 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 if (hLibImm == NULL)
4463 return;
4464
4465 pImmGetCompositionStringA
4466 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
4467 pImmGetCompositionStringW
4468 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
4469 pImmGetContext
4470 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
4471 pImmAssociateContext
4472 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
4473 pImmReleaseContext
4474 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
4475 pImmGetOpenStatus
4476 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
4477 pImmSetOpenStatus
4478 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
4479 pImmGetCompositionFont
4480 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
4481 pImmSetCompositionFont
4482 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
4483 pImmSetCompositionWindow
4484 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
4485 pImmGetConversionStatus
4486 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00004487 pImmSetConversionStatus
4488 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
4490 if ( pImmGetCompositionStringA == NULL
4491 || pImmGetCompositionStringW == NULL
4492 || pImmGetContext == NULL
4493 || pImmAssociateContext == NULL
4494 || pImmReleaseContext == NULL
4495 || pImmGetOpenStatus == NULL
4496 || pImmSetOpenStatus == NULL
4497 || pImmGetCompositionFont == NULL
4498 || pImmSetCompositionFont == NULL
4499 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00004500 || pImmGetConversionStatus == NULL
4501 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 {
4503 FreeLibrary(hLibImm);
4504 hLibImm = NULL;
4505 pImmGetContext = NULL;
4506 return;
4507 }
4508
4509 return;
4510}
4511
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512#endif
4513
4514#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
4515
4516# ifdef FEAT_XPM_W32
4517# define IMAGE_XPM 100
4518# endif
4519
4520typedef struct _signicon_t
4521{
4522 HANDLE hImage;
4523 UINT uType;
4524#ifdef FEAT_XPM_W32
4525 HANDLE hShape; /* Mask bitmap handle */
4526#endif
4527} signicon_t;
4528
4529 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004530gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531{
4532 signicon_t *sign;
4533 int x, y, w, h;
4534
4535 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
4536 return;
4537
4538 x = TEXT_X(col);
4539 y = TEXT_Y(row);
4540 w = gui.char_width * 2;
4541 h = gui.char_height;
4542 switch (sign->uType)
4543 {
4544 case IMAGE_BITMAP:
4545 {
4546 HDC hdcMem;
4547 HBITMAP hbmpOld;
4548
4549 hdcMem = CreateCompatibleDC(s_hdc);
4550 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
4551 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
4552 SelectObject(hdcMem, hbmpOld);
4553 DeleteDC(hdcMem);
4554 }
4555 break;
4556 case IMAGE_ICON:
4557 case IMAGE_CURSOR:
4558 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
4559 break;
4560#ifdef FEAT_XPM_W32
4561 case IMAGE_XPM:
4562 {
4563 HDC hdcMem;
4564 HBITMAP hbmpOld;
4565
4566 hdcMem = CreateCompatibleDC(s_hdc);
4567 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
4568 /* Make hole */
4569 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
4570
4571 SelectObject(hdcMem, sign->hImage);
4572 /* Paint sign */
4573 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
4574 SelectObject(hdcMem, hbmpOld);
4575 DeleteDC(hdcMem);
4576 }
4577 break;
4578#endif
4579 }
4580}
4581
4582 static void
4583close_signicon_image(signicon_t *sign)
4584{
4585 if (sign)
4586 switch (sign->uType)
4587 {
4588 case IMAGE_BITMAP:
4589 DeleteObject((HGDIOBJ)sign->hImage);
4590 break;
4591 case IMAGE_CURSOR:
4592 DestroyCursor((HCURSOR)sign->hImage);
4593 break;
4594 case IMAGE_ICON:
4595 DestroyIcon((HICON)sign->hImage);
4596 break;
4597#ifdef FEAT_XPM_W32
4598 case IMAGE_XPM:
4599 DeleteObject((HBITMAP)sign->hImage);
4600 DeleteObject((HBITMAP)sign->hShape);
4601 break;
4602#endif
4603 }
4604}
4605
4606 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004607gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608{
4609 signicon_t sign, *psign;
4610 char_u *ext;
4611
4612 if (is_winnt_3())
4613 {
4614 EMSG(_(e_signdata));
4615 return NULL;
4616 }
4617
4618 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004619 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 if (ext > signfile)
4621 {
4622 int do_load = 1;
4623
4624 if (!STRICMP(ext, ".bmp"))
4625 sign.uType = IMAGE_BITMAP;
4626 else if (!STRICMP(ext, ".ico"))
4627 sign.uType = IMAGE_ICON;
4628 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
4629 sign.uType = IMAGE_CURSOR;
4630 else
4631 do_load = 0;
4632
4633 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004634 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 gui.char_width * 2, gui.char_height,
4636 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
4637#ifdef FEAT_XPM_W32
4638 if (!STRICMP(ext, ".xpm"))
4639 {
4640 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004641 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
4642 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 }
4644#endif
4645 }
4646
4647 psign = NULL;
4648 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
4649 != NULL)
4650 *psign = sign;
4651
4652 if (!psign)
4653 {
4654 if (sign.hImage)
4655 close_signicon_image(&sign);
4656 EMSG(_(e_signdata));
4657 }
4658 return (void *)psign;
4659
4660}
4661
4662 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004663gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664{
4665 if (sign)
4666 {
4667 close_signicon_image((signicon_t *)sign);
4668 vim_free(sign);
4669 }
4670}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672
4673#if defined(FEAT_BEVAL) || defined(PROTO)
4674
4675/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004676 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 *
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004678 * The only reused thing is gui_beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 * from gui_beval.c (note it uses x and y of the BalloonEval struct
4680 * to get current mouse position).
4681 *
4682 * Trying to use as more Windows services as possible, and as less
4683 * IE version as possible :)).
4684 *
4685 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
4686 * BalloonEval struct.
4687 * 2) Enable/Disable simply create/kill BalloonEval Timer
4688 * 3) When there was enough inactivity, timer procedure posts
4689 * async request to debugger
4690 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
4691 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00004692 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 */
4694
Bram Moolenaar45360022005-07-21 21:08:21 +00004695/*
4696 * determine whether installed Common Controls support multiline tooltips
4697 * (i.e. their version is >= 4.70
4698 */
4699 int
4700multiline_balloon_available(void)
4701{
4702 HINSTANCE hDll;
4703 static char comctl_dll[] = "comctl32.dll";
4704 static int multiline_tip = MAYBE;
4705
4706 if (multiline_tip != MAYBE)
4707 return multiline_tip;
4708
4709 hDll = GetModuleHandle(comctl_dll);
4710 if (hDll != NULL)
4711 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004712 DLLGETVERSIONPROC pGetVer;
4713 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00004714
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004715 if (pGetVer != NULL)
4716 {
4717 DLLVERSIONINFO dvi;
4718 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00004719
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004720 ZeroMemory(&dvi, sizeof(dvi));
4721 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00004722
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004723 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00004724
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004725 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00004726 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004727 || (dvi.dwMajorVersion == 4
4728 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00004729 {
4730 multiline_tip = TRUE;
4731 return multiline_tip;
4732 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004733 }
Bram Moolenaar45360022005-07-21 21:08:21 +00004734 else
4735 {
4736 /* there is chance we have ancient CommCtl 4.70
4737 which doesn't export DllGetVersion */
4738 DWORD dwHandle = 0;
4739 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
4740 if (len > 0)
4741 {
4742 VS_FIXEDFILEINFO *ver;
4743 UINT vlen = 0;
4744 void *data = alloc(len);
4745
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004746 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00004747 && GetFileVersionInfo(comctl_dll, 0, len, data)
4748 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
4749 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004750 && HIWORD(ver->dwFileVersionMS) > 4)
4751 || ((HIWORD(ver->dwFileVersionMS) == 4
4752 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00004753 {
4754 vim_free(data);
4755 multiline_tip = TRUE;
4756 return multiline_tip;
4757 }
4758 vim_free(data);
4759 }
4760 }
4761 }
4762 multiline_tip = FALSE;
4763 return multiline_tip;
4764}
4765
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004767make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768{
Bram Moolenaar45360022005-07-21 21:08:21 +00004769 TOOLINFO *pti;
4770 int ToolInfoSize;
4771
4772 if (multiline_balloon_available() == TRUE)
4773 ToolInfoSize = sizeof(TOOLINFO_NEW);
4774 else
4775 ToolInfoSize = sizeof(TOOLINFO);
4776
4777 pti = (TOOLINFO *)alloc(ToolInfoSize);
4778 if (pti == NULL)
4779 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780
4781 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
4782 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
4783 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4784 beval->target, NULL, s_hinst, NULL);
4785
4786 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
4787 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
4788
Bram Moolenaar45360022005-07-21 21:08:21 +00004789 pti->cbSize = ToolInfoSize;
4790 pti->uFlags = TTF_SUBCLASS;
4791 pti->hwnd = beval->target;
4792 pti->hinst = 0; /* Don't use string resources */
4793 pti->uId = ID_BEVAL_TOOLTIP;
4794
4795 if (multiline_balloon_available() == TRUE)
4796 {
4797 RECT rect;
4798 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
4799 pti->lpszText = LPSTR_TEXTCALLBACK;
4800 ptin->lParam = (LPARAM)text;
4801 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
4802 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
4803 (LPARAM)rect.right);
4804 }
4805 else
4806 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807
4808 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00004809 pti->rect.left = pt.x - 3;
4810 pti->rect.top = pt.y - 3;
4811 pti->rect.right = pt.x + 3;
4812 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
Bram Moolenaar45360022005-07-21 21:08:21 +00004814 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 /* Make tooltip appear sooner */
4816 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00004817 /* I've performed some tests and it seems the longest possible life time
4818 * of tooltip is 30 seconds */
4819 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 /*
4821 * HACK: force tooltip to appear, because it'll not appear until
4822 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00004823 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00004825 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00004827 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828}
4829
4830 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004831delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02004833 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834}
4835
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004836/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004838BevalTimerProc(
4839 HWND hwnd,
4840 UINT uMsg,
4841 UINT_PTR idEvent,
4842 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843{
4844 POINT pt;
4845 RECT rect;
4846
4847 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
4848 return;
4849
4850 GetCursorPos(&pt);
4851 if (WindowFromPoint(pt) != s_textArea)
4852 return;
4853
4854 ScreenToClient(s_textArea, &pt);
4855 GetClientRect(s_textArea, &rect);
4856 if (!PtInRect(&rect, pt))
4857 return;
4858
4859 if (LastActivity > 0
4860 && (dwTime - LastActivity) >= (DWORD)p_bdlay
4861 && (cur_beval->showState != ShS_PENDING
4862 || abs(cur_beval->x - pt.x) > 3
4863 || abs(cur_beval->y - pt.y) > 3))
4864 {
4865 /* Pointer resting in one place long enough, it's time to show
4866 * the tooltip. */
4867 cur_beval->showState = ShS_PENDING;
4868 cur_beval->x = pt.x;
4869 cur_beval->y = pt.y;
4870
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004871 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872
4873 if (cur_beval->msgCB != NULL)
4874 (*cur_beval->msgCB)(cur_beval, 0);
4875 }
4876}
4877
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004878/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004880gui_mch_disable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004882 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004884 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885}
4886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004887/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004889gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004891 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 if (beval == NULL)
4893 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004894 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02004895 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004896 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897}
4898
4899 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004900gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901{
4902 POINT pt;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004903 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 if (beval->showState == ShS_SHOWING)
4905 return;
4906 GetCursorPos(&pt);
4907 ScreenToClient(s_textArea, &pt);
4908
4909 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
4910 /* cursor is still here */
4911 {
4912 gui_mch_disable_beval_area(cur_beval);
4913 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004914 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004916 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917}
4918
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004919/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004921gui_mch_create_beval_area(
4922 void *target, /* ignored, always use s_textArea */
4923 char_u *mesg,
4924 void (*mesgCB)(BalloonEval *, int),
4925 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926{
4927 /* partially stolen from gui_beval.c */
4928 BalloonEval *beval;
4929
4930 if (mesg != NULL && mesgCB != NULL)
4931 {
4932 EMSG(_("E232: Cannot create BalloonEval with both message and callback"));
4933 return NULL;
4934 }
4935
4936 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
4937 if (beval != NULL)
4938 {
4939 beval->target = s_textArea;
4940 beval->balloon = NULL;
4941
4942 beval->showState = ShS_NEUTRAL;
4943 beval->x = 0;
4944 beval->y = 0;
4945 beval->msg = mesg;
4946 beval->msgCB = mesgCB;
4947 beval->clientData = clientData;
4948
4949 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 cur_beval = beval;
4951
4952 if (p_beval)
4953 gui_mch_enable_beval_area(beval);
4954
4955 }
4956 return beval;
4957}
4958
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004959/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 static void
Bram Moolenaar442b4222010-05-24 21:34:22 +02004961Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962{
4963 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
4964 return;
4965
4966 if (cur_beval != NULL)
4967 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004968 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004970 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004971 // TRACE0("TTN_SHOW {{{");
4972 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00004973 break;
4974 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004975 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 delete_tooltip(cur_beval);
4977 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004978 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979
4980 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00004981 break;
4982 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004983 {
4984 /* if you get there then we have new common controls */
4985 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
4986 info->lpszText = (LPSTR)info->lParam;
4987 info->uFlags |= TTF_DI_SETITEM;
4988 }
Bram Moolenaar45360022005-07-21 21:08:21 +00004989 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 }
4991 }
4992}
4993
4994 static void
4995TrackUserActivity(UINT uMsg)
4996{
4997 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
4998 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
4999 LastActivity = GetTickCount();
5000}
5001
5002 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005003gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004{
5005 vim_free(beval);
5006}
5007#endif /* FEAT_BEVAL */
5008
5009#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5010/*
5011 * We have multiple signs to draw at the same location. Draw the
5012 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
5013 */
5014 void
5015netbeans_draw_multisign_indicator(int row)
5016{
5017 int i;
5018 int y;
5019 int x;
5020
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005021 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005022 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005023
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 x = 0;
5025 y = TEXT_Y(row);
5026
5027 for (i = 0; i < gui.char_height - 3; i++)
5028 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
5029
5030 SetPixel(s_hdc, x+0, y, gui.currFgColor);
5031 SetPixel(s_hdc, x+2, y, gui.currFgColor);
5032 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
5033 SetPixel(s_hdc, x+1, y, gui.currFgColor);
5034 SetPixel(s_hdc, x+2, y, gui.currFgColor);
5035 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
5036 SetPixel(s_hdc, x+2, y, gui.currFgColor);
5037}
Bram Moolenaare0874f82016-01-24 20:36:41 +01005038#endif