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