blob: be2354681e285191568ab31452bb6a4dbb9054a5 [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 {
2197 STRNCPY(s_findrep_struct.lpstrFindWhat, entry_text,
2198 s_findrep_struct.wFindWhatLen);
2199 s_findrep_struct.lpstrFindWhat[s_findrep_struct.wFindWhatLen - 1] = NUL;
2200 s_findrep_struct.lpstrReplaceWith[0] = NUL;
2201 }
2202 vim_free(entry_text);
2203}
2204#endif
2205
2206 void
2207gui_mch_find_dialog(exarg_T *eap)
2208{
2209#ifdef MSWIN_FIND_REPLACE
2210 if (s_findrep_msg != 0)
2211 {
2212 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2213 DestroyWindow(s_findrep_hwnd);
2214
2215 if (!IsWindow(s_findrep_hwnd))
2216 {
2217 initialise_findrep(eap->arg);
2218 s_findrep_hwnd = FindText((LPFINDREPLACE) &s_findrep_struct);
2219 }
2220
2221 (void)SetWindowText(s_findrep_hwnd,
2222 (LPCSTR)_("Find string (use '\\\\' to find a '\\')"));
2223 (void)SetFocus(s_findrep_hwnd);
2224
2225 s_findrep_is_find = TRUE;
2226 }
2227#endif
2228}
2229
2230
2231 void
2232gui_mch_replace_dialog(exarg_T *eap)
2233{
2234#ifdef MSWIN_FIND_REPLACE
2235 if (s_findrep_msg != 0)
2236 {
2237 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2238 DestroyWindow(s_findrep_hwnd);
2239
2240 if (!IsWindow(s_findrep_hwnd))
2241 {
2242 initialise_findrep(eap->arg);
2243 s_findrep_hwnd = ReplaceText((LPFINDREPLACE) &s_findrep_struct);
2244 }
2245
2246 (void)SetWindowText(s_findrep_hwnd,
2247 (LPCSTR)_("Find & Replace (use '\\\\' to find a '\\')"));
2248 (void)SetFocus(s_findrep_hwnd);
2249
2250 s_findrep_is_find = FALSE;
2251 }
2252#endif
2253}
2254
2255
2256/*
2257 * Set visibility of the pointer.
2258 */
2259 void
2260gui_mch_mousehide(int hide)
2261{
2262 if (hide != gui.pointer_hidden)
2263 {
2264 ShowCursor(!hide);
2265 gui.pointer_hidden = hide;
2266 }
2267}
2268
2269#ifdef FEAT_MENU
2270 static void
2271gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2272{
2273 /* Unhide the mouse, we don't get move events here. */
2274 gui_mch_mousehide(FALSE);
2275
2276 (void)TrackPopupMenu(
2277 (HMENU)menu->submenu_id,
2278 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2279 x, y,
2280 (int)0, /*reserved param*/
2281 s_hwnd,
2282 NULL);
2283 /*
2284 * NOTE: The pop-up menu can eat the mouse up event.
2285 * We deal with this in normal.c.
2286 */
2287}
2288#endif
2289
2290/*
2291 * Got a message when the system will go down.
2292 */
2293 static void
2294_OnEndSession(void)
2295{
2296 getout_preserve_modified(1);
2297}
2298
2299/*
2300 * Get this message when the user clicks on the cross in the top right corner
2301 * of a Windows95 window.
2302 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002303/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 static void
2305_OnClose(
2306 HWND hwnd)
2307{
2308 gui_shell_closed();
2309}
2310
2311/*
2312 * Get a message when the window is being destroyed.
2313 */
2314 static void
2315_OnDestroy(
2316 HWND hwnd)
2317{
2318#ifdef WIN16_3DLOOK
2319 Ctl3dUnregister(s_hinst);
2320#endif
2321 if (!destroying)
2322 _OnClose(hwnd);
2323}
2324
2325 static void
2326_OnPaint(
2327 HWND hwnd)
2328{
2329 if (!IsMinimized(hwnd))
2330 {
2331 PAINTSTRUCT ps;
2332
2333 out_flush(); /* make sure all output has been processed */
2334 (void)BeginPaint(hwnd, &ps);
2335
2336#ifdef FEAT_MBYTE
2337 /* prevent multi-byte characters from misprinting on an invalid
2338 * rectangle */
2339 if (has_mbyte)
2340 {
2341 RECT rect;
2342
2343 GetClientRect(hwnd, &rect);
2344 ps.rcPaint.left = rect.left;
2345 ps.rcPaint.right = rect.right;
2346 }
2347#endif
2348
2349 if (!IsRectEmpty(&ps.rcPaint))
2350 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2351 ps.rcPaint.right - ps.rcPaint.left + 1,
2352 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2353 EndPaint(hwnd, &ps);
2354 }
2355}
2356
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002357/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 static void
2359_OnSize(
2360 HWND hwnd,
2361 UINT state,
2362 int cx,
2363 int cy)
2364{
2365 if (!IsMinimized(hwnd))
2366 {
2367 gui_resize_shell(cx, cy);
2368
2369#ifdef FEAT_MENU
2370 /* Menu bar may wrap differently now */
2371 gui_mswin_get_menu_height(TRUE);
2372#endif
2373 }
2374}
2375
2376 static void
2377_OnSetFocus(
2378 HWND hwnd,
2379 HWND hwndOldFocus)
2380{
2381 gui_focus_change(TRUE);
2382 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2383}
2384
2385 static void
2386_OnKillFocus(
2387 HWND hwnd,
2388 HWND hwndNewFocus)
2389{
2390 gui_focus_change(FALSE);
2391 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2392}
2393
2394/*
2395 * Get a message when the user switches back to vim
2396 */
2397#ifdef WIN16
2398 static BOOL
2399#else
2400 static LRESULT
2401#endif
2402_OnActivateApp(
2403 HWND hwnd,
2404 BOOL fActivate,
2405#ifdef WIN16
2406 HTASK dwThreadId
2407#else
2408 DWORD dwThreadId
2409#endif
2410 )
2411{
2412 /* we call gui_focus_change() in _OnSetFocus() */
2413 /* gui_focus_change((int)fActivate); */
2414 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2415}
2416
2417#if defined(FEAT_WINDOWS) || defined(PROTO)
2418 void
2419gui_mch_destroy_scrollbar(scrollbar_T *sb)
2420{
2421 DestroyWindow(sb->id);
2422}
2423#endif
2424
2425/*
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002426 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 */
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002428 void
Bram Moolenaara40c5002005-01-09 21:16:21 +00002429gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430{
2431 RECT rct;
2432 POINT mp;
2433
2434 (void)GetWindowRect(s_textArea, &rct);
2435 (void)GetCursorPos((LPPOINT)&mp);
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002436 *x = (int)(mp.x - rct.left);
2437 *y = (int)(mp.y - rct.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438}
2439
2440/*
2441 * Move mouse pointer to character at (x, y).
2442 */
2443 void
2444gui_mch_setmouse(int x, int y)
2445{
2446 RECT rct;
2447
2448 (void)GetWindowRect(s_textArea, &rct);
2449 (void)SetCursorPos(x + gui.border_offset + rct.left,
2450 y + gui.border_offset + rct.top);
2451}
2452
2453 static void
2454gui_mswin_get_valid_dimensions(
2455 int w,
2456 int h,
2457 int *valid_w,
2458 int *valid_h)
2459{
2460 int base_width, base_height;
2461
2462 base_width = gui_get_base_width()
2463 + GetSystemMetrics(SM_CXFRAME) * 2;
2464 base_height = gui_get_base_height()
2465 + GetSystemMetrics(SM_CYFRAME) * 2
2466 + GetSystemMetrics(SM_CYCAPTION)
2467#ifdef FEAT_MENU
2468 + gui_mswin_get_menu_height(FALSE)
2469#endif
2470 ;
2471 *valid_w = base_width +
2472 ((w - base_width) / gui.char_width) * gui.char_width;
2473 *valid_h = base_height +
2474 ((h - base_height) / gui.char_height) * gui.char_height;
2475}
2476
2477 void
2478gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
2479{
2480
2481 *screen_w = GetSystemMetrics(SM_CXFULLSCREEN)
2482 - GetSystemMetrics(SM_CXFRAME) * 2;
2483 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
2484 * the menubar for MSwin, we subtract it from the screen height, so that
2485 * the window size can be made to fit on the screen. */
2486 *screen_h = GetSystemMetrics(SM_CYFULLSCREEN)
2487 - GetSystemMetrics(SM_CYFRAME) * 2
2488#ifdef FEAT_MENU
2489 - gui_mswin_get_menu_height(FALSE)
2490#endif
2491 ;
2492}
2493
2494 void
2495gui_mch_flash(int msec)
2496{
2497 RECT rc;
2498
2499 /*
2500 * Note: InvertRect() excludes right and bottom of rectangle.
2501 */
2502 rc.left = 0;
2503 rc.top = 0;
2504 rc.right = gui.num_cols * gui.char_width;
2505 rc.bottom = gui.num_rows * gui.char_height;
2506 InvertRect(s_hdc, &rc);
2507 gui_mch_flush(); /* make sure it's displayed */
2508
2509 ui_delay((long)msec, TRUE); /* wait for a few msec */
2510
2511 InvertRect(s_hdc, &rc);
2512}
2513
2514/*
2515 * Return flags used for scrolling.
2516 * The SW_INVALIDATE is required when part of the window is covered or
2517 * off-screen. Refer to MS KB Q75236.
2518 */
2519 static int
2520get_scroll_flags(void)
2521{
2522 HWND hwnd;
2523 RECT rcVim, rcOther, rcDest;
2524
2525 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaar0d406992005-05-22 22:03:39 +00002526
2527 /* Check if the window is partly above or below the screen. We don't care
2528 * about partly left or right of the screen, it is not relevant when
2529 * scrolling up or down. */
2530 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
2531 return SW_INVALIDATE;
2532
2533 /* Check if there is an window (partly) on top of us. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
2535 if (IsWindowVisible(hwnd))
2536 {
2537 GetWindowRect(hwnd, &rcOther);
2538 if (IntersectRect(&rcDest, &rcVim, &rcOther))
2539 return SW_INVALIDATE;
2540 }
2541 return 0;
2542}
2543
2544/*
2545 * Delete the given number of lines from the given row, scrolling up any
2546 * text further down within the scroll region.
2547 */
2548 void
2549gui_mch_delete_lines(
2550 int row,
2551 int num_lines)
2552{
2553 RECT rc;
2554
2555 rc.left = FILL_X(gui.scroll_region_left);
2556 rc.right = FILL_X(gui.scroll_region_right + 1);
2557 rc.top = FILL_Y(row);
2558 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
2559
2560 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
2561 &rc, &rc, NULL, NULL, get_scroll_flags());
2562
2563 UpdateWindow(s_textArea);
2564 /* This seems to be required to avoid the cursor disappearing when
2565 * scrolling such that the cursor ends up in the top-left character on
2566 * the screen... But why? (Webb) */
2567 /* It's probably fixed by disabling drawing the cursor while scrolling. */
2568 /* gui.cursor_is_valid = FALSE; */
2569
2570 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
2571 gui.scroll_region_left,
2572 gui.scroll_region_bot, gui.scroll_region_right);
2573}
2574
2575/*
2576 * Insert the given number of lines before the given row, scrolling down any
2577 * following text within the scroll region.
2578 */
2579 void
2580gui_mch_insert_lines(
2581 int row,
2582 int num_lines)
2583{
2584 RECT rc;
2585
2586 rc.left = FILL_X(gui.scroll_region_left);
2587 rc.right = FILL_X(gui.scroll_region_right + 1);
2588 rc.top = FILL_Y(row);
2589 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
2590 /* The SW_INVALIDATE is required when part of the window is covered or
2591 * off-screen. How do we avoid it when it's not needed? */
2592 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
2593 &rc, &rc, NULL, NULL, get_scroll_flags());
2594
2595 UpdateWindow(s_textArea);
2596
2597 gui_clear_block(row, gui.scroll_region_left,
2598 row + num_lines - 1, gui.scroll_region_right);
2599}
2600
2601
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002602/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 void
2604gui_mch_exit(int rc)
2605{
2606 ReleaseDC(s_textArea, s_hdc);
2607 DeleteObject(s_brush);
2608
2609#ifdef FEAT_TEAROFF
2610 /* Unload the tearoff bitmap */
2611 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
2612#endif
2613
2614 /* Destroy our window (if we have one). */
2615 if (s_hwnd != NULL)
2616 {
2617 destroying = TRUE; /* ignore WM_DESTROY message now */
2618 DestroyWindow(s_hwnd);
2619 }
2620
2621#ifdef GLOBAL_IME
2622 global_ime_end();
2623#endif
2624}
2625
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002626 static char_u *
2627logfont2name(LOGFONT lf)
2628{
2629 char *p;
2630 char *res;
2631 char *charset_name;
2632
2633 charset_name = charset_id2name((int)lf.lfCharSet);
2634 res = alloc((unsigned)(strlen(lf.lfFaceName) + 20
2635 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
2636 if (res != NULL)
2637 {
2638 p = res;
2639 /* make a normal font string out of the lf thing:*/
2640 sprintf((char *)p, "%s:h%d", lf.lfFaceName, pixels_to_points(
2641 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
2642 while (*p)
2643 {
2644 if (*p == ' ')
2645 *p = '_';
2646 ++p;
2647 }
2648#ifndef MSWIN16_FASTTEXT
2649 if (lf.lfItalic)
2650 STRCAT(p, ":i");
2651 if (lf.lfWeight >= FW_BOLD)
2652 STRCAT(p, ":b");
2653#endif
2654 if (lf.lfUnderline)
2655 STRCAT(p, ":u");
2656 if (lf.lfStrikeOut)
2657 STRCAT(p, ":s");
2658 if (charset_name != NULL)
2659 {
2660 STRCAT(p, ":c");
2661 STRCAT(p, charset_name);
2662 }
2663 }
2664
2665 return res;
2666}
2667
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002669 * Initialise vim to use the font with the given name.
2670 * Return FAIL if the font could not be loaded, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002672/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 int
2674gui_mch_init_font(char_u *font_name, int fontset)
2675{
2676 LOGFONT lf;
2677 GuiFont font = NOFONT;
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002678 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679
2680 /* Load the font */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002681 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 font = get_font_handle(&lf);
2683 if (font == NOFONT)
2684 return FAIL;
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002685
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 if (font_name == NULL)
2687 font_name = lf.lfFaceName;
2688#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
2689 norm_logfont = lf;
2690#endif
2691#ifdef FEAT_MBYTE_IME
2692 im_set_font(&lf);
2693#endif
2694 gui_mch_free_font(gui.norm_font);
2695 gui.norm_font = font;
2696 current_font_height = lf.lfHeight;
2697 GetFontSize(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002699 p = logfont2name(lf);
2700 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002702 hl_set_font_name(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002704 /* When setting 'guifont' to "*" replace it with the actual font name.
2705 * */
2706 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 vim_free(p_guifont);
2709 p_guifont = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002711 else
2712 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714
2715#ifndef MSWIN16_FASTTEXT
2716 gui_mch_free_font(gui.ital_font);
2717 gui.ital_font = NOFONT;
2718 gui_mch_free_font(gui.bold_font);
2719 gui.bold_font = NOFONT;
2720 gui_mch_free_font(gui.boldital_font);
2721 gui.boldital_font = NOFONT;
2722
2723 if (!lf.lfItalic)
2724 {
2725 lf.lfItalic = TRUE;
2726 gui.ital_font = get_font_handle(&lf);
2727 lf.lfItalic = FALSE;
2728 }
2729 if (lf.lfWeight < FW_BOLD)
2730 {
2731 lf.lfWeight = FW_BOLD;
2732 gui.bold_font = get_font_handle(&lf);
2733 if (!lf.lfItalic)
2734 {
2735 lf.lfItalic = TRUE;
2736 gui.boldital_font = get_font_handle(&lf);
2737 }
2738 }
2739#endif
2740
2741 return OK;
2742}
2743
2744/*
2745 * Return TRUE if the GUI window is maximized, filling the whole screen.
2746 */
2747 int
2748gui_mch_maximized()
2749{
2750 return IsZoomed(s_hwnd);
2751}
2752
2753/*
2754 * Called when the font changed while the window is maximized. Compute the
2755 * new Rows and Columns. This is like resizing the window.
2756 */
2757 void
2758gui_mch_newfont()
2759{
2760 RECT rect;
2761
2762 GetWindowRect(s_hwnd, &rect);
2763 gui_resize_shell(rect.right - rect.left
2764 - GetSystemMetrics(SM_CXFRAME) * 2,
2765 rect.bottom - rect.top
2766 - GetSystemMetrics(SM_CYFRAME) * 2
2767 - GetSystemMetrics(SM_CYCAPTION)
2768#ifdef FEAT_MENU
2769 - gui_mswin_get_menu_height(FALSE)
2770#endif
2771 );
2772}
2773
2774/*
2775 * Set the window title
2776 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002777/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 void
2779gui_mch_settitle(
2780 char_u *title,
2781 char_u *icon)
2782{
2783#ifdef FEAT_MBYTE
2784 if (title != NULL && enc_codepage >= 0 && enc_codepage != (int)GetACP())
2785 {
2786 WCHAR *wbuf;
2787 int n;
2788
2789 /* Convert the title from 'encoding' to ucs2. */
2790 wbuf = (WCHAR *)enc_to_ucs2(title, NULL);
2791 if (wbuf != NULL)
2792 {
2793 n = SetWindowTextW(s_hwnd, wbuf);
2794 vim_free(wbuf);
2795 if (n != 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
2796 return;
2797 /* Retry with non-wide function (for Windows 98). */
2798 }
2799 }
2800#endif
2801 SetWindowText(s_hwnd, (LPCSTR)(title == NULL ? "VIM" : (char *)title));
2802}
2803
2804#ifdef FEAT_MOUSESHAPE
2805/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
2806 * misc2.c! */
2807static LPCSTR mshape_idcs[] =
2808{
2809 MAKEINTRESOURCE(IDC_ARROW), /* arrow */
2810 MAKEINTRESOURCE(0), /* blank */
2811 MAKEINTRESOURCE(IDC_IBEAM), /* beam */
2812 MAKEINTRESOURCE(IDC_SIZENS), /* updown */
2813 MAKEINTRESOURCE(IDC_SIZENS), /* udsizing */
2814 MAKEINTRESOURCE(IDC_SIZEWE), /* leftright */
2815 MAKEINTRESOURCE(IDC_SIZEWE), /* lrsizing */
2816 MAKEINTRESOURCE(IDC_WAIT), /* busy */
2817#ifdef WIN3264
2818 MAKEINTRESOURCE(IDC_NO), /* no */
2819#else
2820 MAKEINTRESOURCE(IDC_ICON), /* no */
2821#endif
2822 MAKEINTRESOURCE(IDC_ARROW), /* crosshair */
2823 MAKEINTRESOURCE(IDC_ARROW), /* hand1 */
2824 MAKEINTRESOURCE(IDC_ARROW), /* hand2 */
2825 MAKEINTRESOURCE(IDC_ARROW), /* pencil */
2826 MAKEINTRESOURCE(IDC_ARROW), /* question */
2827 MAKEINTRESOURCE(IDC_ARROW), /* right-arrow */
2828 MAKEINTRESOURCE(IDC_UPARROW), /* up-arrow */
2829 MAKEINTRESOURCE(IDC_ARROW) /* last one */
2830};
2831
2832 void
2833mch_set_mouse_shape(int shape)
2834{
2835 LPCSTR idc;
2836
2837 if (shape == MSHAPE_HIDE)
2838 ShowCursor(FALSE);
2839 else
2840 {
2841 if (shape >= MSHAPE_NUMBERED)
2842 idc = MAKEINTRESOURCE(IDC_ARROW);
2843 else
2844 idc = mshape_idcs[shape];
2845#ifdef _WIN64
2846 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
2847#else
2848# ifdef WIN32
2849 SetClassLong(s_textArea, GCL_HCURSOR, (LONG)LoadCursor(NULL, idc));
2850# else
2851 SetClassWord(s_textArea, GCW_HCURSOR, (WORD)LoadCursor(NULL, idc));
2852# endif
2853#endif
2854 if (!p_mh)
2855 {
2856 POINT mp;
2857
2858 /* Set the position to make it redrawn with the new shape. */
2859 (void)GetCursorPos((LPPOINT)&mp);
2860 (void)SetCursorPos(mp.x, mp.y);
2861 ShowCursor(TRUE);
2862 }
2863 }
2864}
2865#endif
2866
2867#ifdef FEAT_BROWSE
2868/*
2869 * The file browser exists in two versions: with "W" uses wide characters,
2870 * without "W" the current codepage. When FEAT_MBYTE is defined and on
2871 * Windows NT/2000/XP the "W" functions are used.
2872 */
2873
2874# if defined(FEAT_MBYTE) && defined(WIN3264)
2875/*
2876 * Wide version of convert_filter(). Keep in sync!
2877 */
2878 static WCHAR *
2879convert_filterW(char_u *s)
2880{
2881 WCHAR *res;
2882 unsigned s_len = (unsigned)STRLEN(s);
2883 unsigned i;
2884
2885 res = (WCHAR *)alloc((s_len + 3) * sizeof(WCHAR));
2886 if (res != NULL)
2887 {
2888 for (i = 0; i < s_len; ++i)
2889 if (s[i] == '\t' || s[i] == '\n')
2890 res[i] = '\0';
2891 else
2892 res[i] = s[i];
2893 res[s_len] = NUL;
2894 /* Add two extra NULs to make sure it's properly terminated. */
2895 res[s_len + 1] = NUL;
2896 res[s_len + 2] = NUL;
2897 }
2898 return res;
2899}
2900
2901/*
2902 * Wide version of gui_mch_browse(). Keep in sync!
2903 */
2904 static char_u *
2905gui_mch_browseW(
2906 int saving,
2907 char_u *title,
2908 char_u *dflt,
2909 char_u *ext,
2910 char_u *initdir,
2911 char_u *filter)
2912{
2913 /* We always use the wide function. This means enc_to_ucs2() must work,
2914 * otherwise it fails miserably! */
2915 OPENFILENAMEW fileStruct;
2916 WCHAR fileBuf[MAXPATHL];
2917 WCHAR *wp;
2918 int i;
2919 WCHAR *titlep = NULL;
2920 WCHAR *extp = NULL;
2921 WCHAR *initdirp = NULL;
2922 WCHAR *filterp;
2923 char_u *p;
2924
2925 if (dflt == NULL)
2926 fileBuf[0] = NUL;
2927 else
2928 {
2929 wp = enc_to_ucs2(dflt, NULL);
2930 if (wp == NULL)
2931 fileBuf[0] = NUL;
2932 else
2933 {
2934 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
2935 fileBuf[i] = wp[i];
2936 fileBuf[i] = NUL;
2937 vim_free(wp);
2938 }
2939 }
2940
2941 /* Convert the filter to Windows format. */
2942 filterp = convert_filterW(filter);
2943
2944 memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
2945#ifdef OPENFILENAME_SIZE_VERSION_400
2946 /* be compatible with Windows NT 4.0 */
2947 /* TODO: what to use for OPENFILENAMEW??? */
2948 fileStruct.lStructSize = sizeof(OPENFILENAME_SIZE_VERSION_400);
2949#else
2950 fileStruct.lStructSize = sizeof(fileStruct);
2951#endif
2952
2953 if (title != NULL)
2954 titlep = enc_to_ucs2(title, NULL);
2955 fileStruct.lpstrTitle = titlep;
2956
2957 if (ext != NULL)
2958 extp = enc_to_ucs2(ext, NULL);
2959 fileStruct.lpstrDefExt = extp;
2960
2961 fileStruct.lpstrFile = fileBuf;
2962 fileStruct.nMaxFile = MAXPATHL;
2963 fileStruct.lpstrFilter = filterp;
2964 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
2965 /* has an initial dir been specified? */
2966 if (initdir != NULL && *initdir != NUL)
2967 {
2968 /* Must have backslashes here, no matter what 'shellslash' says */
2969 initdirp = enc_to_ucs2(initdir, NULL);
2970 if (initdirp != NULL)
2971 {
2972 for (wp = initdirp; *wp != NUL; ++wp)
2973 if (*wp == '/')
2974 *wp = '\\';
2975 }
2976 fileStruct.lpstrInitialDir = initdirp;
2977 }
2978
2979 /*
2980 * TODO: Allow selection of multiple files. Needs another arg to this
2981 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
2982 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
2983 * files that don't exist yet, so I haven't put it in. What about
2984 * OFN_PATHMUSTEXIST?
2985 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
2986 */
2987 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
2988#ifdef FEAT_SHORTCUT
2989 if (curbuf->b_p_bin)
2990 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
2991#endif
2992 if (saving)
2993 {
2994 if (!GetSaveFileNameW(&fileStruct))
2995 return NULL;
2996 }
2997 else
2998 {
2999 if (!GetOpenFileNameW(&fileStruct))
3000 return NULL;
3001 }
3002
3003 vim_free(filterp);
3004 vim_free(initdirp);
3005 vim_free(titlep);
3006 vim_free(extp);
3007
3008 /* Convert from UCS2 to 'encoding'. */
3009 p = ucs2_to_enc(fileBuf, NULL);
3010 if (p != NULL)
3011 /* when out of memory we get garbage for non-ASCII chars */
3012 STRCPY(fileBuf, p);
3013 vim_free(p);
3014
3015 /* Give focus back to main window (when using MDI). */
3016 SetFocus(s_hwnd);
3017
3018 /* Shorten the file name if possible */
3019 mch_dirname(IObuff, IOSIZE);
3020 p = shorten_fname((char_u *)fileBuf, IObuff);
3021 if (p == NULL)
3022 p = (char_u *)fileBuf;
3023 return vim_strsave(p);
3024}
3025# endif /* FEAT_MBYTE */
3026
3027
3028/*
3029 * Convert the string s to the proper format for a filter string by replacing
3030 * the \t and \n delimeters with \0.
3031 * Returns the converted string in allocated memory.
3032 *
3033 * Keep in sync with convert_filterW() above!
3034 */
3035 static char_u *
3036convert_filter(char_u *s)
3037{
3038 char_u *res;
3039 unsigned s_len = (unsigned)STRLEN(s);
3040 unsigned i;
3041
3042 res = alloc(s_len + 3);
3043 if (res != NULL)
3044 {
3045 for (i = 0; i < s_len; ++i)
3046 if (s[i] == '\t' || s[i] == '\n')
3047 res[i] = '\0';
3048 else
3049 res[i] = s[i];
3050 res[s_len] = NUL;
3051 /* Add two extra NULs to make sure it's properly terminated. */
3052 res[s_len + 1] = NUL;
3053 res[s_len + 2] = NUL;
3054 }
3055 return res;
3056}
3057
3058/*
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003059 * Select a directory.
3060 */
3061 char_u *
3062gui_mch_browsedir(char_u *title, char_u *initdir)
3063{
3064 /* We fake this: Use a filter that doesn't select anything and a default
3065 * file name that won't be used. */
3066 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3067 initdir, (char_u *)_("Directory\t*.nothing\n"));
3068}
3069
3070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 * Pop open a file browser and return the file selected, in allocated memory,
3072 * or NULL if Cancel is hit.
3073 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3074 * title - Title message for the file browser dialog.
3075 * dflt - Default name of file.
3076 * ext - Default extension to be added to files without extensions.
3077 * initdir - directory in which to open the browser (NULL = current dir)
3078 * filter - Filter for matched files to choose from.
3079 *
3080 * Keep in sync with gui_mch_browseW() above!
3081 */
3082 char_u *
3083gui_mch_browse(
3084 int saving,
3085 char_u *title,
3086 char_u *dflt,
3087 char_u *ext,
3088 char_u *initdir,
3089 char_u *filter)
3090{
3091 OPENFILENAME fileStruct;
3092 char_u fileBuf[MAXPATHL];
3093 char_u *initdirp = NULL;
3094 char_u *filterp;
3095 char_u *p;
3096
3097# if defined(FEAT_MBYTE) && defined(WIN3264)
3098 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
3099 return gui_mch_browseW(saving, title, dflt, ext, initdir, filter);
3100# endif
3101
3102 if (dflt == NULL)
3103 fileBuf[0] = NUL;
3104 else
3105 {
3106 STRNCPY(fileBuf, dflt, MAXPATHL - 1);
3107 fileBuf[MAXPATHL - 1] = NUL;
3108 }
3109
3110 /* Convert the filter to Windows format. */
3111 filterp = convert_filter(filter);
3112
3113 memset(&fileStruct, 0, sizeof(OPENFILENAME));
3114#ifdef OPENFILENAME_SIZE_VERSION_400
3115 /* be compatible with Windows NT 4.0 */
3116 fileStruct.lStructSize = sizeof(OPENFILENAME_SIZE_VERSION_400);
3117#else
3118 fileStruct.lStructSize = sizeof(fileStruct);
3119#endif
3120
3121 fileStruct.lpstrTitle = title;
3122 fileStruct.lpstrDefExt = ext;
3123
3124 fileStruct.lpstrFile = fileBuf;
3125 fileStruct.nMaxFile = MAXPATHL;
3126 fileStruct.lpstrFilter = filterp;
3127 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3128 /* has an initial dir been specified? */
3129 if (initdir != NULL && *initdir != NUL)
3130 {
3131 /* Must have backslashes here, no matter what 'shellslash' says */
3132 initdirp = vim_strsave(initdir);
3133 if (initdirp != NULL)
3134 for (p = initdirp; *p != NUL; ++p)
3135 if (*p == '/')
3136 *p = '\\';
3137 fileStruct.lpstrInitialDir = initdirp;
3138 }
3139
3140 /*
3141 * TODO: Allow selection of multiple files. Needs another arg to this
3142 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3143 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3144 * files that don't exist yet, so I haven't put it in. What about
3145 * OFN_PATHMUSTEXIST?
3146 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3147 */
3148 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
3149#ifdef FEAT_SHORTCUT
3150 if (curbuf->b_p_bin)
3151 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
3152#endif
3153 if (saving)
3154 {
3155 if (!GetSaveFileName(&fileStruct))
3156 return NULL;
3157 }
3158 else
3159 {
3160 if (!GetOpenFileName(&fileStruct))
3161 return NULL;
3162 }
3163
3164 vim_free(filterp);
3165 vim_free(initdirp);
3166
3167 /* Give focus back to main window (when using MDI). */
3168 SetFocus(s_hwnd);
3169
3170 /* Shorten the file name if possible */
3171 mch_dirname(IObuff, IOSIZE);
3172 p = shorten_fname((char_u *)fileBuf, IObuff);
3173 if (p == NULL)
3174 p = (char_u *)fileBuf;
3175 return vim_strsave(p);
3176}
3177#endif /* FEAT_BROWSE */
3178
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003179/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 static void
3181_OnDropFiles(
3182 HWND hwnd,
3183 HDROP hDrop)
3184{
3185#ifdef FEAT_WINDOWS
3186#ifdef WIN3264
3187# define BUFPATHLEN _MAX_PATH
3188# define DRAGQVAL 0xFFFFFFFF
3189#else
3190# define BUFPATHLEN MAXPATHL
3191# define DRAGQVAL 0xFFFF
3192#endif
3193#ifdef FEAT_MBYTE
3194 WCHAR wszFile[BUFPATHLEN];
3195#endif
3196 char szFile[BUFPATHLEN];
3197 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3198 UINT i;
3199 char_u **fnames;
3200 POINT pt;
3201 int_u modifiers = 0;
3202
3203 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3204
3205 /* Obtain dropped position */
3206 DragQueryPoint(hDrop, &pt);
3207 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3208
3209# ifdef FEAT_VISUAL
3210 reset_VIsual();
3211# endif
3212
3213 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3214
3215 if (fnames != NULL)
3216 for (i = 0; i < cFiles; ++i)
3217 {
3218#ifdef FEAT_MBYTE
3219 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3220 fnames[i] = ucs2_to_enc(wszFile, NULL);
3221 else
3222#endif
3223 {
3224 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3225 fnames[i] = vim_strsave(szFile);
3226 }
3227 }
3228
3229 DragFinish(hDrop);
3230
3231 if (fnames != NULL)
3232 {
3233 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3234 modifiers |= MOUSE_SHIFT;
3235 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3236 modifiers |= MOUSE_CTRL;
3237 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3238 modifiers |= MOUSE_ALT;
3239
3240 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3241
3242 s_need_activate = TRUE;
3243 }
3244#endif
3245}
3246
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003247/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 static int
3249_OnScroll(
3250 HWND hwnd,
3251 HWND hwndCtl,
3252 UINT code,
3253 int pos)
3254{
3255 static UINT prev_code = 0; /* code of previous call */
3256 scrollbar_T *sb, *sb_info;
3257 long val;
3258 int dragging = FALSE;
3259 int dont_scroll_save = dont_scroll;
3260#ifndef WIN3264
3261 int nPos;
3262#else
3263 SCROLLINFO si;
3264
3265 si.cbSize = sizeof(si);
3266 si.fMask = SIF_POS;
3267#endif
3268
3269 sb = gui_mswin_find_scrollbar(hwndCtl);
3270 if (sb == NULL)
3271 return 0;
3272
3273 if (sb->wp != NULL) /* Left or right scrollbar */
3274 {
3275 /*
3276 * Careful: need to get scrollbar info out of first (left) scrollbar
3277 * for window, but keep real scrollbar too because we must pass it to
3278 * gui_drag_scrollbar().
3279 */
3280 sb_info = &sb->wp->w_scrollbars[0];
3281 }
3282 else /* Bottom scrollbar */
3283 sb_info = sb;
3284 val = sb_info->value;
3285
3286 switch (code)
3287 {
3288 case SB_THUMBTRACK:
3289 val = pos;
3290 dragging = TRUE;
3291 if (sb->scroll_shift > 0)
3292 val <<= sb->scroll_shift;
3293 break;
3294 case SB_LINEDOWN:
3295 val++;
3296 break;
3297 case SB_LINEUP:
3298 val--;
3299 break;
3300 case SB_PAGEDOWN:
3301 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3302 break;
3303 case SB_PAGEUP:
3304 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3305 break;
3306 case SB_TOP:
3307 val = 0;
3308 break;
3309 case SB_BOTTOM:
3310 val = sb_info->max;
3311 break;
3312 case SB_ENDSCROLL:
3313 if (prev_code == SB_THUMBTRACK)
3314 {
3315 /*
3316 * "pos" only gives us 16-bit data. In case of large file,
3317 * use GetScrollPos() which returns 32-bit. Unfortunately it
3318 * is not valid while the scrollbar is being dragged.
3319 */
3320 val = GetScrollPos(hwndCtl, SB_CTL);
3321 if (sb->scroll_shift > 0)
3322 val <<= sb->scroll_shift;
3323 }
3324 break;
3325
3326 default:
3327 /* TRACE("Unknown scrollbar event %d\n", code); */
3328 return 0;
3329 }
3330 prev_code = code;
3331
3332#ifdef WIN3264
3333 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3334 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
3335#else
3336 nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3337 SetScrollPos(hwndCtl, SB_CTL, nPos, TRUE);
3338#endif
3339
3340 /*
3341 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3342 */
3343 if (sb->wp != NULL)
3344 {
3345 scrollbar_T *sba = sb->wp->w_scrollbars;
3346 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3347
3348#ifdef WIN3264
3349 SetScrollInfo(id, SB_CTL, &si, TRUE);
3350#else
3351 SetScrollPos(id, SB_CTL, nPos, TRUE);
3352#endif
3353 }
3354
3355 /* Don't let us be interrupted here by another message. */
3356 s_busy_processing = TRUE;
3357
3358 /* When "allow_scrollbar" is FALSE still need to remember the new
3359 * position, but don't actually scroll by setting "dont_scroll". */
3360 dont_scroll = !allow_scrollbar;
3361
3362 gui_drag_scrollbar(sb, val, dragging);
3363
3364 s_busy_processing = FALSE;
3365 dont_scroll = dont_scroll_save;
3366
3367 return 0;
3368}
3369
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003370
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371/*
3372 * Get command line arguments.
3373 * Use "prog" as the name of the program and "cmdline" as the arguments.
3374 * Copy the arguments to allocated memory.
3375 * Return the number of arguments (including program name).
3376 * Return pointers to the arguments in "argvp".
3377 * Return pointer to buffer in "tofree".
3378 * Returns zero when out of memory.
3379 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003380/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 int
3382get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
3383{
3384 int i;
3385 char *p;
3386 char *progp;
3387 char *pnew = NULL;
3388 char *newcmdline;
3389 int inquote;
3390 int argc;
3391 char **argv = NULL;
3392 int round;
3393
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003394#ifdef FEAT_MBYTE
3395 /* Try using the Unicode version first, it takes care of conversion when
3396 * 'encoding' is changed. */
3397 argc = get_cmd_argsW(&argv);
3398 if (argc != 0)
3399 goto done;
3400#endif
3401
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 /* Handle the program name. Remove the ".exe" extension, and find the 1st
3403 * non-space. */
3404 p = strrchr(prog, '.');
3405 if (p != NULL)
3406 *p = NUL;
3407 for (progp = prog; *progp == ' '; ++progp)
3408 ;
3409
3410 /* The command line is copied to allocated memory, so that we can change
3411 * it. Add the size of the string, the separating NUL and a terminating
3412 * NUL. */
3413 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
3414 if (newcmdline == NULL)
3415 return 0;
3416
3417 /*
3418 * First round: count the number of arguments ("pnew" == NULL).
3419 * Second round: produce the arguments.
3420 */
3421 for (round = 1; round <= 2; ++round)
3422 {
3423 /* First argument is the program name. */
3424 if (pnew != NULL)
3425 {
3426 argv[0] = pnew;
3427 strcpy(pnew, progp);
3428 pnew += strlen(pnew);
3429 *pnew++ = NUL;
3430 }
3431
3432 /*
3433 * Isolate each argument and put it in argv[].
3434 */
3435 p = cmdline;
3436 argc = 1;
3437 while (*p != NUL)
3438 {
3439 inquote = FALSE;
3440 if (pnew != NULL)
3441 argv[argc] = pnew;
3442 ++argc;
3443 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
3444 {
3445 /* Backslashes are only special when followed by a double
3446 * quote. */
3447 i = strspn(p, "\\");
3448 if (p[i] == '"')
3449 {
3450 /* Halve the number of backslashes. */
3451 if (i > 1 && pnew != NULL)
3452 {
3453 memset(pnew, '\\', i / 2);
3454 pnew += i / 2;
3455 }
3456
3457 /* Even nr of backslashes toggles quoting, uneven copies
3458 * the double quote. */
3459 if ((i & 1) == 0)
3460 inquote = !inquote;
3461 else if (pnew != NULL)
3462 *pnew++ = '"';
3463 p += i + 1;
3464 }
3465 else if (i > 0)
3466 {
3467 /* Copy span of backslashes unmodified. */
3468 if (pnew != NULL)
3469 {
3470 memset(pnew, '\\', i);
3471 pnew += i;
3472 }
3473 p += i;
3474 }
3475 else
3476 {
3477 if (pnew != NULL)
3478 *pnew++ = *p;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00003479#ifdef FEAT_MBYTE
3480 /* Can't use mb_* functions, because 'encoding' is not
3481 * initialized yet here. */
3482 if (IsDBCSLeadByte(*p))
3483 {
3484 ++p;
3485 if (pnew != NULL)
3486 *pnew++ = *p;
3487 }
3488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 ++p;
3490 }
3491 }
3492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003493 if (pnew != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 *pnew++ = NUL;
3495 while (*p == ' ' || *p == '\t')
3496 ++p; /* advance until a non-space */
3497 }
3498
3499 if (round == 1)
3500 {
3501 argv = (char **)malloc((argc + 1) * sizeof(char *));
3502 if (argv == NULL )
3503 return 0; /* malloc error */
3504 pnew = newcmdline;
3505 }
3506 }
3507
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003508done:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003510 argv[argc] = NULL; /* NULL-terminated list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 *argvp = argv;
3512 return argc;
3513}