blob: 1d380fbf930e08b03511fd0e4acb82bba0a2d902 [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;
Bram Moolenaar8424a622006-04-19 21:23:36 +00002455#ifdef FEAT_MBYTE
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00002456 static int use_unicode = FALSE;
2457 int uu;
Bram Moolenaar8424a622006-04-19 21:23:36 +00002458 WCHAR *wstr = NULL;
2459#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002460
2461 if (s_tabhwnd == NULL)
2462 return;
2463
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00002464#if defined(FEAT_MBYTE)
2465# ifndef CCM_SETUNICODEFORMAT
2466 /* For older compilers. We assume this never changes. */
2467# define CCM_SETUNICODEFORMAT 0x2005
2468# endif
2469 uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2470 if (uu != use_unicode)
2471 {
2472 /* Enable/disable unicode support */
2473 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2474 use_unicode = uu;
2475 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00002476#endif
2477
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002478 tie.mask = TCIF_TEXT;
2479 tie.iImage = -1;
2480
Bram Moolenaar4b166d02012-12-12 17:12:25 +01002481 /* Disable redraw for tab updates to eliminate O(N^2) draws. */
2482 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2483
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002484 /* Add a label for each tab page. They all contain the same text area. */
2485 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2486 {
2487 if (tp == curtab)
2488 curtabidx = nr;
2489
Bram Moolenaar4b166d02012-12-12 17:12:25 +01002490 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002491 {
2492 /* Add the tab */
2493 tie.pszText = "-Empty-";
2494 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2495 }
2496
Bram Moolenaar57657d82006-04-21 22:12:41 +00002497 get_tabline_label(tp, FALSE);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002498 tie.pszText = NameBuff;
Bram Moolenaar8424a622006-04-19 21:23:36 +00002499#ifdef FEAT_MBYTE
2500 wstr = NULL;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00002501 if (use_unicode)
Bram Moolenaar8424a622006-04-19 21:23:36 +00002502 {
2503 /* Need to go through Unicode. */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002504 wstr = enc_to_utf16(NameBuff, NULL);
Bram Moolenaar8424a622006-04-19 21:23:36 +00002505 if (wstr != NULL)
2506 {
2507 TCITEMW tiw;
2508
2509 tiw.mask = TCIF_TEXT;
2510 tiw.iImage = -1;
2511 tiw.pszText = wstr;
Bram Moolenaar85f868c2006-11-28 16:16:58 +00002512 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
Bram Moolenaar8424a622006-04-19 21:23:36 +00002513 vim_free(wstr);
2514 }
2515 }
2516 if (wstr == NULL)
2517#endif
2518 {
2519 TabCtrl_SetItem(s_tabhwnd, nr, &tie);
2520 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002521 }
2522
2523 /* Remove any old labels. */
Bram Moolenaar4b166d02012-12-12 17:12:25 +01002524 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002525 TabCtrl_DeleteItem(s_tabhwnd, nr);
2526
2527 if (TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2528 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
Bram Moolenaar4b166d02012-12-12 17:12:25 +01002529
Bram Moolenaar14e28812012-12-16 12:50:39 +01002530 /* Re-enable redraw and redraw. */
Bram Moolenaar4b166d02012-12-12 17:12:25 +01002531 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
Bram Moolenaar14e28812012-12-16 12:50:39 +01002532 RedrawWindow(s_tabhwnd, NULL, NULL,
2533 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002534}
2535
2536/*
2537 * Set the current tab to "nr". First tab is 1.
2538 */
2539 void
2540gui_mch_set_curtab(nr)
2541 int nr;
2542{
2543 if (s_tabhwnd == NULL)
2544 return;
2545
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00002546 if (TabCtrl_GetCurSel(s_tabhwnd) != nr -1)
2547 TabCtrl_SetCurSel(s_tabhwnd, nr -1);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002548}
2549
2550#endif
2551
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552/*
2553 * ":simalt" command.
2554 */
2555 void
2556ex_simalt(exarg_T *eap)
2557{
2558 char_u *keys = eap->arg;
2559
2560 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2561 while (*keys)
2562 {
2563 if (*keys == '~')
2564 *keys = ' '; /* for showing system menu */
2565 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2566 keys++;
2567 }
2568}
2569
2570/*
2571 * Create the find & replace dialogs.
2572 * You can't have both at once: ":find" when replace is showing, destroys
2573 * the replace dialog first, and the other way around.
2574 */
2575#ifdef MSWIN_FIND_REPLACE
2576 static void
2577initialise_findrep(char_u *initial_string)
2578{
2579 int wword = FALSE;
2580 int mcase = !p_ic;
2581 char_u *entry_text;
2582
2583 /* Get the search string to use. */
2584 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2585
2586 s_findrep_struct.hwndOwner = s_hwnd;
2587 s_findrep_struct.Flags = FR_DOWN;
2588 if (mcase)
2589 s_findrep_struct.Flags |= FR_MATCHCASE;
2590 if (wword)
2591 s_findrep_struct.Flags |= FR_WHOLEWORD;
2592 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +00002593 vim_strncpy(s_findrep_struct.lpstrFindWhat, entry_text,
2594 s_findrep_struct.wFindWhatLen - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 vim_free(entry_text);
2596}
2597#endif
2598
Bram Moolenaar908d53a2006-11-07 18:05:31 +00002599 static void
2600set_window_title(HWND hwnd, char *title)
2601{
2602#ifdef FEAT_MBYTE
2603 if (title != NULL && enc_codepage >= 0 && enc_codepage != (int)GetACP())
2604 {
2605 WCHAR *wbuf;
2606 int n;
2607
Bram Moolenaar36f692d2008-11-20 16:10:17 +00002608 /* Convert the title from 'encoding' to UTF-16. */
2609 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
Bram Moolenaar908d53a2006-11-07 18:05:31 +00002610 if (wbuf != NULL)
2611 {
2612 n = SetWindowTextW(hwnd, wbuf);
2613 vim_free(wbuf);
2614 if (n != 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
2615 return;
2616 /* Retry with non-wide function (for Windows 98). */
2617 }
2618 }
2619#endif
2620 (void)SetWindowText(hwnd, (LPCSTR)title);
2621}
2622
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 void
2624gui_mch_find_dialog(exarg_T *eap)
2625{
2626#ifdef MSWIN_FIND_REPLACE
2627 if (s_findrep_msg != 0)
2628 {
2629 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2630 DestroyWindow(s_findrep_hwnd);
2631
2632 if (!IsWindow(s_findrep_hwnd))
2633 {
2634 initialise_findrep(eap->arg);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00002635# if defined(FEAT_MBYTE) && defined(WIN3264)
2636 /* If the OS is Windows NT, and 'encoding' differs from active
2637 * codepage: convert text and use wide function. */
2638 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
2639 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2640 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002641 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00002642 s_findrep_hwnd = FindTextW(
2643 (LPFINDREPLACEW) &s_findrep_struct_w);
2644 }
2645 else
2646# endif
2647 s_findrep_hwnd = FindText((LPFINDREPLACE) &s_findrep_struct);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 }
2649
Bram Moolenaar908d53a2006-11-07 18:05:31 +00002650 set_window_title(s_findrep_hwnd,
2651 _("Find string (use '\\\\' to find a '\\')"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 (void)SetFocus(s_findrep_hwnd);
2653
2654 s_findrep_is_find = TRUE;
2655 }
2656#endif
2657}
2658
2659
2660 void
2661gui_mch_replace_dialog(exarg_T *eap)
2662{
2663#ifdef MSWIN_FIND_REPLACE
2664 if (s_findrep_msg != 0)
2665 {
2666 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2667 DestroyWindow(s_findrep_hwnd);
2668
2669 if (!IsWindow(s_findrep_hwnd))
2670 {
2671 initialise_findrep(eap->arg);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00002672# if defined(FEAT_MBYTE) && defined(WIN3264)
2673 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT
2674 && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2675 {
2676 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2677 s_findrep_hwnd = ReplaceTextW(
2678 (LPFINDREPLACEW) &s_findrep_struct_w);
2679 }
2680 else
2681# endif
2682 s_findrep_hwnd = ReplaceText(
2683 (LPFINDREPLACE) &s_findrep_struct);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 }
2685
Bram Moolenaar908d53a2006-11-07 18:05:31 +00002686 set_window_title(s_findrep_hwnd,
2687 _("Find & Replace (use '\\\\' to find a '\\')"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 (void)SetFocus(s_findrep_hwnd);
2689
2690 s_findrep_is_find = FALSE;
2691 }
2692#endif
2693}
2694
2695
2696/*
2697 * Set visibility of the pointer.
2698 */
2699 void
2700gui_mch_mousehide(int hide)
2701{
2702 if (hide != gui.pointer_hidden)
2703 {
2704 ShowCursor(!hide);
2705 gui.pointer_hidden = hide;
2706 }
2707}
2708
2709#ifdef FEAT_MENU
2710 static void
2711gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2712{
2713 /* Unhide the mouse, we don't get move events here. */
2714 gui_mch_mousehide(FALSE);
2715
2716 (void)TrackPopupMenu(
2717 (HMENU)menu->submenu_id,
2718 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2719 x, y,
2720 (int)0, /*reserved param*/
2721 s_hwnd,
2722 NULL);
2723 /*
2724 * NOTE: The pop-up menu can eat the mouse up event.
2725 * We deal with this in normal.c.
2726 */
2727}
2728#endif
2729
2730/*
2731 * Got a message when the system will go down.
2732 */
2733 static void
2734_OnEndSession(void)
2735{
2736 getout_preserve_modified(1);
2737}
2738
2739/*
2740 * Get this message when the user clicks on the cross in the top right corner
2741 * of a Windows95 window.
2742 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002743/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 static void
2745_OnClose(
2746 HWND hwnd)
2747{
2748 gui_shell_closed();
2749}
2750
2751/*
2752 * Get a message when the window is being destroyed.
2753 */
2754 static void
2755_OnDestroy(
2756 HWND hwnd)
2757{
2758#ifdef WIN16_3DLOOK
2759 Ctl3dUnregister(s_hinst);
2760#endif
2761 if (!destroying)
2762 _OnClose(hwnd);
2763}
2764
2765 static void
2766_OnPaint(
2767 HWND hwnd)
2768{
2769 if (!IsMinimized(hwnd))
2770 {
2771 PAINTSTRUCT ps;
2772
2773 out_flush(); /* make sure all output has been processed */
2774 (void)BeginPaint(hwnd, &ps);
2775
2776#ifdef FEAT_MBYTE
2777 /* prevent multi-byte characters from misprinting on an invalid
2778 * rectangle */
2779 if (has_mbyte)
2780 {
2781 RECT rect;
2782
2783 GetClientRect(hwnd, &rect);
2784 ps.rcPaint.left = rect.left;
2785 ps.rcPaint.right = rect.right;
2786 }
2787#endif
2788
2789 if (!IsRectEmpty(&ps.rcPaint))
2790 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2791 ps.rcPaint.right - ps.rcPaint.left + 1,
2792 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2793 EndPaint(hwnd, &ps);
2794 }
2795}
2796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002797/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 static void
2799_OnSize(
2800 HWND hwnd,
2801 UINT state,
2802 int cx,
2803 int cy)
2804{
2805 if (!IsMinimized(hwnd))
2806 {
2807 gui_resize_shell(cx, cy);
2808
2809#ifdef FEAT_MENU
2810 /* Menu bar may wrap differently now */
2811 gui_mswin_get_menu_height(TRUE);
2812#endif
2813 }
2814}
2815
2816 static void
2817_OnSetFocus(
2818 HWND hwnd,
2819 HWND hwndOldFocus)
2820{
2821 gui_focus_change(TRUE);
Bram Moolenaarea408882007-12-03 21:21:03 +00002822 s_getting_focus = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2824}
2825
2826 static void
2827_OnKillFocus(
2828 HWND hwnd,
2829 HWND hwndNewFocus)
2830{
2831 gui_focus_change(FALSE);
Bram Moolenaarea408882007-12-03 21:21:03 +00002832 s_getting_focus = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2834}
2835
2836/*
2837 * Get a message when the user switches back to vim
2838 */
2839#ifdef WIN16
2840 static BOOL
2841#else
2842 static LRESULT
2843#endif
2844_OnActivateApp(
2845 HWND hwnd,
2846 BOOL fActivate,
2847#ifdef WIN16
2848 HTASK dwThreadId
2849#else
2850 DWORD dwThreadId
2851#endif
2852 )
2853{
2854 /* we call gui_focus_change() in _OnSetFocus() */
2855 /* gui_focus_change((int)fActivate); */
2856 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2857}
2858
2859#if defined(FEAT_WINDOWS) || defined(PROTO)
2860 void
2861gui_mch_destroy_scrollbar(scrollbar_T *sb)
2862{
2863 DestroyWindow(sb->id);
2864}
2865#endif
2866
2867/*
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002868 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 */
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002870 void
Bram Moolenaara40c5002005-01-09 21:16:21 +00002871gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872{
2873 RECT rct;
2874 POINT mp;
2875
2876 (void)GetWindowRect(s_textArea, &rct);
2877 (void)GetCursorPos((LPPOINT)&mp);
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002878 *x = (int)(mp.x - rct.left);
2879 *y = (int)(mp.y - rct.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880}
2881
2882/*
2883 * Move mouse pointer to character at (x, y).
2884 */
2885 void
2886gui_mch_setmouse(int x, int y)
2887{
2888 RECT rct;
2889
2890 (void)GetWindowRect(s_textArea, &rct);
2891 (void)SetCursorPos(x + gui.border_offset + rct.left,
2892 y + gui.border_offset + rct.top);
2893}
2894
2895 static void
2896gui_mswin_get_valid_dimensions(
2897 int w,
2898 int h,
2899 int *valid_w,
2900 int *valid_h)
2901{
2902 int base_width, base_height;
2903
2904 base_width = gui_get_base_width()
2905 + GetSystemMetrics(SM_CXFRAME) * 2;
2906 base_height = gui_get_base_height()
2907 + GetSystemMetrics(SM_CYFRAME) * 2
2908 + GetSystemMetrics(SM_CYCAPTION)
2909#ifdef FEAT_MENU
2910 + gui_mswin_get_menu_height(FALSE)
2911#endif
2912 ;
2913 *valid_w = base_width +
2914 ((w - base_width) / gui.char_width) * gui.char_width;
2915 *valid_h = base_height +
2916 ((h - base_height) / gui.char_height) * gui.char_height;
2917}
2918
2919 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920gui_mch_flash(int msec)
2921{
2922 RECT rc;
2923
2924 /*
2925 * Note: InvertRect() excludes right and bottom of rectangle.
2926 */
2927 rc.left = 0;
2928 rc.top = 0;
2929 rc.right = gui.num_cols * gui.char_width;
2930 rc.bottom = gui.num_rows * gui.char_height;
2931 InvertRect(s_hdc, &rc);
2932 gui_mch_flush(); /* make sure it's displayed */
2933
2934 ui_delay((long)msec, TRUE); /* wait for a few msec */
2935
2936 InvertRect(s_hdc, &rc);
2937}
2938
2939/*
2940 * Return flags used for scrolling.
2941 * The SW_INVALIDATE is required when part of the window is covered or
2942 * off-screen. Refer to MS KB Q75236.
2943 */
2944 static int
2945get_scroll_flags(void)
2946{
2947 HWND hwnd;
2948 RECT rcVim, rcOther, rcDest;
2949
2950 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaar0d406992005-05-22 22:03:39 +00002951
2952 /* Check if the window is partly above or below the screen. We don't care
2953 * about partly left or right of the screen, it is not relevant when
2954 * scrolling up or down. */
2955 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
2956 return SW_INVALIDATE;
2957
2958 /* Check if there is an window (partly) on top of us. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
2960 if (IsWindowVisible(hwnd))
2961 {
2962 GetWindowRect(hwnd, &rcOther);
2963 if (IntersectRect(&rcDest, &rcVim, &rcOther))
2964 return SW_INVALIDATE;
2965 }
2966 return 0;
2967}
2968
2969/*
2970 * Delete the given number of lines from the given row, scrolling up any
2971 * text further down within the scroll region.
2972 */
2973 void
2974gui_mch_delete_lines(
2975 int row,
2976 int num_lines)
2977{
2978 RECT rc;
2979
2980 rc.left = FILL_X(gui.scroll_region_left);
2981 rc.right = FILL_X(gui.scroll_region_right + 1);
2982 rc.top = FILL_Y(row);
2983 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
2984
2985 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
2986 &rc, &rc, NULL, NULL, get_scroll_flags());
2987
2988 UpdateWindow(s_textArea);
2989 /* This seems to be required to avoid the cursor disappearing when
2990 * scrolling such that the cursor ends up in the top-left character on
2991 * the screen... But why? (Webb) */
2992 /* It's probably fixed by disabling drawing the cursor while scrolling. */
2993 /* gui.cursor_is_valid = FALSE; */
2994
2995 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
2996 gui.scroll_region_left,
2997 gui.scroll_region_bot, gui.scroll_region_right);
2998}
2999
3000/*
3001 * Insert the given number of lines before the given row, scrolling down any
3002 * following text within the scroll region.
3003 */
3004 void
3005gui_mch_insert_lines(
3006 int row,
3007 int num_lines)
3008{
3009 RECT rc;
3010
3011 rc.left = FILL_X(gui.scroll_region_left);
3012 rc.right = FILL_X(gui.scroll_region_right + 1);
3013 rc.top = FILL_Y(row);
3014 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3015 /* The SW_INVALIDATE is required when part of the window is covered or
3016 * off-screen. How do we avoid it when it's not needed? */
3017 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
3018 &rc, &rc, NULL, NULL, get_scroll_flags());
3019
3020 UpdateWindow(s_textArea);
3021
3022 gui_clear_block(row, gui.scroll_region_left,
3023 row + num_lines - 1, gui.scroll_region_right);
3024}
3025
3026
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003027/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 void
3029gui_mch_exit(int rc)
3030{
3031 ReleaseDC(s_textArea, s_hdc);
3032 DeleteObject(s_brush);
3033
3034#ifdef FEAT_TEAROFF
3035 /* Unload the tearoff bitmap */
3036 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3037#endif
3038
3039 /* Destroy our window (if we have one). */
3040 if (s_hwnd != NULL)
3041 {
3042 destroying = TRUE; /* ignore WM_DESTROY message now */
3043 DestroyWindow(s_hwnd);
3044 }
3045
3046#ifdef GLOBAL_IME
3047 global_ime_end();
3048#endif
3049}
3050
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003051 static char_u *
3052logfont2name(LOGFONT lf)
3053{
3054 char *p;
3055 char *res;
3056 char *charset_name;
3057
3058 charset_name = charset_id2name((int)lf.lfCharSet);
3059 res = alloc((unsigned)(strlen(lf.lfFaceName) + 20
3060 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
3061 if (res != NULL)
3062 {
3063 p = res;
3064 /* make a normal font string out of the lf thing:*/
3065 sprintf((char *)p, "%s:h%d", lf.lfFaceName, pixels_to_points(
3066 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
3067 while (*p)
3068 {
3069 if (*p == ' ')
3070 *p = '_';
3071 ++p;
3072 }
3073#ifndef MSWIN16_FASTTEXT
3074 if (lf.lfItalic)
3075 STRCAT(p, ":i");
3076 if (lf.lfWeight >= FW_BOLD)
3077 STRCAT(p, ":b");
3078#endif
3079 if (lf.lfUnderline)
3080 STRCAT(p, ":u");
3081 if (lf.lfStrikeOut)
3082 STRCAT(p, ":s");
3083 if (charset_name != NULL)
3084 {
3085 STRCAT(p, ":c");
3086 STRCAT(p, charset_name);
3087 }
3088 }
3089
3090 return res;
3091}
3092
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003094 * Initialise vim to use the font with the given name.
3095 * Return FAIL if the font could not be loaded, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003097/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 int
3099gui_mch_init_font(char_u *font_name, int fontset)
3100{
3101 LOGFONT lf;
3102 GuiFont font = NOFONT;
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003103 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104
3105 /* Load the font */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003106 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 font = get_font_handle(&lf);
3108 if (font == NOFONT)
3109 return FAIL;
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003110
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 if (font_name == NULL)
3112 font_name = lf.lfFaceName;
3113#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3114 norm_logfont = lf;
3115#endif
3116#ifdef FEAT_MBYTE_IME
3117 im_set_font(&lf);
3118#endif
3119 gui_mch_free_font(gui.norm_font);
3120 gui.norm_font = font;
3121 current_font_height = lf.lfHeight;
3122 GetFontSize(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003124 p = logfont2name(lf);
3125 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003127 hl_set_font_name(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003129 /* When setting 'guifont' to "*" replace it with the actual font name.
3130 * */
3131 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 vim_free(p_guifont);
3134 p_guifont = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003136 else
3137 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 }
3139
3140#ifndef MSWIN16_FASTTEXT
3141 gui_mch_free_font(gui.ital_font);
3142 gui.ital_font = NOFONT;
3143 gui_mch_free_font(gui.bold_font);
3144 gui.bold_font = NOFONT;
3145 gui_mch_free_font(gui.boldital_font);
3146 gui.boldital_font = NOFONT;
3147
3148 if (!lf.lfItalic)
3149 {
3150 lf.lfItalic = TRUE;
3151 gui.ital_font = get_font_handle(&lf);
3152 lf.lfItalic = FALSE;
3153 }
3154 if (lf.lfWeight < FW_BOLD)
3155 {
3156 lf.lfWeight = FW_BOLD;
3157 gui.bold_font = get_font_handle(&lf);
3158 if (!lf.lfItalic)
3159 {
3160 lf.lfItalic = TRUE;
3161 gui.boldital_font = get_font_handle(&lf);
3162 }
3163 }
3164#endif
3165
3166 return OK;
3167}
3168
Bram Moolenaar85f868c2006-11-28 16:16:58 +00003169#ifndef WPF_RESTORETOMAXIMIZED
3170# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3171#endif
3172
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173/*
3174 * Return TRUE if the GUI window is maximized, filling the whole screen.
3175 */
3176 int
3177gui_mch_maximized()
3178{
Bram Moolenaar85f868c2006-11-28 16:16:58 +00003179 WINDOWPLACEMENT wp;
3180
3181 wp.length = sizeof(WINDOWPLACEMENT);
3182 if (GetWindowPlacement(s_hwnd, &wp))
3183 return wp.showCmd == SW_SHOWMAXIMIZED
3184 || (wp.showCmd == SW_SHOWMINIMIZED
3185 && wp.flags == WPF_RESTORETOMAXIMIZED);
3186
3187 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188}
3189
3190/*
3191 * Called when the font changed while the window is maximized. Compute the
3192 * new Rows and Columns. This is like resizing the window.
3193 */
3194 void
3195gui_mch_newfont()
3196{
3197 RECT rect;
3198
3199 GetWindowRect(s_hwnd, &rect);
3200 gui_resize_shell(rect.right - rect.left
3201 - GetSystemMetrics(SM_CXFRAME) * 2,
3202 rect.bottom - rect.top
3203 - GetSystemMetrics(SM_CYFRAME) * 2
3204 - GetSystemMetrics(SM_CYCAPTION)
3205#ifdef FEAT_MENU
3206 - gui_mswin_get_menu_height(FALSE)
3207#endif
3208 );
3209}
3210
3211/*
3212 * Set the window title
3213 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003214/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 void
3216gui_mch_settitle(
3217 char_u *title,
3218 char_u *icon)
3219{
Bram Moolenaar908d53a2006-11-07 18:05:31 +00003220 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221}
3222
3223#ifdef FEAT_MOUSESHAPE
3224/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3225 * misc2.c! */
3226static LPCSTR mshape_idcs[] =
3227{
3228 MAKEINTRESOURCE(IDC_ARROW), /* arrow */
3229 MAKEINTRESOURCE(0), /* blank */
3230 MAKEINTRESOURCE(IDC_IBEAM), /* beam */
3231 MAKEINTRESOURCE(IDC_SIZENS), /* updown */
3232 MAKEINTRESOURCE(IDC_SIZENS), /* udsizing */
3233 MAKEINTRESOURCE(IDC_SIZEWE), /* leftright */
3234 MAKEINTRESOURCE(IDC_SIZEWE), /* lrsizing */
3235 MAKEINTRESOURCE(IDC_WAIT), /* busy */
3236#ifdef WIN3264
3237 MAKEINTRESOURCE(IDC_NO), /* no */
3238#else
3239 MAKEINTRESOURCE(IDC_ICON), /* no */
3240#endif
3241 MAKEINTRESOURCE(IDC_ARROW), /* crosshair */
3242 MAKEINTRESOURCE(IDC_ARROW), /* hand1 */
3243 MAKEINTRESOURCE(IDC_ARROW), /* hand2 */
3244 MAKEINTRESOURCE(IDC_ARROW), /* pencil */
3245 MAKEINTRESOURCE(IDC_ARROW), /* question */
3246 MAKEINTRESOURCE(IDC_ARROW), /* right-arrow */
3247 MAKEINTRESOURCE(IDC_UPARROW), /* up-arrow */
3248 MAKEINTRESOURCE(IDC_ARROW) /* last one */
3249};
3250
3251 void
3252mch_set_mouse_shape(int shape)
3253{
3254 LPCSTR idc;
3255
3256 if (shape == MSHAPE_HIDE)
3257 ShowCursor(FALSE);
3258 else
3259 {
3260 if (shape >= MSHAPE_NUMBERED)
3261 idc = MAKEINTRESOURCE(IDC_ARROW);
3262 else
3263 idc = mshape_idcs[shape];
Bram Moolenaare6a91fd2008-07-24 18:51:11 +00003264#ifdef SetClassLongPtr
3265 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266#else
3267# ifdef WIN32
Bram Moolenaare6a91fd2008-07-24 18:51:11 +00003268 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
3269# else /* Win16 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 SetClassWord(s_textArea, GCW_HCURSOR, (WORD)LoadCursor(NULL, idc));
3271# endif
3272#endif
3273 if (!p_mh)
3274 {
3275 POINT mp;
3276
3277 /* Set the position to make it redrawn with the new shape. */
3278 (void)GetCursorPos((LPPOINT)&mp);
3279 (void)SetCursorPos(mp.x, mp.y);
3280 ShowCursor(TRUE);
3281 }
3282 }
3283}
3284#endif
3285
3286#ifdef FEAT_BROWSE
3287/*
3288 * The file browser exists in two versions: with "W" uses wide characters,
3289 * without "W" the current codepage. When FEAT_MBYTE is defined and on
3290 * Windows NT/2000/XP the "W" functions are used.
3291 */
3292
3293# if defined(FEAT_MBYTE) && defined(WIN3264)
3294/*
Bram Moolenaar3ef7cdf2012-01-20 17:57:51 +01003295 * Wide version of convert_filter().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 */
3297 static WCHAR *
3298convert_filterW(char_u *s)
3299{
Bram Moolenaar3ef7cdf2012-01-20 17:57:51 +01003300 char_u *tmp;
3301 int len;
Bram Moolenaar90b28002012-01-20 20:54:19 +01003302 WCHAR *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303
Bram Moolenaar3ef7cdf2012-01-20 17:57:51 +01003304 tmp = convert_filter(s);
3305 if (tmp == NULL)
3306 return NULL;
3307 len = (int)STRLEN(s) + 3;
3308 res = enc_to_utf16(tmp, &len);
3309 vim_free(tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 return res;
3311}
3312
3313/*
3314 * Wide version of gui_mch_browse(). Keep in sync!
3315 */
3316 static char_u *
3317gui_mch_browseW(
3318 int saving,
3319 char_u *title,
3320 char_u *dflt,
3321 char_u *ext,
3322 char_u *initdir,
3323 char_u *filter)
3324{
Bram Moolenaar36f692d2008-11-20 16:10:17 +00003325 /* We always use the wide function. This means enc_to_utf16() must work,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 * otherwise it fails miserably! */
3327 OPENFILENAMEW fileStruct;
3328 WCHAR fileBuf[MAXPATHL];
3329 WCHAR *wp;
3330 int i;
3331 WCHAR *titlep = NULL;
3332 WCHAR *extp = NULL;
3333 WCHAR *initdirp = NULL;
3334 WCHAR *filterp;
3335 char_u *p;
3336
3337 if (dflt == NULL)
3338 fileBuf[0] = NUL;
3339 else
3340 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +00003341 wp = enc_to_utf16(dflt, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 if (wp == NULL)
3343 fileBuf[0] = NUL;
3344 else
3345 {
3346 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3347 fileBuf[i] = wp[i];
3348 fileBuf[i] = NUL;
3349 vim_free(wp);
3350 }
3351 }
3352
3353 /* Convert the filter to Windows format. */
3354 filterp = convert_filterW(filter);
3355
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003356 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357#ifdef OPENFILENAME_SIZE_VERSION_400
3358 /* be compatible with Windows NT 4.0 */
3359 /* TODO: what to use for OPENFILENAMEW??? */
Bram Moolenaar8c83ac32010-02-17 17:34:43 +01003360 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361#else
3362 fileStruct.lStructSize = sizeof(fileStruct);
3363#endif
3364
3365 if (title != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00003366 titlep = enc_to_utf16(title, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 fileStruct.lpstrTitle = titlep;
3368
3369 if (ext != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00003370 extp = enc_to_utf16(ext, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 fileStruct.lpstrDefExt = extp;
3372
3373 fileStruct.lpstrFile = fileBuf;
3374 fileStruct.nMaxFile = MAXPATHL;
3375 fileStruct.lpstrFilter = filterp;
3376 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3377 /* has an initial dir been specified? */
3378 if (initdir != NULL && *initdir != NUL)
3379 {
3380 /* Must have backslashes here, no matter what 'shellslash' says */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00003381 initdirp = enc_to_utf16(initdir, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 if (initdirp != NULL)
3383 {
3384 for (wp = initdirp; *wp != NUL; ++wp)
3385 if (*wp == '/')
3386 *wp = '\\';
3387 }
3388 fileStruct.lpstrInitialDir = initdirp;
3389 }
3390
3391 /*
3392 * TODO: Allow selection of multiple files. Needs another arg to this
3393 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3394 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3395 * files that don't exist yet, so I haven't put it in. What about
3396 * OFN_PATHMUSTEXIST?
3397 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3398 */
3399 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
3400#ifdef FEAT_SHORTCUT
3401 if (curbuf->b_p_bin)
3402 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
3403#endif
3404 if (saving)
3405 {
3406 if (!GetSaveFileNameW(&fileStruct))
3407 return NULL;
3408 }
3409 else
3410 {
3411 if (!GetOpenFileNameW(&fileStruct))
3412 return NULL;
3413 }
3414
3415 vim_free(filterp);
3416 vim_free(initdirp);
3417 vim_free(titlep);
3418 vim_free(extp);
3419
3420 /* Convert from UCS2 to 'encoding'. */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00003421 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 if (p != NULL)
3423 /* when out of memory we get garbage for non-ASCII chars */
3424 STRCPY(fileBuf, p);
3425 vim_free(p);
3426
3427 /* Give focus back to main window (when using MDI). */
3428 SetFocus(s_hwnd);
3429
3430 /* Shorten the file name if possible */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003431 return vim_strsave(shorten_fname1((char_u *)fileBuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432}
3433# endif /* FEAT_MBYTE */
3434
3435
3436/*
3437 * Convert the string s to the proper format for a filter string by replacing
Bram Moolenaar12806c82008-11-12 12:36:30 +00003438 * the \t and \n delimiters with \0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 * Returns the converted string in allocated memory.
3440 *
3441 * Keep in sync with convert_filterW() above!
3442 */
3443 static char_u *
3444convert_filter(char_u *s)
3445{
3446 char_u *res;
3447 unsigned s_len = (unsigned)STRLEN(s);
3448 unsigned i;
3449
3450 res = alloc(s_len + 3);
3451 if (res != NULL)
3452 {
3453 for (i = 0; i < s_len; ++i)
3454 if (s[i] == '\t' || s[i] == '\n')
3455 res[i] = '\0';
3456 else
3457 res[i] = s[i];
3458 res[s_len] = NUL;
3459 /* Add two extra NULs to make sure it's properly terminated. */
3460 res[s_len + 1] = NUL;
3461 res[s_len + 2] = NUL;
3462 }
3463 return res;
3464}
3465
3466/*
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003467 * Select a directory.
3468 */
3469 char_u *
3470gui_mch_browsedir(char_u *title, char_u *initdir)
3471{
3472 /* We fake this: Use a filter that doesn't select anything and a default
3473 * file name that won't be used. */
3474 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3475 initdir, (char_u *)_("Directory\t*.nothing\n"));
3476}
3477
3478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 * Pop open a file browser and return the file selected, in allocated memory,
3480 * or NULL if Cancel is hit.
3481 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3482 * title - Title message for the file browser dialog.
3483 * dflt - Default name of file.
3484 * ext - Default extension to be added to files without extensions.
3485 * initdir - directory in which to open the browser (NULL = current dir)
3486 * filter - Filter for matched files to choose from.
3487 *
3488 * Keep in sync with gui_mch_browseW() above!
3489 */
3490 char_u *
3491gui_mch_browse(
3492 int saving,
3493 char_u *title,
3494 char_u *dflt,
3495 char_u *ext,
3496 char_u *initdir,
3497 char_u *filter)
3498{
3499 OPENFILENAME fileStruct;
3500 char_u fileBuf[MAXPATHL];
3501 char_u *initdirp = NULL;
3502 char_u *filterp;
3503 char_u *p;
3504
3505# if defined(FEAT_MBYTE) && defined(WIN3264)
3506 if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)
3507 return gui_mch_browseW(saving, title, dflt, ext, initdir, filter);
3508# endif
3509
3510 if (dflt == NULL)
3511 fileBuf[0] = NUL;
3512 else
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +00003513 vim_strncpy(fileBuf, dflt, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514
3515 /* Convert the filter to Windows format. */
3516 filterp = convert_filter(filter);
3517
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003518 vim_memset(&fileStruct, 0, sizeof(OPENFILENAME));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519#ifdef OPENFILENAME_SIZE_VERSION_400
3520 /* be compatible with Windows NT 4.0 */
Bram Moolenaar8c83ac32010-02-17 17:34:43 +01003521 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522#else
3523 fileStruct.lStructSize = sizeof(fileStruct);
3524#endif
3525
3526 fileStruct.lpstrTitle = title;
3527 fileStruct.lpstrDefExt = ext;
3528
3529 fileStruct.lpstrFile = fileBuf;
3530 fileStruct.nMaxFile = MAXPATHL;
3531 fileStruct.lpstrFilter = filterp;
3532 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3533 /* has an initial dir been specified? */
3534 if (initdir != NULL && *initdir != NUL)
3535 {
3536 /* Must have backslashes here, no matter what 'shellslash' says */
3537 initdirp = vim_strsave(initdir);
3538 if (initdirp != NULL)
3539 for (p = initdirp; *p != NUL; ++p)
3540 if (*p == '/')
3541 *p = '\\';
3542 fileStruct.lpstrInitialDir = initdirp;
3543 }
3544
3545 /*
3546 * TODO: Allow selection of multiple files. Needs another arg to this
3547 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3548 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3549 * files that don't exist yet, so I haven't put it in. What about
3550 * OFN_PATHMUSTEXIST?
3551 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3552 */
3553 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
3554#ifdef FEAT_SHORTCUT
3555 if (curbuf->b_p_bin)
3556 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
3557#endif
3558 if (saving)
3559 {
3560 if (!GetSaveFileName(&fileStruct))
3561 return NULL;
3562 }
3563 else
3564 {
3565 if (!GetOpenFileName(&fileStruct))
3566 return NULL;
3567 }
3568
3569 vim_free(filterp);
3570 vim_free(initdirp);
3571
3572 /* Give focus back to main window (when using MDI). */
3573 SetFocus(s_hwnd);
3574
3575 /* Shorten the file name if possible */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003576 return vim_strsave(shorten_fname1((char_u *)fileBuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577}
3578#endif /* FEAT_BROWSE */
3579
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003580/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 static void
3582_OnDropFiles(
3583 HWND hwnd,
3584 HDROP hDrop)
3585{
3586#ifdef FEAT_WINDOWS
3587#ifdef WIN3264
3588# define BUFPATHLEN _MAX_PATH
3589# define DRAGQVAL 0xFFFFFFFF
3590#else
3591# define BUFPATHLEN MAXPATHL
3592# define DRAGQVAL 0xFFFF
3593#endif
3594#ifdef FEAT_MBYTE
3595 WCHAR wszFile[BUFPATHLEN];
3596#endif
3597 char szFile[BUFPATHLEN];
3598 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3599 UINT i;
3600 char_u **fnames;
3601 POINT pt;
3602 int_u modifiers = 0;
3603
3604 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3605
3606 /* Obtain dropped position */
3607 DragQueryPoint(hDrop, &pt);
3608 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3609
3610# ifdef FEAT_VISUAL
3611 reset_VIsual();
3612# endif
3613
3614 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3615
3616 if (fnames != NULL)
3617 for (i = 0; i < cFiles; ++i)
3618 {
3619#ifdef FEAT_MBYTE
3620 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00003621 fnames[i] = utf16_to_enc(wszFile, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 else
3623#endif
3624 {
3625 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3626 fnames[i] = vim_strsave(szFile);
3627 }
3628 }
3629
3630 DragFinish(hDrop);
3631
3632 if (fnames != NULL)
3633 {
3634 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3635 modifiers |= MOUSE_SHIFT;
3636 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3637 modifiers |= MOUSE_CTRL;
3638 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3639 modifiers |= MOUSE_ALT;
3640
3641 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3642
3643 s_need_activate = TRUE;
3644 }
3645#endif
3646}
3647
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003648/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 static int
3650_OnScroll(
3651 HWND hwnd,
3652 HWND hwndCtl,
3653 UINT code,
3654 int pos)
3655{
3656 static UINT prev_code = 0; /* code of previous call */
3657 scrollbar_T *sb, *sb_info;
3658 long val;
3659 int dragging = FALSE;
3660 int dont_scroll_save = dont_scroll;
3661#ifndef WIN3264
3662 int nPos;
3663#else
3664 SCROLLINFO si;
3665
3666 si.cbSize = sizeof(si);
3667 si.fMask = SIF_POS;
3668#endif
3669
3670 sb = gui_mswin_find_scrollbar(hwndCtl);
3671 if (sb == NULL)
3672 return 0;
3673
3674 if (sb->wp != NULL) /* Left or right scrollbar */
3675 {
3676 /*
3677 * Careful: need to get scrollbar info out of first (left) scrollbar
3678 * for window, but keep real scrollbar too because we must pass it to
3679 * gui_drag_scrollbar().
3680 */
3681 sb_info = &sb->wp->w_scrollbars[0];
3682 }
3683 else /* Bottom scrollbar */
3684 sb_info = sb;
3685 val = sb_info->value;
3686
3687 switch (code)
3688 {
3689 case SB_THUMBTRACK:
3690 val = pos;
3691 dragging = TRUE;
3692 if (sb->scroll_shift > 0)
3693 val <<= sb->scroll_shift;
3694 break;
3695 case SB_LINEDOWN:
3696 val++;
3697 break;
3698 case SB_LINEUP:
3699 val--;
3700 break;
3701 case SB_PAGEDOWN:
3702 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3703 break;
3704 case SB_PAGEUP:
3705 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3706 break;
3707 case SB_TOP:
3708 val = 0;
3709 break;
3710 case SB_BOTTOM:
3711 val = sb_info->max;
3712 break;
3713 case SB_ENDSCROLL:
3714 if (prev_code == SB_THUMBTRACK)
3715 {
3716 /*
3717 * "pos" only gives us 16-bit data. In case of large file,
3718 * use GetScrollPos() which returns 32-bit. Unfortunately it
3719 * is not valid while the scrollbar is being dragged.
3720 */
3721 val = GetScrollPos(hwndCtl, SB_CTL);
3722 if (sb->scroll_shift > 0)
3723 val <<= sb->scroll_shift;
3724 }
3725 break;
3726
3727 default:
3728 /* TRACE("Unknown scrollbar event %d\n", code); */
3729 return 0;
3730 }
3731 prev_code = code;
3732
3733#ifdef WIN3264
3734 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3735 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
3736#else
3737 nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3738 SetScrollPos(hwndCtl, SB_CTL, nPos, TRUE);
3739#endif
3740
3741 /*
3742 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3743 */
3744 if (sb->wp != NULL)
3745 {
3746 scrollbar_T *sba = sb->wp->w_scrollbars;
3747 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3748
3749#ifdef WIN3264
3750 SetScrollInfo(id, SB_CTL, &si, TRUE);
3751#else
3752 SetScrollPos(id, SB_CTL, nPos, TRUE);
3753#endif
3754 }
3755
3756 /* Don't let us be interrupted here by another message. */
3757 s_busy_processing = TRUE;
3758
3759 /* When "allow_scrollbar" is FALSE still need to remember the new
3760 * position, but don't actually scroll by setting "dont_scroll". */
3761 dont_scroll = !allow_scrollbar;
3762
3763 gui_drag_scrollbar(sb, val, dragging);
3764
3765 s_busy_processing = FALSE;
3766 dont_scroll = dont_scroll_save;
3767
3768 return 0;
3769}
3770
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003771
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772/*
3773 * Get command line arguments.
3774 * Use "prog" as the name of the program and "cmdline" as the arguments.
3775 * Copy the arguments to allocated memory.
3776 * Return the number of arguments (including program name).
Bram Moolenaar12806c82008-11-12 12:36:30 +00003777 * Return pointers to the arguments in "argvp". Memory is allocated with
3778 * malloc(), use free() instead of vim_free().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 * Return pointer to buffer in "tofree".
3780 * Returns zero when out of memory.
3781 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003782/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 int
3784get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
3785{
3786 int i;
3787 char *p;
3788 char *progp;
3789 char *pnew = NULL;
3790 char *newcmdline;
3791 int inquote;
3792 int argc;
3793 char **argv = NULL;
3794 int round;
3795
Bram Moolenaar12806c82008-11-12 12:36:30 +00003796 *tofree = NULL;
3797
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003798#ifdef FEAT_MBYTE
3799 /* Try using the Unicode version first, it takes care of conversion when
3800 * 'encoding' is changed. */
3801 argc = get_cmd_argsW(&argv);
3802 if (argc != 0)
3803 goto done;
3804#endif
3805
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 /* Handle the program name. Remove the ".exe" extension, and find the 1st
3807 * non-space. */
3808 p = strrchr(prog, '.');
3809 if (p != NULL)
3810 *p = NUL;
3811 for (progp = prog; *progp == ' '; ++progp)
3812 ;
3813
3814 /* The command line is copied to allocated memory, so that we can change
3815 * it. Add the size of the string, the separating NUL and a terminating
3816 * NUL. */
3817 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
3818 if (newcmdline == NULL)
3819 return 0;
3820
3821 /*
3822 * First round: count the number of arguments ("pnew" == NULL).
3823 * Second round: produce the arguments.
3824 */
3825 for (round = 1; round <= 2; ++round)
3826 {
3827 /* First argument is the program name. */
3828 if (pnew != NULL)
3829 {
3830 argv[0] = pnew;
3831 strcpy(pnew, progp);
3832 pnew += strlen(pnew);
3833 *pnew++ = NUL;
3834 }
3835
3836 /*
3837 * Isolate each argument and put it in argv[].
3838 */
3839 p = cmdline;
3840 argc = 1;
3841 while (*p != NUL)
3842 {
3843 inquote = FALSE;
3844 if (pnew != NULL)
3845 argv[argc] = pnew;
3846 ++argc;
3847 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
3848 {
3849 /* Backslashes are only special when followed by a double
3850 * quote. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003851 i = (int)strspn(p, "\\");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 if (p[i] == '"')
3853 {
3854 /* Halve the number of backslashes. */
3855 if (i > 1 && pnew != NULL)
3856 {
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003857 vim_memset(pnew, '\\', i / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 pnew += i / 2;
3859 }
3860
3861 /* Even nr of backslashes toggles quoting, uneven copies
3862 * the double quote. */
3863 if ((i & 1) == 0)
3864 inquote = !inquote;
3865 else if (pnew != NULL)
3866 *pnew++ = '"';
3867 p += i + 1;
3868 }
3869 else if (i > 0)
3870 {
3871 /* Copy span of backslashes unmodified. */
3872 if (pnew != NULL)
3873 {
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003874 vim_memset(pnew, '\\', i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 pnew += i;
3876 }
3877 p += i;
3878 }
3879 else
3880 {
3881 if (pnew != NULL)
3882 *pnew++ = *p;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00003883#ifdef FEAT_MBYTE
3884 /* Can't use mb_* functions, because 'encoding' is not
3885 * initialized yet here. */
3886 if (IsDBCSLeadByte(*p))
3887 {
3888 ++p;
3889 if (pnew != NULL)
3890 *pnew++ = *p;
3891 }
3892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 ++p;
3894 }
3895 }
3896
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003897 if (pnew != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 *pnew++ = NUL;
3899 while (*p == ' ' || *p == '\t')
3900 ++p; /* advance until a non-space */
3901 }
3902
3903 if (round == 1)
3904 {
3905 argv = (char **)malloc((argc + 1) * sizeof(char *));
3906 if (argv == NULL )
Bram Moolenaare6b165e2005-06-30 21:56:01 +00003907 {
Bram Moolenaar12806c82008-11-12 12:36:30 +00003908 free(newcmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 return 0; /* malloc error */
Bram Moolenaare6b165e2005-06-30 21:56:01 +00003910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 pnew = newcmdline;
Bram Moolenaar12806c82008-11-12 12:36:30 +00003912 *tofree = newcmdline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 }
3914 }
3915
Bram Moolenaar8765a4a2010-07-27 22:41:43 +02003916#ifdef FEAT_MBYTE
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003917done:
Bram Moolenaar8765a4a2010-07-27 22:41:43 +02003918#endif
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003919 argv[argc] = NULL; /* NULL-terminated list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 *argvp = argv;
3921 return argc;
3922}