Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
vim
GitHub Repository: vim/vim
Path: blob/master/src/gui_w32.c
1760 views
1
/* vi:set ts=8 sts=4 sw=4 noet:
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
* Windows GUI.
12
*
13
* GUI support for Microsoft Windows, aka Win32. Also for Win64.
14
*
15
* George V. Reilly <[email protected]> wrote the original Win32 GUI.
16
* Robert Webb reworked it to use the existing GUI stuff and added menu,
17
* scrollbars, etc.
18
*
19
* Note: Clipboard stuff, for cutting and pasting text to other windows, is in
20
* winclip.c. (It can also be done from the terminal version).
21
*
22
* TODO: Some of the function signatures ought to be updated for Win64;
23
* e.g., replace LONG with LONG_PTR, etc.
24
*/
25
26
#include "vim.h"
27
28
#if defined(FEAT_DIRECTX)
29
# include "gui_dwrite.h"
30
#endif
31
32
// values for "dead_key"
33
#define DEAD_KEY_OFF 0 // no dead key
34
#define DEAD_KEY_SET_DEFAULT 1 // dead key pressed
35
#define DEAD_KEY_TRANSIENT_IN_ON_CHAR 2 // wait for next key press
36
#define DEAD_KEY_SKIP_ON_CHAR 3 // skip next _OnChar()
37
38
#if defined(FEAT_DIRECTX)
39
static DWriteContext *s_dwc = NULL;
40
static int s_directx_enabled = 0;
41
static int s_directx_load_attempted = 0;
42
# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL && enc_utf8)
43
static int directx_enabled(void);
44
static void directx_binddc(void);
45
#endif
46
47
#ifdef FEAT_MENU
48
static int gui_mswin_get_menu_height(int fix_window);
49
#else
50
# define gui_mswin_get_menu_height(fix_window) 0
51
#endif
52
53
typedef struct keycode_trans_strategy {
54
void (*ptr_on_char) (HWND /*hwnd UNUSED*/, UINT /*cch*/, int /*cRepeat UNUSED*/);
55
void (*ptr_on_sys_char) (HWND /*hwnd UNUSED*/, UINT /*cch*/, int /*cRepeat UNUSED*/);
56
void (*ptr_process_message_usual_key) (UINT /*vk*/, const MSG* /*pmsg*/);
57
int (*ptr_get_active_modifiers)(void);
58
int (*is_experimental)(void);
59
} keycode_trans_strategy;
60
61
// forward declarations for input instance initializer
62
static void _OnChar_experimental(HWND /*hwnd UNUSED*/, UINT /*cch*/, int /*cRepeat UNUSED*/);
63
static void _OnSysChar_experimental(HWND /*hwnd UNUSED*/, UINT /*cch*/, int /*cRepeat UNUSED*/);
64
static void process_message_usual_key_experimental(UINT /*vk*/, const MSG* /*pmsg*/);
65
static int get_active_modifiers_experimental(void);
66
static int is_experimental_true(void);
67
68
keycode_trans_strategy keycode_trans_strategy_experimental = {
69
_OnChar_experimental // ptr_on_char
70
, _OnSysChar_experimental // ptr_on_sys_char
71
, process_message_usual_key_experimental // ptr_process_message_usual_key
72
, get_active_modifiers_experimental
73
, is_experimental_true
74
};
75
76
// forward declarations for input instance initializer
77
static void _OnChar_classic(HWND /*hwnd UNUSED*/, UINT /*cch*/, int /*cRepeat UNUSED*/);
78
static void _OnSysChar_classic(HWND /*hwnd UNUSED*/, UINT /*cch*/, int /*cRepeat UNUSED*/);
79
static void process_message_usual_key_classic(UINT /*vk*/, const MSG* /*pmsg*/);
80
static int get_active_modifiers_classic(void);
81
static int is_experimental_false(void);
82
83
keycode_trans_strategy keycode_trans_strategy_classic = {
84
_OnChar_classic // ptr_on_char
85
, _OnSysChar_classic // ptr_on_sys_char
86
, process_message_usual_key_classic // ptr_process_message_usual_key
87
, get_active_modifiers_classic
88
, is_experimental_false
89
};
90
91
keycode_trans_strategy *keycode_trans_strategy_used = NULL;
92
93
static int is_experimental_true(void)
94
{
95
return 1;
96
}
97
98
static int is_experimental_false(void)
99
{
100
return 0;
101
}
102
103
/*
104
* Initialize the keycode translation strategy.
105
*/
106
static void keycode_trans_strategy_init(void)
107
{
108
const char *strategy = NULL;
109
110
// set default value as fallback
111
keycode_trans_strategy_used = &keycode_trans_strategy_classic;
112
113
strategy = getenv("VIM_KEYCODE_TRANS_STRATEGY");
114
if (strategy == NULL)
115
{
116
return;
117
}
118
119
if (STRICMP(strategy, "classic") == 0)
120
{
121
keycode_trans_strategy_used = &keycode_trans_strategy_classic;
122
return;
123
}
124
125
if (STRICMP(strategy, "experimental") == 0)
126
{
127
keycode_trans_strategy_used = &keycode_trans_strategy_experimental;
128
return;
129
}
130
131
}
132
133
#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
134
int
135
gui_mch_set_rendering_options(char_u *s)
136
{
137
# ifdef FEAT_DIRECTX
138
char_u *p, *q;
139
140
int dx_enable = 0;
141
int dx_flags = 0;
142
float dx_gamma = 0.0f;
143
float dx_contrast = 0.0f;
144
float dx_level = 0.0f;
145
int dx_geom = 0;
146
int dx_renmode = 0;
147
int dx_taamode = 0;
148
149
// parse string as rendering options.
150
for (p = s; p != NULL && *p != NUL; )
151
{
152
char_u item[256];
153
char_u name[128];
154
char_u value[128];
155
156
copy_option_part(&p, item, sizeof(item), ",");
157
if (p == NULL)
158
break;
159
q = &item[0];
160
copy_option_part(&q, name, sizeof(name), ":");
161
if (q == NULL)
162
return FAIL;
163
copy_option_part(&q, value, sizeof(value), ":");
164
165
if (STRCMP(name, "type") == 0)
166
{
167
if (STRCMP(value, "directx") == 0)
168
dx_enable = 1;
169
else
170
return FAIL;
171
}
172
else if (STRCMP(name, "gamma") == 0)
173
{
174
dx_flags |= 1 << 0;
175
dx_gamma = (float)atof((char *)value);
176
}
177
else if (STRCMP(name, "contrast") == 0)
178
{
179
dx_flags |= 1 << 1;
180
dx_contrast = (float)atof((char *)value);
181
}
182
else if (STRCMP(name, "level") == 0)
183
{
184
dx_flags |= 1 << 2;
185
dx_level = (float)atof((char *)value);
186
}
187
else if (STRCMP(name, "geom") == 0)
188
{
189
dx_flags |= 1 << 3;
190
dx_geom = atoi((char *)value);
191
if (dx_geom < 0 || dx_geom > 2)
192
return FAIL;
193
}
194
else if (STRCMP(name, "renmode") == 0)
195
{
196
dx_flags |= 1 << 4;
197
dx_renmode = atoi((char *)value);
198
if (dx_renmode < 0 || dx_renmode > 6)
199
return FAIL;
200
}
201
else if (STRCMP(name, "taamode") == 0)
202
{
203
dx_flags |= 1 << 5;
204
dx_taamode = atoi((char *)value);
205
if (dx_taamode < 0 || dx_taamode > 3)
206
return FAIL;
207
}
208
else if (STRCMP(name, "scrlines") == 0)
209
{
210
// Deprecated. Simply ignore it.
211
}
212
else
213
return FAIL;
214
}
215
216
if (!gui.in_use)
217
return OK; // only checking the syntax of the value
218
219
// Enable DirectX/DirectWrite
220
if (dx_enable)
221
{
222
if (!directx_enabled())
223
return FAIL;
224
DWriteContext_SetRenderingParams(s_dwc, NULL);
225
if (dx_flags)
226
{
227
DWriteRenderingParams param;
228
DWriteContext_GetRenderingParams(s_dwc, &param);
229
if (dx_flags & (1 << 0))
230
param.gamma = dx_gamma;
231
if (dx_flags & (1 << 1))
232
param.enhancedContrast = dx_contrast;
233
if (dx_flags & (1 << 2))
234
param.clearTypeLevel = dx_level;
235
if (dx_flags & (1 << 3))
236
param.pixelGeometry = dx_geom;
237
if (dx_flags & (1 << 4))
238
param.renderingMode = dx_renmode;
239
if (dx_flags & (1 << 5))
240
param.textAntialiasMode = dx_taamode;
241
DWriteContext_SetRenderingParams(s_dwc, &param);
242
}
243
}
244
s_directx_enabled = dx_enable;
245
246
return OK;
247
# else
248
return FAIL;
249
# endif
250
}
251
#endif
252
253
/*
254
* These are new in Windows ME/XP, only defined in recent compilers.
255
*/
256
#ifndef HANDLE_WM_XBUTTONUP
257
# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
258
((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
259
#endif
260
#ifndef HANDLE_WM_XBUTTONDOWN
261
# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
262
((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
263
#endif
264
#ifndef HANDLE_WM_XBUTTONDBLCLK
265
# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
266
((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
267
#endif
268
269
270
#include "version.h" // used by dialog box routine for default title
271
#ifdef DEBUG
272
# include <tchar.h>
273
#endif
274
275
// cproto fails on missing include files
276
#ifndef PROTO
277
# include <shellapi.h>
278
# include <commctrl.h>
279
# include <windowsx.h>
280
#endif // PROTO
281
282
#ifdef FEAT_MENU
283
# define MENUHINTS // show menu hints in command line
284
#endif
285
286
// Some parameters for dialog boxes. All in pixels.
287
#define DLG_PADDING_X 10
288
#define DLG_PADDING_Y 10
289
#define DLG_VERT_PADDING_X 4 // For vertical buttons
290
#define DLG_VERT_PADDING_Y 4
291
#define DLG_ICON_WIDTH 34
292
#define DLG_ICON_HEIGHT 34
293
#define DLG_MIN_WIDTH 150
294
#define DLG_FONT_NAME "MS Shell Dlg"
295
#define DLG_FONT_POINT_SIZE 8
296
#define DLG_MIN_MAX_WIDTH 400
297
#define DLG_MIN_MAX_HEIGHT 400
298
299
#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
300
301
#ifndef WM_DPICHANGED
302
# define WM_DPICHANGED 0x02E0
303
#endif
304
305
#ifndef WM_GETDPISCALEDSIZE
306
# define WM_GETDPISCALEDSIZE 0x02E4
307
#endif
308
309
#ifndef WM_MOUSEHWHEEL
310
# define WM_MOUSEHWHEEL 0x020E
311
#endif
312
313
#ifndef SPI_GETWHEELSCROLLCHARS
314
# define SPI_GETWHEELSCROLLCHARS 0x006C
315
#endif
316
317
#ifndef SPI_SETWHEELSCROLLCHARS
318
# define SPI_SETWHEELSCROLLCHARS 0x006D
319
#endif
320
321
#ifdef PROTO
322
/*
323
* Define a few things for generating prototypes. This is just to avoid
324
* syntax errors, the defines do not need to be correct.
325
*/
326
# define APIENTRY
327
# define CALLBACK
328
# define CONST
329
# define FAR
330
# define NEAR
331
# define WINAPI
332
# undef _cdecl
333
# define _cdecl
334
typedef int BOOL;
335
typedef int BYTE;
336
typedef int DWORD;
337
typedef int WCHAR;
338
typedef int ENUMLOGFONT;
339
typedef int FINDREPLACE;
340
typedef int HANDLE;
341
typedef int HBITMAP;
342
typedef int HBRUSH;
343
typedef int HDROP;
344
typedef int INT;
345
typedef int LOGFONTW[];
346
typedef int LPARAM;
347
typedef int LPCREATESTRUCT;
348
typedef int LPCSTR;
349
typedef int LPCTSTR;
350
typedef int LPRECT;
351
typedef int LPSTR;
352
typedef int LPWINDOWPOS;
353
typedef int LPWORD;
354
typedef int LRESULT;
355
typedef int HRESULT;
356
# undef MSG
357
typedef int MSG;
358
typedef int NEWTEXTMETRIC;
359
typedef int NMHDR;
360
typedef int OSVERSIONINFO;
361
typedef int PWORD;
362
typedef int RECT;
363
typedef int SIZE;
364
typedef int UINT;
365
typedef int WORD;
366
typedef int WPARAM;
367
typedef int POINT;
368
typedef void *HINSTANCE;
369
typedef void *HMENU;
370
typedef void *HWND;
371
typedef void *HDC;
372
typedef void VOID;
373
typedef int LPNMHDR;
374
typedef int LONG;
375
typedef int WNDPROC;
376
typedef int UINT_PTR;
377
typedef int COLORREF;
378
typedef int HCURSOR;
379
#endif
380
381
static void _OnPaint(HWND hwnd);
382
static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
383
static void clear_rect(RECT *rcp);
384
385
static WORD s_dlgfntheight; // height of the dialog font
386
static WORD s_dlgfntwidth; // width of the dialog font
387
388
#ifdef FEAT_MENU
389
static HMENU s_menuBar = NULL;
390
#endif
391
#ifdef FEAT_TEAROFF
392
static void rebuild_tearoff(vimmenu_T *menu);
393
static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
394
#endif
395
396
// Flag that is set while processing a message that must not be interrupted by
397
// processing another message.
398
static int s_busy_processing = FALSE;
399
400
static int destroying = FALSE; // call DestroyWindow() ourselves
401
402
#ifdef MSWIN_FIND_REPLACE
403
static UINT s_findrep_msg = 0;
404
static FINDREPLACEW s_findrep_struct;
405
static HWND s_findrep_hwnd = NULL;
406
static int s_findrep_is_find; // TRUE for find dialog, FALSE
407
// for find/replace dialog
408
#endif
409
410
HWND s_hwnd = NULL;
411
static HDC s_hdc = NULL;
412
static HBRUSH s_brush = NULL;
413
414
#ifdef FEAT_TOOLBAR
415
static HWND s_toolbarhwnd = NULL;
416
static WNDPROC s_toolbar_wndproc = NULL;
417
#endif
418
419
#ifdef FEAT_GUI_TABLINE
420
static HWND s_tabhwnd = NULL;
421
static WNDPROC s_tabline_wndproc = NULL;
422
static int showing_tabline = 0;
423
#endif
424
425
static WPARAM s_wParam = 0;
426
static LPARAM s_lParam = 0;
427
428
static HWND s_textArea = NULL;
429
static UINT s_uMsg = 0;
430
431
static char_u *s_textfield; // Used by dialogs to pass back strings
432
433
static int s_need_activate = FALSE;
434
435
// This variable is set when waiting for an event, which is the only moment
436
// scrollbar dragging can be done directly. It's not allowed while commands
437
// are executed, because it may move the cursor and that may cause unexpected
438
// problems (e.g., while ":s" is working).
439
static int allow_scrollbar = FALSE;
440
441
#ifndef _DPI_AWARENESS_CONTEXTS_
442
typedef HANDLE DPI_AWARENESS_CONTEXT;
443
444
typedef enum DPI_AWARENESS {
445
DPI_AWARENESS_INVALID = -1,
446
DPI_AWARENESS_UNAWARE = 0,
447
DPI_AWARENESS_SYSTEM_AWARE = 1,
448
DPI_AWARENESS_PER_MONITOR_AWARE = 2
449
} DPI_AWARENESS;
450
451
# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
452
# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
453
# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
454
# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
455
# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
456
#endif
457
458
#define DEFAULT_DPI 96
459
static int s_dpi = DEFAULT_DPI;
460
static BOOL s_in_dpichanged = FALSE;
461
static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
462
static RECT s_suggested_rect;
463
464
static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
465
static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
466
static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
467
//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
468
static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
469
static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
470
471
static int WINAPI
472
stubGetSystemMetricsForDpi(int nIndex, UINT dpi UNUSED)
473
{
474
return GetSystemMetrics(nIndex);
475
}
476
477
static int
478
adjust_fontsize_by_dpi(int size)
479
{
480
return size * s_dpi / (int)pGetDpiForSystem();
481
}
482
483
static int
484
adjust_by_system_dpi(int size)
485
{
486
return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
487
}
488
489
#if defined(FEAT_DIRECTX)
490
static int
491
directx_enabled(void)
492
{
493
if (s_dwc != NULL)
494
return 1;
495
else if (s_directx_load_attempted)
496
return 0;
497
// load DirectX
498
DWrite_Init();
499
s_directx_load_attempted = 1;
500
s_dwc = DWriteContext_Open();
501
directx_binddc();
502
return s_dwc != NULL ? 1 : 0;
503
}
504
505
static void
506
directx_binddc(void)
507
{
508
if (s_textArea == NULL)
509
return;
510
511
RECT rect;
512
GetClientRect(s_textArea, &rect);
513
DWriteContext_BindDC(s_dwc, s_hdc, &rect);
514
}
515
#endif
516
517
extern int current_font_height; // this is in os_mswin.c
518
519
static struct
520
{
521
UINT key_sym;
522
char_u vim_code0;
523
char_u vim_code1;
524
} special_keys[] =
525
{
526
{VK_UP, 'k', 'u'},
527
{VK_DOWN, 'k', 'd'},
528
{VK_LEFT, 'k', 'l'},
529
{VK_RIGHT, 'k', 'r'},
530
531
{VK_F1, 'k', '1'},
532
{VK_F2, 'k', '2'},
533
{VK_F3, 'k', '3'},
534
{VK_F4, 'k', '4'},
535
{VK_F5, 'k', '5'},
536
{VK_F6, 'k', '6'},
537
{VK_F7, 'k', '7'},
538
{VK_F8, 'k', '8'},
539
{VK_F9, 'k', '9'},
540
{VK_F10, 'k', ';'},
541
542
{VK_F11, 'F', '1'},
543
{VK_F12, 'F', '2'},
544
{VK_F13, 'F', '3'},
545
{VK_F14, 'F', '4'},
546
{VK_F15, 'F', '5'},
547
{VK_F16, 'F', '6'},
548
{VK_F17, 'F', '7'},
549
{VK_F18, 'F', '8'},
550
{VK_F19, 'F', '9'},
551
{VK_F20, 'F', 'A'},
552
553
{VK_F21, 'F', 'B'},
554
#ifdef FEAT_NETBEANS_INTG
555
{VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
556
#endif
557
{VK_F22, 'F', 'C'},
558
{VK_F23, 'F', 'D'},
559
{VK_F24, 'F', 'E'}, // winuser.h defines up to F24
560
561
{VK_HELP, '%', '1'},
562
{VK_BACK, 'k', 'b'},
563
{VK_INSERT, 'k', 'I'},
564
{VK_DELETE, 'k', 'D'},
565
{VK_HOME, 'k', 'h'},
566
{VK_END, '@', '7'},
567
{VK_PRIOR, 'k', 'P'},
568
{VK_NEXT, 'k', 'N'},
569
{VK_PRINT, '%', '9'},
570
{VK_ADD, 'K', '6'},
571
{VK_SUBTRACT, 'K', '7'},
572
{VK_DIVIDE, 'K', '8'},
573
{VK_MULTIPLY, 'K', '9'},
574
{VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
575
{VK_DECIMAL, 'K', 'B'},
576
577
{VK_NUMPAD0, 'K', 'C'},
578
{VK_NUMPAD1, 'K', 'D'},
579
{VK_NUMPAD2, 'K', 'E'},
580
{VK_NUMPAD3, 'K', 'F'},
581
{VK_NUMPAD4, 'K', 'G'},
582
{VK_NUMPAD5, 'K', 'H'},
583
{VK_NUMPAD6, 'K', 'I'},
584
{VK_NUMPAD7, 'K', 'J'},
585
{VK_NUMPAD8, 'K', 'K'},
586
{VK_NUMPAD9, 'K', 'L'},
587
588
// Keys that we want to be able to use any modifier with:
589
{VK_SPACE, ' ', NUL},
590
{VK_TAB, TAB, NUL},
591
{VK_ESCAPE, ESC, NUL},
592
{NL, NL, NUL},
593
{CAR, CAR, NUL},
594
595
// End of list marker:
596
{0, 0, 0}
597
};
598
599
// Local variables
600
static int s_button_pending = -1;
601
602
// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
603
// so don't reset s_button_pending.
604
static int s_getting_focus = FALSE;
605
606
static int s_x_pending;
607
static int s_y_pending;
608
static UINT s_kFlags_pending;
609
static UINT_PTR s_wait_timer = 0; // Timer for get char from user
610
static int s_timed_out = FALSE;
611
static int dead_key = DEAD_KEY_OFF;
612
static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
613
// else a high surrogate
614
615
#ifdef FEAT_BEVAL_GUI
616
// balloon-eval WM_NOTIFY_HANDLER
617
static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
618
static void track_user_activity(UINT uMsg);
619
#endif
620
621
/*
622
* For control IME.
623
*
624
* These LOGFONTW used for IME.
625
*/
626
#ifdef FEAT_MBYTE_IME
627
// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
628
static LOGFONTW norm_logfont;
629
// holds LOGFONTW for 'guifont' always.
630
static LOGFONTW sub_logfont;
631
#endif
632
633
#ifdef FEAT_MBYTE_IME
634
static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
635
#endif
636
637
#if defined(FEAT_BROWSE)
638
static char_u *convert_filter(char_u *s);
639
#endif
640
641
#ifdef DEBUG_PRINT_ERROR
642
/*
643
* Print out the last Windows error message
644
*/
645
static void
646
print_windows_error(void)
647
{
648
LPVOID lpMsgBuf;
649
650
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
651
NULL, GetLastError(),
652
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
653
(LPTSTR) &lpMsgBuf, 0, NULL);
654
TRACE1("Error: %s\n", lpMsgBuf);
655
LocalFree(lpMsgBuf);
656
}
657
#endif
658
659
/*
660
* Cursor blink functions.
661
*
662
* This is a simple state machine:
663
* BLINK_NONE not blinking at all
664
* BLINK_OFF blinking, cursor is not shown
665
* BLINK_ON blinking, cursor is shown
666
*/
667
668
#define BLINK_NONE 0
669
#define BLINK_OFF 1
670
#define BLINK_ON 2
671
672
static int blink_state = BLINK_NONE;
673
static long_u blink_waittime = 700;
674
static long_u blink_ontime = 400;
675
static long_u blink_offtime = 250;
676
static UINT_PTR blink_timer = 0;
677
678
int
679
gui_mch_is_blinking(void)
680
{
681
return blink_state != BLINK_NONE;
682
}
683
684
int
685
gui_mch_is_blink_off(void)
686
{
687
return blink_state == BLINK_OFF;
688
}
689
690
void
691
gui_mch_set_blinking(long wait, long on, long off)
692
{
693
blink_waittime = wait;
694
blink_ontime = on;
695
blink_offtime = off;
696
}
697
698
static VOID CALLBACK
699
_OnBlinkTimer(
700
HWND hwnd,
701
UINT uMsg UNUSED,
702
UINT_PTR idEvent,
703
DWORD dwTime UNUSED)
704
{
705
MSG msg;
706
707
/*
708
TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
709
*/
710
711
KillTimer(NULL, idEvent);
712
713
// Eat spurious WM_TIMER messages
714
while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
715
;
716
717
if (blink_state == BLINK_ON)
718
{
719
gui_undraw_cursor();
720
blink_state = BLINK_OFF;
721
blink_timer = SetTimer(NULL, 0, (UINT)blink_offtime, _OnBlinkTimer);
722
}
723
else
724
{
725
gui_update_cursor(TRUE, FALSE);
726
blink_state = BLINK_ON;
727
blink_timer = SetTimer(NULL, 0, (UINT)blink_ontime, _OnBlinkTimer);
728
}
729
gui_mch_flush();
730
}
731
732
static void
733
gui_mswin_rm_blink_timer(void)
734
{
735
MSG msg;
736
737
if (blink_timer == 0)
738
return;
739
740
KillTimer(NULL, blink_timer);
741
// Eat spurious WM_TIMER messages
742
while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
743
;
744
blink_timer = 0;
745
}
746
747
/*
748
* Stop the cursor blinking. Show the cursor if it wasn't shown.
749
*/
750
void
751
gui_mch_stop_blink(int may_call_gui_update_cursor)
752
{
753
gui_mswin_rm_blink_timer();
754
if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
755
{
756
gui_update_cursor(TRUE, FALSE);
757
gui_mch_flush();
758
}
759
blink_state = BLINK_NONE;
760
}
761
762
/*
763
* Start the cursor blinking. If it was already blinking, this restarts the
764
* waiting time and shows the cursor.
765
*/
766
void
767
gui_mch_start_blink(void)
768
{
769
gui_mswin_rm_blink_timer();
770
771
// Only switch blinking on if none of the times is zero
772
if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
773
{
774
blink_timer = SetTimer(NULL, 0, (UINT)blink_waittime, _OnBlinkTimer);
775
blink_state = BLINK_ON;
776
gui_update_cursor(TRUE, FALSE);
777
gui_mch_flush();
778
}
779
}
780
781
/*
782
* Call-back routines.
783
*/
784
785
static VOID CALLBACK
786
_OnTimer(
787
HWND hwnd,
788
UINT uMsg UNUSED,
789
UINT_PTR idEvent,
790
DWORD dwTime UNUSED)
791
{
792
MSG msg;
793
794
/*
795
TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
796
*/
797
KillTimer(NULL, idEvent);
798
s_timed_out = TRUE;
799
800
// Eat spurious WM_TIMER messages
801
while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
802
;
803
if (idEvent == s_wait_timer)
804
s_wait_timer = 0;
805
}
806
807
static void
808
_OnDeadChar(
809
HWND hwnd UNUSED,
810
UINT ch UNUSED,
811
int cRepeat UNUSED)
812
{
813
dead_key = DEAD_KEY_SET_DEFAULT;
814
}
815
816
/*
817
* Convert Unicode character "ch" to bytes in "string[slen]".
818
* When "had_alt" is TRUE the ALT key was included in "ch".
819
* Return the length.
820
* Because the Windows API uses UTF-16, we have to deal with surrogate
821
* pairs; this is where we choose to deal with them: if "ch" is a high
822
* surrogate, it will be stored, and the length returned will be zero; the next
823
* char_to_string call will then include the high surrogate, decoding the pair
824
* of UTF-16 code units to a single Unicode code point, presuming it is the
825
* matching low surrogate.
826
*/
827
static int
828
char_to_string(int ch, char_u *string, int slen, int had_alt)
829
{
830
int len;
831
int i;
832
WCHAR wstring[2];
833
char_u *ws = NULL;
834
835
if (surrogate_pending_ch != 0)
836
{
837
// We don't guarantee ch is a low surrogate to match the high surrogate
838
// we already have; it should be, but if it isn't, tough luck.
839
wstring[0] = surrogate_pending_ch;
840
wstring[1] = ch;
841
surrogate_pending_ch = 0;
842
len = 2;
843
}
844
else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
845
{
846
// We don't have the entire code point yet, only the first UTF-16 code
847
// unit; so just remember it and use it in the next call.
848
surrogate_pending_ch = ch;
849
return 0;
850
}
851
else
852
{
853
wstring[0] = ch;
854
len = 1;
855
}
856
857
// "ch" is a UTF-16 character. Convert it to a string of bytes. When
858
// "enc_codepage" is non-zero use the standard Win32 function,
859
// otherwise use our own conversion function (e.g., for UTF-8).
860
if (enc_codepage > 0)
861
{
862
len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
863
(LPSTR)string, slen, 0, NULL);
864
// If we had included the ALT key into the character but now the
865
// upper bit is no longer set, that probably means the conversion
866
// failed. Convert the original character and set the upper bit
867
// afterwards.
868
if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
869
{
870
wstring[0] = ch & 0x7f;
871
len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
872
(LPSTR)string, slen, 0, NULL);
873
if (len == 1) // safety check
874
string[0] |= 0x80;
875
}
876
}
877
else
878
{
879
ws = utf16_to_enc(wstring, &len);
880
if (ws == NULL)
881
len = 0;
882
else
883
{
884
if (len > slen) // just in case
885
len = slen;
886
mch_memmove(string, ws, len);
887
vim_free(ws);
888
}
889
}
890
891
if (len == 0)
892
{
893
string[0] = ch;
894
len = 1;
895
}
896
897
for (i = 0; i < len; ++i)
898
if (string[i] == CSI && len <= slen - 2)
899
{
900
// Insert CSI as K_CSI.
901
mch_memmove(string + i + 3, string + i + 1, len - i - 1);
902
string[++i] = KS_EXTRA;
903
string[++i] = (int)KE_CSI;
904
len += 2;
905
}
906
907
return len;
908
}
909
910
/*
911
* Experimental implementation, introduced in v8.2.4807
912
* "processing key event in Win32 GUI is not ideal"
913
*
914
* TODO: since introduction, this experimental function started
915
* to be used as well outside of original key press/processing
916
* area, and usages not via "get_active_modifiers_via_ptr" should
917
* be watched.
918
*/
919
static int
920
get_active_modifiers_experimental(void)
921
{
922
int modifiers = 0;
923
924
if (GetKeyState(VK_CONTROL) & 0x8000)
925
modifiers |= MOD_MASK_CTRL;
926
if (GetKeyState(VK_SHIFT) & 0x8000)
927
modifiers |= MOD_MASK_SHIFT;
928
// Windows handles Ctrl + Alt as AltGr and vice-versa. We can distinguish
929
// the two cases by checking whether the left or the right Alt key is
930
// pressed.
931
if (GetKeyState(VK_LMENU) & 0x8000)
932
modifiers |= MOD_MASK_ALT;
933
if ((modifiers & MOD_MASK_CTRL) && (GetKeyState(VK_RMENU) & 0x8000))
934
modifiers &= ~MOD_MASK_CTRL;
935
// Add RightALT only if it is hold alone (without Ctrl), because if AltGr
936
// is pressed, Windows claims that Ctrl is hold as well. That way we can
937
// recognize Right-ALT alone and be sure that not AltGr is hold.
938
if (!(GetKeyState(VK_CONTROL) & 0x8000)
939
&& (GetKeyState(VK_RMENU) & 0x8000)
940
&& !(GetKeyState(VK_LMENU) & 0x8000)) // seems AltGr has both set
941
modifiers |= MOD_MASK_ALT;
942
943
return modifiers;
944
}
945
946
/*
947
* "Classic" implementation, existing prior to v8.2.4807
948
*/
949
static int
950
get_active_modifiers_classic(void)
951
{
952
int modifiers = 0;
953
954
if (GetKeyState(VK_SHIFT) & 0x8000)
955
modifiers |= MOD_MASK_SHIFT;
956
/*
957
* Don't use caps-lock as shift, because these are special keys
958
* being considered here, and we only want letters to get
959
* shifted -- webb
960
*/
961
/*
962
if (GetKeyState(VK_CAPITAL) & 0x0001)
963
modifiers ^= MOD_MASK_SHIFT;
964
*/
965
if (GetKeyState(VK_CONTROL) & 0x8000)
966
modifiers |= MOD_MASK_CTRL;
967
if (GetKeyState(VK_MENU) & 0x8000)
968
modifiers |= MOD_MASK_ALT;
969
970
return modifiers;
971
}
972
973
static int
974
get_active_modifiers(void)
975
{
976
return get_active_modifiers_experimental();
977
}
978
979
static int
980
get_active_modifiers_via_ptr(void)
981
{
982
// marshal to corresponding implementation
983
return keycode_trans_strategy_used->ptr_get_active_modifiers();
984
}
985
986
/*
987
* Key hit, add it to the input buffer.
988
*/
989
static void
990
_OnChar(
991
HWND hwnd UNUSED,
992
UINT cch,
993
int cRepeat UNUSED)
994
{
995
// marshal to corresponding implementation
996
keycode_trans_strategy_used->ptr_on_char(hwnd, cch, cRepeat);
997
}
998
999
/*
1000
* Experimental implementation, introduced in v8.2.4807
1001
* "processing key event in Win32 GUI is not ideal"
1002
*/
1003
static void
1004
_OnChar_experimental(
1005
HWND hwnd UNUSED,
1006
UINT cch,
1007
int cRepeat UNUSED)
1008
{
1009
char_u string[40];
1010
int len = 0;
1011
int modifiers;
1012
int ch = cch; // special keys are negative
1013
1014
if (dead_key == DEAD_KEY_SKIP_ON_CHAR)
1015
return;
1016
1017
// keep DEAD_KEY_TRANSIENT_IN_ON_CHAR value for later handling in
1018
// process_message()
1019
if (dead_key != DEAD_KEY_TRANSIENT_IN_ON_CHAR)
1020
dead_key = DEAD_KEY_OFF;
1021
1022
modifiers = get_active_modifiers_experimental();
1023
1024
ch = simplify_key(ch, &modifiers);
1025
1026
// Some keys need adjustment when the Ctrl modifier is used.
1027
++no_reduce_keys;
1028
ch = may_adjust_key_for_ctrl(modifiers, ch);
1029
--no_reduce_keys;
1030
1031
// remove the SHIFT modifier for keys where it's already included, e.g.,
1032
// '(' and '*'
1033
modifiers = may_remove_shift_modifier(modifiers, ch);
1034
1035
// Unify modifiers somewhat. No longer use ALT to set the 8th bit.
1036
ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
1037
if (ch == CSI)
1038
ch = K_CSI;
1039
1040
if (modifiers)
1041
{
1042
string[0] = CSI;
1043
string[1] = KS_MODIFIER;
1044
string[2] = modifiers;
1045
add_to_input_buf(string, 3);
1046
}
1047
1048
len = char_to_string(ch, string, 40, FALSE);
1049
if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
1050
{
1051
trash_input_buf();
1052
got_int = TRUE;
1053
}
1054
1055
add_to_input_buf(string, len);
1056
}
1057
1058
/*
1059
* "Classic" implementation, existing prior to v8.2.4807
1060
*/
1061
static void
1062
_OnChar_classic(
1063
HWND hwnd UNUSED,
1064
UINT ch,
1065
int cRepeat UNUSED)
1066
{
1067
char_u string[40];
1068
int len = 0;
1069
1070
dead_key = 0;
1071
1072
len = char_to_string(ch, string, 40, FALSE);
1073
if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
1074
{
1075
trash_input_buf();
1076
got_int = TRUE;
1077
}
1078
1079
add_to_input_buf(string, len);
1080
}
1081
1082
/*
1083
* Alt-Key hit, add it to the input buffer.
1084
*/
1085
static void
1086
_OnSysChar(
1087
HWND hwnd UNUSED,
1088
UINT cch,
1089
int cRepeat UNUSED)
1090
{
1091
// marshal to corresponding implementation
1092
keycode_trans_strategy_used->ptr_on_sys_char(hwnd, cch, cRepeat);
1093
}
1094
1095
/*
1096
* Experimental implementation, introduced in v8.2.4807
1097
* "processing key event in Win32 GUI is not ideal"
1098
*/
1099
static void
1100
_OnSysChar_experimental(
1101
HWND hwnd UNUSED,
1102
UINT cch,
1103
int cRepeat UNUSED)
1104
{
1105
char_u string[40]; // Enough for multibyte character
1106
int len;
1107
int modifiers;
1108
int ch = cch; // special keys are negative
1109
1110
dead_key = DEAD_KEY_OFF;
1111
1112
// OK, we have a character key (given by ch) which was entered with the
1113
// ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
1114
// that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
1115
// CAPSLOCK is pressed) at this point.
1116
modifiers = get_active_modifiers_experimental();
1117
ch = simplify_key(ch, &modifiers);
1118
// remove the SHIFT modifier for keys where it's already included, e.g.,
1119
// '(' and '*'
1120
modifiers = may_remove_shift_modifier(modifiers, ch);
1121
1122
// Unify modifiers somewhat. No longer use ALT to set the 8th bit.
1123
ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
1124
if (ch == CSI)
1125
ch = K_CSI;
1126
1127
len = 0;
1128
if (modifiers)
1129
{
1130
string[len++] = CSI;
1131
string[len++] = KS_MODIFIER;
1132
string[len++] = modifiers;
1133
}
1134
1135
if (IS_SPECIAL((int)ch))
1136
{
1137
string[len++] = CSI;
1138
string[len++] = K_SECOND((int)ch);
1139
string[len++] = K_THIRD((int)ch);
1140
}
1141
else
1142
{
1143
// Although the documentation isn't clear about it, we assume "ch" is
1144
// a Unicode character.
1145
len += char_to_string(ch, string + len, 40 - len, TRUE);
1146
}
1147
1148
add_to_input_buf(string, len);
1149
}
1150
1151
/*
1152
* "Classic" implementation, existing prior to v8.2.4807
1153
*/
1154
static void
1155
_OnSysChar_classic(
1156
HWND hwnd UNUSED,
1157
UINT cch,
1158
int cRepeat UNUSED)
1159
{
1160
char_u string[40]; // Enough for multibyte character
1161
int len;
1162
int modifiers;
1163
int ch = cch; // special keys are negative
1164
1165
dead_key = 0;
1166
1167
// TRACE("OnSysChar(%d, %c)\n", ch, ch);
1168
1169
// OK, we have a character key (given by ch) which was entered with the
1170
// ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
1171
// that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
1172
// CAPSLOCK is pressed) at this point.
1173
modifiers = MOD_MASK_ALT;
1174
if (GetKeyState(VK_SHIFT) & 0x8000)
1175
modifiers |= MOD_MASK_SHIFT;
1176
if (GetKeyState(VK_CONTROL) & 0x8000)
1177
modifiers |= MOD_MASK_CTRL;
1178
1179
ch = simplify_key(ch, &modifiers);
1180
// remove the SHIFT modifier for keys where it's already included, e.g.,
1181
// '(' and '*'
1182
modifiers = may_remove_shift_modifier(modifiers, ch);
1183
1184
// Unify modifiers somewhat. No longer use ALT to set the 8th bit.
1185
ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
1186
if (ch == CSI)
1187
ch = K_CSI;
1188
1189
len = 0;
1190
if (modifiers)
1191
{
1192
string[len++] = CSI;
1193
string[len++] = KS_MODIFIER;
1194
string[len++] = modifiers;
1195
}
1196
1197
if (IS_SPECIAL((int)ch))
1198
{
1199
string[len++] = CSI;
1200
string[len++] = K_SECOND((int)ch);
1201
string[len++] = K_THIRD((int)ch);
1202
}
1203
else
1204
{
1205
// Although the documentation isn't clear about it, we assume "ch" is
1206
// a Unicode character.
1207
len += char_to_string(ch, string + len, 40 - len, TRUE);
1208
}
1209
1210
add_to_input_buf(string, len);
1211
}
1212
1213
static void
1214
_OnMouseEvent(
1215
int button,
1216
int x,
1217
int y,
1218
int repeated_click,
1219
UINT keyFlags)
1220
{
1221
int vim_modifiers = 0x0;
1222
1223
s_getting_focus = FALSE;
1224
1225
if (keyFlags & MK_SHIFT)
1226
vim_modifiers |= MOUSE_SHIFT;
1227
if (keyFlags & MK_CONTROL)
1228
vim_modifiers |= MOUSE_CTRL;
1229
if (GetKeyState(VK_LMENU) & 0x8000)
1230
vim_modifiers |= MOUSE_ALT;
1231
1232
gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
1233
}
1234
1235
static void
1236
_OnMouseButtonDown(
1237
HWND hwnd UNUSED,
1238
BOOL fDoubleClick UNUSED,
1239
int x,
1240
int y,
1241
UINT keyFlags)
1242
{
1243
static LONG s_prevTime = 0;
1244
1245
LONG currentTime = GetMessageTime();
1246
int button = -1;
1247
int repeated_click;
1248
1249
// Give main window the focus: this is so the cursor isn't hollow.
1250
(void)SetFocus(s_hwnd);
1251
1252
if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
1253
button = MOUSE_LEFT;
1254
else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
1255
button = MOUSE_MIDDLE;
1256
else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
1257
button = MOUSE_RIGHT;
1258
else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
1259
{
1260
button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
1261
}
1262
else if (s_uMsg == WM_CAPTURECHANGED)
1263
{
1264
// on W95/NT4, somehow you get in here with an odd Msg
1265
// if you press one button while holding down the other..
1266
if (s_button_pending == MOUSE_LEFT)
1267
button = MOUSE_RIGHT;
1268
else
1269
button = MOUSE_LEFT;
1270
}
1271
1272
if (button < 0)
1273
return;
1274
1275
repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
1276
1277
/*
1278
* Holding down the left and right buttons simulates pushing the middle
1279
* button.
1280
*/
1281
if (repeated_click
1282
&& ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
1283
|| (button == MOUSE_RIGHT
1284
&& s_button_pending == MOUSE_LEFT)))
1285
{
1286
/*
1287
* Hmm, gui.c will ignore more than one button down at a time, so
1288
* pretend we let go of it first.
1289
*/
1290
gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1291
button = MOUSE_MIDDLE;
1292
repeated_click = FALSE;
1293
s_button_pending = -1;
1294
_OnMouseEvent(button, x, y, repeated_click, keyFlags);
1295
}
1296
else if ((repeated_click)
1297
|| (mouse_model_popup() && (button == MOUSE_RIGHT)))
1298
{
1299
if (s_button_pending > -1)
1300
{
1301
_OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1302
s_button_pending = -1;
1303
}
1304
// TRACE("Button down at x %d, y %d\n", x, y);
1305
_OnMouseEvent(button, x, y, repeated_click, keyFlags);
1306
}
1307
else
1308
{
1309
/*
1310
* If this is the first press (i.e. not a multiple click) don't
1311
* action immediately, but store and wait for:
1312
* i) button-up
1313
* ii) mouse move
1314
* iii) another button press
1315
* before using it.
1316
* This enables us to make left+right simulate middle button,
1317
* without left or right being actioned first. The side-effect is
1318
* that if you click and hold the mouse without dragging, the
1319
* cursor doesn't move until you release the button. In practice
1320
* this is hardly a problem.
1321
*/
1322
s_button_pending = button;
1323
s_x_pending = x;
1324
s_y_pending = y;
1325
s_kFlags_pending = keyFlags;
1326
}
1327
1328
s_prevTime = currentTime;
1329
}
1330
1331
static void
1332
_OnMouseMoveOrRelease(
1333
HWND hwnd UNUSED,
1334
int x,
1335
int y,
1336
UINT keyFlags)
1337
{
1338
int button;
1339
1340
s_getting_focus = FALSE;
1341
if (s_button_pending > -1)
1342
{
1343
// Delayed action for mouse down event
1344
_OnMouseEvent(s_button_pending, s_x_pending,
1345
s_y_pending, FALSE, s_kFlags_pending);
1346
s_button_pending = -1;
1347
}
1348
if (s_uMsg == WM_MOUSEMOVE)
1349
{
1350
/*
1351
* It's only a MOUSE_DRAG if one or more mouse buttons are being held
1352
* down.
1353
*/
1354
if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1355
| MK_XBUTTON1 | MK_XBUTTON2)))
1356
{
1357
gui_mouse_moved(x, y);
1358
return;
1359
}
1360
1361
/*
1362
* While button is down, keep grabbing mouse move events when
1363
* the mouse goes outside the window
1364
*/
1365
SetCapture(s_textArea);
1366
button = MOUSE_DRAG;
1367
// TRACE(" move at x %d, y %d\n", x, y);
1368
}
1369
else
1370
{
1371
ReleaseCapture();
1372
button = MOUSE_RELEASE;
1373
// TRACE(" up at x %d, y %d\n", x, y);
1374
}
1375
1376
_OnMouseEvent(button, x, y, FALSE, keyFlags);
1377
}
1378
1379
static void
1380
_OnSizeTextArea(
1381
HWND hwnd UNUSED,
1382
UINT state UNUSED,
1383
int cx UNUSED,
1384
int cy UNUSED)
1385
{
1386
#if defined(FEAT_DIRECTX)
1387
if (IS_ENABLE_DIRECTX())
1388
directx_binddc();
1389
#endif
1390
}
1391
1392
#ifdef FEAT_MENU
1393
/*
1394
* Find the vimmenu_T with the given id
1395
*/
1396
static vimmenu_T *
1397
gui_mswin_find_menu(
1398
vimmenu_T *pMenu,
1399
int id)
1400
{
1401
vimmenu_T *pChildMenu;
1402
1403
while (pMenu)
1404
{
1405
if (pMenu->id == (UINT)id)
1406
break;
1407
if (pMenu->children != NULL)
1408
{
1409
pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1410
if (pChildMenu)
1411
{
1412
pMenu = pChildMenu;
1413
break;
1414
}
1415
}
1416
pMenu = pMenu->next;
1417
}
1418
return pMenu;
1419
}
1420
1421
static void
1422
_OnMenu(
1423
HWND hwnd UNUSED,
1424
int id,
1425
HWND hwndCtl UNUSED,
1426
UINT codeNotify UNUSED)
1427
{
1428
vimmenu_T *pMenu;
1429
1430
pMenu = gui_mswin_find_menu(root_menu, id);
1431
if (pMenu)
1432
gui_menu_cb(pMenu);
1433
}
1434
#endif
1435
1436
#ifdef MSWIN_FIND_REPLACE
1437
/*
1438
* Handle a Find/Replace window message.
1439
*/
1440
static void
1441
_OnFindRepl(void)
1442
{
1443
int flags = 0;
1444
int down;
1445
1446
if (s_findrep_struct.Flags & FR_DIALOGTERM)
1447
// Give main window the focus back.
1448
(void)SetFocus(s_hwnd);
1449
1450
if (s_findrep_struct.Flags & FR_FINDNEXT)
1451
{
1452
flags = FRD_FINDNEXT;
1453
1454
// Give main window the focus back: this is so the cursor isn't
1455
// hollow.
1456
(void)SetFocus(s_hwnd);
1457
}
1458
else if (s_findrep_struct.Flags & FR_REPLACE)
1459
{
1460
flags = FRD_REPLACE;
1461
1462
// Give main window the focus back: this is so the cursor isn't
1463
// hollow.
1464
(void)SetFocus(s_hwnd);
1465
}
1466
else if (s_findrep_struct.Flags & FR_REPLACEALL)
1467
{
1468
flags = FRD_REPLACEALL;
1469
}
1470
1471
if (flags == 0)
1472
return;
1473
1474
char_u *p, *q;
1475
1476
// Call the generic GUI function to do the actual work.
1477
if (s_findrep_struct.Flags & FR_WHOLEWORD)
1478
flags |= FRD_WHOLE_WORD;
1479
if (s_findrep_struct.Flags & FR_MATCHCASE)
1480
flags |= FRD_MATCH_CASE;
1481
down = (s_findrep_struct.Flags & FR_DOWN) != 0;
1482
p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1483
q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1484
if (p != NULL && q != NULL)
1485
gui_do_findrepl(flags, p, q, down);
1486
vim_free(p);
1487
vim_free(q);
1488
}
1489
#endif
1490
1491
static void
1492
HandleMouseHide(UINT uMsg, LPARAM lParam)
1493
{
1494
static LPARAM last_lParam = 0L;
1495
1496
// We sometimes get a mousemove when the mouse didn't move...
1497
if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1498
{
1499
if (lParam == last_lParam)
1500
return;
1501
last_lParam = lParam;
1502
}
1503
1504
// Handle specially, to centralise coding. We need to be sure we catch all
1505
// possible events which should cause us to restore the cursor (as it is a
1506
// shared resource, we take full responsibility for it).
1507
switch (uMsg)
1508
{
1509
case WM_KEYUP:
1510
case WM_CHAR:
1511
/*
1512
* blank out the pointer if necessary
1513
*/
1514
if (p_mh)
1515
gui_mch_mousehide(TRUE);
1516
break;
1517
1518
case WM_SYSKEYUP: // show the pointer when a system-key is pressed
1519
case WM_SYSCHAR:
1520
case WM_MOUSEMOVE: // show the pointer on any mouse action
1521
case WM_LBUTTONDOWN:
1522
case WM_LBUTTONUP:
1523
case WM_MBUTTONDOWN:
1524
case WM_MBUTTONUP:
1525
case WM_RBUTTONDOWN:
1526
case WM_RBUTTONUP:
1527
case WM_XBUTTONDOWN:
1528
case WM_XBUTTONUP:
1529
case WM_NCMOUSEMOVE:
1530
case WM_NCLBUTTONDOWN:
1531
case WM_NCLBUTTONUP:
1532
case WM_NCMBUTTONDOWN:
1533
case WM_NCMBUTTONUP:
1534
case WM_NCRBUTTONDOWN:
1535
case WM_NCRBUTTONUP:
1536
case WM_KILLFOCUS:
1537
/*
1538
* if the pointer is currently hidden, then we should show it.
1539
*/
1540
gui_mch_mousehide(FALSE);
1541
break;
1542
}
1543
}
1544
1545
static LRESULT CALLBACK
1546
_TextAreaWndProc(
1547
HWND hwnd,
1548
UINT uMsg,
1549
WPARAM wParam,
1550
LPARAM lParam)
1551
{
1552
/*
1553
TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1554
hwnd, uMsg, wParam, lParam);
1555
*/
1556
1557
HandleMouseHide(uMsg, lParam);
1558
1559
s_uMsg = uMsg;
1560
s_wParam = wParam;
1561
s_lParam = lParam;
1562
1563
#ifdef FEAT_BEVAL_GUI
1564
track_user_activity(uMsg);
1565
#endif
1566
1567
switch (uMsg)
1568
{
1569
HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1570
HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1571
HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1572
HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1573
HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1574
HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1575
HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1576
HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1577
HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1578
HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1579
HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1580
HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1581
HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1582
HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
1583
HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
1584
1585
#ifdef FEAT_BEVAL_GUI
1586
case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1587
return TRUE;
1588
#endif
1589
default:
1590
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
1591
}
1592
}
1593
1594
/*
1595
* Called when the foreground or background color has been changed.
1596
*/
1597
void
1598
gui_mch_new_colors(void)
1599
{
1600
HBRUSH prevBrush;
1601
1602
s_brush = CreateSolidBrush(gui.back_pixel);
1603
prevBrush = (HBRUSH)SetClassLongPtr(
1604
s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
1605
InvalidateRect(s_hwnd, NULL, TRUE);
1606
DeleteObject(prevBrush);
1607
}
1608
1609
/*
1610
* Set the colors to their default values.
1611
*/
1612
void
1613
gui_mch_def_colors(void)
1614
{
1615
gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1616
gui.back_pixel = GetSysColor(COLOR_WINDOW);
1617
gui.def_norm_pixel = gui.norm_pixel;
1618
gui.def_back_pixel = gui.back_pixel;
1619
}
1620
1621
/*
1622
* Open the GUI window which was created by a call to gui_mch_init().
1623
*/
1624
int
1625
gui_mch_open(void)
1626
{
1627
// Actually open the window, if not already visible
1628
// (may be done already in gui_mch_set_shellsize)
1629
if (!IsWindowVisible(s_hwnd))
1630
ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1631
1632
#ifdef MSWIN_FIND_REPLACE
1633
// Init replace string here, so that we keep it when re-opening the
1634
// dialog.
1635
s_findrep_struct.lpstrReplaceWith[0] = NUL;
1636
#endif
1637
1638
return OK;
1639
}
1640
1641
/*
1642
* Get the position of the top left corner of the window.
1643
*/
1644
int
1645
gui_mch_get_winpos(int *x, int *y)
1646
{
1647
RECT rect;
1648
1649
GetWindowRect(s_hwnd, &rect);
1650
*x = rect.left;
1651
*y = rect.top;
1652
return OK;
1653
}
1654
1655
/*
1656
* Set the position of the top left corner of the window to the given
1657
* coordinates.
1658
*/
1659
void
1660
gui_mch_set_winpos(int x, int y)
1661
{
1662
SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1663
SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1664
}
1665
void
1666
gui_mch_set_text_area_pos(int x, int y, int w, int h)
1667
{
1668
static int oldx = 0;
1669
static int oldy = 0;
1670
1671
SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1672
1673
#ifdef FEAT_TOOLBAR
1674
if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1675
SendMessage(s_toolbarhwnd, WM_SIZE,
1676
(WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
1677
#endif
1678
#if defined(FEAT_GUI_TABLINE)
1679
if (showing_tabline)
1680
{
1681
int top = 0;
1682
RECT rect;
1683
1684
# ifdef FEAT_TOOLBAR
1685
if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1686
top = gui.toolbar_height;
1687
# endif
1688
GetClientRect(s_hwnd, &rect);
1689
MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1690
}
1691
#endif
1692
1693
// When side scroll bar is unshown, the size of window will change.
1694
// then, the text area move left or right. thus client rect should be
1695
// forcedly redrawn. (Yasuhiro Matsumoto)
1696
if (oldx != x || oldy != y)
1697
{
1698
InvalidateRect(s_hwnd, NULL, FALSE);
1699
oldx = x;
1700
oldy = y;
1701
}
1702
}
1703
1704
1705
/*
1706
* Scrollbar stuff:
1707
*/
1708
1709
void
1710
gui_mch_enable_scrollbar(
1711
scrollbar_T *sb,
1712
int flag)
1713
{
1714
ShowScrollBar(sb->id, SB_CTL, flag);
1715
1716
// TODO: When the window is maximized, the size of the window stays the
1717
// same, thus the size of the text area changes. On Win98 it's OK, on Win
1718
// NT 4.0 it's not...
1719
}
1720
1721
void
1722
gui_mch_set_scrollbar_pos(
1723
scrollbar_T *sb,
1724
int x,
1725
int y,
1726
int w,
1727
int h)
1728
{
1729
SetWindowPos(sb->id, NULL, x, y, w, h,
1730
SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1731
}
1732
1733
int
1734
gui_mch_get_scrollbar_xpadding(void)
1735
{
1736
RECT rcTxt, rcWnd;
1737
int xpad;
1738
1739
GetWindowRect(s_textArea, &rcTxt);
1740
GetWindowRect(s_hwnd, &rcWnd);
1741
xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
1742
- pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1743
- pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
1744
return (xpad < 0) ? 0 : xpad;
1745
}
1746
1747
int
1748
gui_mch_get_scrollbar_ypadding(void)
1749
{
1750
RECT rcTxt, rcWnd;
1751
int ypad;
1752
1753
GetWindowRect(s_textArea, &rcTxt);
1754
GetWindowRect(s_hwnd, &rcWnd);
1755
ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
1756
- pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1757
- pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
1758
return (ypad < 0) ? 0 : ypad;
1759
}
1760
1761
void
1762
gui_mch_create_scrollbar(
1763
scrollbar_T *sb,
1764
int orient) // SBAR_VERT or SBAR_HORIZ
1765
{
1766
sb->id = CreateWindow(
1767
"SCROLLBAR", "Scrollbar",
1768
WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
1769
10, // Any value will do for now
1770
10, // Any value will do for now
1771
s_hwnd, NULL,
1772
g_hinst, NULL);
1773
}
1774
1775
/*
1776
* Find the scrollbar with the given hwnd.
1777
*/
1778
static scrollbar_T *
1779
gui_mswin_find_scrollbar(HWND hwnd)
1780
{
1781
win_T *wp;
1782
1783
if (gui.bottom_sbar.id == hwnd)
1784
return &gui.bottom_sbar;
1785
FOR_ALL_WINDOWS(wp)
1786
{
1787
if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1788
return &wp->w_scrollbars[SBAR_LEFT];
1789
if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1790
return &wp->w_scrollbars[SBAR_RIGHT];
1791
}
1792
return NULL;
1793
}
1794
1795
static void
1796
update_scrollbar_size(void)
1797
{
1798
gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1799
gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1800
}
1801
1802
/*
1803
* Get the average character size of a font.
1804
*/
1805
static void
1806
GetAverageFontSize(HDC hdc, SIZE *size)
1807
{
1808
// GetTextMetrics() may not return the right value in tmAveCharWidth
1809
// for some fonts. Do our own average computation.
1810
GetTextExtentPoint(hdc,
1811
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1812
52, size);
1813
size->cx = (size->cx / 26 + 1) / 2;
1814
}
1815
1816
/*
1817
* Get the character size of a font.
1818
*/
1819
static void
1820
GetFontSize(GuiFont font, int *char_width, int *char_height)
1821
{
1822
HWND hwnd = GetDesktopWindow();
1823
HDC hdc = GetWindowDC(hwnd);
1824
HFONT hfntOld = SelectFont(hdc, (HFONT)font);
1825
SIZE size;
1826
TEXTMETRIC tm;
1827
1828
GetTextMetrics(hdc, &tm);
1829
GetAverageFontSize(hdc, &size);
1830
1831
if (char_width)
1832
*char_width = size.cx + tm.tmOverhang;
1833
if (char_height)
1834
*char_height = tm.tmHeight + p_linespace;
1835
1836
SelectFont(hdc, hfntOld);
1837
1838
ReleaseDC(hwnd, hdc);
1839
}
1840
1841
/*
1842
* Update the character size in "gui" structure with the specified font.
1843
*/
1844
static void
1845
UpdateFontSize(GuiFont font)
1846
{
1847
GetFontSize(font, &gui.char_width, &gui.char_height);
1848
}
1849
1850
/*
1851
* Adjust gui.char_height (after 'linespace' was changed).
1852
*/
1853
int
1854
gui_mch_adjust_charheight(void)
1855
{
1856
UpdateFontSize(gui.norm_font);
1857
return OK;
1858
}
1859
1860
static GuiFont
1861
get_font_handle(LOGFONTW *lf)
1862
{
1863
HFONT font = NULL;
1864
1865
// Load the font
1866
font = CreateFontIndirectW(lf);
1867
1868
if (font == NULL)
1869
return NOFONT;
1870
1871
return (GuiFont)font;
1872
}
1873
1874
static int
1875
pixels_to_points(int pixels, int vertical)
1876
{
1877
int points;
1878
HWND hwnd;
1879
HDC hdc;
1880
1881
hwnd = GetDesktopWindow();
1882
hdc = GetWindowDC(hwnd);
1883
1884
points = MulDiv(pixels, 72,
1885
GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1886
1887
ReleaseDC(hwnd, hdc);
1888
1889
return points;
1890
}
1891
1892
GuiFont
1893
gui_mch_get_font(
1894
char_u *name,
1895
int giveErrorIfMissing)
1896
{
1897
LOGFONTW lf;
1898
GuiFont font = NOFONT;
1899
1900
if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1901
{
1902
lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
1903
font = get_font_handle(&lf);
1904
}
1905
if (font == NOFONT && giveErrorIfMissing)
1906
semsg(_(e_unknown_font_str), name);
1907
return font;
1908
}
1909
1910
#if defined(FEAT_EVAL) || defined(PROTO)
1911
/*
1912
* Return the name of font "font" in allocated memory.
1913
* Don't know how to get the actual name, thus use the provided name.
1914
*/
1915
char_u *
1916
gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
1917
{
1918
if (name == NULL)
1919
return NULL;
1920
return vim_strsave(name);
1921
}
1922
#endif
1923
1924
void
1925
gui_mch_free_font(GuiFont font)
1926
{
1927
if (font)
1928
DeleteObject((HFONT)font);
1929
}
1930
1931
/*
1932
* Return the Pixel value (color) for the given color name.
1933
* Return INVALCOLOR for error.
1934
*/
1935
guicolor_T
1936
gui_mch_get_color(char_u *name)
1937
{
1938
int i;
1939
1940
typedef struct SysColorTable
1941
{
1942
char *name;
1943
int color;
1944
} SysColorTable;
1945
1946
static SysColorTable sys_table[] =
1947
{
1948
{"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1949
{"SYS_3DHILIGHT", COLOR_3DHILIGHT},
1950
#ifdef COLOR_3DHIGHLIGHT
1951
{"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1952
#endif
1953
{"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1954
{"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1955
{"SYS_3DLIGHT", COLOR_3DLIGHT},
1956
{"SYS_3DSHADOW", COLOR_3DSHADOW},
1957
{"SYS_DESKTOP", COLOR_DESKTOP},
1958
{"SYS_INFOBK", COLOR_INFOBK},
1959
{"SYS_INFOTEXT", COLOR_INFOTEXT},
1960
{"SYS_3DFACE", COLOR_3DFACE},
1961
{"SYS_BTNFACE", COLOR_BTNFACE},
1962
{"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1963
{"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1964
{"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1965
{"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1966
{"SYS_BACKGROUND", COLOR_BACKGROUND},
1967
{"SYS_BTNTEXT", COLOR_BTNTEXT},
1968
{"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1969
{"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1970
{"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1971
{"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1972
{"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1973
{"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1974
{"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1975
{"SYS_MENU", COLOR_MENU},
1976
{"SYS_MENUTEXT", COLOR_MENUTEXT},
1977
{"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1978
{"SYS_WINDOW", COLOR_WINDOW},
1979
{"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1980
{"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1981
};
1982
1983
/*
1984
* Try to look up a system colour.
1985
*/
1986
for (i = 0; i < (int)ARRAY_LENGTH(sys_table); i++)
1987
if (STRICMP(name, sys_table[i].name) == 0)
1988
return GetSysColor(sys_table[i].color);
1989
1990
return gui_get_color_cmn(name);
1991
}
1992
1993
guicolor_T
1994
gui_mch_get_rgb_color(int r, int g, int b)
1995
{
1996
return gui_get_rgb_color_cmn(r, g, b);
1997
}
1998
1999
/*
2000
* Return OK if the key with the termcap name "name" is supported.
2001
*/
2002
int
2003
gui_mch_haskey(char_u *name)
2004
{
2005
int i;
2006
2007
for (i = 0; special_keys[i].vim_code1 != NUL; i++)
2008
if (name[0] == special_keys[i].vim_code0
2009
&& name[1] == special_keys[i].vim_code1)
2010
return OK;
2011
return FAIL;
2012
}
2013
2014
void
2015
gui_mch_beep(void)
2016
{
2017
MessageBeep((UINT)-1);
2018
}
2019
/*
2020
* Invert a rectangle from row r, column c, for nr rows and nc columns.
2021
*/
2022
void
2023
gui_mch_invert_rectangle(
2024
int r,
2025
int c,
2026
int nr,
2027
int nc)
2028
{
2029
RECT rc;
2030
2031
#if defined(FEAT_DIRECTX)
2032
if (IS_ENABLE_DIRECTX())
2033
DWriteContext_Flush(s_dwc);
2034
#endif
2035
2036
/*
2037
* Note: InvertRect() excludes right and bottom of rectangle.
2038
*/
2039
rc.left = FILL_X(c);
2040
rc.top = FILL_Y(r);
2041
rc.right = rc.left + nc * gui.char_width;
2042
rc.bottom = rc.top + nr * gui.char_height;
2043
InvertRect(s_hdc, &rc);
2044
}
2045
2046
/*
2047
* Iconify the GUI window.
2048
*/
2049
void
2050
gui_mch_iconify(void)
2051
{
2052
ShowWindow(s_hwnd, SW_MINIMIZE);
2053
}
2054
2055
/*
2056
* Draw a cursor without focus.
2057
*/
2058
void
2059
gui_mch_draw_hollow_cursor(guicolor_T color)
2060
{
2061
HBRUSH hbr;
2062
RECT rc;
2063
2064
#if defined(FEAT_DIRECTX)
2065
if (IS_ENABLE_DIRECTX())
2066
DWriteContext_Flush(s_dwc);
2067
#endif
2068
2069
/*
2070
* Note: FrameRect() excludes right and bottom of rectangle.
2071
*/
2072
rc.left = FILL_X(gui.col);
2073
rc.top = FILL_Y(gui.row);
2074
rc.right = rc.left + gui.char_width;
2075
if (mb_lefthalve(gui.row, gui.col))
2076
rc.right += gui.char_width;
2077
rc.bottom = rc.top + gui.char_height;
2078
hbr = CreateSolidBrush(color);
2079
FrameRect(s_hdc, &rc, hbr);
2080
DeleteBrush(hbr);
2081
}
2082
/*
2083
* Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
2084
* color "color".
2085
*/
2086
void
2087
gui_mch_draw_part_cursor(
2088
int w,
2089
int h,
2090
guicolor_T color)
2091
{
2092
RECT rc;
2093
2094
/*
2095
* Note: FillRect() excludes right and bottom of rectangle.
2096
*/
2097
rc.left =
2098
#ifdef FEAT_RIGHTLEFT
2099
// vertical line should be on the right of current point
2100
CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
2101
#endif
2102
FILL_X(gui.col);
2103
rc.top = FILL_Y(gui.row) + gui.char_height - h;
2104
rc.right = rc.left + w;
2105
rc.bottom = rc.top + h;
2106
2107
fill_rect(&rc, NULL, color);
2108
}
2109
2110
2111
/*
2112
* Generates a VK_SPACE when the internal dead_key flag is set to output the
2113
* dead key's nominal character and re-post the original message.
2114
*/
2115
static void
2116
outputDeadKey_rePost_Ex(MSG originalMsg, int dead_key2set)
2117
{
2118
static MSG deadCharExpel;
2119
2120
if (dead_key == DEAD_KEY_OFF)
2121
return;
2122
2123
dead_key = dead_key2set;
2124
2125
// Make Windows generate the dead key's character
2126
deadCharExpel.message = originalMsg.message;
2127
deadCharExpel.hwnd = originalMsg.hwnd;
2128
deadCharExpel.wParam = VK_SPACE;
2129
2130
TranslateMessage(&deadCharExpel);
2131
2132
// re-generate the current character free of the dead char influence
2133
PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
2134
originalMsg.lParam);
2135
}
2136
2137
/*
2138
* Wrapper for outputDeadKey_rePost_Ex which always reset dead_key value.
2139
*/
2140
static void
2141
outputDeadKey_rePost(MSG originalMsg)
2142
{
2143
outputDeadKey_rePost_Ex(originalMsg, DEAD_KEY_OFF);
2144
}
2145
2146
/*
2147
* Refactored out part of process_message(), responsible for
2148
* handling the case of "not a special key"
2149
*/
2150
static void process_message_usual_key(UINT vk, const MSG *pmsg)
2151
{
2152
// marshal to corresponding implementation
2153
keycode_trans_strategy_used->ptr_process_message_usual_key(vk, pmsg);
2154
}
2155
2156
/*
2157
* Experimental implementation, introduced in v8.2.4807
2158
* "processing key event in Win32 GUI is not ideal"
2159
*/
2160
static void
2161
process_message_usual_key_experimental(UINT vk, const MSG *pmsg)
2162
{
2163
WCHAR ch[8];
2164
int len;
2165
int i;
2166
UINT scan_code;
2167
BYTE keyboard_state[256];
2168
2169
// Construct the state table with only a few modifiers, we don't
2170
// really care about the presence of Ctrl/Alt as those modifiers are
2171
// handled by Vim separately.
2172
memset(keyboard_state, 0, 256);
2173
if (GetKeyState(VK_SHIFT) & 0x8000)
2174
keyboard_state[VK_SHIFT] = 0x80;
2175
if (GetKeyState(VK_CAPITAL) & 0x0001)
2176
keyboard_state[VK_CAPITAL] = 0x01;
2177
// Alt-Gr is synthesized as Alt + Ctrl.
2178
if ((GetKeyState(VK_RMENU) & 0x8000)
2179
&& (GetKeyState(VK_CONTROL) & 0x8000))
2180
{
2181
keyboard_state[VK_MENU] = 0x80;
2182
keyboard_state[VK_CONTROL] = 0x80;
2183
}
2184
2185
// Translate the virtual key according to the current keyboard
2186
// layout.
2187
scan_code = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
2188
// Convert the scan-code into a sequence of zero or more unicode
2189
// codepoints.
2190
// If this is a dead key ToUnicode returns a negative value.
2191
len = ToUnicode(vk, scan_code, keyboard_state, ch, ARRAY_LENGTH(ch),
2192
0);
2193
if (len < 0)
2194
dead_key = DEAD_KEY_SET_DEFAULT;
2195
2196
if (len <= 0)
2197
{
2198
int wm_char = NUL;
2199
2200
if (dead_key == DEAD_KEY_SET_DEFAULT
2201
&& (GetKeyState(VK_CONTROL) & 0x8000))
2202
{
2203
if ( // AZERTY CTRL+dead_circumflex
2204
(vk == 221 && scan_code == 26)
2205
// QWERTZ CTRL+dead_circumflex
2206
|| (vk == 220 && scan_code == 41))
2207
wm_char = '[';
2208
if ( // QWERTZ CTRL+dead_two-overdots
2209
(vk == 192 && scan_code == 27))
2210
wm_char = ']';
2211
}
2212
if (wm_char != NUL)
2213
{
2214
// post WM_CHAR='[' - which will be interpreted with CTRL
2215
// still hold as ESC
2216
PostMessageW(pmsg->hwnd, WM_CHAR, wm_char, pmsg->lParam);
2217
// ask _OnChar() to not touch this state, wait for next key
2218
// press and maintain knowledge that we are "poisoned" with
2219
// "dead state"
2220
dead_key = DEAD_KEY_TRANSIENT_IN_ON_CHAR;
2221
}
2222
return;
2223
}
2224
2225
// Post the message as TranslateMessage would do.
2226
if (pmsg->message == WM_KEYDOWN)
2227
{
2228
for (i = 0; i < len; i++)
2229
PostMessageW(pmsg->hwnd, WM_CHAR, ch[i], pmsg->lParam);
2230
}
2231
else
2232
{
2233
for (i = 0; i < len; i++)
2234
PostMessageW(pmsg->hwnd, WM_SYSCHAR, ch[i], pmsg->lParam);
2235
}
2236
}
2237
2238
/*
2239
* "Classic" implementation, existing prior to v8.2.4807
2240
*/
2241
static void
2242
process_message_usual_key_classic(UINT vk, const MSG *pmsg)
2243
{
2244
char_u string[40];
2245
2246
// Some keys need C-S- where they should only need C-.
2247
// Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
2248
// system startup (Helmut Stiegler, 2003 Oct 3).
2249
if (vk != 0xff
2250
&& (GetKeyState(VK_CONTROL) & 0x8000)
2251
&& !(GetKeyState(VK_SHIFT) & 0x8000)
2252
&& !(GetKeyState(VK_MENU) & 0x8000))
2253
{
2254
// CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE
2255
if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
2256
{
2257
string[0] = Ctrl_HAT;
2258
add_to_input_buf(string, 1);
2259
}
2260
// vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY!
2261
else if (vk == 0xBD) // QWERTY for CTRL-'-'
2262
{
2263
string[0] = Ctrl__;
2264
add_to_input_buf(string, 1);
2265
}
2266
// CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0
2267
else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2268
{
2269
string[0] = Ctrl_AT;
2270
add_to_input_buf(string, 1);
2271
}
2272
else
2273
TranslateMessage(pmsg);
2274
}
2275
else
2276
TranslateMessage(pmsg);
2277
}
2278
2279
/*
2280
* Process a single Windows message.
2281
* If one is not available we hang until one is.
2282
*/
2283
static void
2284
process_message(void)
2285
{
2286
MSG msg;
2287
UINT vk = 0; // Virtual key
2288
char_u string[40];
2289
int i;
2290
int modifiers = 0;
2291
int key;
2292
#ifdef FEAT_MENU
2293
static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
2294
#endif
2295
static int keycode_trans_strategy_initialized = 0;
2296
2297
// lazy initialize - first time only
2298
if (!keycode_trans_strategy_initialized)
2299
{
2300
keycode_trans_strategy_initialized = 1;
2301
keycode_trans_strategy_init();
2302
}
2303
2304
GetMessageW(&msg, NULL, 0, 0);
2305
2306
#ifdef FEAT_OLE
2307
// Look after OLE Automation commands
2308
if (msg.message == WM_OLE)
2309
{
2310
char_u *str = (char_u *)msg.lParam;
2311
if (str == NULL || *str == NUL)
2312
{
2313
// Message can't be ours, forward it. Fixes problem with Ultramon
2314
// 3.0.4
2315
DispatchMessageW(&msg);
2316
}
2317
else
2318
{
2319
add_to_input_buf(str, (int)STRLEN(str));
2320
vim_free(str); // was allocated in CVim::SendKeys()
2321
}
2322
return;
2323
}
2324
#endif
2325
2326
#ifdef MSWIN_FIND_REPLACE
2327
// Don't process messages used by the dialog
2328
if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
2329
{
2330
HandleMouseHide(msg.message, msg.lParam);
2331
return;
2332
}
2333
#endif
2334
2335
/*
2336
* Check if it's a special key that we recognise. If not, call
2337
* TranslateMessage().
2338
*/
2339
if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
2340
{
2341
vk = (int) msg.wParam;
2342
2343
/*
2344
* Handle dead keys in special conditions in other cases we let Windows
2345
* handle them and do not interfere.
2346
*
2347
* The dead_key flag must be reset on several occasions:
2348
* - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
2349
* consumed at that point (This is when we let Windows combine the
2350
* dead character on its own)
2351
*
2352
* - Before doing something special such as regenerating keypresses to
2353
* expel the dead character as this could trigger an infinite loop if
2354
* for some reason TranslateMessage() do not trigger a call
2355
* immediately to _OnChar() (or _OnSysChar()).
2356
*/
2357
2358
/*
2359
* We are at the moment after WM_CHAR with DEAD_KEY_SKIP_ON_CHAR event
2360
* was handled by _WndProc, this keypress we want to process normally
2361
*/
2362
if (keycode_trans_strategy_used->is_experimental()
2363
&& dead_key == DEAD_KEY_SKIP_ON_CHAR)
2364
{
2365
dead_key = DEAD_KEY_OFF;
2366
}
2367
2368
if (dead_key != DEAD_KEY_OFF)
2369
{
2370
/*
2371
* Expel the dead key pressed with Ctrl in a special way.
2372
*
2373
* After dead key was pressed with Ctrl in some cases, ESC was
2374
* artificially injected and handled by _OnChar(), now we are
2375
* dealing with completely new key press from the user. If we don't
2376
* do anything, ToUnicode() call will interpret this vk+scan_code
2377
* under influence of "dead-modifier". To prevent this we translate
2378
* this message replacing current char from user with VK_SPACE,
2379
* which will cause WM_CHAR with dead_key's character itself. Using
2380
* DEAD_KEY_SKIP_ON_CHAR value of dead_char we force _OnChar() to
2381
* ignore this one WM_CHAR event completely. Afterwards (due to
2382
* usage of PostMessage), this procedure is scheduled to be called
2383
* again with user char and on next entry we will clean
2384
* DEAD_KEY_SKIP_ON_CHAR. We cannot use original
2385
* outputDeadKey_rePost() since we do not wish to reset dead_key
2386
* value.
2387
*/
2388
if (keycode_trans_strategy_used->is_experimental() &&
2389
dead_key == DEAD_KEY_TRANSIENT_IN_ON_CHAR)
2390
{
2391
outputDeadKey_rePost_Ex(msg,
2392
/*dead_key2set=*/DEAD_KEY_SKIP_ON_CHAR);
2393
return;
2394
}
2395
2396
if (dead_key != DEAD_KEY_SET_DEFAULT)
2397
{
2398
// should never happen - is there a way to make ASSERT here?
2399
return;
2400
}
2401
2402
/*
2403
* If a dead key was pressed and the user presses VK_SPACE,
2404
* VK_BACK, or VK_ESCAPE it means that he actually wants to deal
2405
* with the dead char now, so do nothing special and let Windows
2406
* handle it.
2407
*
2408
* Note that VK_SPACE combines with the dead_key's character and
2409
* only one WM_CHAR will be generated by TranslateMessage(), in
2410
* the two other cases two WM_CHAR will be generated: the dead
2411
* char and VK_BACK or VK_ESCAPE. That is most likely what the
2412
* user expects.
2413
*/
2414
if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
2415
{
2416
dead_key = DEAD_KEY_OFF;
2417
TranslateMessage(&msg);
2418
return;
2419
}
2420
// In modes where we are not typing, dead keys should behave
2421
// normally
2422
else if ((get_real_state()
2423
& (MODE_INSERT | MODE_CMDLINE | MODE_SELECT)) == 0)
2424
{
2425
outputDeadKey_rePost(msg);
2426
return;
2427
}
2428
}
2429
2430
// Check for CTRL-BREAK
2431
if (vk == VK_CANCEL)
2432
{
2433
trash_input_buf();
2434
got_int = TRUE;
2435
ctrl_break_was_pressed = TRUE;
2436
string[0] = Ctrl_C;
2437
add_to_input_buf(string, 1);
2438
}
2439
2440
// This is an IME event or a synthetic keystroke, let Windows handle it.
2441
if (vk == VK_PROCESSKEY || vk == VK_PACKET)
2442
{
2443
TranslateMessage(&msg);
2444
return;
2445
}
2446
2447
for (i = 0; special_keys[i].key_sym != 0; i++)
2448
{
2449
// ignore VK_SPACE when ALT key pressed: system menu
2450
if (special_keys[i].key_sym == vk
2451
&& (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
2452
{
2453
/*
2454
* Behave as expected if we have a dead key and the special key
2455
* is a key that would normally trigger the dead key nominal
2456
* character output (such as a NUMPAD printable character or
2457
* the TAB key, etc...).
2458
*/
2459
if (dead_key == DEAD_KEY_SET_DEFAULT
2460
&& (special_keys[i].vim_code0 == 'K'
2461
|| vk == VK_TAB || vk == CAR))
2462
{
2463
outputDeadKey_rePost(msg);
2464
return;
2465
}
2466
2467
#ifdef FEAT_MENU
2468
// Check for <F10>: Windows selects the menu. When <F10> is
2469
// mapped we want to use the mapping instead.
2470
if (vk == VK_F10
2471
&& gui.menu_is_active
2472
&& check_map(k10, State, FALSE, TRUE, FALSE,
2473
NULL, NULL) == NULL)
2474
break;
2475
#endif
2476
modifiers = get_active_modifiers_via_ptr();
2477
2478
if (special_keys[i].vim_code1 == NUL)
2479
key = special_keys[i].vim_code0;
2480
else
2481
key = TO_SPECIAL(special_keys[i].vim_code0,
2482
special_keys[i].vim_code1);
2483
key = simplify_key(key, &modifiers);
2484
if (key == CSI)
2485
key = K_CSI;
2486
2487
if (modifiers)
2488
{
2489
string[0] = CSI;
2490
string[1] = KS_MODIFIER;
2491
string[2] = modifiers;
2492
add_to_input_buf(string, 3);
2493
}
2494
2495
if (IS_SPECIAL(key))
2496
{
2497
string[0] = CSI;
2498
string[1] = K_SECOND(key);
2499
string[2] = K_THIRD(key);
2500
add_to_input_buf(string, 3);
2501
}
2502
else
2503
{
2504
int len;
2505
2506
// Handle "key" as a Unicode character.
2507
len = char_to_string(key, string, 40, FALSE);
2508
add_to_input_buf(string, len);
2509
}
2510
break;
2511
}
2512
}
2513
2514
// Not a special key.
2515
if (special_keys[i].key_sym == 0)
2516
{
2517
process_message_usual_key(vk, &msg);
2518
}
2519
}
2520
#ifdef FEAT_MBYTE_IME
2521
else if (msg.message == WM_IME_NOTIFY)
2522
_OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2523
else if (msg.message == WM_KEYUP && im_get_status())
2524
// added for non-MS IME (Yasuhiro Matsumoto)
2525
TranslateMessage(&msg);
2526
#endif
2527
2528
#ifdef FEAT_MENU
2529
// Check for <F10>: Default effect is to select the menu. When <F10> is
2530
// mapped we need to stop it here to avoid strange effects (e.g., for the
2531
// key-up event)
2532
if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2533
NULL, NULL) == NULL)
2534
#endif
2535
DispatchMessageW(&msg);
2536
}
2537
2538
/*
2539
* Catch up with any queued events. This may put keyboard input into the
2540
* input buffer, call resize call-backs, trigger timers etc. If there is
2541
* nothing in the event queue (& no timers pending), then we return
2542
* immediately.
2543
*/
2544
void
2545
gui_mch_update(void)
2546
{
2547
MSG msg;
2548
2549
if (!s_busy_processing)
2550
while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
2551
&& !vim_is_input_buf_full())
2552
process_message();
2553
}
2554
2555
static void
2556
remove_any_timer(void)
2557
{
2558
MSG msg;
2559
2560
if (s_wait_timer != 0 && !s_timed_out)
2561
{
2562
KillTimer(NULL, s_wait_timer);
2563
2564
// Eat spurious WM_TIMER messages
2565
while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2566
;
2567
s_wait_timer = 0;
2568
}
2569
}
2570
2571
/*
2572
* GUI input routine called by gui_wait_for_chars(). Waits for a character
2573
* from the keyboard.
2574
* wtime == -1 Wait forever.
2575
* wtime == 0 This should never happen.
2576
* wtime > 0 Wait wtime milliseconds for a character.
2577
* Returns OK if a character was found to be available within the given time,
2578
* or FAIL otherwise.
2579
*/
2580
int
2581
gui_mch_wait_for_chars(int wtime)
2582
{
2583
int focus;
2584
2585
s_timed_out = FALSE;
2586
2587
if (wtime >= 0)
2588
{
2589
// Don't do anything while processing a (scroll) message.
2590
if (s_busy_processing)
2591
return FAIL;
2592
2593
// When called with "wtime" zero, just want one msec.
2594
s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2595
_OnTimer);
2596
}
2597
2598
allow_scrollbar = TRUE;
2599
2600
focus = gui.in_focus;
2601
while (!s_timed_out)
2602
{
2603
// Stop or start blinking when focus changes
2604
if (gui.in_focus != focus)
2605
{
2606
if (gui.in_focus)
2607
gui_mch_start_blink();
2608
else
2609
gui_mch_stop_blink(TRUE);
2610
focus = gui.in_focus;
2611
}
2612
2613
if (s_need_activate)
2614
{
2615
(void)SetForegroundWindow(s_hwnd);
2616
s_need_activate = FALSE;
2617
}
2618
2619
#ifdef FEAT_TIMERS
2620
did_add_timer = FALSE;
2621
#endif
2622
#ifdef MESSAGE_QUEUE
2623
// Check channel I/O while waiting for a message.
2624
for (;;)
2625
{
2626
MSG msg;
2627
2628
parse_queued_messages();
2629
# ifdef FEAT_TIMERS
2630
if (did_add_timer)
2631
break;
2632
# endif
2633
if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
2634
{
2635
process_message();
2636
break;
2637
}
2638
else if (input_available()
2639
// TODO: The 10 msec is a compromise between laggy response
2640
// and consuming more CPU time. Better would be to handle
2641
// channel messages when they arrive.
2642
|| MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
2643
QS_ALLINPUT) != WAIT_TIMEOUT)
2644
break;
2645
}
2646
#else
2647
// Don't use gui_mch_update() because then we will spin-lock until a
2648
// char arrives, instead we use GetMessage() to hang until an
2649
// event arrives. No need to check for input_buf_full because we are
2650
// returning as soon as it contains a single char -- webb
2651
process_message();
2652
#endif
2653
2654
if (input_available())
2655
{
2656
remove_any_timer();
2657
allow_scrollbar = FALSE;
2658
2659
// Clear pending mouse button, the release event may have been
2660
// taken by the dialog window. But don't do this when getting
2661
// focus, we need the mouse-up event then.
2662
if (!s_getting_focus)
2663
s_button_pending = -1;
2664
2665
return OK;
2666
}
2667
2668
#ifdef FEAT_TIMERS
2669
if (did_add_timer)
2670
{
2671
// Need to recompute the waiting time.
2672
remove_any_timer();
2673
break;
2674
}
2675
#endif
2676
}
2677
allow_scrollbar = FALSE;
2678
return FAIL;
2679
}
2680
2681
/*
2682
* Clear a rectangular region of the screen from text pos (row1, col1) to
2683
* (row2, col2) inclusive.
2684
*/
2685
void
2686
gui_mch_clear_block(
2687
int row1,
2688
int col1,
2689
int row2,
2690
int col2)
2691
{
2692
RECT rc;
2693
2694
/*
2695
* Clear one extra pixel at the far right, for when bold characters have
2696
* spilled over to the window border.
2697
* Note: FillRect() excludes right and bottom of rectangle.
2698
*/
2699
rc.left = FILL_X(col1);
2700
rc.top = FILL_Y(row1);
2701
rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2702
rc.bottom = FILL_Y(row2 + 1);
2703
clear_rect(&rc);
2704
}
2705
2706
/*
2707
* Clear the whole text window.
2708
*/
2709
void
2710
gui_mch_clear_all(void)
2711
{
2712
RECT rc;
2713
2714
rc.left = 0;
2715
rc.top = 0;
2716
rc.right = Columns * gui.char_width + 2 * gui.border_width;
2717
rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2718
clear_rect(&rc);
2719
}
2720
/*
2721
* Menu stuff.
2722
*/
2723
2724
void
2725
gui_mch_enable_menu(int flag)
2726
{
2727
#ifdef FEAT_MENU
2728
SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2729
#endif
2730
}
2731
2732
void
2733
gui_mch_set_menu_pos(
2734
int x UNUSED,
2735
int y UNUSED,
2736
int w UNUSED,
2737
int h UNUSED)
2738
{
2739
// It will be in the right place anyway
2740
}
2741
2742
#if defined(FEAT_MENU) || defined(PROTO)
2743
/*
2744
* Make menu item hidden or not hidden
2745
*/
2746
void
2747
gui_mch_menu_hidden(
2748
vimmenu_T *menu,
2749
int hidden)
2750
{
2751
/*
2752
* This doesn't do what we want. Hmm, just grey the menu items for now.
2753
*/
2754
/*
2755
if (hidden)
2756
EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2757
else
2758
EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2759
*/
2760
gui_mch_menu_grey(menu, hidden);
2761
}
2762
2763
/*
2764
* This is called after setting all the menus to grey/hidden or not.
2765
*/
2766
void
2767
gui_mch_draw_menubar(void)
2768
{
2769
DrawMenuBar(s_hwnd);
2770
}
2771
#endif // FEAT_MENU
2772
2773
/*
2774
* Return the RGB value of a pixel as a long.
2775
*/
2776
guicolor_T
2777
gui_mch_get_rgb(guicolor_T pixel)
2778
{
2779
return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2780
+ GetBValue(pixel));
2781
}
2782
2783
#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2784
/*
2785
* Convert pixels in X to dialog units
2786
*/
2787
static WORD
2788
PixelToDialogX(int numPixels)
2789
{
2790
return (WORD)((numPixels * 4) / s_dlgfntwidth);
2791
}
2792
2793
/*
2794
* Convert pixels in Y to dialog units
2795
*/
2796
static WORD
2797
PixelToDialogY(int numPixels)
2798
{
2799
return (WORD)((numPixels * 8) / s_dlgfntheight);
2800
}
2801
2802
/*
2803
* Return the width in pixels of the given text in the given DC.
2804
*/
2805
static int
2806
GetTextWidth(HDC hdc, char_u *str, int len)
2807
{
2808
SIZE size;
2809
2810
GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2811
return size.cx;
2812
}
2813
2814
/*
2815
* Return the width in pixels of the given text in the given DC, taking care
2816
* of 'encoding' to active codepage conversion.
2817
*/
2818
static int
2819
GetTextWidthEnc(HDC hdc, char_u *str, int len)
2820
{
2821
SIZE size;
2822
WCHAR *wstr;
2823
int n;
2824
int wlen = len;
2825
2826
wstr = enc_to_utf16(str, &wlen);
2827
if (wstr == NULL)
2828
return 0;
2829
2830
n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2831
vim_free(wstr);
2832
if (n)
2833
return size.cx;
2834
return 0;
2835
}
2836
2837
static void get_work_area(RECT *spi_rect);
2838
2839
/*
2840
* A quick little routine that will center one window over another, handy for
2841
* dialog boxes. Taken from the Win32SDK samples and modified for multiple
2842
* monitors.
2843
*/
2844
static BOOL
2845
CenterWindow(
2846
HWND hwndChild,
2847
HWND hwndParent)
2848
{
2849
HMONITOR mon;
2850
MONITORINFO moninfo;
2851
RECT rChild, rParent, rScreen;
2852
int wChild, hChild, wParent, hParent;
2853
int xNew, yNew;
2854
HDC hdc;
2855
2856
GetWindowRect(hwndChild, &rChild);
2857
wChild = rChild.right - rChild.left;
2858
hChild = rChild.bottom - rChild.top;
2859
2860
// If Vim is minimized put the window in the middle of the screen.
2861
if (hwndParent == NULL || IsMinimized(hwndParent))
2862
get_work_area(&rParent);
2863
else
2864
GetWindowRect(hwndParent, &rParent);
2865
wParent = rParent.right - rParent.left;
2866
hParent = rParent.bottom - rParent.top;
2867
2868
moninfo.cbSize = sizeof(MONITORINFO);
2869
mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2870
if (mon != NULL && GetMonitorInfo(mon, &moninfo))
2871
{
2872
rScreen = moninfo.rcWork;
2873
}
2874
else
2875
{
2876
hdc = GetDC(hwndChild);
2877
rScreen.left = 0;
2878
rScreen.top = 0;
2879
rScreen.right = GetDeviceCaps(hdc, HORZRES);
2880
rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2881
ReleaseDC(hwndChild, hdc);
2882
}
2883
2884
xNew = rParent.left + ((wParent - wChild) / 2);
2885
if (xNew < rScreen.left)
2886
xNew = rScreen.left;
2887
else if ((xNew + wChild) > rScreen.right)
2888
xNew = rScreen.right - wChild;
2889
2890
yNew = rParent.top + ((hParent - hChild) / 2);
2891
if (yNew < rScreen.top)
2892
yNew = rScreen.top;
2893
else if ((yNew + hChild) > rScreen.bottom)
2894
yNew = rScreen.bottom - hChild;
2895
2896
return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2897
SWP_NOSIZE | SWP_NOZORDER);
2898
}
2899
#endif // FEAT_GUI_DIALOG
2900
2901
#if defined(FEAT_TOOLBAR) || defined(PROTO)
2902
void
2903
gui_mch_show_toolbar(int showit)
2904
{
2905
if (s_toolbarhwnd == NULL)
2906
return;
2907
2908
if (showit)
2909
{
2910
// Enable unicode support
2911
SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2912
(LPARAM)0);
2913
ShowWindow(s_toolbarhwnd, SW_SHOW);
2914
}
2915
else
2916
ShowWindow(s_toolbarhwnd, SW_HIDE);
2917
}
2918
2919
// The number of bitmaps is fixed. Exit is missing!
2920
# define TOOLBAR_BITMAP_COUNT 31
2921
2922
#endif
2923
2924
#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2925
static void
2926
add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2927
{
2928
WCHAR *wn;
2929
MENUITEMINFOW infow;
2930
2931
wn = enc_to_utf16(item_text, NULL);
2932
if (wn == NULL)
2933
return;
2934
2935
infow.cbSize = sizeof(infow);
2936
infow.fMask = MIIM_TYPE | MIIM_ID;
2937
infow.wID = item_id;
2938
infow.fType = MFT_STRING;
2939
infow.dwTypeData = wn;
2940
infow.cch = (UINT)wcslen(wn);
2941
InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2942
vim_free(wn);
2943
}
2944
2945
static void
2946
show_tabline_popup_menu(void)
2947
{
2948
HMENU tab_pmenu;
2949
long rval;
2950
POINT pt;
2951
2952
// When ignoring events don't show the menu.
2953
if (hold_gui_events || cmdwin_type != 0)
2954
return;
2955
2956
tab_pmenu = CreatePopupMenu();
2957
if (tab_pmenu == NULL)
2958
return;
2959
2960
if (first_tabpage->tp_next != NULL)
2961
add_tabline_popup_menu_entry(tab_pmenu,
2962
TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2963
add_tabline_popup_menu_entry(tab_pmenu,
2964
TABLINE_MENU_NEW, (char_u *)_("New tab"));
2965
add_tabline_popup_menu_entry(tab_pmenu,
2966
TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2967
2968
GetCursorPos(&pt);
2969
rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2970
NULL);
2971
2972
DestroyMenu(tab_pmenu);
2973
2974
// Add the string cmd into input buffer
2975
if (rval > 0)
2976
{
2977
TCHITTESTINFO htinfo;
2978
int idx;
2979
2980
if (ScreenToClient(s_tabhwnd, &pt) == 0)
2981
return;
2982
2983
htinfo.pt.x = pt.x;
2984
htinfo.pt.y = pt.y;
2985
idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2986
if (idx == -1)
2987
idx = 0;
2988
else
2989
idx += 1;
2990
2991
send_tabline_menu_event(idx, (int)rval);
2992
}
2993
}
2994
2995
/*
2996
* Show or hide the tabline.
2997
*/
2998
void
2999
gui_mch_show_tabline(int showit)
3000
{
3001
if (s_tabhwnd == NULL)
3002
return;
3003
3004
if (!showit != !showing_tabline)
3005
{
3006
if (showit)
3007
ShowWindow(s_tabhwnd, SW_SHOW);
3008
else
3009
ShowWindow(s_tabhwnd, SW_HIDE);
3010
showing_tabline = showit;
3011
}
3012
}
3013
3014
/*
3015
* Return TRUE when tabline is displayed.
3016
*/
3017
int
3018
gui_mch_showing_tabline(void)
3019
{
3020
return s_tabhwnd != NULL && showing_tabline;
3021
}
3022
3023
/*
3024
* Update the labels of the tabline.
3025
*/
3026
void
3027
gui_mch_update_tabline(void)
3028
{
3029
tabpage_T *tp;
3030
TCITEM tie;
3031
int nr = 0;
3032
int curtabidx = 0;
3033
int tabadded = 0;
3034
WCHAR *wstr = NULL;
3035
3036
if (s_tabhwnd == NULL)
3037
return;
3038
3039
// Enable unicode support
3040
SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
3041
3042
tie.mask = TCIF_TEXT;
3043
tie.iImage = -1;
3044
3045
// Disable redraw for tab updates to eliminate O(N^2) draws.
3046
SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
3047
3048
// Add a label for each tab page. They all contain the same text area.
3049
for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3050
{
3051
if (tp == curtab)
3052
curtabidx = nr;
3053
3054
if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
3055
{
3056
// Add the tab
3057
tie.pszText = "-Empty-";
3058
TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
3059
tabadded = 1;
3060
}
3061
3062
get_tabline_label(tp, FALSE);
3063
tie.pszText = (LPSTR)NameBuff;
3064
3065
wstr = enc_to_utf16(NameBuff, NULL);
3066
if (wstr != NULL)
3067
{
3068
TCITEMW tiw;
3069
3070
tiw.mask = TCIF_TEXT;
3071
tiw.iImage = -1;
3072
tiw.pszText = wstr;
3073
SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
3074
vim_free(wstr);
3075
}
3076
}
3077
3078
// Remove any old labels.
3079
while (nr < TabCtrl_GetItemCount(s_tabhwnd))
3080
TabCtrl_DeleteItem(s_tabhwnd, nr);
3081
3082
if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
3083
TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
3084
3085
// Re-enable redraw and redraw.
3086
SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
3087
RedrawWindow(s_tabhwnd, NULL, NULL,
3088
RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
3089
3090
if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
3091
TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
3092
}
3093
3094
/*
3095
* Set the current tab to "nr". First tab is 1.
3096
*/
3097
void
3098
gui_mch_set_curtab(int nr)
3099
{
3100
if (s_tabhwnd == NULL)
3101
return;
3102
3103
if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
3104
TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
3105
}
3106
3107
#endif
3108
3109
/*
3110
* ":simalt" command.
3111
*/
3112
void
3113
ex_simalt(exarg_T *eap)
3114
{
3115
char_u *keys = eap->arg;
3116
int fill_typebuf = FALSE;
3117
char_u key_name[4];
3118
3119
PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
3120
while (*keys)
3121
{
3122
if (*keys == '~')
3123
*keys = ' '; // for showing system menu
3124
PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
3125
keys++;
3126
fill_typebuf = TRUE;
3127
}
3128
if (fill_typebuf)
3129
{
3130
// Put a NOP in the typeahead buffer so that the message will get
3131
// processed.
3132
key_name[0] = K_SPECIAL;
3133
key_name[1] = KS_EXTRA;
3134
key_name[2] = KE_NOP;
3135
key_name[3] = NUL;
3136
#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3137
typebuf_was_filled = TRUE;
3138
#endif
3139
(void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
3140
}
3141
}
3142
3143
/*
3144
* Create the find & replace dialogs.
3145
* You can't have both at once: ":find" when replace is showing, destroys
3146
* the replace dialog first, and the other way around.
3147
*/
3148
#ifdef MSWIN_FIND_REPLACE
3149
static void
3150
initialise_findrep(char_u *initial_string)
3151
{
3152
int wword = FALSE;
3153
int mcase = !p_ic;
3154
char_u *entry_text;
3155
3156
// Get the search string to use.
3157
entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
3158
3159
s_findrep_struct.hwndOwner = s_hwnd;
3160
s_findrep_struct.Flags = FR_DOWN;
3161
if (mcase)
3162
s_findrep_struct.Flags |= FR_MATCHCASE;
3163
if (wword)
3164
s_findrep_struct.Flags |= FR_WHOLEWORD;
3165
if (entry_text != NULL && *entry_text != NUL)
3166
{
3167
WCHAR *p = enc_to_utf16(entry_text, NULL);
3168
if (p != NULL)
3169
{
3170
int len = s_findrep_struct.wFindWhatLen - 1;
3171
3172
wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
3173
s_findrep_struct.lpstrFindWhat[len] = NUL;
3174
vim_free(p);
3175
}
3176
}
3177
vim_free(entry_text);
3178
}
3179
#endif
3180
3181
static void
3182
set_window_title(HWND hwnd, char *title)
3183
{
3184
if (title != NULL)
3185
{
3186
WCHAR *wbuf;
3187
3188
// Convert the title from 'encoding' to UTF-16.
3189
wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
3190
if (wbuf != NULL)
3191
{
3192
SetWindowTextW(hwnd, wbuf);
3193
vim_free(wbuf);
3194
}
3195
}
3196
else
3197
(void)SetWindowTextW(hwnd, NULL);
3198
}
3199
3200
void
3201
gui_mch_find_dialog(exarg_T *eap)
3202
{
3203
#ifdef MSWIN_FIND_REPLACE
3204
if (s_findrep_msg != 0)
3205
{
3206
if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
3207
DestroyWindow(s_findrep_hwnd);
3208
3209
if (!IsWindow(s_findrep_hwnd))
3210
{
3211
initialise_findrep(eap->arg);
3212
s_findrep_hwnd = FindTextW(&s_findrep_struct);
3213
}
3214
3215
set_window_title(s_findrep_hwnd, _("Find string"));
3216
(void)SetFocus(s_findrep_hwnd);
3217
3218
s_findrep_is_find = TRUE;
3219
}
3220
#endif
3221
}
3222
3223
3224
void
3225
gui_mch_replace_dialog(exarg_T *eap)
3226
{
3227
#ifdef MSWIN_FIND_REPLACE
3228
if (s_findrep_msg != 0)
3229
{
3230
if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
3231
DestroyWindow(s_findrep_hwnd);
3232
3233
if (!IsWindow(s_findrep_hwnd))
3234
{
3235
initialise_findrep(eap->arg);
3236
s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
3237
}
3238
3239
set_window_title(s_findrep_hwnd, _("Find & Replace"));
3240
(void)SetFocus(s_findrep_hwnd);
3241
3242
s_findrep_is_find = FALSE;
3243
}
3244
#endif
3245
}
3246
3247
3248
/*
3249
* Set visibility of the pointer.
3250
*/
3251
void
3252
gui_mch_mousehide(int hide)
3253
{
3254
if (hide == gui.pointer_hidden)
3255
return;
3256
3257
ShowCursor(!hide);
3258
gui.pointer_hidden = hide;
3259
}
3260
3261
#ifdef FEAT_MENU
3262
static void
3263
gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
3264
{
3265
// Unhide the mouse, we don't get move events here.
3266
gui_mch_mousehide(FALSE);
3267
3268
(void)TrackPopupMenu(
3269
(HMENU)menu->submenu_id,
3270
TPM_LEFTALIGN | TPM_LEFTBUTTON,
3271
x, y,
3272
(int)0, //reserved param
3273
s_hwnd,
3274
NULL);
3275
/*
3276
* NOTE: The pop-up menu can eat the mouse up event.
3277
* We deal with this in normal.c.
3278
*/
3279
}
3280
#endif
3281
3282
/*
3283
* Got a message when the system will go down.
3284
*/
3285
static void
3286
_OnEndSession(void)
3287
{
3288
getout_preserve_modified(1);
3289
}
3290
3291
/*
3292
* Get this message when the user clicks on the cross in the top right corner
3293
* of a Windows95 window.
3294
*/
3295
static void
3296
_OnClose(HWND hwnd UNUSED)
3297
{
3298
gui_shell_closed();
3299
}
3300
3301
/*
3302
* Get a message when the window is being destroyed.
3303
*/
3304
static void
3305
_OnDestroy(HWND hwnd)
3306
{
3307
if (!destroying)
3308
_OnClose(hwnd);
3309
}
3310
3311
static void
3312
_OnPaint(
3313
HWND hwnd)
3314
{
3315
if (IsMinimized(hwnd))
3316
return;
3317
3318
PAINTSTRUCT ps;
3319
3320
out_flush(); // make sure all output has been processed
3321
(void)BeginPaint(hwnd, &ps);
3322
3323
// prevent multi-byte characters from misprinting on an invalid
3324
// rectangle
3325
if (has_mbyte)
3326
{
3327
RECT rect;
3328
3329
GetClientRect(hwnd, &rect);
3330
ps.rcPaint.left = rect.left;
3331
ps.rcPaint.right = rect.right;
3332
}
3333
3334
if (!IsRectEmpty(&ps.rcPaint))
3335
{
3336
gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
3337
ps.rcPaint.right - ps.rcPaint.left + 1,
3338
ps.rcPaint.bottom - ps.rcPaint.top + 1);
3339
}
3340
3341
EndPaint(hwnd, &ps);
3342
}
3343
3344
static void
3345
_OnSize(
3346
HWND hwnd,
3347
UINT state UNUSED,
3348
int cx,
3349
int cy)
3350
{
3351
if (!IsMinimized(hwnd) && !s_in_dpichanged)
3352
{
3353
gui_resize_shell(cx, cy);
3354
3355
// Menu bar may wrap differently now
3356
gui_mswin_get_menu_height(TRUE);
3357
}
3358
}
3359
3360
static void
3361
_OnSetFocus(
3362
HWND hwnd,
3363
HWND hwndOldFocus)
3364
{
3365
gui_focus_change(TRUE);
3366
s_getting_focus = TRUE;
3367
(void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
3368
}
3369
3370
static void
3371
_OnKillFocus(
3372
HWND hwnd,
3373
HWND hwndNewFocus)
3374
{
3375
if (destroying)
3376
return;
3377
gui_focus_change(FALSE);
3378
s_getting_focus = FALSE;
3379
(void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
3380
}
3381
3382
/*
3383
* Get a message when the user switches back to vim
3384
*/
3385
static LRESULT
3386
_OnActivateApp(
3387
HWND hwnd,
3388
BOOL fActivate,
3389
DWORD dwThreadId)
3390
{
3391
// we call gui_focus_change() in _OnSetFocus()
3392
// gui_focus_change((int)fActivate);
3393
return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
3394
}
3395
3396
void
3397
gui_mch_destroy_scrollbar(scrollbar_T *sb)
3398
{
3399
DestroyWindow(sb->id);
3400
}
3401
3402
/*
3403
* Get current mouse coordinates in text window.
3404
*/
3405
void
3406
gui_mch_getmouse(int *x, int *y)
3407
{
3408
RECT rct;
3409
POINT mp;
3410
3411
(void)GetWindowRect(s_textArea, &rct);
3412
(void)GetCursorPos(&mp);
3413
*x = (int)(mp.x - rct.left);
3414
*y = (int)(mp.y - rct.top);
3415
}
3416
3417
/*
3418
* Move mouse pointer to character at (x, y).
3419
*/
3420
void
3421
gui_mch_setmouse(int x, int y)
3422
{
3423
RECT rct;
3424
3425
(void)GetWindowRect(s_textArea, &rct);
3426
(void)SetCursorPos(x + gui.border_offset + rct.left,
3427
y + gui.border_offset + rct.top);
3428
}
3429
3430
static void
3431
gui_mswin_get_valid_dimensions(
3432
int w,
3433
int h,
3434
int *valid_w,
3435
int *valid_h,
3436
int *cols,
3437
int *rows)
3438
{
3439
int base_width, base_height;
3440
3441
base_width = gui_get_base_width()
3442
+ (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3443
pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
3444
base_height = gui_get_base_height()
3445
+ (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3446
pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3447
+ pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3448
+ gui_mswin_get_menu_height(FALSE);
3449
*cols = (w - base_width) / gui.char_width;
3450
*rows = (h - base_height) / gui.char_height;
3451
*valid_w = base_width + *cols * gui.char_width;
3452
*valid_h = base_height + *rows * gui.char_height;
3453
}
3454
3455
void
3456
gui_mch_flash(int msec)
3457
{
3458
RECT rc;
3459
3460
#if defined(FEAT_DIRECTX)
3461
if (IS_ENABLE_DIRECTX())
3462
DWriteContext_Flush(s_dwc);
3463
#endif
3464
3465
/*
3466
* Note: InvertRect() excludes right and bottom of rectangle.
3467
*/
3468
rc.left = 0;
3469
rc.top = 0;
3470
rc.right = gui.num_cols * gui.char_width;
3471
rc.bottom = gui.num_rows * gui.char_height;
3472
InvertRect(s_hdc, &rc);
3473
gui_mch_flush(); // make sure it's displayed
3474
3475
ui_delay((long)msec, TRUE); // wait for a few msec
3476
3477
InvertRect(s_hdc, &rc);
3478
}
3479
3480
/*
3481
* Check if the specified point is on-screen. (multi-monitor aware)
3482
*/
3483
static BOOL
3484
is_point_onscreen(int x, int y)
3485
{
3486
POINT pt = {x, y};
3487
3488
return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3489
}
3490
3491
/*
3492
* Check if the whole client area of the specified window is on-screen.
3493
*
3494
* Note about DirectX: Windows 10 1809 or above no longer maintains image of
3495
* the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3496
* only works when the whole window is on-screen.
3497
*/
3498
static BOOL
3499
is_window_onscreen(HWND hwnd)
3500
{
3501
RECT rc;
3502
POINT p1, p2;
3503
3504
GetClientRect(hwnd, &rc);
3505
p1.x = rc.left;
3506
p1.y = rc.top;
3507
p2.x = rc.right - 1;
3508
p2.y = rc.bottom - 1;
3509
ClientToScreen(hwnd, &p1);
3510
ClientToScreen(hwnd, &p2);
3511
3512
if (!is_point_onscreen(p1.x, p1.y))
3513
return FALSE;
3514
if (!is_point_onscreen(p1.x, p2.y))
3515
return FALSE;
3516
if (!is_point_onscreen(p2.x, p1.y))
3517
return FALSE;
3518
if (!is_point_onscreen(p2.x, p2.y))
3519
return FALSE;
3520
return TRUE;
3521
}
3522
3523
/*
3524
* Return flags used for scrolling.
3525
* The SW_INVALIDATE is required when part of the window is covered or
3526
* off-screen. Refer to MS KB Q75236.
3527
*/
3528
static int
3529
get_scroll_flags(void)
3530
{
3531
HWND hwnd;
3532
RECT rcVim, rcOther, rcDest;
3533
3534
// Check if the window is (partly) off-screen.
3535
if (!is_window_onscreen(s_hwnd))
3536
return SW_INVALIDATE;
3537
3538
// Check if there is a window (partly) on top of us.
3539
GetWindowRect(s_hwnd, &rcVim);
3540
for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3541
if (IsWindowVisible(hwnd))
3542
{
3543
GetWindowRect(hwnd, &rcOther);
3544
if (IntersectRect(&rcDest, &rcVim, &rcOther))
3545
return SW_INVALIDATE;
3546
}
3547
return 0;
3548
}
3549
3550
/*
3551
* On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3552
* may not be scrolled out properly.
3553
* For gVim, when _OnScroll() is repeated, the character at the
3554
* previous cursor position may be left drawn after scroll.
3555
* The problem can be avoided by calling GetPixel() to get a pixel in
3556
* the region before ScrollWindowEx().
3557
*/
3558
static void
3559
intel_gpu_workaround(void)
3560
{
3561
GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3562
}
3563
3564
/*
3565
* Delete the given number of lines from the given row, scrolling up any
3566
* text further down within the scroll region.
3567
*/
3568
void
3569
gui_mch_delete_lines(
3570
int row,
3571
int num_lines)
3572
{
3573
RECT rc;
3574
3575
rc.left = FILL_X(gui.scroll_region_left);
3576
rc.right = FILL_X(gui.scroll_region_right + 1);
3577
rc.top = FILL_Y(row);
3578
rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3579
3580
#if defined(FEAT_DIRECTX)
3581
if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
3582
{
3583
DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
3584
}
3585
else
3586
#endif
3587
{
3588
#if defined(FEAT_DIRECTX)
3589
if (IS_ENABLE_DIRECTX())
3590
DWriteContext_Flush(s_dwc);
3591
#endif
3592
intel_gpu_workaround();
3593
ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
3594
&rc, &rc, NULL, NULL, get_scroll_flags());
3595
UpdateWindow(s_textArea);
3596
}
3597
3598
// This seems to be required to avoid the cursor disappearing when
3599
// scrolling such that the cursor ends up in the top-left character on
3600
// the screen... But why? (Webb)
3601
// It's probably fixed by disabling drawing the cursor while scrolling.
3602
// gui.cursor_is_valid = FALSE;
3603
3604
gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3605
gui.scroll_region_left,
3606
gui.scroll_region_bot, gui.scroll_region_right);
3607
}
3608
3609
/*
3610
* Insert the given number of lines before the given row, scrolling down any
3611
* following text within the scroll region.
3612
*/
3613
void
3614
gui_mch_insert_lines(
3615
int row,
3616
int num_lines)
3617
{
3618
RECT rc;
3619
3620
rc.left = FILL_X(gui.scroll_region_left);
3621
rc.right = FILL_X(gui.scroll_region_right + 1);
3622
rc.top = FILL_Y(row);
3623
rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3624
3625
#if defined(FEAT_DIRECTX)
3626
if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
3627
{
3628
DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
3629
}
3630
else
3631
#endif
3632
{
3633
#if defined(FEAT_DIRECTX)
3634
if (IS_ENABLE_DIRECTX())
3635
DWriteContext_Flush(s_dwc);
3636
#endif
3637
intel_gpu_workaround();
3638
// The SW_INVALIDATE is required when part of the window is covered or
3639
// off-screen. How do we avoid it when it's not needed?
3640
ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
3641
&rc, &rc, NULL, NULL, get_scroll_flags());
3642
UpdateWindow(s_textArea);
3643
}
3644
3645
gui_clear_block(row, gui.scroll_region_left,
3646
row + num_lines - 1, gui.scroll_region_right);
3647
}
3648
3649
3650
void
3651
gui_mch_exit(int rc UNUSED)
3652
{
3653
#if defined(FEAT_DIRECTX)
3654
DWriteContext_Close(s_dwc);
3655
DWrite_Final();
3656
s_dwc = NULL;
3657
#endif
3658
3659
ReleaseDC(s_textArea, s_hdc);
3660
DeleteObject(s_brush);
3661
3662
#ifdef FEAT_TEAROFF
3663
// Unload the tearoff bitmap
3664
(void)DeleteObject((HGDIOBJ)s_htearbitmap);
3665
#endif
3666
3667
// Destroy our window (if we have one).
3668
if (s_hwnd != NULL)
3669
{
3670
destroying = TRUE; // ignore WM_DESTROY message now
3671
DestroyWindow(s_hwnd);
3672
}
3673
}
3674
3675
static char_u *
3676
logfont2name(LOGFONTW lf)
3677
{
3678
char *charset_name;
3679
char *quality_name;
3680
char *font_name;
3681
size_t res_size;
3682
char *res;
3683
3684
font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3685
if (font_name == NULL)
3686
return NULL;
3687
charset_name = charset_id2name((int)lf.lfCharSet);
3688
quality_name = quality_id2name((int)lf.lfQuality);
3689
3690
res_size = STRLEN(font_name) + 30
3691
+ (charset_name == NULL ? 0 : STRLEN(charset_name) + 2)
3692
+ (quality_name == NULL ? 0 : STRLEN(quality_name) + 2);
3693
res = alloc(res_size);
3694
if (res != NULL)
3695
{
3696
char *p;
3697
int points;
3698
size_t res_len;
3699
3700
// replace spaces in font_name with underscores.
3701
for (p = font_name; *p != NUL; ++p)
3702
{
3703
if (isspace(*p))
3704
*p = '_';
3705
}
3706
3707
// make a normal font string out of the lf thing:
3708
points = pixels_to_points(
3709
lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3710
if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3711
res_len = vim_snprintf_safelen(
3712
(char *)res, res_size, "%s:h%d", font_name, points);
3713
else
3714
res_len = vim_snprintf_safelen(
3715
(char *)res, res_size, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
3716
3717
res_len += vim_snprintf_safelen(
3718
(char *)res + res_len,
3719
res_size - res_len,
3720
"%s%s%s%s",
3721
lf.lfItalic ? ":i" : "",
3722
lf.lfWeight ? ":b" : "",
3723
lf.lfUnderline ? ":u" : "",
3724
lf.lfStrikeOut ? ":s" : "");
3725
3726
if (charset_name != NULL)
3727
res_len += vim_snprintf_safelen((char *)res + res_len,
3728
res_size - res_len, ":c%s", charset_name);
3729
if (quality_name != NULL)
3730
vim_snprintf((char *)res + res_len,
3731
res_size - res_len, ":q%s", quality_name);
3732
}
3733
3734
vim_free(font_name);
3735
return (char_u *)res;
3736
}
3737
3738
3739
#ifdef FEAT_MBYTE_IME
3740
/*
3741
* Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
3742
* 'guifont'.
3743
*/
3744
static void
3745
update_im_font(void)
3746
{
3747
LOGFONTW lf_wide, lf;
3748
3749
if (p_guifontwide != NULL && *p_guifontwide != NUL
3750
&& gui.wide_font != NOFONT
3751
&& GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
3752
norm_logfont = lf_wide;
3753
else
3754
norm_logfont = sub_logfont;
3755
3756
lf = norm_logfont;
3757
if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3758
// Work around when PerMonitorV2 is not enabled in the process level.
3759
lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3760
im_set_font(&lf);
3761
}
3762
#endif
3763
3764
/*
3765
* Handler of gui.wide_font (p_guifontwide) changed notification.
3766
*/
3767
void
3768
gui_mch_wide_font_changed(void)
3769
{
3770
LOGFONTW lf;
3771
3772
#ifdef FEAT_MBYTE_IME
3773
update_im_font();
3774
#endif
3775
3776
gui_mch_free_font(gui.wide_ital_font);
3777
gui.wide_ital_font = NOFONT;
3778
gui_mch_free_font(gui.wide_bold_font);
3779
gui.wide_bold_font = NOFONT;
3780
gui_mch_free_font(gui.wide_boldital_font);
3781
gui.wide_boldital_font = NOFONT;
3782
3783
if (gui.wide_font
3784
&& GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
3785
{
3786
if (!lf.lfItalic)
3787
{
3788
lf.lfItalic = TRUE;
3789
gui.wide_ital_font = get_font_handle(&lf);
3790
lf.lfItalic = FALSE;
3791
}
3792
if (lf.lfWeight < FW_BOLD)
3793
{
3794
lf.lfWeight = FW_BOLD;
3795
gui.wide_bold_font = get_font_handle(&lf);
3796
if (!lf.lfItalic)
3797
{
3798
lf.lfItalic = TRUE;
3799
gui.wide_boldital_font = get_font_handle(&lf);
3800
}
3801
}
3802
}
3803
}
3804
3805
/*
3806
* Initialise vim to use the font with the given name.
3807
* Return FAIL if the font could not be loaded, OK otherwise.
3808
*/
3809
int
3810
gui_mch_init_font(char_u *font_name, int fontset UNUSED)
3811
{
3812
LOGFONTW lf, lfOrig;
3813
GuiFont font = NOFONT;
3814
char_u *p;
3815
3816
// Load the font
3817
if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3818
{
3819
lfOrig = lf;
3820
lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
3821
font = get_font_handle(&lf);
3822
}
3823
if (font == NOFONT)
3824
return FAIL;
3825
3826
if (font_name == NULL)
3827
font_name = (char_u *)"";
3828
#ifdef FEAT_MBYTE_IME
3829
norm_logfont = lf;
3830
sub_logfont = lf;
3831
if (!s_in_dpichanged)
3832
update_im_font();
3833
#endif
3834
gui_mch_free_font(gui.norm_font);
3835
gui.norm_font = font;
3836
current_font_height = lfOrig.lfHeight;
3837
UpdateFontSize(font);
3838
3839
p = logfont2name(lfOrig);
3840
if (p != NULL)
3841
{
3842
hl_set_font_name(p);
3843
3844
// When setting 'guifont' to "*" replace it with the actual font name.
3845
if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3846
{
3847
vim_free(p_guifont);
3848
p_guifont = p;
3849
}
3850
else
3851
vim_free(p);
3852
}
3853
3854
gui_mch_free_font(gui.ital_font);
3855
gui.ital_font = NOFONT;
3856
gui_mch_free_font(gui.bold_font);
3857
gui.bold_font = NOFONT;
3858
gui_mch_free_font(gui.boldital_font);
3859
gui.boldital_font = NOFONT;
3860
3861
if (!lf.lfItalic)
3862
{
3863
lf.lfItalic = TRUE;
3864
gui.ital_font = get_font_handle(&lf);
3865
lf.lfItalic = FALSE;
3866
}
3867
if (lf.lfWeight < FW_BOLD)
3868
{
3869
lf.lfWeight = FW_BOLD;
3870
gui.bold_font = get_font_handle(&lf);
3871
if (!lf.lfItalic)
3872
{
3873
lf.lfItalic = TRUE;
3874
gui.boldital_font = get_font_handle(&lf);
3875
}
3876
}
3877
3878
return OK;
3879
}
3880
3881
/*
3882
* Return TRUE if the GUI window is maximized, filling the whole screen.
3883
* Also return TRUE if the window is snapped.
3884
*/
3885
int
3886
gui_mch_maximized(void)
3887
{
3888
WINDOWPLACEMENT wp;
3889
RECT rc;
3890
3891
wp.length = sizeof(WINDOWPLACEMENT);
3892
if (GetWindowPlacement(s_hwnd, &wp))
3893
{
3894
if (wp.showCmd == SW_SHOWMAXIMIZED
3895
|| (wp.showCmd == SW_SHOWMINIMIZED
3896
&& wp.flags == WPF_RESTORETOMAXIMIZED))
3897
return TRUE;
3898
if (wp.showCmd == SW_SHOWMINIMIZED)
3899
return FALSE;
3900
3901
// Assume the window is snapped when the sizes from two APIs differ.
3902
GetWindowRect(s_hwnd, &rc);
3903
if ((rc.right - rc.left !=
3904
wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3905
|| (rc.bottom - rc.top !=
3906
wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3907
return TRUE;
3908
}
3909
return FALSE;
3910
}
3911
3912
/*
3913
* Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3914
* is set. Compute the new Rows and Columns. This is like resizing the
3915
* window.
3916
*/
3917
void
3918
gui_mch_newfont(void)
3919
{
3920
RECT rect;
3921
3922
GetWindowRect(s_hwnd, &rect);
3923
if (win_socket_id == 0)
3924
{
3925
gui_resize_shell(rect.right - rect.left
3926
- (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3927
pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
3928
rect.bottom - rect.top
3929
- (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3930
pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3931
- pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3932
- gui_mswin_get_menu_height(FALSE));
3933
}
3934
else
3935
{
3936
// Inside another window, don't use the frame and border.
3937
gui_resize_shell(rect.right - rect.left,
3938
rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
3939
}
3940
}
3941
3942
/*
3943
* Set the window title
3944
*/
3945
void
3946
gui_mch_settitle(
3947
char_u *title,
3948
char_u *icon UNUSED)
3949
{
3950
set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3951
}
3952
3953
#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3954
// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3955
// misc2.c!
3956
static LPCSTR mshape_idcs[] =
3957
{
3958
IDC_ARROW, // arrow
3959
MAKEINTRESOURCE(0), // blank
3960
IDC_IBEAM, // beam
3961
IDC_SIZENS, // updown
3962
IDC_SIZENS, // udsizing
3963
IDC_SIZEWE, // leftright
3964
IDC_SIZEWE, // lrsizing
3965
IDC_WAIT, // busy
3966
IDC_NO, // no
3967
IDC_ARROW, // crosshair
3968
IDC_ARROW, // hand1
3969
IDC_ARROW, // hand2
3970
IDC_ARROW, // pencil
3971
IDC_ARROW, // question
3972
IDC_ARROW, // right-arrow
3973
IDC_UPARROW, // up-arrow
3974
IDC_ARROW // last one
3975
};
3976
3977
void
3978
mch_set_mouse_shape(int shape)
3979
{
3980
LPCSTR idc;
3981
3982
if (shape == MSHAPE_HIDE)
3983
ShowCursor(FALSE);
3984
else
3985
{
3986
if (shape >= MSHAPE_NUMBERED)
3987
idc = IDC_ARROW;
3988
else
3989
idc = mshape_idcs[shape];
3990
SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
3991
if (!p_mh)
3992
{
3993
POINT mp;
3994
3995
// Set the position to make it redrawn with the new shape.
3996
(void)GetCursorPos(&mp);
3997
(void)SetCursorPos(mp.x, mp.y);
3998
ShowCursor(TRUE);
3999
}
4000
}
4001
}
4002
#endif
4003
4004
#if defined(FEAT_BROWSE) || defined(PROTO)
4005
/*
4006
* Wide version of convert_filter().
4007
*/
4008
static WCHAR *
4009
convert_filterW(char_u *s)
4010
{
4011
char_u *tmp;
4012
int len;
4013
WCHAR *res;
4014
4015
tmp = convert_filter(s);
4016
if (tmp == NULL)
4017
return NULL;
4018
len = (int)STRLEN(s) + 3;
4019
res = enc_to_utf16(tmp, &len);
4020
vim_free(tmp);
4021
return res;
4022
}
4023
4024
/*
4025
* Pop open a file browser and return the file selected, in allocated memory,
4026
* or NULL if Cancel is hit.
4027
* saving - TRUE if the file will be saved to, FALSE if it will be opened.
4028
* title - Title message for the file browser dialog.
4029
* dflt - Default name of file.
4030
* ext - Default extension to be added to files without extensions.
4031
* initdir - directory in which to open the browser (NULL = current dir)
4032
* filter - Filter for matched files to choose from.
4033
*/
4034
char_u *
4035
gui_mch_browse(
4036
int saving,
4037
char_u *title,
4038
char_u *dflt,
4039
char_u *ext,
4040
char_u *initdir,
4041
char_u *filter)
4042
{
4043
// We always use the wide function. This means enc_to_utf16() must work,
4044
// otherwise it fails miserably!
4045
OPENFILENAMEW fileStruct;
4046
WCHAR fileBuf[MAXPATHL];
4047
WCHAR *wp;
4048
int i;
4049
WCHAR *titlep = NULL;
4050
WCHAR *extp = NULL;
4051
WCHAR *initdirp = NULL;
4052
WCHAR *filterp;
4053
char_u *p, *q;
4054
BOOL ret;
4055
4056
if (dflt == NULL)
4057
fileBuf[0] = NUL;
4058
else
4059
{
4060
wp = enc_to_utf16(dflt, NULL);
4061
if (wp == NULL)
4062
fileBuf[0] = NUL;
4063
else
4064
{
4065
for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
4066
fileBuf[i] = wp[i];
4067
fileBuf[i] = NUL;
4068
vim_free(wp);
4069
}
4070
}
4071
4072
// Convert the filter to Windows format.
4073
filterp = convert_filterW(filter);
4074
4075
CLEAR_FIELD(fileStruct);
4076
# ifdef OPENFILENAME_SIZE_VERSION_400W
4077
// be compatible with Windows NT 4.0
4078
fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
4079
# else
4080
fileStruct.lStructSize = sizeof(fileStruct);
4081
# endif
4082
4083
if (title != NULL)
4084
titlep = enc_to_utf16(title, NULL);
4085
fileStruct.lpstrTitle = titlep;
4086
4087
if (ext != NULL)
4088
extp = enc_to_utf16(ext, NULL);
4089
fileStruct.lpstrDefExt = extp;
4090
4091
fileStruct.lpstrFile = fileBuf;
4092
fileStruct.nMaxFile = MAXPATHL;
4093
fileStruct.lpstrFilter = filterp;
4094
fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
4095
// has an initial dir been specified?
4096
if (initdir != NULL && *initdir != NUL)
4097
{
4098
// Must have backslashes here, no matter what 'shellslash' says
4099
initdirp = enc_to_utf16(initdir, NULL);
4100
if (initdirp != NULL)
4101
{
4102
for (wp = initdirp; *wp != NUL; ++wp)
4103
if (*wp == '/')
4104
*wp = '\\';
4105
}
4106
fileStruct.lpstrInitialDir = initdirp;
4107
}
4108
4109
/*
4110
* TODO: Allow selection of multiple files. Needs another arg to this
4111
* function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
4112
* Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
4113
* files that don't exist yet, so I haven't put it in. What about
4114
* OFN_PATHMUSTEXIST?
4115
* Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
4116
*/
4117
fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
4118
# ifdef FEAT_SHORTCUT
4119
if (curbuf->b_p_bin)
4120
fileStruct.Flags |= OFN_NODEREFERENCELINKS;
4121
# endif
4122
if (saving)
4123
ret = GetSaveFileNameW(&fileStruct);
4124
else
4125
ret = GetOpenFileNameW(&fileStruct);
4126
4127
vim_free(filterp);
4128
vim_free(initdirp);
4129
vim_free(titlep);
4130
vim_free(extp);
4131
4132
if (!ret)
4133
return NULL;
4134
4135
// Convert from UTF-16 to 'encoding'.
4136
p = utf16_to_enc(fileBuf, NULL);
4137
if (p == NULL)
4138
return NULL;
4139
4140
// Give focus back to main window (when using MDI).
4141
SetFocus(s_hwnd);
4142
4143
// Shorten the file name if possible
4144
q = vim_strsave(shorten_fname1(p));
4145
vim_free(p);
4146
return q;
4147
}
4148
4149
4150
/*
4151
* Convert the string s to the proper format for a filter string by replacing
4152
* the \t and \n delimiters with \0.
4153
* Returns the converted string in allocated memory.
4154
*
4155
* Keep in sync with convert_filterW() above!
4156
*/
4157
static char_u *
4158
convert_filter(char_u *s)
4159
{
4160
char_u *res;
4161
unsigned s_len = (unsigned)STRLEN(s);
4162
unsigned i;
4163
4164
res = alloc(s_len + 3);
4165
if (res != NULL)
4166
{
4167
for (i = 0; i < s_len; ++i)
4168
if (s[i] == '\t' || s[i] == '\n')
4169
res[i] = '\0';
4170
else
4171
res[i] = s[i];
4172
res[s_len] = NUL;
4173
// Add two extra NULs to make sure it's properly terminated.
4174
res[s_len + 1] = NUL;
4175
res[s_len + 2] = NUL;
4176
}
4177
return res;
4178
}
4179
4180
/*
4181
* Select a directory.
4182
*/
4183
char_u *
4184
gui_mch_browsedir(char_u *title, char_u *initdir)
4185
{
4186
// We fake this: Use a filter that doesn't select anything and a default
4187
// file name that won't be used.
4188
return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
4189
initdir, (char_u *)_("Directory\t*.nothing\n"));
4190
}
4191
#endif // FEAT_BROWSE
4192
4193
static void
4194
_OnDropFiles(
4195
HWND hwnd UNUSED,
4196
HDROP hDrop)
4197
{
4198
#define BUFPATHLEN _MAX_PATH
4199
#define DRAGQVAL 0xFFFFFFFF
4200
WCHAR wszFile[BUFPATHLEN];
4201
char szFile[BUFPATHLEN];
4202
UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
4203
UINT i;
4204
char_u **fnames;
4205
POINT pt;
4206
int_u modifiers = 0;
4207
4208
// Obtain dropped position
4209
DragQueryPoint(hDrop, &pt);
4210
MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
4211
4212
reset_VIsual();
4213
4214
fnames = ALLOC_MULT(char_u *, cFiles);
4215
4216
if (fnames != NULL)
4217
for (i = 0; i < cFiles; ++i)
4218
{
4219
if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
4220
fnames[i] = utf16_to_enc(wszFile, NULL);
4221
else
4222
{
4223
DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
4224
fnames[i] = vim_strsave((char_u *)szFile);
4225
}
4226
}
4227
4228
DragFinish(hDrop);
4229
4230
if (fnames == NULL)
4231
return;
4232
4233
int kbd_modifiers = get_active_modifiers();
4234
4235
if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4236
modifiers |= MOUSE_SHIFT;
4237
if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4238
modifiers |= MOUSE_CTRL;
4239
if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4240
modifiers |= MOUSE_ALT;
4241
4242
gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
4243
4244
s_need_activate = TRUE;
4245
}
4246
4247
static int
4248
_OnScroll(
4249
HWND hwnd UNUSED,
4250
HWND hwndCtl,
4251
UINT code,
4252
int pos)
4253
{
4254
static UINT prev_code = 0; // code of previous call
4255
scrollbar_T *sb, *sb_info;
4256
long val;
4257
int dragging = FALSE;
4258
int dont_scroll_save = dont_scroll;
4259
SCROLLINFO si;
4260
4261
si.cbSize = sizeof(si);
4262
si.fMask = SIF_POS;
4263
4264
sb = gui_mswin_find_scrollbar(hwndCtl);
4265
if (sb == NULL)
4266
return 0;
4267
4268
if (sb->wp != NULL) // Left or right scrollbar
4269
{
4270
/*
4271
* Careful: need to get scrollbar info out of first (left) scrollbar
4272
* for window, but keep real scrollbar too because we must pass it to
4273
* gui_drag_scrollbar().
4274
*/
4275
sb_info = &sb->wp->w_scrollbars[0];
4276
}
4277
else // Bottom scrollbar
4278
sb_info = sb;
4279
val = sb_info->value;
4280
4281
switch (code)
4282
{
4283
case SB_THUMBTRACK:
4284
val = pos;
4285
dragging = TRUE;
4286
if (sb->scroll_shift > 0)
4287
val <<= sb->scroll_shift;
4288
break;
4289
case SB_LINEDOWN:
4290
val++;
4291
break;
4292
case SB_LINEUP:
4293
val--;
4294
break;
4295
case SB_PAGEDOWN:
4296
val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
4297
break;
4298
case SB_PAGEUP:
4299
val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
4300
break;
4301
case SB_TOP:
4302
val = 0;
4303
break;
4304
case SB_BOTTOM:
4305
val = sb_info->max;
4306
break;
4307
case SB_ENDSCROLL:
4308
if (prev_code == SB_THUMBTRACK)
4309
{
4310
/*
4311
* "pos" only gives us 16-bit data. In case of large file,
4312
* use GetScrollPos() which returns 32-bit. Unfortunately it
4313
* is not valid while the scrollbar is being dragged.
4314
*/
4315
val = GetScrollPos(hwndCtl, SB_CTL);
4316
if (sb->scroll_shift > 0)
4317
val <<= sb->scroll_shift;
4318
}
4319
break;
4320
4321
default:
4322
// TRACE("Unknown scrollbar event %d\n", code);
4323
return 0;
4324
}
4325
prev_code = code;
4326
4327
si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
4328
SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
4329
4330
/*
4331
* When moving a vertical scrollbar, move the other vertical scrollbar too.
4332
*/
4333
if (sb->wp != NULL)
4334
{
4335
scrollbar_T *sba = sb->wp->w_scrollbars;
4336
HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
4337
4338
SetScrollInfo(id, SB_CTL, &si, TRUE);
4339
}
4340
4341
// Don't let us be interrupted here by another message.
4342
s_busy_processing = TRUE;
4343
4344
// When "allow_scrollbar" is FALSE still need to remember the new
4345
// position, but don't actually scroll by setting "dont_scroll".
4346
dont_scroll = !allow_scrollbar;
4347
4348
mch_disable_flush();
4349
gui_drag_scrollbar(sb, val, dragging);
4350
mch_enable_flush();
4351
gui_may_flush();
4352
4353
s_busy_processing = FALSE;
4354
dont_scroll = dont_scroll_save;
4355
4356
return 0;
4357
}
4358
4359
4360
#ifdef FEAT_XPM_W32
4361
# include "xpm_w32.h"
4362
#endif
4363
4364
4365
// Some parameters for tearoff menus. All in pixels.
4366
#define TEAROFF_PADDING_X 2
4367
#define TEAROFF_BUTTON_PAD_X 8
4368
#define TEAROFF_MIN_WIDTH 200
4369
#define TEAROFF_SUBMENU_LABEL ">>"
4370
#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4371
4372
4373
#ifdef FEAT_BEVAL_GUI
4374
# define ID_BEVAL_TOOLTIP 200
4375
# define BEVAL_TEXT_LEN MAXPATHL
4376
4377
static BalloonEval *cur_beval = NULL;
4378
static UINT_PTR beval_timer_id = 0;
4379
static DWORD last_user_activity = 0;
4380
#endif // defined(FEAT_BEVAL_GUI)
4381
4382
4383
// Local variables:
4384
4385
#ifdef FEAT_MENU
4386
static UINT s_menu_id = 100;
4387
#endif
4388
4389
/*
4390
* Use the system font for dialogs and tear-off menus. Remove this line to
4391
* use DLG_FONT_NAME.
4392
*/
4393
#define USE_SYSMENU_FONT
4394
4395
#define VIM_NAME "vim"
4396
#define VIM_CLASSW L"Vim"
4397
4398
// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4399
// thus there should be room for every dialog. For tearoffs it's made bigger
4400
// when needed.
4401
#define DLG_ALLOC_SIZE 16 * 1024
4402
4403
/*
4404
* stuff for dialogs, menus, tearoffs etc.
4405
*/
4406
static PWORD
4407
add_dialog_element(
4408
PWORD p,
4409
DWORD lStyle,
4410
WORD x,
4411
WORD y,
4412
WORD w,
4413
WORD h,
4414
WORD Id,
4415
WORD clss,
4416
const char *caption);
4417
static LPWORD lpwAlign(LPWORD);
4418
static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
4419
#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
4420
static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
4421
#endif
4422
static void get_dialog_font_metrics(void);
4423
4424
static int dialog_default_button = -1;
4425
4426
#ifdef FEAT_TOOLBAR
4427
static void initialise_toolbar(void);
4428
static void update_toolbar_size(void);
4429
static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
4430
static int get_toolbar_bitmap(vimmenu_T *menu);
4431
#else
4432
# define update_toolbar_size()
4433
#endif
4434
4435
#ifdef FEAT_GUI_TABLINE
4436
static void initialise_tabline(void);
4437
static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
4438
#endif
4439
4440
#ifdef FEAT_MBYTE_IME
4441
static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4442
static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4443
#endif
4444
#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4445
# ifdef NOIME
4446
typedef struct tagCOMPOSITIONFORM {
4447
DWORD dwStyle;
4448
POINT ptCurrentPos;
4449
RECT rcArea;
4450
} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4451
typedef HANDLE HIMC;
4452
# endif
4453
4454
static HINSTANCE hLibImm = NULL;
4455
static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4456
static HIMC (WINAPI *pImmGetContext)(HWND);
4457
static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4458
static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4459
static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4460
static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
4461
static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4462
static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
4463
static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4464
static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
4465
static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
4466
static void dyn_imm_load(void);
4467
#else
4468
# define pImmGetCompositionStringW ImmGetCompositionStringW
4469
# define pImmGetContext ImmGetContext
4470
# define pImmAssociateContext ImmAssociateContext
4471
# define pImmReleaseContext ImmReleaseContext
4472
# define pImmGetOpenStatus ImmGetOpenStatus
4473
# define pImmSetOpenStatus ImmSetOpenStatus
4474
# define pImmGetCompositionFontW ImmGetCompositionFontW
4475
# define pImmSetCompositionFontW ImmSetCompositionFontW
4476
# define pImmSetCompositionWindow ImmSetCompositionWindow
4477
# define pImmGetConversionStatus ImmGetConversionStatus
4478
# define pImmSetConversionStatus ImmSetConversionStatus
4479
#endif
4480
4481
#ifdef FEAT_MENU
4482
/*
4483
* Figure out how high the menu bar is at the moment.
4484
*/
4485
static int
4486
gui_mswin_get_menu_height(
4487
int fix_window) // If TRUE, resize window if menu height changed
4488
{
4489
static int old_menu_height = -1;
4490
4491
RECT rc1, rc2;
4492
int num;
4493
int menu_height;
4494
4495
if (gui.menu_is_active)
4496
num = GetMenuItemCount(s_menuBar);
4497
else
4498
num = 0;
4499
4500
if (num == 0)
4501
menu_height = 0;
4502
else if (IsMinimized(s_hwnd))
4503
{
4504
// The height of the menu cannot be determined while the window is
4505
// minimized. Take the previous height if the menu is changed in that
4506
// state, to avoid that Vim's vertical window size accidentally
4507
// increases due to the unaccounted-for menu height.
4508
menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4509
}
4510
else
4511
{
4512
/*
4513
* In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4514
* seem to have been set yet, so menu wraps in default window
4515
* width which is very narrow. Instead just return height of a
4516
* single menu item. Will still be wrong when the menu really
4517
* should wrap over more than one line.
4518
*/
4519
GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4520
if (gui.starting)
4521
menu_height = rc1.bottom - rc1.top + 1;
4522
else
4523
{
4524
GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4525
menu_height = rc2.bottom - rc1.top + 1;
4526
}
4527
}
4528
4529
if (fix_window && menu_height != old_menu_height)
4530
gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
4531
old_menu_height = menu_height;
4532
4533
return menu_height;
4534
}
4535
#endif // FEAT_MENU
4536
4537
4538
/*
4539
* Setup for the Intellimouse
4540
*/
4541
static long
4542
mouse_vertical_scroll_step(void)
4543
{
4544
UINT val;
4545
if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &val, 0))
4546
return (val != WHEEL_PAGESCROLL) ? (long)val : -1;
4547
return 3; // Safe default;
4548
}
4549
4550
static long
4551
mouse_horizontal_scroll_step(void)
4552
{
4553
UINT val;
4554
if (SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &val, 0))
4555
return (long)val;
4556
return 3; // Safe default;
4557
}
4558
4559
static void
4560
init_mouse_wheel(void)
4561
{
4562
// Get the default values for the horizontal and vertical scroll steps from
4563
// the system.
4564
mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
4565
mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
4566
}
4567
4568
/*
4569
* Mouse scroll event handler.
4570
*/
4571
static void
4572
_OnMouseWheel(HWND hwnd UNUSED, WPARAM wParam, LPARAM lParam, int horizontal)
4573
{
4574
int button;
4575
win_T *wp;
4576
int modifiers = 0;
4577
int kbd_modifiers;
4578
int zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4579
POINT pt;
4580
4581
wp = gui_mouse_window(FIND_POPUP);
4582
4583
#ifdef FEAT_PROP_POPUP
4584
if (wp != NULL && popup_is_popup(wp))
4585
{
4586
cmdarg_T cap;
4587
oparg_T oa;
4588
4589
// Mouse hovers over popup window, scroll it if possible.
4590
mouse_row = wp->w_winrow;
4591
mouse_col = wp->w_wincol;
4592
CLEAR_FIELD(cap);
4593
if (horizontal)
4594
{
4595
cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
4596
cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
4597
}
4598
else
4599
{
4600
cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4601
cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4602
}
4603
clear_oparg(&oa);
4604
cap.oap = &oa;
4605
nv_mousescroll(&cap);
4606
update_screen(0);
4607
setcursor();
4608
out_flush();
4609
return;
4610
}
4611
#endif
4612
4613
if (wp == NULL || !p_scf)
4614
wp = curwin;
4615
4616
// Translate the scroll event into an event that Vim can process so that
4617
// the user has a chance to map the scrollwheel buttons.
4618
if (horizontal)
4619
button = zDelta >= 0 ? MOUSE_6 : MOUSE_7;
4620
else
4621
button = zDelta >= 0 ? MOUSE_4 : MOUSE_5;
4622
4623
kbd_modifiers = get_active_modifiers();
4624
4625
if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4626
modifiers |= MOUSE_SHIFT;
4627
if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4628
modifiers |= MOUSE_CTRL;
4629
if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4630
modifiers |= MOUSE_ALT;
4631
4632
// The cursor position is relative to the upper-left corner of the screen.
4633
pt.x = GET_X_LPARAM(lParam);
4634
pt.y = GET_Y_LPARAM(lParam);
4635
ScreenToClient(s_textArea, &pt);
4636
4637
gui_send_mouse_event(button, pt.x, pt.y, FALSE, modifiers);
4638
}
4639
4640
#ifdef USE_SYSMENU_FONT
4641
/*
4642
* Get Menu Font.
4643
* Return OK or FAIL.
4644
*/
4645
static int
4646
gui_w32_get_menu_font(LOGFONTW *lf)
4647
{
4648
NONCLIENTMETRICSW nm;
4649
4650
nm.cbSize = sizeof(NONCLIENTMETRICSW);
4651
if (!SystemParametersInfoW(
4652
SPI_GETNONCLIENTMETRICS,
4653
sizeof(NONCLIENTMETRICSW),
4654
&nm,
4655
0))
4656
return FAIL;
4657
*lf = nm.lfMenuFont;
4658
return OK;
4659
}
4660
#endif
4661
4662
4663
#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4664
/*
4665
* Set the GUI tabline font to the system menu font
4666
*/
4667
static void
4668
set_tabline_font(void)
4669
{
4670
LOGFONTW lfSysmenu;
4671
HFONT font;
4672
HWND hwnd;
4673
HDC hdc;
4674
HFONT hfntOld;
4675
TEXTMETRIC tm;
4676
4677
if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4678
return;
4679
4680
lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
4681
font = CreateFontIndirectW(&lfSysmenu);
4682
4683
SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4684
4685
/*
4686
* Compute the height of the font used for the tab text
4687
*/
4688
hwnd = GetDesktopWindow();
4689
hdc = GetWindowDC(hwnd);
4690
hfntOld = SelectFont(hdc, font);
4691
4692
GetTextMetrics(hdc, &tm);
4693
4694
SelectFont(hdc, hfntOld);
4695
ReleaseDC(hwnd, hdc);
4696
4697
/*
4698
* The space used by the tab border and the space between the tab label
4699
* and the tab border is included as 7.
4700
*/
4701
gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4702
}
4703
#else
4704
# define set_tabline_font()
4705
#endif
4706
4707
/*
4708
* Invoked when a setting was changed.
4709
*/
4710
static LRESULT CALLBACK
4711
_OnSettingChange(UINT param)
4712
{
4713
switch (param)
4714
{
4715
case SPI_SETWHEELSCROLLLINES:
4716
mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
4717
break;
4718
case SPI_SETWHEELSCROLLCHARS:
4719
mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
4720
break;
4721
case SPI_SETNONCLIENTMETRICS:
4722
set_tabline_font();
4723
break;
4724
default:
4725
break;
4726
}
4727
return 0;
4728
}
4729
4730
#ifdef FEAT_NETBEANS_INTG
4731
static void
4732
_OnWindowPosChanged(
4733
HWND hwnd,
4734
const LPWINDOWPOS lpwpos)
4735
{
4736
static int x = 0, y = 0, cx = 0, cy = 0;
4737
extern int WSInitialized;
4738
4739
if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4740
|| lpwpos->cx != cx || lpwpos->cy != cy))
4741
{
4742
x = lpwpos->x;
4743
y = lpwpos->y;
4744
cx = lpwpos->cx;
4745
cy = lpwpos->cy;
4746
netbeans_frame_moved(x, y);
4747
}
4748
// Allow to send WM_SIZE and WM_MOVE
4749
FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
4750
}
4751
#endif
4752
4753
4754
static HWND hwndTip = NULL;
4755
4756
static void
4757
show_sizing_tip(int cols, int rows)
4758
{
4759
TOOLINFOA ti;
4760
char buf[32];
4761
4762
ti.cbSize = sizeof(ti);
4763
ti.hwnd = s_hwnd;
4764
ti.uId = (UINT_PTR)s_hwnd;
4765
ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4766
ti.lpszText = buf;
4767
sprintf(buf, "%dx%d", cols, rows);
4768
if (hwndTip == NULL)
4769
{
4770
hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4771
WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4772
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4773
s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4774
SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4775
SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4776
}
4777
else
4778
{
4779
SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4780
}
4781
SendMessage(hwndTip, TTM_POPUP, 0, 0);
4782
}
4783
4784
static void
4785
destroy_sizing_tip(void)
4786
{
4787
if (hwndTip == NULL)
4788
return;
4789
4790
DestroyWindow(hwndTip);
4791
hwndTip = NULL;
4792
}
4793
4794
static int
4795
_DuringSizing(
4796
UINT fwSide,
4797
LPRECT lprc)
4798
{
4799
int w, h;
4800
int valid_w, valid_h;
4801
int w_offset, h_offset;
4802
int cols, rows;
4803
4804
w = lprc->right - lprc->left;
4805
h = lprc->bottom - lprc->top;
4806
gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
4807
w_offset = w - valid_w;
4808
h_offset = h - valid_h;
4809
4810
if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4811
|| fwSide == WMSZ_BOTTOMLEFT)
4812
lprc->left += w_offset;
4813
else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4814
|| fwSide == WMSZ_BOTTOMRIGHT)
4815
lprc->right -= w_offset;
4816
4817
if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4818
|| fwSide == WMSZ_TOPRIGHT)
4819
lprc->top += h_offset;
4820
else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4821
|| fwSide == WMSZ_BOTTOMRIGHT)
4822
lprc->bottom -= h_offset;
4823
4824
show_sizing_tip(cols, rows);
4825
return TRUE;
4826
}
4827
4828
#ifdef FEAT_GUI_TABLINE
4829
static void
4830
_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4831
{
4832
if (gui_mch_showing_tabline())
4833
{
4834
POINT pt;
4835
RECT rect;
4836
4837
/*
4838
* If the cursor is on the tabline, display the tab menu
4839
*/
4840
GetCursorPos(&pt);
4841
GetWindowRect(s_textArea, &rect);
4842
if (pt.y < rect.top)
4843
{
4844
show_tabline_popup_menu();
4845
return;
4846
}
4847
}
4848
FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4849
}
4850
4851
static void
4852
_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4853
{
4854
/*
4855
* If the user double clicked the tabline, create a new tab
4856
*/
4857
if (gui_mch_showing_tabline())
4858
{
4859
POINT pt;
4860
RECT rect;
4861
4862
GetCursorPos(&pt);
4863
GetWindowRect(s_textArea, &rect);
4864
if (pt.y < rect.top)
4865
send_tabline_menu_event(0, TABLINE_MENU_NEW);
4866
}
4867
FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4868
}
4869
#endif
4870
4871
static UINT
4872
_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4873
{
4874
UINT result;
4875
int x, y;
4876
4877
result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4878
if (result != HTCLIENT)
4879
return result;
4880
4881
#ifdef FEAT_GUI_TABLINE
4882
if (gui_mch_showing_tabline())
4883
{
4884
RECT rct;
4885
4886
// If the cursor is on the GUI tabline, don't process this event
4887
GetWindowRect(s_textArea, &rct);
4888
if (yPos < rct.top)
4889
return result;
4890
}
4891
#endif
4892
(void)gui_mch_get_winpos(&x, &y);
4893
xPos -= x;
4894
4895
if (xPos < 48) // <VN> TODO should use system metric?
4896
return HTBOTTOMLEFT;
4897
else
4898
return HTBOTTOMRIGHT;
4899
}
4900
4901
#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4902
static LRESULT
4903
_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4904
{
4905
switch (hdr->code)
4906
{
4907
case TTN_GETDISPINFOW:
4908
case TTN_GETDISPINFO:
4909
{
4910
char_u *str = NULL;
4911
static void *tt_text = NULL;
4912
4913
VIM_CLEAR(tt_text);
4914
4915
# ifdef FEAT_GUI_TABLINE
4916
if (gui_mch_showing_tabline()
4917
&& hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4918
{
4919
POINT pt;
4920
/*
4921
* Mouse is over the GUI tabline. Display the
4922
* tooltip for the tab under the cursor
4923
*
4924
* Get the cursor position within the tab control
4925
*/
4926
GetCursorPos(&pt);
4927
if (ScreenToClient(s_tabhwnd, &pt) != 0)
4928
{
4929
TCHITTESTINFO htinfo;
4930
int idx;
4931
4932
/*
4933
* Get the tab under the cursor
4934
*/
4935
htinfo.pt.x = pt.x;
4936
htinfo.pt.y = pt.y;
4937
idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4938
if (idx != -1)
4939
{
4940
tabpage_T *tp;
4941
4942
tp = find_tabpage(idx + 1);
4943
if (tp != NULL)
4944
{
4945
get_tabline_label(tp, TRUE);
4946
str = NameBuff;
4947
}
4948
}
4949
}
4950
}
4951
# endif
4952
# ifdef FEAT_TOOLBAR
4953
# ifdef FEAT_GUI_TABLINE
4954
else
4955
# endif
4956
{
4957
UINT idButton;
4958
vimmenu_T *pMenu;
4959
4960
idButton = (UINT) hdr->idFrom;
4961
pMenu = gui_mswin_find_menu(root_menu, idButton);
4962
if (pMenu)
4963
str = pMenu->strings[MENU_INDEX_TIP];
4964
}
4965
# endif
4966
if (str == NULL)
4967
break;
4968
4969
// Set the maximum width, this also enables using \n for
4970
// line break.
4971
SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4972
4973
if (hdr->code == TTN_GETDISPINFOW)
4974
{
4975
LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4976
4977
tt_text = enc_to_utf16(str, NULL);
4978
lpdi->lpszText = tt_text;
4979
// can't show tooltip if failed
4980
}
4981
else
4982
{
4983
LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4984
size_t len = STRLEN(str);
4985
4986
if (len < sizeof(lpdi->szText)
4987
|| ((tt_text = vim_strnsave(str, len)) == NULL))
4988
vim_strncpy((char_u *)lpdi->szText, str,
4989
sizeof(lpdi->szText) - 1);
4990
else
4991
lpdi->lpszText = tt_text;
4992
}
4993
}
4994
break;
4995
4996
# ifdef FEAT_GUI_TABLINE
4997
case TCN_SELCHANGE:
4998
if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4999
{
5000
send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
5001
return 0L;
5002
}
5003
break;
5004
5005
case NM_RCLICK:
5006
if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
5007
{
5008
show_tabline_popup_menu();
5009
return 0L;
5010
}
5011
break;
5012
# endif
5013
5014
default:
5015
break;
5016
}
5017
return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
5018
}
5019
#endif
5020
5021
#if defined(MENUHINTS) && defined(FEAT_MENU)
5022
static LRESULT
5023
_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
5024
{
5025
if (((UINT) HIWORD(wParam)
5026
& (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
5027
== MF_HILITE
5028
&& (State & MODE_CMDLINE) == 0)
5029
{
5030
vimmenu_T *pMenu;
5031
static int did_menu_tip = FALSE;
5032
5033
if (did_menu_tip)
5034
{
5035
msg_clr_cmdline();
5036
setcursor();
5037
out_flush();
5038
did_menu_tip = FALSE;
5039
}
5040
5041
pMenu = gui_mswin_find_menu(root_menu, (UINT)LOWORD(wParam));
5042
if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != NULL)
5043
{
5044
MENUITEMINFO menuinfo;
5045
5046
menuinfo.cbSize = sizeof(MENUITEMINFO);
5047
menuinfo.fMask = MIIM_ID; // We only want to check if the menu item exists,
5048
// so retrieve something simple.
5049
if (GetMenuItemInfo(s_menuBar, pMenu->id, FALSE, &menuinfo))
5050
{
5051
++msg_hist_off;
5052
msg((char *)pMenu->strings[MENU_INDEX_TIP]);
5053
--msg_hist_off;
5054
setcursor();
5055
out_flush();
5056
did_menu_tip = TRUE;
5057
}
5058
}
5059
return 0L;
5060
}
5061
return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
5062
}
5063
#endif
5064
5065
static BOOL
5066
_OnGetDpiScaledSize(HWND hwnd UNUSED, UINT dpi, SIZE *size)
5067
{
5068
int old_width, old_height;
5069
int new_width, new_height;
5070
LOGFONTW lf;
5071
HFONT font;
5072
5073
//TRACE("DPI: %d, SIZE=(%d,%d), s_dpi: %d", dpi, size->cx, size->cy, s_dpi);
5074
5075
// Calculate new approximate size.
5076
GetFontSize(gui.norm_font, &old_width, &old_height); // Current size
5077
GetObjectW((HFONT)gui.norm_font, sizeof(lf), &lf);
5078
lf.lfHeight = lf.lfHeight * (int)dpi / s_dpi;
5079
font = CreateFontIndirectW(&lf);
5080
if (font)
5081
{
5082
GetFontSize((GuiFont)font, &new_width, &new_height); // New size
5083
DeleteFont(font);
5084
}
5085
else
5086
{
5087
new_width = old_width;
5088
new_height = old_height;
5089
}
5090
size->cx = size->cx * new_width / old_width;
5091
size->cy = size->cy * new_height / old_height;
5092
//TRACE("New approx. SIZE=(%d,%d)", size->cx, size->cy);
5093
5094
return TRUE;
5095
}
5096
5097
static LRESULT
5098
_OnDpiChanged(HWND hwnd, UINT xdpi UNUSED, UINT ydpi, RECT *rc)
5099
{
5100
s_dpi = ydpi;
5101
s_in_dpichanged = TRUE;
5102
//TRACE("DPI: %d", ydpi);
5103
5104
s_suggested_rect = *rc;
5105
//TRACE("Suggested pos&size: %d,%d %d,%d", rc->left, rc->top,
5106
// rc->right - rc->left, rc->bottom - rc->top);
5107
5108
update_scrollbar_size();
5109
update_toolbar_size();
5110
set_tabline_font();
5111
5112
gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
5113
gui_get_wide_font();
5114
gui_mswin_get_menu_height(FALSE);
5115
#ifdef FEAT_MBYTE_IME
5116
im_set_position(gui.row, gui.col);
5117
#endif
5118
InvalidateRect(hwnd, NULL, TRUE);
5119
5120
s_in_dpichanged = FALSE;
5121
return 0L;
5122
}
5123
5124
5125
static LRESULT CALLBACK
5126
_WndProc(
5127
HWND hwnd,
5128
UINT uMsg,
5129
WPARAM wParam,
5130
LPARAM lParam)
5131
{
5132
// ch_log(NULL, "WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x",
5133
// hwnd, uMsg, wParam, lParam);
5134
5135
HandleMouseHide(uMsg, lParam);
5136
5137
s_uMsg = uMsg;
5138
s_wParam = wParam;
5139
s_lParam = lParam;
5140
5141
switch (uMsg)
5142
{
5143
HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
5144
HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
5145
// HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
5146
HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
5147
// HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
5148
HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
5149
HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
5150
HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
5151
HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
5152
#ifdef FEAT_MENU
5153
HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
5154
#endif
5155
// HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
5156
// HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
5157
HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
5158
HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
5159
// HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
5160
// HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
5161
HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
5162
// HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
5163
HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
5164
#ifdef FEAT_NETBEANS_INTG
5165
HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
5166
#endif
5167
#ifdef FEAT_GUI_TABLINE
5168
HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
5169
HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
5170
#endif
5171
HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
5172
5173
case WM_QUERYENDSESSION: // System wants to go down.
5174
gui_shell_closed(); // Will exit when no changed buffers.
5175
return FALSE; // Do NOT allow system to go down.
5176
5177
case WM_ENDSESSION:
5178
if (wParam) // system only really goes down when wParam is TRUE
5179
{
5180
_OnEndSession();
5181
return 0L;
5182
}
5183
break;
5184
5185
case WM_CHAR:
5186
// Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
5187
// byte while we want the UTF-16 character value.
5188
_OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
5189
return 0L;
5190
5191
case WM_SYSCHAR:
5192
/*
5193
* if 'winaltkeys' is "no", or it's "menu" and it's not a menu
5194
* shortcut key, handle like a typed ALT key, otherwise call Windows
5195
* ALT key handling.
5196
*/
5197
#ifdef FEAT_MENU
5198
if ( !gui.menu_is_active
5199
|| p_wak[0] == 'n'
5200
|| (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
5201
)
5202
#endif
5203
{
5204
_OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
5205
return 0L;
5206
}
5207
#ifdef FEAT_MENU
5208
else
5209
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5210
#endif
5211
5212
case WM_SYSKEYUP:
5213
#ifdef FEAT_MENU
5214
// This used to be done only when menu is active: ALT key is used for
5215
// that. But that caused problems when menu is disabled and using
5216
// Alt-Tab-Esc: get into a strange state where no mouse-moved events
5217
// are received, mouse pointer remains hidden.
5218
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5219
#else
5220
return 0L;
5221
#endif
5222
5223
case WM_EXITSIZEMOVE:
5224
destroy_sizing_tip();
5225
break;
5226
5227
case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
5228
return _DuringSizing((UINT)wParam, (LPRECT)lParam);
5229
5230
case WM_MOUSEWHEEL:
5231
case WM_MOUSEHWHEEL:
5232
_OnMouseWheel(hwnd, wParam, lParam, uMsg == WM_MOUSEHWHEEL);
5233
return 0L;
5234
5235
// Notification for change in SystemParametersInfo()
5236
case WM_SETTINGCHANGE:
5237
return _OnSettingChange((UINT)wParam);
5238
5239
#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
5240
case WM_NOTIFY:
5241
return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
5242
#endif
5243
5244
#if defined(MENUHINTS) && defined(FEAT_MENU)
5245
case WM_MENUSELECT:
5246
return _OnMenuSelect(hwnd, wParam, lParam);
5247
#endif
5248
5249
#ifdef FEAT_MBYTE_IME
5250
case WM_IME_NOTIFY:
5251
if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5252
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5253
return 1L;
5254
5255
case WM_IME_COMPOSITION:
5256
if (!_OnImeComposition(hwnd, wParam, lParam))
5257
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5258
return 1L;
5259
#endif
5260
case WM_GETDPISCALEDSIZE:
5261
return _OnGetDpiScaledSize(hwnd, (UINT)wParam, (SIZE *)lParam);
5262
case WM_DPICHANGED:
5263
return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
5264
(RECT*)lParam);
5265
5266
default:
5267
#ifdef MSWIN_FIND_REPLACE
5268
if (uMsg == s_findrep_msg && s_findrep_msg != 0)
5269
_OnFindRepl();
5270
#endif
5271
break;
5272
}
5273
5274
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5275
}
5276
5277
/*
5278
* End of call-back routines
5279
*/
5280
5281
// parent window, if specified with -P
5282
HWND vim_parent_hwnd = NULL;
5283
5284
static BOOL CALLBACK
5285
FindWindowTitle(HWND hwnd, LPARAM lParam)
5286
{
5287
char buf[2048];
5288
char *title = (char *)lParam;
5289
5290
if (GetWindowText(hwnd, buf, sizeof(buf)))
5291
{
5292
if (strstr(buf, title) != NULL)
5293
{
5294
// Found it. Store the window ref. and quit searching if MDI
5295
// works.
5296
vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
5297
if (vim_parent_hwnd != NULL)
5298
return FALSE;
5299
}
5300
}
5301
return TRUE; // continue searching
5302
}
5303
5304
/*
5305
* Invoked for '-P "title"' argument: search for parent application to open
5306
* our window in.
5307
*/
5308
void
5309
gui_mch_set_parent(char *title)
5310
{
5311
EnumWindows(FindWindowTitle, (LPARAM)title);
5312
if (vim_parent_hwnd == NULL)
5313
{
5314
semsg(_(e_cannot_find_window_title_str), title);
5315
mch_exit(2);
5316
}
5317
}
5318
5319
#ifndef FEAT_OLE
5320
static void
5321
ole_error(char *arg)
5322
{
5323
char buf[IOSIZE];
5324
5325
# ifdef VIMDLL
5326
gui.in_use = mch_is_gui_executable();
5327
# endif
5328
5329
// Can't use emsg() here, we have not finished initialisation yet.
5330
vim_snprintf(buf, IOSIZE,
5331
_(e_argument_not_supported_str_use_ole_version), arg);
5332
mch_errmsg(buf);
5333
}
5334
#endif
5335
5336
#if defined(GUI_MAY_SPAWN) || defined(PROTO)
5337
static char *
5338
gvim_error(void)
5339
{
5340
char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
5341
5342
if (starting)
5343
{
5344
mch_errmsg(msg);
5345
mch_errmsg("\n");
5346
mch_exit(2);
5347
}
5348
return msg;
5349
}
5350
5351
char *
5352
gui_mch_do_spawn(char_u *arg)
5353
{
5354
int len;
5355
# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5356
char_u *session = NULL;
5357
LPWSTR tofree1 = NULL;
5358
# endif
5359
WCHAR name[MAX_PATH];
5360
LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
5361
STARTUPINFOW si = {sizeof(si)};
5362
PROCESS_INFORMATION pi;
5363
5364
if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
5365
goto error;
5366
p = wcsrchr(name, L'\\');
5367
if (p == NULL)
5368
goto error;
5369
// Replace the executable name from vim(d).exe to gvim(d).exe.
5370
# ifdef DEBUG
5371
wcscpy(p + 1, L"gvimd.exe");
5372
# else
5373
wcscpy(p + 1, L"gvim.exe");
5374
# endif
5375
5376
# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5377
if (starting)
5378
# endif
5379
{
5380
// Pass the command line to the new process.
5381
p = GetCommandLineW();
5382
// Skip 1st argument.
5383
while (*p && *p != L' ' && *p != L'\t')
5384
{
5385
if (*p == L'"')
5386
{
5387
// Skip quoted strings
5388
while (*++p && *p != L'"');
5389
}
5390
5391
++p;
5392
}
5393
cmd = p;
5394
}
5395
# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5396
else
5397
{
5398
// Create a session file and pass it to the new process.
5399
LPWSTR wsession;
5400
char_u *savebg;
5401
int ret;
5402
5403
session = vim_tempname('s', FALSE);
5404
if (session == NULL)
5405
goto error;
5406
savebg = p_bg;
5407
p_bg = vim_strnsave((char_u *)"light", 5); // Set 'bg' to "light".
5408
if (p_bg == NULL)
5409
{
5410
p_bg = savebg;
5411
goto error;
5412
}
5413
ret = write_session_file(session);
5414
vim_free(p_bg);
5415
p_bg = savebg;
5416
if (!ret)
5417
goto error;
5418
wsession = enc_to_utf16(session, NULL);
5419
if (wsession == NULL)
5420
goto error;
5421
len = (int)wcslen(wsession) * 2 + 27 + 1;
5422
cmd = ALLOC_MULT(WCHAR, len);
5423
if (cmd == NULL)
5424
{
5425
vim_free(wsession);
5426
goto error;
5427
}
5428
tofree1 = cmd;
5429
_snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5430
wsession, wsession);
5431
vim_free(wsession);
5432
}
5433
# endif
5434
5435
// Check additional arguments to the `:gui` command.
5436
if (arg != NULL)
5437
{
5438
warg = enc_to_utf16(arg, NULL);
5439
if (warg == NULL)
5440
goto error;
5441
tofree2 = warg;
5442
}
5443
else
5444
warg = L"";
5445
5446
// Set up the new command line.
5447
len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
5448
newcmd = ALLOC_MULT(WCHAR, len);
5449
if (newcmd == NULL)
5450
goto error;
5451
_snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5452
5453
// Spawn a new GUI process.
5454
if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5455
NULL, NULL, &si, &pi))
5456
goto error;
5457
CloseHandle(pi.hProcess);
5458
CloseHandle(pi.hThread);
5459
mch_exit(0);
5460
5461
error:
5462
# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5463
if (session)
5464
mch_remove(session);
5465
vim_free(session);
5466
vim_free(tofree1);
5467
# endif
5468
vim_free(newcmd);
5469
vim_free(tofree2);
5470
return gvim_error();
5471
}
5472
#endif
5473
5474
/*
5475
* Parse the GUI related command-line arguments. Any arguments used are
5476
* deleted from argv, and *argc is decremented accordingly. This is called
5477
* when Vim is started, whether or not the GUI has been started.
5478
*/
5479
void
5480
gui_mch_prepare(int *argc, char **argv)
5481
{
5482
int silent = FALSE;
5483
int idx;
5484
5485
// Check for special OLE command line parameters
5486
if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5487
{
5488
// Check for a "-silent" argument first.
5489
if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5490
&& (argv[2][0] == '-' || argv[2][0] == '/'))
5491
{
5492
silent = TRUE;
5493
idx = 2;
5494
}
5495
else
5496
idx = 1;
5497
5498
// Register Vim as an OLE Automation server
5499
if (STRICMP(argv[idx] + 1, "register") == 0)
5500
{
5501
#ifdef FEAT_OLE
5502
RegisterMe(silent);
5503
mch_exit(0);
5504
#else
5505
if (!silent)
5506
ole_error("register");
5507
mch_exit(2);
5508
#endif
5509
}
5510
5511
// Unregister Vim as an OLE Automation server
5512
if (STRICMP(argv[idx] + 1, "unregister") == 0)
5513
{
5514
#ifdef FEAT_OLE
5515
UnregisterMe(!silent);
5516
mch_exit(0);
5517
#else
5518
if (!silent)
5519
ole_error("unregister");
5520
mch_exit(2);
5521
#endif
5522
}
5523
5524
// Ignore an -embedding argument. It is only relevant if the
5525
// application wants to treat the case when it is started manually
5526
// differently from the case where it is started via automation (and
5527
// we don't).
5528
if (STRICMP(argv[idx] + 1, "embedding") == 0)
5529
{
5530
#ifdef FEAT_OLE
5531
*argc = 1;
5532
#else
5533
ole_error("embedding");
5534
mch_exit(2);
5535
#endif
5536
}
5537
}
5538
5539
#ifdef FEAT_OLE
5540
# ifdef VIMDLL
5541
if (mch_is_gui_executable())
5542
# endif
5543
{
5544
int bDoRestart = FALSE;
5545
5546
InitOLE(&bDoRestart);
5547
// automatically exit after registering
5548
if (bDoRestart)
5549
mch_exit(0);
5550
}
5551
#endif
5552
5553
#ifdef FEAT_NETBEANS_INTG
5554
{
5555
// stolen from gui_x11.c
5556
int arg;
5557
5558
for (arg = 1; arg < *argc; arg++)
5559
if (strncmp("-nb", argv[arg], 3) == 0)
5560
{
5561
netbeansArg = argv[arg];
5562
mch_memmove(&argv[arg], &argv[arg + 1],
5563
(--*argc - arg) * sizeof(char *));
5564
argv[*argc] = NULL;
5565
break; // enough?
5566
}
5567
}
5568
#endif
5569
}
5570
5571
static void
5572
load_dpi_func(void)
5573
{
5574
HMODULE hUser32;
5575
5576
hUser32 = GetModuleHandle("user32.dll");
5577
if (hUser32 == NULL)
5578
goto fail;
5579
5580
pGetDpiForSystem = (UINT (WINAPI *)(void))GetProcAddress(hUser32, "GetDpiForSystem");
5581
pGetDpiForWindow = (UINT (WINAPI *)(HWND))GetProcAddress(hUser32, "GetDpiForWindow");
5582
pGetSystemMetricsForDpi = (int (WINAPI *)(int, UINT))GetProcAddress(hUser32, "GetSystemMetricsForDpi");
5583
//pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
5584
pSetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5585
pGetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
5586
5587
if (pSetThreadDpiAwarenessContext != NULL)
5588
{
5589
DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5590
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5591
if (oldctx != NULL)
5592
{
5593
TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5594
s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5595
#ifdef DEBUG
5596
if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5597
{
5598
TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5599
}
5600
#endif
5601
return;
5602
}
5603
}
5604
5605
fail:
5606
// Disable PerMonitorV2 APIs.
5607
pGetDpiForSystem = vimGetDpiForSystem;
5608
pGetDpiForWindow = NULL;
5609
pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5610
pSetThreadDpiAwarenessContext = NULL;
5611
pGetAwarenessFromDpiAwarenessContext = NULL;
5612
}
5613
5614
/*
5615
* Initialise the GUI. Create all the windows, set up all the call-backs
5616
* etc.
5617
*/
5618
int
5619
gui_mch_init(void)
5620
{
5621
const WCHAR szVimWndClassW[] = VIM_CLASSW;
5622
const WCHAR szTextAreaClassW[] = L"VimTextArea";
5623
WNDCLASSW wndclassw;
5624
5625
// Return here if the window was already opened (happens when
5626
// gui_mch_dialog() is called early).
5627
if (s_hwnd != NULL)
5628
goto theend;
5629
5630
/*
5631
* Load the tearoff bitmap
5632
*/
5633
#ifdef FEAT_TEAROFF
5634
s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
5635
#endif
5636
5637
load_dpi_func();
5638
5639
s_dpi = pGetDpiForSystem();
5640
update_scrollbar_size();
5641
5642
#ifdef FEAT_MENU
5643
gui.menu_height = 0; // Windows takes care of this
5644
#endif
5645
gui.border_width = 0;
5646
#ifdef FEAT_TOOLBAR
5647
gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5648
#endif
5649
5650
s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5651
5652
// First try using the wide version, so that we can use any title.
5653
// Otherwise only characters in the active codepage will work.
5654
if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
5655
{
5656
wndclassw.style = CS_DBLCLKS;
5657
wndclassw.lpfnWndProc = _WndProc;
5658
wndclassw.cbClsExtra = 0;
5659
wndclassw.cbWndExtra = 0;
5660
wndclassw.hInstance = g_hinst;
5661
wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5662
wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5663
wndclassw.hbrBackground = s_brush;
5664
wndclassw.lpszMenuName = NULL;
5665
wndclassw.lpszClassName = szVimWndClassW;
5666
5667
if (RegisterClassW(&wndclassw) == 0)
5668
return FAIL;
5669
}
5670
5671
if (vim_parent_hwnd != NULL)
5672
{
5673
#ifdef HAVE_TRY_EXCEPT
5674
__try
5675
{
5676
#endif
5677
// Open inside the specified parent window.
5678
// TODO: last argument should point to a CLIENTCREATESTRUCT
5679
// structure.
5680
s_hwnd = CreateWindowExW(
5681
WS_EX_MDICHILD,
5682
szVimWndClassW, L"Vim MSWindows GUI",
5683
WS_OVERLAPPEDWINDOW | WS_CHILD
5684
| WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
5685
gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5686
gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5687
100, // Any value will do
5688
100, // Any value will do
5689
vim_parent_hwnd, NULL,
5690
g_hinst, NULL);
5691
#ifdef HAVE_TRY_EXCEPT
5692
}
5693
__except(EXCEPTION_EXECUTE_HANDLER)
5694
{
5695
// NOP
5696
}
5697
#endif
5698
if (s_hwnd == NULL)
5699
{
5700
emsg(_(e_unable_to_open_window_inside_mdi_application));
5701
mch_exit(2);
5702
}
5703
}
5704
else
5705
{
5706
// If the provided windowid is not valid reset it to zero, so that it
5707
// is ignored and we open our own window.
5708
if (IsWindow((HWND)win_socket_id) <= 0)
5709
win_socket_id = 0;
5710
5711
// Create a window. If win_socket_id is not zero without border and
5712
// titlebar, it will be reparented below.
5713
s_hwnd = CreateWindowW(
5714
szVimWndClassW, L"Vim MSWindows GUI",
5715
(win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5716
| WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
5717
gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5718
gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5719
100, // Any value will do
5720
100, // Any value will do
5721
NULL, NULL,
5722
g_hinst, NULL);
5723
if (s_hwnd != NULL && win_socket_id != 0)
5724
{
5725
SetParent(s_hwnd, (HWND)win_socket_id);
5726
ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5727
}
5728
}
5729
5730
if (s_hwnd == NULL)
5731
return FAIL;
5732
5733
if (pGetDpiForWindow != NULL)
5734
{
5735
s_dpi = pGetDpiForWindow(s_hwnd);
5736
update_scrollbar_size();
5737
//TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5738
}
5739
5740
#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5741
dyn_imm_load();
5742
#endif
5743
5744
// Create the text area window
5745
if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
5746
{
5747
wndclassw.style = CS_OWNDC;
5748
wndclassw.lpfnWndProc = _TextAreaWndProc;
5749
wndclassw.cbClsExtra = 0;
5750
wndclassw.cbWndExtra = 0;
5751
wndclassw.hInstance = g_hinst;
5752
wndclassw.hIcon = NULL;
5753
wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5754
wndclassw.hbrBackground = NULL;
5755
wndclassw.lpszMenuName = NULL;
5756
wndclassw.lpszClassName = szTextAreaClassW;
5757
5758
if (RegisterClassW(&wndclassw) == 0)
5759
return FAIL;
5760
}
5761
5762
s_textArea = CreateWindowExW(
5763
0,
5764
szTextAreaClassW, L"Vim text area",
5765
WS_CHILD | WS_VISIBLE, 0, 0,
5766
100, // Any value will do for now
5767
100, // Any value will do for now
5768
s_hwnd, NULL,
5769
g_hinst, NULL);
5770
5771
if (s_textArea == NULL)
5772
return FAIL;
5773
5774
#ifdef FEAT_LIBCALL
5775
// Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
5776
{
5777
HANDLE hIcon = NULL;
5778
5779
if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
5780
SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
5781
}
5782
#endif
5783
5784
#ifdef FEAT_MENU
5785
s_menuBar = CreateMenu();
5786
#endif
5787
s_hdc = GetDC(s_textArea);
5788
5789
DragAcceptFiles(s_hwnd, TRUE);
5790
5791
// Do we need to bother with this?
5792
// m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
5793
5794
// Get background/foreground colors from the system
5795
gui_mch_def_colors();
5796
5797
// Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5798
// file)
5799
set_normal_colors();
5800
5801
/*
5802
* Check that none of the colors are the same as the background color.
5803
* Then store the current values as the defaults.
5804
*/
5805
gui_check_colors();
5806
gui.def_norm_pixel = gui.norm_pixel;
5807
gui.def_back_pixel = gui.back_pixel;
5808
5809
// Get the colors for the highlight groups (gui_check_colors() might have
5810
// changed them)
5811
highlight_gui_started();
5812
5813
/*
5814
* Start out by adding the configured border width into the border offset.
5815
*/
5816
gui.border_offset = gui.border_width;
5817
5818
/*
5819
* Set up for Intellimouse processing
5820
*/
5821
init_mouse_wheel();
5822
5823
/*
5824
* compute a couple of metrics used for the dialogs
5825
*/
5826
get_dialog_font_metrics();
5827
#ifdef FEAT_TOOLBAR
5828
/*
5829
* Create the toolbar
5830
*/
5831
initialise_toolbar();
5832
#endif
5833
#ifdef FEAT_GUI_TABLINE
5834
/*
5835
* Create the tabline
5836
*/
5837
initialise_tabline();
5838
#endif
5839
#ifdef MSWIN_FIND_REPLACE
5840
/*
5841
* Initialise the dialog box stuff
5842
*/
5843
s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5844
5845
// Initialise the struct
5846
s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
5847
s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
5848
s_findrep_struct.lpstrFindWhat[0] = NUL;
5849
s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
5850
s_findrep_struct.lpstrReplaceWith[0] = NUL;
5851
s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5852
s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5853
#endif
5854
5855
#ifdef FEAT_EVAL
5856
// set the v:windowid variable
5857
set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
5858
#endif
5859
5860
#ifdef FEAT_RENDER_OPTIONS
5861
if (p_rop)
5862
(void)gui_mch_set_rendering_options(p_rop);
5863
#endif
5864
5865
theend:
5866
// Display any pending error messages
5867
display_errors();
5868
5869
return OK;
5870
}
5871
5872
/*
5873
* Get the size of the screen, taking position on multiple monitors into
5874
* account (if supported).
5875
*/
5876
static void
5877
get_work_area(RECT *spi_rect)
5878
{
5879
HMONITOR mon;
5880
MONITORINFO moninfo;
5881
5882
// work out which monitor the window is on, and get *its* work area
5883
mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
5884
if (mon != NULL)
5885
{
5886
moninfo.cbSize = sizeof(MONITORINFO);
5887
if (GetMonitorInfo(mon, &moninfo))
5888
{
5889
*spi_rect = moninfo.rcWork;
5890
return;
5891
}
5892
}
5893
// this is the old method...
5894
SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5895
}
5896
5897
/*
5898
* Set the size of the window to the given width and height in pixels.
5899
*/
5900
void
5901
gui_mch_set_shellsize(
5902
int width,
5903
int height,
5904
int min_width UNUSED,
5905
int min_height UNUSED,
5906
int base_width UNUSED,
5907
int base_height UNUSED,
5908
int direction)
5909
{
5910
RECT workarea_rect;
5911
RECT window_rect;
5912
int win_width, win_height;
5913
5914
// Try to keep window completely on screen.
5915
// Get position of the screen work area. This is the part that is not
5916
// used by the taskbar or appbars.
5917
get_work_area(&workarea_rect);
5918
5919
// Resizing a maximized window looks very strange, unzoom it first.
5920
// But don't do it when still starting up, it may have been requested in
5921
// the shortcut.
5922
if (IsZoomed(s_hwnd) && starting == 0)
5923
ShowWindow(s_hwnd, SW_SHOWNORMAL);
5924
5925
if (s_in_dpichanged)
5926
// Use the suggested position when in WM_DPICHANGED.
5927
window_rect = s_suggested_rect;
5928
else
5929
// Use current position.
5930
GetWindowRect(s_hwnd, &window_rect);
5931
5932
// compute the size of the outside of the window
5933
win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5934
pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5935
win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5936
pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5937
+ pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5938
+ gui_mswin_get_menu_height(FALSE);
5939
5940
// The following should take care of keeping Vim on the same monitor, no
5941
// matter if the secondary monitor is left or right of the primary
5942
// monitor.
5943
window_rect.right = window_rect.left + win_width;
5944
window_rect.bottom = window_rect.top + win_height;
5945
5946
// If the window is going off the screen, move it on to the screen.
5947
// Don't adjust the position when in WM_DPICHANGED.
5948
if (!s_in_dpichanged)
5949
{
5950
if ((direction & RESIZE_HOR)
5951
&& window_rect.right > workarea_rect.right)
5952
OffsetRect(&window_rect,
5953
workarea_rect.right - window_rect.right, 0);
5954
5955
if ((direction & RESIZE_HOR)
5956
&& window_rect.left < workarea_rect.left)
5957
OffsetRect(&window_rect,
5958
workarea_rect.left - window_rect.left, 0);
5959
5960
if ((direction & RESIZE_VERT)
5961
&& window_rect.bottom > workarea_rect.bottom)
5962
OffsetRect(&window_rect,
5963
0, workarea_rect.bottom - window_rect.bottom);
5964
5965
if ((direction & RESIZE_VERT)
5966
&& window_rect.top < workarea_rect.top)
5967
OffsetRect(&window_rect,
5968
0, workarea_rect.top - window_rect.top);
5969
}
5970
5971
MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5972
win_width, win_height, TRUE);
5973
5974
//TRACE("New pos: %d,%d New size: %d,%d",
5975
// window_rect.left, window_rect.top, win_width, win_height);
5976
5977
SetActiveWindow(s_hwnd);
5978
SetFocus(s_hwnd);
5979
5980
// Menu may wrap differently now
5981
gui_mswin_get_menu_height(!gui.starting);
5982
}
5983
5984
5985
void
5986
gui_mch_set_scrollbar_thumb(
5987
scrollbar_T *sb,
5988
long val,
5989
long size,
5990
long max)
5991
{
5992
SCROLLINFO info;
5993
5994
sb->scroll_shift = 0;
5995
while (max > 32767)
5996
{
5997
max = (max + 1) >> 1;
5998
val >>= 1;
5999
size >>= 1;
6000
++sb->scroll_shift;
6001
}
6002
6003
if (sb->scroll_shift > 0)
6004
++size;
6005
6006
info.cbSize = sizeof(info);
6007
info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
6008
info.nPos = val;
6009
info.nMin = 0;
6010
info.nMax = max;
6011
info.nPage = size;
6012
SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
6013
}
6014
6015
6016
/*
6017
* Set the current text font.
6018
*/
6019
void
6020
gui_mch_set_font(GuiFont font)
6021
{
6022
gui.currFont = font;
6023
}
6024
6025
6026
/*
6027
* Set the current text foreground color.
6028
*/
6029
void
6030
gui_mch_set_fg_color(guicolor_T color)
6031
{
6032
gui.currFgColor = color;
6033
}
6034
6035
/*
6036
* Set the current text background color.
6037
*/
6038
void
6039
gui_mch_set_bg_color(guicolor_T color)
6040
{
6041
gui.currBgColor = color;
6042
}
6043
6044
/*
6045
* Set the current text special color.
6046
*/
6047
void
6048
gui_mch_set_sp_color(guicolor_T color)
6049
{
6050
gui.currSpColor = color;
6051
}
6052
6053
#ifdef FEAT_MBYTE_IME
6054
/*
6055
* Multi-byte handling, originally by Sung-Hoon Baek.
6056
* First static functions (no prototypes generated).
6057
*/
6058
# ifdef _MSC_VER
6059
# include <ime.h> // Apparently not needed for Cygwin or MinGW.
6060
# endif
6061
# include <imm.h>
6062
6063
/*
6064
* handle WM_IME_NOTIFY message
6065
*/
6066
static LRESULT
6067
_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
6068
{
6069
LRESULT lResult = 0;
6070
HIMC hImc;
6071
6072
if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
6073
return lResult;
6074
switch (dwCommand)
6075
{
6076
case IMN_SETOPENSTATUS:
6077
if (pImmGetOpenStatus(hImc))
6078
{
6079
LOGFONTW lf = norm_logfont;
6080
if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
6081
// Work around when PerMonitorV2 is not enabled in the process level.
6082
lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
6083
pImmSetCompositionFontW(hImc, &lf);
6084
im_set_position(gui.row, gui.col);
6085
6086
// Disable langmap
6087
State &= ~MODE_LANGMAP;
6088
if (State & MODE_INSERT)
6089
{
6090
# if defined(FEAT_KEYMAP)
6091
// Unshown 'keymap' in status lines
6092
if (curbuf->b_p_iminsert == B_IMODE_LMAP)
6093
{
6094
// Save cursor position
6095
int old_row = gui.row;
6096
int old_col = gui.col;
6097
6098
// This must be called here before
6099
// status_redraw_curbuf(), otherwise the mode
6100
// message may appear in the wrong position.
6101
showmode();
6102
status_redraw_curbuf();
6103
update_screen(0);
6104
// Restore cursor position
6105
gui.row = old_row;
6106
gui.col = old_col;
6107
}
6108
# endif
6109
}
6110
}
6111
gui_update_cursor(TRUE, FALSE);
6112
gui_mch_flush();
6113
lResult = 0;
6114
break;
6115
}
6116
pImmReleaseContext(hWnd, hImc);
6117
return lResult;
6118
}
6119
6120
static LRESULT
6121
_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
6122
{
6123
char_u *ret;
6124
int len;
6125
6126
if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
6127
return 0;
6128
6129
ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
6130
if (ret != NULL)
6131
{
6132
add_to_input_buf_csi(ret, len);
6133
vim_free(ret);
6134
return 1;
6135
}
6136
return 0;
6137
}
6138
6139
/*
6140
* void GetResultStr()
6141
*
6142
* This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
6143
* get complete composition string
6144
*/
6145
static char_u *
6146
GetResultStr(HWND hwnd, int GCS, int *lenp)
6147
{
6148
HIMC hIMC; // Input context handle.
6149
LONG ret;
6150
WCHAR *buf = NULL;
6151
char_u *convbuf = NULL;
6152
6153
if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
6154
return NULL;
6155
6156
// Get the length of the composition string.
6157
ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
6158
if (ret <= 0)
6159
return NULL;
6160
6161
// Allocate the requested buffer plus space for the NUL character.
6162
buf = alloc(ret + sizeof(WCHAR));
6163
if (buf == NULL)
6164
return NULL;
6165
6166
// Reads in the composition string.
6167
pImmGetCompositionStringW(hIMC, GCS, buf, ret);
6168
*lenp = ret / sizeof(WCHAR);
6169
6170
convbuf = utf16_to_enc(buf, lenp);
6171
pImmReleaseContext(hwnd, hIMC);
6172
vim_free(buf);
6173
return convbuf;
6174
}
6175
#endif
6176
6177
// For global functions we need prototypes.
6178
#if defined(FEAT_MBYTE_IME) || defined(PROTO)
6179
6180
/*
6181
* set font to IM.
6182
*/
6183
void
6184
im_set_font(LOGFONTW *lf)
6185
{
6186
HIMC hImc;
6187
6188
if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6189
{
6190
pImmSetCompositionFontW(hImc, lf);
6191
pImmReleaseContext(s_hwnd, hImc);
6192
}
6193
}
6194
6195
/*
6196
* Notify cursor position to IM.
6197
*/
6198
void
6199
im_set_position(int row, int col)
6200
{
6201
HIMC hImc;
6202
6203
if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6204
{
6205
COMPOSITIONFORM cfs;
6206
6207
cfs.dwStyle = CFS_POINT;
6208
cfs.ptCurrentPos.x = FILL_X(col);
6209
cfs.ptCurrentPos.y = FILL_Y(row);
6210
MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
6211
if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
6212
{
6213
// Work around when PerMonitorV2 is not enabled in the process level.
6214
cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
6215
cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
6216
}
6217
pImmSetCompositionWindow(hImc, &cfs);
6218
6219
pImmReleaseContext(s_hwnd, hImc);
6220
}
6221
}
6222
6223
/*
6224
* Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6225
*/
6226
void
6227
im_set_active(int active)
6228
{
6229
HIMC hImc;
6230
static HIMC hImcOld = (HIMC)0;
6231
6232
# ifdef VIMDLL
6233
if (!gui.in_use && !gui.starting)
6234
{
6235
mbyte_im_set_active(active);
6236
return;
6237
}
6238
# endif
6239
6240
if (!pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
6241
return;
6242
6243
if (p_imdisable)
6244
{
6245
if (hImcOld == (HIMC)0)
6246
{
6247
hImcOld = pImmGetContext(s_hwnd);
6248
if (hImcOld)
6249
pImmAssociateContext(s_hwnd, (HIMC)0);
6250
}
6251
active = FALSE;
6252
}
6253
else if (hImcOld != (HIMC)0)
6254
{
6255
pImmAssociateContext(s_hwnd, hImcOld);
6256
hImcOld = (HIMC)0;
6257
}
6258
6259
hImc = pImmGetContext(s_hwnd);
6260
if (!hImc)
6261
return;
6262
6263
/*
6264
* for Korean ime
6265
*/
6266
HKL hKL = GetKeyboardLayout(0);
6267
6268
if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6269
{
6270
static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6271
static BOOL bSaved = FALSE;
6272
6273
if (active)
6274
{
6275
// if we have a saved conversion status, restore it
6276
if (bSaved)
6277
pImmSetConversionStatus(hImc, dwConversionSaved,
6278
dwSentenceSaved);
6279
bSaved = FALSE;
6280
}
6281
else
6282
{
6283
// save conversion status and disable korean
6284
if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6285
&dwSentenceSaved))
6286
{
6287
bSaved = TRUE;
6288
pImmSetConversionStatus(hImc,
6289
dwConversionSaved & ~(IME_CMODE_NATIVE
6290
| IME_CMODE_FULLSHAPE),
6291
dwSentenceSaved);
6292
}
6293
}
6294
}
6295
6296
pImmSetOpenStatus(hImc, active);
6297
pImmReleaseContext(s_hwnd, hImc);
6298
}
6299
6300
/*
6301
* Get IM status. When IM is on, return not 0. Else return 0.
6302
*/
6303
int
6304
im_get_status(void)
6305
{
6306
int status = 0;
6307
HIMC hImc;
6308
6309
# ifdef VIMDLL
6310
if (!gui.in_use && !gui.starting)
6311
return mbyte_im_get_status();
6312
# endif
6313
6314
if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6315
{
6316
status = pImmGetOpenStatus(hImc) ? 1 : 0;
6317
pImmReleaseContext(s_hwnd, hImc);
6318
}
6319
return status;
6320
}
6321
6322
#endif // FEAT_MBYTE_IME
6323
6324
6325
/*
6326
* Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
6327
*/
6328
static void
6329
latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6330
{
6331
int c;
6332
6333
while (--len >= 0)
6334
{
6335
c = *text++;
6336
switch (c)
6337
{
6338
case 0xa4: c = 0x20ac; break; // euro
6339
case 0xa6: c = 0x0160; break; // S hat
6340
case 0xa8: c = 0x0161; break; // S -hat
6341
case 0xb4: c = 0x017d; break; // Z hat
6342
case 0xb8: c = 0x017e; break; // Z -hat
6343
case 0xbc: c = 0x0152; break; // OE
6344
case 0xbd: c = 0x0153; break; // oe
6345
case 0xbe: c = 0x0178; break; // Y
6346
}
6347
*unicodebuf++ = c;
6348
}
6349
}
6350
6351
#ifdef FEAT_RIGHTLEFT
6352
/*
6353
* What is this for? In the case where you are using Win98 or Win2K or later,
6354
* and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6355
* reverses the string sent to the TextOut... family. This sucks, because we
6356
* go to a lot of effort to do the right thing, and there doesn't seem to be a
6357
* way to tell Windblows not to do this!
6358
*
6359
* The short of it is that this 'RevOut' only gets called if you are running
6360
* one of the new, "improved" MS OSes, and only if you are running in
6361
* 'rightleft' mode. It makes display take *slightly* longer, but not
6362
* noticeably so.
6363
*/
6364
static void
6365
RevOut( HDC hdc,
6366
int col,
6367
int row,
6368
UINT foptions,
6369
CONST RECT *pcliprect,
6370
LPCTSTR text,
6371
UINT len,
6372
CONST INT *padding)
6373
{
6374
int ix;
6375
6376
for (ix = 0; ix < (int)len; ++ix)
6377
ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
6378
pcliprect, text + ix, 1, padding);
6379
}
6380
#endif
6381
6382
static void
6383
draw_line(
6384
int x1,
6385
int y1,
6386
int x2,
6387
int y2,
6388
COLORREF color)
6389
{
6390
#if defined(FEAT_DIRECTX)
6391
if (IS_ENABLE_DIRECTX())
6392
DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6393
else
6394
#endif
6395
{
6396
HPEN hpen = CreatePen(PS_SOLID, 1, color);
6397
HPEN old_pen = SelectObject(s_hdc, hpen);
6398
MoveToEx(s_hdc, x1, y1, NULL);
6399
// Note: LineTo() excludes the last pixel in the line.
6400
LineTo(s_hdc, x2, y2);
6401
DeleteObject(SelectObject(s_hdc, old_pen));
6402
}
6403
}
6404
6405
static void
6406
set_pixel(
6407
int x,
6408
int y,
6409
COLORREF color)
6410
{
6411
#if defined(FEAT_DIRECTX)
6412
if (IS_ENABLE_DIRECTX())
6413
DWriteContext_SetPixel(s_dwc, x, y, color);
6414
else
6415
#endif
6416
SetPixel(s_hdc, x, y, color);
6417
}
6418
6419
static void
6420
fill_rect(
6421
const RECT *rcp,
6422
HBRUSH hbr,
6423
COLORREF color)
6424
{
6425
#if defined(FEAT_DIRECTX)
6426
if (IS_ENABLE_DIRECTX())
6427
DWriteContext_FillRect(s_dwc, rcp, color);
6428
else
6429
#endif
6430
{
6431
HBRUSH hbr2;
6432
6433
if (hbr == NULL)
6434
hbr2 = CreateSolidBrush(color);
6435
else
6436
hbr2 = hbr;
6437
FillRect(s_hdc, rcp, hbr2);
6438
if (hbr == NULL)
6439
DeleteBrush(hbr2);
6440
}
6441
}
6442
6443
void
6444
gui_mch_draw_string(
6445
int row,
6446
int col,
6447
char_u *text,
6448
int len,
6449
int flags)
6450
{
6451
static int *padding = NULL;
6452
static int pad_size = 0;
6453
const RECT *pcliprect = NULL;
6454
UINT foptions = 0;
6455
static WCHAR *unicodebuf = NULL;
6456
static int *unicodepdy = NULL;
6457
static int unibuflen = 0;
6458
int n = 0;
6459
int y;
6460
6461
/*
6462
* Italic and bold text seems to have an extra row of pixels at the bottom
6463
* (below where the bottom of the character should be). If we draw the
6464
* characters with a solid background, the top row of pixels in the
6465
* character below will be overwritten. We can fix this by filling in the
6466
* background ourselves, to the correct character proportions, and then
6467
* writing the character in transparent mode. Still have a problem when
6468
* the character is "_", which gets written on to the character below.
6469
* New fix: set gui.char_ascent to -1. This shifts all characters up one
6470
* pixel in their slots, which fixes the problem with the bottom row of
6471
* pixels. We still need this code because otherwise the top row of pixels
6472
* becomes a problem. - webb.
6473
*/
6474
static HBRUSH hbr_cache[2] = {NULL, NULL};
6475
static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6476
static int brush_lru = 0;
6477
HBRUSH hbr;
6478
RECT rc;
6479
6480
if (!(flags & DRAW_TRANSP))
6481
{
6482
/*
6483
* Clear background first.
6484
* Note: FillRect() excludes right and bottom of rectangle.
6485
*/
6486
rc.left = FILL_X(col);
6487
rc.top = FILL_Y(row);
6488
if (has_mbyte)
6489
{
6490
// Compute the length in display cells.
6491
rc.right = FILL_X(col + mb_string2cells(text, len));
6492
}
6493
else
6494
rc.right = FILL_X(col + len);
6495
rc.bottom = FILL_Y(row + 1);
6496
6497
// Cache the created brush, that saves a lot of time. We need two:
6498
// one for cursor background and one for the normal background.
6499
if (gui.currBgColor == brush_color[0])
6500
{
6501
hbr = hbr_cache[0];
6502
brush_lru = 1;
6503
}
6504
else if (gui.currBgColor == brush_color[1])
6505
{
6506
hbr = hbr_cache[1];
6507
brush_lru = 0;
6508
}
6509
else
6510
{
6511
if (hbr_cache[brush_lru] != NULL)
6512
DeleteBrush(hbr_cache[brush_lru]);
6513
hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6514
brush_color[brush_lru] = gui.currBgColor;
6515
hbr = hbr_cache[brush_lru];
6516
brush_lru = !brush_lru;
6517
}
6518
6519
fill_rect(&rc, hbr, gui.currBgColor);
6520
6521
SetBkMode(s_hdc, TRANSPARENT);
6522
6523
/*
6524
* When drawing block cursor, prevent inverted character spilling
6525
* over character cell (can happen with bold/italic)
6526
*/
6527
if (flags & DRAW_CURSOR)
6528
{
6529
pcliprect = &rc;
6530
foptions = ETO_CLIPPED;
6531
}
6532
}
6533
SetTextColor(s_hdc, gui.currFgColor);
6534
SelectFont(s_hdc, gui.currFont);
6535
6536
#ifdef FEAT_DIRECTX
6537
if (IS_ENABLE_DIRECTX())
6538
DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
6539
#endif
6540
6541
if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6542
{
6543
int i;
6544
6545
vim_free(padding);
6546
pad_size = Columns;
6547
6548
// Don't give an out-of-memory message here, it would call us
6549
// recursively.
6550
padding = LALLOC_MULT(int, pad_size);
6551
if (padding != NULL)
6552
for (i = 0; i < pad_size; i++)
6553
padding[i] = gui.char_width;
6554
}
6555
6556
/*
6557
* We have to provide the padding argument because italic and bold versions
6558
* of fixed-width fonts are often one pixel or so wider than their normal
6559
* versions.
6560
* No check for DRAW_BOLD, Windows will have done it already.
6561
*/
6562
6563
// Check if there are any UTF-8 characters. If not, use normal text
6564
// output to speed up output.
6565
if (enc_utf8)
6566
for (n = 0; n < len; ++n)
6567
if (text[n] >= 0x80)
6568
break;
6569
6570
#if defined(FEAT_DIRECTX)
6571
// Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6572
// required that unicode drawing routine, currently. So this forces it
6573
// enabled.
6574
if (IS_ENABLE_DIRECTX())
6575
n = 0; // Keep n < len, to enter block for unicode.
6576
#endif
6577
6578
// Check if the Unicode buffer exists and is big enough. Create it
6579
// with the same length as the multi-byte string, the number of wide
6580
// characters is always equal or smaller.
6581
if ((enc_utf8
6582
|| (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6583
|| enc_latin9)
6584
&& (unicodebuf == NULL || len > unibuflen))
6585
{
6586
vim_free(unicodebuf);
6587
unicodebuf = LALLOC_MULT(WCHAR, len);
6588
6589
vim_free(unicodepdy);
6590
unicodepdy = LALLOC_MULT(int, len);
6591
6592
unibuflen = len;
6593
}
6594
6595
if (enc_utf8 && n < len && unicodebuf != NULL)
6596
{
6597
// Output UTF-8 characters. Composing characters should be
6598
// handled here.
6599
int i;
6600
int wlen; // string length in words
6601
int cells; // cell width of string up to composing char
6602
int cw; // width of current cell
6603
int c;
6604
6605
wlen = 0;
6606
cells = 0;
6607
for (i = 0; i < len; )
6608
{
6609
c = utf_ptr2char(text + i);
6610
if (c >= 0x10000)
6611
{
6612
// Turn into UTF-16 encoding.
6613
unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6614
unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
6615
}
6616
else
6617
{
6618
unicodebuf[wlen++] = c;
6619
}
6620
6621
if (utf_iscomposing(c))
6622
cw = 0;
6623
else
6624
{
6625
cw = utf_char2cells(c);
6626
if (cw > 2) // don't use 4 for unprintable char
6627
cw = 1;
6628
}
6629
6630
if (unicodepdy != NULL)
6631
{
6632
// Use unicodepdy to make characters fit as we expect, even
6633
// when the font uses different widths (e.g., bold character
6634
// is wider).
6635
if (c >= 0x10000)
6636
{
6637
unicodepdy[wlen - 2] = cw * gui.char_width;
6638
unicodepdy[wlen - 1] = 0;
6639
}
6640
else
6641
unicodepdy[wlen - 1] = cw * gui.char_width;
6642
}
6643
cells += cw;
6644
i += utf_ptr2len_len(text + i, len - i);
6645
}
6646
#if defined(FEAT_DIRECTX)
6647
if (IS_ENABLE_DIRECTX())
6648
{
6649
// Add one to "cells" for italics.
6650
DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
6651
TEXT_X(col), TEXT_Y(row),
6652
FILL_X(cells + 1), FILL_Y(1) - p_linespace,
6653
gui.char_width, gui.currFgColor,
6654
foptions, pcliprect, unicodepdy);
6655
}
6656
else
6657
#endif
6658
ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6659
foptions, pcliprect, unicodebuf, wlen, unicodepdy);
6660
len = cells; // used for underlining
6661
}
6662
else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
6663
{
6664
// If we want to display codepage data, and the current CP is not the
6665
// ANSI one, we need to go via Unicode.
6666
if (unicodebuf != NULL)
6667
{
6668
if (enc_latin9)
6669
latin9_to_ucs(text, len, unicodebuf);
6670
else
6671
len = MultiByteToWideChar(enc_codepage,
6672
MB_PRECOMPOSED,
6673
(char *)text, len,
6674
(LPWSTR)unicodebuf, unibuflen);
6675
if (len != 0)
6676
{
6677
// Use unicodepdy to make characters fit as we expect, even
6678
// when the font uses different widths (e.g., bold character
6679
// is wider).
6680
if (unicodepdy != NULL)
6681
{
6682
int i;
6683
int cw;
6684
6685
for (i = 0; i < len; ++i)
6686
{
6687
cw = utf_char2cells(unicodebuf[i]);
6688
if (cw > 2)
6689
cw = 1;
6690
unicodepdy[i] = cw * gui.char_width;
6691
}
6692
}
6693
ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6694
foptions, pcliprect, unicodebuf, len, unicodepdy);
6695
}
6696
}
6697
}
6698
else
6699
{
6700
#ifdef FEAT_RIGHTLEFT
6701
// Windows will mess up RL text, so we have to draw it character by
6702
// character. Only do this if RL is on, since it's slow.
6703
if (curwin->w_p_rl)
6704
RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6705
foptions, pcliprect, (char *)text, len, padding);
6706
else
6707
#endif
6708
ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6709
foptions, pcliprect, (char *)text, len, padding);
6710
}
6711
6712
// Underline
6713
if (flags & DRAW_UNDERL)
6714
{
6715
// When p_linespace is 0, overwrite the bottom row of pixels.
6716
// Otherwise put the line just below the character.
6717
y = FILL_Y(row + 1) - 1;
6718
if (p_linespace > 1)
6719
y -= p_linespace - 1;
6720
draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
6721
}
6722
6723
// Strikethrough
6724
if (flags & DRAW_STRIKE)
6725
{
6726
y = FILL_Y(row + 1) - gui.char_height/2;
6727
draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
6728
}
6729
6730
// Undercurl
6731
if (flags & DRAW_UNDERC)
6732
{
6733
int x;
6734
int offset;
6735
static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
6736
6737
y = FILL_Y(row + 1) - 1;
6738
for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6739
{
6740
offset = val[x % 8];
6741
set_pixel(x, y - offset, gui.currSpColor);
6742
}
6743
}
6744
}
6745
6746
6747
/*
6748
* Output routines.
6749
*/
6750
6751
/*
6752
* Flush any output to the screen
6753
*/
6754
void
6755
gui_mch_flush(void)
6756
{
6757
#if defined(FEAT_DIRECTX)
6758
if (IS_ENABLE_DIRECTX())
6759
DWriteContext_Flush(s_dwc);
6760
#endif
6761
6762
GdiFlush();
6763
}
6764
6765
static void
6766
clear_rect(RECT *rcp)
6767
{
6768
fill_rect(rcp, NULL, gui.back_pixel);
6769
}
6770
6771
6772
void
6773
gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6774
{
6775
RECT workarea_rect;
6776
6777
get_work_area(&workarea_rect);
6778
6779
*screen_w = workarea_rect.right - workarea_rect.left
6780
- (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6781
pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
6782
6783
// FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6784
// the menubar for MSwin, we subtract it from the screen height, so that
6785
// the window size can be made to fit on the screen.
6786
*screen_h = workarea_rect.bottom - workarea_rect.top
6787
- (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6788
pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6789
- pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6790
- gui_mswin_get_menu_height(FALSE);
6791
}
6792
6793
6794
#if defined(FEAT_MENU) || defined(PROTO)
6795
/*
6796
* Add a sub menu to the menu bar.
6797
*/
6798
void
6799
gui_mch_add_menu(
6800
vimmenu_T *menu,
6801
int pos)
6802
{
6803
vimmenu_T *parent = menu->parent;
6804
6805
menu->submenu_id = CreatePopupMenu();
6806
menu->id = s_menu_id++;
6807
6808
if (menu_is_menubar(menu->name))
6809
{
6810
WCHAR *wn;
6811
MENUITEMINFOW infow;
6812
6813
wn = enc_to_utf16(menu->name, NULL);
6814
if (wn == NULL)
6815
return;
6816
6817
infow.cbSize = sizeof(infow);
6818
infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6819
| MIIM_SUBMENU;
6820
infow.dwItemData = (long_u)menu;
6821
infow.wID = menu->id;
6822
infow.fType = MFT_STRING;
6823
infow.dwTypeData = wn;
6824
infow.cch = (UINT)wcslen(wn);
6825
infow.hSubMenu = menu->submenu_id;
6826
InsertMenuItemW((parent == NULL)
6827
? s_menuBar : parent->submenu_id,
6828
(UINT)pos, TRUE, &infow);
6829
vim_free(wn);
6830
}
6831
6832
// Fix window size if menu may have wrapped
6833
if (parent == NULL)
6834
gui_mswin_get_menu_height(!gui.starting);
6835
# ifdef FEAT_TEAROFF
6836
else if (IsWindow(parent->tearoff_handle))
6837
rebuild_tearoff(parent);
6838
# endif
6839
}
6840
6841
void
6842
gui_mch_show_popupmenu(vimmenu_T *menu)
6843
{
6844
POINT mp;
6845
6846
(void)GetCursorPos(&mp);
6847
gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6848
}
6849
6850
void
6851
gui_make_popup(char_u *path_name, int mouse_pos)
6852
{
6853
vimmenu_T *menu = gui_find_menu(path_name);
6854
6855
if (menu == NULL)
6856
return;
6857
6858
POINT p;
6859
6860
// Find the position of the current cursor
6861
GetDCOrgEx(s_hdc, &p);
6862
if (mouse_pos)
6863
{
6864
int mx, my;
6865
6866
gui_mch_getmouse(&mx, &my);
6867
p.x += mx;
6868
p.y += my;
6869
}
6870
else if (curwin != NULL)
6871
{
6872
p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
6873
p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6874
}
6875
msg_scroll = FALSE;
6876
gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6877
}
6878
6879
# if defined(FEAT_TEAROFF) || defined(PROTO)
6880
/*
6881
* Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6882
* create it as a pseudo-"tearoff menu".
6883
*/
6884
void
6885
gui_make_tearoff(char_u *path_name)
6886
{
6887
vimmenu_T *menu = gui_find_menu(path_name);
6888
6889
// Found the menu, so tear it off.
6890
if (menu != NULL)
6891
gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6892
}
6893
# endif
6894
6895
/*
6896
* Add a menu item to a menu
6897
*/
6898
void
6899
gui_mch_add_menu_item(
6900
vimmenu_T *menu,
6901
int idx)
6902
{
6903
vimmenu_T *parent = menu->parent;
6904
6905
menu->id = s_menu_id++;
6906
menu->submenu_id = NULL;
6907
6908
# ifdef FEAT_TEAROFF
6909
if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6910
{
6911
InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6912
(UINT)menu->id, (LPCTSTR) s_htearbitmap);
6913
}
6914
else
6915
# endif
6916
# ifdef FEAT_TOOLBAR
6917
if (menu_is_toolbar(parent->name))
6918
{
6919
TBBUTTON newtb;
6920
6921
CLEAR_FIELD(newtb);
6922
if (menu_is_separator(menu->name))
6923
{
6924
newtb.iBitmap = 0;
6925
newtb.fsStyle = TBSTYLE_SEP;
6926
}
6927
else
6928
{
6929
newtb.iBitmap = get_toolbar_bitmap(menu);
6930
newtb.fsStyle = TBSTYLE_BUTTON;
6931
}
6932
newtb.idCommand = menu->id;
6933
newtb.fsState = TBSTATE_ENABLED;
6934
newtb.iString = 0;
6935
SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6936
(LPARAM)&newtb);
6937
menu->submenu_id = (HMENU)-1;
6938
}
6939
else
6940
# endif
6941
{
6942
WCHAR *wn;
6943
6944
wn = enc_to_utf16(menu->name, NULL);
6945
if (wn != NULL)
6946
{
6947
InsertMenuW(parent->submenu_id, (UINT)idx,
6948
(menu_is_separator(menu->name)
6949
? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6950
(UINT)menu->id, wn);
6951
vim_free(wn);
6952
}
6953
# ifdef FEAT_TEAROFF
6954
if (IsWindow(parent->tearoff_handle))
6955
rebuild_tearoff(parent);
6956
# endif
6957
}
6958
}
6959
6960
/*
6961
* Destroy the machine specific menu widget.
6962
*/
6963
void
6964
gui_mch_destroy_menu(vimmenu_T *menu)
6965
{
6966
# ifdef FEAT_TOOLBAR
6967
/*
6968
* is this a toolbar button?
6969
*/
6970
if (menu->submenu_id == (HMENU)-1)
6971
{
6972
int iButton;
6973
6974
iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6975
(WPARAM)menu->id, 0);
6976
SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6977
}
6978
else
6979
# endif
6980
{
6981
if (menu->parent != NULL
6982
&& menu_is_popup(menu->parent->dname)
6983
&& menu->parent->submenu_id != NULL)
6984
RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6985
else
6986
RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6987
if (menu->submenu_id != NULL)
6988
DestroyMenu(menu->submenu_id);
6989
# ifdef FEAT_TEAROFF
6990
if (IsWindow(menu->tearoff_handle))
6991
DestroyWindow(menu->tearoff_handle);
6992
if (menu->parent != NULL
6993
&& menu->parent->children != NULL
6994
&& IsWindow(menu->parent->tearoff_handle))
6995
{
6996
// This menu must not show up when rebuilding the tearoff window.
6997
menu->modes = 0;
6998
rebuild_tearoff(menu->parent);
6999
}
7000
# endif
7001
}
7002
}
7003
7004
# ifdef FEAT_TEAROFF
7005
static void
7006
rebuild_tearoff(vimmenu_T *menu)
7007
{
7008
//hackish
7009
char_u tbuf[128];
7010
RECT trect;
7011
RECT rct;
7012
RECT roct;
7013
int x, y;
7014
7015
HWND thwnd = menu->tearoff_handle;
7016
7017
GetWindowText(thwnd, (LPSTR)tbuf, 127);
7018
if (GetWindowRect(thwnd, &trect)
7019
&& GetWindowRect(s_hwnd, &rct)
7020
&& GetClientRect(s_hwnd, &roct))
7021
{
7022
x = trect.left - rct.left;
7023
y = (trect.top - rct.bottom + roct.bottom);
7024
}
7025
else
7026
{
7027
x = y = 0xffffL;
7028
}
7029
DestroyWindow(thwnd);
7030
if (menu->children != NULL)
7031
{
7032
gui_mch_tearoff(tbuf, menu, x, y);
7033
if (IsWindow(menu->tearoff_handle))
7034
(void) SetWindowPos(menu->tearoff_handle,
7035
NULL,
7036
(int)trect.left,
7037
(int)trect.top,
7038
0, 0,
7039
SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
7040
}
7041
}
7042
# endif // FEAT_TEAROFF
7043
7044
/*
7045
* Make a menu either grey or not grey.
7046
*/
7047
void
7048
gui_mch_menu_grey(
7049
vimmenu_T *menu,
7050
int grey)
7051
{
7052
# ifdef FEAT_TOOLBAR
7053
/*
7054
* is this a toolbar button?
7055
*/
7056
if (menu->submenu_id == (HMENU)-1)
7057
{
7058
SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
7059
(WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
7060
}
7061
else
7062
# endif
7063
(void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
7064
menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
7065
7066
# ifdef FEAT_TEAROFF
7067
if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
7068
{
7069
WORD menuID;
7070
HWND menuHandle;
7071
7072
/*
7073
* A tearoff button has changed state.
7074
*/
7075
if (menu->children == NULL)
7076
menuID = (WORD)(menu->id);
7077
else
7078
menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
7079
menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
7080
if (menuHandle)
7081
EnableWindow(menuHandle, !grey);
7082
7083
}
7084
# endif
7085
}
7086
7087
#endif // FEAT_MENU
7088
7089
7090
// define some macros used to make the dialogue creation more readable
7091
7092
#define add_word(x) *p++ = (x)
7093
#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
7094
7095
#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
7096
/*
7097
* stuff for dialogs
7098
*/
7099
7100
/*
7101
* The callback routine used by all the dialogs. Very simple. First,
7102
* acknowledges the INITDIALOG message so that Windows knows to do standard
7103
* dialog stuff (Return = default, Esc = cancel....) Second, if a button is
7104
* pressed, return that button's ID - IDCANCEL (2), which is the button's
7105
* number.
7106
*/
7107
static LRESULT CALLBACK
7108
dialog_callback(
7109
HWND hwnd,
7110
UINT message,
7111
WPARAM wParam,
7112
LPARAM lParam UNUSED)
7113
{
7114
if (message == WM_INITDIALOG)
7115
{
7116
CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
7117
// Set focus to the dialog. Set the default button, if specified.
7118
(void)SetFocus(hwnd);
7119
if (dialog_default_button > IDCANCEL)
7120
(void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
7121
else
7122
// We don't have a default, set focus on another element of the
7123
// dialog window, probably the icon
7124
(void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
7125
return FALSE;
7126
}
7127
7128
if (message == WM_COMMAND)
7129
{
7130
int button = LOWORD(wParam);
7131
7132
// Don't end the dialog if something was selected that was
7133
// not a button.
7134
if (button >= DLG_NONBUTTON_CONTROL)
7135
return TRUE;
7136
7137
// If the edit box exists, copy the string.
7138
if (s_textfield != NULL)
7139
{
7140
WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
7141
char_u *p;
7142
7143
GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
7144
p = utf16_to_enc(wp, NULL);
7145
if (p != NULL)
7146
{
7147
vim_strncpy(s_textfield, p, IOSIZE);
7148
vim_free(p);
7149
}
7150
vim_free(wp);
7151
}
7152
7153
/*
7154
* Need to check for IDOK because if the user just hits Return to
7155
* accept the default value, some reason this is what we get.
7156
*/
7157
if (button == IDOK)
7158
{
7159
if (dialog_default_button > IDCANCEL)
7160
EndDialog(hwnd, dialog_default_button);
7161
}
7162
else
7163
EndDialog(hwnd, button - IDCANCEL);
7164
return TRUE;
7165
}
7166
7167
if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7168
{
7169
EndDialog(hwnd, 0);
7170
return TRUE;
7171
}
7172
return FALSE;
7173
}
7174
7175
/*
7176
* Create a dialog dynamically from the parameter strings.
7177
* type = type of dialog (question, alert, etc.)
7178
* title = dialog title. may be NULL for default title.
7179
* message = text to display. Dialog sizes to accommodate it.
7180
* buttons = '\n' separated list of button captions, default first.
7181
* dfltbutton = number of default button.
7182
*
7183
* This routine returns 1 if the first button is pressed,
7184
* 2 for the second, etc.
7185
*
7186
* 0 indicates Esc was pressed.
7187
* -1 for unexpected error
7188
*
7189
* If stubbing out this fn, return 1.
7190
*/
7191
7192
static const char *dlg_icons[] = // must match names in resource file
7193
{
7194
"IDR_VIM",
7195
"IDR_VIM_ERROR",
7196
"IDR_VIM_ALERT",
7197
"IDR_VIM_INFO",
7198
"IDR_VIM_QUESTION"
7199
};
7200
7201
int
7202
gui_mch_dialog(
7203
int type,
7204
char_u *title,
7205
char_u *message,
7206
char_u *buttons,
7207
int dfltbutton,
7208
char_u *textfield,
7209
int ex_cmd UNUSED)
7210
{
7211
WORD *p, *pdlgtemplate, *pnumitems;
7212
DWORD *dwp;
7213
int numButtons;
7214
int *buttonWidths, *buttonPositions;
7215
int buttonYpos;
7216
int nchar, i;
7217
DWORD lStyle;
7218
int dlgwidth = 0;
7219
int dlgheight;
7220
int editboxheight;
7221
int horizWidth = 0;
7222
int msgheight;
7223
char_u *pstart;
7224
char_u *pend;
7225
char_u *last_white;
7226
char_u *tbuffer;
7227
RECT rect;
7228
HWND hwnd;
7229
HDC hdc;
7230
HFONT font, oldFont;
7231
TEXTMETRIC fontInfo;
7232
int fontHeight;
7233
int textWidth, minButtonWidth, messageWidth;
7234
int maxDialogWidth;
7235
int maxDialogHeight;
7236
int scroll_flag = 0;
7237
int vertical;
7238
int dlgPaddingX;
7239
int dlgPaddingY;
7240
# ifdef USE_SYSMENU_FONT
7241
LOGFONTW lfSysmenu;
7242
int use_lfSysmenu = FALSE;
7243
# endif
7244
garray_T ga;
7245
int l;
7246
int dlg_icon_width;
7247
int dlg_icon_height;
7248
int dpi;
7249
7250
# ifndef NO_CONSOLE
7251
// Don't output anything in silent mode ("ex -s")
7252
# ifdef VIMDLL
7253
if (!(gui.in_use || gui.starting))
7254
# endif
7255
if (silent_mode)
7256
return dfltbutton; // return default option
7257
# endif
7258
7259
if (s_hwnd == NULL)
7260
{
7261
load_dpi_func();
7262
s_dpi = dpi = pGetDpiForSystem();
7263
get_dialog_font_metrics();
7264
}
7265
else
7266
dpi = pGetDpiForSystem();
7267
7268
if ((type < 0) || (type > VIM_LAST_TYPE))
7269
type = 0;
7270
7271
// allocate some memory for dialog template
7272
// TODO should compute this really
7273
pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
7274
DLG_ALLOC_SIZE + STRLEN(message) * 2);
7275
7276
if (p == NULL)
7277
return -1;
7278
7279
/*
7280
* make a copy of 'buttons' to fiddle with it. compiler grizzles because
7281
* vim_strsave() doesn't take a const arg (why not?), so cast away the
7282
* const.
7283
*/
7284
tbuffer = vim_strsave(buttons);
7285
if (tbuffer == NULL)
7286
return -1;
7287
7288
--dfltbutton; // Change from one-based to zero-based
7289
7290
// Count buttons
7291
numButtons = 1;
7292
for (i = 0; tbuffer[i] != '\0'; i++)
7293
{
7294
if (tbuffer[i] == DLG_BUTTON_SEP)
7295
numButtons++;
7296
}
7297
if (dfltbutton >= numButtons)
7298
dfltbutton = -1;
7299
7300
// Allocate array to hold the width of each button
7301
buttonWidths = ALLOC_MULT(int, numButtons);
7302
if (buttonWidths == NULL)
7303
return -1;
7304
7305
// Allocate array to hold the X position of each button
7306
buttonPositions = ALLOC_MULT(int, numButtons);
7307
if (buttonPositions == NULL)
7308
return -1;
7309
7310
/*
7311
* Calculate how big the dialog must be.
7312
*/
7313
hwnd = GetDesktopWindow();
7314
hdc = GetWindowDC(hwnd);
7315
# ifdef USE_SYSMENU_FONT
7316
if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7317
{
7318
font = CreateFontIndirectW(&lfSysmenu);
7319
use_lfSysmenu = TRUE;
7320
}
7321
else
7322
# endif
7323
font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7324
0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7325
7326
oldFont = SelectFont(hdc, font);
7327
dlgPaddingX = DLG_PADDING_X;
7328
dlgPaddingY = DLG_PADDING_Y;
7329
7330
GetTextMetrics(hdc, &fontInfo);
7331
fontHeight = fontInfo.tmHeight;
7332
7333
// Minimum width for horizontal button
7334
minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
7335
7336
// Maximum width of a dialog, if possible
7337
if (s_hwnd == NULL)
7338
{
7339
RECT workarea_rect;
7340
7341
// We don't have a window, use the desktop area.
7342
get_work_area(&workarea_rect);
7343
maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7344
if (maxDialogWidth > adjust_by_system_dpi(600))
7345
maxDialogWidth = adjust_by_system_dpi(600);
7346
// Leave some room for the taskbar.
7347
maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
7348
}
7349
else
7350
{
7351
// Use our own window for the size, unless it's very small.
7352
GetWindowRect(s_hwnd, &rect);
7353
maxDialogWidth = rect.right - rect.left
7354
- (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
7355
pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
7356
if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
7357
maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
7358
7359
maxDialogHeight = rect.bottom - rect.top
7360
- (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
7361
pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
7362
- pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
7363
if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
7364
maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
7365
}
7366
7367
// Set dlgwidth to width of message.
7368
// Copy the message into "ga", changing NL to CR-NL and inserting line
7369
// breaks where needed.
7370
pstart = message;
7371
messageWidth = 0;
7372
msgheight = 0;
7373
ga_init2(&ga, sizeof(char), 500);
7374
do
7375
{
7376
msgheight += fontHeight; // at least one line
7377
7378
// Need to figure out where to break the string. The system does it
7379
// at a word boundary, which would mean we can't compute the number of
7380
// wrapped lines.
7381
textWidth = 0;
7382
last_white = NULL;
7383
for (pend = pstart; *pend != NUL && *pend != '\n'; )
7384
{
7385
l = (*mb_ptr2len)(pend);
7386
if (l == 1 && VIM_ISWHITE(*pend)
7387
&& textWidth > maxDialogWidth * 3 / 4)
7388
last_white = pend;
7389
textWidth += GetTextWidthEnc(hdc, pend, l);
7390
if (textWidth >= maxDialogWidth)
7391
{
7392
// Line will wrap.
7393
messageWidth = maxDialogWidth;
7394
msgheight += fontHeight;
7395
textWidth = 0;
7396
7397
if (last_white != NULL)
7398
{
7399
// break the line just after a space
7400
if (pend > last_white)
7401
ga.ga_len -= (int)(pend - (last_white + 1));
7402
pend = last_white + 1;
7403
last_white = NULL;
7404
}
7405
ga_append(&ga, '\r');
7406
ga_append(&ga, '\n');
7407
continue;
7408
}
7409
7410
while (--l >= 0)
7411
ga_append(&ga, *pend++);
7412
}
7413
if (textWidth > messageWidth)
7414
messageWidth = textWidth;
7415
7416
ga_append(&ga, '\r');
7417
ga_append(&ga, '\n');
7418
pstart = pend + 1;
7419
} while (*pend != NUL);
7420
7421
if (ga.ga_data != NULL)
7422
message = ga.ga_data;
7423
7424
messageWidth += 10; // roundoff space
7425
7426
dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7427
dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
7428
7429
// Add width of icon to dlgwidth, and some space
7430
dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7431
+ pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7432
7433
if (msgheight < dlg_icon_height)
7434
msgheight = dlg_icon_height;
7435
7436
/*
7437
* Check button names. A long one will make the dialog wider.
7438
* When called early (-register error message) p_go isn't initialized.
7439
*/
7440
vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
7441
if (!vertical)
7442
{
7443
// Place buttons horizontally if they fit.
7444
horizWidth = dlgPaddingX;
7445
pstart = tbuffer;
7446
i = 0;
7447
do
7448
{
7449
pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7450
if (pend == NULL)
7451
pend = pstart + STRLEN(pstart); // Last button name.
7452
textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
7453
if (textWidth < minButtonWidth)
7454
textWidth = minButtonWidth;
7455
textWidth += dlgPaddingX; // Padding within button
7456
buttonWidths[i] = textWidth;
7457
buttonPositions[i++] = horizWidth;
7458
horizWidth += textWidth + dlgPaddingX; // Pad between buttons
7459
pstart = pend + 1;
7460
} while (*pend != NUL);
7461
7462
if (horizWidth > maxDialogWidth)
7463
vertical = TRUE; // Too wide to fit on the screen.
7464
else if (horizWidth > dlgwidth)
7465
dlgwidth = horizWidth;
7466
}
7467
7468
if (vertical)
7469
{
7470
// Stack buttons vertically.
7471
pstart = tbuffer;
7472
do
7473
{
7474
pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7475
if (pend == NULL)
7476
pend = pstart + STRLEN(pstart); // Last button name.
7477
textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
7478
textWidth += dlgPaddingX; // Padding within button
7479
textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
7480
if (textWidth > dlgwidth)
7481
dlgwidth = textWidth;
7482
pstart = pend + 1;
7483
} while (*pend != NUL);
7484
}
7485
7486
if (dlgwidth < DLG_MIN_WIDTH)
7487
dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
7488
7489
// start to fill in the dlgtemplate information. addressing by WORDs
7490
lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
7491
7492
add_long(lStyle);
7493
add_long(0); // (lExtendedStyle)
7494
pnumitems = p; //save where the number of items must be stored
7495
add_word(0); // NumberOfItems(will change later)
7496
add_word(10); // x
7497
add_word(10); // y
7498
add_word(PixelToDialogX(dlgwidth)); // cx
7499
7500
// Dialog height.
7501
if (vertical)
7502
dlgheight = msgheight + 2 * dlgPaddingY
7503
+ DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
7504
else
7505
dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7506
7507
// Dialog needs to be taller if contains an edit box.
7508
editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7509
if (textfield != NULL)
7510
dlgheight += editboxheight;
7511
7512
// Restrict the size to a maximum. Causes a scrollbar to show up.
7513
if (dlgheight > maxDialogHeight)
7514
{
7515
msgheight = msgheight - (dlgheight - maxDialogHeight);
7516
dlgheight = maxDialogHeight;
7517
scroll_flag = WS_VSCROLL;
7518
// Make sure scrollbar doesn't appear in the middle of the dialog
7519
messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
7520
}
7521
7522
add_word(PixelToDialogY(dlgheight));
7523
7524
add_word(0); // Menu
7525
add_word(0); // Class
7526
7527
// copy the title of the dialog
7528
nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7529
: (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
7530
p += nchar;
7531
7532
// do the font, since DS_3DLOOK doesn't work properly
7533
# ifdef USE_SYSMENU_FONT
7534
if (use_lfSysmenu)
7535
{
7536
// point size
7537
*p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7538
GetDeviceCaps(hdc, LOGPIXELSY));
7539
wcscpy(p, lfSysmenu.lfFaceName);
7540
nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
7541
}
7542
else
7543
# endif
7544
{
7545
*p++ = DLG_FONT_POINT_SIZE; // point size
7546
nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7547
}
7548
p += nchar;
7549
7550
buttonYpos = msgheight + 2 * dlgPaddingY;
7551
7552
if (textfield != NULL)
7553
buttonYpos += editboxheight;
7554
7555
pstart = tbuffer;
7556
if (!vertical)
7557
horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
7558
for (i = 0; i < numButtons; i++)
7559
{
7560
// get end of this button.
7561
for ( pend = pstart;
7562
*pend && (*pend != DLG_BUTTON_SEP);
7563
pend++)
7564
;
7565
7566
if (*pend)
7567
*pend = '\0';
7568
7569
/*
7570
* old NOTE:
7571
* setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7572
* the focus to the first tab-able button and in so doing makes that
7573
* the default!! Grrr. Workaround: Make the default button the only
7574
* one with WS_TABSTOP style. Means user can't tab between buttons, but
7575
* he/she can use arrow keys.
7576
*
7577
* new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
7578
* right button when hitting <Enter>. E.g., for the ":confirm quit"
7579
* dialog. Also needed for when the textfield is the default control.
7580
* It appears to work now (perhaps not on Win95?).
7581
*/
7582
if (vertical)
7583
{
7584
p = add_dialog_element(p,
7585
(i == dfltbutton
7586
? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7587
PixelToDialogX(DLG_VERT_PADDING_X),
7588
PixelToDialogY(buttonYpos // TBK
7589
+ 2 * fontHeight * i),
7590
PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7591
(WORD)(PixelToDialogY(2 * fontHeight) - 1),
7592
(WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
7593
}
7594
else
7595
{
7596
p = add_dialog_element(p,
7597
(i == dfltbutton
7598
? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7599
PixelToDialogX(horizWidth + buttonPositions[i]),
7600
PixelToDialogY(buttonYpos), // TBK
7601
PixelToDialogX(buttonWidths[i]),
7602
(WORD)(PixelToDialogY(2 * fontHeight) - 1),
7603
(WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
7604
}
7605
pstart = pend + 1; //next button
7606
}
7607
*pnumitems += numButtons;
7608
7609
// Vim icon
7610
p = add_dialog_element(p, SS_ICON,
7611
PixelToDialogX(dlgPaddingX),
7612
PixelToDialogY(dlgPaddingY),
7613
PixelToDialogX(dlg_icon_width),
7614
PixelToDialogY(dlg_icon_height),
7615
DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7616
dlg_icons[type]);
7617
7618
// Dialog message
7619
p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7620
PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
7621
PixelToDialogY(dlgPaddingY),
7622
(WORD)(PixelToDialogX(messageWidth) + 1),
7623
PixelToDialogY(msgheight),
7624
DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
7625
7626
// Edit box
7627
if (textfield != NULL)
7628
{
7629
p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7630
PixelToDialogX(2 * dlgPaddingX),
7631
PixelToDialogY(2 * dlgPaddingY + msgheight),
7632
PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7633
PixelToDialogY(fontHeight + dlgPaddingY),
7634
DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
7635
*pnumitems += 1;
7636
}
7637
7638
*pnumitems += 2;
7639
7640
SelectFont(hdc, oldFont);
7641
DeleteObject(font);
7642
ReleaseDC(hwnd, hdc);
7643
7644
// Let the dialog_callback() function know which button to make default
7645
// If we have an edit box, make that the default. We also need to tell
7646
// dialog_callback() if this dialog contains an edit box or not. We do
7647
// this by setting s_textfield if it does.
7648
if (textfield != NULL)
7649
{
7650
dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7651
s_textfield = textfield;
7652
}
7653
else
7654
{
7655
dialog_default_button = IDCANCEL + 1 + dfltbutton;
7656
s_textfield = NULL;
7657
}
7658
7659
// show the dialog box modally and get a return value
7660
nchar = (int)DialogBoxIndirect(
7661
g_hinst,
7662
(LPDLGTEMPLATE)pdlgtemplate,
7663
s_hwnd,
7664
(DLGPROC)dialog_callback);
7665
7666
LocalFree(LocalHandle(pdlgtemplate));
7667
vim_free(tbuffer);
7668
vim_free(buttonWidths);
7669
vim_free(buttonPositions);
7670
vim_free(ga.ga_data);
7671
7672
// Focus back to our window (for when MDI is used).
7673
(void)SetFocus(s_hwnd);
7674
7675
return nchar;
7676
}
7677
7678
#endif // FEAT_GUI_DIALOG
7679
7680
/*
7681
* Put a simple element (basic class) onto a dialog template in memory.
7682
* return a pointer to where the next item should be added.
7683
*
7684
* parameters:
7685
* lStyle = additional style flags
7686
* (be careful, NT3.51 & Win32s will ignore the new ones)
7687
* x,y = x & y positions IN DIALOG UNITS
7688
* w,h = width and height IN DIALOG UNITS
7689
* Id = ID used in messages
7690
* clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7691
* caption = usually text or resource name
7692
*
7693
* TODO: use the length information noted here to enable the dialog creation
7694
* routines to work out more exactly how much memory they need to alloc.
7695
*/
7696
static PWORD
7697
add_dialog_element(
7698
PWORD p,
7699
DWORD lStyle,
7700
WORD x,
7701
WORD y,
7702
WORD w,
7703
WORD h,
7704
WORD Id,
7705
WORD clss,
7706
const char *caption)
7707
{
7708
int nchar;
7709
7710
p = lpwAlign(p); // Align to dword boundary
7711
lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7712
*p++ = LOWORD(lStyle);
7713
*p++ = HIWORD(lStyle);
7714
*p++ = 0; // LOWORD (lExtendedStyle)
7715
*p++ = 0; // HIWORD (lExtendedStyle)
7716
*p++ = x;
7717
*p++ = y;
7718
*p++ = w;
7719
*p++ = h;
7720
*p++ = Id; //9 or 10 words in all
7721
7722
*p++ = (WORD)0xffff;
7723
*p++ = clss; //2 more here
7724
7725
nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
7726
p += nchar;
7727
7728
*p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7729
7730
return p; // total = 15 + strlen(caption) words
7731
// bytes read = 2 * total
7732
}
7733
7734
7735
/*
7736
* Helper routine. Take an input pointer, return closest pointer that is
7737
* aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7738
*/
7739
static LPWORD
7740
lpwAlign(
7741
LPWORD lpIn)
7742
{
7743
long_u ul;
7744
7745
ul = (long_u)lpIn;
7746
ul += 3;
7747
ul >>= 2;
7748
ul <<= 2;
7749
return (LPWORD)ul;
7750
}
7751
7752
/*
7753
* Helper routine. Takes second parameter as Ansi string, copies it to first
7754
* parameter as wide character (16-bits / char) string, and returns integer
7755
* number of wide characters (words) in string (including the trailing wide
7756
* char NULL). Partly taken from the Win32SDK samples.
7757
* If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7758
* ACP is used for "lpAnsiIn". */
7759
static int
7760
nCopyAnsiToWideChar(
7761
LPWORD lpWCStr,
7762
LPSTR lpAnsiIn,
7763
BOOL use_enc)
7764
{
7765
int nChar = 0;
7766
int len = lstrlen(lpAnsiIn) + 1; // include NUL character
7767
int i;
7768
WCHAR *wn;
7769
7770
if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
7771
{
7772
// Not a codepage, use our own conversion function.
7773
wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
7774
if (wn != NULL)
7775
{
7776
wcscpy(lpWCStr, wn);
7777
nChar = (int)wcslen(wn) + 1;
7778
vim_free(wn);
7779
}
7780
}
7781
if (nChar == 0)
7782
// Use Win32 conversion function.
7783
nChar = MultiByteToWideChar(
7784
enc_codepage > 0 ? enc_codepage : CP_ACP,
7785
MB_PRECOMPOSED,
7786
lpAnsiIn, len,
7787
lpWCStr, len);
7788
for (i = 0; i < nChar; ++i)
7789
if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
7790
lpWCStr[i] = (WORD)' ';
7791
7792
return nChar;
7793
}
7794
7795
7796
#ifdef FEAT_TEAROFF
7797
/*
7798
* Lookup menu handle from "menu_id".
7799
*/
7800
static HMENU
7801
tearoff_lookup_menuhandle(
7802
vimmenu_T *menu,
7803
WORD menu_id)
7804
{
7805
for ( ; menu != NULL; menu = menu->next)
7806
{
7807
if (menu->modes == 0) // this menu has just been deleted
7808
continue;
7809
if (menu_is_separator(menu->dname))
7810
continue;
7811
if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7812
return menu->submenu_id;
7813
}
7814
return NULL;
7815
}
7816
7817
/*
7818
* The callback function for all the modeless dialogs that make up the
7819
* "tearoff menus" Very simple - forward button presses (to fool Vim into
7820
* thinking its menus have been clicked), and go away when closed.
7821
*/
7822
static LRESULT CALLBACK
7823
tearoff_callback(
7824
HWND hwnd,
7825
UINT message,
7826
WPARAM wParam,
7827
LPARAM lParam)
7828
{
7829
if (message == WM_INITDIALOG)
7830
{
7831
SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
7832
return (TRUE);
7833
}
7834
7835
// May show the mouse pointer again.
7836
HandleMouseHide(message, lParam);
7837
7838
if (message == WM_COMMAND)
7839
{
7840
if ((WORD)(LOWORD(wParam)) & 0x8000)
7841
{
7842
POINT mp;
7843
RECT rect;
7844
7845
if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7846
{
7847
vimmenu_T *menu;
7848
7849
menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
7850
(void)TrackPopupMenu(
7851
tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
7852
TPM_LEFTALIGN | TPM_LEFTBUTTON,
7853
(int)rect.right - 8,
7854
(int)mp.y,
7855
(int)0, // reserved param
7856
s_hwnd,
7857
NULL);
7858
/*
7859
* NOTE: The pop-up menu can eat the mouse up event.
7860
* We deal with this in normal.c.
7861
*/
7862
}
7863
}
7864
else
7865
// Pass on messages to the main Vim window
7866
PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7867
/*
7868
* Give main window the focus back: this is so after
7869
* choosing a tearoff button you can start typing again
7870
* straight away.
7871
*/
7872
(void)SetFocus(s_hwnd);
7873
return TRUE;
7874
}
7875
if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7876
{
7877
DestroyWindow(hwnd);
7878
return TRUE;
7879
}
7880
7881
// When moved around, give main window the focus back.
7882
if (message == WM_EXITSIZEMOVE)
7883
(void)SetActiveWindow(s_hwnd);
7884
7885
return FALSE;
7886
}
7887
#endif
7888
7889
7890
/*
7891
* Computes the dialog base units based on the current dialog font.
7892
* We don't use the GetDialogBaseUnits() API, because we don't use the
7893
* (old-style) system font.
7894
*/
7895
static void
7896
get_dialog_font_metrics(void)
7897
{
7898
HDC hdc;
7899
HFONT hfontTools = 0;
7900
SIZE size;
7901
#ifdef USE_SYSMENU_FONT
7902
LOGFONTW lfSysmenu;
7903
#endif
7904
7905
#ifdef USE_SYSMENU_FONT
7906
if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7907
hfontTools = CreateFontIndirectW(&lfSysmenu);
7908
else
7909
#endif
7910
hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7911
0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7912
7913
hdc = GetDC(s_hwnd);
7914
SelectObject(hdc, hfontTools);
7915
GetAverageFontSize(hdc, &size);
7916
ReleaseDC(s_hwnd, hdc);
7917
7918
s_dlgfntwidth = (WORD)size.cx;
7919
s_dlgfntheight = (WORD)size.cy;
7920
}
7921
7922
#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7923
/*
7924
* Create a pseudo-"tearoff menu" based on the child
7925
* items of a given menu pointer.
7926
*/
7927
static void
7928
gui_mch_tearoff(
7929
char_u *title,
7930
vimmenu_T *menu,
7931
int initX,
7932
int initY)
7933
{
7934
WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7935
int template_len;
7936
int nchar, textWidth, submenuWidth;
7937
DWORD lStyle;
7938
DWORD lExtendedStyle;
7939
WORD dlgwidth;
7940
WORD menuID;
7941
vimmenu_T *pmenu;
7942
vimmenu_T *top_menu;
7943
vimmenu_T *the_menu = menu;
7944
HWND hwnd;
7945
HDC hdc;
7946
HFONT font, oldFont;
7947
int col, spaceWidth, len;
7948
int columnWidths[2];
7949
char_u *label, *text;
7950
int acLen = 0;
7951
int nameLen;
7952
int padding0, padding1, padding2 = 0;
7953
int sepPadding=0;
7954
int x;
7955
int y;
7956
# ifdef USE_SYSMENU_FONT
7957
LOGFONTW lfSysmenu;
7958
int use_lfSysmenu = FALSE;
7959
# endif
7960
7961
/*
7962
* If this menu is already torn off, move it to the mouse position.
7963
*/
7964
if (IsWindow(menu->tearoff_handle))
7965
{
7966
POINT mp;
7967
if (GetCursorPos(&mp))
7968
{
7969
SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7970
SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7971
}
7972
return;
7973
}
7974
7975
/*
7976
* Create a new tearoff.
7977
*/
7978
if (*title == MNU_HIDDEN_CHAR)
7979
title++;
7980
7981
// Allocate memory to store the dialog template. It's made bigger when
7982
// needed.
7983
template_len = DLG_ALLOC_SIZE;
7984
pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7985
if (p == NULL)
7986
return;
7987
7988
hwnd = GetDesktopWindow();
7989
hdc = GetWindowDC(hwnd);
7990
# ifdef USE_SYSMENU_FONT
7991
if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7992
{
7993
font = CreateFontIndirectW(&lfSysmenu);
7994
use_lfSysmenu = TRUE;
7995
}
7996
else
7997
# endif
7998
font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7999
0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
8000
8001
oldFont = SelectFont(hdc, font);
8002
8003
// Calculate width of a single space. Used for padding columns to the
8004
// right width.
8005
spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
8006
8007
// Figure out max width of the text column, the accelerator column and the
8008
// optional submenu column.
8009
submenuWidth = 0;
8010
for (col = 0; col < 2; col++)
8011
{
8012
columnWidths[col] = 0;
8013
FOR_ALL_CHILD_MENUS(menu, pmenu)
8014
{
8015
// Use "dname" here to compute the width of the visible text.
8016
text = (col == 0) ? pmenu->dname : pmenu->actext;
8017
if (text != NULL && *text != NUL)
8018
{
8019
textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
8020
if (textWidth > columnWidths[col])
8021
columnWidths[col] = textWidth;
8022
}
8023
if (pmenu->children != NULL)
8024
submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
8025
}
8026
}
8027
if (columnWidths[1] == 0)
8028
{
8029
// no accelerators
8030
if (submenuWidth != 0)
8031
columnWidths[0] += submenuWidth;
8032
else
8033
columnWidths[0] += spaceWidth;
8034
}
8035
else
8036
{
8037
// there is an accelerator column
8038
columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
8039
columnWidths[1] += submenuWidth;
8040
}
8041
8042
/*
8043
* Now find the total width of our 'menu'.
8044
*/
8045
textWidth = columnWidths[0] + columnWidths[1];
8046
if (submenuWidth != 0)
8047
{
8048
submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
8049
(int)STRLEN(TEAROFF_SUBMENU_LABEL));
8050
textWidth += submenuWidth;
8051
}
8052
dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
8053
if (textWidth > dlgwidth)
8054
dlgwidth = textWidth;
8055
dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
8056
8057
// start to fill in the dlgtemplate information. addressing by WORDs
8058
lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
8059
8060
lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
8061
*p++ = LOWORD(lStyle);
8062
*p++ = HIWORD(lStyle);
8063
*p++ = LOWORD(lExtendedStyle);
8064
*p++ = HIWORD(lExtendedStyle);
8065
pnumitems = p; // save where the number of items must be stored
8066
*p++ = 0; // NumberOfItems(will change later)
8067
gui_mch_getmouse(&x, &y);
8068
if (initX == 0xffffL)
8069
*p++ = PixelToDialogX(x); // x
8070
else
8071
*p++ = PixelToDialogX(initX); // x
8072
if (initY == 0xffffL)
8073
*p++ = PixelToDialogY(y); // y
8074
else
8075
*p++ = PixelToDialogY(initY); // y
8076
*p++ = PixelToDialogX(dlgwidth); // cx
8077
ptrueheight = p;
8078
*p++ = 0; // dialog height: changed later anyway
8079
*p++ = 0; // Menu
8080
*p++ = 0; // Class
8081
8082
// copy the title of the dialog
8083
nchar = nCopyAnsiToWideChar(p, ((*title)
8084
? (LPSTR)title
8085
: (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
8086
p += nchar;
8087
8088
// do the font, since DS_3DLOOK doesn't work properly
8089
# ifdef USE_SYSMENU_FONT
8090
if (use_lfSysmenu)
8091
{
8092
// point size
8093
*p++ = -MulDiv(lfSysmenu.lfHeight, 72,
8094
GetDeviceCaps(hdc, LOGPIXELSY));
8095
wcscpy(p, lfSysmenu.lfFaceName);
8096
nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
8097
}
8098
else
8099
# endif
8100
{
8101
*p++ = DLG_FONT_POINT_SIZE; // point size
8102
nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
8103
}
8104
p += nchar;
8105
8106
/*
8107
* Loop over all the items in the menu.
8108
* But skip over the tearbar.
8109
*/
8110
if (STRCMP(menu->children->name, TEAR_STRING) == 0)
8111
menu = menu->children->next;
8112
else
8113
menu = menu->children;
8114
top_menu = menu;
8115
for ( ; menu != NULL; menu = menu->next)
8116
{
8117
if (menu->modes == 0) // this menu has just been deleted
8118
continue;
8119
if (menu_is_separator(menu->dname))
8120
{
8121
sepPadding += 3;
8122
continue;
8123
}
8124
8125
// Check if there still is plenty of room in the template. Make it
8126
// larger when needed.
8127
if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8128
{
8129
WORD *newp;
8130
8131
newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8132
if (newp != NULL)
8133
{
8134
template_len += 4096;
8135
mch_memmove(newp, pdlgtemplate,
8136
(char *)p - (char *)pdlgtemplate);
8137
p = newp + (p - pdlgtemplate);
8138
pnumitems = newp + (pnumitems - pdlgtemplate);
8139
ptrueheight = newp + (ptrueheight - pdlgtemplate);
8140
LocalFree(LocalHandle(pdlgtemplate));
8141
pdlgtemplate = newp;
8142
}
8143
}
8144
8145
// Figure out minimal length of this menu label. Use "name" for the
8146
// actual text, "dname" for estimating the displayed size. "name"
8147
// has "&a" for mnemonic and includes the accelerator.
8148
len = nameLen = (int)STRLEN(menu->name);
8149
padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8150
(int)STRLEN(menu->dname))) / spaceWidth;
8151
len += padding0;
8152
8153
if (menu->actext != NULL)
8154
{
8155
acLen = (int)STRLEN(menu->actext);
8156
len += acLen;
8157
textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8158
}
8159
else
8160
textWidth = 0;
8161
padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8162
len += padding1;
8163
8164
if (menu->children == NULL)
8165
{
8166
padding2 = submenuWidth / spaceWidth;
8167
len += padding2;
8168
menuID = (WORD)(menu->id);
8169
}
8170
else
8171
{
8172
len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
8173
menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
8174
}
8175
8176
// Allocate menu label and fill it in
8177
text = label = alloc(len + 1);
8178
if (label == NULL)
8179
break;
8180
8181
vim_strncpy(text, menu->name, nameLen);
8182
text = vim_strchr(text, TAB); // stop at TAB before actext
8183
if (text == NULL)
8184
text = label + nameLen; // no actext, use whole name
8185
while (padding0-- > 0)
8186
*text++ = ' ';
8187
if (menu->actext != NULL)
8188
{
8189
STRNCPY(text, menu->actext, acLen);
8190
text += acLen;
8191
}
8192
while (padding1-- > 0)
8193
*text++ = ' ';
8194
if (menu->children != NULL)
8195
{
8196
STRCPY(text, TEAROFF_SUBMENU_LABEL);
8197
text += STRLEN(TEAROFF_SUBMENU_LABEL);
8198
}
8199
else
8200
{
8201
while (padding2-- > 0)
8202
*text++ = ' ';
8203
}
8204
*text = NUL;
8205
8206
/*
8207
* BS_LEFT will just be ignored on Win32s/NT3.5x - on
8208
* W95/NT4 it makes the tear-off look more like a menu.
8209
*/
8210
p = add_dialog_element(p,
8211
BS_PUSHBUTTON|BS_LEFT,
8212
(WORD)PixelToDialogX(TEAROFF_PADDING_X),
8213
(WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8214
(WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8215
(WORD)12,
8216
menuID, (WORD)0x0080, (char *)label);
8217
vim_free(label);
8218
(*pnumitems)++;
8219
}
8220
8221
*ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8222
8223
8224
// show modelessly
8225
the_menu->tearoff_handle = CreateDialogIndirectParam(
8226
g_hinst,
8227
(LPDLGTEMPLATE)pdlgtemplate,
8228
s_hwnd,
8229
(DLGPROC)tearoff_callback,
8230
(LPARAM)top_menu);
8231
8232
LocalFree(LocalHandle(pdlgtemplate));
8233
SelectFont(hdc, oldFont);
8234
DeleteObject(font);
8235
ReleaseDC(hwnd, hdc);
8236
8237
/*
8238
* Reassert ourselves as the active window. This is so that after creating
8239
* a tearoff, the user doesn't have to click with the mouse just to start
8240
* typing again!
8241
*/
8242
(void)SetActiveWindow(s_hwnd);
8243
8244
// make sure the right buttons are enabled
8245
force_menu_update = TRUE;
8246
}
8247
#endif
8248
8249
#if defined(FEAT_TOOLBAR) || defined(PROTO)
8250
# include "gui_w32_rc.h"
8251
8252
/*
8253
* Create the toolbar, initially unpopulated.
8254
* (just like the menu, there are no defaults, it's all
8255
* set up through menu.vim)
8256
*/
8257
static void
8258
initialise_toolbar(void)
8259
{
8260
InitCommonControls();
8261
s_toolbarhwnd = CreateToolbarEx(
8262
s_hwnd,
8263
WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8264
4000, //any old big number
8265
31, //number of images in initial bitmap
8266
g_hinst,
8267
IDR_TOOLBAR1, // id of initial bitmap
8268
NULL,
8269
0, // initial number of buttons
8270
TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8271
TOOLBAR_BUTTON_HEIGHT,
8272
TOOLBAR_BUTTON_WIDTH,
8273
TOOLBAR_BUTTON_HEIGHT,
8274
sizeof(TBBUTTON)
8275
);
8276
8277
// Remove transparency from the toolbar to prevent the main window
8278
// background colour showing through
8279
SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
8280
SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
8281
8282
s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
8283
8284
gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
8285
8286
update_toolbar_size();
8287
}
8288
8289
static void
8290
update_toolbar_size(void)
8291
{
8292
int w, h;
8293
TBMETRICS tbm;
8294
8295
tbm.cbSize = sizeof(TBMETRICS);
8296
tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
8297
SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
8298
//TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
8299
//TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
8300
8301
w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
8302
h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
8303
//TRACE("button size: %d, %d", w, h);
8304
SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
8305
gui.toolbar_height = h + 6;
8306
8307
//DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
8308
//TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
8309
8310
// TODO:
8311
// Currently, this function only updates the size of toolbar buttons.
8312
// It would be nice if the toolbar images are resized based on DPI.
8313
}
8314
8315
static LRESULT CALLBACK
8316
toolbar_wndproc(
8317
HWND hwnd,
8318
UINT uMsg,
8319
WPARAM wParam,
8320
LPARAM lParam)
8321
{
8322
HandleMouseHide(uMsg, lParam);
8323
return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8324
}
8325
8326
static int
8327
get_toolbar_bitmap(vimmenu_T *menu)
8328
{
8329
int i = -1;
8330
8331
/*
8332
* Check user bitmaps first, unless builtin is specified.
8333
*/
8334
if (!menu->icon_builtin)
8335
{
8336
char_u fname[MAXPATHL];
8337
HANDLE hbitmap = NULL;
8338
8339
if (menu->iconfile != NULL)
8340
{
8341
gui_find_iconfile(menu->iconfile, fname, "bmp");
8342
hbitmap = LoadImage(
8343
NULL,
8344
(LPCSTR)fname,
8345
IMAGE_BITMAP,
8346
TOOLBAR_BUTTON_WIDTH,
8347
TOOLBAR_BUTTON_HEIGHT,
8348
LR_LOADFROMFILE |
8349
LR_LOADMAP3DCOLORS
8350
);
8351
}
8352
8353
/*
8354
* If the LoadImage call failed, or the "icon=" file
8355
* didn't exist or wasn't specified, try the menu name
8356
*/
8357
if (hbitmap == NULL
8358
&& (gui_find_bitmap(
8359
# ifdef FEAT_MULTI_LANG
8360
menu->en_dname != NULL ? menu->en_dname :
8361
# endif
8362
menu->dname, fname, "bmp") == OK))
8363
hbitmap = LoadImage(
8364
NULL,
8365
(LPCSTR)fname,
8366
IMAGE_BITMAP,
8367
TOOLBAR_BUTTON_WIDTH,
8368
TOOLBAR_BUTTON_HEIGHT,
8369
LR_LOADFROMFILE |
8370
LR_LOADMAP3DCOLORS
8371
);
8372
8373
if (hbitmap != NULL)
8374
{
8375
TBADDBITMAP tbAddBitmap;
8376
8377
tbAddBitmap.hInst = NULL;
8378
tbAddBitmap.nID = (long_u)hbitmap;
8379
8380
i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8381
(WPARAM)1, (LPARAM)&tbAddBitmap);
8382
// i will be set to -1 if it fails
8383
}
8384
}
8385
if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8386
i = menu->iconidx;
8387
8388
return i;
8389
}
8390
#endif
8391
8392
#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8393
static void
8394
initialise_tabline(void)
8395
{
8396
InitCommonControls();
8397
8398
s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
8399
WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
8400
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8401
CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
8402
s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
8403
8404
gui.tabline_height = TABLINE_HEIGHT;
8405
8406
set_tabline_font();
8407
}
8408
8409
/*
8410
* Get tabpage_T from POINT.
8411
*/
8412
static tabpage_T *
8413
GetTabFromPoint(
8414
HWND hWnd,
8415
POINT pt)
8416
{
8417
tabpage_T *ptp = NULL;
8418
8419
if (gui_mch_showing_tabline())
8420
{
8421
TCHITTESTINFO htinfo;
8422
htinfo.pt = pt;
8423
// ignore if a window under cursor is not tabcontrol.
8424
if (s_tabhwnd == hWnd)
8425
{
8426
int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8427
if (idx != -1)
8428
ptp = find_tabpage(idx + 1);
8429
}
8430
}
8431
return ptp;
8432
}
8433
8434
static POINT s_pt = {0, 0};
8435
static HCURSOR s_hCursor = NULL;
8436
8437
static LRESULT CALLBACK
8438
tabline_wndproc(
8439
HWND hwnd,
8440
UINT uMsg,
8441
WPARAM wParam,
8442
LPARAM lParam)
8443
{
8444
POINT pt;
8445
tabpage_T *tp;
8446
RECT rect;
8447
int nCenter;
8448
int idx0;
8449
int idx1;
8450
8451
HandleMouseHide(uMsg, lParam);
8452
8453
switch (uMsg)
8454
{
8455
case WM_LBUTTONDOWN:
8456
{
8457
s_pt.x = GET_X_LPARAM(lParam);
8458
s_pt.y = GET_Y_LPARAM(lParam);
8459
SetCapture(hwnd);
8460
s_hCursor = GetCursor(); // backup default cursor
8461
break;
8462
}
8463
case WM_MOUSEMOVE:
8464
if (GetCapture() == hwnd
8465
&& ((wParam & MK_LBUTTON)) != 0)
8466
{
8467
pt.x = GET_X_LPARAM(lParam);
8468
pt.y = s_pt.y;
8469
if (abs(pt.x - s_pt.x) >
8470
pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
8471
{
8472
SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8473
8474
tp = GetTabFromPoint(hwnd, pt);
8475
if (tp != NULL)
8476
{
8477
idx0 = tabpage_index(curtab) - 1;
8478
idx1 = tabpage_index(tp) - 1;
8479
8480
TabCtrl_GetItemRect(hwnd, idx1, &rect);
8481
nCenter = rect.left + (rect.right - rect.left) / 2;
8482
8483
// Check if the mouse cursor goes over the center of
8484
// the next tab to prevent "flickering".
8485
if ((idx0 < idx1) && (nCenter < pt.x))
8486
{
8487
tabpage_move(idx1 + 1);
8488
update_screen(0);
8489
}
8490
else if ((idx1 < idx0) && (pt.x < nCenter))
8491
{
8492
tabpage_move(idx1);
8493
update_screen(0);
8494
}
8495
}
8496
}
8497
}
8498
break;
8499
case WM_LBUTTONUP:
8500
{
8501
if (GetCapture() == hwnd)
8502
{
8503
SetCursor(s_hCursor);
8504
ReleaseCapture();
8505
}
8506
break;
8507
}
8508
case WM_MBUTTONUP:
8509
{
8510
TCHITTESTINFO htinfo;
8511
8512
htinfo.pt.x = GET_X_LPARAM(lParam);
8513
htinfo.pt.y = GET_Y_LPARAM(lParam);
8514
idx0 = TabCtrl_HitTest(hwnd, &htinfo);
8515
if (idx0 != -1)
8516
{
8517
idx0 += 1;
8518
send_tabline_menu_event(idx0, TABLINE_MENU_CLOSE);
8519
}
8520
break;
8521
}
8522
default:
8523
break;
8524
}
8525
8526
return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8527
}
8528
#endif
8529
8530
#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8531
/*
8532
* Make the GUI window come to the foreground.
8533
*/
8534
void
8535
gui_mch_set_foreground(void)
8536
{
8537
if (IsIconic(s_hwnd))
8538
SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8539
SetForegroundWindow(s_hwnd);
8540
}
8541
#endif
8542
8543
#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8544
static void
8545
dyn_imm_load(void)
8546
{
8547
hLibImm = vimLoadLib("imm32.dll");
8548
if (hLibImm == NULL)
8549
return;
8550
8551
pImmGetCompositionStringW
8552
= (LONG (WINAPI *)(HIMC, DWORD, LPVOID, DWORD))GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8553
pImmGetContext
8554
= (HIMC (WINAPI *)(HWND))GetProcAddress(hLibImm, "ImmGetContext");
8555
pImmAssociateContext
8556
= (HIMC (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmAssociateContext");
8557
pImmReleaseContext
8558
= (BOOL (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmReleaseContext");
8559
pImmGetOpenStatus
8560
= (BOOL (WINAPI *)(HIMC))GetProcAddress(hLibImm, "ImmGetOpenStatus");
8561
pImmSetOpenStatus
8562
= (BOOL (WINAPI *)(HIMC, BOOL))GetProcAddress(hLibImm, "ImmSetOpenStatus");
8563
pImmGetCompositionFontW
8564
= (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8565
pImmSetCompositionFontW
8566
= (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmSetCompositionFontW");
8567
pImmSetCompositionWindow
8568
= (BOOL (WINAPI *)(HIMC, LPCOMPOSITIONFORM))GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8569
pImmGetConversionStatus
8570
= (BOOL (WINAPI *)(HIMC, LPDWORD, LPDWORD))GetProcAddress(hLibImm, "ImmGetConversionStatus");
8571
pImmSetConversionStatus
8572
= (BOOL (WINAPI *)(HIMC, DWORD, DWORD))GetProcAddress(hLibImm, "ImmSetConversionStatus");
8573
8574
if ( pImmGetCompositionStringW == NULL
8575
|| pImmGetContext == NULL
8576
|| pImmAssociateContext == NULL
8577
|| pImmReleaseContext == NULL
8578
|| pImmGetOpenStatus == NULL
8579
|| pImmSetOpenStatus == NULL
8580
|| pImmGetCompositionFontW == NULL
8581
|| pImmSetCompositionFontW == NULL
8582
|| pImmSetCompositionWindow == NULL
8583
|| pImmGetConversionStatus == NULL
8584
|| pImmSetConversionStatus == NULL)
8585
{
8586
FreeLibrary(hLibImm);
8587
hLibImm = NULL;
8588
pImmGetContext = NULL;
8589
return;
8590
}
8591
8592
return;
8593
}
8594
8595
#endif
8596
8597
#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8598
8599
# ifdef FEAT_XPM_W32
8600
# define IMAGE_XPM 100
8601
# endif
8602
8603
typedef struct _signicon_t
8604
{
8605
HANDLE hImage;
8606
UINT uType;
8607
# ifdef FEAT_XPM_W32
8608
HANDLE hShape; // Mask bitmap handle
8609
# endif
8610
} signicon_t;
8611
8612
void
8613
gui_mch_drawsign(int row, int col, int typenr)
8614
{
8615
signicon_t *sign;
8616
int x, y, w, h;
8617
8618
if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8619
return;
8620
8621
# if defined(FEAT_DIRECTX)
8622
if (IS_ENABLE_DIRECTX())
8623
DWriteContext_Flush(s_dwc);
8624
# endif
8625
8626
x = TEXT_X(col);
8627
y = TEXT_Y(row);
8628
w = gui.char_width * 2;
8629
h = gui.char_height;
8630
switch (sign->uType)
8631
{
8632
case IMAGE_BITMAP:
8633
{
8634
HDC hdcMem;
8635
HBITMAP hbmpOld;
8636
8637
hdcMem = CreateCompatibleDC(s_hdc);
8638
hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8639
BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8640
SelectObject(hdcMem, hbmpOld);
8641
DeleteDC(hdcMem);
8642
}
8643
break;
8644
case IMAGE_ICON:
8645
case IMAGE_CURSOR:
8646
DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8647
break;
8648
# ifdef FEAT_XPM_W32
8649
case IMAGE_XPM:
8650
{
8651
HDC hdcMem;
8652
HBITMAP hbmpOld;
8653
8654
hdcMem = CreateCompatibleDC(s_hdc);
8655
hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8656
// Make hole
8657
BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8658
8659
SelectObject(hdcMem, sign->hImage);
8660
// Paint sign
8661
BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8662
SelectObject(hdcMem, hbmpOld);
8663
DeleteDC(hdcMem);
8664
}
8665
break;
8666
# endif
8667
}
8668
}
8669
8670
static void
8671
close_signicon_image(signicon_t *sign)
8672
{
8673
if (sign == NULL)
8674
return;
8675
8676
switch (sign->uType)
8677
{
8678
case IMAGE_BITMAP:
8679
DeleteObject((HGDIOBJ)sign->hImage);
8680
break;
8681
case IMAGE_CURSOR:
8682
DestroyCursor((HCURSOR)sign->hImage);
8683
break;
8684
case IMAGE_ICON:
8685
DestroyIcon((HICON)sign->hImage);
8686
break;
8687
# ifdef FEAT_XPM_W32
8688
case IMAGE_XPM:
8689
DeleteObject((HBITMAP)sign->hImage);
8690
DeleteObject((HBITMAP)sign->hShape);
8691
break;
8692
# endif
8693
}
8694
}
8695
8696
void *
8697
gui_mch_register_sign(char_u *signfile)
8698
{
8699
signicon_t sign, *psign;
8700
char_u *ext;
8701
8702
sign.hImage = NULL;
8703
ext = signfile + STRLEN(signfile) - 4; // get extension
8704
if (ext > signfile)
8705
{
8706
int do_load = 1;
8707
8708
if (!STRICMP(ext, ".bmp"))
8709
sign.uType = IMAGE_BITMAP;
8710
else if (!STRICMP(ext, ".ico"))
8711
sign.uType = IMAGE_ICON;
8712
else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8713
sign.uType = IMAGE_CURSOR;
8714
else
8715
do_load = 0;
8716
8717
if (do_load)
8718
sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
8719
gui.char_width * 2, gui.char_height,
8720
LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8721
# ifdef FEAT_XPM_W32
8722
if (!STRICMP(ext, ".xpm"))
8723
{
8724
sign.uType = IMAGE_XPM;
8725
LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8726
(HBITMAP *)&sign.hShape);
8727
}
8728
# endif
8729
}
8730
8731
psign = NULL;
8732
if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
8733
*psign = sign;
8734
8735
if (!psign)
8736
{
8737
if (sign.hImage)
8738
close_signicon_image(&sign);
8739
emsg(_(e_couldnt_read_in_sign_data));
8740
}
8741
return (void *)psign;
8742
8743
}
8744
8745
void
8746
gui_mch_destroy_sign(void *sign)
8747
{
8748
if (sign == NULL)
8749
return;
8750
8751
close_signicon_image((signicon_t *)sign);
8752
vim_free(sign);
8753
}
8754
#endif
8755
8756
#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
8757
8758
/*
8759
* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
8760
* Added by Sergey Khorev <[email protected]>
8761
*
8762
* The only reused thing is beval.h and get_beval_info()
8763
* from gui_beval.c (note it uses x and y of the BalloonEval struct
8764
* to get current mouse position).
8765
*
8766
* Trying to use as more Windows services as possible, and as less
8767
* IE version as possible :)).
8768
*
8769
* 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8770
* BalloonEval struct.
8771
* 2) Enable/Disable simply create/kill BalloonEval Timer
8772
* 3) When there was enough inactivity, timer procedure posts
8773
* async request to debugger
8774
* 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8775
* and performs some actions to show it ASAP
8776
* 5) WM_NOTIFY:TTN_POP destroys created tooltip
8777
*/
8778
8779
static void
8780
make_tooltip(BalloonEval *beval, char *text, POINT pt)
8781
{
8782
TOOLINFOW *pti;
8783
RECT rect;
8784
8785
pti = ALLOC_ONE(TOOLINFOW);
8786
if (pti == NULL)
8787
return;
8788
8789
beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8790
NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8791
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8792
beval->target, NULL, g_hinst, NULL);
8793
8794
SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8795
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8796
8797
pti->cbSize = sizeof(TOOLINFOW);
8798
pti->uFlags = TTF_SUBCLASS;
8799
pti->hwnd = beval->target;
8800
pti->hinst = 0; // Don't use string resources
8801
pti->uId = ID_BEVAL_TOOLTIP;
8802
8803
pti->lpszText = LPSTR_TEXTCALLBACKW;
8804
beval->tofree = enc_to_utf16((char_u*)text, NULL);
8805
pti->lParam = (LPARAM)beval->tofree;
8806
// switch multiline tooltips on
8807
if (GetClientRect(s_textArea, &rect))
8808
SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8809
(LPARAM)rect.right);
8810
8811
// Limit ballooneval bounding rect to CursorPos neighbourhood.
8812
pti->rect.left = pt.x - 3;
8813
pti->rect.top = pt.y - 3;
8814
pti->rect.right = pt.x + 3;
8815
pti->rect.bottom = pt.y + 3;
8816
8817
SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8818
// Make tooltip appear sooner.
8819
SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8820
// I've performed some tests and it seems the longest possible life time
8821
// of tooltip is 30 seconds.
8822
SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8823
/*
8824
* HACK: force tooltip to appear, because it'll not appear until
8825
* first mouse move. D*mn M$
8826
* Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8827
*/
8828
mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8829
mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8830
vim_free(pti);
8831
}
8832
8833
static void
8834
delete_tooltip(BalloonEval *beval)
8835
{
8836
PostMessage(beval->balloon, WM_CLOSE, 0, 0);
8837
}
8838
8839
static VOID CALLBACK
8840
beval_timer_proc(
8841
HWND hwnd UNUSED,
8842
UINT uMsg UNUSED,
8843
UINT_PTR idEvent UNUSED,
8844
DWORD dwTime)
8845
{
8846
POINT pt;
8847
RECT rect;
8848
8849
if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8850
return;
8851
8852
GetCursorPos(&pt);
8853
if (WindowFromPoint(pt) != s_textArea)
8854
return;
8855
8856
ScreenToClient(s_textArea, &pt);
8857
GetClientRect(s_textArea, &rect);
8858
if (!PtInRect(&rect, pt))
8859
return;
8860
8861
if (last_user_activity > 0
8862
&& (dwTime - last_user_activity) >= (DWORD)p_bdlay
8863
&& (cur_beval->showState != ShS_PENDING
8864
|| abs(cur_beval->x - pt.x) > 3
8865
|| abs(cur_beval->y - pt.y) > 3))
8866
{
8867
// Pointer resting in one place long enough, it's time to show
8868
// the tooltip.
8869
cur_beval->showState = ShS_PENDING;
8870
cur_beval->x = pt.x;
8871
cur_beval->y = pt.y;
8872
8873
if (cur_beval->msgCB != NULL)
8874
(*cur_beval->msgCB)(cur_beval, 0);
8875
}
8876
}
8877
8878
void
8879
gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
8880
{
8881
KillTimer(s_textArea, beval_timer_id);
8882
}
8883
8884
void
8885
gui_mch_enable_beval_area(BalloonEval *beval)
8886
{
8887
if (beval == NULL)
8888
return;
8889
beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8890
beval_timer_proc);
8891
}
8892
8893
void
8894
gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
8895
{
8896
POINT pt;
8897
8898
vim_free(beval->msg);
8899
beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8900
if (beval->msg == NULL)
8901
{
8902
delete_tooltip(beval);
8903
beval->showState = ShS_NEUTRAL;
8904
return;
8905
}
8906
8907
if (beval->showState == ShS_SHOWING)
8908
return;
8909
GetCursorPos(&pt);
8910
ScreenToClient(s_textArea, &pt);
8911
8912
if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
8913
{
8914
// cursor is still here
8915
gui_mch_disable_beval_area(cur_beval);
8916
beval->showState = ShS_SHOWING;
8917
make_tooltip(beval, (char *)mesg, pt);
8918
}
8919
}
8920
8921
BalloonEval *
8922
gui_mch_create_beval_area(
8923
void *target UNUSED, // ignored, always use s_textArea
8924
char_u *mesg,
8925
void (*mesgCB)(BalloonEval *, int),
8926
void *clientData)
8927
{
8928
// partially stolen from gui_beval.c
8929
BalloonEval *beval;
8930
8931
if (mesg != NULL && mesgCB != NULL)
8932
{
8933
iemsg(e_cannot_create_ballooneval_with_both_message_and_callback);
8934
return NULL;
8935
}
8936
8937
beval = ALLOC_CLEAR_ONE(BalloonEval);
8938
if (beval != NULL)
8939
{
8940
beval->target = s_textArea;
8941
8942
beval->showState = ShS_NEUTRAL;
8943
beval->msg = mesg;
8944
beval->msgCB = mesgCB;
8945
beval->clientData = clientData;
8946
8947
InitCommonControls();
8948
cur_beval = beval;
8949
8950
if (p_beval)
8951
gui_mch_enable_beval_area(beval);
8952
}
8953
return beval;
8954
}
8955
8956
static void
8957
Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
8958
{
8959
if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
8960
return;
8961
8962
if (cur_beval == NULL)
8963
return;
8964
8965
switch (pnmh->code)
8966
{
8967
case TTN_SHOW:
8968
break;
8969
case TTN_POP: // Before tooltip disappear
8970
delete_tooltip(cur_beval);
8971
gui_mch_enable_beval_area(cur_beval);
8972
8973
cur_beval->showState = ShS_NEUTRAL;
8974
break;
8975
case TTN_GETDISPINFO:
8976
{
8977
// if you get there then we have new common controls
8978
NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
8979
info->lpszText = (LPSTR)info->lParam;
8980
info->uFlags |= TTF_DI_SETITEM;
8981
}
8982
break;
8983
case TTN_GETDISPINFOW:
8984
{
8985
// if we get here then we have new common controls
8986
NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
8987
info->lpszText = (LPWSTR)info->lParam;
8988
info->uFlags |= TTF_DI_SETITEM;
8989
}
8990
break;
8991
}
8992
}
8993
8994
static void
8995
track_user_activity(UINT uMsg)
8996
{
8997
if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8998
|| (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8999
last_user_activity = GetTickCount();
9000
}
9001
9002
void
9003
gui_mch_destroy_beval_area(BalloonEval *beval)
9004
{
9005
# ifdef FEAT_VARTABS
9006
vim_free(beval->vts);
9007
# endif
9008
vim_free(beval->tofree);
9009
vim_free(beval);
9010
}
9011
#endif // FEAT_BEVAL_GUI
9012
9013
#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
9014
/*
9015
* We have multiple signs to draw at the same location. Draw the
9016
* multi-sign indicator (down-arrow) instead. This is the Win32 version.
9017
*/
9018
void
9019
netbeans_draw_multisign_indicator(int row)
9020
{
9021
int i;
9022
int y;
9023
int x;
9024
9025
if (!netbeans_active())
9026
return;
9027
9028
x = 0;
9029
y = TEXT_Y(row);
9030
9031
# if defined(FEAT_DIRECTX)
9032
if (IS_ENABLE_DIRECTX())
9033
DWriteContext_Flush(s_dwc);
9034
# endif
9035
9036
for (i = 0; i < gui.char_height - 3; i++)
9037
SetPixel(s_hdc, x+2, y++, gui.currFgColor);
9038
9039
SetPixel(s_hdc, x+0, y, gui.currFgColor);
9040
SetPixel(s_hdc, x+2, y, gui.currFgColor);
9041
SetPixel(s_hdc, x+4, y++, gui.currFgColor);
9042
SetPixel(s_hdc, x+1, y, gui.currFgColor);
9043
SetPixel(s_hdc, x+2, y, gui.currFgColor);
9044
SetPixel(s_hdc, x+3, y++, gui.currFgColor);
9045
SetPixel(s_hdc, x+2, y, gui.currFgColor);
9046
}
9047
#endif
9048
9049
#if defined(FEAT_EVAL) || defined(PROTO)
9050
9051
// TODO: at the moment, this is just a copy of test_gui_mouse_event.
9052
// But, we could instead generate actual Win32 mouse event messages,
9053
// ie. to make it consistent with test_gui_w32_sendevent_keyboard.
9054
static int
9055
test_gui_w32_sendevent_mouse(dict_T *args)
9056
{
9057
if (!dict_has_key(args, "row") || !dict_has_key(args, "col"))
9058
return FALSE;
9059
9060
// Note: "move" is optional, requires fewer arguments
9061
int move = (int)dict_get_bool(args, "move", FALSE);
9062
9063
if (!move && (!dict_has_key(args, "button")
9064
|| !dict_has_key(args, "multiclick")
9065
|| !dict_has_key(args, "modifiers")))
9066
return FALSE;
9067
9068
int row = (int)dict_get_number(args, "row");
9069
int col = (int)dict_get_number(args, "col");
9070
9071
if (move)
9072
{
9073
// the "move" argument expects row and col coordnates to be in pixels,
9074
// unless "cell" is specified and is TRUE.
9075
if (dict_get_bool(args, "cell", FALSE))
9076
{
9077
// calculate the middle of the character cell
9078
// Note: Cell coordinates are 1-based from Vim script
9079
int pY = (row - 1) * gui.char_height + gui.char_height / 2;
9080
int pX = (col - 1) * gui.char_width + gui.char_width / 2;
9081
gui_mouse_moved(pX, pY);
9082
}
9083
else
9084
gui_mouse_moved(col, row);
9085
}
9086
else
9087
{
9088
int button = (int)dict_get_number(args, "button");
9089
int repeated_click = (int)dict_get_number(args, "multiclick");
9090
int_u mods = (int)dict_get_number(args, "modifiers");
9091
9092
// Reset the scroll values to known values.
9093
// XXX: Remove this when/if the scroll step is made configurable.
9094
mouse_set_hor_scroll_step(6);
9095
mouse_set_vert_scroll_step(3);
9096
9097
gui_send_mouse_event(button, TEXT_X(col - 1), TEXT_Y(row - 1),
9098
repeated_click, mods);
9099
}
9100
return TRUE;
9101
}
9102
9103
static int
9104
test_gui_w32_sendevent_keyboard(dict_T *args)
9105
{
9106
INPUT inputs[1];
9107
INPUT modkeys[3];
9108
SecureZeroMemory(inputs, sizeof(INPUT));
9109
SecureZeroMemory(modkeys, 3 * sizeof(INPUT));
9110
9111
char_u *event = dict_get_string(args, "event", TRUE);
9112
9113
if (event && (STRICMP(event, "keydown") == 0
9114
|| STRICMP(event, "keyup") == 0))
9115
{
9116
WORD vkCode = dict_get_number_def(args, "keycode", 0);
9117
if (vkCode <= 0 || vkCode >= 0xFF)
9118
{
9119
semsg(_(e_invalid_argument_nr), (long)vkCode);
9120
return FALSE;
9121
}
9122
9123
BOOL isModKey = (vkCode == VK_SHIFT || vkCode == VK_CONTROL
9124
|| vkCode == VK_MENU || vkCode == VK_LSHIFT || vkCode == VK_RSHIFT
9125
|| vkCode == VK_LCONTROL || vkCode == VK_RCONTROL
9126
|| vkCode == VK_LMENU || vkCode == VK_RMENU );
9127
9128
BOOL unwrapMods = FALSE;
9129
int mods = (int)dict_get_number(args, "modifiers");
9130
9131
// If there are modifiers in the args, and it is not a keyup event and
9132
// vkCode is not a modifier key, then we generate virtual modifier key
9133
// messages before sending the actual key message.
9134
if (mods && STRICMP(event, "keydown") == 0 && !isModKey)
9135
{
9136
int n = 0;
9137
if (mods & MOD_MASK_SHIFT)
9138
{
9139
modkeys[n].type = INPUT_KEYBOARD;
9140
modkeys[n].ki.wVk = VK_LSHIFT;
9141
n++;
9142
}
9143
if (mods & MOD_MASK_CTRL)
9144
{
9145
modkeys[n].type = INPUT_KEYBOARD;
9146
modkeys[n].ki.wVk = VK_LCONTROL;
9147
n++;
9148
}
9149
if (mods & MOD_MASK_ALT)
9150
{
9151
modkeys[n].type = INPUT_KEYBOARD;
9152
modkeys[n].ki.wVk = VK_LMENU;
9153
n++;
9154
}
9155
if (n)
9156
{
9157
(void)SetForegroundWindow(s_hwnd);
9158
SendInput(n, modkeys, sizeof(INPUT));
9159
}
9160
}
9161
9162
inputs[0].type = INPUT_KEYBOARD;
9163
inputs[0].ki.wVk = vkCode;
9164
if (STRICMP(event, "keyup") == 0)
9165
{
9166
inputs[0].ki.dwFlags = KEYEVENTF_KEYUP;
9167
if (!isModKey)
9168
unwrapMods = TRUE;
9169
}
9170
9171
(void)SetForegroundWindow(s_hwnd);
9172
SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
9173
vim_free(event);
9174
9175
if (unwrapMods)
9176
{
9177
modkeys[0].type = INPUT_KEYBOARD;
9178
modkeys[0].ki.wVk = VK_LSHIFT;
9179
modkeys[0].ki.dwFlags = KEYEVENTF_KEYUP;
9180
9181
modkeys[1].type = INPUT_KEYBOARD;
9182
modkeys[1].ki.wVk = VK_LCONTROL;
9183
modkeys[1].ki.dwFlags = KEYEVENTF_KEYUP;
9184
9185
modkeys[2].type = INPUT_KEYBOARD;
9186
modkeys[2].ki.wVk = VK_LMENU;
9187
modkeys[2].ki.dwFlags = KEYEVENTF_KEYUP;
9188
9189
(void)SetForegroundWindow(s_hwnd);
9190
SendInput(3, modkeys, sizeof(INPUT));
9191
}
9192
}
9193
else
9194
{
9195
if (event == NULL)
9196
{
9197
semsg(_(e_missing_argument_str), "event");
9198
}
9199
else
9200
{
9201
semsg(_(e_invalid_value_for_argument_str_str), "event", event);
9202
vim_free(event);
9203
}
9204
return FALSE;
9205
}
9206
return TRUE;
9207
}
9208
9209
static int
9210
test_gui_w32_sendevent_set_keycode_trans_strategy(dict_T *args)
9211
{
9212
int handled = 0;
9213
char_u *strategy = dict_get_string(args, "strategy", TRUE);
9214
9215
if (strategy)
9216
{
9217
if (STRICMP(strategy, "classic") == 0)
9218
{
9219
handled = 1;
9220
keycode_trans_strategy_used = &keycode_trans_strategy_classic;
9221
}
9222
else if (STRICMP(strategy, "experimental") == 0)
9223
{
9224
handled = 1;
9225
keycode_trans_strategy_used = &keycode_trans_strategy_experimental;
9226
}
9227
}
9228
9229
if (!handled)
9230
{
9231
if (strategy == NULL)
9232
{
9233
semsg(_(e_missing_argument_str), "strategy");
9234
}
9235
else
9236
{
9237
semsg(_(e_invalid_value_for_argument_str_str), "strategy", strategy);
9238
vim_free(strategy);
9239
}
9240
return FALSE;
9241
}
9242
return TRUE;
9243
}
9244
9245
9246
int
9247
test_gui_w32_sendevent(char_u *event, dict_T *args)
9248
{
9249
if (STRICMP(event, "key") == 0)
9250
return test_gui_w32_sendevent_keyboard(args);
9251
else if (STRICMP(event, "mouse") == 0)
9252
return test_gui_w32_sendevent_mouse(args);
9253
else if (STRICMP(event, "set_keycode_trans_strategy") == 0)
9254
return test_gui_w32_sendevent_set_keycode_trans_strategy(args);
9255
else
9256
{
9257
semsg(_(e_invalid_value_for_argument_str_str), "event", event);
9258
return FALSE;
9259
}
9260
}
9261
#endif
9262
9263