blob: 0368dda439ce18ce3f20bbd1b7aac48e46f3c911 [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 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02002593 /* Add one to "cells" for italics. */
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002594 DWriteContext_DrawText(s_dwc, s_hdc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02002595 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002596 gui.char_width, gui.currFgColor);
2597 }
2598 else
2599#endif
2600 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
2601 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 len = cells; /* used for underlining */
2603 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002604 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 {
2606 /* If we want to display codepage data, and the current CP is not the
2607 * ANSI one, we need to go via Unicode. */
2608 if (unicodebuf != NULL)
2609 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002610 if (enc_latin9)
2611 latin9_to_ucs(text, len, unicodebuf);
2612 else
2613 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 MB_PRECOMPOSED,
2615 (char *)text, len,
2616 (LPWSTR)unicodebuf, unibuflen);
2617 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002618 {
2619 /* Use unicodepdy to make characters fit as we expect, even
2620 * when the font uses different widths (e.g., bold character
2621 * is wider). */
2622 if (unicodepdy != NULL)
2623 {
2624 int i;
2625 int cw;
2626
2627 for (i = 0; i < len; ++i)
2628 {
2629 cw = utf_char2cells(unicodebuf[i]);
2630 if (cw > 2)
2631 cw = 1;
2632 unicodepdy[i] = cw * gui.char_width;
2633 }
2634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002636 foptions, pcliprect, unicodebuf, len, unicodepdy);
2637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 }
2639 }
2640 else
2641#endif
2642 {
2643#ifdef FEAT_RIGHTLEFT
2644 /* If we can't use ETO_IGNORELANGUAGE, we can't tell Windows not to
2645 * mess up RL text, so we have to draw it character-by-character.
2646 * Only do this if RL is on, since it's slow. */
2647 if (curwin->w_p_rl && !(foptions & ETO_IGNORELANGUAGE))
2648 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
2649 foptions, pcliprect, (char *)text, len, padding);
2650 else
2651#endif
2652 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
2653 foptions, pcliprect, (char *)text, len, padding);
2654 }
2655
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002656 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 if (flags & DRAW_UNDERL)
2658 {
2659 hpen = CreatePen(PS_SOLID, 1, gui.currFgColor);
2660 old_pen = SelectObject(s_hdc, hpen);
2661 /* When p_linespace is 0, overwrite the bottom row of pixels.
2662 * Otherwise put the line just below the character. */
2663 y = FILL_Y(row + 1) - 1;
2664#ifndef MSWIN16_FASTTEXT
2665 if (p_linespace > 1)
2666 y -= p_linespace - 1;
2667#endif
2668 MoveToEx(s_hdc, FILL_X(col), y, NULL);
2669 /* Note: LineTo() excludes the last pixel in the line. */
2670 LineTo(s_hdc, FILL_X(col + len), y);
2671 DeleteObject(SelectObject(s_hdc, old_pen));
2672 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002673
2674 /* Undercurl */
2675 if (flags & DRAW_UNDERC)
2676 {
2677 int x;
2678 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002679 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002680
2681 y = FILL_Y(row + 1) - 1;
2682 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
2683 {
2684 offset = val[x % 8];
2685 SetPixel(s_hdc, x, y - offset, gui.currSpColor);
2686 }
2687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688}
2689
2690
2691/*
2692 * Output routines.
2693 */
2694
2695/* Flush any output to the screen */
2696 void
2697gui_mch_flush(void)
2698{
2699# if defined(__BORLANDC__)
2700 /*
2701 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
2702 * prototype declaration.
2703 * The compiler complains if __stdcall is not used in both declarations.
2704 */
2705 BOOL __stdcall GdiFlush(void);
2706# endif
2707
2708 GdiFlush();
2709}
2710
2711 static void
2712clear_rect(RECT *rcp)
2713{
2714 HBRUSH hbr;
2715
2716 hbr = CreateSolidBrush(gui.back_pixel);
2717 FillRect(s_hdc, rcp, hbr);
2718 DeleteBrush(hbr);
2719}
2720
2721
Bram Moolenaarc716c302006-01-21 22:12:51 +00002722 void
2723gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
2724{
2725 RECT workarea_rect;
2726
2727 get_work_area(&workarea_rect);
2728
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002729 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02002730 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002731 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00002732
2733 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
2734 * the menubar for MSwin, we subtract it from the screen height, so that
2735 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002736 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02002737 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02002738 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00002739 - GetSystemMetrics(SM_CYCAPTION)
2740#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002741 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00002742#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002743 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00002744}
2745
2746
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747#if defined(FEAT_MENU) || defined(PROTO)
2748/*
2749 * Add a sub menu to the menu bar.
2750 */
2751 void
2752gui_mch_add_menu(
2753 vimmenu_T *menu,
2754 int pos)
2755{
2756 vimmenu_T *parent = menu->parent;
2757
2758 menu->submenu_id = CreatePopupMenu();
2759 menu->id = s_menu_id++;
2760
2761 if (menu_is_menubar(menu->name))
2762 {
2763 if (is_winnt_3())
2764 {
2765 InsertMenu((parent == NULL) ? s_menuBar : parent->submenu_id,
2766 (UINT)pos, MF_POPUP | MF_STRING | MF_BYPOSITION,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002767 (long_u)menu->submenu_id, (LPCTSTR) menu->name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
2769 else
2770 {
2771#ifdef FEAT_MBYTE
2772 WCHAR *wn = NULL;
2773 int n;
2774
2775 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2776 {
2777 /* 'encoding' differs from active codepage: convert menu name
2778 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002779 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 if (wn != NULL)
2781 {
2782 MENUITEMINFOW infow;
2783
2784 infow.cbSize = sizeof(infow);
2785 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
2786 | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002787 infow.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 infow.wID = menu->id;
2789 infow.fType = MFT_STRING;
2790 infow.dwTypeData = wn;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002791 infow.cch = (UINT)wcslen(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 infow.hSubMenu = menu->submenu_id;
2793 n = InsertMenuItemW((parent == NULL)
2794 ? s_menuBar : parent->submenu_id,
2795 (UINT)pos, TRUE, &infow);
2796 vim_free(wn);
2797 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2798 /* Failed, try using non-wide function. */
2799 wn = NULL;
2800 }
2801 }
2802
2803 if (wn == NULL)
2804#endif
2805 {
2806 MENUITEMINFO info;
2807
2808 info.cbSize = sizeof(info);
2809 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002810 info.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 info.wID = menu->id;
2812 info.fType = MFT_STRING;
2813 info.dwTypeData = (LPTSTR)menu->name;
2814 info.cch = (UINT)STRLEN(menu->name);
2815 info.hSubMenu = menu->submenu_id;
2816 InsertMenuItem((parent == NULL)
2817 ? s_menuBar : parent->submenu_id,
2818 (UINT)pos, TRUE, &info);
2819 }
2820 }
2821 }
2822
2823 /* Fix window size if menu may have wrapped */
2824 if (parent == NULL)
2825 gui_mswin_get_menu_height(!gui.starting);
2826#ifdef FEAT_TEAROFF
2827 else if (IsWindow(parent->tearoff_handle))
2828 rebuild_tearoff(parent);
2829#endif
2830}
2831
2832 void
2833gui_mch_show_popupmenu(vimmenu_T *menu)
2834{
2835 POINT mp;
2836
2837 (void)GetCursorPos((LPPOINT)&mp);
2838 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
2839}
2840
2841 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00002842gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843{
2844 vimmenu_T *menu = gui_find_menu(path_name);
2845
2846 if (menu != NULL)
2847 {
2848 POINT p;
2849
2850 /* Find the position of the current cursor */
2851 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00002852 if (mouse_pos)
2853 {
2854 int mx, my;
2855
2856 gui_mch_getmouse(&mx, &my);
2857 p.x += mx;
2858 p.y += my;
2859 }
2860 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 {
2862 p.x += TEXT_X(W_WINCOL(curwin) + curwin->w_wcol + 1);
2863 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
2864 }
2865 msg_scroll = FALSE;
2866 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
2867 }
2868}
2869
2870#if defined(FEAT_TEAROFF) || defined(PROTO)
2871/*
2872 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
2873 * create it as a pseudo-"tearoff menu".
2874 */
2875 void
2876gui_make_tearoff(char_u *path_name)
2877{
2878 vimmenu_T *menu = gui_find_menu(path_name);
2879
2880 /* Found the menu, so tear it off. */
2881 if (menu != NULL)
2882 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
2883}
2884#endif
2885
2886/*
2887 * Add a menu item to a menu
2888 */
2889 void
2890gui_mch_add_menu_item(
2891 vimmenu_T *menu,
2892 int idx)
2893{
2894 vimmenu_T *parent = menu->parent;
2895
2896 menu->id = s_menu_id++;
2897 menu->submenu_id = NULL;
2898
2899#ifdef FEAT_TEAROFF
2900 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
2901 {
2902 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
2903 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
2904 }
2905 else
2906#endif
2907#ifdef FEAT_TOOLBAR
2908 if (menu_is_toolbar(parent->name))
2909 {
2910 TBBUTTON newtb;
2911
2912 vim_memset(&newtb, 0, sizeof(newtb));
2913 if (menu_is_separator(menu->name))
2914 {
2915 newtb.iBitmap = 0;
2916 newtb.fsStyle = TBSTYLE_SEP;
2917 }
2918 else
2919 {
2920 newtb.iBitmap = get_toolbar_bitmap(menu);
2921 newtb.fsStyle = TBSTYLE_BUTTON;
2922 }
2923 newtb.idCommand = menu->id;
2924 newtb.fsState = TBSTATE_ENABLED;
2925 newtb.iString = 0;
2926 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
2927 (LPARAM)&newtb);
2928 menu->submenu_id = (HMENU)-1;
2929 }
2930 else
2931#endif
2932 {
2933#ifdef FEAT_MBYTE
2934 WCHAR *wn = NULL;
2935 int n;
2936
2937 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2938 {
2939 /* 'encoding' differs from active codepage: convert menu item name
2940 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002941 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 if (wn != NULL)
2943 {
2944 n = InsertMenuW(parent->submenu_id, (UINT)idx,
2945 (menu_is_separator(menu->name)
2946 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
2947 (UINT)menu->id, wn);
2948 vim_free(wn);
2949 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2950 /* Failed, try using non-wide function. */
2951 wn = NULL;
2952 }
2953 }
2954 if (wn == NULL)
2955#endif
2956 InsertMenu(parent->submenu_id, (UINT)idx,
2957 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
2958 | MF_BYPOSITION,
2959 (UINT)menu->id, (LPCTSTR)menu->name);
2960#ifdef FEAT_TEAROFF
2961 if (IsWindow(parent->tearoff_handle))
2962 rebuild_tearoff(parent);
2963#endif
2964 }
2965}
2966
2967/*
2968 * Destroy the machine specific menu widget.
2969 */
2970 void
2971gui_mch_destroy_menu(vimmenu_T *menu)
2972{
2973#ifdef FEAT_TOOLBAR
2974 /*
2975 * is this a toolbar button?
2976 */
2977 if (menu->submenu_id == (HMENU)-1)
2978 {
2979 int iButton;
2980
2981 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
2982 (WPARAM)menu->id, 0);
2983 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
2984 }
2985 else
2986#endif
2987 {
2988 if (menu->parent != NULL
2989 && menu_is_popup(menu->parent->dname)
2990 && menu->parent->submenu_id != NULL)
2991 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
2992 else
2993 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
2994 if (menu->submenu_id != NULL)
2995 DestroyMenu(menu->submenu_id);
2996#ifdef FEAT_TEAROFF
2997 if (IsWindow(menu->tearoff_handle))
2998 DestroyWindow(menu->tearoff_handle);
2999 if (menu->parent != NULL
3000 && menu->parent->children != NULL
3001 && IsWindow(menu->parent->tearoff_handle))
3002 {
3003 /* This menu must not show up when rebuilding the tearoff window. */
3004 menu->modes = 0;
3005 rebuild_tearoff(menu->parent);
3006 }
3007#endif
3008 }
3009}
3010
3011#ifdef FEAT_TEAROFF
3012 static void
3013rebuild_tearoff(vimmenu_T *menu)
3014{
3015 /*hackish*/
3016 char_u tbuf[128];
3017 RECT trect;
3018 RECT rct;
3019 RECT roct;
3020 int x, y;
3021
3022 HWND thwnd = menu->tearoff_handle;
3023
3024 GetWindowText(thwnd, tbuf, 127);
3025 if (GetWindowRect(thwnd, &trect)
3026 && GetWindowRect(s_hwnd, &rct)
3027 && GetClientRect(s_hwnd, &roct))
3028 {
3029 x = trect.left - rct.left;
3030 y = (trect.top - rct.bottom + roct.bottom);
3031 }
3032 else
3033 {
3034 x = y = 0xffffL;
3035 }
3036 DestroyWindow(thwnd);
3037 if (menu->children != NULL)
3038 {
3039 gui_mch_tearoff(tbuf, menu, x, y);
3040 if (IsWindow(menu->tearoff_handle))
3041 (void) SetWindowPos(menu->tearoff_handle,
3042 NULL,
3043 (int)trect.left,
3044 (int)trect.top,
3045 0, 0,
3046 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
3047 }
3048}
3049#endif /* FEAT_TEAROFF */
3050
3051/*
3052 * Make a menu either grey or not grey.
3053 */
3054 void
3055gui_mch_menu_grey(
3056 vimmenu_T *menu,
3057 int grey)
3058{
3059#ifdef FEAT_TOOLBAR
3060 /*
3061 * is this a toolbar button?
3062 */
3063 if (menu->submenu_id == (HMENU)-1)
3064 {
3065 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
3066 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
3067 }
3068 else
3069#endif
3070 if (grey)
3071 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_GRAYED);
3072 else
3073 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
3074
3075#ifdef FEAT_TEAROFF
3076 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
3077 {
3078 WORD menuID;
3079 HWND menuHandle;
3080
3081 /*
3082 * A tearoff button has changed state.
3083 */
3084 if (menu->children == NULL)
3085 menuID = (WORD)(menu->id);
3086 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003087 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
3089 if (menuHandle)
3090 EnableWindow(menuHandle, !grey);
3091
3092 }
3093#endif
3094}
3095
3096#endif /* FEAT_MENU */
3097
3098
3099/* define some macros used to make the dialogue creation more readable */
3100
3101#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
3102#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00003103#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104
3105#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
3106/*
3107 * stuff for dialogs
3108 */
3109
3110/*
3111 * The callback routine used by all the dialogs. Very simple. First,
3112 * acknowledges the INITDIALOG message so that Windows knows to do standard
3113 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
3114 * pressed, return that button's ID - IDCANCEL (2), which is the button's
3115 * number.
3116 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003117/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 static LRESULT CALLBACK
3119dialog_callback(
3120 HWND hwnd,
3121 UINT message,
3122 WPARAM wParam,
3123 LPARAM lParam)
3124{
3125 if (message == WM_INITDIALOG)
3126 {
3127 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
3128 /* Set focus to the dialog. Set the default button, if specified. */
3129 (void)SetFocus(hwnd);
3130 if (dialog_default_button > IDCANCEL)
3131 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00003132 else
3133 /* We don't have a default, set focus on another element of the
3134 * dialog window, probably the icon */
3135 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 return FALSE;
3137 }
3138
3139 if (message == WM_COMMAND)
3140 {
3141 int button = LOWORD(wParam);
3142
3143 /* Don't end the dialog if something was selected that was
3144 * not a button.
3145 */
3146 if (button >= DLG_NONBUTTON_CONTROL)
3147 return TRUE;
3148
3149 /* If the edit box exists, copy the string. */
3150 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00003151 {
3152# if defined(FEAT_MBYTE) && defined(WIN3264)
3153 /* If the OS is Windows NT, and 'encoding' differs from active
3154 * codepage: use wide function and convert text. */
3155 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
3156 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003157 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00003158 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
3159 char_u *p;
3160
3161 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
3162 p = utf16_to_enc(wp, NULL);
3163 vim_strncpy(s_textfield, p, IOSIZE);
3164 vim_free(p);
3165 vim_free(wp);
3166 }
3167 else
3168# endif
3169 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00003171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172
3173 /*
3174 * Need to check for IDOK because if the user just hits Return to
3175 * accept the default value, some reason this is what we get.
3176 */
3177 if (button == IDOK)
3178 {
3179 if (dialog_default_button > IDCANCEL)
3180 EndDialog(hwnd, dialog_default_button);
3181 }
3182 else
3183 EndDialog(hwnd, button - IDCANCEL);
3184 return TRUE;
3185 }
3186
3187 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
3188 {
3189 EndDialog(hwnd, 0);
3190 return TRUE;
3191 }
3192 return FALSE;
3193}
3194
3195/*
3196 * Create a dialog dynamically from the parameter strings.
3197 * type = type of dialog (question, alert, etc.)
3198 * title = dialog title. may be NULL for default title.
3199 * message = text to display. Dialog sizes to accommodate it.
3200 * buttons = '\n' separated list of button captions, default first.
3201 * dfltbutton = number of default button.
3202 *
3203 * This routine returns 1 if the first button is pressed,
3204 * 2 for the second, etc.
3205 *
3206 * 0 indicates Esc was pressed.
3207 * -1 for unexpected error
3208 *
3209 * If stubbing out this fn, return 1.
3210 */
3211
3212static const char_u *dlg_icons[] = /* must match names in resource file */
3213{
3214 "IDR_VIM",
3215 "IDR_VIM_ERROR",
3216 "IDR_VIM_ALERT",
3217 "IDR_VIM_INFO",
3218 "IDR_VIM_QUESTION"
3219};
3220
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 int
3222gui_mch_dialog(
3223 int type,
3224 char_u *title,
3225 char_u *message,
3226 char_u *buttons,
3227 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003228 char_u *textfield,
3229 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230{
3231 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00003232 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 int numButtons;
3234 int *buttonWidths, *buttonPositions;
3235 int buttonYpos;
3236 int nchar, i;
3237 DWORD lStyle;
3238 int dlgwidth = 0;
3239 int dlgheight;
3240 int editboxheight;
3241 int horizWidth = 0;
3242 int msgheight;
3243 char_u *pstart;
3244 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003245 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 char_u *tbuffer;
3247 RECT rect;
3248 HWND hwnd;
3249 HDC hdc;
3250 HFONT font, oldFont;
3251 TEXTMETRIC fontInfo;
3252 int fontHeight;
3253 int textWidth, minButtonWidth, messageWidth;
3254 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003255 int maxDialogHeight;
3256 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 int vertical;
3258 int dlgPaddingX;
3259 int dlgPaddingY;
3260#ifdef USE_SYSMENU_FONT
3261 LOGFONT lfSysmenu;
3262 int use_lfSysmenu = FALSE;
3263#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003264 garray_T ga;
3265 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266
3267#ifndef NO_CONSOLE
3268 /* Don't output anything in silent mode ("ex -s") */
3269 if (silent_mode)
3270 return dfltbutton; /* return default option */
3271#endif
3272
Bram Moolenaar748bf032005-02-02 23:04:36 +00003273 if (s_hwnd == NULL)
3274 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
3276 if ((type < 0) || (type > VIM_LAST_TYPE))
3277 type = 0;
3278
3279 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003280 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003281 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003282 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283
3284 if (p == NULL)
3285 return -1;
3286
3287 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003288 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 * vim_strsave() doesn't take a const arg (why not?), so cast away the
3290 * const.
3291 */
3292 tbuffer = vim_strsave(buttons);
3293 if (tbuffer == NULL)
3294 return -1;
3295
3296 --dfltbutton; /* Change from one-based to zero-based */
3297
3298 /* Count buttons */
3299 numButtons = 1;
3300 for (i = 0; tbuffer[i] != '\0'; i++)
3301 {
3302 if (tbuffer[i] == DLG_BUTTON_SEP)
3303 numButtons++;
3304 }
3305 if (dfltbutton >= numButtons)
3306 dfltbutton = -1;
3307
3308 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003309 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 if (buttonWidths == NULL)
3311 return -1;
3312
3313 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003314 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 if (buttonPositions == NULL)
3316 return -1;
3317
3318 /*
3319 * Calculate how big the dialog must be.
3320 */
3321 hwnd = GetDesktopWindow();
3322 hdc = GetWindowDC(hwnd);
3323#ifdef USE_SYSMENU_FONT
3324 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
3325 {
3326 font = CreateFontIndirect(&lfSysmenu);
3327 use_lfSysmenu = TRUE;
3328 }
3329 else
3330#endif
3331 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3332 VARIABLE_PITCH , DLG_FONT_NAME);
3333 if (s_usenewlook)
3334 {
3335 oldFont = SelectFont(hdc, font);
3336 dlgPaddingX = DLG_PADDING_X;
3337 dlgPaddingY = DLG_PADDING_Y;
3338 }
3339 else
3340 {
3341 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
3342 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
3343 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
3344 }
3345 GetTextMetrics(hdc, &fontInfo);
3346 fontHeight = fontInfo.tmHeight;
3347
3348 /* Minimum width for horizontal button */
3349 minButtonWidth = GetTextWidth(hdc, "Cancel", 6);
3350
3351 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003352 if (s_hwnd == NULL)
3353 {
3354 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355
Bram Moolenaarc716c302006-01-21 22:12:51 +00003356 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003357 get_work_area(&workarea_rect);
3358 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
3359 if (maxDialogWidth > 600)
3360 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02003361 /* Leave some room for the taskbar. */
3362 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003363 }
3364 else
3365 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02003366 /* Use our own window for the size, unless it's very small. */
3367 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003368 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02003369 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02003370 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003371 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
3372 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003373
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003374 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02003375 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02003376 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02003377 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003378 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
3379 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
3380 }
3381
3382 /* Set dlgwidth to width of message.
3383 * Copy the message into "ga", changing NL to CR-NL and inserting line
3384 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 pstart = message;
3386 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003387 msgheight = 0;
3388 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 do
3390 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003391 msgheight += fontHeight; /* at least one line */
3392
3393 /* Need to figure out where to break the string. The system does it
3394 * at a word boundary, which would mean we can't compute the number of
3395 * wrapped lines. */
3396 textWidth = 0;
3397 last_white = NULL;
3398 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00003399 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003400#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003401 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003402#else
3403 l = 1;
3404#endif
3405 if (l == 1 && vim_iswhite(*pend)
3406 && textWidth > maxDialogWidth * 3 / 4)
3407 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02003408 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003409 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003410 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003411 /* Line will wrap. */
3412 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003413 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003414 textWidth = 0;
3415
3416 if (last_white != NULL)
3417 {
3418 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003419 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003420 pend = last_white + 1;
3421 last_white = NULL;
3422 }
3423 ga_append(&ga, '\r');
3424 ga_append(&ga, '\n');
3425 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003426 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003427
3428 while (--l >= 0)
3429 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003430 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003431 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003433
3434 ga_append(&ga, '\r');
3435 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 pstart = pend + 1;
3437 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003438
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003439 if (ga.ga_data != NULL)
3440 message = ga.ga_data;
3441
Bram Moolenaar748bf032005-02-02 23:04:36 +00003442 messageWidth += 10; /* roundoff space */
3443
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02003445 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
3446 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447
3448 if (msgheight < DLG_ICON_HEIGHT)
3449 msgheight = DLG_ICON_HEIGHT;
3450
3451 /*
3452 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00003453 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00003455 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 if (!vertical)
3457 {
3458 // Place buttons horizontally if they fit.
3459 horizWidth = dlgPaddingX;
3460 pstart = tbuffer;
3461 i = 0;
3462 do
3463 {
3464 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
3465 if (pend == NULL)
3466 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02003467 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 if (textWidth < minButtonWidth)
3469 textWidth = minButtonWidth;
3470 textWidth += dlgPaddingX; /* Padding within button */
3471 buttonWidths[i] = textWidth;
3472 buttonPositions[i++] = horizWidth;
3473 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
3474 pstart = pend + 1;
3475 } while (*pend != NUL);
3476
3477 if (horizWidth > maxDialogWidth)
3478 vertical = TRUE; // Too wide to fit on the screen.
3479 else if (horizWidth > dlgwidth)
3480 dlgwidth = horizWidth;
3481 }
3482
3483 if (vertical)
3484 {
3485 // Stack buttons vertically.
3486 pstart = tbuffer;
3487 do
3488 {
3489 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
3490 if (pend == NULL)
3491 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02003492 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 textWidth += dlgPaddingX; /* Padding within button */
3494 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
3495 if (textWidth > dlgwidth)
3496 dlgwidth = textWidth;
3497 pstart = pend + 1;
3498 } while (*pend != NUL);
3499 }
3500
3501 if (dlgwidth < DLG_MIN_WIDTH)
3502 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
3503
3504 /* start to fill in the dlgtemplate information. addressing by WORDs */
3505 if (s_usenewlook)
3506 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
3507 else
3508 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
3509
3510 add_long(lStyle);
3511 add_long(0); // (lExtendedStyle)
3512 pnumitems = p; /*save where the number of items must be stored*/
3513 add_word(0); // NumberOfItems(will change later)
3514 add_word(10); // x
3515 add_word(10); // y
3516 add_word(PixelToDialogX(dlgwidth)); // cx
3517
3518 // Dialog height.
3519 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02003520 dlgheight = msgheight + 2 * dlgPaddingY
3521 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 else
3523 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
3524
3525 // Dialog needs to be taller if contains an edit box.
3526 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
3527 if (textfield != NULL)
3528 dlgheight += editboxheight;
3529
Bram Moolenaara95d8232013-08-07 15:27:11 +02003530 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
3531 if (dlgheight > maxDialogHeight)
3532 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02003533 msgheight = msgheight - (dlgheight - maxDialogHeight);
3534 dlgheight = maxDialogHeight;
3535 scroll_flag = WS_VSCROLL;
3536 /* Make sure scrollbar doesn't appear in the middle of the dialog */
3537 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02003538 }
3539
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 add_word(PixelToDialogY(dlgheight));
3541
3542 add_word(0); // Menu
3543 add_word(0); // Class
3544
3545 /* copy the title of the dialog */
3546 nchar = nCopyAnsiToWideChar(p, (title ?
3547 (LPSTR)title :
3548 (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
3549 p += nchar;
3550
3551 if (s_usenewlook)
3552 {
3553 /* do the font, since DS_3DLOOK doesn't work properly */
3554#ifdef USE_SYSMENU_FONT
3555 if (use_lfSysmenu)
3556 {
3557 /* point size */
3558 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
3559 GetDeviceCaps(hdc, LOGPIXELSY));
3560 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
3561 }
3562 else
3563#endif
3564 {
3565 *p++ = DLG_FONT_POINT_SIZE; // point size
3566 nchar = nCopyAnsiToWideChar(p, TEXT(DLG_FONT_NAME));
3567 }
3568 p += nchar;
3569 }
3570
3571 buttonYpos = msgheight + 2 * dlgPaddingY;
3572
3573 if (textfield != NULL)
3574 buttonYpos += editboxheight;
3575
3576 pstart = tbuffer;
3577 if (!vertical)
3578 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
3579 for (i = 0; i < numButtons; i++)
3580 {
3581 /* get end of this button. */
3582 for ( pend = pstart;
3583 *pend && (*pend != DLG_BUTTON_SEP);
3584 pend++)
3585 ;
3586
3587 if (*pend)
3588 *pend = '\0';
3589
3590 /*
3591 * old NOTE:
3592 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
3593 * the focus to the first tab-able button and in so doing makes that
3594 * the default!! Grrr. Workaround: Make the default button the only
3595 * one with WS_TABSTOP style. Means user can't tab between buttons, but
3596 * he/she can use arrow keys.
3597 *
3598 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00003599 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 * dialog. Also needed for when the textfield is the default control.
3601 * It appears to work now (perhaps not on Win95?).
3602 */
3603 if (vertical)
3604 {
3605 p = add_dialog_element(p,
3606 (i == dfltbutton
3607 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
3608 PixelToDialogX(DLG_VERT_PADDING_X),
3609 PixelToDialogY(buttonYpos /* TBK */
3610 + 2 * fontHeight * i),
3611 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
3612 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
3613 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, pstart);
3614 }
3615 else
3616 {
3617 p = add_dialog_element(p,
3618 (i == dfltbutton
3619 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
3620 PixelToDialogX(horizWidth + buttonPositions[i]),
3621 PixelToDialogY(buttonYpos), /* TBK */
3622 PixelToDialogX(buttonWidths[i]),
3623 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
3624 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, pstart);
3625 }
3626 pstart = pend + 1; /*next button*/
3627 }
3628 *pnumitems += numButtons;
3629
3630 /* Vim icon */
3631 p = add_dialog_element(p, SS_ICON,
3632 PixelToDialogX(dlgPaddingX),
3633 PixelToDialogY(dlgPaddingY),
3634 PixelToDialogX(DLG_ICON_WIDTH),
3635 PixelToDialogY(DLG_ICON_HEIGHT),
3636 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
3637 dlg_icons[type]);
3638
Bram Moolenaar748bf032005-02-02 23:04:36 +00003639 /* Dialog message */
3640 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
3641 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
3642 PixelToDialogY(dlgPaddingY),
3643 (WORD)(PixelToDialogX(messageWidth) + 1),
3644 PixelToDialogY(msgheight),
3645 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646
3647 /* Edit box */
3648 if (textfield != NULL)
3649 {
3650 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
3651 PixelToDialogX(2 * dlgPaddingX),
3652 PixelToDialogY(2 * dlgPaddingY + msgheight),
3653 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
3654 PixelToDialogY(fontHeight + dlgPaddingY),
3655 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, textfield);
3656 *pnumitems += 1;
3657 }
3658
3659 *pnumitems += 2;
3660
3661 SelectFont(hdc, oldFont);
3662 DeleteObject(font);
3663 ReleaseDC(hwnd, hdc);
3664
3665 /* Let the dialog_callback() function know which button to make default
3666 * If we have an edit box, make that the default. We also need to tell
3667 * dialog_callback() if this dialog contains an edit box or not. We do
3668 * this by setting s_textfield if it does.
3669 */
3670 if (textfield != NULL)
3671 {
3672 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
3673 s_textfield = textfield;
3674 }
3675 else
3676 {
3677 dialog_default_button = IDCANCEL + 1 + dfltbutton;
3678 s_textfield = NULL;
3679 }
3680
3681 /* show the dialog box modally and get a return value */
3682 nchar = (int)DialogBoxIndirect(
3683 s_hinst,
3684 (LPDLGTEMPLATE)pdlgtemplate,
3685 s_hwnd,
3686 (DLGPROC)dialog_callback);
3687
3688 LocalFree(LocalHandle(pdlgtemplate));
3689 vim_free(tbuffer);
3690 vim_free(buttonWidths);
3691 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003692 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693
3694 /* Focus back to our window (for when MDI is used). */
3695 (void)SetFocus(s_hwnd);
3696
3697 return nchar;
3698}
3699
3700#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003701
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702/*
3703 * Put a simple element (basic class) onto a dialog template in memory.
3704 * return a pointer to where the next item should be added.
3705 *
3706 * parameters:
3707 * lStyle = additional style flags
3708 * (be careful, NT3.51 & Win32s will ignore the new ones)
3709 * x,y = x & y positions IN DIALOG UNITS
3710 * w,h = width and height IN DIALOG UNITS
3711 * Id = ID used in messages
3712 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
3713 * caption = usually text or resource name
3714 *
3715 * TODO: use the length information noted here to enable the dialog creation
3716 * routines to work out more exactly how much memory they need to alloc.
3717 */
3718 static PWORD
3719add_dialog_element(
3720 PWORD p,
3721 DWORD lStyle,
3722 WORD x,
3723 WORD y,
3724 WORD w,
3725 WORD h,
3726 WORD Id,
3727 WORD clss,
3728 const char *caption)
3729{
3730 int nchar;
3731
3732 p = lpwAlign(p); /* Align to dword boundary*/
3733 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
3734 *p++ = LOWORD(lStyle);
3735 *p++ = HIWORD(lStyle);
3736 *p++ = 0; // LOWORD (lExtendedStyle)
3737 *p++ = 0; // HIWORD (lExtendedStyle)
3738 *p++ = x;
3739 *p++ = y;
3740 *p++ = w;
3741 *p++ = h;
3742 *p++ = Id; //9 or 10 words in all
3743
3744 *p++ = (WORD)0xffff;
3745 *p++ = clss; //2 more here
3746
3747 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption); //strlen(caption)+1
3748 p += nchar;
3749
3750 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
3751
3752 return p; //total = 15+ (strlen(caption)) words
3753 // = 30 + 2(strlen(caption) bytes reqd
3754}
3755
3756
3757/*
3758 * Helper routine. Take an input pointer, return closest pointer that is
3759 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
3760 */
3761 static LPWORD
3762lpwAlign(
3763 LPWORD lpIn)
3764{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003765 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003767 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 ul += 3;
3769 ul >>= 2;
3770 ul <<= 2;
3771 return (LPWORD)ul;
3772}
3773
3774/*
3775 * Helper routine. Takes second parameter as Ansi string, copies it to first
3776 * parameter as wide character (16-bits / char) string, and returns integer
3777 * number of wide characters (words) in string (including the trailing wide
3778 * char NULL). Partly taken from the Win32SDK samples.
3779 */
3780 static int
3781nCopyAnsiToWideChar(
3782 LPWORD lpWCStr,
3783 LPSTR lpAnsiIn)
3784{
3785 int nChar = 0;
3786#ifdef FEAT_MBYTE
3787 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
3788 int i;
3789 WCHAR *wn;
3790
3791 if (enc_codepage == 0 && (int)GetACP() != enc_codepage)
3792 {
3793 /* Not a codepage, use our own conversion function. */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00003794 wn = enc_to_utf16(lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 if (wn != NULL)
3796 {
3797 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003798 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 vim_free(wn);
3800 }
3801 }
3802 if (nChar == 0)
3803 /* Use Win32 conversion function. */
3804 nChar = MultiByteToWideChar(
3805 enc_codepage > 0 ? enc_codepage : CP_ACP,
3806 MB_PRECOMPOSED,
3807 lpAnsiIn, len,
3808 lpWCStr, len);
3809 for (i = 0; i < nChar; ++i)
3810 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
3811 lpWCStr[i] = (WORD)' ';
3812#else
3813 do
3814 {
3815 if (*lpAnsiIn == '\t')
3816 *lpWCStr++ = (WORD)' ';
3817 else
3818 *lpWCStr++ = (WORD)*lpAnsiIn;
3819 nChar++;
3820 } while (*lpAnsiIn++);
3821#endif
3822
3823 return nChar;
3824}
3825
3826
3827#ifdef FEAT_TEAROFF
3828/*
3829 * The callback function for all the modeless dialogs that make up the
3830 * "tearoff menus" Very simple - forward button presses (to fool Vim into
3831 * thinking its menus have been clicked), and go away when closed.
3832 */
3833 static LRESULT CALLBACK
3834tearoff_callback(
3835 HWND hwnd,
3836 UINT message,
3837 WPARAM wParam,
3838 LPARAM lParam)
3839{
3840 if (message == WM_INITDIALOG)
3841 return (TRUE);
3842
3843 /* May show the mouse pointer again. */
3844 HandleMouseHide(message, lParam);
3845
3846 if (message == WM_COMMAND)
3847 {
3848 if ((WORD)(LOWORD(wParam)) & 0x8000)
3849 {
3850 POINT mp;
3851 RECT rect;
3852
3853 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
3854 {
3855 (void)TrackPopupMenu(
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003856 (HMENU)(long_u)(LOWORD(wParam) ^ 0x8000),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 TPM_LEFTALIGN | TPM_LEFTBUTTON,
3858 (int)rect.right - 8,
3859 (int)mp.y,
3860 (int)0, /*reserved param*/
3861 s_hwnd,
3862 NULL);
3863 /*
3864 * NOTE: The pop-up menu can eat the mouse up event.
3865 * We deal with this in normal.c.
3866 */
3867 }
3868 }
3869 else
3870 /* Pass on messages to the main Vim window */
3871 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
3872 /*
3873 * Give main window the focus back: this is so after
3874 * choosing a tearoff button you can start typing again
3875 * straight away.
3876 */
3877 (void)SetFocus(s_hwnd);
3878 return TRUE;
3879 }
3880 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
3881 {
3882 DestroyWindow(hwnd);
3883 return TRUE;
3884 }
3885
3886 /* When moved around, give main window the focus back. */
3887 if (message == WM_EXITSIZEMOVE)
3888 (void)SetActiveWindow(s_hwnd);
3889
3890 return FALSE;
3891}
3892#endif
3893
3894
3895/*
3896 * Decide whether to use the "new look" (small, non-bold font) or the "old
3897 * look" (big, clanky font) for dialogs, and work out a few values for use
3898 * later accordingly.
3899 */
3900 static void
3901get_dialog_font_metrics(void)
3902{
3903 HDC hdc;
3904 HFONT hfontTools = 0;
3905 DWORD dlgFontSize;
3906 SIZE size;
3907#ifdef USE_SYSMENU_FONT
3908 LOGFONT lfSysmenu;
3909#endif
3910
3911 s_usenewlook = FALSE;
3912
3913 /*
3914 * For NT3.51 and Win32s, we stick with the old look
3915 * because it matches everything else.
3916 */
3917 if (!is_winnt_3())
3918 {
3919#ifdef USE_SYSMENU_FONT
3920 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
3921 hfontTools = CreateFontIndirect(&lfSysmenu);
3922 else
3923#endif
3924 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
3925 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
3926
3927 if (hfontTools)
3928 {
3929 hdc = GetDC(s_hwnd);
3930 SelectObject(hdc, hfontTools);
3931 /*
3932 * GetTextMetrics() doesn't return the right value in
3933 * tmAveCharWidth, so we have to figure out the dialog base units
3934 * ourselves.
3935 */
3936 GetTextExtentPoint(hdc,
3937 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
3938 52, &size);
3939 ReleaseDC(s_hwnd, hdc);
3940
3941 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
3942 s_dlgfntheight = (WORD)size.cy;
3943 s_usenewlook = TRUE;
3944 }
3945 }
3946
3947 if (!s_usenewlook)
3948 {
3949 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
3950 s_dlgfntwidth = LOWORD(dlgFontSize);
3951 s_dlgfntheight = HIWORD(dlgFontSize);
3952 }
3953}
3954
3955#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
3956/*
3957 * Create a pseudo-"tearoff menu" based on the child
3958 * items of a given menu pointer.
3959 */
3960 static void
3961gui_mch_tearoff(
3962 char_u *title,
3963 vimmenu_T *menu,
3964 int initX,
3965 int initY)
3966{
3967 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
3968 int template_len;
3969 int nchar, textWidth, submenuWidth;
3970 DWORD lStyle;
3971 DWORD lExtendedStyle;
3972 WORD dlgwidth;
3973 WORD menuID;
3974 vimmenu_T *pmenu;
3975 vimmenu_T *the_menu = menu;
3976 HWND hwnd;
3977 HDC hdc;
3978 HFONT font, oldFont;
3979 int col, spaceWidth, len;
3980 int columnWidths[2];
3981 char_u *label, *text;
3982 int acLen = 0;
3983 int nameLen;
3984 int padding0, padding1, padding2 = 0;
3985 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003986 int x;
3987 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988#ifdef USE_SYSMENU_FONT
3989 LOGFONT lfSysmenu;
3990 int use_lfSysmenu = FALSE;
3991#endif
3992
3993 /*
3994 * If this menu is already torn off, move it to the mouse position.
3995 */
3996 if (IsWindow(menu->tearoff_handle))
3997 {
3998 POINT mp;
3999 if (GetCursorPos((LPPOINT)&mp))
4000 {
4001 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
4002 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
4003 }
4004 return;
4005 }
4006
4007 /*
4008 * Create a new tearoff.
4009 */
4010 if (*title == MNU_HIDDEN_CHAR)
4011 title++;
4012
4013 /* Allocate memory to store the dialog template. It's made bigger when
4014 * needed. */
4015 template_len = DLG_ALLOC_SIZE;
4016 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
4017 if (p == NULL)
4018 return;
4019
4020 hwnd = GetDesktopWindow();
4021 hdc = GetWindowDC(hwnd);
4022#ifdef USE_SYSMENU_FONT
4023 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
4024 {
4025 font = CreateFontIndirect(&lfSysmenu);
4026 use_lfSysmenu = TRUE;
4027 }
4028 else
4029#endif
4030 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4031 VARIABLE_PITCH , DLG_FONT_NAME);
4032 if (s_usenewlook)
4033 oldFont = SelectFont(hdc, font);
4034 else
4035 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
4036
4037 /* Calculate width of a single space. Used for padding columns to the
4038 * right width. */
4039 spaceWidth = GetTextWidth(hdc, " ", 1);
4040
4041 /* Figure out max width of the text column, the accelerator column and the
4042 * optional submenu column. */
4043 submenuWidth = 0;
4044 for (col = 0; col < 2; col++)
4045 {
4046 columnWidths[col] = 0;
4047 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
4048 {
4049 /* Use "dname" here to compute the width of the visible text. */
4050 text = (col == 0) ? pmenu->dname : pmenu->actext;
4051 if (text != NULL && *text != NUL)
4052 {
4053 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
4054 if (textWidth > columnWidths[col])
4055 columnWidths[col] = textWidth;
4056 }
4057 if (pmenu->children != NULL)
4058 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
4059 }
4060 }
4061 if (columnWidths[1] == 0)
4062 {
4063 /* no accelerators */
4064 if (submenuWidth != 0)
4065 columnWidths[0] += submenuWidth;
4066 else
4067 columnWidths[0] += spaceWidth;
4068 }
4069 else
4070 {
4071 /* there is an accelerator column */
4072 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
4073 columnWidths[1] += submenuWidth;
4074 }
4075
4076 /*
4077 * Now find the total width of our 'menu'.
4078 */
4079 textWidth = columnWidths[0] + columnWidths[1];
4080 if (submenuWidth != 0)
4081 {
4082 submenuWidth = GetTextWidth(hdc, TEAROFF_SUBMENU_LABEL,
4083 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
4084 textWidth += submenuWidth;
4085 }
4086 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
4087 if (textWidth > dlgwidth)
4088 dlgwidth = textWidth;
4089 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
4090
4091 /* W95 can't do thin dialogs, they look v. weird! */
4092 if (mch_windows95() && dlgwidth < TEAROFF_MIN_WIDTH)
4093 dlgwidth = TEAROFF_MIN_WIDTH;
4094
4095 /* start to fill in the dlgtemplate information. addressing by WORDs */
4096 if (s_usenewlook)
4097 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
4098 else
4099 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
4100
4101 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
4102 *p++ = LOWORD(lStyle);
4103 *p++ = HIWORD(lStyle);
4104 *p++ = LOWORD(lExtendedStyle);
4105 *p++ = HIWORD(lExtendedStyle);
4106 pnumitems = p; /* save where the number of items must be stored */
4107 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004108 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004110 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 else
4112 *p++ = PixelToDialogX(initX); // x
4113 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004114 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 else
4116 *p++ = PixelToDialogY(initY); // y
4117 *p++ = PixelToDialogX(dlgwidth); // cx
4118 ptrueheight = p;
4119 *p++ = 0; // dialog height: changed later anyway
4120 *p++ = 0; // Menu
4121 *p++ = 0; // Class
4122
4123 /* copy the title of the dialog */
4124 nchar = nCopyAnsiToWideChar(p, ((*title)
4125 ? (LPSTR)title
4126 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
4127 p += nchar;
4128
4129 if (s_usenewlook)
4130 {
4131 /* do the font, since DS_3DLOOK doesn't work properly */
4132#ifdef USE_SYSMENU_FONT
4133 if (use_lfSysmenu)
4134 {
4135 /* point size */
4136 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
4137 GetDeviceCaps(hdc, LOGPIXELSY));
4138 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
4139 }
4140 else
4141#endif
4142 {
4143 *p++ = DLG_FONT_POINT_SIZE; // point size
4144 nchar = nCopyAnsiToWideChar (p, TEXT(DLG_FONT_NAME));
4145 }
4146 p += nchar;
4147 }
4148
4149 /*
4150 * Loop over all the items in the menu.
4151 * But skip over the tearbar.
4152 */
4153 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
4154 menu = menu->children->next;
4155 else
4156 menu = menu->children;
4157 for ( ; menu != NULL; menu = menu->next)
4158 {
4159 if (menu->modes == 0) /* this menu has just been deleted */
4160 continue;
4161 if (menu_is_separator(menu->dname))
4162 {
4163 sepPadding += 3;
4164 continue;
4165 }
4166
4167 /* Check if there still is plenty of room in the template. Make it
4168 * larger when needed. */
4169 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
4170 {
4171 WORD *newp;
4172
4173 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
4174 if (newp != NULL)
4175 {
4176 template_len += 4096;
4177 mch_memmove(newp, pdlgtemplate,
4178 (char *)p - (char *)pdlgtemplate);
4179 p = newp + (p - pdlgtemplate);
4180 pnumitems = newp + (pnumitems - pdlgtemplate);
4181 ptrueheight = newp + (ptrueheight - pdlgtemplate);
4182 LocalFree(LocalHandle(pdlgtemplate));
4183 pdlgtemplate = newp;
4184 }
4185 }
4186
4187 /* Figure out minimal length of this menu label. Use "name" for the
4188 * actual text, "dname" for estimating the displayed size. "name"
4189 * has "&a" for mnemonic and includes the accelerator. */
4190 len = nameLen = (int)STRLEN(menu->name);
4191 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
4192 (int)STRLEN(menu->dname))) / spaceWidth;
4193 len += padding0;
4194
4195 if (menu->actext != NULL)
4196 {
4197 acLen = (int)STRLEN(menu->actext);
4198 len += acLen;
4199 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
4200 }
4201 else
4202 textWidth = 0;
4203 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
4204 len += padding1;
4205
4206 if (menu->children == NULL)
4207 {
4208 padding2 = submenuWidth / spaceWidth;
4209 len += padding2;
4210 menuID = (WORD)(menu->id);
4211 }
4212 else
4213 {
4214 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004215 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 }
4217
4218 /* Allocate menu label and fill it in */
4219 text = label = alloc((unsigned)len + 1);
4220 if (label == NULL)
4221 break;
4222
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004223 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 text = vim_strchr(text, TAB); /* stop at TAB before actext */
4225 if (text == NULL)
4226 text = label + nameLen; /* no actext, use whole name */
4227 while (padding0-- > 0)
4228 *text++ = ' ';
4229 if (menu->actext != NULL)
4230 {
4231 STRNCPY(text, menu->actext, acLen);
4232 text += acLen;
4233 }
4234 while (padding1-- > 0)
4235 *text++ = ' ';
4236 if (menu->children != NULL)
4237 {
4238 STRCPY(text, TEAROFF_SUBMENU_LABEL);
4239 text += STRLEN(TEAROFF_SUBMENU_LABEL);
4240 }
4241 else
4242 {
4243 while (padding2-- > 0)
4244 *text++ = ' ';
4245 }
4246 *text = NUL;
4247
4248 /*
4249 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
4250 * W95/NT4 it makes the tear-off look more like a menu.
4251 */
4252 p = add_dialog_element(p,
4253 BS_PUSHBUTTON|BS_LEFT,
4254 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
4255 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
4256 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
4257 (WORD)12,
4258 menuID, (WORD)0x0080, label);
4259 vim_free(label);
4260 (*pnumitems)++;
4261 }
4262
4263 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
4264
4265
4266 /* show modelessly */
4267 the_menu->tearoff_handle = CreateDialogIndirect(
4268 s_hinst,
4269 (LPDLGTEMPLATE)pdlgtemplate,
4270 s_hwnd,
4271 (DLGPROC)tearoff_callback);
4272
4273 LocalFree(LocalHandle(pdlgtemplate));
4274 SelectFont(hdc, oldFont);
4275 DeleteObject(font);
4276 ReleaseDC(hwnd, hdc);
4277
4278 /*
4279 * Reassert ourselves as the active window. This is so that after creating
4280 * a tearoff, the user doesn't have to click with the mouse just to start
4281 * typing again!
4282 */
4283 (void)SetActiveWindow(s_hwnd);
4284
4285 /* make sure the right buttons are enabled */
4286 force_menu_update = TRUE;
4287}
4288#endif
4289
4290#if defined(FEAT_TOOLBAR) || defined(PROTO)
4291#include "gui_w32_rc.h"
4292
4293/* This not defined in older SDKs */
4294# ifndef TBSTYLE_FLAT
4295# define TBSTYLE_FLAT 0x0800
4296# endif
4297
4298/*
4299 * Create the toolbar, initially unpopulated.
4300 * (just like the menu, there are no defaults, it's all
4301 * set up through menu.vim)
4302 */
4303 static void
4304initialise_toolbar(void)
4305{
4306 InitCommonControls();
4307 s_toolbarhwnd = CreateToolbarEx(
4308 s_hwnd,
4309 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
4310 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004311 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 s_hinst,
4313 IDR_TOOLBAR1, // id of initial bitmap
4314 NULL,
4315 0, // initial number of buttons
4316 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
4317 TOOLBAR_BUTTON_HEIGHT,
4318 TOOLBAR_BUTTON_WIDTH,
4319 TOOLBAR_BUTTON_HEIGHT,
4320 sizeof(TBBUTTON)
4321 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004322 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323
4324 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
4325}
4326
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004327 static LRESULT CALLBACK
4328toolbar_wndproc(
4329 HWND hwnd,
4330 UINT uMsg,
4331 WPARAM wParam,
4332 LPARAM lParam)
4333{
4334 HandleMouseHide(uMsg, lParam);
4335 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
4336}
4337
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 static int
4339get_toolbar_bitmap(vimmenu_T *menu)
4340{
4341 int i = -1;
4342
4343 /*
4344 * Check user bitmaps first, unless builtin is specified.
4345 */
4346 if (!is_winnt_3() && !menu->icon_builtin)
4347 {
4348 char_u fname[MAXPATHL];
4349 HANDLE hbitmap = NULL;
4350
4351 if (menu->iconfile != NULL)
4352 {
4353 gui_find_iconfile(menu->iconfile, fname, "bmp");
4354 hbitmap = LoadImage(
4355 NULL,
4356 fname,
4357 IMAGE_BITMAP,
4358 TOOLBAR_BUTTON_WIDTH,
4359 TOOLBAR_BUTTON_HEIGHT,
4360 LR_LOADFROMFILE |
4361 LR_LOADMAP3DCOLORS
4362 );
4363 }
4364
4365 /*
4366 * If the LoadImage call failed, or the "icon=" file
4367 * didn't exist or wasn't specified, try the menu name
4368 */
4369 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02004370 && (gui_find_bitmap(
4371#ifdef FEAT_MULTI_LANG
4372 menu->en_dname != NULL ? menu->en_dname :
4373#endif
4374 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 hbitmap = LoadImage(
4376 NULL,
4377 fname,
4378 IMAGE_BITMAP,
4379 TOOLBAR_BUTTON_WIDTH,
4380 TOOLBAR_BUTTON_HEIGHT,
4381 LR_LOADFROMFILE |
4382 LR_LOADMAP3DCOLORS
4383 );
4384
4385 if (hbitmap != NULL)
4386 {
4387 TBADDBITMAP tbAddBitmap;
4388
4389 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004390 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391
4392 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
4393 (WPARAM)1, (LPARAM)&tbAddBitmap);
4394 /* i will be set to -1 if it fails */
4395 }
4396 }
4397 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
4398 i = menu->iconidx;
4399
4400 return i;
4401}
4402#endif
4403
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004404#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
4405 static void
4406initialise_tabline(void)
4407{
4408 InitCommonControls();
4409
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004410 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004411 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004412 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4413 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004414 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004415
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004416 gui.tabline_height = TABLINE_HEIGHT;
4417
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004418# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004419 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004420# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004421}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004422
4423 static LRESULT CALLBACK
4424tabline_wndproc(
4425 HWND hwnd,
4426 UINT uMsg,
4427 WPARAM wParam,
4428 LPARAM lParam)
4429{
4430 HandleMouseHide(uMsg, lParam);
4431 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
4432}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004433#endif
4434
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
4436/*
4437 * Make the GUI window come to the foreground.
4438 */
4439 void
4440gui_mch_set_foreground(void)
4441{
4442 if (IsIconic(s_hwnd))
4443 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
4444 SetForegroundWindow(s_hwnd);
4445}
4446#endif
4447
4448#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4449 static void
4450dyn_imm_load(void)
4451{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02004452 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 if (hLibImm == NULL)
4454 return;
4455
4456 pImmGetCompositionStringA
4457 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
4458 pImmGetCompositionStringW
4459 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
4460 pImmGetContext
4461 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
4462 pImmAssociateContext
4463 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
4464 pImmReleaseContext
4465 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
4466 pImmGetOpenStatus
4467 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
4468 pImmSetOpenStatus
4469 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
4470 pImmGetCompositionFont
4471 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
4472 pImmSetCompositionFont
4473 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
4474 pImmSetCompositionWindow
4475 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
4476 pImmGetConversionStatus
4477 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00004478 pImmSetConversionStatus
4479 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
4481 if ( pImmGetCompositionStringA == NULL
4482 || pImmGetCompositionStringW == NULL
4483 || pImmGetContext == NULL
4484 || pImmAssociateContext == NULL
4485 || pImmReleaseContext == NULL
4486 || pImmGetOpenStatus == NULL
4487 || pImmSetOpenStatus == NULL
4488 || pImmGetCompositionFont == NULL
4489 || pImmSetCompositionFont == NULL
4490 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00004491 || pImmGetConversionStatus == NULL
4492 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 {
4494 FreeLibrary(hLibImm);
4495 hLibImm = NULL;
4496 pImmGetContext = NULL;
4497 return;
4498 }
4499
4500 return;
4501}
4502
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503#endif
4504
4505#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
4506
4507# ifdef FEAT_XPM_W32
4508# define IMAGE_XPM 100
4509# endif
4510
4511typedef struct _signicon_t
4512{
4513 HANDLE hImage;
4514 UINT uType;
4515#ifdef FEAT_XPM_W32
4516 HANDLE hShape; /* Mask bitmap handle */
4517#endif
4518} signicon_t;
4519
4520 void
4521gui_mch_drawsign(row, col, typenr)
4522 int row;
4523 int col;
4524 int typenr;
4525{
4526 signicon_t *sign;
4527 int x, y, w, h;
4528
4529 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
4530 return;
4531
4532 x = TEXT_X(col);
4533 y = TEXT_Y(row);
4534 w = gui.char_width * 2;
4535 h = gui.char_height;
4536 switch (sign->uType)
4537 {
4538 case IMAGE_BITMAP:
4539 {
4540 HDC hdcMem;
4541 HBITMAP hbmpOld;
4542
4543 hdcMem = CreateCompatibleDC(s_hdc);
4544 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
4545 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
4546 SelectObject(hdcMem, hbmpOld);
4547 DeleteDC(hdcMem);
4548 }
4549 break;
4550 case IMAGE_ICON:
4551 case IMAGE_CURSOR:
4552 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
4553 break;
4554#ifdef FEAT_XPM_W32
4555 case IMAGE_XPM:
4556 {
4557 HDC hdcMem;
4558 HBITMAP hbmpOld;
4559
4560 hdcMem = CreateCompatibleDC(s_hdc);
4561 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
4562 /* Make hole */
4563 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
4564
4565 SelectObject(hdcMem, sign->hImage);
4566 /* Paint sign */
4567 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
4568 SelectObject(hdcMem, hbmpOld);
4569 DeleteDC(hdcMem);
4570 }
4571 break;
4572#endif
4573 }
4574}
4575
4576 static void
4577close_signicon_image(signicon_t *sign)
4578{
4579 if (sign)
4580 switch (sign->uType)
4581 {
4582 case IMAGE_BITMAP:
4583 DeleteObject((HGDIOBJ)sign->hImage);
4584 break;
4585 case IMAGE_CURSOR:
4586 DestroyCursor((HCURSOR)sign->hImage);
4587 break;
4588 case IMAGE_ICON:
4589 DestroyIcon((HICON)sign->hImage);
4590 break;
4591#ifdef FEAT_XPM_W32
4592 case IMAGE_XPM:
4593 DeleteObject((HBITMAP)sign->hImage);
4594 DeleteObject((HBITMAP)sign->hShape);
4595 break;
4596#endif
4597 }
4598}
4599
4600 void *
4601gui_mch_register_sign(signfile)
4602 char_u *signfile;
4603{
4604 signicon_t sign, *psign;
4605 char_u *ext;
4606
4607 if (is_winnt_3())
4608 {
4609 EMSG(_(e_signdata));
4610 return NULL;
4611 }
4612
4613 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004614 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 if (ext > signfile)
4616 {
4617 int do_load = 1;
4618
4619 if (!STRICMP(ext, ".bmp"))
4620 sign.uType = IMAGE_BITMAP;
4621 else if (!STRICMP(ext, ".ico"))
4622 sign.uType = IMAGE_ICON;
4623 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
4624 sign.uType = IMAGE_CURSOR;
4625 else
4626 do_load = 0;
4627
4628 if (do_load)
4629 sign.hImage = (HANDLE)LoadImage(NULL, signfile, sign.uType,
4630 gui.char_width * 2, gui.char_height,
4631 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
4632#ifdef FEAT_XPM_W32
4633 if (!STRICMP(ext, ".xpm"))
4634 {
4635 sign.uType = IMAGE_XPM;
4636 LoadXpmImage(signfile, (HBITMAP *)&sign.hImage, (HBITMAP *)&sign.hShape);
4637 }
4638#endif
4639 }
4640
4641 psign = NULL;
4642 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
4643 != NULL)
4644 *psign = sign;
4645
4646 if (!psign)
4647 {
4648 if (sign.hImage)
4649 close_signicon_image(&sign);
4650 EMSG(_(e_signdata));
4651 }
4652 return (void *)psign;
4653
4654}
4655
4656 void
4657gui_mch_destroy_sign(sign)
4658 void *sign;
4659{
4660 if (sign)
4661 {
4662 close_signicon_image((signicon_t *)sign);
4663 vim_free(sign);
4664 }
4665}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667
4668#if defined(FEAT_BEVAL) || defined(PROTO)
4669
4670/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004671 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 *
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004673 * The only reused thing is gui_beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 * from gui_beval.c (note it uses x and y of the BalloonEval struct
4675 * to get current mouse position).
4676 *
4677 * Trying to use as more Windows services as possible, and as less
4678 * IE version as possible :)).
4679 *
4680 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
4681 * BalloonEval struct.
4682 * 2) Enable/Disable simply create/kill BalloonEval Timer
4683 * 3) When there was enough inactivity, timer procedure posts
4684 * async request to debugger
4685 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
4686 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00004687 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 */
4689
Bram Moolenaar45360022005-07-21 21:08:21 +00004690/*
4691 * determine whether installed Common Controls support multiline tooltips
4692 * (i.e. their version is >= 4.70
4693 */
4694 int
4695multiline_balloon_available(void)
4696{
4697 HINSTANCE hDll;
4698 static char comctl_dll[] = "comctl32.dll";
4699 static int multiline_tip = MAYBE;
4700
4701 if (multiline_tip != MAYBE)
4702 return multiline_tip;
4703
4704 hDll = GetModuleHandle(comctl_dll);
4705 if (hDll != NULL)
4706 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004707 DLLGETVERSIONPROC pGetVer;
4708 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00004709
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004710 if (pGetVer != NULL)
4711 {
4712 DLLVERSIONINFO dvi;
4713 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00004714
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004715 ZeroMemory(&dvi, sizeof(dvi));
4716 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00004717
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004718 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00004719
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004720 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00004721 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004722 || (dvi.dwMajorVersion == 4
4723 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00004724 {
4725 multiline_tip = TRUE;
4726 return multiline_tip;
4727 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004728 }
Bram Moolenaar45360022005-07-21 21:08:21 +00004729 else
4730 {
4731 /* there is chance we have ancient CommCtl 4.70
4732 which doesn't export DllGetVersion */
4733 DWORD dwHandle = 0;
4734 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
4735 if (len > 0)
4736 {
4737 VS_FIXEDFILEINFO *ver;
4738 UINT vlen = 0;
4739 void *data = alloc(len);
4740
4741 if (data != NULL
4742 && GetFileVersionInfo(comctl_dll, 0, len, data)
4743 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
4744 && vlen
4745 && HIWORD(ver->dwFileVersionMS) > 4
4746 || (HIWORD(ver->dwFileVersionMS) == 4
4747 && LOWORD(ver->dwFileVersionMS) >= 70))
4748 {
4749 vim_free(data);
4750 multiline_tip = TRUE;
4751 return multiline_tip;
4752 }
4753 vim_free(data);
4754 }
4755 }
4756 }
4757 multiline_tip = FALSE;
4758 return multiline_tip;
4759}
4760
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 static void
4762make_tooltip(beval, text, pt)
4763 BalloonEval *beval;
4764 char *text;
4765 POINT pt;
4766{
Bram Moolenaar45360022005-07-21 21:08:21 +00004767 TOOLINFO *pti;
4768 int ToolInfoSize;
4769
4770 if (multiline_balloon_available() == TRUE)
4771 ToolInfoSize = sizeof(TOOLINFO_NEW);
4772 else
4773 ToolInfoSize = sizeof(TOOLINFO);
4774
4775 pti = (TOOLINFO *)alloc(ToolInfoSize);
4776 if (pti == NULL)
4777 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778
4779 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
4780 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
4781 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4782 beval->target, NULL, s_hinst, NULL);
4783
4784 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
4785 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
4786
Bram Moolenaar45360022005-07-21 21:08:21 +00004787 pti->cbSize = ToolInfoSize;
4788 pti->uFlags = TTF_SUBCLASS;
4789 pti->hwnd = beval->target;
4790 pti->hinst = 0; /* Don't use string resources */
4791 pti->uId = ID_BEVAL_TOOLTIP;
4792
4793 if (multiline_balloon_available() == TRUE)
4794 {
4795 RECT rect;
4796 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
4797 pti->lpszText = LPSTR_TEXTCALLBACK;
4798 ptin->lParam = (LPARAM)text;
4799 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
4800 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
4801 (LPARAM)rect.right);
4802 }
4803 else
4804 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805
4806 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00004807 pti->rect.left = pt.x - 3;
4808 pti->rect.top = pt.y - 3;
4809 pti->rect.right = pt.x + 3;
4810 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811
Bram Moolenaar45360022005-07-21 21:08:21 +00004812 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 /* Make tooltip appear sooner */
4814 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00004815 /* I've performed some tests and it seems the longest possible life time
4816 * of tooltip is 30 seconds */
4817 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 /*
4819 * HACK: force tooltip to appear, because it'll not appear until
4820 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00004821 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00004823 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00004825 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826}
4827
4828 static void
4829delete_tooltip(beval)
4830 BalloonEval *beval;
4831{
4832 DestroyWindow(beval->balloon);
4833}
4834
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004835/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 static VOID CALLBACK
4837BevalTimerProc(hwnd, uMsg, idEvent, dwTime)
4838 HWND hwnd;
4839 UINT uMsg;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004840 UINT_PTR idEvent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 DWORD dwTime;
4842{
4843 POINT pt;
4844 RECT rect;
4845
4846 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
4847 return;
4848
4849 GetCursorPos(&pt);
4850 if (WindowFromPoint(pt) != s_textArea)
4851 return;
4852
4853 ScreenToClient(s_textArea, &pt);
4854 GetClientRect(s_textArea, &rect);
4855 if (!PtInRect(&rect, pt))
4856 return;
4857
4858 if (LastActivity > 0
4859 && (dwTime - LastActivity) >= (DWORD)p_bdlay
4860 && (cur_beval->showState != ShS_PENDING
4861 || abs(cur_beval->x - pt.x) > 3
4862 || abs(cur_beval->y - pt.y) > 3))
4863 {
4864 /* Pointer resting in one place long enough, it's time to show
4865 * the tooltip. */
4866 cur_beval->showState = ShS_PENDING;
4867 cur_beval->x = pt.x;
4868 cur_beval->y = pt.y;
4869
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004870 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871
4872 if (cur_beval->msgCB != NULL)
4873 (*cur_beval->msgCB)(cur_beval, 0);
4874 }
4875}
4876
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004877/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 void
4879gui_mch_disable_beval_area(beval)
4880 BalloonEval *beval;
4881{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004882 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004884 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885}
4886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004887/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 void
4889gui_mch_enable_beval_area(beval)
4890 BalloonEval *beval;
4891{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004892 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 if (beval == NULL)
4894 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004895 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02004896 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004897 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898}
4899
4900 void
4901gui_mch_post_balloon(beval, mesg)
4902 BalloonEval *beval;
4903 char_u *mesg;
4904{
4905 POINT pt;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004906 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 if (beval->showState == ShS_SHOWING)
4908 return;
4909 GetCursorPos(&pt);
4910 ScreenToClient(s_textArea, &pt);
4911
4912 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
4913 /* cursor is still here */
4914 {
4915 gui_mch_disable_beval_area(cur_beval);
4916 beval->showState = ShS_SHOWING;
4917 make_tooltip(beval, mesg, pt);
4918 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004919 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920}
4921
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004922/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 BalloonEval *
4924gui_mch_create_beval_area(target, mesg, mesgCB, clientData)
4925 void *target; /* ignored, always use s_textArea */
4926 char_u *mesg;
4927 void (*mesgCB)__ARGS((BalloonEval *, int));
4928 void *clientData;
4929{
4930 /* partially stolen from gui_beval.c */
4931 BalloonEval *beval;
4932
4933 if (mesg != NULL && mesgCB != NULL)
4934 {
4935 EMSG(_("E232: Cannot create BalloonEval with both message and callback"));
4936 return NULL;
4937 }
4938
4939 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
4940 if (beval != NULL)
4941 {
4942 beval->target = s_textArea;
4943 beval->balloon = NULL;
4944
4945 beval->showState = ShS_NEUTRAL;
4946 beval->x = 0;
4947 beval->y = 0;
4948 beval->msg = mesg;
4949 beval->msgCB = mesgCB;
4950 beval->clientData = clientData;
4951
4952 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 cur_beval = beval;
4954
4955 if (p_beval)
4956 gui_mch_enable_beval_area(beval);
4957
4958 }
4959 return beval;
4960}
4961
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004962/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 static void
Bram Moolenaar442b4222010-05-24 21:34:22 +02004964Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965{
4966 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
4967 return;
4968
4969 if (cur_beval != NULL)
4970 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004971 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004973 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004974 // TRACE0("TTN_SHOW {{{");
4975 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00004976 break;
4977 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004978 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 delete_tooltip(cur_beval);
4980 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004981 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982
4983 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00004984 break;
4985 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004986 {
4987 /* if you get there then we have new common controls */
4988 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
4989 info->lpszText = (LPSTR)info->lParam;
4990 info->uFlags |= TTF_DI_SETITEM;
4991 }
Bram Moolenaar45360022005-07-21 21:08:21 +00004992 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 }
4994 }
4995}
4996
4997 static void
4998TrackUserActivity(UINT uMsg)
4999{
5000 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
5001 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
5002 LastActivity = GetTickCount();
5003}
5004
5005 void
5006gui_mch_destroy_beval_area(beval)
5007 BalloonEval *beval;
5008{
5009 vim_free(beval);
5010}
5011#endif /* FEAT_BEVAL */
5012
5013#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5014/*
5015 * We have multiple signs to draw at the same location. Draw the
5016 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
5017 */
5018 void
5019netbeans_draw_multisign_indicator(int row)
5020{
5021 int i;
5022 int y;
5023 int x;
5024
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005025 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005026 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005027
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 x = 0;
5029 y = TEXT_Y(row);
5030
5031 for (i = 0; i < gui.char_height - 3; i++)
5032 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
5033
5034 SetPixel(s_hdc, x+0, y, gui.currFgColor);
5035 SetPixel(s_hdc, x+2, y, gui.currFgColor);
5036 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
5037 SetPixel(s_hdc, x+1, y, gui.currFgColor);
5038 SetPixel(s_hdc, x+2, y, gui.currFgColor);
5039 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
5040 SetPixel(s_hdc, x+2, y, gui.currFgColor);
5041}
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005042
5043/*
5044 * Initialize the Winsock dll.
5045 */
5046 void
5047netbeans_init_winsock()
5048{
5049 WSADATA wsaData;
5050 int wsaerr;
5051
5052 if (WSInitialized)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005053 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005054
5055 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
5056 if (wsaerr == 0)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005057 WSInitialized = TRUE;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005058}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059#endif