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