blob: 76ed6b79db91a05f1002a9481db9aad0f5857f78 [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;
Bram Moolenaarea408882007-12-03 21:21:03 +0000293
294/* s_getting_focus is set when we got focus but didn't see mouse-up event yet,
295 * so don't reset s_button_pending. */
296static int s_getting_focus = FALSE;
297
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298static int s_x_pending;
299static int s_y_pending;
300static UINT s_kFlags_pending;
301static UINT s_wait_timer = 0; /* Timer for get char from user */
302static int s_timed_out = FALSE;
303static int dead_key = 0; /* 0 - no dead key, 1 - dead key pressed */
304
305#ifdef WIN3264
306static OSVERSIONINFO os_version; /* like it says. Init in gui_mch_init() */
307#endif
308
309#ifdef FEAT_BEVAL
310/* balloon-eval WM_NOTIFY_HANDLER */
311static void Handle_WM_Notify __ARGS((HWND hwnd, LPNMHDR pnmh));
312static void TrackUserActivity __ARGS((UINT uMsg));
313#endif
314
315/*
316 * For control IME.
317 */
318#ifdef FEAT_MBYTE
319# ifdef USE_IM_CONTROL
320static LOGFONT norm_logfont;
321# endif
322#endif
323
324#ifdef FEAT_MBYTE_IME
325static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
326#endif
327
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000328#ifdef DEBUG_PRINT_ERROR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329/*
330 * Print out the last Windows error message
331 */
332 static void
333print_windows_error(void)
334{
335 LPVOID lpMsgBuf;
336
337 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
338 NULL, GetLastError(),
339 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
340 (LPTSTR) &lpMsgBuf, 0, NULL);
341 TRACE1("Error: %s\n", lpMsgBuf);
342 LocalFree(lpMsgBuf);
343}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345
346/*
347 * Cursor blink functions.
348 *
349 * This is a simple state machine:
350 * BLINK_NONE not blinking at all
351 * BLINK_OFF blinking, cursor is not shown
352 * BLINK_ON blinking, cursor is shown
353 */
354
355#define BLINK_NONE 0
356#define BLINK_OFF 1
357#define BLINK_ON 2
358
359static int blink_state = BLINK_NONE;
360static long_u blink_waittime = 700;
361static long_u blink_ontime = 400;
362static long_u blink_offtime = 250;
363static UINT blink_timer = 0;
364
365 void
366gui_mch_set_blinking(long wait, long on, long off)
367{
368 blink_waittime = wait;
369 blink_ontime = on;
370 blink_offtime = off;
371}
372
373/* ARGSUSED */
374 static VOID CALLBACK
375_OnBlinkTimer(
376 HWND hwnd,
377 UINT uMsg,
378 UINT idEvent,
379 DWORD dwTime)
380{
381 MSG msg;
382
383 /*
384 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
385 */
386
387 KillTimer(NULL, idEvent);
388
389 /* Eat spurious WM_TIMER messages */
390 while (PeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
391 ;
392
393 if (blink_state == BLINK_ON)
394 {
395 gui_undraw_cursor();
396 blink_state = BLINK_OFF;
397 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
398 (TIMERPROC)_OnBlinkTimer);
399 }
400 else
401 {
402 gui_update_cursor(TRUE, FALSE);
403 blink_state = BLINK_ON;
404 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
405 (TIMERPROC)_OnBlinkTimer);
406 }
407}
408
409 static void
410gui_mswin_rm_blink_timer(void)
411{
412 MSG msg;
413
414 if (blink_timer != 0)
415 {
416 KillTimer(NULL, blink_timer);
417 /* Eat spurious WM_TIMER messages */
418 while (PeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
419 ;
420 blink_timer = 0;
421 }
422}
423
424/*
425 * Stop the cursor blinking. Show the cursor if it wasn't shown.
426 */
427 void
428gui_mch_stop_blink(void)
429{
430 gui_mswin_rm_blink_timer();
431 if (blink_state == BLINK_OFF)
432 gui_update_cursor(TRUE, FALSE);
433 blink_state = BLINK_NONE;
434}
435
436/*
437 * Start the cursor blinking. If it was already blinking, this restarts the
438 * waiting time and shows the cursor.
439 */
440 void
441gui_mch_start_blink(void)
442{
443 gui_mswin_rm_blink_timer();
444
445 /* Only switch blinking on if none of the times is zero */
446 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
447 {
448 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
449 (TIMERPROC)_OnBlinkTimer);
450 blink_state = BLINK_ON;
451 gui_update_cursor(TRUE, FALSE);
452 }
453}
454
455/*
456 * Call-back routines.
457 */
458
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000459/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 static VOID CALLBACK
461_OnTimer(
462 HWND hwnd,
463 UINT uMsg,
464 UINT idEvent,
465 DWORD dwTime)
466{
467 MSG msg;
468
469 /*
470 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
471 */
472 KillTimer(NULL, idEvent);
473 s_timed_out = TRUE;
474
475 /* Eat spurious WM_TIMER messages */
476 while (PeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
477 ;
478 if (idEvent == s_wait_timer)
479 s_wait_timer = 0;
480}
481
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000482/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 static void
484_OnDeadChar(
485 HWND hwnd,
486 UINT ch,
487 int cRepeat)
488{
489 dead_key = 1;
490}
491
492/*
493 * Convert Unicode character "ch" to bytes in "string[slen]".
Bram Moolenaar85a3e5c2007-11-20 16:22:16 +0000494 * When "had_alt" is TRUE the ALT key was included in "ch".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 * Return the length.
496 */
497 static int
Bram Moolenaar85a3e5c2007-11-20 16:22:16 +0000498char_to_string(int ch, char_u *string, int slen, int had_alt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499{
500 int len;
501 int i;
502#ifdef FEAT_MBYTE
503 WCHAR wstring[2];
504 char_u *ws = NULL;;
505
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000506 if (os_version.dwPlatformId != VER_PLATFORM_WIN32_NT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000508 /* On Windows 95/98 we apparently get the character in the active
509 * codepage, not in UCS-2. If conversion is needed convert it to
510 * UCS-2 first. */
511 if ((int)GetACP() == enc_codepage)
512 len = 0; /* no conversion required */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 else
514 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000515 string[0] = ch;
516 len = MultiByteToWideChar(GetACP(), 0, string, 1, wstring, 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 }
518 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000519 else
520 {
521 wstring[0] = ch;
522 len = 1;
523 }
524
525 if (len > 0)
526 {
527 /* "ch" is a UTF-16 character. Convert it to a string of bytes. When
528 * "enc_codepage" is non-zero use the standard Win32 function,
529 * otherwise use our own conversion function (e.g., for UTF-8). */
530 if (enc_codepage > 0)
Bram Moolenaar85a3e5c2007-11-20 16:22:16 +0000531 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000532 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
533 string, slen, 0, NULL);
Bram Moolenaar85a3e5c2007-11-20 16:22:16 +0000534 /* If we had included the ALT key into the character but now the
535 * upper bit is no longer set, that probably means the conversion
536 * failed. Convert the original character and set the upper bit
537 * afterwards. */
538 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
539 {
540 wstring[0] = ch & 0x7f;
541 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
542 string, slen, 0, NULL);
543 if (len == 1) /* safety check */
544 string[0] |= 0x80;
545 }
546 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000547 else
548 {
549 len = 1;
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000550 ws = utf16_to_enc(wstring, &len);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000551 if (ws == NULL)
552 len = 0;
553 else
554 {
555 if (len > slen) /* just in case */
556 len = slen;
557 mch_memmove(string, ws, len);
558 vim_free(ws);
559 }
560 }
561 }
562
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 if (len == 0)
564#endif
565 {
566 string[0] = ch;
567 len = 1;
568 }
569
570 for (i = 0; i < len; ++i)
571 if (string[i] == CSI && len <= slen - 2)
572 {
573 /* Insert CSI as K_CSI. */
574 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
575 string[++i] = KS_EXTRA;
576 string[++i] = (int)KE_CSI;
577 len += 2;
578 }
579
580 return len;
581}
582
583/*
584 * Key hit, add it to the input buffer.
585 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000586/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 static void
588_OnChar(
589 HWND hwnd,
590 UINT ch,
591 int cRepeat)
592{
593 char_u string[40];
594 int len = 0;
595
Bram Moolenaar85a3e5c2007-11-20 16:22:16 +0000596 len = char_to_string(ch, string, 40, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
598 {
599 trash_input_buf();
600 got_int = TRUE;
601 }
602
603 add_to_input_buf(string, len);
604}
605
606/*
607 * Alt-Key hit, add it to the input buffer.
608 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000609/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 static void
611_OnSysChar(
612 HWND hwnd,
613 UINT cch,
614 int cRepeat)
615{
616 char_u string[40]; /* Enough for multibyte character */
617 int len;
618 int modifiers;
619 int ch = cch; /* special keys are negative */
620
621 /* TRACE("OnSysChar(%d, %c)\n", ch, ch); */
622
623 /* OK, we have a character key (given by ch) which was entered with the
624 * ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
625 * that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
626 * CAPSLOCK is pressed) at this point.
627 */
628 modifiers = MOD_MASK_ALT;
629 if (GetKeyState(VK_SHIFT) & 0x8000)
630 modifiers |= MOD_MASK_SHIFT;
631 if (GetKeyState(VK_CONTROL) & 0x8000)
632 modifiers |= MOD_MASK_CTRL;
633
634 ch = simplify_key(ch, &modifiers);
635 /* remove the SHIFT modifier for keys where it's already included, e.g.,
636 * '(' and '*' */
637 if (ch < 0x100 && !isalpha(ch) && isprint(ch))
638 modifiers &= ~MOD_MASK_SHIFT;
639
640 /* Interpret the ALT key as making the key META, include SHIFT, etc. */
641 ch = extract_modifiers(ch, &modifiers);
642 if (ch == CSI)
643 ch = K_CSI;
644
645 len = 0;
646 if (modifiers)
647 {
648 string[len++] = CSI;
649 string[len++] = KS_MODIFIER;
650 string[len++] = modifiers;
651 }
652
653 if (IS_SPECIAL((int)ch))
654 {
655 string[len++] = CSI;
656 string[len++] = K_SECOND((int)ch);
657 string[len++] = K_THIRD((int)ch);
658 }
659 else
660 {
661 /* Although the documentation isn't clear about it, we assume "ch" is
662 * a Unicode character. */
Bram Moolenaar85a3e5c2007-11-20 16:22:16 +0000663 len += char_to_string(ch, string + len, 40 - len, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
665
666 add_to_input_buf(string, len);
667}
668
669 static void
670_OnMouseEvent(
671 int button,
672 int x,
673 int y,
674 int repeated_click,
675 UINT keyFlags)
676{
677 int vim_modifiers = 0x0;
678
Bram Moolenaarea408882007-12-03 21:21:03 +0000679 s_getting_focus = FALSE;
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 if (keyFlags & MK_SHIFT)
682 vim_modifiers |= MOUSE_SHIFT;
683 if (keyFlags & MK_CONTROL)
684 vim_modifiers |= MOUSE_CTRL;
685 if (GetKeyState(VK_MENU) & 0x8000)
686 vim_modifiers |= MOUSE_ALT;
687
688 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
689}
690
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000691/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 static void
693_OnMouseButtonDown(
694 HWND hwnd,
695 BOOL fDoubleClick,
696 int x,
697 int y,
698 UINT keyFlags)
699{
700 static LONG s_prevTime = 0;
701
702 LONG currentTime = GetMessageTime();
703 int button = -1;
704 int repeated_click;
705
706 /* Give main window the focus: this is so the cursor isn't hollow. */
707 (void)SetFocus(s_hwnd);
708
709 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
710 button = MOUSE_LEFT;
711 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
712 button = MOUSE_MIDDLE;
713 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
714 button = MOUSE_RIGHT;
715#ifndef WIN16 /*<VN>*/
716 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
717 {
718#ifndef GET_XBUTTON_WPARAM
719# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
720#endif
721 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
722 }
723 else if (s_uMsg == WM_CAPTURECHANGED)
724 {
725 /* on W95/NT4, somehow you get in here with an odd Msg
726 * if you press one button while holding down the other..*/
727 if (s_button_pending == MOUSE_LEFT)
728 button = MOUSE_RIGHT;
729 else
730 button = MOUSE_LEFT;
731 }
732#endif
733 if (button >= 0)
734 {
735 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
736
737 /*
738 * Holding down the left and right buttons simulates pushing the middle
739 * button.
740 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000741 if (repeated_click
742 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
743 || (button == MOUSE_RIGHT
744 && s_button_pending == MOUSE_LEFT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 {
746 /*
747 * Hmm, gui.c will ignore more than one button down at a time, so
748 * pretend we let go of it first.
749 */
750 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
751 button = MOUSE_MIDDLE;
752 repeated_click = FALSE;
753 s_button_pending = -1;
754 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
755 }
756 else if ((repeated_click)
757 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
758 {
759 if (s_button_pending > -1)
760 {
761 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
762 s_button_pending = -1;
763 }
764 /* TRACE("Button down at x %d, y %d\n", x, y); */
765 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
766 }
767 else
768 {
769 /*
770 * If this is the first press (i.e. not a multiple click) don't
771 * action immediately, but store and wait for:
772 * i) button-up
773 * ii) mouse move
774 * iii) another button press
775 * before using it.
776 * This enables us to make left+right simulate middle button,
777 * without left or right being actioned first. The side-effect is
778 * that if you click and hold the mouse without dragging, the
779 * cursor doesn't move until you release the button. In practice
780 * this is hardly a problem.
781 */
782 s_button_pending = button;
783 s_x_pending = x;
784 s_y_pending = y;
785 s_kFlags_pending = keyFlags;
786 }
787
788 s_prevTime = currentTime;
789 }
790}
791
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000792/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 static void
794_OnMouseMoveOrRelease(
795 HWND hwnd,
796 int x,
797 int y,
798 UINT keyFlags)
799{
800 int button;
801
Bram Moolenaarea408882007-12-03 21:21:03 +0000802 s_getting_focus = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 if (s_button_pending > -1)
804 {
805 /* Delayed action for mouse down event */
806 _OnMouseEvent(s_button_pending, s_x_pending,
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000807 s_y_pending, FALSE, s_kFlags_pending);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 s_button_pending = -1;
809 }
810 if (s_uMsg == WM_MOUSEMOVE)
811 {
812 /*
813 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
814 * down.
815 */
816 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
817 | MK_XBUTTON1 | MK_XBUTTON2)))
818 {
819 gui_mouse_moved(x, y);
820 return;
821 }
822
823 /*
824 * While button is down, keep grabbing mouse move events when
825 * the mouse goes outside the window
826 */
827 SetCapture(s_textArea);
828 button = MOUSE_DRAG;
829 /* TRACE(" move at x %d, y %d\n", x, y); */
830 }
831 else
832 {
833 ReleaseCapture();
834 button = MOUSE_RELEASE;
835 /* TRACE(" up at x %d, y %d\n", x, y); */
836 }
837
838 _OnMouseEvent(button, x, y, FALSE, keyFlags);
839}
840
841#ifdef FEAT_MENU
842/*
843 * Find the vimmenu_T with the given id
844 */
845 static vimmenu_T *
846gui_mswin_find_menu(
847 vimmenu_T *pMenu,
848 int id)
849{
850 vimmenu_T *pChildMenu;
851
852 while (pMenu)
853 {
854 if (pMenu->id == (UINT)id)
855 break;
856 if (pMenu->children != NULL)
857 {
858 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
859 if (pChildMenu)
860 {
861 pMenu = pChildMenu;
862 break;
863 }
864 }
865 pMenu = pMenu->next;
866 }
867 return pMenu;
868}
869
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000870/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 static void
872_OnMenu(
873 HWND hwnd,
874 int id,
875 HWND hwndCtl,
876 UINT codeNotify)
877{
878 vimmenu_T *pMenu;
879
880 pMenu = gui_mswin_find_menu(root_menu, id);
881 if (pMenu)
882 gui_menu_cb(pMenu);
883}
884#endif
885
886#ifdef MSWIN_FIND_REPLACE
887/*
888 * Handle a Find/Replace window message.
889 */
890 static void
891_OnFindRepl(void)
892{
893 int flags = 0;
894 int down;
895
896 if (s_findrep_struct.Flags & FR_DIALOGTERM)
897 /* Give main window the focus back. */
898 (void)SetFocus(s_hwnd);
899
900 if (s_findrep_struct.Flags & FR_FINDNEXT)
901 {
902 flags = FRD_FINDNEXT;
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_REPLACE)
909 {
910 flags = FRD_REPLACE;
911
912 /* Give main window the focus back: this is so the cursor isn't
913 * hollow. */
914 (void)SetFocus(s_hwnd);
915 }
916 else if (s_findrep_struct.Flags & FR_REPLACEALL)
917 {
918 flags = FRD_REPLACEALL;
919 }
920
921 if (flags != 0)
922 {
923 /* Call the generic GUI function to do the actual work. */
924 if (s_findrep_struct.Flags & FR_WHOLEWORD)
925 flags |= FRD_WHOLE_WORD;
926 if (s_findrep_struct.Flags & FR_MATCHCASE)
927 flags |= FRD_MATCH_CASE;
928 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
929 gui_do_findrepl(flags, s_findrep_struct.lpstrFindWhat,
930 s_findrep_struct.lpstrReplaceWith, down);
931 }
932}
933#endif
934
935 static void
936HandleMouseHide(UINT uMsg, LPARAM lParam)
937{
938 static LPARAM last_lParam = 0L;
939
940 /* We sometimes get a mousemove when the mouse didn't move... */
941 if (uMsg == WM_MOUSEMOVE)
942 {
943 if (lParam == last_lParam)
944 return;
945 last_lParam = lParam;
946 }
947
948 /* Handle specially, to centralise coding. We need to be sure we catch all
949 * possible events which should cause us to restore the cursor (as it is a
950 * shared resource, we take full responsibility for it).
951 */
952 switch (uMsg)
953 {
954 case WM_KEYUP:
955 case WM_CHAR:
956 /*
957 * blank out the pointer if necessary
958 */
959 if (p_mh)
960 gui_mch_mousehide(TRUE);
961 break;
962
963 case WM_SYSKEYUP: /* show the pointer when a system-key is pressed */
964 case WM_SYSCHAR:
965 case WM_MOUSEMOVE: /* show the pointer on any mouse action */
966 case WM_LBUTTONDOWN:
967 case WM_LBUTTONUP:
968 case WM_MBUTTONDOWN:
969 case WM_MBUTTONUP:
970 case WM_RBUTTONDOWN:
971 case WM_RBUTTONUP:
972 case WM_XBUTTONDOWN:
973 case WM_XBUTTONUP:
974 case WM_NCMOUSEMOVE:
975 case WM_NCLBUTTONDOWN:
976 case WM_NCLBUTTONUP:
977 case WM_NCMBUTTONDOWN:
978 case WM_NCMBUTTONUP:
979 case WM_NCRBUTTONDOWN:
980 case WM_NCRBUTTONUP:
981 case WM_KILLFOCUS:
982 /*
983 * if the pointer is currently hidden, then we should show it.
984 */
985 gui_mch_mousehide(FALSE);
986 break;
987 }
988}
989
990 static LRESULT CALLBACK
991_TextAreaWndProc(
992 HWND hwnd,
993 UINT uMsg,
994 WPARAM wParam,
995 LPARAM lParam)
996{
997 /*
998 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
999 hwnd, uMsg, wParam, lParam);
1000 */
1001
1002 HandleMouseHide(uMsg, lParam);
1003
1004 s_uMsg = uMsg;
1005 s_wParam = wParam;
1006 s_lParam = lParam;
1007
1008#ifdef FEAT_BEVAL
1009 TrackUserActivity(uMsg);
1010#endif
1011
1012 switch (uMsg)
1013 {
1014 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1015 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1016 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1017 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1018 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1019 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1020 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1021 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1022 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1023 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1024 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1025#ifndef WIN16 /*<VN>*/
1026 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1027 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1028 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
1029#endif
1030
1031#ifdef FEAT_BEVAL
1032 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1033 return TRUE;
1034#endif
1035
1036 default:
1037 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1038 }
1039}
1040
1041#if (defined(WIN3264) && defined(FEAT_MBYTE)) \
1042 || defined(GLOBAL_IME) \
1043 || defined(PROTO)
1044# ifdef PROTO
1045typedef int WINAPI;
1046# endif
1047
1048 LRESULT WINAPI
1049vim_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1050{
1051# ifdef GLOBAL_IME
1052 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
1053# else
1054 if (wide_WindowProc)
1055 return DefWindowProcW(hwnd, message, wParam, lParam);
1056 return DefWindowProc(hwnd, message, wParam, lParam);
1057#endif
1058}
1059#endif
1060
1061/*
1062 * Called when the foreground or background color has been changed.
1063 */
1064 void
1065gui_mch_new_colors(void)
1066{
1067 /* nothing to do? */
1068}
1069
1070/*
1071 * Set the colors to their default values.
1072 */
1073 void
1074gui_mch_def_colors()
1075{
1076 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1077 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1078 gui.def_norm_pixel = gui.norm_pixel;
1079 gui.def_back_pixel = gui.back_pixel;
1080}
1081
1082/*
1083 * Open the GUI window which was created by a call to gui_mch_init().
1084 */
1085 int
1086gui_mch_open(void)
1087{
1088#ifndef SW_SHOWDEFAULT
1089# define SW_SHOWDEFAULT 10 /* Borland 5.0 doesn't have it */
1090#endif
1091 /* Actually open the window, if not already visible
1092 * (may be done already in gui_mch_set_shellsize) */
1093 if (!IsWindowVisible(s_hwnd))
1094 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1095
Bram Moolenaare2f98b92006-03-29 21:18:24 +00001096#ifdef MSWIN_FIND_REPLACE
1097 /* Init replace string here, so that we keep it when re-opening the
1098 * dialog. */
1099 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1100#endif
1101
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 return OK;
1103}
1104
1105/*
1106 * Get the position of the top left corner of the window.
1107 */
1108 int
1109gui_mch_get_winpos(int *x, int *y)
1110{
1111 RECT rect;
1112
1113 GetWindowRect(s_hwnd, &rect);
1114 *x = rect.left;
1115 *y = rect.top;
1116 return OK;
1117}
1118
1119/*
1120 * Set the position of the top left corner of the window to the given
1121 * coordinates.
1122 */
1123 void
1124gui_mch_set_winpos(int x, int y)
1125{
1126 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1127 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1128}
1129 void
1130gui_mch_set_text_area_pos(int x, int y, int w, int h)
1131{
1132 static int oldx = 0;
1133 static int oldy = 0;
1134
1135 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1136
1137#ifdef FEAT_TOOLBAR
1138 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1139 SendMessage(s_toolbarhwnd, WM_SIZE,
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001140 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001142#if defined(FEAT_GUI_TABLINE)
1143 if (showing_tabline)
1144 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001145 int top = 0;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00001146 RECT rect;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001147
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001148# ifdef FEAT_TOOLBAR
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001149 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1150 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001151# endif
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001152 GetClientRect(s_hwnd, &rect);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001153 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001154 }
1155#endif
1156
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 /* When side scroll bar is unshown, the size of window will change.
1158 * then, the text area move left or right. thus client rect should be
1159 * forcely redraw. (Yasuhiro Matsumoto) */
1160 if (oldx != x || oldy != y)
1161 {
1162 InvalidateRect(s_hwnd, NULL, FALSE);
1163 oldx = x;
1164 oldy = y;
1165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166}
1167
1168
1169/*
1170 * Scrollbar stuff:
1171 */
1172
1173 void
1174gui_mch_enable_scrollbar(
1175 scrollbar_T *sb,
1176 int flag)
1177{
1178 ShowScrollBar(sb->id, SB_CTL, flag);
1179
1180 /* TODO: When the window is maximized, the size of the window stays the
1181 * same, thus the size of the text area changes. On Win98 it's OK, on Win
1182 * NT 4.0 it's not... */
1183}
1184
1185 void
1186gui_mch_set_scrollbar_pos(
1187 scrollbar_T *sb,
1188 int x,
1189 int y,
1190 int w,
1191 int h)
1192{
Bram Moolenaar02743632005-07-25 20:42:36 +00001193 SetWindowPos(sb->id, NULL, x, y, w, h,
1194 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195}
1196
1197 void
1198gui_mch_create_scrollbar(
1199 scrollbar_T *sb,
1200 int orient) /* SBAR_VERT or SBAR_HORIZ */
1201{
1202 sb->id = CreateWindow(
1203 "SCROLLBAR", "Scrollbar",
1204 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
1205 10, /* Any value will do for now */
1206 10, /* Any value will do for now */
1207 s_hwnd, NULL,
1208 s_hinst, NULL);
1209}
1210
1211/*
1212 * Find the scrollbar with the given hwnd.
1213 */
1214 static scrollbar_T *
1215gui_mswin_find_scrollbar(HWND hwnd)
1216{
1217 win_T *wp;
1218
1219 if (gui.bottom_sbar.id == hwnd)
1220 return &gui.bottom_sbar;
1221 FOR_ALL_WINDOWS(wp)
1222 {
1223 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1224 return &wp->w_scrollbars[SBAR_LEFT];
1225 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1226 return &wp->w_scrollbars[SBAR_RIGHT];
1227 }
1228 return NULL;
1229}
1230
1231/*
1232 * Get the character size of a font.
1233 */
1234 static void
1235GetFontSize(GuiFont font)
1236{
1237 HWND hwnd = GetDesktopWindow();
1238 HDC hdc = GetWindowDC(hwnd);
1239 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
1240 TEXTMETRIC tm;
1241
1242 GetTextMetrics(hdc, &tm);
1243 gui.char_width = tm.tmAveCharWidth + tm.tmOverhang;
1244
1245 gui.char_height = tm.tmHeight
1246#ifndef MSWIN16_FASTTEXT
Bram Moolenaar02743632005-07-25 20:42:36 +00001247 + p_linespace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248#endif
Bram Moolenaar02743632005-07-25 20:42:36 +00001249 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250
1251 SelectFont(hdc, hfntOld);
1252
1253 ReleaseDC(hwnd, hdc);
1254}
1255
Bram Moolenaar02743632005-07-25 20:42:36 +00001256/*
1257 * Adjust gui.char_height (after 'linespace' was changed).
1258 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 int
Bram Moolenaar02743632005-07-25 20:42:36 +00001260gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261{
1262 GetFontSize(gui.norm_font);
1263 return OK;
1264}
1265
1266 static GuiFont
1267get_font_handle(LOGFONT *lf)
1268{
1269 HFONT font = NULL;
1270
1271 /* Load the font */
1272 font = CreateFontIndirect(lf);
1273
1274 if (font == NULL)
1275 return NOFONT;
1276
1277 return (GuiFont)font;
1278}
1279
1280 static int
1281pixels_to_points(int pixels, int vertical)
1282{
1283 int points;
1284 HWND hwnd;
1285 HDC hdc;
1286
1287 hwnd = GetDesktopWindow();
1288 hdc = GetWindowDC(hwnd);
1289
1290 points = MulDiv(pixels, 72,
1291 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1292
1293 ReleaseDC(hwnd, hdc);
1294
1295 return points;
1296}
1297
1298 GuiFont
1299gui_mch_get_font(
1300 char_u *name,
1301 int giveErrorIfMissing)
1302{
1303 LOGFONT lf;
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001304 GuiFont font = NOFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001306 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1307 font = get_font_handle(&lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 if (font == NOFONT && giveErrorIfMissing)
1309 EMSG2(_(e_font), name);
1310 return font;
1311}
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001312
Bram Moolenaardfccaf02004-12-31 20:56:11 +00001313#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001314/*
1315 * Return the name of font "font" in allocated memory.
1316 * Don't know how to get the actual name, thus use the provided name.
1317 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001318/*ARGSUSED*/
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001319 char_u *
1320gui_mch_get_fontname(font, name)
1321 GuiFont font;
1322 char_u *name;
1323{
1324 if (name == NULL)
1325 return NULL;
1326 return vim_strsave(name);
1327}
Bram Moolenaardfccaf02004-12-31 20:56:11 +00001328#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001329
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 void
1331gui_mch_free_font(GuiFont font)
1332{
1333 if (font)
1334 DeleteObject((HFONT)font);
1335}
1336
1337 static int
1338hex_digit(int c)
1339{
1340 if (VIM_ISDIGIT(c))
1341 return c - '0';
1342 c = TOLOWER_ASC(c);
1343 if (c >= 'a' && c <= 'f')
1344 return c - 'a' + 10;
1345 return -1000;
1346}
1347/*
1348 * Return the Pixel value (color) for the given color name.
1349 * Return INVALCOLOR for error.
1350 */
1351 guicolor_T
1352gui_mch_get_color(char_u *name)
1353{
1354 typedef struct guicolor_tTable
1355 {
1356 char *name;
1357 COLORREF color;
1358 } guicolor_tTable;
1359
1360 static guicolor_tTable table[] =
1361 {
1362 {"Black", RGB(0x00, 0x00, 0x00)},
1363 {"DarkGray", RGB(0x80, 0x80, 0x80)},
1364 {"DarkGrey", RGB(0x80, 0x80, 0x80)},
1365 {"Gray", RGB(0xC0, 0xC0, 0xC0)},
1366 {"Grey", RGB(0xC0, 0xC0, 0xC0)},
1367 {"LightGray", RGB(0xE0, 0xE0, 0xE0)},
1368 {"LightGrey", RGB(0xE0, 0xE0, 0xE0)},
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001369 {"Gray10", RGB(0x1A, 0x1A, 0x1A)},
1370 {"Grey10", RGB(0x1A, 0x1A, 0x1A)},
1371 {"Gray20", RGB(0x33, 0x33, 0x33)},
1372 {"Grey20", RGB(0x33, 0x33, 0x33)},
1373 {"Gray30", RGB(0x4D, 0x4D, 0x4D)},
1374 {"Grey30", RGB(0x4D, 0x4D, 0x4D)},
1375 {"Gray40", RGB(0x66, 0x66, 0x66)},
1376 {"Grey40", RGB(0x66, 0x66, 0x66)},
1377 {"Gray50", RGB(0x7F, 0x7F, 0x7F)},
1378 {"Grey50", RGB(0x7F, 0x7F, 0x7F)},
1379 {"Gray60", RGB(0x99, 0x99, 0x99)},
1380 {"Grey60", RGB(0x99, 0x99, 0x99)},
1381 {"Gray70", RGB(0xB3, 0xB3, 0xB3)},
1382 {"Grey70", RGB(0xB3, 0xB3, 0xB3)},
1383 {"Gray80", RGB(0xCC, 0xCC, 0xCC)},
1384 {"Grey80", RGB(0xCC, 0xCC, 0xCC)},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00001385 {"Gray90", RGB(0xE5, 0xE5, 0xE5)},
1386 {"Grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 {"White", RGB(0xFF, 0xFF, 0xFF)},
1388 {"DarkRed", RGB(0x80, 0x00, 0x00)},
1389 {"Red", RGB(0xFF, 0x00, 0x00)},
1390 {"LightRed", RGB(0xFF, 0xA0, 0xA0)},
1391 {"DarkBlue", RGB(0x00, 0x00, 0x80)},
1392 {"Blue", RGB(0x00, 0x00, 0xFF)},
1393 {"LightBlue", RGB(0xA0, 0xA0, 0xFF)},
1394 {"DarkGreen", RGB(0x00, 0x80, 0x00)},
1395 {"Green", RGB(0x00, 0xFF, 0x00)},
1396 {"LightGreen", RGB(0xA0, 0xFF, 0xA0)},
1397 {"DarkCyan", RGB(0x00, 0x80, 0x80)},
1398 {"Cyan", RGB(0x00, 0xFF, 0xFF)},
1399 {"LightCyan", RGB(0xA0, 0xFF, 0xFF)},
1400 {"DarkMagenta", RGB(0x80, 0x00, 0x80)},
1401 {"Magenta", RGB(0xFF, 0x00, 0xFF)},
1402 {"LightMagenta", RGB(0xFF, 0xA0, 0xFF)},
1403 {"Brown", RGB(0x80, 0x40, 0x40)},
1404 {"Yellow", RGB(0xFF, 0xFF, 0x00)},
1405 {"LightYellow", RGB(0xFF, 0xFF, 0xA0)},
1406 {"DarkYellow", RGB(0xBB, 0xBB, 0x00)},
1407 {"SeaGreen", RGB(0x2E, 0x8B, 0x57)},
1408 {"Orange", RGB(0xFF, 0xA5, 0x00)},
1409 {"Purple", RGB(0xA0, 0x20, 0xF0)},
1410 {"SlateBlue", RGB(0x6A, 0x5A, 0xCD)},
1411 {"Violet", RGB(0xEE, 0x82, 0xEE)},
1412 };
1413
1414 typedef struct SysColorTable
1415 {
1416 char *name;
1417 int color;
1418 } SysColorTable;
1419
1420 static SysColorTable sys_table[] =
1421 {
1422#ifdef WIN3264
1423 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1424 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
1425#ifndef __MINGW32__
1426 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1427#endif
1428 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1429 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1430 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1431 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1432 {"SYS_DESKTOP", COLOR_DESKTOP},
1433 {"SYS_INFOBK", COLOR_INFOBK},
1434 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1435 {"SYS_3DFACE", COLOR_3DFACE},
1436#endif
1437 {"SYS_BTNFACE", COLOR_BTNFACE},
1438 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1439 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1440 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1441 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1442 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1443 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1444 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1445 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1446 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1447 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1448 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1449 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1450 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1451 {"SYS_MENU", COLOR_MENU},
1452 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1453 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1454 {"SYS_WINDOW", COLOR_WINDOW},
1455 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1456 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1457 };
1458
1459 int r, g, b;
1460 int i;
1461
1462 if (name[0] == '#' && strlen(name) == 7)
1463 {
1464 /* Name is in "#rrggbb" format */
1465 r = hex_digit(name[1]) * 16 + hex_digit(name[2]);
1466 g = hex_digit(name[3]) * 16 + hex_digit(name[4]);
1467 b = hex_digit(name[5]) * 16 + hex_digit(name[6]);
1468 if (r < 0 || g < 0 || b < 0)
1469 return INVALCOLOR;
1470 return RGB(r, g, b);
1471 }
1472 else
1473 {
1474 /* Check if the name is one of the colors we know */
1475 for (i = 0; i < sizeof(table) / sizeof(table[0]); i++)
1476 if (STRICMP(name, table[i].name) == 0)
1477 return table[i].color;
1478 }
1479
1480 /*
1481 * Try to look up a system colour.
1482 */
1483 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1484 if (STRICMP(name, sys_table[i].name) == 0)
1485 return GetSysColor(sys_table[i].color);
1486
1487 /*
1488 * Last attempt. Look in the file "$VIMRUNTIME/rgb.txt".
1489 */
1490 {
1491#define LINE_LEN 100
1492 FILE *fd;
1493 char line[LINE_LEN];
1494 char_u *fname;
1495
1496 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
1497 if (fname == NULL)
1498 return INVALCOLOR;
1499
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001500 fd = mch_fopen((char *)fname, "rt");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 vim_free(fname);
1502 if (fd == NULL)
1503 return INVALCOLOR;
1504
1505 while (!feof(fd))
1506 {
1507 int len;
1508 int pos;
1509 char *color;
1510
1511 fgets(line, LINE_LEN, fd);
1512 len = (int)STRLEN(line);
1513
1514 if (len <= 1 || line[len-1] != '\n')
1515 continue;
1516
1517 line[len-1] = '\0';
1518
1519 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
1520 if (i != 3)
1521 continue;
1522
1523 color = line + pos;
1524
1525 if (STRICMP(color, name) == 0)
1526 {
1527 fclose(fd);
1528 return (guicolor_T) RGB(r, g, b);
1529 }
1530 }
1531
1532 fclose(fd);
1533 }
1534
1535 return INVALCOLOR;
1536}
1537/*
1538 * Return OK if the key with the termcap name "name" is supported.
1539 */
1540 int
1541gui_mch_haskey(char_u *name)
1542{
1543 int i;
1544
1545 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1546 if (name[0] == special_keys[i].vim_code0 &&
1547 name[1] == special_keys[i].vim_code1)
1548 return OK;
1549 return FAIL;
1550}
1551
1552 void
1553gui_mch_beep(void)
1554{
1555 MessageBeep(MB_OK);
1556}
1557/*
1558 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1559 */
1560 void
1561gui_mch_invert_rectangle(
1562 int r,
1563 int c,
1564 int nr,
1565 int nc)
1566{
1567 RECT rc;
1568
1569 /*
1570 * Note: InvertRect() excludes right and bottom of rectangle.
1571 */
1572 rc.left = FILL_X(c);
1573 rc.top = FILL_Y(r);
1574 rc.right = rc.left + nc * gui.char_width;
1575 rc.bottom = rc.top + nr * gui.char_height;
1576 InvertRect(s_hdc, &rc);
1577}
1578
1579/*
1580 * Iconify the GUI window.
1581 */
1582 void
1583gui_mch_iconify(void)
1584{
1585 ShowWindow(s_hwnd, SW_MINIMIZE);
1586}
1587
1588/*
1589 * Draw a cursor without focus.
1590 */
1591 void
1592gui_mch_draw_hollow_cursor(guicolor_T color)
1593{
1594 HBRUSH hbr;
1595 RECT rc;
1596
1597 /*
1598 * Note: FrameRect() excludes right and bottom of rectangle.
1599 */
1600 rc.left = FILL_X(gui.col);
1601 rc.top = FILL_Y(gui.row);
1602 rc.right = rc.left + gui.char_width;
1603#ifdef FEAT_MBYTE
1604 if (mb_lefthalve(gui.row, gui.col))
1605 rc.right += gui.char_width;
1606#endif
1607 rc.bottom = rc.top + gui.char_height;
1608 hbr = CreateSolidBrush(color);
1609 FrameRect(s_hdc, &rc, hbr);
1610 DeleteBrush(hbr);
1611}
1612/*
1613 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1614 * color "color".
1615 */
1616 void
1617gui_mch_draw_part_cursor(
1618 int w,
1619 int h,
1620 guicolor_T color)
1621{
1622 HBRUSH hbr;
1623 RECT rc;
1624
1625 /*
1626 * Note: FillRect() excludes right and bottom of rectangle.
1627 */
1628 rc.left =
1629#ifdef FEAT_RIGHTLEFT
1630 /* vertical line should be on the right of current point */
1631 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1632#endif
1633 FILL_X(gui.col);
1634 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1635 rc.right = rc.left + w;
1636 rc.bottom = rc.top + h;
1637 hbr = CreateSolidBrush(color);
1638 FillRect(s_hdc, &rc, hbr);
1639 DeleteBrush(hbr);
1640}
1641
1642/*
1643 * Process a single Windows message.
1644 * If one is not available we hang until one is.
1645 */
1646 static void
1647process_message(void)
1648{
1649 MSG msg;
1650 UINT vk = 0; /* Virtual key */
1651 char_u string[40];
1652 int i;
1653 int modifiers = 0;
1654 int key;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001655#ifdef FEAT_MENU
1656 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
1659 GetMessage(&msg, NULL, 0, 0);
1660
1661#ifdef FEAT_OLE
1662 /* Look after OLE Automation commands */
1663 if (msg.message == WM_OLE)
1664 {
1665 char_u *str = (char_u *)msg.lParam;
Bram Moolenaar370feaf2009-01-28 13:18:26 +00001666 if (str == NULL || *str == NUL)
1667 {
1668 /* Message can't be ours, forward it. Fixes problem with Ultramon
1669 * 3.0.4 */
1670 DispatchMessage(&msg);
1671 }
1672 else
1673 {
1674 add_to_input_buf(str, (int)STRLEN(str));
1675 vim_free(str); /* was allocated in CVim::SendKeys() */
1676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 return;
1678 }
1679#endif
1680
1681#ifdef FEAT_NETBEANS_INTG
1682 if (msg.message == WM_NETBEANS)
1683 {
1684 messageFromNetbeansW32();
1685 return;
1686 }
1687#endif
1688
1689#ifdef FEAT_SNIFF
1690 if (sniff_request_waiting && want_sniff_request)
1691 {
1692 static char_u bytes[3] = {CSI, (char_u)KS_EXTRA, (char_u)KE_SNIFF};
1693 add_to_input_buf(bytes, 3); /* K_SNIFF */
1694 sniff_request_waiting = 0;
1695 want_sniff_request = 0;
1696 /* request is handled in normal.c */
1697 }
1698 if (msg.message == WM_USER)
Bram Moolenaar35c92912006-06-22 17:34:26 +00001699 {
Bram Moolenaard8a92d72006-06-23 14:44:06 +00001700 MyTranslateMessage(&msg);
Bram Moolenaar35c92912006-06-22 17:34:26 +00001701 DispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 return;
Bram Moolenaar35c92912006-06-22 17:34:26 +00001703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704#endif
1705
1706#ifdef MSWIN_FIND_REPLACE
1707 /* Don't process messages used by the dialog */
1708 if (s_findrep_hwnd != NULL && IsDialogMessage(s_findrep_hwnd, &msg))
1709 {
1710 HandleMouseHide(msg.message, msg.lParam);
1711 return;
1712 }
1713#endif
1714
1715 /*
1716 * Check if it's a special key that we recognise. If not, call
1717 * TranslateMessage().
1718 */
1719 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1720 {
1721 vk = (int) msg.wParam;
1722 /* handle key after dead key, but ignore shift, alt and control */
1723 if (dead_key && vk != VK_SHIFT && vk != VK_MENU && vk != VK_CONTROL)
1724 {
1725 dead_key = 0;
1726 /* handle non-alphabetic keys (ones that hopefully cannot generate
1727 * umlaut-characters), unless when control is down */
1728 if (vk < 'A' || vk > 'Z' || (GetKeyState(VK_CONTROL) & 0x8000))
1729 {
1730 MSG dm;
1731
1732 dm.message = msg.message;
1733 dm.hwnd = msg.hwnd;
1734 dm.wParam = VK_SPACE;
1735 MyTranslateMessage(&dm); /* generate dead character */
1736 if (vk != VK_SPACE) /* and send current character once more */
1737 PostMessage(msg.hwnd, msg.message, msg.wParam, msg.lParam);
1738 return;
1739 }
1740 }
1741
1742 /* Check for CTRL-BREAK */
1743 if (vk == VK_CANCEL)
1744 {
1745 trash_input_buf();
1746 got_int = TRUE;
1747 string[0] = Ctrl_C;
1748 add_to_input_buf(string, 1);
1749 }
1750
1751 for (i = 0; special_keys[i].key_sym != 0; i++)
1752 {
1753 /* ignore VK_SPACE when ALT key pressed: system menu */
1754 if (special_keys[i].key_sym == vk
1755 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1756 {
1757#ifdef FEAT_MENU
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001758 /* Check for <F10>: Windows selects the menu. When <F10> is
1759 * mapped we want to use the mapping instead. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 if (vk == VK_F10
1761 && gui.menu_is_active
Bram Moolenaar8d6ea5e2006-03-18 21:31:46 +00001762 && check_map(k10, State, FALSE, TRUE, FALSE) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 break;
1764#endif
1765 if (GetKeyState(VK_SHIFT) & 0x8000)
1766 modifiers |= MOD_MASK_SHIFT;
1767 /*
1768 * Don't use caps-lock as shift, because these are special keys
1769 * being considered here, and we only want letters to get
1770 * shifted -- webb
1771 */
1772 /*
1773 if (GetKeyState(VK_CAPITAL) & 0x0001)
1774 modifiers ^= MOD_MASK_SHIFT;
1775 */
1776 if (GetKeyState(VK_CONTROL) & 0x8000)
1777 modifiers |= MOD_MASK_CTRL;
1778 if (GetKeyState(VK_MENU) & 0x8000)
1779 modifiers |= MOD_MASK_ALT;
1780
1781 if (special_keys[i].vim_code1 == NUL)
1782 key = special_keys[i].vim_code0;
1783 else
1784 key = TO_SPECIAL(special_keys[i].vim_code0,
1785 special_keys[i].vim_code1);
1786 key = simplify_key(key, &modifiers);
1787 if (key == CSI)
1788 key = K_CSI;
1789
1790 if (modifiers)
1791 {
1792 string[0] = CSI;
1793 string[1] = KS_MODIFIER;
1794 string[2] = modifiers;
1795 add_to_input_buf(string, 3);
1796 }
1797
1798 if (IS_SPECIAL(key))
1799 {
1800 string[0] = CSI;
1801 string[1] = K_SECOND(key);
1802 string[2] = K_THIRD(key);
1803 add_to_input_buf(string, 3);
1804 }
1805 else
1806 {
1807 int len;
1808
1809 /* Handle "key" as a Unicode character. */
Bram Moolenaar85a3e5c2007-11-20 16:22:16 +00001810 len = char_to_string(key, string, 40, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 add_to_input_buf(string, len);
1812 }
1813 break;
1814 }
1815 }
1816 if (special_keys[i].key_sym == 0)
1817 {
1818 /* Some keys need C-S- where they should only need C-.
1819 * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1820 * system startup (Helmut Stiegler, 2003 Oct 3). */
1821 if (vk != 0xff
1822 && (GetKeyState(VK_CONTROL) & 0x8000)
1823 && !(GetKeyState(VK_SHIFT) & 0x8000)
1824 && !(GetKeyState(VK_MENU) & 0x8000))
1825 {
1826 /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */
1827 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1828 {
1829 string[0] = Ctrl_HAT;
1830 add_to_input_buf(string, 1);
1831 }
1832 /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */
1833 else if (vk == 0xBD) /* QWERTY for CTRL-'-' */
1834 {
1835 string[0] = Ctrl__;
1836 add_to_input_buf(string, 1);
1837 }
1838 /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */
1839 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
1840 {
1841 string[0] = Ctrl_AT;
1842 add_to_input_buf(string, 1);
1843 }
1844 else
1845 MyTranslateMessage(&msg);
1846 }
1847 else
1848 MyTranslateMessage(&msg);
1849 }
1850 }
1851#ifdef FEAT_MBYTE_IME
1852 else if (msg.message == WM_IME_NOTIFY)
1853 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
1854 else if (msg.message == WM_KEYUP && im_get_status())
1855 /* added for non-MS IME (Yasuhiro Matsumoto) */
1856 MyTranslateMessage(&msg);
1857#endif
1858#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
1859/* GIME_TEST */
1860 else if (msg.message == WM_IME_STARTCOMPOSITION)
1861 {
1862 POINT point;
1863
1864 global_ime_set_font(&norm_logfont);
1865 point.x = FILL_X(gui.col);
1866 point.y = FILL_Y(gui.row);
1867 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
1868 global_ime_set_position(&point);
1869 }
1870#endif
1871
1872#ifdef FEAT_MENU
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001873 /* Check for <F10>: Default effect is to select the menu. When <F10> is
1874 * mapped we need to stop it here to avoid strange effects (e.g., for the
1875 * key-up event) */
Bram Moolenaar8d6ea5e2006-03-18 21:31:46 +00001876 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877#endif
1878 DispatchMessage(&msg);
1879}
1880
1881/*
1882 * Catch up with any queued events. This may put keyboard input into the
1883 * input buffer, call resize call-backs, trigger timers etc. If there is
1884 * nothing in the event queue (& no timers pending), then we return
1885 * immediately.
1886 */
1887 void
1888gui_mch_update(void)
1889{
1890 MSG msg;
1891
1892 if (!s_busy_processing)
1893 while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
1894 && !vim_is_input_buf_full())
1895 process_message();
1896}
1897
1898/*
1899 * GUI input routine called by gui_wait_for_chars(). Waits for a character
1900 * from the keyboard.
1901 * wtime == -1 Wait forever.
1902 * wtime == 0 This should never happen.
1903 * wtime > 0 Wait wtime milliseconds for a character.
1904 * Returns OK if a character was found to be available within the given time,
1905 * or FAIL otherwise.
1906 */
1907 int
1908gui_mch_wait_for_chars(int wtime)
1909{
1910 MSG msg;
1911 int focus;
1912
1913 s_timed_out = FALSE;
1914
1915 if (wtime > 0)
1916 {
1917 /* Don't do anything while processing a (scroll) message. */
1918 if (s_busy_processing)
1919 return FAIL;
1920 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)wtime,
1921 (TIMERPROC)_OnTimer);
1922 }
1923
1924 allow_scrollbar = TRUE;
1925
1926 focus = gui.in_focus;
1927 while (!s_timed_out)
1928 {
1929 /* Stop or start blinking when focus changes */
1930 if (gui.in_focus != focus)
1931 {
1932 if (gui.in_focus)
1933 gui_mch_start_blink();
1934 else
1935 gui_mch_stop_blink();
1936 focus = gui.in_focus;
1937 }
1938
1939 if (s_need_activate)
1940 {
1941#ifdef WIN32
1942 (void)SetForegroundWindow(s_hwnd);
1943#else
1944 (void)SetActiveWindow(s_hwnd);
1945#endif
1946 s_need_activate = FALSE;
1947 }
1948
Bram Moolenaar61665aa2008-12-24 11:20:53 +00001949#ifdef FEAT_NETBEANS_INTG
1950 /* Process the queued netbeans messages. */
1951 netbeans_parse_messages();
1952#endif
1953
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 /*
1955 * Don't use gui_mch_update() because then we will spin-lock until a
1956 * char arrives, instead we use GetMessage() to hang until an
1957 * event arrives. No need to check for input_buf_full because we are
1958 * returning as soon as it contains a single char -- webb
1959 */
1960 process_message();
1961
1962 if (input_available())
1963 {
1964 if (s_wait_timer != 0 && !s_timed_out)
1965 {
1966 KillTimer(NULL, s_wait_timer);
1967
1968 /* Eat spurious WM_TIMER messages */
1969 while (PeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
1970 ;
1971 s_wait_timer = 0;
1972 }
1973 allow_scrollbar = FALSE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001974
1975 /* Clear pending mouse button, the release event may have been
Bram Moolenaarea408882007-12-03 21:21:03 +00001976 * taken by the dialog window. But don't do this when getting
1977 * focus, we need the mouse-up event then. */
1978 if (!s_getting_focus)
1979 s_button_pending = -1;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001980
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 return OK;
1982 }
1983 }
1984 allow_scrollbar = FALSE;
1985 return FAIL;
1986}
1987
1988/*
1989 * Clear a rectangular region of the screen from text pos (row1, col1) to
1990 * (row2, col2) inclusive.
1991 */
1992 void
1993gui_mch_clear_block(
1994 int row1,
1995 int col1,
1996 int row2,
1997 int col2)
1998{
1999 RECT rc;
2000
2001 /*
2002 * Clear one extra pixel at the far right, for when bold characters have
2003 * spilled over to the window border.
2004 * Note: FillRect() excludes right and bottom of rectangle.
2005 */
2006 rc.left = FILL_X(col1);
2007 rc.top = FILL_Y(row1);
2008 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2009 rc.bottom = FILL_Y(row2 + 1);
2010 clear_rect(&rc);
2011}
2012
2013/*
2014 * Clear the whole text window.
2015 */
2016 void
2017gui_mch_clear_all(void)
2018{
2019 RECT rc;
2020
2021 rc.left = 0;
2022 rc.top = 0;
2023 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2024 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2025 clear_rect(&rc);
2026}
2027/*
2028 * Menu stuff.
2029 */
2030
2031 void
2032gui_mch_enable_menu(int flag)
2033{
2034#ifdef FEAT_MENU
2035 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2036#endif
2037}
2038
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002039/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 void
2041gui_mch_set_menu_pos(
2042 int x,
2043 int y,
2044 int w,
2045 int h)
2046{
2047 /* It will be in the right place anyway */
2048}
2049
2050#if defined(FEAT_MENU) || defined(PROTO)
2051/*
2052 * Make menu item hidden or not hidden
2053 */
2054 void
2055gui_mch_menu_hidden(
2056 vimmenu_T *menu,
2057 int hidden)
2058{
2059 /*
2060 * This doesn't do what we want. Hmm, just grey the menu items for now.
2061 */
2062 /*
2063 if (hidden)
2064 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2065 else
2066 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2067 */
2068 gui_mch_menu_grey(menu, hidden);
2069}
2070
2071/*
2072 * This is called after setting all the menus to grey/hidden or not.
2073 */
2074 void
2075gui_mch_draw_menubar(void)
2076{
2077 DrawMenuBar(s_hwnd);
2078}
2079#endif /*FEAT_MENU*/
2080
2081#ifndef PROTO
2082void
2083#ifdef VIMDLL
2084_export
2085#endif
2086_cdecl
2087SaveInst(HINSTANCE hInst)
2088{
2089 s_hinst = hInst;
2090}
2091#endif
2092
2093/*
2094 * Return the RGB value of a pixel as a long.
2095 */
2096 long_u
2097gui_mch_get_rgb(guicolor_T pixel)
2098{
2099 return (GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2100 + GetBValue(pixel);
2101}
2102
2103#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2104/* Convert pixels in X to dialog units */
2105 static WORD
2106PixelToDialogX(int numPixels)
2107{
2108 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2109}
2110
2111/* Convert pixels in Y to dialog units */
2112 static WORD
2113PixelToDialogY(int numPixels)
2114{
2115 return (WORD)((numPixels * 8) / s_dlgfntheight);
2116}
2117
2118/* Return the width in pixels of the given text in the given DC. */
2119 static int
2120GetTextWidth(HDC hdc, char_u *str, int len)
2121{
2122 SIZE size;
2123
2124 GetTextExtentPoint(hdc, str, len, &size);
2125 return size.cx;
2126}
2127
2128#ifdef FEAT_MBYTE
2129/*
2130 * Return the width in pixels of the given text in the given DC, taking care
2131 * of 'encoding' to active codepage conversion.
2132 */
2133 static int
2134GetTextWidthEnc(HDC hdc, char_u *str, int len)
2135{
2136 SIZE size;
2137 WCHAR *wstr;
2138 int n;
2139 int wlen = len;
2140
2141 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2142 {
2143 /* 'encoding' differs from active codepage: convert text and use wide
2144 * function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002145 wstr = enc_to_utf16(str, &wlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 if (wstr != NULL)
2147 {
2148 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2149 vim_free(wstr);
2150 if (n)
2151 return size.cx;
2152 }
2153 }
2154
2155 return GetTextWidth(hdc, str, len);
2156}
2157#else
2158# define GetTextWidthEnc(h, s, l) GetTextWidth((h), (s), (l))
2159#endif
2160
2161/*
2162 * A quick little routine that will center one window over another, handy for
2163 * dialog boxes. Taken from the Win32SDK samples.
2164 */
2165 static BOOL
2166CenterWindow(
2167 HWND hwndChild,
2168 HWND hwndParent)
2169{
2170 RECT rChild, rParent;
2171 int wChild, hChild, wParent, hParent;
2172 int wScreen, hScreen, xNew, yNew;
2173 HDC hdc;
2174
2175 GetWindowRect(hwndChild, &rChild);
2176 wChild = rChild.right - rChild.left;
2177 hChild = rChild.bottom - rChild.top;
2178
2179 /* If Vim is minimized put the window in the middle of the screen. */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002180 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 {
2182#ifdef WIN16
2183 rParent.left = 0;
2184 rParent.top = 0;
2185 rParent.right = GetSystemMetrics(SM_CXSCREEN);
2186 rParent.bottom = GetSystemMetrics(SM_CYFULLSCREEN);
2187#else
2188 SystemParametersInfo(SPI_GETWORKAREA, 0, &rParent, 0);
2189#endif
2190 }
2191 else
2192 GetWindowRect(hwndParent, &rParent);
2193 wParent = rParent.right - rParent.left;
2194 hParent = rParent.bottom - rParent.top;
2195
2196 hdc = GetDC(hwndChild);
2197 wScreen = GetDeviceCaps (hdc, HORZRES);
2198 hScreen = GetDeviceCaps (hdc, VERTRES);
2199 ReleaseDC(hwndChild, hdc);
2200
2201 xNew = rParent.left + ((wParent - wChild) /2);
2202 if (xNew < 0)
2203 {
2204 xNew = 0;
2205 }
2206 else if ((xNew+wChild) > wScreen)
2207 {
2208 xNew = wScreen - wChild;
2209 }
2210
2211 yNew = rParent.top + ((hParent - hChild) /2);
2212 if (yNew < 0)
2213 yNew = 0;
2214 else if ((yNew+hChild) > hScreen)
2215 yNew = hScreen - hChild;
2216
2217 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2218 SWP_NOSIZE | SWP_NOZORDER);
2219}
2220#endif /* FEAT_GUI_DIALOG */
2221
2222void
2223gui_mch_activate_window(void)
2224{
2225 (void)SetActiveWindow(s_hwnd);
2226}
2227
2228#if defined(FEAT_TOOLBAR) || defined(PROTO)
2229 void
2230gui_mch_show_toolbar(int showit)
2231{
2232 if (s_toolbarhwnd == NULL)
2233 return;
2234
2235 if (showit)
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00002236 {
2237# ifdef FEAT_MBYTE
2238# ifndef TB_SETUNICODEFORMAT
2239 /* For older compilers. We assume this never changes. */
2240# define TB_SETUNICODEFORMAT 0x2005
2241# endif
2242 /* Enable/disable unicode support */
2243 int uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2244 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2245# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 ShowWindow(s_toolbarhwnd, SW_SHOW);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00002247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 else
2249 ShowWindow(s_toolbarhwnd, SW_HIDE);
2250}
2251
2252/* Then number of bitmaps is fixed. Exit is missing! */
2253#define TOOLBAR_BITMAP_COUNT 31
2254
2255#endif
2256
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002257#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00002258 static void
Bram Moolenaar908d53a2006-11-07 18:05:31 +00002259add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2260{
2261#ifdef FEAT_MBYTE
2262 WCHAR *wn = NULL;
2263 int n;
2264
2265 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2266 {
2267 /* 'encoding' differs from active codepage: convert menu name
2268 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002269 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaar908d53a2006-11-07 18:05:31 +00002270 if (wn != NULL)
2271 {
2272 MENUITEMINFOW infow;
2273
2274 infow.cbSize = sizeof(infow);
2275 infow.fMask = MIIM_TYPE | MIIM_ID;
2276 infow.wID = item_id;
2277 infow.fType = MFT_STRING;
2278 infow.dwTypeData = wn;
2279 infow.cch = (UINT)wcslen(wn);
2280 n = InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2281 vim_free(wn);
2282 if (n == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2283 /* Failed, try using non-wide function. */
2284 wn = NULL;
2285 }
2286 }
2287
2288 if (wn == NULL)
2289#endif
2290 {
2291 MENUITEMINFO info;
2292
2293 info.cbSize = sizeof(info);
2294 info.fMask = MIIM_TYPE | MIIM_ID;
2295 info.wID = item_id;
2296 info.fType = MFT_STRING;
2297 info.dwTypeData = item_text;
2298 info.cch = (UINT)STRLEN(item_text);
2299 InsertMenuItem(pmenu, item_id, FALSE, &info);
2300 }
2301}
2302
2303 static void
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00002304show_tabline_popup_menu(void)
2305{
2306 HMENU tab_pmenu;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00002307 long rval;
2308 POINT pt;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00002309
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002310 /* When ignoring events don't show the menu. */
2311 if (hold_gui_events
2312# ifdef FEAT_CMDWIN
2313 || cmdwin_type != 0
2314# endif
2315 )
2316 return;
2317
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00002318 tab_pmenu = CreatePopupMenu();
2319 if (tab_pmenu == NULL)
2320 return;
2321
Bram Moolenaar908d53a2006-11-07 18:05:31 +00002322 add_tabline_popup_menu_entry(tab_pmenu, TABLINE_MENU_CLOSE, _("Close tab"));
2323 add_tabline_popup_menu_entry(tab_pmenu, TABLINE_MENU_NEW, _("New tab"));
2324 add_tabline_popup_menu_entry(tab_pmenu, TABLINE_MENU_OPEN,
2325 _("Open tab..."));
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00002326
2327 GetCursorPos(&pt);
2328 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2329 NULL);
2330
2331 DestroyMenu(tab_pmenu);
2332
2333 /* Add the string cmd into input buffer */
2334 if (rval > 0)
2335 {
2336 TCHITTESTINFO htinfo;
2337 int idx;
2338
2339 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2340 return;
2341
2342 htinfo.pt.x = pt.x;
2343 htinfo.pt.y = pt.y;
2344 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2345 if (idx == -1)
2346 idx = 0;
2347 else
2348 idx += 1;
2349
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00002350 send_tabline_menu_event(idx, (int)rval);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00002351 }
2352}
2353
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002354/*
2355 * Show or hide the tabline.
2356 */
2357 void
2358gui_mch_show_tabline(int showit)
2359{
2360 if (s_tabhwnd == NULL)
2361 return;
2362
2363 if (!showit != !showing_tabline)
2364 {
2365 if (showit)
2366 ShowWindow(s_tabhwnd, SW_SHOW);
2367 else
2368 ShowWindow(s_tabhwnd, SW_HIDE);
2369 showing_tabline = showit;
2370 }
2371}
2372
2373/*
2374 * Return TRUE when tabline is displayed.
2375 */
2376 int
2377gui_mch_showing_tabline(void)
2378{
2379 return s_tabhwnd != NULL && showing_tabline;
2380}
2381
2382/*
2383 * Update the labels of the tabline.
2384 */
2385 void
2386gui_mch_update_tabline(void)
2387{
2388 tabpage_T *tp;
2389 TCITEM tie;
2390 int nr = 0;
2391 int curtabidx = 0;
2392 RECT rc;
Bram Moolenaar8424a622006-04-19 21:23:36 +00002393#ifdef FEAT_MBYTE
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00002394 static int use_unicode = FALSE;
2395 int uu;
Bram Moolenaar8424a622006-04-19 21:23:36 +00002396 WCHAR *wstr = NULL;
2397#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002398
2399 if (s_tabhwnd == NULL)
2400 return;
2401
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00002402#if defined(FEAT_MBYTE)
2403# ifndef CCM_SETUNICODEFORMAT
2404 /* For older compilers. We assume this never changes. */
2405# define CCM_SETUNICODEFORMAT 0x2005
2406# endif
2407 uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2408 if (uu != use_unicode)
2409 {
2410 /* Enable/disable unicode support */
2411 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2412 use_unicode = uu;
2413 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00002414#endif
2415
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002416 tie.mask = TCIF_TEXT;
2417 tie.iImage = -1;
2418
2419 /* Add a label for each tab page. They all contain the same text area. */
2420 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2421 {
2422 if (tp == curtab)
2423 curtabidx = nr;
2424
2425 if (!TabCtrl_GetItemRect(s_tabhwnd, nr, &rc))
2426 {
2427 /* Add the tab */
2428 tie.pszText = "-Empty-";
2429 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2430 }
2431
Bram Moolenaar57657d82006-04-21 22:12:41 +00002432 get_tabline_label(tp, FALSE);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002433 tie.pszText = NameBuff;
Bram Moolenaar8424a622006-04-19 21:23:36 +00002434#ifdef FEAT_MBYTE
2435 wstr = NULL;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00002436 if (use_unicode)
Bram Moolenaar8424a622006-04-19 21:23:36 +00002437 {
2438 /* Need to go through Unicode. */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002439 wstr = enc_to_utf16(NameBuff, NULL);
Bram Moolenaar8424a622006-04-19 21:23:36 +00002440 if (wstr != NULL)
2441 {
2442 TCITEMW tiw;
2443
2444 tiw.mask = TCIF_TEXT;
2445 tiw.iImage = -1;
2446 tiw.pszText = wstr;
Bram Moolenaar85f868c2006-11-28 16:16:58 +00002447 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
Bram Moolenaar8424a622006-04-19 21:23:36 +00002448 vim_free(wstr);
2449 }
2450 }
2451 if (wstr == NULL)
2452#endif
2453 {
2454 TabCtrl_SetItem(s_tabhwnd, nr, &tie);
2455 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002456 }
2457
2458 /* Remove any old labels. */
2459 while (TabCtrl_GetItemRect(s_tabhwnd, nr, &rc))
2460 TabCtrl_DeleteItem(s_tabhwnd, nr);
2461
2462 if (TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2463 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2464}
2465
2466/*
2467 * Set the current tab to "nr". First tab is 1.
2468 */
2469 void
2470gui_mch_set_curtab(nr)
2471 int nr;
2472{
2473 if (s_tabhwnd == NULL)
2474 return;
2475
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00002476 if (TabCtrl_GetCurSel(s_tabhwnd) != nr -1)
2477 TabCtrl_SetCurSel(s_tabhwnd, nr -1);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002478}
2479
2480#endif
2481
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482/*
2483 * ":simalt" command.
2484 */
2485 void
2486ex_simalt(exarg_T *eap)
2487{
2488 char_u *keys = eap->arg;
2489
2490 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2491 while (*keys)
2492 {
2493 if (*keys == '~')
2494 *keys = ' '; /* for showing system menu */
2495 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2496 keys++;
2497 }
2498}
2499
2500/*
2501 * Create the find & replace dialogs.
2502 * You can't have both at once: ":find" when replace is showing, destroys
2503 * the replace dialog first, and the other way around.
2504 */
2505#ifdef MSWIN_FIND_REPLACE
2506 static void
2507initialise_findrep(char_u *initial_string)
2508{
2509 int wword = FALSE;
2510 int mcase = !p_ic;
2511 char_u *entry_text;
2512
2513 /* Get the search string to use. */
2514 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2515
2516 s_findrep_struct.hwndOwner = s_hwnd;
2517 s_findrep_struct.Flags = FR_DOWN;
2518 if (mcase)
2519 s_findrep_struct.Flags |= FR_MATCHCASE;
2520 if (wword)
2521 s_findrep_struct.Flags |= FR_WHOLEWORD;
2522 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +00002523 vim_strncpy(s_findrep_struct.lpstrFindWhat, entry_text,
2524 s_findrep_struct.wFindWhatLen - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 vim_free(entry_text);
2526}
2527#endif
2528
Bram Moolenaar908d53a2006-11-07 18:05:31 +00002529 static void
2530set_window_title(HWND hwnd, char *title)
2531{
2532#ifdef FEAT_MBYTE
2533 if (title != NULL && enc_codepage >= 0 && enc_codepage != (int)GetACP())
2534 {
2535 WCHAR *wbuf;
2536 int n;
2537
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002538 /* Convert the title from 'encoding' to UTF-16. */
2539 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
Bram Moolenaar908d53a2006-11-07 18:05:31 +00002540 if (wbuf != NULL)
2541 {
2542 n = SetWindowTextW(hwnd, wbuf);
2543 vim_free(wbuf);
2544 if (n != 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
2545 return;
2546 /* Retry with non-wide function (for Windows 98). */
2547 }
2548 }
2549#endif
2550 (void)SetWindowText(hwnd, (LPCSTR)title);
2551}
2552
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 void
2554gui_mch_find_dialog(exarg_T *eap)
2555{
2556#ifdef MSWIN_FIND_REPLACE
2557 if (s_findrep_msg != 0)
2558 {
2559 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2560 DestroyWindow(s_findrep_hwnd);
2561
2562 if (!IsWindow(s_findrep_hwnd))
2563 {
2564 initialise_findrep(eap->arg);
2565 s_findrep_hwnd = FindText((LPFINDREPLACE) &s_findrep_struct);
2566 }
2567
Bram Moolenaar908d53a2006-11-07 18:05:31 +00002568 set_window_title(s_findrep_hwnd,
2569 _("Find string (use '\\\\' to find a '\\')"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 (void)SetFocus(s_findrep_hwnd);
2571
2572 s_findrep_is_find = TRUE;
2573 }
2574#endif
2575}
2576
2577
2578 void
2579gui_mch_replace_dialog(exarg_T *eap)
2580{
2581#ifdef MSWIN_FIND_REPLACE
2582 if (s_findrep_msg != 0)
2583 {
2584 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2585 DestroyWindow(s_findrep_hwnd);
2586
2587 if (!IsWindow(s_findrep_hwnd))
2588 {
2589 initialise_findrep(eap->arg);
2590 s_findrep_hwnd = ReplaceText((LPFINDREPLACE) &s_findrep_struct);
2591 }
2592
Bram Moolenaar908d53a2006-11-07 18:05:31 +00002593 set_window_title(s_findrep_hwnd,
2594 _("Find & Replace (use '\\\\' to find a '\\')"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 (void)SetFocus(s_findrep_hwnd);
2596
2597 s_findrep_is_find = FALSE;
2598 }
2599#endif
2600}
2601
2602
2603/*
2604 * Set visibility of the pointer.
2605 */
2606 void
2607gui_mch_mousehide(int hide)
2608{
2609 if (hide != gui.pointer_hidden)
2610 {
2611 ShowCursor(!hide);
2612 gui.pointer_hidden = hide;
2613 }
2614}
2615
2616#ifdef FEAT_MENU
2617 static void
2618gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2619{
2620 /* Unhide the mouse, we don't get move events here. */
2621 gui_mch_mousehide(FALSE);
2622
2623 (void)TrackPopupMenu(
2624 (HMENU)menu->submenu_id,
2625 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2626 x, y,
2627 (int)0, /*reserved param*/
2628 s_hwnd,
2629 NULL);
2630 /*
2631 * NOTE: The pop-up menu can eat the mouse up event.
2632 * We deal with this in normal.c.
2633 */
2634}
2635#endif
2636
2637/*
2638 * Got a message when the system will go down.
2639 */
2640 static void
2641_OnEndSession(void)
2642{
2643 getout_preserve_modified(1);
2644}
2645
2646/*
2647 * Get this message when the user clicks on the cross in the top right corner
2648 * of a Windows95 window.
2649 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002650/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 static void
2652_OnClose(
2653 HWND hwnd)
2654{
2655 gui_shell_closed();
2656}
2657
2658/*
2659 * Get a message when the window is being destroyed.
2660 */
2661 static void
2662_OnDestroy(
2663 HWND hwnd)
2664{
2665#ifdef WIN16_3DLOOK
2666 Ctl3dUnregister(s_hinst);
2667#endif
2668 if (!destroying)
2669 _OnClose(hwnd);
2670}
2671
2672 static void
2673_OnPaint(
2674 HWND hwnd)
2675{
2676 if (!IsMinimized(hwnd))
2677 {
2678 PAINTSTRUCT ps;
2679
2680 out_flush(); /* make sure all output has been processed */
2681 (void)BeginPaint(hwnd, &ps);
2682
2683#ifdef FEAT_MBYTE
2684 /* prevent multi-byte characters from misprinting on an invalid
2685 * rectangle */
2686 if (has_mbyte)
2687 {
2688 RECT rect;
2689
2690 GetClientRect(hwnd, &rect);
2691 ps.rcPaint.left = rect.left;
2692 ps.rcPaint.right = rect.right;
2693 }
2694#endif
2695
2696 if (!IsRectEmpty(&ps.rcPaint))
2697 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2698 ps.rcPaint.right - ps.rcPaint.left + 1,
2699 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2700 EndPaint(hwnd, &ps);
2701 }
2702}
2703
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002704/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 static void
2706_OnSize(
2707 HWND hwnd,
2708 UINT state,
2709 int cx,
2710 int cy)
2711{
2712 if (!IsMinimized(hwnd))
2713 {
2714 gui_resize_shell(cx, cy);
2715
2716#ifdef FEAT_MENU
2717 /* Menu bar may wrap differently now */
2718 gui_mswin_get_menu_height(TRUE);
2719#endif
2720 }
2721}
2722
2723 static void
2724_OnSetFocus(
2725 HWND hwnd,
2726 HWND hwndOldFocus)
2727{
2728 gui_focus_change(TRUE);
Bram Moolenaarea408882007-12-03 21:21:03 +00002729 s_getting_focus = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2731}
2732
2733 static void
2734_OnKillFocus(
2735 HWND hwnd,
2736 HWND hwndNewFocus)
2737{
2738 gui_focus_change(FALSE);
Bram Moolenaarea408882007-12-03 21:21:03 +00002739 s_getting_focus = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2741}
2742
2743/*
2744 * Get a message when the user switches back to vim
2745 */
2746#ifdef WIN16
2747 static BOOL
2748#else
2749 static LRESULT
2750#endif
2751_OnActivateApp(
2752 HWND hwnd,
2753 BOOL fActivate,
2754#ifdef WIN16
2755 HTASK dwThreadId
2756#else
2757 DWORD dwThreadId
2758#endif
2759 )
2760{
2761 /* we call gui_focus_change() in _OnSetFocus() */
2762 /* gui_focus_change((int)fActivate); */
2763 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2764}
2765
2766#if defined(FEAT_WINDOWS) || defined(PROTO)
2767 void
2768gui_mch_destroy_scrollbar(scrollbar_T *sb)
2769{
2770 DestroyWindow(sb->id);
2771}
2772#endif
2773
2774/*
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002775 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 */
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002777 void
Bram Moolenaara40c5002005-01-09 21:16:21 +00002778gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779{
2780 RECT rct;
2781 POINT mp;
2782
2783 (void)GetWindowRect(s_textArea, &rct);
2784 (void)GetCursorPos((LPPOINT)&mp);
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002785 *x = (int)(mp.x - rct.left);
2786 *y = (int)(mp.y - rct.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787}
2788
2789/*
2790 * Move mouse pointer to character at (x, y).
2791 */
2792 void
2793gui_mch_setmouse(int x, int y)
2794{
2795 RECT rct;
2796
2797 (void)GetWindowRect(s_textArea, &rct);
2798 (void)SetCursorPos(x + gui.border_offset + rct.left,
2799 y + gui.border_offset + rct.top);
2800}
2801
2802 static void
2803gui_mswin_get_valid_dimensions(
2804 int w,
2805 int h,
2806 int *valid_w,
2807 int *valid_h)
2808{
2809 int base_width, base_height;
2810
2811 base_width = gui_get_base_width()
2812 + GetSystemMetrics(SM_CXFRAME) * 2;
2813 base_height = gui_get_base_height()
2814 + GetSystemMetrics(SM_CYFRAME) * 2
2815 + GetSystemMetrics(SM_CYCAPTION)
2816#ifdef FEAT_MENU
2817 + gui_mswin_get_menu_height(FALSE)
2818#endif
2819 ;
2820 *valid_w = base_width +
2821 ((w - base_width) / gui.char_width) * gui.char_width;
2822 *valid_h = base_height +
2823 ((h - base_height) / gui.char_height) * gui.char_height;
2824}
2825
2826 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827gui_mch_flash(int msec)
2828{
2829 RECT rc;
2830
2831 /*
2832 * Note: InvertRect() excludes right and bottom of rectangle.
2833 */
2834 rc.left = 0;
2835 rc.top = 0;
2836 rc.right = gui.num_cols * gui.char_width;
2837 rc.bottom = gui.num_rows * gui.char_height;
2838 InvertRect(s_hdc, &rc);
2839 gui_mch_flush(); /* make sure it's displayed */
2840
2841 ui_delay((long)msec, TRUE); /* wait for a few msec */
2842
2843 InvertRect(s_hdc, &rc);
2844}
2845
2846/*
2847 * Return flags used for scrolling.
2848 * The SW_INVALIDATE is required when part of the window is covered or
2849 * off-screen. Refer to MS KB Q75236.
2850 */
2851 static int
2852get_scroll_flags(void)
2853{
2854 HWND hwnd;
2855 RECT rcVim, rcOther, rcDest;
2856
2857 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaar0d406992005-05-22 22:03:39 +00002858
2859 /* Check if the window is partly above or below the screen. We don't care
2860 * about partly left or right of the screen, it is not relevant when
2861 * scrolling up or down. */
2862 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
2863 return SW_INVALIDATE;
2864
2865 /* Check if there is an window (partly) on top of us. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
2867 if (IsWindowVisible(hwnd))
2868 {
2869 GetWindowRect(hwnd, &rcOther);
2870 if (IntersectRect(&rcDest, &rcVim, &rcOther))
2871 return SW_INVALIDATE;
2872 }
2873 return 0;
2874}
2875
2876/*
2877 * Delete the given number of lines from the given row, scrolling up any
2878 * text further down within the scroll region.
2879 */
2880 void
2881gui_mch_delete_lines(
2882 int row,
2883 int num_lines)
2884{
2885 RECT rc;
2886
2887 rc.left = FILL_X(gui.scroll_region_left);
2888 rc.right = FILL_X(gui.scroll_region_right + 1);
2889 rc.top = FILL_Y(row);
2890 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
2891
2892 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
2893 &rc, &rc, NULL, NULL, get_scroll_flags());
2894
2895 UpdateWindow(s_textArea);
2896 /* This seems to be required to avoid the cursor disappearing when
2897 * scrolling such that the cursor ends up in the top-left character on
2898 * the screen... But why? (Webb) */
2899 /* It's probably fixed by disabling drawing the cursor while scrolling. */
2900 /* gui.cursor_is_valid = FALSE; */
2901
2902 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
2903 gui.scroll_region_left,
2904 gui.scroll_region_bot, gui.scroll_region_right);
2905}
2906
2907/*
2908 * Insert the given number of lines before the given row, scrolling down any
2909 * following text within the scroll region.
2910 */
2911 void
2912gui_mch_insert_lines(
2913 int row,
2914 int num_lines)
2915{
2916 RECT rc;
2917
2918 rc.left = FILL_X(gui.scroll_region_left);
2919 rc.right = FILL_X(gui.scroll_region_right + 1);
2920 rc.top = FILL_Y(row);
2921 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
2922 /* The SW_INVALIDATE is required when part of the window is covered or
2923 * off-screen. How do we avoid it when it's not needed? */
2924 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
2925 &rc, &rc, NULL, NULL, get_scroll_flags());
2926
2927 UpdateWindow(s_textArea);
2928
2929 gui_clear_block(row, gui.scroll_region_left,
2930 row + num_lines - 1, gui.scroll_region_right);
2931}
2932
2933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002934/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 void
2936gui_mch_exit(int rc)
2937{
2938 ReleaseDC(s_textArea, s_hdc);
2939 DeleteObject(s_brush);
2940
2941#ifdef FEAT_TEAROFF
2942 /* Unload the tearoff bitmap */
2943 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
2944#endif
2945
2946 /* Destroy our window (if we have one). */
2947 if (s_hwnd != NULL)
2948 {
2949 destroying = TRUE; /* ignore WM_DESTROY message now */
2950 DestroyWindow(s_hwnd);
2951 }
2952
2953#ifdef GLOBAL_IME
2954 global_ime_end();
2955#endif
2956}
2957
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002958 static char_u *
2959logfont2name(LOGFONT lf)
2960{
2961 char *p;
2962 char *res;
2963 char *charset_name;
2964
2965 charset_name = charset_id2name((int)lf.lfCharSet);
2966 res = alloc((unsigned)(strlen(lf.lfFaceName) + 20
2967 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
2968 if (res != NULL)
2969 {
2970 p = res;
2971 /* make a normal font string out of the lf thing:*/
2972 sprintf((char *)p, "%s:h%d", lf.lfFaceName, pixels_to_points(
2973 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
2974 while (*p)
2975 {
2976 if (*p == ' ')
2977 *p = '_';
2978 ++p;
2979 }
2980#ifndef MSWIN16_FASTTEXT
2981 if (lf.lfItalic)
2982 STRCAT(p, ":i");
2983 if (lf.lfWeight >= FW_BOLD)
2984 STRCAT(p, ":b");
2985#endif
2986 if (lf.lfUnderline)
2987 STRCAT(p, ":u");
2988 if (lf.lfStrikeOut)
2989 STRCAT(p, ":s");
2990 if (charset_name != NULL)
2991 {
2992 STRCAT(p, ":c");
2993 STRCAT(p, charset_name);
2994 }
2995 }
2996
2997 return res;
2998}
2999
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003001 * Initialise vim to use the font with the given name.
3002 * Return FAIL if the font could not be loaded, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003004/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 int
3006gui_mch_init_font(char_u *font_name, int fontset)
3007{
3008 LOGFONT lf;
3009 GuiFont font = NOFONT;
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003010 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
3012 /* Load the font */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003013 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 font = get_font_handle(&lf);
3015 if (font == NOFONT)
3016 return FAIL;
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003017
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 if (font_name == NULL)
3019 font_name = lf.lfFaceName;
3020#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3021 norm_logfont = lf;
3022#endif
3023#ifdef FEAT_MBYTE_IME
3024 im_set_font(&lf);
3025#endif
3026 gui_mch_free_font(gui.norm_font);
3027 gui.norm_font = font;
3028 current_font_height = lf.lfHeight;
3029 GetFontSize(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003031 p = logfont2name(lf);
3032 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003034 hl_set_font_name(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003036 /* When setting 'guifont' to "*" replace it with the actual font name.
3037 * */
3038 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 vim_free(p_guifont);
3041 p_guifont = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003043 else
3044 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 }
3046
3047#ifndef MSWIN16_FASTTEXT
3048 gui_mch_free_font(gui.ital_font);
3049 gui.ital_font = NOFONT;
3050 gui_mch_free_font(gui.bold_font);
3051 gui.bold_font = NOFONT;
3052 gui_mch_free_font(gui.boldital_font);
3053 gui.boldital_font = NOFONT;
3054
3055 if (!lf.lfItalic)
3056 {
3057 lf.lfItalic = TRUE;
3058 gui.ital_font = get_font_handle(&lf);
3059 lf.lfItalic = FALSE;
3060 }
3061 if (lf.lfWeight < FW_BOLD)
3062 {
3063 lf.lfWeight = FW_BOLD;
3064 gui.bold_font = get_font_handle(&lf);
3065 if (!lf.lfItalic)
3066 {
3067 lf.lfItalic = TRUE;
3068 gui.boldital_font = get_font_handle(&lf);
3069 }
3070 }
3071#endif
3072
3073 return OK;
3074}
3075
Bram Moolenaar85f868c2006-11-28 16:16:58 +00003076#ifndef WPF_RESTORETOMAXIMIZED
3077# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3078#endif
3079
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080/*
3081 * Return TRUE if the GUI window is maximized, filling the whole screen.
3082 */
3083 int
3084gui_mch_maximized()
3085{
Bram Moolenaar85f868c2006-11-28 16:16:58 +00003086 WINDOWPLACEMENT wp;
3087
3088 wp.length = sizeof(WINDOWPLACEMENT);
3089 if (GetWindowPlacement(s_hwnd, &wp))
3090 return wp.showCmd == SW_SHOWMAXIMIZED
3091 || (wp.showCmd == SW_SHOWMINIMIZED
3092 && wp.flags == WPF_RESTORETOMAXIMIZED);
3093
3094 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095}
3096
3097/*
3098 * Called when the font changed while the window is maximized. Compute the
3099 * new Rows and Columns. This is like resizing the window.
3100 */
3101 void
3102gui_mch_newfont()
3103{
3104 RECT rect;
3105
3106 GetWindowRect(s_hwnd, &rect);
3107 gui_resize_shell(rect.right - rect.left
3108 - GetSystemMetrics(SM_CXFRAME) * 2,
3109 rect.bottom - rect.top
3110 - GetSystemMetrics(SM_CYFRAME) * 2
3111 - GetSystemMetrics(SM_CYCAPTION)
3112#ifdef FEAT_MENU
3113 - gui_mswin_get_menu_height(FALSE)
3114#endif
3115 );
3116}
3117
3118/*
3119 * Set the window title
3120 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003121/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 void
3123gui_mch_settitle(
3124 char_u *title,
3125 char_u *icon)
3126{
Bram Moolenaar908d53a2006-11-07 18:05:31 +00003127 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128}
3129
3130#ifdef FEAT_MOUSESHAPE
3131/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3132 * misc2.c! */
3133static LPCSTR mshape_idcs[] =
3134{
3135 MAKEINTRESOURCE(IDC_ARROW), /* arrow */
3136 MAKEINTRESOURCE(0), /* blank */
3137 MAKEINTRESOURCE(IDC_IBEAM), /* beam */
3138 MAKEINTRESOURCE(IDC_SIZENS), /* updown */
3139 MAKEINTRESOURCE(IDC_SIZENS), /* udsizing */
3140 MAKEINTRESOURCE(IDC_SIZEWE), /* leftright */
3141 MAKEINTRESOURCE(IDC_SIZEWE), /* lrsizing */
3142 MAKEINTRESOURCE(IDC_WAIT), /* busy */
3143#ifdef WIN3264
3144 MAKEINTRESOURCE(IDC_NO), /* no */
3145#else
3146 MAKEINTRESOURCE(IDC_ICON), /* no */
3147#endif
3148 MAKEINTRESOURCE(IDC_ARROW), /* crosshair */
3149 MAKEINTRESOURCE(IDC_ARROW), /* hand1 */
3150 MAKEINTRESOURCE(IDC_ARROW), /* hand2 */
3151 MAKEINTRESOURCE(IDC_ARROW), /* pencil */
3152 MAKEINTRESOURCE(IDC_ARROW), /* question */
3153 MAKEINTRESOURCE(IDC_ARROW), /* right-arrow */
3154 MAKEINTRESOURCE(IDC_UPARROW), /* up-arrow */
3155 MAKEINTRESOURCE(IDC_ARROW) /* last one */
3156};
3157
3158 void
3159mch_set_mouse_shape(int shape)
3160{
3161 LPCSTR idc;
3162
3163 if (shape == MSHAPE_HIDE)
3164 ShowCursor(FALSE);
3165 else
3166 {
3167 if (shape >= MSHAPE_NUMBERED)
3168 idc = MAKEINTRESOURCE(IDC_ARROW);
3169 else
3170 idc = mshape_idcs[shape];
Bram Moolenaare6a91fd2008-07-24 18:51:11 +00003171#ifdef SetClassLongPtr
3172 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173#else
3174# ifdef WIN32
Bram Moolenaare6a91fd2008-07-24 18:51:11 +00003175 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
3176# else /* Win16 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 SetClassWord(s_textArea, GCW_HCURSOR, (WORD)LoadCursor(NULL, idc));
3178# endif
3179#endif
3180 if (!p_mh)
3181 {
3182 POINT mp;
3183
3184 /* Set the position to make it redrawn with the new shape. */
3185 (void)GetCursorPos((LPPOINT)&mp);
3186 (void)SetCursorPos(mp.x, mp.y);
3187 ShowCursor(TRUE);
3188 }
3189 }
3190}
3191#endif
3192
3193#ifdef FEAT_BROWSE
3194/*
3195 * The file browser exists in two versions: with "W" uses wide characters,
3196 * without "W" the current codepage. When FEAT_MBYTE is defined and on
3197 * Windows NT/2000/XP the "W" functions are used.
3198 */
3199
3200# if defined(FEAT_MBYTE) && defined(WIN3264)
3201/*
3202 * Wide version of convert_filter(). Keep in sync!
3203 */
3204 static WCHAR *
3205convert_filterW(char_u *s)
3206{
3207 WCHAR *res;
3208 unsigned s_len = (unsigned)STRLEN(s);
3209 unsigned i;
3210
3211 res = (WCHAR *)alloc((s_len + 3) * sizeof(WCHAR));
3212 if (res != NULL)
3213 {
3214 for (i = 0; i < s_len; ++i)
3215 if (s[i] == '\t' || s[i] == '\n')
3216 res[i] = '\0';
3217 else
3218 res[i] = s[i];
3219 res[s_len] = NUL;
3220 /* Add two extra NULs to make sure it's properly terminated. */
3221 res[s_len + 1] = NUL;
3222 res[s_len + 2] = NUL;
3223 }
3224 return res;
3225}
3226
3227/*
3228 * Wide version of gui_mch_browse(). Keep in sync!
3229 */
3230 static char_u *
3231gui_mch_browseW(
3232 int saving,
3233 char_u *title,
3234 char_u *dflt,
3235 char_u *ext,
3236 char_u *initdir,
3237 char_u *filter)
3238{
Bram Moolenaar36f692d2008-11-20 16:10:17 +00003239 /* We always use the wide function. This means enc_to_utf16() must work,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 * otherwise it fails miserably! */
3241 OPENFILENAMEW fileStruct;
3242 WCHAR fileBuf[MAXPATHL];
3243 WCHAR *wp;
3244 int i;
3245 WCHAR *titlep = NULL;
3246 WCHAR *extp = NULL;
3247 WCHAR *initdirp = NULL;
3248 WCHAR *filterp;
3249 char_u *p;
3250
3251 if (dflt == NULL)
3252 fileBuf[0] = NUL;
3253 else
3254 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +00003255 wp = enc_to_utf16(dflt, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 if (wp == NULL)
3257 fileBuf[0] = NUL;
3258 else
3259 {
3260 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3261 fileBuf[i] = wp[i];
3262 fileBuf[i] = NUL;
3263 vim_free(wp);
3264 }
3265 }
3266
3267 /* Convert the filter to Windows format. */
3268 filterp = convert_filterW(filter);
3269
3270 memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
3271#ifdef OPENFILENAME_SIZE_VERSION_400
3272 /* be compatible with Windows NT 4.0 */
3273 /* TODO: what to use for OPENFILENAMEW??? */
3274 fileStruct.lStructSize = sizeof(OPENFILENAME_SIZE_VERSION_400);
3275#else
3276 fileStruct.lStructSize = sizeof(fileStruct);
3277#endif
3278
3279 if (title != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00003280 titlep = enc_to_utf16(title, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 fileStruct.lpstrTitle = titlep;
3282
3283 if (ext != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00003284 extp = enc_to_utf16(ext, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 fileStruct.lpstrDefExt = extp;
3286
3287 fileStruct.lpstrFile = fileBuf;
3288 fileStruct.nMaxFile = MAXPATHL;
3289 fileStruct.lpstrFilter = filterp;
3290 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3291 /* has an initial dir been specified? */
3292 if (initdir != NULL && *initdir != NUL)
3293 {
3294 /* Must have backslashes here, no matter what 'shellslash' says */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00003295 initdirp = enc_to_utf16(initdir, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 if (initdirp != NULL)
3297 {
3298 for (wp = initdirp; *wp != NUL; ++wp)
3299 if (*wp == '/')
3300 *wp = '\\';
3301 }
3302 fileStruct.lpstrInitialDir = initdirp;
3303 }
3304
3305 /*
3306 * TODO: Allow selection of multiple files. Needs another arg to this
3307 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3308 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3309 * files that don't exist yet, so I haven't put it in. What about
3310 * OFN_PATHMUSTEXIST?
3311 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3312 */
3313 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
3314#ifdef FEAT_SHORTCUT
3315 if (curbuf->b_p_bin)
3316 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
3317#endif
3318 if (saving)
3319 {
3320 if (!GetSaveFileNameW(&fileStruct))
3321 return NULL;
3322 }
3323 else
3324 {
3325 if (!GetOpenFileNameW(&fileStruct))
3326 return NULL;
3327 }
3328
3329 vim_free(filterp);
3330 vim_free(initdirp);
3331 vim_free(titlep);
3332 vim_free(extp);
3333
3334 /* Convert from UCS2 to 'encoding'. */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00003335 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 if (p != NULL)
3337 /* when out of memory we get garbage for non-ASCII chars */
3338 STRCPY(fileBuf, p);
3339 vim_free(p);
3340
3341 /* Give focus back to main window (when using MDI). */
3342 SetFocus(s_hwnd);
3343
3344 /* Shorten the file name if possible */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003345 return vim_strsave(shorten_fname1((char_u *)fileBuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346}
3347# endif /* FEAT_MBYTE */
3348
3349
3350/*
3351 * Convert the string s to the proper format for a filter string by replacing
Bram Moolenaar12806c82008-11-12 12:36:30 +00003352 * the \t and \n delimiters with \0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 * Returns the converted string in allocated memory.
3354 *
3355 * Keep in sync with convert_filterW() above!
3356 */
3357 static char_u *
3358convert_filter(char_u *s)
3359{
3360 char_u *res;
3361 unsigned s_len = (unsigned)STRLEN(s);
3362 unsigned i;
3363
3364 res = alloc(s_len + 3);
3365 if (res != NULL)
3366 {
3367 for (i = 0; i < s_len; ++i)
3368 if (s[i] == '\t' || s[i] == '\n')
3369 res[i] = '\0';
3370 else
3371 res[i] = s[i];
3372 res[s_len] = NUL;
3373 /* Add two extra NULs to make sure it's properly terminated. */
3374 res[s_len + 1] = NUL;
3375 res[s_len + 2] = NUL;
3376 }
3377 return res;
3378}
3379
3380/*
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003381 * Select a directory.
3382 */
3383 char_u *
3384gui_mch_browsedir(char_u *title, char_u *initdir)
3385{
3386 /* We fake this: Use a filter that doesn't select anything and a default
3387 * file name that won't be used. */
3388 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3389 initdir, (char_u *)_("Directory\t*.nothing\n"));
3390}
3391
3392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 * Pop open a file browser and return the file selected, in allocated memory,
3394 * or NULL if Cancel is hit.
3395 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3396 * title - Title message for the file browser dialog.
3397 * dflt - Default name of file.
3398 * ext - Default extension to be added to files without extensions.
3399 * initdir - directory in which to open the browser (NULL = current dir)
3400 * filter - Filter for matched files to choose from.
3401 *
3402 * Keep in sync with gui_mch_browseW() above!
3403 */
3404 char_u *
3405gui_mch_browse(
3406 int saving,
3407 char_u *title,
3408 char_u *dflt,
3409 char_u *ext,
3410 char_u *initdir,
3411 char_u *filter)
3412{
3413 OPENFILENAME fileStruct;
3414 char_u fileBuf[MAXPATHL];
3415 char_u *initdirp = NULL;
3416 char_u *filterp;
3417 char_u *p;
3418
3419# if defined(FEAT_MBYTE) && defined(WIN3264)
3420 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
3421 return gui_mch_browseW(saving, title, dflt, ext, initdir, filter);
3422# endif
3423
3424 if (dflt == NULL)
3425 fileBuf[0] = NUL;
3426 else
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +00003427 vim_strncpy(fileBuf, dflt, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428
3429 /* Convert the filter to Windows format. */
3430 filterp = convert_filter(filter);
3431
3432 memset(&fileStruct, 0, sizeof(OPENFILENAME));
3433#ifdef OPENFILENAME_SIZE_VERSION_400
3434 /* be compatible with Windows NT 4.0 */
3435 fileStruct.lStructSize = sizeof(OPENFILENAME_SIZE_VERSION_400);
3436#else
3437 fileStruct.lStructSize = sizeof(fileStruct);
3438#endif
3439
3440 fileStruct.lpstrTitle = title;
3441 fileStruct.lpstrDefExt = ext;
3442
3443 fileStruct.lpstrFile = fileBuf;
3444 fileStruct.nMaxFile = MAXPATHL;
3445 fileStruct.lpstrFilter = filterp;
3446 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3447 /* has an initial dir been specified? */
3448 if (initdir != NULL && *initdir != NUL)
3449 {
3450 /* Must have backslashes here, no matter what 'shellslash' says */
3451 initdirp = vim_strsave(initdir);
3452 if (initdirp != NULL)
3453 for (p = initdirp; *p != NUL; ++p)
3454 if (*p == '/')
3455 *p = '\\';
3456 fileStruct.lpstrInitialDir = initdirp;
3457 }
3458
3459 /*
3460 * TODO: Allow selection of multiple files. Needs another arg to this
3461 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3462 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3463 * files that don't exist yet, so I haven't put it in. What about
3464 * OFN_PATHMUSTEXIST?
3465 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3466 */
3467 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
3468#ifdef FEAT_SHORTCUT
3469 if (curbuf->b_p_bin)
3470 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
3471#endif
3472 if (saving)
3473 {
3474 if (!GetSaveFileName(&fileStruct))
3475 return NULL;
3476 }
3477 else
3478 {
3479 if (!GetOpenFileName(&fileStruct))
3480 return NULL;
3481 }
3482
3483 vim_free(filterp);
3484 vim_free(initdirp);
3485
3486 /* Give focus back to main window (when using MDI). */
3487 SetFocus(s_hwnd);
3488
3489 /* Shorten the file name if possible */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003490 return vim_strsave(shorten_fname1((char_u *)fileBuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491}
3492#endif /* FEAT_BROWSE */
3493
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003494/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 static void
3496_OnDropFiles(
3497 HWND hwnd,
3498 HDROP hDrop)
3499{
3500#ifdef FEAT_WINDOWS
3501#ifdef WIN3264
3502# define BUFPATHLEN _MAX_PATH
3503# define DRAGQVAL 0xFFFFFFFF
3504#else
3505# define BUFPATHLEN MAXPATHL
3506# define DRAGQVAL 0xFFFF
3507#endif
3508#ifdef FEAT_MBYTE
3509 WCHAR wszFile[BUFPATHLEN];
3510#endif
3511 char szFile[BUFPATHLEN];
3512 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3513 UINT i;
3514 char_u **fnames;
3515 POINT pt;
3516 int_u modifiers = 0;
3517
3518 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3519
3520 /* Obtain dropped position */
3521 DragQueryPoint(hDrop, &pt);
3522 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3523
3524# ifdef FEAT_VISUAL
3525 reset_VIsual();
3526# endif
3527
3528 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3529
3530 if (fnames != NULL)
3531 for (i = 0; i < cFiles; ++i)
3532 {
3533#ifdef FEAT_MBYTE
3534 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00003535 fnames[i] = utf16_to_enc(wszFile, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 else
3537#endif
3538 {
3539 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3540 fnames[i] = vim_strsave(szFile);
3541 }
3542 }
3543
3544 DragFinish(hDrop);
3545
3546 if (fnames != NULL)
3547 {
3548 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3549 modifiers |= MOUSE_SHIFT;
3550 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3551 modifiers |= MOUSE_CTRL;
3552 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3553 modifiers |= MOUSE_ALT;
3554
3555 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3556
3557 s_need_activate = TRUE;
3558 }
3559#endif
3560}
3561
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003562/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 static int
3564_OnScroll(
3565 HWND hwnd,
3566 HWND hwndCtl,
3567 UINT code,
3568 int pos)
3569{
3570 static UINT prev_code = 0; /* code of previous call */
3571 scrollbar_T *sb, *sb_info;
3572 long val;
3573 int dragging = FALSE;
3574 int dont_scroll_save = dont_scroll;
3575#ifndef WIN3264
3576 int nPos;
3577#else
3578 SCROLLINFO si;
3579
3580 si.cbSize = sizeof(si);
3581 si.fMask = SIF_POS;
3582#endif
3583
3584 sb = gui_mswin_find_scrollbar(hwndCtl);
3585 if (sb == NULL)
3586 return 0;
3587
3588 if (sb->wp != NULL) /* Left or right scrollbar */
3589 {
3590 /*
3591 * Careful: need to get scrollbar info out of first (left) scrollbar
3592 * for window, but keep real scrollbar too because we must pass it to
3593 * gui_drag_scrollbar().
3594 */
3595 sb_info = &sb->wp->w_scrollbars[0];
3596 }
3597 else /* Bottom scrollbar */
3598 sb_info = sb;
3599 val = sb_info->value;
3600
3601 switch (code)
3602 {
3603 case SB_THUMBTRACK:
3604 val = pos;
3605 dragging = TRUE;
3606 if (sb->scroll_shift > 0)
3607 val <<= sb->scroll_shift;
3608 break;
3609 case SB_LINEDOWN:
3610 val++;
3611 break;
3612 case SB_LINEUP:
3613 val--;
3614 break;
3615 case SB_PAGEDOWN:
3616 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3617 break;
3618 case SB_PAGEUP:
3619 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3620 break;
3621 case SB_TOP:
3622 val = 0;
3623 break;
3624 case SB_BOTTOM:
3625 val = sb_info->max;
3626 break;
3627 case SB_ENDSCROLL:
3628 if (prev_code == SB_THUMBTRACK)
3629 {
3630 /*
3631 * "pos" only gives us 16-bit data. In case of large file,
3632 * use GetScrollPos() which returns 32-bit. Unfortunately it
3633 * is not valid while the scrollbar is being dragged.
3634 */
3635 val = GetScrollPos(hwndCtl, SB_CTL);
3636 if (sb->scroll_shift > 0)
3637 val <<= sb->scroll_shift;
3638 }
3639 break;
3640
3641 default:
3642 /* TRACE("Unknown scrollbar event %d\n", code); */
3643 return 0;
3644 }
3645 prev_code = code;
3646
3647#ifdef WIN3264
3648 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3649 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
3650#else
3651 nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3652 SetScrollPos(hwndCtl, SB_CTL, nPos, TRUE);
3653#endif
3654
3655 /*
3656 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3657 */
3658 if (sb->wp != NULL)
3659 {
3660 scrollbar_T *sba = sb->wp->w_scrollbars;
3661 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3662
3663#ifdef WIN3264
3664 SetScrollInfo(id, SB_CTL, &si, TRUE);
3665#else
3666 SetScrollPos(id, SB_CTL, nPos, TRUE);
3667#endif
3668 }
3669
3670 /* Don't let us be interrupted here by another message. */
3671 s_busy_processing = TRUE;
3672
3673 /* When "allow_scrollbar" is FALSE still need to remember the new
3674 * position, but don't actually scroll by setting "dont_scroll". */
3675 dont_scroll = !allow_scrollbar;
3676
3677 gui_drag_scrollbar(sb, val, dragging);
3678
3679 s_busy_processing = FALSE;
3680 dont_scroll = dont_scroll_save;
3681
3682 return 0;
3683}
3684
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003685
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686/*
3687 * Get command line arguments.
3688 * Use "prog" as the name of the program and "cmdline" as the arguments.
3689 * Copy the arguments to allocated memory.
3690 * Return the number of arguments (including program name).
Bram Moolenaar12806c82008-11-12 12:36:30 +00003691 * Return pointers to the arguments in "argvp". Memory is allocated with
3692 * malloc(), use free() instead of vim_free().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 * Return pointer to buffer in "tofree".
3694 * Returns zero when out of memory.
3695 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003696/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 int
3698get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
3699{
3700 int i;
3701 char *p;
3702 char *progp;
3703 char *pnew = NULL;
3704 char *newcmdline;
3705 int inquote;
3706 int argc;
3707 char **argv = NULL;
3708 int round;
3709
Bram Moolenaar12806c82008-11-12 12:36:30 +00003710 *tofree = NULL;
3711
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003712#ifdef FEAT_MBYTE
3713 /* Try using the Unicode version first, it takes care of conversion when
3714 * 'encoding' is changed. */
3715 argc = get_cmd_argsW(&argv);
3716 if (argc != 0)
3717 goto done;
3718#endif
3719
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 /* Handle the program name. Remove the ".exe" extension, and find the 1st
3721 * non-space. */
3722 p = strrchr(prog, '.');
3723 if (p != NULL)
3724 *p = NUL;
3725 for (progp = prog; *progp == ' '; ++progp)
3726 ;
3727
3728 /* The command line is copied to allocated memory, so that we can change
3729 * it. Add the size of the string, the separating NUL and a terminating
3730 * NUL. */
3731 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
3732 if (newcmdline == NULL)
3733 return 0;
3734
3735 /*
3736 * First round: count the number of arguments ("pnew" == NULL).
3737 * Second round: produce the arguments.
3738 */
3739 for (round = 1; round <= 2; ++round)
3740 {
3741 /* First argument is the program name. */
3742 if (pnew != NULL)
3743 {
3744 argv[0] = pnew;
3745 strcpy(pnew, progp);
3746 pnew += strlen(pnew);
3747 *pnew++ = NUL;
3748 }
3749
3750 /*
3751 * Isolate each argument and put it in argv[].
3752 */
3753 p = cmdline;
3754 argc = 1;
3755 while (*p != NUL)
3756 {
3757 inquote = FALSE;
3758 if (pnew != NULL)
3759 argv[argc] = pnew;
3760 ++argc;
3761 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
3762 {
3763 /* Backslashes are only special when followed by a double
3764 * quote. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003765 i = (int)strspn(p, "\\");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 if (p[i] == '"')
3767 {
3768 /* Halve the number of backslashes. */
3769 if (i > 1 && pnew != NULL)
3770 {
3771 memset(pnew, '\\', i / 2);
3772 pnew += i / 2;
3773 }
3774
3775 /* Even nr of backslashes toggles quoting, uneven copies
3776 * the double quote. */
3777 if ((i & 1) == 0)
3778 inquote = !inquote;
3779 else if (pnew != NULL)
3780 *pnew++ = '"';
3781 p += i + 1;
3782 }
3783 else if (i > 0)
3784 {
3785 /* Copy span of backslashes unmodified. */
3786 if (pnew != NULL)
3787 {
3788 memset(pnew, '\\', i);
3789 pnew += i;
3790 }
3791 p += i;
3792 }
3793 else
3794 {
3795 if (pnew != NULL)
3796 *pnew++ = *p;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00003797#ifdef FEAT_MBYTE
3798 /* Can't use mb_* functions, because 'encoding' is not
3799 * initialized yet here. */
3800 if (IsDBCSLeadByte(*p))
3801 {
3802 ++p;
3803 if (pnew != NULL)
3804 *pnew++ = *p;
3805 }
3806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 ++p;
3808 }
3809 }
3810
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003811 if (pnew != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 *pnew++ = NUL;
3813 while (*p == ' ' || *p == '\t')
3814 ++p; /* advance until a non-space */
3815 }
3816
3817 if (round == 1)
3818 {
3819 argv = (char **)malloc((argc + 1) * sizeof(char *));
3820 if (argv == NULL )
Bram Moolenaare6b165e2005-06-30 21:56:01 +00003821 {
Bram Moolenaar12806c82008-11-12 12:36:30 +00003822 free(newcmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 return 0; /* malloc error */
Bram Moolenaare6b165e2005-06-30 21:56:01 +00003824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 pnew = newcmdline;
Bram Moolenaar12806c82008-11-12 12:36:30 +00003826 *tofree = newcmdline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 }
3828 }
3829
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003830done:
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003831 argv[argc] = NULL; /* NULL-terminated list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 *argvp = argv;
3833 return argc;
3834}