blob: b25f50db9ba8e1b708edb92998b905da972a3a81 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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
184static void make_tooltip __ARGS((BalloonEval *beval, char *text, POINT pt));
185static void delete_tooltip __ARGS((BalloonEval *beval));
186static VOID CALLBACK BevalTimerProc __ARGS((HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime));
187
188static BalloonEval *cur_beval = NULL;
189static UINT BevalTimerId = 0;
190static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +0000191
192/*
193 * excerpts from headers since this may not be presented
194 * in the extremelly old compilers
195 */
196#include <pshpack1.h>
197
198typedef struct _DllVersionInfo
199{
200 DWORD cbSize;
201 DWORD dwMajorVersion;
202 DWORD dwMinorVersion;
203 DWORD dwBuildNumber;
204 DWORD dwPlatformID;
205} DLLVERSIONINFO;
206
207typedef struct tagTOOLINFOA_NEW
208{
209 UINT cbSize;
210 UINT uFlags;
211 HWND hwnd;
212 UINT uId;
213 RECT rect;
214 HINSTANCE hinst;
215 LPSTR lpszText;
216 LPARAM lParam;
217} TOOLINFO_NEW;
218
219typedef struct tagNMTTDISPINFO_NEW
220{
221 NMHDR hdr;
222 LPTSTR lpszText;
223 char szText[80];
224 HINSTANCE hinst;
225 UINT uFlags;
226 LPARAM lParam;
227} NMTTDISPINFO_NEW;
228
229#include <poppack.h>
230
231typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
232#ifndef TTM_SETMAXTIPWIDTH
233# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234#endif
235
Bram Moolenaar45360022005-07-21 21:08:21 +0000236#ifndef TTF_DI_SETITEM
237# define TTF_DI_SETITEM 0x8000
238#endif
239
240#ifndef TTN_GETDISPINFO
241# define TTN_GETDISPINFO (TTN_FIRST - 0)
242#endif
243
244#endif /* defined(FEAT_BEVAL) */
245
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246/* Local variables: */
247
248#ifdef FEAT_MENU
249static UINT s_menu_id = 100;
250
251/*
252 * Use the system font for dialogs and tear-off menus. Remove this line to
253 * use DLG_FONT_NAME.
254 */
255# define USE_SYSMENU_FONT
256#endif
257
258#define VIM_NAME "vim"
259#define VIM_CLASS "Vim"
260#define VIM_CLASSW L"Vim"
261
262/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
263 * thus there should be room for every dialog. For tearoffs it's made bigger
264 * when needed. */
265#define DLG_ALLOC_SIZE 16 * 1024
266
267/*
268 * stuff for dialogs, menus, tearoffs etc.
269 */
270static LRESULT APIENTRY dialog_callback(HWND, UINT, WPARAM, LPARAM);
271static LRESULT APIENTRY tearoff_callback(HWND, UINT, WPARAM, LPARAM);
272static PWORD
273add_dialog_element(
274 PWORD p,
275 DWORD lStyle,
276 WORD x,
277 WORD y,
278 WORD w,
279 WORD h,
280 WORD Id,
281 WORD clss,
282 const char *caption);
283static LPWORD lpwAlign(LPWORD);
284static int nCopyAnsiToWideChar(LPWORD, LPSTR);
285static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
286static void get_dialog_font_metrics(void);
287
288static int dialog_default_button = -1;
289
290/* Intellimouse support */
291static int mouse_scroll_lines = 0;
292static UINT msh_msgmousewheel = 0;
293
294static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
295#ifdef FEAT_TOOLBAR
296static void initialise_toolbar(void);
297static int get_toolbar_bitmap(vimmenu_T *menu);
298#endif
299
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000300#ifdef FEAT_GUI_TABLINE
301static void initialise_tabline(void);
302#endif
303
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304#ifdef FEAT_MBYTE_IME
305static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
306static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
307#endif
308#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
309# ifdef NOIME
310typedef struct tagCOMPOSITIONFORM {
311 DWORD dwStyle;
312 POINT ptCurrentPos;
313 RECT rcArea;
314} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
315typedef HANDLE HIMC;
316# endif
317
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000318static HINSTANCE hLibImm = NULL;
319static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
320static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
321static HIMC (WINAPI *pImmGetContext)(HWND);
322static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
323static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
324static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
325static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
326static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
327static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
328static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
329static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +0000330static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331static void dyn_imm_load(void);
332#else
333# define pImmGetCompositionStringA ImmGetCompositionStringA
334# define pImmGetCompositionStringW ImmGetCompositionStringW
335# define pImmGetContext ImmGetContext
336# define pImmAssociateContext ImmAssociateContext
337# define pImmReleaseContext ImmReleaseContext
338# define pImmGetOpenStatus ImmGetOpenStatus
339# define pImmSetOpenStatus ImmSetOpenStatus
340# define pImmGetCompositionFont ImmGetCompositionFontA
341# define pImmSetCompositionFont ImmSetCompositionFontA
342# define pImmSetCompositionWindow ImmSetCompositionWindow
343# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +0000344# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345#endif
346
347#ifndef ETO_IGNORELANGUAGE
348# define ETO_IGNORELANGUAGE 0x1000
349#endif
350
351/* multi monitor support */
352typedef struct _MONITORINFOstruct
353{
354 DWORD cbSize;
355 RECT rcMonitor;
356 RECT rcWork;
357 DWORD dwFlags;
358} _MONITORINFO;
359
360typedef HANDLE _HMONITOR;
361typedef _HMONITOR (WINAPI *TMonitorFromWindow)(HWND, DWORD);
362typedef BOOL (WINAPI *TGetMonitorInfo)(_HMONITOR, _MONITORINFO *);
363
364static TMonitorFromWindow pMonitorFromWindow = NULL;
365static TGetMonitorInfo pGetMonitorInfo = NULL;
366static HANDLE user32_lib = NULL;
367#ifdef FEAT_NETBEANS_INTG
368int WSInitialized = FALSE; /* WinSock is initialized */
369#endif
370/*
371 * Return TRUE when running under Windows NT 3.x or Win32s, both of which have
372 * less fancy GUI APIs.
373 */
374 static int
375is_winnt_3(void)
376{
377 return ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
378 && os_version.dwMajorVersion == 3)
379 || (os_version.dwPlatformId == VER_PLATFORM_WIN32s));
380}
381
382/*
383 * Return TRUE when running under Win32s.
384 */
385 int
386gui_is_win32s(void)
387{
388 return (os_version.dwPlatformId == VER_PLATFORM_WIN32s);
389}
390
391#ifdef FEAT_MENU
392/*
393 * Figure out how high the menu bar is at the moment.
394 */
395 static int
396gui_mswin_get_menu_height(
397 int fix_window) /* If TRUE, resize window if menu height changed */
398{
399 static int old_menu_height = -1;
400
401 RECT rc1, rc2;
402 int num;
403 int menu_height;
404
405 if (gui.menu_is_active)
406 num = GetMenuItemCount(s_menuBar);
407 else
408 num = 0;
409
410 if (num == 0)
411 menu_height = 0;
412 else
413 {
414 if (is_winnt_3()) /* for NT 3.xx */
415 {
416 if (gui.starting)
417 menu_height = GetSystemMetrics(SM_CYMENU);
418 else
419 {
420 RECT r1, r2;
421 int frameht = GetSystemMetrics(SM_CYFRAME);
422 int capht = GetSystemMetrics(SM_CYCAPTION);
423
424 /* get window rect of s_hwnd
425 * get client rect of s_hwnd
426 * get cap height
427 * subtract from window rect, the sum of client height,
428 * (if not maximized)frame thickness, and caption height.
429 */
430 GetWindowRect(s_hwnd, &r1);
431 GetClientRect(s_hwnd, &r2);
432 menu_height = r1.bottom - r1.top - (r2.bottom - r2.top
433 + 2 * frameht * (!IsZoomed(s_hwnd)) + capht);
434 }
435 }
436 else /* win95 and variants (NT 4.0, I guess) */
437 {
438 /*
439 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
440 * seem to have been set yet, so menu wraps in default window
441 * width which is very narrow. Instead just return height of a
442 * single menu item. Will still be wrong when the menu really
443 * should wrap over more than one line.
444 */
445 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
446 if (gui.starting)
447 menu_height = rc1.bottom - rc1.top + 1;
448 else
449 {
450 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
451 menu_height = rc2.bottom - rc1.top + 1;
452 }
453 }
454 }
455
456 if (fix_window && menu_height != old_menu_height)
457 {
458 old_menu_height = menu_height;
459 gui_set_shellsize(FALSE, FALSE);
460 }
461
462 return menu_height;
463}
464#endif /*FEAT_MENU*/
465
466
467/*
468 * Setup for the Intellimouse
469 */
470 static void
471init_mouse_wheel(void)
472{
473
474#ifndef SPI_GETWHEELSCROLLLINES
475# define SPI_GETWHEELSCROLLLINES 104
476#endif
Bram Moolenaare7566042005-06-17 22:00:15 +0000477#ifndef SPI_SETWHEELSCROLLLINES
478# define SPI_SETWHEELSCROLLLINES 105
479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480
481#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
482#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
483#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
484#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
485
486 HWND hdl_mswheel;
487 UINT msh_msgscrolllines;
488
489 msh_msgmousewheel = 0;
490 mouse_scroll_lines = 3; /* reasonable default */
491
492 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
493 && os_version.dwMajorVersion >= 4)
494 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
495 && ((os_version.dwMajorVersion == 4
496 && os_version.dwMinorVersion >= 10)
497 || os_version.dwMajorVersion >= 5)))
498 {
499 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
500 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
501 &mouse_scroll_lines, 0);
502 }
503 else if (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
504 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
505 && os_version.dwMajorVersion < 4))
506 { /*
507 * If Win95 or NT 3.51,
508 * try to find the hidden point32 window.
509 */
510 hdl_mswheel = FindWindow(VMOUSEZ_CLASSNAME, VMOUSEZ_TITLE);
511 if (hdl_mswheel)
512 {
513 msh_msgscrolllines = RegisterWindowMessage(VMSH_SCROLL_LINES);
514 if (msh_msgscrolllines)
515 {
516 mouse_scroll_lines = (int)SendMessage(hdl_mswheel,
517 msh_msgscrolllines, 0, 0);
518 msh_msgmousewheel = RegisterWindowMessage(VMSH_MOUSEWHEEL);
519 }
520 }
521 }
522}
523
524
525/* Intellimouse wheel handler */
526 static void
527_OnMouseWheel(
528 HWND hwnd,
529 short zDelta)
530{
531/* Treat a mouse wheel event as if it were a scroll request */
532 int i;
533 int size;
534 HWND hwndCtl;
535
536 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
537 {
538 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
539 size = curwin->w_scrollbars[SBAR_RIGHT].size;
540 }
541 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
542 {
543 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
544 size = curwin->w_scrollbars[SBAR_LEFT].size;
545 }
546 else
547 return;
548
549 size = curwin->w_height;
550 if (mouse_scroll_lines == 0)
551 init_mouse_wheel();
552
553 if (mouse_scroll_lines > 0
554 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
555 {
556 for (i = mouse_scroll_lines; i > 0; --i)
557 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
558 }
559 else
560 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
561}
562
Bram Moolenaar520470a2005-06-16 21:59:56 +0000563/*
564 * Invoked when a setting was changed.
565 */
566 static LRESULT CALLBACK
567_OnSettingChange(UINT n)
568{
569 if (n == SPI_SETWHEELSCROLLLINES)
570 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
571 &mouse_scroll_lines, 0);
572 return 0;
573}
574
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575#if 0 /* disabled, a gap appears below and beside the window, and the window
576 can be moved (in a strange way) */
577/*
578 * Even though we have _DuringSizing() which makes the rubber band a valid
579 * size, we need this for when the user maximises the window.
580 * TODO: Doesn't seem to adjust the width though for some reason.
581 */
582 static BOOL
583_OnWindowPosChanging(
584 HWND hwnd,
585 LPWINDOWPOS lpwpos)
586{
587 RECT workarea_rect;
588
589 if (!(lpwpos->flags & SWP_NOSIZE))
590 {
591 if (IsMaximized(hwnd)
592 && (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
593 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
594 && os_version.dwMajorVersion >= 4)))
595 {
596 SystemParametersInfo(SPI_GETWORKAREA, 0, &workarea_rect, 0);
597 lpwpos->x = workarea_rect.left;
598 lpwpos->y = workarea_rect.top;
599 lpwpos->cx = workarea_rect.right - workarea_rect.left;
600 lpwpos->cy = workarea_rect.bottom - workarea_rect.top;
601 }
602 gui_mswin_get_valid_dimensions(lpwpos->cx, lpwpos->cy,
603 &lpwpos->cx, &lpwpos->cy);
604 }
605 return 0;
606}
607#endif
608
609#ifdef FEAT_NETBEANS_INTG
610 static void
611_OnWindowPosChanged(
612 HWND hwnd,
613 const LPWINDOWPOS lpwpos)
614{
615 static int x = 0, y = 0, cx = 0, cy = 0;
616
617 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
618 || lpwpos->cx != cx || lpwpos->cy != cy))
619 {
620 x = lpwpos->x;
621 y = lpwpos->y;
622 cx = lpwpos->cx;
623 cy = lpwpos->cy;
624 netbeans_frame_moved(x, y);
625 }
626 /* Allow to send WM_SIZE and WM_MOVE */
627 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
628}
629#endif
630
631 static int
632_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 UINT fwSide,
634 LPRECT lprc)
635{
636 int w, h;
637 int valid_w, valid_h;
638 int w_offset, h_offset;
639
640 w = lprc->right - lprc->left;
641 h = lprc->bottom - lprc->top;
642 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
643 w_offset = w - valid_w;
644 h_offset = h - valid_h;
645
646 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
647 || fwSide == WMSZ_BOTTOMLEFT)
648 lprc->left += w_offset;
649 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
650 || fwSide == WMSZ_BOTTOMRIGHT)
651 lprc->right -= w_offset;
652
653 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
654 || fwSide == WMSZ_TOPRIGHT)
655 lprc->top += h_offset;
656 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
657 || fwSide == WMSZ_BOTTOMRIGHT)
658 lprc->bottom -= h_offset;
659 return TRUE;
660}
661
662
663
664 static LRESULT CALLBACK
665_WndProc(
666 HWND hwnd,
667 UINT uMsg,
668 WPARAM wParam,
669 LPARAM lParam)
670{
671 /*
672 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
673 hwnd, uMsg, wParam, lParam);
674 */
675
676 HandleMouseHide(uMsg, lParam);
677
678 s_uMsg = uMsg;
679 s_wParam = wParam;
680 s_lParam = lParam;
681
682 switch (uMsg)
683 {
684 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
685 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
686 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
687 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
688 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
689 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
690 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
691 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
692 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
693#ifdef FEAT_MENU
694 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
695#endif
696 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
697 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
698 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
699 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
700 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
701 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
702 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
703 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
704 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
705#ifdef FEAT_NETBEANS_INTG
706 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
707#endif
708
709 case WM_QUERYENDSESSION: /* System wants to go down. */
710 gui_shell_closed(); /* Will exit when no changed buffers. */
711 return FALSE; /* Do NOT allow system to go down. */
712
713 case WM_ENDSESSION:
714 if (wParam) /* system only really goes down when wParam is TRUE */
715 _OnEndSession();
716 break;
717
718 case WM_CHAR:
719 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
720 * byte while we want the UTF-16 character value. */
721 _OnChar(hwnd, wParam, (int)(short)LOWORD(lParam));
722 return 0L;
723
724 case WM_SYSCHAR:
725 /*
726 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
727 * shortcut key, handle like a typed ALT key, otherwise call Windows
728 * ALT key handling.
729 */
730#ifdef FEAT_MENU
731 if ( !gui.menu_is_active
732 || p_wak[0] == 'n'
733 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
734 )
735#endif
736 {
737 _OnSysChar(hwnd, wParam, (int)(short)LOWORD(lParam));
738 return 0L;
739 }
740#ifdef FEAT_MENU
741 else
742 return MyWindowProc(hwnd, uMsg, wParam, lParam);
743#endif
744
745 case WM_SYSKEYUP:
746#ifdef FEAT_MENU
747 /* This used to be done only when menu is active: ALT key is used for
748 * that. But that caused problems when menu is disabled and using
749 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
750 * are received, mouse pointer remains hidden. */
751 return MyWindowProc(hwnd, uMsg, wParam, lParam);
752#else
753 return 0;
754#endif
755
756 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000757 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758
759 case WM_MOUSEWHEEL:
760 _OnMouseWheel(hwnd, HIWORD(wParam));
761 break;
762
Bram Moolenaar520470a2005-06-16 21:59:56 +0000763 /* Notification for change in SystemParametersInfo() */
764 case WM_SETTINGCHANGE:
765 return _OnSettingChange((UINT)wParam);
766
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000767#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 case WM_NOTIFY:
769 switch (((LPNMHDR) lParam)->code)
770 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000771# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 case TTN_NEEDTEXT:
773 {
774 LPTOOLTIPTEXT lpttt;
775 UINT idButton;
776 int idx;
777 vimmenu_T *pMenu;
778
779 lpttt = (LPTOOLTIPTEXT)lParam;
780 idButton = (UINT) lpttt->hdr.idFrom;
781 pMenu = gui_mswin_find_menu(root_menu, idButton);
782 if (pMenu)
783 {
784 idx = MENU_INDEX_TIP;
785 if (pMenu->strings[idx])
786 {
787 lpttt->hinst = NULL; /* string, not resource */
788 lpttt->lpszText = pMenu->strings[idx];
789 }
790 }
791 }
792 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000793# endif
794# ifdef FEAT_GUI_TABLINE
795 case TCN_SELCHANGE:
796 if (gui_mch_showing_tabline()
797 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
798 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
799 break;
800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000802# ifdef FEAT_GUI_TABLINE
803 if (gui_mch_showing_tabline()
804 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
805 return MyWindowProc(hwnd, uMsg, wParam, lParam);
806# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 break;
808 }
809 break;
810#endif
811#if defined(MENUHINTS) && defined(FEAT_MENU)
812 case WM_MENUSELECT:
813 if (((UINT) HIWORD(wParam)
814 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
815 == MF_HILITE
816 && (State & CMDLINE) == 0)
817 {
818 UINT idButton;
819 vimmenu_T *pMenu;
820 static int did_menu_tip = FALSE;
821
822 if (did_menu_tip)
823 {
824 msg_clr_cmdline();
825 setcursor();
826 out_flush();
827 did_menu_tip = FALSE;
828 }
829
830 idButton = (UINT)LOWORD(wParam);
831 pMenu = gui_mswin_find_menu(root_menu, idButton);
832 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
833 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
834 {
835 msg(pMenu->strings[MENU_INDEX_TIP]);
836 setcursor();
837 out_flush();
838 did_menu_tip = TRUE;
839 }
840 }
841 break;
842#endif
843 case WM_NCHITTEST:
844 {
845 LRESULT result;
846 int x, y;
847 int xPos = GET_X_LPARAM(lParam);
848
849 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
850 if (result == HTCLIENT)
851 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000852#ifdef FEAT_GUI_TABLINE
853 if (gui_mch_showing_tabline())
854 {
855 int yPos = GET_Y_LPARAM(lParam);
856 RECT rct;
857
858 /* If the cursor is on the GUI tabline, don't process this
859 * event */
860 GetWindowRect(s_textArea, &rct);
861 if (yPos < rct.top)
862 return result;
863 }
864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 gui_mch_get_winpos(&x, &y);
866 xPos -= x;
867
868 if (xPos < 48) /* <VN> TODO should use system metric? */
869 return HTBOTTOMLEFT;
870 else
871 return HTBOTTOMRIGHT;
872 }
873 else
874 return result;
875 }
876 /* break; notreached */
877
878#ifdef FEAT_MBYTE_IME
879 case WM_IME_NOTIFY:
880 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
881 return MyWindowProc(hwnd, uMsg, wParam, lParam);
882 break;
883 case WM_IME_COMPOSITION:
884 if (!_OnImeComposition(hwnd, wParam, lParam))
885 return MyWindowProc(hwnd, uMsg, wParam, lParam);
886 break;
887#endif
888
889 default:
890 if (uMsg == msh_msgmousewheel && msh_msgmousewheel != 0)
891 { /* handle MSH_MOUSEWHEEL messages for Intellimouse */
892 _OnMouseWheel(hwnd, HIWORD(wParam));
893 break;
894 }
895#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000896 else if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 {
898 _OnFindRepl();
899 }
900#endif
901 return MyWindowProc(hwnd, uMsg, wParam, lParam);
902 }
903
904 return 1;
905}
906
907/*
908 * End of call-back routines
909 */
910
911/* parent window, if specified with -P */
912HWND vim_parent_hwnd = NULL;
913
914 static BOOL CALLBACK
915FindWindowTitle(HWND hwnd, LPARAM lParam)
916{
917 char buf[2048];
918 char *title = (char *)lParam;
919
920 if (GetWindowText(hwnd, buf, sizeof(buf)))
921 {
922 if (strstr(buf, title) != NULL)
923 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000924 /* Found it. Store the window ref. and quit searching if MDI
925 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000927 if (vim_parent_hwnd != NULL)
928 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 }
930 }
931 return TRUE; /* continue searching */
932}
933
934/*
935 * Invoked for '-P "title"' argument: search for parent application to open
936 * our window in.
937 */
938 void
939gui_mch_set_parent(char *title)
940{
941 EnumWindows(FindWindowTitle, (LPARAM)title);
942 if (vim_parent_hwnd == NULL)
943 {
944 EMSG2(_("E671: Cannot find window title \"%s\""), title);
945 mch_exit(2);
946 }
947}
948
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000949#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 static void
951ole_error(char *arg)
952{
953 EMSG2(_("E243: Argument not supported: \"-%s\"; Use the OLE version."),
954 arg);
955}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957
958/*
959 * Parse the GUI related command-line arguments. Any arguments used are
960 * deleted from argv, and *argc is decremented accordingly. This is called
961 * when vim is started, whether or not the GUI has been started.
962 */
963 void
964gui_mch_prepare(int *argc, char **argv)
965{
966 int silent = FALSE;
967 int idx;
968
969 /* Check for special OLE command line parameters */
970 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
971 {
972 /* Check for a "-silent" argument first. */
973 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
974 && (argv[2][0] == '-' || argv[2][0] == '/'))
975 {
976 silent = TRUE;
977 idx = 2;
978 }
979 else
980 idx = 1;
981
982 /* Register Vim as an OLE Automation server */
983 if (STRICMP(argv[idx] + 1, "register") == 0)
984 {
985#ifdef FEAT_OLE
986 RegisterMe(silent);
987 mch_exit(0);
988#else
989 if (!silent)
990 ole_error("register");
991 mch_exit(2);
992#endif
993 }
994
995 /* Unregister Vim as an OLE Automation server */
996 if (STRICMP(argv[idx] + 1, "unregister") == 0)
997 {
998#ifdef FEAT_OLE
999 UnregisterMe(!silent);
1000 mch_exit(0);
1001#else
1002 if (!silent)
1003 ole_error("unregister");
1004 mch_exit(2);
1005#endif
1006 }
1007
1008 /* Ignore an -embedding argument. It is only relevant if the
1009 * application wants to treat the case when it is started manually
1010 * differently from the case where it is started via automation (and
1011 * we don't).
1012 */
1013 if (STRICMP(argv[idx] + 1, "embedding") == 0)
1014 {
1015#ifdef FEAT_OLE
1016 *argc = 1;
1017#else
1018 ole_error("embedding");
1019 mch_exit(2);
1020#endif
1021 }
1022 }
1023
1024#ifdef FEAT_OLE
1025 {
1026 int bDoRestart = FALSE;
1027
1028 InitOLE(&bDoRestart);
1029 /* automatically exit after registering */
1030 if (bDoRestart)
1031 mch_exit(0);
1032 }
1033#endif
1034
1035#ifdef FEAT_NETBEANS_INTG
1036 {
1037 /* stolen from gui_x11.x */
1038 int arg;
1039
1040 for (arg = 1; arg < *argc; arg++)
1041 if (strncmp("-nb", argv[arg], 3) == 0)
1042 {
1043 usingNetbeans++;
1044 netbeansArg = argv[arg];
1045 mch_memmove(&argv[arg], &argv[arg + 1],
1046 (--*argc - arg) * sizeof(char *));
1047 argv[*argc] = NULL;
1048 break; /* enough? */
1049 }
1050
1051 if (usingNetbeans)
1052 {
1053 WSADATA wsaData;
1054 int wsaerr;
1055
1056 /* Init WinSock */
1057 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
1058 if (wsaerr == 0)
1059 WSInitialized = TRUE;
1060 }
1061 }
1062#endif
1063
1064 /* get the OS version info */
1065 os_version.dwOSVersionInfoSize = sizeof(os_version);
1066 GetVersionEx(&os_version); /* this call works on Win32s, Win95 and WinNT */
1067
1068 /* try and load the user32.dll library and get the entry points for
1069 * multi-monitor-support. */
1070 if ((user32_lib = LoadLibrary("User32.dll")) != NULL)
1071 {
1072 pMonitorFromWindow = (TMonitorFromWindow)GetProcAddress(user32_lib,
1073 "MonitorFromWindow");
1074
1075 /* there are ...A and ...W version of GetMonitorInfo - looking at
1076 * winuser.h, they have exactly the same declaration. */
1077 pGetMonitorInfo = (TGetMonitorInfo)GetProcAddress(user32_lib,
1078 "GetMonitorInfoA");
1079 }
1080}
1081
1082/*
1083 * Initialise the GUI. Create all the windows, set up all the call-backs
1084 * etc.
1085 */
1086 int
1087gui_mch_init(void)
1088{
1089 const char szVimWndClass[] = VIM_CLASS;
1090 const char szTextAreaClass[] = "VimTextArea";
1091 WNDCLASS wndclass;
1092#ifdef FEAT_MBYTE
1093 const WCHAR szVimWndClassW[] = VIM_CLASSW;
1094 WNDCLASSW wndclassw;
1095#endif
1096#ifdef GLOBAL_IME
1097 ATOM atom;
1098#endif
1099
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 /* Return here if the window was already opened (happens when
1101 * gui_mch_dialog() is called early). */
1102 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00001103 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104
1105 /*
1106 * Load the tearoff bitmap
1107 */
1108#ifdef FEAT_TEAROFF
1109 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
1110#endif
1111
1112 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
1113 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
1114#ifdef FEAT_MENU
1115 gui.menu_height = 0; /* Windows takes care of this */
1116#endif
1117 gui.border_width = 0;
1118
1119 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
1120
1121#ifdef FEAT_MBYTE
1122 /* First try using the wide version, so that we can use any title.
1123 * Otherwise only characters in the active codepage will work. */
1124 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
1125 {
1126 wndclassw.style = 0;
1127 wndclassw.lpfnWndProc = _WndProc;
1128 wndclassw.cbClsExtra = 0;
1129 wndclassw.cbWndExtra = 0;
1130 wndclassw.hInstance = s_hinst;
1131 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
1132 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
1133 wndclassw.hbrBackground = s_brush;
1134 wndclassw.lpszMenuName = NULL;
1135 wndclassw.lpszClassName = szVimWndClassW;
1136
1137 if ((
1138#ifdef GLOBAL_IME
1139 atom =
1140#endif
1141 RegisterClassW(&wndclassw)) == 0)
1142 {
1143 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1144 return FAIL;
1145
1146 /* Must be Windows 98, fall back to non-wide function. */
1147 }
1148 else
1149 wide_WindowProc = TRUE;
1150 }
1151
1152 if (!wide_WindowProc)
1153#endif
1154
1155 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
1156 {
1157 wndclass.style = 0;
1158 wndclass.lpfnWndProc = _WndProc;
1159 wndclass.cbClsExtra = 0;
1160 wndclass.cbWndExtra = 0;
1161 wndclass.hInstance = s_hinst;
1162 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
1163 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1164 wndclass.hbrBackground = s_brush;
1165 wndclass.lpszMenuName = NULL;
1166 wndclass.lpszClassName = szVimWndClass;
1167
1168 if ((
1169#ifdef GLOBAL_IME
1170 atom =
1171#endif
1172 RegisterClass(&wndclass)) == 0)
1173 return FAIL;
1174 }
1175
1176 if (vim_parent_hwnd != NULL)
1177 {
1178#ifdef HAVE_TRY_EXCEPT
1179 __try
1180 {
1181#endif
1182 /* Open inside the specified parent window.
1183 * TODO: last argument should point to a CLIENTCREATESTRUCT
1184 * structure. */
1185 s_hwnd = CreateWindowEx(
1186 WS_EX_MDICHILD,
1187 szVimWndClass, "Vim MSWindows GUI",
1188 WS_OVERLAPPEDWINDOW | WS_CHILD | WS_CLIPSIBLINGS | 0xC000,
1189 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
1190 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
1191 100, /* Any value will do */
1192 100, /* Any value will do */
1193 vim_parent_hwnd, NULL,
1194 s_hinst, NULL);
1195#ifdef HAVE_TRY_EXCEPT
1196 }
1197 __except(EXCEPTION_EXECUTE_HANDLER)
1198 {
1199 /* NOP */
1200 }
1201#endif
1202 if (s_hwnd == NULL)
1203 {
1204 EMSG(_("E672: Unable to open window inside MDI application"));
1205 mch_exit(2);
1206 }
1207 }
1208 else
1209 /* Open toplevel window. */
1210 s_hwnd = CreateWindow(
1211 szVimWndClass, "Vim MSWindows GUI",
1212 WS_OVERLAPPEDWINDOW,
1213 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
1214 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
1215 100, /* Any value will do */
1216 100, /* Any value will do */
1217 NULL, NULL,
1218 s_hinst, NULL);
1219
1220 if (s_hwnd == NULL)
1221 return FAIL;
1222
1223#ifdef GLOBAL_IME
1224 global_ime_init(atom, s_hwnd);
1225#endif
1226#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
1227 dyn_imm_load();
1228#endif
1229
1230 /* Create the text area window */
1231 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
1232 {
1233 wndclass.style = CS_OWNDC;
1234 wndclass.lpfnWndProc = _TextAreaWndProc;
1235 wndclass.cbClsExtra = 0;
1236 wndclass.cbWndExtra = 0;
1237 wndclass.hInstance = s_hinst;
1238 wndclass.hIcon = NULL;
1239 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1240 wndclass.hbrBackground = NULL;
1241 wndclass.lpszMenuName = NULL;
1242 wndclass.lpszClassName = szTextAreaClass;
1243
1244 if (RegisterClass(&wndclass) == 0)
1245 return FAIL;
1246 }
1247 s_textArea = CreateWindowEx(
1248 WS_EX_CLIENTEDGE,
1249 szTextAreaClass, "Vim text area",
1250 WS_CHILD | WS_VISIBLE, 0, 0,
1251 100, /* Any value will do for now */
1252 100, /* Any value will do for now */
1253 s_hwnd, NULL,
1254 s_hinst, NULL);
1255
1256 if (s_textArea == NULL)
1257 return FAIL;
1258
1259#ifdef FEAT_MENU
1260 s_menuBar = CreateMenu();
1261#endif
1262 s_hdc = GetDC(s_textArea);
1263
1264#ifdef MSWIN16_FASTTEXT
1265 SetBkMode(s_hdc, OPAQUE);
1266#endif
1267
1268#ifdef FEAT_WINDOWS
1269 DragAcceptFiles(s_hwnd, TRUE);
1270#endif
1271
1272 /* Do we need to bother with this? */
1273 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
1274
1275 /* Get background/foreground colors from the system */
1276 gui_mch_def_colors();
1277
1278 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
1279 * file) */
1280 set_normal_colors();
1281
1282 /*
1283 * Check that none of the colors are the same as the background color.
1284 * Then store the current values as the defaults.
1285 */
1286 gui_check_colors();
1287 gui.def_norm_pixel = gui.norm_pixel;
1288 gui.def_back_pixel = gui.back_pixel;
1289
1290 /* Get the colors for the highlight groups (gui_check_colors() might have
1291 * changed them) */
1292 highlight_gui_started();
1293
1294 /*
1295 * Start out by adding the configured border width into the border offset
1296 */
1297 gui.border_offset = gui.border_width + 2; /*CLIENT EDGE*/
1298
1299 /*
1300 * Set up for Intellimouse processing
1301 */
1302 init_mouse_wheel();
1303
1304 /*
1305 * compute a couple of metrics used for the dialogs
1306 */
1307 get_dialog_font_metrics();
1308#ifdef FEAT_TOOLBAR
1309 /*
1310 * Create the toolbar
1311 */
1312 initialise_toolbar();
1313#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001314#ifdef FEAT_GUI_TABLINE
1315 /*
1316 * Create the tabline
1317 */
1318 initialise_tabline();
1319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320#ifdef MSWIN_FIND_REPLACE
1321 /*
1322 * Initialise the dialog box stuff
1323 */
1324 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
1325
1326 /* Initialise the struct */
1327 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
1328 s_findrep_struct.lpstrFindWhat = alloc(MSWIN_FR_BUFSIZE);
1329 s_findrep_struct.lpstrFindWhat[0] = NUL;
1330 s_findrep_struct.lpstrReplaceWith = alloc(MSWIN_FR_BUFSIZE);
1331 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1332 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
1333 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
1334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
Bram Moolenaar748bf032005-02-02 23:04:36 +00001336theend:
1337 /* Display any pending error messages */
1338 display_errors();
1339
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 return OK;
1341}
1342
1343/*
1344 * Get the size of the screen, taking position on multiple monitors into
1345 * account (if supported).
1346 */
1347 static void
1348get_work_area(RECT *spi_rect)
1349{
1350 _HMONITOR mon;
1351 _MONITORINFO moninfo;
1352
1353 /* use these functions only if available */
1354 if (pMonitorFromWindow != NULL && pGetMonitorInfo != NULL)
1355 {
1356 /* work out which monitor the window is on, and get *it's* work area */
1357 mon = pMonitorFromWindow(s_hwnd, 1 /*MONITOR_DEFAULTTOPRIMARY*/);
1358 if (mon != NULL)
1359 {
1360 moninfo.cbSize = sizeof(_MONITORINFO);
1361 if (pGetMonitorInfo(mon, &moninfo))
1362 {
1363 *spi_rect = moninfo.rcWork;
1364 return;
1365 }
1366 }
1367 }
1368 /* this is the old method... */
1369 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
1370}
1371
1372/*
1373 * Set the size of the window to the given width and height in pixels.
1374 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001375/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 void
1377gui_mch_set_shellsize(int width, int height,
1378 int min_width, int min_height, int base_width, int base_height)
1379{
1380 RECT workarea_rect;
1381 int win_width, win_height;
1382 int win_xpos, win_ypos;
1383 WINDOWPLACEMENT wndpl;
1384
1385 /* try to keep window completely on screen */
1386 /* get size of the screen work area (excludes taskbar, appbars) */
1387 get_work_area(&workarea_rect);
1388
1389 /* get current posision of our window */
1390 wndpl.length = sizeof(WINDOWPLACEMENT);
1391 GetWindowPlacement(s_hwnd, &wndpl);
1392
1393 /* Resizing a maximized window looks very strange, unzoom it first.
1394 * But don't do it when still starting up, it may have been requested in
1395 * the shortcut. */
1396 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
1397 {
1398 ShowWindow(s_hwnd, SW_SHOWNORMAL);
1399 /* Need to get the settings of the normal window. */
1400 GetWindowPlacement(s_hwnd, &wndpl);
1401 }
1402
1403 win_xpos = wndpl.rcNormalPosition.left;
1404 win_ypos = wndpl.rcNormalPosition.top;
1405
1406 /* compute the size of the outside of the window */
1407 win_width = width + GetSystemMetrics(SM_CXFRAME) * 2;
1408 win_height = height + GetSystemMetrics(SM_CYFRAME) * 2
1409 + GetSystemMetrics(SM_CYCAPTION)
1410#ifdef FEAT_MENU
1411 + gui_mswin_get_menu_height(FALSE)
1412#endif
1413 ;
1414
1415 /* if the window is going off the screen, move it on to the screen */
1416 if (win_xpos + win_width > workarea_rect.right)
1417 win_xpos = workarea_rect.right - win_width;
1418
1419 if (win_xpos < workarea_rect.left)
1420 win_xpos = workarea_rect.left;
1421
1422 if (win_ypos + win_height > workarea_rect.bottom)
1423 win_ypos = workarea_rect.bottom - win_height;
1424
1425 if (win_ypos < workarea_rect.top)
1426 win_ypos = workarea_rect.top;
1427
1428 /* When the taskbar is placed on the left or top of the screen,
1429 * SetWindowPlacement() adds its width or height automatically, compensate
1430 * for that. When the offset is over 400 it's probably something else,
1431 * skip it then (just in case). */
1432 if (workarea_rect.left > 0 && workarea_rect.left < 400)
1433 win_xpos -= workarea_rect.left;
1434 if (workarea_rect.top > 0 && workarea_rect.top < 400)
1435 win_ypos -= workarea_rect.top;
1436
1437 wndpl.rcNormalPosition.left = win_xpos;
1438 wndpl.rcNormalPosition.right = win_xpos + win_width;
1439 wndpl.rcNormalPosition.top = win_ypos;
1440 wndpl.rcNormalPosition.bottom = win_ypos + win_height;
1441
1442 /* set window position - we should use SetWindowPlacement rather than
1443 * SetWindowPos as the MSDN docs say the coord systems returned by
1444 * these two are not compatible. */
1445 SetWindowPlacement(s_hwnd, &wndpl);
1446
1447 SetActiveWindow(s_hwnd);
1448 SetFocus(s_hwnd);
1449
1450#ifdef FEAT_MENU
1451 /* Menu may wrap differently now */
1452 gui_mswin_get_menu_height(!gui.starting);
1453#endif
1454}
1455
1456
1457 void
1458gui_mch_set_scrollbar_thumb(
1459 scrollbar_T *sb,
1460 long val,
1461 long size,
1462 long max)
1463{
1464 SCROLLINFO info;
1465
1466 sb->scroll_shift = 0;
1467 while (max > 32767)
1468 {
1469 max = (max + 1) >> 1;
1470 val >>= 1;
1471 size >>= 1;
1472 ++sb->scroll_shift;
1473 }
1474
1475 if (sb->scroll_shift > 0)
1476 ++size;
1477
1478 info.cbSize = sizeof(info);
1479 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
1480 info.nPos = val;
1481 info.nMin = 0;
1482 info.nMax = max;
1483 info.nPage = size;
1484 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
1485}
1486
1487
1488/*
1489 * Set the current text font.
1490 */
1491 void
1492gui_mch_set_font(GuiFont font)
1493{
1494 gui.currFont = font;
1495}
1496
1497
1498/*
1499 * Set the current text foreground color.
1500 */
1501 void
1502gui_mch_set_fg_color(guicolor_T color)
1503{
1504 gui.currFgColor = color;
1505}
1506
1507/*
1508 * Set the current text background color.
1509 */
1510 void
1511gui_mch_set_bg_color(guicolor_T color)
1512{
1513 gui.currBgColor = color;
1514}
1515
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001516/*
1517 * Set the current text special color.
1518 */
1519 void
1520gui_mch_set_sp_color(guicolor_T color)
1521{
1522 gui.currSpColor = color;
1523}
1524
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525#if defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)
1526/*
1527 * Multi-byte handling, originally by Sung-Hoon Baek.
1528 * First static functions (no prototypes generated).
1529 */
1530#ifdef _MSC_VER
1531# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
1532#endif
1533#include <imm.h>
1534
1535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 * handle WM_IME_NOTIFY message
1537 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001538/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 static LRESULT
1540_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData)
1541{
1542 LRESULT lResult = 0;
1543 HIMC hImc;
1544
1545 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
1546 return lResult;
1547 switch (dwCommand)
1548 {
1549 case IMN_SETOPENSTATUS:
1550 if (pImmGetOpenStatus(hImc))
1551 {
1552 pImmSetCompositionFont(hImc, &norm_logfont);
1553 im_set_position(gui.row, gui.col);
1554
1555 /* Disable langmap */
1556 State &= ~LANGMAP;
1557 if (State & INSERT)
1558 {
1559#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1560 /* Unshown 'keymap' in status lines */
1561 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
1562 {
1563 /* Save cursor position */
1564 int old_row = gui.row;
1565 int old_col = gui.col;
1566
1567 // This must be called here before
1568 // status_redraw_curbuf(), otherwise the mode
1569 // message may appear in the wrong position.
1570 showmode();
1571 status_redraw_curbuf();
1572 update_screen(0);
1573 /* Restore cursor position */
1574 gui.row = old_row;
1575 gui.col = old_col;
1576 }
1577#endif
1578 }
1579 }
1580 gui_update_cursor(TRUE, FALSE);
1581 lResult = 0;
1582 break;
1583 }
1584 pImmReleaseContext(hWnd, hImc);
1585 return lResult;
1586}
1587
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001588/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 static LRESULT
1590_OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param)
1591{
1592 char_u *ret;
1593 int len;
1594
1595 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
1596 return 0;
1597
1598 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
1599 if (ret != NULL)
1600 {
1601 add_to_input_buf_csi(ret, len);
1602 vim_free(ret);
1603 return 1;
1604 }
1605 return 0;
1606}
1607
1608/*
1609 * get the current composition string, in UCS-2; *lenp is the number of
1610 * *lenp is the number of Unicode characters.
1611 */
1612 static short_u *
1613GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
1614{
1615 LONG ret;
1616 LPWSTR wbuf = NULL;
1617 char_u *buf;
1618
1619 if (!pImmGetContext)
1620 return NULL; /* no imm32.dll */
1621
1622 /* Try Unicode; this'll always work on NT regardless of codepage. */
1623 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
1624 if (ret == 0)
1625 return NULL; /* empty */
1626
1627 if (ret > 0)
1628 {
1629 /* Allocate the requested buffer plus space for the NUL character. */
1630 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
1631 if (wbuf != NULL)
1632 {
1633 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
1634 *lenp = ret / sizeof(WCHAR);
1635 }
1636 return (short_u *)wbuf;
1637 }
1638
1639 /* ret < 0; we got an error, so try the ANSI version. This'll work
1640 * on 9x/ME, but only if the codepage happens to be set to whatever
1641 * we're inputting. */
1642 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
1643 if (ret <= 0)
1644 return NULL; /* empty or error */
1645
1646 buf = alloc(ret);
1647 if (buf == NULL)
1648 return NULL;
1649 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
1650
1651 /* convert from codepage to UCS-2 */
1652 MultiByteToWideChar_alloc(GetACP(), 0, buf, ret, &wbuf, lenp);
1653 vim_free(buf);
1654
1655 return (short_u *)wbuf;
1656}
1657
1658/*
1659 * void GetResultStr()
1660 *
1661 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
1662 * get complete composition string
1663 */
1664 static char_u *
1665GetResultStr(HWND hwnd, int GCS, int *lenp)
1666{
1667 HIMC hIMC; /* Input context handle. */
1668 short_u *buf = NULL;
1669 char_u *convbuf = NULL;
1670
1671 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
1672 return NULL;
1673
1674 /* Reads in the composition string. */
1675 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
1676 if (buf == NULL)
1677 return NULL;
1678
1679 convbuf = ucs2_to_enc(buf, lenp);
1680 pImmReleaseContext(hwnd, hIMC);
1681 vim_free(buf);
1682 return convbuf;
1683}
1684#endif
1685
1686/* For global functions we need prototypes. */
1687#if (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) || defined(PROTO)
1688
1689/*
1690 * set font to IM.
1691 */
1692 void
1693im_set_font(LOGFONT *lf)
1694{
1695 HIMC hImc;
1696
1697 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
1698 {
1699 pImmSetCompositionFont(hImc, lf);
1700 pImmReleaseContext(s_hwnd, hImc);
1701 }
1702}
1703
1704/*
1705 * Notify cursor position to IM.
1706 */
1707 void
1708im_set_position(int row, int col)
1709{
1710 HIMC hImc;
1711
1712 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
1713 {
1714 COMPOSITIONFORM cfs;
1715
1716 cfs.dwStyle = CFS_POINT;
1717 cfs.ptCurrentPos.x = FILL_X(col);
1718 cfs.ptCurrentPos.y = FILL_Y(row);
1719 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
1720 pImmSetCompositionWindow(hImc, &cfs);
1721
1722 pImmReleaseContext(s_hwnd, hImc);
1723 }
1724}
1725
1726/*
1727 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
1728 */
1729 void
1730im_set_active(int active)
1731{
1732 HIMC hImc;
1733 static HIMC hImcOld = (HIMC)0;
1734
1735 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
1736 {
1737 if (p_imdisable)
1738 {
1739 if (hImcOld == (HIMC)0)
1740 {
1741 hImcOld = pImmGetContext(s_hwnd);
1742 if (hImcOld)
1743 pImmAssociateContext(s_hwnd, (HIMC)0);
1744 }
1745 active = FALSE;
1746 }
1747 else if (hImcOld != (HIMC)0)
1748 {
1749 pImmAssociateContext(s_hwnd, hImcOld);
1750 hImcOld = (HIMC)0;
1751 }
1752
1753 hImc = pImmGetContext(s_hwnd);
1754 if (hImc)
1755 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001756 /*
1757 * for Korean ime
1758 */
1759 HKL hKL = GetKeyboardLayout(0);
1760
1761 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
1762 {
1763 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
1764 static BOOL bSaved = FALSE;
1765
1766 if (active)
1767 {
1768 /* if we have a saved conversion status, restore it */
1769 if (bSaved)
1770 pImmSetConversionStatus(hImc, dwConversionSaved,
1771 dwSentenceSaved);
1772 bSaved = FALSE;
1773 }
1774 else
1775 {
1776 /* save conversion status and disable korean */
1777 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
1778 &dwSentenceSaved))
1779 {
1780 bSaved = TRUE;
1781 pImmSetConversionStatus(hImc,
1782 dwConversionSaved & ~(IME_CMODE_NATIVE
1783 | IME_CMODE_FULLSHAPE),
1784 dwSentenceSaved);
1785 }
1786 }
1787 }
1788
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 pImmSetOpenStatus(hImc, active);
1790 pImmReleaseContext(s_hwnd, hImc);
1791 }
1792 }
1793}
1794
1795/*
1796 * Get IM status. When IM is on, return not 0. Else return 0.
1797 */
1798 int
1799im_get_status()
1800{
1801 int status = 0;
1802 HIMC hImc;
1803
1804 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
1805 {
1806 status = pImmGetOpenStatus(hImc) ? 1 : 0;
1807 pImmReleaseContext(s_hwnd, hImc);
1808 }
1809 return status;
1810}
1811
1812#endif /* FEAT_MBYTE && FEAT_MBYTE_IME */
1813
1814#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
1815/* Win32 with GLOBAL IME */
1816
1817/*
1818 * Notify cursor position to IM.
1819 */
1820 void
1821im_set_position(int row, int col)
1822{
1823 /* Win32 with GLOBAL IME */
1824 POINT p;
1825
1826 p.x = FILL_X(col);
1827 p.y = FILL_Y(row);
1828 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
1829 global_ime_set_position(&p);
1830}
1831
1832/*
1833 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
1834 */
1835 void
1836im_set_active(int active)
1837{
1838 global_ime_set_status(active);
1839}
1840
1841/*
1842 * Get IM status. When IM is on, return not 0. Else return 0.
1843 */
1844 int
1845im_get_status()
1846{
1847 return global_ime_get_status();
1848}
1849#endif
1850
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001851#ifdef FEAT_MBYTE
1852/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00001853 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001854 */
1855 static void
1856latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
1857{
1858 int c;
1859
Bram Moolenaarca003e12006-03-17 23:19:38 +00001860 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001861 {
1862 c = *text++;
1863 switch (c)
1864 {
1865 case 0xa4: c = 0x20ac; break; /* euro */
1866 case 0xa6: c = 0x0160; break; /* S hat */
1867 case 0xa8: c = 0x0161; break; /* S -hat */
1868 case 0xb4: c = 0x017d; break; /* Z hat */
1869 case 0xb8: c = 0x017e; break; /* Z -hat */
1870 case 0xbc: c = 0x0152; break; /* OE */
1871 case 0xbd: c = 0x0153; break; /* oe */
1872 case 0xbe: c = 0x0178; break; /* Y */
1873 }
1874 *unicodebuf++ = c;
1875 }
1876}
1877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878
1879#ifdef FEAT_RIGHTLEFT
1880/*
1881 * What is this for? In the case where you are using Win98 or Win2K or later,
1882 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
1883 * reverses the string sent to the TextOut... family. This sucks, because we
1884 * go to a lot of effort to do the right thing, and there doesn't seem to be a
1885 * way to tell Windblows not to do this!
1886 *
1887 * The short of it is that this 'RevOut' only gets called if you are running
1888 * one of the new, "improved" MS OSes, and only if you are running in
1889 * 'rightleft' mode. It makes display take *slightly* longer, but not
1890 * noticeably so.
1891 */
1892 static void
1893RevOut( HDC s_hdc,
1894 int col,
1895 int row,
1896 UINT foptions,
1897 CONST RECT *pcliprect,
1898 LPCTSTR text,
1899 UINT len,
1900 CONST INT *padding)
1901{
1902 int ix;
1903 static int special = -1;
1904
1905 if (special == -1)
1906 {
1907 /* Check windows version: special treatment is needed if it is NT 5 or
1908 * Win98 or higher. */
1909 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
1910 && os_version.dwMajorVersion >= 5)
1911 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
1912 && (os_version.dwMajorVersion > 4
1913 || (os_version.dwMajorVersion == 4
1914 && os_version.dwMinorVersion > 0))))
1915 special = 1;
1916 else
1917 special = 0;
1918 }
1919
1920 if (special)
1921 for (ix = 0; ix < (int)len; ++ix)
1922 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
1923 pcliprect, text + ix, 1, padding);
1924 else
1925 ExtTextOut(s_hdc, col, row, foptions, pcliprect, text, len, padding);
1926}
1927#endif
1928
1929 void
1930gui_mch_draw_string(
1931 int row,
1932 int col,
1933 char_u *text,
1934 int len,
1935 int flags)
1936{
1937 static int *padding = NULL;
1938 static int pad_size = 0;
1939 int i;
1940 const RECT *pcliprect = NULL;
1941 UINT foptions = 0;
1942#ifdef FEAT_MBYTE
1943 static WCHAR *unicodebuf = NULL;
1944 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001945 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 int n = 0;
1947#endif
1948 HPEN hpen, old_pen;
1949 int y;
1950
1951#ifndef MSWIN16_FASTTEXT
1952 /*
1953 * Italic and bold text seems to have an extra row of pixels at the bottom
1954 * (below where the bottom of the character should be). If we draw the
1955 * characters with a solid background, the top row of pixels in the
1956 * character below will be overwritten. We can fix this by filling in the
1957 * background ourselves, to the correct character proportions, and then
1958 * writing the character in transparent mode. Still have a problem when
1959 * the character is "_", which gets written on to the character below.
1960 * New fix: set gui.char_ascent to -1. This shifts all characters up one
1961 * pixel in their slots, which fixes the problem with the bottom row of
1962 * pixels. We still need this code because otherwise the top row of pixels
1963 * becomes a problem. - webb.
1964 */
1965 static HBRUSH hbr_cache[2] = {NULL, NULL};
1966 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
1967 static int brush_lru = 0;
1968 HBRUSH hbr;
1969 RECT rc;
1970
1971 if (!(flags & DRAW_TRANSP))
1972 {
1973 /*
1974 * Clear background first.
1975 * Note: FillRect() excludes right and bottom of rectangle.
1976 */
1977 rc.left = FILL_X(col);
1978 rc.top = FILL_Y(row);
1979#ifdef FEAT_MBYTE
1980 if (has_mbyte)
1981 {
1982 int cell_len = 0;
1983
1984 /* Compute the length in display cells. */
1985 for (n = 0; n < len; n += MB_BYTE2LEN(text[n]))
1986 cell_len += (*mb_ptr2cells)(text + n);
1987 rc.right = FILL_X(col + cell_len);
1988 }
1989 else
1990#endif
1991 rc.right = FILL_X(col + len);
1992 rc.bottom = FILL_Y(row + 1);
1993
1994 /* Cache the created brush, that saves a lot of time. We need two:
1995 * one for cursor background and one for the normal background. */
1996 if (gui.currBgColor == brush_color[0])
1997 {
1998 hbr = hbr_cache[0];
1999 brush_lru = 1;
2000 }
2001 else if (gui.currBgColor == brush_color[1])
2002 {
2003 hbr = hbr_cache[1];
2004 brush_lru = 0;
2005 }
2006 else
2007 {
2008 if (hbr_cache[brush_lru] != NULL)
2009 DeleteBrush(hbr_cache[brush_lru]);
2010 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
2011 brush_color[brush_lru] = gui.currBgColor;
2012 hbr = hbr_cache[brush_lru];
2013 brush_lru = !brush_lru;
2014 }
2015 FillRect(s_hdc, &rc, hbr);
2016
2017 SetBkMode(s_hdc, TRANSPARENT);
2018
2019 /*
2020 * When drawing block cursor, prevent inverted character spilling
2021 * over character cell (can happen with bold/italic)
2022 */
2023 if (flags & DRAW_CURSOR)
2024 {
2025 pcliprect = &rc;
2026 foptions = ETO_CLIPPED;
2027 }
2028 }
2029#else
2030 /*
2031 * The alternative would be to write the characters in opaque mode, but
2032 * when the text is not exactly the same proportions as normal text, too
2033 * big or too little a rectangle gets drawn for the background.
2034 */
2035 SetBkMode(s_hdc, OPAQUE);
2036 SetBkColor(s_hdc, gui.currBgColor);
2037#endif
2038 SetTextColor(s_hdc, gui.currFgColor);
2039 SelectFont(s_hdc, gui.currFont);
2040
2041 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
2042 {
2043 vim_free(padding);
2044 pad_size = Columns;
2045
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002046 /* Don't give an out-of-memory message here, it would call us
2047 * recursively. */
2048 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 if (padding != NULL)
2050 for (i = 0; i < pad_size; i++)
2051 padding[i] = gui.char_width;
2052 }
2053
2054 /* On NT, tell the font renderer not to "help" us with Hebrew and Arabic
2055 * text. This doesn't work in 9x, so we have to deal with it manually on
2056 * those systems. */
2057 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
2058 foptions |= ETO_IGNORELANGUAGE;
2059
2060 /*
2061 * We have to provide the padding argument because italic and bold versions
2062 * of fixed-width fonts are often one pixel or so wider than their normal
2063 * versions.
2064 * No check for DRAW_BOLD, Windows will have done it already.
2065 */
2066
2067#ifdef FEAT_MBYTE
2068 /* Check if there are any UTF-8 characters. If not, use normal text
2069 * output to speed up output. */
2070 if (enc_utf8)
2071 for (n = 0; n < len; ++n)
2072 if (text[n] >= 0x80)
2073 break;
2074
2075 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002076 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002078 if ((enc_utf8
2079 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
2080 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 && (unicodebuf == NULL || len > unibuflen))
2082 {
2083 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002084 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085
2086 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002087 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088
2089 unibuflen = len;
2090 }
2091
2092 if (enc_utf8 && n < len && unicodebuf != NULL)
2093 {
2094 /* Output UTF-8 characters. Caller has already separated
2095 * composing characters. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002096 int i;
2097 int wlen; /* string length in words */
2098 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 int cells; /* cell width of string up to composing char */
2100 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002101 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002103 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002104 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002106 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002108 c = utf_ptr2char(text + i);
2109 if (c >= 0x10000)
2110 {
2111 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002112 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
2113 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002114 }
2115 else
2116 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002117 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002118 }
2119 cw = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 if (cw > 2) /* don't use 4 for unprintable char */
2121 cw = 1;
2122 if (unicodepdy != NULL)
2123 {
2124 /* Use unicodepdy to make characters fit as we expect, even
2125 * when the font uses different widths (e.g., bold character
2126 * is wider). */
2127 unicodepdy[clen] = cw * gui.char_width;
2128 }
2129 cells += cw;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002130 i += utfc_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00002131 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 }
2133 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaarca003e12006-03-17 23:19:38 +00002134 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 len = cells; /* used for underlining */
2136 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002137 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
2139 /* If we want to display codepage data, and the current CP is not the
2140 * ANSI one, we need to go via Unicode. */
2141 if (unicodebuf != NULL)
2142 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002143 if (enc_latin9)
2144 latin9_to_ucs(text, len, unicodebuf);
2145 else
2146 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 MB_PRECOMPOSED,
2148 (char *)text, len,
2149 (LPWSTR)unicodebuf, unibuflen);
2150 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002151 {
2152 /* Use unicodepdy to make characters fit as we expect, even
2153 * when the font uses different widths (e.g., bold character
2154 * is wider). */
2155 if (unicodepdy != NULL)
2156 {
2157 int i;
2158 int cw;
2159
2160 for (i = 0; i < len; ++i)
2161 {
2162 cw = utf_char2cells(unicodebuf[i]);
2163 if (cw > 2)
2164 cw = 1;
2165 unicodepdy[i] = cw * gui.char_width;
2166 }
2167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002169 foptions, pcliprect, unicodebuf, len, unicodepdy);
2170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 }
2172 }
2173 else
2174#endif
2175 {
2176#ifdef FEAT_RIGHTLEFT
2177 /* If we can't use ETO_IGNORELANGUAGE, we can't tell Windows not to
2178 * mess up RL text, so we have to draw it character-by-character.
2179 * Only do this if RL is on, since it's slow. */
2180 if (curwin->w_p_rl && !(foptions & ETO_IGNORELANGUAGE))
2181 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
2182 foptions, pcliprect, (char *)text, len, padding);
2183 else
2184#endif
2185 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
2186 foptions, pcliprect, (char *)text, len, padding);
2187 }
2188
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002189 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 if (flags & DRAW_UNDERL)
2191 {
2192 hpen = CreatePen(PS_SOLID, 1, gui.currFgColor);
2193 old_pen = SelectObject(s_hdc, hpen);
2194 /* When p_linespace is 0, overwrite the bottom row of pixels.
2195 * Otherwise put the line just below the character. */
2196 y = FILL_Y(row + 1) - 1;
2197#ifndef MSWIN16_FASTTEXT
2198 if (p_linespace > 1)
2199 y -= p_linespace - 1;
2200#endif
2201 MoveToEx(s_hdc, FILL_X(col), y, NULL);
2202 /* Note: LineTo() excludes the last pixel in the line. */
2203 LineTo(s_hdc, FILL_X(col + len), y);
2204 DeleteObject(SelectObject(s_hdc, old_pen));
2205 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002206
2207 /* Undercurl */
2208 if (flags & DRAW_UNDERC)
2209 {
2210 int x;
2211 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002212 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002213
2214 y = FILL_Y(row + 1) - 1;
2215 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
2216 {
2217 offset = val[x % 8];
2218 SetPixel(s_hdc, x, y - offset, gui.currSpColor);
2219 }
2220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221}
2222
2223
2224/*
2225 * Output routines.
2226 */
2227
2228/* Flush any output to the screen */
2229 void
2230gui_mch_flush(void)
2231{
2232# if defined(__BORLANDC__)
2233 /*
2234 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
2235 * prototype declaration.
2236 * The compiler complains if __stdcall is not used in both declarations.
2237 */
2238 BOOL __stdcall GdiFlush(void);
2239# endif
2240
2241 GdiFlush();
2242}
2243
2244 static void
2245clear_rect(RECT *rcp)
2246{
2247 HBRUSH hbr;
2248
2249 hbr = CreateSolidBrush(gui.back_pixel);
2250 FillRect(s_hdc, rcp, hbr);
2251 DeleteBrush(hbr);
2252}
2253
2254
Bram Moolenaarc716c302006-01-21 22:12:51 +00002255 void
2256gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
2257{
2258 RECT workarea_rect;
2259
2260 get_work_area(&workarea_rect);
2261
2262 *screen_w = workarea_rect.right
2263 - GetSystemMetrics(SM_CXFRAME) * 2;
2264
2265 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
2266 * the menubar for MSwin, we subtract it from the screen height, so that
2267 * the window size can be made to fit on the screen. */
2268 *screen_h = workarea_rect.bottom
2269 - GetSystemMetrics(SM_CYFRAME) * 2
2270 - GetSystemMetrics(SM_CYCAPTION)
2271#ifdef FEAT_MENU
2272 - gui_mswin_get_menu_height(FALSE)
2273#endif
2274 ;
2275}
2276
2277
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278#if defined(FEAT_MENU) || defined(PROTO)
2279/*
2280 * Add a sub menu to the menu bar.
2281 */
2282 void
2283gui_mch_add_menu(
2284 vimmenu_T *menu,
2285 int pos)
2286{
2287 vimmenu_T *parent = menu->parent;
2288
2289 menu->submenu_id = CreatePopupMenu();
2290 menu->id = s_menu_id++;
2291
2292 if (menu_is_menubar(menu->name))
2293 {
2294 if (is_winnt_3())
2295 {
2296 InsertMenu((parent == NULL) ? s_menuBar : parent->submenu_id,
2297 (UINT)pos, MF_POPUP | MF_STRING | MF_BYPOSITION,
2298 (UINT)menu->submenu_id, (LPCTSTR) menu->name);
2299 }
2300 else
2301 {
2302#ifdef FEAT_MBYTE
2303 WCHAR *wn = NULL;
2304 int n;
2305
2306 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2307 {
2308 /* 'encoding' differs from active codepage: convert menu name
2309 * and use wide function */
2310 wn = enc_to_ucs2(menu->name, NULL);
2311 if (wn != NULL)
2312 {
2313 MENUITEMINFOW infow;
2314
2315 infow.cbSize = sizeof(infow);
2316 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
2317 | MIIM_SUBMENU;
2318 infow.dwItemData = (DWORD)menu;
2319 infow.wID = menu->id;
2320 infow.fType = MFT_STRING;
2321 infow.dwTypeData = wn;
2322 infow.cch = wcslen(wn);
2323 infow.hSubMenu = menu->submenu_id;
2324 n = InsertMenuItemW((parent == NULL)
2325 ? s_menuBar : parent->submenu_id,
2326 (UINT)pos, TRUE, &infow);
2327 vim_free(wn);
2328 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2329 /* Failed, try using non-wide function. */
2330 wn = NULL;
2331 }
2332 }
2333
2334 if (wn == NULL)
2335#endif
2336 {
2337 MENUITEMINFO info;
2338
2339 info.cbSize = sizeof(info);
2340 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
2341 info.dwItemData = (DWORD)menu;
2342 info.wID = menu->id;
2343 info.fType = MFT_STRING;
2344 info.dwTypeData = (LPTSTR)menu->name;
2345 info.cch = (UINT)STRLEN(menu->name);
2346 info.hSubMenu = menu->submenu_id;
2347 InsertMenuItem((parent == NULL)
2348 ? s_menuBar : parent->submenu_id,
2349 (UINT)pos, TRUE, &info);
2350 }
2351 }
2352 }
2353
2354 /* Fix window size if menu may have wrapped */
2355 if (parent == NULL)
2356 gui_mswin_get_menu_height(!gui.starting);
2357#ifdef FEAT_TEAROFF
2358 else if (IsWindow(parent->tearoff_handle))
2359 rebuild_tearoff(parent);
2360#endif
2361}
2362
2363 void
2364gui_mch_show_popupmenu(vimmenu_T *menu)
2365{
2366 POINT mp;
2367
2368 (void)GetCursorPos((LPPOINT)&mp);
2369 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
2370}
2371
2372 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00002373gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374{
2375 vimmenu_T *menu = gui_find_menu(path_name);
2376
2377 if (menu != NULL)
2378 {
2379 POINT p;
2380
2381 /* Find the position of the current cursor */
2382 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00002383 if (mouse_pos)
2384 {
2385 int mx, my;
2386
2387 gui_mch_getmouse(&mx, &my);
2388 p.x += mx;
2389 p.y += my;
2390 }
2391 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 {
2393 p.x += TEXT_X(W_WINCOL(curwin) + curwin->w_wcol + 1);
2394 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
2395 }
2396 msg_scroll = FALSE;
2397 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
2398 }
2399}
2400
2401#if defined(FEAT_TEAROFF) || defined(PROTO)
2402/*
2403 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
2404 * create it as a pseudo-"tearoff menu".
2405 */
2406 void
2407gui_make_tearoff(char_u *path_name)
2408{
2409 vimmenu_T *menu = gui_find_menu(path_name);
2410
2411 /* Found the menu, so tear it off. */
2412 if (menu != NULL)
2413 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
2414}
2415#endif
2416
2417/*
2418 * Add a menu item to a menu
2419 */
2420 void
2421gui_mch_add_menu_item(
2422 vimmenu_T *menu,
2423 int idx)
2424{
2425 vimmenu_T *parent = menu->parent;
2426
2427 menu->id = s_menu_id++;
2428 menu->submenu_id = NULL;
2429
2430#ifdef FEAT_TEAROFF
2431 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
2432 {
2433 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
2434 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
2435 }
2436 else
2437#endif
2438#ifdef FEAT_TOOLBAR
2439 if (menu_is_toolbar(parent->name))
2440 {
2441 TBBUTTON newtb;
2442
2443 vim_memset(&newtb, 0, sizeof(newtb));
2444 if (menu_is_separator(menu->name))
2445 {
2446 newtb.iBitmap = 0;
2447 newtb.fsStyle = TBSTYLE_SEP;
2448 }
2449 else
2450 {
2451 newtb.iBitmap = get_toolbar_bitmap(menu);
2452 newtb.fsStyle = TBSTYLE_BUTTON;
2453 }
2454 newtb.idCommand = menu->id;
2455 newtb.fsState = TBSTATE_ENABLED;
2456 newtb.iString = 0;
2457 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
2458 (LPARAM)&newtb);
2459 menu->submenu_id = (HMENU)-1;
2460 }
2461 else
2462#endif
2463 {
2464#ifdef FEAT_MBYTE
2465 WCHAR *wn = NULL;
2466 int n;
2467
2468 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2469 {
2470 /* 'encoding' differs from active codepage: convert menu item name
2471 * and use wide function */
2472 wn = enc_to_ucs2(menu->name, NULL);
2473 if (wn != NULL)
2474 {
2475 n = InsertMenuW(parent->submenu_id, (UINT)idx,
2476 (menu_is_separator(menu->name)
2477 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
2478 (UINT)menu->id, wn);
2479 vim_free(wn);
2480 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2481 /* Failed, try using non-wide function. */
2482 wn = NULL;
2483 }
2484 }
2485 if (wn == NULL)
2486#endif
2487 InsertMenu(parent->submenu_id, (UINT)idx,
2488 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
2489 | MF_BYPOSITION,
2490 (UINT)menu->id, (LPCTSTR)menu->name);
2491#ifdef FEAT_TEAROFF
2492 if (IsWindow(parent->tearoff_handle))
2493 rebuild_tearoff(parent);
2494#endif
2495 }
2496}
2497
2498/*
2499 * Destroy the machine specific menu widget.
2500 */
2501 void
2502gui_mch_destroy_menu(vimmenu_T *menu)
2503{
2504#ifdef FEAT_TOOLBAR
2505 /*
2506 * is this a toolbar button?
2507 */
2508 if (menu->submenu_id == (HMENU)-1)
2509 {
2510 int iButton;
2511
2512 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
2513 (WPARAM)menu->id, 0);
2514 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
2515 }
2516 else
2517#endif
2518 {
2519 if (menu->parent != NULL
2520 && menu_is_popup(menu->parent->dname)
2521 && menu->parent->submenu_id != NULL)
2522 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
2523 else
2524 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
2525 if (menu->submenu_id != NULL)
2526 DestroyMenu(menu->submenu_id);
2527#ifdef FEAT_TEAROFF
2528 if (IsWindow(menu->tearoff_handle))
2529 DestroyWindow(menu->tearoff_handle);
2530 if (menu->parent != NULL
2531 && menu->parent->children != NULL
2532 && IsWindow(menu->parent->tearoff_handle))
2533 {
2534 /* This menu must not show up when rebuilding the tearoff window. */
2535 menu->modes = 0;
2536 rebuild_tearoff(menu->parent);
2537 }
2538#endif
2539 }
2540}
2541
2542#ifdef FEAT_TEAROFF
2543 static void
2544rebuild_tearoff(vimmenu_T *menu)
2545{
2546 /*hackish*/
2547 char_u tbuf[128];
2548 RECT trect;
2549 RECT rct;
2550 RECT roct;
2551 int x, y;
2552
2553 HWND thwnd = menu->tearoff_handle;
2554
2555 GetWindowText(thwnd, tbuf, 127);
2556 if (GetWindowRect(thwnd, &trect)
2557 && GetWindowRect(s_hwnd, &rct)
2558 && GetClientRect(s_hwnd, &roct))
2559 {
2560 x = trect.left - rct.left;
2561 y = (trect.top - rct.bottom + roct.bottom);
2562 }
2563 else
2564 {
2565 x = y = 0xffffL;
2566 }
2567 DestroyWindow(thwnd);
2568 if (menu->children != NULL)
2569 {
2570 gui_mch_tearoff(tbuf, menu, x, y);
2571 if (IsWindow(menu->tearoff_handle))
2572 (void) SetWindowPos(menu->tearoff_handle,
2573 NULL,
2574 (int)trect.left,
2575 (int)trect.top,
2576 0, 0,
2577 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
2578 }
2579}
2580#endif /* FEAT_TEAROFF */
2581
2582/*
2583 * Make a menu either grey or not grey.
2584 */
2585 void
2586gui_mch_menu_grey(
2587 vimmenu_T *menu,
2588 int grey)
2589{
2590#ifdef FEAT_TOOLBAR
2591 /*
2592 * is this a toolbar button?
2593 */
2594 if (menu->submenu_id == (HMENU)-1)
2595 {
2596 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
2597 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
2598 }
2599 else
2600#endif
2601 if (grey)
2602 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_GRAYED);
2603 else
2604 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2605
2606#ifdef FEAT_TEAROFF
2607 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
2608 {
2609 WORD menuID;
2610 HWND menuHandle;
2611
2612 /*
2613 * A tearoff button has changed state.
2614 */
2615 if (menu->children == NULL)
2616 menuID = (WORD)(menu->id);
2617 else
2618 menuID = (WORD)((DWORD)(menu->submenu_id) | (DWORD)0x8000);
2619 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
2620 if (menuHandle)
2621 EnableWindow(menuHandle, !grey);
2622
2623 }
2624#endif
2625}
2626
2627#endif /* FEAT_MENU */
2628
2629
2630/* define some macros used to make the dialogue creation more readable */
2631
2632#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
2633#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00002634#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635
2636#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2637/*
2638 * stuff for dialogs
2639 */
2640
2641/*
2642 * The callback routine used by all the dialogs. Very simple. First,
2643 * acknowledges the INITDIALOG message so that Windows knows to do standard
2644 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
2645 * pressed, return that button's ID - IDCANCEL (2), which is the button's
2646 * number.
2647 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002648/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 static LRESULT CALLBACK
2650dialog_callback(
2651 HWND hwnd,
2652 UINT message,
2653 WPARAM wParam,
2654 LPARAM lParam)
2655{
2656 if (message == WM_INITDIALOG)
2657 {
2658 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
2659 /* Set focus to the dialog. Set the default button, if specified. */
2660 (void)SetFocus(hwnd);
2661 if (dialog_default_button > IDCANCEL)
2662 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
2663 return FALSE;
2664 }
2665
2666 if (message == WM_COMMAND)
2667 {
2668 int button = LOWORD(wParam);
2669
2670 /* Don't end the dialog if something was selected that was
2671 * not a button.
2672 */
2673 if (button >= DLG_NONBUTTON_CONTROL)
2674 return TRUE;
2675
2676 /* If the edit box exists, copy the string. */
2677 if (s_textfield != NULL)
2678 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
2679 s_textfield, IOSIZE);
2680
2681 /*
2682 * Need to check for IDOK because if the user just hits Return to
2683 * accept the default value, some reason this is what we get.
2684 */
2685 if (button == IDOK)
2686 {
2687 if (dialog_default_button > IDCANCEL)
2688 EndDialog(hwnd, dialog_default_button);
2689 }
2690 else
2691 EndDialog(hwnd, button - IDCANCEL);
2692 return TRUE;
2693 }
2694
2695 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
2696 {
2697 EndDialog(hwnd, 0);
2698 return TRUE;
2699 }
2700 return FALSE;
2701}
2702
2703/*
2704 * Create a dialog dynamically from the parameter strings.
2705 * type = type of dialog (question, alert, etc.)
2706 * title = dialog title. may be NULL for default title.
2707 * message = text to display. Dialog sizes to accommodate it.
2708 * buttons = '\n' separated list of button captions, default first.
2709 * dfltbutton = number of default button.
2710 *
2711 * This routine returns 1 if the first button is pressed,
2712 * 2 for the second, etc.
2713 *
2714 * 0 indicates Esc was pressed.
2715 * -1 for unexpected error
2716 *
2717 * If stubbing out this fn, return 1.
2718 */
2719
2720static const char_u *dlg_icons[] = /* must match names in resource file */
2721{
2722 "IDR_VIM",
2723 "IDR_VIM_ERROR",
2724 "IDR_VIM_ALERT",
2725 "IDR_VIM_INFO",
2726 "IDR_VIM_QUESTION"
2727};
2728
2729#ifdef USE_SYSMENU_FONT
2730/*
2731 * Get Menu Font.
2732 * Return OK or FAIL.
2733 */
2734 static int
2735gui_w32_get_menu_font(LOGFONT *lf)
2736{
2737 NONCLIENTMETRICS nm;
2738
2739 nm.cbSize = sizeof(NONCLIENTMETRICS);
2740 if (!SystemParametersInfo(
2741 SPI_GETNONCLIENTMETRICS,
2742 sizeof(NONCLIENTMETRICS),
2743 &nm,
2744 0))
2745 return FAIL;
2746 *lf = nm.lfMenuFont;
2747 return OK;
2748}
2749#endif
2750
2751 int
2752gui_mch_dialog(
2753 int type,
2754 char_u *title,
2755 char_u *message,
2756 char_u *buttons,
2757 int dfltbutton,
2758 char_u *textfield)
2759{
2760 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00002761 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 int numButtons;
2763 int *buttonWidths, *buttonPositions;
2764 int buttonYpos;
2765 int nchar, i;
2766 DWORD lStyle;
2767 int dlgwidth = 0;
2768 int dlgheight;
2769 int editboxheight;
2770 int horizWidth = 0;
2771 int msgheight;
2772 char_u *pstart;
2773 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002774 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 char_u *tbuffer;
2776 RECT rect;
2777 HWND hwnd;
2778 HDC hdc;
2779 HFONT font, oldFont;
2780 TEXTMETRIC fontInfo;
2781 int fontHeight;
2782 int textWidth, minButtonWidth, messageWidth;
2783 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002784 int maxDialogHeight;
2785 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 int vertical;
2787 int dlgPaddingX;
2788 int dlgPaddingY;
2789#ifdef USE_SYSMENU_FONT
2790 LOGFONT lfSysmenu;
2791 int use_lfSysmenu = FALSE;
2792#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002793 garray_T ga;
2794 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795
2796#ifndef NO_CONSOLE
2797 /* Don't output anything in silent mode ("ex -s") */
2798 if (silent_mode)
2799 return dfltbutton; /* return default option */
2800#endif
2801
Bram Moolenaar748bf032005-02-02 23:04:36 +00002802#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 /* If there is no window yet, open it. */
2804 if (s_hwnd == NULL && gui_mch_init() == FAIL)
2805 return dfltbutton;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002806#else
2807 if (s_hwnd == NULL)
2808 get_dialog_font_metrics();
2809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810
2811 if ((type < 0) || (type > VIM_LAST_TYPE))
2812 type = 0;
2813
2814 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002815 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002816 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
2817 DLG_ALLOC_SIZE + STRLEN(message));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818
2819 if (p == NULL)
2820 return -1;
2821
2822 /*
2823 * make a copy of 'buttons' to fiddle with it. complier grizzles because
2824 * vim_strsave() doesn't take a const arg (why not?), so cast away the
2825 * const.
2826 */
2827 tbuffer = vim_strsave(buttons);
2828 if (tbuffer == NULL)
2829 return -1;
2830
2831 --dfltbutton; /* Change from one-based to zero-based */
2832
2833 /* Count buttons */
2834 numButtons = 1;
2835 for (i = 0; tbuffer[i] != '\0'; i++)
2836 {
2837 if (tbuffer[i] == DLG_BUTTON_SEP)
2838 numButtons++;
2839 }
2840 if (dfltbutton >= numButtons)
2841 dfltbutton = -1;
2842
2843 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002844 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 if (buttonWidths == NULL)
2846 return -1;
2847
2848 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002849 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 if (buttonPositions == NULL)
2851 return -1;
2852
2853 /*
2854 * Calculate how big the dialog must be.
2855 */
2856 hwnd = GetDesktopWindow();
2857 hdc = GetWindowDC(hwnd);
2858#ifdef USE_SYSMENU_FONT
2859 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
2860 {
2861 font = CreateFontIndirect(&lfSysmenu);
2862 use_lfSysmenu = TRUE;
2863 }
2864 else
2865#endif
2866 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2867 VARIABLE_PITCH , DLG_FONT_NAME);
2868 if (s_usenewlook)
2869 {
2870 oldFont = SelectFont(hdc, font);
2871 dlgPaddingX = DLG_PADDING_X;
2872 dlgPaddingY = DLG_PADDING_Y;
2873 }
2874 else
2875 {
2876 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
2877 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
2878 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
2879 }
2880 GetTextMetrics(hdc, &fontInfo);
2881 fontHeight = fontInfo.tmHeight;
2882
2883 /* Minimum width for horizontal button */
2884 minButtonWidth = GetTextWidth(hdc, "Cancel", 6);
2885
2886 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002887 if (s_hwnd == NULL)
2888 {
2889 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890
Bram Moolenaarc716c302006-01-21 22:12:51 +00002891 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002892 get_work_area(&workarea_rect);
2893 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
2894 if (maxDialogWidth > 600)
2895 maxDialogWidth = 600;
2896 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 100;
2897 }
2898 else
2899 {
2900 /* Use our own window for the size, unless it's very small. */
2901 GetWindowRect(s_hwnd, &rect);
2902 maxDialogWidth = rect.right - rect.left
2903 - GetSystemMetrics(SM_CXFRAME) * 2;
2904 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
2905 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002906
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002907 maxDialogHeight = rect.bottom - rect.top
2908 - GetSystemMetrics(SM_CXFRAME) * 2;
2909 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
2910 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
2911 }
2912
2913 /* Set dlgwidth to width of message.
2914 * Copy the message into "ga", changing NL to CR-NL and inserting line
2915 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 pstart = message;
2917 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002918 msgheight = 0;
2919 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 do
2921 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002922 msgheight += fontHeight; /* at least one line */
2923
2924 /* Need to figure out where to break the string. The system does it
2925 * at a word boundary, which would mean we can't compute the number of
2926 * wrapped lines. */
2927 textWidth = 0;
2928 last_white = NULL;
2929 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00002930 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002931#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002932 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002933#else
2934 l = 1;
2935#endif
2936 if (l == 1 && vim_iswhite(*pend)
2937 && textWidth > maxDialogWidth * 3 / 4)
2938 last_white = pend;
2939 textWidth += GetTextWidth(hdc, pend, l);
2940 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00002941 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002942 /* Line will wrap. */
2943 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002944 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002945 textWidth = 0;
2946
2947 if (last_white != NULL)
2948 {
2949 /* break the line just after a space */
2950 ga.ga_len -= pend - (last_white + 1);
2951 pend = last_white + 1;
2952 last_white = NULL;
2953 }
2954 ga_append(&ga, '\r');
2955 ga_append(&ga, '\n');
2956 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002957 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002958
2959 while (--l >= 0)
2960 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00002961 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002962 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002964
2965 ga_append(&ga, '\r');
2966 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 pstart = pend + 1;
2968 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00002969
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002970 if (ga.ga_data != NULL)
2971 message = ga.ga_data;
2972
Bram Moolenaar748bf032005-02-02 23:04:36 +00002973 messageWidth += 10; /* roundoff space */
2974
2975 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
2976 if (msgheight > maxDialogHeight)
2977 {
2978 msgheight = maxDialogHeight;
2979 scroll_flag = WS_VSCROLL;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002980 messageWidth += GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00002981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982
2983 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaar748bf032005-02-02 23:04:36 +00002984 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985
2986 if (msgheight < DLG_ICON_HEIGHT)
2987 msgheight = DLG_ICON_HEIGHT;
2988
2989 /*
2990 * Check button names. A long one will make the dialog wider.
2991 */
2992 vertical = (vim_strchr(p_go, GO_VERTICAL) != NULL);
2993 if (!vertical)
2994 {
2995 // Place buttons horizontally if they fit.
2996 horizWidth = dlgPaddingX;
2997 pstart = tbuffer;
2998 i = 0;
2999 do
3000 {
3001 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
3002 if (pend == NULL)
3003 pend = pstart + STRLEN(pstart); // Last button name.
3004 textWidth = GetTextWidth(hdc, pstart, (int)(pend - pstart));
3005 if (textWidth < minButtonWidth)
3006 textWidth = minButtonWidth;
3007 textWidth += dlgPaddingX; /* Padding within button */
3008 buttonWidths[i] = textWidth;
3009 buttonPositions[i++] = horizWidth;
3010 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
3011 pstart = pend + 1;
3012 } while (*pend != NUL);
3013
3014 if (horizWidth > maxDialogWidth)
3015 vertical = TRUE; // Too wide to fit on the screen.
3016 else if (horizWidth > dlgwidth)
3017 dlgwidth = horizWidth;
3018 }
3019
3020 if (vertical)
3021 {
3022 // Stack buttons vertically.
3023 pstart = tbuffer;
3024 do
3025 {
3026 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
3027 if (pend == NULL)
3028 pend = pstart + STRLEN(pstart); // Last button name.
3029 textWidth = GetTextWidth(hdc, pstart, (int)(pend - pstart));
3030 textWidth += dlgPaddingX; /* Padding within button */
3031 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
3032 if (textWidth > dlgwidth)
3033 dlgwidth = textWidth;
3034 pstart = pend + 1;
3035 } while (*pend != NUL);
3036 }
3037
3038 if (dlgwidth < DLG_MIN_WIDTH)
3039 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
3040
3041 /* start to fill in the dlgtemplate information. addressing by WORDs */
3042 if (s_usenewlook)
3043 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
3044 else
3045 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
3046
3047 add_long(lStyle);
3048 add_long(0); // (lExtendedStyle)
3049 pnumitems = p; /*save where the number of items must be stored*/
3050 add_word(0); // NumberOfItems(will change later)
3051 add_word(10); // x
3052 add_word(10); // y
3053 add_word(PixelToDialogX(dlgwidth)); // cx
3054
3055 // Dialog height.
3056 if (vertical)
3057 dlgheight = msgheight + 2 * dlgPaddingY +
3058 DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
3059 else
3060 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
3061
3062 // Dialog needs to be taller if contains an edit box.
3063 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
3064 if (textfield != NULL)
3065 dlgheight += editboxheight;
3066
3067 add_word(PixelToDialogY(dlgheight));
3068
3069 add_word(0); // Menu
3070 add_word(0); // Class
3071
3072 /* copy the title of the dialog */
3073 nchar = nCopyAnsiToWideChar(p, (title ?
3074 (LPSTR)title :
3075 (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
3076 p += nchar;
3077
3078 if (s_usenewlook)
3079 {
3080 /* do the font, since DS_3DLOOK doesn't work properly */
3081#ifdef USE_SYSMENU_FONT
3082 if (use_lfSysmenu)
3083 {
3084 /* point size */
3085 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
3086 GetDeviceCaps(hdc, LOGPIXELSY));
3087 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
3088 }
3089 else
3090#endif
3091 {
3092 *p++ = DLG_FONT_POINT_SIZE; // point size
3093 nchar = nCopyAnsiToWideChar(p, TEXT(DLG_FONT_NAME));
3094 }
3095 p += nchar;
3096 }
3097
3098 buttonYpos = msgheight + 2 * dlgPaddingY;
3099
3100 if (textfield != NULL)
3101 buttonYpos += editboxheight;
3102
3103 pstart = tbuffer;
3104 if (!vertical)
3105 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
3106 for (i = 0; i < numButtons; i++)
3107 {
3108 /* get end of this button. */
3109 for ( pend = pstart;
3110 *pend && (*pend != DLG_BUTTON_SEP);
3111 pend++)
3112 ;
3113
3114 if (*pend)
3115 *pend = '\0';
3116
3117 /*
3118 * old NOTE:
3119 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
3120 * the focus to the first tab-able button and in so doing makes that
3121 * the default!! Grrr. Workaround: Make the default button the only
3122 * one with WS_TABSTOP style. Means user can't tab between buttons, but
3123 * he/she can use arrow keys.
3124 *
3125 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
3126 * right buttun when hitting <Enter>. E.g., for the ":confirm quit"
3127 * dialog. Also needed for when the textfield is the default control.
3128 * It appears to work now (perhaps not on Win95?).
3129 */
3130 if (vertical)
3131 {
3132 p = add_dialog_element(p,
3133 (i == dfltbutton
3134 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
3135 PixelToDialogX(DLG_VERT_PADDING_X),
3136 PixelToDialogY(buttonYpos /* TBK */
3137 + 2 * fontHeight * i),
3138 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
3139 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
3140 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, pstart);
3141 }
3142 else
3143 {
3144 p = add_dialog_element(p,
3145 (i == dfltbutton
3146 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
3147 PixelToDialogX(horizWidth + buttonPositions[i]),
3148 PixelToDialogY(buttonYpos), /* TBK */
3149 PixelToDialogX(buttonWidths[i]),
3150 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
3151 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, pstart);
3152 }
3153 pstart = pend + 1; /*next button*/
3154 }
3155 *pnumitems += numButtons;
3156
3157 /* Vim icon */
3158 p = add_dialog_element(p, SS_ICON,
3159 PixelToDialogX(dlgPaddingX),
3160 PixelToDialogY(dlgPaddingY),
3161 PixelToDialogX(DLG_ICON_WIDTH),
3162 PixelToDialogY(DLG_ICON_HEIGHT),
3163 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
3164 dlg_icons[type]);
3165
Bram Moolenaar748bf032005-02-02 23:04:36 +00003166#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 /* Dialog message */
3168 p = add_dialog_element(p, SS_LEFT,
3169 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
3170 PixelToDialogY(dlgPaddingY),
3171 (WORD)(PixelToDialogX(messageWidth) + 1),
3172 PixelToDialogY(msgheight),
3173 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0082, message);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003174#else
3175 /* Dialog message */
3176 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
3177 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
3178 PixelToDialogY(dlgPaddingY),
3179 (WORD)(PixelToDialogX(messageWidth) + 1),
3180 PixelToDialogY(msgheight),
3181 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, message);
3182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183
3184 /* Edit box */
3185 if (textfield != NULL)
3186 {
3187 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
3188 PixelToDialogX(2 * dlgPaddingX),
3189 PixelToDialogY(2 * dlgPaddingY + msgheight),
3190 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
3191 PixelToDialogY(fontHeight + dlgPaddingY),
3192 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, textfield);
3193 *pnumitems += 1;
3194 }
3195
3196 *pnumitems += 2;
3197
3198 SelectFont(hdc, oldFont);
3199 DeleteObject(font);
3200 ReleaseDC(hwnd, hdc);
3201
3202 /* Let the dialog_callback() function know which button to make default
3203 * If we have an edit box, make that the default. We also need to tell
3204 * dialog_callback() if this dialog contains an edit box or not. We do
3205 * this by setting s_textfield if it does.
3206 */
3207 if (textfield != NULL)
3208 {
3209 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
3210 s_textfield = textfield;
3211 }
3212 else
3213 {
3214 dialog_default_button = IDCANCEL + 1 + dfltbutton;
3215 s_textfield = NULL;
3216 }
3217
3218 /* show the dialog box modally and get a return value */
3219 nchar = (int)DialogBoxIndirect(
3220 s_hinst,
3221 (LPDLGTEMPLATE)pdlgtemplate,
3222 s_hwnd,
3223 (DLGPROC)dialog_callback);
3224
3225 LocalFree(LocalHandle(pdlgtemplate));
3226 vim_free(tbuffer);
3227 vim_free(buttonWidths);
3228 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003229 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230
3231 /* Focus back to our window (for when MDI is used). */
3232 (void)SetFocus(s_hwnd);
3233
3234 return nchar;
3235}
3236
3237#endif /* FEAT_GUI_DIALOG */
3238/*
3239 * Put a simple element (basic class) onto a dialog template in memory.
3240 * return a pointer to where the next item should be added.
3241 *
3242 * parameters:
3243 * lStyle = additional style flags
3244 * (be careful, NT3.51 & Win32s will ignore the new ones)
3245 * x,y = x & y positions IN DIALOG UNITS
3246 * w,h = width and height IN DIALOG UNITS
3247 * Id = ID used in messages
3248 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
3249 * caption = usually text or resource name
3250 *
3251 * TODO: use the length information noted here to enable the dialog creation
3252 * routines to work out more exactly how much memory they need to alloc.
3253 */
3254 static PWORD
3255add_dialog_element(
3256 PWORD p,
3257 DWORD lStyle,
3258 WORD x,
3259 WORD y,
3260 WORD w,
3261 WORD h,
3262 WORD Id,
3263 WORD clss,
3264 const char *caption)
3265{
3266 int nchar;
3267
3268 p = lpwAlign(p); /* Align to dword boundary*/
3269 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
3270 *p++ = LOWORD(lStyle);
3271 *p++ = HIWORD(lStyle);
3272 *p++ = 0; // LOWORD (lExtendedStyle)
3273 *p++ = 0; // HIWORD (lExtendedStyle)
3274 *p++ = x;
3275 *p++ = y;
3276 *p++ = w;
3277 *p++ = h;
3278 *p++ = Id; //9 or 10 words in all
3279
3280 *p++ = (WORD)0xffff;
3281 *p++ = clss; //2 more here
3282
3283 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption); //strlen(caption)+1
3284 p += nchar;
3285
3286 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
3287
3288 return p; //total = 15+ (strlen(caption)) words
3289 // = 30 + 2(strlen(caption) bytes reqd
3290}
3291
3292
3293/*
3294 * Helper routine. Take an input pointer, return closest pointer that is
3295 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
3296 */
3297 static LPWORD
3298lpwAlign(
3299 LPWORD lpIn)
3300{
3301 ULONG ul;
3302
3303 ul = (ULONG)lpIn;
3304 ul += 3;
3305 ul >>= 2;
3306 ul <<= 2;
3307 return (LPWORD)ul;
3308}
3309
3310/*
3311 * Helper routine. Takes second parameter as Ansi string, copies it to first
3312 * parameter as wide character (16-bits / char) string, and returns integer
3313 * number of wide characters (words) in string (including the trailing wide
3314 * char NULL). Partly taken from the Win32SDK samples.
3315 */
3316 static int
3317nCopyAnsiToWideChar(
3318 LPWORD lpWCStr,
3319 LPSTR lpAnsiIn)
3320{
3321 int nChar = 0;
3322#ifdef FEAT_MBYTE
3323 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
3324 int i;
3325 WCHAR *wn;
3326
3327 if (enc_codepage == 0 && (int)GetACP() != enc_codepage)
3328 {
3329 /* Not a codepage, use our own conversion function. */
3330 wn = enc_to_ucs2(lpAnsiIn, NULL);
3331 if (wn != NULL)
3332 {
3333 wcscpy(lpWCStr, wn);
3334 nChar = wcslen(wn) + 1;
3335 vim_free(wn);
3336 }
3337 }
3338 if (nChar == 0)
3339 /* Use Win32 conversion function. */
3340 nChar = MultiByteToWideChar(
3341 enc_codepage > 0 ? enc_codepage : CP_ACP,
3342 MB_PRECOMPOSED,
3343 lpAnsiIn, len,
3344 lpWCStr, len);
3345 for (i = 0; i < nChar; ++i)
3346 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
3347 lpWCStr[i] = (WORD)' ';
3348#else
3349 do
3350 {
3351 if (*lpAnsiIn == '\t')
3352 *lpWCStr++ = (WORD)' ';
3353 else
3354 *lpWCStr++ = (WORD)*lpAnsiIn;
3355 nChar++;
3356 } while (*lpAnsiIn++);
3357#endif
3358
3359 return nChar;
3360}
3361
3362
3363#ifdef FEAT_TEAROFF
3364/*
3365 * The callback function for all the modeless dialogs that make up the
3366 * "tearoff menus" Very simple - forward button presses (to fool Vim into
3367 * thinking its menus have been clicked), and go away when closed.
3368 */
3369 static LRESULT CALLBACK
3370tearoff_callback(
3371 HWND hwnd,
3372 UINT message,
3373 WPARAM wParam,
3374 LPARAM lParam)
3375{
3376 if (message == WM_INITDIALOG)
3377 return (TRUE);
3378
3379 /* May show the mouse pointer again. */
3380 HandleMouseHide(message, lParam);
3381
3382 if (message == WM_COMMAND)
3383 {
3384 if ((WORD)(LOWORD(wParam)) & 0x8000)
3385 {
3386 POINT mp;
3387 RECT rect;
3388
3389 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
3390 {
3391 (void)TrackPopupMenu(
3392 (HMENU)(LOWORD(wParam) ^ 0x8000),
3393 TPM_LEFTALIGN | TPM_LEFTBUTTON,
3394 (int)rect.right - 8,
3395 (int)mp.y,
3396 (int)0, /*reserved param*/
3397 s_hwnd,
3398 NULL);
3399 /*
3400 * NOTE: The pop-up menu can eat the mouse up event.
3401 * We deal with this in normal.c.
3402 */
3403 }
3404 }
3405 else
3406 /* Pass on messages to the main Vim window */
3407 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
3408 /*
3409 * Give main window the focus back: this is so after
3410 * choosing a tearoff button you can start typing again
3411 * straight away.
3412 */
3413 (void)SetFocus(s_hwnd);
3414 return TRUE;
3415 }
3416 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
3417 {
3418 DestroyWindow(hwnd);
3419 return TRUE;
3420 }
3421
3422 /* When moved around, give main window the focus back. */
3423 if (message == WM_EXITSIZEMOVE)
3424 (void)SetActiveWindow(s_hwnd);
3425
3426 return FALSE;
3427}
3428#endif
3429
3430
3431/*
3432 * Decide whether to use the "new look" (small, non-bold font) or the "old
3433 * look" (big, clanky font) for dialogs, and work out a few values for use
3434 * later accordingly.
3435 */
3436 static void
3437get_dialog_font_metrics(void)
3438{
3439 HDC hdc;
3440 HFONT hfontTools = 0;
3441 DWORD dlgFontSize;
3442 SIZE size;
3443#ifdef USE_SYSMENU_FONT
3444 LOGFONT lfSysmenu;
3445#endif
3446
3447 s_usenewlook = FALSE;
3448
3449 /*
3450 * For NT3.51 and Win32s, we stick with the old look
3451 * because it matches everything else.
3452 */
3453 if (!is_winnt_3())
3454 {
3455#ifdef USE_SYSMENU_FONT
3456 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
3457 hfontTools = CreateFontIndirect(&lfSysmenu);
3458 else
3459#endif
3460 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
3461 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
3462
3463 if (hfontTools)
3464 {
3465 hdc = GetDC(s_hwnd);
3466 SelectObject(hdc, hfontTools);
3467 /*
3468 * GetTextMetrics() doesn't return the right value in
3469 * tmAveCharWidth, so we have to figure out the dialog base units
3470 * ourselves.
3471 */
3472 GetTextExtentPoint(hdc,
3473 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
3474 52, &size);
3475 ReleaseDC(s_hwnd, hdc);
3476
3477 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
3478 s_dlgfntheight = (WORD)size.cy;
3479 s_usenewlook = TRUE;
3480 }
3481 }
3482
3483 if (!s_usenewlook)
3484 {
3485 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
3486 s_dlgfntwidth = LOWORD(dlgFontSize);
3487 s_dlgfntheight = HIWORD(dlgFontSize);
3488 }
3489}
3490
3491#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
3492/*
3493 * Create a pseudo-"tearoff menu" based on the child
3494 * items of a given menu pointer.
3495 */
3496 static void
3497gui_mch_tearoff(
3498 char_u *title,
3499 vimmenu_T *menu,
3500 int initX,
3501 int initY)
3502{
3503 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
3504 int template_len;
3505 int nchar, textWidth, submenuWidth;
3506 DWORD lStyle;
3507 DWORD lExtendedStyle;
3508 WORD dlgwidth;
3509 WORD menuID;
3510 vimmenu_T *pmenu;
3511 vimmenu_T *the_menu = menu;
3512 HWND hwnd;
3513 HDC hdc;
3514 HFONT font, oldFont;
3515 int col, spaceWidth, len;
3516 int columnWidths[2];
3517 char_u *label, *text;
3518 int acLen = 0;
3519 int nameLen;
3520 int padding0, padding1, padding2 = 0;
3521 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003522 int x;
3523 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524#ifdef USE_SYSMENU_FONT
3525 LOGFONT lfSysmenu;
3526 int use_lfSysmenu = FALSE;
3527#endif
3528
3529 /*
3530 * If this menu is already torn off, move it to the mouse position.
3531 */
3532 if (IsWindow(menu->tearoff_handle))
3533 {
3534 POINT mp;
3535 if (GetCursorPos((LPPOINT)&mp))
3536 {
3537 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
3538 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
3539 }
3540 return;
3541 }
3542
3543 /*
3544 * Create a new tearoff.
3545 */
3546 if (*title == MNU_HIDDEN_CHAR)
3547 title++;
3548
3549 /* Allocate memory to store the dialog template. It's made bigger when
3550 * needed. */
3551 template_len = DLG_ALLOC_SIZE;
3552 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
3553 if (p == NULL)
3554 return;
3555
3556 hwnd = GetDesktopWindow();
3557 hdc = GetWindowDC(hwnd);
3558#ifdef USE_SYSMENU_FONT
3559 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
3560 {
3561 font = CreateFontIndirect(&lfSysmenu);
3562 use_lfSysmenu = TRUE;
3563 }
3564 else
3565#endif
3566 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3567 VARIABLE_PITCH , DLG_FONT_NAME);
3568 if (s_usenewlook)
3569 oldFont = SelectFont(hdc, font);
3570 else
3571 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
3572
3573 /* Calculate width of a single space. Used for padding columns to the
3574 * right width. */
3575 spaceWidth = GetTextWidth(hdc, " ", 1);
3576
3577 /* Figure out max width of the text column, the accelerator column and the
3578 * optional submenu column. */
3579 submenuWidth = 0;
3580 for (col = 0; col < 2; col++)
3581 {
3582 columnWidths[col] = 0;
3583 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
3584 {
3585 /* Use "dname" here to compute the width of the visible text. */
3586 text = (col == 0) ? pmenu->dname : pmenu->actext;
3587 if (text != NULL && *text != NUL)
3588 {
3589 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
3590 if (textWidth > columnWidths[col])
3591 columnWidths[col] = textWidth;
3592 }
3593 if (pmenu->children != NULL)
3594 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
3595 }
3596 }
3597 if (columnWidths[1] == 0)
3598 {
3599 /* no accelerators */
3600 if (submenuWidth != 0)
3601 columnWidths[0] += submenuWidth;
3602 else
3603 columnWidths[0] += spaceWidth;
3604 }
3605 else
3606 {
3607 /* there is an accelerator column */
3608 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
3609 columnWidths[1] += submenuWidth;
3610 }
3611
3612 /*
3613 * Now find the total width of our 'menu'.
3614 */
3615 textWidth = columnWidths[0] + columnWidths[1];
3616 if (submenuWidth != 0)
3617 {
3618 submenuWidth = GetTextWidth(hdc, TEAROFF_SUBMENU_LABEL,
3619 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
3620 textWidth += submenuWidth;
3621 }
3622 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
3623 if (textWidth > dlgwidth)
3624 dlgwidth = textWidth;
3625 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
3626
3627 /* W95 can't do thin dialogs, they look v. weird! */
3628 if (mch_windows95() && dlgwidth < TEAROFF_MIN_WIDTH)
3629 dlgwidth = TEAROFF_MIN_WIDTH;
3630
3631 /* start to fill in the dlgtemplate information. addressing by WORDs */
3632 if (s_usenewlook)
3633 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
3634 else
3635 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
3636
3637 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
3638 *p++ = LOWORD(lStyle);
3639 *p++ = HIWORD(lStyle);
3640 *p++ = LOWORD(lExtendedStyle);
3641 *p++ = HIWORD(lExtendedStyle);
3642 pnumitems = p; /* save where the number of items must be stored */
3643 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003644 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003646 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 else
3648 *p++ = PixelToDialogX(initX); // x
3649 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003650 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 else
3652 *p++ = PixelToDialogY(initY); // y
3653 *p++ = PixelToDialogX(dlgwidth); // cx
3654 ptrueheight = p;
3655 *p++ = 0; // dialog height: changed later anyway
3656 *p++ = 0; // Menu
3657 *p++ = 0; // Class
3658
3659 /* copy the title of the dialog */
3660 nchar = nCopyAnsiToWideChar(p, ((*title)
3661 ? (LPSTR)title
3662 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
3663 p += nchar;
3664
3665 if (s_usenewlook)
3666 {
3667 /* do the font, since DS_3DLOOK doesn't work properly */
3668#ifdef USE_SYSMENU_FONT
3669 if (use_lfSysmenu)
3670 {
3671 /* point size */
3672 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
3673 GetDeviceCaps(hdc, LOGPIXELSY));
3674 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
3675 }
3676 else
3677#endif
3678 {
3679 *p++ = DLG_FONT_POINT_SIZE; // point size
3680 nchar = nCopyAnsiToWideChar (p, TEXT(DLG_FONT_NAME));
3681 }
3682 p += nchar;
3683 }
3684
3685 /*
3686 * Loop over all the items in the menu.
3687 * But skip over the tearbar.
3688 */
3689 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
3690 menu = menu->children->next;
3691 else
3692 menu = menu->children;
3693 for ( ; menu != NULL; menu = menu->next)
3694 {
3695 if (menu->modes == 0) /* this menu has just been deleted */
3696 continue;
3697 if (menu_is_separator(menu->dname))
3698 {
3699 sepPadding += 3;
3700 continue;
3701 }
3702
3703 /* Check if there still is plenty of room in the template. Make it
3704 * larger when needed. */
3705 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
3706 {
3707 WORD *newp;
3708
3709 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
3710 if (newp != NULL)
3711 {
3712 template_len += 4096;
3713 mch_memmove(newp, pdlgtemplate,
3714 (char *)p - (char *)pdlgtemplate);
3715 p = newp + (p - pdlgtemplate);
3716 pnumitems = newp + (pnumitems - pdlgtemplate);
3717 ptrueheight = newp + (ptrueheight - pdlgtemplate);
3718 LocalFree(LocalHandle(pdlgtemplate));
3719 pdlgtemplate = newp;
3720 }
3721 }
3722
3723 /* Figure out minimal length of this menu label. Use "name" for the
3724 * actual text, "dname" for estimating the displayed size. "name"
3725 * has "&a" for mnemonic and includes the accelerator. */
3726 len = nameLen = (int)STRLEN(menu->name);
3727 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
3728 (int)STRLEN(menu->dname))) / spaceWidth;
3729 len += padding0;
3730
3731 if (menu->actext != NULL)
3732 {
3733 acLen = (int)STRLEN(menu->actext);
3734 len += acLen;
3735 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
3736 }
3737 else
3738 textWidth = 0;
3739 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
3740 len += padding1;
3741
3742 if (menu->children == NULL)
3743 {
3744 padding2 = submenuWidth / spaceWidth;
3745 len += padding2;
3746 menuID = (WORD)(menu->id);
3747 }
3748 else
3749 {
3750 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
3751 menuID = (WORD)((DWORD)(menu->submenu_id) | (DWORD)0x8000);
3752 }
3753
3754 /* Allocate menu label and fill it in */
3755 text = label = alloc((unsigned)len + 1);
3756 if (label == NULL)
3757 break;
3758
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003759 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 text = vim_strchr(text, TAB); /* stop at TAB before actext */
3761 if (text == NULL)
3762 text = label + nameLen; /* no actext, use whole name */
3763 while (padding0-- > 0)
3764 *text++ = ' ';
3765 if (menu->actext != NULL)
3766 {
3767 STRNCPY(text, menu->actext, acLen);
3768 text += acLen;
3769 }
3770 while (padding1-- > 0)
3771 *text++ = ' ';
3772 if (menu->children != NULL)
3773 {
3774 STRCPY(text, TEAROFF_SUBMENU_LABEL);
3775 text += STRLEN(TEAROFF_SUBMENU_LABEL);
3776 }
3777 else
3778 {
3779 while (padding2-- > 0)
3780 *text++ = ' ';
3781 }
3782 *text = NUL;
3783
3784 /*
3785 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
3786 * W95/NT4 it makes the tear-off look more like a menu.
3787 */
3788 p = add_dialog_element(p,
3789 BS_PUSHBUTTON|BS_LEFT,
3790 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
3791 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
3792 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
3793 (WORD)12,
3794 menuID, (WORD)0x0080, label);
3795 vim_free(label);
3796 (*pnumitems)++;
3797 }
3798
3799 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
3800
3801
3802 /* show modelessly */
3803 the_menu->tearoff_handle = CreateDialogIndirect(
3804 s_hinst,
3805 (LPDLGTEMPLATE)pdlgtemplate,
3806 s_hwnd,
3807 (DLGPROC)tearoff_callback);
3808
3809 LocalFree(LocalHandle(pdlgtemplate));
3810 SelectFont(hdc, oldFont);
3811 DeleteObject(font);
3812 ReleaseDC(hwnd, hdc);
3813
3814 /*
3815 * Reassert ourselves as the active window. This is so that after creating
3816 * a tearoff, the user doesn't have to click with the mouse just to start
3817 * typing again!
3818 */
3819 (void)SetActiveWindow(s_hwnd);
3820
3821 /* make sure the right buttons are enabled */
3822 force_menu_update = TRUE;
3823}
3824#endif
3825
3826#if defined(FEAT_TOOLBAR) || defined(PROTO)
3827#include "gui_w32_rc.h"
3828
3829/* This not defined in older SDKs */
3830# ifndef TBSTYLE_FLAT
3831# define TBSTYLE_FLAT 0x0800
3832# endif
3833
3834/*
3835 * Create the toolbar, initially unpopulated.
3836 * (just like the menu, there are no defaults, it's all
3837 * set up through menu.vim)
3838 */
3839 static void
3840initialise_toolbar(void)
3841{
3842 InitCommonControls();
3843 s_toolbarhwnd = CreateToolbarEx(
3844 s_hwnd,
3845 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
3846 4000, //any old big number
3847 31, //number of images in inital bitmap
3848 s_hinst,
3849 IDR_TOOLBAR1, // id of initial bitmap
3850 NULL,
3851 0, // initial number of buttons
3852 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
3853 TOOLBAR_BUTTON_HEIGHT,
3854 TOOLBAR_BUTTON_WIDTH,
3855 TOOLBAR_BUTTON_HEIGHT,
3856 sizeof(TBBUTTON)
3857 );
3858
3859 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
3860}
3861
3862 static int
3863get_toolbar_bitmap(vimmenu_T *menu)
3864{
3865 int i = -1;
3866
3867 /*
3868 * Check user bitmaps first, unless builtin is specified.
3869 */
3870 if (!is_winnt_3() && !menu->icon_builtin)
3871 {
3872 char_u fname[MAXPATHL];
3873 HANDLE hbitmap = NULL;
3874
3875 if (menu->iconfile != NULL)
3876 {
3877 gui_find_iconfile(menu->iconfile, fname, "bmp");
3878 hbitmap = LoadImage(
3879 NULL,
3880 fname,
3881 IMAGE_BITMAP,
3882 TOOLBAR_BUTTON_WIDTH,
3883 TOOLBAR_BUTTON_HEIGHT,
3884 LR_LOADFROMFILE |
3885 LR_LOADMAP3DCOLORS
3886 );
3887 }
3888
3889 /*
3890 * If the LoadImage call failed, or the "icon=" file
3891 * didn't exist or wasn't specified, try the menu name
3892 */
3893 if (hbitmap == NULL
3894 && (gui_find_bitmap(menu->name, fname, "bmp") == OK))
3895 hbitmap = LoadImage(
3896 NULL,
3897 fname,
3898 IMAGE_BITMAP,
3899 TOOLBAR_BUTTON_WIDTH,
3900 TOOLBAR_BUTTON_HEIGHT,
3901 LR_LOADFROMFILE |
3902 LR_LOADMAP3DCOLORS
3903 );
3904
3905 if (hbitmap != NULL)
3906 {
3907 TBADDBITMAP tbAddBitmap;
3908
3909 tbAddBitmap.hInst = NULL;
3910 tbAddBitmap.nID = (UINT)hbitmap;
3911
3912 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
3913 (WPARAM)1, (LPARAM)&tbAddBitmap);
3914 /* i will be set to -1 if it fails */
3915 }
3916 }
3917 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
3918 i = menu->iconidx;
3919
3920 return i;
3921}
3922#endif
3923
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003924#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3925 static void
3926initialise_tabline(void)
3927{
3928 InitCommonControls();
3929
3930 s_tabhwnd = CreateWindow(WC_TABCONTROL, "", WS_CHILD|TCS_FOCUSNEVER,
3931 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
3932 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
3933}
3934#endif
3935
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
3937/*
3938 * Make the GUI window come to the foreground.
3939 */
3940 void
3941gui_mch_set_foreground(void)
3942{
3943 if (IsIconic(s_hwnd))
3944 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
3945 SetForegroundWindow(s_hwnd);
3946}
3947#endif
3948
3949#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
3950 static void
3951dyn_imm_load(void)
3952{
3953 hLibImm = LoadLibrary("imm32.dll");
3954 if (hLibImm == NULL)
3955 return;
3956
3957 pImmGetCompositionStringA
3958 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
3959 pImmGetCompositionStringW
3960 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
3961 pImmGetContext
3962 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
3963 pImmAssociateContext
3964 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
3965 pImmReleaseContext
3966 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
3967 pImmGetOpenStatus
3968 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
3969 pImmSetOpenStatus
3970 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
3971 pImmGetCompositionFont
3972 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
3973 pImmSetCompositionFont
3974 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
3975 pImmSetCompositionWindow
3976 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
3977 pImmGetConversionStatus
3978 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00003979 pImmSetConversionStatus
3980 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981
3982 if ( pImmGetCompositionStringA == NULL
3983 || pImmGetCompositionStringW == NULL
3984 || pImmGetContext == NULL
3985 || pImmAssociateContext == NULL
3986 || pImmReleaseContext == NULL
3987 || pImmGetOpenStatus == NULL
3988 || pImmSetOpenStatus == NULL
3989 || pImmGetCompositionFont == NULL
3990 || pImmSetCompositionFont == NULL
3991 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00003992 || pImmGetConversionStatus == NULL
3993 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 {
3995 FreeLibrary(hLibImm);
3996 hLibImm = NULL;
3997 pImmGetContext = NULL;
3998 return;
3999 }
4000
4001 return;
4002}
4003
4004# if 0 /* not used */
4005 int
4006dyn_imm_unload(void)
4007{
4008 if (!hLibImm)
4009 return FALSE;
4010 FreeLibrary(hLibImm);
4011 hLibImm = NULL;
4012 return TRUE;
4013}
4014# endif
4015
4016#endif
4017
4018#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
4019
4020# ifdef FEAT_XPM_W32
4021# define IMAGE_XPM 100
4022# endif
4023
4024typedef struct _signicon_t
4025{
4026 HANDLE hImage;
4027 UINT uType;
4028#ifdef FEAT_XPM_W32
4029 HANDLE hShape; /* Mask bitmap handle */
4030#endif
4031} signicon_t;
4032
4033 void
4034gui_mch_drawsign(row, col, typenr)
4035 int row;
4036 int col;
4037 int typenr;
4038{
4039 signicon_t *sign;
4040 int x, y, w, h;
4041
4042 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
4043 return;
4044
4045 x = TEXT_X(col);
4046 y = TEXT_Y(row);
4047 w = gui.char_width * 2;
4048 h = gui.char_height;
4049 switch (sign->uType)
4050 {
4051 case IMAGE_BITMAP:
4052 {
4053 HDC hdcMem;
4054 HBITMAP hbmpOld;
4055
4056 hdcMem = CreateCompatibleDC(s_hdc);
4057 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
4058 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
4059 SelectObject(hdcMem, hbmpOld);
4060 DeleteDC(hdcMem);
4061 }
4062 break;
4063 case IMAGE_ICON:
4064 case IMAGE_CURSOR:
4065 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
4066 break;
4067#ifdef FEAT_XPM_W32
4068 case IMAGE_XPM:
4069 {
4070 HDC hdcMem;
4071 HBITMAP hbmpOld;
4072
4073 hdcMem = CreateCompatibleDC(s_hdc);
4074 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
4075 /* Make hole */
4076 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
4077
4078 SelectObject(hdcMem, sign->hImage);
4079 /* Paint sign */
4080 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
4081 SelectObject(hdcMem, hbmpOld);
4082 DeleteDC(hdcMem);
4083 }
4084 break;
4085#endif
4086 }
4087}
4088
4089 static void
4090close_signicon_image(signicon_t *sign)
4091{
4092 if (sign)
4093 switch (sign->uType)
4094 {
4095 case IMAGE_BITMAP:
4096 DeleteObject((HGDIOBJ)sign->hImage);
4097 break;
4098 case IMAGE_CURSOR:
4099 DestroyCursor((HCURSOR)sign->hImage);
4100 break;
4101 case IMAGE_ICON:
4102 DestroyIcon((HICON)sign->hImage);
4103 break;
4104#ifdef FEAT_XPM_W32
4105 case IMAGE_XPM:
4106 DeleteObject((HBITMAP)sign->hImage);
4107 DeleteObject((HBITMAP)sign->hShape);
4108 break;
4109#endif
4110 }
4111}
4112
4113 void *
4114gui_mch_register_sign(signfile)
4115 char_u *signfile;
4116{
4117 signicon_t sign, *psign;
4118 char_u *ext;
4119
4120 if (is_winnt_3())
4121 {
4122 EMSG(_(e_signdata));
4123 return NULL;
4124 }
4125
4126 sign.hImage = NULL;
4127 ext = signfile + STRLEN(signfile) - 4; /* get extention */
4128 if (ext > signfile)
4129 {
4130 int do_load = 1;
4131
4132 if (!STRICMP(ext, ".bmp"))
4133 sign.uType = IMAGE_BITMAP;
4134 else if (!STRICMP(ext, ".ico"))
4135 sign.uType = IMAGE_ICON;
4136 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
4137 sign.uType = IMAGE_CURSOR;
4138 else
4139 do_load = 0;
4140
4141 if (do_load)
4142 sign.hImage = (HANDLE)LoadImage(NULL, signfile, sign.uType,
4143 gui.char_width * 2, gui.char_height,
4144 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
4145#ifdef FEAT_XPM_W32
4146 if (!STRICMP(ext, ".xpm"))
4147 {
4148 sign.uType = IMAGE_XPM;
4149 LoadXpmImage(signfile, (HBITMAP *)&sign.hImage, (HBITMAP *)&sign.hShape);
4150 }
4151#endif
4152 }
4153
4154 psign = NULL;
4155 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
4156 != NULL)
4157 *psign = sign;
4158
4159 if (!psign)
4160 {
4161 if (sign.hImage)
4162 close_signicon_image(&sign);
4163 EMSG(_(e_signdata));
4164 }
4165 return (void *)psign;
4166
4167}
4168
4169 void
4170gui_mch_destroy_sign(sign)
4171 void *sign;
4172{
4173 if (sign)
4174 {
4175 close_signicon_image((signicon_t *)sign);
4176 vim_free(sign);
4177 }
4178}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180
4181#if defined(FEAT_BEVAL) || defined(PROTO)
4182
4183/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004184 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 *
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004186 * The only reused thing is gui_beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 * from gui_beval.c (note it uses x and y of the BalloonEval struct
4188 * to get current mouse position).
4189 *
4190 * Trying to use as more Windows services as possible, and as less
4191 * IE version as possible :)).
4192 *
4193 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
4194 * BalloonEval struct.
4195 * 2) Enable/Disable simply create/kill BalloonEval Timer
4196 * 3) When there was enough inactivity, timer procedure posts
4197 * async request to debugger
4198 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
4199 * and performs some actions to show it ASAP
4200 * 5) WM_NOTOFY:TTN_POP destroys created tooltip
4201 */
4202
Bram Moolenaar45360022005-07-21 21:08:21 +00004203/*
4204 * determine whether installed Common Controls support multiline tooltips
4205 * (i.e. their version is >= 4.70
4206 */
4207 int
4208multiline_balloon_available(void)
4209{
4210 HINSTANCE hDll;
4211 static char comctl_dll[] = "comctl32.dll";
4212 static int multiline_tip = MAYBE;
4213
4214 if (multiline_tip != MAYBE)
4215 return multiline_tip;
4216
4217 hDll = GetModuleHandle(comctl_dll);
4218 if (hDll != NULL)
4219 {
4220 DLLGETVERSIONPROC pGetVer;
4221 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
4222
4223 if (pGetVer != NULL)
4224 {
4225 DLLVERSIONINFO dvi;
4226 HRESULT hr;
4227
4228 ZeroMemory(&dvi, sizeof(dvi));
4229 dvi.cbSize = sizeof(dvi);
4230
4231 hr = (*pGetVer)(&dvi);
4232
4233 if (SUCCEEDED(hr)
4234 && (dvi.dwMajorVersion > 4
4235 || (dvi.dwMajorVersion == 4 && dvi.dwMinorVersion >= 70)))
4236 {
4237 multiline_tip = TRUE;
4238 return multiline_tip;
4239 }
4240 }
4241 else
4242 {
4243 /* there is chance we have ancient CommCtl 4.70
4244 which doesn't export DllGetVersion */
4245 DWORD dwHandle = 0;
4246 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
4247 if (len > 0)
4248 {
4249 VS_FIXEDFILEINFO *ver;
4250 UINT vlen = 0;
4251 void *data = alloc(len);
4252
4253 if (data != NULL
4254 && GetFileVersionInfo(comctl_dll, 0, len, data)
4255 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
4256 && vlen
4257 && HIWORD(ver->dwFileVersionMS) > 4
4258 || (HIWORD(ver->dwFileVersionMS) == 4
4259 && LOWORD(ver->dwFileVersionMS) >= 70))
4260 {
4261 vim_free(data);
4262 multiline_tip = TRUE;
4263 return multiline_tip;
4264 }
4265 vim_free(data);
4266 }
4267 }
4268 }
4269 multiline_tip = FALSE;
4270 return multiline_tip;
4271}
4272
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 static void
4274make_tooltip(beval, text, pt)
4275 BalloonEval *beval;
4276 char *text;
4277 POINT pt;
4278{
Bram Moolenaar45360022005-07-21 21:08:21 +00004279 TOOLINFO *pti;
4280 int ToolInfoSize;
4281
4282 if (multiline_balloon_available() == TRUE)
4283 ToolInfoSize = sizeof(TOOLINFO_NEW);
4284 else
4285 ToolInfoSize = sizeof(TOOLINFO);
4286
4287 pti = (TOOLINFO *)alloc(ToolInfoSize);
4288 if (pti == NULL)
4289 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290
4291 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
4292 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
4293 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4294 beval->target, NULL, s_hinst, NULL);
4295
4296 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
4297 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
4298
Bram Moolenaar45360022005-07-21 21:08:21 +00004299 pti->cbSize = ToolInfoSize;
4300 pti->uFlags = TTF_SUBCLASS;
4301 pti->hwnd = beval->target;
4302 pti->hinst = 0; /* Don't use string resources */
4303 pti->uId = ID_BEVAL_TOOLTIP;
4304
4305 if (multiline_balloon_available() == TRUE)
4306 {
4307 RECT rect;
4308 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
4309 pti->lpszText = LPSTR_TEXTCALLBACK;
4310 ptin->lParam = (LPARAM)text;
4311 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
4312 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
4313 (LPARAM)rect.right);
4314 }
4315 else
4316 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317
4318 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00004319 pti->rect.left = pt.x - 3;
4320 pti->rect.top = pt.y - 3;
4321 pti->rect.right = pt.x + 3;
4322 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323
Bram Moolenaar45360022005-07-21 21:08:21 +00004324 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 /* Make tooltip appear sooner */
4326 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
4327 /*
4328 * HACK: force tooltip to appear, because it'll not appear until
4329 * first mouse move. D*mn M$
4330 */
4331 mouse_event(MOUSEEVENTF_MOVE, 1, 1, 0, 0);
4332 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00004333 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334}
4335
4336 static void
4337delete_tooltip(beval)
4338 BalloonEval *beval;
4339{
4340 DestroyWindow(beval->balloon);
4341}
4342
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004343/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 static VOID CALLBACK
4345BevalTimerProc(hwnd, uMsg, idEvent, dwTime)
4346 HWND hwnd;
4347 UINT uMsg;
4348 UINT idEvent;
4349 DWORD dwTime;
4350{
4351 POINT pt;
4352 RECT rect;
4353
4354 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
4355 return;
4356
4357 GetCursorPos(&pt);
4358 if (WindowFromPoint(pt) != s_textArea)
4359 return;
4360
4361 ScreenToClient(s_textArea, &pt);
4362 GetClientRect(s_textArea, &rect);
4363 if (!PtInRect(&rect, pt))
4364 return;
4365
4366 if (LastActivity > 0
4367 && (dwTime - LastActivity) >= (DWORD)p_bdlay
4368 && (cur_beval->showState != ShS_PENDING
4369 || abs(cur_beval->x - pt.x) > 3
4370 || abs(cur_beval->y - pt.y) > 3))
4371 {
4372 /* Pointer resting in one place long enough, it's time to show
4373 * the tooltip. */
4374 cur_beval->showState = ShS_PENDING;
4375 cur_beval->x = pt.x;
4376 cur_beval->y = pt.y;
4377
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004378 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379
4380 if (cur_beval->msgCB != NULL)
4381 (*cur_beval->msgCB)(cur_beval, 0);
4382 }
4383}
4384
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004385/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 void
4387gui_mch_disable_beval_area(beval)
4388 BalloonEval *beval;
4389{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004390 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004392 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393}
4394
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004395/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 void
4397gui_mch_enable_beval_area(beval)
4398 BalloonEval *beval;
4399{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004400 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 if (beval == NULL)
4402 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004403 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004404 BevalTimerId = SetTimer(s_textArea, 0, p_bdlay / 2,
4405 (TIMERPROC)BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004406 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407}
4408
4409 void
4410gui_mch_post_balloon(beval, mesg)
4411 BalloonEval *beval;
4412 char_u *mesg;
4413{
4414 POINT pt;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004415 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 if (beval->showState == ShS_SHOWING)
4417 return;
4418 GetCursorPos(&pt);
4419 ScreenToClient(s_textArea, &pt);
4420
4421 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
4422 /* cursor is still here */
4423 {
4424 gui_mch_disable_beval_area(cur_beval);
4425 beval->showState = ShS_SHOWING;
4426 make_tooltip(beval, mesg, pt);
4427 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004428 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429}
4430
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004431/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 BalloonEval *
4433gui_mch_create_beval_area(target, mesg, mesgCB, clientData)
4434 void *target; /* ignored, always use s_textArea */
4435 char_u *mesg;
4436 void (*mesgCB)__ARGS((BalloonEval *, int));
4437 void *clientData;
4438{
4439 /* partially stolen from gui_beval.c */
4440 BalloonEval *beval;
4441
4442 if (mesg != NULL && mesgCB != NULL)
4443 {
4444 EMSG(_("E232: Cannot create BalloonEval with both message and callback"));
4445 return NULL;
4446 }
4447
4448 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
4449 if (beval != NULL)
4450 {
4451 beval->target = s_textArea;
4452 beval->balloon = NULL;
4453
4454 beval->showState = ShS_NEUTRAL;
4455 beval->x = 0;
4456 beval->y = 0;
4457 beval->msg = mesg;
4458 beval->msgCB = mesgCB;
4459 beval->clientData = clientData;
4460
4461 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 cur_beval = beval;
4463
4464 if (p_beval)
4465 gui_mch_enable_beval_area(beval);
4466
4467 }
4468 return beval;
4469}
4470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004471/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 static void
4473Handle_WM_Notify(hwnd, pnmh)
4474 HWND hwnd;
4475 LPNMHDR pnmh;
4476{
4477 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
4478 return;
4479
4480 if (cur_beval != NULL)
4481 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004482 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004484 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004485 // TRACE0("TTN_SHOW {{{");
4486 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00004487 break;
4488 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004489 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 delete_tooltip(cur_beval);
4491 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004492 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
4494 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00004495 break;
4496 case TTN_GETDISPINFO:
4497 {
4498 /* if you get there then we have new common controls */
4499 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
4500 info->lpszText = (LPSTR)info->lParam;
4501 info->uFlags |= TTF_DI_SETITEM;
4502 }
4503 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 }
4505 }
4506}
4507
4508 static void
4509TrackUserActivity(UINT uMsg)
4510{
4511 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
4512 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
4513 LastActivity = GetTickCount();
4514}
4515
4516 void
4517gui_mch_destroy_beval_area(beval)
4518 BalloonEval *beval;
4519{
4520 vim_free(beval);
4521}
4522#endif /* FEAT_BEVAL */
4523
4524#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
4525/*
4526 * We have multiple signs to draw at the same location. Draw the
4527 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
4528 */
4529 void
4530netbeans_draw_multisign_indicator(int row)
4531{
4532 int i;
4533 int y;
4534 int x;
4535
4536 x = 0;
4537 y = TEXT_Y(row);
4538
4539 for (i = 0; i < gui.char_height - 3; i++)
4540 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
4541
4542 SetPixel(s_hdc, x+0, y, gui.currFgColor);
4543 SetPixel(s_hdc, x+2, y, gui.currFgColor);
4544 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
4545 SetPixel(s_hdc, x+1, y, gui.currFgColor);
4546 SetPixel(s_hdc, x+2, y, gui.currFgColor);
4547 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
4548 SetPixel(s_hdc, x+2, y, gui.currFgColor);
4549}
4550#endif