blob: 9a0247f4da36a1ffc96bd0ad022a265ac90220b3 [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 }
1116
1117}
1118
1119
1120/*
1121 * Scrollbar stuff:
1122 */
1123
1124 void
1125gui_mch_enable_scrollbar(
1126 scrollbar_T *sb,
1127 int flag)
1128{
1129 ShowScrollBar(sb->id, SB_CTL, flag);
1130
1131 /* TODO: When the window is maximized, the size of the window stays the
1132 * same, thus the size of the text area changes. On Win98 it's OK, on Win
1133 * NT 4.0 it's not... */
1134}
1135
1136 void
1137gui_mch_set_scrollbar_pos(
1138 scrollbar_T *sb,
1139 int x,
1140 int y,
1141 int w,
1142 int h)
1143{
1144 SetWindowPos(sb->id, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1145}
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
1197 + p_linespace
1198#endif
1199 ;
1200
1201 SelectFont(hdc, hfntOld);
1202
1203 ReleaseDC(hwnd, hdc);
1204}
1205
1206 int
1207gui_mch_adjust_charsize(void)
1208{
1209 GetFontSize(gui.norm_font);
1210 return OK;
1211}
1212
1213 static GuiFont
1214get_font_handle(LOGFONT *lf)
1215{
1216 HFONT font = NULL;
1217
1218 /* Load the font */
1219 font = CreateFontIndirect(lf);
1220
1221 if (font == NULL)
1222 return NOFONT;
1223
1224 return (GuiFont)font;
1225}
1226
1227 static int
1228pixels_to_points(int pixels, int vertical)
1229{
1230 int points;
1231 HWND hwnd;
1232 HDC hdc;
1233
1234 hwnd = GetDesktopWindow();
1235 hdc = GetWindowDC(hwnd);
1236
1237 points = MulDiv(pixels, 72,
1238 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1239
1240 ReleaseDC(hwnd, hdc);
1241
1242 return points;
1243}
1244
1245 GuiFont
1246gui_mch_get_font(
1247 char_u *name,
1248 int giveErrorIfMissing)
1249{
1250 LOGFONT lf;
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001251 GuiFont font = NOFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001253 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1254 font = get_font_handle(&lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 if (font == NOFONT && giveErrorIfMissing)
1256 EMSG2(_(e_font), name);
1257 return font;
1258}
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001259
Bram Moolenaardfccaf02004-12-31 20:56:11 +00001260#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001261/*
1262 * Return the name of font "font" in allocated memory.
1263 * Don't know how to get the actual name, thus use the provided name.
1264 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001265/*ARGSUSED*/
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001266 char_u *
1267gui_mch_get_fontname(font, name)
1268 GuiFont font;
1269 char_u *name;
1270{
1271 if (name == NULL)
1272 return NULL;
1273 return vim_strsave(name);
1274}
Bram Moolenaardfccaf02004-12-31 20:56:11 +00001275#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001276
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 void
1278gui_mch_free_font(GuiFont font)
1279{
1280 if (font)
1281 DeleteObject((HFONT)font);
1282}
1283
1284 static int
1285hex_digit(int c)
1286{
1287 if (VIM_ISDIGIT(c))
1288 return c - '0';
1289 c = TOLOWER_ASC(c);
1290 if (c >= 'a' && c <= 'f')
1291 return c - 'a' + 10;
1292 return -1000;
1293}
1294/*
1295 * Return the Pixel value (color) for the given color name.
1296 * Return INVALCOLOR for error.
1297 */
1298 guicolor_T
1299gui_mch_get_color(char_u *name)
1300{
1301 typedef struct guicolor_tTable
1302 {
1303 char *name;
1304 COLORREF color;
1305 } guicolor_tTable;
1306
1307 static guicolor_tTable table[] =
1308 {
1309 {"Black", RGB(0x00, 0x00, 0x00)},
1310 {"DarkGray", RGB(0x80, 0x80, 0x80)},
1311 {"DarkGrey", RGB(0x80, 0x80, 0x80)},
1312 {"Gray", RGB(0xC0, 0xC0, 0xC0)},
1313 {"Grey", RGB(0xC0, 0xC0, 0xC0)},
1314 {"LightGray", RGB(0xE0, 0xE0, 0xE0)},
1315 {"LightGrey", RGB(0xE0, 0xE0, 0xE0)},
1316 {"White", RGB(0xFF, 0xFF, 0xFF)},
1317 {"DarkRed", RGB(0x80, 0x00, 0x00)},
1318 {"Red", RGB(0xFF, 0x00, 0x00)},
1319 {"LightRed", RGB(0xFF, 0xA0, 0xA0)},
1320 {"DarkBlue", RGB(0x00, 0x00, 0x80)},
1321 {"Blue", RGB(0x00, 0x00, 0xFF)},
1322 {"LightBlue", RGB(0xA0, 0xA0, 0xFF)},
1323 {"DarkGreen", RGB(0x00, 0x80, 0x00)},
1324 {"Green", RGB(0x00, 0xFF, 0x00)},
1325 {"LightGreen", RGB(0xA0, 0xFF, 0xA0)},
1326 {"DarkCyan", RGB(0x00, 0x80, 0x80)},
1327 {"Cyan", RGB(0x00, 0xFF, 0xFF)},
1328 {"LightCyan", RGB(0xA0, 0xFF, 0xFF)},
1329 {"DarkMagenta", RGB(0x80, 0x00, 0x80)},
1330 {"Magenta", RGB(0xFF, 0x00, 0xFF)},
1331 {"LightMagenta", RGB(0xFF, 0xA0, 0xFF)},
1332 {"Brown", RGB(0x80, 0x40, 0x40)},
1333 {"Yellow", RGB(0xFF, 0xFF, 0x00)},
1334 {"LightYellow", RGB(0xFF, 0xFF, 0xA0)},
1335 {"DarkYellow", RGB(0xBB, 0xBB, 0x00)},
1336 {"SeaGreen", RGB(0x2E, 0x8B, 0x57)},
1337 {"Orange", RGB(0xFF, 0xA5, 0x00)},
1338 {"Purple", RGB(0xA0, 0x20, 0xF0)},
1339 {"SlateBlue", RGB(0x6A, 0x5A, 0xCD)},
1340 {"Violet", RGB(0xEE, 0x82, 0xEE)},
1341 };
1342
1343 typedef struct SysColorTable
1344 {
1345 char *name;
1346 int color;
1347 } SysColorTable;
1348
1349 static SysColorTable sys_table[] =
1350 {
1351#ifdef WIN3264
1352 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1353 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
1354#ifndef __MINGW32__
1355 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1356#endif
1357 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1358 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1359 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1360 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1361 {"SYS_DESKTOP", COLOR_DESKTOP},
1362 {"SYS_INFOBK", COLOR_INFOBK},
1363 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1364 {"SYS_3DFACE", COLOR_3DFACE},
1365#endif
1366 {"SYS_BTNFACE", COLOR_BTNFACE},
1367 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1368 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1369 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1370 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1371 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1372 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1373 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1374 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1375 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1376 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1377 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1378 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1379 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1380 {"SYS_MENU", COLOR_MENU},
1381 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1382 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1383 {"SYS_WINDOW", COLOR_WINDOW},
1384 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1385 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1386 };
1387
1388 int r, g, b;
1389 int i;
1390
1391 if (name[0] == '#' && strlen(name) == 7)
1392 {
1393 /* Name is in "#rrggbb" format */
1394 r = hex_digit(name[1]) * 16 + hex_digit(name[2]);
1395 g = hex_digit(name[3]) * 16 + hex_digit(name[4]);
1396 b = hex_digit(name[5]) * 16 + hex_digit(name[6]);
1397 if (r < 0 || g < 0 || b < 0)
1398 return INVALCOLOR;
1399 return RGB(r, g, b);
1400 }
1401 else
1402 {
1403 /* Check if the name is one of the colors we know */
1404 for (i = 0; i < sizeof(table) / sizeof(table[0]); i++)
1405 if (STRICMP(name, table[i].name) == 0)
1406 return table[i].color;
1407 }
1408
1409 /*
1410 * Try to look up a system colour.
1411 */
1412 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1413 if (STRICMP(name, sys_table[i].name) == 0)
1414 return GetSysColor(sys_table[i].color);
1415
1416 /*
1417 * Last attempt. Look in the file "$VIMRUNTIME/rgb.txt".
1418 */
1419 {
1420#define LINE_LEN 100
1421 FILE *fd;
1422 char line[LINE_LEN];
1423 char_u *fname;
1424
1425 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
1426 if (fname == NULL)
1427 return INVALCOLOR;
1428
1429 fd = fopen((char *)fname, "rt");
1430 vim_free(fname);
1431 if (fd == NULL)
1432 return INVALCOLOR;
1433
1434 while (!feof(fd))
1435 {
1436 int len;
1437 int pos;
1438 char *color;
1439
1440 fgets(line, LINE_LEN, fd);
1441 len = (int)STRLEN(line);
1442
1443 if (len <= 1 || line[len-1] != '\n')
1444 continue;
1445
1446 line[len-1] = '\0';
1447
1448 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
1449 if (i != 3)
1450 continue;
1451
1452 color = line + pos;
1453
1454 if (STRICMP(color, name) == 0)
1455 {
1456 fclose(fd);
1457 return (guicolor_T) RGB(r, g, b);
1458 }
1459 }
1460
1461 fclose(fd);
1462 }
1463
1464 return INVALCOLOR;
1465}
1466/*
1467 * Return OK if the key with the termcap name "name" is supported.
1468 */
1469 int
1470gui_mch_haskey(char_u *name)
1471{
1472 int i;
1473
1474 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1475 if (name[0] == special_keys[i].vim_code0 &&
1476 name[1] == special_keys[i].vim_code1)
1477 return OK;
1478 return FAIL;
1479}
1480
1481 void
1482gui_mch_beep(void)
1483{
1484 MessageBeep(MB_OK);
1485}
1486/*
1487 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1488 */
1489 void
1490gui_mch_invert_rectangle(
1491 int r,
1492 int c,
1493 int nr,
1494 int nc)
1495{
1496 RECT rc;
1497
1498 /*
1499 * Note: InvertRect() excludes right and bottom of rectangle.
1500 */
1501 rc.left = FILL_X(c);
1502 rc.top = FILL_Y(r);
1503 rc.right = rc.left + nc * gui.char_width;
1504 rc.bottom = rc.top + nr * gui.char_height;
1505 InvertRect(s_hdc, &rc);
1506}
1507
1508/*
1509 * Iconify the GUI window.
1510 */
1511 void
1512gui_mch_iconify(void)
1513{
1514 ShowWindow(s_hwnd, SW_MINIMIZE);
1515}
1516
1517/*
1518 * Draw a cursor without focus.
1519 */
1520 void
1521gui_mch_draw_hollow_cursor(guicolor_T color)
1522{
1523 HBRUSH hbr;
1524 RECT rc;
1525
1526 /*
1527 * Note: FrameRect() excludes right and bottom of rectangle.
1528 */
1529 rc.left = FILL_X(gui.col);
1530 rc.top = FILL_Y(gui.row);
1531 rc.right = rc.left + gui.char_width;
1532#ifdef FEAT_MBYTE
1533 if (mb_lefthalve(gui.row, gui.col))
1534 rc.right += gui.char_width;
1535#endif
1536 rc.bottom = rc.top + gui.char_height;
1537 hbr = CreateSolidBrush(color);
1538 FrameRect(s_hdc, &rc, hbr);
1539 DeleteBrush(hbr);
1540}
1541/*
1542 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1543 * color "color".
1544 */
1545 void
1546gui_mch_draw_part_cursor(
1547 int w,
1548 int h,
1549 guicolor_T color)
1550{
1551 HBRUSH hbr;
1552 RECT rc;
1553
1554 /*
1555 * Note: FillRect() excludes right and bottom of rectangle.
1556 */
1557 rc.left =
1558#ifdef FEAT_RIGHTLEFT
1559 /* vertical line should be on the right of current point */
1560 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1561#endif
1562 FILL_X(gui.col);
1563 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1564 rc.right = rc.left + w;
1565 rc.bottom = rc.top + h;
1566 hbr = CreateSolidBrush(color);
1567 FillRect(s_hdc, &rc, hbr);
1568 DeleteBrush(hbr);
1569}
1570
1571/*
1572 * Process a single Windows message.
1573 * If one is not available we hang until one is.
1574 */
1575 static void
1576process_message(void)
1577{
1578 MSG msg;
1579 UINT vk = 0; /* Virtual key */
1580 char_u string[40];
1581 int i;
1582 int modifiers = 0;
1583 int key;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001584#ifdef FEAT_MENU
1585 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
1588 GetMessage(&msg, NULL, 0, 0);
1589
1590#ifdef FEAT_OLE
1591 /* Look after OLE Automation commands */
1592 if (msg.message == WM_OLE)
1593 {
1594 char_u *str = (char_u *)msg.lParam;
1595 add_to_input_buf(str, (int)STRLEN(str));
1596 vim_free(str);
1597 return;
1598 }
1599#endif
1600
1601#ifdef FEAT_NETBEANS_INTG
1602 if (msg.message == WM_NETBEANS)
1603 {
1604 messageFromNetbeansW32();
1605 return;
1606 }
1607#endif
1608
1609#ifdef FEAT_SNIFF
1610 if (sniff_request_waiting && want_sniff_request)
1611 {
1612 static char_u bytes[3] = {CSI, (char_u)KS_EXTRA, (char_u)KE_SNIFF};
1613 add_to_input_buf(bytes, 3); /* K_SNIFF */
1614 sniff_request_waiting = 0;
1615 want_sniff_request = 0;
1616 /* request is handled in normal.c */
1617 }
1618 if (msg.message == WM_USER)
1619 return;
1620#endif
1621
1622#ifdef MSWIN_FIND_REPLACE
1623 /* Don't process messages used by the dialog */
1624 if (s_findrep_hwnd != NULL && IsDialogMessage(s_findrep_hwnd, &msg))
1625 {
1626 HandleMouseHide(msg.message, msg.lParam);
1627 return;
1628 }
1629#endif
1630
1631 /*
1632 * Check if it's a special key that we recognise. If not, call
1633 * TranslateMessage().
1634 */
1635 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1636 {
1637 vk = (int) msg.wParam;
1638 /* handle key after dead key, but ignore shift, alt and control */
1639 if (dead_key && vk != VK_SHIFT && vk != VK_MENU && vk != VK_CONTROL)
1640 {
1641 dead_key = 0;
1642 /* handle non-alphabetic keys (ones that hopefully cannot generate
1643 * umlaut-characters), unless when control is down */
1644 if (vk < 'A' || vk > 'Z' || (GetKeyState(VK_CONTROL) & 0x8000))
1645 {
1646 MSG dm;
1647
1648 dm.message = msg.message;
1649 dm.hwnd = msg.hwnd;
1650 dm.wParam = VK_SPACE;
1651 MyTranslateMessage(&dm); /* generate dead character */
1652 if (vk != VK_SPACE) /* and send current character once more */
1653 PostMessage(msg.hwnd, msg.message, msg.wParam, msg.lParam);
1654 return;
1655 }
1656 }
1657
1658 /* Check for CTRL-BREAK */
1659 if (vk == VK_CANCEL)
1660 {
1661 trash_input_buf();
1662 got_int = TRUE;
1663 string[0] = Ctrl_C;
1664 add_to_input_buf(string, 1);
1665 }
1666
1667 for (i = 0; special_keys[i].key_sym != 0; i++)
1668 {
1669 /* ignore VK_SPACE when ALT key pressed: system menu */
1670 if (special_keys[i].key_sym == vk
1671 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1672 {
1673#ifdef FEAT_MENU
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001674 /* Check for <F10>: Windows selects the menu. When <F10> is
1675 * mapped we want to use the mapping instead. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 if (vk == VK_F10
1677 && gui.menu_is_active
Bram Moolenaarc1087e62005-05-20 21:22:17 +00001678 && check_map(k10, State, FALSE, TRUE) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 break;
1680#endif
1681 if (GetKeyState(VK_SHIFT) & 0x8000)
1682 modifiers |= MOD_MASK_SHIFT;
1683 /*
1684 * Don't use caps-lock as shift, because these are special keys
1685 * being considered here, and we only want letters to get
1686 * shifted -- webb
1687 */
1688 /*
1689 if (GetKeyState(VK_CAPITAL) & 0x0001)
1690 modifiers ^= MOD_MASK_SHIFT;
1691 */
1692 if (GetKeyState(VK_CONTROL) & 0x8000)
1693 modifiers |= MOD_MASK_CTRL;
1694 if (GetKeyState(VK_MENU) & 0x8000)
1695 modifiers |= MOD_MASK_ALT;
1696
1697 if (special_keys[i].vim_code1 == NUL)
1698 key = special_keys[i].vim_code0;
1699 else
1700 key = TO_SPECIAL(special_keys[i].vim_code0,
1701 special_keys[i].vim_code1);
1702 key = simplify_key(key, &modifiers);
1703 if (key == CSI)
1704 key = K_CSI;
1705
1706 if (modifiers)
1707 {
1708 string[0] = CSI;
1709 string[1] = KS_MODIFIER;
1710 string[2] = modifiers;
1711 add_to_input_buf(string, 3);
1712 }
1713
1714 if (IS_SPECIAL(key))
1715 {
1716 string[0] = CSI;
1717 string[1] = K_SECOND(key);
1718 string[2] = K_THIRD(key);
1719 add_to_input_buf(string, 3);
1720 }
1721 else
1722 {
1723 int len;
1724
1725 /* Handle "key" as a Unicode character. */
1726 len = char_to_string(key, string, 40);
1727 add_to_input_buf(string, len);
1728 }
1729 break;
1730 }
1731 }
1732 if (special_keys[i].key_sym == 0)
1733 {
1734 /* Some keys need C-S- where they should only need C-.
1735 * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1736 * system startup (Helmut Stiegler, 2003 Oct 3). */
1737 if (vk != 0xff
1738 && (GetKeyState(VK_CONTROL) & 0x8000)
1739 && !(GetKeyState(VK_SHIFT) & 0x8000)
1740 && !(GetKeyState(VK_MENU) & 0x8000))
1741 {
1742 /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */
1743 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1744 {
1745 string[0] = Ctrl_HAT;
1746 add_to_input_buf(string, 1);
1747 }
1748 /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */
1749 else if (vk == 0xBD) /* QWERTY for CTRL-'-' */
1750 {
1751 string[0] = Ctrl__;
1752 add_to_input_buf(string, 1);
1753 }
1754 /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */
1755 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
1756 {
1757 string[0] = Ctrl_AT;
1758 add_to_input_buf(string, 1);
1759 }
1760 else
1761 MyTranslateMessage(&msg);
1762 }
1763 else
1764 MyTranslateMessage(&msg);
1765 }
1766 }
1767#ifdef FEAT_MBYTE_IME
1768 else if (msg.message == WM_IME_NOTIFY)
1769 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
1770 else if (msg.message == WM_KEYUP && im_get_status())
1771 /* added for non-MS IME (Yasuhiro Matsumoto) */
1772 MyTranslateMessage(&msg);
1773#endif
1774#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
1775/* GIME_TEST */
1776 else if (msg.message == WM_IME_STARTCOMPOSITION)
1777 {
1778 POINT point;
1779
1780 global_ime_set_font(&norm_logfont);
1781 point.x = FILL_X(gui.col);
1782 point.y = FILL_Y(gui.row);
1783 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
1784 global_ime_set_position(&point);
1785 }
1786#endif
1787
1788#ifdef FEAT_MENU
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001789 /* Check for <F10>: Default effect is to select the menu. When <F10> is
1790 * mapped we need to stop it here to avoid strange effects (e.g., for the
1791 * key-up event) */
Bram Moolenaarc1087e62005-05-20 21:22:17 +00001792 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#endif
1794 DispatchMessage(&msg);
1795}
1796
1797/*
1798 * Catch up with any queued events. This may put keyboard input into the
1799 * input buffer, call resize call-backs, trigger timers etc. If there is
1800 * nothing in the event queue (& no timers pending), then we return
1801 * immediately.
1802 */
1803 void
1804gui_mch_update(void)
1805{
1806 MSG msg;
1807
1808 if (!s_busy_processing)
1809 while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
1810 && !vim_is_input_buf_full())
1811 process_message();
1812}
1813
1814/*
1815 * GUI input routine called by gui_wait_for_chars(). Waits for a character
1816 * from the keyboard.
1817 * wtime == -1 Wait forever.
1818 * wtime == 0 This should never happen.
1819 * wtime > 0 Wait wtime milliseconds for a character.
1820 * Returns OK if a character was found to be available within the given time,
1821 * or FAIL otherwise.
1822 */
1823 int
1824gui_mch_wait_for_chars(int wtime)
1825{
1826 MSG msg;
1827 int focus;
1828
1829 s_timed_out = FALSE;
1830
1831 if (wtime > 0)
1832 {
1833 /* Don't do anything while processing a (scroll) message. */
1834 if (s_busy_processing)
1835 return FAIL;
1836 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)wtime,
1837 (TIMERPROC)_OnTimer);
1838 }
1839
1840 allow_scrollbar = TRUE;
1841
1842 focus = gui.in_focus;
1843 while (!s_timed_out)
1844 {
1845 /* Stop or start blinking when focus changes */
1846 if (gui.in_focus != focus)
1847 {
1848 if (gui.in_focus)
1849 gui_mch_start_blink();
1850 else
1851 gui_mch_stop_blink();
1852 focus = gui.in_focus;
1853 }
1854
1855 if (s_need_activate)
1856 {
1857#ifdef WIN32
1858 (void)SetForegroundWindow(s_hwnd);
1859#else
1860 (void)SetActiveWindow(s_hwnd);
1861#endif
1862 s_need_activate = FALSE;
1863 }
1864
1865 /*
1866 * Don't use gui_mch_update() because then we will spin-lock until a
1867 * char arrives, instead we use GetMessage() to hang until an
1868 * event arrives. No need to check for input_buf_full because we are
1869 * returning as soon as it contains a single char -- webb
1870 */
1871 process_message();
1872
1873 if (input_available())
1874 {
1875 if (s_wait_timer != 0 && !s_timed_out)
1876 {
1877 KillTimer(NULL, s_wait_timer);
1878
1879 /* Eat spurious WM_TIMER messages */
1880 while (PeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
1881 ;
1882 s_wait_timer = 0;
1883 }
1884 allow_scrollbar = FALSE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001885
1886 /* Clear pending mouse button, the release event may have been
1887 * taken by the dialog window. */
1888 s_button_pending = -1;
1889
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 return OK;
1891 }
1892 }
1893 allow_scrollbar = FALSE;
1894 return FAIL;
1895}
1896
1897/*
1898 * Clear a rectangular region of the screen from text pos (row1, col1) to
1899 * (row2, col2) inclusive.
1900 */
1901 void
1902gui_mch_clear_block(
1903 int row1,
1904 int col1,
1905 int row2,
1906 int col2)
1907{
1908 RECT rc;
1909
1910 /*
1911 * Clear one extra pixel at the far right, for when bold characters have
1912 * spilled over to the window border.
1913 * Note: FillRect() excludes right and bottom of rectangle.
1914 */
1915 rc.left = FILL_X(col1);
1916 rc.top = FILL_Y(row1);
1917 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
1918 rc.bottom = FILL_Y(row2 + 1);
1919 clear_rect(&rc);
1920}
1921
1922/*
1923 * Clear the whole text window.
1924 */
1925 void
1926gui_mch_clear_all(void)
1927{
1928 RECT rc;
1929
1930 rc.left = 0;
1931 rc.top = 0;
1932 rc.right = Columns * gui.char_width + 2 * gui.border_width;
1933 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
1934 clear_rect(&rc);
1935}
1936/*
1937 * Menu stuff.
1938 */
1939
1940 void
1941gui_mch_enable_menu(int flag)
1942{
1943#ifdef FEAT_MENU
1944 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
1945#endif
1946}
1947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001948/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 void
1950gui_mch_set_menu_pos(
1951 int x,
1952 int y,
1953 int w,
1954 int h)
1955{
1956 /* It will be in the right place anyway */
1957}
1958
1959#if defined(FEAT_MENU) || defined(PROTO)
1960/*
1961 * Make menu item hidden or not hidden
1962 */
1963 void
1964gui_mch_menu_hidden(
1965 vimmenu_T *menu,
1966 int hidden)
1967{
1968 /*
1969 * This doesn't do what we want. Hmm, just grey the menu items for now.
1970 */
1971 /*
1972 if (hidden)
1973 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
1974 else
1975 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
1976 */
1977 gui_mch_menu_grey(menu, hidden);
1978}
1979
1980/*
1981 * This is called after setting all the menus to grey/hidden or not.
1982 */
1983 void
1984gui_mch_draw_menubar(void)
1985{
1986 DrawMenuBar(s_hwnd);
1987}
1988#endif /*FEAT_MENU*/
1989
1990#ifndef PROTO
1991void
1992#ifdef VIMDLL
1993_export
1994#endif
1995_cdecl
1996SaveInst(HINSTANCE hInst)
1997{
1998 s_hinst = hInst;
1999}
2000#endif
2001
2002/*
2003 * Return the RGB value of a pixel as a long.
2004 */
2005 long_u
2006gui_mch_get_rgb(guicolor_T pixel)
2007{
2008 return (GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2009 + GetBValue(pixel);
2010}
2011
2012#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2013/* Convert pixels in X to dialog units */
2014 static WORD
2015PixelToDialogX(int numPixels)
2016{
2017 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2018}
2019
2020/* Convert pixels in Y to dialog units */
2021 static WORD
2022PixelToDialogY(int numPixels)
2023{
2024 return (WORD)((numPixels * 8) / s_dlgfntheight);
2025}
2026
2027/* Return the width in pixels of the given text in the given DC. */
2028 static int
2029GetTextWidth(HDC hdc, char_u *str, int len)
2030{
2031 SIZE size;
2032
2033 GetTextExtentPoint(hdc, str, len, &size);
2034 return size.cx;
2035}
2036
2037#ifdef FEAT_MBYTE
2038/*
2039 * Return the width in pixels of the given text in the given DC, taking care
2040 * of 'encoding' to active codepage conversion.
2041 */
2042 static int
2043GetTextWidthEnc(HDC hdc, char_u *str, int len)
2044{
2045 SIZE size;
2046 WCHAR *wstr;
2047 int n;
2048 int wlen = len;
2049
2050 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2051 {
2052 /* 'encoding' differs from active codepage: convert text and use wide
2053 * function */
2054 wstr = enc_to_ucs2(str, &wlen);
2055 if (wstr != NULL)
2056 {
2057 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2058 vim_free(wstr);
2059 if (n)
2060 return size.cx;
2061 }
2062 }
2063
2064 return GetTextWidth(hdc, str, len);
2065}
2066#else
2067# define GetTextWidthEnc(h, s, l) GetTextWidth((h), (s), (l))
2068#endif
2069
2070/*
2071 * A quick little routine that will center one window over another, handy for
2072 * dialog boxes. Taken from the Win32SDK samples.
2073 */
2074 static BOOL
2075CenterWindow(
2076 HWND hwndChild,
2077 HWND hwndParent)
2078{
2079 RECT rChild, rParent;
2080 int wChild, hChild, wParent, hParent;
2081 int wScreen, hScreen, xNew, yNew;
2082 HDC hdc;
2083
2084 GetWindowRect(hwndChild, &rChild);
2085 wChild = rChild.right - rChild.left;
2086 hChild = rChild.bottom - rChild.top;
2087
2088 /* If Vim is minimized put the window in the middle of the screen. */
2089 if (IsMinimized(hwndParent))
2090 {
2091#ifdef WIN16
2092 rParent.left = 0;
2093 rParent.top = 0;
2094 rParent.right = GetSystemMetrics(SM_CXSCREEN);
2095 rParent.bottom = GetSystemMetrics(SM_CYFULLSCREEN);
2096#else
2097 SystemParametersInfo(SPI_GETWORKAREA, 0, &rParent, 0);
2098#endif
2099 }
2100 else
2101 GetWindowRect(hwndParent, &rParent);
2102 wParent = rParent.right - rParent.left;
2103 hParent = rParent.bottom - rParent.top;
2104
2105 hdc = GetDC(hwndChild);
2106 wScreen = GetDeviceCaps (hdc, HORZRES);
2107 hScreen = GetDeviceCaps (hdc, VERTRES);
2108 ReleaseDC(hwndChild, hdc);
2109
2110 xNew = rParent.left + ((wParent - wChild) /2);
2111 if (xNew < 0)
2112 {
2113 xNew = 0;
2114 }
2115 else if ((xNew+wChild) > wScreen)
2116 {
2117 xNew = wScreen - wChild;
2118 }
2119
2120 yNew = rParent.top + ((hParent - hChild) /2);
2121 if (yNew < 0)
2122 yNew = 0;
2123 else if ((yNew+hChild) > hScreen)
2124 yNew = hScreen - hChild;
2125
2126 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2127 SWP_NOSIZE | SWP_NOZORDER);
2128}
2129#endif /* FEAT_GUI_DIALOG */
2130
2131void
2132gui_mch_activate_window(void)
2133{
2134 (void)SetActiveWindow(s_hwnd);
2135}
2136
2137#if defined(FEAT_TOOLBAR) || defined(PROTO)
2138 void
2139gui_mch_show_toolbar(int showit)
2140{
2141 if (s_toolbarhwnd == NULL)
2142 return;
2143
2144 if (showit)
2145 ShowWindow(s_toolbarhwnd, SW_SHOW);
2146 else
2147 ShowWindow(s_toolbarhwnd, SW_HIDE);
2148}
2149
2150/* Then number of bitmaps is fixed. Exit is missing! */
2151#define TOOLBAR_BITMAP_COUNT 31
2152
2153#endif
2154
2155/*
2156 * ":simalt" command.
2157 */
2158 void
2159ex_simalt(exarg_T *eap)
2160{
2161 char_u *keys = eap->arg;
2162
2163 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2164 while (*keys)
2165 {
2166 if (*keys == '~')
2167 *keys = ' '; /* for showing system menu */
2168 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2169 keys++;
2170 }
2171}
2172
2173/*
2174 * Create the find & replace dialogs.
2175 * You can't have both at once: ":find" when replace is showing, destroys
2176 * the replace dialog first, and the other way around.
2177 */
2178#ifdef MSWIN_FIND_REPLACE
2179 static void
2180initialise_findrep(char_u *initial_string)
2181{
2182 int wword = FALSE;
2183 int mcase = !p_ic;
2184 char_u *entry_text;
2185
2186 /* Get the search string to use. */
2187 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2188
2189 s_findrep_struct.hwndOwner = s_hwnd;
2190 s_findrep_struct.Flags = FR_DOWN;
2191 if (mcase)
2192 s_findrep_struct.Flags |= FR_MATCHCASE;
2193 if (wword)
2194 s_findrep_struct.Flags |= FR_WHOLEWORD;
2195 if (entry_text != NULL && *entry_text != NUL)
2196 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +00002197 vim_strncpy(s_findrep_struct.lpstrFindWhat, entry_text,
2198 s_findrep_struct.wFindWhatLen - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 s_findrep_struct.lpstrReplaceWith[0] = NUL;
2200 }
2201 vim_free(entry_text);
2202}
2203#endif
2204
2205 void
2206gui_mch_find_dialog(exarg_T *eap)
2207{
2208#ifdef MSWIN_FIND_REPLACE
2209 if (s_findrep_msg != 0)
2210 {
2211 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2212 DestroyWindow(s_findrep_hwnd);
2213
2214 if (!IsWindow(s_findrep_hwnd))
2215 {
2216 initialise_findrep(eap->arg);
2217 s_findrep_hwnd = FindText((LPFINDREPLACE) &s_findrep_struct);
2218 }
2219
2220 (void)SetWindowText(s_findrep_hwnd,
2221 (LPCSTR)_("Find string (use '\\\\' to find a '\\')"));
2222 (void)SetFocus(s_findrep_hwnd);
2223
2224 s_findrep_is_find = TRUE;
2225 }
2226#endif
2227}
2228
2229
2230 void
2231gui_mch_replace_dialog(exarg_T *eap)
2232{
2233#ifdef MSWIN_FIND_REPLACE
2234 if (s_findrep_msg != 0)
2235 {
2236 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2237 DestroyWindow(s_findrep_hwnd);
2238
2239 if (!IsWindow(s_findrep_hwnd))
2240 {
2241 initialise_findrep(eap->arg);
2242 s_findrep_hwnd = ReplaceText((LPFINDREPLACE) &s_findrep_struct);
2243 }
2244
2245 (void)SetWindowText(s_findrep_hwnd,
2246 (LPCSTR)_("Find & Replace (use '\\\\' to find a '\\')"));
2247 (void)SetFocus(s_findrep_hwnd);
2248
2249 s_findrep_is_find = FALSE;
2250 }
2251#endif
2252}
2253
2254
2255/*
2256 * Set visibility of the pointer.
2257 */
2258 void
2259gui_mch_mousehide(int hide)
2260{
2261 if (hide != gui.pointer_hidden)
2262 {
2263 ShowCursor(!hide);
2264 gui.pointer_hidden = hide;
2265 }
2266}
2267
2268#ifdef FEAT_MENU
2269 static void
2270gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2271{
2272 /* Unhide the mouse, we don't get move events here. */
2273 gui_mch_mousehide(FALSE);
2274
2275 (void)TrackPopupMenu(
2276 (HMENU)menu->submenu_id,
2277 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2278 x, y,
2279 (int)0, /*reserved param*/
2280 s_hwnd,
2281 NULL);
2282 /*
2283 * NOTE: The pop-up menu can eat the mouse up event.
2284 * We deal with this in normal.c.
2285 */
2286}
2287#endif
2288
2289/*
2290 * Got a message when the system will go down.
2291 */
2292 static void
2293_OnEndSession(void)
2294{
2295 getout_preserve_modified(1);
2296}
2297
2298/*
2299 * Get this message when the user clicks on the cross in the top right corner
2300 * of a Windows95 window.
2301 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002302/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 static void
2304_OnClose(
2305 HWND hwnd)
2306{
2307 gui_shell_closed();
2308}
2309
2310/*
2311 * Get a message when the window is being destroyed.
2312 */
2313 static void
2314_OnDestroy(
2315 HWND hwnd)
2316{
2317#ifdef WIN16_3DLOOK
2318 Ctl3dUnregister(s_hinst);
2319#endif
2320 if (!destroying)
2321 _OnClose(hwnd);
2322}
2323
2324 static void
2325_OnPaint(
2326 HWND hwnd)
2327{
2328 if (!IsMinimized(hwnd))
2329 {
2330 PAINTSTRUCT ps;
2331
2332 out_flush(); /* make sure all output has been processed */
2333 (void)BeginPaint(hwnd, &ps);
2334
2335#ifdef FEAT_MBYTE
2336 /* prevent multi-byte characters from misprinting on an invalid
2337 * rectangle */
2338 if (has_mbyte)
2339 {
2340 RECT rect;
2341
2342 GetClientRect(hwnd, &rect);
2343 ps.rcPaint.left = rect.left;
2344 ps.rcPaint.right = rect.right;
2345 }
2346#endif
2347
2348 if (!IsRectEmpty(&ps.rcPaint))
2349 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2350 ps.rcPaint.right - ps.rcPaint.left + 1,
2351 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2352 EndPaint(hwnd, &ps);
2353 }
2354}
2355
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002356/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 static void
2358_OnSize(
2359 HWND hwnd,
2360 UINT state,
2361 int cx,
2362 int cy)
2363{
2364 if (!IsMinimized(hwnd))
2365 {
2366 gui_resize_shell(cx, cy);
2367
2368#ifdef FEAT_MENU
2369 /* Menu bar may wrap differently now */
2370 gui_mswin_get_menu_height(TRUE);
2371#endif
2372 }
2373}
2374
2375 static void
2376_OnSetFocus(
2377 HWND hwnd,
2378 HWND hwndOldFocus)
2379{
2380 gui_focus_change(TRUE);
2381 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2382}
2383
2384 static void
2385_OnKillFocus(
2386 HWND hwnd,
2387 HWND hwndNewFocus)
2388{
2389 gui_focus_change(FALSE);
2390 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2391}
2392
2393/*
2394 * Get a message when the user switches back to vim
2395 */
2396#ifdef WIN16
2397 static BOOL
2398#else
2399 static LRESULT
2400#endif
2401_OnActivateApp(
2402 HWND hwnd,
2403 BOOL fActivate,
2404#ifdef WIN16
2405 HTASK dwThreadId
2406#else
2407 DWORD dwThreadId
2408#endif
2409 )
2410{
2411 /* we call gui_focus_change() in _OnSetFocus() */
2412 /* gui_focus_change((int)fActivate); */
2413 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2414}
2415
2416#if defined(FEAT_WINDOWS) || defined(PROTO)
2417 void
2418gui_mch_destroy_scrollbar(scrollbar_T *sb)
2419{
2420 DestroyWindow(sb->id);
2421}
2422#endif
2423
2424/*
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002425 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 */
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002427 void
Bram Moolenaara40c5002005-01-09 21:16:21 +00002428gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429{
2430 RECT rct;
2431 POINT mp;
2432
2433 (void)GetWindowRect(s_textArea, &rct);
2434 (void)GetCursorPos((LPPOINT)&mp);
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002435 *x = (int)(mp.x - rct.left);
2436 *y = (int)(mp.y - rct.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437}
2438
2439/*
2440 * Move mouse pointer to character at (x, y).
2441 */
2442 void
2443gui_mch_setmouse(int x, int y)
2444{
2445 RECT rct;
2446
2447 (void)GetWindowRect(s_textArea, &rct);
2448 (void)SetCursorPos(x + gui.border_offset + rct.left,
2449 y + gui.border_offset + rct.top);
2450}
2451
2452 static void
2453gui_mswin_get_valid_dimensions(
2454 int w,
2455 int h,
2456 int *valid_w,
2457 int *valid_h)
2458{
2459 int base_width, base_height;
2460
2461 base_width = gui_get_base_width()
2462 + GetSystemMetrics(SM_CXFRAME) * 2;
2463 base_height = gui_get_base_height()
2464 + GetSystemMetrics(SM_CYFRAME) * 2
2465 + GetSystemMetrics(SM_CYCAPTION)
2466#ifdef FEAT_MENU
2467 + gui_mswin_get_menu_height(FALSE)
2468#endif
2469 ;
2470 *valid_w = base_width +
2471 ((w - base_width) / gui.char_width) * gui.char_width;
2472 *valid_h = base_height +
2473 ((h - base_height) / gui.char_height) * gui.char_height;
2474}
2475
2476 void
2477gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
2478{
2479
2480 *screen_w = GetSystemMetrics(SM_CXFULLSCREEN)
2481 - GetSystemMetrics(SM_CXFRAME) * 2;
2482 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
2483 * the menubar for MSwin, we subtract it from the screen height, so that
2484 * the window size can be made to fit on the screen. */
2485 *screen_h = GetSystemMetrics(SM_CYFULLSCREEN)
2486 - GetSystemMetrics(SM_CYFRAME) * 2
2487#ifdef FEAT_MENU
2488 - gui_mswin_get_menu_height(FALSE)
2489#endif
2490 ;
2491}
2492
2493 void
2494gui_mch_flash(int msec)
2495{
2496 RECT rc;
2497
2498 /*
2499 * Note: InvertRect() excludes right and bottom of rectangle.
2500 */
2501 rc.left = 0;
2502 rc.top = 0;
2503 rc.right = gui.num_cols * gui.char_width;
2504 rc.bottom = gui.num_rows * gui.char_height;
2505 InvertRect(s_hdc, &rc);
2506 gui_mch_flush(); /* make sure it's displayed */
2507
2508 ui_delay((long)msec, TRUE); /* wait for a few msec */
2509
2510 InvertRect(s_hdc, &rc);
2511}
2512
2513/*
2514 * Return flags used for scrolling.
2515 * The SW_INVALIDATE is required when part of the window is covered or
2516 * off-screen. Refer to MS KB Q75236.
2517 */
2518 static int
2519get_scroll_flags(void)
2520{
2521 HWND hwnd;
2522 RECT rcVim, rcOther, rcDest;
2523
2524 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaar0d406992005-05-22 22:03:39 +00002525
2526 /* Check if the window is partly above or below the screen. We don't care
2527 * about partly left or right of the screen, it is not relevant when
2528 * scrolling up or down. */
2529 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
2530 return SW_INVALIDATE;
2531
2532 /* Check if there is an window (partly) on top of us. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
2534 if (IsWindowVisible(hwnd))
2535 {
2536 GetWindowRect(hwnd, &rcOther);
2537 if (IntersectRect(&rcDest, &rcVim, &rcOther))
2538 return SW_INVALIDATE;
2539 }
2540 return 0;
2541}
2542
2543/*
2544 * Delete the given number of lines from the given row, scrolling up any
2545 * text further down within the scroll region.
2546 */
2547 void
2548gui_mch_delete_lines(
2549 int row,
2550 int num_lines)
2551{
2552 RECT rc;
2553
2554 rc.left = FILL_X(gui.scroll_region_left);
2555 rc.right = FILL_X(gui.scroll_region_right + 1);
2556 rc.top = FILL_Y(row);
2557 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
2558
2559 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
2560 &rc, &rc, NULL, NULL, get_scroll_flags());
2561
2562 UpdateWindow(s_textArea);
2563 /* This seems to be required to avoid the cursor disappearing when
2564 * scrolling such that the cursor ends up in the top-left character on
2565 * the screen... But why? (Webb) */
2566 /* It's probably fixed by disabling drawing the cursor while scrolling. */
2567 /* gui.cursor_is_valid = FALSE; */
2568
2569 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
2570 gui.scroll_region_left,
2571 gui.scroll_region_bot, gui.scroll_region_right);
2572}
2573
2574/*
2575 * Insert the given number of lines before the given row, scrolling down any
2576 * following text within the scroll region.
2577 */
2578 void
2579gui_mch_insert_lines(
2580 int row,
2581 int num_lines)
2582{
2583 RECT rc;
2584
2585 rc.left = FILL_X(gui.scroll_region_left);
2586 rc.right = FILL_X(gui.scroll_region_right + 1);
2587 rc.top = FILL_Y(row);
2588 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
2589 /* The SW_INVALIDATE is required when part of the window is covered or
2590 * off-screen. How do we avoid it when it's not needed? */
2591 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
2592 &rc, &rc, NULL, NULL, get_scroll_flags());
2593
2594 UpdateWindow(s_textArea);
2595
2596 gui_clear_block(row, gui.scroll_region_left,
2597 row + num_lines - 1, gui.scroll_region_right);
2598}
2599
2600
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002601/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 void
2603gui_mch_exit(int rc)
2604{
2605 ReleaseDC(s_textArea, s_hdc);
2606 DeleteObject(s_brush);
2607
2608#ifdef FEAT_TEAROFF
2609 /* Unload the tearoff bitmap */
2610 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
2611#endif
2612
2613 /* Destroy our window (if we have one). */
2614 if (s_hwnd != NULL)
2615 {
2616 destroying = TRUE; /* ignore WM_DESTROY message now */
2617 DestroyWindow(s_hwnd);
2618 }
2619
2620#ifdef GLOBAL_IME
2621 global_ime_end();
2622#endif
2623}
2624
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002625 static char_u *
2626logfont2name(LOGFONT lf)
2627{
2628 char *p;
2629 char *res;
2630 char *charset_name;
2631
2632 charset_name = charset_id2name((int)lf.lfCharSet);
2633 res = alloc((unsigned)(strlen(lf.lfFaceName) + 20
2634 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
2635 if (res != NULL)
2636 {
2637 p = res;
2638 /* make a normal font string out of the lf thing:*/
2639 sprintf((char *)p, "%s:h%d", lf.lfFaceName, pixels_to_points(
2640 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
2641 while (*p)
2642 {
2643 if (*p == ' ')
2644 *p = '_';
2645 ++p;
2646 }
2647#ifndef MSWIN16_FASTTEXT
2648 if (lf.lfItalic)
2649 STRCAT(p, ":i");
2650 if (lf.lfWeight >= FW_BOLD)
2651 STRCAT(p, ":b");
2652#endif
2653 if (lf.lfUnderline)
2654 STRCAT(p, ":u");
2655 if (lf.lfStrikeOut)
2656 STRCAT(p, ":s");
2657 if (charset_name != NULL)
2658 {
2659 STRCAT(p, ":c");
2660 STRCAT(p, charset_name);
2661 }
2662 }
2663
2664 return res;
2665}
2666
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002668 * Initialise vim to use the font with the given name.
2669 * Return FAIL if the font could not be loaded, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002671/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 int
2673gui_mch_init_font(char_u *font_name, int fontset)
2674{
2675 LOGFONT lf;
2676 GuiFont font = NOFONT;
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002677 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
2679 /* Load the font */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002680 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 font = get_font_handle(&lf);
2682 if (font == NOFONT)
2683 return FAIL;
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002684
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 if (font_name == NULL)
2686 font_name = lf.lfFaceName;
2687#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
2688 norm_logfont = lf;
2689#endif
2690#ifdef FEAT_MBYTE_IME
2691 im_set_font(&lf);
2692#endif
2693 gui_mch_free_font(gui.norm_font);
2694 gui.norm_font = font;
2695 current_font_height = lf.lfHeight;
2696 GetFontSize(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002698 p = logfont2name(lf);
2699 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002701 hl_set_font_name(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002703 /* When setting 'guifont' to "*" replace it with the actual font name.
2704 * */
2705 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 vim_free(p_guifont);
2708 p_guifont = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002710 else
2711 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
2713
2714#ifndef MSWIN16_FASTTEXT
2715 gui_mch_free_font(gui.ital_font);
2716 gui.ital_font = NOFONT;
2717 gui_mch_free_font(gui.bold_font);
2718 gui.bold_font = NOFONT;
2719 gui_mch_free_font(gui.boldital_font);
2720 gui.boldital_font = NOFONT;
2721
2722 if (!lf.lfItalic)
2723 {
2724 lf.lfItalic = TRUE;
2725 gui.ital_font = get_font_handle(&lf);
2726 lf.lfItalic = FALSE;
2727 }
2728 if (lf.lfWeight < FW_BOLD)
2729 {
2730 lf.lfWeight = FW_BOLD;
2731 gui.bold_font = get_font_handle(&lf);
2732 if (!lf.lfItalic)
2733 {
2734 lf.lfItalic = TRUE;
2735 gui.boldital_font = get_font_handle(&lf);
2736 }
2737 }
2738#endif
2739
2740 return OK;
2741}
2742
2743/*
2744 * Return TRUE if the GUI window is maximized, filling the whole screen.
2745 */
2746 int
2747gui_mch_maximized()
2748{
2749 return IsZoomed(s_hwnd);
2750}
2751
2752/*
2753 * Called when the font changed while the window is maximized. Compute the
2754 * new Rows and Columns. This is like resizing the window.
2755 */
2756 void
2757gui_mch_newfont()
2758{
2759 RECT rect;
2760
2761 GetWindowRect(s_hwnd, &rect);
2762 gui_resize_shell(rect.right - rect.left
2763 - GetSystemMetrics(SM_CXFRAME) * 2,
2764 rect.bottom - rect.top
2765 - GetSystemMetrics(SM_CYFRAME) * 2
2766 - GetSystemMetrics(SM_CYCAPTION)
2767#ifdef FEAT_MENU
2768 - gui_mswin_get_menu_height(FALSE)
2769#endif
2770 );
2771}
2772
2773/*
2774 * Set the window title
2775 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002776/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 void
2778gui_mch_settitle(
2779 char_u *title,
2780 char_u *icon)
2781{
2782#ifdef FEAT_MBYTE
2783 if (title != NULL && enc_codepage >= 0 && enc_codepage != (int)GetACP())
2784 {
2785 WCHAR *wbuf;
2786 int n;
2787
2788 /* Convert the title from 'encoding' to ucs2. */
2789 wbuf = (WCHAR *)enc_to_ucs2(title, NULL);
2790 if (wbuf != NULL)
2791 {
2792 n = SetWindowTextW(s_hwnd, wbuf);
2793 vim_free(wbuf);
2794 if (n != 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
2795 return;
2796 /* Retry with non-wide function (for Windows 98). */
2797 }
2798 }
2799#endif
2800 SetWindowText(s_hwnd, (LPCSTR)(title == NULL ? "VIM" : (char *)title));
2801}
2802
2803#ifdef FEAT_MOUSESHAPE
2804/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
2805 * misc2.c! */
2806static LPCSTR mshape_idcs[] =
2807{
2808 MAKEINTRESOURCE(IDC_ARROW), /* arrow */
2809 MAKEINTRESOURCE(0), /* blank */
2810 MAKEINTRESOURCE(IDC_IBEAM), /* beam */
2811 MAKEINTRESOURCE(IDC_SIZENS), /* updown */
2812 MAKEINTRESOURCE(IDC_SIZENS), /* udsizing */
2813 MAKEINTRESOURCE(IDC_SIZEWE), /* leftright */
2814 MAKEINTRESOURCE(IDC_SIZEWE), /* lrsizing */
2815 MAKEINTRESOURCE(IDC_WAIT), /* busy */
2816#ifdef WIN3264
2817 MAKEINTRESOURCE(IDC_NO), /* no */
2818#else
2819 MAKEINTRESOURCE(IDC_ICON), /* no */
2820#endif
2821 MAKEINTRESOURCE(IDC_ARROW), /* crosshair */
2822 MAKEINTRESOURCE(IDC_ARROW), /* hand1 */
2823 MAKEINTRESOURCE(IDC_ARROW), /* hand2 */
2824 MAKEINTRESOURCE(IDC_ARROW), /* pencil */
2825 MAKEINTRESOURCE(IDC_ARROW), /* question */
2826 MAKEINTRESOURCE(IDC_ARROW), /* right-arrow */
2827 MAKEINTRESOURCE(IDC_UPARROW), /* up-arrow */
2828 MAKEINTRESOURCE(IDC_ARROW) /* last one */
2829};
2830
2831 void
2832mch_set_mouse_shape(int shape)
2833{
2834 LPCSTR idc;
2835
2836 if (shape == MSHAPE_HIDE)
2837 ShowCursor(FALSE);
2838 else
2839 {
2840 if (shape >= MSHAPE_NUMBERED)
2841 idc = MAKEINTRESOURCE(IDC_ARROW);
2842 else
2843 idc = mshape_idcs[shape];
2844#ifdef _WIN64
2845 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
2846#else
2847# ifdef WIN32
2848 SetClassLong(s_textArea, GCL_HCURSOR, (LONG)LoadCursor(NULL, idc));
2849# else
2850 SetClassWord(s_textArea, GCW_HCURSOR, (WORD)LoadCursor(NULL, idc));
2851# endif
2852#endif
2853 if (!p_mh)
2854 {
2855 POINT mp;
2856
2857 /* Set the position to make it redrawn with the new shape. */
2858 (void)GetCursorPos((LPPOINT)&mp);
2859 (void)SetCursorPos(mp.x, mp.y);
2860 ShowCursor(TRUE);
2861 }
2862 }
2863}
2864#endif
2865
2866#ifdef FEAT_BROWSE
2867/*
2868 * The file browser exists in two versions: with "W" uses wide characters,
2869 * without "W" the current codepage. When FEAT_MBYTE is defined and on
2870 * Windows NT/2000/XP the "W" functions are used.
2871 */
2872
2873# if defined(FEAT_MBYTE) && defined(WIN3264)
2874/*
2875 * Wide version of convert_filter(). Keep in sync!
2876 */
2877 static WCHAR *
2878convert_filterW(char_u *s)
2879{
2880 WCHAR *res;
2881 unsigned s_len = (unsigned)STRLEN(s);
2882 unsigned i;
2883
2884 res = (WCHAR *)alloc((s_len + 3) * sizeof(WCHAR));
2885 if (res != NULL)
2886 {
2887 for (i = 0; i < s_len; ++i)
2888 if (s[i] == '\t' || s[i] == '\n')
2889 res[i] = '\0';
2890 else
2891 res[i] = s[i];
2892 res[s_len] = NUL;
2893 /* Add two extra NULs to make sure it's properly terminated. */
2894 res[s_len + 1] = NUL;
2895 res[s_len + 2] = NUL;
2896 }
2897 return res;
2898}
2899
2900/*
2901 * Wide version of gui_mch_browse(). Keep in sync!
2902 */
2903 static char_u *
2904gui_mch_browseW(
2905 int saving,
2906 char_u *title,
2907 char_u *dflt,
2908 char_u *ext,
2909 char_u *initdir,
2910 char_u *filter)
2911{
2912 /* We always use the wide function. This means enc_to_ucs2() must work,
2913 * otherwise it fails miserably! */
2914 OPENFILENAMEW fileStruct;
2915 WCHAR fileBuf[MAXPATHL];
2916 WCHAR *wp;
2917 int i;
2918 WCHAR *titlep = NULL;
2919 WCHAR *extp = NULL;
2920 WCHAR *initdirp = NULL;
2921 WCHAR *filterp;
2922 char_u *p;
2923
2924 if (dflt == NULL)
2925 fileBuf[0] = NUL;
2926 else
2927 {
2928 wp = enc_to_ucs2(dflt, NULL);
2929 if (wp == NULL)
2930 fileBuf[0] = NUL;
2931 else
2932 {
2933 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
2934 fileBuf[i] = wp[i];
2935 fileBuf[i] = NUL;
2936 vim_free(wp);
2937 }
2938 }
2939
2940 /* Convert the filter to Windows format. */
2941 filterp = convert_filterW(filter);
2942
2943 memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
2944#ifdef OPENFILENAME_SIZE_VERSION_400
2945 /* be compatible with Windows NT 4.0 */
2946 /* TODO: what to use for OPENFILENAMEW??? */
2947 fileStruct.lStructSize = sizeof(OPENFILENAME_SIZE_VERSION_400);
2948#else
2949 fileStruct.lStructSize = sizeof(fileStruct);
2950#endif
2951
2952 if (title != NULL)
2953 titlep = enc_to_ucs2(title, NULL);
2954 fileStruct.lpstrTitle = titlep;
2955
2956 if (ext != NULL)
2957 extp = enc_to_ucs2(ext, NULL);
2958 fileStruct.lpstrDefExt = extp;
2959
2960 fileStruct.lpstrFile = fileBuf;
2961 fileStruct.nMaxFile = MAXPATHL;
2962 fileStruct.lpstrFilter = filterp;
2963 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
2964 /* has an initial dir been specified? */
2965 if (initdir != NULL && *initdir != NUL)
2966 {
2967 /* Must have backslashes here, no matter what 'shellslash' says */
2968 initdirp = enc_to_ucs2(initdir, NULL);
2969 if (initdirp != NULL)
2970 {
2971 for (wp = initdirp; *wp != NUL; ++wp)
2972 if (*wp == '/')
2973 *wp = '\\';
2974 }
2975 fileStruct.lpstrInitialDir = initdirp;
2976 }
2977
2978 /*
2979 * TODO: Allow selection of multiple files. Needs another arg to this
2980 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
2981 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
2982 * files that don't exist yet, so I haven't put it in. What about
2983 * OFN_PATHMUSTEXIST?
2984 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
2985 */
2986 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
2987#ifdef FEAT_SHORTCUT
2988 if (curbuf->b_p_bin)
2989 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
2990#endif
2991 if (saving)
2992 {
2993 if (!GetSaveFileNameW(&fileStruct))
2994 return NULL;
2995 }
2996 else
2997 {
2998 if (!GetOpenFileNameW(&fileStruct))
2999 return NULL;
3000 }
3001
3002 vim_free(filterp);
3003 vim_free(initdirp);
3004 vim_free(titlep);
3005 vim_free(extp);
3006
3007 /* Convert from UCS2 to 'encoding'. */
3008 p = ucs2_to_enc(fileBuf, NULL);
3009 if (p != NULL)
3010 /* when out of memory we get garbage for non-ASCII chars */
3011 STRCPY(fileBuf, p);
3012 vim_free(p);
3013
3014 /* Give focus back to main window (when using MDI). */
3015 SetFocus(s_hwnd);
3016
3017 /* Shorten the file name if possible */
3018 mch_dirname(IObuff, IOSIZE);
3019 p = shorten_fname((char_u *)fileBuf, IObuff);
3020 if (p == NULL)
3021 p = (char_u *)fileBuf;
3022 return vim_strsave(p);
3023}
3024# endif /* FEAT_MBYTE */
3025
3026
3027/*
3028 * Convert the string s to the proper format for a filter string by replacing
3029 * the \t and \n delimeters with \0.
3030 * Returns the converted string in allocated memory.
3031 *
3032 * Keep in sync with convert_filterW() above!
3033 */
3034 static char_u *
3035convert_filter(char_u *s)
3036{
3037 char_u *res;
3038 unsigned s_len = (unsigned)STRLEN(s);
3039 unsigned i;
3040
3041 res = alloc(s_len + 3);
3042 if (res != NULL)
3043 {
3044 for (i = 0; i < s_len; ++i)
3045 if (s[i] == '\t' || s[i] == '\n')
3046 res[i] = '\0';
3047 else
3048 res[i] = s[i];
3049 res[s_len] = NUL;
3050 /* Add two extra NULs to make sure it's properly terminated. */
3051 res[s_len + 1] = NUL;
3052 res[s_len + 2] = NUL;
3053 }
3054 return res;
3055}
3056
3057/*
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003058 * Select a directory.
3059 */
3060 char_u *
3061gui_mch_browsedir(char_u *title, char_u *initdir)
3062{
3063 /* We fake this: Use a filter that doesn't select anything and a default
3064 * file name that won't be used. */
3065 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3066 initdir, (char_u *)_("Directory\t*.nothing\n"));
3067}
3068
3069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 * Pop open a file browser and return the file selected, in allocated memory,
3071 * or NULL if Cancel is hit.
3072 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3073 * title - Title message for the file browser dialog.
3074 * dflt - Default name of file.
3075 * ext - Default extension to be added to files without extensions.
3076 * initdir - directory in which to open the browser (NULL = current dir)
3077 * filter - Filter for matched files to choose from.
3078 *
3079 * Keep in sync with gui_mch_browseW() above!
3080 */
3081 char_u *
3082gui_mch_browse(
3083 int saving,
3084 char_u *title,
3085 char_u *dflt,
3086 char_u *ext,
3087 char_u *initdir,
3088 char_u *filter)
3089{
3090 OPENFILENAME fileStruct;
3091 char_u fileBuf[MAXPATHL];
3092 char_u *initdirp = NULL;
3093 char_u *filterp;
3094 char_u *p;
3095
3096# if defined(FEAT_MBYTE) && defined(WIN3264)
3097 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
3098 return gui_mch_browseW(saving, title, dflt, ext, initdir, filter);
3099# endif
3100
3101 if (dflt == NULL)
3102 fileBuf[0] = NUL;
3103 else
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +00003104 vim_strncpy(fileBuf, dflt, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105
3106 /* Convert the filter to Windows format. */
3107 filterp = convert_filter(filter);
3108
3109 memset(&fileStruct, 0, sizeof(OPENFILENAME));
3110#ifdef OPENFILENAME_SIZE_VERSION_400
3111 /* be compatible with Windows NT 4.0 */
3112 fileStruct.lStructSize = sizeof(OPENFILENAME_SIZE_VERSION_400);
3113#else
3114 fileStruct.lStructSize = sizeof(fileStruct);
3115#endif
3116
3117 fileStruct.lpstrTitle = title;
3118 fileStruct.lpstrDefExt = ext;
3119
3120 fileStruct.lpstrFile = fileBuf;
3121 fileStruct.nMaxFile = MAXPATHL;
3122 fileStruct.lpstrFilter = filterp;
3123 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3124 /* has an initial dir been specified? */
3125 if (initdir != NULL && *initdir != NUL)
3126 {
3127 /* Must have backslashes here, no matter what 'shellslash' says */
3128 initdirp = vim_strsave(initdir);
3129 if (initdirp != NULL)
3130 for (p = initdirp; *p != NUL; ++p)
3131 if (*p == '/')
3132 *p = '\\';
3133 fileStruct.lpstrInitialDir = initdirp;
3134 }
3135
3136 /*
3137 * TODO: Allow selection of multiple files. Needs another arg to this
3138 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3139 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3140 * files that don't exist yet, so I haven't put it in. What about
3141 * OFN_PATHMUSTEXIST?
3142 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3143 */
3144 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
3145#ifdef FEAT_SHORTCUT
3146 if (curbuf->b_p_bin)
3147 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
3148#endif
3149 if (saving)
3150 {
3151 if (!GetSaveFileName(&fileStruct))
3152 return NULL;
3153 }
3154 else
3155 {
3156 if (!GetOpenFileName(&fileStruct))
3157 return NULL;
3158 }
3159
3160 vim_free(filterp);
3161 vim_free(initdirp);
3162
3163 /* Give focus back to main window (when using MDI). */
3164 SetFocus(s_hwnd);
3165
3166 /* Shorten the file name if possible */
3167 mch_dirname(IObuff, IOSIZE);
3168 p = shorten_fname((char_u *)fileBuf, IObuff);
3169 if (p == NULL)
3170 p = (char_u *)fileBuf;
3171 return vim_strsave(p);
3172}
3173#endif /* FEAT_BROWSE */
3174
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003175/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 static void
3177_OnDropFiles(
3178 HWND hwnd,
3179 HDROP hDrop)
3180{
3181#ifdef FEAT_WINDOWS
3182#ifdef WIN3264
3183# define BUFPATHLEN _MAX_PATH
3184# define DRAGQVAL 0xFFFFFFFF
3185#else
3186# define BUFPATHLEN MAXPATHL
3187# define DRAGQVAL 0xFFFF
3188#endif
3189#ifdef FEAT_MBYTE
3190 WCHAR wszFile[BUFPATHLEN];
3191#endif
3192 char szFile[BUFPATHLEN];
3193 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3194 UINT i;
3195 char_u **fnames;
3196 POINT pt;
3197 int_u modifiers = 0;
3198
3199 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3200
3201 /* Obtain dropped position */
3202 DragQueryPoint(hDrop, &pt);
3203 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3204
3205# ifdef FEAT_VISUAL
3206 reset_VIsual();
3207# endif
3208
3209 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3210
3211 if (fnames != NULL)
3212 for (i = 0; i < cFiles; ++i)
3213 {
3214#ifdef FEAT_MBYTE
3215 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3216 fnames[i] = ucs2_to_enc(wszFile, NULL);
3217 else
3218#endif
3219 {
3220 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3221 fnames[i] = vim_strsave(szFile);
3222 }
3223 }
3224
3225 DragFinish(hDrop);
3226
3227 if (fnames != NULL)
3228 {
3229 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3230 modifiers |= MOUSE_SHIFT;
3231 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3232 modifiers |= MOUSE_CTRL;
3233 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3234 modifiers |= MOUSE_ALT;
3235
3236 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3237
3238 s_need_activate = TRUE;
3239 }
3240#endif
3241}
3242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003243/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 static int
3245_OnScroll(
3246 HWND hwnd,
3247 HWND hwndCtl,
3248 UINT code,
3249 int pos)
3250{
3251 static UINT prev_code = 0; /* code of previous call */
3252 scrollbar_T *sb, *sb_info;
3253 long val;
3254 int dragging = FALSE;
3255 int dont_scroll_save = dont_scroll;
3256#ifndef WIN3264
3257 int nPos;
3258#else
3259 SCROLLINFO si;
3260
3261 si.cbSize = sizeof(si);
3262 si.fMask = SIF_POS;
3263#endif
3264
3265 sb = gui_mswin_find_scrollbar(hwndCtl);
3266 if (sb == NULL)
3267 return 0;
3268
3269 if (sb->wp != NULL) /* Left or right scrollbar */
3270 {
3271 /*
3272 * Careful: need to get scrollbar info out of first (left) scrollbar
3273 * for window, but keep real scrollbar too because we must pass it to
3274 * gui_drag_scrollbar().
3275 */
3276 sb_info = &sb->wp->w_scrollbars[0];
3277 }
3278 else /* Bottom scrollbar */
3279 sb_info = sb;
3280 val = sb_info->value;
3281
3282 switch (code)
3283 {
3284 case SB_THUMBTRACK:
3285 val = pos;
3286 dragging = TRUE;
3287 if (sb->scroll_shift > 0)
3288 val <<= sb->scroll_shift;
3289 break;
3290 case SB_LINEDOWN:
3291 val++;
3292 break;
3293 case SB_LINEUP:
3294 val--;
3295 break;
3296 case SB_PAGEDOWN:
3297 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3298 break;
3299 case SB_PAGEUP:
3300 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3301 break;
3302 case SB_TOP:
3303 val = 0;
3304 break;
3305 case SB_BOTTOM:
3306 val = sb_info->max;
3307 break;
3308 case SB_ENDSCROLL:
3309 if (prev_code == SB_THUMBTRACK)
3310 {
3311 /*
3312 * "pos" only gives us 16-bit data. In case of large file,
3313 * use GetScrollPos() which returns 32-bit. Unfortunately it
3314 * is not valid while the scrollbar is being dragged.
3315 */
3316 val = GetScrollPos(hwndCtl, SB_CTL);
3317 if (sb->scroll_shift > 0)
3318 val <<= sb->scroll_shift;
3319 }
3320 break;
3321
3322 default:
3323 /* TRACE("Unknown scrollbar event %d\n", code); */
3324 return 0;
3325 }
3326 prev_code = code;
3327
3328#ifdef WIN3264
3329 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3330 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
3331#else
3332 nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3333 SetScrollPos(hwndCtl, SB_CTL, nPos, TRUE);
3334#endif
3335
3336 /*
3337 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3338 */
3339 if (sb->wp != NULL)
3340 {
3341 scrollbar_T *sba = sb->wp->w_scrollbars;
3342 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3343
3344#ifdef WIN3264
3345 SetScrollInfo(id, SB_CTL, &si, TRUE);
3346#else
3347 SetScrollPos(id, SB_CTL, nPos, TRUE);
3348#endif
3349 }
3350
3351 /* Don't let us be interrupted here by another message. */
3352 s_busy_processing = TRUE;
3353
3354 /* When "allow_scrollbar" is FALSE still need to remember the new
3355 * position, but don't actually scroll by setting "dont_scroll". */
3356 dont_scroll = !allow_scrollbar;
3357
3358 gui_drag_scrollbar(sb, val, dragging);
3359
3360 s_busy_processing = FALSE;
3361 dont_scroll = dont_scroll_save;
3362
3363 return 0;
3364}
3365
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003366
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367/*
3368 * Get command line arguments.
3369 * Use "prog" as the name of the program and "cmdline" as the arguments.
3370 * Copy the arguments to allocated memory.
3371 * Return the number of arguments (including program name).
3372 * Return pointers to the arguments in "argvp".
3373 * Return pointer to buffer in "tofree".
3374 * Returns zero when out of memory.
3375 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003376/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 int
3378get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
3379{
3380 int i;
3381 char *p;
3382 char *progp;
3383 char *pnew = NULL;
3384 char *newcmdline;
3385 int inquote;
3386 int argc;
3387 char **argv = NULL;
3388 int round;
3389
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003390#ifdef FEAT_MBYTE
3391 /* Try using the Unicode version first, it takes care of conversion when
3392 * 'encoding' is changed. */
3393 argc = get_cmd_argsW(&argv);
3394 if (argc != 0)
3395 goto done;
3396#endif
3397
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 /* Handle the program name. Remove the ".exe" extension, and find the 1st
3399 * non-space. */
3400 p = strrchr(prog, '.');
3401 if (p != NULL)
3402 *p = NUL;
3403 for (progp = prog; *progp == ' '; ++progp)
3404 ;
3405
3406 /* The command line is copied to allocated memory, so that we can change
3407 * it. Add the size of the string, the separating NUL and a terminating
3408 * NUL. */
3409 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
3410 if (newcmdline == NULL)
3411 return 0;
3412
3413 /*
3414 * First round: count the number of arguments ("pnew" == NULL).
3415 * Second round: produce the arguments.
3416 */
3417 for (round = 1; round <= 2; ++round)
3418 {
3419 /* First argument is the program name. */
3420 if (pnew != NULL)
3421 {
3422 argv[0] = pnew;
3423 strcpy(pnew, progp);
3424 pnew += strlen(pnew);
3425 *pnew++ = NUL;
3426 }
3427
3428 /*
3429 * Isolate each argument and put it in argv[].
3430 */
3431 p = cmdline;
3432 argc = 1;
3433 while (*p != NUL)
3434 {
3435 inquote = FALSE;
3436 if (pnew != NULL)
3437 argv[argc] = pnew;
3438 ++argc;
3439 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
3440 {
3441 /* Backslashes are only special when followed by a double
3442 * quote. */
3443 i = strspn(p, "\\");
3444 if (p[i] == '"')
3445 {
3446 /* Halve the number of backslashes. */
3447 if (i > 1 && pnew != NULL)
3448 {
3449 memset(pnew, '\\', i / 2);
3450 pnew += i / 2;
3451 }
3452
3453 /* Even nr of backslashes toggles quoting, uneven copies
3454 * the double quote. */
3455 if ((i & 1) == 0)
3456 inquote = !inquote;
3457 else if (pnew != NULL)
3458 *pnew++ = '"';
3459 p += i + 1;
3460 }
3461 else if (i > 0)
3462 {
3463 /* Copy span of backslashes unmodified. */
3464 if (pnew != NULL)
3465 {
3466 memset(pnew, '\\', i);
3467 pnew += i;
3468 }
3469 p += i;
3470 }
3471 else
3472 {
3473 if (pnew != NULL)
3474 *pnew++ = *p;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00003475#ifdef FEAT_MBYTE
3476 /* Can't use mb_* functions, because 'encoding' is not
3477 * initialized yet here. */
3478 if (IsDBCSLeadByte(*p))
3479 {
3480 ++p;
3481 if (pnew != NULL)
3482 *pnew++ = *p;
3483 }
3484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 ++p;
3486 }
3487 }
3488
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003489 if (pnew != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 *pnew++ = NUL;
3491 while (*p == ' ' || *p == '\t')
3492 ++p; /* advance until a non-space */
3493 }
3494
3495 if (round == 1)
3496 {
3497 argv = (char **)malloc((argc + 1) * sizeof(char *));
3498 if (argv == NULL )
Bram Moolenaare6b165e2005-06-30 21:56:01 +00003499 {
3500 vim_free(newcmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 return 0; /* malloc error */
Bram Moolenaare6b165e2005-06-30 21:56:01 +00003502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 pnew = newcmdline;
3504 }
3505 }
3506
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003507done:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003509 argv[argc] = NULL; /* NULL-terminated list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 *argvp = argv;
3511 return argc;
3512}