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