blob: c8f409c46f5ed138df94d0fd1218a5868acb2608 [file] [log] [blame]
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00001s, if any./* 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
26/*
27 * These are new in Windows ME/XP, only defined in recent compilers.
28 */
29#ifndef HANDLE_WM_XBUTTONUP
30# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
31 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
32#endif
33#ifndef HANDLE_WM_XBUTTONDOWN
34# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
35 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
36#endif
37#ifndef HANDLE_WM_XBUTTONDBLCLK
38# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
39 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
40#endif
41
42/*
43 * Include the common stuff for MS-Windows GUI.
44 */
45#include "gui_w48.c"
46
47#ifdef FEAT_XPM_W32
48# include "xpm_w32.h"
49#endif
50
51#ifdef PROTO
52# define WINAPI
53#endif
54
55#ifdef __MINGW32__
56/*
57 * Add a lot of missing defines.
58 * They are not always missing, we need the #ifndef's.
59 */
60# ifndef _cdecl
61# define _cdecl
62# endif
63# ifndef IsMinimized
64# define IsMinimized(hwnd) IsIconic(hwnd)
65# endif
66# ifndef IsMaximized
67# define IsMaximized(hwnd) IsZoomed(hwnd)
68# endif
69# ifndef SelectFont
70# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
71# endif
72# ifndef GetStockBrush
73# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
74# endif
75# ifndef DeleteBrush
76# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
77# endif
78
79# ifndef HANDLE_WM_RBUTTONDBLCLK
80# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
81 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
82# endif
83# ifndef HANDLE_WM_MBUTTONUP
84# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
85 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
86# endif
87# ifndef HANDLE_WM_MBUTTONDBLCLK
88# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
89 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
90# endif
91# ifndef HANDLE_WM_LBUTTONDBLCLK
92# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
93 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
94# endif
95# ifndef HANDLE_WM_RBUTTONDOWN
96# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
97 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
98# endif
99# ifndef HANDLE_WM_MOUSEMOVE
100# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
101 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
102# endif
103# ifndef HANDLE_WM_RBUTTONUP
104# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
105 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
106# endif
107# ifndef HANDLE_WM_MBUTTONDOWN
108# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
109 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
110# endif
111# ifndef HANDLE_WM_LBUTTONUP
112# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
113 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
114# endif
115# ifndef HANDLE_WM_LBUTTONDOWN
116# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
117 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
118# endif
119# ifndef HANDLE_WM_SYSCHAR
120# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
121 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
122# endif
123# ifndef HANDLE_WM_ACTIVATEAPP
124# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
125 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
126# endif
127# ifndef HANDLE_WM_WINDOWPOSCHANGING
128# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
129 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
130# endif
131# ifndef HANDLE_WM_VSCROLL
132# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
133 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
134# endif
135# ifndef HANDLE_WM_SETFOCUS
136# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
137 ((fn)((hwnd), (HWND)(wParam)), 0L)
138# endif
139# ifndef HANDLE_WM_KILLFOCUS
140# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
141 ((fn)((hwnd), (HWND)(wParam)), 0L)
142# endif
143# ifndef HANDLE_WM_HSCROLL
144# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
145 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
146# endif
147# ifndef HANDLE_WM_DROPFILES
148# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
149 ((fn)((hwnd), (HDROP)(wParam)), 0L)
150# endif
151# ifndef HANDLE_WM_CHAR
152# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
153 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
154# endif
155# ifndef HANDLE_WM_SYSDEADCHAR
156# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
157 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
158# endif
159# ifndef HANDLE_WM_DEADCHAR
160# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
161 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
162# endif
163#endif /* __MINGW32__ */
164
165
166/* Some parameters for tearoff menus. All in pixels. */
167#define TEAROFF_PADDING_X 2
168#define TEAROFF_BUTTON_PAD_X 8
169#define TEAROFF_MIN_WIDTH 200
170#define TEAROFF_SUBMENU_LABEL ">>"
171#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
172
173
174/* For the Intellimouse: */
175#ifndef WM_MOUSEWHEEL
176#define WM_MOUSEWHEEL 0x20a
177#endif
178
179
180#ifdef FEAT_BEVAL
181# define ID_BEVAL_TOOLTIP 200
182# define BEVAL_TEXT_LEN MAXPATHL
183
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000184#if _MSC_VER < 1300
185/* Work around old versions of basetsd.h which wrongly declare
186 * UINT_PTR as unsigned long */
Bram Moolenaar8424a622006-04-19 21:23:36 +0000187# define UINT_PTR UINT
188#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000189
190static void make_tooltip __ARGS((BalloonEval *beval, char *text, POINT pt));
191static void delete_tooltip __ARGS((BalloonEval *beval));
192static VOID CALLBACK BevalTimerProc __ARGS((HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime));
193
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000195static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +0000197
198/*
199 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000200 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +0000201 */
202#include <pshpack1.h>
203
204typedef struct _DllVersionInfo
205{
206 DWORD cbSize;
207 DWORD dwMajorVersion;
208 DWORD dwMinorVersion;
209 DWORD dwBuildNumber;
210 DWORD dwPlatformID;
211} DLLVERSIONINFO;
212
213typedef struct tagTOOLINFOA_NEW
214{
215 UINT cbSize;
216 UINT uFlags;
217 HWND hwnd;
218 UINT uId;
219 RECT rect;
220 HINSTANCE hinst;
221 LPSTR lpszText;
222 LPARAM lParam;
223} TOOLINFO_NEW;
224
225typedef struct tagNMTTDISPINFO_NEW
226{
227 NMHDR hdr;
228 LPTSTR lpszText;
229 char szText[80];
230 HINSTANCE hinst;
231 UINT uFlags;
232 LPARAM lParam;
233} NMTTDISPINFO_NEW;
234
235#include <poppack.h>
236
237typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
238#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000239# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240#endif
241
Bram Moolenaar45360022005-07-21 21:08:21 +0000242#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000243# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +0000244#endif
245
246#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000247# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +0000248#endif
249
250#endif /* defined(FEAT_BEVAL) */
251
Bram Moolenaar2c7a7632007-05-10 18:19:11 +0000252#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
253/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
254 * it here if LPNMTTDISPINFO isn't defined.
255 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
256 * _MSC_VER. */
257# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
258typedef struct tagNMTTDISPINFOA {
259 NMHDR hdr;
260 LPSTR lpszText;
261 char szText[80];
262 HINSTANCE hinst;
263 UINT uFlags;
264 LPARAM lParam;
265} NMTTDISPINFOA, *LPNMTTDISPINFOA;
266# define LPNMTTDISPINFO LPNMTTDISPINFOA
267
268# ifdef FEAT_MBYTE
269typedef struct tagNMTTDISPINFOW {
270 NMHDR hdr;
271 LPWSTR lpszText;
272 WCHAR szText[80];
273 HINSTANCE hinst;
274 UINT uFlags;
275 LPARAM lParam;
276} NMTTDISPINFOW, *LPNMTTDISPINFOW;
277# endif
278# endif
279#endif
280
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000281#ifndef TTN_GETDISPINFOW
282# define TTN_GETDISPINFOW (TTN_FIRST - 10)
283#endif
284
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285/* Local variables: */
286
287#ifdef FEAT_MENU
288static UINT s_menu_id = 100;
289
290/*
291 * Use the system font for dialogs and tear-off menus. Remove this line to
292 * use DLG_FONT_NAME.
293 */
294# define USE_SYSMENU_FONT
295#endif
296
297#define VIM_NAME "vim"
298#define VIM_CLASS "Vim"
299#define VIM_CLASSW L"Vim"
300
301/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
302 * thus there should be room for every dialog. For tearoffs it's made bigger
303 * when needed. */
304#define DLG_ALLOC_SIZE 16 * 1024
305
306/*
307 * stuff for dialogs, menus, tearoffs etc.
308 */
309static LRESULT APIENTRY dialog_callback(HWND, UINT, WPARAM, LPARAM);
310static LRESULT APIENTRY tearoff_callback(HWND, UINT, WPARAM, LPARAM);
311static PWORD
312add_dialog_element(
313 PWORD p,
314 DWORD lStyle,
315 WORD x,
316 WORD y,
317 WORD w,
318 WORD h,
319 WORD Id,
320 WORD clss,
321 const char *caption);
322static LPWORD lpwAlign(LPWORD);
323static int nCopyAnsiToWideChar(LPWORD, LPSTR);
324static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
325static void get_dialog_font_metrics(void);
326
327static int dialog_default_button = -1;
328
329/* Intellimouse support */
330static int mouse_scroll_lines = 0;
331static UINT msh_msgmousewheel = 0;
332
333static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
334#ifdef FEAT_TOOLBAR
335static void initialise_toolbar(void);
336static int get_toolbar_bitmap(vimmenu_T *menu);
337#endif
338
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000339#ifdef FEAT_GUI_TABLINE
340static void initialise_tabline(void);
341#endif
342
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#ifdef FEAT_MBYTE_IME
344static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
345static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
346#endif
347#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
348# ifdef NOIME
349typedef struct tagCOMPOSITIONFORM {
350 DWORD dwStyle;
351 POINT ptCurrentPos;
352 RECT rcArea;
353} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
354typedef HANDLE HIMC;
355# endif
356
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000357static HINSTANCE hLibImm = NULL;
358static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
359static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
360static HIMC (WINAPI *pImmGetContext)(HWND);
361static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
362static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
363static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
364static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
365static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
366static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
367static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
368static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +0000369static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370static void dyn_imm_load(void);
371#else
372# define pImmGetCompositionStringA ImmGetCompositionStringA
373# define pImmGetCompositionStringW ImmGetCompositionStringW
374# define pImmGetContext ImmGetContext
375# define pImmAssociateContext ImmAssociateContext
376# define pImmReleaseContext ImmReleaseContext
377# define pImmGetOpenStatus ImmGetOpenStatus
378# define pImmSetOpenStatus ImmSetOpenStatus
379# define pImmGetCompositionFont ImmGetCompositionFontA
380# define pImmSetCompositionFont ImmSetCompositionFontA
381# define pImmSetCompositionWindow ImmSetCompositionWindow
382# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +0000383# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384#endif
385
386#ifndef ETO_IGNORELANGUAGE
387# define ETO_IGNORELANGUAGE 0x1000
388#endif
389
390/* multi monitor support */
391typedef struct _MONITORINFOstruct
392{
393 DWORD cbSize;
394 RECT rcMonitor;
395 RECT rcWork;
396 DWORD dwFlags;
397} _MONITORINFO;
398
399typedef HANDLE _HMONITOR;
400typedef _HMONITOR (WINAPI *TMonitorFromWindow)(HWND, DWORD);
401typedef BOOL (WINAPI *TGetMonitorInfo)(_HMONITOR, _MONITORINFO *);
402
403static TMonitorFromWindow pMonitorFromWindow = NULL;
404static TGetMonitorInfo pGetMonitorInfo = NULL;
405static HANDLE user32_lib = NULL;
406#ifdef FEAT_NETBEANS_INTG
407int WSInitialized = FALSE; /* WinSock is initialized */
408#endif
409/*
410 * Return TRUE when running under Windows NT 3.x or Win32s, both of which have
411 * less fancy GUI APIs.
412 */
413 static int
414is_winnt_3(void)
415{
416 return ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
417 && os_version.dwMajorVersion == 3)
418 || (os_version.dwPlatformId == VER_PLATFORM_WIN32s));
419}
420
421/*
422 * Return TRUE when running under Win32s.
423 */
424 int
425gui_is_win32s(void)
426{
427 return (os_version.dwPlatformId == VER_PLATFORM_WIN32s);
428}
429
430#ifdef FEAT_MENU
431/*
432 * Figure out how high the menu bar is at the moment.
433 */
434 static int
435gui_mswin_get_menu_height(
436 int fix_window) /* If TRUE, resize window if menu height changed */
437{
438 static int old_menu_height = -1;
439
440 RECT rc1, rc2;
441 int num;
442 int menu_height;
443
444 if (gui.menu_is_active)
445 num = GetMenuItemCount(s_menuBar);
446 else
447 num = 0;
448
449 if (num == 0)
450 menu_height = 0;
451 else
452 {
453 if (is_winnt_3()) /* for NT 3.xx */
454 {
455 if (gui.starting)
456 menu_height = GetSystemMetrics(SM_CYMENU);
457 else
458 {
459 RECT r1, r2;
460 int frameht = GetSystemMetrics(SM_CYFRAME);
461 int capht = GetSystemMetrics(SM_CYCAPTION);
462
463 /* get window rect of s_hwnd
464 * get client rect of s_hwnd
465 * get cap height
466 * subtract from window rect, the sum of client height,
467 * (if not maximized)frame thickness, and caption height.
468 */
469 GetWindowRect(s_hwnd, &r1);
470 GetClientRect(s_hwnd, &r2);
471 menu_height = r1.bottom - r1.top - (r2.bottom - r2.top
472 + 2 * frameht * (!IsZoomed(s_hwnd)) + capht);
473 }
474 }
475 else /* win95 and variants (NT 4.0, I guess) */
476 {
477 /*
478 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
479 * seem to have been set yet, so menu wraps in default window
480 * width which is very narrow. Instead just return height of a
481 * single menu item. Will still be wrong when the menu really
482 * should wrap over more than one line.
483 */
484 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
485 if (gui.starting)
486 menu_height = rc1.bottom - rc1.top + 1;
487 else
488 {
489 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
490 menu_height = rc2.bottom - rc1.top + 1;
491 }
492 }
493 }
494
495 if (fix_window && menu_height != old_menu_height)
496 {
497 old_menu_height = menu_height;
Bram Moolenaarafa24992006-03-27 20:58:26 +0000498 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 }
500
501 return menu_height;
502}
503#endif /*FEAT_MENU*/
504
505
506/*
507 * Setup for the Intellimouse
508 */
509 static void
510init_mouse_wheel(void)
511{
512
513#ifndef SPI_GETWHEELSCROLLLINES
514# define SPI_GETWHEELSCROLLLINES 104
515#endif
Bram Moolenaare7566042005-06-17 22:00:15 +0000516#ifndef SPI_SETWHEELSCROLLLINES
517# define SPI_SETWHEELSCROLLLINES 105
518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519
520#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
521#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
522#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
523#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
524
525 HWND hdl_mswheel;
526 UINT msh_msgscrolllines;
527
528 msh_msgmousewheel = 0;
529 mouse_scroll_lines = 3; /* reasonable default */
530
531 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
532 && os_version.dwMajorVersion >= 4)
533 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
534 && ((os_version.dwMajorVersion == 4
535 && os_version.dwMinorVersion >= 10)
536 || os_version.dwMajorVersion >= 5)))
537 {
538 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
539 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
540 &mouse_scroll_lines, 0);
541 }
542 else if (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
543 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
544 && os_version.dwMajorVersion < 4))
545 { /*
546 * If Win95 or NT 3.51,
547 * try to find the hidden point32 window.
548 */
549 hdl_mswheel = FindWindow(VMOUSEZ_CLASSNAME, VMOUSEZ_TITLE);
550 if (hdl_mswheel)
551 {
552 msh_msgscrolllines = RegisterWindowMessage(VMSH_SCROLL_LINES);
553 if (msh_msgscrolllines)
554 {
555 mouse_scroll_lines = (int)SendMessage(hdl_mswheel,
556 msh_msgscrolllines, 0, 0);
557 msh_msgmousewheel = RegisterWindowMessage(VMSH_MOUSEWHEEL);
558 }
559 }
560 }
561}
562
563
564/* Intellimouse wheel handler */
565 static void
566_OnMouseWheel(
567 HWND hwnd,
568 short zDelta)
569{
570/* Treat a mouse wheel event as if it were a scroll request */
571 int i;
572 int size;
573 HWND hwndCtl;
574
575 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
576 {
577 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
578 size = curwin->w_scrollbars[SBAR_RIGHT].size;
579 }
580 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
581 {
582 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
583 size = curwin->w_scrollbars[SBAR_LEFT].size;
584 }
585 else
586 return;
587
588 size = curwin->w_height;
589 if (mouse_scroll_lines == 0)
590 init_mouse_wheel();
591
592 if (mouse_scroll_lines > 0
593 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
594 {
595 for (i = mouse_scroll_lines; i > 0; --i)
596 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
597 }
598 else
599 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
600}
601
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000602#ifdef USE_SYSMENU_FONT
603/*
604 * Get Menu Font.
605 * Return OK or FAIL.
606 */
607 static int
608gui_w32_get_menu_font(LOGFONT *lf)
609{
610 NONCLIENTMETRICS nm;
611
612 nm.cbSize = sizeof(NONCLIENTMETRICS);
613 if (!SystemParametersInfo(
614 SPI_GETNONCLIENTMETRICS,
615 sizeof(NONCLIENTMETRICS),
616 &nm,
617 0))
618 return FAIL;
619 *lf = nm.lfMenuFont;
620 return OK;
621}
622#endif
623
624
625#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
626/*
627 * Set the GUI tabline font to the system menu font
628 */
629 static void
630set_tabline_font(void)
631{
632 LOGFONT lfSysmenu;
633 HFONT font;
634 HWND hwnd;
635 HDC hdc;
636 HFONT hfntOld;
637 TEXTMETRIC tm;
638
639 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
640 return;
641
642 font = CreateFontIndirect(&lfSysmenu);
643
644 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
645
646 /*
647 * Compute the height of the font used for the tab text
648 */
649 hwnd = GetDesktopWindow();
650 hdc = GetWindowDC(hwnd);
651 hfntOld = SelectFont(hdc, font);
652
653 GetTextMetrics(hdc, &tm);
654
655 SelectFont(hdc, hfntOld);
656 ReleaseDC(hwnd, hdc);
657
658 /*
659 * The space used by the tab border and the space between the tab label
660 * and the tab border is included as 7.
661 */
662 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
663}
664#endif
665
Bram Moolenaar520470a2005-06-16 21:59:56 +0000666/*
667 * Invoked when a setting was changed.
668 */
669 static LRESULT CALLBACK
670_OnSettingChange(UINT n)
671{
672 if (n == SPI_SETWHEELSCROLLLINES)
673 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
674 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000675#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
676 if (n == SPI_SETNONCLIENTMETRICS)
677 set_tabline_font();
678#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +0000679 return 0;
680}
681
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682#if 0 /* disabled, a gap appears below and beside the window, and the window
683 can be moved (in a strange way) */
684/*
685 * Even though we have _DuringSizing() which makes the rubber band a valid
686 * size, we need this for when the user maximises the window.
687 * TODO: Doesn't seem to adjust the width though for some reason.
688 */
689 static BOOL
690_OnWindowPosChanging(
691 HWND hwnd,
692 LPWINDOWPOS lpwpos)
693{
694 RECT workarea_rect;
695
696 if (!(lpwpos->flags & SWP_NOSIZE))
697 {
698 if (IsMaximized(hwnd)
699 && (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
700 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
701 && os_version.dwMajorVersion >= 4)))
702 {
703 SystemParametersInfo(SPI_GETWORKAREA, 0, &workarea_rect, 0);
704 lpwpos->x = workarea_rect.left;
705 lpwpos->y = workarea_rect.top;
706 lpwpos->cx = workarea_rect.right - workarea_rect.left;
707 lpwpos->cy = workarea_rect.bottom - workarea_rect.top;
708 }
709 gui_mswin_get_valid_dimensions(lpwpos->cx, lpwpos->cy,
710 &lpwpos->cx, &lpwpos->cy);
711 }
712 return 0;
713}
714#endif
715
716#ifdef FEAT_NETBEANS_INTG
717 static void
718_OnWindowPosChanged(
719 HWND hwnd,
720 const LPWINDOWPOS lpwpos)
721{
722 static int x = 0, y = 0, cx = 0, cy = 0;
723
724 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
725 || lpwpos->cx != cx || lpwpos->cy != cy))
726 {
727 x = lpwpos->x;
728 y = lpwpos->y;
729 cx = lpwpos->cx;
730 cy = lpwpos->cy;
731 netbeans_frame_moved(x, y);
732 }
733 /* Allow to send WM_SIZE and WM_MOVE */
734 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
735}
736#endif
737
738 static int
739_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 UINT fwSide,
741 LPRECT lprc)
742{
743 int w, h;
744 int valid_w, valid_h;
745 int w_offset, h_offset;
746
747 w = lprc->right - lprc->left;
748 h = lprc->bottom - lprc->top;
749 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
750 w_offset = w - valid_w;
751 h_offset = h - valid_h;
752
753 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
754 || fwSide == WMSZ_BOTTOMLEFT)
755 lprc->left += w_offset;
756 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
757 || fwSide == WMSZ_BOTTOMRIGHT)
758 lprc->right -= w_offset;
759
760 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
761 || fwSide == WMSZ_TOPRIGHT)
762 lprc->top += h_offset;
763 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
764 || fwSide == WMSZ_BOTTOMRIGHT)
765 lprc->bottom -= h_offset;
766 return TRUE;
767}
768
769
770
771 static LRESULT CALLBACK
772_WndProc(
773 HWND hwnd,
774 UINT uMsg,
775 WPARAM wParam,
776 LPARAM lParam)
777{
778 /*
779 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
780 hwnd, uMsg, wParam, lParam);
781 */
782
783 HandleMouseHide(uMsg, lParam);
784
785 s_uMsg = uMsg;
786 s_wParam = wParam;
787 s_lParam = lParam;
788
789 switch (uMsg)
790 {
791 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
792 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
793 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
794 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
795 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
796 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
797 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
798 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
799 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
800#ifdef FEAT_MENU
801 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
802#endif
803 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
804 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
805 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
806 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
807 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
808 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
809 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
810 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
811 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
812#ifdef FEAT_NETBEANS_INTG
813 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
814#endif
815
Bram Moolenaarafa24992006-03-27 20:58:26 +0000816#ifdef FEAT_GUI_TABLINE
817 case WM_RBUTTONUP:
818 {
819 if (gui_mch_showing_tabline())
820 {
821 POINT pt;
822 RECT rect;
823
824 /*
825 * If the cursor is on the tabline, display the tab menu
826 */
827 GetCursorPos((LPPOINT)&pt);
828 GetWindowRect(s_textArea, &rect);
829 if (pt.y < rect.top)
830 {
831 show_tabline_popup_menu();
832 return 0;
833 }
834 }
835 return MyWindowProc(hwnd, uMsg, wParam, lParam);
836 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000837 case WM_LBUTTONDBLCLK:
838 {
839 /*
840 * If the user double clicked the tabline, create a new tab
841 */
842 if (gui_mch_showing_tabline())
843 {
844 POINT pt;
845 RECT rect;
846
847 GetCursorPos((LPPOINT)&pt);
848 GetWindowRect(s_textArea, &rect);
849 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000850 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000851 }
852 return MyWindowProc(hwnd, uMsg, wParam, lParam);
853 }
Bram Moolenaarafa24992006-03-27 20:58:26 +0000854#endif
855
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 case WM_QUERYENDSESSION: /* System wants to go down. */
857 gui_shell_closed(); /* Will exit when no changed buffers. */
858 return FALSE; /* Do NOT allow system to go down. */
859
860 case WM_ENDSESSION:
861 if (wParam) /* system only really goes down when wParam is TRUE */
862 _OnEndSession();
863 break;
864
865 case WM_CHAR:
866 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
867 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000868 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 return 0L;
870
871 case WM_SYSCHAR:
872 /*
873 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
874 * shortcut key, handle like a typed ALT key, otherwise call Windows
875 * ALT key handling.
876 */
877#ifdef FEAT_MENU
878 if ( !gui.menu_is_active
879 || p_wak[0] == 'n'
880 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
881 )
882#endif
883 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000884 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 return 0L;
886 }
887#ifdef FEAT_MENU
888 else
889 return MyWindowProc(hwnd, uMsg, wParam, lParam);
890#endif
891
892 case WM_SYSKEYUP:
893#ifdef FEAT_MENU
894 /* This used to be done only when menu is active: ALT key is used for
895 * that. But that caused problems when menu is disabled and using
896 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
897 * are received, mouse pointer remains hidden. */
898 return MyWindowProc(hwnd, uMsg, wParam, lParam);
899#else
900 return 0;
901#endif
902
903 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000904 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905
906 case WM_MOUSEWHEEL:
907 _OnMouseWheel(hwnd, HIWORD(wParam));
908 break;
909
Bram Moolenaar520470a2005-06-16 21:59:56 +0000910 /* Notification for change in SystemParametersInfo() */
911 case WM_SETTINGCHANGE:
912 return _OnSettingChange((UINT)wParam);
913
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000914#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 case WM_NOTIFY:
916 switch (((LPNMHDR) lParam)->code)
917 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000918# ifdef FEAT_MBYTE
919 case TTN_GETDISPINFOW:
920# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000921 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000923 LPNMHDR hdr = (LPNMHDR)lParam;
924 char_u *str = NULL;
925 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000926
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000927 vim_free(tt_text);
928 tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000929
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000930# ifdef FEAT_GUI_TABLINE
931 if (gui_mch_showing_tabline()
932 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000933 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000934 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000935 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000936 * Mouse is over the GUI tabline. Display the
937 * tooltip for the tab under the cursor
938 *
939 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000940 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000941 GetCursorPos(&pt);
942 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000943 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000944 TCHITTESTINFO htinfo;
945 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000946
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000947 /*
948 * Get the tab under the cursor
949 */
950 htinfo.pt.x = pt.x;
951 htinfo.pt.y = pt.y;
952 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
953 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000954 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000955 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000956
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000957 tp = find_tabpage(idx + 1);
958 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000959 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000960 get_tabline_label(tp, TRUE);
961 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000962 }
963 }
964 }
965 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000966# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000967# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000968# ifdef FEAT_GUI_TABLINE
969 else
970# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000972 UINT idButton;
973 vimmenu_T *pMenu;
974
975 idButton = (UINT) hdr->idFrom;
976 pMenu = gui_mswin_find_menu(root_menu, idButton);
977 if (pMenu)
978 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000980# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +0000981 if (str != NULL)
982 {
983# ifdef FEAT_MBYTE
984 if (hdr->code == TTN_GETDISPINFOW)
985 {
986 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
987
988 tt_text = enc_to_ucs2(str, NULL);
989 lpdi->lpszText = tt_text;
990 /* can't show tooltip if failed */
991 }
992 else
993# endif
994 {
995 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
996
997 if (STRLEN(str) < sizeof(lpdi->szText)
998 || ((tt_text = vim_strsave(str)) == NULL))
999 vim_strncpy(lpdi->szText, str,
1000 sizeof(lpdi->szText) - 1);
1001 else
1002 lpdi->lpszText = tt_text;
1003 }
1004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 }
1006 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001007# ifdef FEAT_GUI_TABLINE
1008 case TCN_SELCHANGE:
1009 if (gui_mch_showing_tabline()
1010 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
1011 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
1012 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00001013
1014 case NM_RCLICK:
1015 if (gui_mch_showing_tabline()
1016 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
1017 show_tabline_popup_menu();
1018 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001019# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001021# ifdef FEAT_GUI_TABLINE
1022 if (gui_mch_showing_tabline()
1023 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
1024 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1025# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 break;
1027 }
1028 break;
1029#endif
1030#if defined(MENUHINTS) && defined(FEAT_MENU)
1031 case WM_MENUSELECT:
1032 if (((UINT) HIWORD(wParam)
1033 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
1034 == MF_HILITE
1035 && (State & CMDLINE) == 0)
1036 {
1037 UINT idButton;
1038 vimmenu_T *pMenu;
1039 static int did_menu_tip = FALSE;
1040
1041 if (did_menu_tip)
1042 {
1043 msg_clr_cmdline();
1044 setcursor();
1045 out_flush();
1046 did_menu_tip = FALSE;
1047 }
1048
1049 idButton = (UINT)LOWORD(wParam);
1050 pMenu = gui_mswin_find_menu(root_menu, idButton);
1051 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
1052 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
1053 {
1054 msg(pMenu->strings[MENU_INDEX_TIP]);
1055 setcursor();
1056 out_flush();
1057 did_menu_tip = TRUE;
1058 }
1059 }
1060 break;
1061#endif
1062 case WM_NCHITTEST:
1063 {
1064 LRESULT result;
1065 int x, y;
1066 int xPos = GET_X_LPARAM(lParam);
1067
1068 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
1069 if (result == HTCLIENT)
1070 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001071#ifdef FEAT_GUI_TABLINE
1072 if (gui_mch_showing_tabline())
1073 {
1074 int yPos = GET_Y_LPARAM(lParam);
1075 RECT rct;
1076
1077 /* If the cursor is on the GUI tabline, don't process this
1078 * event */
1079 GetWindowRect(s_textArea, &rct);
1080 if (yPos < rct.top)
1081 return result;
1082 }
1083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 gui_mch_get_winpos(&x, &y);
1085 xPos -= x;
1086
1087 if (xPos < 48) /* <VN> TODO should use system metric? */
1088 return HTBOTTOMLEFT;
1089 else
1090 return HTBOTTOMRIGHT;
1091 }
1092 else
1093 return result;
1094 }
1095 /* break; notreached */
1096
1097#ifdef FEAT_MBYTE_IME
1098 case WM_IME_NOTIFY:
1099 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
1100 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1101 break;
1102 case WM_IME_COMPOSITION:
1103 if (!_OnImeComposition(hwnd, wParam, lParam))
1104 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1105 break;
1106#endif
1107
1108 default:
1109 if (uMsg == msh_msgmousewheel && msh_msgmousewheel != 0)
1110 { /* handle MSH_MOUSEWHEEL messages for Intellimouse */
1111 _OnMouseWheel(hwnd, HIWORD(wParam));
1112 break;
1113 }
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
1123 return 1;
1124}
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 {
1261 /* stolen from gui_x11.x */
1262 int arg;
1263
1264 for (arg = 1; arg < *argc; arg++)
1265 if (strncmp("-nb", argv[arg], 3) == 0)
1266 {
1267 usingNetbeans++;
1268 netbeansArg = argv[arg];
1269 mch_memmove(&argv[arg], &argv[arg + 1],
1270 (--*argc - arg) * sizeof(char *));
1271 argv[*argc] = NULL;
1272 break; /* enough? */
1273 }
1274
1275 if (usingNetbeans)
1276 {
1277 WSADATA wsaData;
1278 int wsaerr;
1279
1280 /* Init WinSock */
1281 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
1282 if (wsaerr == 0)
1283 WSInitialized = TRUE;
1284 }
1285 }
1286#endif
1287
1288 /* get the OS version info */
1289 os_version.dwOSVersionInfoSize = sizeof(os_version);
1290 GetVersionEx(&os_version); /* this call works on Win32s, Win95 and WinNT */
1291
1292 /* try and load the user32.dll library and get the entry points for
1293 * multi-monitor-support. */
1294 if ((user32_lib = LoadLibrary("User32.dll")) != NULL)
1295 {
1296 pMonitorFromWindow = (TMonitorFromWindow)GetProcAddress(user32_lib,
1297 "MonitorFromWindow");
1298
1299 /* there are ...A and ...W version of GetMonitorInfo - looking at
1300 * winuser.h, they have exactly the same declaration. */
1301 pGetMonitorInfo = (TGetMonitorInfo)GetProcAddress(user32_lib,
1302 "GetMonitorInfoA");
1303 }
1304}
1305
1306/*
1307 * Initialise the GUI. Create all the windows, set up all the call-backs
1308 * etc.
1309 */
1310 int
1311gui_mch_init(void)
1312{
1313 const char szVimWndClass[] = VIM_CLASS;
1314 const char szTextAreaClass[] = "VimTextArea";
1315 WNDCLASS wndclass;
1316#ifdef FEAT_MBYTE
1317 const WCHAR szVimWndClassW[] = VIM_CLASSW;
1318 WNDCLASSW wndclassw;
1319#endif
1320#ifdef GLOBAL_IME
1321 ATOM atom;
1322#endif
1323
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 /* Return here if the window was already opened (happens when
1325 * gui_mch_dialog() is called early). */
1326 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00001327 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328
1329 /*
1330 * Load the tearoff bitmap
1331 */
1332#ifdef FEAT_TEAROFF
1333 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
1334#endif
1335
1336 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
1337 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
1338#ifdef FEAT_MENU
1339 gui.menu_height = 0; /* Windows takes care of this */
1340#endif
1341 gui.border_width = 0;
1342
1343 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
1344
1345#ifdef FEAT_MBYTE
1346 /* First try using the wide version, so that we can use any title.
1347 * Otherwise only characters in the active codepage will work. */
1348 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
1349 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001350 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 wndclassw.lpfnWndProc = _WndProc;
1352 wndclassw.cbClsExtra = 0;
1353 wndclassw.cbWndExtra = 0;
1354 wndclassw.hInstance = s_hinst;
1355 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
1356 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
1357 wndclassw.hbrBackground = s_brush;
1358 wndclassw.lpszMenuName = NULL;
1359 wndclassw.lpszClassName = szVimWndClassW;
1360
1361 if ((
1362#ifdef GLOBAL_IME
1363 atom =
1364#endif
1365 RegisterClassW(&wndclassw)) == 0)
1366 {
1367 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1368 return FAIL;
1369
1370 /* Must be Windows 98, fall back to non-wide function. */
1371 }
1372 else
1373 wide_WindowProc = TRUE;
1374 }
1375
1376 if (!wide_WindowProc)
1377#endif
1378
1379 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
1380 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001381 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 wndclass.lpfnWndProc = _WndProc;
1383 wndclass.cbClsExtra = 0;
1384 wndclass.cbWndExtra = 0;
1385 wndclass.hInstance = s_hinst;
1386 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
1387 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1388 wndclass.hbrBackground = s_brush;
1389 wndclass.lpszMenuName = NULL;
1390 wndclass.lpszClassName = szVimWndClass;
1391
1392 if ((
1393#ifdef GLOBAL_IME
1394 atom =
1395#endif
1396 RegisterClass(&wndclass)) == 0)
1397 return FAIL;
1398 }
1399
1400 if (vim_parent_hwnd != NULL)
1401 {
1402#ifdef HAVE_TRY_EXCEPT
1403 __try
1404 {
1405#endif
1406 /* Open inside the specified parent window.
1407 * TODO: last argument should point to a CLIENTCREATESTRUCT
1408 * structure. */
1409 s_hwnd = CreateWindowEx(
1410 WS_EX_MDICHILD,
1411 szVimWndClass, "Vim MSWindows GUI",
1412 WS_OVERLAPPEDWINDOW | WS_CHILD | WS_CLIPSIBLINGS | 0xC000,
1413 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
1414 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
1415 100, /* Any value will do */
1416 100, /* Any value will do */
1417 vim_parent_hwnd, NULL,
1418 s_hinst, NULL);
1419#ifdef HAVE_TRY_EXCEPT
1420 }
1421 __except(EXCEPTION_EXECUTE_HANDLER)
1422 {
1423 /* NOP */
1424 }
1425#endif
1426 if (s_hwnd == NULL)
1427 {
1428 EMSG(_("E672: Unable to open window inside MDI application"));
1429 mch_exit(2);
1430 }
1431 }
1432 else
1433 /* Open toplevel window. */
1434 s_hwnd = CreateWindow(
1435 szVimWndClass, "Vim MSWindows GUI",
1436 WS_OVERLAPPEDWINDOW,
1437 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
1438 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
1439 100, /* Any value will do */
1440 100, /* Any value will do */
1441 NULL, NULL,
1442 s_hinst, NULL);
1443
1444 if (s_hwnd == NULL)
1445 return FAIL;
1446
1447#ifdef GLOBAL_IME
1448 global_ime_init(atom, s_hwnd);
1449#endif
1450#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
1451 dyn_imm_load();
1452#endif
1453
1454 /* Create the text area window */
1455 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
1456 {
1457 wndclass.style = CS_OWNDC;
1458 wndclass.lpfnWndProc = _TextAreaWndProc;
1459 wndclass.cbClsExtra = 0;
1460 wndclass.cbWndExtra = 0;
1461 wndclass.hInstance = s_hinst;
1462 wndclass.hIcon = NULL;
1463 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1464 wndclass.hbrBackground = NULL;
1465 wndclass.lpszMenuName = NULL;
1466 wndclass.lpszClassName = szTextAreaClass;
1467
1468 if (RegisterClass(&wndclass) == 0)
1469 return FAIL;
1470 }
1471 s_textArea = CreateWindowEx(
1472 WS_EX_CLIENTEDGE,
1473 szTextAreaClass, "Vim text area",
1474 WS_CHILD | WS_VISIBLE, 0, 0,
1475 100, /* Any value will do for now */
1476 100, /* Any value will do for now */
1477 s_hwnd, NULL,
1478 s_hinst, NULL);
1479
1480 if (s_textArea == NULL)
1481 return FAIL;
1482
1483#ifdef FEAT_MENU
1484 s_menuBar = CreateMenu();
1485#endif
1486 s_hdc = GetDC(s_textArea);
1487
1488#ifdef MSWIN16_FASTTEXT
1489 SetBkMode(s_hdc, OPAQUE);
1490#endif
1491
1492#ifdef FEAT_WINDOWS
1493 DragAcceptFiles(s_hwnd, TRUE);
1494#endif
1495
1496 /* Do we need to bother with this? */
1497 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
1498
1499 /* Get background/foreground colors from the system */
1500 gui_mch_def_colors();
1501
1502 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
1503 * file) */
1504 set_normal_colors();
1505
1506 /*
1507 * Check that none of the colors are the same as the background color.
1508 * Then store the current values as the defaults.
1509 */
1510 gui_check_colors();
1511 gui.def_norm_pixel = gui.norm_pixel;
1512 gui.def_back_pixel = gui.back_pixel;
1513
1514 /* Get the colors for the highlight groups (gui_check_colors() might have
1515 * changed them) */
1516 highlight_gui_started();
1517
1518 /*
1519 * Start out by adding the configured border width into the border offset
1520 */
1521 gui.border_offset = gui.border_width + 2; /*CLIENT EDGE*/
1522
1523 /*
1524 * Set up for Intellimouse processing
1525 */
1526 init_mouse_wheel();
1527
1528 /*
1529 * compute a couple of metrics used for the dialogs
1530 */
1531 get_dialog_font_metrics();
1532#ifdef FEAT_TOOLBAR
1533 /*
1534 * Create the toolbar
1535 */
1536 initialise_toolbar();
1537#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001538#ifdef FEAT_GUI_TABLINE
1539 /*
1540 * Create the tabline
1541 */
1542 initialise_tabline();
1543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544#ifdef MSWIN_FIND_REPLACE
1545 /*
1546 * Initialise the dialog box stuff
1547 */
1548 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
1549
1550 /* Initialise the struct */
1551 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
1552 s_findrep_struct.lpstrFindWhat = alloc(MSWIN_FR_BUFSIZE);
1553 s_findrep_struct.lpstrFindWhat[0] = NUL;
1554 s_findrep_struct.lpstrReplaceWith = alloc(MSWIN_FR_BUFSIZE);
1555 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1556 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
1557 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
1558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
Bram Moolenaar748bf032005-02-02 23:04:36 +00001560theend:
1561 /* Display any pending error messages */
1562 display_errors();
1563
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 return OK;
1565}
1566
1567/*
1568 * Get the size of the screen, taking position on multiple monitors into
1569 * account (if supported).
1570 */
1571 static void
1572get_work_area(RECT *spi_rect)
1573{
1574 _HMONITOR mon;
1575 _MONITORINFO moninfo;
1576
1577 /* use these functions only if available */
1578 if (pMonitorFromWindow != NULL && pGetMonitorInfo != NULL)
1579 {
1580 /* work out which monitor the window is on, and get *it's* work area */
1581 mon = pMonitorFromWindow(s_hwnd, 1 /*MONITOR_DEFAULTTOPRIMARY*/);
1582 if (mon != NULL)
1583 {
1584 moninfo.cbSize = sizeof(_MONITORINFO);
1585 if (pGetMonitorInfo(mon, &moninfo))
1586 {
1587 *spi_rect = moninfo.rcWork;
1588 return;
1589 }
1590 }
1591 }
1592 /* this is the old method... */
1593 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
1594}
1595
1596/*
1597 * Set the size of the window to the given width and height in pixels.
1598 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001599/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 void
1601gui_mch_set_shellsize(int width, int height,
Bram Moolenaarafa24992006-03-27 20:58:26 +00001602 int min_width, int min_height, int base_width, int base_height,
1603 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604{
1605 RECT workarea_rect;
1606 int win_width, win_height;
1607 int win_xpos, win_ypos;
1608 WINDOWPLACEMENT wndpl;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00001609 int workarea_left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001611 /* Try to keep window completely on screen. */
1612 /* Get position of the screen work area. This is the part that is not
1613 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 get_work_area(&workarea_rect);
1615
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001616 /* Get current posision of our window. Note that the .left and .top are
1617 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 wndpl.length = sizeof(WINDOWPLACEMENT);
1619 GetWindowPlacement(s_hwnd, &wndpl);
1620
1621 /* Resizing a maximized window looks very strange, unzoom it first.
1622 * But don't do it when still starting up, it may have been requested in
1623 * the shortcut. */
1624 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
1625 {
1626 ShowWindow(s_hwnd, SW_SHOWNORMAL);
1627 /* Need to get the settings of the normal window. */
1628 GetWindowPlacement(s_hwnd, &wndpl);
1629 }
1630
1631 win_xpos = wndpl.rcNormalPosition.left;
1632 win_ypos = wndpl.rcNormalPosition.top;
1633
1634 /* compute the size of the outside of the window */
1635 win_width = width + GetSystemMetrics(SM_CXFRAME) * 2;
1636 win_height = height + GetSystemMetrics(SM_CYFRAME) * 2
1637 + GetSystemMetrics(SM_CYCAPTION)
1638#ifdef FEAT_MENU
1639 + gui_mswin_get_menu_height(FALSE)
1640#endif
1641 ;
1642
Bram Moolenaar56a907a2006-05-06 21:44:30 +00001643 /* There is an inconsistency when using two monitors and Vim is on the
1644 * second (right) one: win_xpos will be the offset from the workarea of
1645 * the left monitor. While with one monitor it's the offset from the
1646 * workarea (including a possible taskbar on the left). Detect the second
1647 * monitor by checking for the left offset to be quite big. */
1648 if (workarea_rect.left > 300)
1649 workarea_left = 0;
1650 else
1651 workarea_left = workarea_rect.left;
1652
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001653 /* If the window is going off the screen, move it on to the screen.
1654 * win_xpos and win_ypos are relative to the workarea. */
1655 if ((direction & RESIZE_HOR)
Bram Moolenaar56a907a2006-05-06 21:44:30 +00001656 && workarea_left + win_xpos + win_width > workarea_rect.right)
1657 win_xpos = workarea_rect.right - win_width - workarea_left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001659 if ((direction & RESIZE_HOR) && win_xpos < 0)
1660 win_xpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661
Bram Moolenaarafa24992006-03-27 20:58:26 +00001662 if ((direction & RESIZE_VERT)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001663 && workarea_rect.top + win_ypos + win_height > workarea_rect.bottom)
1664 win_ypos = workarea_rect.bottom - win_height - workarea_rect.top;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001666 if ((direction & RESIZE_VERT) && win_ypos < 0)
1667 win_ypos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668
1669 wndpl.rcNormalPosition.left = win_xpos;
1670 wndpl.rcNormalPosition.right = win_xpos + win_width;
1671 wndpl.rcNormalPosition.top = win_ypos;
1672 wndpl.rcNormalPosition.bottom = win_ypos + win_height;
1673
1674 /* set window position - we should use SetWindowPlacement rather than
1675 * SetWindowPos as the MSDN docs say the coord systems returned by
1676 * these two are not compatible. */
1677 SetWindowPlacement(s_hwnd, &wndpl);
1678
1679 SetActiveWindow(s_hwnd);
1680 SetFocus(s_hwnd);
1681
1682#ifdef FEAT_MENU
1683 /* Menu may wrap differently now */
1684 gui_mswin_get_menu_height(!gui.starting);
1685#endif
1686}
1687
1688
1689 void
1690gui_mch_set_scrollbar_thumb(
1691 scrollbar_T *sb,
1692 long val,
1693 long size,
1694 long max)
1695{
1696 SCROLLINFO info;
1697
1698 sb->scroll_shift = 0;
1699 while (max > 32767)
1700 {
1701 max = (max + 1) >> 1;
1702 val >>= 1;
1703 size >>= 1;
1704 ++sb->scroll_shift;
1705 }
1706
1707 if (sb->scroll_shift > 0)
1708 ++size;
1709
1710 info.cbSize = sizeof(info);
1711 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
1712 info.nPos = val;
1713 info.nMin = 0;
1714 info.nMax = max;
1715 info.nPage = size;
1716 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
1717}
1718
1719
1720/*
1721 * Set the current text font.
1722 */
1723 void
1724gui_mch_set_font(GuiFont font)
1725{
1726 gui.currFont = font;
1727}
1728
1729
1730/*
1731 * Set the current text foreground color.
1732 */
1733 void
1734gui_mch_set_fg_color(guicolor_T color)
1735{
1736 gui.currFgColor = color;
1737}
1738
1739/*
1740 * Set the current text background color.
1741 */
1742 void
1743gui_mch_set_bg_color(guicolor_T color)
1744{
1745 gui.currBgColor = color;
1746}
1747
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001748/*
1749 * Set the current text special color.
1750 */
1751 void
1752gui_mch_set_sp_color(guicolor_T color)
1753{
1754 gui.currSpColor = color;
1755}
1756
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757#if defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)
1758/*
1759 * Multi-byte handling, originally by Sung-Hoon Baek.
1760 * First static functions (no prototypes generated).
1761 */
1762#ifdef _MSC_VER
1763# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
1764#endif
1765#include <imm.h>
1766
1767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 * handle WM_IME_NOTIFY message
1769 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001770/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 static LRESULT
1772_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData)
1773{
1774 LRESULT lResult = 0;
1775 HIMC hImc;
1776
1777 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
1778 return lResult;
1779 switch (dwCommand)
1780 {
1781 case IMN_SETOPENSTATUS:
1782 if (pImmGetOpenStatus(hImc))
1783 {
1784 pImmSetCompositionFont(hImc, &norm_logfont);
1785 im_set_position(gui.row, gui.col);
1786
1787 /* Disable langmap */
1788 State &= ~LANGMAP;
1789 if (State & INSERT)
1790 {
1791#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1792 /* Unshown 'keymap' in status lines */
1793 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
1794 {
1795 /* Save cursor position */
1796 int old_row = gui.row;
1797 int old_col = gui.col;
1798
1799 // This must be called here before
1800 // status_redraw_curbuf(), otherwise the mode
1801 // message may appear in the wrong position.
1802 showmode();
1803 status_redraw_curbuf();
1804 update_screen(0);
1805 /* Restore cursor position */
1806 gui.row = old_row;
1807 gui.col = old_col;
1808 }
1809#endif
1810 }
1811 }
1812 gui_update_cursor(TRUE, FALSE);
1813 lResult = 0;
1814 break;
1815 }
1816 pImmReleaseContext(hWnd, hImc);
1817 return lResult;
1818}
1819
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001820/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 static LRESULT
1822_OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param)
1823{
1824 char_u *ret;
1825 int len;
1826
1827 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
1828 return 0;
1829
1830 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
1831 if (ret != NULL)
1832 {
1833 add_to_input_buf_csi(ret, len);
1834 vim_free(ret);
1835 return 1;
1836 }
1837 return 0;
1838}
1839
1840/*
1841 * get the current composition string, in UCS-2; *lenp is the number of
1842 * *lenp is the number of Unicode characters.
1843 */
1844 static short_u *
1845GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
1846{
1847 LONG ret;
1848 LPWSTR wbuf = NULL;
1849 char_u *buf;
1850
1851 if (!pImmGetContext)
1852 return NULL; /* no imm32.dll */
1853
1854 /* Try Unicode; this'll always work on NT regardless of codepage. */
1855 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
1856 if (ret == 0)
1857 return NULL; /* empty */
1858
1859 if (ret > 0)
1860 {
1861 /* Allocate the requested buffer plus space for the NUL character. */
1862 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
1863 if (wbuf != NULL)
1864 {
1865 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
1866 *lenp = ret / sizeof(WCHAR);
1867 }
1868 return (short_u *)wbuf;
1869 }
1870
1871 /* ret < 0; we got an error, so try the ANSI version. This'll work
1872 * on 9x/ME, but only if the codepage happens to be set to whatever
1873 * we're inputting. */
1874 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
1875 if (ret <= 0)
1876 return NULL; /* empty or error */
1877
1878 buf = alloc(ret);
1879 if (buf == NULL)
1880 return NULL;
1881 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
1882
1883 /* convert from codepage to UCS-2 */
1884 MultiByteToWideChar_alloc(GetACP(), 0, buf, ret, &wbuf, lenp);
1885 vim_free(buf);
1886
1887 return (short_u *)wbuf;
1888}
1889
1890/*
1891 * void GetResultStr()
1892 *
1893 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
1894 * get complete composition string
1895 */
1896 static char_u *
1897GetResultStr(HWND hwnd, int GCS, int *lenp)
1898{
1899 HIMC hIMC; /* Input context handle. */
1900 short_u *buf = NULL;
1901 char_u *convbuf = NULL;
1902
1903 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
1904 return NULL;
1905
1906 /* Reads in the composition string. */
1907 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
1908 if (buf == NULL)
1909 return NULL;
1910
1911 convbuf = ucs2_to_enc(buf, lenp);
1912 pImmReleaseContext(hwnd, hIMC);
1913 vim_free(buf);
1914 return convbuf;
1915}
1916#endif
1917
1918/* For global functions we need prototypes. */
1919#if (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) || defined(PROTO)
1920
1921/*
1922 * set font to IM.
1923 */
1924 void
1925im_set_font(LOGFONT *lf)
1926{
1927 HIMC hImc;
1928
1929 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
1930 {
1931 pImmSetCompositionFont(hImc, lf);
1932 pImmReleaseContext(s_hwnd, hImc);
1933 }
1934}
1935
1936/*
1937 * Notify cursor position to IM.
1938 */
1939 void
1940im_set_position(int row, int col)
1941{
1942 HIMC hImc;
1943
1944 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
1945 {
1946 COMPOSITIONFORM cfs;
1947
1948 cfs.dwStyle = CFS_POINT;
1949 cfs.ptCurrentPos.x = FILL_X(col);
1950 cfs.ptCurrentPos.y = FILL_Y(row);
1951 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
1952 pImmSetCompositionWindow(hImc, &cfs);
1953
1954 pImmReleaseContext(s_hwnd, hImc);
1955 }
1956}
1957
1958/*
1959 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
1960 */
1961 void
1962im_set_active(int active)
1963{
1964 HIMC hImc;
1965 static HIMC hImcOld = (HIMC)0;
1966
1967 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
1968 {
1969 if (p_imdisable)
1970 {
1971 if (hImcOld == (HIMC)0)
1972 {
1973 hImcOld = pImmGetContext(s_hwnd);
1974 if (hImcOld)
1975 pImmAssociateContext(s_hwnd, (HIMC)0);
1976 }
1977 active = FALSE;
1978 }
1979 else if (hImcOld != (HIMC)0)
1980 {
1981 pImmAssociateContext(s_hwnd, hImcOld);
1982 hImcOld = (HIMC)0;
1983 }
1984
1985 hImc = pImmGetContext(s_hwnd);
1986 if (hImc)
1987 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001988 /*
1989 * for Korean ime
1990 */
1991 HKL hKL = GetKeyboardLayout(0);
1992
1993 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
1994 {
1995 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
1996 static BOOL bSaved = FALSE;
1997
1998 if (active)
1999 {
2000 /* if we have a saved conversion status, restore it */
2001 if (bSaved)
2002 pImmSetConversionStatus(hImc, dwConversionSaved,
2003 dwSentenceSaved);
2004 bSaved = FALSE;
2005 }
2006 else
2007 {
2008 /* save conversion status and disable korean */
2009 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
2010 &dwSentenceSaved))
2011 {
2012 bSaved = TRUE;
2013 pImmSetConversionStatus(hImc,
2014 dwConversionSaved & ~(IME_CMODE_NATIVE
2015 | IME_CMODE_FULLSHAPE),
2016 dwSentenceSaved);
2017 }
2018 }
2019 }
2020
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 pImmSetOpenStatus(hImc, active);
2022 pImmReleaseContext(s_hwnd, hImc);
2023 }
2024 }
2025}
2026
2027/*
2028 * Get IM status. When IM is on, return not 0. Else return 0.
2029 */
2030 int
2031im_get_status()
2032{
2033 int status = 0;
2034 HIMC hImc;
2035
2036 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
2037 {
2038 status = pImmGetOpenStatus(hImc) ? 1 : 0;
2039 pImmReleaseContext(s_hwnd, hImc);
2040 }
2041 return status;
2042}
2043
2044#endif /* FEAT_MBYTE && FEAT_MBYTE_IME */
2045
2046#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
2047/* Win32 with GLOBAL IME */
2048
2049/*
2050 * Notify cursor position to IM.
2051 */
2052 void
2053im_set_position(int row, int col)
2054{
2055 /* Win32 with GLOBAL IME */
2056 POINT p;
2057
2058 p.x = FILL_X(col);
2059 p.y = FILL_Y(row);
2060 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
2061 global_ime_set_position(&p);
2062}
2063
2064/*
2065 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
2066 */
2067 void
2068im_set_active(int active)
2069{
2070 global_ime_set_status(active);
2071}
2072
2073/*
2074 * Get IM status. When IM is on, return not 0. Else return 0.
2075 */
2076 int
2077im_get_status()
2078{
2079 return global_ime_get_status();
2080}
2081#endif
2082
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002083#ifdef FEAT_MBYTE
2084/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00002085 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002086 */
2087 static void
2088latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
2089{
2090 int c;
2091
Bram Moolenaarca003e12006-03-17 23:19:38 +00002092 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002093 {
2094 c = *text++;
2095 switch (c)
2096 {
2097 case 0xa4: c = 0x20ac; break; /* euro */
2098 case 0xa6: c = 0x0160; break; /* S hat */
2099 case 0xa8: c = 0x0161; break; /* S -hat */
2100 case 0xb4: c = 0x017d; break; /* Z hat */
2101 case 0xb8: c = 0x017e; break; /* Z -hat */
2102 case 0xbc: c = 0x0152; break; /* OE */
2103 case 0xbd: c = 0x0153; break; /* oe */
2104 case 0xbe: c = 0x0178; break; /* Y */
2105 }
2106 *unicodebuf++ = c;
2107 }
2108}
2109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110
2111#ifdef FEAT_RIGHTLEFT
2112/*
2113 * What is this for? In the case where you are using Win98 or Win2K or later,
2114 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
2115 * reverses the string sent to the TextOut... family. This sucks, because we
2116 * go to a lot of effort to do the right thing, and there doesn't seem to be a
2117 * way to tell Windblows not to do this!
2118 *
2119 * The short of it is that this 'RevOut' only gets called if you are running
2120 * one of the new, "improved" MS OSes, and only if you are running in
2121 * 'rightleft' mode. It makes display take *slightly* longer, but not
2122 * noticeably so.
2123 */
2124 static void
2125RevOut( HDC s_hdc,
2126 int col,
2127 int row,
2128 UINT foptions,
2129 CONST RECT *pcliprect,
2130 LPCTSTR text,
2131 UINT len,
2132 CONST INT *padding)
2133{
2134 int ix;
2135 static int special = -1;
2136
2137 if (special == -1)
2138 {
2139 /* Check windows version: special treatment is needed if it is NT 5 or
2140 * Win98 or higher. */
2141 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
2142 && os_version.dwMajorVersion >= 5)
2143 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
2144 && (os_version.dwMajorVersion > 4
2145 || (os_version.dwMajorVersion == 4
2146 && os_version.dwMinorVersion > 0))))
2147 special = 1;
2148 else
2149 special = 0;
2150 }
2151
2152 if (special)
2153 for (ix = 0; ix < (int)len; ++ix)
2154 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
2155 pcliprect, text + ix, 1, padding);
2156 else
2157 ExtTextOut(s_hdc, col, row, foptions, pcliprect, text, len, padding);
2158}
2159#endif
2160
2161 void
2162gui_mch_draw_string(
2163 int row,
2164 int col,
2165 char_u *text,
2166 int len,
2167 int flags)
2168{
2169 static int *padding = NULL;
2170 static int pad_size = 0;
2171 int i;
2172 const RECT *pcliprect = NULL;
2173 UINT foptions = 0;
2174#ifdef FEAT_MBYTE
2175 static WCHAR *unicodebuf = NULL;
2176 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002177 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 int n = 0;
2179#endif
2180 HPEN hpen, old_pen;
2181 int y;
2182
2183#ifndef MSWIN16_FASTTEXT
2184 /*
2185 * Italic and bold text seems to have an extra row of pixels at the bottom
2186 * (below where the bottom of the character should be). If we draw the
2187 * characters with a solid background, the top row of pixels in the
2188 * character below will be overwritten. We can fix this by filling in the
2189 * background ourselves, to the correct character proportions, and then
2190 * writing the character in transparent mode. Still have a problem when
2191 * the character is "_", which gets written on to the character below.
2192 * New fix: set gui.char_ascent to -1. This shifts all characters up one
2193 * pixel in their slots, which fixes the problem with the bottom row of
2194 * pixels. We still need this code because otherwise the top row of pixels
2195 * becomes a problem. - webb.
2196 */
2197 static HBRUSH hbr_cache[2] = {NULL, NULL};
2198 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
2199 static int brush_lru = 0;
2200 HBRUSH hbr;
2201 RECT rc;
2202
2203 if (!(flags & DRAW_TRANSP))
2204 {
2205 /*
2206 * Clear background first.
2207 * Note: FillRect() excludes right and bottom of rectangle.
2208 */
2209 rc.left = FILL_X(col);
2210 rc.top = FILL_Y(row);
2211#ifdef FEAT_MBYTE
2212 if (has_mbyte)
2213 {
2214 int cell_len = 0;
2215
2216 /* Compute the length in display cells. */
2217 for (n = 0; n < len; n += MB_BYTE2LEN(text[n]))
2218 cell_len += (*mb_ptr2cells)(text + n);
2219 rc.right = FILL_X(col + cell_len);
2220 }
2221 else
2222#endif
2223 rc.right = FILL_X(col + len);
2224 rc.bottom = FILL_Y(row + 1);
2225
2226 /* Cache the created brush, that saves a lot of time. We need two:
2227 * one for cursor background and one for the normal background. */
2228 if (gui.currBgColor == brush_color[0])
2229 {
2230 hbr = hbr_cache[0];
2231 brush_lru = 1;
2232 }
2233 else if (gui.currBgColor == brush_color[1])
2234 {
2235 hbr = hbr_cache[1];
2236 brush_lru = 0;
2237 }
2238 else
2239 {
2240 if (hbr_cache[brush_lru] != NULL)
2241 DeleteBrush(hbr_cache[brush_lru]);
2242 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
2243 brush_color[brush_lru] = gui.currBgColor;
2244 hbr = hbr_cache[brush_lru];
2245 brush_lru = !brush_lru;
2246 }
2247 FillRect(s_hdc, &rc, hbr);
2248
2249 SetBkMode(s_hdc, TRANSPARENT);
2250
2251 /*
2252 * When drawing block cursor, prevent inverted character spilling
2253 * over character cell (can happen with bold/italic)
2254 */
2255 if (flags & DRAW_CURSOR)
2256 {
2257 pcliprect = &rc;
2258 foptions = ETO_CLIPPED;
2259 }
2260 }
2261#else
2262 /*
2263 * The alternative would be to write the characters in opaque mode, but
2264 * when the text is not exactly the same proportions as normal text, too
2265 * big or too little a rectangle gets drawn for the background.
2266 */
2267 SetBkMode(s_hdc, OPAQUE);
2268 SetBkColor(s_hdc, gui.currBgColor);
2269#endif
2270 SetTextColor(s_hdc, gui.currFgColor);
2271 SelectFont(s_hdc, gui.currFont);
2272
2273 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
2274 {
2275 vim_free(padding);
2276 pad_size = Columns;
2277
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002278 /* Don't give an out-of-memory message here, it would call us
2279 * recursively. */
2280 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 if (padding != NULL)
2282 for (i = 0; i < pad_size; i++)
2283 padding[i] = gui.char_width;
2284 }
2285
2286 /* On NT, tell the font renderer not to "help" us with Hebrew and Arabic
2287 * text. This doesn't work in 9x, so we have to deal with it manually on
2288 * those systems. */
2289 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
2290 foptions |= ETO_IGNORELANGUAGE;
2291
2292 /*
2293 * We have to provide the padding argument because italic and bold versions
2294 * of fixed-width fonts are often one pixel or so wider than their normal
2295 * versions.
2296 * No check for DRAW_BOLD, Windows will have done it already.
2297 */
2298
2299#ifdef FEAT_MBYTE
2300 /* Check if there are any UTF-8 characters. If not, use normal text
2301 * output to speed up output. */
2302 if (enc_utf8)
2303 for (n = 0; n < len; ++n)
2304 if (text[n] >= 0x80)
2305 break;
2306
2307 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002308 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002310 if ((enc_utf8
2311 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
2312 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 && (unicodebuf == NULL || len > unibuflen))
2314 {
2315 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002316 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317
2318 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002319 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320
2321 unibuflen = len;
2322 }
2323
2324 if (enc_utf8 && n < len && unicodebuf != NULL)
2325 {
2326 /* Output UTF-8 characters. Caller has already separated
2327 * composing characters. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002328 int i;
2329 int wlen; /* string length in words */
2330 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 int cells; /* cell width of string up to composing char */
2332 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002333 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002335 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002336 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002338 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002340 c = utf_ptr2char(text + i);
2341 if (c >= 0x10000)
2342 {
2343 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002344 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
2345 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002346 }
2347 else
2348 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002349 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002350 }
2351 cw = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 if (cw > 2) /* don't use 4 for unprintable char */
2353 cw = 1;
2354 if (unicodepdy != NULL)
2355 {
2356 /* Use unicodepdy to make characters fit as we expect, even
2357 * when the font uses different widths (e.g., bold character
2358 * is wider). */
2359 unicodepdy[clen] = cw * gui.char_width;
2360 }
2361 cells += cw;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002362 i += utfc_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00002363 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 }
2365 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaarca003e12006-03-17 23:19:38 +00002366 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 len = cells; /* used for underlining */
2368 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002369 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 {
2371 /* If we want to display codepage data, and the current CP is not the
2372 * ANSI one, we need to go via Unicode. */
2373 if (unicodebuf != NULL)
2374 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002375 if (enc_latin9)
2376 latin9_to_ucs(text, len, unicodebuf);
2377 else
2378 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 MB_PRECOMPOSED,
2380 (char *)text, len,
2381 (LPWSTR)unicodebuf, unibuflen);
2382 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002383 {
2384 /* Use unicodepdy to make characters fit as we expect, even
2385 * when the font uses different widths (e.g., bold character
2386 * is wider). */
2387 if (unicodepdy != NULL)
2388 {
2389 int i;
2390 int cw;
2391
2392 for (i = 0; i < len; ++i)
2393 {
2394 cw = utf_char2cells(unicodebuf[i]);
2395 if (cw > 2)
2396 cw = 1;
2397 unicodepdy[i] = cw * gui.char_width;
2398 }
2399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002401 foptions, pcliprect, unicodebuf, len, unicodepdy);
2402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
2404 }
2405 else
2406#endif
2407 {
2408#ifdef FEAT_RIGHTLEFT
2409 /* If we can't use ETO_IGNORELANGUAGE, we can't tell Windows not to
2410 * mess up RL text, so we have to draw it character-by-character.
2411 * Only do this if RL is on, since it's slow. */
2412 if (curwin->w_p_rl && !(foptions & ETO_IGNORELANGUAGE))
2413 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
2414 foptions, pcliprect, (char *)text, len, padding);
2415 else
2416#endif
2417 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
2418 foptions, pcliprect, (char *)text, len, padding);
2419 }
2420
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002421 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 if (flags & DRAW_UNDERL)
2423 {
2424 hpen = CreatePen(PS_SOLID, 1, gui.currFgColor);
2425 old_pen = SelectObject(s_hdc, hpen);
2426 /* When p_linespace is 0, overwrite the bottom row of pixels.
2427 * Otherwise put the line just below the character. */
2428 y = FILL_Y(row + 1) - 1;
2429#ifndef MSWIN16_FASTTEXT
2430 if (p_linespace > 1)
2431 y -= p_linespace - 1;
2432#endif
2433 MoveToEx(s_hdc, FILL_X(col), y, NULL);
2434 /* Note: LineTo() excludes the last pixel in the line. */
2435 LineTo(s_hdc, FILL_X(col + len), y);
2436 DeleteObject(SelectObject(s_hdc, old_pen));
2437 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002438
2439 /* Undercurl */
2440 if (flags & DRAW_UNDERC)
2441 {
2442 int x;
2443 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002444 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002445
2446 y = FILL_Y(row + 1) - 1;
2447 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
2448 {
2449 offset = val[x % 8];
2450 SetPixel(s_hdc, x, y - offset, gui.currSpColor);
2451 }
2452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453}
2454
2455
2456/*
2457 * Output routines.
2458 */
2459
2460/* Flush any output to the screen */
2461 void
2462gui_mch_flush(void)
2463{
2464# if defined(__BORLANDC__)
2465 /*
2466 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
2467 * prototype declaration.
2468 * The compiler complains if __stdcall is not used in both declarations.
2469 */
2470 BOOL __stdcall GdiFlush(void);
2471# endif
2472
2473 GdiFlush();
2474}
2475
2476 static void
2477clear_rect(RECT *rcp)
2478{
2479 HBRUSH hbr;
2480
2481 hbr = CreateSolidBrush(gui.back_pixel);
2482 FillRect(s_hdc, rcp, hbr);
2483 DeleteBrush(hbr);
2484}
2485
2486
Bram Moolenaarc716c302006-01-21 22:12:51 +00002487 void
2488gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
2489{
2490 RECT workarea_rect;
2491
2492 get_work_area(&workarea_rect);
2493
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002494 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002495 - GetSystemMetrics(SM_CXFRAME) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00002496
2497 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
2498 * the menubar for MSwin, we subtract it from the screen height, so that
2499 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002500 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002501 - GetSystemMetrics(SM_CYFRAME) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00002502 - GetSystemMetrics(SM_CYCAPTION)
2503#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002504 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00002505#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002506 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00002507}
2508
2509
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510#if defined(FEAT_MENU) || defined(PROTO)
2511/*
2512 * Add a sub menu to the menu bar.
2513 */
2514 void
2515gui_mch_add_menu(
2516 vimmenu_T *menu,
2517 int pos)
2518{
2519 vimmenu_T *parent = menu->parent;
2520
2521 menu->submenu_id = CreatePopupMenu();
2522 menu->id = s_menu_id++;
2523
2524 if (menu_is_menubar(menu->name))
2525 {
2526 if (is_winnt_3())
2527 {
2528 InsertMenu((parent == NULL) ? s_menuBar : parent->submenu_id,
2529 (UINT)pos, MF_POPUP | MF_STRING | MF_BYPOSITION,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002530 (long_u)menu->submenu_id, (LPCTSTR) menu->name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 }
2532 else
2533 {
2534#ifdef FEAT_MBYTE
2535 WCHAR *wn = NULL;
2536 int n;
2537
2538 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2539 {
2540 /* 'encoding' differs from active codepage: convert menu name
2541 * and use wide function */
2542 wn = enc_to_ucs2(menu->name, NULL);
2543 if (wn != NULL)
2544 {
2545 MENUITEMINFOW infow;
2546
2547 infow.cbSize = sizeof(infow);
2548 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
2549 | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002550 infow.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 infow.wID = menu->id;
2552 infow.fType = MFT_STRING;
2553 infow.dwTypeData = wn;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002554 infow.cch = (UINT)wcslen(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 infow.hSubMenu = menu->submenu_id;
2556 n = InsertMenuItemW((parent == NULL)
2557 ? s_menuBar : parent->submenu_id,
2558 (UINT)pos, TRUE, &infow);
2559 vim_free(wn);
2560 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2561 /* Failed, try using non-wide function. */
2562 wn = NULL;
2563 }
2564 }
2565
2566 if (wn == NULL)
2567#endif
2568 {
2569 MENUITEMINFO info;
2570
2571 info.cbSize = sizeof(info);
2572 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002573 info.dwItemData = (long_u)menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 info.wID = menu->id;
2575 info.fType = MFT_STRING;
2576 info.dwTypeData = (LPTSTR)menu->name;
2577 info.cch = (UINT)STRLEN(menu->name);
2578 info.hSubMenu = menu->submenu_id;
2579 InsertMenuItem((parent == NULL)
2580 ? s_menuBar : parent->submenu_id,
2581 (UINT)pos, TRUE, &info);
2582 }
2583 }
2584 }
2585
2586 /* Fix window size if menu may have wrapped */
2587 if (parent == NULL)
2588 gui_mswin_get_menu_height(!gui.starting);
2589#ifdef FEAT_TEAROFF
2590 else if (IsWindow(parent->tearoff_handle))
2591 rebuild_tearoff(parent);
2592#endif
2593}
2594
2595 void
2596gui_mch_show_popupmenu(vimmenu_T *menu)
2597{
2598 POINT mp;
2599
2600 (void)GetCursorPos((LPPOINT)&mp);
2601 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
2602}
2603
2604 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00002605gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606{
2607 vimmenu_T *menu = gui_find_menu(path_name);
2608
2609 if (menu != NULL)
2610 {
2611 POINT p;
2612
2613 /* Find the position of the current cursor */
2614 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00002615 if (mouse_pos)
2616 {
2617 int mx, my;
2618
2619 gui_mch_getmouse(&mx, &my);
2620 p.x += mx;
2621 p.y += my;
2622 }
2623 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 {
2625 p.x += TEXT_X(W_WINCOL(curwin) + curwin->w_wcol + 1);
2626 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
2627 }
2628 msg_scroll = FALSE;
2629 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
2630 }
2631}
2632
2633#if defined(FEAT_TEAROFF) || defined(PROTO)
2634/*
2635 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
2636 * create it as a pseudo-"tearoff menu".
2637 */
2638 void
2639gui_make_tearoff(char_u *path_name)
2640{
2641 vimmenu_T *menu = gui_find_menu(path_name);
2642
2643 /* Found the menu, so tear it off. */
2644 if (menu != NULL)
2645 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
2646}
2647#endif
2648
2649/*
2650 * Add a menu item to a menu
2651 */
2652 void
2653gui_mch_add_menu_item(
2654 vimmenu_T *menu,
2655 int idx)
2656{
2657 vimmenu_T *parent = menu->parent;
2658
2659 menu->id = s_menu_id++;
2660 menu->submenu_id = NULL;
2661
2662#ifdef FEAT_TEAROFF
2663 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
2664 {
2665 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
2666 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
2667 }
2668 else
2669#endif
2670#ifdef FEAT_TOOLBAR
2671 if (menu_is_toolbar(parent->name))
2672 {
2673 TBBUTTON newtb;
2674
2675 vim_memset(&newtb, 0, sizeof(newtb));
2676 if (menu_is_separator(menu->name))
2677 {
2678 newtb.iBitmap = 0;
2679 newtb.fsStyle = TBSTYLE_SEP;
2680 }
2681 else
2682 {
2683 newtb.iBitmap = get_toolbar_bitmap(menu);
2684 newtb.fsStyle = TBSTYLE_BUTTON;
2685 }
2686 newtb.idCommand = menu->id;
2687 newtb.fsState = TBSTATE_ENABLED;
2688 newtb.iString = 0;
2689 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
2690 (LPARAM)&newtb);
2691 menu->submenu_id = (HMENU)-1;
2692 }
2693 else
2694#endif
2695 {
2696#ifdef FEAT_MBYTE
2697 WCHAR *wn = NULL;
2698 int n;
2699
2700 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2701 {
2702 /* 'encoding' differs from active codepage: convert menu item name
2703 * and use wide function */
2704 wn = enc_to_ucs2(menu->name, NULL);
2705 if (wn != NULL)
2706 {
2707 n = InsertMenuW(parent->submenu_id, (UINT)idx,
2708 (menu_is_separator(menu->name)
2709 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
2710 (UINT)menu->id, wn);
2711 vim_free(wn);
2712 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2713 /* Failed, try using non-wide function. */
2714 wn = NULL;
2715 }
2716 }
2717 if (wn == NULL)
2718#endif
2719 InsertMenu(parent->submenu_id, (UINT)idx,
2720 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
2721 | MF_BYPOSITION,
2722 (UINT)menu->id, (LPCTSTR)menu->name);
2723#ifdef FEAT_TEAROFF
2724 if (IsWindow(parent->tearoff_handle))
2725 rebuild_tearoff(parent);
2726#endif
2727 }
2728}
2729
2730/*
2731 * Destroy the machine specific menu widget.
2732 */
2733 void
2734gui_mch_destroy_menu(vimmenu_T *menu)
2735{
2736#ifdef FEAT_TOOLBAR
2737 /*
2738 * is this a toolbar button?
2739 */
2740 if (menu->submenu_id == (HMENU)-1)
2741 {
2742 int iButton;
2743
2744 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
2745 (WPARAM)menu->id, 0);
2746 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
2747 }
2748 else
2749#endif
2750 {
2751 if (menu->parent != NULL
2752 && menu_is_popup(menu->parent->dname)
2753 && menu->parent->submenu_id != NULL)
2754 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
2755 else
2756 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
2757 if (menu->submenu_id != NULL)
2758 DestroyMenu(menu->submenu_id);
2759#ifdef FEAT_TEAROFF
2760 if (IsWindow(menu->tearoff_handle))
2761 DestroyWindow(menu->tearoff_handle);
2762 if (menu->parent != NULL
2763 && menu->parent->children != NULL
2764 && IsWindow(menu->parent->tearoff_handle))
2765 {
2766 /* This menu must not show up when rebuilding the tearoff window. */
2767 menu->modes = 0;
2768 rebuild_tearoff(menu->parent);
2769 }
2770#endif
2771 }
2772}
2773
2774#ifdef FEAT_TEAROFF
2775 static void
2776rebuild_tearoff(vimmenu_T *menu)
2777{
2778 /*hackish*/
2779 char_u tbuf[128];
2780 RECT trect;
2781 RECT rct;
2782 RECT roct;
2783 int x, y;
2784
2785 HWND thwnd = menu->tearoff_handle;
2786
2787 GetWindowText(thwnd, tbuf, 127);
2788 if (GetWindowRect(thwnd, &trect)
2789 && GetWindowRect(s_hwnd, &rct)
2790 && GetClientRect(s_hwnd, &roct))
2791 {
2792 x = trect.left - rct.left;
2793 y = (trect.top - rct.bottom + roct.bottom);
2794 }
2795 else
2796 {
2797 x = y = 0xffffL;
2798 }
2799 DestroyWindow(thwnd);
2800 if (menu->children != NULL)
2801 {
2802 gui_mch_tearoff(tbuf, menu, x, y);
2803 if (IsWindow(menu->tearoff_handle))
2804 (void) SetWindowPos(menu->tearoff_handle,
2805 NULL,
2806 (int)trect.left,
2807 (int)trect.top,
2808 0, 0,
2809 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
2810 }
2811}
2812#endif /* FEAT_TEAROFF */
2813
2814/*
2815 * Make a menu either grey or not grey.
2816 */
2817 void
2818gui_mch_menu_grey(
2819 vimmenu_T *menu,
2820 int grey)
2821{
2822#ifdef FEAT_TOOLBAR
2823 /*
2824 * is this a toolbar button?
2825 */
2826 if (menu->submenu_id == (HMENU)-1)
2827 {
2828 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
2829 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
2830 }
2831 else
2832#endif
2833 if (grey)
2834 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_GRAYED);
2835 else
2836 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2837
2838#ifdef FEAT_TEAROFF
2839 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
2840 {
2841 WORD menuID;
2842 HWND menuHandle;
2843
2844 /*
2845 * A tearoff button has changed state.
2846 */
2847 if (menu->children == NULL)
2848 menuID = (WORD)(menu->id);
2849 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002850 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
2852 if (menuHandle)
2853 EnableWindow(menuHandle, !grey);
2854
2855 }
2856#endif
2857}
2858
2859#endif /* FEAT_MENU */
2860
2861
2862/* define some macros used to make the dialogue creation more readable */
2863
2864#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
2865#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00002866#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867
2868#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2869/*
2870 * stuff for dialogs
2871 */
2872
2873/*
2874 * The callback routine used by all the dialogs. Very simple. First,
2875 * acknowledges the INITDIALOG message so that Windows knows to do standard
2876 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
2877 * pressed, return that button's ID - IDCANCEL (2), which is the button's
2878 * number.
2879 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002880/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 static LRESULT CALLBACK
2882dialog_callback(
2883 HWND hwnd,
2884 UINT message,
2885 WPARAM wParam,
2886 LPARAM lParam)
2887{
2888 if (message == WM_INITDIALOG)
2889 {
2890 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
2891 /* Set focus to the dialog. Set the default button, if specified. */
2892 (void)SetFocus(hwnd);
2893 if (dialog_default_button > IDCANCEL)
2894 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
2895 return FALSE;
2896 }
2897
2898 if (message == WM_COMMAND)
2899 {
2900 int button = LOWORD(wParam);
2901
2902 /* Don't end the dialog if something was selected that was
2903 * not a button.
2904 */
2905 if (button >= DLG_NONBUTTON_CONTROL)
2906 return TRUE;
2907
2908 /* If the edit box exists, copy the string. */
2909 if (s_textfield != NULL)
2910 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
2911 s_textfield, IOSIZE);
2912
2913 /*
2914 * Need to check for IDOK because if the user just hits Return to
2915 * accept the default value, some reason this is what we get.
2916 */
2917 if (button == IDOK)
2918 {
2919 if (dialog_default_button > IDCANCEL)
2920 EndDialog(hwnd, dialog_default_button);
2921 }
2922 else
2923 EndDialog(hwnd, button - IDCANCEL);
2924 return TRUE;
2925 }
2926
2927 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
2928 {
2929 EndDialog(hwnd, 0);
2930 return TRUE;
2931 }
2932 return FALSE;
2933}
2934
2935/*
2936 * Create a dialog dynamically from the parameter strings.
2937 * type = type of dialog (question, alert, etc.)
2938 * title = dialog title. may be NULL for default title.
2939 * message = text to display. Dialog sizes to accommodate it.
2940 * buttons = '\n' separated list of button captions, default first.
2941 * dfltbutton = number of default button.
2942 *
2943 * This routine returns 1 if the first button is pressed,
2944 * 2 for the second, etc.
2945 *
2946 * 0 indicates Esc was pressed.
2947 * -1 for unexpected error
2948 *
2949 * If stubbing out this fn, return 1.
2950 */
2951
2952static const char_u *dlg_icons[] = /* must match names in resource file */
2953{
2954 "IDR_VIM",
2955 "IDR_VIM_ERROR",
2956 "IDR_VIM_ALERT",
2957 "IDR_VIM_INFO",
2958 "IDR_VIM_QUESTION"
2959};
2960
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 int
2962gui_mch_dialog(
2963 int type,
2964 char_u *title,
2965 char_u *message,
2966 char_u *buttons,
2967 int dfltbutton,
2968 char_u *textfield)
2969{
2970 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00002971 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 int numButtons;
2973 int *buttonWidths, *buttonPositions;
2974 int buttonYpos;
2975 int nchar, i;
2976 DWORD lStyle;
2977 int dlgwidth = 0;
2978 int dlgheight;
2979 int editboxheight;
2980 int horizWidth = 0;
2981 int msgheight;
2982 char_u *pstart;
2983 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002984 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 char_u *tbuffer;
2986 RECT rect;
2987 HWND hwnd;
2988 HDC hdc;
2989 HFONT font, oldFont;
2990 TEXTMETRIC fontInfo;
2991 int fontHeight;
2992 int textWidth, minButtonWidth, messageWidth;
2993 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002994 int maxDialogHeight;
2995 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 int vertical;
2997 int dlgPaddingX;
2998 int dlgPaddingY;
2999#ifdef USE_SYSMENU_FONT
3000 LOGFONT lfSysmenu;
3001 int use_lfSysmenu = FALSE;
3002#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003003 garray_T ga;
3004 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005
3006#ifndef NO_CONSOLE
3007 /* Don't output anything in silent mode ("ex -s") */
3008 if (silent_mode)
3009 return dfltbutton; /* return default option */
3010#endif
3011
Bram Moolenaar748bf032005-02-02 23:04:36 +00003012#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 /* If there is no window yet, open it. */
3014 if (s_hwnd == NULL && gui_mch_init() == FAIL)
3015 return dfltbutton;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003016#else
3017 if (s_hwnd == NULL)
3018 get_dialog_font_metrics();
3019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020
3021 if ((type < 0) || (type > VIM_LAST_TYPE))
3022 type = 0;
3023
3024 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003025 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003026 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003027 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028
3029 if (p == NULL)
3030 return -1;
3031
3032 /*
3033 * make a copy of 'buttons' to fiddle with it. complier grizzles because
3034 * vim_strsave() doesn't take a const arg (why not?), so cast away the
3035 * const.
3036 */
3037 tbuffer = vim_strsave(buttons);
3038 if (tbuffer == NULL)
3039 return -1;
3040
3041 --dfltbutton; /* Change from one-based to zero-based */
3042
3043 /* Count buttons */
3044 numButtons = 1;
3045 for (i = 0; tbuffer[i] != '\0'; i++)
3046 {
3047 if (tbuffer[i] == DLG_BUTTON_SEP)
3048 numButtons++;
3049 }
3050 if (dfltbutton >= numButtons)
3051 dfltbutton = -1;
3052
3053 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003054 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 if (buttonWidths == NULL)
3056 return -1;
3057
3058 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003059 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 if (buttonPositions == NULL)
3061 return -1;
3062
3063 /*
3064 * Calculate how big the dialog must be.
3065 */
3066 hwnd = GetDesktopWindow();
3067 hdc = GetWindowDC(hwnd);
3068#ifdef USE_SYSMENU_FONT
3069 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
3070 {
3071 font = CreateFontIndirect(&lfSysmenu);
3072 use_lfSysmenu = TRUE;
3073 }
3074 else
3075#endif
3076 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3077 VARIABLE_PITCH , DLG_FONT_NAME);
3078 if (s_usenewlook)
3079 {
3080 oldFont = SelectFont(hdc, font);
3081 dlgPaddingX = DLG_PADDING_X;
3082 dlgPaddingY = DLG_PADDING_Y;
3083 }
3084 else
3085 {
3086 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
3087 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
3088 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
3089 }
3090 GetTextMetrics(hdc, &fontInfo);
3091 fontHeight = fontInfo.tmHeight;
3092
3093 /* Minimum width for horizontal button */
3094 minButtonWidth = GetTextWidth(hdc, "Cancel", 6);
3095
3096 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003097 if (s_hwnd == NULL)
3098 {
3099 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100
Bram Moolenaarc716c302006-01-21 22:12:51 +00003101 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003102 get_work_area(&workarea_rect);
3103 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
3104 if (maxDialogWidth > 600)
3105 maxDialogWidth = 600;
3106 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 100;
3107 }
3108 else
3109 {
3110 /* Use our own window for the size, unless it's very small. */
3111 GetWindowRect(s_hwnd, &rect);
3112 maxDialogWidth = rect.right - rect.left
3113 - GetSystemMetrics(SM_CXFRAME) * 2;
3114 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
3115 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003116
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003117 maxDialogHeight = rect.bottom - rect.top
3118 - GetSystemMetrics(SM_CXFRAME) * 2;
3119 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
3120 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
3121 }
3122
3123 /* Set dlgwidth to width of message.
3124 * Copy the message into "ga", changing NL to CR-NL and inserting line
3125 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 pstart = message;
3127 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003128 msgheight = 0;
3129 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 do
3131 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003132 msgheight += fontHeight; /* at least one line */
3133
3134 /* Need to figure out where to break the string. The system does it
3135 * at a word boundary, which would mean we can't compute the number of
3136 * wrapped lines. */
3137 textWidth = 0;
3138 last_white = NULL;
3139 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00003140 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003141#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003142 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003143#else
3144 l = 1;
3145#endif
3146 if (l == 1 && vim_iswhite(*pend)
3147 && textWidth > maxDialogWidth * 3 / 4)
3148 last_white = pend;
3149 textWidth += GetTextWidth(hdc, pend, l);
3150 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003151 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003152 /* Line will wrap. */
3153 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003154 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003155 textWidth = 0;
3156
3157 if (last_white != NULL)
3158 {
3159 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003160 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003161 pend = last_white + 1;
3162 last_white = NULL;
3163 }
3164 ga_append(&ga, '\r');
3165 ga_append(&ga, '\n');
3166 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003167 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003168
3169 while (--l >= 0)
3170 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003171 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003172 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003174
3175 ga_append(&ga, '\r');
3176 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 pstart = pend + 1;
3178 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003179
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003180 if (ga.ga_data != NULL)
3181 message = ga.ga_data;
3182
Bram Moolenaar748bf032005-02-02 23:04:36 +00003183 messageWidth += 10; /* roundoff space */
3184
3185 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
3186 if (msgheight > maxDialogHeight)
3187 {
3188 msgheight = maxDialogHeight;
3189 scroll_flag = WS_VSCROLL;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003190 messageWidth += GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192
3193 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003194 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195
3196 if (msgheight < DLG_ICON_HEIGHT)
3197 msgheight = DLG_ICON_HEIGHT;
3198
3199 /*
3200 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00003201 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00003203 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 if (!vertical)
3205 {
3206 // Place buttons horizontally if they fit.
3207 horizWidth = dlgPaddingX;
3208 pstart = tbuffer;
3209 i = 0;
3210 do
3211 {
3212 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
3213 if (pend == NULL)
3214 pend = pstart + STRLEN(pstart); // Last button name.
3215 textWidth = GetTextWidth(hdc, pstart, (int)(pend - pstart));
3216 if (textWidth < minButtonWidth)
3217 textWidth = minButtonWidth;
3218 textWidth += dlgPaddingX; /* Padding within button */
3219 buttonWidths[i] = textWidth;
3220 buttonPositions[i++] = horizWidth;
3221 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
3222 pstart = pend + 1;
3223 } while (*pend != NUL);
3224
3225 if (horizWidth > maxDialogWidth)
3226 vertical = TRUE; // Too wide to fit on the screen.
3227 else if (horizWidth > dlgwidth)
3228 dlgwidth = horizWidth;
3229 }
3230
3231 if (vertical)
3232 {
3233 // Stack buttons vertically.
3234 pstart = tbuffer;
3235 do
3236 {
3237 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
3238 if (pend == NULL)
3239 pend = pstart + STRLEN(pstart); // Last button name.
3240 textWidth = GetTextWidth(hdc, pstart, (int)(pend - pstart));
3241 textWidth += dlgPaddingX; /* Padding within button */
3242 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
3243 if (textWidth > dlgwidth)
3244 dlgwidth = textWidth;
3245 pstart = pend + 1;
3246 } while (*pend != NUL);
3247 }
3248
3249 if (dlgwidth < DLG_MIN_WIDTH)
3250 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
3251
3252 /* start to fill in the dlgtemplate information. addressing by WORDs */
3253 if (s_usenewlook)
3254 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
3255 else
3256 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
3257
3258 add_long(lStyle);
3259 add_long(0); // (lExtendedStyle)
3260 pnumitems = p; /*save where the number of items must be stored*/
3261 add_word(0); // NumberOfItems(will change later)
3262 add_word(10); // x
3263 add_word(10); // y
3264 add_word(PixelToDialogX(dlgwidth)); // cx
3265
3266 // Dialog height.
3267 if (vertical)
3268 dlgheight = msgheight + 2 * dlgPaddingY +
3269 DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
3270 else
3271 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
3272
3273 // Dialog needs to be taller if contains an edit box.
3274 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
3275 if (textfield != NULL)
3276 dlgheight += editboxheight;
3277
3278 add_word(PixelToDialogY(dlgheight));
3279
3280 add_word(0); // Menu
3281 add_word(0); // Class
3282
3283 /* copy the title of the dialog */
3284 nchar = nCopyAnsiToWideChar(p, (title ?
3285 (LPSTR)title :
3286 (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
3287 p += nchar;
3288
3289 if (s_usenewlook)
3290 {
3291 /* do the font, since DS_3DLOOK doesn't work properly */
3292#ifdef USE_SYSMENU_FONT
3293 if (use_lfSysmenu)
3294 {
3295 /* point size */
3296 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
3297 GetDeviceCaps(hdc, LOGPIXELSY));
3298 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
3299 }
3300 else
3301#endif
3302 {
3303 *p++ = DLG_FONT_POINT_SIZE; // point size
3304 nchar = nCopyAnsiToWideChar(p, TEXT(DLG_FONT_NAME));
3305 }
3306 p += nchar;
3307 }
3308
3309 buttonYpos = msgheight + 2 * dlgPaddingY;
3310
3311 if (textfield != NULL)
3312 buttonYpos += editboxheight;
3313
3314 pstart = tbuffer;
3315 if (!vertical)
3316 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
3317 for (i = 0; i < numButtons; i++)
3318 {
3319 /* get end of this button. */
3320 for ( pend = pstart;
3321 *pend && (*pend != DLG_BUTTON_SEP);
3322 pend++)
3323 ;
3324
3325 if (*pend)
3326 *pend = '\0';
3327
3328 /*
3329 * old NOTE:
3330 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
3331 * the focus to the first tab-able button and in so doing makes that
3332 * the default!! Grrr. Workaround: Make the default button the only
3333 * one with WS_TABSTOP style. Means user can't tab between buttons, but
3334 * he/she can use arrow keys.
3335 *
3336 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00003337 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 * dialog. Also needed for when the textfield is the default control.
3339 * It appears to work now (perhaps not on Win95?).
3340 */
3341 if (vertical)
3342 {
3343 p = add_dialog_element(p,
3344 (i == dfltbutton
3345 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
3346 PixelToDialogX(DLG_VERT_PADDING_X),
3347 PixelToDialogY(buttonYpos /* TBK */
3348 + 2 * fontHeight * i),
3349 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
3350 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
3351 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, pstart);
3352 }
3353 else
3354 {
3355 p = add_dialog_element(p,
3356 (i == dfltbutton
3357 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
3358 PixelToDialogX(horizWidth + buttonPositions[i]),
3359 PixelToDialogY(buttonYpos), /* TBK */
3360 PixelToDialogX(buttonWidths[i]),
3361 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
3362 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, pstart);
3363 }
3364 pstart = pend + 1; /*next button*/
3365 }
3366 *pnumitems += numButtons;
3367
3368 /* Vim icon */
3369 p = add_dialog_element(p, SS_ICON,
3370 PixelToDialogX(dlgPaddingX),
3371 PixelToDialogY(dlgPaddingY),
3372 PixelToDialogX(DLG_ICON_WIDTH),
3373 PixelToDialogY(DLG_ICON_HEIGHT),
3374 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
3375 dlg_icons[type]);
3376
Bram Moolenaar748bf032005-02-02 23:04:36 +00003377#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 /* Dialog message */
3379 p = add_dialog_element(p, SS_LEFT,
3380 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
3381 PixelToDialogY(dlgPaddingY),
3382 (WORD)(PixelToDialogX(messageWidth) + 1),
3383 PixelToDialogY(msgheight),
3384 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0082, message);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003385#else
3386 /* Dialog message */
3387 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
3388 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
3389 PixelToDialogY(dlgPaddingY),
3390 (WORD)(PixelToDialogX(messageWidth) + 1),
3391 PixelToDialogY(msgheight),
3392 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, message);
3393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
3395 /* Edit box */
3396 if (textfield != NULL)
3397 {
3398 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
3399 PixelToDialogX(2 * dlgPaddingX),
3400 PixelToDialogY(2 * dlgPaddingY + msgheight),
3401 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
3402 PixelToDialogY(fontHeight + dlgPaddingY),
3403 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, textfield);
3404 *pnumitems += 1;
3405 }
3406
3407 *pnumitems += 2;
3408
3409 SelectFont(hdc, oldFont);
3410 DeleteObject(font);
3411 ReleaseDC(hwnd, hdc);
3412
3413 /* Let the dialog_callback() function know which button to make default
3414 * If we have an edit box, make that the default. We also need to tell
3415 * dialog_callback() if this dialog contains an edit box or not. We do
3416 * this by setting s_textfield if it does.
3417 */
3418 if (textfield != NULL)
3419 {
3420 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
3421 s_textfield = textfield;
3422 }
3423 else
3424 {
3425 dialog_default_button = IDCANCEL + 1 + dfltbutton;
3426 s_textfield = NULL;
3427 }
3428
3429 /* show the dialog box modally and get a return value */
3430 nchar = (int)DialogBoxIndirect(
3431 s_hinst,
3432 (LPDLGTEMPLATE)pdlgtemplate,
3433 s_hwnd,
3434 (DLGPROC)dialog_callback);
3435
3436 LocalFree(LocalHandle(pdlgtemplate));
3437 vim_free(tbuffer);
3438 vim_free(buttonWidths);
3439 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003440 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441
3442 /* Focus back to our window (for when MDI is used). */
3443 (void)SetFocus(s_hwnd);
3444
3445 return nchar;
3446}
3447
3448#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003449
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450/*
3451 * Put a simple element (basic class) onto a dialog template in memory.
3452 * return a pointer to where the next item should be added.
3453 *
3454 * parameters:
3455 * lStyle = additional style flags
3456 * (be careful, NT3.51 & Win32s will ignore the new ones)
3457 * x,y = x & y positions IN DIALOG UNITS
3458 * w,h = width and height IN DIALOG UNITS
3459 * Id = ID used in messages
3460 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
3461 * caption = usually text or resource name
3462 *
3463 * TODO: use the length information noted here to enable the dialog creation
3464 * routines to work out more exactly how much memory they need to alloc.
3465 */
3466 static PWORD
3467add_dialog_element(
3468 PWORD p,
3469 DWORD lStyle,
3470 WORD x,
3471 WORD y,
3472 WORD w,
3473 WORD h,
3474 WORD Id,
3475 WORD clss,
3476 const char *caption)
3477{
3478 int nchar;
3479
3480 p = lpwAlign(p); /* Align to dword boundary*/
3481 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
3482 *p++ = LOWORD(lStyle);
3483 *p++ = HIWORD(lStyle);
3484 *p++ = 0; // LOWORD (lExtendedStyle)
3485 *p++ = 0; // HIWORD (lExtendedStyle)
3486 *p++ = x;
3487 *p++ = y;
3488 *p++ = w;
3489 *p++ = h;
3490 *p++ = Id; //9 or 10 words in all
3491
3492 *p++ = (WORD)0xffff;
3493 *p++ = clss; //2 more here
3494
3495 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption); //strlen(caption)+1
3496 p += nchar;
3497
3498 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
3499
3500 return p; //total = 15+ (strlen(caption)) words
3501 // = 30 + 2(strlen(caption) bytes reqd
3502}
3503
3504
3505/*
3506 * Helper routine. Take an input pointer, return closest pointer that is
3507 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
3508 */
3509 static LPWORD
3510lpwAlign(
3511 LPWORD lpIn)
3512{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003513 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003515 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 ul += 3;
3517 ul >>= 2;
3518 ul <<= 2;
3519 return (LPWORD)ul;
3520}
3521
3522/*
3523 * Helper routine. Takes second parameter as Ansi string, copies it to first
3524 * parameter as wide character (16-bits / char) string, and returns integer
3525 * number of wide characters (words) in string (including the trailing wide
3526 * char NULL). Partly taken from the Win32SDK samples.
3527 */
3528 static int
3529nCopyAnsiToWideChar(
3530 LPWORD lpWCStr,
3531 LPSTR lpAnsiIn)
3532{
3533 int nChar = 0;
3534#ifdef FEAT_MBYTE
3535 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
3536 int i;
3537 WCHAR *wn;
3538
3539 if (enc_codepage == 0 && (int)GetACP() != enc_codepage)
3540 {
3541 /* Not a codepage, use our own conversion function. */
3542 wn = enc_to_ucs2(lpAnsiIn, NULL);
3543 if (wn != NULL)
3544 {
3545 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003546 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 vim_free(wn);
3548 }
3549 }
3550 if (nChar == 0)
3551 /* Use Win32 conversion function. */
3552 nChar = MultiByteToWideChar(
3553 enc_codepage > 0 ? enc_codepage : CP_ACP,
3554 MB_PRECOMPOSED,
3555 lpAnsiIn, len,
3556 lpWCStr, len);
3557 for (i = 0; i < nChar; ++i)
3558 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
3559 lpWCStr[i] = (WORD)' ';
3560#else
3561 do
3562 {
3563 if (*lpAnsiIn == '\t')
3564 *lpWCStr++ = (WORD)' ';
3565 else
3566 *lpWCStr++ = (WORD)*lpAnsiIn;
3567 nChar++;
3568 } while (*lpAnsiIn++);
3569#endif
3570
3571 return nChar;
3572}
3573
3574
3575#ifdef FEAT_TEAROFF
3576/*
3577 * The callback function for all the modeless dialogs that make up the
3578 * "tearoff menus" Very simple - forward button presses (to fool Vim into
3579 * thinking its menus have been clicked), and go away when closed.
3580 */
3581 static LRESULT CALLBACK
3582tearoff_callback(
3583 HWND hwnd,
3584 UINT message,
3585 WPARAM wParam,
3586 LPARAM lParam)
3587{
3588 if (message == WM_INITDIALOG)
3589 return (TRUE);
3590
3591 /* May show the mouse pointer again. */
3592 HandleMouseHide(message, lParam);
3593
3594 if (message == WM_COMMAND)
3595 {
3596 if ((WORD)(LOWORD(wParam)) & 0x8000)
3597 {
3598 POINT mp;
3599 RECT rect;
3600
3601 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
3602 {
3603 (void)TrackPopupMenu(
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003604 (HMENU)(long_u)(LOWORD(wParam) ^ 0x8000),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 TPM_LEFTALIGN | TPM_LEFTBUTTON,
3606 (int)rect.right - 8,
3607 (int)mp.y,
3608 (int)0, /*reserved param*/
3609 s_hwnd,
3610 NULL);
3611 /*
3612 * NOTE: The pop-up menu can eat the mouse up event.
3613 * We deal with this in normal.c.
3614 */
3615 }
3616 }
3617 else
3618 /* Pass on messages to the main Vim window */
3619 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
3620 /*
3621 * Give main window the focus back: this is so after
3622 * choosing a tearoff button you can start typing again
3623 * straight away.
3624 */
3625 (void)SetFocus(s_hwnd);
3626 return TRUE;
3627 }
3628 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
3629 {
3630 DestroyWindow(hwnd);
3631 return TRUE;
3632 }
3633
3634 /* When moved around, give main window the focus back. */
3635 if (message == WM_EXITSIZEMOVE)
3636 (void)SetActiveWindow(s_hwnd);
3637
3638 return FALSE;
3639}
3640#endif
3641
3642
3643/*
3644 * Decide whether to use the "new look" (small, non-bold font) or the "old
3645 * look" (big, clanky font) for dialogs, and work out a few values for use
3646 * later accordingly.
3647 */
3648 static void
3649get_dialog_font_metrics(void)
3650{
3651 HDC hdc;
3652 HFONT hfontTools = 0;
3653 DWORD dlgFontSize;
3654 SIZE size;
3655#ifdef USE_SYSMENU_FONT
3656 LOGFONT lfSysmenu;
3657#endif
3658
3659 s_usenewlook = FALSE;
3660
3661 /*
3662 * For NT3.51 and Win32s, we stick with the old look
3663 * because it matches everything else.
3664 */
3665 if (!is_winnt_3())
3666 {
3667#ifdef USE_SYSMENU_FONT
3668 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
3669 hfontTools = CreateFontIndirect(&lfSysmenu);
3670 else
3671#endif
3672 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
3673 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
3674
3675 if (hfontTools)
3676 {
3677 hdc = GetDC(s_hwnd);
3678 SelectObject(hdc, hfontTools);
3679 /*
3680 * GetTextMetrics() doesn't return the right value in
3681 * tmAveCharWidth, so we have to figure out the dialog base units
3682 * ourselves.
3683 */
3684 GetTextExtentPoint(hdc,
3685 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
3686 52, &size);
3687 ReleaseDC(s_hwnd, hdc);
3688
3689 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
3690 s_dlgfntheight = (WORD)size.cy;
3691 s_usenewlook = TRUE;
3692 }
3693 }
3694
3695 if (!s_usenewlook)
3696 {
3697 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
3698 s_dlgfntwidth = LOWORD(dlgFontSize);
3699 s_dlgfntheight = HIWORD(dlgFontSize);
3700 }
3701}
3702
3703#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
3704/*
3705 * Create a pseudo-"tearoff menu" based on the child
3706 * items of a given menu pointer.
3707 */
3708 static void
3709gui_mch_tearoff(
3710 char_u *title,
3711 vimmenu_T *menu,
3712 int initX,
3713 int initY)
3714{
3715 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
3716 int template_len;
3717 int nchar, textWidth, submenuWidth;
3718 DWORD lStyle;
3719 DWORD lExtendedStyle;
3720 WORD dlgwidth;
3721 WORD menuID;
3722 vimmenu_T *pmenu;
3723 vimmenu_T *the_menu = menu;
3724 HWND hwnd;
3725 HDC hdc;
3726 HFONT font, oldFont;
3727 int col, spaceWidth, len;
3728 int columnWidths[2];
3729 char_u *label, *text;
3730 int acLen = 0;
3731 int nameLen;
3732 int padding0, padding1, padding2 = 0;
3733 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003734 int x;
3735 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736#ifdef USE_SYSMENU_FONT
3737 LOGFONT lfSysmenu;
3738 int use_lfSysmenu = FALSE;
3739#endif
3740
3741 /*
3742 * If this menu is already torn off, move it to the mouse position.
3743 */
3744 if (IsWindow(menu->tearoff_handle))
3745 {
3746 POINT mp;
3747 if (GetCursorPos((LPPOINT)&mp))
3748 {
3749 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
3750 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
3751 }
3752 return;
3753 }
3754
3755 /*
3756 * Create a new tearoff.
3757 */
3758 if (*title == MNU_HIDDEN_CHAR)
3759 title++;
3760
3761 /* Allocate memory to store the dialog template. It's made bigger when
3762 * needed. */
3763 template_len = DLG_ALLOC_SIZE;
3764 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
3765 if (p == NULL)
3766 return;
3767
3768 hwnd = GetDesktopWindow();
3769 hdc = GetWindowDC(hwnd);
3770#ifdef USE_SYSMENU_FONT
3771 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
3772 {
3773 font = CreateFontIndirect(&lfSysmenu);
3774 use_lfSysmenu = TRUE;
3775 }
3776 else
3777#endif
3778 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3779 VARIABLE_PITCH , DLG_FONT_NAME);
3780 if (s_usenewlook)
3781 oldFont = SelectFont(hdc, font);
3782 else
3783 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
3784
3785 /* Calculate width of a single space. Used for padding columns to the
3786 * right width. */
3787 spaceWidth = GetTextWidth(hdc, " ", 1);
3788
3789 /* Figure out max width of the text column, the accelerator column and the
3790 * optional submenu column. */
3791 submenuWidth = 0;
3792 for (col = 0; col < 2; col++)
3793 {
3794 columnWidths[col] = 0;
3795 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
3796 {
3797 /* Use "dname" here to compute the width of the visible text. */
3798 text = (col == 0) ? pmenu->dname : pmenu->actext;
3799 if (text != NULL && *text != NUL)
3800 {
3801 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
3802 if (textWidth > columnWidths[col])
3803 columnWidths[col] = textWidth;
3804 }
3805 if (pmenu->children != NULL)
3806 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
3807 }
3808 }
3809 if (columnWidths[1] == 0)
3810 {
3811 /* no accelerators */
3812 if (submenuWidth != 0)
3813 columnWidths[0] += submenuWidth;
3814 else
3815 columnWidths[0] += spaceWidth;
3816 }
3817 else
3818 {
3819 /* there is an accelerator column */
3820 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
3821 columnWidths[1] += submenuWidth;
3822 }
3823
3824 /*
3825 * Now find the total width of our 'menu'.
3826 */
3827 textWidth = columnWidths[0] + columnWidths[1];
3828 if (submenuWidth != 0)
3829 {
3830 submenuWidth = GetTextWidth(hdc, TEAROFF_SUBMENU_LABEL,
3831 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
3832 textWidth += submenuWidth;
3833 }
3834 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
3835 if (textWidth > dlgwidth)
3836 dlgwidth = textWidth;
3837 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
3838
3839 /* W95 can't do thin dialogs, they look v. weird! */
3840 if (mch_windows95() && dlgwidth < TEAROFF_MIN_WIDTH)
3841 dlgwidth = TEAROFF_MIN_WIDTH;
3842
3843 /* start to fill in the dlgtemplate information. addressing by WORDs */
3844 if (s_usenewlook)
3845 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
3846 else
3847 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
3848
3849 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
3850 *p++ = LOWORD(lStyle);
3851 *p++ = HIWORD(lStyle);
3852 *p++ = LOWORD(lExtendedStyle);
3853 *p++ = HIWORD(lExtendedStyle);
3854 pnumitems = p; /* save where the number of items must be stored */
3855 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003856 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003858 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 else
3860 *p++ = PixelToDialogX(initX); // x
3861 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003862 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 else
3864 *p++ = PixelToDialogY(initY); // y
3865 *p++ = PixelToDialogX(dlgwidth); // cx
3866 ptrueheight = p;
3867 *p++ = 0; // dialog height: changed later anyway
3868 *p++ = 0; // Menu
3869 *p++ = 0; // Class
3870
3871 /* copy the title of the dialog */
3872 nchar = nCopyAnsiToWideChar(p, ((*title)
3873 ? (LPSTR)title
3874 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
3875 p += nchar;
3876
3877 if (s_usenewlook)
3878 {
3879 /* do the font, since DS_3DLOOK doesn't work properly */
3880#ifdef USE_SYSMENU_FONT
3881 if (use_lfSysmenu)
3882 {
3883 /* point size */
3884 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
3885 GetDeviceCaps(hdc, LOGPIXELSY));
3886 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
3887 }
3888 else
3889#endif
3890 {
3891 *p++ = DLG_FONT_POINT_SIZE; // point size
3892 nchar = nCopyAnsiToWideChar (p, TEXT(DLG_FONT_NAME));
3893 }
3894 p += nchar;
3895 }
3896
3897 /*
3898 * Loop over all the items in the menu.
3899 * But skip over the tearbar.
3900 */
3901 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
3902 menu = menu->children->next;
3903 else
3904 menu = menu->children;
3905 for ( ; menu != NULL; menu = menu->next)
3906 {
3907 if (menu->modes == 0) /* this menu has just been deleted */
3908 continue;
3909 if (menu_is_separator(menu->dname))
3910 {
3911 sepPadding += 3;
3912 continue;
3913 }
3914
3915 /* Check if there still is plenty of room in the template. Make it
3916 * larger when needed. */
3917 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
3918 {
3919 WORD *newp;
3920
3921 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
3922 if (newp != NULL)
3923 {
3924 template_len += 4096;
3925 mch_memmove(newp, pdlgtemplate,
3926 (char *)p - (char *)pdlgtemplate);
3927 p = newp + (p - pdlgtemplate);
3928 pnumitems = newp + (pnumitems - pdlgtemplate);
3929 ptrueheight = newp + (ptrueheight - pdlgtemplate);
3930 LocalFree(LocalHandle(pdlgtemplate));
3931 pdlgtemplate = newp;
3932 }
3933 }
3934
3935 /* Figure out minimal length of this menu label. Use "name" for the
3936 * actual text, "dname" for estimating the displayed size. "name"
3937 * has "&a" for mnemonic and includes the accelerator. */
3938 len = nameLen = (int)STRLEN(menu->name);
3939 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
3940 (int)STRLEN(menu->dname))) / spaceWidth;
3941 len += padding0;
3942
3943 if (menu->actext != NULL)
3944 {
3945 acLen = (int)STRLEN(menu->actext);
3946 len += acLen;
3947 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
3948 }
3949 else
3950 textWidth = 0;
3951 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
3952 len += padding1;
3953
3954 if (menu->children == NULL)
3955 {
3956 padding2 = submenuWidth / spaceWidth;
3957 len += padding2;
3958 menuID = (WORD)(menu->id);
3959 }
3960 else
3961 {
3962 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003963 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 }
3965
3966 /* Allocate menu label and fill it in */
3967 text = label = alloc((unsigned)len + 1);
3968 if (label == NULL)
3969 break;
3970
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003971 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 text = vim_strchr(text, TAB); /* stop at TAB before actext */
3973 if (text == NULL)
3974 text = label + nameLen; /* no actext, use whole name */
3975 while (padding0-- > 0)
3976 *text++ = ' ';
3977 if (menu->actext != NULL)
3978 {
3979 STRNCPY(text, menu->actext, acLen);
3980 text += acLen;
3981 }
3982 while (padding1-- > 0)
3983 *text++ = ' ';
3984 if (menu->children != NULL)
3985 {
3986 STRCPY(text, TEAROFF_SUBMENU_LABEL);
3987 text += STRLEN(TEAROFF_SUBMENU_LABEL);
3988 }
3989 else
3990 {
3991 while (padding2-- > 0)
3992 *text++ = ' ';
3993 }
3994 *text = NUL;
3995
3996 /*
3997 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
3998 * W95/NT4 it makes the tear-off look more like a menu.
3999 */
4000 p = add_dialog_element(p,
4001 BS_PUSHBUTTON|BS_LEFT,
4002 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
4003 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
4004 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
4005 (WORD)12,
4006 menuID, (WORD)0x0080, label);
4007 vim_free(label);
4008 (*pnumitems)++;
4009 }
4010
4011 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
4012
4013
4014 /* show modelessly */
4015 the_menu->tearoff_handle = CreateDialogIndirect(
4016 s_hinst,
4017 (LPDLGTEMPLATE)pdlgtemplate,
4018 s_hwnd,
4019 (DLGPROC)tearoff_callback);
4020
4021 LocalFree(LocalHandle(pdlgtemplate));
4022 SelectFont(hdc, oldFont);
4023 DeleteObject(font);
4024 ReleaseDC(hwnd, hdc);
4025
4026 /*
4027 * Reassert ourselves as the active window. This is so that after creating
4028 * a tearoff, the user doesn't have to click with the mouse just to start
4029 * typing again!
4030 */
4031 (void)SetActiveWindow(s_hwnd);
4032
4033 /* make sure the right buttons are enabled */
4034 force_menu_update = TRUE;
4035}
4036#endif
4037
4038#if defined(FEAT_TOOLBAR) || defined(PROTO)
4039#include "gui_w32_rc.h"
4040
4041/* This not defined in older SDKs */
4042# ifndef TBSTYLE_FLAT
4043# define TBSTYLE_FLAT 0x0800
4044# endif
4045
4046/*
4047 * Create the toolbar, initially unpopulated.
4048 * (just like the menu, there are no defaults, it's all
4049 * set up through menu.vim)
4050 */
4051 static void
4052initialise_toolbar(void)
4053{
4054 InitCommonControls();
4055 s_toolbarhwnd = CreateToolbarEx(
4056 s_hwnd,
4057 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
4058 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004059 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 s_hinst,
4061 IDR_TOOLBAR1, // id of initial bitmap
4062 NULL,
4063 0, // initial number of buttons
4064 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
4065 TOOLBAR_BUTTON_HEIGHT,
4066 TOOLBAR_BUTTON_WIDTH,
4067 TOOLBAR_BUTTON_HEIGHT,
4068 sizeof(TBBUTTON)
4069 );
4070
4071 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
4072}
4073
4074 static int
4075get_toolbar_bitmap(vimmenu_T *menu)
4076{
4077 int i = -1;
4078
4079 /*
4080 * Check user bitmaps first, unless builtin is specified.
4081 */
4082 if (!is_winnt_3() && !menu->icon_builtin)
4083 {
4084 char_u fname[MAXPATHL];
4085 HANDLE hbitmap = NULL;
4086
4087 if (menu->iconfile != NULL)
4088 {
4089 gui_find_iconfile(menu->iconfile, fname, "bmp");
4090 hbitmap = LoadImage(
4091 NULL,
4092 fname,
4093 IMAGE_BITMAP,
4094 TOOLBAR_BUTTON_WIDTH,
4095 TOOLBAR_BUTTON_HEIGHT,
4096 LR_LOADFROMFILE |
4097 LR_LOADMAP3DCOLORS
4098 );
4099 }
4100
4101 /*
4102 * If the LoadImage call failed, or the "icon=" file
4103 * didn't exist or wasn't specified, try the menu name
4104 */
4105 if (hbitmap == NULL
4106 && (gui_find_bitmap(menu->name, fname, "bmp") == OK))
4107 hbitmap = LoadImage(
4108 NULL,
4109 fname,
4110 IMAGE_BITMAP,
4111 TOOLBAR_BUTTON_WIDTH,
4112 TOOLBAR_BUTTON_HEIGHT,
4113 LR_LOADFROMFILE |
4114 LR_LOADMAP3DCOLORS
4115 );
4116
4117 if (hbitmap != NULL)
4118 {
4119 TBADDBITMAP tbAddBitmap;
4120
4121 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004122 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123
4124 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
4125 (WPARAM)1, (LPARAM)&tbAddBitmap);
4126 /* i will be set to -1 if it fails */
4127 }
4128 }
4129 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
4130 i = menu->iconidx;
4131
4132 return i;
4133}
4134#endif
4135
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004136#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
4137 static void
4138initialise_tabline(void)
4139{
4140 InitCommonControls();
4141
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004142 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004143 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004144 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4145 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004146
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004147 gui.tabline_height = TABLINE_HEIGHT;
4148
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004149# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004150 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004151# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004152}
4153#endif
4154
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
4156/*
4157 * Make the GUI window come to the foreground.
4158 */
4159 void
4160gui_mch_set_foreground(void)
4161{
4162 if (IsIconic(s_hwnd))
4163 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
4164 SetForegroundWindow(s_hwnd);
4165}
4166#endif
4167
4168#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4169 static void
4170dyn_imm_load(void)
4171{
4172 hLibImm = LoadLibrary("imm32.dll");
4173 if (hLibImm == NULL)
4174 return;
4175
4176 pImmGetCompositionStringA
4177 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
4178 pImmGetCompositionStringW
4179 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
4180 pImmGetContext
4181 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
4182 pImmAssociateContext
4183 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
4184 pImmReleaseContext
4185 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
4186 pImmGetOpenStatus
4187 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
4188 pImmSetOpenStatus
4189 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
4190 pImmGetCompositionFont
4191 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
4192 pImmSetCompositionFont
4193 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
4194 pImmSetCompositionWindow
4195 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
4196 pImmGetConversionStatus
4197 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00004198 pImmSetConversionStatus
4199 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200
4201 if ( pImmGetCompositionStringA == NULL
4202 || pImmGetCompositionStringW == NULL
4203 || pImmGetContext == NULL
4204 || pImmAssociateContext == NULL
4205 || pImmReleaseContext == NULL
4206 || pImmGetOpenStatus == NULL
4207 || pImmSetOpenStatus == NULL
4208 || pImmGetCompositionFont == NULL
4209 || pImmSetCompositionFont == NULL
4210 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00004211 || pImmGetConversionStatus == NULL
4212 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 {
4214 FreeLibrary(hLibImm);
4215 hLibImm = NULL;
4216 pImmGetContext = NULL;
4217 return;
4218 }
4219
4220 return;
4221}
4222
4223# if 0 /* not used */
4224 int
4225dyn_imm_unload(void)
4226{
4227 if (!hLibImm)
4228 return FALSE;
4229 FreeLibrary(hLibImm);
4230 hLibImm = NULL;
4231 return TRUE;
4232}
4233# endif
4234
4235#endif
4236
4237#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
4238
4239# ifdef FEAT_XPM_W32
4240# define IMAGE_XPM 100
4241# endif
4242
4243typedef struct _signicon_t
4244{
4245 HANDLE hImage;
4246 UINT uType;
4247#ifdef FEAT_XPM_W32
4248 HANDLE hShape; /* Mask bitmap handle */
4249#endif
4250} signicon_t;
4251
4252 void
4253gui_mch_drawsign(row, col, typenr)
4254 int row;
4255 int col;
4256 int typenr;
4257{
4258 signicon_t *sign;
4259 int x, y, w, h;
4260
4261 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
4262 return;
4263
4264 x = TEXT_X(col);
4265 y = TEXT_Y(row);
4266 w = gui.char_width * 2;
4267 h = gui.char_height;
4268 switch (sign->uType)
4269 {
4270 case IMAGE_BITMAP:
4271 {
4272 HDC hdcMem;
4273 HBITMAP hbmpOld;
4274
4275 hdcMem = CreateCompatibleDC(s_hdc);
4276 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
4277 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
4278 SelectObject(hdcMem, hbmpOld);
4279 DeleteDC(hdcMem);
4280 }
4281 break;
4282 case IMAGE_ICON:
4283 case IMAGE_CURSOR:
4284 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
4285 break;
4286#ifdef FEAT_XPM_W32
4287 case IMAGE_XPM:
4288 {
4289 HDC hdcMem;
4290 HBITMAP hbmpOld;
4291
4292 hdcMem = CreateCompatibleDC(s_hdc);
4293 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
4294 /* Make hole */
4295 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
4296
4297 SelectObject(hdcMem, sign->hImage);
4298 /* Paint sign */
4299 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
4300 SelectObject(hdcMem, hbmpOld);
4301 DeleteDC(hdcMem);
4302 }
4303 break;
4304#endif
4305 }
4306}
4307
4308 static void
4309close_signicon_image(signicon_t *sign)
4310{
4311 if (sign)
4312 switch (sign->uType)
4313 {
4314 case IMAGE_BITMAP:
4315 DeleteObject((HGDIOBJ)sign->hImage);
4316 break;
4317 case IMAGE_CURSOR:
4318 DestroyCursor((HCURSOR)sign->hImage);
4319 break;
4320 case IMAGE_ICON:
4321 DestroyIcon((HICON)sign->hImage);
4322 break;
4323#ifdef FEAT_XPM_W32
4324 case IMAGE_XPM:
4325 DeleteObject((HBITMAP)sign->hImage);
4326 DeleteObject((HBITMAP)sign->hShape);
4327 break;
4328#endif
4329 }
4330}
4331
4332 void *
4333gui_mch_register_sign(signfile)
4334 char_u *signfile;
4335{
4336 signicon_t sign, *psign;
4337 char_u *ext;
4338
4339 if (is_winnt_3())
4340 {
4341 EMSG(_(e_signdata));
4342 return NULL;
4343 }
4344
4345 sign.hImage = NULL;
4346 ext = signfile + STRLEN(signfile) - 4; /* get extention */
4347 if (ext > signfile)
4348 {
4349 int do_load = 1;
4350
4351 if (!STRICMP(ext, ".bmp"))
4352 sign.uType = IMAGE_BITMAP;
4353 else if (!STRICMP(ext, ".ico"))
4354 sign.uType = IMAGE_ICON;
4355 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
4356 sign.uType = IMAGE_CURSOR;
4357 else
4358 do_load = 0;
4359
4360 if (do_load)
4361 sign.hImage = (HANDLE)LoadImage(NULL, signfile, sign.uType,
4362 gui.char_width * 2, gui.char_height,
4363 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
4364#ifdef FEAT_XPM_W32
4365 if (!STRICMP(ext, ".xpm"))
4366 {
4367 sign.uType = IMAGE_XPM;
4368 LoadXpmImage(signfile, (HBITMAP *)&sign.hImage, (HBITMAP *)&sign.hShape);
4369 }
4370#endif
4371 }
4372
4373 psign = NULL;
4374 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
4375 != NULL)
4376 *psign = sign;
4377
4378 if (!psign)
4379 {
4380 if (sign.hImage)
4381 close_signicon_image(&sign);
4382 EMSG(_(e_signdata));
4383 }
4384 return (void *)psign;
4385
4386}
4387
4388 void
4389gui_mch_destroy_sign(sign)
4390 void *sign;
4391{
4392 if (sign)
4393 {
4394 close_signicon_image((signicon_t *)sign);
4395 vim_free(sign);
4396 }
4397}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399
4400#if defined(FEAT_BEVAL) || defined(PROTO)
4401
4402/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004403 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 *
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004405 * The only reused thing is gui_beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 * from gui_beval.c (note it uses x and y of the BalloonEval struct
4407 * to get current mouse position).
4408 *
4409 * Trying to use as more Windows services as possible, and as less
4410 * IE version as possible :)).
4411 *
4412 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
4413 * BalloonEval struct.
4414 * 2) Enable/Disable simply create/kill BalloonEval Timer
4415 * 3) When there was enough inactivity, timer procedure posts
4416 * async request to debugger
4417 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
4418 * and performs some actions to show it ASAP
4419 * 5) WM_NOTOFY:TTN_POP destroys created tooltip
4420 */
4421
Bram Moolenaar45360022005-07-21 21:08:21 +00004422/*
4423 * determine whether installed Common Controls support multiline tooltips
4424 * (i.e. their version is >= 4.70
4425 */
4426 int
4427multiline_balloon_available(void)
4428{
4429 HINSTANCE hDll;
4430 static char comctl_dll[] = "comctl32.dll";
4431 static int multiline_tip = MAYBE;
4432
4433 if (multiline_tip != MAYBE)
4434 return multiline_tip;
4435
4436 hDll = GetModuleHandle(comctl_dll);
4437 if (hDll != NULL)
4438 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004439 DLLGETVERSIONPROC pGetVer;
4440 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00004441
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004442 if (pGetVer != NULL)
4443 {
4444 DLLVERSIONINFO dvi;
4445 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00004446
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004447 ZeroMemory(&dvi, sizeof(dvi));
4448 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00004449
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004450 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00004451
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004452 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00004453 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004454 || (dvi.dwMajorVersion == 4
4455 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00004456 {
4457 multiline_tip = TRUE;
4458 return multiline_tip;
4459 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004460 }
Bram Moolenaar45360022005-07-21 21:08:21 +00004461 else
4462 {
4463 /* there is chance we have ancient CommCtl 4.70
4464 which doesn't export DllGetVersion */
4465 DWORD dwHandle = 0;
4466 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
4467 if (len > 0)
4468 {
4469 VS_FIXEDFILEINFO *ver;
4470 UINT vlen = 0;
4471 void *data = alloc(len);
4472
4473 if (data != NULL
4474 && GetFileVersionInfo(comctl_dll, 0, len, data)
4475 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
4476 && vlen
4477 && HIWORD(ver->dwFileVersionMS) > 4
4478 || (HIWORD(ver->dwFileVersionMS) == 4
4479 && LOWORD(ver->dwFileVersionMS) >= 70))
4480 {
4481 vim_free(data);
4482 multiline_tip = TRUE;
4483 return multiline_tip;
4484 }
4485 vim_free(data);
4486 }
4487 }
4488 }
4489 multiline_tip = FALSE;
4490 return multiline_tip;
4491}
4492
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 static void
4494make_tooltip(beval, text, pt)
4495 BalloonEval *beval;
4496 char *text;
4497 POINT pt;
4498{
Bram Moolenaar45360022005-07-21 21:08:21 +00004499 TOOLINFO *pti;
4500 int ToolInfoSize;
4501
4502 if (multiline_balloon_available() == TRUE)
4503 ToolInfoSize = sizeof(TOOLINFO_NEW);
4504 else
4505 ToolInfoSize = sizeof(TOOLINFO);
4506
4507 pti = (TOOLINFO *)alloc(ToolInfoSize);
4508 if (pti == NULL)
4509 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510
4511 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
4512 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
4513 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4514 beval->target, NULL, s_hinst, NULL);
4515
4516 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
4517 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
4518
Bram Moolenaar45360022005-07-21 21:08:21 +00004519 pti->cbSize = ToolInfoSize;
4520 pti->uFlags = TTF_SUBCLASS;
4521 pti->hwnd = beval->target;
4522 pti->hinst = 0; /* Don't use string resources */
4523 pti->uId = ID_BEVAL_TOOLTIP;
4524
4525 if (multiline_balloon_available() == TRUE)
4526 {
4527 RECT rect;
4528 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
4529 pti->lpszText = LPSTR_TEXTCALLBACK;
4530 ptin->lParam = (LPARAM)text;
4531 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
4532 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
4533 (LPARAM)rect.right);
4534 }
4535 else
4536 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537
4538 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00004539 pti->rect.left = pt.x - 3;
4540 pti->rect.top = pt.y - 3;
4541 pti->rect.right = pt.x + 3;
4542 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543
Bram Moolenaar45360022005-07-21 21:08:21 +00004544 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 /* Make tooltip appear sooner */
4546 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
4547 /*
4548 * HACK: force tooltip to appear, because it'll not appear until
4549 * first mouse move. D*mn M$
4550 */
4551 mouse_event(MOUSEEVENTF_MOVE, 1, 1, 0, 0);
4552 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00004553 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554}
4555
4556 static void
4557delete_tooltip(beval)
4558 BalloonEval *beval;
4559{
4560 DestroyWindow(beval->balloon);
4561}
4562
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004563/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 static VOID CALLBACK
4565BevalTimerProc(hwnd, uMsg, idEvent, dwTime)
4566 HWND hwnd;
4567 UINT uMsg;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004568 UINT_PTR idEvent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 DWORD dwTime;
4570{
4571 POINT pt;
4572 RECT rect;
4573
4574 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
4575 return;
4576
4577 GetCursorPos(&pt);
4578 if (WindowFromPoint(pt) != s_textArea)
4579 return;
4580
4581 ScreenToClient(s_textArea, &pt);
4582 GetClientRect(s_textArea, &rect);
4583 if (!PtInRect(&rect, pt))
4584 return;
4585
4586 if (LastActivity > 0
4587 && (dwTime - LastActivity) >= (DWORD)p_bdlay
4588 && (cur_beval->showState != ShS_PENDING
4589 || abs(cur_beval->x - pt.x) > 3
4590 || abs(cur_beval->y - pt.y) > 3))
4591 {
4592 /* Pointer resting in one place long enough, it's time to show
4593 * the tooltip. */
4594 cur_beval->showState = ShS_PENDING;
4595 cur_beval->x = pt.x;
4596 cur_beval->y = pt.y;
4597
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004598 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599
4600 if (cur_beval->msgCB != NULL)
4601 (*cur_beval->msgCB)(cur_beval, 0);
4602 }
4603}
4604
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004605/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 void
4607gui_mch_disable_beval_area(beval)
4608 BalloonEval *beval;
4609{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004610 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004612 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613}
4614
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004615/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 void
4617gui_mch_enable_beval_area(beval)
4618 BalloonEval *beval;
4619{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004620 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 if (beval == NULL)
4622 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004623 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004624 BevalTimerId = SetTimer(s_textArea, 0, p_bdlay / 2, BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004625 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626}
4627
4628 void
4629gui_mch_post_balloon(beval, mesg)
4630 BalloonEval *beval;
4631 char_u *mesg;
4632{
4633 POINT pt;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004634 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 if (beval->showState == ShS_SHOWING)
4636 return;
4637 GetCursorPos(&pt);
4638 ScreenToClient(s_textArea, &pt);
4639
4640 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
4641 /* cursor is still here */
4642 {
4643 gui_mch_disable_beval_area(cur_beval);
4644 beval->showState = ShS_SHOWING;
4645 make_tooltip(beval, mesg, pt);
4646 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004647 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648}
4649
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004650/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 BalloonEval *
4652gui_mch_create_beval_area(target, mesg, mesgCB, clientData)
4653 void *target; /* ignored, always use s_textArea */
4654 char_u *mesg;
4655 void (*mesgCB)__ARGS((BalloonEval *, int));
4656 void *clientData;
4657{
4658 /* partially stolen from gui_beval.c */
4659 BalloonEval *beval;
4660
4661 if (mesg != NULL && mesgCB != NULL)
4662 {
4663 EMSG(_("E232: Cannot create BalloonEval with both message and callback"));
4664 return NULL;
4665 }
4666
4667 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
4668 if (beval != NULL)
4669 {
4670 beval->target = s_textArea;
4671 beval->balloon = NULL;
4672
4673 beval->showState = ShS_NEUTRAL;
4674 beval->x = 0;
4675 beval->y = 0;
4676 beval->msg = mesg;
4677 beval->msgCB = mesgCB;
4678 beval->clientData = clientData;
4679
4680 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 cur_beval = beval;
4682
4683 if (p_beval)
4684 gui_mch_enable_beval_area(beval);
4685
4686 }
4687 return beval;
4688}
4689
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004690/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 static void
4692Handle_WM_Notify(hwnd, pnmh)
4693 HWND hwnd;
4694 LPNMHDR pnmh;
4695{
4696 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
4697 return;
4698
4699 if (cur_beval != NULL)
4700 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004701 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004703 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004704 // TRACE0("TTN_SHOW {{{");
4705 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00004706 break;
4707 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004708 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 delete_tooltip(cur_beval);
4710 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004711 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712
4713 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00004714 break;
4715 case TTN_GETDISPINFO:
4716 {
4717 /* if you get there then we have new common controls */
4718 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
4719 info->lpszText = (LPSTR)info->lParam;
4720 info->uFlags |= TTF_DI_SETITEM;
4721 }
4722 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 }
4724 }
4725}
4726
4727 static void
4728TrackUserActivity(UINT uMsg)
4729{
4730 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
4731 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
4732 LastActivity = GetTickCount();
4733}
4734
4735 void
4736gui_mch_destroy_beval_area(beval)
4737 BalloonEval *beval;
4738{
4739 vim_free(beval);
4740}
4741#endif /* FEAT_BEVAL */
4742
4743#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
4744/*
4745 * We have multiple signs to draw at the same location. Draw the
4746 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
4747 */
4748 void
4749netbeans_draw_multisign_indicator(int row)
4750{
4751 int i;
4752 int y;
4753 int x;
4754
4755 x = 0;
4756 y = TEXT_Y(row);
4757
4758 for (i = 0; i < gui.char_height - 3; i++)
4759 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
4760
4761 SetPixel(s_hdc, x+0, y, gui.currFgColor);
4762 SetPixel(s_hdc, x+2, y, gui.currFgColor);
4763 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
4764 SetPixel(s_hdc, x+1, y, gui.currFgColor);
4765 SetPixel(s_hdc, x+2, y, gui.currFgColor);
4766 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
4767 SetPixel(s_hdc, x+2, y, gui.currFgColor);
4768}
4769#endif