blob: b42817271c362eecf922e855e089dd28b7eef232 [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
20 * os_win32.c. (It can also be done from the terminal version).
21 *
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
32#if defined(FEAT_DIRECTX) || defined(PROTO)
33static 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)
37
38 int
39directx_enabled(void)
40{
41 if (s_dwc != NULL)
42 return 1;
43 else if (s_directx_load_attempted)
44 return 0;
45 /* load DirectX */
46 DWrite_Init();
47 s_directx_load_attempted = 1;
48 s_dwc = DWriteContext_Open();
49 return s_dwc != NULL ? 1 : 0;
50}
51#endif
52
53#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
54 int
55gui_mch_set_rendering_options(char_u *s)
56{
57#ifdef FEAT_DIRECTX
58 int retval = FAIL;
59 char_u *p, *q;
60
61 int dx_enable = 0;
62 int dx_flags = 0;
63 float dx_gamma = 0.0f;
64 float dx_contrast = 0.0f;
65 float dx_level = 0.0f;
66 int dx_geom = 0;
67 int dx_renmode = 0;
68 int dx_taamode = 0;
69
70 /* parse string as rendering options. */
71 for (p = s; p != NULL && *p != NUL; )
72 {
73 char_u item[256];
74 char_u name[128];
75 char_u value[128];
76
77 copy_option_part(&p, item, sizeof(item), ",");
78 if (p == NULL)
79 break;
80 q = &item[0];
81 copy_option_part(&q, name, sizeof(name), ":");
82 if (q == NULL)
83 return FAIL;
84 copy_option_part(&q, value, sizeof(value), ":");
85
86 if (STRCMP(name, "type") == 0)
87 {
88 if (STRCMP(value, "directx") == 0)
89 dx_enable = 1;
90 else
91 return FAIL;
92 }
93 else if (STRCMP(name, "gamma") == 0)
94 {
95 dx_flags |= 1 << 0;
96 dx_gamma = (float)atof(value);
97 }
98 else if (STRCMP(name, "contrast") == 0)
99 {
100 dx_flags |= 1 << 1;
101 dx_contrast = (float)atof(value);
102 }
103 else if (STRCMP(name, "level") == 0)
104 {
105 dx_flags |= 1 << 2;
106 dx_level = (float)atof(value);
107 }
108 else if (STRCMP(name, "geom") == 0)
109 {
110 dx_flags |= 1 << 3;
111 dx_geom = atoi(value);
112 if (dx_geom < 0 || dx_geom > 2)
113 return FAIL;
114 }
115 else if (STRCMP(name, "renmode") == 0)
116 {
117 dx_flags |= 1 << 4;
118 dx_renmode = atoi(value);
119 if (dx_renmode < 0 || dx_renmode > 6)
120 return FAIL;
121 }
122 else if (STRCMP(name, "taamode") == 0)
123 {
124 dx_flags |= 1 << 5;
125 dx_taamode = atoi(value);
126 if (dx_taamode < 0 || dx_taamode > 3)
127 return FAIL;
128 }
129 else
130 return FAIL;
131 }
132
133 /* Enable DirectX/DirectWrite */
134 if (dx_enable)
135 {
136 if (!directx_enabled())
137 return FAIL;
138 DWriteContext_SetRenderingParams(s_dwc, NULL);
139 if (dx_flags)
140 {
141 DWriteRenderingParams param;
142 DWriteContext_GetRenderingParams(s_dwc, &param);
143 if (dx_flags & (1 << 0))
144 param.gamma = dx_gamma;
145 if (dx_flags & (1 << 1))
146 param.enhancedContrast = dx_contrast;
147 if (dx_flags & (1 << 2))
148 param.clearTypeLevel = dx_level;
149 if (dx_flags & (1 << 3))
150 param.pixelGeometry = dx_geom;
151 if (dx_flags & (1 << 4))
152 param.renderingMode = dx_renmode;
153 if (dx_flags & (1 << 5))
154 param.textAntialiasMode = dx_taamode;
155 DWriteContext_SetRenderingParams(s_dwc, &param);
156 }
157 }
158 s_directx_enabled = dx_enable;
159
160 return OK;
161#else
162 return FAIL;
163#endif
164}
165#endif
166
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167/*
168 * These are new in Windows ME/XP, only defined in recent compilers.
169 */
170#ifndef HANDLE_WM_XBUTTONUP
171# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
172 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
173#endif
174#ifndef HANDLE_WM_XBUTTONDOWN
175# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
176 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
177#endif
178#ifndef HANDLE_WM_XBUTTONDBLCLK
179# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
180 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
181#endif
182
183/*
184 * Include the common stuff for MS-Windows GUI.
185 */
186#include "gui_w48.c"
187
188#ifdef FEAT_XPM_W32
189# include "xpm_w32.h"
190#endif
191
192#ifdef PROTO
193# define WINAPI
194#endif
195
196#ifdef __MINGW32__
197/*
198 * Add a lot of missing defines.
199 * They are not always missing, we need the #ifndef's.
200 */
201# ifndef _cdecl
202# define _cdecl
203# endif
204# ifndef IsMinimized
205# define IsMinimized(hwnd) IsIconic(hwnd)
206# endif
207# ifndef IsMaximized
208# define IsMaximized(hwnd) IsZoomed(hwnd)
209# endif
210# ifndef SelectFont
211# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
212# endif
213# ifndef GetStockBrush
214# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
215# endif
216# ifndef DeleteBrush
217# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
218# endif
219
220# ifndef HANDLE_WM_RBUTTONDBLCLK
221# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
222 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
223# endif
224# ifndef HANDLE_WM_MBUTTONUP
225# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
226 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
227# endif
228# ifndef HANDLE_WM_MBUTTONDBLCLK
229# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
230 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
231# endif
232# ifndef HANDLE_WM_LBUTTONDBLCLK
233# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
234 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
235# endif
236# ifndef HANDLE_WM_RBUTTONDOWN
237# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
238 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
239# endif
240# ifndef HANDLE_WM_MOUSEMOVE
241# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
242 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
243# endif
244# ifndef HANDLE_WM_RBUTTONUP
245# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
246 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
247# endif
248# ifndef HANDLE_WM_MBUTTONDOWN
249# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
250 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
251# endif
252# ifndef HANDLE_WM_LBUTTONUP
253# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
254 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
255# endif
256# ifndef HANDLE_WM_LBUTTONDOWN
257# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
258 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
259# endif
260# ifndef HANDLE_WM_SYSCHAR
261# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
262 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
263# endif
264# ifndef HANDLE_WM_ACTIVATEAPP
265# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
266 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
267# endif
268# ifndef HANDLE_WM_WINDOWPOSCHANGING
269# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
270 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
271# endif
272# ifndef HANDLE_WM_VSCROLL
273# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
274 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
275# endif
276# ifndef HANDLE_WM_SETFOCUS
277# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
278 ((fn)((hwnd), (HWND)(wParam)), 0L)
279# endif
280# ifndef HANDLE_WM_KILLFOCUS
281# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
282 ((fn)((hwnd), (HWND)(wParam)), 0L)
283# endif
284# ifndef HANDLE_WM_HSCROLL
285# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
286 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
287# endif
288# ifndef HANDLE_WM_DROPFILES
289# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
290 ((fn)((hwnd), (HDROP)(wParam)), 0L)
291# endif
292# ifndef HANDLE_WM_CHAR
293# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
294 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
295# endif
296# ifndef HANDLE_WM_SYSDEADCHAR
297# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
298 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
299# endif
300# ifndef HANDLE_WM_DEADCHAR
301# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
302 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
303# endif
304#endif /* __MINGW32__ */
305
306
307/* Some parameters for tearoff menus. All in pixels. */
308#define TEAROFF_PADDING_X 2
309#define TEAROFF_BUTTON_PAD_X 8
310#define TEAROFF_MIN_WIDTH 200
311#define TEAROFF_SUBMENU_LABEL ">>"
312#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
313
314
315/* For the Intellimouse: */
316#ifndef WM_MOUSEWHEEL
317#define WM_MOUSEWHEEL 0x20a
318#endif
319
320
321#ifdef FEAT_BEVAL
322# define ID_BEVAL_TOOLTIP 200
323# define BEVAL_TEXT_LEN MAXPATHL
324
Bram Moolenaar167632f2010-05-26 21:42:54 +0200325#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +0000326/* Work around old versions of basetsd.h which wrongly declares
327 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +0200328# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +0000329# define UINT_PTR UINT
330#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000331
332static void make_tooltip __ARGS((BalloonEval *beval, char *text, POINT pt));
333static void delete_tooltip __ARGS((BalloonEval *beval));
334static VOID CALLBACK BevalTimerProc __ARGS((HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime));
335
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000337static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +0000339
Bram Moolenaar82881492012-11-20 16:53:39 +0100340
341/* cproto fails on missing include files */
342#ifndef PROTO
343
Bram Moolenaar45360022005-07-21 21:08:21 +0000344/*
345 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000346 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +0000347 */
Bram Moolenaar82881492012-11-20 16:53:39 +0100348# include <pshpack1.h>
349
350#endif
Bram Moolenaar45360022005-07-21 21:08:21 +0000351
352typedef struct _DllVersionInfo
353{
354 DWORD cbSize;
355 DWORD dwMajorVersion;
356 DWORD dwMinorVersion;
357 DWORD dwBuildNumber;
358 DWORD dwPlatformID;
359} DLLVERSIONINFO;
360
Bram Moolenaar82881492012-11-20 16:53:39 +0100361#ifndef PROTO
362# include <poppack.h>
363#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +0000364
Bram Moolenaar45360022005-07-21 21:08:21 +0000365typedef struct tagTOOLINFOA_NEW
366{
367 UINT cbSize;
368 UINT uFlags;
369 HWND hwnd;
Bram Moolenaar281daf62009-12-24 15:11:40 +0000370 UINT_PTR uId;
Bram Moolenaar45360022005-07-21 21:08:21 +0000371 RECT rect;
372 HINSTANCE hinst;
373 LPSTR lpszText;
374 LPARAM lParam;
375} TOOLINFO_NEW;
376
377typedef struct tagNMTTDISPINFO_NEW
378{
379 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +0000380 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +0000381 char szText[80];
382 HINSTANCE hinst;
383 UINT uFlags;
384 LPARAM lParam;
385} NMTTDISPINFO_NEW;
386
Bram Moolenaar45360022005-07-21 21:08:21 +0000387typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
388#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000389# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390#endif
391
Bram Moolenaar45360022005-07-21 21:08:21 +0000392#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000393# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +0000394#endif
395
396#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000397# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +0000398#endif
399
400#endif /* defined(FEAT_BEVAL) */
401
Bram Moolenaar2c7a7632007-05-10 18:19:11 +0000402#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
403/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
404 * it here if LPNMTTDISPINFO isn't defined.
405 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
406 * _MSC_VER. */
407# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
408typedef struct tagNMTTDISPINFOA {
409 NMHDR hdr;
410 LPSTR lpszText;
411 char szText[80];
412 HINSTANCE hinst;
413 UINT uFlags;
414 LPARAM lParam;
415} NMTTDISPINFOA, *LPNMTTDISPINFOA;
416# define LPNMTTDISPINFO LPNMTTDISPINFOA
417
418# ifdef FEAT_MBYTE
419typedef struct tagNMTTDISPINFOW {
420 NMHDR hdr;
421 LPWSTR lpszText;
422 WCHAR szText[80];
423 HINSTANCE hinst;
424 UINT uFlags;
425 LPARAM lParam;
426} NMTTDISPINFOW, *LPNMTTDISPINFOW;
427# endif
428# endif
429#endif
430
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000431#ifndef TTN_GETDISPINFOW
432# define TTN_GETDISPINFOW (TTN_FIRST - 10)
433#endif
434
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435/* Local variables: */
436
437#ifdef FEAT_MENU
438static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +0200439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440
441/*
442 * Use the system font for dialogs and tear-off menus. Remove this line to
443 * use DLG_FONT_NAME.
444 */
Bram Moolenaar786989b2010-10-27 12:15:33 +0200445#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
447#define VIM_NAME "vim"
448#define VIM_CLASS "Vim"
449#define VIM_CLASSW L"Vim"
450
451/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
452 * thus there should be room for every dialog. For tearoffs it's made bigger
453 * when needed. */
454#define DLG_ALLOC_SIZE 16 * 1024
455
456/*
457 * stuff for dialogs, menus, tearoffs etc.
458 */
459static LRESULT APIENTRY dialog_callback(HWND, UINT, WPARAM, LPARAM);
460static LRESULT APIENTRY tearoff_callback(HWND, UINT, WPARAM, LPARAM);
461static PWORD
462add_dialog_element(
463 PWORD p,
464 DWORD lStyle,
465 WORD x,
466 WORD y,
467 WORD w,
468 WORD h,
469 WORD Id,
470 WORD clss,
471 const char *caption);
472static LPWORD lpwAlign(LPWORD);
473static int nCopyAnsiToWideChar(LPWORD, LPSTR);
474static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
475static void get_dialog_font_metrics(void);
476
477static int dialog_default_button = -1;
478
479/* Intellimouse support */
480static int mouse_scroll_lines = 0;
481static UINT msh_msgmousewheel = 0;
482
483static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
484#ifdef FEAT_TOOLBAR
485static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +0200486static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487static int get_toolbar_bitmap(vimmenu_T *menu);
488#endif
489
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000490#ifdef FEAT_GUI_TABLINE
491static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +0200492static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000493#endif
494
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495#ifdef FEAT_MBYTE_IME
496static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
497static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
498#endif
499#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
500# ifdef NOIME
501typedef struct tagCOMPOSITIONFORM {
502 DWORD dwStyle;
503 POINT ptCurrentPos;
504 RECT rcArea;
505} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
506typedef HANDLE HIMC;
507# endif
508
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000509static HINSTANCE hLibImm = NULL;
510static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
511static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
512static HIMC (WINAPI *pImmGetContext)(HWND);
513static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
514static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
515static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
516static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
517static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
518static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
519static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
520static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +0000521static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522static void dyn_imm_load(void);
523#else
524# define pImmGetCompositionStringA ImmGetCompositionStringA
525# define pImmGetCompositionStringW ImmGetCompositionStringW
526# define pImmGetContext ImmGetContext
527# define pImmAssociateContext ImmAssociateContext
528# define pImmReleaseContext ImmReleaseContext
529# define pImmGetOpenStatus ImmGetOpenStatus
530# define pImmSetOpenStatus ImmSetOpenStatus
531# define pImmGetCompositionFont ImmGetCompositionFontA
532# define pImmSetCompositionFont ImmSetCompositionFontA
533# define pImmSetCompositionWindow ImmSetCompositionWindow
534# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +0000535# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536#endif
537
538#ifndef ETO_IGNORELANGUAGE
539# define ETO_IGNORELANGUAGE 0x1000
540#endif
541
542/* multi monitor support */
543typedef struct _MONITORINFOstruct
544{
545 DWORD cbSize;
546 RECT rcMonitor;
547 RECT rcWork;
548 DWORD dwFlags;
549} _MONITORINFO;
550
551typedef HANDLE _HMONITOR;
552typedef _HMONITOR (WINAPI *TMonitorFromWindow)(HWND, DWORD);
553typedef BOOL (WINAPI *TGetMonitorInfo)(_HMONITOR, _MONITORINFO *);
554
555static TMonitorFromWindow pMonitorFromWindow = NULL;
556static TGetMonitorInfo pGetMonitorInfo = NULL;
557static HANDLE user32_lib = NULL;
558#ifdef FEAT_NETBEANS_INTG
559int WSInitialized = FALSE; /* WinSock is initialized */
560#endif
561/*
562 * Return TRUE when running under Windows NT 3.x or Win32s, both of which have
563 * less fancy GUI APIs.
564 */
565 static int
566is_winnt_3(void)
567{
568 return ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
569 && os_version.dwMajorVersion == 3)
570 || (os_version.dwPlatformId == VER_PLATFORM_WIN32s));
571}
572
573/*
574 * Return TRUE when running under Win32s.
575 */
576 int
577gui_is_win32s(void)
578{
579 return (os_version.dwPlatformId == VER_PLATFORM_WIN32s);
580}
581
582#ifdef FEAT_MENU
583/*
584 * Figure out how high the menu bar is at the moment.
585 */
586 static int
587gui_mswin_get_menu_height(
588 int fix_window) /* If TRUE, resize window if menu height changed */
589{
590 static int old_menu_height = -1;
591
592 RECT rc1, rc2;
593 int num;
594 int menu_height;
595
596 if (gui.menu_is_active)
597 num = GetMenuItemCount(s_menuBar);
598 else
599 num = 0;
600
601 if (num == 0)
602 menu_height = 0;
603 else
604 {
605 if (is_winnt_3()) /* for NT 3.xx */
606 {
607 if (gui.starting)
608 menu_height = GetSystemMetrics(SM_CYMENU);
609 else
610 {
611 RECT r1, r2;
612 int frameht = GetSystemMetrics(SM_CYFRAME);
613 int capht = GetSystemMetrics(SM_CYCAPTION);
614
615 /* get window rect of s_hwnd
616 * get client rect of s_hwnd
617 * get cap height
618 * subtract from window rect, the sum of client height,
619 * (if not maximized)frame thickness, and caption height.
620 */
621 GetWindowRect(s_hwnd, &r1);
622 GetClientRect(s_hwnd, &r2);
623 menu_height = r1.bottom - r1.top - (r2.bottom - r2.top
624 + 2 * frameht * (!IsZoomed(s_hwnd)) + capht);
625 }
626 }
627 else /* win95 and variants (NT 4.0, I guess) */
628 {
629 /*
630 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
631 * seem to have been set yet, so menu wraps in default window
632 * width which is very narrow. Instead just return height of a
633 * single menu item. Will still be wrong when the menu really
634 * should wrap over more than one line.
635 */
636 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
637 if (gui.starting)
638 menu_height = rc1.bottom - rc1.top + 1;
639 else
640 {
641 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
642 menu_height = rc2.bottom - rc1.top + 1;
643 }
644 }
645 }
646
647 if (fix_window && menu_height != old_menu_height)
648 {
649 old_menu_height = menu_height;
Bram Moolenaarafa24992006-03-27 20:58:26 +0000650 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 }
652
653 return menu_height;
654}
655#endif /*FEAT_MENU*/
656
657
658/*
659 * Setup for the Intellimouse
660 */
661 static void
662init_mouse_wheel(void)
663{
664
665#ifndef SPI_GETWHEELSCROLLLINES
666# define SPI_GETWHEELSCROLLLINES 104
667#endif
Bram Moolenaare7566042005-06-17 22:00:15 +0000668#ifndef SPI_SETWHEELSCROLLLINES
669# define SPI_SETWHEELSCROLLLINES 105
670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671
672#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
673#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
674#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
675#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
676
677 HWND hdl_mswheel;
678 UINT msh_msgscrolllines;
679
680 msh_msgmousewheel = 0;
681 mouse_scroll_lines = 3; /* reasonable default */
682
683 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
684 && os_version.dwMajorVersion >= 4)
685 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
686 && ((os_version.dwMajorVersion == 4
687 && os_version.dwMinorVersion >= 10)
688 || os_version.dwMajorVersion >= 5)))
689 {
690 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
691 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
692 &mouse_scroll_lines, 0);
693 }
694 else if (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
695 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
696 && os_version.dwMajorVersion < 4))
697 { /*
698 * If Win95 or NT 3.51,
699 * try to find the hidden point32 window.
700 */
701 hdl_mswheel = FindWindow(VMOUSEZ_CLASSNAME, VMOUSEZ_TITLE);
702 if (hdl_mswheel)
703 {
704 msh_msgscrolllines = RegisterWindowMessage(VMSH_SCROLL_LINES);
705 if (msh_msgscrolllines)
706 {
707 mouse_scroll_lines = (int)SendMessage(hdl_mswheel,
708 msh_msgscrolllines, 0, 0);
709 msh_msgmousewheel = RegisterWindowMessage(VMSH_MOUSEWHEEL);
710 }
711 }
712 }
713}
714
715
716/* Intellimouse wheel handler */
717 static void
718_OnMouseWheel(
719 HWND hwnd,
720 short zDelta)
721{
722/* Treat a mouse wheel event as if it were a scroll request */
723 int i;
724 int size;
725 HWND hwndCtl;
726
727 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
728 {
729 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
730 size = curwin->w_scrollbars[SBAR_RIGHT].size;
731 }
732 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
733 {
734 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
735 size = curwin->w_scrollbars[SBAR_LEFT].size;
736 }
737 else
738 return;
739
740 size = curwin->w_height;
741 if (mouse_scroll_lines == 0)
742 init_mouse_wheel();
743
744 if (mouse_scroll_lines > 0
745 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
746 {
747 for (i = mouse_scroll_lines; i > 0; --i)
748 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
749 }
750 else
751 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
752}
753
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000754#ifdef USE_SYSMENU_FONT
755/*
756 * Get Menu Font.
757 * Return OK or FAIL.
758 */
759 static int
760gui_w32_get_menu_font(LOGFONT *lf)
761{
762 NONCLIENTMETRICS nm;
763
764 nm.cbSize = sizeof(NONCLIENTMETRICS);
765 if (!SystemParametersInfo(
766 SPI_GETNONCLIENTMETRICS,
767 sizeof(NONCLIENTMETRICS),
768 &nm,
769 0))
770 return FAIL;
771 *lf = nm.lfMenuFont;
772 return OK;
773}
774#endif
775
776
777#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
778/*
779 * Set the GUI tabline font to the system menu font
780 */
781 static void
782set_tabline_font(void)
783{
784 LOGFONT lfSysmenu;
785 HFONT font;
786 HWND hwnd;
787 HDC hdc;
788 HFONT hfntOld;
789 TEXTMETRIC tm;
790
791 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
792 return;
793
794 font = CreateFontIndirect(&lfSysmenu);
795
796 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
797
798 /*
799 * Compute the height of the font used for the tab text
800 */
801 hwnd = GetDesktopWindow();
802 hdc = GetWindowDC(hwnd);
803 hfntOld = SelectFont(hdc, font);
804
805 GetTextMetrics(hdc, &tm);
806
807 SelectFont(hdc, hfntOld);
808 ReleaseDC(hwnd, hdc);
809
810 /*
811 * The space used by the tab border and the space between the tab label
812 * and the tab border is included as 7.
813 */
814 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
815}
816#endif
817
Bram Moolenaar520470a2005-06-16 21:59:56 +0000818/*
819 * Invoked when a setting was changed.
820 */
821 static LRESULT CALLBACK
822_OnSettingChange(UINT n)
823{
824 if (n == SPI_SETWHEELSCROLLLINES)
825 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
826 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000827#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
828 if (n == SPI_SETNONCLIENTMETRICS)
829 set_tabline_font();
830#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +0000831 return 0;
832}
833
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834#ifdef FEAT_NETBEANS_INTG
835 static void
836_OnWindowPosChanged(
837 HWND hwnd,
838 const LPWINDOWPOS lpwpos)
839{
840 static int x = 0, y = 0, cx = 0, cy = 0;
841
842 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
843 || lpwpos->cx != cx || lpwpos->cy != cy))
844 {
845 x = lpwpos->x;
846 y = lpwpos->y;
847 cx = lpwpos->cx;
848 cy = lpwpos->cy;
849 netbeans_frame_moved(x, y);
850 }
851 /* Allow to send WM_SIZE and WM_MOVE */
852 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
853}
854#endif
855
856 static int
857_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 UINT fwSide,
859 LPRECT lprc)
860{
861 int w, h;
862 int valid_w, valid_h;
863 int w_offset, h_offset;
864
865 w = lprc->right - lprc->left;
866 h = lprc->bottom - lprc->top;
867 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
868 w_offset = w - valid_w;
869 h_offset = h - valid_h;
870
871 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
872 || fwSide == WMSZ_BOTTOMLEFT)
873 lprc->left += w_offset;
874 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
875 || fwSide == WMSZ_BOTTOMRIGHT)
876 lprc->right -= w_offset;
877
878 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
879 || fwSide == WMSZ_TOPRIGHT)
880 lprc->top += h_offset;
881 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
882 || fwSide == WMSZ_BOTTOMRIGHT)
883 lprc->bottom -= h_offset;
884 return TRUE;
885}
886
887
888
889 static LRESULT CALLBACK
890_WndProc(
891 HWND hwnd,
892 UINT uMsg,
893 WPARAM wParam,
894 LPARAM lParam)
895{
896 /*
897 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
898 hwnd, uMsg, wParam, lParam);
899 */
900
901 HandleMouseHide(uMsg, lParam);
902
903 s_uMsg = uMsg;
904 s_wParam = wParam;
905 s_lParam = lParam;
906
907 switch (uMsg)
908 {
909 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
910 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
911 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
912 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
913 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
914 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
915 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
916 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
917 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
918#ifdef FEAT_MENU
919 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
920#endif
921 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
922 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
923 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
924 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
925 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
926 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
927 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
928 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
929 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
930#ifdef FEAT_NETBEANS_INTG
931 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
932#endif
933
Bram Moolenaarafa24992006-03-27 20:58:26 +0000934#ifdef FEAT_GUI_TABLINE
935 case WM_RBUTTONUP:
936 {
937 if (gui_mch_showing_tabline())
938 {
939 POINT pt;
940 RECT rect;
941
942 /*
943 * If the cursor is on the tabline, display the tab menu
944 */
945 GetCursorPos((LPPOINT)&pt);
946 GetWindowRect(s_textArea, &rect);
947 if (pt.y < rect.top)
948 {
949 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +0100950 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +0000951 }
952 }
953 return MyWindowProc(hwnd, uMsg, wParam, lParam);
954 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000955 case WM_LBUTTONDBLCLK:
956 {
957 /*
958 * If the user double clicked the tabline, create a new tab
959 */
960 if (gui_mch_showing_tabline())
961 {
962 POINT pt;
963 RECT rect;
964
965 GetCursorPos((LPPOINT)&pt);
966 GetWindowRect(s_textArea, &rect);
967 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000968 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000969 }
970 return MyWindowProc(hwnd, uMsg, wParam, lParam);
971 }
Bram Moolenaarafa24992006-03-27 20:58:26 +0000972#endif
973
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 case WM_QUERYENDSESSION: /* System wants to go down. */
975 gui_shell_closed(); /* Will exit when no changed buffers. */
976 return FALSE; /* Do NOT allow system to go down. */
977
978 case WM_ENDSESSION:
979 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +0100980 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +0100982 return 0L;
983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 break;
985
986 case WM_CHAR:
987 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
988 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000989 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 return 0L;
991
992 case WM_SYSCHAR:
993 /*
994 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
995 * shortcut key, handle like a typed ALT key, otherwise call Windows
996 * ALT key handling.
997 */
998#ifdef FEAT_MENU
999 if ( !gui.menu_is_active
1000 || p_wak[0] == 'n'
1001 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
1002 )
1003#endif
1004 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001005 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 return 0L;
1007 }
1008#ifdef FEAT_MENU
1009 else
1010 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1011#endif
1012
1013 case WM_SYSKEYUP:
1014#ifdef FEAT_MENU
1015 /* This used to be done only when menu is active: ALT key is used for
1016 * that. But that caused problems when menu is disabled and using
1017 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
1018 * are received, mouse pointer remains hidden. */
1019 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1020#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01001021 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022#endif
1023
1024 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001025 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026
1027 case WM_MOUSEWHEEL:
1028 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01001029 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030
Bram Moolenaar520470a2005-06-16 21:59:56 +00001031 /* Notification for change in SystemParametersInfo() */
1032 case WM_SETTINGCHANGE:
1033 return _OnSettingChange((UINT)wParam);
1034
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001035#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 case WM_NOTIFY:
1037 switch (((LPNMHDR) lParam)->code)
1038 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001039# ifdef FEAT_MBYTE
1040 case TTN_GETDISPINFOW:
1041# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001042 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001044 LPNMHDR hdr = (LPNMHDR)lParam;
1045 char_u *str = NULL;
1046 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001047
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001048 vim_free(tt_text);
1049 tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001050
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001051# ifdef FEAT_GUI_TABLINE
1052 if (gui_mch_showing_tabline()
1053 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001054 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001055 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001056 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001057 * Mouse is over the GUI tabline. Display the
1058 * tooltip for the tab under the cursor
1059 *
1060 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001061 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001062 GetCursorPos(&pt);
1063 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001064 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001065 TCHITTESTINFO htinfo;
1066 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001067
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001068 /*
1069 * Get the tab under the cursor
1070 */
1071 htinfo.pt.x = pt.x;
1072 htinfo.pt.y = pt.y;
1073 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
1074 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001075 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001076 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001077
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001078 tp = find_tabpage(idx + 1);
1079 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001080 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001081 get_tabline_label(tp, TRUE);
1082 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001083 }
1084 }
1085 }
1086 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001087# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001088# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001089# ifdef FEAT_GUI_TABLINE
1090 else
1091# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001093 UINT idButton;
1094 vimmenu_T *pMenu;
1095
1096 idButton = (UINT) hdr->idFrom;
1097 pMenu = gui_mswin_find_menu(root_menu, idButton);
1098 if (pMenu)
1099 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001101# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001102 if (str != NULL)
1103 {
1104# ifdef FEAT_MBYTE
1105 if (hdr->code == TTN_GETDISPINFOW)
1106 {
1107 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
1108
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00001109 /* Set the maximum width, this also enables using
1110 * \n for line break. */
1111 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
1112 0, 500);
1113
Bram Moolenaar36f692d2008-11-20 16:10:17 +00001114 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001115 lpdi->lpszText = tt_text;
1116 /* can't show tooltip if failed */
1117 }
1118 else
1119# endif
1120 {
1121 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
1122
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00001123 /* Set the maximum width, this also enables using
1124 * \n for line break. */
1125 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
1126 0, 500);
1127
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00001128 if (STRLEN(str) < sizeof(lpdi->szText)
1129 || ((tt_text = vim_strsave(str)) == NULL))
1130 vim_strncpy(lpdi->szText, str,
1131 sizeof(lpdi->szText) - 1);
1132 else
1133 lpdi->lpszText = tt_text;
1134 }
1135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 }
1137 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001138# ifdef FEAT_GUI_TABLINE
1139 case TCN_SELCHANGE:
1140 if (gui_mch_showing_tabline()
1141 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01001142 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001143 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01001144 return 0L;
1145 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001146 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00001147
1148 case NM_RCLICK:
1149 if (gui_mch_showing_tabline()
1150 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01001151 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00001152 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01001153 return 0L;
1154 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00001155 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001156# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001158# ifdef FEAT_GUI_TABLINE
1159 if (gui_mch_showing_tabline()
1160 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
1161 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1162# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 break;
1164 }
1165 break;
1166#endif
1167#if defined(MENUHINTS) && defined(FEAT_MENU)
1168 case WM_MENUSELECT:
1169 if (((UINT) HIWORD(wParam)
1170 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
1171 == MF_HILITE
1172 && (State & CMDLINE) == 0)
1173 {
1174 UINT idButton;
1175 vimmenu_T *pMenu;
1176 static int did_menu_tip = FALSE;
1177
1178 if (did_menu_tip)
1179 {
1180 msg_clr_cmdline();
1181 setcursor();
1182 out_flush();
1183 did_menu_tip = FALSE;
1184 }
1185
1186 idButton = (UINT)LOWORD(wParam);
1187 pMenu = gui_mswin_find_menu(root_menu, idButton);
1188 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
1189 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
1190 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00001191 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00001193 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 setcursor();
1195 out_flush();
1196 did_menu_tip = TRUE;
1197 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01001198 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 }
1200 break;
1201#endif
1202 case WM_NCHITTEST:
1203 {
1204 LRESULT result;
1205 int x, y;
1206 int xPos = GET_X_LPARAM(lParam);
1207
1208 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
1209 if (result == HTCLIENT)
1210 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001211#ifdef FEAT_GUI_TABLINE
1212 if (gui_mch_showing_tabline())
1213 {
1214 int yPos = GET_Y_LPARAM(lParam);
1215 RECT rct;
1216
1217 /* If the cursor is on the GUI tabline, don't process this
1218 * event */
1219 GetWindowRect(s_textArea, &rct);
1220 if (yPos < rct.top)
1221 return result;
1222 }
1223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 gui_mch_get_winpos(&x, &y);
1225 xPos -= x;
1226
1227 if (xPos < 48) /* <VN> TODO should use system metric? */
1228 return HTBOTTOMLEFT;
1229 else
1230 return HTBOTTOMRIGHT;
1231 }
1232 else
1233 return result;
1234 }
1235 /* break; notreached */
1236
1237#ifdef FEAT_MBYTE_IME
1238 case WM_IME_NOTIFY:
1239 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
1240 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01001241 return 1L;
1242
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 case WM_IME_COMPOSITION:
1244 if (!_OnImeComposition(hwnd, wParam, lParam))
1245 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01001246 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247#endif
1248
1249 default:
1250 if (uMsg == msh_msgmousewheel && msh_msgmousewheel != 0)
1251 { /* handle MSH_MOUSEWHEEL messages for Intellimouse */
1252 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01001253 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 }
1255#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001256 else if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 {
1258 _OnFindRepl();
1259 }
1260#endif
1261 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1262 }
1263
Bram Moolenaar2787ab92011-12-14 15:23:59 +01001264 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265}
1266
1267/*
1268 * End of call-back routines
1269 */
1270
1271/* parent window, if specified with -P */
1272HWND vim_parent_hwnd = NULL;
1273
1274 static BOOL CALLBACK
1275FindWindowTitle(HWND hwnd, LPARAM lParam)
1276{
1277 char buf[2048];
1278 char *title = (char *)lParam;
1279
1280 if (GetWindowText(hwnd, buf, sizeof(buf)))
1281 {
1282 if (strstr(buf, title) != NULL)
1283 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001284 /* Found it. Store the window ref. and quit searching if MDI
1285 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001287 if (vim_parent_hwnd != NULL)
1288 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
1290 }
1291 return TRUE; /* continue searching */
1292}
1293
1294/*
1295 * Invoked for '-P "title"' argument: search for parent application to open
1296 * our window in.
1297 */
1298 void
1299gui_mch_set_parent(char *title)
1300{
1301 EnumWindows(FindWindowTitle, (LPARAM)title);
1302 if (vim_parent_hwnd == NULL)
1303 {
1304 EMSG2(_("E671: Cannot find window title \"%s\""), title);
1305 mch_exit(2);
1306 }
1307}
1308
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001309#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 static void
1311ole_error(char *arg)
1312{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00001313 char buf[IOSIZE];
1314
1315 /* Can't use EMSG() here, we have not finished initialisation yet. */
1316 vim_snprintf(buf, IOSIZE,
1317 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
1318 arg);
1319 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322
1323/*
1324 * Parse the GUI related command-line arguments. Any arguments used are
1325 * deleted from argv, and *argc is decremented accordingly. This is called
1326 * when vim is started, whether or not the GUI has been started.
1327 */
1328 void
1329gui_mch_prepare(int *argc, char **argv)
1330{
1331 int silent = FALSE;
1332 int idx;
1333
1334 /* Check for special OLE command line parameters */
1335 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
1336 {
1337 /* Check for a "-silent" argument first. */
1338 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
1339 && (argv[2][0] == '-' || argv[2][0] == '/'))
1340 {
1341 silent = TRUE;
1342 idx = 2;
1343 }
1344 else
1345 idx = 1;
1346
1347 /* Register Vim as an OLE Automation server */
1348 if (STRICMP(argv[idx] + 1, "register") == 0)
1349 {
1350#ifdef FEAT_OLE
1351 RegisterMe(silent);
1352 mch_exit(0);
1353#else
1354 if (!silent)
1355 ole_error("register");
1356 mch_exit(2);
1357#endif
1358 }
1359
1360 /* Unregister Vim as an OLE Automation server */
1361 if (STRICMP(argv[idx] + 1, "unregister") == 0)
1362 {
1363#ifdef FEAT_OLE
1364 UnregisterMe(!silent);
1365 mch_exit(0);
1366#else
1367 if (!silent)
1368 ole_error("unregister");
1369 mch_exit(2);
1370#endif
1371 }
1372
1373 /* Ignore an -embedding argument. It is only relevant if the
1374 * application wants to treat the case when it is started manually
1375 * differently from the case where it is started via automation (and
1376 * we don't).
1377 */
1378 if (STRICMP(argv[idx] + 1, "embedding") == 0)
1379 {
1380#ifdef FEAT_OLE
1381 *argc = 1;
1382#else
1383 ole_error("embedding");
1384 mch_exit(2);
1385#endif
1386 }
1387 }
1388
1389#ifdef FEAT_OLE
1390 {
1391 int bDoRestart = FALSE;
1392
1393 InitOLE(&bDoRestart);
1394 /* automatically exit after registering */
1395 if (bDoRestart)
1396 mch_exit(0);
1397 }
1398#endif
1399
1400#ifdef FEAT_NETBEANS_INTG
1401 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001402 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 int arg;
1404
1405 for (arg = 1; arg < *argc; arg++)
1406 if (strncmp("-nb", argv[arg], 3) == 0)
1407 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 netbeansArg = argv[arg];
1409 mch_memmove(&argv[arg], &argv[arg + 1],
1410 (--*argc - arg) * sizeof(char *));
1411 argv[*argc] = NULL;
1412 break; /* enough? */
1413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 }
1415#endif
1416
1417 /* get the OS version info */
1418 os_version.dwOSVersionInfoSize = sizeof(os_version);
1419 GetVersionEx(&os_version); /* this call works on Win32s, Win95 and WinNT */
1420
1421 /* try and load the user32.dll library and get the entry points for
1422 * multi-monitor-support. */
Bram Moolenaarebbcb822010-10-23 14:02:54 +02001423 if ((user32_lib = vimLoadLib("User32.dll")) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 {
1425 pMonitorFromWindow = (TMonitorFromWindow)GetProcAddress(user32_lib,
1426 "MonitorFromWindow");
1427
1428 /* there are ...A and ...W version of GetMonitorInfo - looking at
1429 * winuser.h, they have exactly the same declaration. */
1430 pGetMonitorInfo = (TGetMonitorInfo)GetProcAddress(user32_lib,
1431 "GetMonitorInfoA");
1432 }
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001433
1434#ifdef FEAT_MBYTE
1435 /* If the OS is Windows NT, use wide functions;
1436 * this enables common dialogs input unicode from IME. */
1437 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
1438 {
1439 pDispatchMessage = DispatchMessageW;
1440 pGetMessage = GetMessageW;
1441 pIsDialogMessage = IsDialogMessageW;
1442 pPeekMessage = PeekMessageW;
1443 }
1444 else
1445 {
1446 pDispatchMessage = DispatchMessageA;
1447 pGetMessage = GetMessageA;
1448 pIsDialogMessage = IsDialogMessageA;
1449 pPeekMessage = PeekMessageA;
1450 }
1451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452}
1453
1454/*
1455 * Initialise the GUI. Create all the windows, set up all the call-backs
1456 * etc.
1457 */
1458 int
1459gui_mch_init(void)
1460{
1461 const char szVimWndClass[] = VIM_CLASS;
1462 const char szTextAreaClass[] = "VimTextArea";
1463 WNDCLASS wndclass;
1464#ifdef FEAT_MBYTE
1465 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01001466 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 WNDCLASSW wndclassw;
1468#endif
1469#ifdef GLOBAL_IME
1470 ATOM atom;
1471#endif
1472
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 /* Return here if the window was already opened (happens when
1474 * gui_mch_dialog() is called early). */
1475 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00001476 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477
1478 /*
1479 * Load the tearoff bitmap
1480 */
1481#ifdef FEAT_TEAROFF
1482 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
1483#endif
1484
1485 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
1486 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
1487#ifdef FEAT_MENU
1488 gui.menu_height = 0; /* Windows takes care of this */
1489#endif
1490 gui.border_width = 0;
1491
1492 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
1493
1494#ifdef FEAT_MBYTE
1495 /* First try using the wide version, so that we can use any title.
1496 * Otherwise only characters in the active codepage will work. */
1497 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
1498 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001499 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 wndclassw.lpfnWndProc = _WndProc;
1501 wndclassw.cbClsExtra = 0;
1502 wndclassw.cbWndExtra = 0;
1503 wndclassw.hInstance = s_hinst;
1504 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
1505 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
1506 wndclassw.hbrBackground = s_brush;
1507 wndclassw.lpszMenuName = NULL;
1508 wndclassw.lpszClassName = szVimWndClassW;
1509
1510 if ((
1511#ifdef GLOBAL_IME
1512 atom =
1513#endif
1514 RegisterClassW(&wndclassw)) == 0)
1515 {
1516 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1517 return FAIL;
1518
1519 /* Must be Windows 98, fall back to non-wide function. */
1520 }
1521 else
1522 wide_WindowProc = TRUE;
1523 }
1524
1525 if (!wide_WindowProc)
1526#endif
1527
1528 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
1529 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001530 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 wndclass.lpfnWndProc = _WndProc;
1532 wndclass.cbClsExtra = 0;
1533 wndclass.cbWndExtra = 0;
1534 wndclass.hInstance = s_hinst;
1535 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
1536 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1537 wndclass.hbrBackground = s_brush;
1538 wndclass.lpszMenuName = NULL;
1539 wndclass.lpszClassName = szVimWndClass;
1540
1541 if ((
1542#ifdef GLOBAL_IME
1543 atom =
1544#endif
1545 RegisterClass(&wndclass)) == 0)
1546 return FAIL;
1547 }
1548
1549 if (vim_parent_hwnd != NULL)
1550 {
1551#ifdef HAVE_TRY_EXCEPT
1552 __try
1553 {
1554#endif
1555 /* Open inside the specified parent window.
1556 * TODO: last argument should point to a CLIENTCREATESTRUCT
1557 * structure. */
1558 s_hwnd = CreateWindowEx(
1559 WS_EX_MDICHILD,
1560 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02001561 WS_OVERLAPPEDWINDOW | WS_CHILD
1562 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
1564 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
1565 100, /* Any value will do */
1566 100, /* Any value will do */
1567 vim_parent_hwnd, NULL,
1568 s_hinst, NULL);
1569#ifdef HAVE_TRY_EXCEPT
1570 }
1571 __except(EXCEPTION_EXECUTE_HANDLER)
1572 {
1573 /* NOP */
1574 }
1575#endif
1576 if (s_hwnd == NULL)
1577 {
1578 EMSG(_("E672: Unable to open window inside MDI application"));
1579 mch_exit(2);
1580 }
1581 }
1582 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001583 {
1584 /* If the provided windowid is not valid reset it to zero, so that it
1585 * is ignored and we open our own window. */
1586 if (IsWindow((HWND)win_socket_id) <= 0)
1587 win_socket_id = 0;
1588
1589 /* Create a window. If win_socket_id is not zero without border and
1590 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00001592 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02001593 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
1594 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00001595 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
1596 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
1597 100, /* Any value will do */
1598 100, /* Any value will do */
1599 NULL, NULL,
1600 s_hinst, NULL);
1601 if (s_hwnd != NULL && win_socket_id != 0)
1602 {
1603 SetParent(s_hwnd, (HWND)win_socket_id);
1604 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
1605 }
1606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607
1608 if (s_hwnd == NULL)
1609 return FAIL;
1610
1611#ifdef GLOBAL_IME
1612 global_ime_init(atom, s_hwnd);
1613#endif
1614#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
1615 dyn_imm_load();
1616#endif
1617
1618 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01001619#ifdef FEAT_MBYTE
1620 if (wide_WindowProc)
1621 {
1622 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
1623 {
1624 wndclassw.style = CS_OWNDC;
1625 wndclassw.lpfnWndProc = _TextAreaWndProc;
1626 wndclassw.cbClsExtra = 0;
1627 wndclassw.cbWndExtra = 0;
1628 wndclassw.hInstance = s_hinst;
1629 wndclassw.hIcon = NULL;
1630 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
1631 wndclassw.hbrBackground = NULL;
1632 wndclassw.lpszMenuName = NULL;
1633 wndclassw.lpszClassName = szTextAreaClassW;
1634
1635 if (RegisterClassW(&wndclassw) == 0)
1636 return FAIL;
1637 }
1638 }
1639 else
1640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
1642 {
1643 wndclass.style = CS_OWNDC;
1644 wndclass.lpfnWndProc = _TextAreaWndProc;
1645 wndclass.cbClsExtra = 0;
1646 wndclass.cbWndExtra = 0;
1647 wndclass.hInstance = s_hinst;
1648 wndclass.hIcon = NULL;
1649 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1650 wndclass.hbrBackground = NULL;
1651 wndclass.lpszMenuName = NULL;
1652 wndclass.lpszClassName = szTextAreaClass;
1653
1654 if (RegisterClass(&wndclass) == 0)
1655 return FAIL;
1656 }
1657 s_textArea = CreateWindowEx(
1658 WS_EX_CLIENTEDGE,
1659 szTextAreaClass, "Vim text area",
1660 WS_CHILD | WS_VISIBLE, 0, 0,
1661 100, /* Any value will do for now */
1662 100, /* Any value will do for now */
1663 s_hwnd, NULL,
1664 s_hinst, NULL);
1665
1666 if (s_textArea == NULL)
1667 return FAIL;
1668
1669#ifdef FEAT_MENU
1670 s_menuBar = CreateMenu();
1671#endif
1672 s_hdc = GetDC(s_textArea);
1673
1674#ifdef MSWIN16_FASTTEXT
1675 SetBkMode(s_hdc, OPAQUE);
1676#endif
1677
1678#ifdef FEAT_WINDOWS
1679 DragAcceptFiles(s_hwnd, TRUE);
1680#endif
1681
1682 /* Do we need to bother with this? */
1683 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
1684
1685 /* Get background/foreground colors from the system */
1686 gui_mch_def_colors();
1687
1688 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
1689 * file) */
1690 set_normal_colors();
1691
1692 /*
1693 * Check that none of the colors are the same as the background color.
1694 * Then store the current values as the defaults.
1695 */
1696 gui_check_colors();
1697 gui.def_norm_pixel = gui.norm_pixel;
1698 gui.def_back_pixel = gui.back_pixel;
1699
1700 /* Get the colors for the highlight groups (gui_check_colors() might have
1701 * changed them) */
1702 highlight_gui_started();
1703
1704 /*
1705 * Start out by adding the configured border width into the border offset
1706 */
1707 gui.border_offset = gui.border_width + 2; /*CLIENT EDGE*/
1708
1709 /*
1710 * Set up for Intellimouse processing
1711 */
1712 init_mouse_wheel();
1713
1714 /*
1715 * compute a couple of metrics used for the dialogs
1716 */
1717 get_dialog_font_metrics();
1718#ifdef FEAT_TOOLBAR
1719 /*
1720 * Create the toolbar
1721 */
1722 initialise_toolbar();
1723#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001724#ifdef FEAT_GUI_TABLINE
1725 /*
1726 * Create the tabline
1727 */
1728 initialise_tabline();
1729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730#ifdef MSWIN_FIND_REPLACE
1731 /*
1732 * Initialise the dialog box stuff
1733 */
1734 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
1735
1736 /* Initialise the struct */
1737 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
1738 s_findrep_struct.lpstrFindWhat = alloc(MSWIN_FR_BUFSIZE);
1739 s_findrep_struct.lpstrFindWhat[0] = NUL;
1740 s_findrep_struct.lpstrReplaceWith = alloc(MSWIN_FR_BUFSIZE);
1741 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1742 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
1743 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00001744# if defined(FEAT_MBYTE) && defined(WIN3264)
1745 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
1746 s_findrep_struct_w.lpstrFindWhat =
1747 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
1748 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
1749 s_findrep_struct_w.lpstrReplaceWith =
1750 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
1751 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
1752 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
1753 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
1754# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02001757#ifdef FEAT_EVAL
Bram Moolenaaraf62ff32013-03-19 14:48:29 +01001758# ifndef HandleToLong
Bram Moolenaar4da95d32011-07-07 17:43:41 +02001759/* HandleToLong() only exists in compilers that can do 64 bit builds */
1760# define HandleToLong(h) ((long)(h))
1761# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02001762 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02001763 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02001764#endif
1765
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02001766#ifdef FEAT_RENDER_OPTIONS
1767 if (p_rop)
1768 (void)gui_mch_set_rendering_options(p_rop);
1769#endif
1770
Bram Moolenaar748bf032005-02-02 23:04:36 +00001771theend:
1772 /* Display any pending error messages */
1773 display_errors();
1774
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 return OK;
1776}
1777
1778/*
1779 * Get the size of the screen, taking position on multiple monitors into
1780 * account (if supported).
1781 */
1782 static void
1783get_work_area(RECT *spi_rect)
1784{
1785 _HMONITOR mon;
1786 _MONITORINFO moninfo;
1787
1788 /* use these functions only if available */
1789 if (pMonitorFromWindow != NULL && pGetMonitorInfo != NULL)
1790 {
1791 /* work out which monitor the window is on, and get *it's* work area */
1792 mon = pMonitorFromWindow(s_hwnd, 1 /*MONITOR_DEFAULTTOPRIMARY*/);
1793 if (mon != NULL)
1794 {
1795 moninfo.cbSize = sizeof(_MONITORINFO);
1796 if (pGetMonitorInfo(mon, &moninfo))
1797 {
1798 *spi_rect = moninfo.rcWork;
1799 return;
1800 }
1801 }
1802 }
1803 /* this is the old method... */
1804 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
1805}
1806
1807/*
1808 * Set the size of the window to the given width and height in pixels.
1809 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001810/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 void
1812gui_mch_set_shellsize(int width, int height,
Bram Moolenaarafa24992006-03-27 20:58:26 +00001813 int min_width, int min_height, int base_width, int base_height,
1814 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815{
1816 RECT workarea_rect;
1817 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 WINDOWPLACEMENT wndpl;
1819
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001820 /* Try to keep window completely on screen. */
1821 /* Get position of the screen work area. This is the part that is not
1822 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 get_work_area(&workarea_rect);
1824
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001825 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001826 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 wndpl.length = sizeof(WINDOWPLACEMENT);
1828 GetWindowPlacement(s_hwnd, &wndpl);
1829
1830 /* Resizing a maximized window looks very strange, unzoom it first.
1831 * But don't do it when still starting up, it may have been requested in
1832 * the shortcut. */
1833 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
1834 {
1835 ShowWindow(s_hwnd, SW_SHOWNORMAL);
1836 /* Need to get the settings of the normal window. */
1837 GetWindowPlacement(s_hwnd, &wndpl);
1838 }
1839
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02001841 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02001842 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02001843 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02001844 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 + GetSystemMetrics(SM_CYCAPTION)
1846#ifdef FEAT_MENU
1847 + gui_mswin_get_menu_height(FALSE)
1848#endif
1849 ;
1850
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001851 /* The following should take care of keeping Vim on the same monitor, no
1852 * matter if the secondary monitor is left or right of the primary
1853 * monitor. */
1854 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
1855 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00001856
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001857 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001858 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001859 && wndpl.rcNormalPosition.right > workarea_rect.right)
1860 OffsetRect(&wndpl.rcNormalPosition,
1861 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001863 if ((direction & RESIZE_HOR)
1864 && wndpl.rcNormalPosition.left < workarea_rect.left)
1865 OffsetRect(&wndpl.rcNormalPosition,
1866 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
Bram Moolenaarafa24992006-03-27 20:58:26 +00001868 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001869 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
1870 OffsetRect(&wndpl.rcNormalPosition,
1871 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001873 if ((direction & RESIZE_VERT)
1874 && wndpl.rcNormalPosition.top < workarea_rect.top)
1875 OffsetRect(&wndpl.rcNormalPosition,
1876 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
1878 /* set window position - we should use SetWindowPlacement rather than
1879 * SetWindowPos as the MSDN docs say the coord systems returned by
1880 * these two are not compatible. */
1881 SetWindowPlacement(s_hwnd, &wndpl);
1882
1883 SetActiveWindow(s_hwnd);
1884 SetFocus(s_hwnd);
1885
1886#ifdef FEAT_MENU
1887 /* Menu may wrap differently now */
1888 gui_mswin_get_menu_height(!gui.starting);
1889#endif
1890}
1891
1892
1893 void
1894gui_mch_set_scrollbar_thumb(
1895 scrollbar_T *sb,
1896 long val,
1897 long size,
1898 long max)
1899{
1900 SCROLLINFO info;
1901
1902 sb->scroll_shift = 0;
1903 while (max > 32767)
1904 {
1905 max = (max + 1) >> 1;
1906 val >>= 1;
1907 size >>= 1;
1908 ++sb->scroll_shift;
1909 }
1910
1911 if (sb->scroll_shift > 0)
1912 ++size;
1913
1914 info.cbSize = sizeof(info);
1915 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
1916 info.nPos = val;
1917 info.nMin = 0;
1918 info.nMax = max;
1919 info.nPage = size;
1920 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
1921}
1922
1923
1924/*
1925 * Set the current text font.
1926 */
1927 void
1928gui_mch_set_font(GuiFont font)
1929{
1930 gui.currFont = font;
1931}
1932
1933
1934/*
1935 * Set the current text foreground color.
1936 */
1937 void
1938gui_mch_set_fg_color(guicolor_T color)
1939{
1940 gui.currFgColor = color;
1941}
1942
1943/*
1944 * Set the current text background color.
1945 */
1946 void
1947gui_mch_set_bg_color(guicolor_T color)
1948{
1949 gui.currBgColor = color;
1950}
1951
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001952/*
1953 * Set the current text special color.
1954 */
1955 void
1956gui_mch_set_sp_color(guicolor_T color)
1957{
1958 gui.currSpColor = color;
1959}
1960
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961#if defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)
1962/*
1963 * Multi-byte handling, originally by Sung-Hoon Baek.
1964 * First static functions (no prototypes generated).
1965 */
1966#ifdef _MSC_VER
1967# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
1968#endif
1969#include <imm.h>
1970
1971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 * handle WM_IME_NOTIFY message
1973 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001974/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 static LRESULT
1976_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData)
1977{
1978 LRESULT lResult = 0;
1979 HIMC hImc;
1980
1981 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
1982 return lResult;
1983 switch (dwCommand)
1984 {
1985 case IMN_SETOPENSTATUS:
1986 if (pImmGetOpenStatus(hImc))
1987 {
1988 pImmSetCompositionFont(hImc, &norm_logfont);
1989 im_set_position(gui.row, gui.col);
1990
1991 /* Disable langmap */
1992 State &= ~LANGMAP;
1993 if (State & INSERT)
1994 {
1995#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1996 /* Unshown 'keymap' in status lines */
1997 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
1998 {
1999 /* Save cursor position */
2000 int old_row = gui.row;
2001 int old_col = gui.col;
2002
2003 // This must be called here before
2004 // status_redraw_curbuf(), otherwise the mode
2005 // message may appear in the wrong position.
2006 showmode();
2007 status_redraw_curbuf();
2008 update_screen(0);
2009 /* Restore cursor position */
2010 gui.row = old_row;
2011 gui.col = old_col;
2012 }
2013#endif
2014 }
2015 }
2016 gui_update_cursor(TRUE, FALSE);
2017 lResult = 0;
2018 break;
2019 }
2020 pImmReleaseContext(hWnd, hImc);
2021 return lResult;
2022}
2023
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002024/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 static LRESULT
2026_OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param)
2027{
2028 char_u *ret;
2029 int len;
2030
2031 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
2032 return 0;
2033
2034 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
2035 if (ret != NULL)
2036 {
2037 add_to_input_buf_csi(ret, len);
2038 vim_free(ret);
2039 return 1;
2040 }
2041 return 0;
2042}
2043
2044/*
2045 * get the current composition string, in UCS-2; *lenp is the number of
2046 * *lenp is the number of Unicode characters.
2047 */
2048 static short_u *
2049GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
2050{
2051 LONG ret;
2052 LPWSTR wbuf = NULL;
2053 char_u *buf;
2054
2055 if (!pImmGetContext)
2056 return NULL; /* no imm32.dll */
2057
2058 /* Try Unicode; this'll always work on NT regardless of codepage. */
2059 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
2060 if (ret == 0)
2061 return NULL; /* empty */
2062
2063 if (ret > 0)
2064 {
2065 /* Allocate the requested buffer plus space for the NUL character. */
2066 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
2067 if (wbuf != NULL)
2068 {
2069 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
2070 *lenp = ret / sizeof(WCHAR);
2071 }
2072 return (short_u *)wbuf;
2073 }
2074
2075 /* ret < 0; we got an error, so try the ANSI version. This'll work
2076 * on 9x/ME, but only if the codepage happens to be set to whatever
2077 * we're inputting. */
2078 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
2079 if (ret <= 0)
2080 return NULL; /* empty or error */
2081
2082 buf = alloc(ret);
2083 if (buf == NULL)
2084 return NULL;
2085 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
2086
2087 /* convert from codepage to UCS-2 */
2088 MultiByteToWideChar_alloc(GetACP(), 0, buf, ret, &wbuf, lenp);
2089 vim_free(buf);
2090
2091 return (short_u *)wbuf;
2092}
2093
2094/*
2095 * void GetResultStr()
2096 *
2097 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
2098 * get complete composition string
2099 */
2100 static char_u *
2101GetResultStr(HWND hwnd, int GCS, int *lenp)
2102{
2103 HIMC hIMC; /* Input context handle. */
2104 short_u *buf = NULL;
2105 char_u *convbuf = NULL;
2106
2107 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
2108 return NULL;
2109
2110 /* Reads in the composition string. */
2111 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
2112 if (buf == NULL)
2113 return NULL;
2114
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002115 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 pImmReleaseContext(hwnd, hIMC);
2117 vim_free(buf);
2118 return convbuf;
2119}
2120#endif
2121
2122/* For global functions we need prototypes. */
2123#if (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) || defined(PROTO)
2124
2125/*
2126 * set font to IM.
2127 */
2128 void
2129im_set_font(LOGFONT *lf)
2130{
2131 HIMC hImc;
2132
2133 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
2134 {
2135 pImmSetCompositionFont(hImc, lf);
2136 pImmReleaseContext(s_hwnd, hImc);
2137 }
2138}
2139
2140/*
2141 * Notify cursor position to IM.
2142 */
2143 void
2144im_set_position(int row, int col)
2145{
2146 HIMC hImc;
2147
2148 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
2149 {
2150 COMPOSITIONFORM cfs;
2151
2152 cfs.dwStyle = CFS_POINT;
2153 cfs.ptCurrentPos.x = FILL_X(col);
2154 cfs.ptCurrentPos.y = FILL_Y(row);
2155 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
2156 pImmSetCompositionWindow(hImc, &cfs);
2157
2158 pImmReleaseContext(s_hwnd, hImc);
2159 }
2160}
2161
2162/*
2163 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
2164 */
2165 void
2166im_set_active(int active)
2167{
2168 HIMC hImc;
2169 static HIMC hImcOld = (HIMC)0;
2170
2171 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
2172 {
2173 if (p_imdisable)
2174 {
2175 if (hImcOld == (HIMC)0)
2176 {
2177 hImcOld = pImmGetContext(s_hwnd);
2178 if (hImcOld)
2179 pImmAssociateContext(s_hwnd, (HIMC)0);
2180 }
2181 active = FALSE;
2182 }
2183 else if (hImcOld != (HIMC)0)
2184 {
2185 pImmAssociateContext(s_hwnd, hImcOld);
2186 hImcOld = (HIMC)0;
2187 }
2188
2189 hImc = pImmGetContext(s_hwnd);
2190 if (hImc)
2191 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002192 /*
2193 * for Korean ime
2194 */
2195 HKL hKL = GetKeyboardLayout(0);
2196
2197 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
2198 {
2199 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
2200 static BOOL bSaved = FALSE;
2201
2202 if (active)
2203 {
2204 /* if we have a saved conversion status, restore it */
2205 if (bSaved)
2206 pImmSetConversionStatus(hImc, dwConversionSaved,
2207 dwSentenceSaved);
2208 bSaved = FALSE;
2209 }
2210 else
2211 {
2212 /* save conversion status and disable korean */
2213 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
2214 &dwSentenceSaved))
2215 {
2216 bSaved = TRUE;
2217 pImmSetConversionStatus(hImc,
2218 dwConversionSaved & ~(IME_CMODE_NATIVE
2219 | IME_CMODE_FULLSHAPE),
2220 dwSentenceSaved);
2221 }
2222 }
2223 }
2224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 pImmSetOpenStatus(hImc, active);
2226 pImmReleaseContext(s_hwnd, hImc);
2227 }
2228 }
2229}
2230
2231/*
2232 * Get IM status. When IM is on, return not 0. Else return 0.
2233 */
2234 int
2235im_get_status()
2236{
2237 int status = 0;
2238 HIMC hImc;
2239
2240 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
2241 {
2242 status = pImmGetOpenStatus(hImc) ? 1 : 0;
2243 pImmReleaseContext(s_hwnd, hImc);
2244 }
2245 return status;
2246}
2247
2248#endif /* FEAT_MBYTE && FEAT_MBYTE_IME */
2249
2250#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
2251/* Win32 with GLOBAL IME */
2252
2253/*
2254 * Notify cursor position to IM.
2255 */
2256 void
2257im_set_position(int row, int col)
2258{
2259 /* Win32 with GLOBAL IME */
2260 POINT p;
2261
2262 p.x = FILL_X(col);
2263 p.y = FILL_Y(row);
2264 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
2265 global_ime_set_position(&p);
2266}
2267
2268/*
2269 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
2270 */
2271 void
2272im_set_active(int active)
2273{
2274 global_ime_set_status(active);
2275}
2276
2277/*
2278 * Get IM status. When IM is on, return not 0. Else return 0.
2279 */
2280 int
2281im_get_status()
2282{
2283 return global_ime_get_status();
2284}
2285#endif
2286
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002287#ifdef FEAT_MBYTE
2288/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00002289 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002290 */
2291 static void
2292latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
2293{
2294 int c;
2295
Bram Moolenaarca003e12006-03-17 23:19:38 +00002296 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002297 {
2298 c = *text++;
2299 switch (c)
2300 {
2301 case 0xa4: c = 0x20ac; break; /* euro */
2302 case 0xa6: c = 0x0160; break; /* S hat */
2303 case 0xa8: c = 0x0161; break; /* S -hat */
2304 case 0xb4: c = 0x017d; break; /* Z hat */
2305 case 0xb8: c = 0x017e; break; /* Z -hat */
2306 case 0xbc: c = 0x0152; break; /* OE */
2307 case 0xbd: c = 0x0153; break; /* oe */
2308 case 0xbe: c = 0x0178; break; /* Y */
2309 }
2310 *unicodebuf++ = c;
2311 }
2312}
2313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314
2315#ifdef FEAT_RIGHTLEFT
2316/*
2317 * What is this for? In the case where you are using Win98 or Win2K or later,
2318 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
2319 * reverses the string sent to the TextOut... family. This sucks, because we
2320 * go to a lot of effort to do the right thing, and there doesn't seem to be a
2321 * way to tell Windblows not to do this!
2322 *
2323 * The short of it is that this 'RevOut' only gets called if you are running
2324 * one of the new, "improved" MS OSes, and only if you are running in
2325 * 'rightleft' mode. It makes display take *slightly* longer, but not
2326 * noticeably so.
2327 */
2328 static void
2329RevOut( HDC s_hdc,
2330 int col,
2331 int row,
2332 UINT foptions,
2333 CONST RECT *pcliprect,
2334 LPCTSTR text,
2335 UINT len,
2336 CONST INT *padding)
2337{
2338 int ix;
2339 static int special = -1;
2340
2341 if (special == -1)
2342 {
2343 /* Check windows version: special treatment is needed if it is NT 5 or
2344 * Win98 or higher. */
2345 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
2346 && os_version.dwMajorVersion >= 5)
2347 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
2348 && (os_version.dwMajorVersion > 4
2349 || (os_version.dwMajorVersion == 4
2350 && os_version.dwMinorVersion > 0))))
2351 special = 1;
2352 else
2353 special = 0;
2354 }
2355
2356 if (special)
2357 for (ix = 0; ix < (int)len; ++ix)
2358 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
2359 pcliprect, text + ix, 1, padding);
2360 else
2361 ExtTextOut(s_hdc, col, row, foptions, pcliprect, text, len, padding);
2362}
2363#endif
2364
2365 void
2366gui_mch_draw_string(
2367 int row,
2368 int col,
2369 char_u *text,
2370 int len,
2371 int flags)
2372{
2373 static int *padding = NULL;
2374 static int pad_size = 0;
2375 int i;
2376 const RECT *pcliprect = NULL;
2377 UINT foptions = 0;
2378#ifdef FEAT_MBYTE
2379 static WCHAR *unicodebuf = NULL;
2380 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002381 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 int n = 0;
2383#endif
2384 HPEN hpen, old_pen;
2385 int y;
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002386#ifdef FEAT_DIRECTX
2387 int font_is_ttf_or_vector = 0;
2388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389
2390#ifndef MSWIN16_FASTTEXT
2391 /*
2392 * Italic and bold text seems to have an extra row of pixels at the bottom
2393 * (below where the bottom of the character should be). If we draw the
2394 * characters with a solid background, the top row of pixels in the
2395 * character below will be overwritten. We can fix this by filling in the
2396 * background ourselves, to the correct character proportions, and then
2397 * writing the character in transparent mode. Still have a problem when
2398 * the character is "_", which gets written on to the character below.
2399 * New fix: set gui.char_ascent to -1. This shifts all characters up one
2400 * pixel in their slots, which fixes the problem with the bottom row of
2401 * pixels. We still need this code because otherwise the top row of pixels
2402 * becomes a problem. - webb.
2403 */
2404 static HBRUSH hbr_cache[2] = {NULL, NULL};
2405 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
2406 static int brush_lru = 0;
2407 HBRUSH hbr;
2408 RECT rc;
2409
2410 if (!(flags & DRAW_TRANSP))
2411 {
2412 /*
2413 * Clear background first.
2414 * Note: FillRect() excludes right and bottom of rectangle.
2415 */
2416 rc.left = FILL_X(col);
2417 rc.top = FILL_Y(row);
2418#ifdef FEAT_MBYTE
2419 if (has_mbyte)
2420 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002422 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 }
2424 else
2425#endif
2426 rc.right = FILL_X(col + len);
2427 rc.bottom = FILL_Y(row + 1);
2428
2429 /* Cache the created brush, that saves a lot of time. We need two:
2430 * one for cursor background and one for the normal background. */
2431 if (gui.currBgColor == brush_color[0])
2432 {
2433 hbr = hbr_cache[0];
2434 brush_lru = 1;
2435 }
2436 else if (gui.currBgColor == brush_color[1])
2437 {
2438 hbr = hbr_cache[1];
2439 brush_lru = 0;
2440 }
2441 else
2442 {
2443 if (hbr_cache[brush_lru] != NULL)
2444 DeleteBrush(hbr_cache[brush_lru]);
2445 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
2446 brush_color[brush_lru] = gui.currBgColor;
2447 hbr = hbr_cache[brush_lru];
2448 brush_lru = !brush_lru;
2449 }
2450 FillRect(s_hdc, &rc, hbr);
2451
2452 SetBkMode(s_hdc, TRANSPARENT);
2453
2454 /*
2455 * When drawing block cursor, prevent inverted character spilling
2456 * over character cell (can happen with bold/italic)
2457 */
2458 if (flags & DRAW_CURSOR)
2459 {
2460 pcliprect = &rc;
2461 foptions = ETO_CLIPPED;
2462 }
2463 }
2464#else
2465 /*
2466 * The alternative would be to write the characters in opaque mode, but
2467 * when the text is not exactly the same proportions as normal text, too
2468 * big or too little a rectangle gets drawn for the background.
2469 */
2470 SetBkMode(s_hdc, OPAQUE);
2471 SetBkColor(s_hdc, gui.currBgColor);
2472#endif
2473 SetTextColor(s_hdc, gui.currFgColor);
2474 SelectFont(s_hdc, gui.currFont);
2475
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002476#ifdef FEAT_DIRECTX
2477 if (IS_ENABLE_DIRECTX())
2478 {
2479 TEXTMETRIC tm;
2480
2481 GetTextMetrics(s_hdc, &tm);
2482 if (tm.tmPitchAndFamily & (TMPF_TRUETYPE | TMPF_VECTOR))
2483 {
2484 font_is_ttf_or_vector = 1;
2485 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
2486 }
2487 }
2488#endif
2489
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
2491 {
2492 vim_free(padding);
2493 pad_size = Columns;
2494
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002495 /* Don't give an out-of-memory message here, it would call us
2496 * recursively. */
2497 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 if (padding != NULL)
2499 for (i = 0; i < pad_size; i++)
2500 padding[i] = gui.char_width;
2501 }
2502
2503 /* On NT, tell the font renderer not to "help" us with Hebrew and Arabic
2504 * text. This doesn't work in 9x, so we have to deal with it manually on
2505 * those systems. */
2506 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
2507 foptions |= ETO_IGNORELANGUAGE;
2508
2509 /*
2510 * We have to provide the padding argument because italic and bold versions
2511 * of fixed-width fonts are often one pixel or so wider than their normal
2512 * versions.
2513 * No check for DRAW_BOLD, Windows will have done it already.
2514 */
2515
2516#ifdef FEAT_MBYTE
2517 /* Check if there are any UTF-8 characters. If not, use normal text
2518 * output to speed up output. */
2519 if (enc_utf8)
2520 for (n = 0; n < len; ++n)
2521 if (text[n] >= 0x80)
2522 break;
2523
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002524#if defined(FEAT_DIRECTX)
2525 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
2526 * required that unicode drawing routine, currently. So this forces it
2527 * enabled. */
2528 if (enc_utf8 && IS_ENABLE_DIRECTX())
2529 n = 0; /* Keep n < len, to enter block for unicode. */
2530#endif
2531
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002533 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002535 if ((enc_utf8
2536 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
2537 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 && (unicodebuf == NULL || len > unibuflen))
2539 {
2540 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002541 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
2543 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002544 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545
2546 unibuflen = len;
2547 }
2548
2549 if (enc_utf8 && n < len && unicodebuf != NULL)
2550 {
2551 /* Output UTF-8 characters. Caller has already separated
2552 * composing characters. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002553 int i;
2554 int wlen; /* string length in words */
2555 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 int cells; /* cell width of string up to composing char */
2557 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002558 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002560 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002561 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002563 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002565 c = utf_ptr2char(text + i);
2566 if (c >= 0x10000)
2567 {
2568 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002569 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
2570 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002571 }
2572 else
2573 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002574 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002575 }
2576 cw = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 if (cw > 2) /* don't use 4 for unprintable char */
2578 cw = 1;
2579 if (unicodepdy != NULL)
2580 {
2581 /* Use unicodepdy to make characters fit as we expect, even
2582 * when the font uses different widths (e.g., bold character
2583 * is wider). */
2584 unicodepdy[clen] = cw * gui.char_width;
2585 }
2586 cells += cw;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002587 i += utfc_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00002588 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002590#if defined(FEAT_DIRECTX)
2591 if (IS_ENABLE_DIRECTX() && font_is_ttf_or_vector)
2592 {
2593 DWriteContext_DrawText(s_dwc, s_hdc, unicodebuf, wlen,
2594 TEXT_X(col), TEXT_Y(row), FILL_X(cells), FILL_Y(1),
2595 gui.char_width, gui.currFgColor);
2596 }
2597 else
2598#endif
2599 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
2600 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 len = cells; /* used for underlining */
2602 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002603 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 {
2605 /* If we want to display codepage data, and the current CP is not the
2606 * ANSI one, we need to go via Unicode. */
2607 if (unicodebuf != NULL)
2608 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002609 if (enc_latin9)
2610 latin9_to_ucs(text, len, unicodebuf);
2611 else
2612 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 MB_PRECOMPOSED,
2614 (char *)text, len,
2615 (LPWSTR)unicodebuf, unibuflen);
2616 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002617 {
2618 /* Use unicodepdy to make characters fit as we expect, even
2619 * when the font uses different widths (e.g., bold character
2620 * is wider). */
2621 if (unicodepdy != NULL)
2622 {
2623 int i;
2624 int cw;
2625
2626 for (i = 0; i < len; ++i)
2627 {
2628 cw = utf_char2cells(unicodebuf[i]);
2629 if (cw > 2)
2630 cw = 1;
2631 unicodepdy[i] = cw * gui.char_width;
2632 }
2633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002635 foptions, pcliprect, unicodebuf, len, unicodepdy);
2636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 }
2638 }
2639 else
2640#endif
2641 {
2642#ifdef FEAT_RIGHTLEFT
2643 /* If we can't use ETO_IGNORELANGUAGE, we can't tell Windows not to
2644 * mess up RL text, so we have to draw it character-by-character.
2645 * Only do this if RL is on, since it's slow. */
2646 if (curwin->w_p_rl && !(foptions & ETO_IGNORELANGUAGE))
2647 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
2648 foptions, pcliprect, (char *)text, len, padding);
2649 else
2650#endif
2651 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
2652 foptions, pcliprect, (char *)text, len, padding);
2653 }
2654
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002655 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 if (flags & DRAW_UNDERL)
2657 {
2658 hpen = CreatePen(PS_SOLID, 1, gui.currFgColor);
2659 old_pen = SelectObject(s_hdc, hpen);
2660 /* When p_linespace is 0, overwrite the bottom row of pixels.
2661 * Otherwise put the line just below the character. */
2662 y = FILL_Y(row + 1) - 1;
2663#ifndef MSWIN16_FASTTEXT
2664 if (p_linespace > 1)
2665 y -= p_linespace - 1;
2666#endif
2667 MoveToEx(s_hdc, FILL_X(col), y, NULL);
2668 /* Note: LineTo() excludes the last pixel in the line. */
2669 LineTo(s_hdc, FILL_X(col + len), y);
2670 DeleteObject(SelectObject(s_hdc, old_pen));
2671 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002672
2673 /* Undercurl */
2674 if (flags & DRAW_UNDERC)
2675 {
2676 int x;
2677 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002678 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002679
2680 y = FILL_Y(row + 1) - 1;
2681 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
2682 {
2683 offset = val[x % 8];
2684 SetPixel(s_hdc, x, y - offset, gui.currSpColor);
2685 }
2686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687}
2688
2689
2690/*
2691 * Output routines.
2692 */
2693
2694/* Flush any output to the screen */
2695 void
2696gui_mch_flush(void)
2697{
2698# if defined(__BORLANDC__)
2699 /*
2700 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
2701 * prototype declaration.
2702 * The compiler complains if __stdcall is not used in both declarations.
2703 */
2704 BOOL __stdcall GdiFlush(void);
2705# endif
2706
2707 GdiFlush();
2708}
2709
2710 static void
2711clear_rect(RECT *rcp)
2712{
2713 HBRUSH hbr;
2714
2715 hbr = CreateSolidBrush(gui.back_pixel);
2716 FillRect(s_hdc, rcp, hbr);
2717 DeleteBrush(hbr);
2718}
2719
2720
Bram Moolenaarc716c302006-01-21 22:12:51 +00002721 void
2722gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
2723{
2724 RECT workarea_rect;
2725
2726 get_work_area(&workarea_rect);
2727
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002728 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02002729 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002730 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00002731
2732 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
2733 * the menubar for MSwin, we subtract it from the screen height, so that
2734 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002735 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02002736 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002737 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00002738 - GetSystemMetrics(SM_CYCAPTION)
2739#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002740 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00002741#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002742 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00002743}
2744
2745
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746#if defined(FEAT_MENU) || defined(PROTO)
2747/*
2748 * Add a sub menu to the menu bar.
2749 */
2750 void
2751gui_mch_add_menu(
2752 vimmenu_T *menu,
2753 int pos)
2754{
2755 vimmenu_T *parent = menu->parent;
2756
2757 menu->submenu_id = CreatePopupMenu();
2758 menu->id = s_menu_id++;
2759
2760 if (menu_is_menubar(menu->name))
2761 {
2762 if (is_winnt_3())
2763 {
2764 InsertMenu((parent == NULL) ? s_menuBar : parent->submenu_id,
2765 (UINT)pos, MF_POPUP | MF_STRING | MF_BYPOSITION,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002766 (long_u)menu->submenu_id, (LPCTSTR) menu->name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 }
2768 else
2769 {
2770#ifdef FEAT_MBYTE
2771 WCHAR *wn = NULL;
2772 int n;
2773
2774 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2775 {
2776 /* 'encoding' differs from active codepage: convert menu name
2777 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002778 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 if (wn != NULL)
2780 {
2781 MENUITEMINFOW infow;
2782
2783 infow.cbSize = sizeof(infow);
2784 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
2785 | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002786 infow.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 infow.wID = menu->id;
2788 infow.fType = MFT_STRING;
2789 infow.dwTypeData = wn;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002790 infow.cch = (UINT)wcslen(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 infow.hSubMenu = menu->submenu_id;
2792 n = InsertMenuItemW((parent == NULL)
2793 ? s_menuBar : parent->submenu_id,
2794 (UINT)pos, TRUE, &infow);
2795 vim_free(wn);
2796 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2797 /* Failed, try using non-wide function. */
2798 wn = NULL;
2799 }
2800 }
2801
2802 if (wn == NULL)
2803#endif
2804 {
2805 MENUITEMINFO info;
2806
2807 info.cbSize = sizeof(info);
2808 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002809 info.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 info.wID = menu->id;
2811 info.fType = MFT_STRING;
2812 info.dwTypeData = (LPTSTR)menu->name;
2813 info.cch = (UINT)STRLEN(menu->name);
2814 info.hSubMenu = menu->submenu_id;
2815 InsertMenuItem((parent == NULL)
2816 ? s_menuBar : parent->submenu_id,
2817 (UINT)pos, TRUE, &info);
2818 }
2819 }
2820 }
2821
2822 /* Fix window size if menu may have wrapped */
2823 if (parent == NULL)
2824 gui_mswin_get_menu_height(!gui.starting);
2825#ifdef FEAT_TEAROFF
2826 else if (IsWindow(parent->tearoff_handle))
2827 rebuild_tearoff(parent);
2828#endif
2829}
2830
2831 void
2832gui_mch_show_popupmenu(vimmenu_T *menu)
2833{
2834 POINT mp;
2835
2836 (void)GetCursorPos((LPPOINT)&mp);
2837 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
2838}
2839
2840 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00002841gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
2843 vimmenu_T *menu = gui_find_menu(path_name);
2844
2845 if (menu != NULL)
2846 {
2847 POINT p;
2848
2849 /* Find the position of the current cursor */
2850 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00002851 if (mouse_pos)
2852 {
2853 int mx, my;
2854
2855 gui_mch_getmouse(&mx, &my);
2856 p.x += mx;
2857 p.y += my;
2858 }
2859 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 {
2861 p.x += TEXT_X(W_WINCOL(curwin) + curwin->w_wcol + 1);
2862 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
2863 }
2864 msg_scroll = FALSE;
2865 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
2866 }
2867}
2868
2869#if defined(FEAT_TEAROFF) || defined(PROTO)
2870/*
2871 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
2872 * create it as a pseudo-"tearoff menu".
2873 */
2874 void
2875gui_make_tearoff(char_u *path_name)
2876{
2877 vimmenu_T *menu = gui_find_menu(path_name);
2878
2879 /* Found the menu, so tear it off. */
2880 if (menu != NULL)
2881 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
2882}
2883#endif
2884
2885/*
2886 * Add a menu item to a menu
2887 */
2888 void
2889gui_mch_add_menu_item(
2890 vimmenu_T *menu,
2891 int idx)
2892{
2893 vimmenu_T *parent = menu->parent;
2894
2895 menu->id = s_menu_id++;
2896 menu->submenu_id = NULL;
2897
2898#ifdef FEAT_TEAROFF
2899 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
2900 {
2901 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
2902 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
2903 }
2904 else
2905#endif
2906#ifdef FEAT_TOOLBAR
2907 if (menu_is_toolbar(parent->name))
2908 {
2909 TBBUTTON newtb;
2910
2911 vim_memset(&newtb, 0, sizeof(newtb));
2912 if (menu_is_separator(menu->name))
2913 {
2914 newtb.iBitmap = 0;
2915 newtb.fsStyle = TBSTYLE_SEP;
2916 }
2917 else
2918 {
2919 newtb.iBitmap = get_toolbar_bitmap(menu);
2920 newtb.fsStyle = TBSTYLE_BUTTON;
2921 }
2922 newtb.idCommand = menu->id;
2923 newtb.fsState = TBSTATE_ENABLED;
2924 newtb.iString = 0;
2925 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
2926 (LPARAM)&newtb);
2927 menu->submenu_id = (HMENU)-1;
2928 }
2929 else
2930#endif
2931 {
2932#ifdef FEAT_MBYTE
2933 WCHAR *wn = NULL;
2934 int n;
2935
2936 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2937 {
2938 /* 'encoding' differs from active codepage: convert menu item name
2939 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002940 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 if (wn != NULL)
2942 {
2943 n = InsertMenuW(parent->submenu_id, (UINT)idx,
2944 (menu_is_separator(menu->name)
2945 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
2946 (UINT)menu->id, wn);
2947 vim_free(wn);
2948 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2949 /* Failed, try using non-wide function. */
2950 wn = NULL;
2951 }
2952 }
2953 if (wn == NULL)
2954#endif
2955 InsertMenu(parent->submenu_id, (UINT)idx,
2956 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
2957 | MF_BYPOSITION,
2958 (UINT)menu->id, (LPCTSTR)menu->name);
2959#ifdef FEAT_TEAROFF
2960 if (IsWindow(parent->tearoff_handle))
2961 rebuild_tearoff(parent);
2962#endif
2963 }
2964}
2965
2966/*
2967 * Destroy the machine specific menu widget.
2968 */
2969 void
2970gui_mch_destroy_menu(vimmenu_T *menu)
2971{
2972#ifdef FEAT_TOOLBAR
2973 /*
2974 * is this a toolbar button?
2975 */
2976 if (menu->submenu_id == (HMENU)-1)
2977 {
2978 int iButton;
2979
2980 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
2981 (WPARAM)menu->id, 0);
2982 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
2983 }
2984 else
2985#endif
2986 {
2987 if (menu->parent != NULL
2988 && menu_is_popup(menu->parent->dname)
2989 && menu->parent->submenu_id != NULL)
2990 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
2991 else
2992 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
2993 if (menu->submenu_id != NULL)
2994 DestroyMenu(menu->submenu_id);
2995#ifdef FEAT_TEAROFF
2996 if (IsWindow(menu->tearoff_handle))
2997 DestroyWindow(menu->tearoff_handle);
2998 if (menu->parent != NULL
2999 && menu->parent->children != NULL
3000 && IsWindow(menu->parent->tearoff_handle))
3001 {
3002 /* This menu must not show up when rebuilding the tearoff window. */
3003 menu->modes = 0;
3004 rebuild_tearoff(menu->parent);
3005 }
3006#endif
3007 }
3008}
3009
3010#ifdef FEAT_TEAROFF
3011 static void
3012rebuild_tearoff(vimmenu_T *menu)
3013{
3014 /*hackish*/
3015 char_u tbuf[128];
3016 RECT trect;
3017 RECT rct;
3018 RECT roct;
3019 int x, y;
3020
3021 HWND thwnd = menu->tearoff_handle;
3022
3023 GetWindowText(thwnd, tbuf, 127);
3024 if (GetWindowRect(thwnd, &trect)
3025 && GetWindowRect(s_hwnd, &rct)
3026 && GetClientRect(s_hwnd, &roct))
3027 {
3028 x = trect.left - rct.left;
3029 y = (trect.top - rct.bottom + roct.bottom);
3030 }
3031 else
3032 {
3033 x = y = 0xffffL;
3034 }
3035 DestroyWindow(thwnd);
3036 if (menu->children != NULL)
3037 {
3038 gui_mch_tearoff(tbuf, menu, x, y);
3039 if (IsWindow(menu->tearoff_handle))
3040 (void) SetWindowPos(menu->tearoff_handle,
3041 NULL,
3042 (int)trect.left,
3043 (int)trect.top,
3044 0, 0,
3045 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
3046 }
3047}
3048#endif /* FEAT_TEAROFF */
3049
3050/*
3051 * Make a menu either grey or not grey.
3052 */
3053 void
3054gui_mch_menu_grey(
3055 vimmenu_T *menu,
3056 int grey)
3057{
3058#ifdef FEAT_TOOLBAR
3059 /*
3060 * is this a toolbar button?
3061 */
3062 if (menu->submenu_id == (HMENU)-1)
3063 {
3064 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
3065 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
3066 }
3067 else
3068#endif
3069 if (grey)
3070 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_GRAYED);
3071 else
3072 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
3073
3074#ifdef FEAT_TEAROFF
3075 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
3076 {
3077 WORD menuID;
3078 HWND menuHandle;
3079
3080 /*
3081 * A tearoff button has changed state.
3082 */
3083 if (menu->children == NULL)
3084 menuID = (WORD)(menu->id);
3085 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003086 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
3088 if (menuHandle)
3089 EnableWindow(menuHandle, !grey);
3090
3091 }
3092#endif
3093}
3094
3095#endif /* FEAT_MENU */
3096
3097
3098/* define some macros used to make the dialogue creation more readable */
3099
3100#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
3101#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00003102#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103
3104#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
3105/*
3106 * stuff for dialogs
3107 */
3108
3109/*
3110 * The callback routine used by all the dialogs. Very simple. First,
3111 * acknowledges the INITDIALOG message so that Windows knows to do standard
3112 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
3113 * pressed, return that button's ID - IDCANCEL (2), which is the button's
3114 * number.
3115 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003116/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 static LRESULT CALLBACK
3118dialog_callback(
3119 HWND hwnd,
3120 UINT message,
3121 WPARAM wParam,
3122 LPARAM lParam)
3123{
3124 if (message == WM_INITDIALOG)
3125 {
3126 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
3127 /* Set focus to the dialog. Set the default button, if specified. */
3128 (void)SetFocus(hwnd);
3129 if (dialog_default_button > IDCANCEL)
3130 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00003131 else
3132 /* We don't have a default, set focus on another element of the
3133 * dialog window, probably the icon */
3134 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 return FALSE;
3136 }
3137
3138 if (message == WM_COMMAND)
3139 {
3140 int button = LOWORD(wParam);
3141
3142 /* Don't end the dialog if something was selected that was
3143 * not a button.
3144 */
3145 if (button >= DLG_NONBUTTON_CONTROL)
3146 return TRUE;
3147
3148 /* If the edit box exists, copy the string. */
3149 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00003150 {
3151# if defined(FEAT_MBYTE) && defined(WIN3264)
3152 /* If the OS is Windows NT, and 'encoding' differs from active
3153 * codepage: use wide function and convert text. */
3154 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
3155 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003156 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00003157 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
3158 char_u *p;
3159
3160 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
3161 p = utf16_to_enc(wp, NULL);
3162 vim_strncpy(s_textfield, p, IOSIZE);
3163 vim_free(p);
3164 vim_free(wp);
3165 }
3166 else
3167# endif
3168 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00003170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171
3172 /*
3173 * Need to check for IDOK because if the user just hits Return to
3174 * accept the default value, some reason this is what we get.
3175 */
3176 if (button == IDOK)
3177 {
3178 if (dialog_default_button > IDCANCEL)
3179 EndDialog(hwnd, dialog_default_button);
3180 }
3181 else
3182 EndDialog(hwnd, button - IDCANCEL);
3183 return TRUE;
3184 }
3185
3186 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
3187 {
3188 EndDialog(hwnd, 0);
3189 return TRUE;
3190 }
3191 return FALSE;
3192}
3193
3194/*
3195 * Create a dialog dynamically from the parameter strings.
3196 * type = type of dialog (question, alert, etc.)
3197 * title = dialog title. may be NULL for default title.
3198 * message = text to display. Dialog sizes to accommodate it.
3199 * buttons = '\n' separated list of button captions, default first.
3200 * dfltbutton = number of default button.
3201 *
3202 * This routine returns 1 if the first button is pressed,
3203 * 2 for the second, etc.
3204 *
3205 * 0 indicates Esc was pressed.
3206 * -1 for unexpected error
3207 *
3208 * If stubbing out this fn, return 1.
3209 */
3210
3211static const char_u *dlg_icons[] = /* must match names in resource file */
3212{
3213 "IDR_VIM",
3214 "IDR_VIM_ERROR",
3215 "IDR_VIM_ALERT",
3216 "IDR_VIM_INFO",
3217 "IDR_VIM_QUESTION"
3218};
3219
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 int
3221gui_mch_dialog(
3222 int type,
3223 char_u *title,
3224 char_u *message,
3225 char_u *buttons,
3226 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003227 char_u *textfield,
3228 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229{
3230 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00003231 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 int numButtons;
3233 int *buttonWidths, *buttonPositions;
3234 int buttonYpos;
3235 int nchar, i;
3236 DWORD lStyle;
3237 int dlgwidth = 0;
3238 int dlgheight;
3239 int editboxheight;
3240 int horizWidth = 0;
3241 int msgheight;
3242 char_u *pstart;
3243 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003244 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 char_u *tbuffer;
3246 RECT rect;
3247 HWND hwnd;
3248 HDC hdc;
3249 HFONT font, oldFont;
3250 TEXTMETRIC fontInfo;
3251 int fontHeight;
3252 int textWidth, minButtonWidth, messageWidth;
3253 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003254 int maxDialogHeight;
3255 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 int vertical;
3257 int dlgPaddingX;
3258 int dlgPaddingY;
3259#ifdef USE_SYSMENU_FONT
3260 LOGFONT lfSysmenu;
3261 int use_lfSysmenu = FALSE;
3262#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003263 garray_T ga;
3264 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265
3266#ifndef NO_CONSOLE
3267 /* Don't output anything in silent mode ("ex -s") */
3268 if (silent_mode)
3269 return dfltbutton; /* return default option */
3270#endif
3271
Bram Moolenaar748bf032005-02-02 23:04:36 +00003272 if (s_hwnd == NULL)
3273 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274
3275 if ((type < 0) || (type > VIM_LAST_TYPE))
3276 type = 0;
3277
3278 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003279 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003280 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003281 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282
3283 if (p == NULL)
3284 return -1;
3285
3286 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003287 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 * vim_strsave() doesn't take a const arg (why not?), so cast away the
3289 * const.
3290 */
3291 tbuffer = vim_strsave(buttons);
3292 if (tbuffer == NULL)
3293 return -1;
3294
3295 --dfltbutton; /* Change from one-based to zero-based */
3296
3297 /* Count buttons */
3298 numButtons = 1;
3299 for (i = 0; tbuffer[i] != '\0'; i++)
3300 {
3301 if (tbuffer[i] == DLG_BUTTON_SEP)
3302 numButtons++;
3303 }
3304 if (dfltbutton >= numButtons)
3305 dfltbutton = -1;
3306
3307 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003308 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 if (buttonWidths == NULL)
3310 return -1;
3311
3312 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003313 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 if (buttonPositions == NULL)
3315 return -1;
3316
3317 /*
3318 * Calculate how big the dialog must be.
3319 */
3320 hwnd = GetDesktopWindow();
3321 hdc = GetWindowDC(hwnd);
3322#ifdef USE_SYSMENU_FONT
3323 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
3324 {
3325 font = CreateFontIndirect(&lfSysmenu);
3326 use_lfSysmenu = TRUE;
3327 }
3328 else
3329#endif
3330 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3331 VARIABLE_PITCH , DLG_FONT_NAME);
3332 if (s_usenewlook)
3333 {
3334 oldFont = SelectFont(hdc, font);
3335 dlgPaddingX = DLG_PADDING_X;
3336 dlgPaddingY = DLG_PADDING_Y;
3337 }
3338 else
3339 {
3340 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
3341 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
3342 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
3343 }
3344 GetTextMetrics(hdc, &fontInfo);
3345 fontHeight = fontInfo.tmHeight;
3346
3347 /* Minimum width for horizontal button */
3348 minButtonWidth = GetTextWidth(hdc, "Cancel", 6);
3349
3350 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003351 if (s_hwnd == NULL)
3352 {
3353 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354
Bram Moolenaarc716c302006-01-21 22:12:51 +00003355 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003356 get_work_area(&workarea_rect);
3357 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
3358 if (maxDialogWidth > 600)
3359 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02003360 /* Leave some room for the taskbar. */
3361 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003362 }
3363 else
3364 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02003365 /* Use our own window for the size, unless it's very small. */
3366 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003367 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02003368 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02003369 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003370 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
3371 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003372
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003373 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02003374 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02003375 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02003376 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003377 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
3378 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
3379 }
3380
3381 /* Set dlgwidth to width of message.
3382 * Copy the message into "ga", changing NL to CR-NL and inserting line
3383 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 pstart = message;
3385 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003386 msgheight = 0;
3387 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 do
3389 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003390 msgheight += fontHeight; /* at least one line */
3391
3392 /* Need to figure out where to break the string. The system does it
3393 * at a word boundary, which would mean we can't compute the number of
3394 * wrapped lines. */
3395 textWidth = 0;
3396 last_white = NULL;
3397 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00003398 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003399#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003400 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003401#else
3402 l = 1;
3403#endif
3404 if (l == 1 && vim_iswhite(*pend)
3405 && textWidth > maxDialogWidth * 3 / 4)
3406 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02003407 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003408 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003409 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003410 /* Line will wrap. */
3411 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003412 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003413 textWidth = 0;
3414
3415 if (last_white != NULL)
3416 {
3417 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003418 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003419 pend = last_white + 1;
3420 last_white = NULL;
3421 }
3422 ga_append(&ga, '\r');
3423 ga_append(&ga, '\n');
3424 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003425 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003426
3427 while (--l >= 0)
3428 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003429 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003430 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003432
3433 ga_append(&ga, '\r');
3434 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 pstart = pend + 1;
3436 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003437
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003438 if (ga.ga_data != NULL)
3439 message = ga.ga_data;
3440
Bram Moolenaar748bf032005-02-02 23:04:36 +00003441 messageWidth += 10; /* roundoff space */
3442
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02003444 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
3445 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
3447 if (msgheight < DLG_ICON_HEIGHT)
3448 msgheight = DLG_ICON_HEIGHT;
3449
3450 /*
3451 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00003452 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00003454 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 if (!vertical)
3456 {
3457 // Place buttons horizontally if they fit.
3458 horizWidth = dlgPaddingX;
3459 pstart = tbuffer;
3460 i = 0;
3461 do
3462 {
3463 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
3464 if (pend == NULL)
3465 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02003466 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 if (textWidth < minButtonWidth)
3468 textWidth = minButtonWidth;
3469 textWidth += dlgPaddingX; /* Padding within button */
3470 buttonWidths[i] = textWidth;
3471 buttonPositions[i++] = horizWidth;
3472 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
3473 pstart = pend + 1;
3474 } while (*pend != NUL);
3475
3476 if (horizWidth > maxDialogWidth)
3477 vertical = TRUE; // Too wide to fit on the screen.
3478 else if (horizWidth > dlgwidth)
3479 dlgwidth = horizWidth;
3480 }
3481
3482 if (vertical)
3483 {
3484 // Stack buttons vertically.
3485 pstart = tbuffer;
3486 do
3487 {
3488 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
3489 if (pend == NULL)
3490 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02003491 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 textWidth += dlgPaddingX; /* Padding within button */
3493 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
3494 if (textWidth > dlgwidth)
3495 dlgwidth = textWidth;
3496 pstart = pend + 1;
3497 } while (*pend != NUL);
3498 }
3499
3500 if (dlgwidth < DLG_MIN_WIDTH)
3501 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
3502
3503 /* start to fill in the dlgtemplate information. addressing by WORDs */
3504 if (s_usenewlook)
3505 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
3506 else
3507 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
3508
3509 add_long(lStyle);
3510 add_long(0); // (lExtendedStyle)
3511 pnumitems = p; /*save where the number of items must be stored*/
3512 add_word(0); // NumberOfItems(will change later)
3513 add_word(10); // x
3514 add_word(10); // y
3515 add_word(PixelToDialogX(dlgwidth)); // cx
3516
3517 // Dialog height.
3518 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02003519 dlgheight = msgheight + 2 * dlgPaddingY
3520 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 else
3522 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
3523
3524 // Dialog needs to be taller if contains an edit box.
3525 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
3526 if (textfield != NULL)
3527 dlgheight += editboxheight;
3528
Bram Moolenaara95d8232013-08-07 15:27:11 +02003529 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
3530 if (dlgheight > maxDialogHeight)
3531 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02003532 msgheight = msgheight - (dlgheight - maxDialogHeight);
3533 dlgheight = maxDialogHeight;
3534 scroll_flag = WS_VSCROLL;
3535 /* Make sure scrollbar doesn't appear in the middle of the dialog */
3536 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02003537 }
3538
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 add_word(PixelToDialogY(dlgheight));
3540
3541 add_word(0); // Menu
3542 add_word(0); // Class
3543
3544 /* copy the title of the dialog */
3545 nchar = nCopyAnsiToWideChar(p, (title ?
3546 (LPSTR)title :
3547 (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
3548 p += nchar;
3549
3550 if (s_usenewlook)
3551 {
3552 /* do the font, since DS_3DLOOK doesn't work properly */
3553#ifdef USE_SYSMENU_FONT
3554 if (use_lfSysmenu)
3555 {
3556 /* point size */
3557 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
3558 GetDeviceCaps(hdc, LOGPIXELSY));
3559 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
3560 }
3561 else
3562#endif
3563 {
3564 *p++ = DLG_FONT_POINT_SIZE; // point size
3565 nchar = nCopyAnsiToWideChar(p, TEXT(DLG_FONT_NAME));
3566 }
3567 p += nchar;
3568 }
3569
3570 buttonYpos = msgheight + 2 * dlgPaddingY;
3571
3572 if (textfield != NULL)
3573 buttonYpos += editboxheight;
3574
3575 pstart = tbuffer;
3576 if (!vertical)
3577 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
3578 for (i = 0; i < numButtons; i++)
3579 {
3580 /* get end of this button. */
3581 for ( pend = pstart;
3582 *pend && (*pend != DLG_BUTTON_SEP);
3583 pend++)
3584 ;
3585
3586 if (*pend)
3587 *pend = '\0';
3588
3589 /*
3590 * old NOTE:
3591 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
3592 * the focus to the first tab-able button and in so doing makes that
3593 * the default!! Grrr. Workaround: Make the default button the only
3594 * one with WS_TABSTOP style. Means user can't tab between buttons, but
3595 * he/she can use arrow keys.
3596 *
3597 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00003598 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 * dialog. Also needed for when the textfield is the default control.
3600 * It appears to work now (perhaps not on Win95?).
3601 */
3602 if (vertical)
3603 {
3604 p = add_dialog_element(p,
3605 (i == dfltbutton
3606 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
3607 PixelToDialogX(DLG_VERT_PADDING_X),
3608 PixelToDialogY(buttonYpos /* TBK */
3609 + 2 * fontHeight * i),
3610 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
3611 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
3612 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, pstart);
3613 }
3614 else
3615 {
3616 p = add_dialog_element(p,
3617 (i == dfltbutton
3618 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
3619 PixelToDialogX(horizWidth + buttonPositions[i]),
3620 PixelToDialogY(buttonYpos), /* TBK */
3621 PixelToDialogX(buttonWidths[i]),
3622 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
3623 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, pstart);
3624 }
3625 pstart = pend + 1; /*next button*/
3626 }
3627 *pnumitems += numButtons;
3628
3629 /* Vim icon */
3630 p = add_dialog_element(p, SS_ICON,
3631 PixelToDialogX(dlgPaddingX),
3632 PixelToDialogY(dlgPaddingY),
3633 PixelToDialogX(DLG_ICON_WIDTH),
3634 PixelToDialogY(DLG_ICON_HEIGHT),
3635 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
3636 dlg_icons[type]);
3637
Bram Moolenaar748bf032005-02-02 23:04:36 +00003638 /* Dialog message */
3639 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
3640 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
3641 PixelToDialogY(dlgPaddingY),
3642 (WORD)(PixelToDialogX(messageWidth) + 1),
3643 PixelToDialogY(msgheight),
3644 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645
3646 /* Edit box */
3647 if (textfield != NULL)
3648 {
3649 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
3650 PixelToDialogX(2 * dlgPaddingX),
3651 PixelToDialogY(2 * dlgPaddingY + msgheight),
3652 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
3653 PixelToDialogY(fontHeight + dlgPaddingY),
3654 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, textfield);
3655 *pnumitems += 1;
3656 }
3657
3658 *pnumitems += 2;
3659
3660 SelectFont(hdc, oldFont);
3661 DeleteObject(font);
3662 ReleaseDC(hwnd, hdc);
3663
3664 /* Let the dialog_callback() function know which button to make default
3665 * If we have an edit box, make that the default. We also need to tell
3666 * dialog_callback() if this dialog contains an edit box or not. We do
3667 * this by setting s_textfield if it does.
3668 */
3669 if (textfield != NULL)
3670 {
3671 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
3672 s_textfield = textfield;
3673 }
3674 else
3675 {
3676 dialog_default_button = IDCANCEL + 1 + dfltbutton;
3677 s_textfield = NULL;
3678 }
3679
3680 /* show the dialog box modally and get a return value */
3681 nchar = (int)DialogBoxIndirect(
3682 s_hinst,
3683 (LPDLGTEMPLATE)pdlgtemplate,
3684 s_hwnd,
3685 (DLGPROC)dialog_callback);
3686
3687 LocalFree(LocalHandle(pdlgtemplate));
3688 vim_free(tbuffer);
3689 vim_free(buttonWidths);
3690 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003691 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692
3693 /* Focus back to our window (for when MDI is used). */
3694 (void)SetFocus(s_hwnd);
3695
3696 return nchar;
3697}
3698
3699#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003700
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701/*
3702 * Put a simple element (basic class) onto a dialog template in memory.
3703 * return a pointer to where the next item should be added.
3704 *
3705 * parameters:
3706 * lStyle = additional style flags
3707 * (be careful, NT3.51 & Win32s will ignore the new ones)
3708 * x,y = x & y positions IN DIALOG UNITS
3709 * w,h = width and height IN DIALOG UNITS
3710 * Id = ID used in messages
3711 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
3712 * caption = usually text or resource name
3713 *
3714 * TODO: use the length information noted here to enable the dialog creation
3715 * routines to work out more exactly how much memory they need to alloc.
3716 */
3717 static PWORD
3718add_dialog_element(
3719 PWORD p,
3720 DWORD lStyle,
3721 WORD x,
3722 WORD y,
3723 WORD w,
3724 WORD h,
3725 WORD Id,
3726 WORD clss,
3727 const char *caption)
3728{
3729 int nchar;
3730
3731 p = lpwAlign(p); /* Align to dword boundary*/
3732 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
3733 *p++ = LOWORD(lStyle);
3734 *p++ = HIWORD(lStyle);
3735 *p++ = 0; // LOWORD (lExtendedStyle)
3736 *p++ = 0; // HIWORD (lExtendedStyle)
3737 *p++ = x;
3738 *p++ = y;
3739 *p++ = w;
3740 *p++ = h;
3741 *p++ = Id; //9 or 10 words in all
3742
3743 *p++ = (WORD)0xffff;
3744 *p++ = clss; //2 more here
3745
3746 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption); //strlen(caption)+1
3747 p += nchar;
3748
3749 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
3750
3751 return p; //total = 15+ (strlen(caption)) words
3752 // = 30 + 2(strlen(caption) bytes reqd
3753}
3754
3755
3756/*
3757 * Helper routine. Take an input pointer, return closest pointer that is
3758 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
3759 */
3760 static LPWORD
3761lpwAlign(
3762 LPWORD lpIn)
3763{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003764 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003766 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 ul += 3;
3768 ul >>= 2;
3769 ul <<= 2;
3770 return (LPWORD)ul;
3771}
3772
3773/*
3774 * Helper routine. Takes second parameter as Ansi string, copies it to first
3775 * parameter as wide character (16-bits / char) string, and returns integer
3776 * number of wide characters (words) in string (including the trailing wide
3777 * char NULL). Partly taken from the Win32SDK samples.
3778 */
3779 static int
3780nCopyAnsiToWideChar(
3781 LPWORD lpWCStr,
3782 LPSTR lpAnsiIn)
3783{
3784 int nChar = 0;
3785#ifdef FEAT_MBYTE
3786 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
3787 int i;
3788 WCHAR *wn;
3789
3790 if (enc_codepage == 0 && (int)GetACP() != enc_codepage)
3791 {
3792 /* Not a codepage, use our own conversion function. */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00003793 wn = enc_to_utf16(lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 if (wn != NULL)
3795 {
3796 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003797 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 vim_free(wn);
3799 }
3800 }
3801 if (nChar == 0)
3802 /* Use Win32 conversion function. */
3803 nChar = MultiByteToWideChar(
3804 enc_codepage > 0 ? enc_codepage : CP_ACP,
3805 MB_PRECOMPOSED,
3806 lpAnsiIn, len,
3807 lpWCStr, len);
3808 for (i = 0; i < nChar; ++i)
3809 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
3810 lpWCStr[i] = (WORD)' ';
3811#else
3812 do
3813 {
3814 if (*lpAnsiIn == '\t')
3815 *lpWCStr++ = (WORD)' ';
3816 else
3817 *lpWCStr++ = (WORD)*lpAnsiIn;
3818 nChar++;
3819 } while (*lpAnsiIn++);
3820#endif
3821
3822 return nChar;
3823}
3824
3825
3826#ifdef FEAT_TEAROFF
3827/*
3828 * The callback function for all the modeless dialogs that make up the
3829 * "tearoff menus" Very simple - forward button presses (to fool Vim into
3830 * thinking its menus have been clicked), and go away when closed.
3831 */
3832 static LRESULT CALLBACK
3833tearoff_callback(
3834 HWND hwnd,
3835 UINT message,
3836 WPARAM wParam,
3837 LPARAM lParam)
3838{
3839 if (message == WM_INITDIALOG)
3840 return (TRUE);
3841
3842 /* May show the mouse pointer again. */
3843 HandleMouseHide(message, lParam);
3844
3845 if (message == WM_COMMAND)
3846 {
3847 if ((WORD)(LOWORD(wParam)) & 0x8000)
3848 {
3849 POINT mp;
3850 RECT rect;
3851
3852 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
3853 {
3854 (void)TrackPopupMenu(
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003855 (HMENU)(long_u)(LOWORD(wParam) ^ 0x8000),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 TPM_LEFTALIGN | TPM_LEFTBUTTON,
3857 (int)rect.right - 8,
3858 (int)mp.y,
3859 (int)0, /*reserved param*/
3860 s_hwnd,
3861 NULL);
3862 /*
3863 * NOTE: The pop-up menu can eat the mouse up event.
3864 * We deal with this in normal.c.
3865 */
3866 }
3867 }
3868 else
3869 /* Pass on messages to the main Vim window */
3870 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
3871 /*
3872 * Give main window the focus back: this is so after
3873 * choosing a tearoff button you can start typing again
3874 * straight away.
3875 */
3876 (void)SetFocus(s_hwnd);
3877 return TRUE;
3878 }
3879 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
3880 {
3881 DestroyWindow(hwnd);
3882 return TRUE;
3883 }
3884
3885 /* When moved around, give main window the focus back. */
3886 if (message == WM_EXITSIZEMOVE)
3887 (void)SetActiveWindow(s_hwnd);
3888
3889 return FALSE;
3890}
3891#endif
3892
3893
3894/*
3895 * Decide whether to use the "new look" (small, non-bold font) or the "old
3896 * look" (big, clanky font) for dialogs, and work out a few values for use
3897 * later accordingly.
3898 */
3899 static void
3900get_dialog_font_metrics(void)
3901{
3902 HDC hdc;
3903 HFONT hfontTools = 0;
3904 DWORD dlgFontSize;
3905 SIZE size;
3906#ifdef USE_SYSMENU_FONT
3907 LOGFONT lfSysmenu;
3908#endif
3909
3910 s_usenewlook = FALSE;
3911
3912 /*
3913 * For NT3.51 and Win32s, we stick with the old look
3914 * because it matches everything else.
3915 */
3916 if (!is_winnt_3())
3917 {
3918#ifdef USE_SYSMENU_FONT
3919 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
3920 hfontTools = CreateFontIndirect(&lfSysmenu);
3921 else
3922#endif
3923 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
3924 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
3925
3926 if (hfontTools)
3927 {
3928 hdc = GetDC(s_hwnd);
3929 SelectObject(hdc, hfontTools);
3930 /*
3931 * GetTextMetrics() doesn't return the right value in
3932 * tmAveCharWidth, so we have to figure out the dialog base units
3933 * ourselves.
3934 */
3935 GetTextExtentPoint(hdc,
3936 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
3937 52, &size);
3938 ReleaseDC(s_hwnd, hdc);
3939
3940 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
3941 s_dlgfntheight = (WORD)size.cy;
3942 s_usenewlook = TRUE;
3943 }
3944 }
3945
3946 if (!s_usenewlook)
3947 {
3948 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
3949 s_dlgfntwidth = LOWORD(dlgFontSize);
3950 s_dlgfntheight = HIWORD(dlgFontSize);
3951 }
3952}
3953
3954#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
3955/*
3956 * Create a pseudo-"tearoff menu" based on the child
3957 * items of a given menu pointer.
3958 */
3959 static void
3960gui_mch_tearoff(
3961 char_u *title,
3962 vimmenu_T *menu,
3963 int initX,
3964 int initY)
3965{
3966 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
3967 int template_len;
3968 int nchar, textWidth, submenuWidth;
3969 DWORD lStyle;
3970 DWORD lExtendedStyle;
3971 WORD dlgwidth;
3972 WORD menuID;
3973 vimmenu_T *pmenu;
3974 vimmenu_T *the_menu = menu;
3975 HWND hwnd;
3976 HDC hdc;
3977 HFONT font, oldFont;
3978 int col, spaceWidth, len;
3979 int columnWidths[2];
3980 char_u *label, *text;
3981 int acLen = 0;
3982 int nameLen;
3983 int padding0, padding1, padding2 = 0;
3984 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003985 int x;
3986 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987#ifdef USE_SYSMENU_FONT
3988 LOGFONT lfSysmenu;
3989 int use_lfSysmenu = FALSE;
3990#endif
3991
3992 /*
3993 * If this menu is already torn off, move it to the mouse position.
3994 */
3995 if (IsWindow(menu->tearoff_handle))
3996 {
3997 POINT mp;
3998 if (GetCursorPos((LPPOINT)&mp))
3999 {
4000 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
4001 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
4002 }
4003 return;
4004 }
4005
4006 /*
4007 * Create a new tearoff.
4008 */
4009 if (*title == MNU_HIDDEN_CHAR)
4010 title++;
4011
4012 /* Allocate memory to store the dialog template. It's made bigger when
4013 * needed. */
4014 template_len = DLG_ALLOC_SIZE;
4015 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
4016 if (p == NULL)
4017 return;
4018
4019 hwnd = GetDesktopWindow();
4020 hdc = GetWindowDC(hwnd);
4021#ifdef USE_SYSMENU_FONT
4022 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
4023 {
4024 font = CreateFontIndirect(&lfSysmenu);
4025 use_lfSysmenu = TRUE;
4026 }
4027 else
4028#endif
4029 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4030 VARIABLE_PITCH , DLG_FONT_NAME);
4031 if (s_usenewlook)
4032 oldFont = SelectFont(hdc, font);
4033 else
4034 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
4035
4036 /* Calculate width of a single space. Used for padding columns to the
4037 * right width. */
4038 spaceWidth = GetTextWidth(hdc, " ", 1);
4039
4040 /* Figure out max width of the text column, the accelerator column and the
4041 * optional submenu column. */
4042 submenuWidth = 0;
4043 for (col = 0; col < 2; col++)
4044 {
4045 columnWidths[col] = 0;
4046 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
4047 {
4048 /* Use "dname" here to compute the width of the visible text. */
4049 text = (col == 0) ? pmenu->dname : pmenu->actext;
4050 if (text != NULL && *text != NUL)
4051 {
4052 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
4053 if (textWidth > columnWidths[col])
4054 columnWidths[col] = textWidth;
4055 }
4056 if (pmenu->children != NULL)
4057 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
4058 }
4059 }
4060 if (columnWidths[1] == 0)
4061 {
4062 /* no accelerators */
4063 if (submenuWidth != 0)
4064 columnWidths[0] += submenuWidth;
4065 else
4066 columnWidths[0] += spaceWidth;
4067 }
4068 else
4069 {
4070 /* there is an accelerator column */
4071 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
4072 columnWidths[1] += submenuWidth;
4073 }
4074
4075 /*
4076 * Now find the total width of our 'menu'.
4077 */
4078 textWidth = columnWidths[0] + columnWidths[1];
4079 if (submenuWidth != 0)
4080 {
4081 submenuWidth = GetTextWidth(hdc, TEAROFF_SUBMENU_LABEL,
4082 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
4083 textWidth += submenuWidth;
4084 }
4085 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
4086 if (textWidth > dlgwidth)
4087 dlgwidth = textWidth;
4088 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
4089
4090 /* W95 can't do thin dialogs, they look v. weird! */
4091 if (mch_windows95() && dlgwidth < TEAROFF_MIN_WIDTH)
4092 dlgwidth = TEAROFF_MIN_WIDTH;
4093
4094 /* start to fill in the dlgtemplate information. addressing by WORDs */
4095 if (s_usenewlook)
4096 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
4097 else
4098 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
4099
4100 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
4101 *p++ = LOWORD(lStyle);
4102 *p++ = HIWORD(lStyle);
4103 *p++ = LOWORD(lExtendedStyle);
4104 *p++ = HIWORD(lExtendedStyle);
4105 pnumitems = p; /* save where the number of items must be stored */
4106 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004107 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004109 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 else
4111 *p++ = PixelToDialogX(initX); // x
4112 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004113 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 else
4115 *p++ = PixelToDialogY(initY); // y
4116 *p++ = PixelToDialogX(dlgwidth); // cx
4117 ptrueheight = p;
4118 *p++ = 0; // dialog height: changed later anyway
4119 *p++ = 0; // Menu
4120 *p++ = 0; // Class
4121
4122 /* copy the title of the dialog */
4123 nchar = nCopyAnsiToWideChar(p, ((*title)
4124 ? (LPSTR)title
4125 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
4126 p += nchar;
4127
4128 if (s_usenewlook)
4129 {
4130 /* do the font, since DS_3DLOOK doesn't work properly */
4131#ifdef USE_SYSMENU_FONT
4132 if (use_lfSysmenu)
4133 {
4134 /* point size */
4135 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
4136 GetDeviceCaps(hdc, LOGPIXELSY));
4137 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
4138 }
4139 else
4140#endif
4141 {
4142 *p++ = DLG_FONT_POINT_SIZE; // point size
4143 nchar = nCopyAnsiToWideChar (p, TEXT(DLG_FONT_NAME));
4144 }
4145 p += nchar;
4146 }
4147
4148 /*
4149 * Loop over all the items in the menu.
4150 * But skip over the tearbar.
4151 */
4152 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
4153 menu = menu->children->next;
4154 else
4155 menu = menu->children;
4156 for ( ; menu != NULL; menu = menu->next)
4157 {
4158 if (menu->modes == 0) /* this menu has just been deleted */
4159 continue;
4160 if (menu_is_separator(menu->dname))
4161 {
4162 sepPadding += 3;
4163 continue;
4164 }
4165
4166 /* Check if there still is plenty of room in the template. Make it
4167 * larger when needed. */
4168 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
4169 {
4170 WORD *newp;
4171
4172 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
4173 if (newp != NULL)
4174 {
4175 template_len += 4096;
4176 mch_memmove(newp, pdlgtemplate,
4177 (char *)p - (char *)pdlgtemplate);
4178 p = newp + (p - pdlgtemplate);
4179 pnumitems = newp + (pnumitems - pdlgtemplate);
4180 ptrueheight = newp + (ptrueheight - pdlgtemplate);
4181 LocalFree(LocalHandle(pdlgtemplate));
4182 pdlgtemplate = newp;
4183 }
4184 }
4185
4186 /* Figure out minimal length of this menu label. Use "name" for the
4187 * actual text, "dname" for estimating the displayed size. "name"
4188 * has "&a" for mnemonic and includes the accelerator. */
4189 len = nameLen = (int)STRLEN(menu->name);
4190 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
4191 (int)STRLEN(menu->dname))) / spaceWidth;
4192 len += padding0;
4193
4194 if (menu->actext != NULL)
4195 {
4196 acLen = (int)STRLEN(menu->actext);
4197 len += acLen;
4198 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
4199 }
4200 else
4201 textWidth = 0;
4202 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
4203 len += padding1;
4204
4205 if (menu->children == NULL)
4206 {
4207 padding2 = submenuWidth / spaceWidth;
4208 len += padding2;
4209 menuID = (WORD)(menu->id);
4210 }
4211 else
4212 {
4213 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004214 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 }
4216
4217 /* Allocate menu label and fill it in */
4218 text = label = alloc((unsigned)len + 1);
4219 if (label == NULL)
4220 break;
4221
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004222 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 text = vim_strchr(text, TAB); /* stop at TAB before actext */
4224 if (text == NULL)
4225 text = label + nameLen; /* no actext, use whole name */
4226 while (padding0-- > 0)
4227 *text++ = ' ';
4228 if (menu->actext != NULL)
4229 {
4230 STRNCPY(text, menu->actext, acLen);
4231 text += acLen;
4232 }
4233 while (padding1-- > 0)
4234 *text++ = ' ';
4235 if (menu->children != NULL)
4236 {
4237 STRCPY(text, TEAROFF_SUBMENU_LABEL);
4238 text += STRLEN(TEAROFF_SUBMENU_LABEL);
4239 }
4240 else
4241 {
4242 while (padding2-- > 0)
4243 *text++ = ' ';
4244 }
4245 *text = NUL;
4246
4247 /*
4248 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
4249 * W95/NT4 it makes the tear-off look more like a menu.
4250 */
4251 p = add_dialog_element(p,
4252 BS_PUSHBUTTON|BS_LEFT,
4253 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
4254 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
4255 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
4256 (WORD)12,
4257 menuID, (WORD)0x0080, label);
4258 vim_free(label);
4259 (*pnumitems)++;
4260 }
4261
4262 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
4263
4264
4265 /* show modelessly */
4266 the_menu->tearoff_handle = CreateDialogIndirect(
4267 s_hinst,
4268 (LPDLGTEMPLATE)pdlgtemplate,
4269 s_hwnd,
4270 (DLGPROC)tearoff_callback);
4271
4272 LocalFree(LocalHandle(pdlgtemplate));
4273 SelectFont(hdc, oldFont);
4274 DeleteObject(font);
4275 ReleaseDC(hwnd, hdc);
4276
4277 /*
4278 * Reassert ourselves as the active window. This is so that after creating
4279 * a tearoff, the user doesn't have to click with the mouse just to start
4280 * typing again!
4281 */
4282 (void)SetActiveWindow(s_hwnd);
4283
4284 /* make sure the right buttons are enabled */
4285 force_menu_update = TRUE;
4286}
4287#endif
4288
4289#if defined(FEAT_TOOLBAR) || defined(PROTO)
4290#include "gui_w32_rc.h"
4291
4292/* This not defined in older SDKs */
4293# ifndef TBSTYLE_FLAT
4294# define TBSTYLE_FLAT 0x0800
4295# endif
4296
4297/*
4298 * Create the toolbar, initially unpopulated.
4299 * (just like the menu, there are no defaults, it's all
4300 * set up through menu.vim)
4301 */
4302 static void
4303initialise_toolbar(void)
4304{
4305 InitCommonControls();
4306 s_toolbarhwnd = CreateToolbarEx(
4307 s_hwnd,
4308 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
4309 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004310 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 s_hinst,
4312 IDR_TOOLBAR1, // id of initial bitmap
4313 NULL,
4314 0, // initial number of buttons
4315 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
4316 TOOLBAR_BUTTON_HEIGHT,
4317 TOOLBAR_BUTTON_WIDTH,
4318 TOOLBAR_BUTTON_HEIGHT,
4319 sizeof(TBBUTTON)
4320 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004321 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
4323 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
4324}
4325
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004326 static LRESULT CALLBACK
4327toolbar_wndproc(
4328 HWND hwnd,
4329 UINT uMsg,
4330 WPARAM wParam,
4331 LPARAM lParam)
4332{
4333 HandleMouseHide(uMsg, lParam);
4334 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
4335}
4336
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 static int
4338get_toolbar_bitmap(vimmenu_T *menu)
4339{
4340 int i = -1;
4341
4342 /*
4343 * Check user bitmaps first, unless builtin is specified.
4344 */
4345 if (!is_winnt_3() && !menu->icon_builtin)
4346 {
4347 char_u fname[MAXPATHL];
4348 HANDLE hbitmap = NULL;
4349
4350 if (menu->iconfile != NULL)
4351 {
4352 gui_find_iconfile(menu->iconfile, fname, "bmp");
4353 hbitmap = LoadImage(
4354 NULL,
4355 fname,
4356 IMAGE_BITMAP,
4357 TOOLBAR_BUTTON_WIDTH,
4358 TOOLBAR_BUTTON_HEIGHT,
4359 LR_LOADFROMFILE |
4360 LR_LOADMAP3DCOLORS
4361 );
4362 }
4363
4364 /*
4365 * If the LoadImage call failed, or the "icon=" file
4366 * didn't exist or wasn't specified, try the menu name
4367 */
4368 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02004369 && (gui_find_bitmap(
4370#ifdef FEAT_MULTI_LANG
4371 menu->en_dname != NULL ? menu->en_dname :
4372#endif
4373 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 hbitmap = LoadImage(
4375 NULL,
4376 fname,
4377 IMAGE_BITMAP,
4378 TOOLBAR_BUTTON_WIDTH,
4379 TOOLBAR_BUTTON_HEIGHT,
4380 LR_LOADFROMFILE |
4381 LR_LOADMAP3DCOLORS
4382 );
4383
4384 if (hbitmap != NULL)
4385 {
4386 TBADDBITMAP tbAddBitmap;
4387
4388 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004389 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
4391 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
4392 (WPARAM)1, (LPARAM)&tbAddBitmap);
4393 /* i will be set to -1 if it fails */
4394 }
4395 }
4396 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
4397 i = menu->iconidx;
4398
4399 return i;
4400}
4401#endif
4402
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004403#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
4404 static void
4405initialise_tabline(void)
4406{
4407 InitCommonControls();
4408
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004409 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004410 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004411 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4412 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004413 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004414
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004415 gui.tabline_height = TABLINE_HEIGHT;
4416
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004417# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004418 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004419# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004420}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004421
4422 static LRESULT CALLBACK
4423tabline_wndproc(
4424 HWND hwnd,
4425 UINT uMsg,
4426 WPARAM wParam,
4427 LPARAM lParam)
4428{
4429 HandleMouseHide(uMsg, lParam);
4430 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
4431}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004432#endif
4433
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
4435/*
4436 * Make the GUI window come to the foreground.
4437 */
4438 void
4439gui_mch_set_foreground(void)
4440{
4441 if (IsIconic(s_hwnd))
4442 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
4443 SetForegroundWindow(s_hwnd);
4444}
4445#endif
4446
4447#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4448 static void
4449dyn_imm_load(void)
4450{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02004451 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 if (hLibImm == NULL)
4453 return;
4454
4455 pImmGetCompositionStringA
4456 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
4457 pImmGetCompositionStringW
4458 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
4459 pImmGetContext
4460 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
4461 pImmAssociateContext
4462 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
4463 pImmReleaseContext
4464 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
4465 pImmGetOpenStatus
4466 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
4467 pImmSetOpenStatus
4468 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
4469 pImmGetCompositionFont
4470 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
4471 pImmSetCompositionFont
4472 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
4473 pImmSetCompositionWindow
4474 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
4475 pImmGetConversionStatus
4476 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00004477 pImmSetConversionStatus
4478 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479
4480 if ( pImmGetCompositionStringA == NULL
4481 || pImmGetCompositionStringW == NULL
4482 || pImmGetContext == NULL
4483 || pImmAssociateContext == NULL
4484 || pImmReleaseContext == NULL
4485 || pImmGetOpenStatus == NULL
4486 || pImmSetOpenStatus == NULL
4487 || pImmGetCompositionFont == NULL
4488 || pImmSetCompositionFont == NULL
4489 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00004490 || pImmGetConversionStatus == NULL
4491 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
4493 FreeLibrary(hLibImm);
4494 hLibImm = NULL;
4495 pImmGetContext = NULL;
4496 return;
4497 }
4498
4499 return;
4500}
4501
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502#endif
4503
4504#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
4505
4506# ifdef FEAT_XPM_W32
4507# define IMAGE_XPM 100
4508# endif
4509
4510typedef struct _signicon_t
4511{
4512 HANDLE hImage;
4513 UINT uType;
4514#ifdef FEAT_XPM_W32
4515 HANDLE hShape; /* Mask bitmap handle */
4516#endif
4517} signicon_t;
4518
4519 void
4520gui_mch_drawsign(row, col, typenr)
4521 int row;
4522 int col;
4523 int typenr;
4524{
4525 signicon_t *sign;
4526 int x, y, w, h;
4527
4528 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
4529 return;
4530
4531 x = TEXT_X(col);
4532 y = TEXT_Y(row);
4533 w = gui.char_width * 2;
4534 h = gui.char_height;
4535 switch (sign->uType)
4536 {
4537 case IMAGE_BITMAP:
4538 {
4539 HDC hdcMem;
4540 HBITMAP hbmpOld;
4541
4542 hdcMem = CreateCompatibleDC(s_hdc);
4543 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
4544 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
4545 SelectObject(hdcMem, hbmpOld);
4546 DeleteDC(hdcMem);
4547 }
4548 break;
4549 case IMAGE_ICON:
4550 case IMAGE_CURSOR:
4551 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
4552 break;
4553#ifdef FEAT_XPM_W32
4554 case IMAGE_XPM:
4555 {
4556 HDC hdcMem;
4557 HBITMAP hbmpOld;
4558
4559 hdcMem = CreateCompatibleDC(s_hdc);
4560 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
4561 /* Make hole */
4562 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
4563
4564 SelectObject(hdcMem, sign->hImage);
4565 /* Paint sign */
4566 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
4567 SelectObject(hdcMem, hbmpOld);
4568 DeleteDC(hdcMem);
4569 }
4570 break;
4571#endif
4572 }
4573}
4574
4575 static void
4576close_signicon_image(signicon_t *sign)
4577{
4578 if (sign)
4579 switch (sign->uType)
4580 {
4581 case IMAGE_BITMAP:
4582 DeleteObject((HGDIOBJ)sign->hImage);
4583 break;
4584 case IMAGE_CURSOR:
4585 DestroyCursor((HCURSOR)sign->hImage);
4586 break;
4587 case IMAGE_ICON:
4588 DestroyIcon((HICON)sign->hImage);
4589 break;
4590#ifdef FEAT_XPM_W32
4591 case IMAGE_XPM:
4592 DeleteObject((HBITMAP)sign->hImage);
4593 DeleteObject((HBITMAP)sign->hShape);
4594 break;
4595#endif
4596 }
4597}
4598
4599 void *
4600gui_mch_register_sign(signfile)
4601 char_u *signfile;
4602{
4603 signicon_t sign, *psign;
4604 char_u *ext;
4605
4606 if (is_winnt_3())
4607 {
4608 EMSG(_(e_signdata));
4609 return NULL;
4610 }
4611
4612 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004613 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 if (ext > signfile)
4615 {
4616 int do_load = 1;
4617
4618 if (!STRICMP(ext, ".bmp"))
4619 sign.uType = IMAGE_BITMAP;
4620 else if (!STRICMP(ext, ".ico"))
4621 sign.uType = IMAGE_ICON;
4622 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
4623 sign.uType = IMAGE_CURSOR;
4624 else
4625 do_load = 0;
4626
4627 if (do_load)
4628 sign.hImage = (HANDLE)LoadImage(NULL, signfile, sign.uType,
4629 gui.char_width * 2, gui.char_height,
4630 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
4631#ifdef FEAT_XPM_W32
4632 if (!STRICMP(ext, ".xpm"))
4633 {
4634 sign.uType = IMAGE_XPM;
4635 LoadXpmImage(signfile, (HBITMAP *)&sign.hImage, (HBITMAP *)&sign.hShape);
4636 }
4637#endif
4638 }
4639
4640 psign = NULL;
4641 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
4642 != NULL)
4643 *psign = sign;
4644
4645 if (!psign)
4646 {
4647 if (sign.hImage)
4648 close_signicon_image(&sign);
4649 EMSG(_(e_signdata));
4650 }
4651 return (void *)psign;
4652
4653}
4654
4655 void
4656gui_mch_destroy_sign(sign)
4657 void *sign;
4658{
4659 if (sign)
4660 {
4661 close_signicon_image((signicon_t *)sign);
4662 vim_free(sign);
4663 }
4664}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666
4667#if defined(FEAT_BEVAL) || defined(PROTO)
4668
4669/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004670 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 *
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004672 * The only reused thing is gui_beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 * from gui_beval.c (note it uses x and y of the BalloonEval struct
4674 * to get current mouse position).
4675 *
4676 * Trying to use as more Windows services as possible, and as less
4677 * IE version as possible :)).
4678 *
4679 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
4680 * BalloonEval struct.
4681 * 2) Enable/Disable simply create/kill BalloonEval Timer
4682 * 3) When there was enough inactivity, timer procedure posts
4683 * async request to debugger
4684 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
4685 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00004686 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 */
4688
Bram Moolenaar45360022005-07-21 21:08:21 +00004689/*
4690 * determine whether installed Common Controls support multiline tooltips
4691 * (i.e. their version is >= 4.70
4692 */
4693 int
4694multiline_balloon_available(void)
4695{
4696 HINSTANCE hDll;
4697 static char comctl_dll[] = "comctl32.dll";
4698 static int multiline_tip = MAYBE;
4699
4700 if (multiline_tip != MAYBE)
4701 return multiline_tip;
4702
4703 hDll = GetModuleHandle(comctl_dll);
4704 if (hDll != NULL)
4705 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004706 DLLGETVERSIONPROC pGetVer;
4707 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00004708
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004709 if (pGetVer != NULL)
4710 {
4711 DLLVERSIONINFO dvi;
4712 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00004713
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004714 ZeroMemory(&dvi, sizeof(dvi));
4715 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00004716
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004717 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00004718
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004719 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00004720 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004721 || (dvi.dwMajorVersion == 4
4722 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00004723 {
4724 multiline_tip = TRUE;
4725 return multiline_tip;
4726 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004727 }
Bram Moolenaar45360022005-07-21 21:08:21 +00004728 else
4729 {
4730 /* there is chance we have ancient CommCtl 4.70
4731 which doesn't export DllGetVersion */
4732 DWORD dwHandle = 0;
4733 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
4734 if (len > 0)
4735 {
4736 VS_FIXEDFILEINFO *ver;
4737 UINT vlen = 0;
4738 void *data = alloc(len);
4739
4740 if (data != NULL
4741 && GetFileVersionInfo(comctl_dll, 0, len, data)
4742 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
4743 && vlen
4744 && HIWORD(ver->dwFileVersionMS) > 4
4745 || (HIWORD(ver->dwFileVersionMS) == 4
4746 && LOWORD(ver->dwFileVersionMS) >= 70))
4747 {
4748 vim_free(data);
4749 multiline_tip = TRUE;
4750 return multiline_tip;
4751 }
4752 vim_free(data);
4753 }
4754 }
4755 }
4756 multiline_tip = FALSE;
4757 return multiline_tip;
4758}
4759
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 static void
4761make_tooltip(beval, text, pt)
4762 BalloonEval *beval;
4763 char *text;
4764 POINT pt;
4765{
Bram Moolenaar45360022005-07-21 21:08:21 +00004766 TOOLINFO *pti;
4767 int ToolInfoSize;
4768
4769 if (multiline_balloon_available() == TRUE)
4770 ToolInfoSize = sizeof(TOOLINFO_NEW);
4771 else
4772 ToolInfoSize = sizeof(TOOLINFO);
4773
4774 pti = (TOOLINFO *)alloc(ToolInfoSize);
4775 if (pti == NULL)
4776 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777
4778 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
4779 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
4780 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4781 beval->target, NULL, s_hinst, NULL);
4782
4783 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
4784 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
4785
Bram Moolenaar45360022005-07-21 21:08:21 +00004786 pti->cbSize = ToolInfoSize;
4787 pti->uFlags = TTF_SUBCLASS;
4788 pti->hwnd = beval->target;
4789 pti->hinst = 0; /* Don't use string resources */
4790 pti->uId = ID_BEVAL_TOOLTIP;
4791
4792 if (multiline_balloon_available() == TRUE)
4793 {
4794 RECT rect;
4795 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
4796 pti->lpszText = LPSTR_TEXTCALLBACK;
4797 ptin->lParam = (LPARAM)text;
4798 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
4799 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
4800 (LPARAM)rect.right);
4801 }
4802 else
4803 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804
4805 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00004806 pti->rect.left = pt.x - 3;
4807 pti->rect.top = pt.y - 3;
4808 pti->rect.right = pt.x + 3;
4809 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810
Bram Moolenaar45360022005-07-21 21:08:21 +00004811 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 /* Make tooltip appear sooner */
4813 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00004814 /* I've performed some tests and it seems the longest possible life time
4815 * of tooltip is 30 seconds */
4816 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 /*
4818 * HACK: force tooltip to appear, because it'll not appear until
4819 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00004820 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00004822 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00004824 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825}
4826
4827 static void
4828delete_tooltip(beval)
4829 BalloonEval *beval;
4830{
4831 DestroyWindow(beval->balloon);
4832}
4833
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004834/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 static VOID CALLBACK
4836BevalTimerProc(hwnd, uMsg, idEvent, dwTime)
4837 HWND hwnd;
4838 UINT uMsg;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004839 UINT_PTR idEvent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 DWORD dwTime;
4841{
4842 POINT pt;
4843 RECT rect;
4844
4845 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
4846 return;
4847
4848 GetCursorPos(&pt);
4849 if (WindowFromPoint(pt) != s_textArea)
4850 return;
4851
4852 ScreenToClient(s_textArea, &pt);
4853 GetClientRect(s_textArea, &rect);
4854 if (!PtInRect(&rect, pt))
4855 return;
4856
4857 if (LastActivity > 0
4858 && (dwTime - LastActivity) >= (DWORD)p_bdlay
4859 && (cur_beval->showState != ShS_PENDING
4860 || abs(cur_beval->x - pt.x) > 3
4861 || abs(cur_beval->y - pt.y) > 3))
4862 {
4863 /* Pointer resting in one place long enough, it's time to show
4864 * the tooltip. */
4865 cur_beval->showState = ShS_PENDING;
4866 cur_beval->x = pt.x;
4867 cur_beval->y = pt.y;
4868
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004869 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870
4871 if (cur_beval->msgCB != NULL)
4872 (*cur_beval->msgCB)(cur_beval, 0);
4873 }
4874}
4875
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004876/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 void
4878gui_mch_disable_beval_area(beval)
4879 BalloonEval *beval;
4880{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004881 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004883 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884}
4885
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004886/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 void
4888gui_mch_enable_beval_area(beval)
4889 BalloonEval *beval;
4890{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004891 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 if (beval == NULL)
4893 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004894 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02004895 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004896 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897}
4898
4899 void
4900gui_mch_post_balloon(beval, mesg)
4901 BalloonEval *beval;
4902 char_u *mesg;
4903{
4904 POINT pt;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004905 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 if (beval->showState == ShS_SHOWING)
4907 return;
4908 GetCursorPos(&pt);
4909 ScreenToClient(s_textArea, &pt);
4910
4911 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
4912 /* cursor is still here */
4913 {
4914 gui_mch_disable_beval_area(cur_beval);
4915 beval->showState = ShS_SHOWING;
4916 make_tooltip(beval, mesg, pt);
4917 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004918 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919}
4920
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004921/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 BalloonEval *
4923gui_mch_create_beval_area(target, mesg, mesgCB, clientData)
4924 void *target; /* ignored, always use s_textArea */
4925 char_u *mesg;
4926 void (*mesgCB)__ARGS((BalloonEval *, int));
4927 void *clientData;
4928{
4929 /* partially stolen from gui_beval.c */
4930 BalloonEval *beval;
4931
4932 if (mesg != NULL && mesgCB != NULL)
4933 {
4934 EMSG(_("E232: Cannot create BalloonEval with both message and callback"));
4935 return NULL;
4936 }
4937
4938 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
4939 if (beval != NULL)
4940 {
4941 beval->target = s_textArea;
4942 beval->balloon = NULL;
4943
4944 beval->showState = ShS_NEUTRAL;
4945 beval->x = 0;
4946 beval->y = 0;
4947 beval->msg = mesg;
4948 beval->msgCB = mesgCB;
4949 beval->clientData = clientData;
4950
4951 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 cur_beval = beval;
4953
4954 if (p_beval)
4955 gui_mch_enable_beval_area(beval);
4956
4957 }
4958 return beval;
4959}
4960
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004961/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 static void
Bram Moolenaar442b4222010-05-24 21:34:22 +02004963Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964{
4965 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
4966 return;
4967
4968 if (cur_beval != NULL)
4969 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004970 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004972 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004973 // TRACE0("TTN_SHOW {{{");
4974 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00004975 break;
4976 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004977 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 delete_tooltip(cur_beval);
4979 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004980 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981
4982 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00004983 break;
4984 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004985 {
4986 /* if you get there then we have new common controls */
4987 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
4988 info->lpszText = (LPSTR)info->lParam;
4989 info->uFlags |= TTF_DI_SETITEM;
4990 }
Bram Moolenaar45360022005-07-21 21:08:21 +00004991 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 }
4993 }
4994}
4995
4996 static void
4997TrackUserActivity(UINT uMsg)
4998{
4999 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
5000 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
5001 LastActivity = GetTickCount();
5002}
5003
5004 void
5005gui_mch_destroy_beval_area(beval)
5006 BalloonEval *beval;
5007{
5008 vim_free(beval);
5009}
5010#endif /* FEAT_BEVAL */
5011
5012#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5013/*
5014 * We have multiple signs to draw at the same location. Draw the
5015 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
5016 */
5017 void
5018netbeans_draw_multisign_indicator(int row)
5019{
5020 int i;
5021 int y;
5022 int x;
5023
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005024 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005025 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005026
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 x = 0;
5028 y = TEXT_Y(row);
5029
5030 for (i = 0; i < gui.char_height - 3; i++)
5031 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
5032
5033 SetPixel(s_hdc, x+0, y, gui.currFgColor);
5034 SetPixel(s_hdc, x+2, y, gui.currFgColor);
5035 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
5036 SetPixel(s_hdc, x+1, y, gui.currFgColor);
5037 SetPixel(s_hdc, x+2, y, gui.currFgColor);
5038 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
5039 SetPixel(s_hdc, x+2, y, gui.currFgColor);
5040}
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005041
5042/*
5043 * Initialize the Winsock dll.
5044 */
5045 void
5046netbeans_init_winsock()
5047{
5048 WSADATA wsaData;
5049 int wsaerr;
5050
5051 if (WSInitialized)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005052 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005053
5054 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
5055 if (wsaerr == 0)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005056 WSInitialized = TRUE;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005057}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058#endif