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