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