blob: b496166e3f69e1ab8f5349ba6d6ed92c2fb4a766 [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
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020060 char_u *p, *q;
61
62 int dx_enable = 0;
63 int dx_flags = 0;
64 float dx_gamma = 0.0f;
65 float dx_contrast = 0.0f;
66 float dx_level = 0.0f;
67 int dx_geom = 0;
68 int dx_renmode = 0;
69 int dx_taamode = 0;
70
71 /* parse string as rendering options. */
72 for (p = s; p != NULL && *p != NUL; )
73 {
74 char_u item[256];
75 char_u name[128];
76 char_u value[128];
77
Bram Moolenaarcde88542015-08-11 19:14:00 +020078 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020079 if (p == NULL)
80 break;
81 q = &item[0];
82 copy_option_part(&q, name, sizeof(name), ":");
83 if (q == NULL)
84 return FAIL;
85 copy_option_part(&q, value, sizeof(value), ":");
86
87 if (STRCMP(name, "type") == 0)
88 {
89 if (STRCMP(value, "directx") == 0)
90 dx_enable = 1;
91 else
92 return FAIL;
93 }
94 else if (STRCMP(name, "gamma") == 0)
95 {
96 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010097 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020098 }
99 else if (STRCMP(name, "contrast") == 0)
100 {
101 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100102 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200103 }
104 else if (STRCMP(name, "level") == 0)
105 {
106 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100107 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200108 }
109 else if (STRCMP(name, "geom") == 0)
110 {
111 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100112 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200113 if (dx_geom < 0 || dx_geom > 2)
114 return FAIL;
115 }
116 else if (STRCMP(name, "renmode") == 0)
117 {
118 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100119 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200120 if (dx_renmode < 0 || dx_renmode > 6)
121 return FAIL;
122 }
123 else if (STRCMP(name, "taamode") == 0)
124 {
125 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100126 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200127 if (dx_taamode < 0 || dx_taamode > 3)
128 return FAIL;
129 }
130 else
131 return FAIL;
132 }
133
134 /* Enable DirectX/DirectWrite */
135 if (dx_enable)
136 {
137 if (!directx_enabled())
138 return FAIL;
139 DWriteContext_SetRenderingParams(s_dwc, NULL);
140 if (dx_flags)
141 {
142 DWriteRenderingParams param;
143 DWriteContext_GetRenderingParams(s_dwc, &param);
144 if (dx_flags & (1 << 0))
145 param.gamma = dx_gamma;
146 if (dx_flags & (1 << 1))
147 param.enhancedContrast = dx_contrast;
148 if (dx_flags & (1 << 2))
149 param.clearTypeLevel = dx_level;
150 if (dx_flags & (1 << 3))
151 param.pixelGeometry = dx_geom;
152 if (dx_flags & (1 << 4))
153 param.renderingMode = dx_renmode;
154 if (dx_flags & (1 << 5))
155 param.textAntialiasMode = dx_taamode;
156 DWriteContext_SetRenderingParams(s_dwc, &param);
157 }
158 }
159 s_directx_enabled = dx_enable;
160
161 return OK;
162#else
163 return FAIL;
164#endif
165}
166#endif
167
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168/*
169 * These are new in Windows ME/XP, only defined in recent compilers.
170 */
171#ifndef HANDLE_WM_XBUTTONUP
172# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
173 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
174#endif
175#ifndef HANDLE_WM_XBUTTONDOWN
176# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
177 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
178#endif
179#ifndef HANDLE_WM_XBUTTONDBLCLK
180# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
181 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
182#endif
183
184/*
185 * Include the common stuff for MS-Windows GUI.
186 */
187#include "gui_w48.c"
188
189#ifdef FEAT_XPM_W32
190# include "xpm_w32.h"
191#endif
192
193#ifdef PROTO
194# define WINAPI
195#endif
196
197#ifdef __MINGW32__
198/*
199 * Add a lot of missing defines.
200 * They are not always missing, we need the #ifndef's.
201 */
202# ifndef _cdecl
203# define _cdecl
204# endif
205# ifndef IsMinimized
206# define IsMinimized(hwnd) IsIconic(hwnd)
207# endif
208# ifndef IsMaximized
209# define IsMaximized(hwnd) IsZoomed(hwnd)
210# endif
211# ifndef SelectFont
212# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
213# endif
214# ifndef GetStockBrush
215# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
216# endif
217# ifndef DeleteBrush
218# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
219# endif
220
221# ifndef HANDLE_WM_RBUTTONDBLCLK
222# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
223 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
224# endif
225# ifndef HANDLE_WM_MBUTTONUP
226# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
227 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
228# endif
229# ifndef HANDLE_WM_MBUTTONDBLCLK
230# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
231 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
232# endif
233# ifndef HANDLE_WM_LBUTTONDBLCLK
234# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
235 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
236# endif
237# ifndef HANDLE_WM_RBUTTONDOWN
238# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
239 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
240# endif
241# ifndef HANDLE_WM_MOUSEMOVE
242# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
243 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
244# endif
245# ifndef HANDLE_WM_RBUTTONUP
246# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
247 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
248# endif
249# ifndef HANDLE_WM_MBUTTONDOWN
250# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
251 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
252# endif
253# ifndef HANDLE_WM_LBUTTONUP
254# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
255 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
256# endif
257# ifndef HANDLE_WM_LBUTTONDOWN
258# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
259 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
260# endif
261# ifndef HANDLE_WM_SYSCHAR
262# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
263 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
264# endif
265# ifndef HANDLE_WM_ACTIVATEAPP
266# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
267 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
268# endif
269# ifndef HANDLE_WM_WINDOWPOSCHANGING
270# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
271 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
272# endif
273# ifndef HANDLE_WM_VSCROLL
274# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
275 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
276# endif
277# ifndef HANDLE_WM_SETFOCUS
278# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
279 ((fn)((hwnd), (HWND)(wParam)), 0L)
280# endif
281# ifndef HANDLE_WM_KILLFOCUS
282# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
283 ((fn)((hwnd), (HWND)(wParam)), 0L)
284# endif
285# ifndef HANDLE_WM_HSCROLL
286# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
287 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
288# endif
289# ifndef HANDLE_WM_DROPFILES
290# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
291 ((fn)((hwnd), (HDROP)(wParam)), 0L)
292# endif
293# ifndef HANDLE_WM_CHAR
294# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
295 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
296# endif
297# ifndef HANDLE_WM_SYSDEADCHAR
298# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
299 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
300# endif
301# ifndef HANDLE_WM_DEADCHAR
302# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
303 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
304# endif
305#endif /* __MINGW32__ */
306
307
308/* Some parameters for tearoff menus. All in pixels. */
309#define TEAROFF_PADDING_X 2
310#define TEAROFF_BUTTON_PAD_X 8
311#define TEAROFF_MIN_WIDTH 200
312#define TEAROFF_SUBMENU_LABEL ">>"
313#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
314
315
316/* For the Intellimouse: */
317#ifndef WM_MOUSEWHEEL
318#define WM_MOUSEWHEEL 0x20a
319#endif
320
321
322#ifdef FEAT_BEVAL
323# define ID_BEVAL_TOOLTIP 200
324# define BEVAL_TEXT_LEN MAXPATHL
325
Bram Moolenaar167632f2010-05-26 21:42:54 +0200326#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +0000327/* Work around old versions of basetsd.h which wrongly declares
328 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +0200329# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +0000330# define UINT_PTR UINT
331#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000332
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100333static void make_tooltip(BalloonEval *beval, char *text, POINT pt);
334static void delete_tooltip(BalloonEval *beval);
335static VOID CALLBACK BevalTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000336
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000338static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +0000340
Bram Moolenaar82881492012-11-20 16:53:39 +0100341
342/* cproto fails on missing include files */
343#ifndef PROTO
344
Bram Moolenaar45360022005-07-21 21:08:21 +0000345/*
346 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000347 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +0000348 */
Bram Moolenaar82881492012-11-20 16:53:39 +0100349# include <pshpack1.h>
350
351#endif
Bram Moolenaar45360022005-07-21 21:08:21 +0000352
353typedef struct _DllVersionInfo
354{
355 DWORD cbSize;
356 DWORD dwMajorVersion;
357 DWORD dwMinorVersion;
358 DWORD dwBuildNumber;
359 DWORD dwPlatformID;
360} DLLVERSIONINFO;
361
Bram Moolenaar82881492012-11-20 16:53:39 +0100362#ifndef PROTO
363# include <poppack.h>
364#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +0000365
Bram Moolenaar45360022005-07-21 21:08:21 +0000366typedef struct tagTOOLINFOA_NEW
367{
368 UINT cbSize;
369 UINT uFlags;
370 HWND hwnd;
Bram Moolenaar281daf62009-12-24 15:11:40 +0000371 UINT_PTR uId;
Bram Moolenaar45360022005-07-21 21:08:21 +0000372 RECT rect;
373 HINSTANCE hinst;
374 LPSTR lpszText;
375 LPARAM lParam;
376} TOOLINFO_NEW;
377
378typedef struct tagNMTTDISPINFO_NEW
379{
380 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +0000381 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +0000382 char szText[80];
383 HINSTANCE hinst;
384 UINT uFlags;
385 LPARAM lParam;
386} NMTTDISPINFO_NEW;
387
Bram Moolenaar45360022005-07-21 21:08:21 +0000388typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
389#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000390# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391#endif
392
Bram Moolenaar45360022005-07-21 21:08:21 +0000393#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000394# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +0000395#endif
396
397#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000398# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +0000399#endif
400
401#endif /* defined(FEAT_BEVAL) */
402
Bram Moolenaar2c7a7632007-05-10 18:19:11 +0000403#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
404/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
405 * it here if LPNMTTDISPINFO isn't defined.
406 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
407 * _MSC_VER. */
408# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
409typedef struct tagNMTTDISPINFOA {
410 NMHDR hdr;
411 LPSTR lpszText;
412 char szText[80];
413 HINSTANCE hinst;
414 UINT uFlags;
415 LPARAM lParam;
416} NMTTDISPINFOA, *LPNMTTDISPINFOA;
417# define LPNMTTDISPINFO LPNMTTDISPINFOA
418
419# ifdef FEAT_MBYTE
420typedef struct tagNMTTDISPINFOW {
421 NMHDR hdr;
422 LPWSTR lpszText;
423 WCHAR szText[80];
424 HINSTANCE hinst;
425 UINT uFlags;
426 LPARAM lParam;
427} NMTTDISPINFOW, *LPNMTTDISPINFOW;
428# endif
429# endif
430#endif
431
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000432#ifndef TTN_GETDISPINFOW
433# define TTN_GETDISPINFOW (TTN_FIRST - 10)
434#endif
435
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436/* Local variables: */
437
438#ifdef FEAT_MENU
439static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +0200440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441
442/*
443 * Use the system font for dialogs and tear-off menus. Remove this line to
444 * use DLG_FONT_NAME.
445 */
Bram Moolenaar786989b2010-10-27 12:15:33 +0200446#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447
448#define VIM_NAME "vim"
449#define VIM_CLASS "Vim"
450#define VIM_CLASSW L"Vim"
451
452/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
453 * thus there should be room for every dialog. For tearoffs it's made bigger
454 * when needed. */
455#define DLG_ALLOC_SIZE 16 * 1024
456
457/*
458 * stuff for dialogs, menus, tearoffs etc.
459 */
460static LRESULT APIENTRY dialog_callback(HWND, UINT, WPARAM, LPARAM);
461static LRESULT APIENTRY tearoff_callback(HWND, UINT, WPARAM, LPARAM);
462static PWORD
463add_dialog_element(
464 PWORD p,
465 DWORD lStyle,
466 WORD x,
467 WORD y,
468 WORD w,
469 WORD h,
470 WORD Id,
471 WORD clss,
472 const char *caption);
473static LPWORD lpwAlign(LPWORD);
474static int nCopyAnsiToWideChar(LPWORD, LPSTR);
475static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
476static void get_dialog_font_metrics(void);
477
478static int dialog_default_button = -1;
479
480/* Intellimouse support */
481static int mouse_scroll_lines = 0;
482static UINT msh_msgmousewheel = 0;
483
484static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
485#ifdef FEAT_TOOLBAR
486static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +0200487static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488static int get_toolbar_bitmap(vimmenu_T *menu);
489#endif
490
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000491#ifdef FEAT_GUI_TABLINE
492static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +0200493static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000494#endif
495
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496#ifdef FEAT_MBYTE_IME
497static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
498static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
499#endif
500#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
501# ifdef NOIME
502typedef struct tagCOMPOSITIONFORM {
503 DWORD dwStyle;
504 POINT ptCurrentPos;
505 RECT rcArea;
506} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
507typedef HANDLE HIMC;
508# endif
509
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000510static HINSTANCE hLibImm = NULL;
511static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
512static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
513static HIMC (WINAPI *pImmGetContext)(HWND);
514static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
515static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
516static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
517static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
518static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
519static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
520static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
521static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +0000522static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523static void dyn_imm_load(void);
524#else
525# define pImmGetCompositionStringA ImmGetCompositionStringA
526# define pImmGetCompositionStringW ImmGetCompositionStringW
527# define pImmGetContext ImmGetContext
528# define pImmAssociateContext ImmAssociateContext
529# define pImmReleaseContext ImmReleaseContext
530# define pImmGetOpenStatus ImmGetOpenStatus
531# define pImmSetOpenStatus ImmSetOpenStatus
532# define pImmGetCompositionFont ImmGetCompositionFontA
533# define pImmSetCompositionFont ImmSetCompositionFontA
534# define pImmSetCompositionWindow ImmSetCompositionWindow
535# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +0000536# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537#endif
538
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539/* multi monitor support */
540typedef struct _MONITORINFOstruct
541{
542 DWORD cbSize;
543 RECT rcMonitor;
544 RECT rcWork;
545 DWORD dwFlags;
546} _MONITORINFO;
547
548typedef HANDLE _HMONITOR;
549typedef _HMONITOR (WINAPI *TMonitorFromWindow)(HWND, DWORD);
550typedef BOOL (WINAPI *TGetMonitorInfo)(_HMONITOR, _MONITORINFO *);
551
552static TMonitorFromWindow pMonitorFromWindow = NULL;
553static TGetMonitorInfo pGetMonitorInfo = NULL;
554static HANDLE user32_lib = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555/*
556 * Return TRUE when running under Windows NT 3.x or Win32s, both of which have
557 * less fancy GUI APIs.
558 */
559 static int
560is_winnt_3(void)
561{
562 return ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
563 && os_version.dwMajorVersion == 3)
564 || (os_version.dwPlatformId == VER_PLATFORM_WIN32s));
565}
566
567/*
568 * Return TRUE when running under Win32s.
569 */
570 int
571gui_is_win32s(void)
572{
573 return (os_version.dwPlatformId == VER_PLATFORM_WIN32s);
574}
575
576#ifdef FEAT_MENU
577/*
578 * Figure out how high the menu bar is at the moment.
579 */
580 static int
581gui_mswin_get_menu_height(
582 int fix_window) /* If TRUE, resize window if menu height changed */
583{
584 static int old_menu_height = -1;
585
586 RECT rc1, rc2;
587 int num;
588 int menu_height;
589
590 if (gui.menu_is_active)
591 num = GetMenuItemCount(s_menuBar);
592 else
593 num = 0;
594
595 if (num == 0)
596 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +0100597 else if (IsMinimized(s_hwnd))
598 {
599 /* The height of the menu cannot be determined while the window is
600 * minimized. Take the previous height if the menu is changed in that
601 * state, to avoid that Vim's vertical window size accidentally
602 * increases due to the unaccounted-for menu height. */
603 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 else
606 {
607 if (is_winnt_3()) /* for NT 3.xx */
608 {
609 if (gui.starting)
610 menu_height = GetSystemMetrics(SM_CYMENU);
611 else
612 {
613 RECT r1, r2;
614 int frameht = GetSystemMetrics(SM_CYFRAME);
615 int capht = GetSystemMetrics(SM_CYCAPTION);
616
617 /* get window rect of s_hwnd
618 * get client rect of s_hwnd
619 * get cap height
620 * subtract from window rect, the sum of client height,
621 * (if not maximized)frame thickness, and caption height.
622 */
623 GetWindowRect(s_hwnd, &r1);
624 GetClientRect(s_hwnd, &r2);
625 menu_height = r1.bottom - r1.top - (r2.bottom - r2.top
626 + 2 * frameht * (!IsZoomed(s_hwnd)) + capht);
627 }
628 }
629 else /* win95 and variants (NT 4.0, I guess) */
630 {
631 /*
632 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
633 * seem to have been set yet, so menu wraps in default window
634 * width which is very narrow. Instead just return height of a
635 * single menu item. Will still be wrong when the menu really
636 * should wrap over more than one line.
637 */
638 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
639 if (gui.starting)
640 menu_height = rc1.bottom - rc1.top + 1;
641 else
642 {
643 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
644 menu_height = rc2.bottom - rc1.top + 1;
645 }
646 }
647 }
648
649 if (fix_window && menu_height != old_menu_height)
650 {
Bram Moolenaarafa24992006-03-27 20:58:26 +0000651 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 }
Bram Moolenaar71371b12015-03-24 17:57:12 +0100653 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
655 return menu_height;
656}
657#endif /*FEAT_MENU*/
658
659
660/*
661 * Setup for the Intellimouse
662 */
663 static void
664init_mouse_wheel(void)
665{
666
667#ifndef SPI_GETWHEELSCROLLLINES
668# define SPI_GETWHEELSCROLLLINES 104
669#endif
Bram Moolenaare7566042005-06-17 22:00:15 +0000670#ifndef SPI_SETWHEELSCROLLLINES
671# define SPI_SETWHEELSCROLLLINES 105
672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673
674#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
675#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
676#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
677#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
678
679 HWND hdl_mswheel;
680 UINT msh_msgscrolllines;
681
682 msh_msgmousewheel = 0;
683 mouse_scroll_lines = 3; /* reasonable default */
684
685 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
686 && os_version.dwMajorVersion >= 4)
687 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
688 && ((os_version.dwMajorVersion == 4
689 && os_version.dwMinorVersion >= 10)
690 || os_version.dwMajorVersion >= 5)))
691 {
692 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
693 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
694 &mouse_scroll_lines, 0);
695 }
696 else if (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
697 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
698 && os_version.dwMajorVersion < 4))
699 { /*
700 * If Win95 or NT 3.51,
701 * try to find the hidden point32 window.
702 */
703 hdl_mswheel = FindWindow(VMOUSEZ_CLASSNAME, VMOUSEZ_TITLE);
704 if (hdl_mswheel)
705 {
706 msh_msgscrolllines = RegisterWindowMessage(VMSH_SCROLL_LINES);
707 if (msh_msgscrolllines)
708 {
709 mouse_scroll_lines = (int)SendMessage(hdl_mswheel,
710 msh_msgscrolllines, 0, 0);
711 msh_msgmousewheel = RegisterWindowMessage(VMSH_MOUSEWHEEL);
712 }
713 }
714 }
715}
716
717
718/* Intellimouse wheel handler */
719 static void
720_OnMouseWheel(
721 HWND hwnd,
722 short zDelta)
723{
724/* Treat a mouse wheel event as if it were a scroll request */
725 int i;
726 int size;
727 HWND hwndCtl;
728
729 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
730 {
731 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
732 size = curwin->w_scrollbars[SBAR_RIGHT].size;
733 }
734 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
735 {
736 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
737 size = curwin->w_scrollbars[SBAR_LEFT].size;
738 }
739 else
740 return;
741
742 size = curwin->w_height;
743 if (mouse_scroll_lines == 0)
744 init_mouse_wheel();
745
746 if (mouse_scroll_lines > 0
747 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
748 {
749 for (i = mouse_scroll_lines; i > 0; --i)
750 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
751 }
752 else
753 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
754}
755
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000756#ifdef USE_SYSMENU_FONT
757/*
758 * Get Menu Font.
759 * Return OK or FAIL.
760 */
761 static int
762gui_w32_get_menu_font(LOGFONT *lf)
763{
764 NONCLIENTMETRICS nm;
765
766 nm.cbSize = sizeof(NONCLIENTMETRICS);
767 if (!SystemParametersInfo(
768 SPI_GETNONCLIENTMETRICS,
769 sizeof(NONCLIENTMETRICS),
770 &nm,
771 0))
772 return FAIL;
773 *lf = nm.lfMenuFont;
774 return OK;
775}
776#endif
777
778
779#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
780/*
781 * Set the GUI tabline font to the system menu font
782 */
783 static void
784set_tabline_font(void)
785{
786 LOGFONT lfSysmenu;
787 HFONT font;
788 HWND hwnd;
789 HDC hdc;
790 HFONT hfntOld;
791 TEXTMETRIC tm;
792
793 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
794 return;
795
796 font = CreateFontIndirect(&lfSysmenu);
797
798 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
799
800 /*
801 * Compute the height of the font used for the tab text
802 */
803 hwnd = GetDesktopWindow();
804 hdc = GetWindowDC(hwnd);
805 hfntOld = SelectFont(hdc, font);
806
807 GetTextMetrics(hdc, &tm);
808
809 SelectFont(hdc, hfntOld);
810 ReleaseDC(hwnd, hdc);
811
812 /*
813 * The space used by the tab border and the space between the tab label
814 * and the tab border is included as 7.
815 */
816 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
817}
818#endif
819
Bram Moolenaar520470a2005-06-16 21:59:56 +0000820/*
821 * Invoked when a setting was changed.
822 */
823 static LRESULT CALLBACK
824_OnSettingChange(UINT n)
825{
826 if (n == SPI_SETWHEELSCROLLLINES)
827 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
828 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000829#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
830 if (n == SPI_SETNONCLIENTMETRICS)
831 set_tabline_font();
832#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +0000833 return 0;
834}
835
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836#ifdef FEAT_NETBEANS_INTG
837 static void
838_OnWindowPosChanged(
839 HWND hwnd,
840 const LPWINDOWPOS lpwpos)
841{
842 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100843 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
845 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
846 || lpwpos->cx != cx || lpwpos->cy != cy))
847 {
848 x = lpwpos->x;
849 y = lpwpos->y;
850 cx = lpwpos->cx;
851 cy = lpwpos->cy;
852 netbeans_frame_moved(x, y);
853 }
854 /* Allow to send WM_SIZE and WM_MOVE */
855 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
856}
857#endif
858
859 static int
860_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 UINT fwSide,
862 LPRECT lprc)
863{
864 int w, h;
865 int valid_w, valid_h;
866 int w_offset, h_offset;
867
868 w = lprc->right - lprc->left;
869 h = lprc->bottom - lprc->top;
870 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
871 w_offset = w - valid_w;
872 h_offset = h - valid_h;
873
874 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
875 || fwSide == WMSZ_BOTTOMLEFT)
876 lprc->left += w_offset;
877 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
878 || fwSide == WMSZ_BOTTOMRIGHT)
879 lprc->right -= w_offset;
880
881 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
882 || fwSide == WMSZ_TOPRIGHT)
883 lprc->top += h_offset;
884 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
885 || fwSide == WMSZ_BOTTOMRIGHT)
886 lprc->bottom -= h_offset;
887 return TRUE;
888}
889
890
891
892 static LRESULT CALLBACK
893_WndProc(
894 HWND hwnd,
895 UINT uMsg,
896 WPARAM wParam,
897 LPARAM lParam)
898{
899 /*
900 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
901 hwnd, uMsg, wParam, lParam);
902 */
903
904 HandleMouseHide(uMsg, lParam);
905
906 s_uMsg = uMsg;
907 s_wParam = wParam;
908 s_lParam = lParam;
909
910 switch (uMsg)
911 {
912 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
913 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
914 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
915 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
916 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
917 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
918 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
919 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
920 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
921#ifdef FEAT_MENU
922 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
923#endif
924 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
925 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
926 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
927 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
928 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
929 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
930 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
931 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
932 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
933#ifdef FEAT_NETBEANS_INTG
934 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
935#endif
936
Bram Moolenaarafa24992006-03-27 20:58:26 +0000937#ifdef FEAT_GUI_TABLINE
938 case WM_RBUTTONUP:
939 {
940 if (gui_mch_showing_tabline())
941 {
942 POINT pt;
943 RECT rect;
944
945 /*
946 * If the cursor is on the tabline, display the tab menu
947 */
948 GetCursorPos((LPPOINT)&pt);
949 GetWindowRect(s_textArea, &rect);
950 if (pt.y < rect.top)
951 {
952 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +0100953 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +0000954 }
955 }
956 return MyWindowProc(hwnd, uMsg, wParam, lParam);
957 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000958 case WM_LBUTTONDBLCLK:
959 {
960 /*
961 * If the user double clicked the tabline, create a new tab
962 */
963 if (gui_mch_showing_tabline())
964 {
965 POINT pt;
966 RECT rect;
967
968 GetCursorPos((LPPOINT)&pt);
969 GetWindowRect(s_textArea, &rect);
970 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000971 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000972 }
973 return MyWindowProc(hwnd, uMsg, wParam, lParam);
974 }
Bram Moolenaarafa24992006-03-27 20:58:26 +0000975#endif
976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 case WM_QUERYENDSESSION: /* System wants to go down. */
978 gui_shell_closed(); /* Will exit when no changed buffers. */
979 return FALSE; /* Do NOT allow system to go down. */
980
981 case WM_ENDSESSION:
982 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +0100983 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +0100985 return 0L;
986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 break;
988
989 case WM_CHAR:
990 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
991 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000992 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 return 0L;
994
995 case WM_SYSCHAR:
996 /*
997 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
998 * shortcut key, handle like a typed ALT key, otherwise call Windows
999 * ALT key handling.
1000 */
1001#ifdef FEAT_MENU
1002 if ( !gui.menu_is_active
1003 || p_wak[0] == 'n'
1004 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
1005 )
1006#endif
1007 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001008 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 return 0L;
1010 }
1011#ifdef FEAT_MENU
1012 else
1013 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1014#endif
1015
1016 case WM_SYSKEYUP:
1017#ifdef FEAT_MENU
1018 /* This used to be done only when menu is active: ALT key is used for
1019 * that. But that caused problems when menu is disabled and using
1020 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
1021 * are received, mouse pointer remains hidden. */
1022 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1023#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01001024 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025#endif
1026
1027 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001028 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029
1030 case WM_MOUSEWHEEL:
1031 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01001032 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033
Bram Moolenaar520470a2005-06-16 21:59:56 +00001034 /* Notification for change in SystemParametersInfo() */
1035 case WM_SETTINGCHANGE:
1036 return _OnSettingChange((UINT)wParam);
1037
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001038#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 case WM_NOTIFY:
1040 switch (((LPNMHDR) lParam)->code)
1041 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001042# ifdef FEAT_MBYTE
1043 case TTN_GETDISPINFOW:
1044# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001045 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001047 LPNMHDR hdr = (LPNMHDR)lParam;
1048 char_u *str = NULL;
1049 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001050
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001051 vim_free(tt_text);
1052 tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001053
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001054# ifdef FEAT_GUI_TABLINE
1055 if (gui_mch_showing_tabline()
1056 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001057 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001058 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001059 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001060 * Mouse is over the GUI tabline. Display the
1061 * tooltip for the tab under the cursor
1062 *
1063 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001064 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001065 GetCursorPos(&pt);
1066 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001067 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001068 TCHITTESTINFO htinfo;
1069 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001070
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001071 /*
1072 * Get the tab under the cursor
1073 */
1074 htinfo.pt.x = pt.x;
1075 htinfo.pt.y = pt.y;
1076 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
1077 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001078 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001079 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001080
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001081 tp = find_tabpage(idx + 1);
1082 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001083 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001084 get_tabline_label(tp, TRUE);
1085 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001086 }
1087 }
1088 }
1089 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001090# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001091# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001092# ifdef FEAT_GUI_TABLINE
1093 else
1094# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001096 UINT idButton;
1097 vimmenu_T *pMenu;
1098
1099 idButton = (UINT) hdr->idFrom;
1100 pMenu = gui_mswin_find_menu(root_menu, idButton);
1101 if (pMenu)
1102 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001104# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001105 if (str != NULL)
1106 {
1107# ifdef FEAT_MBYTE
1108 if (hdr->code == TTN_GETDISPINFOW)
1109 {
1110 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
1111
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00001112 /* Set the maximum width, this also enables using
1113 * \n for line break. */
1114 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
1115 0, 500);
1116
Bram Moolenaar36f692d2008-11-20 16:10:17 +00001117 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001118 lpdi->lpszText = tt_text;
1119 /* can't show tooltip if failed */
1120 }
1121 else
1122# endif
1123 {
1124 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
1125
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00001126 /* Set the maximum width, this also enables using
1127 * \n for line break. */
1128 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
1129 0, 500);
1130
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001131 if (STRLEN(str) < sizeof(lpdi->szText)
1132 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01001133 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001134 sizeof(lpdi->szText) - 1);
1135 else
1136 lpdi->lpszText = tt_text;
1137 }
1138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 }
1140 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001141# ifdef FEAT_GUI_TABLINE
1142 case TCN_SELCHANGE:
1143 if (gui_mch_showing_tabline()
1144 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01001145 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001146 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01001147 return 0L;
1148 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001149 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00001150
1151 case NM_RCLICK:
1152 if (gui_mch_showing_tabline()
1153 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01001154 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00001155 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01001156 return 0L;
1157 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00001158 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001159# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001161# ifdef FEAT_GUI_TABLINE
1162 if (gui_mch_showing_tabline()
1163 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
1164 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1165# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 break;
1167 }
1168 break;
1169#endif
1170#if defined(MENUHINTS) && defined(FEAT_MENU)
1171 case WM_MENUSELECT:
1172 if (((UINT) HIWORD(wParam)
1173 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
1174 == MF_HILITE
1175 && (State & CMDLINE) == 0)
1176 {
1177 UINT idButton;
1178 vimmenu_T *pMenu;
1179 static int did_menu_tip = FALSE;
1180
1181 if (did_menu_tip)
1182 {
1183 msg_clr_cmdline();
1184 setcursor();
1185 out_flush();
1186 did_menu_tip = FALSE;
1187 }
1188
1189 idButton = (UINT)LOWORD(wParam);
1190 pMenu = gui_mswin_find_menu(root_menu, idButton);
1191 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
1192 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
1193 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00001194 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00001196 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 setcursor();
1198 out_flush();
1199 did_menu_tip = TRUE;
1200 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01001201 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 }
1203 break;
1204#endif
1205 case WM_NCHITTEST:
1206 {
1207 LRESULT result;
1208 int x, y;
1209 int xPos = GET_X_LPARAM(lParam);
1210
1211 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
1212 if (result == HTCLIENT)
1213 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001214#ifdef FEAT_GUI_TABLINE
1215 if (gui_mch_showing_tabline())
1216 {
1217 int yPos = GET_Y_LPARAM(lParam);
1218 RECT rct;
1219
1220 /* If the cursor is on the GUI tabline, don't process this
1221 * event */
1222 GetWindowRect(s_textArea, &rct);
1223 if (yPos < rct.top)
1224 return result;
1225 }
1226#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02001227 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 xPos -= x;
1229
1230 if (xPos < 48) /* <VN> TODO should use system metric? */
1231 return HTBOTTOMLEFT;
1232 else
1233 return HTBOTTOMRIGHT;
1234 }
1235 else
1236 return result;
1237 }
1238 /* break; notreached */
1239
1240#ifdef FEAT_MBYTE_IME
1241 case WM_IME_NOTIFY:
1242 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
1243 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01001244 return 1L;
1245
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 case WM_IME_COMPOSITION:
1247 if (!_OnImeComposition(hwnd, wParam, lParam))
1248 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01001249 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250#endif
1251
1252 default:
1253 if (uMsg == msh_msgmousewheel && msh_msgmousewheel != 0)
1254 { /* handle MSH_MOUSEWHEEL messages for Intellimouse */
1255 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01001256 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 }
1258#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001259 else if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 {
1261 _OnFindRepl();
1262 }
1263#endif
1264 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1265 }
1266
Bram Moolenaar2787ab92011-12-14 15:23:59 +01001267 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268}
1269
1270/*
1271 * End of call-back routines
1272 */
1273
1274/* parent window, if specified with -P */
1275HWND vim_parent_hwnd = NULL;
1276
1277 static BOOL CALLBACK
1278FindWindowTitle(HWND hwnd, LPARAM lParam)
1279{
1280 char buf[2048];
1281 char *title = (char *)lParam;
1282
1283 if (GetWindowText(hwnd, buf, sizeof(buf)))
1284 {
1285 if (strstr(buf, title) != NULL)
1286 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001287 /* Found it. Store the window ref. and quit searching if MDI
1288 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001290 if (vim_parent_hwnd != NULL)
1291 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 }
1293 }
1294 return TRUE; /* continue searching */
1295}
1296
1297/*
1298 * Invoked for '-P "title"' argument: search for parent application to open
1299 * our window in.
1300 */
1301 void
1302gui_mch_set_parent(char *title)
1303{
1304 EnumWindows(FindWindowTitle, (LPARAM)title);
1305 if (vim_parent_hwnd == NULL)
1306 {
1307 EMSG2(_("E671: Cannot find window title \"%s\""), title);
1308 mch_exit(2);
1309 }
1310}
1311
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001312#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 static void
1314ole_error(char *arg)
1315{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00001316 char buf[IOSIZE];
1317
1318 /* Can't use EMSG() here, we have not finished initialisation yet. */
1319 vim_snprintf(buf, IOSIZE,
1320 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
1321 arg);
1322 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325
1326/*
1327 * Parse the GUI related command-line arguments. Any arguments used are
1328 * deleted from argv, and *argc is decremented accordingly. This is called
1329 * when vim is started, whether or not the GUI has been started.
1330 */
1331 void
1332gui_mch_prepare(int *argc, char **argv)
1333{
1334 int silent = FALSE;
1335 int idx;
1336
1337 /* Check for special OLE command line parameters */
1338 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
1339 {
1340 /* Check for a "-silent" argument first. */
1341 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
1342 && (argv[2][0] == '-' || argv[2][0] == '/'))
1343 {
1344 silent = TRUE;
1345 idx = 2;
1346 }
1347 else
1348 idx = 1;
1349
1350 /* Register Vim as an OLE Automation server */
1351 if (STRICMP(argv[idx] + 1, "register") == 0)
1352 {
1353#ifdef FEAT_OLE
1354 RegisterMe(silent);
1355 mch_exit(0);
1356#else
1357 if (!silent)
1358 ole_error("register");
1359 mch_exit(2);
1360#endif
1361 }
1362
1363 /* Unregister Vim as an OLE Automation server */
1364 if (STRICMP(argv[idx] + 1, "unregister") == 0)
1365 {
1366#ifdef FEAT_OLE
1367 UnregisterMe(!silent);
1368 mch_exit(0);
1369#else
1370 if (!silent)
1371 ole_error("unregister");
1372 mch_exit(2);
1373#endif
1374 }
1375
1376 /* Ignore an -embedding argument. It is only relevant if the
1377 * application wants to treat the case when it is started manually
1378 * differently from the case where it is started via automation (and
1379 * we don't).
1380 */
1381 if (STRICMP(argv[idx] + 1, "embedding") == 0)
1382 {
1383#ifdef FEAT_OLE
1384 *argc = 1;
1385#else
1386 ole_error("embedding");
1387 mch_exit(2);
1388#endif
1389 }
1390 }
1391
1392#ifdef FEAT_OLE
1393 {
1394 int bDoRestart = FALSE;
1395
1396 InitOLE(&bDoRestart);
1397 /* automatically exit after registering */
1398 if (bDoRestart)
1399 mch_exit(0);
1400 }
1401#endif
1402
1403#ifdef FEAT_NETBEANS_INTG
1404 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001405 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 int arg;
1407
1408 for (arg = 1; arg < *argc; arg++)
1409 if (strncmp("-nb", argv[arg], 3) == 0)
1410 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 netbeansArg = argv[arg];
1412 mch_memmove(&argv[arg], &argv[arg + 1],
1413 (--*argc - arg) * sizeof(char *));
1414 argv[*argc] = NULL;
1415 break; /* enough? */
1416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 }
1418#endif
1419
1420 /* get the OS version info */
1421 os_version.dwOSVersionInfoSize = sizeof(os_version);
1422 GetVersionEx(&os_version); /* this call works on Win32s, Win95 and WinNT */
1423
1424 /* try and load the user32.dll library and get the entry points for
1425 * multi-monitor-support. */
Bram Moolenaarebbcb822010-10-23 14:02:54 +02001426 if ((user32_lib = vimLoadLib("User32.dll")) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 {
1428 pMonitorFromWindow = (TMonitorFromWindow)GetProcAddress(user32_lib,
1429 "MonitorFromWindow");
1430
1431 /* there are ...A and ...W version of GetMonitorInfo - looking at
1432 * winuser.h, they have exactly the same declaration. */
1433 pGetMonitorInfo = (TGetMonitorInfo)GetProcAddress(user32_lib,
1434 "GetMonitorInfoA");
1435 }
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001436
1437#ifdef FEAT_MBYTE
1438 /* If the OS is Windows NT, use wide functions;
1439 * this enables common dialogs input unicode from IME. */
1440 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
1441 {
1442 pDispatchMessage = DispatchMessageW;
1443 pGetMessage = GetMessageW;
1444 pIsDialogMessage = IsDialogMessageW;
1445 pPeekMessage = PeekMessageW;
1446 }
1447 else
1448 {
1449 pDispatchMessage = DispatchMessageA;
1450 pGetMessage = GetMessageA;
1451 pIsDialogMessage = IsDialogMessageA;
1452 pPeekMessage = PeekMessageA;
1453 }
1454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455}
1456
1457/*
1458 * Initialise the GUI. Create all the windows, set up all the call-backs
1459 * etc.
1460 */
1461 int
1462gui_mch_init(void)
1463{
1464 const char szVimWndClass[] = VIM_CLASS;
1465 const char szTextAreaClass[] = "VimTextArea";
1466 WNDCLASS wndclass;
1467#ifdef FEAT_MBYTE
1468 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01001469 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 WNDCLASSW wndclassw;
1471#endif
1472#ifdef GLOBAL_IME
1473 ATOM atom;
1474#endif
1475
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 /* Return here if the window was already opened (happens when
1477 * gui_mch_dialog() is called early). */
1478 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00001479 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480
1481 /*
1482 * Load the tearoff bitmap
1483 */
1484#ifdef FEAT_TEAROFF
1485 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
1486#endif
1487
1488 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
1489 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
1490#ifdef FEAT_MENU
1491 gui.menu_height = 0; /* Windows takes care of this */
1492#endif
1493 gui.border_width = 0;
1494
1495 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
1496
1497#ifdef FEAT_MBYTE
1498 /* First try using the wide version, so that we can use any title.
1499 * Otherwise only characters in the active codepage will work. */
1500 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
1501 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001502 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 wndclassw.lpfnWndProc = _WndProc;
1504 wndclassw.cbClsExtra = 0;
1505 wndclassw.cbWndExtra = 0;
1506 wndclassw.hInstance = s_hinst;
1507 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
1508 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
1509 wndclassw.hbrBackground = s_brush;
1510 wndclassw.lpszMenuName = NULL;
1511 wndclassw.lpszClassName = szVimWndClassW;
1512
1513 if ((
1514#ifdef GLOBAL_IME
1515 atom =
1516#endif
1517 RegisterClassW(&wndclassw)) == 0)
1518 {
1519 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1520 return FAIL;
1521
1522 /* Must be Windows 98, fall back to non-wide function. */
1523 }
1524 else
1525 wide_WindowProc = TRUE;
1526 }
1527
1528 if (!wide_WindowProc)
1529#endif
1530
1531 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
1532 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001533 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 wndclass.lpfnWndProc = _WndProc;
1535 wndclass.cbClsExtra = 0;
1536 wndclass.cbWndExtra = 0;
1537 wndclass.hInstance = s_hinst;
1538 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
1539 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1540 wndclass.hbrBackground = s_brush;
1541 wndclass.lpszMenuName = NULL;
1542 wndclass.lpszClassName = szVimWndClass;
1543
1544 if ((
1545#ifdef GLOBAL_IME
1546 atom =
1547#endif
1548 RegisterClass(&wndclass)) == 0)
1549 return FAIL;
1550 }
1551
1552 if (vim_parent_hwnd != NULL)
1553 {
1554#ifdef HAVE_TRY_EXCEPT
1555 __try
1556 {
1557#endif
1558 /* Open inside the specified parent window.
1559 * TODO: last argument should point to a CLIENTCREATESTRUCT
1560 * structure. */
1561 s_hwnd = CreateWindowEx(
1562 WS_EX_MDICHILD,
1563 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02001564 WS_OVERLAPPEDWINDOW | WS_CHILD
1565 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
1567 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
1568 100, /* Any value will do */
1569 100, /* Any value will do */
1570 vim_parent_hwnd, NULL,
1571 s_hinst, NULL);
1572#ifdef HAVE_TRY_EXCEPT
1573 }
1574 __except(EXCEPTION_EXECUTE_HANDLER)
1575 {
1576 /* NOP */
1577 }
1578#endif
1579 if (s_hwnd == NULL)
1580 {
1581 EMSG(_("E672: Unable to open window inside MDI application"));
1582 mch_exit(2);
1583 }
1584 }
1585 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001586 {
1587 /* If the provided windowid is not valid reset it to zero, so that it
1588 * is ignored and we open our own window. */
1589 if (IsWindow((HWND)win_socket_id) <= 0)
1590 win_socket_id = 0;
1591
1592 /* Create a window. If win_socket_id is not zero without border and
1593 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00001595 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02001596 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
1597 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00001598 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
1599 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
1600 100, /* Any value will do */
1601 100, /* Any value will do */
1602 NULL, NULL,
1603 s_hinst, NULL);
1604 if (s_hwnd != NULL && win_socket_id != 0)
1605 {
1606 SetParent(s_hwnd, (HWND)win_socket_id);
1607 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
1608 }
1609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610
1611 if (s_hwnd == NULL)
1612 return FAIL;
1613
1614#ifdef GLOBAL_IME
1615 global_ime_init(atom, s_hwnd);
1616#endif
1617#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
1618 dyn_imm_load();
1619#endif
1620
1621 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01001622#ifdef FEAT_MBYTE
1623 if (wide_WindowProc)
1624 {
1625 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
1626 {
1627 wndclassw.style = CS_OWNDC;
1628 wndclassw.lpfnWndProc = _TextAreaWndProc;
1629 wndclassw.cbClsExtra = 0;
1630 wndclassw.cbWndExtra = 0;
1631 wndclassw.hInstance = s_hinst;
1632 wndclassw.hIcon = NULL;
1633 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
1634 wndclassw.hbrBackground = NULL;
1635 wndclassw.lpszMenuName = NULL;
1636 wndclassw.lpszClassName = szTextAreaClassW;
1637
1638 if (RegisterClassW(&wndclassw) == 0)
1639 return FAIL;
1640 }
1641 }
1642 else
1643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
1645 {
1646 wndclass.style = CS_OWNDC;
1647 wndclass.lpfnWndProc = _TextAreaWndProc;
1648 wndclass.cbClsExtra = 0;
1649 wndclass.cbWndExtra = 0;
1650 wndclass.hInstance = s_hinst;
1651 wndclass.hIcon = NULL;
1652 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1653 wndclass.hbrBackground = NULL;
1654 wndclass.lpszMenuName = NULL;
1655 wndclass.lpszClassName = szTextAreaClass;
1656
1657 if (RegisterClass(&wndclass) == 0)
1658 return FAIL;
1659 }
1660 s_textArea = CreateWindowEx(
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01001661 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 szTextAreaClass, "Vim text area",
1663 WS_CHILD | WS_VISIBLE, 0, 0,
1664 100, /* Any value will do for now */
1665 100, /* Any value will do for now */
1666 s_hwnd, NULL,
1667 s_hinst, NULL);
1668
1669 if (s_textArea == NULL)
1670 return FAIL;
1671
Bram Moolenaar20321902016-02-17 12:30:17 +01001672#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02001673 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
1674 {
1675 HANDLE hIcon = NULL;
1676
1677 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02001678 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02001679 }
Bram Moolenaar20321902016-02-17 12:30:17 +01001680#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02001681
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682#ifdef FEAT_MENU
1683 s_menuBar = CreateMenu();
1684#endif
1685 s_hdc = GetDC(s_textArea);
1686
1687#ifdef MSWIN16_FASTTEXT
1688 SetBkMode(s_hdc, OPAQUE);
1689#endif
1690
1691#ifdef FEAT_WINDOWS
1692 DragAcceptFiles(s_hwnd, TRUE);
1693#endif
1694
1695 /* Do we need to bother with this? */
1696 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
1697
1698 /* Get background/foreground colors from the system */
1699 gui_mch_def_colors();
1700
1701 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
1702 * file) */
1703 set_normal_colors();
1704
1705 /*
1706 * Check that none of the colors are the same as the background color.
1707 * Then store the current values as the defaults.
1708 */
1709 gui_check_colors();
1710 gui.def_norm_pixel = gui.norm_pixel;
1711 gui.def_back_pixel = gui.back_pixel;
1712
1713 /* Get the colors for the highlight groups (gui_check_colors() might have
1714 * changed them) */
1715 highlight_gui_started();
1716
1717 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01001718 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01001720 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
1722 /*
1723 * Set up for Intellimouse processing
1724 */
1725 init_mouse_wheel();
1726
1727 /*
1728 * compute a couple of metrics used for the dialogs
1729 */
1730 get_dialog_font_metrics();
1731#ifdef FEAT_TOOLBAR
1732 /*
1733 * Create the toolbar
1734 */
1735 initialise_toolbar();
1736#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001737#ifdef FEAT_GUI_TABLINE
1738 /*
1739 * Create the tabline
1740 */
1741 initialise_tabline();
1742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743#ifdef MSWIN_FIND_REPLACE
1744 /*
1745 * Initialise the dialog box stuff
1746 */
1747 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
1748
1749 /* Initialise the struct */
1750 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01001751 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01001753 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1755 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
1756 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00001757# if defined(FEAT_MBYTE) && defined(WIN3264)
1758 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
1759 s_findrep_struct_w.lpstrFindWhat =
1760 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
1761 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
1762 s_findrep_struct_w.lpstrReplaceWith =
1763 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
1764 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
1765 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
1766 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
1767# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02001770#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01001771# if !defined(_MSC_VER) || (_MSC_VER < 1400)
1772/* Define HandleToLong for old MS and non-MS compilers if not defined. */
1773# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01001774# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01001775# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02001776# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02001777 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02001778 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02001779#endif
1780
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02001781#ifdef FEAT_RENDER_OPTIONS
1782 if (p_rop)
1783 (void)gui_mch_set_rendering_options(p_rop);
1784#endif
1785
Bram Moolenaar748bf032005-02-02 23:04:36 +00001786theend:
1787 /* Display any pending error messages */
1788 display_errors();
1789
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 return OK;
1791}
1792
1793/*
1794 * Get the size of the screen, taking position on multiple monitors into
1795 * account (if supported).
1796 */
1797 static void
1798get_work_area(RECT *spi_rect)
1799{
1800 _HMONITOR mon;
1801 _MONITORINFO moninfo;
1802
1803 /* use these functions only if available */
1804 if (pMonitorFromWindow != NULL && pGetMonitorInfo != NULL)
1805 {
1806 /* work out which monitor the window is on, and get *it's* work area */
1807 mon = pMonitorFromWindow(s_hwnd, 1 /*MONITOR_DEFAULTTOPRIMARY*/);
1808 if (mon != NULL)
1809 {
1810 moninfo.cbSize = sizeof(_MONITORINFO);
1811 if (pGetMonitorInfo(mon, &moninfo))
1812 {
1813 *spi_rect = moninfo.rcWork;
1814 return;
1815 }
1816 }
1817 }
1818 /* this is the old method... */
1819 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
1820}
1821
1822/*
1823 * Set the size of the window to the given width and height in pixels.
1824 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001825/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 void
1827gui_mch_set_shellsize(int width, int height,
Bram Moolenaarafa24992006-03-27 20:58:26 +00001828 int min_width, int min_height, int base_width, int base_height,
1829 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830{
1831 RECT workarea_rect;
1832 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 WINDOWPLACEMENT wndpl;
1834
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001835 /* Try to keep window completely on screen. */
1836 /* Get position of the screen work area. This is the part that is not
1837 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 get_work_area(&workarea_rect);
1839
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001840 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001841 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 wndpl.length = sizeof(WINDOWPLACEMENT);
1843 GetWindowPlacement(s_hwnd, &wndpl);
1844
1845 /* Resizing a maximized window looks very strange, unzoom it first.
1846 * But don't do it when still starting up, it may have been requested in
1847 * the shortcut. */
1848 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
1849 {
1850 ShowWindow(s_hwnd, SW_SHOWNORMAL);
1851 /* Need to get the settings of the normal window. */
1852 GetWindowPlacement(s_hwnd, &wndpl);
1853 }
1854
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02001856 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02001857 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02001858 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02001859 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 + GetSystemMetrics(SM_CYCAPTION)
1861#ifdef FEAT_MENU
1862 + gui_mswin_get_menu_height(FALSE)
1863#endif
1864 ;
1865
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001866 /* The following should take care of keeping Vim on the same monitor, no
1867 * matter if the secondary monitor is left or right of the primary
1868 * monitor. */
1869 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
1870 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00001871
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001872 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001873 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001874 && wndpl.rcNormalPosition.right > workarea_rect.right)
1875 OffsetRect(&wndpl.rcNormalPosition,
1876 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001878 if ((direction & RESIZE_HOR)
1879 && wndpl.rcNormalPosition.left < workarea_rect.left)
1880 OffsetRect(&wndpl.rcNormalPosition,
1881 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882
Bram Moolenaarafa24992006-03-27 20:58:26 +00001883 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001884 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
1885 OffsetRect(&wndpl.rcNormalPosition,
1886 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001888 if ((direction & RESIZE_VERT)
1889 && wndpl.rcNormalPosition.top < workarea_rect.top)
1890 OffsetRect(&wndpl.rcNormalPosition,
1891 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892
1893 /* set window position - we should use SetWindowPlacement rather than
1894 * SetWindowPos as the MSDN docs say the coord systems returned by
1895 * these two are not compatible. */
1896 SetWindowPlacement(s_hwnd, &wndpl);
1897
1898 SetActiveWindow(s_hwnd);
1899 SetFocus(s_hwnd);
1900
1901#ifdef FEAT_MENU
1902 /* Menu may wrap differently now */
1903 gui_mswin_get_menu_height(!gui.starting);
1904#endif
1905}
1906
1907
1908 void
1909gui_mch_set_scrollbar_thumb(
1910 scrollbar_T *sb,
1911 long val,
1912 long size,
1913 long max)
1914{
1915 SCROLLINFO info;
1916
1917 sb->scroll_shift = 0;
1918 while (max > 32767)
1919 {
1920 max = (max + 1) >> 1;
1921 val >>= 1;
1922 size >>= 1;
1923 ++sb->scroll_shift;
1924 }
1925
1926 if (sb->scroll_shift > 0)
1927 ++size;
1928
1929 info.cbSize = sizeof(info);
1930 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
1931 info.nPos = val;
1932 info.nMin = 0;
1933 info.nMax = max;
1934 info.nPage = size;
1935 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
1936}
1937
1938
1939/*
1940 * Set the current text font.
1941 */
1942 void
1943gui_mch_set_font(GuiFont font)
1944{
1945 gui.currFont = font;
1946}
1947
1948
1949/*
1950 * Set the current text foreground color.
1951 */
1952 void
1953gui_mch_set_fg_color(guicolor_T color)
1954{
1955 gui.currFgColor = color;
1956}
1957
1958/*
1959 * Set the current text background color.
1960 */
1961 void
1962gui_mch_set_bg_color(guicolor_T color)
1963{
1964 gui.currBgColor = color;
1965}
1966
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001967/*
1968 * Set the current text special color.
1969 */
1970 void
1971gui_mch_set_sp_color(guicolor_T color)
1972{
1973 gui.currSpColor = color;
1974}
1975
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976#if defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)
1977/*
1978 * Multi-byte handling, originally by Sung-Hoon Baek.
1979 * First static functions (no prototypes generated).
1980 */
1981#ifdef _MSC_VER
1982# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
1983#endif
1984#include <imm.h>
1985
1986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 * handle WM_IME_NOTIFY message
1988 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001989/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 static LRESULT
1991_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData)
1992{
1993 LRESULT lResult = 0;
1994 HIMC hImc;
1995
1996 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
1997 return lResult;
1998 switch (dwCommand)
1999 {
2000 case IMN_SETOPENSTATUS:
2001 if (pImmGetOpenStatus(hImc))
2002 {
2003 pImmSetCompositionFont(hImc, &norm_logfont);
2004 im_set_position(gui.row, gui.col);
2005
2006 /* Disable langmap */
2007 State &= ~LANGMAP;
2008 if (State & INSERT)
2009 {
2010#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
2011 /* Unshown 'keymap' in status lines */
2012 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2013 {
2014 /* Save cursor position */
2015 int old_row = gui.row;
2016 int old_col = gui.col;
2017
2018 // This must be called here before
2019 // status_redraw_curbuf(), otherwise the mode
2020 // message may appear in the wrong position.
2021 showmode();
2022 status_redraw_curbuf();
2023 update_screen(0);
2024 /* Restore cursor position */
2025 gui.row = old_row;
2026 gui.col = old_col;
2027 }
2028#endif
2029 }
2030 }
2031 gui_update_cursor(TRUE, FALSE);
2032 lResult = 0;
2033 break;
2034 }
2035 pImmReleaseContext(hWnd, hImc);
2036 return lResult;
2037}
2038
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002039/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 static LRESULT
2041_OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param)
2042{
2043 char_u *ret;
2044 int len;
2045
2046 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
2047 return 0;
2048
2049 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
2050 if (ret != NULL)
2051 {
2052 add_to_input_buf_csi(ret, len);
2053 vim_free(ret);
2054 return 1;
2055 }
2056 return 0;
2057}
2058
2059/*
2060 * get the current composition string, in UCS-2; *lenp is the number of
2061 * *lenp is the number of Unicode characters.
2062 */
2063 static short_u *
2064GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
2065{
2066 LONG ret;
2067 LPWSTR wbuf = NULL;
2068 char_u *buf;
2069
2070 if (!pImmGetContext)
2071 return NULL; /* no imm32.dll */
2072
2073 /* Try Unicode; this'll always work on NT regardless of codepage. */
2074 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
2075 if (ret == 0)
2076 return NULL; /* empty */
2077
2078 if (ret > 0)
2079 {
2080 /* Allocate the requested buffer plus space for the NUL character. */
2081 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
2082 if (wbuf != NULL)
2083 {
2084 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
2085 *lenp = ret / sizeof(WCHAR);
2086 }
2087 return (short_u *)wbuf;
2088 }
2089
2090 /* ret < 0; we got an error, so try the ANSI version. This'll work
2091 * on 9x/ME, but only if the codepage happens to be set to whatever
2092 * we're inputting. */
2093 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
2094 if (ret <= 0)
2095 return NULL; /* empty or error */
2096
2097 buf = alloc(ret);
2098 if (buf == NULL)
2099 return NULL;
2100 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
2101
2102 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01002103 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 vim_free(buf);
2105
2106 return (short_u *)wbuf;
2107}
2108
2109/*
2110 * void GetResultStr()
2111 *
2112 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
2113 * get complete composition string
2114 */
2115 static char_u *
2116GetResultStr(HWND hwnd, int GCS, int *lenp)
2117{
2118 HIMC hIMC; /* Input context handle. */
2119 short_u *buf = NULL;
2120 char_u *convbuf = NULL;
2121
2122 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
2123 return NULL;
2124
2125 /* Reads in the composition string. */
2126 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
2127 if (buf == NULL)
2128 return NULL;
2129
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002130 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 pImmReleaseContext(hwnd, hIMC);
2132 vim_free(buf);
2133 return convbuf;
2134}
2135#endif
2136
2137/* For global functions we need prototypes. */
2138#if (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) || defined(PROTO)
2139
2140/*
2141 * set font to IM.
2142 */
2143 void
2144im_set_font(LOGFONT *lf)
2145{
2146 HIMC hImc;
2147
2148 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
2149 {
2150 pImmSetCompositionFont(hImc, lf);
2151 pImmReleaseContext(s_hwnd, hImc);
2152 }
2153}
2154
2155/*
2156 * Notify cursor position to IM.
2157 */
2158 void
2159im_set_position(int row, int col)
2160{
2161 HIMC hImc;
2162
2163 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
2164 {
2165 COMPOSITIONFORM cfs;
2166
2167 cfs.dwStyle = CFS_POINT;
2168 cfs.ptCurrentPos.x = FILL_X(col);
2169 cfs.ptCurrentPos.y = FILL_Y(row);
2170 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
2171 pImmSetCompositionWindow(hImc, &cfs);
2172
2173 pImmReleaseContext(s_hwnd, hImc);
2174 }
2175}
2176
2177/*
2178 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
2179 */
2180 void
2181im_set_active(int active)
2182{
2183 HIMC hImc;
2184 static HIMC hImcOld = (HIMC)0;
2185
2186 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
2187 {
2188 if (p_imdisable)
2189 {
2190 if (hImcOld == (HIMC)0)
2191 {
2192 hImcOld = pImmGetContext(s_hwnd);
2193 if (hImcOld)
2194 pImmAssociateContext(s_hwnd, (HIMC)0);
2195 }
2196 active = FALSE;
2197 }
2198 else if (hImcOld != (HIMC)0)
2199 {
2200 pImmAssociateContext(s_hwnd, hImcOld);
2201 hImcOld = (HIMC)0;
2202 }
2203
2204 hImc = pImmGetContext(s_hwnd);
2205 if (hImc)
2206 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002207 /*
2208 * for Korean ime
2209 */
2210 HKL hKL = GetKeyboardLayout(0);
2211
2212 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
2213 {
2214 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
2215 static BOOL bSaved = FALSE;
2216
2217 if (active)
2218 {
2219 /* if we have a saved conversion status, restore it */
2220 if (bSaved)
2221 pImmSetConversionStatus(hImc, dwConversionSaved,
2222 dwSentenceSaved);
2223 bSaved = FALSE;
2224 }
2225 else
2226 {
2227 /* save conversion status and disable korean */
2228 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
2229 &dwSentenceSaved))
2230 {
2231 bSaved = TRUE;
2232 pImmSetConversionStatus(hImc,
2233 dwConversionSaved & ~(IME_CMODE_NATIVE
2234 | IME_CMODE_FULLSHAPE),
2235 dwSentenceSaved);
2236 }
2237 }
2238 }
2239
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 pImmSetOpenStatus(hImc, active);
2241 pImmReleaseContext(s_hwnd, hImc);
2242 }
2243 }
2244}
2245
2246/*
2247 * Get IM status. When IM is on, return not 0. Else return 0.
2248 */
2249 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002250im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251{
2252 int status = 0;
2253 HIMC hImc;
2254
2255 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
2256 {
2257 status = pImmGetOpenStatus(hImc) ? 1 : 0;
2258 pImmReleaseContext(s_hwnd, hImc);
2259 }
2260 return status;
2261}
2262
2263#endif /* FEAT_MBYTE && FEAT_MBYTE_IME */
2264
2265#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
2266/* Win32 with GLOBAL IME */
2267
2268/*
2269 * Notify cursor position to IM.
2270 */
2271 void
2272im_set_position(int row, int col)
2273{
2274 /* Win32 with GLOBAL IME */
2275 POINT p;
2276
2277 p.x = FILL_X(col);
2278 p.y = FILL_Y(row);
2279 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
2280 global_ime_set_position(&p);
2281}
2282
2283/*
2284 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
2285 */
2286 void
2287im_set_active(int active)
2288{
2289 global_ime_set_status(active);
2290}
2291
2292/*
2293 * Get IM status. When IM is on, return not 0. Else return 0.
2294 */
2295 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002296im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297{
2298 return global_ime_get_status();
2299}
2300#endif
2301
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002302#ifdef FEAT_MBYTE
2303/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00002304 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002305 */
2306 static void
2307latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
2308{
2309 int c;
2310
Bram Moolenaarca003e12006-03-17 23:19:38 +00002311 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002312 {
2313 c = *text++;
2314 switch (c)
2315 {
2316 case 0xa4: c = 0x20ac; break; /* euro */
2317 case 0xa6: c = 0x0160; break; /* S hat */
2318 case 0xa8: c = 0x0161; break; /* S -hat */
2319 case 0xb4: c = 0x017d; break; /* Z hat */
2320 case 0xb8: c = 0x017e; break; /* Z -hat */
2321 case 0xbc: c = 0x0152; break; /* OE */
2322 case 0xbd: c = 0x0153; break; /* oe */
2323 case 0xbe: c = 0x0178; break; /* Y */
2324 }
2325 *unicodebuf++ = c;
2326 }
2327}
2328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329
2330#ifdef FEAT_RIGHTLEFT
2331/*
2332 * What is this for? In the case where you are using Win98 or Win2K or later,
2333 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
2334 * reverses the string sent to the TextOut... family. This sucks, because we
2335 * go to a lot of effort to do the right thing, and there doesn't seem to be a
2336 * way to tell Windblows not to do this!
2337 *
2338 * The short of it is that this 'RevOut' only gets called if you are running
2339 * one of the new, "improved" MS OSes, and only if you are running in
2340 * 'rightleft' mode. It makes display take *slightly* longer, but not
2341 * noticeably so.
2342 */
2343 static void
2344RevOut( HDC s_hdc,
2345 int col,
2346 int row,
2347 UINT foptions,
2348 CONST RECT *pcliprect,
2349 LPCTSTR text,
2350 UINT len,
2351 CONST INT *padding)
2352{
2353 int ix;
2354 static int special = -1;
2355
2356 if (special == -1)
2357 {
2358 /* Check windows version: special treatment is needed if it is NT 5 or
2359 * Win98 or higher. */
2360 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
2361 && os_version.dwMajorVersion >= 5)
2362 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
2363 && (os_version.dwMajorVersion > 4
2364 || (os_version.dwMajorVersion == 4
2365 && os_version.dwMinorVersion > 0))))
2366 special = 1;
2367 else
2368 special = 0;
2369 }
2370
2371 if (special)
2372 for (ix = 0; ix < (int)len; ++ix)
2373 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
2374 pcliprect, text + ix, 1, padding);
2375 else
2376 ExtTextOut(s_hdc, col, row, foptions, pcliprect, text, len, padding);
2377}
2378#endif
2379
2380 void
2381gui_mch_draw_string(
2382 int row,
2383 int col,
2384 char_u *text,
2385 int len,
2386 int flags)
2387{
2388 static int *padding = NULL;
2389 static int pad_size = 0;
2390 int i;
2391 const RECT *pcliprect = NULL;
2392 UINT foptions = 0;
2393#ifdef FEAT_MBYTE
2394 static WCHAR *unicodebuf = NULL;
2395 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002396 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 int n = 0;
2398#endif
2399 HPEN hpen, old_pen;
2400 int y;
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002401#ifdef FEAT_DIRECTX
2402 int font_is_ttf_or_vector = 0;
2403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404
2405#ifndef MSWIN16_FASTTEXT
2406 /*
2407 * Italic and bold text seems to have an extra row of pixels at the bottom
2408 * (below where the bottom of the character should be). If we draw the
2409 * characters with a solid background, the top row of pixels in the
2410 * character below will be overwritten. We can fix this by filling in the
2411 * background ourselves, to the correct character proportions, and then
2412 * writing the character in transparent mode. Still have a problem when
2413 * the character is "_", which gets written on to the character below.
2414 * New fix: set gui.char_ascent to -1. This shifts all characters up one
2415 * pixel in their slots, which fixes the problem with the bottom row of
2416 * pixels. We still need this code because otherwise the top row of pixels
2417 * becomes a problem. - webb.
2418 */
2419 static HBRUSH hbr_cache[2] = {NULL, NULL};
2420 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
2421 static int brush_lru = 0;
2422 HBRUSH hbr;
2423 RECT rc;
2424
2425 if (!(flags & DRAW_TRANSP))
2426 {
2427 /*
2428 * Clear background first.
2429 * Note: FillRect() excludes right and bottom of rectangle.
2430 */
2431 rc.left = FILL_X(col);
2432 rc.top = FILL_Y(row);
2433#ifdef FEAT_MBYTE
2434 if (has_mbyte)
2435 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002437 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 }
2439 else
2440#endif
2441 rc.right = FILL_X(col + len);
2442 rc.bottom = FILL_Y(row + 1);
2443
2444 /* Cache the created brush, that saves a lot of time. We need two:
2445 * one for cursor background and one for the normal background. */
2446 if (gui.currBgColor == brush_color[0])
2447 {
2448 hbr = hbr_cache[0];
2449 brush_lru = 1;
2450 }
2451 else if (gui.currBgColor == brush_color[1])
2452 {
2453 hbr = hbr_cache[1];
2454 brush_lru = 0;
2455 }
2456 else
2457 {
2458 if (hbr_cache[brush_lru] != NULL)
2459 DeleteBrush(hbr_cache[brush_lru]);
2460 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
2461 brush_color[brush_lru] = gui.currBgColor;
2462 hbr = hbr_cache[brush_lru];
2463 brush_lru = !brush_lru;
2464 }
2465 FillRect(s_hdc, &rc, hbr);
2466
2467 SetBkMode(s_hdc, TRANSPARENT);
2468
2469 /*
2470 * When drawing block cursor, prevent inverted character spilling
2471 * over character cell (can happen with bold/italic)
2472 */
2473 if (flags & DRAW_CURSOR)
2474 {
2475 pcliprect = &rc;
2476 foptions = ETO_CLIPPED;
2477 }
2478 }
2479#else
2480 /*
2481 * The alternative would be to write the characters in opaque mode, but
2482 * when the text is not exactly the same proportions as normal text, too
2483 * big or too little a rectangle gets drawn for the background.
2484 */
2485 SetBkMode(s_hdc, OPAQUE);
2486 SetBkColor(s_hdc, gui.currBgColor);
2487#endif
2488 SetTextColor(s_hdc, gui.currFgColor);
2489 SelectFont(s_hdc, gui.currFont);
2490
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002491#ifdef FEAT_DIRECTX
2492 if (IS_ENABLE_DIRECTX())
2493 {
2494 TEXTMETRIC tm;
2495
2496 GetTextMetrics(s_hdc, &tm);
2497 if (tm.tmPitchAndFamily & (TMPF_TRUETYPE | TMPF_VECTOR))
2498 {
2499 font_is_ttf_or_vector = 1;
2500 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
2501 }
2502 }
2503#endif
2504
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
2506 {
2507 vim_free(padding);
2508 pad_size = Columns;
2509
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002510 /* Don't give an out-of-memory message here, it would call us
2511 * recursively. */
2512 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 if (padding != NULL)
2514 for (i = 0; i < pad_size; i++)
2515 padding[i] = gui.char_width;
2516 }
2517
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 /*
2519 * We have to provide the padding argument because italic and bold versions
2520 * of fixed-width fonts are often one pixel or so wider than their normal
2521 * versions.
2522 * No check for DRAW_BOLD, Windows will have done it already.
2523 */
2524
2525#ifdef FEAT_MBYTE
2526 /* Check if there are any UTF-8 characters. If not, use normal text
2527 * output to speed up output. */
2528 if (enc_utf8)
2529 for (n = 0; n < len; ++n)
2530 if (text[n] >= 0x80)
2531 break;
2532
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002533#if defined(FEAT_DIRECTX)
2534 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
2535 * required that unicode drawing routine, currently. So this forces it
2536 * enabled. */
2537 if (enc_utf8 && IS_ENABLE_DIRECTX())
2538 n = 0; /* Keep n < len, to enter block for unicode. */
2539#endif
2540
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002542 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002544 if ((enc_utf8
2545 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
2546 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 && (unicodebuf == NULL || len > unibuflen))
2548 {
2549 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002550 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
2552 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002553 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554
2555 unibuflen = len;
2556 }
2557
2558 if (enc_utf8 && n < len && unicodebuf != NULL)
2559 {
2560 /* Output UTF-8 characters. Caller has already separated
2561 * composing characters. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002562 int i;
2563 int wlen; /* string length in words */
2564 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 int cells; /* cell width of string up to composing char */
2566 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002567 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002569 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002570 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002572 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002574 c = utf_ptr2char(text + i);
2575 if (c >= 0x10000)
2576 {
2577 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002578 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
2579 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002580 }
2581 else
2582 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002583 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002584 }
2585 cw = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 if (cw > 2) /* don't use 4 for unprintable char */
2587 cw = 1;
2588 if (unicodepdy != NULL)
2589 {
2590 /* Use unicodepdy to make characters fit as we expect, even
2591 * when the font uses different widths (e.g., bold character
2592 * is wider). */
2593 unicodepdy[clen] = cw * gui.char_width;
2594 }
2595 cells += cw;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002596 i += utfc_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00002597 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002599#if defined(FEAT_DIRECTX)
2600 if (IS_ENABLE_DIRECTX() && font_is_ttf_or_vector)
2601 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02002602 /* Add one to "cells" for italics. */
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002603 DWriteContext_DrawText(s_dwc, s_hdc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02002604 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002605 gui.char_width, gui.currFgColor);
2606 }
2607 else
2608#endif
2609 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
2610 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 len = cells; /* used for underlining */
2612 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002613 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {
2615 /* If we want to display codepage data, and the current CP is not the
2616 * ANSI one, we need to go via Unicode. */
2617 if (unicodebuf != NULL)
2618 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002619 if (enc_latin9)
2620 latin9_to_ucs(text, len, unicodebuf);
2621 else
2622 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 MB_PRECOMPOSED,
2624 (char *)text, len,
2625 (LPWSTR)unicodebuf, unibuflen);
2626 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002627 {
2628 /* Use unicodepdy to make characters fit as we expect, even
2629 * when the font uses different widths (e.g., bold character
2630 * is wider). */
2631 if (unicodepdy != NULL)
2632 {
2633 int i;
2634 int cw;
2635
2636 for (i = 0; i < len; ++i)
2637 {
2638 cw = utf_char2cells(unicodebuf[i]);
2639 if (cw > 2)
2640 cw = 1;
2641 unicodepdy[i] = cw * gui.char_width;
2642 }
2643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002645 foptions, pcliprect, unicodebuf, len, unicodepdy);
2646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 }
2648 }
2649 else
2650#endif
2651 {
2652#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02002653 /* Windows will mess up RL text, so we have to draw it character by
2654 * character. Only do this if RL is on, since it's slow. */
2655 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
2657 foptions, pcliprect, (char *)text, len, padding);
2658 else
2659#endif
2660 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
2661 foptions, pcliprect, (char *)text, len, padding);
2662 }
2663
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002664 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 if (flags & DRAW_UNDERL)
2666 {
2667 hpen = CreatePen(PS_SOLID, 1, gui.currFgColor);
2668 old_pen = SelectObject(s_hdc, hpen);
2669 /* When p_linespace is 0, overwrite the bottom row of pixels.
2670 * Otherwise put the line just below the character. */
2671 y = FILL_Y(row + 1) - 1;
2672#ifndef MSWIN16_FASTTEXT
2673 if (p_linespace > 1)
2674 y -= p_linespace - 1;
2675#endif
2676 MoveToEx(s_hdc, FILL_X(col), y, NULL);
2677 /* Note: LineTo() excludes the last pixel in the line. */
2678 LineTo(s_hdc, FILL_X(col + len), y);
2679 DeleteObject(SelectObject(s_hdc, old_pen));
2680 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002681
2682 /* Undercurl */
2683 if (flags & DRAW_UNDERC)
2684 {
2685 int x;
2686 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002687 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002688
2689 y = FILL_Y(row + 1) - 1;
2690 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
2691 {
2692 offset = val[x % 8];
2693 SetPixel(s_hdc, x, y - offset, gui.currSpColor);
2694 }
2695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696}
2697
2698
2699/*
2700 * Output routines.
2701 */
2702
2703/* Flush any output to the screen */
2704 void
2705gui_mch_flush(void)
2706{
2707# if defined(__BORLANDC__)
2708 /*
2709 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
2710 * prototype declaration.
2711 * The compiler complains if __stdcall is not used in both declarations.
2712 */
2713 BOOL __stdcall GdiFlush(void);
2714# endif
2715
2716 GdiFlush();
2717}
2718
2719 static void
2720clear_rect(RECT *rcp)
2721{
2722 HBRUSH hbr;
2723
2724 hbr = CreateSolidBrush(gui.back_pixel);
2725 FillRect(s_hdc, rcp, hbr);
2726 DeleteBrush(hbr);
2727}
2728
2729
Bram Moolenaarc716c302006-01-21 22:12:51 +00002730 void
2731gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
2732{
2733 RECT workarea_rect;
2734
2735 get_work_area(&workarea_rect);
2736
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002737 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02002738 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002739 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00002740
2741 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
2742 * the menubar for MSwin, we subtract it from the screen height, so that
2743 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002744 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02002745 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002746 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00002747 - GetSystemMetrics(SM_CYCAPTION)
2748#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002749 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00002750#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002751 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00002752}
2753
2754
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755#if defined(FEAT_MENU) || defined(PROTO)
2756/*
2757 * Add a sub menu to the menu bar.
2758 */
2759 void
2760gui_mch_add_menu(
2761 vimmenu_T *menu,
2762 int pos)
2763{
2764 vimmenu_T *parent = menu->parent;
2765
2766 menu->submenu_id = CreatePopupMenu();
2767 menu->id = s_menu_id++;
2768
2769 if (menu_is_menubar(menu->name))
2770 {
2771 if (is_winnt_3())
2772 {
2773 InsertMenu((parent == NULL) ? s_menuBar : parent->submenu_id,
2774 (UINT)pos, MF_POPUP | MF_STRING | MF_BYPOSITION,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002775 (long_u)menu->submenu_id, (LPCTSTR) menu->name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 }
2777 else
2778 {
2779#ifdef FEAT_MBYTE
2780 WCHAR *wn = NULL;
2781 int n;
2782
2783 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2784 {
2785 /* 'encoding' differs from active codepage: convert menu name
2786 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002787 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 if (wn != NULL)
2789 {
2790 MENUITEMINFOW infow;
2791
2792 infow.cbSize = sizeof(infow);
2793 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
2794 | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002795 infow.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 infow.wID = menu->id;
2797 infow.fType = MFT_STRING;
2798 infow.dwTypeData = wn;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002799 infow.cch = (UINT)wcslen(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 infow.hSubMenu = menu->submenu_id;
2801 n = InsertMenuItemW((parent == NULL)
2802 ? s_menuBar : parent->submenu_id,
2803 (UINT)pos, TRUE, &infow);
2804 vim_free(wn);
2805 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2806 /* Failed, try using non-wide function. */
2807 wn = NULL;
2808 }
2809 }
2810
2811 if (wn == NULL)
2812#endif
2813 {
2814 MENUITEMINFO info;
2815
2816 info.cbSize = sizeof(info);
2817 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002818 info.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 info.wID = menu->id;
2820 info.fType = MFT_STRING;
2821 info.dwTypeData = (LPTSTR)menu->name;
2822 info.cch = (UINT)STRLEN(menu->name);
2823 info.hSubMenu = menu->submenu_id;
2824 InsertMenuItem((parent == NULL)
2825 ? s_menuBar : parent->submenu_id,
2826 (UINT)pos, TRUE, &info);
2827 }
2828 }
2829 }
2830
2831 /* Fix window size if menu may have wrapped */
2832 if (parent == NULL)
2833 gui_mswin_get_menu_height(!gui.starting);
2834#ifdef FEAT_TEAROFF
2835 else if (IsWindow(parent->tearoff_handle))
2836 rebuild_tearoff(parent);
2837#endif
2838}
2839
2840 void
2841gui_mch_show_popupmenu(vimmenu_T *menu)
2842{
2843 POINT mp;
2844
2845 (void)GetCursorPos((LPPOINT)&mp);
2846 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
2847}
2848
2849 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00002850gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851{
2852 vimmenu_T *menu = gui_find_menu(path_name);
2853
2854 if (menu != NULL)
2855 {
2856 POINT p;
2857
2858 /* Find the position of the current cursor */
2859 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00002860 if (mouse_pos)
2861 {
2862 int mx, my;
2863
2864 gui_mch_getmouse(&mx, &my);
2865 p.x += mx;
2866 p.y += my;
2867 }
2868 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 {
2870 p.x += TEXT_X(W_WINCOL(curwin) + curwin->w_wcol + 1);
2871 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
2872 }
2873 msg_scroll = FALSE;
2874 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
2875 }
2876}
2877
2878#if defined(FEAT_TEAROFF) || defined(PROTO)
2879/*
2880 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
2881 * create it as a pseudo-"tearoff menu".
2882 */
2883 void
2884gui_make_tearoff(char_u *path_name)
2885{
2886 vimmenu_T *menu = gui_find_menu(path_name);
2887
2888 /* Found the menu, so tear it off. */
2889 if (menu != NULL)
2890 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
2891}
2892#endif
2893
2894/*
2895 * Add a menu item to a menu
2896 */
2897 void
2898gui_mch_add_menu_item(
2899 vimmenu_T *menu,
2900 int idx)
2901{
2902 vimmenu_T *parent = menu->parent;
2903
2904 menu->id = s_menu_id++;
2905 menu->submenu_id = NULL;
2906
2907#ifdef FEAT_TEAROFF
2908 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
2909 {
2910 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
2911 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
2912 }
2913 else
2914#endif
2915#ifdef FEAT_TOOLBAR
2916 if (menu_is_toolbar(parent->name))
2917 {
2918 TBBUTTON newtb;
2919
2920 vim_memset(&newtb, 0, sizeof(newtb));
2921 if (menu_is_separator(menu->name))
2922 {
2923 newtb.iBitmap = 0;
2924 newtb.fsStyle = TBSTYLE_SEP;
2925 }
2926 else
2927 {
2928 newtb.iBitmap = get_toolbar_bitmap(menu);
2929 newtb.fsStyle = TBSTYLE_BUTTON;
2930 }
2931 newtb.idCommand = menu->id;
2932 newtb.fsState = TBSTATE_ENABLED;
2933 newtb.iString = 0;
2934 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
2935 (LPARAM)&newtb);
2936 menu->submenu_id = (HMENU)-1;
2937 }
2938 else
2939#endif
2940 {
2941#ifdef FEAT_MBYTE
2942 WCHAR *wn = NULL;
2943 int n;
2944
2945 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2946 {
2947 /* 'encoding' differs from active codepage: convert menu item name
2948 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002949 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 if (wn != NULL)
2951 {
2952 n = InsertMenuW(parent->submenu_id, (UINT)idx,
2953 (menu_is_separator(menu->name)
2954 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
2955 (UINT)menu->id, wn);
2956 vim_free(wn);
2957 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2958 /* Failed, try using non-wide function. */
2959 wn = NULL;
2960 }
2961 }
2962 if (wn == NULL)
2963#endif
2964 InsertMenu(parent->submenu_id, (UINT)idx,
2965 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
2966 | MF_BYPOSITION,
2967 (UINT)menu->id, (LPCTSTR)menu->name);
2968#ifdef FEAT_TEAROFF
2969 if (IsWindow(parent->tearoff_handle))
2970 rebuild_tearoff(parent);
2971#endif
2972 }
2973}
2974
2975/*
2976 * Destroy the machine specific menu widget.
2977 */
2978 void
2979gui_mch_destroy_menu(vimmenu_T *menu)
2980{
2981#ifdef FEAT_TOOLBAR
2982 /*
2983 * is this a toolbar button?
2984 */
2985 if (menu->submenu_id == (HMENU)-1)
2986 {
2987 int iButton;
2988
2989 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
2990 (WPARAM)menu->id, 0);
2991 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
2992 }
2993 else
2994#endif
2995 {
2996 if (menu->parent != NULL
2997 && menu_is_popup(menu->parent->dname)
2998 && menu->parent->submenu_id != NULL)
2999 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
3000 else
3001 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
3002 if (menu->submenu_id != NULL)
3003 DestroyMenu(menu->submenu_id);
3004#ifdef FEAT_TEAROFF
3005 if (IsWindow(menu->tearoff_handle))
3006 DestroyWindow(menu->tearoff_handle);
3007 if (menu->parent != NULL
3008 && menu->parent->children != NULL
3009 && IsWindow(menu->parent->tearoff_handle))
3010 {
3011 /* This menu must not show up when rebuilding the tearoff window. */
3012 menu->modes = 0;
3013 rebuild_tearoff(menu->parent);
3014 }
3015#endif
3016 }
3017}
3018
3019#ifdef FEAT_TEAROFF
3020 static void
3021rebuild_tearoff(vimmenu_T *menu)
3022{
3023 /*hackish*/
3024 char_u tbuf[128];
3025 RECT trect;
3026 RECT rct;
3027 RECT roct;
3028 int x, y;
3029
3030 HWND thwnd = menu->tearoff_handle;
3031
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003032 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 if (GetWindowRect(thwnd, &trect)
3034 && GetWindowRect(s_hwnd, &rct)
3035 && GetClientRect(s_hwnd, &roct))
3036 {
3037 x = trect.left - rct.left;
3038 y = (trect.top - rct.bottom + roct.bottom);
3039 }
3040 else
3041 {
3042 x = y = 0xffffL;
3043 }
3044 DestroyWindow(thwnd);
3045 if (menu->children != NULL)
3046 {
3047 gui_mch_tearoff(tbuf, menu, x, y);
3048 if (IsWindow(menu->tearoff_handle))
3049 (void) SetWindowPos(menu->tearoff_handle,
3050 NULL,
3051 (int)trect.left,
3052 (int)trect.top,
3053 0, 0,
3054 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
3055 }
3056}
3057#endif /* FEAT_TEAROFF */
3058
3059/*
3060 * Make a menu either grey or not grey.
3061 */
3062 void
3063gui_mch_menu_grey(
3064 vimmenu_T *menu,
3065 int grey)
3066{
3067#ifdef FEAT_TOOLBAR
3068 /*
3069 * is this a toolbar button?
3070 */
3071 if (menu->submenu_id == (HMENU)-1)
3072 {
3073 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
3074 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
3075 }
3076 else
3077#endif
3078 if (grey)
3079 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_GRAYED);
3080 else
3081 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
3082
3083#ifdef FEAT_TEAROFF
3084 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
3085 {
3086 WORD menuID;
3087 HWND menuHandle;
3088
3089 /*
3090 * A tearoff button has changed state.
3091 */
3092 if (menu->children == NULL)
3093 menuID = (WORD)(menu->id);
3094 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003095 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
3097 if (menuHandle)
3098 EnableWindow(menuHandle, !grey);
3099
3100 }
3101#endif
3102}
3103
3104#endif /* FEAT_MENU */
3105
3106
3107/* define some macros used to make the dialogue creation more readable */
3108
3109#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
3110#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00003111#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112
3113#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
3114/*
3115 * stuff for dialogs
3116 */
3117
3118/*
3119 * The callback routine used by all the dialogs. Very simple. First,
3120 * acknowledges the INITDIALOG message so that Windows knows to do standard
3121 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
3122 * pressed, return that button's ID - IDCANCEL (2), which is the button's
3123 * number.
3124 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003125/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 static LRESULT CALLBACK
3127dialog_callback(
3128 HWND hwnd,
3129 UINT message,
3130 WPARAM wParam,
3131 LPARAM lParam)
3132{
3133 if (message == WM_INITDIALOG)
3134 {
3135 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
3136 /* Set focus to the dialog. Set the default button, if specified. */
3137 (void)SetFocus(hwnd);
3138 if (dialog_default_button > IDCANCEL)
3139 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00003140 else
3141 /* We don't have a default, set focus on another element of the
3142 * dialog window, probably the icon */
3143 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 return FALSE;
3145 }
3146
3147 if (message == WM_COMMAND)
3148 {
3149 int button = LOWORD(wParam);
3150
3151 /* Don't end the dialog if something was selected that was
3152 * not a button.
3153 */
3154 if (button >= DLG_NONBUTTON_CONTROL)
3155 return TRUE;
3156
3157 /* If the edit box exists, copy the string. */
3158 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00003159 {
3160# if defined(FEAT_MBYTE) && defined(WIN3264)
3161 /* If the OS is Windows NT, and 'encoding' differs from active
3162 * codepage: use wide function and convert text. */
3163 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
3164 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003165 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00003166 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
3167 char_u *p;
3168
3169 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
3170 p = utf16_to_enc(wp, NULL);
3171 vim_strncpy(s_textfield, p, IOSIZE);
3172 vim_free(p);
3173 vim_free(wp);
3174 }
3175 else
3176# endif
3177 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003178 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00003179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
3181 /*
3182 * Need to check for IDOK because if the user just hits Return to
3183 * accept the default value, some reason this is what we get.
3184 */
3185 if (button == IDOK)
3186 {
3187 if (dialog_default_button > IDCANCEL)
3188 EndDialog(hwnd, dialog_default_button);
3189 }
3190 else
3191 EndDialog(hwnd, button - IDCANCEL);
3192 return TRUE;
3193 }
3194
3195 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
3196 {
3197 EndDialog(hwnd, 0);
3198 return TRUE;
3199 }
3200 return FALSE;
3201}
3202
3203/*
3204 * Create a dialog dynamically from the parameter strings.
3205 * type = type of dialog (question, alert, etc.)
3206 * title = dialog title. may be NULL for default title.
3207 * message = text to display. Dialog sizes to accommodate it.
3208 * buttons = '\n' separated list of button captions, default first.
3209 * dfltbutton = number of default button.
3210 *
3211 * This routine returns 1 if the first button is pressed,
3212 * 2 for the second, etc.
3213 *
3214 * 0 indicates Esc was pressed.
3215 * -1 for unexpected error
3216 *
3217 * If stubbing out this fn, return 1.
3218 */
3219
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003220static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
3222 "IDR_VIM",
3223 "IDR_VIM_ERROR",
3224 "IDR_VIM_ALERT",
3225 "IDR_VIM_INFO",
3226 "IDR_VIM_QUESTION"
3227};
3228
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 int
3230gui_mch_dialog(
3231 int type,
3232 char_u *title,
3233 char_u *message,
3234 char_u *buttons,
3235 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003236 char_u *textfield,
3237 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238{
3239 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00003240 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 int numButtons;
3242 int *buttonWidths, *buttonPositions;
3243 int buttonYpos;
3244 int nchar, i;
3245 DWORD lStyle;
3246 int dlgwidth = 0;
3247 int dlgheight;
3248 int editboxheight;
3249 int horizWidth = 0;
3250 int msgheight;
3251 char_u *pstart;
3252 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003253 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 char_u *tbuffer;
3255 RECT rect;
3256 HWND hwnd;
3257 HDC hdc;
3258 HFONT font, oldFont;
3259 TEXTMETRIC fontInfo;
3260 int fontHeight;
3261 int textWidth, minButtonWidth, messageWidth;
3262 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003263 int maxDialogHeight;
3264 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 int vertical;
3266 int dlgPaddingX;
3267 int dlgPaddingY;
3268#ifdef USE_SYSMENU_FONT
3269 LOGFONT lfSysmenu;
3270 int use_lfSysmenu = FALSE;
3271#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003272 garray_T ga;
3273 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274
3275#ifndef NO_CONSOLE
3276 /* Don't output anything in silent mode ("ex -s") */
3277 if (silent_mode)
3278 return dfltbutton; /* return default option */
3279#endif
3280
Bram Moolenaar748bf032005-02-02 23:04:36 +00003281 if (s_hwnd == NULL)
3282 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283
3284 if ((type < 0) || (type > VIM_LAST_TYPE))
3285 type = 0;
3286
3287 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003288 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003289 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003290 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291
3292 if (p == NULL)
3293 return -1;
3294
3295 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003296 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 * vim_strsave() doesn't take a const arg (why not?), so cast away the
3298 * const.
3299 */
3300 tbuffer = vim_strsave(buttons);
3301 if (tbuffer == NULL)
3302 return -1;
3303
3304 --dfltbutton; /* Change from one-based to zero-based */
3305
3306 /* Count buttons */
3307 numButtons = 1;
3308 for (i = 0; tbuffer[i] != '\0'; i++)
3309 {
3310 if (tbuffer[i] == DLG_BUTTON_SEP)
3311 numButtons++;
3312 }
3313 if (dfltbutton >= numButtons)
3314 dfltbutton = -1;
3315
3316 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003317 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 if (buttonWidths == NULL)
3319 return -1;
3320
3321 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003322 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 if (buttonPositions == NULL)
3324 return -1;
3325
3326 /*
3327 * Calculate how big the dialog must be.
3328 */
3329 hwnd = GetDesktopWindow();
3330 hdc = GetWindowDC(hwnd);
3331#ifdef USE_SYSMENU_FONT
3332 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
3333 {
3334 font = CreateFontIndirect(&lfSysmenu);
3335 use_lfSysmenu = TRUE;
3336 }
3337 else
3338#endif
3339 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3340 VARIABLE_PITCH , DLG_FONT_NAME);
3341 if (s_usenewlook)
3342 {
3343 oldFont = SelectFont(hdc, font);
3344 dlgPaddingX = DLG_PADDING_X;
3345 dlgPaddingY = DLG_PADDING_Y;
3346 }
3347 else
3348 {
3349 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
3350 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
3351 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
3352 }
3353 GetTextMetrics(hdc, &fontInfo);
3354 fontHeight = fontInfo.tmHeight;
3355
3356 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003357 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
3359 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003360 if (s_hwnd == NULL)
3361 {
3362 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363
Bram Moolenaarc716c302006-01-21 22:12:51 +00003364 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003365 get_work_area(&workarea_rect);
3366 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
3367 if (maxDialogWidth > 600)
3368 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02003369 /* Leave some room for the taskbar. */
3370 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003371 }
3372 else
3373 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02003374 /* Use our own window for the size, unless it's very small. */
3375 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003376 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02003377 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02003378 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003379 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
3380 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003381
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003382 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02003383 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02003384 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02003385 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003386 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
3387 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
3388 }
3389
3390 /* Set dlgwidth to width of message.
3391 * Copy the message into "ga", changing NL to CR-NL and inserting line
3392 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 pstart = message;
3394 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003395 msgheight = 0;
3396 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 do
3398 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003399 msgheight += fontHeight; /* at least one line */
3400
3401 /* Need to figure out where to break the string. The system does it
3402 * at a word boundary, which would mean we can't compute the number of
3403 * wrapped lines. */
3404 textWidth = 0;
3405 last_white = NULL;
3406 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00003407 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003408#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003409 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003410#else
3411 l = 1;
3412#endif
3413 if (l == 1 && vim_iswhite(*pend)
3414 && textWidth > maxDialogWidth * 3 / 4)
3415 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02003416 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003417 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003418 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003419 /* Line will wrap. */
3420 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003421 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003422 textWidth = 0;
3423
3424 if (last_white != NULL)
3425 {
3426 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003427 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003428 pend = last_white + 1;
3429 last_white = NULL;
3430 }
3431 ga_append(&ga, '\r');
3432 ga_append(&ga, '\n');
3433 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003434 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003435
3436 while (--l >= 0)
3437 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003438 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003439 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003441
3442 ga_append(&ga, '\r');
3443 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 pstart = pend + 1;
3445 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003446
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003447 if (ga.ga_data != NULL)
3448 message = ga.ga_data;
3449
Bram Moolenaar748bf032005-02-02 23:04:36 +00003450 messageWidth += 10; /* roundoff space */
3451
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02003453 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
3454 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455
3456 if (msgheight < DLG_ICON_HEIGHT)
3457 msgheight = DLG_ICON_HEIGHT;
3458
3459 /*
3460 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00003461 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00003463 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 if (!vertical)
3465 {
3466 // Place buttons horizontally if they fit.
3467 horizWidth = dlgPaddingX;
3468 pstart = tbuffer;
3469 i = 0;
3470 do
3471 {
3472 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
3473 if (pend == NULL)
3474 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02003475 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 if (textWidth < minButtonWidth)
3477 textWidth = minButtonWidth;
3478 textWidth += dlgPaddingX; /* Padding within button */
3479 buttonWidths[i] = textWidth;
3480 buttonPositions[i++] = horizWidth;
3481 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
3482 pstart = pend + 1;
3483 } while (*pend != NUL);
3484
3485 if (horizWidth > maxDialogWidth)
3486 vertical = TRUE; // Too wide to fit on the screen.
3487 else if (horizWidth > dlgwidth)
3488 dlgwidth = horizWidth;
3489 }
3490
3491 if (vertical)
3492 {
3493 // Stack buttons vertically.
3494 pstart = tbuffer;
3495 do
3496 {
3497 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
3498 if (pend == NULL)
3499 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02003500 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 textWidth += dlgPaddingX; /* Padding within button */
3502 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
3503 if (textWidth > dlgwidth)
3504 dlgwidth = textWidth;
3505 pstart = pend + 1;
3506 } while (*pend != NUL);
3507 }
3508
3509 if (dlgwidth < DLG_MIN_WIDTH)
3510 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
3511
3512 /* start to fill in the dlgtemplate information. addressing by WORDs */
3513 if (s_usenewlook)
3514 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
3515 else
3516 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
3517
3518 add_long(lStyle);
3519 add_long(0); // (lExtendedStyle)
3520 pnumitems = p; /*save where the number of items must be stored*/
3521 add_word(0); // NumberOfItems(will change later)
3522 add_word(10); // x
3523 add_word(10); // y
3524 add_word(PixelToDialogX(dlgwidth)); // cx
3525
3526 // Dialog height.
3527 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02003528 dlgheight = msgheight + 2 * dlgPaddingY
3529 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 else
3531 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
3532
3533 // Dialog needs to be taller if contains an edit box.
3534 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
3535 if (textfield != NULL)
3536 dlgheight += editboxheight;
3537
Bram Moolenaara95d8232013-08-07 15:27:11 +02003538 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
3539 if (dlgheight > maxDialogHeight)
3540 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02003541 msgheight = msgheight - (dlgheight - maxDialogHeight);
3542 dlgheight = maxDialogHeight;
3543 scroll_flag = WS_VSCROLL;
3544 /* Make sure scrollbar doesn't appear in the middle of the dialog */
3545 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02003546 }
3547
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 add_word(PixelToDialogY(dlgheight));
3549
3550 add_word(0); // Menu
3551 add_word(0); // Class
3552
3553 /* copy the title of the dialog */
3554 nchar = nCopyAnsiToWideChar(p, (title ?
3555 (LPSTR)title :
3556 (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
3557 p += nchar;
3558
3559 if (s_usenewlook)
3560 {
3561 /* do the font, since DS_3DLOOK doesn't work properly */
3562#ifdef USE_SYSMENU_FONT
3563 if (use_lfSysmenu)
3564 {
3565 /* point size */
3566 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
3567 GetDeviceCaps(hdc, LOGPIXELSY));
3568 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
3569 }
3570 else
3571#endif
3572 {
3573 *p++ = DLG_FONT_POINT_SIZE; // point size
3574 nchar = nCopyAnsiToWideChar(p, TEXT(DLG_FONT_NAME));
3575 }
3576 p += nchar;
3577 }
3578
3579 buttonYpos = msgheight + 2 * dlgPaddingY;
3580
3581 if (textfield != NULL)
3582 buttonYpos += editboxheight;
3583
3584 pstart = tbuffer;
3585 if (!vertical)
3586 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
3587 for (i = 0; i < numButtons; i++)
3588 {
3589 /* get end of this button. */
3590 for ( pend = pstart;
3591 *pend && (*pend != DLG_BUTTON_SEP);
3592 pend++)
3593 ;
3594
3595 if (*pend)
3596 *pend = '\0';
3597
3598 /*
3599 * old NOTE:
3600 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
3601 * the focus to the first tab-able button and in so doing makes that
3602 * the default!! Grrr. Workaround: Make the default button the only
3603 * one with WS_TABSTOP style. Means user can't tab between buttons, but
3604 * he/she can use arrow keys.
3605 *
3606 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00003607 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 * dialog. Also needed for when the textfield is the default control.
3609 * It appears to work now (perhaps not on Win95?).
3610 */
3611 if (vertical)
3612 {
3613 p = add_dialog_element(p,
3614 (i == dfltbutton
3615 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
3616 PixelToDialogX(DLG_VERT_PADDING_X),
3617 PixelToDialogY(buttonYpos /* TBK */
3618 + 2 * fontHeight * i),
3619 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
3620 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003621 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 }
3623 else
3624 {
3625 p = add_dialog_element(p,
3626 (i == dfltbutton
3627 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
3628 PixelToDialogX(horizWidth + buttonPositions[i]),
3629 PixelToDialogY(buttonYpos), /* TBK */
3630 PixelToDialogX(buttonWidths[i]),
3631 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003632 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 }
3634 pstart = pend + 1; /*next button*/
3635 }
3636 *pnumitems += numButtons;
3637
3638 /* Vim icon */
3639 p = add_dialog_element(p, SS_ICON,
3640 PixelToDialogX(dlgPaddingX),
3641 PixelToDialogY(dlgPaddingY),
3642 PixelToDialogX(DLG_ICON_WIDTH),
3643 PixelToDialogY(DLG_ICON_HEIGHT),
3644 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
3645 dlg_icons[type]);
3646
Bram Moolenaar748bf032005-02-02 23:04:36 +00003647 /* Dialog message */
3648 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
3649 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
3650 PixelToDialogY(dlgPaddingY),
3651 (WORD)(PixelToDialogX(messageWidth) + 1),
3652 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003653 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
3655 /* Edit box */
3656 if (textfield != NULL)
3657 {
3658 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
3659 PixelToDialogX(2 * dlgPaddingX),
3660 PixelToDialogY(2 * dlgPaddingY + msgheight),
3661 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
3662 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003663 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 *pnumitems += 1;
3665 }
3666
3667 *pnumitems += 2;
3668
3669 SelectFont(hdc, oldFont);
3670 DeleteObject(font);
3671 ReleaseDC(hwnd, hdc);
3672
3673 /* Let the dialog_callback() function know which button to make default
3674 * If we have an edit box, make that the default. We also need to tell
3675 * dialog_callback() if this dialog contains an edit box or not. We do
3676 * this by setting s_textfield if it does.
3677 */
3678 if (textfield != NULL)
3679 {
3680 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
3681 s_textfield = textfield;
3682 }
3683 else
3684 {
3685 dialog_default_button = IDCANCEL + 1 + dfltbutton;
3686 s_textfield = NULL;
3687 }
3688
3689 /* show the dialog box modally and get a return value */
3690 nchar = (int)DialogBoxIndirect(
3691 s_hinst,
3692 (LPDLGTEMPLATE)pdlgtemplate,
3693 s_hwnd,
3694 (DLGPROC)dialog_callback);
3695
3696 LocalFree(LocalHandle(pdlgtemplate));
3697 vim_free(tbuffer);
3698 vim_free(buttonWidths);
3699 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003700 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701
3702 /* Focus back to our window (for when MDI is used). */
3703 (void)SetFocus(s_hwnd);
3704
3705 return nchar;
3706}
3707
3708#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003709
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710/*
3711 * Put a simple element (basic class) onto a dialog template in memory.
3712 * return a pointer to where the next item should be added.
3713 *
3714 * parameters:
3715 * lStyle = additional style flags
3716 * (be careful, NT3.51 & Win32s will ignore the new ones)
3717 * x,y = x & y positions IN DIALOG UNITS
3718 * w,h = width and height IN DIALOG UNITS
3719 * Id = ID used in messages
3720 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
3721 * caption = usually text or resource name
3722 *
3723 * TODO: use the length information noted here to enable the dialog creation
3724 * routines to work out more exactly how much memory they need to alloc.
3725 */
3726 static PWORD
3727add_dialog_element(
3728 PWORD p,
3729 DWORD lStyle,
3730 WORD x,
3731 WORD y,
3732 WORD w,
3733 WORD h,
3734 WORD Id,
3735 WORD clss,
3736 const char *caption)
3737{
3738 int nchar;
3739
3740 p = lpwAlign(p); /* Align to dword boundary*/
3741 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
3742 *p++ = LOWORD(lStyle);
3743 *p++ = HIWORD(lStyle);
3744 *p++ = 0; // LOWORD (lExtendedStyle)
3745 *p++ = 0; // HIWORD (lExtendedStyle)
3746 *p++ = x;
3747 *p++ = y;
3748 *p++ = w;
3749 *p++ = h;
3750 *p++ = Id; //9 or 10 words in all
3751
3752 *p++ = (WORD)0xffff;
3753 *p++ = clss; //2 more here
3754
3755 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption); //strlen(caption)+1
3756 p += nchar;
3757
3758 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
3759
3760 return p; //total = 15+ (strlen(caption)) words
3761 // = 30 + 2(strlen(caption) bytes reqd
3762}
3763
3764
3765/*
3766 * Helper routine. Take an input pointer, return closest pointer that is
3767 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
3768 */
3769 static LPWORD
3770lpwAlign(
3771 LPWORD lpIn)
3772{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003773 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003775 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 ul += 3;
3777 ul >>= 2;
3778 ul <<= 2;
3779 return (LPWORD)ul;
3780}
3781
3782/*
3783 * Helper routine. Takes second parameter as Ansi string, copies it to first
3784 * parameter as wide character (16-bits / char) string, and returns integer
3785 * number of wide characters (words) in string (including the trailing wide
3786 * char NULL). Partly taken from the Win32SDK samples.
3787 */
3788 static int
3789nCopyAnsiToWideChar(
3790 LPWORD lpWCStr,
3791 LPSTR lpAnsiIn)
3792{
3793 int nChar = 0;
3794#ifdef FEAT_MBYTE
3795 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
3796 int i;
3797 WCHAR *wn;
3798
3799 if (enc_codepage == 0 && (int)GetACP() != enc_codepage)
3800 {
3801 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003802 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 if (wn != NULL)
3804 {
3805 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003806 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 vim_free(wn);
3808 }
3809 }
3810 if (nChar == 0)
3811 /* Use Win32 conversion function. */
3812 nChar = MultiByteToWideChar(
3813 enc_codepage > 0 ? enc_codepage : CP_ACP,
3814 MB_PRECOMPOSED,
3815 lpAnsiIn, len,
3816 lpWCStr, len);
3817 for (i = 0; i < nChar; ++i)
3818 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
3819 lpWCStr[i] = (WORD)' ';
3820#else
3821 do
3822 {
3823 if (*lpAnsiIn == '\t')
3824 *lpWCStr++ = (WORD)' ';
3825 else
3826 *lpWCStr++ = (WORD)*lpAnsiIn;
3827 nChar++;
3828 } while (*lpAnsiIn++);
3829#endif
3830
3831 return nChar;
3832}
3833
3834
3835#ifdef FEAT_TEAROFF
3836/*
3837 * The callback function for all the modeless dialogs that make up the
3838 * "tearoff menus" Very simple - forward button presses (to fool Vim into
3839 * thinking its menus have been clicked), and go away when closed.
3840 */
3841 static LRESULT CALLBACK
3842tearoff_callback(
3843 HWND hwnd,
3844 UINT message,
3845 WPARAM wParam,
3846 LPARAM lParam)
3847{
3848 if (message == WM_INITDIALOG)
3849 return (TRUE);
3850
3851 /* May show the mouse pointer again. */
3852 HandleMouseHide(message, lParam);
3853
3854 if (message == WM_COMMAND)
3855 {
3856 if ((WORD)(LOWORD(wParam)) & 0x8000)
3857 {
3858 POINT mp;
3859 RECT rect;
3860
3861 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
3862 {
3863 (void)TrackPopupMenu(
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003864 (HMENU)(long_u)(LOWORD(wParam) ^ 0x8000),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 TPM_LEFTALIGN | TPM_LEFTBUTTON,
3866 (int)rect.right - 8,
3867 (int)mp.y,
3868 (int)0, /*reserved param*/
3869 s_hwnd,
3870 NULL);
3871 /*
3872 * NOTE: The pop-up menu can eat the mouse up event.
3873 * We deal with this in normal.c.
3874 */
3875 }
3876 }
3877 else
3878 /* Pass on messages to the main Vim window */
3879 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
3880 /*
3881 * Give main window the focus back: this is so after
3882 * choosing a tearoff button you can start typing again
3883 * straight away.
3884 */
3885 (void)SetFocus(s_hwnd);
3886 return TRUE;
3887 }
3888 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
3889 {
3890 DestroyWindow(hwnd);
3891 return TRUE;
3892 }
3893
3894 /* When moved around, give main window the focus back. */
3895 if (message == WM_EXITSIZEMOVE)
3896 (void)SetActiveWindow(s_hwnd);
3897
3898 return FALSE;
3899}
3900#endif
3901
3902
3903/*
3904 * Decide whether to use the "new look" (small, non-bold font) or the "old
3905 * look" (big, clanky font) for dialogs, and work out a few values for use
3906 * later accordingly.
3907 */
3908 static void
3909get_dialog_font_metrics(void)
3910{
3911 HDC hdc;
3912 HFONT hfontTools = 0;
3913 DWORD dlgFontSize;
3914 SIZE size;
3915#ifdef USE_SYSMENU_FONT
3916 LOGFONT lfSysmenu;
3917#endif
3918
3919 s_usenewlook = FALSE;
3920
3921 /*
3922 * For NT3.51 and Win32s, we stick with the old look
3923 * because it matches everything else.
3924 */
3925 if (!is_winnt_3())
3926 {
3927#ifdef USE_SYSMENU_FONT
3928 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
3929 hfontTools = CreateFontIndirect(&lfSysmenu);
3930 else
3931#endif
3932 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
3933 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
3934
3935 if (hfontTools)
3936 {
3937 hdc = GetDC(s_hwnd);
3938 SelectObject(hdc, hfontTools);
3939 /*
3940 * GetTextMetrics() doesn't return the right value in
3941 * tmAveCharWidth, so we have to figure out the dialog base units
3942 * ourselves.
3943 */
3944 GetTextExtentPoint(hdc,
3945 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
3946 52, &size);
3947 ReleaseDC(s_hwnd, hdc);
3948
3949 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
3950 s_dlgfntheight = (WORD)size.cy;
3951 s_usenewlook = TRUE;
3952 }
3953 }
3954
3955 if (!s_usenewlook)
3956 {
3957 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
3958 s_dlgfntwidth = LOWORD(dlgFontSize);
3959 s_dlgfntheight = HIWORD(dlgFontSize);
3960 }
3961}
3962
3963#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
3964/*
3965 * Create a pseudo-"tearoff menu" based on the child
3966 * items of a given menu pointer.
3967 */
3968 static void
3969gui_mch_tearoff(
3970 char_u *title,
3971 vimmenu_T *menu,
3972 int initX,
3973 int initY)
3974{
3975 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
3976 int template_len;
3977 int nchar, textWidth, submenuWidth;
3978 DWORD lStyle;
3979 DWORD lExtendedStyle;
3980 WORD dlgwidth;
3981 WORD menuID;
3982 vimmenu_T *pmenu;
3983 vimmenu_T *the_menu = menu;
3984 HWND hwnd;
3985 HDC hdc;
3986 HFONT font, oldFont;
3987 int col, spaceWidth, len;
3988 int columnWidths[2];
3989 char_u *label, *text;
3990 int acLen = 0;
3991 int nameLen;
3992 int padding0, padding1, padding2 = 0;
3993 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003994 int x;
3995 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996#ifdef USE_SYSMENU_FONT
3997 LOGFONT lfSysmenu;
3998 int use_lfSysmenu = FALSE;
3999#endif
4000
4001 /*
4002 * If this menu is already torn off, move it to the mouse position.
4003 */
4004 if (IsWindow(menu->tearoff_handle))
4005 {
4006 POINT mp;
4007 if (GetCursorPos((LPPOINT)&mp))
4008 {
4009 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
4010 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
4011 }
4012 return;
4013 }
4014
4015 /*
4016 * Create a new tearoff.
4017 */
4018 if (*title == MNU_HIDDEN_CHAR)
4019 title++;
4020
4021 /* Allocate memory to store the dialog template. It's made bigger when
4022 * needed. */
4023 template_len = DLG_ALLOC_SIZE;
4024 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
4025 if (p == NULL)
4026 return;
4027
4028 hwnd = GetDesktopWindow();
4029 hdc = GetWindowDC(hwnd);
4030#ifdef USE_SYSMENU_FONT
4031 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
4032 {
4033 font = CreateFontIndirect(&lfSysmenu);
4034 use_lfSysmenu = TRUE;
4035 }
4036 else
4037#endif
4038 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4039 VARIABLE_PITCH , DLG_FONT_NAME);
4040 if (s_usenewlook)
4041 oldFont = SelectFont(hdc, font);
4042 else
4043 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
4044
4045 /* Calculate width of a single space. Used for padding columns to the
4046 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004047 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048
4049 /* Figure out max width of the text column, the accelerator column and the
4050 * optional submenu column. */
4051 submenuWidth = 0;
4052 for (col = 0; col < 2; col++)
4053 {
4054 columnWidths[col] = 0;
4055 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
4056 {
4057 /* Use "dname" here to compute the width of the visible text. */
4058 text = (col == 0) ? pmenu->dname : pmenu->actext;
4059 if (text != NULL && *text != NUL)
4060 {
4061 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
4062 if (textWidth > columnWidths[col])
4063 columnWidths[col] = textWidth;
4064 }
4065 if (pmenu->children != NULL)
4066 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
4067 }
4068 }
4069 if (columnWidths[1] == 0)
4070 {
4071 /* no accelerators */
4072 if (submenuWidth != 0)
4073 columnWidths[0] += submenuWidth;
4074 else
4075 columnWidths[0] += spaceWidth;
4076 }
4077 else
4078 {
4079 /* there is an accelerator column */
4080 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
4081 columnWidths[1] += submenuWidth;
4082 }
4083
4084 /*
4085 * Now find the total width of our 'menu'.
4086 */
4087 textWidth = columnWidths[0] + columnWidths[1];
4088 if (submenuWidth != 0)
4089 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004090 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
4092 textWidth += submenuWidth;
4093 }
4094 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
4095 if (textWidth > dlgwidth)
4096 dlgwidth = textWidth;
4097 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
4098
4099 /* W95 can't do thin dialogs, they look v. weird! */
4100 if (mch_windows95() && dlgwidth < TEAROFF_MIN_WIDTH)
4101 dlgwidth = TEAROFF_MIN_WIDTH;
4102
4103 /* start to fill in the dlgtemplate information. addressing by WORDs */
4104 if (s_usenewlook)
4105 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
4106 else
4107 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
4108
4109 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
4110 *p++ = LOWORD(lStyle);
4111 *p++ = HIWORD(lStyle);
4112 *p++ = LOWORD(lExtendedStyle);
4113 *p++ = HIWORD(lExtendedStyle);
4114 pnumitems = p; /* save where the number of items must be stored */
4115 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004116 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004118 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 else
4120 *p++ = PixelToDialogX(initX); // x
4121 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004122 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 else
4124 *p++ = PixelToDialogY(initY); // y
4125 *p++ = PixelToDialogX(dlgwidth); // cx
4126 ptrueheight = p;
4127 *p++ = 0; // dialog height: changed later anyway
4128 *p++ = 0; // Menu
4129 *p++ = 0; // Class
4130
4131 /* copy the title of the dialog */
4132 nchar = nCopyAnsiToWideChar(p, ((*title)
4133 ? (LPSTR)title
4134 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
4135 p += nchar;
4136
4137 if (s_usenewlook)
4138 {
4139 /* do the font, since DS_3DLOOK doesn't work properly */
4140#ifdef USE_SYSMENU_FONT
4141 if (use_lfSysmenu)
4142 {
4143 /* point size */
4144 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
4145 GetDeviceCaps(hdc, LOGPIXELSY));
4146 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
4147 }
4148 else
4149#endif
4150 {
4151 *p++ = DLG_FONT_POINT_SIZE; // point size
4152 nchar = nCopyAnsiToWideChar (p, TEXT(DLG_FONT_NAME));
4153 }
4154 p += nchar;
4155 }
4156
4157 /*
4158 * Loop over all the items in the menu.
4159 * But skip over the tearbar.
4160 */
4161 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
4162 menu = menu->children->next;
4163 else
4164 menu = menu->children;
4165 for ( ; menu != NULL; menu = menu->next)
4166 {
4167 if (menu->modes == 0) /* this menu has just been deleted */
4168 continue;
4169 if (menu_is_separator(menu->dname))
4170 {
4171 sepPadding += 3;
4172 continue;
4173 }
4174
4175 /* Check if there still is plenty of room in the template. Make it
4176 * larger when needed. */
4177 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
4178 {
4179 WORD *newp;
4180
4181 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
4182 if (newp != NULL)
4183 {
4184 template_len += 4096;
4185 mch_memmove(newp, pdlgtemplate,
4186 (char *)p - (char *)pdlgtemplate);
4187 p = newp + (p - pdlgtemplate);
4188 pnumitems = newp + (pnumitems - pdlgtemplate);
4189 ptrueheight = newp + (ptrueheight - pdlgtemplate);
4190 LocalFree(LocalHandle(pdlgtemplate));
4191 pdlgtemplate = newp;
4192 }
4193 }
4194
4195 /* Figure out minimal length of this menu label. Use "name" for the
4196 * actual text, "dname" for estimating the displayed size. "name"
4197 * has "&a" for mnemonic and includes the accelerator. */
4198 len = nameLen = (int)STRLEN(menu->name);
4199 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
4200 (int)STRLEN(menu->dname))) / spaceWidth;
4201 len += padding0;
4202
4203 if (menu->actext != NULL)
4204 {
4205 acLen = (int)STRLEN(menu->actext);
4206 len += acLen;
4207 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
4208 }
4209 else
4210 textWidth = 0;
4211 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
4212 len += padding1;
4213
4214 if (menu->children == NULL)
4215 {
4216 padding2 = submenuWidth / spaceWidth;
4217 len += padding2;
4218 menuID = (WORD)(menu->id);
4219 }
4220 else
4221 {
4222 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004223 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 }
4225
4226 /* Allocate menu label and fill it in */
4227 text = label = alloc((unsigned)len + 1);
4228 if (label == NULL)
4229 break;
4230
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004231 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 text = vim_strchr(text, TAB); /* stop at TAB before actext */
4233 if (text == NULL)
4234 text = label + nameLen; /* no actext, use whole name */
4235 while (padding0-- > 0)
4236 *text++ = ' ';
4237 if (menu->actext != NULL)
4238 {
4239 STRNCPY(text, menu->actext, acLen);
4240 text += acLen;
4241 }
4242 while (padding1-- > 0)
4243 *text++ = ' ';
4244 if (menu->children != NULL)
4245 {
4246 STRCPY(text, TEAROFF_SUBMENU_LABEL);
4247 text += STRLEN(TEAROFF_SUBMENU_LABEL);
4248 }
4249 else
4250 {
4251 while (padding2-- > 0)
4252 *text++ = ' ';
4253 }
4254 *text = NUL;
4255
4256 /*
4257 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
4258 * W95/NT4 it makes the tear-off look more like a menu.
4259 */
4260 p = add_dialog_element(p,
4261 BS_PUSHBUTTON|BS_LEFT,
4262 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
4263 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
4264 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
4265 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004266 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 vim_free(label);
4268 (*pnumitems)++;
4269 }
4270
4271 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
4272
4273
4274 /* show modelessly */
4275 the_menu->tearoff_handle = CreateDialogIndirect(
4276 s_hinst,
4277 (LPDLGTEMPLATE)pdlgtemplate,
4278 s_hwnd,
4279 (DLGPROC)tearoff_callback);
4280
4281 LocalFree(LocalHandle(pdlgtemplate));
4282 SelectFont(hdc, oldFont);
4283 DeleteObject(font);
4284 ReleaseDC(hwnd, hdc);
4285
4286 /*
4287 * Reassert ourselves as the active window. This is so that after creating
4288 * a tearoff, the user doesn't have to click with the mouse just to start
4289 * typing again!
4290 */
4291 (void)SetActiveWindow(s_hwnd);
4292
4293 /* make sure the right buttons are enabled */
4294 force_menu_update = TRUE;
4295}
4296#endif
4297
4298#if defined(FEAT_TOOLBAR) || defined(PROTO)
4299#include "gui_w32_rc.h"
4300
4301/* This not defined in older SDKs */
4302# ifndef TBSTYLE_FLAT
4303# define TBSTYLE_FLAT 0x0800
4304# endif
4305
4306/*
4307 * Create the toolbar, initially unpopulated.
4308 * (just like the menu, there are no defaults, it's all
4309 * set up through menu.vim)
4310 */
4311 static void
4312initialise_toolbar(void)
4313{
4314 InitCommonControls();
4315 s_toolbarhwnd = CreateToolbarEx(
4316 s_hwnd,
4317 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
4318 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004319 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 s_hinst,
4321 IDR_TOOLBAR1, // id of initial bitmap
4322 NULL,
4323 0, // initial number of buttons
4324 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
4325 TOOLBAR_BUTTON_HEIGHT,
4326 TOOLBAR_BUTTON_WIDTH,
4327 TOOLBAR_BUTTON_HEIGHT,
4328 sizeof(TBBUTTON)
4329 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004330 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331
4332 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
4333}
4334
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004335 static LRESULT CALLBACK
4336toolbar_wndproc(
4337 HWND hwnd,
4338 UINT uMsg,
4339 WPARAM wParam,
4340 LPARAM lParam)
4341{
4342 HandleMouseHide(uMsg, lParam);
4343 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
4344}
4345
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 static int
4347get_toolbar_bitmap(vimmenu_T *menu)
4348{
4349 int i = -1;
4350
4351 /*
4352 * Check user bitmaps first, unless builtin is specified.
4353 */
4354 if (!is_winnt_3() && !menu->icon_builtin)
4355 {
4356 char_u fname[MAXPATHL];
4357 HANDLE hbitmap = NULL;
4358
4359 if (menu->iconfile != NULL)
4360 {
4361 gui_find_iconfile(menu->iconfile, fname, "bmp");
4362 hbitmap = LoadImage(
4363 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004364 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 IMAGE_BITMAP,
4366 TOOLBAR_BUTTON_WIDTH,
4367 TOOLBAR_BUTTON_HEIGHT,
4368 LR_LOADFROMFILE |
4369 LR_LOADMAP3DCOLORS
4370 );
4371 }
4372
4373 /*
4374 * If the LoadImage call failed, or the "icon=" file
4375 * didn't exist or wasn't specified, try the menu name
4376 */
4377 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02004378 && (gui_find_bitmap(
4379#ifdef FEAT_MULTI_LANG
4380 menu->en_dname != NULL ? menu->en_dname :
4381#endif
4382 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 hbitmap = LoadImage(
4384 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004385 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 IMAGE_BITMAP,
4387 TOOLBAR_BUTTON_WIDTH,
4388 TOOLBAR_BUTTON_HEIGHT,
4389 LR_LOADFROMFILE |
4390 LR_LOADMAP3DCOLORS
4391 );
4392
4393 if (hbitmap != NULL)
4394 {
4395 TBADDBITMAP tbAddBitmap;
4396
4397 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004398 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399
4400 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
4401 (WPARAM)1, (LPARAM)&tbAddBitmap);
4402 /* i will be set to -1 if it fails */
4403 }
4404 }
4405 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
4406 i = menu->iconidx;
4407
4408 return i;
4409}
4410#endif
4411
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004412#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
4413 static void
4414initialise_tabline(void)
4415{
4416 InitCommonControls();
4417
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004418 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004419 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004420 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4421 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004422 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004423
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004424 gui.tabline_height = TABLINE_HEIGHT;
4425
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004426# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004427 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004428# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004429}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004430
4431 static LRESULT CALLBACK
4432tabline_wndproc(
4433 HWND hwnd,
4434 UINT uMsg,
4435 WPARAM wParam,
4436 LPARAM lParam)
4437{
4438 HandleMouseHide(uMsg, lParam);
4439 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
4440}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004441#endif
4442
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
4444/*
4445 * Make the GUI window come to the foreground.
4446 */
4447 void
4448gui_mch_set_foreground(void)
4449{
4450 if (IsIconic(s_hwnd))
4451 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
4452 SetForegroundWindow(s_hwnd);
4453}
4454#endif
4455
4456#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4457 static void
4458dyn_imm_load(void)
4459{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02004460 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 if (hLibImm == NULL)
4462 return;
4463
4464 pImmGetCompositionStringA
4465 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
4466 pImmGetCompositionStringW
4467 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
4468 pImmGetContext
4469 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
4470 pImmAssociateContext
4471 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
4472 pImmReleaseContext
4473 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
4474 pImmGetOpenStatus
4475 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
4476 pImmSetOpenStatus
4477 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
4478 pImmGetCompositionFont
4479 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
4480 pImmSetCompositionFont
4481 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
4482 pImmSetCompositionWindow
4483 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
4484 pImmGetConversionStatus
4485 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00004486 pImmSetConversionStatus
4487 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488
4489 if ( pImmGetCompositionStringA == NULL
4490 || pImmGetCompositionStringW == NULL
4491 || pImmGetContext == NULL
4492 || pImmAssociateContext == NULL
4493 || pImmReleaseContext == NULL
4494 || pImmGetOpenStatus == NULL
4495 || pImmSetOpenStatus == NULL
4496 || pImmGetCompositionFont == NULL
4497 || pImmSetCompositionFont == NULL
4498 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00004499 || pImmGetConversionStatus == NULL
4500 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 {
4502 FreeLibrary(hLibImm);
4503 hLibImm = NULL;
4504 pImmGetContext = NULL;
4505 return;
4506 }
4507
4508 return;
4509}
4510
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511#endif
4512
4513#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
4514
4515# ifdef FEAT_XPM_W32
4516# define IMAGE_XPM 100
4517# endif
4518
4519typedef struct _signicon_t
4520{
4521 HANDLE hImage;
4522 UINT uType;
4523#ifdef FEAT_XPM_W32
4524 HANDLE hShape; /* Mask bitmap handle */
4525#endif
4526} signicon_t;
4527
4528 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004529gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530{
4531 signicon_t *sign;
4532 int x, y, w, h;
4533
4534 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
4535 return;
4536
4537 x = TEXT_X(col);
4538 y = TEXT_Y(row);
4539 w = gui.char_width * 2;
4540 h = gui.char_height;
4541 switch (sign->uType)
4542 {
4543 case IMAGE_BITMAP:
4544 {
4545 HDC hdcMem;
4546 HBITMAP hbmpOld;
4547
4548 hdcMem = CreateCompatibleDC(s_hdc);
4549 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
4550 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
4551 SelectObject(hdcMem, hbmpOld);
4552 DeleteDC(hdcMem);
4553 }
4554 break;
4555 case IMAGE_ICON:
4556 case IMAGE_CURSOR:
4557 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
4558 break;
4559#ifdef FEAT_XPM_W32
4560 case IMAGE_XPM:
4561 {
4562 HDC hdcMem;
4563 HBITMAP hbmpOld;
4564
4565 hdcMem = CreateCompatibleDC(s_hdc);
4566 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
4567 /* Make hole */
4568 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
4569
4570 SelectObject(hdcMem, sign->hImage);
4571 /* Paint sign */
4572 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
4573 SelectObject(hdcMem, hbmpOld);
4574 DeleteDC(hdcMem);
4575 }
4576 break;
4577#endif
4578 }
4579}
4580
4581 static void
4582close_signicon_image(signicon_t *sign)
4583{
4584 if (sign)
4585 switch (sign->uType)
4586 {
4587 case IMAGE_BITMAP:
4588 DeleteObject((HGDIOBJ)sign->hImage);
4589 break;
4590 case IMAGE_CURSOR:
4591 DestroyCursor((HCURSOR)sign->hImage);
4592 break;
4593 case IMAGE_ICON:
4594 DestroyIcon((HICON)sign->hImage);
4595 break;
4596#ifdef FEAT_XPM_W32
4597 case IMAGE_XPM:
4598 DeleteObject((HBITMAP)sign->hImage);
4599 DeleteObject((HBITMAP)sign->hShape);
4600 break;
4601#endif
4602 }
4603}
4604
4605 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004606gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607{
4608 signicon_t sign, *psign;
4609 char_u *ext;
4610
4611 if (is_winnt_3())
4612 {
4613 EMSG(_(e_signdata));
4614 return NULL;
4615 }
4616
4617 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004618 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 if (ext > signfile)
4620 {
4621 int do_load = 1;
4622
4623 if (!STRICMP(ext, ".bmp"))
4624 sign.uType = IMAGE_BITMAP;
4625 else if (!STRICMP(ext, ".ico"))
4626 sign.uType = IMAGE_ICON;
4627 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
4628 sign.uType = IMAGE_CURSOR;
4629 else
4630 do_load = 0;
4631
4632 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004633 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 gui.char_width * 2, gui.char_height,
4635 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
4636#ifdef FEAT_XPM_W32
4637 if (!STRICMP(ext, ".xpm"))
4638 {
4639 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004640 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
4641 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 }
4643#endif
4644 }
4645
4646 psign = NULL;
4647 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
4648 != NULL)
4649 *psign = sign;
4650
4651 if (!psign)
4652 {
4653 if (sign.hImage)
4654 close_signicon_image(&sign);
4655 EMSG(_(e_signdata));
4656 }
4657 return (void *)psign;
4658
4659}
4660
4661 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004662gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663{
4664 if (sign)
4665 {
4666 close_signicon_image((signicon_t *)sign);
4667 vim_free(sign);
4668 }
4669}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
4672#if defined(FEAT_BEVAL) || defined(PROTO)
4673
4674/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004675 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 *
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004677 * The only reused thing is gui_beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 * from gui_beval.c (note it uses x and y of the BalloonEval struct
4679 * to get current mouse position).
4680 *
4681 * Trying to use as more Windows services as possible, and as less
4682 * IE version as possible :)).
4683 *
4684 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
4685 * BalloonEval struct.
4686 * 2) Enable/Disable simply create/kill BalloonEval Timer
4687 * 3) When there was enough inactivity, timer procedure posts
4688 * async request to debugger
4689 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
4690 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00004691 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 */
4693
Bram Moolenaar45360022005-07-21 21:08:21 +00004694/*
4695 * determine whether installed Common Controls support multiline tooltips
4696 * (i.e. their version is >= 4.70
4697 */
4698 int
4699multiline_balloon_available(void)
4700{
4701 HINSTANCE hDll;
4702 static char comctl_dll[] = "comctl32.dll";
4703 static int multiline_tip = MAYBE;
4704
4705 if (multiline_tip != MAYBE)
4706 return multiline_tip;
4707
4708 hDll = GetModuleHandle(comctl_dll);
4709 if (hDll != NULL)
4710 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004711 DLLGETVERSIONPROC pGetVer;
4712 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00004713
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004714 if (pGetVer != NULL)
4715 {
4716 DLLVERSIONINFO dvi;
4717 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00004718
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004719 ZeroMemory(&dvi, sizeof(dvi));
4720 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00004721
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004722 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00004723
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004724 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00004725 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004726 || (dvi.dwMajorVersion == 4
4727 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00004728 {
4729 multiline_tip = TRUE;
4730 return multiline_tip;
4731 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004732 }
Bram Moolenaar45360022005-07-21 21:08:21 +00004733 else
4734 {
4735 /* there is chance we have ancient CommCtl 4.70
4736 which doesn't export DllGetVersion */
4737 DWORD dwHandle = 0;
4738 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
4739 if (len > 0)
4740 {
4741 VS_FIXEDFILEINFO *ver;
4742 UINT vlen = 0;
4743 void *data = alloc(len);
4744
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004745 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00004746 && GetFileVersionInfo(comctl_dll, 0, len, data)
4747 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
4748 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004749 && HIWORD(ver->dwFileVersionMS) > 4)
4750 || ((HIWORD(ver->dwFileVersionMS) == 4
4751 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00004752 {
4753 vim_free(data);
4754 multiline_tip = TRUE;
4755 return multiline_tip;
4756 }
4757 vim_free(data);
4758 }
4759 }
4760 }
4761 multiline_tip = FALSE;
4762 return multiline_tip;
4763}
4764
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004766make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767{
Bram Moolenaar45360022005-07-21 21:08:21 +00004768 TOOLINFO *pti;
4769 int ToolInfoSize;
4770
4771 if (multiline_balloon_available() == TRUE)
4772 ToolInfoSize = sizeof(TOOLINFO_NEW);
4773 else
4774 ToolInfoSize = sizeof(TOOLINFO);
4775
4776 pti = (TOOLINFO *)alloc(ToolInfoSize);
4777 if (pti == NULL)
4778 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779
4780 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
4781 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
4782 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4783 beval->target, NULL, s_hinst, NULL);
4784
4785 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
4786 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
4787
Bram Moolenaar45360022005-07-21 21:08:21 +00004788 pti->cbSize = ToolInfoSize;
4789 pti->uFlags = TTF_SUBCLASS;
4790 pti->hwnd = beval->target;
4791 pti->hinst = 0; /* Don't use string resources */
4792 pti->uId = ID_BEVAL_TOOLTIP;
4793
4794 if (multiline_balloon_available() == TRUE)
4795 {
4796 RECT rect;
4797 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
4798 pti->lpszText = LPSTR_TEXTCALLBACK;
4799 ptin->lParam = (LPARAM)text;
4800 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
4801 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
4802 (LPARAM)rect.right);
4803 }
4804 else
4805 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806
4807 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00004808 pti->rect.left = pt.x - 3;
4809 pti->rect.top = pt.y - 3;
4810 pti->rect.right = pt.x + 3;
4811 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
Bram Moolenaar45360022005-07-21 21:08:21 +00004813 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 /* Make tooltip appear sooner */
4815 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00004816 /* I've performed some tests and it seems the longest possible life time
4817 * of tooltip is 30 seconds */
4818 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 /*
4820 * HACK: force tooltip to appear, because it'll not appear until
4821 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00004822 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00004824 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00004826 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827}
4828
4829 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004830delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02004832 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833}
4834
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004835/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004837BevalTimerProc(
4838 HWND hwnd,
4839 UINT uMsg,
4840 UINT_PTR idEvent,
4841 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842{
4843 POINT pt;
4844 RECT rect;
4845
4846 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
4847 return;
4848
4849 GetCursorPos(&pt);
4850 if (WindowFromPoint(pt) != s_textArea)
4851 return;
4852
4853 ScreenToClient(s_textArea, &pt);
4854 GetClientRect(s_textArea, &rect);
4855 if (!PtInRect(&rect, pt))
4856 return;
4857
4858 if (LastActivity > 0
4859 && (dwTime - LastActivity) >= (DWORD)p_bdlay
4860 && (cur_beval->showState != ShS_PENDING
4861 || abs(cur_beval->x - pt.x) > 3
4862 || abs(cur_beval->y - pt.y) > 3))
4863 {
4864 /* Pointer resting in one place long enough, it's time to show
4865 * the tooltip. */
4866 cur_beval->showState = ShS_PENDING;
4867 cur_beval->x = pt.x;
4868 cur_beval->y = pt.y;
4869
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004870 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871
4872 if (cur_beval->msgCB != NULL)
4873 (*cur_beval->msgCB)(cur_beval, 0);
4874 }
4875}
4876
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004877/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004879gui_mch_disable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004881 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004883 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884}
4885
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004886/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004888gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004890 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 if (beval == NULL)
4892 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004893 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02004894 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004895 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896}
4897
4898 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004899gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900{
4901 POINT pt;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004902 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 if (beval->showState == ShS_SHOWING)
4904 return;
4905 GetCursorPos(&pt);
4906 ScreenToClient(s_textArea, &pt);
4907
4908 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
4909 /* cursor is still here */
4910 {
4911 gui_mch_disable_beval_area(cur_beval);
4912 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004913 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004915 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916}
4917
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004918/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004920gui_mch_create_beval_area(
4921 void *target, /* ignored, always use s_textArea */
4922 char_u *mesg,
4923 void (*mesgCB)(BalloonEval *, int),
4924 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925{
4926 /* partially stolen from gui_beval.c */
4927 BalloonEval *beval;
4928
4929 if (mesg != NULL && mesgCB != NULL)
4930 {
4931 EMSG(_("E232: Cannot create BalloonEval with both message and callback"));
4932 return NULL;
4933 }
4934
4935 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
4936 if (beval != NULL)
4937 {
4938 beval->target = s_textArea;
4939 beval->balloon = NULL;
4940
4941 beval->showState = ShS_NEUTRAL;
4942 beval->x = 0;
4943 beval->y = 0;
4944 beval->msg = mesg;
4945 beval->msgCB = mesgCB;
4946 beval->clientData = clientData;
4947
4948 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 cur_beval = beval;
4950
4951 if (p_beval)
4952 gui_mch_enable_beval_area(beval);
4953
4954 }
4955 return beval;
4956}
4957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004958/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 static void
Bram Moolenaar442b4222010-05-24 21:34:22 +02004960Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961{
4962 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
4963 return;
4964
4965 if (cur_beval != NULL)
4966 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004967 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004969 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004970 // TRACE0("TTN_SHOW {{{");
4971 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00004972 break;
4973 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004974 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 delete_tooltip(cur_beval);
4976 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004977 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978
4979 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00004980 break;
4981 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004982 {
4983 /* if you get there then we have new common controls */
4984 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
4985 info->lpszText = (LPSTR)info->lParam;
4986 info->uFlags |= TTF_DI_SETITEM;
4987 }
Bram Moolenaar45360022005-07-21 21:08:21 +00004988 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 }
4990 }
4991}
4992
4993 static void
4994TrackUserActivity(UINT uMsg)
4995{
4996 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
4997 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
4998 LastActivity = GetTickCount();
4999}
5000
5001 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005002gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003{
5004 vim_free(beval);
5005}
5006#endif /* FEAT_BEVAL */
5007
5008#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5009/*
5010 * We have multiple signs to draw at the same location. Draw the
5011 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
5012 */
5013 void
5014netbeans_draw_multisign_indicator(int row)
5015{
5016 int i;
5017 int y;
5018 int x;
5019
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005020 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005021 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005022
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 x = 0;
5024 y = TEXT_Y(row);
5025
5026 for (i = 0; i < gui.char_height - 3; i++)
5027 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
5028
5029 SetPixel(s_hdc, x+0, y, gui.currFgColor);
5030 SetPixel(s_hdc, x+2, y, gui.currFgColor);
5031 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
5032 SetPixel(s_hdc, x+1, y, gui.currFgColor);
5033 SetPixel(s_hdc, x+2, y, gui.currFgColor);
5034 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
5035 SetPixel(s_hdc, x+2, y, gui.currFgColor);
5036}
Bram Moolenaare0874f82016-01-24 20:36:41 +01005037#endif