blob: a00e4efdf36de3bf123de3a7ebccaf7dc578b3b4 [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 Moolenaar071d4272004-06-13 20:20:40 +000028/*
29 * These are new in Windows ME/XP, only defined in recent compilers.
30 */
31#ifndef HANDLE_WM_XBUTTONUP
32# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
33 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
34#endif
35#ifndef HANDLE_WM_XBUTTONDOWN
36# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
37 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
38#endif
39#ifndef HANDLE_WM_XBUTTONDBLCLK
40# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
41 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
42#endif
43
44/*
45 * Include the common stuff for MS-Windows GUI.
46 */
47#include "gui_w48.c"
48
49#ifdef FEAT_XPM_W32
50# include "xpm_w32.h"
51#endif
52
53#ifdef PROTO
54# define WINAPI
55#endif
56
57#ifdef __MINGW32__
58/*
59 * Add a lot of missing defines.
60 * They are not always missing, we need the #ifndef's.
61 */
62# ifndef _cdecl
63# define _cdecl
64# endif
65# ifndef IsMinimized
66# define IsMinimized(hwnd) IsIconic(hwnd)
67# endif
68# ifndef IsMaximized
69# define IsMaximized(hwnd) IsZoomed(hwnd)
70# endif
71# ifndef SelectFont
72# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
73# endif
74# ifndef GetStockBrush
75# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
76# endif
77# ifndef DeleteBrush
78# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
79# endif
80
81# ifndef HANDLE_WM_RBUTTONDBLCLK
82# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
83 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
84# endif
85# ifndef HANDLE_WM_MBUTTONUP
86# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
87 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
88# endif
89# ifndef HANDLE_WM_MBUTTONDBLCLK
90# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
91 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
92# endif
93# ifndef HANDLE_WM_LBUTTONDBLCLK
94# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
95 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
96# endif
97# ifndef HANDLE_WM_RBUTTONDOWN
98# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
99 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
100# endif
101# ifndef HANDLE_WM_MOUSEMOVE
102# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
103 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
104# endif
105# ifndef HANDLE_WM_RBUTTONUP
106# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
107 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
108# endif
109# ifndef HANDLE_WM_MBUTTONDOWN
110# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
111 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
112# endif
113# ifndef HANDLE_WM_LBUTTONUP
114# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
115 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
116# endif
117# ifndef HANDLE_WM_LBUTTONDOWN
118# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
119 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
120# endif
121# ifndef HANDLE_WM_SYSCHAR
122# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
123 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
124# endif
125# ifndef HANDLE_WM_ACTIVATEAPP
126# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
127 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
128# endif
129# ifndef HANDLE_WM_WINDOWPOSCHANGING
130# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
131 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
132# endif
133# ifndef HANDLE_WM_VSCROLL
134# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
135 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
136# endif
137# ifndef HANDLE_WM_SETFOCUS
138# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
139 ((fn)((hwnd), (HWND)(wParam)), 0L)
140# endif
141# ifndef HANDLE_WM_KILLFOCUS
142# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
143 ((fn)((hwnd), (HWND)(wParam)), 0L)
144# endif
145# ifndef HANDLE_WM_HSCROLL
146# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
147 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
148# endif
149# ifndef HANDLE_WM_DROPFILES
150# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
151 ((fn)((hwnd), (HDROP)(wParam)), 0L)
152# endif
153# ifndef HANDLE_WM_CHAR
154# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
155 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
156# endif
157# ifndef HANDLE_WM_SYSDEADCHAR
158# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
159 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
160# endif
161# ifndef HANDLE_WM_DEADCHAR
162# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
163 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
164# endif
165#endif /* __MINGW32__ */
166
167
168/* Some parameters for tearoff menus. All in pixels. */
169#define TEAROFF_PADDING_X 2
170#define TEAROFF_BUTTON_PAD_X 8
171#define TEAROFF_MIN_WIDTH 200
172#define TEAROFF_SUBMENU_LABEL ">>"
173#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
174
175
176/* For the Intellimouse: */
177#ifndef WM_MOUSEWHEEL
178#define WM_MOUSEWHEEL 0x20a
179#endif
180
181
182#ifdef FEAT_BEVAL
183# define ID_BEVAL_TOOLTIP 200
184# define BEVAL_TEXT_LEN MAXPATHL
185
Bram Moolenaar167632f2010-05-26 21:42:54 +0200186#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +0000187/* Work around old versions of basetsd.h which wrongly declares
188 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +0200189# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +0000190# define UINT_PTR UINT
191#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000192
193static void make_tooltip __ARGS((BalloonEval *beval, char *text, POINT pt));
194static void delete_tooltip __ARGS((BalloonEval *beval));
195static VOID CALLBACK BevalTimerProc __ARGS((HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime));
196
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000198static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +0000200
Bram Moolenaar82881492012-11-20 16:53:39 +0100201
202/* cproto fails on missing include files */
203#ifndef PROTO
204
Bram Moolenaar45360022005-07-21 21:08:21 +0000205/*
206 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000207 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +0000208 */
Bram Moolenaar82881492012-11-20 16:53:39 +0100209# include <pshpack1.h>
210
211#endif
Bram Moolenaar45360022005-07-21 21:08:21 +0000212
213typedef struct _DllVersionInfo
214{
215 DWORD cbSize;
216 DWORD dwMajorVersion;
217 DWORD dwMinorVersion;
218 DWORD dwBuildNumber;
219 DWORD dwPlatformID;
220} DLLVERSIONINFO;
221
Bram Moolenaar82881492012-11-20 16:53:39 +0100222#ifndef PROTO
223# include <poppack.h>
224#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +0000225
Bram Moolenaar45360022005-07-21 21:08:21 +0000226typedef struct tagTOOLINFOA_NEW
227{
228 UINT cbSize;
229 UINT uFlags;
230 HWND hwnd;
Bram Moolenaar281daf62009-12-24 15:11:40 +0000231 UINT_PTR uId;
Bram Moolenaar45360022005-07-21 21:08:21 +0000232 RECT rect;
233 HINSTANCE hinst;
234 LPSTR lpszText;
235 LPARAM lParam;
236} TOOLINFO_NEW;
237
238typedef struct tagNMTTDISPINFO_NEW
239{
240 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +0000241 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +0000242 char szText[80];
243 HINSTANCE hinst;
244 UINT uFlags;
245 LPARAM lParam;
246} NMTTDISPINFO_NEW;
247
Bram Moolenaar45360022005-07-21 21:08:21 +0000248typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
249#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000250# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#endif
252
Bram Moolenaar45360022005-07-21 21:08:21 +0000253#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000254# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +0000255#endif
256
257#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000258# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +0000259#endif
260
261#endif /* defined(FEAT_BEVAL) */
262
Bram Moolenaar2c7a7632007-05-10 18:19:11 +0000263#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
264/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
265 * it here if LPNMTTDISPINFO isn't defined.
266 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
267 * _MSC_VER. */
268# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
269typedef struct tagNMTTDISPINFOA {
270 NMHDR hdr;
271 LPSTR lpszText;
272 char szText[80];
273 HINSTANCE hinst;
274 UINT uFlags;
275 LPARAM lParam;
276} NMTTDISPINFOA, *LPNMTTDISPINFOA;
277# define LPNMTTDISPINFO LPNMTTDISPINFOA
278
279# ifdef FEAT_MBYTE
280typedef struct tagNMTTDISPINFOW {
281 NMHDR hdr;
282 LPWSTR lpszText;
283 WCHAR szText[80];
284 HINSTANCE hinst;
285 UINT uFlags;
286 LPARAM lParam;
287} NMTTDISPINFOW, *LPNMTTDISPINFOW;
288# endif
289# endif
290#endif
291
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000292#ifndef TTN_GETDISPINFOW
293# define TTN_GETDISPINFOW (TTN_FIRST - 10)
294#endif
295
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296/* Local variables: */
297
298#ifdef FEAT_MENU
299static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +0200300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301
302/*
303 * Use the system font for dialogs and tear-off menus. Remove this line to
304 * use DLG_FONT_NAME.
305 */
Bram Moolenaar786989b2010-10-27 12:15:33 +0200306#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307
308#define VIM_NAME "vim"
309#define VIM_CLASS "Vim"
310#define VIM_CLASSW L"Vim"
311
312/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
313 * thus there should be room for every dialog. For tearoffs it's made bigger
314 * when needed. */
315#define DLG_ALLOC_SIZE 16 * 1024
316
317/*
318 * stuff for dialogs, menus, tearoffs etc.
319 */
320static LRESULT APIENTRY dialog_callback(HWND, UINT, WPARAM, LPARAM);
321static LRESULT APIENTRY tearoff_callback(HWND, UINT, WPARAM, LPARAM);
322static PWORD
323add_dialog_element(
324 PWORD p,
325 DWORD lStyle,
326 WORD x,
327 WORD y,
328 WORD w,
329 WORD h,
330 WORD Id,
331 WORD clss,
332 const char *caption);
333static LPWORD lpwAlign(LPWORD);
334static int nCopyAnsiToWideChar(LPWORD, LPSTR);
335static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
336static void get_dialog_font_metrics(void);
337
338static int dialog_default_button = -1;
339
340/* Intellimouse support */
341static int mouse_scroll_lines = 0;
342static UINT msh_msgmousewheel = 0;
343
344static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
345#ifdef FEAT_TOOLBAR
346static void initialise_toolbar(void);
347static int get_toolbar_bitmap(vimmenu_T *menu);
348#endif
349
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000350#ifdef FEAT_GUI_TABLINE
351static void initialise_tabline(void);
352#endif
353
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354#ifdef FEAT_MBYTE_IME
355static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
356static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
357#endif
358#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
359# ifdef NOIME
360typedef struct tagCOMPOSITIONFORM {
361 DWORD dwStyle;
362 POINT ptCurrentPos;
363 RECT rcArea;
364} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
365typedef HANDLE HIMC;
366# endif
367
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000368static HINSTANCE hLibImm = NULL;
369static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
370static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
371static HIMC (WINAPI *pImmGetContext)(HWND);
372static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
373static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
374static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
375static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
376static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
377static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
378static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
379static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +0000380static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381static void dyn_imm_load(void);
382#else
383# define pImmGetCompositionStringA ImmGetCompositionStringA
384# define pImmGetCompositionStringW ImmGetCompositionStringW
385# define pImmGetContext ImmGetContext
386# define pImmAssociateContext ImmAssociateContext
387# define pImmReleaseContext ImmReleaseContext
388# define pImmGetOpenStatus ImmGetOpenStatus
389# define pImmSetOpenStatus ImmSetOpenStatus
390# define pImmGetCompositionFont ImmGetCompositionFontA
391# define pImmSetCompositionFont ImmSetCompositionFontA
392# define pImmSetCompositionWindow ImmSetCompositionWindow
393# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +0000394# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395#endif
396
397#ifndef ETO_IGNORELANGUAGE
398# define ETO_IGNORELANGUAGE 0x1000
399#endif
400
401/* multi monitor support */
402typedef struct _MONITORINFOstruct
403{
404 DWORD cbSize;
405 RECT rcMonitor;
406 RECT rcWork;
407 DWORD dwFlags;
408} _MONITORINFO;
409
410typedef HANDLE _HMONITOR;
411typedef _HMONITOR (WINAPI *TMonitorFromWindow)(HWND, DWORD);
412typedef BOOL (WINAPI *TGetMonitorInfo)(_HMONITOR, _MONITORINFO *);
413
414static TMonitorFromWindow pMonitorFromWindow = NULL;
415static TGetMonitorInfo pGetMonitorInfo = NULL;
416static HANDLE user32_lib = NULL;
417#ifdef FEAT_NETBEANS_INTG
418int WSInitialized = FALSE; /* WinSock is initialized */
419#endif
420/*
421 * Return TRUE when running under Windows NT 3.x or Win32s, both of which have
422 * less fancy GUI APIs.
423 */
424 static int
425is_winnt_3(void)
426{
427 return ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
428 && os_version.dwMajorVersion == 3)
429 || (os_version.dwPlatformId == VER_PLATFORM_WIN32s));
430}
431
432/*
433 * Return TRUE when running under Win32s.
434 */
435 int
436gui_is_win32s(void)
437{
438 return (os_version.dwPlatformId == VER_PLATFORM_WIN32s);
439}
440
441#ifdef FEAT_MENU
442/*
443 * Figure out how high the menu bar is at the moment.
444 */
445 static int
446gui_mswin_get_menu_height(
447 int fix_window) /* If TRUE, resize window if menu height changed */
448{
449 static int old_menu_height = -1;
450
451 RECT rc1, rc2;
452 int num;
453 int menu_height;
454
455 if (gui.menu_is_active)
456 num = GetMenuItemCount(s_menuBar);
457 else
458 num = 0;
459
460 if (num == 0)
461 menu_height = 0;
462 else
463 {
464 if (is_winnt_3()) /* for NT 3.xx */
465 {
466 if (gui.starting)
467 menu_height = GetSystemMetrics(SM_CYMENU);
468 else
469 {
470 RECT r1, r2;
471 int frameht = GetSystemMetrics(SM_CYFRAME);
472 int capht = GetSystemMetrics(SM_CYCAPTION);
473
474 /* get window rect of s_hwnd
475 * get client rect of s_hwnd
476 * get cap height
477 * subtract from window rect, the sum of client height,
478 * (if not maximized)frame thickness, and caption height.
479 */
480 GetWindowRect(s_hwnd, &r1);
481 GetClientRect(s_hwnd, &r2);
482 menu_height = r1.bottom - r1.top - (r2.bottom - r2.top
483 + 2 * frameht * (!IsZoomed(s_hwnd)) + capht);
484 }
485 }
486 else /* win95 and variants (NT 4.0, I guess) */
487 {
488 /*
489 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
490 * seem to have been set yet, so menu wraps in default window
491 * width which is very narrow. Instead just return height of a
492 * single menu item. Will still be wrong when the menu really
493 * should wrap over more than one line.
494 */
495 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
496 if (gui.starting)
497 menu_height = rc1.bottom - rc1.top + 1;
498 else
499 {
500 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
501 menu_height = rc2.bottom - rc1.top + 1;
502 }
503 }
504 }
505
506 if (fix_window && menu_height != old_menu_height)
507 {
508 old_menu_height = menu_height;
Bram Moolenaarafa24992006-03-27 20:58:26 +0000509 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 }
511
512 return menu_height;
513}
514#endif /*FEAT_MENU*/
515
516
517/*
518 * Setup for the Intellimouse
519 */
520 static void
521init_mouse_wheel(void)
522{
523
524#ifndef SPI_GETWHEELSCROLLLINES
525# define SPI_GETWHEELSCROLLLINES 104
526#endif
Bram Moolenaare7566042005-06-17 22:00:15 +0000527#ifndef SPI_SETWHEELSCROLLLINES
528# define SPI_SETWHEELSCROLLLINES 105
529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530
531#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
532#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
533#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
534#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
535
536 HWND hdl_mswheel;
537 UINT msh_msgscrolllines;
538
539 msh_msgmousewheel = 0;
540 mouse_scroll_lines = 3; /* reasonable default */
541
542 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
543 && os_version.dwMajorVersion >= 4)
544 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
545 && ((os_version.dwMajorVersion == 4
546 && os_version.dwMinorVersion >= 10)
547 || os_version.dwMajorVersion >= 5)))
548 {
549 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
550 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
551 &mouse_scroll_lines, 0);
552 }
553 else if (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
554 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
555 && os_version.dwMajorVersion < 4))
556 { /*
557 * If Win95 or NT 3.51,
558 * try to find the hidden point32 window.
559 */
560 hdl_mswheel = FindWindow(VMOUSEZ_CLASSNAME, VMOUSEZ_TITLE);
561 if (hdl_mswheel)
562 {
563 msh_msgscrolllines = RegisterWindowMessage(VMSH_SCROLL_LINES);
564 if (msh_msgscrolllines)
565 {
566 mouse_scroll_lines = (int)SendMessage(hdl_mswheel,
567 msh_msgscrolllines, 0, 0);
568 msh_msgmousewheel = RegisterWindowMessage(VMSH_MOUSEWHEEL);
569 }
570 }
571 }
572}
573
574
575/* Intellimouse wheel handler */
576 static void
577_OnMouseWheel(
578 HWND hwnd,
579 short zDelta)
580{
581/* Treat a mouse wheel event as if it were a scroll request */
582 int i;
583 int size;
584 HWND hwndCtl;
585
586 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
587 {
588 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
589 size = curwin->w_scrollbars[SBAR_RIGHT].size;
590 }
591 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
592 {
593 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
594 size = curwin->w_scrollbars[SBAR_LEFT].size;
595 }
596 else
597 return;
598
599 size = curwin->w_height;
600 if (mouse_scroll_lines == 0)
601 init_mouse_wheel();
602
603 if (mouse_scroll_lines > 0
604 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
605 {
606 for (i = mouse_scroll_lines; i > 0; --i)
607 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
608 }
609 else
610 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
611}
612
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000613#ifdef USE_SYSMENU_FONT
614/*
615 * Get Menu Font.
616 * Return OK or FAIL.
617 */
618 static int
619gui_w32_get_menu_font(LOGFONT *lf)
620{
621 NONCLIENTMETRICS nm;
622
623 nm.cbSize = sizeof(NONCLIENTMETRICS);
624 if (!SystemParametersInfo(
625 SPI_GETNONCLIENTMETRICS,
626 sizeof(NONCLIENTMETRICS),
627 &nm,
628 0))
629 return FAIL;
630 *lf = nm.lfMenuFont;
631 return OK;
632}
633#endif
634
635
636#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
637/*
638 * Set the GUI tabline font to the system menu font
639 */
640 static void
641set_tabline_font(void)
642{
643 LOGFONT lfSysmenu;
644 HFONT font;
645 HWND hwnd;
646 HDC hdc;
647 HFONT hfntOld;
648 TEXTMETRIC tm;
649
650 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
651 return;
652
653 font = CreateFontIndirect(&lfSysmenu);
654
655 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
656
657 /*
658 * Compute the height of the font used for the tab text
659 */
660 hwnd = GetDesktopWindow();
661 hdc = GetWindowDC(hwnd);
662 hfntOld = SelectFont(hdc, font);
663
664 GetTextMetrics(hdc, &tm);
665
666 SelectFont(hdc, hfntOld);
667 ReleaseDC(hwnd, hdc);
668
669 /*
670 * The space used by the tab border and the space between the tab label
671 * and the tab border is included as 7.
672 */
673 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
674}
675#endif
676
Bram Moolenaar520470a2005-06-16 21:59:56 +0000677/*
678 * Invoked when a setting was changed.
679 */
680 static LRESULT CALLBACK
681_OnSettingChange(UINT n)
682{
683 if (n == SPI_SETWHEELSCROLLLINES)
684 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
685 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000686#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
687 if (n == SPI_SETNONCLIENTMETRICS)
688 set_tabline_font();
689#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +0000690 return 0;
691}
692
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693#ifdef FEAT_NETBEANS_INTG
694 static void
695_OnWindowPosChanged(
696 HWND hwnd,
697 const LPWINDOWPOS lpwpos)
698{
699 static int x = 0, y = 0, cx = 0, cy = 0;
700
701 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
702 || lpwpos->cx != cx || lpwpos->cy != cy))
703 {
704 x = lpwpos->x;
705 y = lpwpos->y;
706 cx = lpwpos->cx;
707 cy = lpwpos->cy;
708 netbeans_frame_moved(x, y);
709 }
710 /* Allow to send WM_SIZE and WM_MOVE */
711 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
712}
713#endif
714
715 static int
716_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 UINT fwSide,
718 LPRECT lprc)
719{
720 int w, h;
721 int valid_w, valid_h;
722 int w_offset, h_offset;
723
724 w = lprc->right - lprc->left;
725 h = lprc->bottom - lprc->top;
726 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
727 w_offset = w - valid_w;
728 h_offset = h - valid_h;
729
730 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
731 || fwSide == WMSZ_BOTTOMLEFT)
732 lprc->left += w_offset;
733 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
734 || fwSide == WMSZ_BOTTOMRIGHT)
735 lprc->right -= w_offset;
736
737 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
738 || fwSide == WMSZ_TOPRIGHT)
739 lprc->top += h_offset;
740 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
741 || fwSide == WMSZ_BOTTOMRIGHT)
742 lprc->bottom -= h_offset;
743 return TRUE;
744}
745
746
747
748 static LRESULT CALLBACK
749_WndProc(
750 HWND hwnd,
751 UINT uMsg,
752 WPARAM wParam,
753 LPARAM lParam)
754{
755 /*
756 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
757 hwnd, uMsg, wParam, lParam);
758 */
759
760 HandleMouseHide(uMsg, lParam);
761
762 s_uMsg = uMsg;
763 s_wParam = wParam;
764 s_lParam = lParam;
765
766 switch (uMsg)
767 {
768 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
769 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
770 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
771 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
772 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
773 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
774 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
775 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
776 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
777#ifdef FEAT_MENU
778 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
779#endif
780 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
781 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
782 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
783 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
784 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
785 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
786 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
787 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
788 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
789#ifdef FEAT_NETBEANS_INTG
790 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
791#endif
792
Bram Moolenaarafa24992006-03-27 20:58:26 +0000793#ifdef FEAT_GUI_TABLINE
794 case WM_RBUTTONUP:
795 {
796 if (gui_mch_showing_tabline())
797 {
798 POINT pt;
799 RECT rect;
800
801 /*
802 * If the cursor is on the tabline, display the tab menu
803 */
804 GetCursorPos((LPPOINT)&pt);
805 GetWindowRect(s_textArea, &rect);
806 if (pt.y < rect.top)
807 {
808 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +0100809 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +0000810 }
811 }
812 return MyWindowProc(hwnd, uMsg, wParam, lParam);
813 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000814 case WM_LBUTTONDBLCLK:
815 {
816 /*
817 * If the user double clicked the tabline, create a new tab
818 */
819 if (gui_mch_showing_tabline())
820 {
821 POINT pt;
822 RECT rect;
823
824 GetCursorPos((LPPOINT)&pt);
825 GetWindowRect(s_textArea, &rect);
826 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000827 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000828 }
829 return MyWindowProc(hwnd, uMsg, wParam, lParam);
830 }
Bram Moolenaarafa24992006-03-27 20:58:26 +0000831#endif
832
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 case WM_QUERYENDSESSION: /* System wants to go down. */
834 gui_shell_closed(); /* Will exit when no changed buffers. */
835 return FALSE; /* Do NOT allow system to go down. */
836
837 case WM_ENDSESSION:
838 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +0100839 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +0100841 return 0L;
842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 break;
844
845 case WM_CHAR:
846 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
847 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000848 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 return 0L;
850
851 case WM_SYSCHAR:
852 /*
853 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
854 * shortcut key, handle like a typed ALT key, otherwise call Windows
855 * ALT key handling.
856 */
857#ifdef FEAT_MENU
858 if ( !gui.menu_is_active
859 || p_wak[0] == 'n'
860 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
861 )
862#endif
863 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000864 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 return 0L;
866 }
867#ifdef FEAT_MENU
868 else
869 return MyWindowProc(hwnd, uMsg, wParam, lParam);
870#endif
871
872 case WM_SYSKEYUP:
873#ifdef FEAT_MENU
874 /* This used to be done only when menu is active: ALT key is used for
875 * that. But that caused problems when menu is disabled and using
876 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
877 * are received, mouse pointer remains hidden. */
878 return MyWindowProc(hwnd, uMsg, wParam, lParam);
879#else
Bram Moolenaar213ae482011-12-15 21:51:36 +0100880 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881#endif
882
883 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000884 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885
886 case WM_MOUSEWHEEL:
887 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +0100888 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889
Bram Moolenaar520470a2005-06-16 21:59:56 +0000890 /* Notification for change in SystemParametersInfo() */
891 case WM_SETTINGCHANGE:
892 return _OnSettingChange((UINT)wParam);
893
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000894#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 case WM_NOTIFY:
896 switch (((LPNMHDR) lParam)->code)
897 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000898# ifdef FEAT_MBYTE
899 case TTN_GETDISPINFOW:
900# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000901 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000903 LPNMHDR hdr = (LPNMHDR)lParam;
904 char_u *str = NULL;
905 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000906
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000907 vim_free(tt_text);
908 tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000909
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000910# ifdef FEAT_GUI_TABLINE
911 if (gui_mch_showing_tabline()
912 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000913 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000914 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000915 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000916 * Mouse is over the GUI tabline. Display the
917 * tooltip for the tab under the cursor
918 *
919 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000920 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000921 GetCursorPos(&pt);
922 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000923 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000924 TCHITTESTINFO htinfo;
925 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000926
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000927 /*
928 * Get the tab under the cursor
929 */
930 htinfo.pt.x = pt.x;
931 htinfo.pt.y = pt.y;
932 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
933 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000934 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000935 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000936
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000937 tp = find_tabpage(idx + 1);
938 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000939 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000940 get_tabline_label(tp, TRUE);
941 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000942 }
943 }
944 }
945 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000946# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000947# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000948# ifdef FEAT_GUI_TABLINE
949 else
950# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000952 UINT idButton;
953 vimmenu_T *pMenu;
954
955 idButton = (UINT) hdr->idFrom;
956 pMenu = gui_mswin_find_menu(root_menu, idButton);
957 if (pMenu)
958 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000960# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000961 if (str != NULL)
962 {
963# ifdef FEAT_MBYTE
964 if (hdr->code == TTN_GETDISPINFOW)
965 {
966 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
967
Bram Moolenaar6c9176d2008-01-03 19:45:15 +0000968 /* Set the maximum width, this also enables using
969 * \n for line break. */
970 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
971 0, 500);
972
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000973 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000974 lpdi->lpszText = tt_text;
975 /* can't show tooltip if failed */
976 }
977 else
978# endif
979 {
980 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
981
Bram Moolenaar6c9176d2008-01-03 19:45:15 +0000982 /* Set the maximum width, this also enables using
983 * \n for line break. */
984 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
985 0, 500);
986
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000987 if (STRLEN(str) < sizeof(lpdi->szText)
988 || ((tt_text = vim_strsave(str)) == NULL))
989 vim_strncpy(lpdi->szText, str,
990 sizeof(lpdi->szText) - 1);
991 else
992 lpdi->lpszText = tt_text;
993 }
994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 }
996 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000997# ifdef FEAT_GUI_TABLINE
998 case TCN_SELCHANGE:
999 if (gui_mch_showing_tabline()
1000 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01001001 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001002 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01001003 return 0L;
1004 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001005 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00001006
1007 case NM_RCLICK:
1008 if (gui_mch_showing_tabline()
1009 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01001010 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00001011 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01001012 return 0L;
1013 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00001014 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001017# ifdef FEAT_GUI_TABLINE
1018 if (gui_mch_showing_tabline()
1019 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
1020 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1021# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 break;
1023 }
1024 break;
1025#endif
1026#if defined(MENUHINTS) && defined(FEAT_MENU)
1027 case WM_MENUSELECT:
1028 if (((UINT) HIWORD(wParam)
1029 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
1030 == MF_HILITE
1031 && (State & CMDLINE) == 0)
1032 {
1033 UINT idButton;
1034 vimmenu_T *pMenu;
1035 static int did_menu_tip = FALSE;
1036
1037 if (did_menu_tip)
1038 {
1039 msg_clr_cmdline();
1040 setcursor();
1041 out_flush();
1042 did_menu_tip = FALSE;
1043 }
1044
1045 idButton = (UINT)LOWORD(wParam);
1046 pMenu = gui_mswin_find_menu(root_menu, idButton);
1047 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
1048 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
1049 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00001050 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00001052 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 setcursor();
1054 out_flush();
1055 did_menu_tip = TRUE;
1056 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01001057 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 }
1059 break;
1060#endif
1061 case WM_NCHITTEST:
1062 {
1063 LRESULT result;
1064 int x, y;
1065 int xPos = GET_X_LPARAM(lParam);
1066
1067 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
1068 if (result == HTCLIENT)
1069 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001070#ifdef FEAT_GUI_TABLINE
1071 if (gui_mch_showing_tabline())
1072 {
1073 int yPos = GET_Y_LPARAM(lParam);
1074 RECT rct;
1075
1076 /* If the cursor is on the GUI tabline, don't process this
1077 * event */
1078 GetWindowRect(s_textArea, &rct);
1079 if (yPos < rct.top)
1080 return result;
1081 }
1082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 gui_mch_get_winpos(&x, &y);
1084 xPos -= x;
1085
1086 if (xPos < 48) /* <VN> TODO should use system metric? */
1087 return HTBOTTOMLEFT;
1088 else
1089 return HTBOTTOMRIGHT;
1090 }
1091 else
1092 return result;
1093 }
1094 /* break; notreached */
1095
1096#ifdef FEAT_MBYTE_IME
1097 case WM_IME_NOTIFY:
1098 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
1099 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01001100 return 1L;
1101
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 case WM_IME_COMPOSITION:
1103 if (!_OnImeComposition(hwnd, wParam, lParam))
1104 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01001105 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106#endif
1107
1108 default:
1109 if (uMsg == msh_msgmousewheel && msh_msgmousewheel != 0)
1110 { /* handle MSH_MOUSEWHEEL messages for Intellimouse */
1111 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01001112 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 }
1114#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001115 else if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 {
1117 _OnFindRepl();
1118 }
1119#endif
1120 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1121 }
1122
Bram Moolenaar2787ab92011-12-14 15:23:59 +01001123 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124}
1125
1126/*
1127 * End of call-back routines
1128 */
1129
1130/* parent window, if specified with -P */
1131HWND vim_parent_hwnd = NULL;
1132
1133 static BOOL CALLBACK
1134FindWindowTitle(HWND hwnd, LPARAM lParam)
1135{
1136 char buf[2048];
1137 char *title = (char *)lParam;
1138
1139 if (GetWindowText(hwnd, buf, sizeof(buf)))
1140 {
1141 if (strstr(buf, title) != NULL)
1142 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001143 /* Found it. Store the window ref. and quit searching if MDI
1144 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001146 if (vim_parent_hwnd != NULL)
1147 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 }
1149 }
1150 return TRUE; /* continue searching */
1151}
1152
1153/*
1154 * Invoked for '-P "title"' argument: search for parent application to open
1155 * our window in.
1156 */
1157 void
1158gui_mch_set_parent(char *title)
1159{
1160 EnumWindows(FindWindowTitle, (LPARAM)title);
1161 if (vim_parent_hwnd == NULL)
1162 {
1163 EMSG2(_("E671: Cannot find window title \"%s\""), title);
1164 mch_exit(2);
1165 }
1166}
1167
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001168#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 static void
1170ole_error(char *arg)
1171{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00001172 char buf[IOSIZE];
1173
1174 /* Can't use EMSG() here, we have not finished initialisation yet. */
1175 vim_snprintf(buf, IOSIZE,
1176 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
1177 arg);
1178 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181
1182/*
1183 * Parse the GUI related command-line arguments. Any arguments used are
1184 * deleted from argv, and *argc is decremented accordingly. This is called
1185 * when vim is started, whether or not the GUI has been started.
1186 */
1187 void
1188gui_mch_prepare(int *argc, char **argv)
1189{
1190 int silent = FALSE;
1191 int idx;
1192
1193 /* Check for special OLE command line parameters */
1194 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
1195 {
1196 /* Check for a "-silent" argument first. */
1197 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
1198 && (argv[2][0] == '-' || argv[2][0] == '/'))
1199 {
1200 silent = TRUE;
1201 idx = 2;
1202 }
1203 else
1204 idx = 1;
1205
1206 /* Register Vim as an OLE Automation server */
1207 if (STRICMP(argv[idx] + 1, "register") == 0)
1208 {
1209#ifdef FEAT_OLE
1210 RegisterMe(silent);
1211 mch_exit(0);
1212#else
1213 if (!silent)
1214 ole_error("register");
1215 mch_exit(2);
1216#endif
1217 }
1218
1219 /* Unregister Vim as an OLE Automation server */
1220 if (STRICMP(argv[idx] + 1, "unregister") == 0)
1221 {
1222#ifdef FEAT_OLE
1223 UnregisterMe(!silent);
1224 mch_exit(0);
1225#else
1226 if (!silent)
1227 ole_error("unregister");
1228 mch_exit(2);
1229#endif
1230 }
1231
1232 /* Ignore an -embedding argument. It is only relevant if the
1233 * application wants to treat the case when it is started manually
1234 * differently from the case where it is started via automation (and
1235 * we don't).
1236 */
1237 if (STRICMP(argv[idx] + 1, "embedding") == 0)
1238 {
1239#ifdef FEAT_OLE
1240 *argc = 1;
1241#else
1242 ole_error("embedding");
1243 mch_exit(2);
1244#endif
1245 }
1246 }
1247
1248#ifdef FEAT_OLE
1249 {
1250 int bDoRestart = FALSE;
1251
1252 InitOLE(&bDoRestart);
1253 /* automatically exit after registering */
1254 if (bDoRestart)
1255 mch_exit(0);
1256 }
1257#endif
1258
1259#ifdef FEAT_NETBEANS_INTG
1260 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001261 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 int arg;
1263
1264 for (arg = 1; arg < *argc; arg++)
1265 if (strncmp("-nb", argv[arg], 3) == 0)
1266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 netbeansArg = argv[arg];
1268 mch_memmove(&argv[arg], &argv[arg + 1],
1269 (--*argc - arg) * sizeof(char *));
1270 argv[*argc] = NULL;
1271 break; /* enough? */
1272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 }
1274#endif
1275
1276 /* get the OS version info */
1277 os_version.dwOSVersionInfoSize = sizeof(os_version);
1278 GetVersionEx(&os_version); /* this call works on Win32s, Win95 and WinNT */
1279
1280 /* try and load the user32.dll library and get the entry points for
1281 * multi-monitor-support. */
Bram Moolenaarebbcb822010-10-23 14:02:54 +02001282 if ((user32_lib = vimLoadLib("User32.dll")) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 {
1284 pMonitorFromWindow = (TMonitorFromWindow)GetProcAddress(user32_lib,
1285 "MonitorFromWindow");
1286
1287 /* there are ...A and ...W version of GetMonitorInfo - looking at
1288 * winuser.h, they have exactly the same declaration. */
1289 pGetMonitorInfo = (TGetMonitorInfo)GetProcAddress(user32_lib,
1290 "GetMonitorInfoA");
1291 }
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001292
1293#ifdef FEAT_MBYTE
1294 /* If the OS is Windows NT, use wide functions;
1295 * this enables common dialogs input unicode from IME. */
1296 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
1297 {
1298 pDispatchMessage = DispatchMessageW;
1299 pGetMessage = GetMessageW;
1300 pIsDialogMessage = IsDialogMessageW;
1301 pPeekMessage = PeekMessageW;
1302 }
1303 else
1304 {
1305 pDispatchMessage = DispatchMessageA;
1306 pGetMessage = GetMessageA;
1307 pIsDialogMessage = IsDialogMessageA;
1308 pPeekMessage = PeekMessageA;
1309 }
1310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311}
1312
1313/*
1314 * Initialise the GUI. Create all the windows, set up all the call-backs
1315 * etc.
1316 */
1317 int
1318gui_mch_init(void)
1319{
1320 const char szVimWndClass[] = VIM_CLASS;
1321 const char szTextAreaClass[] = "VimTextArea";
1322 WNDCLASS wndclass;
1323#ifdef FEAT_MBYTE
1324 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01001325 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 WNDCLASSW wndclassw;
1327#endif
1328#ifdef GLOBAL_IME
1329 ATOM atom;
1330#endif
1331
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 /* Return here if the window was already opened (happens when
1333 * gui_mch_dialog() is called early). */
1334 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00001335 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336
1337 /*
1338 * Load the tearoff bitmap
1339 */
1340#ifdef FEAT_TEAROFF
1341 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
1342#endif
1343
1344 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
1345 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
1346#ifdef FEAT_MENU
1347 gui.menu_height = 0; /* Windows takes care of this */
1348#endif
1349 gui.border_width = 0;
1350
1351 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
1352
1353#ifdef FEAT_MBYTE
1354 /* First try using the wide version, so that we can use any title.
1355 * Otherwise only characters in the active codepage will work. */
1356 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
1357 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001358 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 wndclassw.lpfnWndProc = _WndProc;
1360 wndclassw.cbClsExtra = 0;
1361 wndclassw.cbWndExtra = 0;
1362 wndclassw.hInstance = s_hinst;
1363 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
1364 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
1365 wndclassw.hbrBackground = s_brush;
1366 wndclassw.lpszMenuName = NULL;
1367 wndclassw.lpszClassName = szVimWndClassW;
1368
1369 if ((
1370#ifdef GLOBAL_IME
1371 atom =
1372#endif
1373 RegisterClassW(&wndclassw)) == 0)
1374 {
1375 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1376 return FAIL;
1377
1378 /* Must be Windows 98, fall back to non-wide function. */
1379 }
1380 else
1381 wide_WindowProc = TRUE;
1382 }
1383
1384 if (!wide_WindowProc)
1385#endif
1386
1387 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
1388 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001389 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 wndclass.lpfnWndProc = _WndProc;
1391 wndclass.cbClsExtra = 0;
1392 wndclass.cbWndExtra = 0;
1393 wndclass.hInstance = s_hinst;
1394 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
1395 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1396 wndclass.hbrBackground = s_brush;
1397 wndclass.lpszMenuName = NULL;
1398 wndclass.lpszClassName = szVimWndClass;
1399
1400 if ((
1401#ifdef GLOBAL_IME
1402 atom =
1403#endif
1404 RegisterClass(&wndclass)) == 0)
1405 return FAIL;
1406 }
1407
1408 if (vim_parent_hwnd != NULL)
1409 {
1410#ifdef HAVE_TRY_EXCEPT
1411 __try
1412 {
1413#endif
1414 /* Open inside the specified parent window.
1415 * TODO: last argument should point to a CLIENTCREATESTRUCT
1416 * structure. */
1417 s_hwnd = CreateWindowEx(
1418 WS_EX_MDICHILD,
1419 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02001420 WS_OVERLAPPEDWINDOW | WS_CHILD
1421 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
1423 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
1424 100, /* Any value will do */
1425 100, /* Any value will do */
1426 vim_parent_hwnd, NULL,
1427 s_hinst, NULL);
1428#ifdef HAVE_TRY_EXCEPT
1429 }
1430 __except(EXCEPTION_EXECUTE_HANDLER)
1431 {
1432 /* NOP */
1433 }
1434#endif
1435 if (s_hwnd == NULL)
1436 {
1437 EMSG(_("E672: Unable to open window inside MDI application"));
1438 mch_exit(2);
1439 }
1440 }
1441 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001442 {
1443 /* If the provided windowid is not valid reset it to zero, so that it
1444 * is ignored and we open our own window. */
1445 if (IsWindow((HWND)win_socket_id) <= 0)
1446 win_socket_id = 0;
1447
1448 /* Create a window. If win_socket_id is not zero without border and
1449 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00001451 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02001452 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
1453 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00001454 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
1455 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
1456 100, /* Any value will do */
1457 100, /* Any value will do */
1458 NULL, NULL,
1459 s_hinst, NULL);
1460 if (s_hwnd != NULL && win_socket_id != 0)
1461 {
1462 SetParent(s_hwnd, (HWND)win_socket_id);
1463 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
1464 }
1465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466
1467 if (s_hwnd == NULL)
1468 return FAIL;
1469
1470#ifdef GLOBAL_IME
1471 global_ime_init(atom, s_hwnd);
1472#endif
1473#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
1474 dyn_imm_load();
1475#endif
1476
1477 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01001478#ifdef FEAT_MBYTE
1479 if (wide_WindowProc)
1480 {
1481 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
1482 {
1483 wndclassw.style = CS_OWNDC;
1484 wndclassw.lpfnWndProc = _TextAreaWndProc;
1485 wndclassw.cbClsExtra = 0;
1486 wndclassw.cbWndExtra = 0;
1487 wndclassw.hInstance = s_hinst;
1488 wndclassw.hIcon = NULL;
1489 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
1490 wndclassw.hbrBackground = NULL;
1491 wndclassw.lpszMenuName = NULL;
1492 wndclassw.lpszClassName = szTextAreaClassW;
1493
1494 if (RegisterClassW(&wndclassw) == 0)
1495 return FAIL;
1496 }
1497 }
1498 else
1499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
1501 {
1502 wndclass.style = CS_OWNDC;
1503 wndclass.lpfnWndProc = _TextAreaWndProc;
1504 wndclass.cbClsExtra = 0;
1505 wndclass.cbWndExtra = 0;
1506 wndclass.hInstance = s_hinst;
1507 wndclass.hIcon = NULL;
1508 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1509 wndclass.hbrBackground = NULL;
1510 wndclass.lpszMenuName = NULL;
1511 wndclass.lpszClassName = szTextAreaClass;
1512
1513 if (RegisterClass(&wndclass) == 0)
1514 return FAIL;
1515 }
1516 s_textArea = CreateWindowEx(
1517 WS_EX_CLIENTEDGE,
1518 szTextAreaClass, "Vim text area",
1519 WS_CHILD | WS_VISIBLE, 0, 0,
1520 100, /* Any value will do for now */
1521 100, /* Any value will do for now */
1522 s_hwnd, NULL,
1523 s_hinst, NULL);
1524
1525 if (s_textArea == NULL)
1526 return FAIL;
1527
1528#ifdef FEAT_MENU
1529 s_menuBar = CreateMenu();
1530#endif
1531 s_hdc = GetDC(s_textArea);
1532
1533#ifdef MSWIN16_FASTTEXT
1534 SetBkMode(s_hdc, OPAQUE);
1535#endif
1536
1537#ifdef FEAT_WINDOWS
1538 DragAcceptFiles(s_hwnd, TRUE);
1539#endif
1540
1541 /* Do we need to bother with this? */
1542 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
1543
1544 /* Get background/foreground colors from the system */
1545 gui_mch_def_colors();
1546
1547 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
1548 * file) */
1549 set_normal_colors();
1550
1551 /*
1552 * Check that none of the colors are the same as the background color.
1553 * Then store the current values as the defaults.
1554 */
1555 gui_check_colors();
1556 gui.def_norm_pixel = gui.norm_pixel;
1557 gui.def_back_pixel = gui.back_pixel;
1558
1559 /* Get the colors for the highlight groups (gui_check_colors() might have
1560 * changed them) */
1561 highlight_gui_started();
1562
1563 /*
1564 * Start out by adding the configured border width into the border offset
1565 */
1566 gui.border_offset = gui.border_width + 2; /*CLIENT EDGE*/
1567
1568 /*
1569 * Set up for Intellimouse processing
1570 */
1571 init_mouse_wheel();
1572
1573 /*
1574 * compute a couple of metrics used for the dialogs
1575 */
1576 get_dialog_font_metrics();
1577#ifdef FEAT_TOOLBAR
1578 /*
1579 * Create the toolbar
1580 */
1581 initialise_toolbar();
1582#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001583#ifdef FEAT_GUI_TABLINE
1584 /*
1585 * Create the tabline
1586 */
1587 initialise_tabline();
1588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589#ifdef MSWIN_FIND_REPLACE
1590 /*
1591 * Initialise the dialog box stuff
1592 */
1593 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
1594
1595 /* Initialise the struct */
1596 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
1597 s_findrep_struct.lpstrFindWhat = alloc(MSWIN_FR_BUFSIZE);
1598 s_findrep_struct.lpstrFindWhat[0] = NUL;
1599 s_findrep_struct.lpstrReplaceWith = alloc(MSWIN_FR_BUFSIZE);
1600 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1601 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
1602 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00001603# if defined(FEAT_MBYTE) && defined(WIN3264)
1604 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
1605 s_findrep_struct_w.lpstrFindWhat =
1606 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
1607 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
1608 s_findrep_struct_w.lpstrReplaceWith =
1609 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
1610 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
1611 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
1612 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
1613# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02001616#ifdef FEAT_EVAL
Bram Moolenaaraf62ff32013-03-19 14:48:29 +01001617# ifndef HandleToLong
Bram Moolenaar4da95d32011-07-07 17:43:41 +02001618/* HandleToLong() only exists in compilers that can do 64 bit builds */
1619# define HandleToLong(h) ((long)(h))
1620# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02001621 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02001622 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02001623#endif
1624
Bram Moolenaar748bf032005-02-02 23:04:36 +00001625theend:
1626 /* Display any pending error messages */
1627 display_errors();
1628
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 return OK;
1630}
1631
1632/*
1633 * Get the size of the screen, taking position on multiple monitors into
1634 * account (if supported).
1635 */
1636 static void
1637get_work_area(RECT *spi_rect)
1638{
1639 _HMONITOR mon;
1640 _MONITORINFO moninfo;
1641
1642 /* use these functions only if available */
1643 if (pMonitorFromWindow != NULL && pGetMonitorInfo != NULL)
1644 {
1645 /* work out which monitor the window is on, and get *it's* work area */
1646 mon = pMonitorFromWindow(s_hwnd, 1 /*MONITOR_DEFAULTTOPRIMARY*/);
1647 if (mon != NULL)
1648 {
1649 moninfo.cbSize = sizeof(_MONITORINFO);
1650 if (pGetMonitorInfo(mon, &moninfo))
1651 {
1652 *spi_rect = moninfo.rcWork;
1653 return;
1654 }
1655 }
1656 }
1657 /* this is the old method... */
1658 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
1659}
1660
1661/*
1662 * Set the size of the window to the given width and height in pixels.
1663 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001664/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 void
1666gui_mch_set_shellsize(int width, int height,
Bram Moolenaarafa24992006-03-27 20:58:26 +00001667 int min_width, int min_height, int base_width, int base_height,
1668 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669{
1670 RECT workarea_rect;
1671 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 WINDOWPLACEMENT wndpl;
1673
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001674 /* Try to keep window completely on screen. */
1675 /* Get position of the screen work area. This is the part that is not
1676 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 get_work_area(&workarea_rect);
1678
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001679 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001680 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 wndpl.length = sizeof(WINDOWPLACEMENT);
1682 GetWindowPlacement(s_hwnd, &wndpl);
1683
1684 /* Resizing a maximized window looks very strange, unzoom it first.
1685 * But don't do it when still starting up, it may have been requested in
1686 * the shortcut. */
1687 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
1688 {
1689 ShowWindow(s_hwnd, SW_SHOWNORMAL);
1690 /* Need to get the settings of the normal window. */
1691 GetWindowPlacement(s_hwnd, &wndpl);
1692 }
1693
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 /* compute the size of the outside of the window */
1695 win_width = width + GetSystemMetrics(SM_CXFRAME) * 2;
1696 win_height = height + GetSystemMetrics(SM_CYFRAME) * 2
1697 + GetSystemMetrics(SM_CYCAPTION)
1698#ifdef FEAT_MENU
1699 + gui_mswin_get_menu_height(FALSE)
1700#endif
1701 ;
1702
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001703 /* The following should take care of keeping Vim on the same monitor, no
1704 * matter if the secondary monitor is left or right of the primary
1705 * monitor. */
1706 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
1707 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00001708
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001709 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001710 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001711 && wndpl.rcNormalPosition.right > workarea_rect.right)
1712 OffsetRect(&wndpl.rcNormalPosition,
1713 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001715 if ((direction & RESIZE_HOR)
1716 && wndpl.rcNormalPosition.left < workarea_rect.left)
1717 OffsetRect(&wndpl.rcNormalPosition,
1718 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719
Bram Moolenaarafa24992006-03-27 20:58:26 +00001720 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001721 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
1722 OffsetRect(&wndpl.rcNormalPosition,
1723 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01001725 if ((direction & RESIZE_VERT)
1726 && wndpl.rcNormalPosition.top < workarea_rect.top)
1727 OffsetRect(&wndpl.rcNormalPosition,
1728 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729
1730 /* set window position - we should use SetWindowPlacement rather than
1731 * SetWindowPos as the MSDN docs say the coord systems returned by
1732 * these two are not compatible. */
1733 SetWindowPlacement(s_hwnd, &wndpl);
1734
1735 SetActiveWindow(s_hwnd);
1736 SetFocus(s_hwnd);
1737
1738#ifdef FEAT_MENU
1739 /* Menu may wrap differently now */
1740 gui_mswin_get_menu_height(!gui.starting);
1741#endif
1742}
1743
1744
1745 void
1746gui_mch_set_scrollbar_thumb(
1747 scrollbar_T *sb,
1748 long val,
1749 long size,
1750 long max)
1751{
1752 SCROLLINFO info;
1753
1754 sb->scroll_shift = 0;
1755 while (max > 32767)
1756 {
1757 max = (max + 1) >> 1;
1758 val >>= 1;
1759 size >>= 1;
1760 ++sb->scroll_shift;
1761 }
1762
1763 if (sb->scroll_shift > 0)
1764 ++size;
1765
1766 info.cbSize = sizeof(info);
1767 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
1768 info.nPos = val;
1769 info.nMin = 0;
1770 info.nMax = max;
1771 info.nPage = size;
1772 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
1773}
1774
1775
1776/*
1777 * Set the current text font.
1778 */
1779 void
1780gui_mch_set_font(GuiFont font)
1781{
1782 gui.currFont = font;
1783}
1784
1785
1786/*
1787 * Set the current text foreground color.
1788 */
1789 void
1790gui_mch_set_fg_color(guicolor_T color)
1791{
1792 gui.currFgColor = color;
1793}
1794
1795/*
1796 * Set the current text background color.
1797 */
1798 void
1799gui_mch_set_bg_color(guicolor_T color)
1800{
1801 gui.currBgColor = color;
1802}
1803
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001804/*
1805 * Set the current text special color.
1806 */
1807 void
1808gui_mch_set_sp_color(guicolor_T color)
1809{
1810 gui.currSpColor = color;
1811}
1812
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813#if defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)
1814/*
1815 * Multi-byte handling, originally by Sung-Hoon Baek.
1816 * First static functions (no prototypes generated).
1817 */
1818#ifdef _MSC_VER
1819# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
1820#endif
1821#include <imm.h>
1822
1823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 * handle WM_IME_NOTIFY message
1825 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001826/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 static LRESULT
1828_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData)
1829{
1830 LRESULT lResult = 0;
1831 HIMC hImc;
1832
1833 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
1834 return lResult;
1835 switch (dwCommand)
1836 {
1837 case IMN_SETOPENSTATUS:
1838 if (pImmGetOpenStatus(hImc))
1839 {
1840 pImmSetCompositionFont(hImc, &norm_logfont);
1841 im_set_position(gui.row, gui.col);
1842
1843 /* Disable langmap */
1844 State &= ~LANGMAP;
1845 if (State & INSERT)
1846 {
1847#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1848 /* Unshown 'keymap' in status lines */
1849 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
1850 {
1851 /* Save cursor position */
1852 int old_row = gui.row;
1853 int old_col = gui.col;
1854
1855 // This must be called here before
1856 // status_redraw_curbuf(), otherwise the mode
1857 // message may appear in the wrong position.
1858 showmode();
1859 status_redraw_curbuf();
1860 update_screen(0);
1861 /* Restore cursor position */
1862 gui.row = old_row;
1863 gui.col = old_col;
1864 }
1865#endif
1866 }
1867 }
1868 gui_update_cursor(TRUE, FALSE);
1869 lResult = 0;
1870 break;
1871 }
1872 pImmReleaseContext(hWnd, hImc);
1873 return lResult;
1874}
1875
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001876/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 static LRESULT
1878_OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param)
1879{
1880 char_u *ret;
1881 int len;
1882
1883 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
1884 return 0;
1885
1886 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
1887 if (ret != NULL)
1888 {
1889 add_to_input_buf_csi(ret, len);
1890 vim_free(ret);
1891 return 1;
1892 }
1893 return 0;
1894}
1895
1896/*
1897 * get the current composition string, in UCS-2; *lenp is the number of
1898 * *lenp is the number of Unicode characters.
1899 */
1900 static short_u *
1901GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
1902{
1903 LONG ret;
1904 LPWSTR wbuf = NULL;
1905 char_u *buf;
1906
1907 if (!pImmGetContext)
1908 return NULL; /* no imm32.dll */
1909
1910 /* Try Unicode; this'll always work on NT regardless of codepage. */
1911 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
1912 if (ret == 0)
1913 return NULL; /* empty */
1914
1915 if (ret > 0)
1916 {
1917 /* Allocate the requested buffer plus space for the NUL character. */
1918 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
1919 if (wbuf != NULL)
1920 {
1921 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
1922 *lenp = ret / sizeof(WCHAR);
1923 }
1924 return (short_u *)wbuf;
1925 }
1926
1927 /* ret < 0; we got an error, so try the ANSI version. This'll work
1928 * on 9x/ME, but only if the codepage happens to be set to whatever
1929 * we're inputting. */
1930 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
1931 if (ret <= 0)
1932 return NULL; /* empty or error */
1933
1934 buf = alloc(ret);
1935 if (buf == NULL)
1936 return NULL;
1937 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
1938
1939 /* convert from codepage to UCS-2 */
1940 MultiByteToWideChar_alloc(GetACP(), 0, buf, ret, &wbuf, lenp);
1941 vim_free(buf);
1942
1943 return (short_u *)wbuf;
1944}
1945
1946/*
1947 * void GetResultStr()
1948 *
1949 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
1950 * get complete composition string
1951 */
1952 static char_u *
1953GetResultStr(HWND hwnd, int GCS, int *lenp)
1954{
1955 HIMC hIMC; /* Input context handle. */
1956 short_u *buf = NULL;
1957 char_u *convbuf = NULL;
1958
1959 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
1960 return NULL;
1961
1962 /* Reads in the composition string. */
1963 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
1964 if (buf == NULL)
1965 return NULL;
1966
Bram Moolenaar36f692d2008-11-20 16:10:17 +00001967 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 pImmReleaseContext(hwnd, hIMC);
1969 vim_free(buf);
1970 return convbuf;
1971}
1972#endif
1973
1974/* For global functions we need prototypes. */
1975#if (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) || defined(PROTO)
1976
1977/*
1978 * set font to IM.
1979 */
1980 void
1981im_set_font(LOGFONT *lf)
1982{
1983 HIMC hImc;
1984
1985 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
1986 {
1987 pImmSetCompositionFont(hImc, lf);
1988 pImmReleaseContext(s_hwnd, hImc);
1989 }
1990}
1991
1992/*
1993 * Notify cursor position to IM.
1994 */
1995 void
1996im_set_position(int row, int col)
1997{
1998 HIMC hImc;
1999
2000 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
2001 {
2002 COMPOSITIONFORM cfs;
2003
2004 cfs.dwStyle = CFS_POINT;
2005 cfs.ptCurrentPos.x = FILL_X(col);
2006 cfs.ptCurrentPos.y = FILL_Y(row);
2007 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
2008 pImmSetCompositionWindow(hImc, &cfs);
2009
2010 pImmReleaseContext(s_hwnd, hImc);
2011 }
2012}
2013
2014/*
2015 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
2016 */
2017 void
2018im_set_active(int active)
2019{
2020 HIMC hImc;
2021 static HIMC hImcOld = (HIMC)0;
2022
2023 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
2024 {
2025 if (p_imdisable)
2026 {
2027 if (hImcOld == (HIMC)0)
2028 {
2029 hImcOld = pImmGetContext(s_hwnd);
2030 if (hImcOld)
2031 pImmAssociateContext(s_hwnd, (HIMC)0);
2032 }
2033 active = FALSE;
2034 }
2035 else if (hImcOld != (HIMC)0)
2036 {
2037 pImmAssociateContext(s_hwnd, hImcOld);
2038 hImcOld = (HIMC)0;
2039 }
2040
2041 hImc = pImmGetContext(s_hwnd);
2042 if (hImc)
2043 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002044 /*
2045 * for Korean ime
2046 */
2047 HKL hKL = GetKeyboardLayout(0);
2048
2049 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
2050 {
2051 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
2052 static BOOL bSaved = FALSE;
2053
2054 if (active)
2055 {
2056 /* if we have a saved conversion status, restore it */
2057 if (bSaved)
2058 pImmSetConversionStatus(hImc, dwConversionSaved,
2059 dwSentenceSaved);
2060 bSaved = FALSE;
2061 }
2062 else
2063 {
2064 /* save conversion status and disable korean */
2065 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
2066 &dwSentenceSaved))
2067 {
2068 bSaved = TRUE;
2069 pImmSetConversionStatus(hImc,
2070 dwConversionSaved & ~(IME_CMODE_NATIVE
2071 | IME_CMODE_FULLSHAPE),
2072 dwSentenceSaved);
2073 }
2074 }
2075 }
2076
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 pImmSetOpenStatus(hImc, active);
2078 pImmReleaseContext(s_hwnd, hImc);
2079 }
2080 }
2081}
2082
2083/*
2084 * Get IM status. When IM is on, return not 0. Else return 0.
2085 */
2086 int
2087im_get_status()
2088{
2089 int status = 0;
2090 HIMC hImc;
2091
2092 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
2093 {
2094 status = pImmGetOpenStatus(hImc) ? 1 : 0;
2095 pImmReleaseContext(s_hwnd, hImc);
2096 }
2097 return status;
2098}
2099
2100#endif /* FEAT_MBYTE && FEAT_MBYTE_IME */
2101
2102#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
2103/* Win32 with GLOBAL IME */
2104
2105/*
2106 * Notify cursor position to IM.
2107 */
2108 void
2109im_set_position(int row, int col)
2110{
2111 /* Win32 with GLOBAL IME */
2112 POINT p;
2113
2114 p.x = FILL_X(col);
2115 p.y = FILL_Y(row);
2116 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
2117 global_ime_set_position(&p);
2118}
2119
2120/*
2121 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
2122 */
2123 void
2124im_set_active(int active)
2125{
2126 global_ime_set_status(active);
2127}
2128
2129/*
2130 * Get IM status. When IM is on, return not 0. Else return 0.
2131 */
2132 int
2133im_get_status()
2134{
2135 return global_ime_get_status();
2136}
2137#endif
2138
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002139#ifdef FEAT_MBYTE
2140/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00002141 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002142 */
2143 static void
2144latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
2145{
2146 int c;
2147
Bram Moolenaarca003e12006-03-17 23:19:38 +00002148 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002149 {
2150 c = *text++;
2151 switch (c)
2152 {
2153 case 0xa4: c = 0x20ac; break; /* euro */
2154 case 0xa6: c = 0x0160; break; /* S hat */
2155 case 0xa8: c = 0x0161; break; /* S -hat */
2156 case 0xb4: c = 0x017d; break; /* Z hat */
2157 case 0xb8: c = 0x017e; break; /* Z -hat */
2158 case 0xbc: c = 0x0152; break; /* OE */
2159 case 0xbd: c = 0x0153; break; /* oe */
2160 case 0xbe: c = 0x0178; break; /* Y */
2161 }
2162 *unicodebuf++ = c;
2163 }
2164}
2165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166
2167#ifdef FEAT_RIGHTLEFT
2168/*
2169 * What is this for? In the case where you are using Win98 or Win2K or later,
2170 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
2171 * reverses the string sent to the TextOut... family. This sucks, because we
2172 * go to a lot of effort to do the right thing, and there doesn't seem to be a
2173 * way to tell Windblows not to do this!
2174 *
2175 * The short of it is that this 'RevOut' only gets called if you are running
2176 * one of the new, "improved" MS OSes, and only if you are running in
2177 * 'rightleft' mode. It makes display take *slightly* longer, but not
2178 * noticeably so.
2179 */
2180 static void
2181RevOut( HDC s_hdc,
2182 int col,
2183 int row,
2184 UINT foptions,
2185 CONST RECT *pcliprect,
2186 LPCTSTR text,
2187 UINT len,
2188 CONST INT *padding)
2189{
2190 int ix;
2191 static int special = -1;
2192
2193 if (special == -1)
2194 {
2195 /* Check windows version: special treatment is needed if it is NT 5 or
2196 * Win98 or higher. */
2197 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
2198 && os_version.dwMajorVersion >= 5)
2199 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
2200 && (os_version.dwMajorVersion > 4
2201 || (os_version.dwMajorVersion == 4
2202 && os_version.dwMinorVersion > 0))))
2203 special = 1;
2204 else
2205 special = 0;
2206 }
2207
2208 if (special)
2209 for (ix = 0; ix < (int)len; ++ix)
2210 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
2211 pcliprect, text + ix, 1, padding);
2212 else
2213 ExtTextOut(s_hdc, col, row, foptions, pcliprect, text, len, padding);
2214}
2215#endif
2216
2217 void
2218gui_mch_draw_string(
2219 int row,
2220 int col,
2221 char_u *text,
2222 int len,
2223 int flags)
2224{
2225 static int *padding = NULL;
2226 static int pad_size = 0;
2227 int i;
2228 const RECT *pcliprect = NULL;
2229 UINT foptions = 0;
2230#ifdef FEAT_MBYTE
2231 static WCHAR *unicodebuf = NULL;
2232 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002233 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 int n = 0;
2235#endif
2236 HPEN hpen, old_pen;
2237 int y;
2238
2239#ifndef MSWIN16_FASTTEXT
2240 /*
2241 * Italic and bold text seems to have an extra row of pixels at the bottom
2242 * (below where the bottom of the character should be). If we draw the
2243 * characters with a solid background, the top row of pixels in the
2244 * character below will be overwritten. We can fix this by filling in the
2245 * background ourselves, to the correct character proportions, and then
2246 * writing the character in transparent mode. Still have a problem when
2247 * the character is "_", which gets written on to the character below.
2248 * New fix: set gui.char_ascent to -1. This shifts all characters up one
2249 * pixel in their slots, which fixes the problem with the bottom row of
2250 * pixels. We still need this code because otherwise the top row of pixels
2251 * becomes a problem. - webb.
2252 */
2253 static HBRUSH hbr_cache[2] = {NULL, NULL};
2254 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
2255 static int brush_lru = 0;
2256 HBRUSH hbr;
2257 RECT rc;
2258
2259 if (!(flags & DRAW_TRANSP))
2260 {
2261 /*
2262 * Clear background first.
2263 * Note: FillRect() excludes right and bottom of rectangle.
2264 */
2265 rc.left = FILL_X(col);
2266 rc.top = FILL_Y(row);
2267#ifdef FEAT_MBYTE
2268 if (has_mbyte)
2269 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002271 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 }
2273 else
2274#endif
2275 rc.right = FILL_X(col + len);
2276 rc.bottom = FILL_Y(row + 1);
2277
2278 /* Cache the created brush, that saves a lot of time. We need two:
2279 * one for cursor background and one for the normal background. */
2280 if (gui.currBgColor == brush_color[0])
2281 {
2282 hbr = hbr_cache[0];
2283 brush_lru = 1;
2284 }
2285 else if (gui.currBgColor == brush_color[1])
2286 {
2287 hbr = hbr_cache[1];
2288 brush_lru = 0;
2289 }
2290 else
2291 {
2292 if (hbr_cache[brush_lru] != NULL)
2293 DeleteBrush(hbr_cache[brush_lru]);
2294 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
2295 brush_color[brush_lru] = gui.currBgColor;
2296 hbr = hbr_cache[brush_lru];
2297 brush_lru = !brush_lru;
2298 }
2299 FillRect(s_hdc, &rc, hbr);
2300
2301 SetBkMode(s_hdc, TRANSPARENT);
2302
2303 /*
2304 * When drawing block cursor, prevent inverted character spilling
2305 * over character cell (can happen with bold/italic)
2306 */
2307 if (flags & DRAW_CURSOR)
2308 {
2309 pcliprect = &rc;
2310 foptions = ETO_CLIPPED;
2311 }
2312 }
2313#else
2314 /*
2315 * The alternative would be to write the characters in opaque mode, but
2316 * when the text is not exactly the same proportions as normal text, too
2317 * big or too little a rectangle gets drawn for the background.
2318 */
2319 SetBkMode(s_hdc, OPAQUE);
2320 SetBkColor(s_hdc, gui.currBgColor);
2321#endif
2322 SetTextColor(s_hdc, gui.currFgColor);
2323 SelectFont(s_hdc, gui.currFont);
2324
2325 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
2326 {
2327 vim_free(padding);
2328 pad_size = Columns;
2329
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002330 /* Don't give an out-of-memory message here, it would call us
2331 * recursively. */
2332 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 if (padding != NULL)
2334 for (i = 0; i < pad_size; i++)
2335 padding[i] = gui.char_width;
2336 }
2337
2338 /* On NT, tell the font renderer not to "help" us with Hebrew and Arabic
2339 * text. This doesn't work in 9x, so we have to deal with it manually on
2340 * those systems. */
2341 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
2342 foptions |= ETO_IGNORELANGUAGE;
2343
2344 /*
2345 * We have to provide the padding argument because italic and bold versions
2346 * of fixed-width fonts are often one pixel or so wider than their normal
2347 * versions.
2348 * No check for DRAW_BOLD, Windows will have done it already.
2349 */
2350
2351#ifdef FEAT_MBYTE
2352 /* Check if there are any UTF-8 characters. If not, use normal text
2353 * output to speed up output. */
2354 if (enc_utf8)
2355 for (n = 0; n < len; ++n)
2356 if (text[n] >= 0x80)
2357 break;
2358
2359 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002360 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002362 if ((enc_utf8
2363 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
2364 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 && (unicodebuf == NULL || len > unibuflen))
2366 {
2367 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002368 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
2370 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002371 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
2373 unibuflen = len;
2374 }
2375
2376 if (enc_utf8 && n < len && unicodebuf != NULL)
2377 {
2378 /* Output UTF-8 characters. Caller has already separated
2379 * composing characters. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002380 int i;
2381 int wlen; /* string length in words */
2382 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 int cells; /* cell width of string up to composing char */
2384 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002385 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002387 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002388 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002390 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002392 c = utf_ptr2char(text + i);
2393 if (c >= 0x10000)
2394 {
2395 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002396 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
2397 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002398 }
2399 else
2400 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002401 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002402 }
2403 cw = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 if (cw > 2) /* don't use 4 for unprintable char */
2405 cw = 1;
2406 if (unicodepdy != NULL)
2407 {
2408 /* Use unicodepdy to make characters fit as we expect, even
2409 * when the font uses different widths (e.g., bold character
2410 * is wider). */
2411 unicodepdy[clen] = cw * gui.char_width;
2412 }
2413 cells += cw;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002414 i += utfc_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00002415 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 }
2417 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaarca003e12006-03-17 23:19:38 +00002418 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 len = cells; /* used for underlining */
2420 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002421 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 {
2423 /* If we want to display codepage data, and the current CP is not the
2424 * ANSI one, we need to go via Unicode. */
2425 if (unicodebuf != NULL)
2426 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002427 if (enc_latin9)
2428 latin9_to_ucs(text, len, unicodebuf);
2429 else
2430 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 MB_PRECOMPOSED,
2432 (char *)text, len,
2433 (LPWSTR)unicodebuf, unibuflen);
2434 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002435 {
2436 /* Use unicodepdy to make characters fit as we expect, even
2437 * when the font uses different widths (e.g., bold character
2438 * is wider). */
2439 if (unicodepdy != NULL)
2440 {
2441 int i;
2442 int cw;
2443
2444 for (i = 0; i < len; ++i)
2445 {
2446 cw = utf_char2cells(unicodebuf[i]);
2447 if (cw > 2)
2448 cw = 1;
2449 unicodepdy[i] = cw * gui.char_width;
2450 }
2451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002453 foptions, pcliprect, unicodebuf, len, unicodepdy);
2454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 }
2456 }
2457 else
2458#endif
2459 {
2460#ifdef FEAT_RIGHTLEFT
2461 /* If we can't use ETO_IGNORELANGUAGE, we can't tell Windows not to
2462 * mess up RL text, so we have to draw it character-by-character.
2463 * Only do this if RL is on, since it's slow. */
2464 if (curwin->w_p_rl && !(foptions & ETO_IGNORELANGUAGE))
2465 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
2466 foptions, pcliprect, (char *)text, len, padding);
2467 else
2468#endif
2469 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
2470 foptions, pcliprect, (char *)text, len, padding);
2471 }
2472
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002473 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 if (flags & DRAW_UNDERL)
2475 {
2476 hpen = CreatePen(PS_SOLID, 1, gui.currFgColor);
2477 old_pen = SelectObject(s_hdc, hpen);
2478 /* When p_linespace is 0, overwrite the bottom row of pixels.
2479 * Otherwise put the line just below the character. */
2480 y = FILL_Y(row + 1) - 1;
2481#ifndef MSWIN16_FASTTEXT
2482 if (p_linespace > 1)
2483 y -= p_linespace - 1;
2484#endif
2485 MoveToEx(s_hdc, FILL_X(col), y, NULL);
2486 /* Note: LineTo() excludes the last pixel in the line. */
2487 LineTo(s_hdc, FILL_X(col + len), y);
2488 DeleteObject(SelectObject(s_hdc, old_pen));
2489 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002490
2491 /* Undercurl */
2492 if (flags & DRAW_UNDERC)
2493 {
2494 int x;
2495 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002496 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002497
2498 y = FILL_Y(row + 1) - 1;
2499 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
2500 {
2501 offset = val[x % 8];
2502 SetPixel(s_hdc, x, y - offset, gui.currSpColor);
2503 }
2504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505}
2506
2507
2508/*
2509 * Output routines.
2510 */
2511
2512/* Flush any output to the screen */
2513 void
2514gui_mch_flush(void)
2515{
2516# if defined(__BORLANDC__)
2517 /*
2518 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
2519 * prototype declaration.
2520 * The compiler complains if __stdcall is not used in both declarations.
2521 */
2522 BOOL __stdcall GdiFlush(void);
2523# endif
2524
2525 GdiFlush();
2526}
2527
2528 static void
2529clear_rect(RECT *rcp)
2530{
2531 HBRUSH hbr;
2532
2533 hbr = CreateSolidBrush(gui.back_pixel);
2534 FillRect(s_hdc, rcp, hbr);
2535 DeleteBrush(hbr);
2536}
2537
2538
Bram Moolenaarc716c302006-01-21 22:12:51 +00002539 void
2540gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
2541{
2542 RECT workarea_rect;
2543
2544 get_work_area(&workarea_rect);
2545
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002546 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002547 - GetSystemMetrics(SM_CXFRAME) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00002548
2549 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
2550 * the menubar for MSwin, we subtract it from the screen height, so that
2551 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002552 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002553 - GetSystemMetrics(SM_CYFRAME) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00002554 - GetSystemMetrics(SM_CYCAPTION)
2555#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002556 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00002557#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002558 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00002559}
2560
2561
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562#if defined(FEAT_MENU) || defined(PROTO)
2563/*
2564 * Add a sub menu to the menu bar.
2565 */
2566 void
2567gui_mch_add_menu(
2568 vimmenu_T *menu,
2569 int pos)
2570{
2571 vimmenu_T *parent = menu->parent;
2572
2573 menu->submenu_id = CreatePopupMenu();
2574 menu->id = s_menu_id++;
2575
2576 if (menu_is_menubar(menu->name))
2577 {
2578 if (is_winnt_3())
2579 {
2580 InsertMenu((parent == NULL) ? s_menuBar : parent->submenu_id,
2581 (UINT)pos, MF_POPUP | MF_STRING | MF_BYPOSITION,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002582 (long_u)menu->submenu_id, (LPCTSTR) menu->name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 }
2584 else
2585 {
2586#ifdef FEAT_MBYTE
2587 WCHAR *wn = NULL;
2588 int n;
2589
2590 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2591 {
2592 /* 'encoding' differs from active codepage: convert menu name
2593 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002594 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 if (wn != NULL)
2596 {
2597 MENUITEMINFOW infow;
2598
2599 infow.cbSize = sizeof(infow);
2600 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
2601 | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002602 infow.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 infow.wID = menu->id;
2604 infow.fType = MFT_STRING;
2605 infow.dwTypeData = wn;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002606 infow.cch = (UINT)wcslen(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 infow.hSubMenu = menu->submenu_id;
2608 n = InsertMenuItemW((parent == NULL)
2609 ? s_menuBar : parent->submenu_id,
2610 (UINT)pos, TRUE, &infow);
2611 vim_free(wn);
2612 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2613 /* Failed, try using non-wide function. */
2614 wn = NULL;
2615 }
2616 }
2617
2618 if (wn == NULL)
2619#endif
2620 {
2621 MENUITEMINFO info;
2622
2623 info.cbSize = sizeof(info);
2624 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002625 info.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 info.wID = menu->id;
2627 info.fType = MFT_STRING;
2628 info.dwTypeData = (LPTSTR)menu->name;
2629 info.cch = (UINT)STRLEN(menu->name);
2630 info.hSubMenu = menu->submenu_id;
2631 InsertMenuItem((parent == NULL)
2632 ? s_menuBar : parent->submenu_id,
2633 (UINT)pos, TRUE, &info);
2634 }
2635 }
2636 }
2637
2638 /* Fix window size if menu may have wrapped */
2639 if (parent == NULL)
2640 gui_mswin_get_menu_height(!gui.starting);
2641#ifdef FEAT_TEAROFF
2642 else if (IsWindow(parent->tearoff_handle))
2643 rebuild_tearoff(parent);
2644#endif
2645}
2646
2647 void
2648gui_mch_show_popupmenu(vimmenu_T *menu)
2649{
2650 POINT mp;
2651
2652 (void)GetCursorPos((LPPOINT)&mp);
2653 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
2654}
2655
2656 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00002657gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658{
2659 vimmenu_T *menu = gui_find_menu(path_name);
2660
2661 if (menu != NULL)
2662 {
2663 POINT p;
2664
2665 /* Find the position of the current cursor */
2666 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00002667 if (mouse_pos)
2668 {
2669 int mx, my;
2670
2671 gui_mch_getmouse(&mx, &my);
2672 p.x += mx;
2673 p.y += my;
2674 }
2675 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 {
2677 p.x += TEXT_X(W_WINCOL(curwin) + curwin->w_wcol + 1);
2678 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
2679 }
2680 msg_scroll = FALSE;
2681 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
2682 }
2683}
2684
2685#if defined(FEAT_TEAROFF) || defined(PROTO)
2686/*
2687 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
2688 * create it as a pseudo-"tearoff menu".
2689 */
2690 void
2691gui_make_tearoff(char_u *path_name)
2692{
2693 vimmenu_T *menu = gui_find_menu(path_name);
2694
2695 /* Found the menu, so tear it off. */
2696 if (menu != NULL)
2697 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
2698}
2699#endif
2700
2701/*
2702 * Add a menu item to a menu
2703 */
2704 void
2705gui_mch_add_menu_item(
2706 vimmenu_T *menu,
2707 int idx)
2708{
2709 vimmenu_T *parent = menu->parent;
2710
2711 menu->id = s_menu_id++;
2712 menu->submenu_id = NULL;
2713
2714#ifdef FEAT_TEAROFF
2715 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
2716 {
2717 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
2718 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
2719 }
2720 else
2721#endif
2722#ifdef FEAT_TOOLBAR
2723 if (menu_is_toolbar(parent->name))
2724 {
2725 TBBUTTON newtb;
2726
2727 vim_memset(&newtb, 0, sizeof(newtb));
2728 if (menu_is_separator(menu->name))
2729 {
2730 newtb.iBitmap = 0;
2731 newtb.fsStyle = TBSTYLE_SEP;
2732 }
2733 else
2734 {
2735 newtb.iBitmap = get_toolbar_bitmap(menu);
2736 newtb.fsStyle = TBSTYLE_BUTTON;
2737 }
2738 newtb.idCommand = menu->id;
2739 newtb.fsState = TBSTATE_ENABLED;
2740 newtb.iString = 0;
2741 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
2742 (LPARAM)&newtb);
2743 menu->submenu_id = (HMENU)-1;
2744 }
2745 else
2746#endif
2747 {
2748#ifdef FEAT_MBYTE
2749 WCHAR *wn = NULL;
2750 int n;
2751
2752 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2753 {
2754 /* 'encoding' differs from active codepage: convert menu item name
2755 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002756 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 if (wn != NULL)
2758 {
2759 n = InsertMenuW(parent->submenu_id, (UINT)idx,
2760 (menu_is_separator(menu->name)
2761 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
2762 (UINT)menu->id, wn);
2763 vim_free(wn);
2764 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2765 /* Failed, try using non-wide function. */
2766 wn = NULL;
2767 }
2768 }
2769 if (wn == NULL)
2770#endif
2771 InsertMenu(parent->submenu_id, (UINT)idx,
2772 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
2773 | MF_BYPOSITION,
2774 (UINT)menu->id, (LPCTSTR)menu->name);
2775#ifdef FEAT_TEAROFF
2776 if (IsWindow(parent->tearoff_handle))
2777 rebuild_tearoff(parent);
2778#endif
2779 }
2780}
2781
2782/*
2783 * Destroy the machine specific menu widget.
2784 */
2785 void
2786gui_mch_destroy_menu(vimmenu_T *menu)
2787{
2788#ifdef FEAT_TOOLBAR
2789 /*
2790 * is this a toolbar button?
2791 */
2792 if (menu->submenu_id == (HMENU)-1)
2793 {
2794 int iButton;
2795
2796 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
2797 (WPARAM)menu->id, 0);
2798 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
2799 }
2800 else
2801#endif
2802 {
2803 if (menu->parent != NULL
2804 && menu_is_popup(menu->parent->dname)
2805 && menu->parent->submenu_id != NULL)
2806 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
2807 else
2808 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
2809 if (menu->submenu_id != NULL)
2810 DestroyMenu(menu->submenu_id);
2811#ifdef FEAT_TEAROFF
2812 if (IsWindow(menu->tearoff_handle))
2813 DestroyWindow(menu->tearoff_handle);
2814 if (menu->parent != NULL
2815 && menu->parent->children != NULL
2816 && IsWindow(menu->parent->tearoff_handle))
2817 {
2818 /* This menu must not show up when rebuilding the tearoff window. */
2819 menu->modes = 0;
2820 rebuild_tearoff(menu->parent);
2821 }
2822#endif
2823 }
2824}
2825
2826#ifdef FEAT_TEAROFF
2827 static void
2828rebuild_tearoff(vimmenu_T *menu)
2829{
2830 /*hackish*/
2831 char_u tbuf[128];
2832 RECT trect;
2833 RECT rct;
2834 RECT roct;
2835 int x, y;
2836
2837 HWND thwnd = menu->tearoff_handle;
2838
2839 GetWindowText(thwnd, tbuf, 127);
2840 if (GetWindowRect(thwnd, &trect)
2841 && GetWindowRect(s_hwnd, &rct)
2842 && GetClientRect(s_hwnd, &roct))
2843 {
2844 x = trect.left - rct.left;
2845 y = (trect.top - rct.bottom + roct.bottom);
2846 }
2847 else
2848 {
2849 x = y = 0xffffL;
2850 }
2851 DestroyWindow(thwnd);
2852 if (menu->children != NULL)
2853 {
2854 gui_mch_tearoff(tbuf, menu, x, y);
2855 if (IsWindow(menu->tearoff_handle))
2856 (void) SetWindowPos(menu->tearoff_handle,
2857 NULL,
2858 (int)trect.left,
2859 (int)trect.top,
2860 0, 0,
2861 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
2862 }
2863}
2864#endif /* FEAT_TEAROFF */
2865
2866/*
2867 * Make a menu either grey or not grey.
2868 */
2869 void
2870gui_mch_menu_grey(
2871 vimmenu_T *menu,
2872 int grey)
2873{
2874#ifdef FEAT_TOOLBAR
2875 /*
2876 * is this a toolbar button?
2877 */
2878 if (menu->submenu_id == (HMENU)-1)
2879 {
2880 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
2881 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
2882 }
2883 else
2884#endif
2885 if (grey)
2886 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_GRAYED);
2887 else
2888 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2889
2890#ifdef FEAT_TEAROFF
2891 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
2892 {
2893 WORD menuID;
2894 HWND menuHandle;
2895
2896 /*
2897 * A tearoff button has changed state.
2898 */
2899 if (menu->children == NULL)
2900 menuID = (WORD)(menu->id);
2901 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002902 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
2904 if (menuHandle)
2905 EnableWindow(menuHandle, !grey);
2906
2907 }
2908#endif
2909}
2910
2911#endif /* FEAT_MENU */
2912
2913
2914/* define some macros used to make the dialogue creation more readable */
2915
2916#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
2917#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00002918#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919
2920#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2921/*
2922 * stuff for dialogs
2923 */
2924
2925/*
2926 * The callback routine used by all the dialogs. Very simple. First,
2927 * acknowledges the INITDIALOG message so that Windows knows to do standard
2928 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
2929 * pressed, return that button's ID - IDCANCEL (2), which is the button's
2930 * number.
2931 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002932/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 static LRESULT CALLBACK
2934dialog_callback(
2935 HWND hwnd,
2936 UINT message,
2937 WPARAM wParam,
2938 LPARAM lParam)
2939{
2940 if (message == WM_INITDIALOG)
2941 {
2942 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
2943 /* Set focus to the dialog. Set the default button, if specified. */
2944 (void)SetFocus(hwnd);
2945 if (dialog_default_button > IDCANCEL)
2946 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00002947 else
2948 /* We don't have a default, set focus on another element of the
2949 * dialog window, probably the icon */
2950 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 return FALSE;
2952 }
2953
2954 if (message == WM_COMMAND)
2955 {
2956 int button = LOWORD(wParam);
2957
2958 /* Don't end the dialog if something was selected that was
2959 * not a button.
2960 */
2961 if (button >= DLG_NONBUTTON_CONTROL)
2962 return TRUE;
2963
2964 /* If the edit box exists, copy the string. */
2965 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00002966 {
2967# if defined(FEAT_MBYTE) && defined(WIN3264)
2968 /* If the OS is Windows NT, and 'encoding' differs from active
2969 * codepage: use wide function and convert text. */
2970 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
2971 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002972 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00002973 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
2974 char_u *p;
2975
2976 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
2977 p = utf16_to_enc(wp, NULL);
2978 vim_strncpy(s_textfield, p, IOSIZE);
2979 vim_free(p);
2980 vim_free(wp);
2981 }
2982 else
2983# endif
2984 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00002986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987
2988 /*
2989 * Need to check for IDOK because if the user just hits Return to
2990 * accept the default value, some reason this is what we get.
2991 */
2992 if (button == IDOK)
2993 {
2994 if (dialog_default_button > IDCANCEL)
2995 EndDialog(hwnd, dialog_default_button);
2996 }
2997 else
2998 EndDialog(hwnd, button - IDCANCEL);
2999 return TRUE;
3000 }
3001
3002 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
3003 {
3004 EndDialog(hwnd, 0);
3005 return TRUE;
3006 }
3007 return FALSE;
3008}
3009
3010/*
3011 * Create a dialog dynamically from the parameter strings.
3012 * type = type of dialog (question, alert, etc.)
3013 * title = dialog title. may be NULL for default title.
3014 * message = text to display. Dialog sizes to accommodate it.
3015 * buttons = '\n' separated list of button captions, default first.
3016 * dfltbutton = number of default button.
3017 *
3018 * This routine returns 1 if the first button is pressed,
3019 * 2 for the second, etc.
3020 *
3021 * 0 indicates Esc was pressed.
3022 * -1 for unexpected error
3023 *
3024 * If stubbing out this fn, return 1.
3025 */
3026
3027static const char_u *dlg_icons[] = /* must match names in resource file */
3028{
3029 "IDR_VIM",
3030 "IDR_VIM_ERROR",
3031 "IDR_VIM_ALERT",
3032 "IDR_VIM_INFO",
3033 "IDR_VIM_QUESTION"
3034};
3035
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 int
3037gui_mch_dialog(
3038 int type,
3039 char_u *title,
3040 char_u *message,
3041 char_u *buttons,
3042 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003043 char_u *textfield,
3044 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
3046 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00003047 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 int numButtons;
3049 int *buttonWidths, *buttonPositions;
3050 int buttonYpos;
3051 int nchar, i;
3052 DWORD lStyle;
3053 int dlgwidth = 0;
3054 int dlgheight;
3055 int editboxheight;
3056 int horizWidth = 0;
3057 int msgheight;
3058 char_u *pstart;
3059 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003060 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 char_u *tbuffer;
3062 RECT rect;
3063 HWND hwnd;
3064 HDC hdc;
3065 HFONT font, oldFont;
3066 TEXTMETRIC fontInfo;
3067 int fontHeight;
3068 int textWidth, minButtonWidth, messageWidth;
3069 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003070 int maxDialogHeight;
3071 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 int vertical;
3073 int dlgPaddingX;
3074 int dlgPaddingY;
3075#ifdef USE_SYSMENU_FONT
3076 LOGFONT lfSysmenu;
3077 int use_lfSysmenu = FALSE;
3078#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003079 garray_T ga;
3080 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081
3082#ifndef NO_CONSOLE
3083 /* Don't output anything in silent mode ("ex -s") */
3084 if (silent_mode)
3085 return dfltbutton; /* return default option */
3086#endif
3087
Bram Moolenaar748bf032005-02-02 23:04:36 +00003088 if (s_hwnd == NULL)
3089 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090
3091 if ((type < 0) || (type > VIM_LAST_TYPE))
3092 type = 0;
3093
3094 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003095 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003096 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003097 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098
3099 if (p == NULL)
3100 return -1;
3101
3102 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003103 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 * vim_strsave() doesn't take a const arg (why not?), so cast away the
3105 * const.
3106 */
3107 tbuffer = vim_strsave(buttons);
3108 if (tbuffer == NULL)
3109 return -1;
3110
3111 --dfltbutton; /* Change from one-based to zero-based */
3112
3113 /* Count buttons */
3114 numButtons = 1;
3115 for (i = 0; tbuffer[i] != '\0'; i++)
3116 {
3117 if (tbuffer[i] == DLG_BUTTON_SEP)
3118 numButtons++;
3119 }
3120 if (dfltbutton >= numButtons)
3121 dfltbutton = -1;
3122
3123 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003124 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 if (buttonWidths == NULL)
3126 return -1;
3127
3128 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003129 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 if (buttonPositions == NULL)
3131 return -1;
3132
3133 /*
3134 * Calculate how big the dialog must be.
3135 */
3136 hwnd = GetDesktopWindow();
3137 hdc = GetWindowDC(hwnd);
3138#ifdef USE_SYSMENU_FONT
3139 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
3140 {
3141 font = CreateFontIndirect(&lfSysmenu);
3142 use_lfSysmenu = TRUE;
3143 }
3144 else
3145#endif
3146 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3147 VARIABLE_PITCH , DLG_FONT_NAME);
3148 if (s_usenewlook)
3149 {
3150 oldFont = SelectFont(hdc, font);
3151 dlgPaddingX = DLG_PADDING_X;
3152 dlgPaddingY = DLG_PADDING_Y;
3153 }
3154 else
3155 {
3156 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
3157 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
3158 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
3159 }
3160 GetTextMetrics(hdc, &fontInfo);
3161 fontHeight = fontInfo.tmHeight;
3162
3163 /* Minimum width for horizontal button */
3164 minButtonWidth = GetTextWidth(hdc, "Cancel", 6);
3165
3166 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003167 if (s_hwnd == NULL)
3168 {
3169 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
Bram Moolenaarc716c302006-01-21 22:12:51 +00003171 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003172 get_work_area(&workarea_rect);
3173 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
3174 if (maxDialogWidth > 600)
3175 maxDialogWidth = 600;
3176 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 100;
3177 }
3178 else
3179 {
3180 /* Use our own window for the size, unless it's very small. */
3181 GetWindowRect(s_hwnd, &rect);
3182 maxDialogWidth = rect.right - rect.left
3183 - GetSystemMetrics(SM_CXFRAME) * 2;
3184 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
3185 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003186
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003187 maxDialogHeight = rect.bottom - rect.top
3188 - GetSystemMetrics(SM_CXFRAME) * 2;
3189 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
3190 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
3191 }
3192
3193 /* Set dlgwidth to width of message.
3194 * Copy the message into "ga", changing NL to CR-NL and inserting line
3195 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 pstart = message;
3197 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003198 msgheight = 0;
3199 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 do
3201 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003202 msgheight += fontHeight; /* at least one line */
3203
3204 /* Need to figure out where to break the string. The system does it
3205 * at a word boundary, which would mean we can't compute the number of
3206 * wrapped lines. */
3207 textWidth = 0;
3208 last_white = NULL;
3209 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00003210 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003211#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003212 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003213#else
3214 l = 1;
3215#endif
3216 if (l == 1 && vim_iswhite(*pend)
3217 && textWidth > maxDialogWidth * 3 / 4)
3218 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02003219 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003220 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003221 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003222 /* Line will wrap. */
3223 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003224 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003225 textWidth = 0;
3226
3227 if (last_white != NULL)
3228 {
3229 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003230 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003231 pend = last_white + 1;
3232 last_white = NULL;
3233 }
3234 ga_append(&ga, '\r');
3235 ga_append(&ga, '\n');
3236 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003237 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003238
3239 while (--l >= 0)
3240 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003241 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003242 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003244
3245 ga_append(&ga, '\r');
3246 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 pstart = pend + 1;
3248 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003249
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003250 if (ga.ga_data != NULL)
3251 message = ga.ga_data;
3252
Bram Moolenaar748bf032005-02-02 23:04:36 +00003253 messageWidth += 10; /* roundoff space */
3254
3255 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
3256 if (msgheight > maxDialogHeight)
3257 {
3258 msgheight = maxDialogHeight;
3259 scroll_flag = WS_VSCROLL;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003260 messageWidth += GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262
3263 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003264 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265
3266 if (msgheight < DLG_ICON_HEIGHT)
3267 msgheight = DLG_ICON_HEIGHT;
3268
3269 /*
3270 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00003271 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00003273 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 if (!vertical)
3275 {
3276 // Place buttons horizontally if they fit.
3277 horizWidth = dlgPaddingX;
3278 pstart = tbuffer;
3279 i = 0;
3280 do
3281 {
3282 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
3283 if (pend == NULL)
3284 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02003285 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 if (textWidth < minButtonWidth)
3287 textWidth = minButtonWidth;
3288 textWidth += dlgPaddingX; /* Padding within button */
3289 buttonWidths[i] = textWidth;
3290 buttonPositions[i++] = horizWidth;
3291 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
3292 pstart = pend + 1;
3293 } while (*pend != NUL);
3294
3295 if (horizWidth > maxDialogWidth)
3296 vertical = TRUE; // Too wide to fit on the screen.
3297 else if (horizWidth > dlgwidth)
3298 dlgwidth = horizWidth;
3299 }
3300
3301 if (vertical)
3302 {
3303 // Stack buttons vertically.
3304 pstart = tbuffer;
3305 do
3306 {
3307 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
3308 if (pend == NULL)
3309 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02003310 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 textWidth += dlgPaddingX; /* Padding within button */
3312 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
3313 if (textWidth > dlgwidth)
3314 dlgwidth = textWidth;
3315 pstart = pend + 1;
3316 } while (*pend != NUL);
3317 }
3318
3319 if (dlgwidth < DLG_MIN_WIDTH)
3320 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
3321
3322 /* start to fill in the dlgtemplate information. addressing by WORDs */
3323 if (s_usenewlook)
3324 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
3325 else
3326 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
3327
3328 add_long(lStyle);
3329 add_long(0); // (lExtendedStyle)
3330 pnumitems = p; /*save where the number of items must be stored*/
3331 add_word(0); // NumberOfItems(will change later)
3332 add_word(10); // x
3333 add_word(10); // y
3334 add_word(PixelToDialogX(dlgwidth)); // cx
3335
3336 // Dialog height.
3337 if (vertical)
3338 dlgheight = msgheight + 2 * dlgPaddingY +
3339 DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
3340 else
3341 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
3342
3343 // Dialog needs to be taller if contains an edit box.
3344 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
3345 if (textfield != NULL)
3346 dlgheight += editboxheight;
3347
3348 add_word(PixelToDialogY(dlgheight));
3349
3350 add_word(0); // Menu
3351 add_word(0); // Class
3352
3353 /* copy the title of the dialog */
3354 nchar = nCopyAnsiToWideChar(p, (title ?
3355 (LPSTR)title :
3356 (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
3357 p += nchar;
3358
3359 if (s_usenewlook)
3360 {
3361 /* do the font, since DS_3DLOOK doesn't work properly */
3362#ifdef USE_SYSMENU_FONT
3363 if (use_lfSysmenu)
3364 {
3365 /* point size */
3366 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
3367 GetDeviceCaps(hdc, LOGPIXELSY));
3368 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
3369 }
3370 else
3371#endif
3372 {
3373 *p++ = DLG_FONT_POINT_SIZE; // point size
3374 nchar = nCopyAnsiToWideChar(p, TEXT(DLG_FONT_NAME));
3375 }
3376 p += nchar;
3377 }
3378
3379 buttonYpos = msgheight + 2 * dlgPaddingY;
3380
3381 if (textfield != NULL)
3382 buttonYpos += editboxheight;
3383
3384 pstart = tbuffer;
3385 if (!vertical)
3386 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
3387 for (i = 0; i < numButtons; i++)
3388 {
3389 /* get end of this button. */
3390 for ( pend = pstart;
3391 *pend && (*pend != DLG_BUTTON_SEP);
3392 pend++)
3393 ;
3394
3395 if (*pend)
3396 *pend = '\0';
3397
3398 /*
3399 * old NOTE:
3400 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
3401 * the focus to the first tab-able button and in so doing makes that
3402 * the default!! Grrr. Workaround: Make the default button the only
3403 * one with WS_TABSTOP style. Means user can't tab between buttons, but
3404 * he/she can use arrow keys.
3405 *
3406 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00003407 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 * dialog. Also needed for when the textfield is the default control.
3409 * It appears to work now (perhaps not on Win95?).
3410 */
3411 if (vertical)
3412 {
3413 p = add_dialog_element(p,
3414 (i == dfltbutton
3415 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
3416 PixelToDialogX(DLG_VERT_PADDING_X),
3417 PixelToDialogY(buttonYpos /* TBK */
3418 + 2 * fontHeight * i),
3419 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
3420 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
3421 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, pstart);
3422 }
3423 else
3424 {
3425 p = add_dialog_element(p,
3426 (i == dfltbutton
3427 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
3428 PixelToDialogX(horizWidth + buttonPositions[i]),
3429 PixelToDialogY(buttonYpos), /* TBK */
3430 PixelToDialogX(buttonWidths[i]),
3431 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
3432 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, pstart);
3433 }
3434 pstart = pend + 1; /*next button*/
3435 }
3436 *pnumitems += numButtons;
3437
3438 /* Vim icon */
3439 p = add_dialog_element(p, SS_ICON,
3440 PixelToDialogX(dlgPaddingX),
3441 PixelToDialogY(dlgPaddingY),
3442 PixelToDialogX(DLG_ICON_WIDTH),
3443 PixelToDialogY(DLG_ICON_HEIGHT),
3444 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
3445 dlg_icons[type]);
3446
Bram Moolenaar748bf032005-02-02 23:04:36 +00003447 /* Dialog message */
3448 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
3449 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
3450 PixelToDialogY(dlgPaddingY),
3451 (WORD)(PixelToDialogX(messageWidth) + 1),
3452 PixelToDialogY(msgheight),
3453 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454
3455 /* Edit box */
3456 if (textfield != NULL)
3457 {
3458 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
3459 PixelToDialogX(2 * dlgPaddingX),
3460 PixelToDialogY(2 * dlgPaddingY + msgheight),
3461 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
3462 PixelToDialogY(fontHeight + dlgPaddingY),
3463 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, textfield);
3464 *pnumitems += 1;
3465 }
3466
3467 *pnumitems += 2;
3468
3469 SelectFont(hdc, oldFont);
3470 DeleteObject(font);
3471 ReleaseDC(hwnd, hdc);
3472
3473 /* Let the dialog_callback() function know which button to make default
3474 * If we have an edit box, make that the default. We also need to tell
3475 * dialog_callback() if this dialog contains an edit box or not. We do
3476 * this by setting s_textfield if it does.
3477 */
3478 if (textfield != NULL)
3479 {
3480 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
3481 s_textfield = textfield;
3482 }
3483 else
3484 {
3485 dialog_default_button = IDCANCEL + 1 + dfltbutton;
3486 s_textfield = NULL;
3487 }
3488
3489 /* show the dialog box modally and get a return value */
3490 nchar = (int)DialogBoxIndirect(
3491 s_hinst,
3492 (LPDLGTEMPLATE)pdlgtemplate,
3493 s_hwnd,
3494 (DLGPROC)dialog_callback);
3495
3496 LocalFree(LocalHandle(pdlgtemplate));
3497 vim_free(tbuffer);
3498 vim_free(buttonWidths);
3499 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003500 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501
3502 /* Focus back to our window (for when MDI is used). */
3503 (void)SetFocus(s_hwnd);
3504
3505 return nchar;
3506}
3507
3508#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003509
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510/*
3511 * Put a simple element (basic class) onto a dialog template in memory.
3512 * return a pointer to where the next item should be added.
3513 *
3514 * parameters:
3515 * lStyle = additional style flags
3516 * (be careful, NT3.51 & Win32s will ignore the new ones)
3517 * x,y = x & y positions IN DIALOG UNITS
3518 * w,h = width and height IN DIALOG UNITS
3519 * Id = ID used in messages
3520 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
3521 * caption = usually text or resource name
3522 *
3523 * TODO: use the length information noted here to enable the dialog creation
3524 * routines to work out more exactly how much memory they need to alloc.
3525 */
3526 static PWORD
3527add_dialog_element(
3528 PWORD p,
3529 DWORD lStyle,
3530 WORD x,
3531 WORD y,
3532 WORD w,
3533 WORD h,
3534 WORD Id,
3535 WORD clss,
3536 const char *caption)
3537{
3538 int nchar;
3539
3540 p = lpwAlign(p); /* Align to dword boundary*/
3541 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
3542 *p++ = LOWORD(lStyle);
3543 *p++ = HIWORD(lStyle);
3544 *p++ = 0; // LOWORD (lExtendedStyle)
3545 *p++ = 0; // HIWORD (lExtendedStyle)
3546 *p++ = x;
3547 *p++ = y;
3548 *p++ = w;
3549 *p++ = h;
3550 *p++ = Id; //9 or 10 words in all
3551
3552 *p++ = (WORD)0xffff;
3553 *p++ = clss; //2 more here
3554
3555 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption); //strlen(caption)+1
3556 p += nchar;
3557
3558 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
3559
3560 return p; //total = 15+ (strlen(caption)) words
3561 // = 30 + 2(strlen(caption) bytes reqd
3562}
3563
3564
3565/*
3566 * Helper routine. Take an input pointer, return closest pointer that is
3567 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
3568 */
3569 static LPWORD
3570lpwAlign(
3571 LPWORD lpIn)
3572{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003573 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003575 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 ul += 3;
3577 ul >>= 2;
3578 ul <<= 2;
3579 return (LPWORD)ul;
3580}
3581
3582/*
3583 * Helper routine. Takes second parameter as Ansi string, copies it to first
3584 * parameter as wide character (16-bits / char) string, and returns integer
3585 * number of wide characters (words) in string (including the trailing wide
3586 * char NULL). Partly taken from the Win32SDK samples.
3587 */
3588 static int
3589nCopyAnsiToWideChar(
3590 LPWORD lpWCStr,
3591 LPSTR lpAnsiIn)
3592{
3593 int nChar = 0;
3594#ifdef FEAT_MBYTE
3595 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
3596 int i;
3597 WCHAR *wn;
3598
3599 if (enc_codepage == 0 && (int)GetACP() != enc_codepage)
3600 {
3601 /* Not a codepage, use our own conversion function. */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00003602 wn = enc_to_utf16(lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 if (wn != NULL)
3604 {
3605 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003606 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 vim_free(wn);
3608 }
3609 }
3610 if (nChar == 0)
3611 /* Use Win32 conversion function. */
3612 nChar = MultiByteToWideChar(
3613 enc_codepage > 0 ? enc_codepage : CP_ACP,
3614 MB_PRECOMPOSED,
3615 lpAnsiIn, len,
3616 lpWCStr, len);
3617 for (i = 0; i < nChar; ++i)
3618 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
3619 lpWCStr[i] = (WORD)' ';
3620#else
3621 do
3622 {
3623 if (*lpAnsiIn == '\t')
3624 *lpWCStr++ = (WORD)' ';
3625 else
3626 *lpWCStr++ = (WORD)*lpAnsiIn;
3627 nChar++;
3628 } while (*lpAnsiIn++);
3629#endif
3630
3631 return nChar;
3632}
3633
3634
3635#ifdef FEAT_TEAROFF
3636/*
3637 * The callback function for all the modeless dialogs that make up the
3638 * "tearoff menus" Very simple - forward button presses (to fool Vim into
3639 * thinking its menus have been clicked), and go away when closed.
3640 */
3641 static LRESULT CALLBACK
3642tearoff_callback(
3643 HWND hwnd,
3644 UINT message,
3645 WPARAM wParam,
3646 LPARAM lParam)
3647{
3648 if (message == WM_INITDIALOG)
3649 return (TRUE);
3650
3651 /* May show the mouse pointer again. */
3652 HandleMouseHide(message, lParam);
3653
3654 if (message == WM_COMMAND)
3655 {
3656 if ((WORD)(LOWORD(wParam)) & 0x8000)
3657 {
3658 POINT mp;
3659 RECT rect;
3660
3661 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
3662 {
3663 (void)TrackPopupMenu(
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003664 (HMENU)(long_u)(LOWORD(wParam) ^ 0x8000),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 TPM_LEFTALIGN | TPM_LEFTBUTTON,
3666 (int)rect.right - 8,
3667 (int)mp.y,
3668 (int)0, /*reserved param*/
3669 s_hwnd,
3670 NULL);
3671 /*
3672 * NOTE: The pop-up menu can eat the mouse up event.
3673 * We deal with this in normal.c.
3674 */
3675 }
3676 }
3677 else
3678 /* Pass on messages to the main Vim window */
3679 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
3680 /*
3681 * Give main window the focus back: this is so after
3682 * choosing a tearoff button you can start typing again
3683 * straight away.
3684 */
3685 (void)SetFocus(s_hwnd);
3686 return TRUE;
3687 }
3688 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
3689 {
3690 DestroyWindow(hwnd);
3691 return TRUE;
3692 }
3693
3694 /* When moved around, give main window the focus back. */
3695 if (message == WM_EXITSIZEMOVE)
3696 (void)SetActiveWindow(s_hwnd);
3697
3698 return FALSE;
3699}
3700#endif
3701
3702
3703/*
3704 * Decide whether to use the "new look" (small, non-bold font) or the "old
3705 * look" (big, clanky font) for dialogs, and work out a few values for use
3706 * later accordingly.
3707 */
3708 static void
3709get_dialog_font_metrics(void)
3710{
3711 HDC hdc;
3712 HFONT hfontTools = 0;
3713 DWORD dlgFontSize;
3714 SIZE size;
3715#ifdef USE_SYSMENU_FONT
3716 LOGFONT lfSysmenu;
3717#endif
3718
3719 s_usenewlook = FALSE;
3720
3721 /*
3722 * For NT3.51 and Win32s, we stick with the old look
3723 * because it matches everything else.
3724 */
3725 if (!is_winnt_3())
3726 {
3727#ifdef USE_SYSMENU_FONT
3728 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
3729 hfontTools = CreateFontIndirect(&lfSysmenu);
3730 else
3731#endif
3732 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
3733 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
3734
3735 if (hfontTools)
3736 {
3737 hdc = GetDC(s_hwnd);
3738 SelectObject(hdc, hfontTools);
3739 /*
3740 * GetTextMetrics() doesn't return the right value in
3741 * tmAveCharWidth, so we have to figure out the dialog base units
3742 * ourselves.
3743 */
3744 GetTextExtentPoint(hdc,
3745 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
3746 52, &size);
3747 ReleaseDC(s_hwnd, hdc);
3748
3749 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
3750 s_dlgfntheight = (WORD)size.cy;
3751 s_usenewlook = TRUE;
3752 }
3753 }
3754
3755 if (!s_usenewlook)
3756 {
3757 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
3758 s_dlgfntwidth = LOWORD(dlgFontSize);
3759 s_dlgfntheight = HIWORD(dlgFontSize);
3760 }
3761}
3762
3763#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
3764/*
3765 * Create a pseudo-"tearoff menu" based on the child
3766 * items of a given menu pointer.
3767 */
3768 static void
3769gui_mch_tearoff(
3770 char_u *title,
3771 vimmenu_T *menu,
3772 int initX,
3773 int initY)
3774{
3775 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
3776 int template_len;
3777 int nchar, textWidth, submenuWidth;
3778 DWORD lStyle;
3779 DWORD lExtendedStyle;
3780 WORD dlgwidth;
3781 WORD menuID;
3782 vimmenu_T *pmenu;
3783 vimmenu_T *the_menu = menu;
3784 HWND hwnd;
3785 HDC hdc;
3786 HFONT font, oldFont;
3787 int col, spaceWidth, len;
3788 int columnWidths[2];
3789 char_u *label, *text;
3790 int acLen = 0;
3791 int nameLen;
3792 int padding0, padding1, padding2 = 0;
3793 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003794 int x;
3795 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796#ifdef USE_SYSMENU_FONT
3797 LOGFONT lfSysmenu;
3798 int use_lfSysmenu = FALSE;
3799#endif
3800
3801 /*
3802 * If this menu is already torn off, move it to the mouse position.
3803 */
3804 if (IsWindow(menu->tearoff_handle))
3805 {
3806 POINT mp;
3807 if (GetCursorPos((LPPOINT)&mp))
3808 {
3809 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
3810 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
3811 }
3812 return;
3813 }
3814
3815 /*
3816 * Create a new tearoff.
3817 */
3818 if (*title == MNU_HIDDEN_CHAR)
3819 title++;
3820
3821 /* Allocate memory to store the dialog template. It's made bigger when
3822 * needed. */
3823 template_len = DLG_ALLOC_SIZE;
3824 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
3825 if (p == NULL)
3826 return;
3827
3828 hwnd = GetDesktopWindow();
3829 hdc = GetWindowDC(hwnd);
3830#ifdef USE_SYSMENU_FONT
3831 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
3832 {
3833 font = CreateFontIndirect(&lfSysmenu);
3834 use_lfSysmenu = TRUE;
3835 }
3836 else
3837#endif
3838 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3839 VARIABLE_PITCH , DLG_FONT_NAME);
3840 if (s_usenewlook)
3841 oldFont = SelectFont(hdc, font);
3842 else
3843 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
3844
3845 /* Calculate width of a single space. Used for padding columns to the
3846 * right width. */
3847 spaceWidth = GetTextWidth(hdc, " ", 1);
3848
3849 /* Figure out max width of the text column, the accelerator column and the
3850 * optional submenu column. */
3851 submenuWidth = 0;
3852 for (col = 0; col < 2; col++)
3853 {
3854 columnWidths[col] = 0;
3855 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
3856 {
3857 /* Use "dname" here to compute the width of the visible text. */
3858 text = (col == 0) ? pmenu->dname : pmenu->actext;
3859 if (text != NULL && *text != NUL)
3860 {
3861 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
3862 if (textWidth > columnWidths[col])
3863 columnWidths[col] = textWidth;
3864 }
3865 if (pmenu->children != NULL)
3866 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
3867 }
3868 }
3869 if (columnWidths[1] == 0)
3870 {
3871 /* no accelerators */
3872 if (submenuWidth != 0)
3873 columnWidths[0] += submenuWidth;
3874 else
3875 columnWidths[0] += spaceWidth;
3876 }
3877 else
3878 {
3879 /* there is an accelerator column */
3880 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
3881 columnWidths[1] += submenuWidth;
3882 }
3883
3884 /*
3885 * Now find the total width of our 'menu'.
3886 */
3887 textWidth = columnWidths[0] + columnWidths[1];
3888 if (submenuWidth != 0)
3889 {
3890 submenuWidth = GetTextWidth(hdc, TEAROFF_SUBMENU_LABEL,
3891 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
3892 textWidth += submenuWidth;
3893 }
3894 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
3895 if (textWidth > dlgwidth)
3896 dlgwidth = textWidth;
3897 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
3898
3899 /* W95 can't do thin dialogs, they look v. weird! */
3900 if (mch_windows95() && dlgwidth < TEAROFF_MIN_WIDTH)
3901 dlgwidth = TEAROFF_MIN_WIDTH;
3902
3903 /* start to fill in the dlgtemplate information. addressing by WORDs */
3904 if (s_usenewlook)
3905 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
3906 else
3907 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
3908
3909 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
3910 *p++ = LOWORD(lStyle);
3911 *p++ = HIWORD(lStyle);
3912 *p++ = LOWORD(lExtendedStyle);
3913 *p++ = HIWORD(lExtendedStyle);
3914 pnumitems = p; /* save where the number of items must be stored */
3915 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003916 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003918 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 else
3920 *p++ = PixelToDialogX(initX); // x
3921 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003922 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 else
3924 *p++ = PixelToDialogY(initY); // y
3925 *p++ = PixelToDialogX(dlgwidth); // cx
3926 ptrueheight = p;
3927 *p++ = 0; // dialog height: changed later anyway
3928 *p++ = 0; // Menu
3929 *p++ = 0; // Class
3930
3931 /* copy the title of the dialog */
3932 nchar = nCopyAnsiToWideChar(p, ((*title)
3933 ? (LPSTR)title
3934 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
3935 p += nchar;
3936
3937 if (s_usenewlook)
3938 {
3939 /* do the font, since DS_3DLOOK doesn't work properly */
3940#ifdef USE_SYSMENU_FONT
3941 if (use_lfSysmenu)
3942 {
3943 /* point size */
3944 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
3945 GetDeviceCaps(hdc, LOGPIXELSY));
3946 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
3947 }
3948 else
3949#endif
3950 {
3951 *p++ = DLG_FONT_POINT_SIZE; // point size
3952 nchar = nCopyAnsiToWideChar (p, TEXT(DLG_FONT_NAME));
3953 }
3954 p += nchar;
3955 }
3956
3957 /*
3958 * Loop over all the items in the menu.
3959 * But skip over the tearbar.
3960 */
3961 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
3962 menu = menu->children->next;
3963 else
3964 menu = menu->children;
3965 for ( ; menu != NULL; menu = menu->next)
3966 {
3967 if (menu->modes == 0) /* this menu has just been deleted */
3968 continue;
3969 if (menu_is_separator(menu->dname))
3970 {
3971 sepPadding += 3;
3972 continue;
3973 }
3974
3975 /* Check if there still is plenty of room in the template. Make it
3976 * larger when needed. */
3977 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
3978 {
3979 WORD *newp;
3980
3981 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
3982 if (newp != NULL)
3983 {
3984 template_len += 4096;
3985 mch_memmove(newp, pdlgtemplate,
3986 (char *)p - (char *)pdlgtemplate);
3987 p = newp + (p - pdlgtemplate);
3988 pnumitems = newp + (pnumitems - pdlgtemplate);
3989 ptrueheight = newp + (ptrueheight - pdlgtemplate);
3990 LocalFree(LocalHandle(pdlgtemplate));
3991 pdlgtemplate = newp;
3992 }
3993 }
3994
3995 /* Figure out minimal length of this menu label. Use "name" for the
3996 * actual text, "dname" for estimating the displayed size. "name"
3997 * has "&a" for mnemonic and includes the accelerator. */
3998 len = nameLen = (int)STRLEN(menu->name);
3999 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
4000 (int)STRLEN(menu->dname))) / spaceWidth;
4001 len += padding0;
4002
4003 if (menu->actext != NULL)
4004 {
4005 acLen = (int)STRLEN(menu->actext);
4006 len += acLen;
4007 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
4008 }
4009 else
4010 textWidth = 0;
4011 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
4012 len += padding1;
4013
4014 if (menu->children == NULL)
4015 {
4016 padding2 = submenuWidth / spaceWidth;
4017 len += padding2;
4018 menuID = (WORD)(menu->id);
4019 }
4020 else
4021 {
4022 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004023 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 }
4025
4026 /* Allocate menu label and fill it in */
4027 text = label = alloc((unsigned)len + 1);
4028 if (label == NULL)
4029 break;
4030
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004031 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 text = vim_strchr(text, TAB); /* stop at TAB before actext */
4033 if (text == NULL)
4034 text = label + nameLen; /* no actext, use whole name */
4035 while (padding0-- > 0)
4036 *text++ = ' ';
4037 if (menu->actext != NULL)
4038 {
4039 STRNCPY(text, menu->actext, acLen);
4040 text += acLen;
4041 }
4042 while (padding1-- > 0)
4043 *text++ = ' ';
4044 if (menu->children != NULL)
4045 {
4046 STRCPY(text, TEAROFF_SUBMENU_LABEL);
4047 text += STRLEN(TEAROFF_SUBMENU_LABEL);
4048 }
4049 else
4050 {
4051 while (padding2-- > 0)
4052 *text++ = ' ';
4053 }
4054 *text = NUL;
4055
4056 /*
4057 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
4058 * W95/NT4 it makes the tear-off look more like a menu.
4059 */
4060 p = add_dialog_element(p,
4061 BS_PUSHBUTTON|BS_LEFT,
4062 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
4063 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
4064 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
4065 (WORD)12,
4066 menuID, (WORD)0x0080, label);
4067 vim_free(label);
4068 (*pnumitems)++;
4069 }
4070
4071 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
4072
4073
4074 /* show modelessly */
4075 the_menu->tearoff_handle = CreateDialogIndirect(
4076 s_hinst,
4077 (LPDLGTEMPLATE)pdlgtemplate,
4078 s_hwnd,
4079 (DLGPROC)tearoff_callback);
4080
4081 LocalFree(LocalHandle(pdlgtemplate));
4082 SelectFont(hdc, oldFont);
4083 DeleteObject(font);
4084 ReleaseDC(hwnd, hdc);
4085
4086 /*
4087 * Reassert ourselves as the active window. This is so that after creating
4088 * a tearoff, the user doesn't have to click with the mouse just to start
4089 * typing again!
4090 */
4091 (void)SetActiveWindow(s_hwnd);
4092
4093 /* make sure the right buttons are enabled */
4094 force_menu_update = TRUE;
4095}
4096#endif
4097
4098#if defined(FEAT_TOOLBAR) || defined(PROTO)
4099#include "gui_w32_rc.h"
4100
4101/* This not defined in older SDKs */
4102# ifndef TBSTYLE_FLAT
4103# define TBSTYLE_FLAT 0x0800
4104# endif
4105
4106/*
4107 * Create the toolbar, initially unpopulated.
4108 * (just like the menu, there are no defaults, it's all
4109 * set up through menu.vim)
4110 */
4111 static void
4112initialise_toolbar(void)
4113{
4114 InitCommonControls();
4115 s_toolbarhwnd = CreateToolbarEx(
4116 s_hwnd,
4117 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
4118 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004119 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 s_hinst,
4121 IDR_TOOLBAR1, // id of initial bitmap
4122 NULL,
4123 0, // initial number of buttons
4124 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
4125 TOOLBAR_BUTTON_HEIGHT,
4126 TOOLBAR_BUTTON_WIDTH,
4127 TOOLBAR_BUTTON_HEIGHT,
4128 sizeof(TBBUTTON)
4129 );
4130
4131 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
4132}
4133
4134 static int
4135get_toolbar_bitmap(vimmenu_T *menu)
4136{
4137 int i = -1;
4138
4139 /*
4140 * Check user bitmaps first, unless builtin is specified.
4141 */
4142 if (!is_winnt_3() && !menu->icon_builtin)
4143 {
4144 char_u fname[MAXPATHL];
4145 HANDLE hbitmap = NULL;
4146
4147 if (menu->iconfile != NULL)
4148 {
4149 gui_find_iconfile(menu->iconfile, fname, "bmp");
4150 hbitmap = LoadImage(
4151 NULL,
4152 fname,
4153 IMAGE_BITMAP,
4154 TOOLBAR_BUTTON_WIDTH,
4155 TOOLBAR_BUTTON_HEIGHT,
4156 LR_LOADFROMFILE |
4157 LR_LOADMAP3DCOLORS
4158 );
4159 }
4160
4161 /*
4162 * If the LoadImage call failed, or the "icon=" file
4163 * didn't exist or wasn't specified, try the menu name
4164 */
4165 if (hbitmap == NULL
Bram Moolenaarc9dcb422013-06-27 22:02:02 +02004166 && (gui_find_bitmap(menu->en_dname != NULL
4167 ? menu->en_dname
4168 : menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 hbitmap = LoadImage(
4170 NULL,
4171 fname,
4172 IMAGE_BITMAP,
4173 TOOLBAR_BUTTON_WIDTH,
4174 TOOLBAR_BUTTON_HEIGHT,
4175 LR_LOADFROMFILE |
4176 LR_LOADMAP3DCOLORS
4177 );
4178
4179 if (hbitmap != NULL)
4180 {
4181 TBADDBITMAP tbAddBitmap;
4182
4183 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004184 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185
4186 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
4187 (WPARAM)1, (LPARAM)&tbAddBitmap);
4188 /* i will be set to -1 if it fails */
4189 }
4190 }
4191 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
4192 i = menu->iconidx;
4193
4194 return i;
4195}
4196#endif
4197
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004198#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
4199 static void
4200initialise_tabline(void)
4201{
4202 InitCommonControls();
4203
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004204 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004205 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004206 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4207 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004208
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004209 gui.tabline_height = TABLINE_HEIGHT;
4210
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004211# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004212 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004213# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004214}
4215#endif
4216
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
4218/*
4219 * Make the GUI window come to the foreground.
4220 */
4221 void
4222gui_mch_set_foreground(void)
4223{
4224 if (IsIconic(s_hwnd))
4225 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
4226 SetForegroundWindow(s_hwnd);
4227}
4228#endif
4229
4230#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4231 static void
4232dyn_imm_load(void)
4233{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02004234 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 if (hLibImm == NULL)
4236 return;
4237
4238 pImmGetCompositionStringA
4239 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
4240 pImmGetCompositionStringW
4241 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
4242 pImmGetContext
4243 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
4244 pImmAssociateContext
4245 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
4246 pImmReleaseContext
4247 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
4248 pImmGetOpenStatus
4249 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
4250 pImmSetOpenStatus
4251 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
4252 pImmGetCompositionFont
4253 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
4254 pImmSetCompositionFont
4255 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
4256 pImmSetCompositionWindow
4257 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
4258 pImmGetConversionStatus
4259 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00004260 pImmSetConversionStatus
4261 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262
4263 if ( pImmGetCompositionStringA == NULL
4264 || pImmGetCompositionStringW == NULL
4265 || pImmGetContext == NULL
4266 || pImmAssociateContext == NULL
4267 || pImmReleaseContext == NULL
4268 || pImmGetOpenStatus == NULL
4269 || pImmSetOpenStatus == NULL
4270 || pImmGetCompositionFont == NULL
4271 || pImmSetCompositionFont == NULL
4272 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00004273 || pImmGetConversionStatus == NULL
4274 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 {
4276 FreeLibrary(hLibImm);
4277 hLibImm = NULL;
4278 pImmGetContext = NULL;
4279 return;
4280 }
4281
4282 return;
4283}
4284
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285#endif
4286
4287#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
4288
4289# ifdef FEAT_XPM_W32
4290# define IMAGE_XPM 100
4291# endif
4292
4293typedef struct _signicon_t
4294{
4295 HANDLE hImage;
4296 UINT uType;
4297#ifdef FEAT_XPM_W32
4298 HANDLE hShape; /* Mask bitmap handle */
4299#endif
4300} signicon_t;
4301
4302 void
4303gui_mch_drawsign(row, col, typenr)
4304 int row;
4305 int col;
4306 int typenr;
4307{
4308 signicon_t *sign;
4309 int x, y, w, h;
4310
4311 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
4312 return;
4313
4314 x = TEXT_X(col);
4315 y = TEXT_Y(row);
4316 w = gui.char_width * 2;
4317 h = gui.char_height;
4318 switch (sign->uType)
4319 {
4320 case IMAGE_BITMAP:
4321 {
4322 HDC hdcMem;
4323 HBITMAP hbmpOld;
4324
4325 hdcMem = CreateCompatibleDC(s_hdc);
4326 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
4327 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
4328 SelectObject(hdcMem, hbmpOld);
4329 DeleteDC(hdcMem);
4330 }
4331 break;
4332 case IMAGE_ICON:
4333 case IMAGE_CURSOR:
4334 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
4335 break;
4336#ifdef FEAT_XPM_W32
4337 case IMAGE_XPM:
4338 {
4339 HDC hdcMem;
4340 HBITMAP hbmpOld;
4341
4342 hdcMem = CreateCompatibleDC(s_hdc);
4343 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
4344 /* Make hole */
4345 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
4346
4347 SelectObject(hdcMem, sign->hImage);
4348 /* Paint sign */
4349 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
4350 SelectObject(hdcMem, hbmpOld);
4351 DeleteDC(hdcMem);
4352 }
4353 break;
4354#endif
4355 }
4356}
4357
4358 static void
4359close_signicon_image(signicon_t *sign)
4360{
4361 if (sign)
4362 switch (sign->uType)
4363 {
4364 case IMAGE_BITMAP:
4365 DeleteObject((HGDIOBJ)sign->hImage);
4366 break;
4367 case IMAGE_CURSOR:
4368 DestroyCursor((HCURSOR)sign->hImage);
4369 break;
4370 case IMAGE_ICON:
4371 DestroyIcon((HICON)sign->hImage);
4372 break;
4373#ifdef FEAT_XPM_W32
4374 case IMAGE_XPM:
4375 DeleteObject((HBITMAP)sign->hImage);
4376 DeleteObject((HBITMAP)sign->hShape);
4377 break;
4378#endif
4379 }
4380}
4381
4382 void *
4383gui_mch_register_sign(signfile)
4384 char_u *signfile;
4385{
4386 signicon_t sign, *psign;
4387 char_u *ext;
4388
4389 if (is_winnt_3())
4390 {
4391 EMSG(_(e_signdata));
4392 return NULL;
4393 }
4394
4395 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004396 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 if (ext > signfile)
4398 {
4399 int do_load = 1;
4400
4401 if (!STRICMP(ext, ".bmp"))
4402 sign.uType = IMAGE_BITMAP;
4403 else if (!STRICMP(ext, ".ico"))
4404 sign.uType = IMAGE_ICON;
4405 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
4406 sign.uType = IMAGE_CURSOR;
4407 else
4408 do_load = 0;
4409
4410 if (do_load)
4411 sign.hImage = (HANDLE)LoadImage(NULL, signfile, sign.uType,
4412 gui.char_width * 2, gui.char_height,
4413 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
4414#ifdef FEAT_XPM_W32
4415 if (!STRICMP(ext, ".xpm"))
4416 {
4417 sign.uType = IMAGE_XPM;
4418 LoadXpmImage(signfile, (HBITMAP *)&sign.hImage, (HBITMAP *)&sign.hShape);
4419 }
4420#endif
4421 }
4422
4423 psign = NULL;
4424 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
4425 != NULL)
4426 *psign = sign;
4427
4428 if (!psign)
4429 {
4430 if (sign.hImage)
4431 close_signicon_image(&sign);
4432 EMSG(_(e_signdata));
4433 }
4434 return (void *)psign;
4435
4436}
4437
4438 void
4439gui_mch_destroy_sign(sign)
4440 void *sign;
4441{
4442 if (sign)
4443 {
4444 close_signicon_image((signicon_t *)sign);
4445 vim_free(sign);
4446 }
4447}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449
4450#if defined(FEAT_BEVAL) || defined(PROTO)
4451
4452/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004453 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 *
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004455 * The only reused thing is gui_beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 * from gui_beval.c (note it uses x and y of the BalloonEval struct
4457 * to get current mouse position).
4458 *
4459 * Trying to use as more Windows services as possible, and as less
4460 * IE version as possible :)).
4461 *
4462 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
4463 * BalloonEval struct.
4464 * 2) Enable/Disable simply create/kill BalloonEval Timer
4465 * 3) When there was enough inactivity, timer procedure posts
4466 * async request to debugger
4467 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
4468 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00004469 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 */
4471
Bram Moolenaar45360022005-07-21 21:08:21 +00004472/*
4473 * determine whether installed Common Controls support multiline tooltips
4474 * (i.e. their version is >= 4.70
4475 */
4476 int
4477multiline_balloon_available(void)
4478{
4479 HINSTANCE hDll;
4480 static char comctl_dll[] = "comctl32.dll";
4481 static int multiline_tip = MAYBE;
4482
4483 if (multiline_tip != MAYBE)
4484 return multiline_tip;
4485
4486 hDll = GetModuleHandle(comctl_dll);
4487 if (hDll != NULL)
4488 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004489 DLLGETVERSIONPROC pGetVer;
4490 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00004491
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004492 if (pGetVer != NULL)
4493 {
4494 DLLVERSIONINFO dvi;
4495 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00004496
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004497 ZeroMemory(&dvi, sizeof(dvi));
4498 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00004499
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004500 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00004501
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004502 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00004503 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004504 || (dvi.dwMajorVersion == 4
4505 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00004506 {
4507 multiline_tip = TRUE;
4508 return multiline_tip;
4509 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004510 }
Bram Moolenaar45360022005-07-21 21:08:21 +00004511 else
4512 {
4513 /* there is chance we have ancient CommCtl 4.70
4514 which doesn't export DllGetVersion */
4515 DWORD dwHandle = 0;
4516 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
4517 if (len > 0)
4518 {
4519 VS_FIXEDFILEINFO *ver;
4520 UINT vlen = 0;
4521 void *data = alloc(len);
4522
4523 if (data != NULL
4524 && GetFileVersionInfo(comctl_dll, 0, len, data)
4525 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
4526 && vlen
4527 && HIWORD(ver->dwFileVersionMS) > 4
4528 || (HIWORD(ver->dwFileVersionMS) == 4
4529 && LOWORD(ver->dwFileVersionMS) >= 70))
4530 {
4531 vim_free(data);
4532 multiline_tip = TRUE;
4533 return multiline_tip;
4534 }
4535 vim_free(data);
4536 }
4537 }
4538 }
4539 multiline_tip = FALSE;
4540 return multiline_tip;
4541}
4542
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 static void
4544make_tooltip(beval, text, pt)
4545 BalloonEval *beval;
4546 char *text;
4547 POINT pt;
4548{
Bram Moolenaar45360022005-07-21 21:08:21 +00004549 TOOLINFO *pti;
4550 int ToolInfoSize;
4551
4552 if (multiline_balloon_available() == TRUE)
4553 ToolInfoSize = sizeof(TOOLINFO_NEW);
4554 else
4555 ToolInfoSize = sizeof(TOOLINFO);
4556
4557 pti = (TOOLINFO *)alloc(ToolInfoSize);
4558 if (pti == NULL)
4559 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560
4561 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
4562 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
4563 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4564 beval->target, NULL, s_hinst, NULL);
4565
4566 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
4567 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
4568
Bram Moolenaar45360022005-07-21 21:08:21 +00004569 pti->cbSize = ToolInfoSize;
4570 pti->uFlags = TTF_SUBCLASS;
4571 pti->hwnd = beval->target;
4572 pti->hinst = 0; /* Don't use string resources */
4573 pti->uId = ID_BEVAL_TOOLTIP;
4574
4575 if (multiline_balloon_available() == TRUE)
4576 {
4577 RECT rect;
4578 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
4579 pti->lpszText = LPSTR_TEXTCALLBACK;
4580 ptin->lParam = (LPARAM)text;
4581 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
4582 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
4583 (LPARAM)rect.right);
4584 }
4585 else
4586 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587
4588 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00004589 pti->rect.left = pt.x - 3;
4590 pti->rect.top = pt.y - 3;
4591 pti->rect.right = pt.x + 3;
4592 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593
Bram Moolenaar45360022005-07-21 21:08:21 +00004594 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 /* Make tooltip appear sooner */
4596 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00004597 /* I've performed some tests and it seems the longest possible life time
4598 * of tooltip is 30 seconds */
4599 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 /*
4601 * HACK: force tooltip to appear, because it'll not appear until
4602 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00004603 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00004605 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00004607 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608}
4609
4610 static void
4611delete_tooltip(beval)
4612 BalloonEval *beval;
4613{
4614 DestroyWindow(beval->balloon);
4615}
4616
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004617/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 static VOID CALLBACK
4619BevalTimerProc(hwnd, uMsg, idEvent, dwTime)
4620 HWND hwnd;
4621 UINT uMsg;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004622 UINT_PTR idEvent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 DWORD dwTime;
4624{
4625 POINT pt;
4626 RECT rect;
4627
4628 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
4629 return;
4630
4631 GetCursorPos(&pt);
4632 if (WindowFromPoint(pt) != s_textArea)
4633 return;
4634
4635 ScreenToClient(s_textArea, &pt);
4636 GetClientRect(s_textArea, &rect);
4637 if (!PtInRect(&rect, pt))
4638 return;
4639
4640 if (LastActivity > 0
4641 && (dwTime - LastActivity) >= (DWORD)p_bdlay
4642 && (cur_beval->showState != ShS_PENDING
4643 || abs(cur_beval->x - pt.x) > 3
4644 || abs(cur_beval->y - pt.y) > 3))
4645 {
4646 /* Pointer resting in one place long enough, it's time to show
4647 * the tooltip. */
4648 cur_beval->showState = ShS_PENDING;
4649 cur_beval->x = pt.x;
4650 cur_beval->y = pt.y;
4651
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004652 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653
4654 if (cur_beval->msgCB != NULL)
4655 (*cur_beval->msgCB)(cur_beval, 0);
4656 }
4657}
4658
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004659/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 void
4661gui_mch_disable_beval_area(beval)
4662 BalloonEval *beval;
4663{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004664 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004666 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667}
4668
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004669/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 void
4671gui_mch_enable_beval_area(beval)
4672 BalloonEval *beval;
4673{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004674 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 if (beval == NULL)
4676 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004677 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02004678 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004679 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680}
4681
4682 void
4683gui_mch_post_balloon(beval, mesg)
4684 BalloonEval *beval;
4685 char_u *mesg;
4686{
4687 POINT pt;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004688 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 if (beval->showState == ShS_SHOWING)
4690 return;
4691 GetCursorPos(&pt);
4692 ScreenToClient(s_textArea, &pt);
4693
4694 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
4695 /* cursor is still here */
4696 {
4697 gui_mch_disable_beval_area(cur_beval);
4698 beval->showState = ShS_SHOWING;
4699 make_tooltip(beval, mesg, pt);
4700 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004701 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702}
4703
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004704/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 BalloonEval *
4706gui_mch_create_beval_area(target, mesg, mesgCB, clientData)
4707 void *target; /* ignored, always use s_textArea */
4708 char_u *mesg;
4709 void (*mesgCB)__ARGS((BalloonEval *, int));
4710 void *clientData;
4711{
4712 /* partially stolen from gui_beval.c */
4713 BalloonEval *beval;
4714
4715 if (mesg != NULL && mesgCB != NULL)
4716 {
4717 EMSG(_("E232: Cannot create BalloonEval with both message and callback"));
4718 return NULL;
4719 }
4720
4721 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
4722 if (beval != NULL)
4723 {
4724 beval->target = s_textArea;
4725 beval->balloon = NULL;
4726
4727 beval->showState = ShS_NEUTRAL;
4728 beval->x = 0;
4729 beval->y = 0;
4730 beval->msg = mesg;
4731 beval->msgCB = mesgCB;
4732 beval->clientData = clientData;
4733
4734 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 cur_beval = beval;
4736
4737 if (p_beval)
4738 gui_mch_enable_beval_area(beval);
4739
4740 }
4741 return beval;
4742}
4743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004744/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 static void
Bram Moolenaar442b4222010-05-24 21:34:22 +02004746Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747{
4748 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
4749 return;
4750
4751 if (cur_beval != NULL)
4752 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004753 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004755 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004756 // TRACE0("TTN_SHOW {{{");
4757 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00004758 break;
4759 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004760 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 delete_tooltip(cur_beval);
4762 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004763 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764
4765 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00004766 break;
4767 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004768 {
4769 /* if you get there then we have new common controls */
4770 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
4771 info->lpszText = (LPSTR)info->lParam;
4772 info->uFlags |= TTF_DI_SETITEM;
4773 }
Bram Moolenaar45360022005-07-21 21:08:21 +00004774 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 }
4776 }
4777}
4778
4779 static void
4780TrackUserActivity(UINT uMsg)
4781{
4782 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
4783 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
4784 LastActivity = GetTickCount();
4785}
4786
4787 void
4788gui_mch_destroy_beval_area(beval)
4789 BalloonEval *beval;
4790{
4791 vim_free(beval);
4792}
4793#endif /* FEAT_BEVAL */
4794
4795#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
4796/*
4797 * We have multiple signs to draw at the same location. Draw the
4798 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
4799 */
4800 void
4801netbeans_draw_multisign_indicator(int row)
4802{
4803 int i;
4804 int y;
4805 int x;
4806
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004807 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004808 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004809
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 x = 0;
4811 y = TEXT_Y(row);
4812
4813 for (i = 0; i < gui.char_height - 3; i++)
4814 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
4815
4816 SetPixel(s_hdc, x+0, y, gui.currFgColor);
4817 SetPixel(s_hdc, x+2, y, gui.currFgColor);
4818 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
4819 SetPixel(s_hdc, x+1, y, gui.currFgColor);
4820 SetPixel(s_hdc, x+2, y, gui.currFgColor);
4821 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
4822 SetPixel(s_hdc, x+2, y, gui.currFgColor);
4823}
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004824
4825/*
4826 * Initialize the Winsock dll.
4827 */
4828 void
4829netbeans_init_winsock()
4830{
4831 WSADATA wsaData;
4832 int wsaerr;
4833
4834 if (WSInitialized)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004835 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004836
4837 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
4838 if (wsaerr == 0)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004839 WSInitialized = TRUE;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004840}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841#endif