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