blob: 7a01d98732bb232f23fc69118cc179c700250c7c [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;
Bram Moolenaarafa24992006-03-27 20:58:26 +0000459 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 }
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
Bram Moolenaarafa24992006-03-27 20:58:26 +0000709#ifdef FEAT_GUI_TABLINE
710 case WM_RBUTTONUP:
711 {
712 if (gui_mch_showing_tabline())
713 {
714 POINT pt;
715 RECT rect;
716
717 /*
718 * If the cursor is on the tabline, display the tab menu
719 */
720 GetCursorPos((LPPOINT)&pt);
721 GetWindowRect(s_textArea, &rect);
722 if (pt.y < rect.top)
723 {
724 show_tabline_popup_menu();
725 return 0;
726 }
727 }
728 return MyWindowProc(hwnd, uMsg, wParam, lParam);
729 }
730#endif
731
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 case WM_QUERYENDSESSION: /* System wants to go down. */
733 gui_shell_closed(); /* Will exit when no changed buffers. */
734 return FALSE; /* Do NOT allow system to go down. */
735
736 case WM_ENDSESSION:
737 if (wParam) /* system only really goes down when wParam is TRUE */
738 _OnEndSession();
739 break;
740
741 case WM_CHAR:
742 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
743 * byte while we want the UTF-16 character value. */
744 _OnChar(hwnd, wParam, (int)(short)LOWORD(lParam));
745 return 0L;
746
747 case WM_SYSCHAR:
748 /*
749 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
750 * shortcut key, handle like a typed ALT key, otherwise call Windows
751 * ALT key handling.
752 */
753#ifdef FEAT_MENU
754 if ( !gui.menu_is_active
755 || p_wak[0] == 'n'
756 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
757 )
758#endif
759 {
760 _OnSysChar(hwnd, wParam, (int)(short)LOWORD(lParam));
761 return 0L;
762 }
763#ifdef FEAT_MENU
764 else
765 return MyWindowProc(hwnd, uMsg, wParam, lParam);
766#endif
767
768 case WM_SYSKEYUP:
769#ifdef FEAT_MENU
770 /* This used to be done only when menu is active: ALT key is used for
771 * that. But that caused problems when menu is disabled and using
772 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
773 * are received, mouse pointer remains hidden. */
774 return MyWindowProc(hwnd, uMsg, wParam, lParam);
775#else
776 return 0;
777#endif
778
779 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000780 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781
782 case WM_MOUSEWHEEL:
783 _OnMouseWheel(hwnd, HIWORD(wParam));
784 break;
785
Bram Moolenaar520470a2005-06-16 21:59:56 +0000786 /* Notification for change in SystemParametersInfo() */
787 case WM_SETTINGCHANGE:
788 return _OnSettingChange((UINT)wParam);
789
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000790#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 case WM_NOTIFY:
792 switch (((LPNMHDR) lParam)->code)
793 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000794# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 case TTN_NEEDTEXT:
796 {
797 LPTOOLTIPTEXT lpttt;
798 UINT idButton;
799 int idx;
800 vimmenu_T *pMenu;
801
802 lpttt = (LPTOOLTIPTEXT)lParam;
803 idButton = (UINT) lpttt->hdr.idFrom;
804 pMenu = gui_mswin_find_menu(root_menu, idButton);
805 if (pMenu)
806 {
807 idx = MENU_INDEX_TIP;
808 if (pMenu->strings[idx])
809 {
810 lpttt->hinst = NULL; /* string, not resource */
811 lpttt->lpszText = pMenu->strings[idx];
812 }
813 }
814 }
815 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000816# endif
817# ifdef FEAT_GUI_TABLINE
818 case TCN_SELCHANGE:
819 if (gui_mch_showing_tabline()
820 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
821 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
822 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +0000823
824 case NM_RCLICK:
825 if (gui_mch_showing_tabline()
826 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
827 show_tabline_popup_menu();
828 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000829# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000831# ifdef FEAT_GUI_TABLINE
832 if (gui_mch_showing_tabline()
833 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
834 return MyWindowProc(hwnd, uMsg, wParam, lParam);
835# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 break;
837 }
838 break;
839#endif
840#if defined(MENUHINTS) && defined(FEAT_MENU)
841 case WM_MENUSELECT:
842 if (((UINT) HIWORD(wParam)
843 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
844 == MF_HILITE
845 && (State & CMDLINE) == 0)
846 {
847 UINT idButton;
848 vimmenu_T *pMenu;
849 static int did_menu_tip = FALSE;
850
851 if (did_menu_tip)
852 {
853 msg_clr_cmdline();
854 setcursor();
855 out_flush();
856 did_menu_tip = FALSE;
857 }
858
859 idButton = (UINT)LOWORD(wParam);
860 pMenu = gui_mswin_find_menu(root_menu, idButton);
861 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
862 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
863 {
864 msg(pMenu->strings[MENU_INDEX_TIP]);
865 setcursor();
866 out_flush();
867 did_menu_tip = TRUE;
868 }
869 }
870 break;
871#endif
872 case WM_NCHITTEST:
873 {
874 LRESULT result;
875 int x, y;
876 int xPos = GET_X_LPARAM(lParam);
877
878 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
879 if (result == HTCLIENT)
880 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000881#ifdef FEAT_GUI_TABLINE
882 if (gui_mch_showing_tabline())
883 {
884 int yPos = GET_Y_LPARAM(lParam);
885 RECT rct;
886
887 /* If the cursor is on the GUI tabline, don't process this
888 * event */
889 GetWindowRect(s_textArea, &rct);
890 if (yPos < rct.top)
891 return result;
892 }
893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 gui_mch_get_winpos(&x, &y);
895 xPos -= x;
896
897 if (xPos < 48) /* <VN> TODO should use system metric? */
898 return HTBOTTOMLEFT;
899 else
900 return HTBOTTOMRIGHT;
901 }
902 else
903 return result;
904 }
905 /* break; notreached */
906
907#ifdef FEAT_MBYTE_IME
908 case WM_IME_NOTIFY:
909 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
910 return MyWindowProc(hwnd, uMsg, wParam, lParam);
911 break;
912 case WM_IME_COMPOSITION:
913 if (!_OnImeComposition(hwnd, wParam, lParam))
914 return MyWindowProc(hwnd, uMsg, wParam, lParam);
915 break;
916#endif
917
918 default:
919 if (uMsg == msh_msgmousewheel && msh_msgmousewheel != 0)
920 { /* handle MSH_MOUSEWHEEL messages for Intellimouse */
921 _OnMouseWheel(hwnd, HIWORD(wParam));
922 break;
923 }
924#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000925 else if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 {
927 _OnFindRepl();
928 }
929#endif
930 return MyWindowProc(hwnd, uMsg, wParam, lParam);
931 }
932
933 return 1;
934}
935
936/*
937 * End of call-back routines
938 */
939
940/* parent window, if specified with -P */
941HWND vim_parent_hwnd = NULL;
942
943 static BOOL CALLBACK
944FindWindowTitle(HWND hwnd, LPARAM lParam)
945{
946 char buf[2048];
947 char *title = (char *)lParam;
948
949 if (GetWindowText(hwnd, buf, sizeof(buf)))
950 {
951 if (strstr(buf, title) != NULL)
952 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000953 /* Found it. Store the window ref. and quit searching if MDI
954 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000956 if (vim_parent_hwnd != NULL)
957 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 }
959 }
960 return TRUE; /* continue searching */
961}
962
963/*
964 * Invoked for '-P "title"' argument: search for parent application to open
965 * our window in.
966 */
967 void
968gui_mch_set_parent(char *title)
969{
970 EnumWindows(FindWindowTitle, (LPARAM)title);
971 if (vim_parent_hwnd == NULL)
972 {
973 EMSG2(_("E671: Cannot find window title \"%s\""), title);
974 mch_exit(2);
975 }
976}
977
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000978#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 static void
980ole_error(char *arg)
981{
982 EMSG2(_("E243: Argument not supported: \"-%s\"; Use the OLE version."),
983 arg);
984}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
987/*
988 * Parse the GUI related command-line arguments. Any arguments used are
989 * deleted from argv, and *argc is decremented accordingly. This is called
990 * when vim is started, whether or not the GUI has been started.
991 */
992 void
993gui_mch_prepare(int *argc, char **argv)
994{
995 int silent = FALSE;
996 int idx;
997
998 /* Check for special OLE command line parameters */
999 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
1000 {
1001 /* Check for a "-silent" argument first. */
1002 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
1003 && (argv[2][0] == '-' || argv[2][0] == '/'))
1004 {
1005 silent = TRUE;
1006 idx = 2;
1007 }
1008 else
1009 idx = 1;
1010
1011 /* Register Vim as an OLE Automation server */
1012 if (STRICMP(argv[idx] + 1, "register") == 0)
1013 {
1014#ifdef FEAT_OLE
1015 RegisterMe(silent);
1016 mch_exit(0);
1017#else
1018 if (!silent)
1019 ole_error("register");
1020 mch_exit(2);
1021#endif
1022 }
1023
1024 /* Unregister Vim as an OLE Automation server */
1025 if (STRICMP(argv[idx] + 1, "unregister") == 0)
1026 {
1027#ifdef FEAT_OLE
1028 UnregisterMe(!silent);
1029 mch_exit(0);
1030#else
1031 if (!silent)
1032 ole_error("unregister");
1033 mch_exit(2);
1034#endif
1035 }
1036
1037 /* Ignore an -embedding argument. It is only relevant if the
1038 * application wants to treat the case when it is started manually
1039 * differently from the case where it is started via automation (and
1040 * we don't).
1041 */
1042 if (STRICMP(argv[idx] + 1, "embedding") == 0)
1043 {
1044#ifdef FEAT_OLE
1045 *argc = 1;
1046#else
1047 ole_error("embedding");
1048 mch_exit(2);
1049#endif
1050 }
1051 }
1052
1053#ifdef FEAT_OLE
1054 {
1055 int bDoRestart = FALSE;
1056
1057 InitOLE(&bDoRestart);
1058 /* automatically exit after registering */
1059 if (bDoRestart)
1060 mch_exit(0);
1061 }
1062#endif
1063
1064#ifdef FEAT_NETBEANS_INTG
1065 {
1066 /* stolen from gui_x11.x */
1067 int arg;
1068
1069 for (arg = 1; arg < *argc; arg++)
1070 if (strncmp("-nb", argv[arg], 3) == 0)
1071 {
1072 usingNetbeans++;
1073 netbeansArg = argv[arg];
1074 mch_memmove(&argv[arg], &argv[arg + 1],
1075 (--*argc - arg) * sizeof(char *));
1076 argv[*argc] = NULL;
1077 break; /* enough? */
1078 }
1079
1080 if (usingNetbeans)
1081 {
1082 WSADATA wsaData;
1083 int wsaerr;
1084
1085 /* Init WinSock */
1086 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
1087 if (wsaerr == 0)
1088 WSInitialized = TRUE;
1089 }
1090 }
1091#endif
1092
1093 /* get the OS version info */
1094 os_version.dwOSVersionInfoSize = sizeof(os_version);
1095 GetVersionEx(&os_version); /* this call works on Win32s, Win95 and WinNT */
1096
1097 /* try and load the user32.dll library and get the entry points for
1098 * multi-monitor-support. */
1099 if ((user32_lib = LoadLibrary("User32.dll")) != NULL)
1100 {
1101 pMonitorFromWindow = (TMonitorFromWindow)GetProcAddress(user32_lib,
1102 "MonitorFromWindow");
1103
1104 /* there are ...A and ...W version of GetMonitorInfo - looking at
1105 * winuser.h, they have exactly the same declaration. */
1106 pGetMonitorInfo = (TGetMonitorInfo)GetProcAddress(user32_lib,
1107 "GetMonitorInfoA");
1108 }
1109}
1110
1111/*
1112 * Initialise the GUI. Create all the windows, set up all the call-backs
1113 * etc.
1114 */
1115 int
1116gui_mch_init(void)
1117{
1118 const char szVimWndClass[] = VIM_CLASS;
1119 const char szTextAreaClass[] = "VimTextArea";
1120 WNDCLASS wndclass;
1121#ifdef FEAT_MBYTE
1122 const WCHAR szVimWndClassW[] = VIM_CLASSW;
1123 WNDCLASSW wndclassw;
1124#endif
1125#ifdef GLOBAL_IME
1126 ATOM atom;
1127#endif
1128
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 /* Return here if the window was already opened (happens when
1130 * gui_mch_dialog() is called early). */
1131 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00001132 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133
1134 /*
1135 * Load the tearoff bitmap
1136 */
1137#ifdef FEAT_TEAROFF
1138 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
1139#endif
1140
1141 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
1142 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
1143#ifdef FEAT_MENU
1144 gui.menu_height = 0; /* Windows takes care of this */
1145#endif
1146 gui.border_width = 0;
1147
1148 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
1149
1150#ifdef FEAT_MBYTE
1151 /* First try using the wide version, so that we can use any title.
1152 * Otherwise only characters in the active codepage will work. */
1153 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
1154 {
1155 wndclassw.style = 0;
1156 wndclassw.lpfnWndProc = _WndProc;
1157 wndclassw.cbClsExtra = 0;
1158 wndclassw.cbWndExtra = 0;
1159 wndclassw.hInstance = s_hinst;
1160 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
1161 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
1162 wndclassw.hbrBackground = s_brush;
1163 wndclassw.lpszMenuName = NULL;
1164 wndclassw.lpszClassName = szVimWndClassW;
1165
1166 if ((
1167#ifdef GLOBAL_IME
1168 atom =
1169#endif
1170 RegisterClassW(&wndclassw)) == 0)
1171 {
1172 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1173 return FAIL;
1174
1175 /* Must be Windows 98, fall back to non-wide function. */
1176 }
1177 else
1178 wide_WindowProc = TRUE;
1179 }
1180
1181 if (!wide_WindowProc)
1182#endif
1183
1184 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
1185 {
1186 wndclass.style = 0;
1187 wndclass.lpfnWndProc = _WndProc;
1188 wndclass.cbClsExtra = 0;
1189 wndclass.cbWndExtra = 0;
1190 wndclass.hInstance = s_hinst;
1191 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
1192 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1193 wndclass.hbrBackground = s_brush;
1194 wndclass.lpszMenuName = NULL;
1195 wndclass.lpszClassName = szVimWndClass;
1196
1197 if ((
1198#ifdef GLOBAL_IME
1199 atom =
1200#endif
1201 RegisterClass(&wndclass)) == 0)
1202 return FAIL;
1203 }
1204
1205 if (vim_parent_hwnd != NULL)
1206 {
1207#ifdef HAVE_TRY_EXCEPT
1208 __try
1209 {
1210#endif
1211 /* Open inside the specified parent window.
1212 * TODO: last argument should point to a CLIENTCREATESTRUCT
1213 * structure. */
1214 s_hwnd = CreateWindowEx(
1215 WS_EX_MDICHILD,
1216 szVimWndClass, "Vim MSWindows GUI",
1217 WS_OVERLAPPEDWINDOW | WS_CHILD | WS_CLIPSIBLINGS | 0xC000,
1218 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
1219 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
1220 100, /* Any value will do */
1221 100, /* Any value will do */
1222 vim_parent_hwnd, NULL,
1223 s_hinst, NULL);
1224#ifdef HAVE_TRY_EXCEPT
1225 }
1226 __except(EXCEPTION_EXECUTE_HANDLER)
1227 {
1228 /* NOP */
1229 }
1230#endif
1231 if (s_hwnd == NULL)
1232 {
1233 EMSG(_("E672: Unable to open window inside MDI application"));
1234 mch_exit(2);
1235 }
1236 }
1237 else
1238 /* Open toplevel window. */
1239 s_hwnd = CreateWindow(
1240 szVimWndClass, "Vim MSWindows GUI",
1241 WS_OVERLAPPEDWINDOW,
1242 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
1243 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
1244 100, /* Any value will do */
1245 100, /* Any value will do */
1246 NULL, NULL,
1247 s_hinst, NULL);
1248
1249 if (s_hwnd == NULL)
1250 return FAIL;
1251
1252#ifdef GLOBAL_IME
1253 global_ime_init(atom, s_hwnd);
1254#endif
1255#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
1256 dyn_imm_load();
1257#endif
1258
1259 /* Create the text area window */
1260 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
1261 {
1262 wndclass.style = CS_OWNDC;
1263 wndclass.lpfnWndProc = _TextAreaWndProc;
1264 wndclass.cbClsExtra = 0;
1265 wndclass.cbWndExtra = 0;
1266 wndclass.hInstance = s_hinst;
1267 wndclass.hIcon = NULL;
1268 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1269 wndclass.hbrBackground = NULL;
1270 wndclass.lpszMenuName = NULL;
1271 wndclass.lpszClassName = szTextAreaClass;
1272
1273 if (RegisterClass(&wndclass) == 0)
1274 return FAIL;
1275 }
1276 s_textArea = CreateWindowEx(
1277 WS_EX_CLIENTEDGE,
1278 szTextAreaClass, "Vim text area",
1279 WS_CHILD | WS_VISIBLE, 0, 0,
1280 100, /* Any value will do for now */
1281 100, /* Any value will do for now */
1282 s_hwnd, NULL,
1283 s_hinst, NULL);
1284
1285 if (s_textArea == NULL)
1286 return FAIL;
1287
1288#ifdef FEAT_MENU
1289 s_menuBar = CreateMenu();
1290#endif
1291 s_hdc = GetDC(s_textArea);
1292
1293#ifdef MSWIN16_FASTTEXT
1294 SetBkMode(s_hdc, OPAQUE);
1295#endif
1296
1297#ifdef FEAT_WINDOWS
1298 DragAcceptFiles(s_hwnd, TRUE);
1299#endif
1300
1301 /* Do we need to bother with this? */
1302 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
1303
1304 /* Get background/foreground colors from the system */
1305 gui_mch_def_colors();
1306
1307 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
1308 * file) */
1309 set_normal_colors();
1310
1311 /*
1312 * Check that none of the colors are the same as the background color.
1313 * Then store the current values as the defaults.
1314 */
1315 gui_check_colors();
1316 gui.def_norm_pixel = gui.norm_pixel;
1317 gui.def_back_pixel = gui.back_pixel;
1318
1319 /* Get the colors for the highlight groups (gui_check_colors() might have
1320 * changed them) */
1321 highlight_gui_started();
1322
1323 /*
1324 * Start out by adding the configured border width into the border offset
1325 */
1326 gui.border_offset = gui.border_width + 2; /*CLIENT EDGE*/
1327
1328 /*
1329 * Set up for Intellimouse processing
1330 */
1331 init_mouse_wheel();
1332
1333 /*
1334 * compute a couple of metrics used for the dialogs
1335 */
1336 get_dialog_font_metrics();
1337#ifdef FEAT_TOOLBAR
1338 /*
1339 * Create the toolbar
1340 */
1341 initialise_toolbar();
1342#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001343#ifdef FEAT_GUI_TABLINE
1344 /*
1345 * Create the tabline
1346 */
1347 initialise_tabline();
1348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349#ifdef MSWIN_FIND_REPLACE
1350 /*
1351 * Initialise the dialog box stuff
1352 */
1353 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
1354
1355 /* Initialise the struct */
1356 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
1357 s_findrep_struct.lpstrFindWhat = alloc(MSWIN_FR_BUFSIZE);
1358 s_findrep_struct.lpstrFindWhat[0] = NUL;
1359 s_findrep_struct.lpstrReplaceWith = alloc(MSWIN_FR_BUFSIZE);
1360 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1361 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
1362 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
1363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364
Bram Moolenaar748bf032005-02-02 23:04:36 +00001365theend:
1366 /* Display any pending error messages */
1367 display_errors();
1368
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 return OK;
1370}
1371
1372/*
1373 * Get the size of the screen, taking position on multiple monitors into
1374 * account (if supported).
1375 */
1376 static void
1377get_work_area(RECT *spi_rect)
1378{
1379 _HMONITOR mon;
1380 _MONITORINFO moninfo;
1381
1382 /* use these functions only if available */
1383 if (pMonitorFromWindow != NULL && pGetMonitorInfo != NULL)
1384 {
1385 /* work out which monitor the window is on, and get *it's* work area */
1386 mon = pMonitorFromWindow(s_hwnd, 1 /*MONITOR_DEFAULTTOPRIMARY*/);
1387 if (mon != NULL)
1388 {
1389 moninfo.cbSize = sizeof(_MONITORINFO);
1390 if (pGetMonitorInfo(mon, &moninfo))
1391 {
1392 *spi_rect = moninfo.rcWork;
1393 return;
1394 }
1395 }
1396 }
1397 /* this is the old method... */
1398 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
1399}
1400
1401/*
1402 * Set the size of the window to the given width and height in pixels.
1403 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001404/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 void
1406gui_mch_set_shellsize(int width, int height,
Bram Moolenaarafa24992006-03-27 20:58:26 +00001407 int min_width, int min_height, int base_width, int base_height,
1408 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409{
1410 RECT workarea_rect;
1411 int win_width, win_height;
1412 int win_xpos, win_ypos;
1413 WINDOWPLACEMENT wndpl;
1414
1415 /* try to keep window completely on screen */
1416 /* get size of the screen work area (excludes taskbar, appbars) */
1417 get_work_area(&workarea_rect);
1418
1419 /* get current posision of our window */
1420 wndpl.length = sizeof(WINDOWPLACEMENT);
1421 GetWindowPlacement(s_hwnd, &wndpl);
1422
1423 /* Resizing a maximized window looks very strange, unzoom it first.
1424 * But don't do it when still starting up, it may have been requested in
1425 * the shortcut. */
1426 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
1427 {
1428 ShowWindow(s_hwnd, SW_SHOWNORMAL);
1429 /* Need to get the settings of the normal window. */
1430 GetWindowPlacement(s_hwnd, &wndpl);
1431 }
1432
1433 win_xpos = wndpl.rcNormalPosition.left;
1434 win_ypos = wndpl.rcNormalPosition.top;
1435
1436 /* compute the size of the outside of the window */
1437 win_width = width + GetSystemMetrics(SM_CXFRAME) * 2;
1438 win_height = height + GetSystemMetrics(SM_CYFRAME) * 2
1439 + GetSystemMetrics(SM_CYCAPTION)
1440#ifdef FEAT_MENU
1441 + gui_mswin_get_menu_height(FALSE)
1442#endif
1443 ;
1444
1445 /* if the window is going off the screen, move it on to the screen */
Bram Moolenaarafa24992006-03-27 20:58:26 +00001446 if ((direction & RESIZE_HOR) && win_xpos + win_width > workarea_rect.right)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 win_xpos = workarea_rect.right - win_width;
1448
Bram Moolenaarafa24992006-03-27 20:58:26 +00001449 if ((direction & RESIZE_HOR) && win_xpos < workarea_rect.left)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 win_xpos = workarea_rect.left;
1451
Bram Moolenaarafa24992006-03-27 20:58:26 +00001452 if ((direction & RESIZE_VERT)
1453 && win_ypos + win_height > workarea_rect.bottom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 win_ypos = workarea_rect.bottom - win_height;
1455
Bram Moolenaarafa24992006-03-27 20:58:26 +00001456 if ((direction & RESIZE_VERT) && win_ypos < workarea_rect.top)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 win_ypos = workarea_rect.top;
1458
1459 /* When the taskbar is placed on the left or top of the screen,
1460 * SetWindowPlacement() adds its width or height automatically, compensate
1461 * for that. When the offset is over 400 it's probably something else,
1462 * skip it then (just in case). */
1463 if (workarea_rect.left > 0 && workarea_rect.left < 400)
1464 win_xpos -= workarea_rect.left;
1465 if (workarea_rect.top > 0 && workarea_rect.top < 400)
1466 win_ypos -= workarea_rect.top;
1467
1468 wndpl.rcNormalPosition.left = win_xpos;
1469 wndpl.rcNormalPosition.right = win_xpos + win_width;
1470 wndpl.rcNormalPosition.top = win_ypos;
1471 wndpl.rcNormalPosition.bottom = win_ypos + win_height;
1472
1473 /* set window position - we should use SetWindowPlacement rather than
1474 * SetWindowPos as the MSDN docs say the coord systems returned by
1475 * these two are not compatible. */
1476 SetWindowPlacement(s_hwnd, &wndpl);
1477
1478 SetActiveWindow(s_hwnd);
1479 SetFocus(s_hwnd);
1480
1481#ifdef FEAT_MENU
1482 /* Menu may wrap differently now */
1483 gui_mswin_get_menu_height(!gui.starting);
1484#endif
1485}
1486
1487
1488 void
1489gui_mch_set_scrollbar_thumb(
1490 scrollbar_T *sb,
1491 long val,
1492 long size,
1493 long max)
1494{
1495 SCROLLINFO info;
1496
1497 sb->scroll_shift = 0;
1498 while (max > 32767)
1499 {
1500 max = (max + 1) >> 1;
1501 val >>= 1;
1502 size >>= 1;
1503 ++sb->scroll_shift;
1504 }
1505
1506 if (sb->scroll_shift > 0)
1507 ++size;
1508
1509 info.cbSize = sizeof(info);
1510 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
1511 info.nPos = val;
1512 info.nMin = 0;
1513 info.nMax = max;
1514 info.nPage = size;
1515 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
1516}
1517
1518
1519/*
1520 * Set the current text font.
1521 */
1522 void
1523gui_mch_set_font(GuiFont font)
1524{
1525 gui.currFont = font;
1526}
1527
1528
1529/*
1530 * Set the current text foreground color.
1531 */
1532 void
1533gui_mch_set_fg_color(guicolor_T color)
1534{
1535 gui.currFgColor = color;
1536}
1537
1538/*
1539 * Set the current text background color.
1540 */
1541 void
1542gui_mch_set_bg_color(guicolor_T color)
1543{
1544 gui.currBgColor = color;
1545}
1546
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001547/*
1548 * Set the current text special color.
1549 */
1550 void
1551gui_mch_set_sp_color(guicolor_T color)
1552{
1553 gui.currSpColor = color;
1554}
1555
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556#if defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)
1557/*
1558 * Multi-byte handling, originally by Sung-Hoon Baek.
1559 * First static functions (no prototypes generated).
1560 */
1561#ifdef _MSC_VER
1562# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
1563#endif
1564#include <imm.h>
1565
1566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 * handle WM_IME_NOTIFY message
1568 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001569/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 static LRESULT
1571_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData)
1572{
1573 LRESULT lResult = 0;
1574 HIMC hImc;
1575
1576 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
1577 return lResult;
1578 switch (dwCommand)
1579 {
1580 case IMN_SETOPENSTATUS:
1581 if (pImmGetOpenStatus(hImc))
1582 {
1583 pImmSetCompositionFont(hImc, &norm_logfont);
1584 im_set_position(gui.row, gui.col);
1585
1586 /* Disable langmap */
1587 State &= ~LANGMAP;
1588 if (State & INSERT)
1589 {
1590#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1591 /* Unshown 'keymap' in status lines */
1592 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
1593 {
1594 /* Save cursor position */
1595 int old_row = gui.row;
1596 int old_col = gui.col;
1597
1598 // This must be called here before
1599 // status_redraw_curbuf(), otherwise the mode
1600 // message may appear in the wrong position.
1601 showmode();
1602 status_redraw_curbuf();
1603 update_screen(0);
1604 /* Restore cursor position */
1605 gui.row = old_row;
1606 gui.col = old_col;
1607 }
1608#endif
1609 }
1610 }
1611 gui_update_cursor(TRUE, FALSE);
1612 lResult = 0;
1613 break;
1614 }
1615 pImmReleaseContext(hWnd, hImc);
1616 return lResult;
1617}
1618
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001619/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 static LRESULT
1621_OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param)
1622{
1623 char_u *ret;
1624 int len;
1625
1626 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
1627 return 0;
1628
1629 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
1630 if (ret != NULL)
1631 {
1632 add_to_input_buf_csi(ret, len);
1633 vim_free(ret);
1634 return 1;
1635 }
1636 return 0;
1637}
1638
1639/*
1640 * get the current composition string, in UCS-2; *lenp is the number of
1641 * *lenp is the number of Unicode characters.
1642 */
1643 static short_u *
1644GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
1645{
1646 LONG ret;
1647 LPWSTR wbuf = NULL;
1648 char_u *buf;
1649
1650 if (!pImmGetContext)
1651 return NULL; /* no imm32.dll */
1652
1653 /* Try Unicode; this'll always work on NT regardless of codepage. */
1654 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
1655 if (ret == 0)
1656 return NULL; /* empty */
1657
1658 if (ret > 0)
1659 {
1660 /* Allocate the requested buffer plus space for the NUL character. */
1661 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
1662 if (wbuf != NULL)
1663 {
1664 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
1665 *lenp = ret / sizeof(WCHAR);
1666 }
1667 return (short_u *)wbuf;
1668 }
1669
1670 /* ret < 0; we got an error, so try the ANSI version. This'll work
1671 * on 9x/ME, but only if the codepage happens to be set to whatever
1672 * we're inputting. */
1673 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
1674 if (ret <= 0)
1675 return NULL; /* empty or error */
1676
1677 buf = alloc(ret);
1678 if (buf == NULL)
1679 return NULL;
1680 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
1681
1682 /* convert from codepage to UCS-2 */
1683 MultiByteToWideChar_alloc(GetACP(), 0, buf, ret, &wbuf, lenp);
1684 vim_free(buf);
1685
1686 return (short_u *)wbuf;
1687}
1688
1689/*
1690 * void GetResultStr()
1691 *
1692 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
1693 * get complete composition string
1694 */
1695 static char_u *
1696GetResultStr(HWND hwnd, int GCS, int *lenp)
1697{
1698 HIMC hIMC; /* Input context handle. */
1699 short_u *buf = NULL;
1700 char_u *convbuf = NULL;
1701
1702 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
1703 return NULL;
1704
1705 /* Reads in the composition string. */
1706 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
1707 if (buf == NULL)
1708 return NULL;
1709
1710 convbuf = ucs2_to_enc(buf, lenp);
1711 pImmReleaseContext(hwnd, hIMC);
1712 vim_free(buf);
1713 return convbuf;
1714}
1715#endif
1716
1717/* For global functions we need prototypes. */
1718#if (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) || defined(PROTO)
1719
1720/*
1721 * set font to IM.
1722 */
1723 void
1724im_set_font(LOGFONT *lf)
1725{
1726 HIMC hImc;
1727
1728 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
1729 {
1730 pImmSetCompositionFont(hImc, lf);
1731 pImmReleaseContext(s_hwnd, hImc);
1732 }
1733}
1734
1735/*
1736 * Notify cursor position to IM.
1737 */
1738 void
1739im_set_position(int row, int col)
1740{
1741 HIMC hImc;
1742
1743 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
1744 {
1745 COMPOSITIONFORM cfs;
1746
1747 cfs.dwStyle = CFS_POINT;
1748 cfs.ptCurrentPos.x = FILL_X(col);
1749 cfs.ptCurrentPos.y = FILL_Y(row);
1750 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
1751 pImmSetCompositionWindow(hImc, &cfs);
1752
1753 pImmReleaseContext(s_hwnd, hImc);
1754 }
1755}
1756
1757/*
1758 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
1759 */
1760 void
1761im_set_active(int active)
1762{
1763 HIMC hImc;
1764 static HIMC hImcOld = (HIMC)0;
1765
1766 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
1767 {
1768 if (p_imdisable)
1769 {
1770 if (hImcOld == (HIMC)0)
1771 {
1772 hImcOld = pImmGetContext(s_hwnd);
1773 if (hImcOld)
1774 pImmAssociateContext(s_hwnd, (HIMC)0);
1775 }
1776 active = FALSE;
1777 }
1778 else if (hImcOld != (HIMC)0)
1779 {
1780 pImmAssociateContext(s_hwnd, hImcOld);
1781 hImcOld = (HIMC)0;
1782 }
1783
1784 hImc = pImmGetContext(s_hwnd);
1785 if (hImc)
1786 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001787 /*
1788 * for Korean ime
1789 */
1790 HKL hKL = GetKeyboardLayout(0);
1791
1792 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
1793 {
1794 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
1795 static BOOL bSaved = FALSE;
1796
1797 if (active)
1798 {
1799 /* if we have a saved conversion status, restore it */
1800 if (bSaved)
1801 pImmSetConversionStatus(hImc, dwConversionSaved,
1802 dwSentenceSaved);
1803 bSaved = FALSE;
1804 }
1805 else
1806 {
1807 /* save conversion status and disable korean */
1808 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
1809 &dwSentenceSaved))
1810 {
1811 bSaved = TRUE;
1812 pImmSetConversionStatus(hImc,
1813 dwConversionSaved & ~(IME_CMODE_NATIVE
1814 | IME_CMODE_FULLSHAPE),
1815 dwSentenceSaved);
1816 }
1817 }
1818 }
1819
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 pImmSetOpenStatus(hImc, active);
1821 pImmReleaseContext(s_hwnd, hImc);
1822 }
1823 }
1824}
1825
1826/*
1827 * Get IM status. When IM is on, return not 0. Else return 0.
1828 */
1829 int
1830im_get_status()
1831{
1832 int status = 0;
1833 HIMC hImc;
1834
1835 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
1836 {
1837 status = pImmGetOpenStatus(hImc) ? 1 : 0;
1838 pImmReleaseContext(s_hwnd, hImc);
1839 }
1840 return status;
1841}
1842
1843#endif /* FEAT_MBYTE && FEAT_MBYTE_IME */
1844
1845#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
1846/* Win32 with GLOBAL IME */
1847
1848/*
1849 * Notify cursor position to IM.
1850 */
1851 void
1852im_set_position(int row, int col)
1853{
1854 /* Win32 with GLOBAL IME */
1855 POINT p;
1856
1857 p.x = FILL_X(col);
1858 p.y = FILL_Y(row);
1859 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
1860 global_ime_set_position(&p);
1861}
1862
1863/*
1864 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
1865 */
1866 void
1867im_set_active(int active)
1868{
1869 global_ime_set_status(active);
1870}
1871
1872/*
1873 * Get IM status. When IM is on, return not 0. Else return 0.
1874 */
1875 int
1876im_get_status()
1877{
1878 return global_ime_get_status();
1879}
1880#endif
1881
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001882#ifdef FEAT_MBYTE
1883/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00001884 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001885 */
1886 static void
1887latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
1888{
1889 int c;
1890
Bram Moolenaarca003e12006-03-17 23:19:38 +00001891 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001892 {
1893 c = *text++;
1894 switch (c)
1895 {
1896 case 0xa4: c = 0x20ac; break; /* euro */
1897 case 0xa6: c = 0x0160; break; /* S hat */
1898 case 0xa8: c = 0x0161; break; /* S -hat */
1899 case 0xb4: c = 0x017d; break; /* Z hat */
1900 case 0xb8: c = 0x017e; break; /* Z -hat */
1901 case 0xbc: c = 0x0152; break; /* OE */
1902 case 0xbd: c = 0x0153; break; /* oe */
1903 case 0xbe: c = 0x0178; break; /* Y */
1904 }
1905 *unicodebuf++ = c;
1906 }
1907}
1908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909
1910#ifdef FEAT_RIGHTLEFT
1911/*
1912 * What is this for? In the case where you are using Win98 or Win2K or later,
1913 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
1914 * reverses the string sent to the TextOut... family. This sucks, because we
1915 * go to a lot of effort to do the right thing, and there doesn't seem to be a
1916 * way to tell Windblows not to do this!
1917 *
1918 * The short of it is that this 'RevOut' only gets called if you are running
1919 * one of the new, "improved" MS OSes, and only if you are running in
1920 * 'rightleft' mode. It makes display take *slightly* longer, but not
1921 * noticeably so.
1922 */
1923 static void
1924RevOut( HDC s_hdc,
1925 int col,
1926 int row,
1927 UINT foptions,
1928 CONST RECT *pcliprect,
1929 LPCTSTR text,
1930 UINT len,
1931 CONST INT *padding)
1932{
1933 int ix;
1934 static int special = -1;
1935
1936 if (special == -1)
1937 {
1938 /* Check windows version: special treatment is needed if it is NT 5 or
1939 * Win98 or higher. */
1940 if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
1941 && os_version.dwMajorVersion >= 5)
1942 || (os_version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
1943 && (os_version.dwMajorVersion > 4
1944 || (os_version.dwMajorVersion == 4
1945 && os_version.dwMinorVersion > 0))))
1946 special = 1;
1947 else
1948 special = 0;
1949 }
1950
1951 if (special)
1952 for (ix = 0; ix < (int)len; ++ix)
1953 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
1954 pcliprect, text + ix, 1, padding);
1955 else
1956 ExtTextOut(s_hdc, col, row, foptions, pcliprect, text, len, padding);
1957}
1958#endif
1959
1960 void
1961gui_mch_draw_string(
1962 int row,
1963 int col,
1964 char_u *text,
1965 int len,
1966 int flags)
1967{
1968 static int *padding = NULL;
1969 static int pad_size = 0;
1970 int i;
1971 const RECT *pcliprect = NULL;
1972 UINT foptions = 0;
1973#ifdef FEAT_MBYTE
1974 static WCHAR *unicodebuf = NULL;
1975 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001976 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 int n = 0;
1978#endif
1979 HPEN hpen, old_pen;
1980 int y;
1981
1982#ifndef MSWIN16_FASTTEXT
1983 /*
1984 * Italic and bold text seems to have an extra row of pixels at the bottom
1985 * (below where the bottom of the character should be). If we draw the
1986 * characters with a solid background, the top row of pixels in the
1987 * character below will be overwritten. We can fix this by filling in the
1988 * background ourselves, to the correct character proportions, and then
1989 * writing the character in transparent mode. Still have a problem when
1990 * the character is "_", which gets written on to the character below.
1991 * New fix: set gui.char_ascent to -1. This shifts all characters up one
1992 * pixel in their slots, which fixes the problem with the bottom row of
1993 * pixels. We still need this code because otherwise the top row of pixels
1994 * becomes a problem. - webb.
1995 */
1996 static HBRUSH hbr_cache[2] = {NULL, NULL};
1997 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
1998 static int brush_lru = 0;
1999 HBRUSH hbr;
2000 RECT rc;
2001
2002 if (!(flags & DRAW_TRANSP))
2003 {
2004 /*
2005 * Clear background first.
2006 * Note: FillRect() excludes right and bottom of rectangle.
2007 */
2008 rc.left = FILL_X(col);
2009 rc.top = FILL_Y(row);
2010#ifdef FEAT_MBYTE
2011 if (has_mbyte)
2012 {
2013 int cell_len = 0;
2014
2015 /* Compute the length in display cells. */
2016 for (n = 0; n < len; n += MB_BYTE2LEN(text[n]))
2017 cell_len += (*mb_ptr2cells)(text + n);
2018 rc.right = FILL_X(col + cell_len);
2019 }
2020 else
2021#endif
2022 rc.right = FILL_X(col + len);
2023 rc.bottom = FILL_Y(row + 1);
2024
2025 /* Cache the created brush, that saves a lot of time. We need two:
2026 * one for cursor background and one for the normal background. */
2027 if (gui.currBgColor == brush_color[0])
2028 {
2029 hbr = hbr_cache[0];
2030 brush_lru = 1;
2031 }
2032 else if (gui.currBgColor == brush_color[1])
2033 {
2034 hbr = hbr_cache[1];
2035 brush_lru = 0;
2036 }
2037 else
2038 {
2039 if (hbr_cache[brush_lru] != NULL)
2040 DeleteBrush(hbr_cache[brush_lru]);
2041 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
2042 brush_color[brush_lru] = gui.currBgColor;
2043 hbr = hbr_cache[brush_lru];
2044 brush_lru = !brush_lru;
2045 }
2046 FillRect(s_hdc, &rc, hbr);
2047
2048 SetBkMode(s_hdc, TRANSPARENT);
2049
2050 /*
2051 * When drawing block cursor, prevent inverted character spilling
2052 * over character cell (can happen with bold/italic)
2053 */
2054 if (flags & DRAW_CURSOR)
2055 {
2056 pcliprect = &rc;
2057 foptions = ETO_CLIPPED;
2058 }
2059 }
2060#else
2061 /*
2062 * The alternative would be to write the characters in opaque mode, but
2063 * when the text is not exactly the same proportions as normal text, too
2064 * big or too little a rectangle gets drawn for the background.
2065 */
2066 SetBkMode(s_hdc, OPAQUE);
2067 SetBkColor(s_hdc, gui.currBgColor);
2068#endif
2069 SetTextColor(s_hdc, gui.currFgColor);
2070 SelectFont(s_hdc, gui.currFont);
2071
2072 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
2073 {
2074 vim_free(padding);
2075 pad_size = Columns;
2076
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002077 /* Don't give an out-of-memory message here, it would call us
2078 * recursively. */
2079 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 if (padding != NULL)
2081 for (i = 0; i < pad_size; i++)
2082 padding[i] = gui.char_width;
2083 }
2084
2085 /* On NT, tell the font renderer not to "help" us with Hebrew and Arabic
2086 * text. This doesn't work in 9x, so we have to deal with it manually on
2087 * those systems. */
2088 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
2089 foptions |= ETO_IGNORELANGUAGE;
2090
2091 /*
2092 * We have to provide the padding argument because italic and bold versions
2093 * of fixed-width fonts are often one pixel or so wider than their normal
2094 * versions.
2095 * No check for DRAW_BOLD, Windows will have done it already.
2096 */
2097
2098#ifdef FEAT_MBYTE
2099 /* Check if there are any UTF-8 characters. If not, use normal text
2100 * output to speed up output. */
2101 if (enc_utf8)
2102 for (n = 0; n < len; ++n)
2103 if (text[n] >= 0x80)
2104 break;
2105
2106 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002107 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002109 if ((enc_utf8
2110 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
2111 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 && (unicodebuf == NULL || len > unibuflen))
2113 {
2114 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002115 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
2117 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002118 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119
2120 unibuflen = len;
2121 }
2122
2123 if (enc_utf8 && n < len && unicodebuf != NULL)
2124 {
2125 /* Output UTF-8 characters. Caller has already separated
2126 * composing characters. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002127 int i;
2128 int wlen; /* string length in words */
2129 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 int cells; /* cell width of string up to composing char */
2131 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002132 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002134 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002135 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002137 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002139 c = utf_ptr2char(text + i);
2140 if (c >= 0x10000)
2141 {
2142 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002143 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
2144 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002145 }
2146 else
2147 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002148 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002149 }
2150 cw = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 if (cw > 2) /* don't use 4 for unprintable char */
2152 cw = 1;
2153 if (unicodepdy != NULL)
2154 {
2155 /* Use unicodepdy to make characters fit as we expect, even
2156 * when the font uses different widths (e.g., bold character
2157 * is wider). */
2158 unicodepdy[clen] = cw * gui.char_width;
2159 }
2160 cells += cw;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002161 i += utfc_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00002162 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
2164 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaarca003e12006-03-17 23:19:38 +00002165 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 len = cells; /* used for underlining */
2167 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002168 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 {
2170 /* If we want to display codepage data, and the current CP is not the
2171 * ANSI one, we need to go via Unicode. */
2172 if (unicodebuf != NULL)
2173 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002174 if (enc_latin9)
2175 latin9_to_ucs(text, len, unicodebuf);
2176 else
2177 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 MB_PRECOMPOSED,
2179 (char *)text, len,
2180 (LPWSTR)unicodebuf, unibuflen);
2181 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002182 {
2183 /* Use unicodepdy to make characters fit as we expect, even
2184 * when the font uses different widths (e.g., bold character
2185 * is wider). */
2186 if (unicodepdy != NULL)
2187 {
2188 int i;
2189 int cw;
2190
2191 for (i = 0; i < len; ++i)
2192 {
2193 cw = utf_char2cells(unicodebuf[i]);
2194 if (cw > 2)
2195 cw = 1;
2196 unicodepdy[i] = cw * gui.char_width;
2197 }
2198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002200 foptions, pcliprect, unicodebuf, len, unicodepdy);
2201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 }
2203 }
2204 else
2205#endif
2206 {
2207#ifdef FEAT_RIGHTLEFT
2208 /* If we can't use ETO_IGNORELANGUAGE, we can't tell Windows not to
2209 * mess up RL text, so we have to draw it character-by-character.
2210 * Only do this if RL is on, since it's slow. */
2211 if (curwin->w_p_rl && !(foptions & ETO_IGNORELANGUAGE))
2212 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
2213 foptions, pcliprect, (char *)text, len, padding);
2214 else
2215#endif
2216 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
2217 foptions, pcliprect, (char *)text, len, padding);
2218 }
2219
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002220 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 if (flags & DRAW_UNDERL)
2222 {
2223 hpen = CreatePen(PS_SOLID, 1, gui.currFgColor);
2224 old_pen = SelectObject(s_hdc, hpen);
2225 /* When p_linespace is 0, overwrite the bottom row of pixels.
2226 * Otherwise put the line just below the character. */
2227 y = FILL_Y(row + 1) - 1;
2228#ifndef MSWIN16_FASTTEXT
2229 if (p_linespace > 1)
2230 y -= p_linespace - 1;
2231#endif
2232 MoveToEx(s_hdc, FILL_X(col), y, NULL);
2233 /* Note: LineTo() excludes the last pixel in the line. */
2234 LineTo(s_hdc, FILL_X(col + len), y);
2235 DeleteObject(SelectObject(s_hdc, old_pen));
2236 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002237
2238 /* Undercurl */
2239 if (flags & DRAW_UNDERC)
2240 {
2241 int x;
2242 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002243 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002244
2245 y = FILL_Y(row + 1) - 1;
2246 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
2247 {
2248 offset = val[x % 8];
2249 SetPixel(s_hdc, x, y - offset, gui.currSpColor);
2250 }
2251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252}
2253
2254
2255/*
2256 * Output routines.
2257 */
2258
2259/* Flush any output to the screen */
2260 void
2261gui_mch_flush(void)
2262{
2263# if defined(__BORLANDC__)
2264 /*
2265 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
2266 * prototype declaration.
2267 * The compiler complains if __stdcall is not used in both declarations.
2268 */
2269 BOOL __stdcall GdiFlush(void);
2270# endif
2271
2272 GdiFlush();
2273}
2274
2275 static void
2276clear_rect(RECT *rcp)
2277{
2278 HBRUSH hbr;
2279
2280 hbr = CreateSolidBrush(gui.back_pixel);
2281 FillRect(s_hdc, rcp, hbr);
2282 DeleteBrush(hbr);
2283}
2284
2285
Bram Moolenaarc716c302006-01-21 22:12:51 +00002286 void
2287gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
2288{
2289 RECT workarea_rect;
2290
2291 get_work_area(&workarea_rect);
2292
2293 *screen_w = workarea_rect.right
2294 - GetSystemMetrics(SM_CXFRAME) * 2;
2295
2296 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
2297 * the menubar for MSwin, we subtract it from the screen height, so that
2298 * the window size can be made to fit on the screen. */
2299 *screen_h = workarea_rect.bottom
2300 - GetSystemMetrics(SM_CYFRAME) * 2
2301 - GetSystemMetrics(SM_CYCAPTION)
2302#ifdef FEAT_MENU
2303 - gui_mswin_get_menu_height(FALSE)
2304#endif
2305 ;
2306}
2307
2308
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309#if defined(FEAT_MENU) || defined(PROTO)
2310/*
2311 * Add a sub menu to the menu bar.
2312 */
2313 void
2314gui_mch_add_menu(
2315 vimmenu_T *menu,
2316 int pos)
2317{
2318 vimmenu_T *parent = menu->parent;
2319
2320 menu->submenu_id = CreatePopupMenu();
2321 menu->id = s_menu_id++;
2322
2323 if (menu_is_menubar(menu->name))
2324 {
2325 if (is_winnt_3())
2326 {
2327 InsertMenu((parent == NULL) ? s_menuBar : parent->submenu_id,
2328 (UINT)pos, MF_POPUP | MF_STRING | MF_BYPOSITION,
2329 (UINT)menu->submenu_id, (LPCTSTR) menu->name);
2330 }
2331 else
2332 {
2333#ifdef FEAT_MBYTE
2334 WCHAR *wn = NULL;
2335 int n;
2336
2337 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2338 {
2339 /* 'encoding' differs from active codepage: convert menu name
2340 * and use wide function */
2341 wn = enc_to_ucs2(menu->name, NULL);
2342 if (wn != NULL)
2343 {
2344 MENUITEMINFOW infow;
2345
2346 infow.cbSize = sizeof(infow);
2347 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
2348 | MIIM_SUBMENU;
2349 infow.dwItemData = (DWORD)menu;
2350 infow.wID = menu->id;
2351 infow.fType = MFT_STRING;
2352 infow.dwTypeData = wn;
2353 infow.cch = wcslen(wn);
2354 infow.hSubMenu = menu->submenu_id;
2355 n = InsertMenuItemW((parent == NULL)
2356 ? s_menuBar : parent->submenu_id,
2357 (UINT)pos, TRUE, &infow);
2358 vim_free(wn);
2359 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2360 /* Failed, try using non-wide function. */
2361 wn = NULL;
2362 }
2363 }
2364
2365 if (wn == NULL)
2366#endif
2367 {
2368 MENUITEMINFO info;
2369
2370 info.cbSize = sizeof(info);
2371 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
2372 info.dwItemData = (DWORD)menu;
2373 info.wID = menu->id;
2374 info.fType = MFT_STRING;
2375 info.dwTypeData = (LPTSTR)menu->name;
2376 info.cch = (UINT)STRLEN(menu->name);
2377 info.hSubMenu = menu->submenu_id;
2378 InsertMenuItem((parent == NULL)
2379 ? s_menuBar : parent->submenu_id,
2380 (UINT)pos, TRUE, &info);
2381 }
2382 }
2383 }
2384
2385 /* Fix window size if menu may have wrapped */
2386 if (parent == NULL)
2387 gui_mswin_get_menu_height(!gui.starting);
2388#ifdef FEAT_TEAROFF
2389 else if (IsWindow(parent->tearoff_handle))
2390 rebuild_tearoff(parent);
2391#endif
2392}
2393
2394 void
2395gui_mch_show_popupmenu(vimmenu_T *menu)
2396{
2397 POINT mp;
2398
2399 (void)GetCursorPos((LPPOINT)&mp);
2400 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
2401}
2402
2403 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00002404gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405{
2406 vimmenu_T *menu = gui_find_menu(path_name);
2407
2408 if (menu != NULL)
2409 {
2410 POINT p;
2411
2412 /* Find the position of the current cursor */
2413 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00002414 if (mouse_pos)
2415 {
2416 int mx, my;
2417
2418 gui_mch_getmouse(&mx, &my);
2419 p.x += mx;
2420 p.y += my;
2421 }
2422 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 {
2424 p.x += TEXT_X(W_WINCOL(curwin) + curwin->w_wcol + 1);
2425 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
2426 }
2427 msg_scroll = FALSE;
2428 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
2429 }
2430}
2431
2432#if defined(FEAT_TEAROFF) || defined(PROTO)
2433/*
2434 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
2435 * create it as a pseudo-"tearoff menu".
2436 */
2437 void
2438gui_make_tearoff(char_u *path_name)
2439{
2440 vimmenu_T *menu = gui_find_menu(path_name);
2441
2442 /* Found the menu, so tear it off. */
2443 if (menu != NULL)
2444 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
2445}
2446#endif
2447
2448/*
2449 * Add a menu item to a menu
2450 */
2451 void
2452gui_mch_add_menu_item(
2453 vimmenu_T *menu,
2454 int idx)
2455{
2456 vimmenu_T *parent = menu->parent;
2457
2458 menu->id = s_menu_id++;
2459 menu->submenu_id = NULL;
2460
2461#ifdef FEAT_TEAROFF
2462 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
2463 {
2464 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
2465 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
2466 }
2467 else
2468#endif
2469#ifdef FEAT_TOOLBAR
2470 if (menu_is_toolbar(parent->name))
2471 {
2472 TBBUTTON newtb;
2473
2474 vim_memset(&newtb, 0, sizeof(newtb));
2475 if (menu_is_separator(menu->name))
2476 {
2477 newtb.iBitmap = 0;
2478 newtb.fsStyle = TBSTYLE_SEP;
2479 }
2480 else
2481 {
2482 newtb.iBitmap = get_toolbar_bitmap(menu);
2483 newtb.fsStyle = TBSTYLE_BUTTON;
2484 }
2485 newtb.idCommand = menu->id;
2486 newtb.fsState = TBSTATE_ENABLED;
2487 newtb.iString = 0;
2488 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
2489 (LPARAM)&newtb);
2490 menu->submenu_id = (HMENU)-1;
2491 }
2492 else
2493#endif
2494 {
2495#ifdef FEAT_MBYTE
2496 WCHAR *wn = NULL;
2497 int n;
2498
2499 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2500 {
2501 /* 'encoding' differs from active codepage: convert menu item name
2502 * and use wide function */
2503 wn = enc_to_ucs2(menu->name, NULL);
2504 if (wn != NULL)
2505 {
2506 n = InsertMenuW(parent->submenu_id, (UINT)idx,
2507 (menu_is_separator(menu->name)
2508 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
2509 (UINT)menu->id, wn);
2510 vim_free(wn);
2511 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2512 /* Failed, try using non-wide function. */
2513 wn = NULL;
2514 }
2515 }
2516 if (wn == NULL)
2517#endif
2518 InsertMenu(parent->submenu_id, (UINT)idx,
2519 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
2520 | MF_BYPOSITION,
2521 (UINT)menu->id, (LPCTSTR)menu->name);
2522#ifdef FEAT_TEAROFF
2523 if (IsWindow(parent->tearoff_handle))
2524 rebuild_tearoff(parent);
2525#endif
2526 }
2527}
2528
2529/*
2530 * Destroy the machine specific menu widget.
2531 */
2532 void
2533gui_mch_destroy_menu(vimmenu_T *menu)
2534{
2535#ifdef FEAT_TOOLBAR
2536 /*
2537 * is this a toolbar button?
2538 */
2539 if (menu->submenu_id == (HMENU)-1)
2540 {
2541 int iButton;
2542
2543 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
2544 (WPARAM)menu->id, 0);
2545 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
2546 }
2547 else
2548#endif
2549 {
2550 if (menu->parent != NULL
2551 && menu_is_popup(menu->parent->dname)
2552 && menu->parent->submenu_id != NULL)
2553 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
2554 else
2555 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
2556 if (menu->submenu_id != NULL)
2557 DestroyMenu(menu->submenu_id);
2558#ifdef FEAT_TEAROFF
2559 if (IsWindow(menu->tearoff_handle))
2560 DestroyWindow(menu->tearoff_handle);
2561 if (menu->parent != NULL
2562 && menu->parent->children != NULL
2563 && IsWindow(menu->parent->tearoff_handle))
2564 {
2565 /* This menu must not show up when rebuilding the tearoff window. */
2566 menu->modes = 0;
2567 rebuild_tearoff(menu->parent);
2568 }
2569#endif
2570 }
2571}
2572
2573#ifdef FEAT_TEAROFF
2574 static void
2575rebuild_tearoff(vimmenu_T *menu)
2576{
2577 /*hackish*/
2578 char_u tbuf[128];
2579 RECT trect;
2580 RECT rct;
2581 RECT roct;
2582 int x, y;
2583
2584 HWND thwnd = menu->tearoff_handle;
2585
2586 GetWindowText(thwnd, tbuf, 127);
2587 if (GetWindowRect(thwnd, &trect)
2588 && GetWindowRect(s_hwnd, &rct)
2589 && GetClientRect(s_hwnd, &roct))
2590 {
2591 x = trect.left - rct.left;
2592 y = (trect.top - rct.bottom + roct.bottom);
2593 }
2594 else
2595 {
2596 x = y = 0xffffL;
2597 }
2598 DestroyWindow(thwnd);
2599 if (menu->children != NULL)
2600 {
2601 gui_mch_tearoff(tbuf, menu, x, y);
2602 if (IsWindow(menu->tearoff_handle))
2603 (void) SetWindowPos(menu->tearoff_handle,
2604 NULL,
2605 (int)trect.left,
2606 (int)trect.top,
2607 0, 0,
2608 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
2609 }
2610}
2611#endif /* FEAT_TEAROFF */
2612
2613/*
2614 * Make a menu either grey or not grey.
2615 */
2616 void
2617gui_mch_menu_grey(
2618 vimmenu_T *menu,
2619 int grey)
2620{
2621#ifdef FEAT_TOOLBAR
2622 /*
2623 * is this a toolbar button?
2624 */
2625 if (menu->submenu_id == (HMENU)-1)
2626 {
2627 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
2628 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
2629 }
2630 else
2631#endif
2632 if (grey)
2633 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_GRAYED);
2634 else
2635 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2636
2637#ifdef FEAT_TEAROFF
2638 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
2639 {
2640 WORD menuID;
2641 HWND menuHandle;
2642
2643 /*
2644 * A tearoff button has changed state.
2645 */
2646 if (menu->children == NULL)
2647 menuID = (WORD)(menu->id);
2648 else
2649 menuID = (WORD)((DWORD)(menu->submenu_id) | (DWORD)0x8000);
2650 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
2651 if (menuHandle)
2652 EnableWindow(menuHandle, !grey);
2653
2654 }
2655#endif
2656}
2657
2658#endif /* FEAT_MENU */
2659
2660
2661/* define some macros used to make the dialogue creation more readable */
2662
2663#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
2664#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00002665#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666
2667#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2668/*
2669 * stuff for dialogs
2670 */
2671
2672/*
2673 * The callback routine used by all the dialogs. Very simple. First,
2674 * acknowledges the INITDIALOG message so that Windows knows to do standard
2675 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
2676 * pressed, return that button's ID - IDCANCEL (2), which is the button's
2677 * number.
2678 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002679/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 static LRESULT CALLBACK
2681dialog_callback(
2682 HWND hwnd,
2683 UINT message,
2684 WPARAM wParam,
2685 LPARAM lParam)
2686{
2687 if (message == WM_INITDIALOG)
2688 {
2689 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
2690 /* Set focus to the dialog. Set the default button, if specified. */
2691 (void)SetFocus(hwnd);
2692 if (dialog_default_button > IDCANCEL)
2693 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
2694 return FALSE;
2695 }
2696
2697 if (message == WM_COMMAND)
2698 {
2699 int button = LOWORD(wParam);
2700
2701 /* Don't end the dialog if something was selected that was
2702 * not a button.
2703 */
2704 if (button >= DLG_NONBUTTON_CONTROL)
2705 return TRUE;
2706
2707 /* If the edit box exists, copy the string. */
2708 if (s_textfield != NULL)
2709 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
2710 s_textfield, IOSIZE);
2711
2712 /*
2713 * Need to check for IDOK because if the user just hits Return to
2714 * accept the default value, some reason this is what we get.
2715 */
2716 if (button == IDOK)
2717 {
2718 if (dialog_default_button > IDCANCEL)
2719 EndDialog(hwnd, dialog_default_button);
2720 }
2721 else
2722 EndDialog(hwnd, button - IDCANCEL);
2723 return TRUE;
2724 }
2725
2726 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
2727 {
2728 EndDialog(hwnd, 0);
2729 return TRUE;
2730 }
2731 return FALSE;
2732}
2733
2734/*
2735 * Create a dialog dynamically from the parameter strings.
2736 * type = type of dialog (question, alert, etc.)
2737 * title = dialog title. may be NULL for default title.
2738 * message = text to display. Dialog sizes to accommodate it.
2739 * buttons = '\n' separated list of button captions, default first.
2740 * dfltbutton = number of default button.
2741 *
2742 * This routine returns 1 if the first button is pressed,
2743 * 2 for the second, etc.
2744 *
2745 * 0 indicates Esc was pressed.
2746 * -1 for unexpected error
2747 *
2748 * If stubbing out this fn, return 1.
2749 */
2750
2751static const char_u *dlg_icons[] = /* must match names in resource file */
2752{
2753 "IDR_VIM",
2754 "IDR_VIM_ERROR",
2755 "IDR_VIM_ALERT",
2756 "IDR_VIM_INFO",
2757 "IDR_VIM_QUESTION"
2758};
2759
2760#ifdef USE_SYSMENU_FONT
2761/*
2762 * Get Menu Font.
2763 * Return OK or FAIL.
2764 */
2765 static int
2766gui_w32_get_menu_font(LOGFONT *lf)
2767{
2768 NONCLIENTMETRICS nm;
2769
2770 nm.cbSize = sizeof(NONCLIENTMETRICS);
2771 if (!SystemParametersInfo(
2772 SPI_GETNONCLIENTMETRICS,
2773 sizeof(NONCLIENTMETRICS),
2774 &nm,
2775 0))
2776 return FAIL;
2777 *lf = nm.lfMenuFont;
2778 return OK;
2779}
2780#endif
2781
2782 int
2783gui_mch_dialog(
2784 int type,
2785 char_u *title,
2786 char_u *message,
2787 char_u *buttons,
2788 int dfltbutton,
2789 char_u *textfield)
2790{
2791 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00002792 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 int numButtons;
2794 int *buttonWidths, *buttonPositions;
2795 int buttonYpos;
2796 int nchar, i;
2797 DWORD lStyle;
2798 int dlgwidth = 0;
2799 int dlgheight;
2800 int editboxheight;
2801 int horizWidth = 0;
2802 int msgheight;
2803 char_u *pstart;
2804 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002805 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 char_u *tbuffer;
2807 RECT rect;
2808 HWND hwnd;
2809 HDC hdc;
2810 HFONT font, oldFont;
2811 TEXTMETRIC fontInfo;
2812 int fontHeight;
2813 int textWidth, minButtonWidth, messageWidth;
2814 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002815 int maxDialogHeight;
2816 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 int vertical;
2818 int dlgPaddingX;
2819 int dlgPaddingY;
2820#ifdef USE_SYSMENU_FONT
2821 LOGFONT lfSysmenu;
2822 int use_lfSysmenu = FALSE;
2823#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002824 garray_T ga;
2825 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826
2827#ifndef NO_CONSOLE
2828 /* Don't output anything in silent mode ("ex -s") */
2829 if (silent_mode)
2830 return dfltbutton; /* return default option */
2831#endif
2832
Bram Moolenaar748bf032005-02-02 23:04:36 +00002833#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 /* If there is no window yet, open it. */
2835 if (s_hwnd == NULL && gui_mch_init() == FAIL)
2836 return dfltbutton;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002837#else
2838 if (s_hwnd == NULL)
2839 get_dialog_font_metrics();
2840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841
2842 if ((type < 0) || (type > VIM_LAST_TYPE))
2843 type = 0;
2844
2845 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002846 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002847 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
2848 DLG_ALLOC_SIZE + STRLEN(message));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849
2850 if (p == NULL)
2851 return -1;
2852
2853 /*
2854 * make a copy of 'buttons' to fiddle with it. complier grizzles because
2855 * vim_strsave() doesn't take a const arg (why not?), so cast away the
2856 * const.
2857 */
2858 tbuffer = vim_strsave(buttons);
2859 if (tbuffer == NULL)
2860 return -1;
2861
2862 --dfltbutton; /* Change from one-based to zero-based */
2863
2864 /* Count buttons */
2865 numButtons = 1;
2866 for (i = 0; tbuffer[i] != '\0'; i++)
2867 {
2868 if (tbuffer[i] == DLG_BUTTON_SEP)
2869 numButtons++;
2870 }
2871 if (dfltbutton >= numButtons)
2872 dfltbutton = -1;
2873
2874 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002875 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 if (buttonWidths == NULL)
2877 return -1;
2878
2879 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00002880 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 if (buttonPositions == NULL)
2882 return -1;
2883
2884 /*
2885 * Calculate how big the dialog must be.
2886 */
2887 hwnd = GetDesktopWindow();
2888 hdc = GetWindowDC(hwnd);
2889#ifdef USE_SYSMENU_FONT
2890 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
2891 {
2892 font = CreateFontIndirect(&lfSysmenu);
2893 use_lfSysmenu = TRUE;
2894 }
2895 else
2896#endif
2897 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2898 VARIABLE_PITCH , DLG_FONT_NAME);
2899 if (s_usenewlook)
2900 {
2901 oldFont = SelectFont(hdc, font);
2902 dlgPaddingX = DLG_PADDING_X;
2903 dlgPaddingY = DLG_PADDING_Y;
2904 }
2905 else
2906 {
2907 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
2908 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
2909 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
2910 }
2911 GetTextMetrics(hdc, &fontInfo);
2912 fontHeight = fontInfo.tmHeight;
2913
2914 /* Minimum width for horizontal button */
2915 minButtonWidth = GetTextWidth(hdc, "Cancel", 6);
2916
2917 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002918 if (s_hwnd == NULL)
2919 {
2920 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921
Bram Moolenaarc716c302006-01-21 22:12:51 +00002922 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002923 get_work_area(&workarea_rect);
2924 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
2925 if (maxDialogWidth > 600)
2926 maxDialogWidth = 600;
2927 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 100;
2928 }
2929 else
2930 {
2931 /* Use our own window for the size, unless it's very small. */
2932 GetWindowRect(s_hwnd, &rect);
2933 maxDialogWidth = rect.right - rect.left
2934 - GetSystemMetrics(SM_CXFRAME) * 2;
2935 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
2936 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002937
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002938 maxDialogHeight = rect.bottom - rect.top
2939 - GetSystemMetrics(SM_CXFRAME) * 2;
2940 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
2941 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
2942 }
2943
2944 /* Set dlgwidth to width of message.
2945 * Copy the message into "ga", changing NL to CR-NL and inserting line
2946 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 pstart = message;
2948 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002949 msgheight = 0;
2950 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 do
2952 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002953 msgheight += fontHeight; /* at least one line */
2954
2955 /* Need to figure out where to break the string. The system does it
2956 * at a word boundary, which would mean we can't compute the number of
2957 * wrapped lines. */
2958 textWidth = 0;
2959 last_white = NULL;
2960 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00002961 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002962#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002963 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002964#else
2965 l = 1;
2966#endif
2967 if (l == 1 && vim_iswhite(*pend)
2968 && textWidth > maxDialogWidth * 3 / 4)
2969 last_white = pend;
2970 textWidth += GetTextWidth(hdc, pend, l);
2971 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00002972 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002973 /* Line will wrap. */
2974 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002975 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002976 textWidth = 0;
2977
2978 if (last_white != NULL)
2979 {
2980 /* break the line just after a space */
2981 ga.ga_len -= pend - (last_white + 1);
2982 pend = last_white + 1;
2983 last_white = NULL;
2984 }
2985 ga_append(&ga, '\r');
2986 ga_append(&ga, '\n');
2987 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002988 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002989
2990 while (--l >= 0)
2991 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00002992 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002993 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002995
2996 ga_append(&ga, '\r');
2997 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 pstart = pend + 1;
2999 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003000
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003001 if (ga.ga_data != NULL)
3002 message = ga.ga_data;
3003
Bram Moolenaar748bf032005-02-02 23:04:36 +00003004 messageWidth += 10; /* roundoff space */
3005
3006 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
3007 if (msgheight > maxDialogHeight)
3008 {
3009 msgheight = maxDialogHeight;
3010 scroll_flag = WS_VSCROLL;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003011 messageWidth += GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013
3014 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003015 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016
3017 if (msgheight < DLG_ICON_HEIGHT)
3018 msgheight = DLG_ICON_HEIGHT;
3019
3020 /*
3021 * Check button names. A long one will make the dialog wider.
3022 */
3023 vertical = (vim_strchr(p_go, GO_VERTICAL) != NULL);
3024 if (!vertical)
3025 {
3026 // Place buttons horizontally if they fit.
3027 horizWidth = dlgPaddingX;
3028 pstart = tbuffer;
3029 i = 0;
3030 do
3031 {
3032 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
3033 if (pend == NULL)
3034 pend = pstart + STRLEN(pstart); // Last button name.
3035 textWidth = GetTextWidth(hdc, pstart, (int)(pend - pstart));
3036 if (textWidth < minButtonWidth)
3037 textWidth = minButtonWidth;
3038 textWidth += dlgPaddingX; /* Padding within button */
3039 buttonWidths[i] = textWidth;
3040 buttonPositions[i++] = horizWidth;
3041 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
3042 pstart = pend + 1;
3043 } while (*pend != NUL);
3044
3045 if (horizWidth > maxDialogWidth)
3046 vertical = TRUE; // Too wide to fit on the screen.
3047 else if (horizWidth > dlgwidth)
3048 dlgwidth = horizWidth;
3049 }
3050
3051 if (vertical)
3052 {
3053 // Stack buttons vertically.
3054 pstart = tbuffer;
3055 do
3056 {
3057 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
3058 if (pend == NULL)
3059 pend = pstart + STRLEN(pstart); // Last button name.
3060 textWidth = GetTextWidth(hdc, pstart, (int)(pend - pstart));
3061 textWidth += dlgPaddingX; /* Padding within button */
3062 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
3063 if (textWidth > dlgwidth)
3064 dlgwidth = textWidth;
3065 pstart = pend + 1;
3066 } while (*pend != NUL);
3067 }
3068
3069 if (dlgwidth < DLG_MIN_WIDTH)
3070 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
3071
3072 /* start to fill in the dlgtemplate information. addressing by WORDs */
3073 if (s_usenewlook)
3074 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
3075 else
3076 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
3077
3078 add_long(lStyle);
3079 add_long(0); // (lExtendedStyle)
3080 pnumitems = p; /*save where the number of items must be stored*/
3081 add_word(0); // NumberOfItems(will change later)
3082 add_word(10); // x
3083 add_word(10); // y
3084 add_word(PixelToDialogX(dlgwidth)); // cx
3085
3086 // Dialog height.
3087 if (vertical)
3088 dlgheight = msgheight + 2 * dlgPaddingY +
3089 DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
3090 else
3091 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
3092
3093 // Dialog needs to be taller if contains an edit box.
3094 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
3095 if (textfield != NULL)
3096 dlgheight += editboxheight;
3097
3098 add_word(PixelToDialogY(dlgheight));
3099
3100 add_word(0); // Menu
3101 add_word(0); // Class
3102
3103 /* copy the title of the dialog */
3104 nchar = nCopyAnsiToWideChar(p, (title ?
3105 (LPSTR)title :
3106 (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
3107 p += nchar;
3108
3109 if (s_usenewlook)
3110 {
3111 /* do the font, since DS_3DLOOK doesn't work properly */
3112#ifdef USE_SYSMENU_FONT
3113 if (use_lfSysmenu)
3114 {
3115 /* point size */
3116 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
3117 GetDeviceCaps(hdc, LOGPIXELSY));
3118 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
3119 }
3120 else
3121#endif
3122 {
3123 *p++ = DLG_FONT_POINT_SIZE; // point size
3124 nchar = nCopyAnsiToWideChar(p, TEXT(DLG_FONT_NAME));
3125 }
3126 p += nchar;
3127 }
3128
3129 buttonYpos = msgheight + 2 * dlgPaddingY;
3130
3131 if (textfield != NULL)
3132 buttonYpos += editboxheight;
3133
3134 pstart = tbuffer;
3135 if (!vertical)
3136 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
3137 for (i = 0; i < numButtons; i++)
3138 {
3139 /* get end of this button. */
3140 for ( pend = pstart;
3141 *pend && (*pend != DLG_BUTTON_SEP);
3142 pend++)
3143 ;
3144
3145 if (*pend)
3146 *pend = '\0';
3147
3148 /*
3149 * old NOTE:
3150 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
3151 * the focus to the first tab-able button and in so doing makes that
3152 * the default!! Grrr. Workaround: Make the default button the only
3153 * one with WS_TABSTOP style. Means user can't tab between buttons, but
3154 * he/she can use arrow keys.
3155 *
3156 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
3157 * right buttun when hitting <Enter>. E.g., for the ":confirm quit"
3158 * dialog. Also needed for when the textfield is the default control.
3159 * It appears to work now (perhaps not on Win95?).
3160 */
3161 if (vertical)
3162 {
3163 p = add_dialog_element(p,
3164 (i == dfltbutton
3165 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
3166 PixelToDialogX(DLG_VERT_PADDING_X),
3167 PixelToDialogY(buttonYpos /* TBK */
3168 + 2 * fontHeight * i),
3169 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
3170 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
3171 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, pstart);
3172 }
3173 else
3174 {
3175 p = add_dialog_element(p,
3176 (i == dfltbutton
3177 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
3178 PixelToDialogX(horizWidth + buttonPositions[i]),
3179 PixelToDialogY(buttonYpos), /* TBK */
3180 PixelToDialogX(buttonWidths[i]),
3181 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
3182 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, pstart);
3183 }
3184 pstart = pend + 1; /*next button*/
3185 }
3186 *pnumitems += numButtons;
3187
3188 /* Vim icon */
3189 p = add_dialog_element(p, SS_ICON,
3190 PixelToDialogX(dlgPaddingX),
3191 PixelToDialogY(dlgPaddingY),
3192 PixelToDialogX(DLG_ICON_WIDTH),
3193 PixelToDialogY(DLG_ICON_HEIGHT),
3194 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
3195 dlg_icons[type]);
3196
Bram Moolenaar748bf032005-02-02 23:04:36 +00003197#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 /* Dialog message */
3199 p = add_dialog_element(p, SS_LEFT,
3200 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
3201 PixelToDialogY(dlgPaddingY),
3202 (WORD)(PixelToDialogX(messageWidth) + 1),
3203 PixelToDialogY(msgheight),
3204 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0082, message);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003205#else
3206 /* Dialog message */
3207 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
3208 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
3209 PixelToDialogY(dlgPaddingY),
3210 (WORD)(PixelToDialogX(messageWidth) + 1),
3211 PixelToDialogY(msgheight),
3212 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, message);
3213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
3215 /* Edit box */
3216 if (textfield != NULL)
3217 {
3218 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
3219 PixelToDialogX(2 * dlgPaddingX),
3220 PixelToDialogY(2 * dlgPaddingY + msgheight),
3221 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
3222 PixelToDialogY(fontHeight + dlgPaddingY),
3223 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, textfield);
3224 *pnumitems += 1;
3225 }
3226
3227 *pnumitems += 2;
3228
3229 SelectFont(hdc, oldFont);
3230 DeleteObject(font);
3231 ReleaseDC(hwnd, hdc);
3232
3233 /* Let the dialog_callback() function know which button to make default
3234 * If we have an edit box, make that the default. We also need to tell
3235 * dialog_callback() if this dialog contains an edit box or not. We do
3236 * this by setting s_textfield if it does.
3237 */
3238 if (textfield != NULL)
3239 {
3240 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
3241 s_textfield = textfield;
3242 }
3243 else
3244 {
3245 dialog_default_button = IDCANCEL + 1 + dfltbutton;
3246 s_textfield = NULL;
3247 }
3248
3249 /* show the dialog box modally and get a return value */
3250 nchar = (int)DialogBoxIndirect(
3251 s_hinst,
3252 (LPDLGTEMPLATE)pdlgtemplate,
3253 s_hwnd,
3254 (DLGPROC)dialog_callback);
3255
3256 LocalFree(LocalHandle(pdlgtemplate));
3257 vim_free(tbuffer);
3258 vim_free(buttonWidths);
3259 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003260 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261
3262 /* Focus back to our window (for when MDI is used). */
3263 (void)SetFocus(s_hwnd);
3264
3265 return nchar;
3266}
3267
3268#endif /* FEAT_GUI_DIALOG */
3269/*
3270 * Put a simple element (basic class) onto a dialog template in memory.
3271 * return a pointer to where the next item should be added.
3272 *
3273 * parameters:
3274 * lStyle = additional style flags
3275 * (be careful, NT3.51 & Win32s will ignore the new ones)
3276 * x,y = x & y positions IN DIALOG UNITS
3277 * w,h = width and height IN DIALOG UNITS
3278 * Id = ID used in messages
3279 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
3280 * caption = usually text or resource name
3281 *
3282 * TODO: use the length information noted here to enable the dialog creation
3283 * routines to work out more exactly how much memory they need to alloc.
3284 */
3285 static PWORD
3286add_dialog_element(
3287 PWORD p,
3288 DWORD lStyle,
3289 WORD x,
3290 WORD y,
3291 WORD w,
3292 WORD h,
3293 WORD Id,
3294 WORD clss,
3295 const char *caption)
3296{
3297 int nchar;
3298
3299 p = lpwAlign(p); /* Align to dword boundary*/
3300 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
3301 *p++ = LOWORD(lStyle);
3302 *p++ = HIWORD(lStyle);
3303 *p++ = 0; // LOWORD (lExtendedStyle)
3304 *p++ = 0; // HIWORD (lExtendedStyle)
3305 *p++ = x;
3306 *p++ = y;
3307 *p++ = w;
3308 *p++ = h;
3309 *p++ = Id; //9 or 10 words in all
3310
3311 *p++ = (WORD)0xffff;
3312 *p++ = clss; //2 more here
3313
3314 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption); //strlen(caption)+1
3315 p += nchar;
3316
3317 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
3318
3319 return p; //total = 15+ (strlen(caption)) words
3320 // = 30 + 2(strlen(caption) bytes reqd
3321}
3322
3323
3324/*
3325 * Helper routine. Take an input pointer, return closest pointer that is
3326 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
3327 */
3328 static LPWORD
3329lpwAlign(
3330 LPWORD lpIn)
3331{
3332 ULONG ul;
3333
3334 ul = (ULONG)lpIn;
3335 ul += 3;
3336 ul >>= 2;
3337 ul <<= 2;
3338 return (LPWORD)ul;
3339}
3340
3341/*
3342 * Helper routine. Takes second parameter as Ansi string, copies it to first
3343 * parameter as wide character (16-bits / char) string, and returns integer
3344 * number of wide characters (words) in string (including the trailing wide
3345 * char NULL). Partly taken from the Win32SDK samples.
3346 */
3347 static int
3348nCopyAnsiToWideChar(
3349 LPWORD lpWCStr,
3350 LPSTR lpAnsiIn)
3351{
3352 int nChar = 0;
3353#ifdef FEAT_MBYTE
3354 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
3355 int i;
3356 WCHAR *wn;
3357
3358 if (enc_codepage == 0 && (int)GetACP() != enc_codepage)
3359 {
3360 /* Not a codepage, use our own conversion function. */
3361 wn = enc_to_ucs2(lpAnsiIn, NULL);
3362 if (wn != NULL)
3363 {
3364 wcscpy(lpWCStr, wn);
3365 nChar = wcslen(wn) + 1;
3366 vim_free(wn);
3367 }
3368 }
3369 if (nChar == 0)
3370 /* Use Win32 conversion function. */
3371 nChar = MultiByteToWideChar(
3372 enc_codepage > 0 ? enc_codepage : CP_ACP,
3373 MB_PRECOMPOSED,
3374 lpAnsiIn, len,
3375 lpWCStr, len);
3376 for (i = 0; i < nChar; ++i)
3377 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
3378 lpWCStr[i] = (WORD)' ';
3379#else
3380 do
3381 {
3382 if (*lpAnsiIn == '\t')
3383 *lpWCStr++ = (WORD)' ';
3384 else
3385 *lpWCStr++ = (WORD)*lpAnsiIn;
3386 nChar++;
3387 } while (*lpAnsiIn++);
3388#endif
3389
3390 return nChar;
3391}
3392
3393
3394#ifdef FEAT_TEAROFF
3395/*
3396 * The callback function for all the modeless dialogs that make up the
3397 * "tearoff menus" Very simple - forward button presses (to fool Vim into
3398 * thinking its menus have been clicked), and go away when closed.
3399 */
3400 static LRESULT CALLBACK
3401tearoff_callback(
3402 HWND hwnd,
3403 UINT message,
3404 WPARAM wParam,
3405 LPARAM lParam)
3406{
3407 if (message == WM_INITDIALOG)
3408 return (TRUE);
3409
3410 /* May show the mouse pointer again. */
3411 HandleMouseHide(message, lParam);
3412
3413 if (message == WM_COMMAND)
3414 {
3415 if ((WORD)(LOWORD(wParam)) & 0x8000)
3416 {
3417 POINT mp;
3418 RECT rect;
3419
3420 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
3421 {
3422 (void)TrackPopupMenu(
3423 (HMENU)(LOWORD(wParam) ^ 0x8000),
3424 TPM_LEFTALIGN | TPM_LEFTBUTTON,
3425 (int)rect.right - 8,
3426 (int)mp.y,
3427 (int)0, /*reserved param*/
3428 s_hwnd,
3429 NULL);
3430 /*
3431 * NOTE: The pop-up menu can eat the mouse up event.
3432 * We deal with this in normal.c.
3433 */
3434 }
3435 }
3436 else
3437 /* Pass on messages to the main Vim window */
3438 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
3439 /*
3440 * Give main window the focus back: this is so after
3441 * choosing a tearoff button you can start typing again
3442 * straight away.
3443 */
3444 (void)SetFocus(s_hwnd);
3445 return TRUE;
3446 }
3447 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
3448 {
3449 DestroyWindow(hwnd);
3450 return TRUE;
3451 }
3452
3453 /* When moved around, give main window the focus back. */
3454 if (message == WM_EXITSIZEMOVE)
3455 (void)SetActiveWindow(s_hwnd);
3456
3457 return FALSE;
3458}
3459#endif
3460
3461
3462/*
3463 * Decide whether to use the "new look" (small, non-bold font) or the "old
3464 * look" (big, clanky font) for dialogs, and work out a few values for use
3465 * later accordingly.
3466 */
3467 static void
3468get_dialog_font_metrics(void)
3469{
3470 HDC hdc;
3471 HFONT hfontTools = 0;
3472 DWORD dlgFontSize;
3473 SIZE size;
3474#ifdef USE_SYSMENU_FONT
3475 LOGFONT lfSysmenu;
3476#endif
3477
3478 s_usenewlook = FALSE;
3479
3480 /*
3481 * For NT3.51 and Win32s, we stick with the old look
3482 * because it matches everything else.
3483 */
3484 if (!is_winnt_3())
3485 {
3486#ifdef USE_SYSMENU_FONT
3487 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
3488 hfontTools = CreateFontIndirect(&lfSysmenu);
3489 else
3490#endif
3491 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
3492 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
3493
3494 if (hfontTools)
3495 {
3496 hdc = GetDC(s_hwnd);
3497 SelectObject(hdc, hfontTools);
3498 /*
3499 * GetTextMetrics() doesn't return the right value in
3500 * tmAveCharWidth, so we have to figure out the dialog base units
3501 * ourselves.
3502 */
3503 GetTextExtentPoint(hdc,
3504 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
3505 52, &size);
3506 ReleaseDC(s_hwnd, hdc);
3507
3508 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
3509 s_dlgfntheight = (WORD)size.cy;
3510 s_usenewlook = TRUE;
3511 }
3512 }
3513
3514 if (!s_usenewlook)
3515 {
3516 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
3517 s_dlgfntwidth = LOWORD(dlgFontSize);
3518 s_dlgfntheight = HIWORD(dlgFontSize);
3519 }
3520}
3521
3522#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
3523/*
3524 * Create a pseudo-"tearoff menu" based on the child
3525 * items of a given menu pointer.
3526 */
3527 static void
3528gui_mch_tearoff(
3529 char_u *title,
3530 vimmenu_T *menu,
3531 int initX,
3532 int initY)
3533{
3534 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
3535 int template_len;
3536 int nchar, textWidth, submenuWidth;
3537 DWORD lStyle;
3538 DWORD lExtendedStyle;
3539 WORD dlgwidth;
3540 WORD menuID;
3541 vimmenu_T *pmenu;
3542 vimmenu_T *the_menu = menu;
3543 HWND hwnd;
3544 HDC hdc;
3545 HFONT font, oldFont;
3546 int col, spaceWidth, len;
3547 int columnWidths[2];
3548 char_u *label, *text;
3549 int acLen = 0;
3550 int nameLen;
3551 int padding0, padding1, padding2 = 0;
3552 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003553 int x;
3554 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555#ifdef USE_SYSMENU_FONT
3556 LOGFONT lfSysmenu;
3557 int use_lfSysmenu = FALSE;
3558#endif
3559
3560 /*
3561 * If this menu is already torn off, move it to the mouse position.
3562 */
3563 if (IsWindow(menu->tearoff_handle))
3564 {
3565 POINT mp;
3566 if (GetCursorPos((LPPOINT)&mp))
3567 {
3568 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
3569 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
3570 }
3571 return;
3572 }
3573
3574 /*
3575 * Create a new tearoff.
3576 */
3577 if (*title == MNU_HIDDEN_CHAR)
3578 title++;
3579
3580 /* Allocate memory to store the dialog template. It's made bigger when
3581 * needed. */
3582 template_len = DLG_ALLOC_SIZE;
3583 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
3584 if (p == NULL)
3585 return;
3586
3587 hwnd = GetDesktopWindow();
3588 hdc = GetWindowDC(hwnd);
3589#ifdef USE_SYSMENU_FONT
3590 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
3591 {
3592 font = CreateFontIndirect(&lfSysmenu);
3593 use_lfSysmenu = TRUE;
3594 }
3595 else
3596#endif
3597 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3598 VARIABLE_PITCH , DLG_FONT_NAME);
3599 if (s_usenewlook)
3600 oldFont = SelectFont(hdc, font);
3601 else
3602 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
3603
3604 /* Calculate width of a single space. Used for padding columns to the
3605 * right width. */
3606 spaceWidth = GetTextWidth(hdc, " ", 1);
3607
3608 /* Figure out max width of the text column, the accelerator column and the
3609 * optional submenu column. */
3610 submenuWidth = 0;
3611 for (col = 0; col < 2; col++)
3612 {
3613 columnWidths[col] = 0;
3614 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
3615 {
3616 /* Use "dname" here to compute the width of the visible text. */
3617 text = (col == 0) ? pmenu->dname : pmenu->actext;
3618 if (text != NULL && *text != NUL)
3619 {
3620 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
3621 if (textWidth > columnWidths[col])
3622 columnWidths[col] = textWidth;
3623 }
3624 if (pmenu->children != NULL)
3625 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
3626 }
3627 }
3628 if (columnWidths[1] == 0)
3629 {
3630 /* no accelerators */
3631 if (submenuWidth != 0)
3632 columnWidths[0] += submenuWidth;
3633 else
3634 columnWidths[0] += spaceWidth;
3635 }
3636 else
3637 {
3638 /* there is an accelerator column */
3639 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
3640 columnWidths[1] += submenuWidth;
3641 }
3642
3643 /*
3644 * Now find the total width of our 'menu'.
3645 */
3646 textWidth = columnWidths[0] + columnWidths[1];
3647 if (submenuWidth != 0)
3648 {
3649 submenuWidth = GetTextWidth(hdc, TEAROFF_SUBMENU_LABEL,
3650 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
3651 textWidth += submenuWidth;
3652 }
3653 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
3654 if (textWidth > dlgwidth)
3655 dlgwidth = textWidth;
3656 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
3657
3658 /* W95 can't do thin dialogs, they look v. weird! */
3659 if (mch_windows95() && dlgwidth < TEAROFF_MIN_WIDTH)
3660 dlgwidth = TEAROFF_MIN_WIDTH;
3661
3662 /* start to fill in the dlgtemplate information. addressing by WORDs */
3663 if (s_usenewlook)
3664 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
3665 else
3666 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
3667
3668 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
3669 *p++ = LOWORD(lStyle);
3670 *p++ = HIWORD(lStyle);
3671 *p++ = LOWORD(lExtendedStyle);
3672 *p++ = HIWORD(lExtendedStyle);
3673 pnumitems = p; /* save where the number of items must be stored */
3674 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003675 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003677 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 else
3679 *p++ = PixelToDialogX(initX); // x
3680 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003681 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 else
3683 *p++ = PixelToDialogY(initY); // y
3684 *p++ = PixelToDialogX(dlgwidth); // cx
3685 ptrueheight = p;
3686 *p++ = 0; // dialog height: changed later anyway
3687 *p++ = 0; // Menu
3688 *p++ = 0; // Class
3689
3690 /* copy the title of the dialog */
3691 nchar = nCopyAnsiToWideChar(p, ((*title)
3692 ? (LPSTR)title
3693 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)));
3694 p += nchar;
3695
3696 if (s_usenewlook)
3697 {
3698 /* do the font, since DS_3DLOOK doesn't work properly */
3699#ifdef USE_SYSMENU_FONT
3700 if (use_lfSysmenu)
3701 {
3702 /* point size */
3703 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
3704 GetDeviceCaps(hdc, LOGPIXELSY));
3705 nchar = nCopyAnsiToWideChar(p, TEXT(lfSysmenu.lfFaceName));
3706 }
3707 else
3708#endif
3709 {
3710 *p++ = DLG_FONT_POINT_SIZE; // point size
3711 nchar = nCopyAnsiToWideChar (p, TEXT(DLG_FONT_NAME));
3712 }
3713 p += nchar;
3714 }
3715
3716 /*
3717 * Loop over all the items in the menu.
3718 * But skip over the tearbar.
3719 */
3720 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
3721 menu = menu->children->next;
3722 else
3723 menu = menu->children;
3724 for ( ; menu != NULL; menu = menu->next)
3725 {
3726 if (menu->modes == 0) /* this menu has just been deleted */
3727 continue;
3728 if (menu_is_separator(menu->dname))
3729 {
3730 sepPadding += 3;
3731 continue;
3732 }
3733
3734 /* Check if there still is plenty of room in the template. Make it
3735 * larger when needed. */
3736 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
3737 {
3738 WORD *newp;
3739
3740 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
3741 if (newp != NULL)
3742 {
3743 template_len += 4096;
3744 mch_memmove(newp, pdlgtemplate,
3745 (char *)p - (char *)pdlgtemplate);
3746 p = newp + (p - pdlgtemplate);
3747 pnumitems = newp + (pnumitems - pdlgtemplate);
3748 ptrueheight = newp + (ptrueheight - pdlgtemplate);
3749 LocalFree(LocalHandle(pdlgtemplate));
3750 pdlgtemplate = newp;
3751 }
3752 }
3753
3754 /* Figure out minimal length of this menu label. Use "name" for the
3755 * actual text, "dname" for estimating the displayed size. "name"
3756 * has "&a" for mnemonic and includes the accelerator. */
3757 len = nameLen = (int)STRLEN(menu->name);
3758 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
3759 (int)STRLEN(menu->dname))) / spaceWidth;
3760 len += padding0;
3761
3762 if (menu->actext != NULL)
3763 {
3764 acLen = (int)STRLEN(menu->actext);
3765 len += acLen;
3766 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
3767 }
3768 else
3769 textWidth = 0;
3770 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
3771 len += padding1;
3772
3773 if (menu->children == NULL)
3774 {
3775 padding2 = submenuWidth / spaceWidth;
3776 len += padding2;
3777 menuID = (WORD)(menu->id);
3778 }
3779 else
3780 {
3781 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
3782 menuID = (WORD)((DWORD)(menu->submenu_id) | (DWORD)0x8000);
3783 }
3784
3785 /* Allocate menu label and fill it in */
3786 text = label = alloc((unsigned)len + 1);
3787 if (label == NULL)
3788 break;
3789
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003790 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 text = vim_strchr(text, TAB); /* stop at TAB before actext */
3792 if (text == NULL)
3793 text = label + nameLen; /* no actext, use whole name */
3794 while (padding0-- > 0)
3795 *text++ = ' ';
3796 if (menu->actext != NULL)
3797 {
3798 STRNCPY(text, menu->actext, acLen);
3799 text += acLen;
3800 }
3801 while (padding1-- > 0)
3802 *text++ = ' ';
3803 if (menu->children != NULL)
3804 {
3805 STRCPY(text, TEAROFF_SUBMENU_LABEL);
3806 text += STRLEN(TEAROFF_SUBMENU_LABEL);
3807 }
3808 else
3809 {
3810 while (padding2-- > 0)
3811 *text++ = ' ';
3812 }
3813 *text = NUL;
3814
3815 /*
3816 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
3817 * W95/NT4 it makes the tear-off look more like a menu.
3818 */
3819 p = add_dialog_element(p,
3820 BS_PUSHBUTTON|BS_LEFT,
3821 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
3822 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
3823 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
3824 (WORD)12,
3825 menuID, (WORD)0x0080, label);
3826 vim_free(label);
3827 (*pnumitems)++;
3828 }
3829
3830 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
3831
3832
3833 /* show modelessly */
3834 the_menu->tearoff_handle = CreateDialogIndirect(
3835 s_hinst,
3836 (LPDLGTEMPLATE)pdlgtemplate,
3837 s_hwnd,
3838 (DLGPROC)tearoff_callback);
3839
3840 LocalFree(LocalHandle(pdlgtemplate));
3841 SelectFont(hdc, oldFont);
3842 DeleteObject(font);
3843 ReleaseDC(hwnd, hdc);
3844
3845 /*
3846 * Reassert ourselves as the active window. This is so that after creating
3847 * a tearoff, the user doesn't have to click with the mouse just to start
3848 * typing again!
3849 */
3850 (void)SetActiveWindow(s_hwnd);
3851
3852 /* make sure the right buttons are enabled */
3853 force_menu_update = TRUE;
3854}
3855#endif
3856
3857#if defined(FEAT_TOOLBAR) || defined(PROTO)
3858#include "gui_w32_rc.h"
3859
3860/* This not defined in older SDKs */
3861# ifndef TBSTYLE_FLAT
3862# define TBSTYLE_FLAT 0x0800
3863# endif
3864
3865/*
3866 * Create the toolbar, initially unpopulated.
3867 * (just like the menu, there are no defaults, it's all
3868 * set up through menu.vim)
3869 */
3870 static void
3871initialise_toolbar(void)
3872{
3873 InitCommonControls();
3874 s_toolbarhwnd = CreateToolbarEx(
3875 s_hwnd,
3876 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
3877 4000, //any old big number
3878 31, //number of images in inital bitmap
3879 s_hinst,
3880 IDR_TOOLBAR1, // id of initial bitmap
3881 NULL,
3882 0, // initial number of buttons
3883 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
3884 TOOLBAR_BUTTON_HEIGHT,
3885 TOOLBAR_BUTTON_WIDTH,
3886 TOOLBAR_BUTTON_HEIGHT,
3887 sizeof(TBBUTTON)
3888 );
3889
3890 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
3891}
3892
3893 static int
3894get_toolbar_bitmap(vimmenu_T *menu)
3895{
3896 int i = -1;
3897
3898 /*
3899 * Check user bitmaps first, unless builtin is specified.
3900 */
3901 if (!is_winnt_3() && !menu->icon_builtin)
3902 {
3903 char_u fname[MAXPATHL];
3904 HANDLE hbitmap = NULL;
3905
3906 if (menu->iconfile != NULL)
3907 {
3908 gui_find_iconfile(menu->iconfile, fname, "bmp");
3909 hbitmap = LoadImage(
3910 NULL,
3911 fname,
3912 IMAGE_BITMAP,
3913 TOOLBAR_BUTTON_WIDTH,
3914 TOOLBAR_BUTTON_HEIGHT,
3915 LR_LOADFROMFILE |
3916 LR_LOADMAP3DCOLORS
3917 );
3918 }
3919
3920 /*
3921 * If the LoadImage call failed, or the "icon=" file
3922 * didn't exist or wasn't specified, try the menu name
3923 */
3924 if (hbitmap == NULL
3925 && (gui_find_bitmap(menu->name, fname, "bmp") == OK))
3926 hbitmap = LoadImage(
3927 NULL,
3928 fname,
3929 IMAGE_BITMAP,
3930 TOOLBAR_BUTTON_WIDTH,
3931 TOOLBAR_BUTTON_HEIGHT,
3932 LR_LOADFROMFILE |
3933 LR_LOADMAP3DCOLORS
3934 );
3935
3936 if (hbitmap != NULL)
3937 {
3938 TBADDBITMAP tbAddBitmap;
3939
3940 tbAddBitmap.hInst = NULL;
3941 tbAddBitmap.nID = (UINT)hbitmap;
3942
3943 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
3944 (WPARAM)1, (LPARAM)&tbAddBitmap);
3945 /* i will be set to -1 if it fails */
3946 }
3947 }
3948 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
3949 i = menu->iconidx;
3950
3951 return i;
3952}
3953#endif
3954
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003955#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3956 static void
3957initialise_tabline(void)
3958{
3959 InitCommonControls();
3960
3961 s_tabhwnd = CreateWindow(WC_TABCONTROL, "", WS_CHILD|TCS_FOCUSNEVER,
3962 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
3963 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
3964}
3965#endif
3966
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
3968/*
3969 * Make the GUI window come to the foreground.
3970 */
3971 void
3972gui_mch_set_foreground(void)
3973{
3974 if (IsIconic(s_hwnd))
3975 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
3976 SetForegroundWindow(s_hwnd);
3977}
3978#endif
3979
3980#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
3981 static void
3982dyn_imm_load(void)
3983{
3984 hLibImm = LoadLibrary("imm32.dll");
3985 if (hLibImm == NULL)
3986 return;
3987
3988 pImmGetCompositionStringA
3989 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
3990 pImmGetCompositionStringW
3991 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
3992 pImmGetContext
3993 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
3994 pImmAssociateContext
3995 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
3996 pImmReleaseContext
3997 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
3998 pImmGetOpenStatus
3999 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
4000 pImmSetOpenStatus
4001 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
4002 pImmGetCompositionFont
4003 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
4004 pImmSetCompositionFont
4005 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
4006 pImmSetCompositionWindow
4007 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
4008 pImmGetConversionStatus
4009 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00004010 pImmSetConversionStatus
4011 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
4013 if ( pImmGetCompositionStringA == NULL
4014 || pImmGetCompositionStringW == NULL
4015 || pImmGetContext == NULL
4016 || pImmAssociateContext == NULL
4017 || pImmReleaseContext == NULL
4018 || pImmGetOpenStatus == NULL
4019 || pImmSetOpenStatus == NULL
4020 || pImmGetCompositionFont == NULL
4021 || pImmSetCompositionFont == NULL
4022 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00004023 || pImmGetConversionStatus == NULL
4024 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 {
4026 FreeLibrary(hLibImm);
4027 hLibImm = NULL;
4028 pImmGetContext = NULL;
4029 return;
4030 }
4031
4032 return;
4033}
4034
4035# if 0 /* not used */
4036 int
4037dyn_imm_unload(void)
4038{
4039 if (!hLibImm)
4040 return FALSE;
4041 FreeLibrary(hLibImm);
4042 hLibImm = NULL;
4043 return TRUE;
4044}
4045# endif
4046
4047#endif
4048
4049#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
4050
4051# ifdef FEAT_XPM_W32
4052# define IMAGE_XPM 100
4053# endif
4054
4055typedef struct _signicon_t
4056{
4057 HANDLE hImage;
4058 UINT uType;
4059#ifdef FEAT_XPM_W32
4060 HANDLE hShape; /* Mask bitmap handle */
4061#endif
4062} signicon_t;
4063
4064 void
4065gui_mch_drawsign(row, col, typenr)
4066 int row;
4067 int col;
4068 int typenr;
4069{
4070 signicon_t *sign;
4071 int x, y, w, h;
4072
4073 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
4074 return;
4075
4076 x = TEXT_X(col);
4077 y = TEXT_Y(row);
4078 w = gui.char_width * 2;
4079 h = gui.char_height;
4080 switch (sign->uType)
4081 {
4082 case IMAGE_BITMAP:
4083 {
4084 HDC hdcMem;
4085 HBITMAP hbmpOld;
4086
4087 hdcMem = CreateCompatibleDC(s_hdc);
4088 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
4089 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
4090 SelectObject(hdcMem, hbmpOld);
4091 DeleteDC(hdcMem);
4092 }
4093 break;
4094 case IMAGE_ICON:
4095 case IMAGE_CURSOR:
4096 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
4097 break;
4098#ifdef FEAT_XPM_W32
4099 case IMAGE_XPM:
4100 {
4101 HDC hdcMem;
4102 HBITMAP hbmpOld;
4103
4104 hdcMem = CreateCompatibleDC(s_hdc);
4105 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
4106 /* Make hole */
4107 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
4108
4109 SelectObject(hdcMem, sign->hImage);
4110 /* Paint sign */
4111 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
4112 SelectObject(hdcMem, hbmpOld);
4113 DeleteDC(hdcMem);
4114 }
4115 break;
4116#endif
4117 }
4118}
4119
4120 static void
4121close_signicon_image(signicon_t *sign)
4122{
4123 if (sign)
4124 switch (sign->uType)
4125 {
4126 case IMAGE_BITMAP:
4127 DeleteObject((HGDIOBJ)sign->hImage);
4128 break;
4129 case IMAGE_CURSOR:
4130 DestroyCursor((HCURSOR)sign->hImage);
4131 break;
4132 case IMAGE_ICON:
4133 DestroyIcon((HICON)sign->hImage);
4134 break;
4135#ifdef FEAT_XPM_W32
4136 case IMAGE_XPM:
4137 DeleteObject((HBITMAP)sign->hImage);
4138 DeleteObject((HBITMAP)sign->hShape);
4139 break;
4140#endif
4141 }
4142}
4143
4144 void *
4145gui_mch_register_sign(signfile)
4146 char_u *signfile;
4147{
4148 signicon_t sign, *psign;
4149 char_u *ext;
4150
4151 if (is_winnt_3())
4152 {
4153 EMSG(_(e_signdata));
4154 return NULL;
4155 }
4156
4157 sign.hImage = NULL;
4158 ext = signfile + STRLEN(signfile) - 4; /* get extention */
4159 if (ext > signfile)
4160 {
4161 int do_load = 1;
4162
4163 if (!STRICMP(ext, ".bmp"))
4164 sign.uType = IMAGE_BITMAP;
4165 else if (!STRICMP(ext, ".ico"))
4166 sign.uType = IMAGE_ICON;
4167 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
4168 sign.uType = IMAGE_CURSOR;
4169 else
4170 do_load = 0;
4171
4172 if (do_load)
4173 sign.hImage = (HANDLE)LoadImage(NULL, signfile, sign.uType,
4174 gui.char_width * 2, gui.char_height,
4175 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
4176#ifdef FEAT_XPM_W32
4177 if (!STRICMP(ext, ".xpm"))
4178 {
4179 sign.uType = IMAGE_XPM;
4180 LoadXpmImage(signfile, (HBITMAP *)&sign.hImage, (HBITMAP *)&sign.hShape);
4181 }
4182#endif
4183 }
4184
4185 psign = NULL;
4186 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
4187 != NULL)
4188 *psign = sign;
4189
4190 if (!psign)
4191 {
4192 if (sign.hImage)
4193 close_signicon_image(&sign);
4194 EMSG(_(e_signdata));
4195 }
4196 return (void *)psign;
4197
4198}
4199
4200 void
4201gui_mch_destroy_sign(sign)
4202 void *sign;
4203{
4204 if (sign)
4205 {
4206 close_signicon_image((signicon_t *)sign);
4207 vim_free(sign);
4208 }
4209}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211
4212#if defined(FEAT_BEVAL) || defined(PROTO)
4213
4214/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004215 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 *
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004217 * The only reused thing is gui_beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 * from gui_beval.c (note it uses x and y of the BalloonEval struct
4219 * to get current mouse position).
4220 *
4221 * Trying to use as more Windows services as possible, and as less
4222 * IE version as possible :)).
4223 *
4224 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
4225 * BalloonEval struct.
4226 * 2) Enable/Disable simply create/kill BalloonEval Timer
4227 * 3) When there was enough inactivity, timer procedure posts
4228 * async request to debugger
4229 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
4230 * and performs some actions to show it ASAP
4231 * 5) WM_NOTOFY:TTN_POP destroys created tooltip
4232 */
4233
Bram Moolenaar45360022005-07-21 21:08:21 +00004234/*
4235 * determine whether installed Common Controls support multiline tooltips
4236 * (i.e. their version is >= 4.70
4237 */
4238 int
4239multiline_balloon_available(void)
4240{
4241 HINSTANCE hDll;
4242 static char comctl_dll[] = "comctl32.dll";
4243 static int multiline_tip = MAYBE;
4244
4245 if (multiline_tip != MAYBE)
4246 return multiline_tip;
4247
4248 hDll = GetModuleHandle(comctl_dll);
4249 if (hDll != NULL)
4250 {
4251 DLLGETVERSIONPROC pGetVer;
4252 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
4253
4254 if (pGetVer != NULL)
4255 {
4256 DLLVERSIONINFO dvi;
4257 HRESULT hr;
4258
4259 ZeroMemory(&dvi, sizeof(dvi));
4260 dvi.cbSize = sizeof(dvi);
4261
4262 hr = (*pGetVer)(&dvi);
4263
4264 if (SUCCEEDED(hr)
4265 && (dvi.dwMajorVersion > 4
4266 || (dvi.dwMajorVersion == 4 && dvi.dwMinorVersion >= 70)))
4267 {
4268 multiline_tip = TRUE;
4269 return multiline_tip;
4270 }
4271 }
4272 else
4273 {
4274 /* there is chance we have ancient CommCtl 4.70
4275 which doesn't export DllGetVersion */
4276 DWORD dwHandle = 0;
4277 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
4278 if (len > 0)
4279 {
4280 VS_FIXEDFILEINFO *ver;
4281 UINT vlen = 0;
4282 void *data = alloc(len);
4283
4284 if (data != NULL
4285 && GetFileVersionInfo(comctl_dll, 0, len, data)
4286 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
4287 && vlen
4288 && HIWORD(ver->dwFileVersionMS) > 4
4289 || (HIWORD(ver->dwFileVersionMS) == 4
4290 && LOWORD(ver->dwFileVersionMS) >= 70))
4291 {
4292 vim_free(data);
4293 multiline_tip = TRUE;
4294 return multiline_tip;
4295 }
4296 vim_free(data);
4297 }
4298 }
4299 }
4300 multiline_tip = FALSE;
4301 return multiline_tip;
4302}
4303
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 static void
4305make_tooltip(beval, text, pt)
4306 BalloonEval *beval;
4307 char *text;
4308 POINT pt;
4309{
Bram Moolenaar45360022005-07-21 21:08:21 +00004310 TOOLINFO *pti;
4311 int ToolInfoSize;
4312
4313 if (multiline_balloon_available() == TRUE)
4314 ToolInfoSize = sizeof(TOOLINFO_NEW);
4315 else
4316 ToolInfoSize = sizeof(TOOLINFO);
4317
4318 pti = (TOOLINFO *)alloc(ToolInfoSize);
4319 if (pti == NULL)
4320 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321
4322 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
4323 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
4324 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4325 beval->target, NULL, s_hinst, NULL);
4326
4327 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
4328 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
4329
Bram Moolenaar45360022005-07-21 21:08:21 +00004330 pti->cbSize = ToolInfoSize;
4331 pti->uFlags = TTF_SUBCLASS;
4332 pti->hwnd = beval->target;
4333 pti->hinst = 0; /* Don't use string resources */
4334 pti->uId = ID_BEVAL_TOOLTIP;
4335
4336 if (multiline_balloon_available() == TRUE)
4337 {
4338 RECT rect;
4339 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
4340 pti->lpszText = LPSTR_TEXTCALLBACK;
4341 ptin->lParam = (LPARAM)text;
4342 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
4343 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
4344 (LPARAM)rect.right);
4345 }
4346 else
4347 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348
4349 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00004350 pti->rect.left = pt.x - 3;
4351 pti->rect.top = pt.y - 3;
4352 pti->rect.right = pt.x + 3;
4353 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
Bram Moolenaar45360022005-07-21 21:08:21 +00004355 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 /* Make tooltip appear sooner */
4357 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
4358 /*
4359 * HACK: force tooltip to appear, because it'll not appear until
4360 * first mouse move. D*mn M$
4361 */
4362 mouse_event(MOUSEEVENTF_MOVE, 1, 1, 0, 0);
4363 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00004364 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365}
4366
4367 static void
4368delete_tooltip(beval)
4369 BalloonEval *beval;
4370{
4371 DestroyWindow(beval->balloon);
4372}
4373
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004374/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 static VOID CALLBACK
4376BevalTimerProc(hwnd, uMsg, idEvent, dwTime)
4377 HWND hwnd;
4378 UINT uMsg;
4379 UINT idEvent;
4380 DWORD dwTime;
4381{
4382 POINT pt;
4383 RECT rect;
4384
4385 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
4386 return;
4387
4388 GetCursorPos(&pt);
4389 if (WindowFromPoint(pt) != s_textArea)
4390 return;
4391
4392 ScreenToClient(s_textArea, &pt);
4393 GetClientRect(s_textArea, &rect);
4394 if (!PtInRect(&rect, pt))
4395 return;
4396
4397 if (LastActivity > 0
4398 && (dwTime - LastActivity) >= (DWORD)p_bdlay
4399 && (cur_beval->showState != ShS_PENDING
4400 || abs(cur_beval->x - pt.x) > 3
4401 || abs(cur_beval->y - pt.y) > 3))
4402 {
4403 /* Pointer resting in one place long enough, it's time to show
4404 * the tooltip. */
4405 cur_beval->showState = ShS_PENDING;
4406 cur_beval->x = pt.x;
4407 cur_beval->y = pt.y;
4408
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004409 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410
4411 if (cur_beval->msgCB != NULL)
4412 (*cur_beval->msgCB)(cur_beval, 0);
4413 }
4414}
4415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004416/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 void
4418gui_mch_disable_beval_area(beval)
4419 BalloonEval *beval;
4420{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004421 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004423 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424}
4425
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004426/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 void
4428gui_mch_enable_beval_area(beval)
4429 BalloonEval *beval;
4430{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004431 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 if (beval == NULL)
4433 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004434 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004435 BevalTimerId = SetTimer(s_textArea, 0, p_bdlay / 2,
4436 (TIMERPROC)BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004437 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438}
4439
4440 void
4441gui_mch_post_balloon(beval, mesg)
4442 BalloonEval *beval;
4443 char_u *mesg;
4444{
4445 POINT pt;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004446 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 if (beval->showState == ShS_SHOWING)
4448 return;
4449 GetCursorPos(&pt);
4450 ScreenToClient(s_textArea, &pt);
4451
4452 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
4453 /* cursor is still here */
4454 {
4455 gui_mch_disable_beval_area(cur_beval);
4456 beval->showState = ShS_SHOWING;
4457 make_tooltip(beval, mesg, pt);
4458 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004459 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460}
4461
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004462/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 BalloonEval *
4464gui_mch_create_beval_area(target, mesg, mesgCB, clientData)
4465 void *target; /* ignored, always use s_textArea */
4466 char_u *mesg;
4467 void (*mesgCB)__ARGS((BalloonEval *, int));
4468 void *clientData;
4469{
4470 /* partially stolen from gui_beval.c */
4471 BalloonEval *beval;
4472
4473 if (mesg != NULL && mesgCB != NULL)
4474 {
4475 EMSG(_("E232: Cannot create BalloonEval with both message and callback"));
4476 return NULL;
4477 }
4478
4479 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
4480 if (beval != NULL)
4481 {
4482 beval->target = s_textArea;
4483 beval->balloon = NULL;
4484
4485 beval->showState = ShS_NEUTRAL;
4486 beval->x = 0;
4487 beval->y = 0;
4488 beval->msg = mesg;
4489 beval->msgCB = mesgCB;
4490 beval->clientData = clientData;
4491
4492 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 cur_beval = beval;
4494
4495 if (p_beval)
4496 gui_mch_enable_beval_area(beval);
4497
4498 }
4499 return beval;
4500}
4501
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004502/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 static void
4504Handle_WM_Notify(hwnd, pnmh)
4505 HWND hwnd;
4506 LPNMHDR pnmh;
4507{
4508 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
4509 return;
4510
4511 if (cur_beval != NULL)
4512 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004513 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004515 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004516 // TRACE0("TTN_SHOW {{{");
4517 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00004518 break;
4519 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004520 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 delete_tooltip(cur_beval);
4522 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004523 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524
4525 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00004526 break;
4527 case TTN_GETDISPINFO:
4528 {
4529 /* if you get there then we have new common controls */
4530 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
4531 info->lpszText = (LPSTR)info->lParam;
4532 info->uFlags |= TTF_DI_SETITEM;
4533 }
4534 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 }
4536 }
4537}
4538
4539 static void
4540TrackUserActivity(UINT uMsg)
4541{
4542 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
4543 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
4544 LastActivity = GetTickCount();
4545}
4546
4547 void
4548gui_mch_destroy_beval_area(beval)
4549 BalloonEval *beval;
4550{
4551 vim_free(beval);
4552}
4553#endif /* FEAT_BEVAL */
4554
4555#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
4556/*
4557 * We have multiple signs to draw at the same location. Draw the
4558 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
4559 */
4560 void
4561netbeans_draw_multisign_indicator(int row)
4562{
4563 int i;
4564 int y;
4565 int x;
4566
4567 x = 0;
4568 y = TEXT_Y(row);
4569
4570 for (i = 0; i < gui.char_height - 3; i++)
4571 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
4572
4573 SetPixel(s_hdc, x+0, y, gui.currFgColor);
4574 SetPixel(s_hdc, x+2, y, gui.currFgColor);
4575 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
4576 SetPixel(s_hdc, x+1, y, gui.currFgColor);
4577 SetPixel(s_hdc, x+2, y, gui.currFgColor);
4578 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
4579 SetPixel(s_hdc, x+2, y, gui.currFgColor);
4580}
4581#endif