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