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