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