blob: feeb6d402cb650c34e9c00029cd30b91d6957578 [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 Moolenaar065bbac2016-02-20 13:08:46 +010039#ifdef FEAT_MENU
40static int gui_mswin_get_menu_height(int fix_window);
41#endif
42
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010043#if defined(FEAT_DIRECTX) || defined(PROTO)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020044 int
45directx_enabled(void)
46{
47 if (s_dwc != NULL)
48 return 1;
49 else if (s_directx_load_attempted)
50 return 0;
51 /* load DirectX */
52 DWrite_Init();
53 s_directx_load_attempted = 1;
54 s_dwc = DWriteContext_Open();
55 return s_dwc != NULL ? 1 : 0;
56}
57#endif
58
59#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
60 int
61gui_mch_set_rendering_options(char_u *s)
62{
63#ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020064 char_u *p, *q;
65
66 int dx_enable = 0;
67 int dx_flags = 0;
68 float dx_gamma = 0.0f;
69 float dx_contrast = 0.0f;
70 float dx_level = 0.0f;
71 int dx_geom = 0;
72 int dx_renmode = 0;
73 int dx_taamode = 0;
74
75 /* parse string as rendering options. */
76 for (p = s; p != NULL && *p != NUL; )
77 {
78 char_u item[256];
79 char_u name[128];
80 char_u value[128];
81
Bram Moolenaarcde88542015-08-11 19:14:00 +020082 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020083 if (p == NULL)
84 break;
85 q = &item[0];
86 copy_option_part(&q, name, sizeof(name), ":");
87 if (q == NULL)
88 return FAIL;
89 copy_option_part(&q, value, sizeof(value), ":");
90
91 if (STRCMP(name, "type") == 0)
92 {
93 if (STRCMP(value, "directx") == 0)
94 dx_enable = 1;
95 else
96 return FAIL;
97 }
98 else if (STRCMP(name, "gamma") == 0)
99 {
100 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100101 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200102 }
103 else if (STRCMP(name, "contrast") == 0)
104 {
105 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100106 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200107 }
108 else if (STRCMP(name, "level") == 0)
109 {
110 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100111 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200112 }
113 else if (STRCMP(name, "geom") == 0)
114 {
115 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100116 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200117 if (dx_geom < 0 || dx_geom > 2)
118 return FAIL;
119 }
120 else if (STRCMP(name, "renmode") == 0)
121 {
122 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100123 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200124 if (dx_renmode < 0 || dx_renmode > 6)
125 return FAIL;
126 }
127 else if (STRCMP(name, "taamode") == 0)
128 {
129 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100130 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200131 if (dx_taamode < 0 || dx_taamode > 3)
132 return FAIL;
133 }
134 else
135 return FAIL;
136 }
137
138 /* Enable DirectX/DirectWrite */
139 if (dx_enable)
140 {
141 if (!directx_enabled())
142 return FAIL;
143 DWriteContext_SetRenderingParams(s_dwc, NULL);
144 if (dx_flags)
145 {
146 DWriteRenderingParams param;
147 DWriteContext_GetRenderingParams(s_dwc, &param);
148 if (dx_flags & (1 << 0))
149 param.gamma = dx_gamma;
150 if (dx_flags & (1 << 1))
151 param.enhancedContrast = dx_contrast;
152 if (dx_flags & (1 << 2))
153 param.clearTypeLevel = dx_level;
154 if (dx_flags & (1 << 3))
155 param.pixelGeometry = dx_geom;
156 if (dx_flags & (1 << 4))
157 param.renderingMode = dx_renmode;
158 if (dx_flags & (1 << 5))
159 param.textAntialiasMode = dx_taamode;
160 DWriteContext_SetRenderingParams(s_dwc, &param);
161 }
162 }
163 s_directx_enabled = dx_enable;
164
165 return OK;
166#else
167 return FAIL;
168#endif
169}
170#endif
171
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172/*
173 * These are new in Windows ME/XP, only defined in recent compilers.
174 */
175#ifndef HANDLE_WM_XBUTTONUP
176# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
177 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
178#endif
179#ifndef HANDLE_WM_XBUTTONDOWN
180# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
181 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
182#endif
183#ifndef HANDLE_WM_XBUTTONDBLCLK
184# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
185 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
186#endif
187
188/*
189 * Include the common stuff for MS-Windows GUI.
190 */
191#include "gui_w48.c"
192
193#ifdef FEAT_XPM_W32
194# include "xpm_w32.h"
195#endif
196
197#ifdef PROTO
198# define WINAPI
199#endif
200
201#ifdef __MINGW32__
202/*
203 * Add a lot of missing defines.
204 * They are not always missing, we need the #ifndef's.
205 */
206# ifndef _cdecl
207# define _cdecl
208# endif
209# ifndef IsMinimized
210# define IsMinimized(hwnd) IsIconic(hwnd)
211# endif
212# ifndef IsMaximized
213# define IsMaximized(hwnd) IsZoomed(hwnd)
214# endif
215# ifndef SelectFont
216# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
217# endif
218# ifndef GetStockBrush
219# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
220# endif
221# ifndef DeleteBrush
222# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
223# endif
224
225# ifndef HANDLE_WM_RBUTTONDBLCLK
226# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
227 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
228# endif
229# ifndef HANDLE_WM_MBUTTONUP
230# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
231 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
232# endif
233# ifndef HANDLE_WM_MBUTTONDBLCLK
234# define HANDLE_WM_MBUTTONDBLCLK(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_LBUTTONDBLCLK
238# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
239 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
240# endif
241# ifndef HANDLE_WM_RBUTTONDOWN
242# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
243 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
244# endif
245# ifndef HANDLE_WM_MOUSEMOVE
246# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
247 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
248# endif
249# ifndef HANDLE_WM_RBUTTONUP
250# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
251 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
252# endif
253# ifndef HANDLE_WM_MBUTTONDOWN
254# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
255 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
256# endif
257# ifndef HANDLE_WM_LBUTTONUP
258# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
259 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
260# endif
261# ifndef HANDLE_WM_LBUTTONDOWN
262# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
263 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
264# endif
265# ifndef HANDLE_WM_SYSCHAR
266# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
267 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
268# endif
269# ifndef HANDLE_WM_ACTIVATEAPP
270# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
271 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
272# endif
273# ifndef HANDLE_WM_WINDOWPOSCHANGING
274# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
275 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
276# endif
277# ifndef HANDLE_WM_VSCROLL
278# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
279 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
280# endif
281# ifndef HANDLE_WM_SETFOCUS
282# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
283 ((fn)((hwnd), (HWND)(wParam)), 0L)
284# endif
285# ifndef HANDLE_WM_KILLFOCUS
286# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
287 ((fn)((hwnd), (HWND)(wParam)), 0L)
288# endif
289# ifndef HANDLE_WM_HSCROLL
290# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
291 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
292# endif
293# ifndef HANDLE_WM_DROPFILES
294# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
295 ((fn)((hwnd), (HDROP)(wParam)), 0L)
296# endif
297# ifndef HANDLE_WM_CHAR
298# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
299 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
300# endif
301# ifndef HANDLE_WM_SYSDEADCHAR
302# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
303 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
304# endif
305# ifndef HANDLE_WM_DEADCHAR
306# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
307 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
308# endif
309#endif /* __MINGW32__ */
310
311
312/* Some parameters for tearoff menus. All in pixels. */
313#define TEAROFF_PADDING_X 2
314#define TEAROFF_BUTTON_PAD_X 8
315#define TEAROFF_MIN_WIDTH 200
316#define TEAROFF_SUBMENU_LABEL ">>"
317#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
318
319
320/* For the Intellimouse: */
321#ifndef WM_MOUSEWHEEL
322#define WM_MOUSEWHEEL 0x20a
323#endif
324
325
326#ifdef FEAT_BEVAL
327# define ID_BEVAL_TOOLTIP 200
328# define BEVAL_TEXT_LEN MAXPATHL
329
Bram Moolenaar167632f2010-05-26 21:42:54 +0200330#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +0000331/* Work around old versions of basetsd.h which wrongly declares
332 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +0200333# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +0000334# define UINT_PTR UINT
335#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000336
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100337static void make_tooltip(BalloonEval *beval, char *text, POINT pt);
338static void delete_tooltip(BalloonEval *beval);
339static VOID CALLBACK BevalTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000340
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000342static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +0000344
Bram Moolenaar82881492012-11-20 16:53:39 +0100345
346/* cproto fails on missing include files */
347#ifndef PROTO
348
Bram Moolenaar45360022005-07-21 21:08:21 +0000349/*
350 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000351 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +0000352 */
Bram Moolenaar82881492012-11-20 16:53:39 +0100353# include <pshpack1.h>
354
355#endif
Bram Moolenaar45360022005-07-21 21:08:21 +0000356
357typedef struct _DllVersionInfo
358{
359 DWORD cbSize;
360 DWORD dwMajorVersion;
361 DWORD dwMinorVersion;
362 DWORD dwBuildNumber;
363 DWORD dwPlatformID;
364} DLLVERSIONINFO;
365
Bram Moolenaar82881492012-11-20 16:53:39 +0100366#ifndef PROTO
367# include <poppack.h>
368#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +0000369
Bram Moolenaar45360022005-07-21 21:08:21 +0000370typedef struct tagTOOLINFOA_NEW
371{
372 UINT cbSize;
373 UINT uFlags;
374 HWND hwnd;
Bram Moolenaar281daf62009-12-24 15:11:40 +0000375 UINT_PTR uId;
Bram Moolenaar45360022005-07-21 21:08:21 +0000376 RECT rect;
377 HINSTANCE hinst;
378 LPSTR lpszText;
379 LPARAM lParam;
380} TOOLINFO_NEW;
381
382typedef struct tagNMTTDISPINFO_NEW
383{
384 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +0000385 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +0000386 char szText[80];
387 HINSTANCE hinst;
388 UINT uFlags;
389 LPARAM lParam;
390} NMTTDISPINFO_NEW;
391
Bram Moolenaar45360022005-07-21 21:08:21 +0000392typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
393#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000394# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395#endif
396
Bram Moolenaar45360022005-07-21 21:08:21 +0000397#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000398# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +0000399#endif
400
401#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000402# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +0000403#endif
404
405#endif /* defined(FEAT_BEVAL) */
406
Bram Moolenaar2c7a7632007-05-10 18:19:11 +0000407#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
408/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
409 * it here if LPNMTTDISPINFO isn't defined.
410 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
411 * _MSC_VER. */
412# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
413typedef struct tagNMTTDISPINFOA {
414 NMHDR hdr;
415 LPSTR lpszText;
416 char szText[80];
417 HINSTANCE hinst;
418 UINT uFlags;
419 LPARAM lParam;
420} NMTTDISPINFOA, *LPNMTTDISPINFOA;
421# define LPNMTTDISPINFO LPNMTTDISPINFOA
422
423# ifdef FEAT_MBYTE
424typedef struct tagNMTTDISPINFOW {
425 NMHDR hdr;
426 LPWSTR lpszText;
427 WCHAR szText[80];
428 HINSTANCE hinst;
429 UINT uFlags;
430 LPARAM lParam;
431} NMTTDISPINFOW, *LPNMTTDISPINFOW;
432# endif
433# endif
434#endif
435
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000436#ifndef TTN_GETDISPINFOW
437# define TTN_GETDISPINFOW (TTN_FIRST - 10)
438#endif
439
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440/* Local variables: */
441
442#ifdef FEAT_MENU
443static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +0200444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445
446/*
447 * Use the system font for dialogs and tear-off menus. Remove this line to
448 * use DLG_FONT_NAME.
449 */
Bram Moolenaar786989b2010-10-27 12:15:33 +0200450#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451
452#define VIM_NAME "vim"
453#define VIM_CLASS "Vim"
454#define VIM_CLASSW L"Vim"
455
456/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
457 * thus there should be room for every dialog. For tearoffs it's made bigger
458 * when needed. */
459#define DLG_ALLOC_SIZE 16 * 1024
460
461/*
462 * stuff for dialogs, menus, tearoffs etc.
463 */
464static LRESULT APIENTRY dialog_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +0100465#ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466static LRESULT APIENTRY tearoff_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +0100467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468static PWORD
469add_dialog_element(
470 PWORD p,
471 DWORD lStyle,
472 WORD x,
473 WORD y,
474 WORD w,
475 WORD h,
476 WORD Id,
477 WORD clss,
478 const char *caption);
479static LPWORD lpwAlign(LPWORD);
480static int nCopyAnsiToWideChar(LPWORD, LPSTR);
Bram Moolenaar065bbac2016-02-20 13:08:46 +0100481#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +0100483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484static void get_dialog_font_metrics(void);
485
486static int dialog_default_button = -1;
487
488/* Intellimouse support */
489static int mouse_scroll_lines = 0;
490static UINT msh_msgmousewheel = 0;
491
492static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
493#ifdef FEAT_TOOLBAR
494static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +0200495static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496static int get_toolbar_bitmap(vimmenu_T *menu);
497#endif
498
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000499#ifdef FEAT_GUI_TABLINE
500static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +0200501static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000502#endif
503
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504#ifdef FEAT_MBYTE_IME
505static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
506static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
507#endif
508#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
509# ifdef NOIME
510typedef struct tagCOMPOSITIONFORM {
511 DWORD dwStyle;
512 POINT ptCurrentPos;
513 RECT rcArea;
514} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
515typedef HANDLE HIMC;
516# endif
517
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000518static HINSTANCE hLibImm = NULL;
519static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
520static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
521static HIMC (WINAPI *pImmGetContext)(HWND);
522static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
523static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
524static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
525static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
526static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
527static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
528static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
529static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +0000530static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531static void dyn_imm_load(void);
532#else
533# define pImmGetCompositionStringA ImmGetCompositionStringA
534# define pImmGetCompositionStringW ImmGetCompositionStringW
535# define pImmGetContext ImmGetContext
536# define pImmAssociateContext ImmAssociateContext
537# define pImmReleaseContext ImmReleaseContext
538# define pImmGetOpenStatus ImmGetOpenStatus
539# define pImmSetOpenStatus ImmSetOpenStatus
540# define pImmGetCompositionFont ImmGetCompositionFontA
541# define pImmSetCompositionFont ImmSetCompositionFontA
542# define pImmSetCompositionWindow ImmSetCompositionWindow
543# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +0000544# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545#endif
546
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547/* multi monitor support */
548typedef struct _MONITORINFOstruct
549{
550 DWORD cbSize;
551 RECT rcMonitor;
552 RECT rcWork;
553 DWORD dwFlags;
554} _MONITORINFO;
555
556typedef HANDLE _HMONITOR;
557typedef _HMONITOR (WINAPI *TMonitorFromWindow)(HWND, DWORD);
558typedef BOOL (WINAPI *TGetMonitorInfo)(_HMONITOR, _MONITORINFO *);
559
560static TMonitorFromWindow pMonitorFromWindow = NULL;
561static TGetMonitorInfo pGetMonitorInfo = NULL;
562static HANDLE user32_lib = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563/*
564 * Return TRUE when running under Windows NT 3.x or Win32s, both of which have
565 * less fancy GUI APIs.
566 */
567 static int
568is_winnt_3(void)
569{
570 return ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
571 && os_version.dwMajorVersion == 3)
572 || (os_version.dwPlatformId == VER_PLATFORM_WIN32s));
573}
574
575/*
576 * Return TRUE when running under Win32s.
577 */
578 int
579gui_is_win32s(void)
580{
581 return (os_version.dwPlatformId == VER_PLATFORM_WIN32s);
582}
583
584#ifdef FEAT_MENU
585/*
586 * Figure out how high the menu bar is at the moment.
587 */
588 static int
589gui_mswin_get_menu_height(
590 int fix_window) /* If TRUE, resize window if menu height changed */
591{
592 static int old_menu_height = -1;
593
594 RECT rc1, rc2;
595 int num;
596 int menu_height;
597
598 if (gui.menu_is_active)
599 num = GetMenuItemCount(s_menuBar);
600 else
601 num = 0;
602
603 if (num == 0)
604 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +0100605 else if (IsMinimized(s_hwnd))
606 {
607 /* The height of the menu cannot be determined while the window is
608 * minimized. Take the previous height if the menu is changed in that
609 * state, to avoid that Vim's vertical window size accidentally
610 * increases due to the unaccounted-for menu height. */
611 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 else
614 {
615 if (is_winnt_3()) /* for NT 3.xx */
616 {
617 if (gui.starting)
618 menu_height = GetSystemMetrics(SM_CYMENU);
619 else
620 {
621 RECT r1, r2;
622 int frameht = GetSystemMetrics(SM_CYFRAME);
623 int capht = GetSystemMetrics(SM_CYCAPTION);
624
625 /* get window rect of s_hwnd
626 * get client rect of s_hwnd
627 * get cap height
628 * subtract from window rect, the sum of client height,
629 * (if not maximized)frame thickness, and caption height.
630 */
631 GetWindowRect(s_hwnd, &r1);
632 GetClientRect(s_hwnd, &r2);
633 menu_height = r1.bottom - r1.top - (r2.bottom - r2.top
634 + 2 * frameht * (!IsZoomed(s_hwnd)) + capht);
635 }
636 }
637 else /* win95 and variants (NT 4.0, I guess) */
638 {
639 /*
640 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
641 * seem to have been set yet, so menu wraps in default window
642 * width which is very narrow. Instead just return height of a
643 * single menu item. Will still be wrong when the menu really
644 * should wrap over more than one line.
645 */
646 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
647 if (gui.starting)
648 menu_height = rc1.bottom - rc1.top + 1;
649 else
650 {
651 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
652 menu_height = rc2.bottom - rc1.top + 1;
653 }
654 }
655 }
656
657 if (fix_window && menu_height != old_menu_height)
658 {
Bram Moolenaarafa24992006-03-27 20:58:26 +0000659 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 }
Bram Moolenaar71371b12015-03-24 17:57:12 +0100661 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662
663 return menu_height;
664}
665#endif /*FEAT_MENU*/
666
667
668/*
669 * Setup for the Intellimouse
670 */
671 static void
672init_mouse_wheel(void)
673{
674
675#ifndef SPI_GETWHEELSCROLLLINES
676# define SPI_GETWHEELSCROLLLINES 104
677#endif
Bram Moolenaare7566042005-06-17 22:00:15 +0000678#ifndef SPI_SETWHEELSCROLLLINES
679# define SPI_SETWHEELSCROLLLINES 105
680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
682#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
683#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
684#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
685#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
686
687 HWND hdl_mswheel;
688 UINT msh_msgscrolllines;
689
690 msh_msgmousewheel = 0;
691 mouse_scroll_lines = 3; /* reasonable default */
692
693 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
694 && os_version.dwMajorVersion >= 4)
695 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
696 && ((os_version.dwMajorVersion == 4
697 && os_version.dwMinorVersion >= 10)
698 || os_version.dwMajorVersion >= 5)))
699 {
700 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
701 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
702 &mouse_scroll_lines, 0);
703 }
704 else if (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
705 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
706 && os_version.dwMajorVersion < 4))
707 { /*
708 * If Win95 or NT 3.51,
709 * try to find the hidden point32 window.
710 */
711 hdl_mswheel = FindWindow(VMOUSEZ_CLASSNAME, VMOUSEZ_TITLE);
712 if (hdl_mswheel)
713 {
714 msh_msgscrolllines = RegisterWindowMessage(VMSH_SCROLL_LINES);
715 if (msh_msgscrolllines)
716 {
717 mouse_scroll_lines = (int)SendMessage(hdl_mswheel,
718 msh_msgscrolllines, 0, 0);
719 msh_msgmousewheel = RegisterWindowMessage(VMSH_MOUSEWHEEL);
720 }
721 }
722 }
723}
724
725
726/* Intellimouse wheel handler */
727 static void
728_OnMouseWheel(
729 HWND hwnd,
730 short zDelta)
731{
732/* Treat a mouse wheel event as if it were a scroll request */
733 int i;
734 int size;
735 HWND hwndCtl;
736
737 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
738 {
739 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
740 size = curwin->w_scrollbars[SBAR_RIGHT].size;
741 }
742 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
743 {
744 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
745 size = curwin->w_scrollbars[SBAR_LEFT].size;
746 }
747 else
748 return;
749
750 size = curwin->w_height;
751 if (mouse_scroll_lines == 0)
752 init_mouse_wheel();
753
754 if (mouse_scroll_lines > 0
755 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
756 {
757 for (i = mouse_scroll_lines; i > 0; --i)
758 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
759 }
760 else
761 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
762}
763
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000764#ifdef USE_SYSMENU_FONT
765/*
766 * Get Menu Font.
767 * Return OK or FAIL.
768 */
769 static int
770gui_w32_get_menu_font(LOGFONT *lf)
771{
772 NONCLIENTMETRICS nm;
773
774 nm.cbSize = sizeof(NONCLIENTMETRICS);
775 if (!SystemParametersInfo(
776 SPI_GETNONCLIENTMETRICS,
777 sizeof(NONCLIENTMETRICS),
778 &nm,
779 0))
780 return FAIL;
781 *lf = nm.lfMenuFont;
782 return OK;
783}
784#endif
785
786
787#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
788/*
789 * Set the GUI tabline font to the system menu font
790 */
791 static void
792set_tabline_font(void)
793{
794 LOGFONT lfSysmenu;
795 HFONT font;
796 HWND hwnd;
797 HDC hdc;
798 HFONT hfntOld;
799 TEXTMETRIC tm;
800
801 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
802 return;
803
804 font = CreateFontIndirect(&lfSysmenu);
805
806 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
807
808 /*
809 * Compute the height of the font used for the tab text
810 */
811 hwnd = GetDesktopWindow();
812 hdc = GetWindowDC(hwnd);
813 hfntOld = SelectFont(hdc, font);
814
815 GetTextMetrics(hdc, &tm);
816
817 SelectFont(hdc, hfntOld);
818 ReleaseDC(hwnd, hdc);
819
820 /*
821 * The space used by the tab border and the space between the tab label
822 * and the tab border is included as 7.
823 */
824 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
825}
826#endif
827
Bram Moolenaar520470a2005-06-16 21:59:56 +0000828/*
829 * Invoked when a setting was changed.
830 */
831 static LRESULT CALLBACK
832_OnSettingChange(UINT n)
833{
834 if (n == SPI_SETWHEELSCROLLLINES)
835 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
836 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000837#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
838 if (n == SPI_SETNONCLIENTMETRICS)
839 set_tabline_font();
840#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +0000841 return 0;
842}
843
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844#ifdef FEAT_NETBEANS_INTG
845 static void
846_OnWindowPosChanged(
847 HWND hwnd,
848 const LPWINDOWPOS lpwpos)
849{
850 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100851 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
853 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
854 || lpwpos->cx != cx || lpwpos->cy != cy))
855 {
856 x = lpwpos->x;
857 y = lpwpos->y;
858 cx = lpwpos->cx;
859 cy = lpwpos->cy;
860 netbeans_frame_moved(x, y);
861 }
862 /* Allow to send WM_SIZE and WM_MOVE */
863 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
864}
865#endif
866
867 static int
868_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 UINT fwSide,
870 LPRECT lprc)
871{
872 int w, h;
873 int valid_w, valid_h;
874 int w_offset, h_offset;
875
876 w = lprc->right - lprc->left;
877 h = lprc->bottom - lprc->top;
878 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
879 w_offset = w - valid_w;
880 h_offset = h - valid_h;
881
882 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
883 || fwSide == WMSZ_BOTTOMLEFT)
884 lprc->left += w_offset;
885 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
886 || fwSide == WMSZ_BOTTOMRIGHT)
887 lprc->right -= w_offset;
888
889 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
890 || fwSide == WMSZ_TOPRIGHT)
891 lprc->top += h_offset;
892 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
893 || fwSide == WMSZ_BOTTOMRIGHT)
894 lprc->bottom -= h_offset;
895 return TRUE;
896}
897
898
899
900 static LRESULT CALLBACK
901_WndProc(
902 HWND hwnd,
903 UINT uMsg,
904 WPARAM wParam,
905 LPARAM lParam)
906{
907 /*
908 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
909 hwnd, uMsg, wParam, lParam);
910 */
911
912 HandleMouseHide(uMsg, lParam);
913
914 s_uMsg = uMsg;
915 s_wParam = wParam;
916 s_lParam = lParam;
917
918 switch (uMsg)
919 {
920 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
921 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
922 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
923 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
924 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
925 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
926 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
927 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
928 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
929#ifdef FEAT_MENU
930 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
931#endif
932 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
933 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
934 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
935 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
936 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
937 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
938 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
939 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
940 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
941#ifdef FEAT_NETBEANS_INTG
942 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
943#endif
944
Bram Moolenaarafa24992006-03-27 20:58:26 +0000945#ifdef FEAT_GUI_TABLINE
946 case WM_RBUTTONUP:
947 {
948 if (gui_mch_showing_tabline())
949 {
950 POINT pt;
951 RECT rect;
952
953 /*
954 * If the cursor is on the tabline, display the tab menu
955 */
956 GetCursorPos((LPPOINT)&pt);
957 GetWindowRect(s_textArea, &rect);
958 if (pt.y < rect.top)
959 {
960 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +0100961 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +0000962 }
963 }
964 return MyWindowProc(hwnd, uMsg, wParam, lParam);
965 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000966 case WM_LBUTTONDBLCLK:
967 {
968 /*
969 * If the user double clicked the tabline, create a new tab
970 */
971 if (gui_mch_showing_tabline())
972 {
973 POINT pt;
974 RECT rect;
975
976 GetCursorPos((LPPOINT)&pt);
977 GetWindowRect(s_textArea, &rect);
978 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000979 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000980 }
981 return MyWindowProc(hwnd, uMsg, wParam, lParam);
982 }
Bram Moolenaarafa24992006-03-27 20:58:26 +0000983#endif
984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 case WM_QUERYENDSESSION: /* System wants to go down. */
986 gui_shell_closed(); /* Will exit when no changed buffers. */
987 return FALSE; /* Do NOT allow system to go down. */
988
989 case WM_ENDSESSION:
990 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +0100991 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +0100993 return 0L;
994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 break;
996
997 case WM_CHAR:
998 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
999 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001000 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 return 0L;
1002
1003 case WM_SYSCHAR:
1004 /*
1005 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
1006 * shortcut key, handle like a typed ALT key, otherwise call Windows
1007 * ALT key handling.
1008 */
1009#ifdef FEAT_MENU
1010 if ( !gui.menu_is_active
1011 || p_wak[0] == 'n'
1012 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
1013 )
1014#endif
1015 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001016 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 return 0L;
1018 }
1019#ifdef FEAT_MENU
1020 else
1021 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1022#endif
1023
1024 case WM_SYSKEYUP:
1025#ifdef FEAT_MENU
1026 /* This used to be done only when menu is active: ALT key is used for
1027 * that. But that caused problems when menu is disabled and using
1028 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
1029 * are received, mouse pointer remains hidden. */
1030 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1031#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01001032 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033#endif
1034
1035 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001036 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037
1038 case WM_MOUSEWHEEL:
1039 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01001040 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
Bram Moolenaar520470a2005-06-16 21:59:56 +00001042 /* Notification for change in SystemParametersInfo() */
1043 case WM_SETTINGCHANGE:
1044 return _OnSettingChange((UINT)wParam);
1045
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001046#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 case WM_NOTIFY:
1048 switch (((LPNMHDR) lParam)->code)
1049 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001050# ifdef FEAT_MBYTE
1051 case TTN_GETDISPINFOW:
1052# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001053 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001055 LPNMHDR hdr = (LPNMHDR)lParam;
1056 char_u *str = NULL;
1057 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001058
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001059 vim_free(tt_text);
1060 tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001061
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001062# ifdef FEAT_GUI_TABLINE
1063 if (gui_mch_showing_tabline()
1064 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001065 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001066 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001067 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001068 * Mouse is over the GUI tabline. Display the
1069 * tooltip for the tab under the cursor
1070 *
1071 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001072 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001073 GetCursorPos(&pt);
1074 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001075 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001076 TCHITTESTINFO htinfo;
1077 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001078
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001079 /*
1080 * Get the tab under the cursor
1081 */
1082 htinfo.pt.x = pt.x;
1083 htinfo.pt.y = pt.y;
1084 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
1085 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001086 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001087 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001088
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001089 tp = find_tabpage(idx + 1);
1090 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001091 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001092 get_tabline_label(tp, TRUE);
1093 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001094 }
1095 }
1096 }
1097 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001098# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001099# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001100# ifdef FEAT_GUI_TABLINE
1101 else
1102# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001104 UINT idButton;
1105 vimmenu_T *pMenu;
1106
1107 idButton = (UINT) hdr->idFrom;
1108 pMenu = gui_mswin_find_menu(root_menu, idButton);
1109 if (pMenu)
1110 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001112# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001113 if (str != NULL)
1114 {
1115# ifdef FEAT_MBYTE
1116 if (hdr->code == TTN_GETDISPINFOW)
1117 {
1118 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
1119
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00001120 /* Set the maximum width, this also enables using
1121 * \n for line break. */
1122 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
1123 0, 500);
1124
Bram Moolenaar36f692d2008-11-20 16:10:17 +00001125 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001126 lpdi->lpszText = tt_text;
1127 /* can't show tooltip if failed */
1128 }
1129 else
1130# endif
1131 {
1132 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
1133
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00001134 /* Set the maximum width, this also enables using
1135 * \n for line break. */
1136 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
1137 0, 500);
1138
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001139 if (STRLEN(str) < sizeof(lpdi->szText)
1140 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01001141 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001142 sizeof(lpdi->szText) - 1);
1143 else
1144 lpdi->lpszText = tt_text;
1145 }
1146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 }
1148 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001149# ifdef FEAT_GUI_TABLINE
1150 case TCN_SELCHANGE:
1151 if (gui_mch_showing_tabline()
1152 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01001153 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001154 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01001155 return 0L;
1156 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001157 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00001158
1159 case NM_RCLICK:
1160 if (gui_mch_showing_tabline()
1161 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01001162 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00001163 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01001164 return 0L;
1165 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00001166 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001167# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001169# ifdef FEAT_GUI_TABLINE
1170 if (gui_mch_showing_tabline()
1171 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
1172 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1173# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 break;
1175 }
1176 break;
1177#endif
1178#if defined(MENUHINTS) && defined(FEAT_MENU)
1179 case WM_MENUSELECT:
1180 if (((UINT) HIWORD(wParam)
1181 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
1182 == MF_HILITE
1183 && (State & CMDLINE) == 0)
1184 {
1185 UINT idButton;
1186 vimmenu_T *pMenu;
1187 static int did_menu_tip = FALSE;
1188
1189 if (did_menu_tip)
1190 {
1191 msg_clr_cmdline();
1192 setcursor();
1193 out_flush();
1194 did_menu_tip = FALSE;
1195 }
1196
1197 idButton = (UINT)LOWORD(wParam);
1198 pMenu = gui_mswin_find_menu(root_menu, idButton);
1199 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
1200 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
1201 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00001202 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00001204 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 setcursor();
1206 out_flush();
1207 did_menu_tip = TRUE;
1208 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01001209 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 }
1211 break;
1212#endif
1213 case WM_NCHITTEST:
1214 {
1215 LRESULT result;
1216 int x, y;
1217 int xPos = GET_X_LPARAM(lParam);
1218
1219 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
1220 if (result == HTCLIENT)
1221 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001222#ifdef FEAT_GUI_TABLINE
1223 if (gui_mch_showing_tabline())
1224 {
1225 int yPos = GET_Y_LPARAM(lParam);
1226 RECT rct;
1227
1228 /* If the cursor is on the GUI tabline, don't process this
1229 * event */
1230 GetWindowRect(s_textArea, &rct);
1231 if (yPos < rct.top)
1232 return result;
1233 }
1234#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02001235 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 xPos -= x;
1237
1238 if (xPos < 48) /* <VN> TODO should use system metric? */
1239 return HTBOTTOMLEFT;
1240 else
1241 return HTBOTTOMRIGHT;
1242 }
1243 else
1244 return result;
1245 }
1246 /* break; notreached */
1247
1248#ifdef FEAT_MBYTE_IME
1249 case WM_IME_NOTIFY:
1250 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
1251 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01001252 return 1L;
1253
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 case WM_IME_COMPOSITION:
1255 if (!_OnImeComposition(hwnd, wParam, lParam))
1256 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01001257 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258#endif
1259
1260 default:
1261 if (uMsg == msh_msgmousewheel && msh_msgmousewheel != 0)
1262 { /* handle MSH_MOUSEWHEEL messages for Intellimouse */
1263 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01001264 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 }
1266#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001267 else if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 {
1269 _OnFindRepl();
1270 }
1271#endif
1272 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1273 }
1274
Bram Moolenaar2787ab92011-12-14 15:23:59 +01001275 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276}
1277
1278/*
1279 * End of call-back routines
1280 */
1281
1282/* parent window, if specified with -P */
1283HWND vim_parent_hwnd = NULL;
1284
1285 static BOOL CALLBACK
1286FindWindowTitle(HWND hwnd, LPARAM lParam)
1287{
1288 char buf[2048];
1289 char *title = (char *)lParam;
1290
1291 if (GetWindowText(hwnd, buf, sizeof(buf)))
1292 {
1293 if (strstr(buf, title) != NULL)
1294 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001295 /* Found it. Store the window ref. and quit searching if MDI
1296 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001298 if (vim_parent_hwnd != NULL)
1299 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 }
1301 }
1302 return TRUE; /* continue searching */
1303}
1304
1305/*
1306 * Invoked for '-P "title"' argument: search for parent application to open
1307 * our window in.
1308 */
1309 void
1310gui_mch_set_parent(char *title)
1311{
1312 EnumWindows(FindWindowTitle, (LPARAM)title);
1313 if (vim_parent_hwnd == NULL)
1314 {
1315 EMSG2(_("E671: Cannot find window title \"%s\""), title);
1316 mch_exit(2);
1317 }
1318}
1319
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001320#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 static void
1322ole_error(char *arg)
1323{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00001324 char buf[IOSIZE];
1325
1326 /* Can't use EMSG() here, we have not finished initialisation yet. */
1327 vim_snprintf(buf, IOSIZE,
1328 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
1329 arg);
1330 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333
1334/*
1335 * Parse the GUI related command-line arguments. Any arguments used are
1336 * deleted from argv, and *argc is decremented accordingly. This is called
1337 * when vim is started, whether or not the GUI has been started.
1338 */
1339 void
1340gui_mch_prepare(int *argc, char **argv)
1341{
1342 int silent = FALSE;
1343 int idx;
1344
1345 /* Check for special OLE command line parameters */
1346 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
1347 {
1348 /* Check for a "-silent" argument first. */
1349 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
1350 && (argv[2][0] == '-' || argv[2][0] == '/'))
1351 {
1352 silent = TRUE;
1353 idx = 2;
1354 }
1355 else
1356 idx = 1;
1357
1358 /* Register Vim as an OLE Automation server */
1359 if (STRICMP(argv[idx] + 1, "register") == 0)
1360 {
1361#ifdef FEAT_OLE
1362 RegisterMe(silent);
1363 mch_exit(0);
1364#else
1365 if (!silent)
1366 ole_error("register");
1367 mch_exit(2);
1368#endif
1369 }
1370
1371 /* Unregister Vim as an OLE Automation server */
1372 if (STRICMP(argv[idx] + 1, "unregister") == 0)
1373 {
1374#ifdef FEAT_OLE
1375 UnregisterMe(!silent);
1376 mch_exit(0);
1377#else
1378 if (!silent)
1379 ole_error("unregister");
1380 mch_exit(2);
1381#endif
1382 }
1383
1384 /* Ignore an -embedding argument. It is only relevant if the
1385 * application wants to treat the case when it is started manually
1386 * differently from the case where it is started via automation (and
1387 * we don't).
1388 */
1389 if (STRICMP(argv[idx] + 1, "embedding") == 0)
1390 {
1391#ifdef FEAT_OLE
1392 *argc = 1;
1393#else
1394 ole_error("embedding");
1395 mch_exit(2);
1396#endif
1397 }
1398 }
1399
1400#ifdef FEAT_OLE
1401 {
1402 int bDoRestart = FALSE;
1403
1404 InitOLE(&bDoRestart);
1405 /* automatically exit after registering */
1406 if (bDoRestart)
1407 mch_exit(0);
1408 }
1409#endif
1410
1411#ifdef FEAT_NETBEANS_INTG
1412 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001413 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 int arg;
1415
1416 for (arg = 1; arg < *argc; arg++)
1417 if (strncmp("-nb", argv[arg], 3) == 0)
1418 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 netbeansArg = argv[arg];
1420 mch_memmove(&argv[arg], &argv[arg + 1],
1421 (--*argc - arg) * sizeof(char *));
1422 argv[*argc] = NULL;
1423 break; /* enough? */
1424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 }
1426#endif
1427
1428 /* get the OS version info */
1429 os_version.dwOSVersionInfoSize = sizeof(os_version);
1430 GetVersionEx(&os_version); /* this call works on Win32s, Win95 and WinNT */
1431
1432 /* try and load the user32.dll library and get the entry points for
1433 * multi-monitor-support. */
Bram Moolenaarebbcb822010-10-23 14:02:54 +02001434 if ((user32_lib = vimLoadLib("User32.dll")) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 {
1436 pMonitorFromWindow = (TMonitorFromWindow)GetProcAddress(user32_lib,
1437 "MonitorFromWindow");
1438
1439 /* there are ...A and ...W version of GetMonitorInfo - looking at
1440 * winuser.h, they have exactly the same declaration. */
1441 pGetMonitorInfo = (TGetMonitorInfo)GetProcAddress(user32_lib,
1442 "GetMonitorInfoA");
1443 }
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001444
1445#ifdef FEAT_MBYTE
1446 /* If the OS is Windows NT, use wide functions;
1447 * this enables common dialogs input unicode from IME. */
1448 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
1449 {
1450 pDispatchMessage = DispatchMessageW;
1451 pGetMessage = GetMessageW;
1452 pIsDialogMessage = IsDialogMessageW;
1453 pPeekMessage = PeekMessageW;
1454 }
1455 else
1456 {
1457 pDispatchMessage = DispatchMessageA;
1458 pGetMessage = GetMessageA;
1459 pIsDialogMessage = IsDialogMessageA;
1460 pPeekMessage = PeekMessageA;
1461 }
1462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463}
1464
1465/*
1466 * Initialise the GUI. Create all the windows, set up all the call-backs
1467 * etc.
1468 */
1469 int
1470gui_mch_init(void)
1471{
1472 const char szVimWndClass[] = VIM_CLASS;
1473 const char szTextAreaClass[] = "VimTextArea";
1474 WNDCLASS wndclass;
1475#ifdef FEAT_MBYTE
1476 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01001477 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 WNDCLASSW wndclassw;
1479#endif
1480#ifdef GLOBAL_IME
1481 ATOM atom;
1482#endif
1483
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 /* Return here if the window was already opened (happens when
1485 * gui_mch_dialog() is called early). */
1486 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00001487 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488
1489 /*
1490 * Load the tearoff bitmap
1491 */
1492#ifdef FEAT_TEAROFF
1493 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
1494#endif
1495
1496 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
1497 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
1498#ifdef FEAT_MENU
1499 gui.menu_height = 0; /* Windows takes care of this */
1500#endif
1501 gui.border_width = 0;
1502
1503 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
1504
1505#ifdef FEAT_MBYTE
1506 /* First try using the wide version, so that we can use any title.
1507 * Otherwise only characters in the active codepage will work. */
1508 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
1509 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001510 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 wndclassw.lpfnWndProc = _WndProc;
1512 wndclassw.cbClsExtra = 0;
1513 wndclassw.cbWndExtra = 0;
1514 wndclassw.hInstance = s_hinst;
1515 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
1516 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
1517 wndclassw.hbrBackground = s_brush;
1518 wndclassw.lpszMenuName = NULL;
1519 wndclassw.lpszClassName = szVimWndClassW;
1520
1521 if ((
1522#ifdef GLOBAL_IME
1523 atom =
1524#endif
1525 RegisterClassW(&wndclassw)) == 0)
1526 {
1527 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1528 return FAIL;
1529
1530 /* Must be Windows 98, fall back to non-wide function. */
1531 }
1532 else
1533 wide_WindowProc = TRUE;
1534 }
1535
1536 if (!wide_WindowProc)
1537#endif
1538
1539 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
1540 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001541 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 wndclass.lpfnWndProc = _WndProc;
1543 wndclass.cbClsExtra = 0;
1544 wndclass.cbWndExtra = 0;
1545 wndclass.hInstance = s_hinst;
1546 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
1547 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1548 wndclass.hbrBackground = s_brush;
1549 wndclass.lpszMenuName = NULL;
1550 wndclass.lpszClassName = szVimWndClass;
1551
1552 if ((
1553#ifdef GLOBAL_IME
1554 atom =
1555#endif
1556 RegisterClass(&wndclass)) == 0)
1557 return FAIL;
1558 }
1559
1560 if (vim_parent_hwnd != NULL)
1561 {
1562#ifdef HAVE_TRY_EXCEPT
1563 __try
1564 {
1565#endif
1566 /* Open inside the specified parent window.
1567 * TODO: last argument should point to a CLIENTCREATESTRUCT
1568 * structure. */
1569 s_hwnd = CreateWindowEx(
1570 WS_EX_MDICHILD,
1571 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02001572 WS_OVERLAPPEDWINDOW | WS_CHILD
1573 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
1575 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
1576 100, /* Any value will do */
1577 100, /* Any value will do */
1578 vim_parent_hwnd, NULL,
1579 s_hinst, NULL);
1580#ifdef HAVE_TRY_EXCEPT
1581 }
1582 __except(EXCEPTION_EXECUTE_HANDLER)
1583 {
1584 /* NOP */
1585 }
1586#endif
1587 if (s_hwnd == NULL)
1588 {
1589 EMSG(_("E672: Unable to open window inside MDI application"));
1590 mch_exit(2);
1591 }
1592 }
1593 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001594 {
1595 /* If the provided windowid is not valid reset it to zero, so that it
1596 * is ignored and we open our own window. */
1597 if (IsWindow((HWND)win_socket_id) <= 0)
1598 win_socket_id = 0;
1599
1600 /* Create a window. If win_socket_id is not zero without border and
1601 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00001603 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02001604 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
1605 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00001606 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
1607 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
1608 100, /* Any value will do */
1609 100, /* Any value will do */
1610 NULL, NULL,
1611 s_hinst, NULL);
1612 if (s_hwnd != NULL && win_socket_id != 0)
1613 {
1614 SetParent(s_hwnd, (HWND)win_socket_id);
1615 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
1616 }
1617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
1619 if (s_hwnd == NULL)
1620 return FAIL;
1621
1622#ifdef GLOBAL_IME
1623 global_ime_init(atom, s_hwnd);
1624#endif
1625#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
1626 dyn_imm_load();
1627#endif
1628
1629 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01001630#ifdef FEAT_MBYTE
1631 if (wide_WindowProc)
1632 {
1633 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
1634 {
1635 wndclassw.style = CS_OWNDC;
1636 wndclassw.lpfnWndProc = _TextAreaWndProc;
1637 wndclassw.cbClsExtra = 0;
1638 wndclassw.cbWndExtra = 0;
1639 wndclassw.hInstance = s_hinst;
1640 wndclassw.hIcon = NULL;
1641 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
1642 wndclassw.hbrBackground = NULL;
1643 wndclassw.lpszMenuName = NULL;
1644 wndclassw.lpszClassName = szTextAreaClassW;
1645
1646 if (RegisterClassW(&wndclassw) == 0)
1647 return FAIL;
1648 }
1649 }
1650 else
1651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
1653 {
1654 wndclass.style = CS_OWNDC;
1655 wndclass.lpfnWndProc = _TextAreaWndProc;
1656 wndclass.cbClsExtra = 0;
1657 wndclass.cbWndExtra = 0;
1658 wndclass.hInstance = s_hinst;
1659 wndclass.hIcon = NULL;
1660 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1661 wndclass.hbrBackground = NULL;
1662 wndclass.lpszMenuName = NULL;
1663 wndclass.lpszClassName = szTextAreaClass;
1664
1665 if (RegisterClass(&wndclass) == 0)
1666 return FAIL;
1667 }
1668 s_textArea = CreateWindowEx(
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01001669 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 szTextAreaClass, "Vim text area",
1671 WS_CHILD | WS_VISIBLE, 0, 0,
1672 100, /* Any value will do for now */
1673 100, /* Any value will do for now */
1674 s_hwnd, NULL,
1675 s_hinst, NULL);
1676
1677 if (s_textArea == NULL)
1678 return FAIL;
1679
Bram Moolenaar20321902016-02-17 12:30:17 +01001680#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02001681 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
1682 {
1683 HANDLE hIcon = NULL;
1684
1685 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02001686 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02001687 }
Bram Moolenaar20321902016-02-17 12:30:17 +01001688#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02001689
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690#ifdef FEAT_MENU
1691 s_menuBar = CreateMenu();
1692#endif
1693 s_hdc = GetDC(s_textArea);
1694
1695#ifdef MSWIN16_FASTTEXT
1696 SetBkMode(s_hdc, OPAQUE);
1697#endif
1698
1699#ifdef FEAT_WINDOWS
1700 DragAcceptFiles(s_hwnd, TRUE);
1701#endif
1702
1703 /* Do we need to bother with this? */
1704 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
1705
1706 /* Get background/foreground colors from the system */
1707 gui_mch_def_colors();
1708
1709 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
1710 * file) */
1711 set_normal_colors();
1712
1713 /*
1714 * Check that none of the colors are the same as the background color.
1715 * Then store the current values as the defaults.
1716 */
1717 gui_check_colors();
1718 gui.def_norm_pixel = gui.norm_pixel;
1719 gui.def_back_pixel = gui.back_pixel;
1720
1721 /* Get the colors for the highlight groups (gui_check_colors() might have
1722 * changed them) */
1723 highlight_gui_started();
1724
1725 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01001726 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01001728 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729
1730 /*
1731 * Set up for Intellimouse processing
1732 */
1733 init_mouse_wheel();
1734
1735 /*
1736 * compute a couple of metrics used for the dialogs
1737 */
1738 get_dialog_font_metrics();
1739#ifdef FEAT_TOOLBAR
1740 /*
1741 * Create the toolbar
1742 */
1743 initialise_toolbar();
1744#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001745#ifdef FEAT_GUI_TABLINE
1746 /*
1747 * Create the tabline
1748 */
1749 initialise_tabline();
1750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751#ifdef MSWIN_FIND_REPLACE
1752 /*
1753 * Initialise the dialog box stuff
1754 */
1755 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
1756
1757 /* Initialise the struct */
1758 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01001759 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01001761 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1763 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
1764 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00001765# if defined(FEAT_MBYTE) && defined(WIN3264)
1766 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
1767 s_findrep_struct_w.lpstrFindWhat =
1768 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
1769 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
1770 s_findrep_struct_w.lpstrReplaceWith =
1771 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
1772 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
1773 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
1774 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
1775# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02001778#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01001779# if !defined(_MSC_VER) || (_MSC_VER < 1400)
1780/* Define HandleToLong for old MS and non-MS compilers if not defined. */
1781# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01001782# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01001783# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02001784# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02001785 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02001786 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02001787#endif
1788
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02001789#ifdef FEAT_RENDER_OPTIONS
1790 if (p_rop)
1791 (void)gui_mch_set_rendering_options(p_rop);
1792#endif
1793
Bram Moolenaar748bf032005-02-02 23:04:36 +00001794theend:
1795 /* Display any pending error messages */
1796 display_errors();
1797
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 return OK;
1799}
1800
1801/*
1802 * Get the size of the screen, taking position on multiple monitors into
1803 * account (if supported).
1804 */
1805 static void
1806get_work_area(RECT *spi_rect)
1807{
1808 _HMONITOR mon;
1809 _MONITORINFO moninfo;
1810
1811 /* use these functions only if available */
1812 if (pMonitorFromWindow != NULL && pGetMonitorInfo != NULL)
1813 {
1814 /* work out which monitor the window is on, and get *it's* work area */
1815 mon = pMonitorFromWindow(s_hwnd, 1 /*MONITOR_DEFAULTTOPRIMARY*/);
1816 if (mon != NULL)
1817 {
1818 moninfo.cbSize = sizeof(_MONITORINFO);
1819 if (pGetMonitorInfo(mon, &moninfo))
1820 {
1821 *spi_rect = moninfo.rcWork;
1822 return;
1823 }
1824 }
1825 }
1826 /* this is the old method... */
1827 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
1828}
1829
1830/*
1831 * Set the size of the window to the given width and height in pixels.
1832 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001833/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 void
1835gui_mch_set_shellsize(int width, int height,
Bram Moolenaarafa24992006-03-27 20:58:26 +00001836 int min_width, int min_height, int base_width, int base_height,
1837 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838{
1839 RECT workarea_rect;
1840 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 WINDOWPLACEMENT wndpl;
1842
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001843 /* Try to keep window completely on screen. */
1844 /* Get position of the screen work area. This is the part that is not
1845 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 get_work_area(&workarea_rect);
1847
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001848 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001849 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 wndpl.length = sizeof(WINDOWPLACEMENT);
1851 GetWindowPlacement(s_hwnd, &wndpl);
1852
1853 /* Resizing a maximized window looks very strange, unzoom it first.
1854 * But don't do it when still starting up, it may have been requested in
1855 * the shortcut. */
1856 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
1857 {
1858 ShowWindow(s_hwnd, SW_SHOWNORMAL);
1859 /* Need to get the settings of the normal window. */
1860 GetWindowPlacement(s_hwnd, &wndpl);
1861 }
1862
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02001864 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02001865 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02001866 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02001867 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 + GetSystemMetrics(SM_CYCAPTION)
1869#ifdef FEAT_MENU
1870 + gui_mswin_get_menu_height(FALSE)
1871#endif
1872 ;
1873
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001874 /* The following should take care of keeping Vim on the same monitor, no
1875 * matter if the secondary monitor is left or right of the primary
1876 * monitor. */
1877 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
1878 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00001879
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001880 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001881 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001882 && wndpl.rcNormalPosition.right > workarea_rect.right)
1883 OffsetRect(&wndpl.rcNormalPosition,
1884 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001886 if ((direction & RESIZE_HOR)
1887 && wndpl.rcNormalPosition.left < workarea_rect.left)
1888 OffsetRect(&wndpl.rcNormalPosition,
1889 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890
Bram Moolenaarafa24992006-03-27 20:58:26 +00001891 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001892 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
1893 OffsetRect(&wndpl.rcNormalPosition,
1894 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001896 if ((direction & RESIZE_VERT)
1897 && wndpl.rcNormalPosition.top < workarea_rect.top)
1898 OffsetRect(&wndpl.rcNormalPosition,
1899 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900
1901 /* set window position - we should use SetWindowPlacement rather than
1902 * SetWindowPos as the MSDN docs say the coord systems returned by
1903 * these two are not compatible. */
1904 SetWindowPlacement(s_hwnd, &wndpl);
1905
1906 SetActiveWindow(s_hwnd);
1907 SetFocus(s_hwnd);
1908
1909#ifdef FEAT_MENU
1910 /* Menu may wrap differently now */
1911 gui_mswin_get_menu_height(!gui.starting);
1912#endif
1913}
1914
1915
1916 void
1917gui_mch_set_scrollbar_thumb(
1918 scrollbar_T *sb,
1919 long val,
1920 long size,
1921 long max)
1922{
1923 SCROLLINFO info;
1924
1925 sb->scroll_shift = 0;
1926 while (max > 32767)
1927 {
1928 max = (max + 1) >> 1;
1929 val >>= 1;
1930 size >>= 1;
1931 ++sb->scroll_shift;
1932 }
1933
1934 if (sb->scroll_shift > 0)
1935 ++size;
1936
1937 info.cbSize = sizeof(info);
1938 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
1939 info.nPos = val;
1940 info.nMin = 0;
1941 info.nMax = max;
1942 info.nPage = size;
1943 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
1944}
1945
1946
1947/*
1948 * Set the current text font.
1949 */
1950 void
1951gui_mch_set_font(GuiFont font)
1952{
1953 gui.currFont = font;
1954}
1955
1956
1957/*
1958 * Set the current text foreground color.
1959 */
1960 void
1961gui_mch_set_fg_color(guicolor_T color)
1962{
1963 gui.currFgColor = color;
1964}
1965
1966/*
1967 * Set the current text background color.
1968 */
1969 void
1970gui_mch_set_bg_color(guicolor_T color)
1971{
1972 gui.currBgColor = color;
1973}
1974
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001975/*
1976 * Set the current text special color.
1977 */
1978 void
1979gui_mch_set_sp_color(guicolor_T color)
1980{
1981 gui.currSpColor = color;
1982}
1983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984#if defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)
1985/*
1986 * Multi-byte handling, originally by Sung-Hoon Baek.
1987 * First static functions (no prototypes generated).
1988 */
1989#ifdef _MSC_VER
1990# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
1991#endif
1992#include <imm.h>
1993
1994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 * handle WM_IME_NOTIFY message
1996 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001997/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 static LRESULT
1999_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData)
2000{
2001 LRESULT lResult = 0;
2002 HIMC hImc;
2003
2004 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
2005 return lResult;
2006 switch (dwCommand)
2007 {
2008 case IMN_SETOPENSTATUS:
2009 if (pImmGetOpenStatus(hImc))
2010 {
2011 pImmSetCompositionFont(hImc, &norm_logfont);
2012 im_set_position(gui.row, gui.col);
2013
2014 /* Disable langmap */
2015 State &= ~LANGMAP;
2016 if (State & INSERT)
2017 {
2018#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
2019 /* Unshown 'keymap' in status lines */
2020 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2021 {
2022 /* Save cursor position */
2023 int old_row = gui.row;
2024 int old_col = gui.col;
2025
2026 // This must be called here before
2027 // status_redraw_curbuf(), otherwise the mode
2028 // message may appear in the wrong position.
2029 showmode();
2030 status_redraw_curbuf();
2031 update_screen(0);
2032 /* Restore cursor position */
2033 gui.row = old_row;
2034 gui.col = old_col;
2035 }
2036#endif
2037 }
2038 }
2039 gui_update_cursor(TRUE, FALSE);
2040 lResult = 0;
2041 break;
2042 }
2043 pImmReleaseContext(hWnd, hImc);
2044 return lResult;
2045}
2046
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002047/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 static LRESULT
2049_OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param)
2050{
2051 char_u *ret;
2052 int len;
2053
2054 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
2055 return 0;
2056
2057 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
2058 if (ret != NULL)
2059 {
2060 add_to_input_buf_csi(ret, len);
2061 vim_free(ret);
2062 return 1;
2063 }
2064 return 0;
2065}
2066
2067/*
2068 * get the current composition string, in UCS-2; *lenp is the number of
2069 * *lenp is the number of Unicode characters.
2070 */
2071 static short_u *
2072GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
2073{
2074 LONG ret;
2075 LPWSTR wbuf = NULL;
2076 char_u *buf;
2077
2078 if (!pImmGetContext)
2079 return NULL; /* no imm32.dll */
2080
2081 /* Try Unicode; this'll always work on NT regardless of codepage. */
2082 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
2083 if (ret == 0)
2084 return NULL; /* empty */
2085
2086 if (ret > 0)
2087 {
2088 /* Allocate the requested buffer plus space for the NUL character. */
2089 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
2090 if (wbuf != NULL)
2091 {
2092 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
2093 *lenp = ret / sizeof(WCHAR);
2094 }
2095 return (short_u *)wbuf;
2096 }
2097
2098 /* ret < 0; we got an error, so try the ANSI version. This'll work
2099 * on 9x/ME, but only if the codepage happens to be set to whatever
2100 * we're inputting. */
2101 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
2102 if (ret <= 0)
2103 return NULL; /* empty or error */
2104
2105 buf = alloc(ret);
2106 if (buf == NULL)
2107 return NULL;
2108 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
2109
2110 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01002111 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 vim_free(buf);
2113
2114 return (short_u *)wbuf;
2115}
2116
2117/*
2118 * void GetResultStr()
2119 *
2120 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
2121 * get complete composition string
2122 */
2123 static char_u *
2124GetResultStr(HWND hwnd, int GCS, int *lenp)
2125{
2126 HIMC hIMC; /* Input context handle. */
2127 short_u *buf = NULL;
2128 char_u *convbuf = NULL;
2129
2130 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
2131 return NULL;
2132
2133 /* Reads in the composition string. */
2134 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
2135 if (buf == NULL)
2136 return NULL;
2137
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002138 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 pImmReleaseContext(hwnd, hIMC);
2140 vim_free(buf);
2141 return convbuf;
2142}
2143#endif
2144
2145/* For global functions we need prototypes. */
2146#if (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) || defined(PROTO)
2147
2148/*
2149 * set font to IM.
2150 */
2151 void
2152im_set_font(LOGFONT *lf)
2153{
2154 HIMC hImc;
2155
2156 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
2157 {
2158 pImmSetCompositionFont(hImc, lf);
2159 pImmReleaseContext(s_hwnd, hImc);
2160 }
2161}
2162
2163/*
2164 * Notify cursor position to IM.
2165 */
2166 void
2167im_set_position(int row, int col)
2168{
2169 HIMC hImc;
2170
2171 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
2172 {
2173 COMPOSITIONFORM cfs;
2174
2175 cfs.dwStyle = CFS_POINT;
2176 cfs.ptCurrentPos.x = FILL_X(col);
2177 cfs.ptCurrentPos.y = FILL_Y(row);
2178 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
2179 pImmSetCompositionWindow(hImc, &cfs);
2180
2181 pImmReleaseContext(s_hwnd, hImc);
2182 }
2183}
2184
2185/*
2186 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
2187 */
2188 void
2189im_set_active(int active)
2190{
2191 HIMC hImc;
2192 static HIMC hImcOld = (HIMC)0;
2193
2194 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
2195 {
2196 if (p_imdisable)
2197 {
2198 if (hImcOld == (HIMC)0)
2199 {
2200 hImcOld = pImmGetContext(s_hwnd);
2201 if (hImcOld)
2202 pImmAssociateContext(s_hwnd, (HIMC)0);
2203 }
2204 active = FALSE;
2205 }
2206 else if (hImcOld != (HIMC)0)
2207 {
2208 pImmAssociateContext(s_hwnd, hImcOld);
2209 hImcOld = (HIMC)0;
2210 }
2211
2212 hImc = pImmGetContext(s_hwnd);
2213 if (hImc)
2214 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002215 /*
2216 * for Korean ime
2217 */
2218 HKL hKL = GetKeyboardLayout(0);
2219
2220 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
2221 {
2222 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
2223 static BOOL bSaved = FALSE;
2224
2225 if (active)
2226 {
2227 /* if we have a saved conversion status, restore it */
2228 if (bSaved)
2229 pImmSetConversionStatus(hImc, dwConversionSaved,
2230 dwSentenceSaved);
2231 bSaved = FALSE;
2232 }
2233 else
2234 {
2235 /* save conversion status and disable korean */
2236 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
2237 &dwSentenceSaved))
2238 {
2239 bSaved = TRUE;
2240 pImmSetConversionStatus(hImc,
2241 dwConversionSaved & ~(IME_CMODE_NATIVE
2242 | IME_CMODE_FULLSHAPE),
2243 dwSentenceSaved);
2244 }
2245 }
2246 }
2247
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 pImmSetOpenStatus(hImc, active);
2249 pImmReleaseContext(s_hwnd, hImc);
2250 }
2251 }
2252}
2253
2254/*
2255 * Get IM status. When IM is on, return not 0. Else return 0.
2256 */
2257 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002258im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259{
2260 int status = 0;
2261 HIMC hImc;
2262
2263 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
2264 {
2265 status = pImmGetOpenStatus(hImc) ? 1 : 0;
2266 pImmReleaseContext(s_hwnd, hImc);
2267 }
2268 return status;
2269}
2270
2271#endif /* FEAT_MBYTE && FEAT_MBYTE_IME */
2272
2273#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
2274/* Win32 with GLOBAL IME */
2275
2276/*
2277 * Notify cursor position to IM.
2278 */
2279 void
2280im_set_position(int row, int col)
2281{
2282 /* Win32 with GLOBAL IME */
2283 POINT p;
2284
2285 p.x = FILL_X(col);
2286 p.y = FILL_Y(row);
2287 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
2288 global_ime_set_position(&p);
2289}
2290
2291/*
2292 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
2293 */
2294 void
2295im_set_active(int active)
2296{
2297 global_ime_set_status(active);
2298}
2299
2300/*
2301 * Get IM status. When IM is on, return not 0. Else return 0.
2302 */
2303 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002304im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305{
2306 return global_ime_get_status();
2307}
2308#endif
2309
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002310#ifdef FEAT_MBYTE
2311/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00002312 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002313 */
2314 static void
2315latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
2316{
2317 int c;
2318
Bram Moolenaarca003e12006-03-17 23:19:38 +00002319 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002320 {
2321 c = *text++;
2322 switch (c)
2323 {
2324 case 0xa4: c = 0x20ac; break; /* euro */
2325 case 0xa6: c = 0x0160; break; /* S hat */
2326 case 0xa8: c = 0x0161; break; /* S -hat */
2327 case 0xb4: c = 0x017d; break; /* Z hat */
2328 case 0xb8: c = 0x017e; break; /* Z -hat */
2329 case 0xbc: c = 0x0152; break; /* OE */
2330 case 0xbd: c = 0x0153; break; /* oe */
2331 case 0xbe: c = 0x0178; break; /* Y */
2332 }
2333 *unicodebuf++ = c;
2334 }
2335}
2336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
2338#ifdef FEAT_RIGHTLEFT
2339/*
2340 * What is this for? In the case where you are using Win98 or Win2K or later,
2341 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
2342 * reverses the string sent to the TextOut... family. This sucks, because we
2343 * go to a lot of effort to do the right thing, and there doesn't seem to be a
2344 * way to tell Windblows not to do this!
2345 *
2346 * The short of it is that this 'RevOut' only gets called if you are running
2347 * one of the new, "improved" MS OSes, and only if you are running in
2348 * 'rightleft' mode. It makes display take *slightly* longer, but not
2349 * noticeably so.
2350 */
2351 static void
2352RevOut( HDC s_hdc,
2353 int col,
2354 int row,
2355 UINT foptions,
2356 CONST RECT *pcliprect,
2357 LPCTSTR text,
2358 UINT len,
2359 CONST INT *padding)
2360{
2361 int ix;
2362 static int special = -1;
2363
2364 if (special == -1)
2365 {
2366 /* Check windows version: special treatment is needed if it is NT 5 or
2367 * Win98 or higher. */
2368 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
2369 && os_version.dwMajorVersion >= 5)
2370 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
2371 && (os_version.dwMajorVersion > 4
2372 || (os_version.dwMajorVersion == 4
2373 && os_version.dwMinorVersion > 0))))
2374 special = 1;
2375 else
2376 special = 0;
2377 }
2378
2379 if (special)
2380 for (ix = 0; ix < (int)len; ++ix)
2381 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
2382 pcliprect, text + ix, 1, padding);
2383 else
2384 ExtTextOut(s_hdc, col, row, foptions, pcliprect, text, len, padding);
2385}
2386#endif
2387
2388 void
2389gui_mch_draw_string(
2390 int row,
2391 int col,
2392 char_u *text,
2393 int len,
2394 int flags)
2395{
2396 static int *padding = NULL;
2397 static int pad_size = 0;
2398 int i;
2399 const RECT *pcliprect = NULL;
2400 UINT foptions = 0;
2401#ifdef FEAT_MBYTE
2402 static WCHAR *unicodebuf = NULL;
2403 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002404 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 int n = 0;
2406#endif
2407 HPEN hpen, old_pen;
2408 int y;
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002409#ifdef FEAT_DIRECTX
2410 int font_is_ttf_or_vector = 0;
2411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
2413#ifndef MSWIN16_FASTTEXT
2414 /*
2415 * Italic and bold text seems to have an extra row of pixels at the bottom
2416 * (below where the bottom of the character should be). If we draw the
2417 * characters with a solid background, the top row of pixels in the
2418 * character below will be overwritten. We can fix this by filling in the
2419 * background ourselves, to the correct character proportions, and then
2420 * writing the character in transparent mode. Still have a problem when
2421 * the character is "_", which gets written on to the character below.
2422 * New fix: set gui.char_ascent to -1. This shifts all characters up one
2423 * pixel in their slots, which fixes the problem with the bottom row of
2424 * pixels. We still need this code because otherwise the top row of pixels
2425 * becomes a problem. - webb.
2426 */
2427 static HBRUSH hbr_cache[2] = {NULL, NULL};
2428 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
2429 static int brush_lru = 0;
2430 HBRUSH hbr;
2431 RECT rc;
2432
2433 if (!(flags & DRAW_TRANSP))
2434 {
2435 /*
2436 * Clear background first.
2437 * Note: FillRect() excludes right and bottom of rectangle.
2438 */
2439 rc.left = FILL_X(col);
2440 rc.top = FILL_Y(row);
2441#ifdef FEAT_MBYTE
2442 if (has_mbyte)
2443 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002445 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 }
2447 else
2448#endif
2449 rc.right = FILL_X(col + len);
2450 rc.bottom = FILL_Y(row + 1);
2451
2452 /* Cache the created brush, that saves a lot of time. We need two:
2453 * one for cursor background and one for the normal background. */
2454 if (gui.currBgColor == brush_color[0])
2455 {
2456 hbr = hbr_cache[0];
2457 brush_lru = 1;
2458 }
2459 else if (gui.currBgColor == brush_color[1])
2460 {
2461 hbr = hbr_cache[1];
2462 brush_lru = 0;
2463 }
2464 else
2465 {
2466 if (hbr_cache[brush_lru] != NULL)
2467 DeleteBrush(hbr_cache[brush_lru]);
2468 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
2469 brush_color[brush_lru] = gui.currBgColor;
2470 hbr = hbr_cache[brush_lru];
2471 brush_lru = !brush_lru;
2472 }
2473 FillRect(s_hdc, &rc, hbr);
2474
2475 SetBkMode(s_hdc, TRANSPARENT);
2476
2477 /*
2478 * When drawing block cursor, prevent inverted character spilling
2479 * over character cell (can happen with bold/italic)
2480 */
2481 if (flags & DRAW_CURSOR)
2482 {
2483 pcliprect = &rc;
2484 foptions = ETO_CLIPPED;
2485 }
2486 }
2487#else
2488 /*
2489 * The alternative would be to write the characters in opaque mode, but
2490 * when the text is not exactly the same proportions as normal text, too
2491 * big or too little a rectangle gets drawn for the background.
2492 */
2493 SetBkMode(s_hdc, OPAQUE);
2494 SetBkColor(s_hdc, gui.currBgColor);
2495#endif
2496 SetTextColor(s_hdc, gui.currFgColor);
2497 SelectFont(s_hdc, gui.currFont);
2498
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002499#ifdef FEAT_DIRECTX
2500 if (IS_ENABLE_DIRECTX())
2501 {
2502 TEXTMETRIC tm;
2503
2504 GetTextMetrics(s_hdc, &tm);
2505 if (tm.tmPitchAndFamily & (TMPF_TRUETYPE | TMPF_VECTOR))
2506 {
2507 font_is_ttf_or_vector = 1;
2508 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
2509 }
2510 }
2511#endif
2512
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
2514 {
2515 vim_free(padding);
2516 pad_size = Columns;
2517
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002518 /* Don't give an out-of-memory message here, it would call us
2519 * recursively. */
2520 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 if (padding != NULL)
2522 for (i = 0; i < pad_size; i++)
2523 padding[i] = gui.char_width;
2524 }
2525
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 /*
2527 * We have to provide the padding argument because italic and bold versions
2528 * of fixed-width fonts are often one pixel or so wider than their normal
2529 * versions.
2530 * No check for DRAW_BOLD, Windows will have done it already.
2531 */
2532
2533#ifdef FEAT_MBYTE
2534 /* Check if there are any UTF-8 characters. If not, use normal text
2535 * output to speed up output. */
2536 if (enc_utf8)
2537 for (n = 0; n < len; ++n)
2538 if (text[n] >= 0x80)
2539 break;
2540
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002541#if defined(FEAT_DIRECTX)
2542 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
2543 * required that unicode drawing routine, currently. So this forces it
2544 * enabled. */
2545 if (enc_utf8 && IS_ENABLE_DIRECTX())
2546 n = 0; /* Keep n < len, to enter block for unicode. */
2547#endif
2548
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002550 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002552 if ((enc_utf8
2553 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
2554 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 && (unicodebuf == NULL || len > unibuflen))
2556 {
2557 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002558 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559
2560 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002561 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562
2563 unibuflen = len;
2564 }
2565
2566 if (enc_utf8 && n < len && unicodebuf != NULL)
2567 {
2568 /* Output UTF-8 characters. Caller has already separated
2569 * composing characters. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002570 int i;
2571 int wlen; /* string length in words */
2572 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 int cells; /* cell width of string up to composing char */
2574 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002575 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002577 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002578 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002580 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002582 c = utf_ptr2char(text + i);
2583 if (c >= 0x10000)
2584 {
2585 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002586 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
2587 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002588 }
2589 else
2590 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002591 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002592 }
2593 cw = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 if (cw > 2) /* don't use 4 for unprintable char */
2595 cw = 1;
2596 if (unicodepdy != NULL)
2597 {
2598 /* Use unicodepdy to make characters fit as we expect, even
2599 * when the font uses different widths (e.g., bold character
2600 * is wider). */
2601 unicodepdy[clen] = cw * gui.char_width;
2602 }
2603 cells += cw;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002604 i += utfc_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00002605 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002607#if defined(FEAT_DIRECTX)
2608 if (IS_ENABLE_DIRECTX() && font_is_ttf_or_vector)
2609 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02002610 /* Add one to "cells" for italics. */
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002611 DWriteContext_DrawText(s_dwc, s_hdc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02002612 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002613 gui.char_width, gui.currFgColor);
2614 }
2615 else
2616#endif
2617 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
2618 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 len = cells; /* used for underlining */
2620 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002621 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 {
2623 /* If we want to display codepage data, and the current CP is not the
2624 * ANSI one, we need to go via Unicode. */
2625 if (unicodebuf != NULL)
2626 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002627 if (enc_latin9)
2628 latin9_to_ucs(text, len, unicodebuf);
2629 else
2630 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 MB_PRECOMPOSED,
2632 (char *)text, len,
2633 (LPWSTR)unicodebuf, unibuflen);
2634 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002635 {
2636 /* Use unicodepdy to make characters fit as we expect, even
2637 * when the font uses different widths (e.g., bold character
2638 * is wider). */
2639 if (unicodepdy != NULL)
2640 {
2641 int i;
2642 int cw;
2643
2644 for (i = 0; i < len; ++i)
2645 {
2646 cw = utf_char2cells(unicodebuf[i]);
2647 if (cw > 2)
2648 cw = 1;
2649 unicodepdy[i] = cw * gui.char_width;
2650 }
2651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002653 foptions, pcliprect, unicodebuf, len, unicodepdy);
2654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 }
2656 }
2657 else
2658#endif
2659 {
2660#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02002661 /* Windows will mess up RL text, so we have to draw it character by
2662 * character. Only do this if RL is on, since it's slow. */
2663 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
2665 foptions, pcliprect, (char *)text, len, padding);
2666 else
2667#endif
2668 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
2669 foptions, pcliprect, (char *)text, len, padding);
2670 }
2671
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002672 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 if (flags & DRAW_UNDERL)
2674 {
2675 hpen = CreatePen(PS_SOLID, 1, gui.currFgColor);
2676 old_pen = SelectObject(s_hdc, hpen);
2677 /* When p_linespace is 0, overwrite the bottom row of pixels.
2678 * Otherwise put the line just below the character. */
2679 y = FILL_Y(row + 1) - 1;
2680#ifndef MSWIN16_FASTTEXT
2681 if (p_linespace > 1)
2682 y -= p_linespace - 1;
2683#endif
2684 MoveToEx(s_hdc, FILL_X(col), y, NULL);
2685 /* Note: LineTo() excludes the last pixel in the line. */
2686 LineTo(s_hdc, FILL_X(col + len), y);
2687 DeleteObject(SelectObject(s_hdc, old_pen));
2688 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002689
2690 /* Undercurl */
2691 if (flags & DRAW_UNDERC)
2692 {
2693 int x;
2694 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002695 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002696
2697 y = FILL_Y(row + 1) - 1;
2698 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
2699 {
2700 offset = val[x % 8];
2701 SetPixel(s_hdc, x, y - offset, gui.currSpColor);
2702 }
2703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704}
2705
2706
2707/*
2708 * Output routines.
2709 */
2710
2711/* Flush any output to the screen */
2712 void
2713gui_mch_flush(void)
2714{
2715# if defined(__BORLANDC__)
2716 /*
2717 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
2718 * prototype declaration.
2719 * The compiler complains if __stdcall is not used in both declarations.
2720 */
2721 BOOL __stdcall GdiFlush(void);
2722# endif
2723
2724 GdiFlush();
2725}
2726
2727 static void
2728clear_rect(RECT *rcp)
2729{
2730 HBRUSH hbr;
2731
2732 hbr = CreateSolidBrush(gui.back_pixel);
2733 FillRect(s_hdc, rcp, hbr);
2734 DeleteBrush(hbr);
2735}
2736
2737
Bram Moolenaarc716c302006-01-21 22:12:51 +00002738 void
2739gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
2740{
2741 RECT workarea_rect;
2742
2743 get_work_area(&workarea_rect);
2744
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002745 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02002746 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002747 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00002748
2749 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
2750 * the menubar for MSwin, we subtract it from the screen height, so that
2751 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002752 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02002753 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002754 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00002755 - GetSystemMetrics(SM_CYCAPTION)
2756#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002757 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00002758#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002759 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00002760}
2761
2762
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763#if defined(FEAT_MENU) || defined(PROTO)
2764/*
2765 * Add a sub menu to the menu bar.
2766 */
2767 void
2768gui_mch_add_menu(
2769 vimmenu_T *menu,
2770 int pos)
2771{
2772 vimmenu_T *parent = menu->parent;
2773
2774 menu->submenu_id = CreatePopupMenu();
2775 menu->id = s_menu_id++;
2776
2777 if (menu_is_menubar(menu->name))
2778 {
2779 if (is_winnt_3())
2780 {
2781 InsertMenu((parent == NULL) ? s_menuBar : parent->submenu_id,
2782 (UINT)pos, MF_POPUP | MF_STRING | MF_BYPOSITION,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002783 (long_u)menu->submenu_id, (LPCTSTR) menu->name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 }
2785 else
2786 {
2787#ifdef FEAT_MBYTE
2788 WCHAR *wn = NULL;
2789 int n;
2790
2791 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2792 {
2793 /* 'encoding' differs from active codepage: convert menu name
2794 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002795 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 if (wn != NULL)
2797 {
2798 MENUITEMINFOW infow;
2799
2800 infow.cbSize = sizeof(infow);
2801 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
2802 | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002803 infow.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 infow.wID = menu->id;
2805 infow.fType = MFT_STRING;
2806 infow.dwTypeData = wn;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002807 infow.cch = (UINT)wcslen(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 infow.hSubMenu = menu->submenu_id;
2809 n = InsertMenuItemW((parent == NULL)
2810 ? s_menuBar : parent->submenu_id,
2811 (UINT)pos, TRUE, &infow);
2812 vim_free(wn);
2813 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2814 /* Failed, try using non-wide function. */
2815 wn = NULL;
2816 }
2817 }
2818
2819 if (wn == NULL)
2820#endif
2821 {
2822 MENUITEMINFO info;
2823
2824 info.cbSize = sizeof(info);
2825 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002826 info.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 info.wID = menu->id;
2828 info.fType = MFT_STRING;
2829 info.dwTypeData = (LPTSTR)menu->name;
2830 info.cch = (UINT)STRLEN(menu->name);
2831 info.hSubMenu = menu->submenu_id;
2832 InsertMenuItem((parent == NULL)
2833 ? s_menuBar : parent->submenu_id,
2834 (UINT)pos, TRUE, &info);
2835 }
2836 }
2837 }
2838
2839 /* Fix window size if menu may have wrapped */
2840 if (parent == NULL)
2841 gui_mswin_get_menu_height(!gui.starting);
2842#ifdef FEAT_TEAROFF
2843 else if (IsWindow(parent->tearoff_handle))
2844 rebuild_tearoff(parent);
2845#endif
2846}
2847
2848 void
2849gui_mch_show_popupmenu(vimmenu_T *menu)
2850{
2851 POINT mp;
2852
2853 (void)GetCursorPos((LPPOINT)&mp);
2854 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
2855}
2856
2857 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00002858gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859{
2860 vimmenu_T *menu = gui_find_menu(path_name);
2861
2862 if (menu != NULL)
2863 {
2864 POINT p;
2865
2866 /* Find the position of the current cursor */
2867 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00002868 if (mouse_pos)
2869 {
2870 int mx, my;
2871
2872 gui_mch_getmouse(&mx, &my);
2873 p.x += mx;
2874 p.y += my;
2875 }
2876 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 {
2878 p.x += TEXT_X(W_WINCOL(curwin) + curwin->w_wcol + 1);
2879 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
2880 }
2881 msg_scroll = FALSE;
2882 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
2883 }
2884}
2885
2886#if defined(FEAT_TEAROFF) || defined(PROTO)
2887/*
2888 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
2889 * create it as a pseudo-"tearoff menu".
2890 */
2891 void
2892gui_make_tearoff(char_u *path_name)
2893{
2894 vimmenu_T *menu = gui_find_menu(path_name);
2895
2896 /* Found the menu, so tear it off. */
2897 if (menu != NULL)
2898 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
2899}
2900#endif
2901
2902/*
2903 * Add a menu item to a menu
2904 */
2905 void
2906gui_mch_add_menu_item(
2907 vimmenu_T *menu,
2908 int idx)
2909{
2910 vimmenu_T *parent = menu->parent;
2911
2912 menu->id = s_menu_id++;
2913 menu->submenu_id = NULL;
2914
2915#ifdef FEAT_TEAROFF
2916 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
2917 {
2918 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
2919 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
2920 }
2921 else
2922#endif
2923#ifdef FEAT_TOOLBAR
2924 if (menu_is_toolbar(parent->name))
2925 {
2926 TBBUTTON newtb;
2927
2928 vim_memset(&newtb, 0, sizeof(newtb));
2929 if (menu_is_separator(menu->name))
2930 {
2931 newtb.iBitmap = 0;
2932 newtb.fsStyle = TBSTYLE_SEP;
2933 }
2934 else
2935 {
2936 newtb.iBitmap = get_toolbar_bitmap(menu);
2937 newtb.fsStyle = TBSTYLE_BUTTON;
2938 }
2939 newtb.idCommand = menu->id;
2940 newtb.fsState = TBSTATE_ENABLED;
2941 newtb.iString = 0;
2942 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
2943 (LPARAM)&newtb);
2944 menu->submenu_id = (HMENU)-1;
2945 }
2946 else
2947#endif
2948 {
2949#ifdef FEAT_MBYTE
2950 WCHAR *wn = NULL;
2951 int n;
2952
2953 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2954 {
2955 /* 'encoding' differs from active codepage: convert menu item name
2956 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002957 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 if (wn != NULL)
2959 {
2960 n = InsertMenuW(parent->submenu_id, (UINT)idx,
2961 (menu_is_separator(menu->name)
2962 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
2963 (UINT)menu->id, wn);
2964 vim_free(wn);
2965 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2966 /* Failed, try using non-wide function. */
2967 wn = NULL;
2968 }
2969 }
2970 if (wn == NULL)
2971#endif
2972 InsertMenu(parent->submenu_id, (UINT)idx,
2973 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
2974 | MF_BYPOSITION,
2975 (UINT)menu->id, (LPCTSTR)menu->name);
2976#ifdef FEAT_TEAROFF
2977 if (IsWindow(parent->tearoff_handle))
2978 rebuild_tearoff(parent);
2979#endif
2980 }
2981}
2982
2983/*
2984 * Destroy the machine specific menu widget.
2985 */
2986 void
2987gui_mch_destroy_menu(vimmenu_T *menu)
2988{
2989#ifdef FEAT_TOOLBAR
2990 /*
2991 * is this a toolbar button?
2992 */
2993 if (menu->submenu_id == (HMENU)-1)
2994 {
2995 int iButton;
2996
2997 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
2998 (WPARAM)menu->id, 0);
2999 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
3000 }
3001 else
3002#endif
3003 {
3004 if (menu->parent != NULL
3005 && menu_is_popup(menu->parent->dname)
3006 && menu->parent->submenu_id != NULL)
3007 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
3008 else
3009 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
3010 if (menu->submenu_id != NULL)
3011 DestroyMenu(menu->submenu_id);
3012#ifdef FEAT_TEAROFF
3013 if (IsWindow(menu->tearoff_handle))
3014 DestroyWindow(menu->tearoff_handle);
3015 if (menu->parent != NULL
3016 && menu->parent->children != NULL
3017 && IsWindow(menu->parent->tearoff_handle))
3018 {
3019 /* This menu must not show up when rebuilding the tearoff window. */
3020 menu->modes = 0;
3021 rebuild_tearoff(menu->parent);
3022 }
3023#endif
3024 }
3025}
3026
3027#ifdef FEAT_TEAROFF
3028 static void
3029rebuild_tearoff(vimmenu_T *menu)
3030{
3031 /*hackish*/
3032 char_u tbuf[128];
3033 RECT trect;
3034 RECT rct;
3035 RECT roct;
3036 int x, y;
3037
3038 HWND thwnd = menu->tearoff_handle;
3039
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003040 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 if (GetWindowRect(thwnd, &trect)
3042 && GetWindowRect(s_hwnd, &rct)
3043 && GetClientRect(s_hwnd, &roct))
3044 {
3045 x = trect.left - rct.left;
3046 y = (trect.top - rct.bottom + roct.bottom);
3047 }
3048 else
3049 {
3050 x = y = 0xffffL;
3051 }
3052 DestroyWindow(thwnd);
3053 if (menu->children != NULL)
3054 {
3055 gui_mch_tearoff(tbuf, menu, x, y);
3056 if (IsWindow(menu->tearoff_handle))
3057 (void) SetWindowPos(menu->tearoff_handle,
3058 NULL,
3059 (int)trect.left,
3060 (int)trect.top,
3061 0, 0,
3062 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
3063 }
3064}
3065#endif /* FEAT_TEAROFF */
3066
3067/*
3068 * Make a menu either grey or not grey.
3069 */
3070 void
3071gui_mch_menu_grey(
3072 vimmenu_T *menu,
3073 int grey)
3074{
3075#ifdef FEAT_TOOLBAR
3076 /*
3077 * is this a toolbar button?
3078 */
3079 if (menu->submenu_id == (HMENU)-1)
3080 {
3081 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
3082 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
3083 }
3084 else
3085#endif
3086 if (grey)
3087 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_GRAYED);
3088 else
3089 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
3090
3091#ifdef FEAT_TEAROFF
3092 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
3093 {
3094 WORD menuID;
3095 HWND menuHandle;
3096
3097 /*
3098 * A tearoff button has changed state.
3099 */
3100 if (menu->children == NULL)
3101 menuID = (WORD)(menu->id);
3102 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003103 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
3105 if (menuHandle)
3106 EnableWindow(menuHandle, !grey);
3107
3108 }
3109#endif
3110}
3111
3112#endif /* FEAT_MENU */
3113
3114
3115/* define some macros used to make the dialogue creation more readable */
3116
3117#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
3118#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00003119#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120
3121#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
3122/*
3123 * stuff for dialogs
3124 */
3125
3126/*
3127 * The callback routine used by all the dialogs. Very simple. First,
3128 * acknowledges the INITDIALOG message so that Windows knows to do standard
3129 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
3130 * pressed, return that button's ID - IDCANCEL (2), which is the button's
3131 * number.
3132 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003133/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 static LRESULT CALLBACK
3135dialog_callback(
3136 HWND hwnd,
3137 UINT message,
3138 WPARAM wParam,
3139 LPARAM lParam)
3140{
3141 if (message == WM_INITDIALOG)
3142 {
3143 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
3144 /* Set focus to the dialog. Set the default button, if specified. */
3145 (void)SetFocus(hwnd);
3146 if (dialog_default_button > IDCANCEL)
3147 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00003148 else
3149 /* We don't have a default, set focus on another element of the
3150 * dialog window, probably the icon */
3151 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 return FALSE;
3153 }
3154
3155 if (message == WM_COMMAND)
3156 {
3157 int button = LOWORD(wParam);
3158
3159 /* Don't end the dialog if something was selected that was
3160 * not a button.
3161 */
3162 if (button >= DLG_NONBUTTON_CONTROL)
3163 return TRUE;
3164
3165 /* If the edit box exists, copy the string. */
3166 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00003167 {
3168# if defined(FEAT_MBYTE) && defined(WIN3264)
3169 /* If the OS is Windows NT, and 'encoding' differs from active
3170 * codepage: use wide function and convert text. */
3171 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
3172 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003173 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00003174 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
3175 char_u *p;
3176
3177 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
3178 p = utf16_to_enc(wp, NULL);
3179 vim_strncpy(s_textfield, p, IOSIZE);
3180 vim_free(p);
3181 vim_free(wp);
3182 }
3183 else
3184# endif
3185 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003186 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00003187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
3189 /*
3190 * Need to check for IDOK because if the user just hits Return to
3191 * accept the default value, some reason this is what we get.
3192 */
3193 if (button == IDOK)
3194 {
3195 if (dialog_default_button > IDCANCEL)
3196 EndDialog(hwnd, dialog_default_button);
3197 }
3198 else
3199 EndDialog(hwnd, button - IDCANCEL);
3200 return TRUE;
3201 }
3202
3203 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
3204 {
3205 EndDialog(hwnd, 0);
3206 return TRUE;
3207 }
3208 return FALSE;
3209}
3210
3211/*
3212 * Create a dialog dynamically from the parameter strings.
3213 * type = type of dialog (question, alert, etc.)
3214 * title = dialog title. may be NULL for default title.
3215 * message = text to display. Dialog sizes to accommodate it.
3216 * buttons = '\n' separated list of button captions, default first.
3217 * dfltbutton = number of default button.
3218 *
3219 * This routine returns 1 if the first button is pressed,
3220 * 2 for the second, etc.
3221 *
3222 * 0 indicates Esc was pressed.
3223 * -1 for unexpected error
3224 *
3225 * If stubbing out this fn, return 1.
3226 */
3227
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003228static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229{
3230 "IDR_VIM",
3231 "IDR_VIM_ERROR",
3232 "IDR_VIM_ALERT",
3233 "IDR_VIM_INFO",
3234 "IDR_VIM_QUESTION"
3235};
3236
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 int
3238gui_mch_dialog(
3239 int type,
3240 char_u *title,
3241 char_u *message,
3242 char_u *buttons,
3243 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003244 char_u *textfield,
3245 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246{
3247 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00003248 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 int numButtons;
3250 int *buttonWidths, *buttonPositions;
3251 int buttonYpos;
3252 int nchar, i;
3253 DWORD lStyle;
3254 int dlgwidth = 0;
3255 int dlgheight;
3256 int editboxheight;
3257 int horizWidth = 0;
3258 int msgheight;
3259 char_u *pstart;
3260 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003261 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 char_u *tbuffer;
3263 RECT rect;
3264 HWND hwnd;
3265 HDC hdc;
3266 HFONT font, oldFont;
3267 TEXTMETRIC fontInfo;
3268 int fontHeight;
3269 int textWidth, minButtonWidth, messageWidth;
3270 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003271 int maxDialogHeight;
3272 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 int vertical;
3274 int dlgPaddingX;
3275 int dlgPaddingY;
3276#ifdef USE_SYSMENU_FONT
3277 LOGFONT lfSysmenu;
3278 int use_lfSysmenu = FALSE;
3279#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003280 garray_T ga;
3281 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282
3283#ifndef NO_CONSOLE
3284 /* Don't output anything in silent mode ("ex -s") */
3285 if (silent_mode)
3286 return dfltbutton; /* return default option */
3287#endif
3288
Bram Moolenaar748bf032005-02-02 23:04:36 +00003289 if (s_hwnd == NULL)
3290 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291
3292 if ((type < 0) || (type > VIM_LAST_TYPE))
3293 type = 0;
3294
3295 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003296 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003297 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003298 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299
3300 if (p == NULL)
3301 return -1;
3302
3303 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003304 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 * vim_strsave() doesn't take a const arg (why not?), so cast away the
3306 * const.
3307 */
3308 tbuffer = vim_strsave(buttons);
3309 if (tbuffer == NULL)
3310 return -1;
3311
3312 --dfltbutton; /* Change from one-based to zero-based */
3313
3314 /* Count buttons */
3315 numButtons = 1;
3316 for (i = 0; tbuffer[i] != '\0'; i++)
3317 {
3318 if (tbuffer[i] == DLG_BUTTON_SEP)
3319 numButtons++;
3320 }
3321 if (dfltbutton >= numButtons)
3322 dfltbutton = -1;
3323
3324 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003325 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 if (buttonWidths == NULL)
3327 return -1;
3328
3329 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003330 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 if (buttonPositions == NULL)
3332 return -1;
3333
3334 /*
3335 * Calculate how big the dialog must be.
3336 */
3337 hwnd = GetDesktopWindow();
3338 hdc = GetWindowDC(hwnd);
3339#ifdef USE_SYSMENU_FONT
3340 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
3341 {
3342 font = CreateFontIndirect(&lfSysmenu);
3343 use_lfSysmenu = TRUE;
3344 }
3345 else
3346#endif
3347 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3348 VARIABLE_PITCH , DLG_FONT_NAME);
3349 if (s_usenewlook)
3350 {
3351 oldFont = SelectFont(hdc, font);
3352 dlgPaddingX = DLG_PADDING_X;
3353 dlgPaddingY = DLG_PADDING_Y;
3354 }
3355 else
3356 {
3357 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
3358 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
3359 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
3360 }
3361 GetTextMetrics(hdc, &fontInfo);
3362 fontHeight = fontInfo.tmHeight;
3363
3364 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003365 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366
3367 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003368 if (s_hwnd == NULL)
3369 {
3370 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371
Bram Moolenaarc716c302006-01-21 22:12:51 +00003372 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003373 get_work_area(&workarea_rect);
3374 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
3375 if (maxDialogWidth > 600)
3376 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02003377 /* Leave some room for the taskbar. */
3378 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003379 }
3380 else
3381 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02003382 /* Use our own window for the size, unless it's very small. */
3383 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003384 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02003385 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02003386 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003387 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
3388 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003389
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003390 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02003391 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02003392 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02003393 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003394 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
3395 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
3396 }
3397
3398 /* Set dlgwidth to width of message.
3399 * Copy the message into "ga", changing NL to CR-NL and inserting line
3400 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 pstart = message;
3402 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003403 msgheight = 0;
3404 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 do
3406 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003407 msgheight += fontHeight; /* at least one line */
3408
3409 /* Need to figure out where to break the string. The system does it
3410 * at a word boundary, which would mean we can't compute the number of
3411 * wrapped lines. */
3412 textWidth = 0;
3413 last_white = NULL;
3414 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00003415 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003416#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003417 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003418#else
3419 l = 1;
3420#endif
3421 if (l == 1 && vim_iswhite(*pend)
3422 && textWidth > maxDialogWidth * 3 / 4)
3423 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02003424 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003425 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003426 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003427 /* Line will wrap. */
3428 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003429 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003430 textWidth = 0;
3431
3432 if (last_white != NULL)
3433 {
3434 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003435 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003436 pend = last_white + 1;
3437 last_white = NULL;
3438 }
3439 ga_append(&ga, '\r');
3440 ga_append(&ga, '\n');
3441 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003442 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003443
3444 while (--l >= 0)
3445 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003446 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003447 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003449
3450 ga_append(&ga, '\r');
3451 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 pstart = pend + 1;
3453 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003454
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003455 if (ga.ga_data != NULL)
3456 message = ga.ga_data;
3457
Bram Moolenaar748bf032005-02-02 23:04:36 +00003458 messageWidth += 10; /* roundoff space */
3459
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02003461 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
3462 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
3464 if (msgheight < DLG_ICON_HEIGHT)
3465 msgheight = DLG_ICON_HEIGHT;
3466
3467 /*
3468 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00003469 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00003471 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 if (!vertical)
3473 {
3474 // Place buttons horizontally if they fit.
3475 horizWidth = dlgPaddingX;
3476 pstart = tbuffer;
3477 i = 0;
3478 do
3479 {
3480 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
3481 if (pend == NULL)
3482 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02003483 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 if (textWidth < minButtonWidth)
3485 textWidth = minButtonWidth;
3486 textWidth += dlgPaddingX; /* Padding within button */
3487 buttonWidths[i] = textWidth;
3488 buttonPositions[i++] = horizWidth;
3489 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
3490 pstart = pend + 1;
3491 } while (*pend != NUL);
3492
3493 if (horizWidth > maxDialogWidth)
3494 vertical = TRUE; // Too wide to fit on the screen.
3495 else if (horizWidth > dlgwidth)
3496 dlgwidth = horizWidth;
3497 }
3498
3499 if (vertical)
3500 {
3501 // Stack buttons vertically.
3502 pstart = tbuffer;
3503 do
3504 {
3505 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
3506 if (pend == NULL)
3507 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02003508 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 textWidth += dlgPaddingX; /* Padding within button */
3510 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
3511 if (textWidth > dlgwidth)
3512 dlgwidth = textWidth;
3513 pstart = pend + 1;
3514 } while (*pend != NUL);
3515 }
3516
3517 if (dlgwidth < DLG_MIN_WIDTH)
3518 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
3519
3520 /* start to fill in the dlgtemplate information. addressing by WORDs */
3521 if (s_usenewlook)
3522 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
3523 else
3524 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
3525
3526 add_long(lStyle);
3527 add_long(0); // (lExtendedStyle)
3528 pnumitems = p; /*save where the number of items must be stored*/
3529 add_word(0); // NumberOfItems(will change later)
3530 add_word(10); // x
3531 add_word(10); // y
3532 add_word(PixelToDialogX(dlgwidth)); // cx
3533
3534 // Dialog height.
3535 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02003536 dlgheight = msgheight + 2 * dlgPaddingY
3537 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 else
3539 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
3540
3541 // Dialog needs to be taller if contains an edit box.
3542 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
3543 if (textfield != NULL)
3544 dlgheight += editboxheight;
3545
Bram Moolenaara95d8232013-08-07 15:27:11 +02003546 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
3547 if (dlgheight > maxDialogHeight)
3548 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02003549 msgheight = msgheight - (dlgheight - maxDialogHeight);
3550 dlgheight = maxDialogHeight;
3551 scroll_flag = WS_VSCROLL;
3552 /* Make sure scrollbar doesn't appear in the middle of the dialog */
3553 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02003554 }
3555
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 add_word(PixelToDialogY(dlgheight));
3557
3558 add_word(0); // Menu
3559 add_word(0); // Class
3560
3561 /* copy the title of the dialog */
3562 nchar = nCopyAnsiToWideChar(p, (title ?
3563 (LPSTR)title :
3564 (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
3565 p += nchar;
3566
3567 if (s_usenewlook)
3568 {
3569 /* do the font, since DS_3DLOOK doesn't work properly */
3570#ifdef USE_SYSMENU_FONT
3571 if (use_lfSysmenu)
3572 {
3573 /* point size */
3574 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
3575 GetDeviceCaps(hdc, LOGPIXELSY));
3576 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
3577 }
3578 else
3579#endif
3580 {
3581 *p++ = DLG_FONT_POINT_SIZE; // point size
3582 nchar = nCopyAnsiToWideChar(p, TEXT(DLG_FONT_NAME));
3583 }
3584 p += nchar;
3585 }
3586
3587 buttonYpos = msgheight + 2 * dlgPaddingY;
3588
3589 if (textfield != NULL)
3590 buttonYpos += editboxheight;
3591
3592 pstart = tbuffer;
3593 if (!vertical)
3594 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
3595 for (i = 0; i < numButtons; i++)
3596 {
3597 /* get end of this button. */
3598 for ( pend = pstart;
3599 *pend && (*pend != DLG_BUTTON_SEP);
3600 pend++)
3601 ;
3602
3603 if (*pend)
3604 *pend = '\0';
3605
3606 /*
3607 * old NOTE:
3608 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
3609 * the focus to the first tab-able button and in so doing makes that
3610 * the default!! Grrr. Workaround: Make the default button the only
3611 * one with WS_TABSTOP style. Means user can't tab between buttons, but
3612 * he/she can use arrow keys.
3613 *
3614 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00003615 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 * dialog. Also needed for when the textfield is the default control.
3617 * It appears to work now (perhaps not on Win95?).
3618 */
3619 if (vertical)
3620 {
3621 p = add_dialog_element(p,
3622 (i == dfltbutton
3623 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
3624 PixelToDialogX(DLG_VERT_PADDING_X),
3625 PixelToDialogY(buttonYpos /* TBK */
3626 + 2 * fontHeight * i),
3627 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
3628 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003629 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
3631 else
3632 {
3633 p = add_dialog_element(p,
3634 (i == dfltbutton
3635 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
3636 PixelToDialogX(horizWidth + buttonPositions[i]),
3637 PixelToDialogY(buttonYpos), /* TBK */
3638 PixelToDialogX(buttonWidths[i]),
3639 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003640 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 }
3642 pstart = pend + 1; /*next button*/
3643 }
3644 *pnumitems += numButtons;
3645
3646 /* Vim icon */
3647 p = add_dialog_element(p, SS_ICON,
3648 PixelToDialogX(dlgPaddingX),
3649 PixelToDialogY(dlgPaddingY),
3650 PixelToDialogX(DLG_ICON_WIDTH),
3651 PixelToDialogY(DLG_ICON_HEIGHT),
3652 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
3653 dlg_icons[type]);
3654
Bram Moolenaar748bf032005-02-02 23:04:36 +00003655 /* Dialog message */
3656 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
3657 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
3658 PixelToDialogY(dlgPaddingY),
3659 (WORD)(PixelToDialogX(messageWidth) + 1),
3660 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003661 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662
3663 /* Edit box */
3664 if (textfield != NULL)
3665 {
3666 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
3667 PixelToDialogX(2 * dlgPaddingX),
3668 PixelToDialogY(2 * dlgPaddingY + msgheight),
3669 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
3670 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003671 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 *pnumitems += 1;
3673 }
3674
3675 *pnumitems += 2;
3676
3677 SelectFont(hdc, oldFont);
3678 DeleteObject(font);
3679 ReleaseDC(hwnd, hdc);
3680
3681 /* Let the dialog_callback() function know which button to make default
3682 * If we have an edit box, make that the default. We also need to tell
3683 * dialog_callback() if this dialog contains an edit box or not. We do
3684 * this by setting s_textfield if it does.
3685 */
3686 if (textfield != NULL)
3687 {
3688 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
3689 s_textfield = textfield;
3690 }
3691 else
3692 {
3693 dialog_default_button = IDCANCEL + 1 + dfltbutton;
3694 s_textfield = NULL;
3695 }
3696
3697 /* show the dialog box modally and get a return value */
3698 nchar = (int)DialogBoxIndirect(
3699 s_hinst,
3700 (LPDLGTEMPLATE)pdlgtemplate,
3701 s_hwnd,
3702 (DLGPROC)dialog_callback);
3703
3704 LocalFree(LocalHandle(pdlgtemplate));
3705 vim_free(tbuffer);
3706 vim_free(buttonWidths);
3707 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003708 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709
3710 /* Focus back to our window (for when MDI is used). */
3711 (void)SetFocus(s_hwnd);
3712
3713 return nchar;
3714}
3715
3716#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003717
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718/*
3719 * Put a simple element (basic class) onto a dialog template in memory.
3720 * return a pointer to where the next item should be added.
3721 *
3722 * parameters:
3723 * lStyle = additional style flags
3724 * (be careful, NT3.51 & Win32s will ignore the new ones)
3725 * x,y = x & y positions IN DIALOG UNITS
3726 * w,h = width and height IN DIALOG UNITS
3727 * Id = ID used in messages
3728 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
3729 * caption = usually text or resource name
3730 *
3731 * TODO: use the length information noted here to enable the dialog creation
3732 * routines to work out more exactly how much memory they need to alloc.
3733 */
3734 static PWORD
3735add_dialog_element(
3736 PWORD p,
3737 DWORD lStyle,
3738 WORD x,
3739 WORD y,
3740 WORD w,
3741 WORD h,
3742 WORD Id,
3743 WORD clss,
3744 const char *caption)
3745{
3746 int nchar;
3747
3748 p = lpwAlign(p); /* Align to dword boundary*/
3749 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
3750 *p++ = LOWORD(lStyle);
3751 *p++ = HIWORD(lStyle);
3752 *p++ = 0; // LOWORD (lExtendedStyle)
3753 *p++ = 0; // HIWORD (lExtendedStyle)
3754 *p++ = x;
3755 *p++ = y;
3756 *p++ = w;
3757 *p++ = h;
3758 *p++ = Id; //9 or 10 words in all
3759
3760 *p++ = (WORD)0xffff;
3761 *p++ = clss; //2 more here
3762
3763 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption); //strlen(caption)+1
3764 p += nchar;
3765
3766 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
3767
3768 return p; //total = 15+ (strlen(caption)) words
3769 // = 30 + 2(strlen(caption) bytes reqd
3770}
3771
3772
3773/*
3774 * Helper routine. Take an input pointer, return closest pointer that is
3775 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
3776 */
3777 static LPWORD
3778lpwAlign(
3779 LPWORD lpIn)
3780{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003781 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003783 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 ul += 3;
3785 ul >>= 2;
3786 ul <<= 2;
3787 return (LPWORD)ul;
3788}
3789
3790/*
3791 * Helper routine. Takes second parameter as Ansi string, copies it to first
3792 * parameter as wide character (16-bits / char) string, and returns integer
3793 * number of wide characters (words) in string (including the trailing wide
3794 * char NULL). Partly taken from the Win32SDK samples.
3795 */
3796 static int
3797nCopyAnsiToWideChar(
3798 LPWORD lpWCStr,
3799 LPSTR lpAnsiIn)
3800{
3801 int nChar = 0;
3802#ifdef FEAT_MBYTE
3803 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
3804 int i;
3805 WCHAR *wn;
3806
3807 if (enc_codepage == 0 && (int)GetACP() != enc_codepage)
3808 {
3809 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003810 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 if (wn != NULL)
3812 {
3813 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003814 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 vim_free(wn);
3816 }
3817 }
3818 if (nChar == 0)
3819 /* Use Win32 conversion function. */
3820 nChar = MultiByteToWideChar(
3821 enc_codepage > 0 ? enc_codepage : CP_ACP,
3822 MB_PRECOMPOSED,
3823 lpAnsiIn, len,
3824 lpWCStr, len);
3825 for (i = 0; i < nChar; ++i)
3826 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
3827 lpWCStr[i] = (WORD)' ';
3828#else
3829 do
3830 {
3831 if (*lpAnsiIn == '\t')
3832 *lpWCStr++ = (WORD)' ';
3833 else
3834 *lpWCStr++ = (WORD)*lpAnsiIn;
3835 nChar++;
3836 } while (*lpAnsiIn++);
3837#endif
3838
3839 return nChar;
3840}
3841
3842
3843#ifdef FEAT_TEAROFF
3844/*
3845 * The callback function for all the modeless dialogs that make up the
3846 * "tearoff menus" Very simple - forward button presses (to fool Vim into
3847 * thinking its menus have been clicked), and go away when closed.
3848 */
3849 static LRESULT CALLBACK
3850tearoff_callback(
3851 HWND hwnd,
3852 UINT message,
3853 WPARAM wParam,
3854 LPARAM lParam)
3855{
3856 if (message == WM_INITDIALOG)
3857 return (TRUE);
3858
3859 /* May show the mouse pointer again. */
3860 HandleMouseHide(message, lParam);
3861
3862 if (message == WM_COMMAND)
3863 {
3864 if ((WORD)(LOWORD(wParam)) & 0x8000)
3865 {
3866 POINT mp;
3867 RECT rect;
3868
3869 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
3870 {
3871 (void)TrackPopupMenu(
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003872 (HMENU)(long_u)(LOWORD(wParam) ^ 0x8000),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 TPM_LEFTALIGN | TPM_LEFTBUTTON,
3874 (int)rect.right - 8,
3875 (int)mp.y,
3876 (int)0, /*reserved param*/
3877 s_hwnd,
3878 NULL);
3879 /*
3880 * NOTE: The pop-up menu can eat the mouse up event.
3881 * We deal with this in normal.c.
3882 */
3883 }
3884 }
3885 else
3886 /* Pass on messages to the main Vim window */
3887 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
3888 /*
3889 * Give main window the focus back: this is so after
3890 * choosing a tearoff button you can start typing again
3891 * straight away.
3892 */
3893 (void)SetFocus(s_hwnd);
3894 return TRUE;
3895 }
3896 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
3897 {
3898 DestroyWindow(hwnd);
3899 return TRUE;
3900 }
3901
3902 /* When moved around, give main window the focus back. */
3903 if (message == WM_EXITSIZEMOVE)
3904 (void)SetActiveWindow(s_hwnd);
3905
3906 return FALSE;
3907}
3908#endif
3909
3910
3911/*
3912 * Decide whether to use the "new look" (small, non-bold font) or the "old
3913 * look" (big, clanky font) for dialogs, and work out a few values for use
3914 * later accordingly.
3915 */
3916 static void
3917get_dialog_font_metrics(void)
3918{
3919 HDC hdc;
3920 HFONT hfontTools = 0;
3921 DWORD dlgFontSize;
3922 SIZE size;
3923#ifdef USE_SYSMENU_FONT
3924 LOGFONT lfSysmenu;
3925#endif
3926
3927 s_usenewlook = FALSE;
3928
3929 /*
3930 * For NT3.51 and Win32s, we stick with the old look
3931 * because it matches everything else.
3932 */
3933 if (!is_winnt_3())
3934 {
3935#ifdef USE_SYSMENU_FONT
3936 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
3937 hfontTools = CreateFontIndirect(&lfSysmenu);
3938 else
3939#endif
3940 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
3941 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
3942
3943 if (hfontTools)
3944 {
3945 hdc = GetDC(s_hwnd);
3946 SelectObject(hdc, hfontTools);
3947 /*
3948 * GetTextMetrics() doesn't return the right value in
3949 * tmAveCharWidth, so we have to figure out the dialog base units
3950 * ourselves.
3951 */
3952 GetTextExtentPoint(hdc,
3953 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
3954 52, &size);
3955 ReleaseDC(s_hwnd, hdc);
3956
3957 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
3958 s_dlgfntheight = (WORD)size.cy;
3959 s_usenewlook = TRUE;
3960 }
3961 }
3962
3963 if (!s_usenewlook)
3964 {
3965 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
3966 s_dlgfntwidth = LOWORD(dlgFontSize);
3967 s_dlgfntheight = HIWORD(dlgFontSize);
3968 }
3969}
3970
3971#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
3972/*
3973 * Create a pseudo-"tearoff menu" based on the child
3974 * items of a given menu pointer.
3975 */
3976 static void
3977gui_mch_tearoff(
3978 char_u *title,
3979 vimmenu_T *menu,
3980 int initX,
3981 int initY)
3982{
3983 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
3984 int template_len;
3985 int nchar, textWidth, submenuWidth;
3986 DWORD lStyle;
3987 DWORD lExtendedStyle;
3988 WORD dlgwidth;
3989 WORD menuID;
3990 vimmenu_T *pmenu;
3991 vimmenu_T *the_menu = menu;
3992 HWND hwnd;
3993 HDC hdc;
3994 HFONT font, oldFont;
3995 int col, spaceWidth, len;
3996 int columnWidths[2];
3997 char_u *label, *text;
3998 int acLen = 0;
3999 int nameLen;
4000 int padding0, padding1, padding2 = 0;
4001 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004002 int x;
4003 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004#ifdef USE_SYSMENU_FONT
4005 LOGFONT lfSysmenu;
4006 int use_lfSysmenu = FALSE;
4007#endif
4008
4009 /*
4010 * If this menu is already torn off, move it to the mouse position.
4011 */
4012 if (IsWindow(menu->tearoff_handle))
4013 {
4014 POINT mp;
4015 if (GetCursorPos((LPPOINT)&mp))
4016 {
4017 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
4018 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
4019 }
4020 return;
4021 }
4022
4023 /*
4024 * Create a new tearoff.
4025 */
4026 if (*title == MNU_HIDDEN_CHAR)
4027 title++;
4028
4029 /* Allocate memory to store the dialog template. It's made bigger when
4030 * needed. */
4031 template_len = DLG_ALLOC_SIZE;
4032 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
4033 if (p == NULL)
4034 return;
4035
4036 hwnd = GetDesktopWindow();
4037 hdc = GetWindowDC(hwnd);
4038#ifdef USE_SYSMENU_FONT
4039 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
4040 {
4041 font = CreateFontIndirect(&lfSysmenu);
4042 use_lfSysmenu = TRUE;
4043 }
4044 else
4045#endif
4046 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4047 VARIABLE_PITCH , DLG_FONT_NAME);
4048 if (s_usenewlook)
4049 oldFont = SelectFont(hdc, font);
4050 else
4051 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
4052
4053 /* Calculate width of a single space. Used for padding columns to the
4054 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004055 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056
4057 /* Figure out max width of the text column, the accelerator column and the
4058 * optional submenu column. */
4059 submenuWidth = 0;
4060 for (col = 0; col < 2; col++)
4061 {
4062 columnWidths[col] = 0;
4063 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
4064 {
4065 /* Use "dname" here to compute the width of the visible text. */
4066 text = (col == 0) ? pmenu->dname : pmenu->actext;
4067 if (text != NULL && *text != NUL)
4068 {
4069 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
4070 if (textWidth > columnWidths[col])
4071 columnWidths[col] = textWidth;
4072 }
4073 if (pmenu->children != NULL)
4074 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
4075 }
4076 }
4077 if (columnWidths[1] == 0)
4078 {
4079 /* no accelerators */
4080 if (submenuWidth != 0)
4081 columnWidths[0] += submenuWidth;
4082 else
4083 columnWidths[0] += spaceWidth;
4084 }
4085 else
4086 {
4087 /* there is an accelerator column */
4088 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
4089 columnWidths[1] += submenuWidth;
4090 }
4091
4092 /*
4093 * Now find the total width of our 'menu'.
4094 */
4095 textWidth = columnWidths[0] + columnWidths[1];
4096 if (submenuWidth != 0)
4097 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004098 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
4100 textWidth += submenuWidth;
4101 }
4102 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
4103 if (textWidth > dlgwidth)
4104 dlgwidth = textWidth;
4105 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
4106
4107 /* W95 can't do thin dialogs, they look v. weird! */
4108 if (mch_windows95() && dlgwidth < TEAROFF_MIN_WIDTH)
4109 dlgwidth = TEAROFF_MIN_WIDTH;
4110
4111 /* start to fill in the dlgtemplate information. addressing by WORDs */
4112 if (s_usenewlook)
4113 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
4114 else
4115 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
4116
4117 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
4118 *p++ = LOWORD(lStyle);
4119 *p++ = HIWORD(lStyle);
4120 *p++ = LOWORD(lExtendedStyle);
4121 *p++ = HIWORD(lExtendedStyle);
4122 pnumitems = p; /* save where the number of items must be stored */
4123 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004124 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004126 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 else
4128 *p++ = PixelToDialogX(initX); // x
4129 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004130 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 else
4132 *p++ = PixelToDialogY(initY); // y
4133 *p++ = PixelToDialogX(dlgwidth); // cx
4134 ptrueheight = p;
4135 *p++ = 0; // dialog height: changed later anyway
4136 *p++ = 0; // Menu
4137 *p++ = 0; // Class
4138
4139 /* copy the title of the dialog */
4140 nchar = nCopyAnsiToWideChar(p, ((*title)
4141 ? (LPSTR)title
4142 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
4143 p += nchar;
4144
4145 if (s_usenewlook)
4146 {
4147 /* do the font, since DS_3DLOOK doesn't work properly */
4148#ifdef USE_SYSMENU_FONT
4149 if (use_lfSysmenu)
4150 {
4151 /* point size */
4152 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
4153 GetDeviceCaps(hdc, LOGPIXELSY));
4154 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
4155 }
4156 else
4157#endif
4158 {
4159 *p++ = DLG_FONT_POINT_SIZE; // point size
4160 nchar = nCopyAnsiToWideChar (p, TEXT(DLG_FONT_NAME));
4161 }
4162 p += nchar;
4163 }
4164
4165 /*
4166 * Loop over all the items in the menu.
4167 * But skip over the tearbar.
4168 */
4169 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
4170 menu = menu->children->next;
4171 else
4172 menu = menu->children;
4173 for ( ; menu != NULL; menu = menu->next)
4174 {
4175 if (menu->modes == 0) /* this menu has just been deleted */
4176 continue;
4177 if (menu_is_separator(menu->dname))
4178 {
4179 sepPadding += 3;
4180 continue;
4181 }
4182
4183 /* Check if there still is plenty of room in the template. Make it
4184 * larger when needed. */
4185 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
4186 {
4187 WORD *newp;
4188
4189 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
4190 if (newp != NULL)
4191 {
4192 template_len += 4096;
4193 mch_memmove(newp, pdlgtemplate,
4194 (char *)p - (char *)pdlgtemplate);
4195 p = newp + (p - pdlgtemplate);
4196 pnumitems = newp + (pnumitems - pdlgtemplate);
4197 ptrueheight = newp + (ptrueheight - pdlgtemplate);
4198 LocalFree(LocalHandle(pdlgtemplate));
4199 pdlgtemplate = newp;
4200 }
4201 }
4202
4203 /* Figure out minimal length of this menu label. Use "name" for the
4204 * actual text, "dname" for estimating the displayed size. "name"
4205 * has "&a" for mnemonic and includes the accelerator. */
4206 len = nameLen = (int)STRLEN(menu->name);
4207 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
4208 (int)STRLEN(menu->dname))) / spaceWidth;
4209 len += padding0;
4210
4211 if (menu->actext != NULL)
4212 {
4213 acLen = (int)STRLEN(menu->actext);
4214 len += acLen;
4215 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
4216 }
4217 else
4218 textWidth = 0;
4219 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
4220 len += padding1;
4221
4222 if (menu->children == NULL)
4223 {
4224 padding2 = submenuWidth / spaceWidth;
4225 len += padding2;
4226 menuID = (WORD)(menu->id);
4227 }
4228 else
4229 {
4230 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004231 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
4233
4234 /* Allocate menu label and fill it in */
4235 text = label = alloc((unsigned)len + 1);
4236 if (label == NULL)
4237 break;
4238
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004239 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 text = vim_strchr(text, TAB); /* stop at TAB before actext */
4241 if (text == NULL)
4242 text = label + nameLen; /* no actext, use whole name */
4243 while (padding0-- > 0)
4244 *text++ = ' ';
4245 if (menu->actext != NULL)
4246 {
4247 STRNCPY(text, menu->actext, acLen);
4248 text += acLen;
4249 }
4250 while (padding1-- > 0)
4251 *text++ = ' ';
4252 if (menu->children != NULL)
4253 {
4254 STRCPY(text, TEAROFF_SUBMENU_LABEL);
4255 text += STRLEN(TEAROFF_SUBMENU_LABEL);
4256 }
4257 else
4258 {
4259 while (padding2-- > 0)
4260 *text++ = ' ';
4261 }
4262 *text = NUL;
4263
4264 /*
4265 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
4266 * W95/NT4 it makes the tear-off look more like a menu.
4267 */
4268 p = add_dialog_element(p,
4269 BS_PUSHBUTTON|BS_LEFT,
4270 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
4271 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
4272 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
4273 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004274 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 vim_free(label);
4276 (*pnumitems)++;
4277 }
4278
4279 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
4280
4281
4282 /* show modelessly */
4283 the_menu->tearoff_handle = CreateDialogIndirect(
4284 s_hinst,
4285 (LPDLGTEMPLATE)pdlgtemplate,
4286 s_hwnd,
4287 (DLGPROC)tearoff_callback);
4288
4289 LocalFree(LocalHandle(pdlgtemplate));
4290 SelectFont(hdc, oldFont);
4291 DeleteObject(font);
4292 ReleaseDC(hwnd, hdc);
4293
4294 /*
4295 * Reassert ourselves as the active window. This is so that after creating
4296 * a tearoff, the user doesn't have to click with the mouse just to start
4297 * typing again!
4298 */
4299 (void)SetActiveWindow(s_hwnd);
4300
4301 /* make sure the right buttons are enabled */
4302 force_menu_update = TRUE;
4303}
4304#endif
4305
4306#if defined(FEAT_TOOLBAR) || defined(PROTO)
4307#include "gui_w32_rc.h"
4308
4309/* This not defined in older SDKs */
4310# ifndef TBSTYLE_FLAT
4311# define TBSTYLE_FLAT 0x0800
4312# endif
4313
4314/*
4315 * Create the toolbar, initially unpopulated.
4316 * (just like the menu, there are no defaults, it's all
4317 * set up through menu.vim)
4318 */
4319 static void
4320initialise_toolbar(void)
4321{
4322 InitCommonControls();
4323 s_toolbarhwnd = CreateToolbarEx(
4324 s_hwnd,
4325 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
4326 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004327 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 s_hinst,
4329 IDR_TOOLBAR1, // id of initial bitmap
4330 NULL,
4331 0, // initial number of buttons
4332 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
4333 TOOLBAR_BUTTON_HEIGHT,
4334 TOOLBAR_BUTTON_WIDTH,
4335 TOOLBAR_BUTTON_HEIGHT,
4336 sizeof(TBBUTTON)
4337 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004338 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339
4340 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
4341}
4342
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004343 static LRESULT CALLBACK
4344toolbar_wndproc(
4345 HWND hwnd,
4346 UINT uMsg,
4347 WPARAM wParam,
4348 LPARAM lParam)
4349{
4350 HandleMouseHide(uMsg, lParam);
4351 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
4352}
4353
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 static int
4355get_toolbar_bitmap(vimmenu_T *menu)
4356{
4357 int i = -1;
4358
4359 /*
4360 * Check user bitmaps first, unless builtin is specified.
4361 */
4362 if (!is_winnt_3() && !menu->icon_builtin)
4363 {
4364 char_u fname[MAXPATHL];
4365 HANDLE hbitmap = NULL;
4366
4367 if (menu->iconfile != NULL)
4368 {
4369 gui_find_iconfile(menu->iconfile, fname, "bmp");
4370 hbitmap = LoadImage(
4371 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004372 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 IMAGE_BITMAP,
4374 TOOLBAR_BUTTON_WIDTH,
4375 TOOLBAR_BUTTON_HEIGHT,
4376 LR_LOADFROMFILE |
4377 LR_LOADMAP3DCOLORS
4378 );
4379 }
4380
4381 /*
4382 * If the LoadImage call failed, or the "icon=" file
4383 * didn't exist or wasn't specified, try the menu name
4384 */
4385 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02004386 && (gui_find_bitmap(
4387#ifdef FEAT_MULTI_LANG
4388 menu->en_dname != NULL ? menu->en_dname :
4389#endif
4390 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 hbitmap = LoadImage(
4392 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004393 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 IMAGE_BITMAP,
4395 TOOLBAR_BUTTON_WIDTH,
4396 TOOLBAR_BUTTON_HEIGHT,
4397 LR_LOADFROMFILE |
4398 LR_LOADMAP3DCOLORS
4399 );
4400
4401 if (hbitmap != NULL)
4402 {
4403 TBADDBITMAP tbAddBitmap;
4404
4405 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004406 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407
4408 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
4409 (WPARAM)1, (LPARAM)&tbAddBitmap);
4410 /* i will be set to -1 if it fails */
4411 }
4412 }
4413 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
4414 i = menu->iconidx;
4415
4416 return i;
4417}
4418#endif
4419
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004420#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
4421 static void
4422initialise_tabline(void)
4423{
4424 InitCommonControls();
4425
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004426 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004427 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004428 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4429 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004430 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004431
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004432 gui.tabline_height = TABLINE_HEIGHT;
4433
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004434# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004435 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004436# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004437}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004438
4439 static LRESULT CALLBACK
4440tabline_wndproc(
4441 HWND hwnd,
4442 UINT uMsg,
4443 WPARAM wParam,
4444 LPARAM lParam)
4445{
4446 HandleMouseHide(uMsg, lParam);
4447 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
4448}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004449#endif
4450
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
4452/*
4453 * Make the GUI window come to the foreground.
4454 */
4455 void
4456gui_mch_set_foreground(void)
4457{
4458 if (IsIconic(s_hwnd))
4459 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
4460 SetForegroundWindow(s_hwnd);
4461}
4462#endif
4463
4464#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4465 static void
4466dyn_imm_load(void)
4467{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02004468 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 if (hLibImm == NULL)
4470 return;
4471
4472 pImmGetCompositionStringA
4473 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
4474 pImmGetCompositionStringW
4475 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
4476 pImmGetContext
4477 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
4478 pImmAssociateContext
4479 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
4480 pImmReleaseContext
4481 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
4482 pImmGetOpenStatus
4483 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
4484 pImmSetOpenStatus
4485 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
4486 pImmGetCompositionFont
4487 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
4488 pImmSetCompositionFont
4489 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
4490 pImmSetCompositionWindow
4491 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
4492 pImmGetConversionStatus
4493 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00004494 pImmSetConversionStatus
4495 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
4497 if ( pImmGetCompositionStringA == NULL
4498 || pImmGetCompositionStringW == NULL
4499 || pImmGetContext == NULL
4500 || pImmAssociateContext == NULL
4501 || pImmReleaseContext == NULL
4502 || pImmGetOpenStatus == NULL
4503 || pImmSetOpenStatus == NULL
4504 || pImmGetCompositionFont == NULL
4505 || pImmSetCompositionFont == NULL
4506 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00004507 || pImmGetConversionStatus == NULL
4508 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 {
4510 FreeLibrary(hLibImm);
4511 hLibImm = NULL;
4512 pImmGetContext = NULL;
4513 return;
4514 }
4515
4516 return;
4517}
4518
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519#endif
4520
4521#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
4522
4523# ifdef FEAT_XPM_W32
4524# define IMAGE_XPM 100
4525# endif
4526
4527typedef struct _signicon_t
4528{
4529 HANDLE hImage;
4530 UINT uType;
4531#ifdef FEAT_XPM_W32
4532 HANDLE hShape; /* Mask bitmap handle */
4533#endif
4534} signicon_t;
4535
4536 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004537gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538{
4539 signicon_t *sign;
4540 int x, y, w, h;
4541
4542 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
4543 return;
4544
4545 x = TEXT_X(col);
4546 y = TEXT_Y(row);
4547 w = gui.char_width * 2;
4548 h = gui.char_height;
4549 switch (sign->uType)
4550 {
4551 case IMAGE_BITMAP:
4552 {
4553 HDC hdcMem;
4554 HBITMAP hbmpOld;
4555
4556 hdcMem = CreateCompatibleDC(s_hdc);
4557 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
4558 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
4559 SelectObject(hdcMem, hbmpOld);
4560 DeleteDC(hdcMem);
4561 }
4562 break;
4563 case IMAGE_ICON:
4564 case IMAGE_CURSOR:
4565 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
4566 break;
4567#ifdef FEAT_XPM_W32
4568 case IMAGE_XPM:
4569 {
4570 HDC hdcMem;
4571 HBITMAP hbmpOld;
4572
4573 hdcMem = CreateCompatibleDC(s_hdc);
4574 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
4575 /* Make hole */
4576 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
4577
4578 SelectObject(hdcMem, sign->hImage);
4579 /* Paint sign */
4580 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
4581 SelectObject(hdcMem, hbmpOld);
4582 DeleteDC(hdcMem);
4583 }
4584 break;
4585#endif
4586 }
4587}
4588
4589 static void
4590close_signicon_image(signicon_t *sign)
4591{
4592 if (sign)
4593 switch (sign->uType)
4594 {
4595 case IMAGE_BITMAP:
4596 DeleteObject((HGDIOBJ)sign->hImage);
4597 break;
4598 case IMAGE_CURSOR:
4599 DestroyCursor((HCURSOR)sign->hImage);
4600 break;
4601 case IMAGE_ICON:
4602 DestroyIcon((HICON)sign->hImage);
4603 break;
4604#ifdef FEAT_XPM_W32
4605 case IMAGE_XPM:
4606 DeleteObject((HBITMAP)sign->hImage);
4607 DeleteObject((HBITMAP)sign->hShape);
4608 break;
4609#endif
4610 }
4611}
4612
4613 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004614gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615{
4616 signicon_t sign, *psign;
4617 char_u *ext;
4618
4619 if (is_winnt_3())
4620 {
4621 EMSG(_(e_signdata));
4622 return NULL;
4623 }
4624
4625 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004626 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 if (ext > signfile)
4628 {
4629 int do_load = 1;
4630
4631 if (!STRICMP(ext, ".bmp"))
4632 sign.uType = IMAGE_BITMAP;
4633 else if (!STRICMP(ext, ".ico"))
4634 sign.uType = IMAGE_ICON;
4635 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
4636 sign.uType = IMAGE_CURSOR;
4637 else
4638 do_load = 0;
4639
4640 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004641 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 gui.char_width * 2, gui.char_height,
4643 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
4644#ifdef FEAT_XPM_W32
4645 if (!STRICMP(ext, ".xpm"))
4646 {
4647 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004648 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
4649 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 }
4651#endif
4652 }
4653
4654 psign = NULL;
4655 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
4656 != NULL)
4657 *psign = sign;
4658
4659 if (!psign)
4660 {
4661 if (sign.hImage)
4662 close_signicon_image(&sign);
4663 EMSG(_(e_signdata));
4664 }
4665 return (void *)psign;
4666
4667}
4668
4669 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004670gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671{
4672 if (sign)
4673 {
4674 close_signicon_image((signicon_t *)sign);
4675 vim_free(sign);
4676 }
4677}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679
4680#if defined(FEAT_BEVAL) || defined(PROTO)
4681
4682/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004683 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 *
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004685 * The only reused thing is gui_beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 * from gui_beval.c (note it uses x and y of the BalloonEval struct
4687 * to get current mouse position).
4688 *
4689 * Trying to use as more Windows services as possible, and as less
4690 * IE version as possible :)).
4691 *
4692 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
4693 * BalloonEval struct.
4694 * 2) Enable/Disable simply create/kill BalloonEval Timer
4695 * 3) When there was enough inactivity, timer procedure posts
4696 * async request to debugger
4697 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
4698 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00004699 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 */
4701
Bram Moolenaar45360022005-07-21 21:08:21 +00004702/*
4703 * determine whether installed Common Controls support multiline tooltips
4704 * (i.e. their version is >= 4.70
4705 */
4706 int
4707multiline_balloon_available(void)
4708{
4709 HINSTANCE hDll;
4710 static char comctl_dll[] = "comctl32.dll";
4711 static int multiline_tip = MAYBE;
4712
4713 if (multiline_tip != MAYBE)
4714 return multiline_tip;
4715
4716 hDll = GetModuleHandle(comctl_dll);
4717 if (hDll != NULL)
4718 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004719 DLLGETVERSIONPROC pGetVer;
4720 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00004721
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004722 if (pGetVer != NULL)
4723 {
4724 DLLVERSIONINFO dvi;
4725 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00004726
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004727 ZeroMemory(&dvi, sizeof(dvi));
4728 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00004729
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004730 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00004731
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004732 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00004733 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004734 || (dvi.dwMajorVersion == 4
4735 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00004736 {
4737 multiline_tip = TRUE;
4738 return multiline_tip;
4739 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004740 }
Bram Moolenaar45360022005-07-21 21:08:21 +00004741 else
4742 {
4743 /* there is chance we have ancient CommCtl 4.70
4744 which doesn't export DllGetVersion */
4745 DWORD dwHandle = 0;
4746 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
4747 if (len > 0)
4748 {
4749 VS_FIXEDFILEINFO *ver;
4750 UINT vlen = 0;
4751 void *data = alloc(len);
4752
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004753 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00004754 && GetFileVersionInfo(comctl_dll, 0, len, data)
4755 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
4756 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004757 && HIWORD(ver->dwFileVersionMS) > 4)
4758 || ((HIWORD(ver->dwFileVersionMS) == 4
4759 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00004760 {
4761 vim_free(data);
4762 multiline_tip = TRUE;
4763 return multiline_tip;
4764 }
4765 vim_free(data);
4766 }
4767 }
4768 }
4769 multiline_tip = FALSE;
4770 return multiline_tip;
4771}
4772
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004774make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775{
Bram Moolenaar45360022005-07-21 21:08:21 +00004776 TOOLINFO *pti;
4777 int ToolInfoSize;
4778
4779 if (multiline_balloon_available() == TRUE)
4780 ToolInfoSize = sizeof(TOOLINFO_NEW);
4781 else
4782 ToolInfoSize = sizeof(TOOLINFO);
4783
4784 pti = (TOOLINFO *)alloc(ToolInfoSize);
4785 if (pti == NULL)
4786 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787
4788 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
4789 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
4790 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4791 beval->target, NULL, s_hinst, NULL);
4792
4793 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
4794 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
4795
Bram Moolenaar45360022005-07-21 21:08:21 +00004796 pti->cbSize = ToolInfoSize;
4797 pti->uFlags = TTF_SUBCLASS;
4798 pti->hwnd = beval->target;
4799 pti->hinst = 0; /* Don't use string resources */
4800 pti->uId = ID_BEVAL_TOOLTIP;
4801
4802 if (multiline_balloon_available() == TRUE)
4803 {
4804 RECT rect;
4805 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
4806 pti->lpszText = LPSTR_TEXTCALLBACK;
4807 ptin->lParam = (LPARAM)text;
4808 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
4809 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
4810 (LPARAM)rect.right);
4811 }
4812 else
4813 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814
4815 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00004816 pti->rect.left = pt.x - 3;
4817 pti->rect.top = pt.y - 3;
4818 pti->rect.right = pt.x + 3;
4819 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820
Bram Moolenaar45360022005-07-21 21:08:21 +00004821 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 /* Make tooltip appear sooner */
4823 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00004824 /* I've performed some tests and it seems the longest possible life time
4825 * of tooltip is 30 seconds */
4826 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 /*
4828 * HACK: force tooltip to appear, because it'll not appear until
4829 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00004830 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00004832 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00004834 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835}
4836
4837 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004838delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02004840 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841}
4842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004843/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004845BevalTimerProc(
4846 HWND hwnd,
4847 UINT uMsg,
4848 UINT_PTR idEvent,
4849 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850{
4851 POINT pt;
4852 RECT rect;
4853
4854 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
4855 return;
4856
4857 GetCursorPos(&pt);
4858 if (WindowFromPoint(pt) != s_textArea)
4859 return;
4860
4861 ScreenToClient(s_textArea, &pt);
4862 GetClientRect(s_textArea, &rect);
4863 if (!PtInRect(&rect, pt))
4864 return;
4865
4866 if (LastActivity > 0
4867 && (dwTime - LastActivity) >= (DWORD)p_bdlay
4868 && (cur_beval->showState != ShS_PENDING
4869 || abs(cur_beval->x - pt.x) > 3
4870 || abs(cur_beval->y - pt.y) > 3))
4871 {
4872 /* Pointer resting in one place long enough, it's time to show
4873 * the tooltip. */
4874 cur_beval->showState = ShS_PENDING;
4875 cur_beval->x = pt.x;
4876 cur_beval->y = pt.y;
4877
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004878 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879
4880 if (cur_beval->msgCB != NULL)
4881 (*cur_beval->msgCB)(cur_beval, 0);
4882 }
4883}
4884
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004885/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004887gui_mch_disable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004889 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004891 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892}
4893
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004894/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004896gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004898 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 if (beval == NULL)
4900 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004901 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02004902 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004903 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904}
4905
4906 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004907gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908{
4909 POINT pt;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004910 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 if (beval->showState == ShS_SHOWING)
4912 return;
4913 GetCursorPos(&pt);
4914 ScreenToClient(s_textArea, &pt);
4915
4916 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
4917 /* cursor is still here */
4918 {
4919 gui_mch_disable_beval_area(cur_beval);
4920 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004921 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004923 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924}
4925
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004926/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01004928gui_mch_create_beval_area(
4929 void *target, /* ignored, always use s_textArea */
4930 char_u *mesg,
4931 void (*mesgCB)(BalloonEval *, int),
4932 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933{
4934 /* partially stolen from gui_beval.c */
4935 BalloonEval *beval;
4936
4937 if (mesg != NULL && mesgCB != NULL)
4938 {
4939 EMSG(_("E232: Cannot create BalloonEval with both message and callback"));
4940 return NULL;
4941 }
4942
4943 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
4944 if (beval != NULL)
4945 {
4946 beval->target = s_textArea;
4947 beval->balloon = NULL;
4948
4949 beval->showState = ShS_NEUTRAL;
4950 beval->x = 0;
4951 beval->y = 0;
4952 beval->msg = mesg;
4953 beval->msgCB = mesgCB;
4954 beval->clientData = clientData;
4955
4956 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 cur_beval = beval;
4958
4959 if (p_beval)
4960 gui_mch_enable_beval_area(beval);
4961
4962 }
4963 return beval;
4964}
4965
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004966/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 static void
Bram Moolenaar442b4222010-05-24 21:34:22 +02004968Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969{
4970 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
4971 return;
4972
4973 if (cur_beval != NULL)
4974 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004975 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004977 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004978 // TRACE0("TTN_SHOW {{{");
4979 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00004980 break;
4981 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004982 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 delete_tooltip(cur_beval);
4984 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004985 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986
4987 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00004988 break;
4989 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004990 {
4991 /* if you get there then we have new common controls */
4992 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
4993 info->lpszText = (LPSTR)info->lParam;
4994 info->uFlags |= TTF_DI_SETITEM;
4995 }
Bram Moolenaar45360022005-07-21 21:08:21 +00004996 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 }
4998 }
4999}
5000
5001 static void
5002TrackUserActivity(UINT uMsg)
5003{
5004 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
5005 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
5006 LastActivity = GetTickCount();
5007}
5008
5009 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005010gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011{
5012 vim_free(beval);
5013}
5014#endif /* FEAT_BEVAL */
5015
5016#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5017/*
5018 * We have multiple signs to draw at the same location. Draw the
5019 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
5020 */
5021 void
5022netbeans_draw_multisign_indicator(int row)
5023{
5024 int i;
5025 int y;
5026 int x;
5027
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005028 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005029 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005030
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 x = 0;
5032 y = TEXT_Y(row);
5033
5034 for (i = 0; i < gui.char_height - 3; i++)
5035 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
5036
5037 SetPixel(s_hdc, x+0, y, gui.currFgColor);
5038 SetPixel(s_hdc, x+2, y, gui.currFgColor);
5039 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
5040 SetPixel(s_hdc, x+1, y, gui.currFgColor);
5041 SetPixel(s_hdc, x+2, y, gui.currFgColor);
5042 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
5043 SetPixel(s_hdc, x+2, y, gui.currFgColor);
5044}
Bram Moolenaare0874f82016-01-24 20:36:41 +01005045#endif