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