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