blob: 5649aeb18c5873d94882bb0e3359724fb7d1f9c7 [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 * gui_w48.c: This file is included in gui_w16.c and gui_w32.c.
12 *
13 * GUI support for Microsoft Windows (Win16 + Win32 = Win48 :-)
14 *
15 * The combined efforts of:
16 * George V. Reilly <george@reilly.org>
17 * Robert Webb
18 * Vince Negri
19 * ...and contributions from many others
20 *
21 */
22
23#include "vim.h"
24#include "version.h" /* used by dialog box routine for default title */
25#ifdef DEBUG
26# include <tchar.h>
27#endif
28#ifndef __MINGW32__
29# include <shellapi.h>
30#endif
31#if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL)
32# include <commctrl.h>
33#endif
34#ifdef WIN16
35# include <commdlg.h>
36# include <shellapi.h>
37# ifdef WIN16_3DLOOK
38# include <ctl3d.h>
39# endif
40#endif
41#include <windowsx.h>
42
43#ifdef GLOBAL_IME
44# include "glbl_ime.h"
45#endif
46
47#ifdef FEAT_MENU
48# define MENUHINTS /* show menu hints in command line */
49#endif
50
51/* Some parameters for dialog boxes. All in pixels. */
52#define DLG_PADDING_X 10
53#define DLG_PADDING_Y 10
54#define DLG_OLD_STYLE_PADDING_X 5
55#define DLG_OLD_STYLE_PADDING_Y 5
56#define DLG_VERT_PADDING_X 4 /* For vertical buttons */
57#define DLG_VERT_PADDING_Y 4
58#define DLG_ICON_WIDTH 34
59#define DLG_ICON_HEIGHT 34
60#define DLG_MIN_WIDTH 150
61#define DLG_FONT_NAME "MS Sans Serif"
62#define DLG_FONT_POINT_SIZE 8
63#define DLG_MIN_MAX_WIDTH 400
Bram Moolenaar748bf032005-02-02 23:04:36 +000064#define DLG_MIN_MAX_HEIGHT 400
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
66#define DLG_NONBUTTON_CONTROL 5000 /* First ID of non-button controls */
67
68#ifndef WM_XBUTTONDOWN /* For Win2K / winME ONLY */
69# define WM_XBUTTONDOWN 0x020B
70# define WM_XBUTTONUP 0x020C
71# define WM_XBUTTONDBLCLK 0x020D
72# define MK_XBUTTON1 0x0020
73# define MK_XBUTTON2 0x0040
74#endif
75
76#ifdef PROTO
77/*
78 * Define a few things for generating prototypes. This is just to avoid
79 * syntax errors, the defines do not need to be correct.
80 */
81# define APIENTRY
82# define CALLBACK
83# define CONST
84# define FAR
85# define NEAR
86# define _cdecl
87typedef int BOOL;
88typedef int BYTE;
89typedef int DWORD;
90typedef int WCHAR;
91typedef int ENUMLOGFONT;
92typedef int FINDREPLACE;
93typedef int HANDLE;
94typedef int HBITMAP;
95typedef int HBRUSH;
96typedef int HDROP;
97typedef int INT;
98typedef int LOGFONT[];
99typedef int LPARAM;
100typedef int LPCREATESTRUCT;
101typedef int LPCSTR;
102typedef int LPCTSTR;
103typedef int LPRECT;
104typedef int LPSTR;
105typedef int LPWINDOWPOS;
106typedef int LPWORD;
107typedef int LRESULT;
108# undef MSG
109typedef int MSG;
110typedef int NEWTEXTMETRIC;
111typedef int OSVERSIONINFO;
112typedef int PWORD;
113typedef int RECT;
114typedef int UINT;
115typedef int WORD;
116typedef int WPARAM;
117typedef int POINT;
118typedef void *HINSTANCE;
119typedef void *HMENU;
120typedef void *HWND;
121typedef void *HDC;
122typedef void VOID;
123typedef int LPNMHDR;
124typedef int LONG;
125#endif
126
127#ifndef GET_X_LPARAM
128# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
129#endif
130
131static void _OnPaint( HWND hwnd);
132static void clear_rect(RECT *rcp);
133static int gui_mswin_get_menu_height(int fix_window);
134
135static WORD s_dlgfntheight; /* height of the dialog font */
136static WORD s_dlgfntwidth; /* width of the dialog font */
137
138#ifdef FEAT_MENU
139static HMENU s_menuBar = NULL;
140#endif
141#ifdef FEAT_TEAROFF
142static void rebuild_tearoff(vimmenu_T *menu);
143static HBITMAP s_htearbitmap; /* bitmap used to indicate tearoff */
144#endif
145
146/* Flag that is set while processing a message that must not be interupted by
147 * processing another message. */
148static int s_busy_processing = FALSE;
149
150static int destroying = FALSE; /* call DestroyWindow() ourselves */
151
152#ifdef MSWIN_FIND_REPLACE
153static UINT s_findrep_msg = 0; /* set in gui_w[16/32].c */
154static FINDREPLACE s_findrep_struct;
155static HWND s_findrep_hwnd = NULL;
156static int s_findrep_is_find; /* TRUE for find dialog, FALSE
157 for find/replace dialog */
158#endif
159
160static HINSTANCE s_hinst = NULL;
161#if !defined(FEAT_SNIFF) && !defined(FEAT_GUI)
162static
163#endif
164HWND s_hwnd = NULL;
165static HDC s_hdc = NULL;
166static HBRUSH s_brush = NULL;
167
168#ifdef FEAT_TOOLBAR
169static HWND s_toolbarhwnd = NULL;
170#endif
171
172static WPARAM s_wParam = 0;
173static LPARAM s_lParam = 0;
174
175static HWND s_textArea = NULL;
176static UINT s_uMsg = 0;
177
178static char_u *s_textfield; /* Used by dialogs to pass back strings */
179
180static int s_need_activate = FALSE;
181
182/* This variable is set when waiting for an event, which is the only moment
183 * scrollbar dragging can be done directly. It's not allowed while commands
184 * are executed, because it may move the cursor and that may cause unexpected
185 * problems (e.g., while ":s" is working).
186 */
187static int allow_scrollbar = FALSE;
188
189#ifdef GLOBAL_IME
190# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
191#else
192# define MyTranslateMessage(x) TranslateMessage(x)
193#endif
194
195#if (defined(WIN3264) && defined(FEAT_MBYTE)) || defined(GLOBAL_IME)
196 /* use of WindowProc depends on wide_WindowProc */
197# define MyWindowProc vim_WindowProc
198#else
199 /* use ordinary WindowProc */
200# define MyWindowProc DefWindowProc
201#endif
202
203extern int current_font_height; /* this is in os_mswin.c */
204
205static struct
206{
207 UINT key_sym;
208 char_u vim_code0;
209 char_u vim_code1;
210} special_keys[] =
211{
212 {VK_UP, 'k', 'u'},
213 {VK_DOWN, 'k', 'd'},
214 {VK_LEFT, 'k', 'l'},
215 {VK_RIGHT, 'k', 'r'},
216
217 {VK_F1, 'k', '1'},
218 {VK_F2, 'k', '2'},
219 {VK_F3, 'k', '3'},
220 {VK_F4, 'k', '4'},
221 {VK_F5, 'k', '5'},
222 {VK_F6, 'k', '6'},
223 {VK_F7, 'k', '7'},
224 {VK_F8, 'k', '8'},
225 {VK_F9, 'k', '9'},
226 {VK_F10, 'k', ';'},
227
228 {VK_F11, 'F', '1'},
229 {VK_F12, 'F', '2'},
230 {VK_F13, 'F', '3'},
231 {VK_F14, 'F', '4'},
232 {VK_F15, 'F', '5'},
233 {VK_F16, 'F', '6'},
234 {VK_F17, 'F', '7'},
235 {VK_F18, 'F', '8'},
236 {VK_F19, 'F', '9'},
237 {VK_F20, 'F', 'A'},
238
239 {VK_F21, 'F', 'B'},
240#ifdef FEAT_NETBEANS_INTG
241 {VK_PAUSE, 'F', 'B'}, /* Pause == F21 (see gui_gtk_x11.c) */
242#endif
243 {VK_F22, 'F', 'C'},
244 {VK_F23, 'F', 'D'},
245 {VK_F24, 'F', 'E'}, /* winuser.h defines up to F24 */
246
247 {VK_HELP, '%', '1'},
248 {VK_BACK, 'k', 'b'},
249 {VK_INSERT, 'k', 'I'},
250 {VK_DELETE, 'k', 'D'},
251 {VK_HOME, 'k', 'h'},
252 {VK_END, '@', '7'},
253 {VK_PRIOR, 'k', 'P'},
254 {VK_NEXT, 'k', 'N'},
255 {VK_PRINT, '%', '9'},
256 {VK_ADD, 'K', '6'},
257 {VK_SUBTRACT, 'K', '7'},
258 {VK_DIVIDE, 'K', '8'},
259 {VK_MULTIPLY, 'K', '9'},
260 {VK_SEPARATOR, 'K', 'A'}, /* Keypad Enter */
261 {VK_DECIMAL, 'K', 'B'},
262
263 {VK_NUMPAD0, 'K', 'C'},
264 {VK_NUMPAD1, 'K', 'D'},
265 {VK_NUMPAD2, 'K', 'E'},
266 {VK_NUMPAD3, 'K', 'F'},
267 {VK_NUMPAD4, 'K', 'G'},
268 {VK_NUMPAD5, 'K', 'H'},
269 {VK_NUMPAD6, 'K', 'I'},
270 {VK_NUMPAD7, 'K', 'J'},
271 {VK_NUMPAD8, 'K', 'K'},
272 {VK_NUMPAD9, 'K', 'L'},
273
274 /* Keys that we want to be able to use any modifier with: */
275 {VK_SPACE, ' ', NUL},
276 {VK_TAB, TAB, NUL},
277 {VK_ESCAPE, ESC, NUL},
278 {NL, NL, NUL},
279 {CAR, CAR, NUL},
280
281 /* End of list marker: */
282 {0, 0, 0}
283};
284
285/* Local variables */
286static int s_button_pending = -1;
287static int s_x_pending;
288static int s_y_pending;
289static UINT s_kFlags_pending;
290static UINT s_wait_timer = 0; /* Timer for get char from user */
291static int s_timed_out = FALSE;
292static int dead_key = 0; /* 0 - no dead key, 1 - dead key pressed */
293
294#ifdef WIN3264
295static OSVERSIONINFO os_version; /* like it says. Init in gui_mch_init() */
296#endif
297
298#ifdef FEAT_BEVAL
299/* balloon-eval WM_NOTIFY_HANDLER */
300static void Handle_WM_Notify __ARGS((HWND hwnd, LPNMHDR pnmh));
301static void TrackUserActivity __ARGS((UINT uMsg));
302#endif
303
304/*
305 * For control IME.
306 */
307#ifdef FEAT_MBYTE
308# ifdef USE_IM_CONTROL
309static LOGFONT norm_logfont;
310# endif
311#endif
312
313#ifdef FEAT_MBYTE_IME
314static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
315#endif
316
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000317#ifdef DEBUG_PRINT_ERROR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318/*
319 * Print out the last Windows error message
320 */
321 static void
322print_windows_error(void)
323{
324 LPVOID lpMsgBuf;
325
326 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
327 NULL, GetLastError(),
328 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
329 (LPTSTR) &lpMsgBuf, 0, NULL);
330 TRACE1("Error: %s\n", lpMsgBuf);
331 LocalFree(lpMsgBuf);
332}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334
335/*
336 * Cursor blink functions.
337 *
338 * This is a simple state machine:
339 * BLINK_NONE not blinking at all
340 * BLINK_OFF blinking, cursor is not shown
341 * BLINK_ON blinking, cursor is shown
342 */
343
344#define BLINK_NONE 0
345#define BLINK_OFF 1
346#define BLINK_ON 2
347
348static int blink_state = BLINK_NONE;
349static long_u blink_waittime = 700;
350static long_u blink_ontime = 400;
351static long_u blink_offtime = 250;
352static UINT blink_timer = 0;
353
354 void
355gui_mch_set_blinking(long wait, long on, long off)
356{
357 blink_waittime = wait;
358 blink_ontime = on;
359 blink_offtime = off;
360}
361
362/* ARGSUSED */
363 static VOID CALLBACK
364_OnBlinkTimer(
365 HWND hwnd,
366 UINT uMsg,
367 UINT idEvent,
368 DWORD dwTime)
369{
370 MSG msg;
371
372 /*
373 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
374 */
375
376 KillTimer(NULL, idEvent);
377
378 /* Eat spurious WM_TIMER messages */
379 while (PeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
380 ;
381
382 if (blink_state == BLINK_ON)
383 {
384 gui_undraw_cursor();
385 blink_state = BLINK_OFF;
386 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
387 (TIMERPROC)_OnBlinkTimer);
388 }
389 else
390 {
391 gui_update_cursor(TRUE, FALSE);
392 blink_state = BLINK_ON;
393 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
394 (TIMERPROC)_OnBlinkTimer);
395 }
396}
397
398 static void
399gui_mswin_rm_blink_timer(void)
400{
401 MSG msg;
402
403 if (blink_timer != 0)
404 {
405 KillTimer(NULL, blink_timer);
406 /* Eat spurious WM_TIMER messages */
407 while (PeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
408 ;
409 blink_timer = 0;
410 }
411}
412
413/*
414 * Stop the cursor blinking. Show the cursor if it wasn't shown.
415 */
416 void
417gui_mch_stop_blink(void)
418{
419 gui_mswin_rm_blink_timer();
420 if (blink_state == BLINK_OFF)
421 gui_update_cursor(TRUE, FALSE);
422 blink_state = BLINK_NONE;
423}
424
425/*
426 * Start the cursor blinking. If it was already blinking, this restarts the
427 * waiting time and shows the cursor.
428 */
429 void
430gui_mch_start_blink(void)
431{
432 gui_mswin_rm_blink_timer();
433
434 /* Only switch blinking on if none of the times is zero */
435 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
436 {
437 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
438 (TIMERPROC)_OnBlinkTimer);
439 blink_state = BLINK_ON;
440 gui_update_cursor(TRUE, FALSE);
441 }
442}
443
444/*
445 * Call-back routines.
446 */
447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000448/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 static VOID CALLBACK
450_OnTimer(
451 HWND hwnd,
452 UINT uMsg,
453 UINT idEvent,
454 DWORD dwTime)
455{
456 MSG msg;
457
458 /*
459 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
460 */
461 KillTimer(NULL, idEvent);
462 s_timed_out = TRUE;
463
464 /* Eat spurious WM_TIMER messages */
465 while (PeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
466 ;
467 if (idEvent == s_wait_timer)
468 s_wait_timer = 0;
469}
470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000471/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 static void
473_OnDeadChar(
474 HWND hwnd,
475 UINT ch,
476 int cRepeat)
477{
478 dead_key = 1;
479}
480
481/*
482 * Convert Unicode character "ch" to bytes in "string[slen]".
483 * Return the length.
484 */
485 static int
486char_to_string(int ch, char_u *string, int slen)
487{
488 int len;
489 int i;
490#ifdef FEAT_MBYTE
491 WCHAR wstring[2];
492 char_u *ws = NULL;;
493
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000494 if (os_version.dwPlatformId != VER_PLATFORM_WIN32_NT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000496 /* On Windows 95/98 we apparently get the character in the active
497 * codepage, not in UCS-2. If conversion is needed convert it to
498 * UCS-2 first. */
499 if ((int)GetACP() == enc_codepage)
500 len = 0; /* no conversion required */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 else
502 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000503 string[0] = ch;
504 len = MultiByteToWideChar(GetACP(), 0, string, 1, wstring, 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 }
506 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000507 else
508 {
509 wstring[0] = ch;
510 len = 1;
511 }
512
513 if (len > 0)
514 {
515 /* "ch" is a UTF-16 character. Convert it to a string of bytes. When
516 * "enc_codepage" is non-zero use the standard Win32 function,
517 * otherwise use our own conversion function (e.g., for UTF-8). */
518 if (enc_codepage > 0)
519 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
520 string, slen, 0, NULL);
521 else
522 {
523 len = 1;
524 ws = ucs2_to_enc(wstring, &len);
525 if (ws == NULL)
526 len = 0;
527 else
528 {
529 if (len > slen) /* just in case */
530 len = slen;
531 mch_memmove(string, ws, len);
532 vim_free(ws);
533 }
534 }
535 }
536
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 if (len == 0)
538#endif
539 {
540 string[0] = ch;
541 len = 1;
542 }
543
544 for (i = 0; i < len; ++i)
545 if (string[i] == CSI && len <= slen - 2)
546 {
547 /* Insert CSI as K_CSI. */
548 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
549 string[++i] = KS_EXTRA;
550 string[++i] = (int)KE_CSI;
551 len += 2;
552 }
553
554 return len;
555}
556
557/*
558 * Key hit, add it to the input buffer.
559 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000560/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 static void
562_OnChar(
563 HWND hwnd,
564 UINT ch,
565 int cRepeat)
566{
567 char_u string[40];
568 int len = 0;
569
570 len = char_to_string(ch, string, 40);
571 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
572 {
573 trash_input_buf();
574 got_int = TRUE;
575 }
576
577 add_to_input_buf(string, len);
578}
579
580/*
581 * Alt-Key hit, add it to the input buffer.
582 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000583/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 static void
585_OnSysChar(
586 HWND hwnd,
587 UINT cch,
588 int cRepeat)
589{
590 char_u string[40]; /* Enough for multibyte character */
591 int len;
592 int modifiers;
593 int ch = cch; /* special keys are negative */
594
595 /* TRACE("OnSysChar(%d, %c)\n", ch, ch); */
596
597 /* OK, we have a character key (given by ch) which was entered with the
598 * ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
599 * that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
600 * CAPSLOCK is pressed) at this point.
601 */
602 modifiers = MOD_MASK_ALT;
603 if (GetKeyState(VK_SHIFT) & 0x8000)
604 modifiers |= MOD_MASK_SHIFT;
605 if (GetKeyState(VK_CONTROL) & 0x8000)
606 modifiers |= MOD_MASK_CTRL;
607
608 ch = simplify_key(ch, &modifiers);
609 /* remove the SHIFT modifier for keys where it's already included, e.g.,
610 * '(' and '*' */
611 if (ch < 0x100 && !isalpha(ch) && isprint(ch))
612 modifiers &= ~MOD_MASK_SHIFT;
613
614 /* Interpret the ALT key as making the key META, include SHIFT, etc. */
615 ch = extract_modifiers(ch, &modifiers);
616 if (ch == CSI)
617 ch = K_CSI;
618
619 len = 0;
620 if (modifiers)
621 {
622 string[len++] = CSI;
623 string[len++] = KS_MODIFIER;
624 string[len++] = modifiers;
625 }
626
627 if (IS_SPECIAL((int)ch))
628 {
629 string[len++] = CSI;
630 string[len++] = K_SECOND((int)ch);
631 string[len++] = K_THIRD((int)ch);
632 }
633 else
634 {
635 /* Although the documentation isn't clear about it, we assume "ch" is
636 * a Unicode character. */
637 len += char_to_string(ch, string + len, 40 - len);
638 }
639
640 add_to_input_buf(string, len);
641}
642
643 static void
644_OnMouseEvent(
645 int button,
646 int x,
647 int y,
648 int repeated_click,
649 UINT keyFlags)
650{
651 int vim_modifiers = 0x0;
652
653 if (keyFlags & MK_SHIFT)
654 vim_modifiers |= MOUSE_SHIFT;
655 if (keyFlags & MK_CONTROL)
656 vim_modifiers |= MOUSE_CTRL;
657 if (GetKeyState(VK_MENU) & 0x8000)
658 vim_modifiers |= MOUSE_ALT;
659
660 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
661}
662
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000663/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 static void
665_OnMouseButtonDown(
666 HWND hwnd,
667 BOOL fDoubleClick,
668 int x,
669 int y,
670 UINT keyFlags)
671{
672 static LONG s_prevTime = 0;
673
674 LONG currentTime = GetMessageTime();
675 int button = -1;
676 int repeated_click;
677
678 /* Give main window the focus: this is so the cursor isn't hollow. */
679 (void)SetFocus(s_hwnd);
680
681 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
682 button = MOUSE_LEFT;
683 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
684 button = MOUSE_MIDDLE;
685 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
686 button = MOUSE_RIGHT;
687#ifndef WIN16 /*<VN>*/
688 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
689 {
690#ifndef GET_XBUTTON_WPARAM
691# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
692#endif
693 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
694 }
695 else if (s_uMsg == WM_CAPTURECHANGED)
696 {
697 /* on W95/NT4, somehow you get in here with an odd Msg
698 * if you press one button while holding down the other..*/
699 if (s_button_pending == MOUSE_LEFT)
700 button = MOUSE_RIGHT;
701 else
702 button = MOUSE_LEFT;
703 }
704#endif
705 if (button >= 0)
706 {
707 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
708
709 /*
710 * Holding down the left and right buttons simulates pushing the middle
711 * button.
712 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000713 if (repeated_click
714 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
715 || (button == MOUSE_RIGHT
716 && s_button_pending == MOUSE_LEFT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 {
718 /*
719 * Hmm, gui.c will ignore more than one button down at a time, so
720 * pretend we let go of it first.
721 */
722 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
723 button = MOUSE_MIDDLE;
724 repeated_click = FALSE;
725 s_button_pending = -1;
726 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
727 }
728 else if ((repeated_click)
729 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
730 {
731 if (s_button_pending > -1)
732 {
733 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
734 s_button_pending = -1;
735 }
736 /* TRACE("Button down at x %d, y %d\n", x, y); */
737 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
738 }
739 else
740 {
741 /*
742 * If this is the first press (i.e. not a multiple click) don't
743 * action immediately, but store and wait for:
744 * i) button-up
745 * ii) mouse move
746 * iii) another button press
747 * before using it.
748 * This enables us to make left+right simulate middle button,
749 * without left or right being actioned first. The side-effect is
750 * that if you click and hold the mouse without dragging, the
751 * cursor doesn't move until you release the button. In practice
752 * this is hardly a problem.
753 */
754 s_button_pending = button;
755 s_x_pending = x;
756 s_y_pending = y;
757 s_kFlags_pending = keyFlags;
758 }
759
760 s_prevTime = currentTime;
761 }
762}
763
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000764/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 static void
766_OnMouseMoveOrRelease(
767 HWND hwnd,
768 int x,
769 int y,
770 UINT keyFlags)
771{
772 int button;
773
774 if (s_button_pending > -1)
775 {
776 /* Delayed action for mouse down event */
777 _OnMouseEvent(s_button_pending, s_x_pending,
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000778 s_y_pending, FALSE, s_kFlags_pending);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 s_button_pending = -1;
780 }
781 if (s_uMsg == WM_MOUSEMOVE)
782 {
783 /*
784 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
785 * down.
786 */
787 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
788 | MK_XBUTTON1 | MK_XBUTTON2)))
789 {
790 gui_mouse_moved(x, y);
791 return;
792 }
793
794 /*
795 * While button is down, keep grabbing mouse move events when
796 * the mouse goes outside the window
797 */
798 SetCapture(s_textArea);
799 button = MOUSE_DRAG;
800 /* TRACE(" move at x %d, y %d\n", x, y); */
801 }
802 else
803 {
804 ReleaseCapture();
805 button = MOUSE_RELEASE;
806 /* TRACE(" up at x %d, y %d\n", x, y); */
807 }
808
809 _OnMouseEvent(button, x, y, FALSE, keyFlags);
810}
811
812#ifdef FEAT_MENU
813/*
814 * Find the vimmenu_T with the given id
815 */
816 static vimmenu_T *
817gui_mswin_find_menu(
818 vimmenu_T *pMenu,
819 int id)
820{
821 vimmenu_T *pChildMenu;
822
823 while (pMenu)
824 {
825 if (pMenu->id == (UINT)id)
826 break;
827 if (pMenu->children != NULL)
828 {
829 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
830 if (pChildMenu)
831 {
832 pMenu = pChildMenu;
833 break;
834 }
835 }
836 pMenu = pMenu->next;
837 }
838 return pMenu;
839}
840
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000841/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 static void
843_OnMenu(
844 HWND hwnd,
845 int id,
846 HWND hwndCtl,
847 UINT codeNotify)
848{
849 vimmenu_T *pMenu;
850
851 pMenu = gui_mswin_find_menu(root_menu, id);
852 if (pMenu)
853 gui_menu_cb(pMenu);
854}
855#endif
856
857#ifdef MSWIN_FIND_REPLACE
858/*
859 * Handle a Find/Replace window message.
860 */
861 static void
862_OnFindRepl(void)
863{
864 int flags = 0;
865 int down;
866
867 if (s_findrep_struct.Flags & FR_DIALOGTERM)
868 /* Give main window the focus back. */
869 (void)SetFocus(s_hwnd);
870
871 if (s_findrep_struct.Flags & FR_FINDNEXT)
872 {
873 flags = FRD_FINDNEXT;
874
875 /* Give main window the focus back: this is so the cursor isn't
876 * hollow. */
877 (void)SetFocus(s_hwnd);
878 }
879 else if (s_findrep_struct.Flags & FR_REPLACE)
880 {
881 flags = FRD_REPLACE;
882
883 /* Give main window the focus back: this is so the cursor isn't
884 * hollow. */
885 (void)SetFocus(s_hwnd);
886 }
887 else if (s_findrep_struct.Flags & FR_REPLACEALL)
888 {
889 flags = FRD_REPLACEALL;
890 }
891
892 if (flags != 0)
893 {
894 /* Call the generic GUI function to do the actual work. */
895 if (s_findrep_struct.Flags & FR_WHOLEWORD)
896 flags |= FRD_WHOLE_WORD;
897 if (s_findrep_struct.Flags & FR_MATCHCASE)
898 flags |= FRD_MATCH_CASE;
899 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
900 gui_do_findrepl(flags, s_findrep_struct.lpstrFindWhat,
901 s_findrep_struct.lpstrReplaceWith, down);
902 }
903}
904#endif
905
906 static void
907HandleMouseHide(UINT uMsg, LPARAM lParam)
908{
909 static LPARAM last_lParam = 0L;
910
911 /* We sometimes get a mousemove when the mouse didn't move... */
912 if (uMsg == WM_MOUSEMOVE)
913 {
914 if (lParam == last_lParam)
915 return;
916 last_lParam = lParam;
917 }
918
919 /* Handle specially, to centralise coding. We need to be sure we catch all
920 * possible events which should cause us to restore the cursor (as it is a
921 * shared resource, we take full responsibility for it).
922 */
923 switch (uMsg)
924 {
925 case WM_KEYUP:
926 case WM_CHAR:
927 /*
928 * blank out the pointer if necessary
929 */
930 if (p_mh)
931 gui_mch_mousehide(TRUE);
932 break;
933
934 case WM_SYSKEYUP: /* show the pointer when a system-key is pressed */
935 case WM_SYSCHAR:
936 case WM_MOUSEMOVE: /* show the pointer on any mouse action */
937 case WM_LBUTTONDOWN:
938 case WM_LBUTTONUP:
939 case WM_MBUTTONDOWN:
940 case WM_MBUTTONUP:
941 case WM_RBUTTONDOWN:
942 case WM_RBUTTONUP:
943 case WM_XBUTTONDOWN:
944 case WM_XBUTTONUP:
945 case WM_NCMOUSEMOVE:
946 case WM_NCLBUTTONDOWN:
947 case WM_NCLBUTTONUP:
948 case WM_NCMBUTTONDOWN:
949 case WM_NCMBUTTONUP:
950 case WM_NCRBUTTONDOWN:
951 case WM_NCRBUTTONUP:
952 case WM_KILLFOCUS:
953 /*
954 * if the pointer is currently hidden, then we should show it.
955 */
956 gui_mch_mousehide(FALSE);
957 break;
958 }
959}
960
961 static LRESULT CALLBACK
962_TextAreaWndProc(
963 HWND hwnd,
964 UINT uMsg,
965 WPARAM wParam,
966 LPARAM lParam)
967{
968 /*
969 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
970 hwnd, uMsg, wParam, lParam);
971 */
972
973 HandleMouseHide(uMsg, lParam);
974
975 s_uMsg = uMsg;
976 s_wParam = wParam;
977 s_lParam = lParam;
978
979#ifdef FEAT_BEVAL
980 TrackUserActivity(uMsg);
981#endif
982
983 switch (uMsg)
984 {
985 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
986 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
987 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
988 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
989 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
990 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
991 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
992 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
993 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
994 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
995 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
996#ifndef WIN16 /*<VN>*/
997 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
998 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
999 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
1000#endif
1001
1002#ifdef FEAT_BEVAL
1003 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1004 return TRUE;
1005#endif
1006
1007 default:
1008 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1009 }
1010}
1011
1012#if (defined(WIN3264) && defined(FEAT_MBYTE)) \
1013 || defined(GLOBAL_IME) \
1014 || defined(PROTO)
1015# ifdef PROTO
1016typedef int WINAPI;
1017# endif
1018
1019 LRESULT WINAPI
1020vim_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1021{
1022# ifdef GLOBAL_IME
1023 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
1024# else
1025 if (wide_WindowProc)
1026 return DefWindowProcW(hwnd, message, wParam, lParam);
1027 return DefWindowProc(hwnd, message, wParam, lParam);
1028#endif
1029}
1030#endif
1031
1032/*
1033 * Called when the foreground or background color has been changed.
1034 */
1035 void
1036gui_mch_new_colors(void)
1037{
1038 /* nothing to do? */
1039}
1040
1041/*
1042 * Set the colors to their default values.
1043 */
1044 void
1045gui_mch_def_colors()
1046{
1047 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1048 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1049 gui.def_norm_pixel = gui.norm_pixel;
1050 gui.def_back_pixel = gui.back_pixel;
1051}
1052
1053/*
1054 * Open the GUI window which was created by a call to gui_mch_init().
1055 */
1056 int
1057gui_mch_open(void)
1058{
1059#ifndef SW_SHOWDEFAULT
1060# define SW_SHOWDEFAULT 10 /* Borland 5.0 doesn't have it */
1061#endif
1062 /* Actually open the window, if not already visible
1063 * (may be done already in gui_mch_set_shellsize) */
1064 if (!IsWindowVisible(s_hwnd))
1065 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1066
1067 return OK;
1068}
1069
1070/*
1071 * Get the position of the top left corner of the window.
1072 */
1073 int
1074gui_mch_get_winpos(int *x, int *y)
1075{
1076 RECT rect;
1077
1078 GetWindowRect(s_hwnd, &rect);
1079 *x = rect.left;
1080 *y = rect.top;
1081 return OK;
1082}
1083
1084/*
1085 * Set the position of the top left corner of the window to the given
1086 * coordinates.
1087 */
1088 void
1089gui_mch_set_winpos(int x, int y)
1090{
1091 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1092 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1093}
1094 void
1095gui_mch_set_text_area_pos(int x, int y, int w, int h)
1096{
1097 static int oldx = 0;
1098 static int oldy = 0;
1099
1100 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1101
1102#ifdef FEAT_TOOLBAR
1103 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1104 SendMessage(s_toolbarhwnd, WM_SIZE,
1105 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1106#endif
1107 /* When side scroll bar is unshown, the size of window will change.
1108 * then, the text area move left or right. thus client rect should be
1109 * forcely redraw. (Yasuhiro Matsumoto) */
1110 if (oldx != x || oldy != y)
1111 {
1112 InvalidateRect(s_hwnd, NULL, FALSE);
1113 oldx = x;
1114 oldy = y;
1115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116}
1117
1118
1119/*
1120 * Scrollbar stuff:
1121 */
1122
1123 void
1124gui_mch_enable_scrollbar(
1125 scrollbar_T *sb,
1126 int flag)
1127{
1128 ShowScrollBar(sb->id, SB_CTL, flag);
1129
1130 /* TODO: When the window is maximized, the size of the window stays the
1131 * same, thus the size of the text area changes. On Win98 it's OK, on Win
1132 * NT 4.0 it's not... */
1133}
1134
1135 void
1136gui_mch_set_scrollbar_pos(
1137 scrollbar_T *sb,
1138 int x,
1139 int y,
1140 int w,
1141 int h)
1142{
Bram Moolenaar02743632005-07-25 20:42:36 +00001143 SetWindowPos(sb->id, NULL, x, y, w, h,
1144 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145}
1146
1147 void
1148gui_mch_create_scrollbar(
1149 scrollbar_T *sb,
1150 int orient) /* SBAR_VERT or SBAR_HORIZ */
1151{
1152 sb->id = CreateWindow(
1153 "SCROLLBAR", "Scrollbar",
1154 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
1155 10, /* Any value will do for now */
1156 10, /* Any value will do for now */
1157 s_hwnd, NULL,
1158 s_hinst, NULL);
1159}
1160
1161/*
1162 * Find the scrollbar with the given hwnd.
1163 */
1164 static scrollbar_T *
1165gui_mswin_find_scrollbar(HWND hwnd)
1166{
1167 win_T *wp;
1168
1169 if (gui.bottom_sbar.id == hwnd)
1170 return &gui.bottom_sbar;
1171 FOR_ALL_WINDOWS(wp)
1172 {
1173 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1174 return &wp->w_scrollbars[SBAR_LEFT];
1175 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1176 return &wp->w_scrollbars[SBAR_RIGHT];
1177 }
1178 return NULL;
1179}
1180
1181/*
1182 * Get the character size of a font.
1183 */
1184 static void
1185GetFontSize(GuiFont font)
1186{
1187 HWND hwnd = GetDesktopWindow();
1188 HDC hdc = GetWindowDC(hwnd);
1189 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
1190 TEXTMETRIC tm;
1191
1192 GetTextMetrics(hdc, &tm);
1193 gui.char_width = tm.tmAveCharWidth + tm.tmOverhang;
1194
1195 gui.char_height = tm.tmHeight
1196#ifndef MSWIN16_FASTTEXT
Bram Moolenaar02743632005-07-25 20:42:36 +00001197 + p_linespace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198#endif
Bram Moolenaar02743632005-07-25 20:42:36 +00001199 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
1201 SelectFont(hdc, hfntOld);
1202
1203 ReleaseDC(hwnd, hdc);
1204}
1205
Bram Moolenaar02743632005-07-25 20:42:36 +00001206/*
1207 * Adjust gui.char_height (after 'linespace' was changed).
1208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 int
Bram Moolenaar02743632005-07-25 20:42:36 +00001210gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211{
1212 GetFontSize(gui.norm_font);
1213 return OK;
1214}
1215
1216 static GuiFont
1217get_font_handle(LOGFONT *lf)
1218{
1219 HFONT font = NULL;
1220
1221 /* Load the font */
1222 font = CreateFontIndirect(lf);
1223
1224 if (font == NULL)
1225 return NOFONT;
1226
1227 return (GuiFont)font;
1228}
1229
1230 static int
1231pixels_to_points(int pixels, int vertical)
1232{
1233 int points;
1234 HWND hwnd;
1235 HDC hdc;
1236
1237 hwnd = GetDesktopWindow();
1238 hdc = GetWindowDC(hwnd);
1239
1240 points = MulDiv(pixels, 72,
1241 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1242
1243 ReleaseDC(hwnd, hdc);
1244
1245 return points;
1246}
1247
1248 GuiFont
1249gui_mch_get_font(
1250 char_u *name,
1251 int giveErrorIfMissing)
1252{
1253 LOGFONT lf;
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001254 GuiFont font = NOFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001256 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1257 font = get_font_handle(&lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 if (font == NOFONT && giveErrorIfMissing)
1259 EMSG2(_(e_font), name);
1260 return font;
1261}
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001262
Bram Moolenaardfccaf02004-12-31 20:56:11 +00001263#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001264/*
1265 * Return the name of font "font" in allocated memory.
1266 * Don't know how to get the actual name, thus use the provided name.
1267 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001268/*ARGSUSED*/
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001269 char_u *
1270gui_mch_get_fontname(font, name)
1271 GuiFont font;
1272 char_u *name;
1273{
1274 if (name == NULL)
1275 return NULL;
1276 return vim_strsave(name);
1277}
Bram Moolenaardfccaf02004-12-31 20:56:11 +00001278#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001279
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 void
1281gui_mch_free_font(GuiFont font)
1282{
1283 if (font)
1284 DeleteObject((HFONT)font);
1285}
1286
1287 static int
1288hex_digit(int c)
1289{
1290 if (VIM_ISDIGIT(c))
1291 return c - '0';
1292 c = TOLOWER_ASC(c);
1293 if (c >= 'a' && c <= 'f')
1294 return c - 'a' + 10;
1295 return -1000;
1296}
1297/*
1298 * Return the Pixel value (color) for the given color name.
1299 * Return INVALCOLOR for error.
1300 */
1301 guicolor_T
1302gui_mch_get_color(char_u *name)
1303{
1304 typedef struct guicolor_tTable
1305 {
1306 char *name;
1307 COLORREF color;
1308 } guicolor_tTable;
1309
1310 static guicolor_tTable table[] =
1311 {
1312 {"Black", RGB(0x00, 0x00, 0x00)},
1313 {"DarkGray", RGB(0x80, 0x80, 0x80)},
1314 {"DarkGrey", RGB(0x80, 0x80, 0x80)},
1315 {"Gray", RGB(0xC0, 0xC0, 0xC0)},
1316 {"Grey", RGB(0xC0, 0xC0, 0xC0)},
1317 {"LightGray", RGB(0xE0, 0xE0, 0xE0)},
1318 {"LightGrey", RGB(0xE0, 0xE0, 0xE0)},
1319 {"White", RGB(0xFF, 0xFF, 0xFF)},
1320 {"DarkRed", RGB(0x80, 0x00, 0x00)},
1321 {"Red", RGB(0xFF, 0x00, 0x00)},
1322 {"LightRed", RGB(0xFF, 0xA0, 0xA0)},
1323 {"DarkBlue", RGB(0x00, 0x00, 0x80)},
1324 {"Blue", RGB(0x00, 0x00, 0xFF)},
1325 {"LightBlue", RGB(0xA0, 0xA0, 0xFF)},
1326 {"DarkGreen", RGB(0x00, 0x80, 0x00)},
1327 {"Green", RGB(0x00, 0xFF, 0x00)},
1328 {"LightGreen", RGB(0xA0, 0xFF, 0xA0)},
1329 {"DarkCyan", RGB(0x00, 0x80, 0x80)},
1330 {"Cyan", RGB(0x00, 0xFF, 0xFF)},
1331 {"LightCyan", RGB(0xA0, 0xFF, 0xFF)},
1332 {"DarkMagenta", RGB(0x80, 0x00, 0x80)},
1333 {"Magenta", RGB(0xFF, 0x00, 0xFF)},
1334 {"LightMagenta", RGB(0xFF, 0xA0, 0xFF)},
1335 {"Brown", RGB(0x80, 0x40, 0x40)},
1336 {"Yellow", RGB(0xFF, 0xFF, 0x00)},
1337 {"LightYellow", RGB(0xFF, 0xFF, 0xA0)},
1338 {"DarkYellow", RGB(0xBB, 0xBB, 0x00)},
1339 {"SeaGreen", RGB(0x2E, 0x8B, 0x57)},
1340 {"Orange", RGB(0xFF, 0xA5, 0x00)},
1341 {"Purple", RGB(0xA0, 0x20, 0xF0)},
1342 {"SlateBlue", RGB(0x6A, 0x5A, 0xCD)},
1343 {"Violet", RGB(0xEE, 0x82, 0xEE)},
1344 };
1345
1346 typedef struct SysColorTable
1347 {
1348 char *name;
1349 int color;
1350 } SysColorTable;
1351
1352 static SysColorTable sys_table[] =
1353 {
1354#ifdef WIN3264
1355 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1356 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
1357#ifndef __MINGW32__
1358 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1359#endif
1360 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1361 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1362 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1363 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1364 {"SYS_DESKTOP", COLOR_DESKTOP},
1365 {"SYS_INFOBK", COLOR_INFOBK},
1366 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1367 {"SYS_3DFACE", COLOR_3DFACE},
1368#endif
1369 {"SYS_BTNFACE", COLOR_BTNFACE},
1370 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1371 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1372 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1373 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1374 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1375 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1376 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1377 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1378 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1379 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1380 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1381 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1382 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1383 {"SYS_MENU", COLOR_MENU},
1384 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1385 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1386 {"SYS_WINDOW", COLOR_WINDOW},
1387 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1388 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1389 };
1390
1391 int r, g, b;
1392 int i;
1393
1394 if (name[0] == '#' && strlen(name) == 7)
1395 {
1396 /* Name is in "#rrggbb" format */
1397 r = hex_digit(name[1]) * 16 + hex_digit(name[2]);
1398 g = hex_digit(name[3]) * 16 + hex_digit(name[4]);
1399 b = hex_digit(name[5]) * 16 + hex_digit(name[6]);
1400 if (r < 0 || g < 0 || b < 0)
1401 return INVALCOLOR;
1402 return RGB(r, g, b);
1403 }
1404 else
1405 {
1406 /* Check if the name is one of the colors we know */
1407 for (i = 0; i < sizeof(table) / sizeof(table[0]); i++)
1408 if (STRICMP(name, table[i].name) == 0)
1409 return table[i].color;
1410 }
1411
1412 /*
1413 * Try to look up a system colour.
1414 */
1415 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1416 if (STRICMP(name, sys_table[i].name) == 0)
1417 return GetSysColor(sys_table[i].color);
1418
1419 /*
1420 * Last attempt. Look in the file "$VIMRUNTIME/rgb.txt".
1421 */
1422 {
1423#define LINE_LEN 100
1424 FILE *fd;
1425 char line[LINE_LEN];
1426 char_u *fname;
1427
1428 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
1429 if (fname == NULL)
1430 return INVALCOLOR;
1431
1432 fd = fopen((char *)fname, "rt");
1433 vim_free(fname);
1434 if (fd == NULL)
1435 return INVALCOLOR;
1436
1437 while (!feof(fd))
1438 {
1439 int len;
1440 int pos;
1441 char *color;
1442
1443 fgets(line, LINE_LEN, fd);
1444 len = (int)STRLEN(line);
1445
1446 if (len <= 1 || line[len-1] != '\n')
1447 continue;
1448
1449 line[len-1] = '\0';
1450
1451 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
1452 if (i != 3)
1453 continue;
1454
1455 color = line + pos;
1456
1457 if (STRICMP(color, name) == 0)
1458 {
1459 fclose(fd);
1460 return (guicolor_T) RGB(r, g, b);
1461 }
1462 }
1463
1464 fclose(fd);
1465 }
1466
1467 return INVALCOLOR;
1468}
1469/*
1470 * Return OK if the key with the termcap name "name" is supported.
1471 */
1472 int
1473gui_mch_haskey(char_u *name)
1474{
1475 int i;
1476
1477 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1478 if (name[0] == special_keys[i].vim_code0 &&
1479 name[1] == special_keys[i].vim_code1)
1480 return OK;
1481 return FAIL;
1482}
1483
1484 void
1485gui_mch_beep(void)
1486{
1487 MessageBeep(MB_OK);
1488}
1489/*
1490 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1491 */
1492 void
1493gui_mch_invert_rectangle(
1494 int r,
1495 int c,
1496 int nr,
1497 int nc)
1498{
1499 RECT rc;
1500
1501 /*
1502 * Note: InvertRect() excludes right and bottom of rectangle.
1503 */
1504 rc.left = FILL_X(c);
1505 rc.top = FILL_Y(r);
1506 rc.right = rc.left + nc * gui.char_width;
1507 rc.bottom = rc.top + nr * gui.char_height;
1508 InvertRect(s_hdc, &rc);
1509}
1510
1511/*
1512 * Iconify the GUI window.
1513 */
1514 void
1515gui_mch_iconify(void)
1516{
1517 ShowWindow(s_hwnd, SW_MINIMIZE);
1518}
1519
1520/*
1521 * Draw a cursor without focus.
1522 */
1523 void
1524gui_mch_draw_hollow_cursor(guicolor_T color)
1525{
1526 HBRUSH hbr;
1527 RECT rc;
1528
1529 /*
1530 * Note: FrameRect() excludes right and bottom of rectangle.
1531 */
1532 rc.left = FILL_X(gui.col);
1533 rc.top = FILL_Y(gui.row);
1534 rc.right = rc.left + gui.char_width;
1535#ifdef FEAT_MBYTE
1536 if (mb_lefthalve(gui.row, gui.col))
1537 rc.right += gui.char_width;
1538#endif
1539 rc.bottom = rc.top + gui.char_height;
1540 hbr = CreateSolidBrush(color);
1541 FrameRect(s_hdc, &rc, hbr);
1542 DeleteBrush(hbr);
1543}
1544/*
1545 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1546 * color "color".
1547 */
1548 void
1549gui_mch_draw_part_cursor(
1550 int w,
1551 int h,
1552 guicolor_T color)
1553{
1554 HBRUSH hbr;
1555 RECT rc;
1556
1557 /*
1558 * Note: FillRect() excludes right and bottom of rectangle.
1559 */
1560 rc.left =
1561#ifdef FEAT_RIGHTLEFT
1562 /* vertical line should be on the right of current point */
1563 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1564#endif
1565 FILL_X(gui.col);
1566 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1567 rc.right = rc.left + w;
1568 rc.bottom = rc.top + h;
1569 hbr = CreateSolidBrush(color);
1570 FillRect(s_hdc, &rc, hbr);
1571 DeleteBrush(hbr);
1572}
1573
1574/*
1575 * Process a single Windows message.
1576 * If one is not available we hang until one is.
1577 */
1578 static void
1579process_message(void)
1580{
1581 MSG msg;
1582 UINT vk = 0; /* Virtual key */
1583 char_u string[40];
1584 int i;
1585 int modifiers = 0;
1586 int key;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001587#ifdef FEAT_MENU
1588 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590
1591 GetMessage(&msg, NULL, 0, 0);
1592
1593#ifdef FEAT_OLE
1594 /* Look after OLE Automation commands */
1595 if (msg.message == WM_OLE)
1596 {
1597 char_u *str = (char_u *)msg.lParam;
1598 add_to_input_buf(str, (int)STRLEN(str));
1599 vim_free(str);
1600 return;
1601 }
1602#endif
1603
1604#ifdef FEAT_NETBEANS_INTG
1605 if (msg.message == WM_NETBEANS)
1606 {
1607 messageFromNetbeansW32();
1608 return;
1609 }
1610#endif
1611
1612#ifdef FEAT_SNIFF
1613 if (sniff_request_waiting && want_sniff_request)
1614 {
1615 static char_u bytes[3] = {CSI, (char_u)KS_EXTRA, (char_u)KE_SNIFF};
1616 add_to_input_buf(bytes, 3); /* K_SNIFF */
1617 sniff_request_waiting = 0;
1618 want_sniff_request = 0;
1619 /* request is handled in normal.c */
1620 }
1621 if (msg.message == WM_USER)
1622 return;
1623#endif
1624
1625#ifdef MSWIN_FIND_REPLACE
1626 /* Don't process messages used by the dialog */
1627 if (s_findrep_hwnd != NULL && IsDialogMessage(s_findrep_hwnd, &msg))
1628 {
1629 HandleMouseHide(msg.message, msg.lParam);
1630 return;
1631 }
1632#endif
1633
1634 /*
1635 * Check if it's a special key that we recognise. If not, call
1636 * TranslateMessage().
1637 */
1638 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1639 {
1640 vk = (int) msg.wParam;
1641 /* handle key after dead key, but ignore shift, alt and control */
1642 if (dead_key && vk != VK_SHIFT && vk != VK_MENU && vk != VK_CONTROL)
1643 {
1644 dead_key = 0;
1645 /* handle non-alphabetic keys (ones that hopefully cannot generate
1646 * umlaut-characters), unless when control is down */
1647 if (vk < 'A' || vk > 'Z' || (GetKeyState(VK_CONTROL) & 0x8000))
1648 {
1649 MSG dm;
1650
1651 dm.message = msg.message;
1652 dm.hwnd = msg.hwnd;
1653 dm.wParam = VK_SPACE;
1654 MyTranslateMessage(&dm); /* generate dead character */
1655 if (vk != VK_SPACE) /* and send current character once more */
1656 PostMessage(msg.hwnd, msg.message, msg.wParam, msg.lParam);
1657 return;
1658 }
1659 }
1660
1661 /* Check for CTRL-BREAK */
1662 if (vk == VK_CANCEL)
1663 {
1664 trash_input_buf();
1665 got_int = TRUE;
1666 string[0] = Ctrl_C;
1667 add_to_input_buf(string, 1);
1668 }
1669
1670 for (i = 0; special_keys[i].key_sym != 0; i++)
1671 {
1672 /* ignore VK_SPACE when ALT key pressed: system menu */
1673 if (special_keys[i].key_sym == vk
1674 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1675 {
1676#ifdef FEAT_MENU
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001677 /* Check for <F10>: Windows selects the menu. When <F10> is
1678 * mapped we want to use the mapping instead. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 if (vk == VK_F10
1680 && gui.menu_is_active
Bram Moolenaarc1087e62005-05-20 21:22:17 +00001681 && check_map(k10, State, FALSE, TRUE) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 break;
1683#endif
1684 if (GetKeyState(VK_SHIFT) & 0x8000)
1685 modifiers |= MOD_MASK_SHIFT;
1686 /*
1687 * Don't use caps-lock as shift, because these are special keys
1688 * being considered here, and we only want letters to get
1689 * shifted -- webb
1690 */
1691 /*
1692 if (GetKeyState(VK_CAPITAL) & 0x0001)
1693 modifiers ^= MOD_MASK_SHIFT;
1694 */
1695 if (GetKeyState(VK_CONTROL) & 0x8000)
1696 modifiers |= MOD_MASK_CTRL;
1697 if (GetKeyState(VK_MENU) & 0x8000)
1698 modifiers |= MOD_MASK_ALT;
1699
1700 if (special_keys[i].vim_code1 == NUL)
1701 key = special_keys[i].vim_code0;
1702 else
1703 key = TO_SPECIAL(special_keys[i].vim_code0,
1704 special_keys[i].vim_code1);
1705 key = simplify_key(key, &modifiers);
1706 if (key == CSI)
1707 key = K_CSI;
1708
1709 if (modifiers)
1710 {
1711 string[0] = CSI;
1712 string[1] = KS_MODIFIER;
1713 string[2] = modifiers;
1714 add_to_input_buf(string, 3);
1715 }
1716
1717 if (IS_SPECIAL(key))
1718 {
1719 string[0] = CSI;
1720 string[1] = K_SECOND(key);
1721 string[2] = K_THIRD(key);
1722 add_to_input_buf(string, 3);
1723 }
1724 else
1725 {
1726 int len;
1727
1728 /* Handle "key" as a Unicode character. */
1729 len = char_to_string(key, string, 40);
1730 add_to_input_buf(string, len);
1731 }
1732 break;
1733 }
1734 }
1735 if (special_keys[i].key_sym == 0)
1736 {
1737 /* Some keys need C-S- where they should only need C-.
1738 * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1739 * system startup (Helmut Stiegler, 2003 Oct 3). */
1740 if (vk != 0xff
1741 && (GetKeyState(VK_CONTROL) & 0x8000)
1742 && !(GetKeyState(VK_SHIFT) & 0x8000)
1743 && !(GetKeyState(VK_MENU) & 0x8000))
1744 {
1745 /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */
1746 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1747 {
1748 string[0] = Ctrl_HAT;
1749 add_to_input_buf(string, 1);
1750 }
1751 /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */
1752 else if (vk == 0xBD) /* QWERTY for CTRL-'-' */
1753 {
1754 string[0] = Ctrl__;
1755 add_to_input_buf(string, 1);
1756 }
1757 /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */
1758 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
1759 {
1760 string[0] = Ctrl_AT;
1761 add_to_input_buf(string, 1);
1762 }
1763 else
1764 MyTranslateMessage(&msg);
1765 }
1766 else
1767 MyTranslateMessage(&msg);
1768 }
1769 }
1770#ifdef FEAT_MBYTE_IME
1771 else if (msg.message == WM_IME_NOTIFY)
1772 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
1773 else if (msg.message == WM_KEYUP && im_get_status())
1774 /* added for non-MS IME (Yasuhiro Matsumoto) */
1775 MyTranslateMessage(&msg);
1776#endif
1777#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
1778/* GIME_TEST */
1779 else if (msg.message == WM_IME_STARTCOMPOSITION)
1780 {
1781 POINT point;
1782
1783 global_ime_set_font(&norm_logfont);
1784 point.x = FILL_X(gui.col);
1785 point.y = FILL_Y(gui.row);
1786 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
1787 global_ime_set_position(&point);
1788 }
1789#endif
1790
1791#ifdef FEAT_MENU
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001792 /* Check for <F10>: Default effect is to select the menu. When <F10> is
1793 * mapped we need to stop it here to avoid strange effects (e.g., for the
1794 * key-up event) */
Bram Moolenaarc1087e62005-05-20 21:22:17 +00001795 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796#endif
1797 DispatchMessage(&msg);
1798}
1799
1800/*
1801 * Catch up with any queued events. This may put keyboard input into the
1802 * input buffer, call resize call-backs, trigger timers etc. If there is
1803 * nothing in the event queue (& no timers pending), then we return
1804 * immediately.
1805 */
1806 void
1807gui_mch_update(void)
1808{
1809 MSG msg;
1810
1811 if (!s_busy_processing)
1812 while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
1813 && !vim_is_input_buf_full())
1814 process_message();
1815}
1816
1817/*
1818 * GUI input routine called by gui_wait_for_chars(). Waits for a character
1819 * from the keyboard.
1820 * wtime == -1 Wait forever.
1821 * wtime == 0 This should never happen.
1822 * wtime > 0 Wait wtime milliseconds for a character.
1823 * Returns OK if a character was found to be available within the given time,
1824 * or FAIL otherwise.
1825 */
1826 int
1827gui_mch_wait_for_chars(int wtime)
1828{
1829 MSG msg;
1830 int focus;
1831
1832 s_timed_out = FALSE;
1833
1834 if (wtime > 0)
1835 {
1836 /* Don't do anything while processing a (scroll) message. */
1837 if (s_busy_processing)
1838 return FAIL;
1839 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)wtime,
1840 (TIMERPROC)_OnTimer);
1841 }
1842
1843 allow_scrollbar = TRUE;
1844
1845 focus = gui.in_focus;
1846 while (!s_timed_out)
1847 {
1848 /* Stop or start blinking when focus changes */
1849 if (gui.in_focus != focus)
1850 {
1851 if (gui.in_focus)
1852 gui_mch_start_blink();
1853 else
1854 gui_mch_stop_blink();
1855 focus = gui.in_focus;
1856 }
1857
1858 if (s_need_activate)
1859 {
1860#ifdef WIN32
1861 (void)SetForegroundWindow(s_hwnd);
1862#else
1863 (void)SetActiveWindow(s_hwnd);
1864#endif
1865 s_need_activate = FALSE;
1866 }
1867
1868 /*
1869 * Don't use gui_mch_update() because then we will spin-lock until a
1870 * char arrives, instead we use GetMessage() to hang until an
1871 * event arrives. No need to check for input_buf_full because we are
1872 * returning as soon as it contains a single char -- webb
1873 */
1874 process_message();
1875
1876 if (input_available())
1877 {
1878 if (s_wait_timer != 0 && !s_timed_out)
1879 {
1880 KillTimer(NULL, s_wait_timer);
1881
1882 /* Eat spurious WM_TIMER messages */
1883 while (PeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
1884 ;
1885 s_wait_timer = 0;
1886 }
1887 allow_scrollbar = FALSE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001888
1889 /* Clear pending mouse button, the release event may have been
1890 * taken by the dialog window. */
1891 s_button_pending = -1;
1892
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 return OK;
1894 }
1895 }
1896 allow_scrollbar = FALSE;
1897 return FAIL;
1898}
1899
1900/*
1901 * Clear a rectangular region of the screen from text pos (row1, col1) to
1902 * (row2, col2) inclusive.
1903 */
1904 void
1905gui_mch_clear_block(
1906 int row1,
1907 int col1,
1908 int row2,
1909 int col2)
1910{
1911 RECT rc;
1912
1913 /*
1914 * Clear one extra pixel at the far right, for when bold characters have
1915 * spilled over to the window border.
1916 * Note: FillRect() excludes right and bottom of rectangle.
1917 */
1918 rc.left = FILL_X(col1);
1919 rc.top = FILL_Y(row1);
1920 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
1921 rc.bottom = FILL_Y(row2 + 1);
1922 clear_rect(&rc);
1923}
1924
1925/*
1926 * Clear the whole text window.
1927 */
1928 void
1929gui_mch_clear_all(void)
1930{
1931 RECT rc;
1932
1933 rc.left = 0;
1934 rc.top = 0;
1935 rc.right = Columns * gui.char_width + 2 * gui.border_width;
1936 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
1937 clear_rect(&rc);
1938}
1939/*
1940 * Menu stuff.
1941 */
1942
1943 void
1944gui_mch_enable_menu(int flag)
1945{
1946#ifdef FEAT_MENU
1947 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
1948#endif
1949}
1950
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001951/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 void
1953gui_mch_set_menu_pos(
1954 int x,
1955 int y,
1956 int w,
1957 int h)
1958{
1959 /* It will be in the right place anyway */
1960}
1961
1962#if defined(FEAT_MENU) || defined(PROTO)
1963/*
1964 * Make menu item hidden or not hidden
1965 */
1966 void
1967gui_mch_menu_hidden(
1968 vimmenu_T *menu,
1969 int hidden)
1970{
1971 /*
1972 * This doesn't do what we want. Hmm, just grey the menu items for now.
1973 */
1974 /*
1975 if (hidden)
1976 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
1977 else
1978 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
1979 */
1980 gui_mch_menu_grey(menu, hidden);
1981}
1982
1983/*
1984 * This is called after setting all the menus to grey/hidden or not.
1985 */
1986 void
1987gui_mch_draw_menubar(void)
1988{
1989 DrawMenuBar(s_hwnd);
1990}
1991#endif /*FEAT_MENU*/
1992
1993#ifndef PROTO
1994void
1995#ifdef VIMDLL
1996_export
1997#endif
1998_cdecl
1999SaveInst(HINSTANCE hInst)
2000{
2001 s_hinst = hInst;
2002}
2003#endif
2004
2005/*
2006 * Return the RGB value of a pixel as a long.
2007 */
2008 long_u
2009gui_mch_get_rgb(guicolor_T pixel)
2010{
2011 return (GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2012 + GetBValue(pixel);
2013}
2014
2015#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2016/* Convert pixels in X to dialog units */
2017 static WORD
2018PixelToDialogX(int numPixels)
2019{
2020 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2021}
2022
2023/* Convert pixels in Y to dialog units */
2024 static WORD
2025PixelToDialogY(int numPixels)
2026{
2027 return (WORD)((numPixels * 8) / s_dlgfntheight);
2028}
2029
2030/* Return the width in pixels of the given text in the given DC. */
2031 static int
2032GetTextWidth(HDC hdc, char_u *str, int len)
2033{
2034 SIZE size;
2035
2036 GetTextExtentPoint(hdc, str, len, &size);
2037 return size.cx;
2038}
2039
2040#ifdef FEAT_MBYTE
2041/*
2042 * Return the width in pixels of the given text in the given DC, taking care
2043 * of 'encoding' to active codepage conversion.
2044 */
2045 static int
2046GetTextWidthEnc(HDC hdc, char_u *str, int len)
2047{
2048 SIZE size;
2049 WCHAR *wstr;
2050 int n;
2051 int wlen = len;
2052
2053 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2054 {
2055 /* 'encoding' differs from active codepage: convert text and use wide
2056 * function */
2057 wstr = enc_to_ucs2(str, &wlen);
2058 if (wstr != NULL)
2059 {
2060 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2061 vim_free(wstr);
2062 if (n)
2063 return size.cx;
2064 }
2065 }
2066
2067 return GetTextWidth(hdc, str, len);
2068}
2069#else
2070# define GetTextWidthEnc(h, s, l) GetTextWidth((h), (s), (l))
2071#endif
2072
2073/*
2074 * A quick little routine that will center one window over another, handy for
2075 * dialog boxes. Taken from the Win32SDK samples.
2076 */
2077 static BOOL
2078CenterWindow(
2079 HWND hwndChild,
2080 HWND hwndParent)
2081{
2082 RECT rChild, rParent;
2083 int wChild, hChild, wParent, hParent;
2084 int wScreen, hScreen, xNew, yNew;
2085 HDC hdc;
2086
2087 GetWindowRect(hwndChild, &rChild);
2088 wChild = rChild.right - rChild.left;
2089 hChild = rChild.bottom - rChild.top;
2090
2091 /* If Vim is minimized put the window in the middle of the screen. */
2092 if (IsMinimized(hwndParent))
2093 {
2094#ifdef WIN16
2095 rParent.left = 0;
2096 rParent.top = 0;
2097 rParent.right = GetSystemMetrics(SM_CXSCREEN);
2098 rParent.bottom = GetSystemMetrics(SM_CYFULLSCREEN);
2099#else
2100 SystemParametersInfo(SPI_GETWORKAREA, 0, &rParent, 0);
2101#endif
2102 }
2103 else
2104 GetWindowRect(hwndParent, &rParent);
2105 wParent = rParent.right - rParent.left;
2106 hParent = rParent.bottom - rParent.top;
2107
2108 hdc = GetDC(hwndChild);
2109 wScreen = GetDeviceCaps (hdc, HORZRES);
2110 hScreen = GetDeviceCaps (hdc, VERTRES);
2111 ReleaseDC(hwndChild, hdc);
2112
2113 xNew = rParent.left + ((wParent - wChild) /2);
2114 if (xNew < 0)
2115 {
2116 xNew = 0;
2117 }
2118 else if ((xNew+wChild) > wScreen)
2119 {
2120 xNew = wScreen - wChild;
2121 }
2122
2123 yNew = rParent.top + ((hParent - hChild) /2);
2124 if (yNew < 0)
2125 yNew = 0;
2126 else if ((yNew+hChild) > hScreen)
2127 yNew = hScreen - hChild;
2128
2129 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2130 SWP_NOSIZE | SWP_NOZORDER);
2131}
2132#endif /* FEAT_GUI_DIALOG */
2133
2134void
2135gui_mch_activate_window(void)
2136{
2137 (void)SetActiveWindow(s_hwnd);
2138}
2139
2140#if defined(FEAT_TOOLBAR) || defined(PROTO)
2141 void
2142gui_mch_show_toolbar(int showit)
2143{
2144 if (s_toolbarhwnd == NULL)
2145 return;
2146
2147 if (showit)
2148 ShowWindow(s_toolbarhwnd, SW_SHOW);
2149 else
2150 ShowWindow(s_toolbarhwnd, SW_HIDE);
2151}
2152
2153/* Then number of bitmaps is fixed. Exit is missing! */
2154#define TOOLBAR_BITMAP_COUNT 31
2155
2156#endif
2157
2158/*
2159 * ":simalt" command.
2160 */
2161 void
2162ex_simalt(exarg_T *eap)
2163{
2164 char_u *keys = eap->arg;
2165
2166 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2167 while (*keys)
2168 {
2169 if (*keys == '~')
2170 *keys = ' '; /* for showing system menu */
2171 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2172 keys++;
2173 }
2174}
2175
2176/*
2177 * Create the find & replace dialogs.
2178 * You can't have both at once: ":find" when replace is showing, destroys
2179 * the replace dialog first, and the other way around.
2180 */
2181#ifdef MSWIN_FIND_REPLACE
2182 static void
2183initialise_findrep(char_u *initial_string)
2184{
2185 int wword = FALSE;
2186 int mcase = !p_ic;
2187 char_u *entry_text;
2188
2189 /* Get the search string to use. */
2190 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2191
2192 s_findrep_struct.hwndOwner = s_hwnd;
2193 s_findrep_struct.Flags = FR_DOWN;
2194 if (mcase)
2195 s_findrep_struct.Flags |= FR_MATCHCASE;
2196 if (wword)
2197 s_findrep_struct.Flags |= FR_WHOLEWORD;
2198 if (entry_text != NULL && *entry_text != NUL)
2199 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +00002200 vim_strncpy(s_findrep_struct.lpstrFindWhat, entry_text,
2201 s_findrep_struct.wFindWhatLen - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 s_findrep_struct.lpstrReplaceWith[0] = NUL;
2203 }
2204 vim_free(entry_text);
2205}
2206#endif
2207
2208 void
2209gui_mch_find_dialog(exarg_T *eap)
2210{
2211#ifdef MSWIN_FIND_REPLACE
2212 if (s_findrep_msg != 0)
2213 {
2214 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2215 DestroyWindow(s_findrep_hwnd);
2216
2217 if (!IsWindow(s_findrep_hwnd))
2218 {
2219 initialise_findrep(eap->arg);
2220 s_findrep_hwnd = FindText((LPFINDREPLACE) &s_findrep_struct);
2221 }
2222
2223 (void)SetWindowText(s_findrep_hwnd,
2224 (LPCSTR)_("Find string (use '\\\\' to find a '\\')"));
2225 (void)SetFocus(s_findrep_hwnd);
2226
2227 s_findrep_is_find = TRUE;
2228 }
2229#endif
2230}
2231
2232
2233 void
2234gui_mch_replace_dialog(exarg_T *eap)
2235{
2236#ifdef MSWIN_FIND_REPLACE
2237 if (s_findrep_msg != 0)
2238 {
2239 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2240 DestroyWindow(s_findrep_hwnd);
2241
2242 if (!IsWindow(s_findrep_hwnd))
2243 {
2244 initialise_findrep(eap->arg);
2245 s_findrep_hwnd = ReplaceText((LPFINDREPLACE) &s_findrep_struct);
2246 }
2247
2248 (void)SetWindowText(s_findrep_hwnd,
2249 (LPCSTR)_("Find & Replace (use '\\\\' to find a '\\')"));
2250 (void)SetFocus(s_findrep_hwnd);
2251
2252 s_findrep_is_find = FALSE;
2253 }
2254#endif
2255}
2256
2257
2258/*
2259 * Set visibility of the pointer.
2260 */
2261 void
2262gui_mch_mousehide(int hide)
2263{
2264 if (hide != gui.pointer_hidden)
2265 {
2266 ShowCursor(!hide);
2267 gui.pointer_hidden = hide;
2268 }
2269}
2270
2271#ifdef FEAT_MENU
2272 static void
2273gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2274{
2275 /* Unhide the mouse, we don't get move events here. */
2276 gui_mch_mousehide(FALSE);
2277
2278 (void)TrackPopupMenu(
2279 (HMENU)menu->submenu_id,
2280 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2281 x, y,
2282 (int)0, /*reserved param*/
2283 s_hwnd,
2284 NULL);
2285 /*
2286 * NOTE: The pop-up menu can eat the mouse up event.
2287 * We deal with this in normal.c.
2288 */
2289}
2290#endif
2291
2292/*
2293 * Got a message when the system will go down.
2294 */
2295 static void
2296_OnEndSession(void)
2297{
2298 getout_preserve_modified(1);
2299}
2300
2301/*
2302 * Get this message when the user clicks on the cross in the top right corner
2303 * of a Windows95 window.
2304 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002305/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 static void
2307_OnClose(
2308 HWND hwnd)
2309{
2310 gui_shell_closed();
2311}
2312
2313/*
2314 * Get a message when the window is being destroyed.
2315 */
2316 static void
2317_OnDestroy(
2318 HWND hwnd)
2319{
2320#ifdef WIN16_3DLOOK
2321 Ctl3dUnregister(s_hinst);
2322#endif
2323 if (!destroying)
2324 _OnClose(hwnd);
2325}
2326
2327 static void
2328_OnPaint(
2329 HWND hwnd)
2330{
2331 if (!IsMinimized(hwnd))
2332 {
2333 PAINTSTRUCT ps;
2334
2335 out_flush(); /* make sure all output has been processed */
2336 (void)BeginPaint(hwnd, &ps);
2337
2338#ifdef FEAT_MBYTE
2339 /* prevent multi-byte characters from misprinting on an invalid
2340 * rectangle */
2341 if (has_mbyte)
2342 {
2343 RECT rect;
2344
2345 GetClientRect(hwnd, &rect);
2346 ps.rcPaint.left = rect.left;
2347 ps.rcPaint.right = rect.right;
2348 }
2349#endif
2350
2351 if (!IsRectEmpty(&ps.rcPaint))
2352 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2353 ps.rcPaint.right - ps.rcPaint.left + 1,
2354 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2355 EndPaint(hwnd, &ps);
2356 }
2357}
2358
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002359/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 static void
2361_OnSize(
2362 HWND hwnd,
2363 UINT state,
2364 int cx,
2365 int cy)
2366{
2367 if (!IsMinimized(hwnd))
2368 {
2369 gui_resize_shell(cx, cy);
2370
2371#ifdef FEAT_MENU
2372 /* Menu bar may wrap differently now */
2373 gui_mswin_get_menu_height(TRUE);
2374#endif
2375 }
2376}
2377
2378 static void
2379_OnSetFocus(
2380 HWND hwnd,
2381 HWND hwndOldFocus)
2382{
2383 gui_focus_change(TRUE);
2384 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2385}
2386
2387 static void
2388_OnKillFocus(
2389 HWND hwnd,
2390 HWND hwndNewFocus)
2391{
2392 gui_focus_change(FALSE);
2393 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2394}
2395
2396/*
2397 * Get a message when the user switches back to vim
2398 */
2399#ifdef WIN16
2400 static BOOL
2401#else
2402 static LRESULT
2403#endif
2404_OnActivateApp(
2405 HWND hwnd,
2406 BOOL fActivate,
2407#ifdef WIN16
2408 HTASK dwThreadId
2409#else
2410 DWORD dwThreadId
2411#endif
2412 )
2413{
2414 /* we call gui_focus_change() in _OnSetFocus() */
2415 /* gui_focus_change((int)fActivate); */
2416 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2417}
2418
2419#if defined(FEAT_WINDOWS) || defined(PROTO)
2420 void
2421gui_mch_destroy_scrollbar(scrollbar_T *sb)
2422{
2423 DestroyWindow(sb->id);
2424}
2425#endif
2426
2427/*
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002428 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 */
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002430 void
Bram Moolenaara40c5002005-01-09 21:16:21 +00002431gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432{
2433 RECT rct;
2434 POINT mp;
2435
2436 (void)GetWindowRect(s_textArea, &rct);
2437 (void)GetCursorPos((LPPOINT)&mp);
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002438 *x = (int)(mp.x - rct.left);
2439 *y = (int)(mp.y - rct.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440}
2441
2442/*
2443 * Move mouse pointer to character at (x, y).
2444 */
2445 void
2446gui_mch_setmouse(int x, int y)
2447{
2448 RECT rct;
2449
2450 (void)GetWindowRect(s_textArea, &rct);
2451 (void)SetCursorPos(x + gui.border_offset + rct.left,
2452 y + gui.border_offset + rct.top);
2453}
2454
2455 static void
2456gui_mswin_get_valid_dimensions(
2457 int w,
2458 int h,
2459 int *valid_w,
2460 int *valid_h)
2461{
2462 int base_width, base_height;
2463
2464 base_width = gui_get_base_width()
2465 + GetSystemMetrics(SM_CXFRAME) * 2;
2466 base_height = gui_get_base_height()
2467 + GetSystemMetrics(SM_CYFRAME) * 2
2468 + GetSystemMetrics(SM_CYCAPTION)
2469#ifdef FEAT_MENU
2470 + gui_mswin_get_menu_height(FALSE)
2471#endif
2472 ;
2473 *valid_w = base_width +
2474 ((w - base_width) / gui.char_width) * gui.char_width;
2475 *valid_h = base_height +
2476 ((h - base_height) / gui.char_height) * gui.char_height;
2477}
2478
2479 void
2480gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
2481{
2482
2483 *screen_w = GetSystemMetrics(SM_CXFULLSCREEN)
2484 - GetSystemMetrics(SM_CXFRAME) * 2;
2485 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
2486 * the menubar for MSwin, we subtract it from the screen height, so that
2487 * the window size can be made to fit on the screen. */
2488 *screen_h = GetSystemMetrics(SM_CYFULLSCREEN)
2489 - GetSystemMetrics(SM_CYFRAME) * 2
2490#ifdef FEAT_MENU
2491 - gui_mswin_get_menu_height(FALSE)
2492#endif
2493 ;
2494}
2495
2496 void
2497gui_mch_flash(int msec)
2498{
2499 RECT rc;
2500
2501 /*
2502 * Note: InvertRect() excludes right and bottom of rectangle.
2503 */
2504 rc.left = 0;
2505 rc.top = 0;
2506 rc.right = gui.num_cols * gui.char_width;
2507 rc.bottom = gui.num_rows * gui.char_height;
2508 InvertRect(s_hdc, &rc);
2509 gui_mch_flush(); /* make sure it's displayed */
2510
2511 ui_delay((long)msec, TRUE); /* wait for a few msec */
2512
2513 InvertRect(s_hdc, &rc);
2514}
2515
2516/*
2517 * Return flags used for scrolling.
2518 * The SW_INVALIDATE is required when part of the window is covered or
2519 * off-screen. Refer to MS KB Q75236.
2520 */
2521 static int
2522get_scroll_flags(void)
2523{
2524 HWND hwnd;
2525 RECT rcVim, rcOther, rcDest;
2526
2527 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaar0d406992005-05-22 22:03:39 +00002528
2529 /* Check if the window is partly above or below the screen. We don't care
2530 * about partly left or right of the screen, it is not relevant when
2531 * scrolling up or down. */
2532 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
2533 return SW_INVALIDATE;
2534
2535 /* Check if there is an window (partly) on top of us. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
2537 if (IsWindowVisible(hwnd))
2538 {
2539 GetWindowRect(hwnd, &rcOther);
2540 if (IntersectRect(&rcDest, &rcVim, &rcOther))
2541 return SW_INVALIDATE;
2542 }
2543 return 0;
2544}
2545
2546/*
2547 * Delete the given number of lines from the given row, scrolling up any
2548 * text further down within the scroll region.
2549 */
2550 void
2551gui_mch_delete_lines(
2552 int row,
2553 int num_lines)
2554{
2555 RECT rc;
2556
2557 rc.left = FILL_X(gui.scroll_region_left);
2558 rc.right = FILL_X(gui.scroll_region_right + 1);
2559 rc.top = FILL_Y(row);
2560 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
2561
2562 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
2563 &rc, &rc, NULL, NULL, get_scroll_flags());
2564
2565 UpdateWindow(s_textArea);
2566 /* This seems to be required to avoid the cursor disappearing when
2567 * scrolling such that the cursor ends up in the top-left character on
2568 * the screen... But why? (Webb) */
2569 /* It's probably fixed by disabling drawing the cursor while scrolling. */
2570 /* gui.cursor_is_valid = FALSE; */
2571
2572 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
2573 gui.scroll_region_left,
2574 gui.scroll_region_bot, gui.scroll_region_right);
2575}
2576
2577/*
2578 * Insert the given number of lines before the given row, scrolling down any
2579 * following text within the scroll region.
2580 */
2581 void
2582gui_mch_insert_lines(
2583 int row,
2584 int num_lines)
2585{
2586 RECT rc;
2587
2588 rc.left = FILL_X(gui.scroll_region_left);
2589 rc.right = FILL_X(gui.scroll_region_right + 1);
2590 rc.top = FILL_Y(row);
2591 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
2592 /* The SW_INVALIDATE is required when part of the window is covered or
2593 * off-screen. How do we avoid it when it's not needed? */
2594 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
2595 &rc, &rc, NULL, NULL, get_scroll_flags());
2596
2597 UpdateWindow(s_textArea);
2598
2599 gui_clear_block(row, gui.scroll_region_left,
2600 row + num_lines - 1, gui.scroll_region_right);
2601}
2602
2603
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002604/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 void
2606gui_mch_exit(int rc)
2607{
2608 ReleaseDC(s_textArea, s_hdc);
2609 DeleteObject(s_brush);
2610
2611#ifdef FEAT_TEAROFF
2612 /* Unload the tearoff bitmap */
2613 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
2614#endif
2615
2616 /* Destroy our window (if we have one). */
2617 if (s_hwnd != NULL)
2618 {
2619 destroying = TRUE; /* ignore WM_DESTROY message now */
2620 DestroyWindow(s_hwnd);
2621 }
2622
2623#ifdef GLOBAL_IME
2624 global_ime_end();
2625#endif
2626}
2627
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002628 static char_u *
2629logfont2name(LOGFONT lf)
2630{
2631 char *p;
2632 char *res;
2633 char *charset_name;
2634
2635 charset_name = charset_id2name((int)lf.lfCharSet);
2636 res = alloc((unsigned)(strlen(lf.lfFaceName) + 20
2637 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
2638 if (res != NULL)
2639 {
2640 p = res;
2641 /* make a normal font string out of the lf thing:*/
2642 sprintf((char *)p, "%s:h%d", lf.lfFaceName, pixels_to_points(
2643 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
2644 while (*p)
2645 {
2646 if (*p == ' ')
2647 *p = '_';
2648 ++p;
2649 }
2650#ifndef MSWIN16_FASTTEXT
2651 if (lf.lfItalic)
2652 STRCAT(p, ":i");
2653 if (lf.lfWeight >= FW_BOLD)
2654 STRCAT(p, ":b");
2655#endif
2656 if (lf.lfUnderline)
2657 STRCAT(p, ":u");
2658 if (lf.lfStrikeOut)
2659 STRCAT(p, ":s");
2660 if (charset_name != NULL)
2661 {
2662 STRCAT(p, ":c");
2663 STRCAT(p, charset_name);
2664 }
2665 }
2666
2667 return res;
2668}
2669
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002671 * Initialise vim to use the font with the given name.
2672 * Return FAIL if the font could not be loaded, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002674/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 int
2676gui_mch_init_font(char_u *font_name, int fontset)
2677{
2678 LOGFONT lf;
2679 GuiFont font = NOFONT;
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002680 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681
2682 /* Load the font */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002683 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 font = get_font_handle(&lf);
2685 if (font == NOFONT)
2686 return FAIL;
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002687
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 if (font_name == NULL)
2689 font_name = lf.lfFaceName;
2690#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
2691 norm_logfont = lf;
2692#endif
2693#ifdef FEAT_MBYTE_IME
2694 im_set_font(&lf);
2695#endif
2696 gui_mch_free_font(gui.norm_font);
2697 gui.norm_font = font;
2698 current_font_height = lf.lfHeight;
2699 GetFontSize(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002701 p = logfont2name(lf);
2702 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002704 hl_set_font_name(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002706 /* When setting 'guifont' to "*" replace it with the actual font name.
2707 * */
2708 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 vim_free(p_guifont);
2711 p_guifont = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002713 else
2714 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 }
2716
2717#ifndef MSWIN16_FASTTEXT
2718 gui_mch_free_font(gui.ital_font);
2719 gui.ital_font = NOFONT;
2720 gui_mch_free_font(gui.bold_font);
2721 gui.bold_font = NOFONT;
2722 gui_mch_free_font(gui.boldital_font);
2723 gui.boldital_font = NOFONT;
2724
2725 if (!lf.lfItalic)
2726 {
2727 lf.lfItalic = TRUE;
2728 gui.ital_font = get_font_handle(&lf);
2729 lf.lfItalic = FALSE;
2730 }
2731 if (lf.lfWeight < FW_BOLD)
2732 {
2733 lf.lfWeight = FW_BOLD;
2734 gui.bold_font = get_font_handle(&lf);
2735 if (!lf.lfItalic)
2736 {
2737 lf.lfItalic = TRUE;
2738 gui.boldital_font = get_font_handle(&lf);
2739 }
2740 }
2741#endif
2742
2743 return OK;
2744}
2745
2746/*
2747 * Return TRUE if the GUI window is maximized, filling the whole screen.
2748 */
2749 int
2750gui_mch_maximized()
2751{
2752 return IsZoomed(s_hwnd);
2753}
2754
2755/*
2756 * Called when the font changed while the window is maximized. Compute the
2757 * new Rows and Columns. This is like resizing the window.
2758 */
2759 void
2760gui_mch_newfont()
2761{
2762 RECT rect;
2763
2764 GetWindowRect(s_hwnd, &rect);
2765 gui_resize_shell(rect.right - rect.left
2766 - GetSystemMetrics(SM_CXFRAME) * 2,
2767 rect.bottom - rect.top
2768 - GetSystemMetrics(SM_CYFRAME) * 2
2769 - GetSystemMetrics(SM_CYCAPTION)
2770#ifdef FEAT_MENU
2771 - gui_mswin_get_menu_height(FALSE)
2772#endif
2773 );
2774}
2775
2776/*
2777 * Set the window title
2778 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002779/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 void
2781gui_mch_settitle(
2782 char_u *title,
2783 char_u *icon)
2784{
2785#ifdef FEAT_MBYTE
2786 if (title != NULL && enc_codepage >= 0 && enc_codepage != (int)GetACP())
2787 {
2788 WCHAR *wbuf;
2789 int n;
2790
2791 /* Convert the title from 'encoding' to ucs2. */
2792 wbuf = (WCHAR *)enc_to_ucs2(title, NULL);
2793 if (wbuf != NULL)
2794 {
2795 n = SetWindowTextW(s_hwnd, wbuf);
2796 vim_free(wbuf);
2797 if (n != 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
2798 return;
2799 /* Retry with non-wide function (for Windows 98). */
2800 }
2801 }
2802#endif
2803 SetWindowText(s_hwnd, (LPCSTR)(title == NULL ? "VIM" : (char *)title));
2804}
2805
2806#ifdef FEAT_MOUSESHAPE
2807/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
2808 * misc2.c! */
2809static LPCSTR mshape_idcs[] =
2810{
2811 MAKEINTRESOURCE(IDC_ARROW), /* arrow */
2812 MAKEINTRESOURCE(0), /* blank */
2813 MAKEINTRESOURCE(IDC_IBEAM), /* beam */
2814 MAKEINTRESOURCE(IDC_SIZENS), /* updown */
2815 MAKEINTRESOURCE(IDC_SIZENS), /* udsizing */
2816 MAKEINTRESOURCE(IDC_SIZEWE), /* leftright */
2817 MAKEINTRESOURCE(IDC_SIZEWE), /* lrsizing */
2818 MAKEINTRESOURCE(IDC_WAIT), /* busy */
2819#ifdef WIN3264
2820 MAKEINTRESOURCE(IDC_NO), /* no */
2821#else
2822 MAKEINTRESOURCE(IDC_ICON), /* no */
2823#endif
2824 MAKEINTRESOURCE(IDC_ARROW), /* crosshair */
2825 MAKEINTRESOURCE(IDC_ARROW), /* hand1 */
2826 MAKEINTRESOURCE(IDC_ARROW), /* hand2 */
2827 MAKEINTRESOURCE(IDC_ARROW), /* pencil */
2828 MAKEINTRESOURCE(IDC_ARROW), /* question */
2829 MAKEINTRESOURCE(IDC_ARROW), /* right-arrow */
2830 MAKEINTRESOURCE(IDC_UPARROW), /* up-arrow */
2831 MAKEINTRESOURCE(IDC_ARROW) /* last one */
2832};
2833
2834 void
2835mch_set_mouse_shape(int shape)
2836{
2837 LPCSTR idc;
2838
2839 if (shape == MSHAPE_HIDE)
2840 ShowCursor(FALSE);
2841 else
2842 {
2843 if (shape >= MSHAPE_NUMBERED)
2844 idc = MAKEINTRESOURCE(IDC_ARROW);
2845 else
2846 idc = mshape_idcs[shape];
2847#ifdef _WIN64
2848 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
2849#else
2850# ifdef WIN32
2851 SetClassLong(s_textArea, GCL_HCURSOR, (LONG)LoadCursor(NULL, idc));
2852# else
2853 SetClassWord(s_textArea, GCW_HCURSOR, (WORD)LoadCursor(NULL, idc));
2854# endif
2855#endif
2856 if (!p_mh)
2857 {
2858 POINT mp;
2859
2860 /* Set the position to make it redrawn with the new shape. */
2861 (void)GetCursorPos((LPPOINT)&mp);
2862 (void)SetCursorPos(mp.x, mp.y);
2863 ShowCursor(TRUE);
2864 }
2865 }
2866}
2867#endif
2868
2869#ifdef FEAT_BROWSE
2870/*
2871 * The file browser exists in two versions: with "W" uses wide characters,
2872 * without "W" the current codepage. When FEAT_MBYTE is defined and on
2873 * Windows NT/2000/XP the "W" functions are used.
2874 */
2875
2876# if defined(FEAT_MBYTE) && defined(WIN3264)
2877/*
2878 * Wide version of convert_filter(). Keep in sync!
2879 */
2880 static WCHAR *
2881convert_filterW(char_u *s)
2882{
2883 WCHAR *res;
2884 unsigned s_len = (unsigned)STRLEN(s);
2885 unsigned i;
2886
2887 res = (WCHAR *)alloc((s_len + 3) * sizeof(WCHAR));
2888 if (res != NULL)
2889 {
2890 for (i = 0; i < s_len; ++i)
2891 if (s[i] == '\t' || s[i] == '\n')
2892 res[i] = '\0';
2893 else
2894 res[i] = s[i];
2895 res[s_len] = NUL;
2896 /* Add two extra NULs to make sure it's properly terminated. */
2897 res[s_len + 1] = NUL;
2898 res[s_len + 2] = NUL;
2899 }
2900 return res;
2901}
2902
2903/*
2904 * Wide version of gui_mch_browse(). Keep in sync!
2905 */
2906 static char_u *
2907gui_mch_browseW(
2908 int saving,
2909 char_u *title,
2910 char_u *dflt,
2911 char_u *ext,
2912 char_u *initdir,
2913 char_u *filter)
2914{
2915 /* We always use the wide function. This means enc_to_ucs2() must work,
2916 * otherwise it fails miserably! */
2917 OPENFILENAMEW fileStruct;
2918 WCHAR fileBuf[MAXPATHL];
2919 WCHAR *wp;
2920 int i;
2921 WCHAR *titlep = NULL;
2922 WCHAR *extp = NULL;
2923 WCHAR *initdirp = NULL;
2924 WCHAR *filterp;
2925 char_u *p;
2926
2927 if (dflt == NULL)
2928 fileBuf[0] = NUL;
2929 else
2930 {
2931 wp = enc_to_ucs2(dflt, NULL);
2932 if (wp == NULL)
2933 fileBuf[0] = NUL;
2934 else
2935 {
2936 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
2937 fileBuf[i] = wp[i];
2938 fileBuf[i] = NUL;
2939 vim_free(wp);
2940 }
2941 }
2942
2943 /* Convert the filter to Windows format. */
2944 filterp = convert_filterW(filter);
2945
2946 memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
2947#ifdef OPENFILENAME_SIZE_VERSION_400
2948 /* be compatible with Windows NT 4.0 */
2949 /* TODO: what to use for OPENFILENAMEW??? */
2950 fileStruct.lStructSize = sizeof(OPENFILENAME_SIZE_VERSION_400);
2951#else
2952 fileStruct.lStructSize = sizeof(fileStruct);
2953#endif
2954
2955 if (title != NULL)
2956 titlep = enc_to_ucs2(title, NULL);
2957 fileStruct.lpstrTitle = titlep;
2958
2959 if (ext != NULL)
2960 extp = enc_to_ucs2(ext, NULL);
2961 fileStruct.lpstrDefExt = extp;
2962
2963 fileStruct.lpstrFile = fileBuf;
2964 fileStruct.nMaxFile = MAXPATHL;
2965 fileStruct.lpstrFilter = filterp;
2966 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
2967 /* has an initial dir been specified? */
2968 if (initdir != NULL && *initdir != NUL)
2969 {
2970 /* Must have backslashes here, no matter what 'shellslash' says */
2971 initdirp = enc_to_ucs2(initdir, NULL);
2972 if (initdirp != NULL)
2973 {
2974 for (wp = initdirp; *wp != NUL; ++wp)
2975 if (*wp == '/')
2976 *wp = '\\';
2977 }
2978 fileStruct.lpstrInitialDir = initdirp;
2979 }
2980
2981 /*
2982 * TODO: Allow selection of multiple files. Needs another arg to this
2983 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
2984 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
2985 * files that don't exist yet, so I haven't put it in. What about
2986 * OFN_PATHMUSTEXIST?
2987 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
2988 */
2989 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
2990#ifdef FEAT_SHORTCUT
2991 if (curbuf->b_p_bin)
2992 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
2993#endif
2994 if (saving)
2995 {
2996 if (!GetSaveFileNameW(&fileStruct))
2997 return NULL;
2998 }
2999 else
3000 {
3001 if (!GetOpenFileNameW(&fileStruct))
3002 return NULL;
3003 }
3004
3005 vim_free(filterp);
3006 vim_free(initdirp);
3007 vim_free(titlep);
3008 vim_free(extp);
3009
3010 /* Convert from UCS2 to 'encoding'. */
3011 p = ucs2_to_enc(fileBuf, NULL);
3012 if (p != NULL)
3013 /* when out of memory we get garbage for non-ASCII chars */
3014 STRCPY(fileBuf, p);
3015 vim_free(p);
3016
3017 /* Give focus back to main window (when using MDI). */
3018 SetFocus(s_hwnd);
3019
3020 /* Shorten the file name if possible */
3021 mch_dirname(IObuff, IOSIZE);
3022 p = shorten_fname((char_u *)fileBuf, IObuff);
3023 if (p == NULL)
3024 p = (char_u *)fileBuf;
3025 return vim_strsave(p);
3026}
3027# endif /* FEAT_MBYTE */
3028
3029
3030/*
3031 * Convert the string s to the proper format for a filter string by replacing
3032 * the \t and \n delimeters with \0.
3033 * Returns the converted string in allocated memory.
3034 *
3035 * Keep in sync with convert_filterW() above!
3036 */
3037 static char_u *
3038convert_filter(char_u *s)
3039{
3040 char_u *res;
3041 unsigned s_len = (unsigned)STRLEN(s);
3042 unsigned i;
3043
3044 res = alloc(s_len + 3);
3045 if (res != NULL)
3046 {
3047 for (i = 0; i < s_len; ++i)
3048 if (s[i] == '\t' || s[i] == '\n')
3049 res[i] = '\0';
3050 else
3051 res[i] = s[i];
3052 res[s_len] = NUL;
3053 /* Add two extra NULs to make sure it's properly terminated. */
3054 res[s_len + 1] = NUL;
3055 res[s_len + 2] = NUL;
3056 }
3057 return res;
3058}
3059
3060/*
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003061 * Select a directory.
3062 */
3063 char_u *
3064gui_mch_browsedir(char_u *title, char_u *initdir)
3065{
3066 /* We fake this: Use a filter that doesn't select anything and a default
3067 * file name that won't be used. */
3068 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3069 initdir, (char_u *)_("Directory\t*.nothing\n"));
3070}
3071
3072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 * Pop open a file browser and return the file selected, in allocated memory,
3074 * or NULL if Cancel is hit.
3075 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3076 * title - Title message for the file browser dialog.
3077 * dflt - Default name of file.
3078 * ext - Default extension to be added to files without extensions.
3079 * initdir - directory in which to open the browser (NULL = current dir)
3080 * filter - Filter for matched files to choose from.
3081 *
3082 * Keep in sync with gui_mch_browseW() above!
3083 */
3084 char_u *
3085gui_mch_browse(
3086 int saving,
3087 char_u *title,
3088 char_u *dflt,
3089 char_u *ext,
3090 char_u *initdir,
3091 char_u *filter)
3092{
3093 OPENFILENAME fileStruct;
3094 char_u fileBuf[MAXPATHL];
3095 char_u *initdirp = NULL;
3096 char_u *filterp;
3097 char_u *p;
3098
3099# if defined(FEAT_MBYTE) && defined(WIN3264)
3100 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
3101 return gui_mch_browseW(saving, title, dflt, ext, initdir, filter);
3102# endif
3103
3104 if (dflt == NULL)
3105 fileBuf[0] = NUL;
3106 else
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +00003107 vim_strncpy(fileBuf, dflt, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108
3109 /* Convert the filter to Windows format. */
3110 filterp = convert_filter(filter);
3111
3112 memset(&fileStruct, 0, sizeof(OPENFILENAME));
3113#ifdef OPENFILENAME_SIZE_VERSION_400
3114 /* be compatible with Windows NT 4.0 */
3115 fileStruct.lStructSize = sizeof(OPENFILENAME_SIZE_VERSION_400);
3116#else
3117 fileStruct.lStructSize = sizeof(fileStruct);
3118#endif
3119
3120 fileStruct.lpstrTitle = title;
3121 fileStruct.lpstrDefExt = ext;
3122
3123 fileStruct.lpstrFile = fileBuf;
3124 fileStruct.nMaxFile = MAXPATHL;
3125 fileStruct.lpstrFilter = filterp;
3126 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3127 /* has an initial dir been specified? */
3128 if (initdir != NULL && *initdir != NUL)
3129 {
3130 /* Must have backslashes here, no matter what 'shellslash' says */
3131 initdirp = vim_strsave(initdir);
3132 if (initdirp != NULL)
3133 for (p = initdirp; *p != NUL; ++p)
3134 if (*p == '/')
3135 *p = '\\';
3136 fileStruct.lpstrInitialDir = initdirp;
3137 }
3138
3139 /*
3140 * TODO: Allow selection of multiple files. Needs another arg to this
3141 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3142 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3143 * files that don't exist yet, so I haven't put it in. What about
3144 * OFN_PATHMUSTEXIST?
3145 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3146 */
3147 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
3148#ifdef FEAT_SHORTCUT
3149 if (curbuf->b_p_bin)
3150 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
3151#endif
3152 if (saving)
3153 {
3154 if (!GetSaveFileName(&fileStruct))
3155 return NULL;
3156 }
3157 else
3158 {
3159 if (!GetOpenFileName(&fileStruct))
3160 return NULL;
3161 }
3162
3163 vim_free(filterp);
3164 vim_free(initdirp);
3165
3166 /* Give focus back to main window (when using MDI). */
3167 SetFocus(s_hwnd);
3168
3169 /* Shorten the file name if possible */
3170 mch_dirname(IObuff, IOSIZE);
3171 p = shorten_fname((char_u *)fileBuf, IObuff);
3172 if (p == NULL)
3173 p = (char_u *)fileBuf;
3174 return vim_strsave(p);
3175}
3176#endif /* FEAT_BROWSE */
3177
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003178/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 static void
3180_OnDropFiles(
3181 HWND hwnd,
3182 HDROP hDrop)
3183{
3184#ifdef FEAT_WINDOWS
3185#ifdef WIN3264
3186# define BUFPATHLEN _MAX_PATH
3187# define DRAGQVAL 0xFFFFFFFF
3188#else
3189# define BUFPATHLEN MAXPATHL
3190# define DRAGQVAL 0xFFFF
3191#endif
3192#ifdef FEAT_MBYTE
3193 WCHAR wszFile[BUFPATHLEN];
3194#endif
3195 char szFile[BUFPATHLEN];
3196 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3197 UINT i;
3198 char_u **fnames;
3199 POINT pt;
3200 int_u modifiers = 0;
3201
3202 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3203
3204 /* Obtain dropped position */
3205 DragQueryPoint(hDrop, &pt);
3206 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3207
3208# ifdef FEAT_VISUAL
3209 reset_VIsual();
3210# endif
3211
3212 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3213
3214 if (fnames != NULL)
3215 for (i = 0; i < cFiles; ++i)
3216 {
3217#ifdef FEAT_MBYTE
3218 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3219 fnames[i] = ucs2_to_enc(wszFile, NULL);
3220 else
3221#endif
3222 {
3223 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3224 fnames[i] = vim_strsave(szFile);
3225 }
3226 }
3227
3228 DragFinish(hDrop);
3229
3230 if (fnames != NULL)
3231 {
3232 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3233 modifiers |= MOUSE_SHIFT;
3234 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3235 modifiers |= MOUSE_CTRL;
3236 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3237 modifiers |= MOUSE_ALT;
3238
3239 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3240
3241 s_need_activate = TRUE;
3242 }
3243#endif
3244}
3245
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003246/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 static int
3248_OnScroll(
3249 HWND hwnd,
3250 HWND hwndCtl,
3251 UINT code,
3252 int pos)
3253{
3254 static UINT prev_code = 0; /* code of previous call */
3255 scrollbar_T *sb, *sb_info;
3256 long val;
3257 int dragging = FALSE;
3258 int dont_scroll_save = dont_scroll;
3259#ifndef WIN3264
3260 int nPos;
3261#else
3262 SCROLLINFO si;
3263
3264 si.cbSize = sizeof(si);
3265 si.fMask = SIF_POS;
3266#endif
3267
3268 sb = gui_mswin_find_scrollbar(hwndCtl);
3269 if (sb == NULL)
3270 return 0;
3271
3272 if (sb->wp != NULL) /* Left or right scrollbar */
3273 {
3274 /*
3275 * Careful: need to get scrollbar info out of first (left) scrollbar
3276 * for window, but keep real scrollbar too because we must pass it to
3277 * gui_drag_scrollbar().
3278 */
3279 sb_info = &sb->wp->w_scrollbars[0];
3280 }
3281 else /* Bottom scrollbar */
3282 sb_info = sb;
3283 val = sb_info->value;
3284
3285 switch (code)
3286 {
3287 case SB_THUMBTRACK:
3288 val = pos;
3289 dragging = TRUE;
3290 if (sb->scroll_shift > 0)
3291 val <<= sb->scroll_shift;
3292 break;
3293 case SB_LINEDOWN:
3294 val++;
3295 break;
3296 case SB_LINEUP:
3297 val--;
3298 break;
3299 case SB_PAGEDOWN:
3300 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3301 break;
3302 case SB_PAGEUP:
3303 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3304 break;
3305 case SB_TOP:
3306 val = 0;
3307 break;
3308 case SB_BOTTOM:
3309 val = sb_info->max;
3310 break;
3311 case SB_ENDSCROLL:
3312 if (prev_code == SB_THUMBTRACK)
3313 {
3314 /*
3315 * "pos" only gives us 16-bit data. In case of large file,
3316 * use GetScrollPos() which returns 32-bit. Unfortunately it
3317 * is not valid while the scrollbar is being dragged.
3318 */
3319 val = GetScrollPos(hwndCtl, SB_CTL);
3320 if (sb->scroll_shift > 0)
3321 val <<= sb->scroll_shift;
3322 }
3323 break;
3324
3325 default:
3326 /* TRACE("Unknown scrollbar event %d\n", code); */
3327 return 0;
3328 }
3329 prev_code = code;
3330
3331#ifdef WIN3264
3332 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3333 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
3334#else
3335 nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3336 SetScrollPos(hwndCtl, SB_CTL, nPos, TRUE);
3337#endif
3338
3339 /*
3340 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3341 */
3342 if (sb->wp != NULL)
3343 {
3344 scrollbar_T *sba = sb->wp->w_scrollbars;
3345 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3346
3347#ifdef WIN3264
3348 SetScrollInfo(id, SB_CTL, &si, TRUE);
3349#else
3350 SetScrollPos(id, SB_CTL, nPos, TRUE);
3351#endif
3352 }
3353
3354 /* Don't let us be interrupted here by another message. */
3355 s_busy_processing = TRUE;
3356
3357 /* When "allow_scrollbar" is FALSE still need to remember the new
3358 * position, but don't actually scroll by setting "dont_scroll". */
3359 dont_scroll = !allow_scrollbar;
3360
3361 gui_drag_scrollbar(sb, val, dragging);
3362
3363 s_busy_processing = FALSE;
3364 dont_scroll = dont_scroll_save;
3365
3366 return 0;
3367}
3368
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003369
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370/*
3371 * Get command line arguments.
3372 * Use "prog" as the name of the program and "cmdline" as the arguments.
3373 * Copy the arguments to allocated memory.
3374 * Return the number of arguments (including program name).
3375 * Return pointers to the arguments in "argvp".
3376 * Return pointer to buffer in "tofree".
3377 * Returns zero when out of memory.
3378 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003379/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 int
3381get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
3382{
3383 int i;
3384 char *p;
3385 char *progp;
3386 char *pnew = NULL;
3387 char *newcmdline;
3388 int inquote;
3389 int argc;
3390 char **argv = NULL;
3391 int round;
3392
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003393#ifdef FEAT_MBYTE
3394 /* Try using the Unicode version first, it takes care of conversion when
3395 * 'encoding' is changed. */
3396 argc = get_cmd_argsW(&argv);
3397 if (argc != 0)
3398 goto done;
3399#endif
3400
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 /* Handle the program name. Remove the ".exe" extension, and find the 1st
3402 * non-space. */
3403 p = strrchr(prog, '.');
3404 if (p != NULL)
3405 *p = NUL;
3406 for (progp = prog; *progp == ' '; ++progp)
3407 ;
3408
3409 /* The command line is copied to allocated memory, so that we can change
3410 * it. Add the size of the string, the separating NUL and a terminating
3411 * NUL. */
3412 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
3413 if (newcmdline == NULL)
3414 return 0;
3415
3416 /*
3417 * First round: count the number of arguments ("pnew" == NULL).
3418 * Second round: produce the arguments.
3419 */
3420 for (round = 1; round <= 2; ++round)
3421 {
3422 /* First argument is the program name. */
3423 if (pnew != NULL)
3424 {
3425 argv[0] = pnew;
3426 strcpy(pnew, progp);
3427 pnew += strlen(pnew);
3428 *pnew++ = NUL;
3429 }
3430
3431 /*
3432 * Isolate each argument and put it in argv[].
3433 */
3434 p = cmdline;
3435 argc = 1;
3436 while (*p != NUL)
3437 {
3438 inquote = FALSE;
3439 if (pnew != NULL)
3440 argv[argc] = pnew;
3441 ++argc;
3442 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
3443 {
3444 /* Backslashes are only special when followed by a double
3445 * quote. */
3446 i = strspn(p, "\\");
3447 if (p[i] == '"')
3448 {
3449 /* Halve the number of backslashes. */
3450 if (i > 1 && pnew != NULL)
3451 {
3452 memset(pnew, '\\', i / 2);
3453 pnew += i / 2;
3454 }
3455
3456 /* Even nr of backslashes toggles quoting, uneven copies
3457 * the double quote. */
3458 if ((i & 1) == 0)
3459 inquote = !inquote;
3460 else if (pnew != NULL)
3461 *pnew++ = '"';
3462 p += i + 1;
3463 }
3464 else if (i > 0)
3465 {
3466 /* Copy span of backslashes unmodified. */
3467 if (pnew != NULL)
3468 {
3469 memset(pnew, '\\', i);
3470 pnew += i;
3471 }
3472 p += i;
3473 }
3474 else
3475 {
3476 if (pnew != NULL)
3477 *pnew++ = *p;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00003478#ifdef FEAT_MBYTE
3479 /* Can't use mb_* functions, because 'encoding' is not
3480 * initialized yet here. */
3481 if (IsDBCSLeadByte(*p))
3482 {
3483 ++p;
3484 if (pnew != NULL)
3485 *pnew++ = *p;
3486 }
3487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 ++p;
3489 }
3490 }
3491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003492 if (pnew != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 *pnew++ = NUL;
3494 while (*p == ' ' || *p == '\t')
3495 ++p; /* advance until a non-space */
3496 }
3497
3498 if (round == 1)
3499 {
3500 argv = (char **)malloc((argc + 1) * sizeof(char *));
3501 if (argv == NULL )
Bram Moolenaare6b165e2005-06-30 21:56:01 +00003502 {
3503 vim_free(newcmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 return 0; /* malloc error */
Bram Moolenaare6b165e2005-06-30 21:56:01 +00003505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 pnew = newcmdline;
3507 }
3508 }
3509
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003510done:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003512 argv[argc] = NULL; /* NULL-terminated list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 *argvp = argv;
3514 return argc;
3515}