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