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