blob: 42fb7ca86ec7d4a5238a7cfbb535c4d971716615 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI/Motif 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 * Common code for the Motif and Athena GUI.
12 * Not used for GTK.
13 */
14
Bram Moolenaarf7506ca2017-02-25 16:01:49 +010015#include "vim.h"
16
Bram Moolenaar071d4272004-06-13 20:20:40 +000017#include <X11/keysym.h>
18#include <X11/Xatom.h>
19#include <X11/StringDefs.h>
20#include <X11/Intrinsic.h>
21#include <X11/Shell.h>
22#include <X11/cursorfont.h>
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
25 * For Workshop XpmP.h is preferred, because it makes the signs drawn with a
26 * transparent background instead of black.
27 */
28#if defined(HAVE_XM_XPMP_H) && defined(FEAT_GUI_MOTIF) \
29 && (!defined(HAVE_X11_XPM_H) || defined(FEAT_SUN_WORKSHOP))
30# include <Xm/XpmP.h>
31#else
32# ifdef HAVE_X11_XPM_H
33# include <X11/xpm.h>
34# endif
35#endif
36
37#ifdef FEAT_XFONTSET
38# ifdef X_LOCALE
39# include <X11/Xlocale.h>
40# else
41# include <locale.h>
42# endif
43#endif
44
45#ifdef HAVE_X11_SUNKEYSYM_H
46# include <X11/Sunkeysym.h>
47#endif
48
49#ifdef HAVE_X11_XMU_EDITRES_H
50# include <X11/Xmu/Editres.h>
51#endif
52
Bram Moolenaar071d4272004-06-13 20:20:40 +000053#define VIM_NAME "vim"
54#define VIM_CLASS "Vim"
55
56/* Default resource values */
57#define DFLT_FONT "7x13"
58#ifdef FONTSET_ALWAYS
59# define DFLT_MENU_FONT XtDefaultFontSet
60#else
61# define DFLT_MENU_FONT XtDefaultFont
62#endif
63#define DFLT_TOOLTIP_FONT XtDefaultFontSet
64
65#ifdef FEAT_GUI_ATHENA
66# define DFLT_MENU_BG_COLOR "gray77"
67# define DFLT_MENU_FG_COLOR "black"
68# define DFLT_SCROLL_BG_COLOR "gray60"
69# define DFLT_SCROLL_FG_COLOR "gray77"
Bram Moolenaar46582282016-07-23 14:35:12 +020070# define DFLT_TOOLTIP_BG_COLOR "#ffff91"
71# define DFLT_TOOLTIP_FG_COLOR "#000000"
Bram Moolenaar071d4272004-06-13 20:20:40 +000072#else
73/* use the default (CDE) colors */
74# define DFLT_MENU_BG_COLOR ""
75# define DFLT_MENU_FG_COLOR ""
76# define DFLT_SCROLL_BG_COLOR ""
77# define DFLT_SCROLL_FG_COLOR ""
Bram Moolenaar46582282016-07-23 14:35:12 +020078# define DFLT_TOOLTIP_BG_COLOR "#ffff91"
79# define DFLT_TOOLTIP_FG_COLOR "#000000"
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#endif
81
82Widget vimShell = (Widget)0;
83
84static Atom wm_atoms[2]; /* Window Manager Atoms */
85#define DELETE_WINDOW_IDX 0 /* index in wm_atoms[] for WM_DELETE_WINDOW */
86#define SAVE_YOURSELF_IDX 1 /* index in wm_atoms[] for WM_SAVE_YOURSELF */
87
88#ifdef FEAT_XFONTSET
89/*
90 * We either draw with a fontset (when current_fontset != NULL) or with a
91 * normal font (current_fontset == NULL, use gui.text_gc and gui.back_gc).
92 */
93static XFontSet current_fontset = NULL;
94
95#define XDrawString(dpy, win, gc, x, y, str, n) \
96 do \
97 { \
98 if (current_fontset != NULL) \
99 XmbDrawString(dpy, win, current_fontset, gc, x, y, str, n); \
100 else \
101 XDrawString(dpy, win, gc, x, y, str, n); \
102 } while (0)
103
104#define XDrawString16(dpy, win, gc, x, y, str, n) \
105 do \
106 { \
107 if (current_fontset != NULL) \
108 XwcDrawString(dpy, win, current_fontset, gc, x, y, (wchar_t *)str, n); \
109 else \
Bram Moolenaara3227e22006-03-08 21:32:40 +0000110 XDrawString16(dpy, win, gc, x, y, (XChar2b *)str, n); \
111 } while (0)
112
113#define XDrawImageString16(dpy, win, gc, x, y, str, n) \
114 do \
115 { \
116 if (current_fontset != NULL) \
117 XwcDrawImageString(dpy, win, current_fontset, gc, x, y, (wchar_t *)str, n); \
118 else \
119 XDrawImageString16(dpy, win, gc, x, y, (XChar2b *)str, n); \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 } while (0)
121
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100122static int check_fontset_sanity(XFontSet fs);
123static int fontset_width(XFontSet fs);
124static int fontset_ascent(XFontSet fs);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#endif
126
127static guicolor_T prev_fg_color = INVALCOLOR;
128static guicolor_T prev_bg_color = INVALCOLOR;
Bram Moolenaarfb269802005-03-15 22:46:30 +0000129static guicolor_T prev_sp_color = INVALCOLOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130
131#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
132static XButtonPressedEvent last_mouse_event;
133#endif
134
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100135static void gui_x11_check_copy_area(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136#ifdef FEAT_CLIENTSERVER
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100137static void gui_x11_send_event_handler(Widget, XtPointer, XEvent *, Boolean *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100139static void gui_x11_wm_protocol_handler(Widget, XtPointer, XEvent *, Boolean *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100140static Cursor gui_x11_create_blank_mouse(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141
142
143/*
144 * Keycodes recognized by vim.
145 * NOTE: when changing this, the table in gui_gtk_x11.c probably needs the
146 * same change!
147 */
148static struct specialkey
149{
150 KeySym key_sym;
151 char_u vim_code0;
152 char_u vim_code1;
153} special_keys[] =
154{
155 {XK_Up, 'k', 'u'},
156 {XK_Down, 'k', 'd'},
157 {XK_Left, 'k', 'l'},
158 {XK_Right, 'k', 'r'},
159
160 {XK_F1, 'k', '1'},
161 {XK_F2, 'k', '2'},
162 {XK_F3, 'k', '3'},
163 {XK_F4, 'k', '4'},
164 {XK_F5, 'k', '5'},
165 {XK_F6, 'k', '6'},
166 {XK_F7, 'k', '7'},
167 {XK_F8, 'k', '8'},
168 {XK_F9, 'k', '9'},
169 {XK_F10, 'k', ';'},
170
171 {XK_F11, 'F', '1'},
172 {XK_F12, 'F', '2'},
173 {XK_F13, 'F', '3'},
174 {XK_F14, 'F', '4'},
175 {XK_F15, 'F', '5'},
176 {XK_F16, 'F', '6'},
177 {XK_F17, 'F', '7'},
178 {XK_F18, 'F', '8'},
179 {XK_F19, 'F', '9'},
180 {XK_F20, 'F', 'A'},
181
182 {XK_F21, 'F', 'B'},
183 {XK_F22, 'F', 'C'},
184 {XK_F23, 'F', 'D'},
185 {XK_F24, 'F', 'E'},
186 {XK_F25, 'F', 'F'},
187 {XK_F26, 'F', 'G'},
188 {XK_F27, 'F', 'H'},
189 {XK_F28, 'F', 'I'},
190 {XK_F29, 'F', 'J'},
191 {XK_F30, 'F', 'K'},
192
193 {XK_F31, 'F', 'L'},
194 {XK_F32, 'F', 'M'},
195 {XK_F33, 'F', 'N'},
196 {XK_F34, 'F', 'O'},
197 {XK_F35, 'F', 'P'}, /* keysymdef.h defines up to F35 */
198#ifdef SunXK_F36
199 {SunXK_F36, 'F', 'Q'},
200 {SunXK_F37, 'F', 'R'},
201#endif
202
203 {XK_Help, '%', '1'},
204 {XK_Undo, '&', '8'},
205 {XK_BackSpace, 'k', 'b'},
206 {XK_Insert, 'k', 'I'},
207 {XK_Delete, 'k', 'D'},
208 {XK_Home, 'k', 'h'},
209 {XK_End, '@', '7'},
210 {XK_Prior, 'k', 'P'},
211 {XK_Next, 'k', 'N'},
212 {XK_Print, '%', '9'},
213
214 /* Keypad keys: */
215#ifdef XK_KP_Left
216 {XK_KP_Left, 'k', 'l'},
217 {XK_KP_Right, 'k', 'r'},
218 {XK_KP_Up, 'k', 'u'},
219 {XK_KP_Down, 'k', 'd'},
220 {XK_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
221 {XK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
222 {XK_KP_Home, 'K', '1'},
223 {XK_KP_End, 'K', '4'},
224 {XK_KP_Prior, 'K', '3'},
225 {XK_KP_Next, 'K', '5'},
226
227 {XK_KP_Add, 'K', '6'},
228 {XK_KP_Subtract, 'K', '7'},
229 {XK_KP_Divide, 'K', '8'},
230 {XK_KP_Multiply, 'K', '9'},
231 {XK_KP_Enter, 'K', 'A'},
232 {XK_KP_Decimal, 'K', 'B'},
233
234 {XK_KP_0, 'K', 'C'},
235 {XK_KP_1, 'K', 'D'},
236 {XK_KP_2, 'K', 'E'},
237 {XK_KP_3, 'K', 'F'},
238 {XK_KP_4, 'K', 'G'},
239 {XK_KP_5, 'K', 'H'},
240 {XK_KP_6, 'K', 'I'},
241 {XK_KP_7, 'K', 'J'},
242 {XK_KP_8, 'K', 'K'},
243 {XK_KP_9, 'K', 'L'},
244#endif
245
246 /* End of list marker: */
247 {(KeySym)0, 0, 0}
248};
249
250#define XtNboldFont "boldFont"
251#define XtCBoldFont "BoldFont"
252#define XtNitalicFont "italicFont"
253#define XtCItalicFont "ItalicFont"
254#define XtNboldItalicFont "boldItalicFont"
255#define XtCBoldItalicFont "BoldItalicFont"
256#define XtNscrollbarWidth "scrollbarWidth"
257#define XtCScrollbarWidth "ScrollbarWidth"
258#define XtNmenuHeight "menuHeight"
259#define XtCMenuHeight "MenuHeight"
260#define XtNmenuFont "menuFont"
261#define XtCMenuFont "MenuFont"
262#define XtNmenuFontSet "menuFontSet"
263#define XtCMenuFontSet "MenuFontSet"
264
265
266/* Resources for setting the foreground and background colors of menus */
267#define XtNmenuBackground "menuBackground"
268#define XtCMenuBackground "MenuBackground"
269#define XtNmenuForeground "menuForeground"
270#define XtCMenuForeground "MenuForeground"
271
272/* Resources for setting the foreground and background colors of scrollbars */
273#define XtNscrollBackground "scrollBackground"
274#define XtCScrollBackground "ScrollBackground"
275#define XtNscrollForeground "scrollForeground"
276#define XtCScrollForeground "ScrollForeground"
277
278/* Resources for setting the foreground and background colors of tooltip */
279#define XtNtooltipBackground "tooltipBackground"
280#define XtCTooltipBackground "TooltipBackground"
281#define XtNtooltipForeground "tooltipForeground"
282#define XtCTooltipForeground "TooltipForeground"
283#define XtNtooltipFont "tooltipFont"
284#define XtCTooltipFont "TooltipFont"
285
286/*
287 * X Resources:
288 */
289static XtResource vim_resources[] =
290{
291 {
292 XtNforeground,
293 XtCForeground,
294 XtRPixel,
295 sizeof(Pixel),
296 XtOffsetOf(gui_T, def_norm_pixel),
297 XtRString,
298 XtDefaultForeground
299 },
300 {
301 XtNbackground,
302 XtCBackground,
303 XtRPixel,
304 sizeof(Pixel),
305 XtOffsetOf(gui_T, def_back_pixel),
306 XtRString,
307 XtDefaultBackground
308 },
309 {
310 XtNfont,
311 XtCFont,
312 XtRString,
313 sizeof(String *),
314 XtOffsetOf(gui_T, rsrc_font_name),
315 XtRImmediate,
316 XtDefaultFont
317 },
318 {
319 XtNboldFont,
320 XtCBoldFont,
321 XtRString,
322 sizeof(String *),
323 XtOffsetOf(gui_T, rsrc_bold_font_name),
324 XtRImmediate,
325 ""
326 },
327 {
328 XtNitalicFont,
329 XtCItalicFont,
330 XtRString,
331 sizeof(String *),
332 XtOffsetOf(gui_T, rsrc_ital_font_name),
333 XtRImmediate,
334 ""
335 },
336 {
337 XtNboldItalicFont,
338 XtCBoldItalicFont,
339 XtRString,
340 sizeof(String *),
341 XtOffsetOf(gui_T, rsrc_boldital_font_name),
342 XtRImmediate,
343 ""
344 },
345 {
346 XtNgeometry,
347 XtCGeometry,
348 XtRString,
349 sizeof(String *),
350 XtOffsetOf(gui_T, geom),
351 XtRImmediate,
352 ""
353 },
354 {
355 XtNreverseVideo,
356 XtCReverseVideo,
357 XtRBool,
358 sizeof(Bool),
359 XtOffsetOf(gui_T, rsrc_rev_video),
360 XtRImmediate,
361 (XtPointer)False
362 },
363 {
364 XtNborderWidth,
365 XtCBorderWidth,
366 XtRInt,
367 sizeof(int),
368 XtOffsetOf(gui_T, border_width),
369 XtRImmediate,
370 (XtPointer)2
371 },
372 {
373 XtNscrollbarWidth,
374 XtCScrollbarWidth,
375 XtRInt,
376 sizeof(int),
377 XtOffsetOf(gui_T, scrollbar_width),
378 XtRImmediate,
379 (XtPointer)SB_DEFAULT_WIDTH
380 },
381#ifdef FEAT_MENU
382# ifdef FEAT_GUI_ATHENA /* with Motif the height is always computed */
383 {
384 XtNmenuHeight,
385 XtCMenuHeight,
386 XtRInt,
387 sizeof(int),
388 XtOffsetOf(gui_T, menu_height),
389 XtRImmediate,
390 (XtPointer)MENU_DEFAULT_HEIGHT /* Should figure out at run time */
391 },
392# endif
393 {
394# ifdef FONTSET_ALWAYS
395 XtNmenuFontSet,
396 XtCMenuFontSet,
397#else
398 XtNmenuFont,
399 XtCMenuFont,
400#endif
401 XtRString,
402 sizeof(char *),
403 XtOffsetOf(gui_T, rsrc_menu_font_name),
404 XtRString,
405 DFLT_MENU_FONT
406 },
407#endif
408 {
409 XtNmenuForeground,
410 XtCMenuForeground,
411 XtRString,
412 sizeof(char *),
413 XtOffsetOf(gui_T, rsrc_menu_fg_name),
414 XtRString,
415 DFLT_MENU_FG_COLOR
416 },
417 {
418 XtNmenuBackground,
419 XtCMenuBackground,
420 XtRString,
421 sizeof(char *),
422 XtOffsetOf(gui_T, rsrc_menu_bg_name),
423 XtRString,
424 DFLT_MENU_BG_COLOR
425 },
426 {
427 XtNscrollForeground,
428 XtCScrollForeground,
429 XtRString,
430 sizeof(char *),
431 XtOffsetOf(gui_T, rsrc_scroll_fg_name),
432 XtRString,
433 DFLT_SCROLL_FG_COLOR
434 },
435 {
436 XtNscrollBackground,
437 XtCScrollBackground,
438 XtRString,
439 sizeof(char *),
440 XtOffsetOf(gui_T, rsrc_scroll_bg_name),
441 XtRString,
442 DFLT_SCROLL_BG_COLOR
443 },
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100444#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 {
446 XtNtooltipForeground,
447 XtCTooltipForeground,
448 XtRString,
449 sizeof(char *),
450 XtOffsetOf(gui_T, rsrc_tooltip_fg_name),
451 XtRString,
452 DFLT_TOOLTIP_FG_COLOR
453 },
454 {
455 XtNtooltipBackground,
456 XtCTooltipBackground,
457 XtRString,
458 sizeof(char *),
459 XtOffsetOf(gui_T, rsrc_tooltip_bg_name),
460 XtRString,
461 DFLT_TOOLTIP_BG_COLOR
462 },
463 {
464 XtNtooltipFont,
465 XtCTooltipFont,
466 XtRString,
467 sizeof(char *),
468 XtOffsetOf(gui_T, rsrc_tooltip_font_name),
469 XtRString,
470 DFLT_TOOLTIP_FONT
471 },
472 /* This one isn't really needed, keep for Sun Workshop? */
473 {
474 "balloonEvalFontSet",
475 XtCFontSet,
476 XtRFontSet,
477 sizeof(XFontSet),
478 XtOffsetOf(gui_T, tooltip_fontset),
479 XtRImmediate,
480 (XtPointer)NOFONTSET
481 },
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100482#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483#ifdef FEAT_XIM
484 {
485 "preeditType",
486 "PreeditType",
487 XtRString,
488 sizeof(char*),
489 XtOffsetOf(gui_T, rsrc_preedit_type_name),
490 XtRString,
491 (XtPointer)"OverTheSpot,OffTheSpot,Root"
492 },
493 {
494 "inputMethod",
495 "InputMethod",
496 XtRString,
497 sizeof(char*),
498 XtOffsetOf(gui_T, rsrc_input_method),
499 XtRString,
500 NULL
501 },
502#endif /* FEAT_XIM */
503};
504
505/*
506 * This table holds all the X GUI command line options allowed. This includes
507 * the standard ones so that we can skip them when vim is started without the
508 * GUI (but the GUI might start up later).
509 * When changing this, also update doc/vim_gui.txt and the usage message!!!
510 */
511static XrmOptionDescRec cmdline_options[] =
512{
513 /* We handle these options ourselves */
514 {"-bg", ".background", XrmoptionSepArg, NULL},
515 {"-background", ".background", XrmoptionSepArg, NULL},
516 {"-fg", ".foreground", XrmoptionSepArg, NULL},
517 {"-foreground", ".foreground", XrmoptionSepArg, NULL},
518 {"-fn", ".font", XrmoptionSepArg, NULL},
519 {"-font", ".font", XrmoptionSepArg, NULL},
520 {"-boldfont", ".boldFont", XrmoptionSepArg, NULL},
521 {"-italicfont", ".italicFont", XrmoptionSepArg, NULL},
522 {"-geom", ".geometry", XrmoptionSepArg, NULL},
523 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
524 {"-reverse", "*reverseVideo", XrmoptionNoArg, "True"},
525 {"-rv", "*reverseVideo", XrmoptionNoArg, "True"},
526 {"+reverse", "*reverseVideo", XrmoptionNoArg, "False"},
527 {"+rv", "*reverseVideo", XrmoptionNoArg, "False"},
528 {"-display", ".display", XrmoptionSepArg, NULL},
Bram Moolenaara5792f52005-11-23 21:25:05 +0000529 {"-iconic", ".iconic", XrmoptionNoArg, "True"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 {"-name", ".name", XrmoptionSepArg, NULL},
531 {"-bw", ".borderWidth", XrmoptionSepArg, NULL},
532 {"-borderwidth", ".borderWidth", XrmoptionSepArg, NULL},
533 {"-sw", ".scrollbarWidth", XrmoptionSepArg, NULL},
534 {"-scrollbarwidth", ".scrollbarWidth", XrmoptionSepArg, NULL},
535 {"-mh", ".menuHeight", XrmoptionSepArg, NULL},
536 {"-menuheight", ".menuHeight", XrmoptionSepArg, NULL},
537#ifdef FONTSET_ALWAYS
538 {"-mf", ".menuFontSet", XrmoptionSepArg, NULL},
539 {"-menufont", ".menuFontSet", XrmoptionSepArg, NULL},
540 {"-menufontset", ".menuFontSet", XrmoptionSepArg, NULL},
541#else
542 {"-mf", ".menuFont", XrmoptionSepArg, NULL},
543 {"-menufont", ".menuFont", XrmoptionSepArg, NULL},
544#endif
545 {"-xrm", NULL, XrmoptionResArg, NULL}
546};
547
548static int gui_argc = 0;
549static char **gui_argv = NULL;
550
551/*
552 * Call-back routines.
553 */
554
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100556gui_x11_timer_cb(
557 XtPointer timed_out,
558 XtIntervalId *interval_id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559{
560 *((int *)timed_out) = TRUE;
561}
562
Bram Moolenaar1dccf632017-08-27 17:38:27 +0200563#ifdef FEAT_JOB_CHANNEL
564 static void
565channel_poll_cb(
566 XtPointer client_data,
567 XtIntervalId *interval_id UNUSED)
568{
569 XtIntervalId *channel_timer = (XtIntervalId *)client_data;
570
571 /* Using an event handler for a channel that may be disconnected does
572 * not work, it hangs. Instead poll for messages. */
573 channel_handle_events(TRUE);
574 parse_queued_messages();
575
576 /* repeat */
577 *channel_timer = XtAppAddTimeOut(app_context, (long_u)20,
578 channel_poll_cb, client_data);
579}
580#endif
581
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100583gui_x11_visibility_cb(
584 Widget w UNUSED,
585 XtPointer dud UNUSED,
586 XEvent *event,
587 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
589 if (event->type != VisibilityNotify)
590 return;
591
592 gui.visibility = event->xvisibility.state;
593
594 /*
595 * When we do an XCopyArea(), and the window is partially obscured, we want
596 * to receive an event to tell us whether it worked or not.
597 */
598 XSetGraphicsExposures(gui.dpy, gui.text_gc,
599 gui.visibility != VisibilityUnobscured);
600
601 /* This is needed for when redrawing is slow. */
602 gui_mch_update();
603}
604
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100606gui_x11_expose_cb(
607 Widget w UNUSED,
608 XtPointer dud UNUSED,
609 XEvent *event,
610 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611{
612 XExposeEvent *gevent;
613 int new_x;
614
615 if (event->type != Expose)
616 return;
617
618 out_flush(); /* make sure all output has been processed */
619
620 gevent = (XExposeEvent *)event;
621 gui_redraw(gevent->x, gevent->y, gevent->width, gevent->height);
622
623 new_x = FILL_X(0);
624
625 /* Clear the border areas if needed */
626 if (gevent->x < new_x)
627 XClearArea(gui.dpy, gui.wid, 0, 0, new_x, 0, False);
628 if (gevent->y < FILL_Y(0))
629 XClearArea(gui.dpy, gui.wid, 0, 0, 0, FILL_Y(0), False);
630 if (gevent->x > FILL_X(Columns))
631 XClearArea(gui.dpy, gui.wid, FILL_X((int)Columns), 0, 0, 0, False);
632 if (gevent->y > FILL_Y(Rows))
633 XClearArea(gui.dpy, gui.wid, 0, FILL_Y((int)Rows), 0, 0, False);
634
635 /* This is needed for when redrawing is slow. */
636 gui_mch_update();
637}
638
Bram Moolenaar67c53842010-05-22 18:28:27 +0200639#if ((defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)) \
640 && defined(FEAT_GUI_MOTIF)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641/*
Bram Moolenaar231334e2005-07-25 20:46:57 +0000642 * This function fills in the XRectangle object with the current x,y
643 * coordinates and height, width so that an XtVaSetValues to the same shell of
Bram Moolenaar49325942007-05-10 19:19:59 +0000644 * those resources will restore the window to its former position and
Bram Moolenaar231334e2005-07-25 20:46:57 +0000645 * dimensions.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 *
Bram Moolenaar231334e2005-07-25 20:46:57 +0000647 * Note: This function may fail, in which case the XRectangle will be
648 * unchanged. Be sure to have the XRectangle set with the proper values for a
649 * failed condition prior to calling this function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 */
651 static void
652shellRectangle(Widget shell, XRectangle *r)
653{
654 Window rootw, shellw, child, parentw;
655 int absx, absy;
656 XWindowAttributes a;
657 Window *children;
658 unsigned int childrenCount;
659
660 shellw = XtWindow(shell);
661 if (shellw == 0)
662 return;
663 for (;;)
664 {
665 XQueryTree(XtDisplay(shell), shellw, &rootw, &parentw,
666 &children, &childrenCount);
667 XFree(children);
668 if (parentw == rootw)
669 break;
670 shellw = parentw;
671 }
672 XGetWindowAttributes(XtDisplay(shell), shellw, &a);
673 XTranslateCoordinates(XtDisplay(shell), shellw, a.root, 0, 0,
674 &absx, &absy, &child);
675 r->x = absx;
676 r->y = absy;
677 XtVaGetValues(shell, XmNheight, &r->height, XmNwidth, &r->width, NULL);
678}
679#endif
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100682gui_x11_resize_window_cb(
683 Widget w UNUSED,
684 XtPointer dud UNUSED,
685 XEvent *event,
686 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687{
688 static int lastWidth, lastHeight;
689
690 if (event->type != ConfigureNotify)
691 return;
692
693 if (event->xconfigure.width != lastWidth
694 || event->xconfigure.height != lastHeight)
695 {
696 lastWidth = event->xconfigure.width;
697 lastHeight = event->xconfigure.height;
698 gui_resize_shell(event->xconfigure.width, event->xconfigure.height
699#ifdef FEAT_XIM
700 - xim_get_status_area_height()
701#endif
702 );
703 }
704#ifdef FEAT_SUN_WORKSHOP
705 if (usingSunWorkShop)
706 {
707 XRectangle rec;
708
709 shellRectangle(w, &rec);
710 workshop_frame_moved(rec.x, rec.y, rec.width, rec.height);
711 }
712#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +0200713#if defined(FEAT_NETBEANS_INTG) && defined(FEAT_GUI_MOTIF)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200714 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 {
716 XRectangle rec;
717
718 shellRectangle(w, &rec);
719 netbeans_frame_moved(rec.x, rec.y);
720 }
721#endif
722#ifdef FEAT_XIM
723 xim_set_preedit();
724#endif
725}
726
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100728gui_x11_focus_change_cb(
729 Widget w UNUSED,
730 XtPointer data UNUSED,
731 XEvent *event,
732 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733{
734 gui_focus_change(event->type == FocusIn);
735}
736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100738gui_x11_enter_cb(
739 Widget w UNUSED,
740 XtPointer data UNUSED,
741 XEvent *event UNUSED,
742 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743{
744 gui_focus_change(TRUE);
745}
746
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100748gui_x11_leave_cb(
749 Widget w UNUSED,
750 XtPointer data UNUSED,
751 XEvent *event UNUSED,
752 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753{
754 gui_focus_change(FALSE);
755}
756
757#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
758# if X_HAVE_UTF8_STRING
759# define USE_UTF8LOOKUP
760# endif
761#endif
762
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100764gui_x11_key_hit_cb(
765 Widget w UNUSED,
766 XtPointer dud UNUSED,
767 XEvent *event,
768 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769{
770 XKeyPressedEvent *ev_press;
771#ifdef FEAT_XIM
772 char_u string2[256];
773 char_u string_shortbuf[256];
774 char_u *string = string_shortbuf;
775 Boolean string_alloced = False;
776 Status status;
777#else
778 char_u string[4], string2[3];
779#endif
780 KeySym key_sym, key_sym2;
781 int len, len2;
782 int i;
783 int modifiers;
784 int key;
785
786 ev_press = (XKeyPressedEvent *)event;
787
788#ifdef FEAT_XIM
789 if (xic)
790 {
791# ifdef USE_UTF8LOOKUP
792 /* XFree86 4.0.2 or newer: Be able to get UTF-8 characters even when
793 * the locale isn't utf-8. */
794 if (enc_utf8)
795 len = Xutf8LookupString(xic, ev_press, (char *)string,
796 sizeof(string_shortbuf), &key_sym, &status);
797 else
798# endif
799 len = XmbLookupString(xic, ev_press, (char *)string,
800 sizeof(string_shortbuf), &key_sym, &status);
801 if (status == XBufferOverflow)
802 {
803 string = (char_u *)XtMalloc(len + 1);
804 string_alloced = True;
805# ifdef USE_UTF8LOOKUP
806 /* XFree86 4.0.2 or newer: Be able to get UTF-8 characters even
807 * when the locale isn't utf-8. */
808 if (enc_utf8)
809 len = Xutf8LookupString(xic, ev_press, (char *)string,
810 len, &key_sym, &status);
811 else
812# endif
813 len = XmbLookupString(xic, ev_press, (char *)string,
814 len, &key_sym, &status);
815 }
816 if (status == XLookupNone || status == XLookupChars)
817 key_sym = XK_VoidSymbol;
818
819# ifdef FEAT_MBYTE
820 /* Do conversion from 'termencoding' to 'encoding'. When using
821 * Xutf8LookupString() it has already been done. */
822 if (len > 0 && input_conv.vc_type != CONV_NONE
823# ifdef USE_UTF8LOOKUP
824 && !enc_utf8
825# endif
826 )
827 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100828 int maxlen = len * 4 + 40; /* guessed */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 char_u *p = (char_u *)XtMalloc(maxlen);
830
831 mch_memmove(p, string, len);
832 if (string_alloced)
833 XtFree((char *)string);
834 string = p;
835 string_alloced = True;
836 len = convert_input(p, len, maxlen);
837 }
838# endif
839
840 /* Translate CSI to K_CSI, otherwise it could be recognized as the
841 * start of a special key. */
842 for (i = 0; i < len; ++i)
843 if (string[i] == CSI)
844 {
845 char_u *p = (char_u *)XtMalloc(len + 3);
846
847 mch_memmove(p, string, i + 1);
848 p[i + 1] = KS_EXTRA;
849 p[i + 2] = (int)KE_CSI;
850 mch_memmove(p + i + 3, string + i + 1, len - i);
851 if (string_alloced)
852 XtFree((char *)string);
853 string = p;
854 string_alloced = True;
855 i += 2;
856 len += 2;
857 }
858 }
859 else
860#endif
861 len = XLookupString(ev_press, (char *)string, sizeof(string),
862 &key_sym, NULL);
863
864#ifdef SunXK_F36
865 /*
866 * These keys have bogus lookup strings, and trapping them here is
867 * easier than trying to XRebindKeysym() on them with every possible
868 * combination of modifiers.
869 */
870 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
871 len = 0;
872#endif
873
874#ifdef FEAT_HANGULIN
875 if ((key_sym == XK_space) && (ev_press->state & ShiftMask))
876 {
877 hangul_input_state_toggle();
878 goto theend;
879 }
880#endif
881
882 if (key_sym == XK_space)
883 string[0] = ' '; /* Otherwise Ctrl-Space doesn't work */
884
885 /*
886 * Only on some machines ^_ requires Ctrl+Shift+minus. For consistency,
887 * allow just Ctrl+minus too.
888 */
889 if (key_sym == XK_minus && (ev_press->state & ControlMask))
890 string[0] = Ctrl__;
891
892#ifdef XK_ISO_Left_Tab
893 /* why do we get XK_ISO_Left_Tab instead of XK_Tab for shift-tab? */
894 if (key_sym == XK_ISO_Left_Tab)
895 {
896 key_sym = XK_Tab;
897 string[0] = TAB;
898 len = 1;
899 }
900#endif
901
902 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
903 * that already has the 8th bit set. And not when using a double-byte
904 * encoding, setting the 8th bit may make it the lead byte of a
905 * double-byte character. */
906 if (len == 1
907 && (ev_press->state & Mod1Mask)
908 && !(key_sym == XK_BackSpace || key_sym == XK_Delete)
909 && (string[0] & 0x80) == 0
910#ifdef FEAT_MBYTE
911 && !enc_dbcs
912#endif
913 )
914 {
915#if defined(FEAT_MENU) && defined(FEAT_GUI_MOTIF)
916 /* Ignore ALT keys when they are used for the menu only */
917 if (gui.menu_is_active
918 && (p_wak[0] == 'y'
919 || (p_wak[0] == 'm' && gui_is_menu_shortcut(string[0]))))
920 goto theend;
921#endif
922 /*
923 * Before we set the 8th bit, check to make sure the user doesn't
924 * already have a mapping defined for this sequence. We determine this
925 * by checking to see if the input would be the same without the
926 * Alt/Meta key.
927 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
928 */
929 ev_press->state &= ~Mod1Mask;
930 len2 = XLookupString(ev_press, (char *)string2, sizeof(string2),
931 &key_sym2, NULL);
932 if (key_sym2 == XK_space)
933 string2[0] = ' '; /* Otherwise Meta-Ctrl-Space doesn't work */
934 if ( len2 == 1
935 && string[0] == string2[0]
936 && !(key_sym == XK_Tab && (ev_press->state & ShiftMask)))
937 {
938 string[0] |= 0x80;
939#ifdef FEAT_MBYTE
940 if (enc_utf8) /* convert to utf-8 */
941 {
942 string[1] = string[0] & 0xbf;
943 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
944 if (string[1] == CSI)
945 {
946 string[2] = KS_EXTRA;
947 string[3] = (int)KE_CSI;
948 len = 4;
949 }
950 else
951 len = 2;
952 }
953#endif
954 }
955 else
956 ev_press->state |= Mod1Mask;
957 }
958
959 if (len == 1 && string[0] == CSI)
960 {
961 string[1] = KS_EXTRA;
962 string[2] = (int)KE_CSI;
963 len = -3;
964 }
965
966 /* Check for special keys. Also do this when len == 1 (key has an ASCII
967 * value) to detect backspace, delete and keypad keys. */
968 if (len == 0 || len == 1)
969 {
970 for (i = 0; special_keys[i].key_sym != (KeySym)0; i++)
971 {
972 if (special_keys[i].key_sym == key_sym)
973 {
974 string[0] = CSI;
975 string[1] = special_keys[i].vim_code0;
976 string[2] = special_keys[i].vim_code1;
977 len = -3;
978 break;
979 }
980 }
981 }
982
983 /* Unrecognised key is ignored. */
984 if (len == 0)
985 goto theend;
986
987 /* Special keys (and a few others) may have modifiers. Also when using a
988 * double-byte encoding (can't set the 8th bit). */
989 if (len == -3 || key_sym == XK_space || key_sym == XK_Tab
990 || key_sym == XK_Return || key_sym == XK_Linefeed
991 || key_sym == XK_Escape
992#ifdef FEAT_MBYTE
993 || (enc_dbcs && len == 1 && (ev_press->state & Mod1Mask))
994#endif
995 )
996 {
997 modifiers = 0;
998 if (ev_press->state & ShiftMask)
999 modifiers |= MOD_MASK_SHIFT;
1000 if (ev_press->state & ControlMask)
1001 modifiers |= MOD_MASK_CTRL;
1002 if (ev_press->state & Mod1Mask)
1003 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001004 if (ev_press->state & Mod4Mask)
1005 modifiers |= MOD_MASK_META;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
1007 /*
1008 * For some keys a shift modifier is translated into another key
1009 * code.
1010 */
1011 if (len == -3)
1012 key = TO_SPECIAL(string[1], string[2]);
1013 else
1014 key = string[0];
1015 key = simplify_key(key, &modifiers);
1016 if (key == CSI)
1017 key = K_CSI;
1018 if (IS_SPECIAL(key))
1019 {
1020 string[0] = CSI;
1021 string[1] = K_SECOND(key);
1022 string[2] = K_THIRD(key);
1023 len = 3;
1024 }
1025 else
1026 {
1027 string[0] = key;
1028 len = 1;
1029 }
1030
1031 if (modifiers != 0)
1032 {
1033 string2[0] = CSI;
1034 string2[1] = KS_MODIFIER;
1035 string2[2] = modifiers;
1036 add_to_input_buf(string2, 3);
1037 }
1038 }
1039
1040 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1041#ifdef UNIX
1042 || (intr_char != 0 && string[0] == intr_char)
1043#endif
1044 ))
1045 {
1046 trash_input_buf();
1047 got_int = TRUE;
1048 }
1049
1050 add_to_input_buf(string, len);
1051
1052 /*
1053 * blank out the pointer if necessary
1054 */
1055 if (p_mh)
1056 gui_mch_mousehide(TRUE);
1057
1058#if defined(FEAT_BEVAL_TIP)
1059 {
1060 BalloonEval *be;
1061
1062 if ((be = gui_mch_currently_showing_beval()) != NULL)
1063 gui_mch_unpost_balloon(be);
1064 }
1065#endif
1066theend:
1067 {} /* some compilers need a statement here */
1068#ifdef FEAT_XIM
1069 if (string_alloced)
1070 XtFree((char *)string);
1071#endif
1072}
1073
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001075gui_x11_mouse_cb(
1076 Widget w UNUSED,
1077 XtPointer dud UNUSED,
1078 XEvent *event,
1079 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080{
1081 static XtIntervalId timer = (XtIntervalId)0;
1082 static int timed_out = TRUE;
1083
1084 int button;
1085 int repeated_click = FALSE;
1086 int x, y;
1087 int_u x_modifiers;
1088 int_u vim_modifiers;
1089
1090 if (event->type == MotionNotify)
1091 {
1092 /* Get the latest position, avoids lagging behind on a drag. */
1093 x = event->xmotion.x;
1094 y = event->xmotion.y;
1095 x_modifiers = event->xmotion.state;
1096 button = (x_modifiers & (Button1Mask | Button2Mask | Button3Mask))
1097 ? MOUSE_DRAG : ' ';
1098
1099 /*
1100 * if our pointer is currently hidden, then we should show it.
1101 */
1102 gui_mch_mousehide(FALSE);
1103
1104 if (button != MOUSE_DRAG) /* just moving the rodent */
1105 {
1106#ifdef FEAT_MENU
1107 if (dud) /* moved in vimForm */
1108 y -= gui.menu_height;
1109#endif
1110 gui_mouse_moved(x, y);
1111 return;
1112 }
1113 }
1114 else
1115 {
1116 x = event->xbutton.x;
1117 y = event->xbutton.y;
1118 if (event->type == ButtonPress)
1119 {
1120 /* Handle multiple clicks */
1121 if (!timed_out)
1122 {
1123 XtRemoveTimeOut(timer);
1124 repeated_click = TRUE;
1125 }
1126 timed_out = FALSE;
1127 timer = XtAppAddTimeOut(app_context, (long_u)p_mouset,
1128 gui_x11_timer_cb, &timed_out);
1129 switch (event->xbutton.button)
1130 {
Bram Moolenaar88e484b2015-11-24 15:38:44 +01001131 /* keep in sync with gui_gtk_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 case Button1: button = MOUSE_LEFT; break;
1133 case Button2: button = MOUSE_MIDDLE; break;
1134 case Button3: button = MOUSE_RIGHT; break;
1135 case Button4: button = MOUSE_4; break;
1136 case Button5: button = MOUSE_5; break;
Bram Moolenaar88e484b2015-11-24 15:38:44 +01001137 case 6: button = MOUSE_7; break;
1138 case 7: button = MOUSE_6; break;
1139 case 8: button = MOUSE_X1; break;
1140 case 9: button = MOUSE_X2; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 default:
1142 return; /* Unknown button */
1143 }
1144 }
1145 else if (event->type == ButtonRelease)
1146 button = MOUSE_RELEASE;
1147 else
1148 return; /* Unknown mouse event type */
1149
1150 x_modifiers = event->xbutton.state;
1151#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1152 last_mouse_event = event->xbutton;
1153#endif
1154 }
1155
1156 vim_modifiers = 0x0;
1157 if (x_modifiers & ShiftMask)
1158 vim_modifiers |= MOUSE_SHIFT;
1159 if (x_modifiers & ControlMask)
1160 vim_modifiers |= MOUSE_CTRL;
1161 if (x_modifiers & Mod1Mask) /* Alt or Meta key */
1162 vim_modifiers |= MOUSE_ALT;
1163
1164 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
1165}
1166
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167/*
1168 * End of call-back routines
1169 */
1170
1171/*
1172 * Parse the GUI related command-line arguments. Any arguments used are
1173 * deleted from argv, and *argc is decremented accordingly. This is called
1174 * when vim is started, whether or not the GUI has been started.
1175 */
1176 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001177gui_mch_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178{
1179 int arg;
1180 int i;
1181
1182 /*
1183 * Move all the entries in argv which are relevant to X into gui_argv.
1184 */
1185 gui_argc = 0;
1186 gui_argv = (char **)lalloc((long_u)(*argc * sizeof(char *)), FALSE);
1187 if (gui_argv == NULL)
1188 return;
1189 gui_argv[gui_argc++] = argv[0];
1190 arg = 1;
1191 while (arg < *argc)
1192 {
1193 /* Look for argv[arg] in cmdline_options[] table */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001194 for (i = 0; i < (int)XtNumber(cmdline_options); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 if (strcmp(argv[arg], cmdline_options[i].option) == 0)
1196 break;
1197
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001198 if (i < (int)XtNumber(cmdline_options))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 {
1200 /* Remember finding "-rv" or "-reverse" */
1201 if (strcmp("-rv", argv[arg]) == 0
1202 || strcmp("-reverse", argv[arg]) == 0)
1203 found_reverse_arg = TRUE;
1204 else if ((strcmp("-fn", argv[arg]) == 0
1205 || strcmp("-font", argv[arg]) == 0)
1206 && arg + 1 < *argc)
1207 font_argument = argv[arg + 1];
1208
1209 /* Found match in table, so move it into gui_argv */
1210 gui_argv[gui_argc++] = argv[arg];
1211 if (--*argc > arg)
1212 {
1213 mch_memmove(&argv[arg], &argv[arg + 1], (*argc - arg)
1214 * sizeof(char *));
1215 if (cmdline_options[i].argKind != XrmoptionNoArg)
1216 {
1217 /* Move the options argument as well */
1218 gui_argv[gui_argc++] = argv[arg];
1219 if (--*argc > arg)
1220 mch_memmove(&argv[arg], &argv[arg + 1], (*argc - arg)
1221 * sizeof(char *));
1222 }
1223 }
1224 argv[*argc] = NULL;
1225 }
1226 else
1227#ifdef FEAT_SUN_WORKSHOP
1228 if (strcmp("-ws", argv[arg]) == 0)
1229 {
1230 usingSunWorkShop++;
1231 p_acd = TRUE;
1232 gui.dofork = FALSE; /* don't fork() when starting GUI */
1233 mch_memmove(&argv[arg], &argv[arg + 1],
1234 (--*argc - arg) * sizeof(char *));
1235 argv[*argc] = NULL;
1236# ifdef WSDEBUG
1237 wsdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
1238 wsdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
1239# endif
1240 }
1241 else
1242#endif
1243#ifdef FEAT_NETBEANS_INTG
1244 if (strncmp("-nb", argv[arg], 3) == 0)
1245 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 gui.dofork = FALSE; /* don't fork() when starting GUI */
1247 netbeansArg = argv[arg];
1248 mch_memmove(&argv[arg], &argv[arg + 1],
1249 (--*argc - arg) * sizeof(char *));
1250 argv[*argc] = NULL;
1251 }
1252 else
1253#endif
1254 arg++;
1255 }
1256}
1257
1258#ifndef XtSpecificationRelease
1259# define CARDINAL (Cardinal *)
1260#else
1261# if XtSpecificationRelease == 4
1262# define CARDINAL (Cardinal *)
1263# else
1264# define CARDINAL (int *)
1265# endif
1266#endif
1267
1268/*
1269 * Check if the GUI can be started. Called before gvimrc is sourced.
1270 * Return OK or FAIL.
1271 */
1272 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001273gui_mch_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274{
1275#ifdef FEAT_XIM
1276 XtSetLanguageProc(NULL, NULL, NULL);
1277#endif
1278 open_app_context();
1279 if (app_context != NULL)
1280 gui.dpy = XtOpenDisplay(app_context, 0, VIM_NAME, VIM_CLASS,
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00001281 cmdline_options, XtNumber(cmdline_options),
1282 CARDINAL &gui_argc, gui_argv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283
Bram Moolenaar889fe2c2018-05-13 16:23:40 +02001284# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1285 {
1286 /* The call to XtOpenDisplay() may have set the locale from the
1287 * environment. Set LC_NUMERIC to "C" to make sure that strtod() uses a
1288 * decimal point, not a comma. */
1289 char *p = setlocale(LC_NUMERIC, NULL);
1290
1291 if (p == NULL || strcmp(p, "C") != 0)
1292 setlocale(LC_NUMERIC, "C");
1293 }
1294# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 if (app_context == NULL || gui.dpy == NULL)
1296 {
1297 gui.dying = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001298 emsg(_(e_opendisp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 return FAIL;
1300 }
1301 return OK;
1302}
1303
1304
1305#ifdef USE_XSMP
1306/*
1307 * Handle XSMP processing, de-registering the attachment upon error
1308 */
1309static XtInputId _xsmp_xtinputid;
1310
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001312local_xsmp_handle_requests(
1313 XtPointer c UNUSED,
1314 int *s UNUSED,
1315 XtInputId *i UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316{
1317 if (xsmp_handle_requests() == FAIL)
1318 XtRemoveInput(_xsmp_xtinputid);
1319}
1320#endif
1321
1322
1323/*
1324 * Initialise the X GUI. Create all the windows, set up all the call-backs etc.
1325 * Returns OK for success, FAIL when the GUI can't be started.
1326 */
1327 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001328gui_mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329{
1330 XtGCMask gc_mask;
1331 XGCValues gc_vals;
1332 int x, y, mask;
1333 unsigned w, h;
1334
1335#if 0
1336 /* Uncomment this to enable synchronous mode for debugging */
1337 XSynchronize(gui.dpy, True);
1338#endif
1339
1340 vimShell = XtVaAppCreateShell(VIM_NAME, VIM_CLASS,
1341 applicationShellWidgetClass, gui.dpy, NULL);
1342
1343 /*
1344 * Get the application resources
1345 */
1346 XtVaGetApplicationResources(vimShell, (XtPointer)&gui,
1347 vim_resources, XtNumber(vim_resources), NULL);
1348
1349 gui.scrollbar_height = gui.scrollbar_width;
1350
1351 /*
1352 * Get the colors ourselves. Using the automatic conversion doesn't
1353 * handle looking for approximate colors.
1354 */
1355 /* NOTE: These next few lines are an exact duplicate of gui_athena.c's
1356 * gui_mch_def_colors(). Why?
1357 */
1358 gui.menu_fg_pixel = gui_get_color((char_u *)gui.rsrc_menu_fg_name);
1359 gui.menu_bg_pixel = gui_get_color((char_u *)gui.rsrc_menu_bg_name);
1360 gui.scroll_fg_pixel = gui_get_color((char_u *)gui.rsrc_scroll_fg_name);
1361 gui.scroll_bg_pixel = gui_get_color((char_u *)gui.rsrc_scroll_bg_name);
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001362#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 gui.tooltip_fg_pixel = gui_get_color((char_u *)gui.rsrc_tooltip_fg_name);
1364 gui.tooltip_bg_pixel = gui_get_color((char_u *)gui.rsrc_tooltip_bg_name);
1365#endif
1366
1367#if defined(FEAT_MENU) && defined(FEAT_GUI_ATHENA)
1368 /* If the menu height was set, don't change it at runtime */
1369 if (gui.menu_height != MENU_DEFAULT_HEIGHT)
1370 gui.menu_height_fixed = TRUE;
1371#endif
1372
1373 /* Set default foreground and background colours */
1374 gui.norm_pixel = gui.def_norm_pixel;
1375 gui.back_pixel = gui.def_back_pixel;
1376
1377 /* Check if reverse video needs to be applied (on Sun it's done by X) */
1378 if (gui.rsrc_rev_video && gui_get_lightness(gui.back_pixel)
1379 > gui_get_lightness(gui.norm_pixel))
1380 {
1381 gui.norm_pixel = gui.def_back_pixel;
1382 gui.back_pixel = gui.def_norm_pixel;
1383 gui.def_norm_pixel = gui.norm_pixel;
1384 gui.def_back_pixel = gui.back_pixel;
1385 }
1386
1387 /* Get the colors from the "Normal", "Tooltip", "Scrollbar" and "Menu"
1388 * group (set in syntax.c or in a vimrc file) */
1389 set_normal_colors();
1390
1391 /*
1392 * Check that none of the colors are the same as the background color
1393 */
1394 gui_check_colors();
1395
1396 /*
1397 * Set up the GCs. The font attributes will be set in gui_init_font().
1398 */
1399 gc_mask = GCForeground | GCBackground;
1400 gc_vals.foreground = gui.norm_pixel;
1401 gc_vals.background = gui.back_pixel;
1402 gui.text_gc = XtGetGC(vimShell, gc_mask, &gc_vals);
1403
1404 gc_vals.foreground = gui.back_pixel;
1405 gc_vals.background = gui.norm_pixel;
1406 gui.back_gc = XtGetGC(vimShell, gc_mask, &gc_vals);
1407
1408 gc_mask |= GCFunction;
1409 gc_vals.foreground = gui.norm_pixel ^ gui.back_pixel;
1410 gc_vals.background = gui.norm_pixel ^ gui.back_pixel;
1411 gc_vals.function = GXxor;
1412 gui.invert_gc = XtGetGC(vimShell, gc_mask, &gc_vals);
1413
1414 gui.visibility = VisibilityUnobscured;
1415 x11_setup_atoms(gui.dpy);
1416
1417 if (gui_win_x != -1 && gui_win_y != -1)
1418 gui_mch_set_winpos(gui_win_x, gui_win_y);
1419
1420 /* Now adapt the supplied(?) geometry-settings */
1421 /* Added by Kjetil Jacobsen <kjetilja@stud.cs.uit.no> */
1422 if (gui.geom != NULL && *gui.geom != NUL)
1423 {
1424 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
1425 if (mask & WidthValue)
1426 Columns = w;
1427 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00001428 {
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001429 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00001430 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00001432 }
Bram Moolenaarc33916a2013-07-01 21:43:08 +02001433 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 /*
1435 * Set the (x,y) position of the main window only if specified in the
1436 * users geometry, so we get good defaults when they don't. This needs
1437 * to be done before the shell is popped up.
1438 */
1439 if (mask & (XValue|YValue))
1440 XtVaSetValues(vimShell, XtNgeometry, gui.geom, NULL);
1441 }
1442
1443 gui_x11_create_widgets();
1444
1445 /*
1446 * Add an icon to Vim (Marcel Douben: 11 May 1998).
1447 */
1448 if (vim_strchr(p_go, GO_ICON) != NULL)
1449 {
1450#ifndef HAVE_XPM
1451# include "vim_icon.xbm"
1452# include "vim_mask.xbm"
1453
1454 Arg arg[2];
1455
1456 XtSetArg(arg[0], XtNiconPixmap,
1457 XCreateBitmapFromData(gui.dpy,
1458 DefaultRootWindow(gui.dpy),
1459 (char *)vim_icon_bits,
1460 vim_icon_width,
1461 vim_icon_height));
1462 XtSetArg(arg[1], XtNiconMask,
1463 XCreateBitmapFromData(gui.dpy,
1464 DefaultRootWindow(gui.dpy),
1465 (char *)vim_mask_icon_bits,
1466 vim_mask_icon_width,
1467 vim_mask_icon_height));
1468 XtSetValues(vimShell, arg, (Cardinal)2);
1469#else
1470/* Use Pixmaps, looking much nicer. */
1471
1472/* If you get an error message here, you still need to unpack the runtime
1473 * archive! */
1474# ifdef magick
1475# undef magick
1476# endif
1477# define magick vim32x32
1478# include "../runtime/vim32x32.xpm"
1479# undef magick
1480# define magick vim16x16
1481# include "../runtime/vim16x16.xpm"
1482# undef magick
1483# define magick vim48x48
1484# include "../runtime/vim48x48.xpm"
1485# undef magick
1486
1487 static Pixmap icon = 0;
1488 static Pixmap icon_mask = 0;
1489 static char **magick = vim32x32;
1490 Window root_window;
1491 XIconSize *size;
1492 int number_sizes;
1493 Display *dsp;
1494 Screen *scr;
1495 XpmAttributes attr;
1496 Colormap cmap;
1497
1498 /*
1499 * Adjust the icon to the preferences of the actual window manager.
1500 */
1501 root_window = XRootWindowOfScreen(XtScreen(vimShell));
1502 if (XGetIconSizes(XtDisplay(vimShell), root_window,
1503 &size, &number_sizes) != 0)
1504 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 if (number_sizes > 0)
1506 {
Bram Moolenaar6f292662013-07-14 15:06:50 +02001507 if (size->max_height >= 48 && size->max_width >= 48)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 magick = vim48x48;
Bram Moolenaar6f292662013-07-14 15:06:50 +02001509 else if (size->max_height >= 32 && size->max_width >= 32)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 magick = vim32x32;
Bram Moolenaar6f292662013-07-14 15:06:50 +02001511 else if (size->max_height >= 16 && size->max_width >= 16)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 magick = vim16x16;
1513 }
1514 }
1515
1516 dsp = XtDisplay(vimShell);
1517 scr = XtScreen(vimShell);
1518
1519 cmap = DefaultColormap(dsp, DefaultScreen(dsp));
1520 XtVaSetValues(vimShell, XtNcolormap, cmap, NULL);
1521
1522 attr.valuemask = 0L;
1523 attr.valuemask = XpmCloseness | XpmReturnPixels | XpmColormap | XpmDepth;
1524 attr.closeness = 65535; /* accuracy isn't crucial */
1525 attr.colormap = cmap;
1526 attr.depth = DefaultDepthOfScreen(scr);
1527
1528 if (!icon)
Bram Moolenaare8208012008-06-20 09:59:25 +00001529 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 XpmCreatePixmapFromData(dsp, root_window, magick, &icon,
1531 &icon_mask, &attr);
Bram Moolenaare8208012008-06-20 09:59:25 +00001532 XpmFreeAttributes(&attr);
1533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534
1535# ifdef FEAT_GUI_ATHENA
1536 XtVaSetValues(vimShell, XtNiconPixmap, icon, XtNiconMask, icon_mask, NULL);
1537# else
1538 XtVaSetValues(vimShell, XmNiconPixmap, icon, XmNiconMask, icon_mask, NULL);
1539# endif
1540#endif
1541 }
1542
1543 if (gui.color_approx)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001544 emsg(_("Vim E458: Cannot allocate colormap entry, some colors may be incorrect"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545
1546#ifdef FEAT_SUN_WORKSHOP
1547 if (usingSunWorkShop)
1548 workshop_connect(app_context);
1549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001551#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 gui_init_tooltip_font();
1553#endif
1554#ifdef FEAT_MENU
1555 gui_init_menu_font();
1556#endif
1557
1558#ifdef USE_XSMP
1559 /* Attach listener on ICE connection */
1560 if (-1 != xsmp_icefd)
1561 _xsmp_xtinputid = XtAppAddInput(app_context, xsmp_icefd,
1562 (XtPointer)XtInputReadMask, local_xsmp_handle_requests, NULL);
1563#endif
1564
1565 return OK;
1566}
1567
1568/*
1569 * Called when starting the GUI fails after calling gui_mch_init().
1570 */
1571 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001572gui_mch_uninit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573{
1574 gui_x11_destroy_widgets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 XtCloseDisplay(gui.dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 gui.dpy = NULL;
1577 vimShell = (Widget)0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001578 VIM_CLEAR(gui_argv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579}
1580
1581/*
1582 * Called when the foreground or background color has been changed.
1583 */
1584 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001585gui_mch_new_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586{
1587 long_u gc_mask;
1588 XGCValues gc_vals;
1589
1590 gc_mask = GCForeground | GCBackground;
1591 gc_vals.foreground = gui.norm_pixel;
1592 gc_vals.background = gui.back_pixel;
1593 if (gui.text_gc != NULL)
1594 XChangeGC(gui.dpy, gui.text_gc, gc_mask, &gc_vals);
1595
1596 gc_vals.foreground = gui.back_pixel;
1597 gc_vals.background = gui.norm_pixel;
1598 if (gui.back_gc != NULL)
1599 XChangeGC(gui.dpy, gui.back_gc, gc_mask, &gc_vals);
1600
1601 gc_mask |= GCFunction;
1602 gc_vals.foreground = gui.norm_pixel ^ gui.back_pixel;
1603 gc_vals.background = gui.norm_pixel ^ gui.back_pixel;
1604 gc_vals.function = GXxor;
1605 if (gui.invert_gc != NULL)
1606 XChangeGC(gui.dpy, gui.invert_gc, gc_mask, &gc_vals);
1607
1608 gui_x11_set_back_color();
1609}
1610
1611/*
1612 * Open the GUI window which was created by a call to gui_mch_init().
1613 */
1614 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001615gui_mch_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616{
1617 /* Actually open the window */
Bram Moolenaara5792f52005-11-23 21:25:05 +00001618 XtRealizeWidget(vimShell);
1619 XtManageChild(XtNameToWidget(vimShell, "*vimForm"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620
1621 gui.wid = gui_x11_get_wid();
1622 gui.blank_pointer = gui_x11_create_blank_mouse();
1623
1624 /*
1625 * Add a callback for the Close item on the window managers menu, and the
1626 * save-yourself event.
1627 */
1628 wm_atoms[SAVE_YOURSELF_IDX] =
1629 XInternAtom(gui.dpy, "WM_SAVE_YOURSELF", False);
1630 wm_atoms[DELETE_WINDOW_IDX] =
1631 XInternAtom(gui.dpy, "WM_DELETE_WINDOW", False);
1632 XSetWMProtocols(gui.dpy, XtWindow(vimShell), wm_atoms, 2);
1633 XtAddEventHandler(vimShell, NoEventMask, True, gui_x11_wm_protocol_handler,
1634 NULL);
1635#ifdef HAVE_X11_XMU_EDITRES_H
1636 /*
1637 * Enable editres protocol (see "man editres").
1638 * Usually will need to add -lXmu to the linker line as well.
1639 */
1640 XtAddEventHandler(vimShell, (EventMask)0, True, _XEditResCheckMessages,
1641 (XtPointer)NULL);
1642#endif
1643
1644#ifdef FEAT_CLIENTSERVER
1645 if (serverName == NULL && serverDelayedStartName != NULL)
1646 {
1647 /* This is a :gui command in a plain vim with no previous server */
1648 commWindow = XtWindow(vimShell);
1649 (void)serverRegisterName(gui.dpy, serverDelayedStartName);
1650 }
1651 else
1652 {
1653 /*
1654 * Cannot handle "widget-less" windows with XtProcessEvent() we'll
1655 * have to change the "server" registration to that of the main window
1656 * If we have not registered a name yet, remember the window
1657 */
1658 serverChangeRegisteredWindow(gui.dpy, XtWindow(vimShell));
1659 }
1660 XtAddEventHandler(vimShell, PropertyChangeMask, False,
1661 gui_x11_send_event_handler, NULL);
1662#endif
1663
1664
1665#if defined(FEAT_MENU) && defined(FEAT_GUI_ATHENA)
1666 /* The Athena GUI needs this again after opening the window */
1667 gui_position_menu();
1668# ifdef FEAT_TOOLBAR
1669 gui_mch_set_toolbar_pos(0, gui.menu_height, gui.menu_width,
1670 gui.toolbar_height);
1671# endif
1672#endif
1673
1674 /* Get the colors for the highlight groups (gui_check_colors() might have
1675 * changed them) */
1676 highlight_gui_started(); /* re-init colors and fonts */
1677
1678#ifdef FEAT_HANGULIN
1679 hangul_keyboard_set();
1680#endif
1681#ifdef FEAT_XIM
1682 xim_init();
1683#endif
1684#ifdef FEAT_SUN_WORKSHOP
1685 workshop_postinit();
1686#endif
1687
1688 return OK;
1689}
1690
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001691#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692/*
1693 * Convert the tooltip fontset name to an XFontSet.
1694 */
1695 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001696gui_init_tooltip_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697{
1698 XrmValue from, to;
1699
1700 from.addr = (char *)gui.rsrc_tooltip_font_name;
1701 from.size = strlen(from.addr);
1702 to.addr = (XtPointer)&gui.tooltip_fontset;
1703 to.size = sizeof(XFontSet);
1704
1705 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontSet, &to) == False)
1706 {
1707 /* Failed. What to do? */
1708 }
1709}
1710#endif
1711
1712#if defined(FEAT_MENU) || defined(PROTO)
1713/* Convert the menu font/fontset name to an XFontStruct/XFontset */
1714 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001715gui_init_menu_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716{
1717 XrmValue from, to;
1718
1719#ifdef FONTSET_ALWAYS
1720 from.addr = (char *)gui.rsrc_menu_font_name;
1721 from.size = strlen(from.addr);
1722 to.addr = (XtPointer)&gui.menu_fontset;
1723 to.size = sizeof(GuiFontset);
1724
1725 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontSet, &to) == False)
1726 {
1727 /* Failed. What to do? */
1728 }
1729#else
1730 from.addr = (char *)gui.rsrc_menu_font_name;
1731 from.size = strlen(from.addr);
1732 to.addr = (XtPointer)&gui.menu_font;
1733 to.size = sizeof(GuiFont);
1734
1735 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontStruct, &to) == False)
1736 {
1737 /* Failed. What to do? */
1738 }
1739#endif
1740}
1741#endif
1742
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001744gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745{
1746#if 0
1747 /* Lesstif gives an error message here, and so does Solaris. The man page
1748 * says that this isn't needed when exiting, so just skip it. */
1749 XtCloseDisplay(gui.dpy);
1750#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01001751 VIM_CLEAR(gui_argv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752}
1753
1754/*
1755 * Get the position of the top left corner of the window.
1756 */
1757 int
Bram Moolenaar02fdaea2016-01-30 18:13:55 +01001758gui_mch_get_winpos(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759{
1760 Dimension xpos, ypos;
1761
1762 XtVaGetValues(vimShell,
1763 XtNx, &xpos,
1764 XtNy, &ypos,
1765 NULL);
1766 *x = xpos;
1767 *y = ypos;
1768 return OK;
1769}
1770
1771/*
1772 * Set the position of the top left corner of the window to the given
1773 * coordinates.
1774 */
1775 void
Bram Moolenaar02fdaea2016-01-30 18:13:55 +01001776gui_mch_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
1778 XtVaSetValues(vimShell,
1779 XtNx, x,
1780 XtNy, y,
1781 NULL);
1782}
1783
1784 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001785gui_mch_set_shellsize(
1786 int width,
1787 int height,
1788 int min_width,
1789 int min_height,
1790 int base_width,
1791 int base_height,
1792 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793{
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00001794#ifdef FEAT_XIM
1795 height += xim_get_status_area_height(),
1796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 XtVaSetValues(vimShell,
1798 XtNwidthInc, gui.char_width,
1799 XtNheightInc, gui.char_height,
1800#if defined(XtSpecificationRelease) && XtSpecificationRelease >= 4
1801 XtNbaseWidth, base_width,
1802 XtNbaseHeight, base_height,
1803#endif
1804 XtNminWidth, min_width,
1805 XtNminHeight, min_height,
1806 XtNwidth, width,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 XtNheight, height,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 NULL);
1809}
1810
1811/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00001812 * Allow 10 pixels for horizontal borders, 'guiheadroom' for vertical borders.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 * Is there no way in X to find out how wide the borders really are?
1814 */
1815 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001816gui_mch_get_screen_dimensions(
1817 int *screen_w,
1818 int *screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819{
1820 *screen_w = DisplayWidth(gui.dpy, DefaultScreen(gui.dpy)) - 10;
1821 *screen_h = DisplayHeight(gui.dpy, DefaultScreen(gui.dpy)) - p_ghr;
1822}
1823
1824/*
1825 * Initialise vim to use the font "font_name". If it's NULL, pick a default
1826 * font.
1827 * If "fontset" is TRUE, load the "font_name" as a fontset.
1828 * Return FAIL if the font could not be loaded, OK otherwise.
1829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001831gui_mch_init_font(
1832 char_u *font_name,
1833 int do_fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834{
1835 XFontStruct *font = NULL;
1836
1837#ifdef FEAT_XFONTSET
1838 XFontSet fontset = NULL;
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001841#ifdef FEAT_GUI_MOTIF
1842 /* A font name equal "*" is indicating, that we should activate the font
1843 * selection dialogue to get a new font name. So let us do it here. */
1844 if (font_name != NULL && STRCMP(font_name, "*") == 0)
1845 font_name = gui_xm_select_font(hl_get_font_name());
1846#endif
1847
1848#ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 if (do_fontset)
1850 {
1851 /* If 'guifontset' is set, VIM treats all font specifications as if
1852 * they were fontsets, and 'guifontset' becomes the default. */
1853 if (font_name != NULL)
1854 {
1855 fontset = (XFontSet)gui_mch_get_fontset(font_name, FALSE, TRUE);
1856 if (fontset == NULL)
1857 return FAIL;
1858 }
1859 }
1860 else
1861#endif
1862 {
1863 if (font_name == NULL)
1864 {
1865 /*
1866 * If none of the fonts in 'font' could be loaded, try the one set
1867 * in the X resource, and finally just try using DFLT_FONT, which
1868 * will hopefully always be there.
1869 */
1870 font_name = gui.rsrc_font_name;
1871 font = (XFontStruct *)gui_mch_get_font(font_name, FALSE);
1872 if (font == NULL)
1873 font_name = (char_u *)DFLT_FONT;
1874 }
1875 if (font == NULL)
1876 font = (XFontStruct *)gui_mch_get_font(font_name, FALSE);
1877 if (font == NULL)
1878 return FAIL;
1879 }
1880
1881 gui_mch_free_font(gui.norm_font);
1882#ifdef FEAT_XFONTSET
1883 gui_mch_free_fontset(gui.fontset);
1884
1885 if (fontset != NULL)
1886 {
1887 gui.norm_font = NOFONT;
1888 gui.fontset = (GuiFontset)fontset;
1889 gui.char_width = fontset_width(fontset);
1890 gui.char_height = fontset_height(fontset) + p_linespace;
1891 gui.char_ascent = fontset_ascent(fontset) + p_linespace / 2;
1892 }
1893 else
1894#endif
1895 {
1896 gui.norm_font = (GuiFont)font;
1897#ifdef FEAT_XFONTSET
1898 gui.fontset = NOFONTSET;
1899#endif
1900 gui.char_width = font->max_bounds.width;
1901 gui.char_height = font->ascent + font->descent + p_linespace;
1902 gui.char_ascent = font->ascent + p_linespace / 2;
1903 }
1904
1905 hl_set_font_name(font_name);
1906
1907 /*
1908 * Try to load other fonts for bold, italic, and bold-italic.
1909 * We should also try to work out what font to use for these when they are
1910 * not specified by X resources, but we don't yet.
1911 */
1912 if (font_name == gui.rsrc_font_name)
1913 {
1914 if (gui.bold_font == NOFONT
1915 && gui.rsrc_bold_font_name != NULL
1916 && *gui.rsrc_bold_font_name != NUL)
1917 gui.bold_font = gui_mch_get_font(gui.rsrc_bold_font_name, FALSE);
1918 if (gui.ital_font == NOFONT
1919 && gui.rsrc_ital_font_name != NULL
1920 && *gui.rsrc_ital_font_name != NUL)
1921 gui.ital_font = gui_mch_get_font(gui.rsrc_ital_font_name, FALSE);
1922 if (gui.boldital_font == NOFONT
1923 && gui.rsrc_boldital_font_name != NULL
1924 && *gui.rsrc_boldital_font_name != NUL)
1925 gui.boldital_font = gui_mch_get_font(gui.rsrc_boldital_font_name,
1926 FALSE);
1927 }
1928 else
1929 {
1930 /* When not using the font specified by the resources, also don't use
1931 * the bold/italic fonts, otherwise setting 'guifont' will look very
1932 * strange. */
1933 if (gui.bold_font != NOFONT)
1934 {
1935 XFreeFont(gui.dpy, (XFontStruct *)gui.bold_font);
1936 gui.bold_font = NOFONT;
1937 }
1938 if (gui.ital_font != NOFONT)
1939 {
1940 XFreeFont(gui.dpy, (XFontStruct *)gui.ital_font);
1941 gui.ital_font = NOFONT;
1942 }
1943 if (gui.boldital_font != NOFONT)
1944 {
1945 XFreeFont(gui.dpy, (XFontStruct *)gui.boldital_font);
1946 gui.boldital_font = NOFONT;
1947 }
1948 }
1949
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001950#ifdef FEAT_GUI_MOTIF
1951 gui_motif_synch_fonts();
1952#endif
1953
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 return OK;
1955}
1956
1957/*
1958 * Get a font structure for highlighting.
1959 */
1960 GuiFont
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001961gui_mch_get_font(char_u *name, int giveErrorIfMissing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962{
1963 XFontStruct *font;
1964
Bram Moolenaard23a8232018-02-10 18:45:26 +01001965 if (!gui.in_use || name == NULL) /* can't do this when GUI not running */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 return NOFONT;
1967
1968 font = XLoadQueryFont(gui.dpy, (char *)name);
1969
1970 if (font == NULL)
1971 {
1972 if (giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001973 semsg(_(e_font), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 return NOFONT;
1975 }
1976
1977#ifdef DEBUG
1978 printf("Font Information for '%s':\n", name);
1979 printf(" w = %d, h = %d, ascent = %d, descent = %d\n",
1980 font->max_bounds.width, font->ascent + font->descent,
1981 font->ascent, font->descent);
1982 printf(" max ascent = %d, max descent = %d, max h = %d\n",
1983 font->max_bounds.ascent, font->max_bounds.descent,
1984 font->max_bounds.ascent + font->max_bounds.descent);
1985 printf(" min lbearing = %d, min rbearing = %d\n",
1986 font->min_bounds.lbearing, font->min_bounds.rbearing);
1987 printf(" max lbearing = %d, max rbearing = %d\n",
1988 font->max_bounds.lbearing, font->max_bounds.rbearing);
1989 printf(" leftink = %d, rightink = %d\n",
1990 (font->min_bounds.lbearing < 0),
1991 (font->max_bounds.rbearing > font->max_bounds.width));
1992 printf("\n");
1993#endif
1994
1995 if (font->max_bounds.width != font->min_bounds.width)
1996 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001997 semsg(_(e_fontwidth), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 XFreeFont(gui.dpy, font);
1999 return NOFONT;
2000 }
2001 return (GuiFont)font;
2002}
2003
Bram Moolenaar567e4de2004-12-31 21:01:02 +00002004#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002005/*
2006 * Return the name of font "font" in allocated memory.
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002007 */
2008 char_u *
Bram Moolenaar87748452017-03-12 17:10:33 +01002009gui_mch_get_fontname(GuiFont font, char_u *name)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002010{
Bram Moolenaar87748452017-03-12 17:10:33 +01002011 char_u *ret = NULL;
2012
2013 if (name != NULL && font == NULL)
2014 {
2015 /* In this case, there's no way other than doing this. */
2016 ret = vim_strsave(name);
2017 }
2018 else if (font != NULL)
2019 {
2020 /* In this case, try to retrieve the XLFD corresponding to 'font'->fid;
2021 * if failed, use 'name' unless it's NULL. */
2022 unsigned long value = 0L;
2023
2024 if (XGetFontProperty(font, XA_FONT, &value))
2025 {
2026 char *xa_font_name = NULL;
2027
2028 xa_font_name = XGetAtomName(gui.dpy, value);
2029 if (xa_font_name != NULL)
2030 {
2031 ret = vim_strsave((char_u *)xa_font_name);
2032 XFree(xa_font_name);
2033 }
2034 else if (name != NULL)
2035 ret = vim_strsave(name);
2036 }
2037 else if (name != NULL)
2038 ret = vim_strsave(name);
2039 }
2040 return ret;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002041}
Bram Moolenaar567e4de2004-12-31 21:01:02 +00002042#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002043
Bram Moolenaar231334e2005-07-25 20:46:57 +00002044/*
2045 * Adjust gui.char_height (after 'linespace' was changed).
2046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002048gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049{
2050#ifdef FEAT_XFONTSET
2051 if (gui.fontset != NOFONTSET)
2052 {
2053 gui.char_height = fontset_height((XFontSet)gui.fontset) + p_linespace;
2054 gui.char_ascent = fontset_ascent((XFontSet)gui.fontset)
2055 + p_linespace / 2;
2056 }
2057 else
2058#endif
2059 {
2060 XFontStruct *font = (XFontStruct *)gui.norm_font;
2061
2062 gui.char_height = font->ascent + font->descent + p_linespace;
2063 gui.char_ascent = font->ascent + p_linespace / 2;
2064 }
2065 return OK;
2066}
2067
2068/*
2069 * Set the current text font.
2070 */
2071 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002072gui_mch_set_font(GuiFont font)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073{
2074 static Font prev_font = (Font)-1;
2075 Font fid = ((XFontStruct *)font)->fid;
2076
2077 if (fid != prev_font)
2078 {
2079 XSetFont(gui.dpy, gui.text_gc, fid);
2080 XSetFont(gui.dpy, gui.back_gc, fid);
2081 prev_font = fid;
2082 gui.char_ascent = ((XFontStruct *)font)->ascent + p_linespace / 2;
2083 }
2084#ifdef FEAT_XFONTSET
2085 current_fontset = (XFontSet)NULL;
2086#endif
2087}
2088
2089#if defined(FEAT_XFONTSET) || defined(PROTO)
2090/*
2091 * Set the current text fontset.
2092 * Adjust the ascent, in case it's different.
2093 */
2094 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002095gui_mch_set_fontset(GuiFontset fontset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096{
2097 current_fontset = (XFontSet)fontset;
2098 gui.char_ascent = fontset_ascent(current_fontset) + p_linespace / 2;
2099}
2100#endif
2101
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102/*
2103 * If a font is not going to be used, free its structure.
2104 */
2105 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002106gui_mch_free_font(GuiFont font)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107{
2108 if (font != NOFONT)
2109 XFreeFont(gui.dpy, (XFontStruct *)font);
2110}
2111
2112#if defined(FEAT_XFONTSET) || defined(PROTO)
2113/*
2114 * If a fontset is not going to be used, free its structure.
2115 */
2116 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002117gui_mch_free_fontset(GuiFontset fontset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118{
2119 if (fontset != NOFONTSET)
2120 XFreeFontSet(gui.dpy, (XFontSet)fontset);
2121}
2122
2123/*
2124 * Load the fontset "name".
2125 * Return a reference to the fontset, or NOFONTSET when failing.
2126 */
2127 GuiFontset
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002128gui_mch_get_fontset(
2129 char_u *name,
2130 int giveErrorIfMissing,
2131 int fixed_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132{
2133 XFontSet fontset;
2134 char **missing, *def_str;
2135 int num_missing;
2136
2137 if (!gui.in_use || name == NULL)
2138 return NOFONTSET;
2139
2140 fontset = XCreateFontSet(gui.dpy, (char *)name, &missing, &num_missing,
2141 &def_str);
2142 if (num_missing > 0)
2143 {
2144 int i;
2145
2146 if (giveErrorIfMissing)
2147 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002148 semsg(_("E250: Fonts for the following charsets are missing in fontset %s:"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 for (i = 0; i < num_missing; i++)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002150 semsg("%s", missing[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 }
2152 XFreeStringList(missing);
2153 }
2154
2155 if (fontset == NULL)
2156 {
2157 if (giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002158 semsg(_(e_fontset), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 return NOFONTSET;
2160 }
2161
2162 if (fixed_width && check_fontset_sanity(fontset) == FAIL)
2163 {
2164 XFreeFontSet(gui.dpy, fontset);
2165 return NOFONTSET;
2166 }
2167 return (GuiFontset)fontset;
2168}
2169
2170/*
2171 * Check if fontset "fs" is fixed width.
2172 */
2173 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002174check_fontset_sanity(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175{
2176 XFontStruct **xfs;
2177 char **font_name;
2178 int fn;
2179 char *base_name;
2180 int i;
2181 int min_width;
2182 int min_font_idx = 0;
2183
2184 base_name = XBaseFontNameListOfFontSet(fs);
2185 fn = XFontsOfFontSet(fs, &xfs, &font_name);
2186 for (i = 0; i < fn; i++)
2187 {
2188 if (xfs[i]->max_bounds.width != xfs[i]->min_bounds.width)
2189 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002190 semsg(_("E252: Fontset name: %s"), base_name);
2191 semsg(_("Font '%s' is not fixed-width"), font_name[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 return FAIL;
2193 }
2194 }
2195 /* scan base font width */
2196 min_width = 32767;
2197 for (i = 0; i < fn; i++)
2198 {
2199 if (xfs[i]->max_bounds.width<min_width)
2200 {
2201 min_width = xfs[i]->max_bounds.width;
2202 min_font_idx = i;
2203 }
2204 }
2205 for (i = 0; i < fn; i++)
2206 {
2207 if ( xfs[i]->max_bounds.width != 2 * min_width
2208 && xfs[i]->max_bounds.width != min_width)
2209 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002210 semsg(_("E253: Fontset name: %s"), base_name);
2211 semsg(_("Font0: %s"), font_name[min_font_idx]);
2212 semsg(_("Font1: %s"), font_name[i]);
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01002213 semsg(_("Font%d width is not twice that of font0"), i);
2214 semsg(_("Font0 width: %d"),
2215 (int)xfs[min_font_idx]->max_bounds.width);
2216 semsg(_("Font%d width: %d"), i, (int)xfs[i]->max_bounds.width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 return FAIL;
2218 }
2219 }
2220 /* it seems ok. Good Luck!! */
2221 return OK;
2222}
2223
2224 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002225fontset_width(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226{
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002227 return XmbTextEscapement(fs, "Vim", 3) / 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228}
2229
2230 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002231fontset_height(
2232 XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233{
2234 XFontSetExtents *extents;
2235
2236 extents = XExtentsOfFontSet(fs);
2237 return extents->max_logical_extent.height;
2238}
2239
2240#if (defined(FONTSET_ALWAYS) && defined(FEAT_GUI_ATHENA) \
2241 && defined(FEAT_MENU)) || defined(PROTO)
2242/*
2243 * Returns the bounding box height around the actual glyph image of all
2244 * characters in all fonts of the fontset.
2245 */
2246 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002247fontset_height2(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248{
2249 XFontSetExtents *extents;
2250
2251 extents = XExtentsOfFontSet(fs);
2252 return extents->max_ink_extent.height;
2253}
2254#endif
2255
2256/* NOT USED YET
2257 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002258fontset_descent(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259{
2260 XFontSetExtents *extents;
2261
2262 extents = XExtentsOfFontSet (fs);
2263 return extents->max_logical_extent.height + extents->max_logical_extent.y;
2264}
2265*/
2266
2267 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002268fontset_ascent(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269{
2270 XFontSetExtents *extents;
2271
2272 extents = XExtentsOfFontSet(fs);
2273 return -extents->max_logical_extent.y;
2274}
2275
2276#endif /* FEAT_XFONTSET */
2277
2278/*
2279 * Return the Pixel value (color) for the given color name.
2280 * Return INVALCOLOR for error.
2281 */
2282 guicolor_T
Bram Moolenaar46582282016-07-23 14:35:12 +02002283gui_mch_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284{
Bram Moolenaard23a8232018-02-10 18:45:26 +01002285 guicolor_T requested;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286
2287 /* can't do this when GUI not running */
Bram Moolenaar46582282016-07-23 14:35:12 +02002288 if (!gui.in_use || name == NULL || *name == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 return INVALCOLOR;
2290
Bram Moolenaar46582282016-07-23 14:35:12 +02002291 requested = gui_get_color_cmn(name);
2292 if (requested == INVALCOLOR)
2293 return INVALCOLOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002295 return gui_mch_get_rgb_color(
Bram Moolenaar46582282016-07-23 14:35:12 +02002296 (requested & 0xff0000) >> 16,
2297 (requested & 0xff00) >> 8,
2298 requested & 0xff);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002299}
2300
2301/*
2302 * Return the Pixel value (color) for the given RGB values.
2303 * Return INVALCOLOR for error.
2304 */
2305 guicolor_T
2306gui_mch_get_rgb_color(int r, int g, int b)
2307{
Bram Moolenaard23a8232018-02-10 18:45:26 +01002308 XColor available;
Bram Moolenaard60547b2017-07-24 20:15:30 +02002309 Colormap colormap;
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002310
Bram Moolenaar778df2a2018-05-06 19:19:36 +02002311/* Using XParseColor() is very slow, put rgb in XColor directly.
2312
2313 char spec[8]; // space enough to hold "#RRGGBB"
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002314 vim_snprintf(spec, sizeof(spec), "#%.2x%.2x%.2x", r, g, b);
Bram Moolenaar46582282016-07-23 14:35:12 +02002315 if (XParseColor(gui.dpy, colormap, (char *)spec, &available) != 0
2316 && XAllocColor(gui.dpy, colormap, &available) != 0)
2317 return (guicolor_T)available.pixel;
Bram Moolenaar778df2a2018-05-06 19:19:36 +02002318*/
2319 colormap = DefaultColormap(gui.dpy, DefaultScreen(gui.dpy));
2320 vim_memset(&available, 0, sizeof(XColor));
2321 available.red = r << 8;
2322 available.green = g << 8;
2323 available.blue = b << 8;
2324 if (XAllocColor(gui.dpy, colormap, &available) != 0)
2325 return (guicolor_T)available.pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326
2327 return INVALCOLOR;
2328}
2329
2330/*
Bram Moolenaarfb269802005-03-15 22:46:30 +00002331 * Set the current text foreground color.
2332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002334gui_mch_set_fg_color(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335{
2336 if (color != prev_fg_color)
2337 {
2338 XSetForeground(gui.dpy, gui.text_gc, (Pixel)color);
2339 prev_fg_color = color;
2340 }
2341}
2342
2343/*
2344 * Set the current text background color.
2345 */
2346 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002347gui_mch_set_bg_color(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348{
2349 if (color != prev_bg_color)
2350 {
2351 XSetBackground(gui.dpy, gui.text_gc, (Pixel)color);
2352 prev_bg_color = color;
2353 }
2354}
2355
2356/*
Bram Moolenaarfb269802005-03-15 22:46:30 +00002357 * Set the current text special color.
2358 */
2359 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002360gui_mch_set_sp_color(guicolor_T color)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002361{
2362 prev_sp_color = color;
2363}
2364
2365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 * create a mouse pointer that is blank
2367 */
2368 static Cursor
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002369gui_x11_create_blank_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370{
2371 Pixmap blank_pixmap = XCreatePixmap(gui.dpy, gui.wid, 1, 1, 1);
2372 GC gc = XCreateGC(gui.dpy, blank_pixmap, (unsigned long)0, (XGCValues*)0);
2373 XDrawPoint(gui.dpy, blank_pixmap, gc, 0, 0);
2374 XFreeGC(gui.dpy, gc);
2375 return XCreatePixmapCursor(gui.dpy, blank_pixmap, blank_pixmap,
2376 (XColor*)&gui.norm_pixel, (XColor*)&gui.norm_pixel, 0, 0);
2377}
2378
Bram Moolenaarfb269802005-03-15 22:46:30 +00002379/*
2380 * Draw a curled line at the bottom of the character cell.
2381 */
2382 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002383draw_curl(int row, int col, int cells)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002384{
2385 int i;
2386 int offset;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002387 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarfb269802005-03-15 22:46:30 +00002388
2389 XSetForeground(gui.dpy, gui.text_gc, prev_sp_color);
2390 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
2391 {
2392 offset = val[i % 8];
2393 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, i,
2394 FILL_Y(row + 1) - 1 - offset);
2395 }
2396 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
2397}
2398
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002400gui_mch_draw_string(
2401 int row,
2402 int col,
2403 char_u *s,
2404 int len,
2405 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406{
2407 int cells = len;
2408#ifdef FEAT_MBYTE
Bram Moolenaara3227e22006-03-08 21:32:40 +00002409 static void *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 static int buflen = 0;
2411 char_u *p;
2412 int wlen = 0;
2413 int c;
2414
2415 if (enc_utf8)
2416 {
2417 /* Convert UTF-8 byte sequence to 16 bit characters for the X
2418 * functions. Need a buffer for the 16 bit characters. Keep it
2419 * between calls, because allocating it each time is slow. */
2420 if (buflen < len)
2421 {
2422 XtFree((char *)buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002423 buf = (void *)XtMalloc(len * (sizeof(XChar2b) < sizeof(wchar_t)
2424 ? sizeof(wchar_t) : sizeof(XChar2b)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 buflen = len;
2426 }
2427 p = s;
2428 cells = 0;
2429 while (p < s + len)
2430 {
2431 c = utf_ptr2char(p);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002432# ifdef FEAT_XFONTSET
2433 if (current_fontset != NULL)
2434 {
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002435# ifdef SMALL_WCHAR_T
2436 if (c >= 0x10000)
Bram Moolenaara3227e22006-03-08 21:32:40 +00002437 c = 0xbf; /* show chars > 0xffff as ? */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002438# endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00002439 ((wchar_t *)buf)[wlen] = c;
2440 }
2441 else
2442# endif
2443 {
2444 if (c >= 0x10000)
2445 c = 0xbf; /* show chars > 0xffff as ? */
2446 ((XChar2b *)buf)[wlen].byte1 = (unsigned)c >> 8;
2447 ((XChar2b *)buf)[wlen].byte2 = c;
2448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 ++wlen;
2450 cells += utf_char2cells(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002451 p += utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 }
2453 }
2454 else if (has_mbyte)
2455 {
2456 cells = 0;
2457 for (p = s; p < s + len; )
2458 {
2459 cells += ptr2cells(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002460 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 }
2462 }
2463
2464#endif
2465
2466#ifdef FEAT_XFONTSET
2467 if (current_fontset != NULL)
2468 {
2469 /* Setup a clip rectangle to avoid spilling over in the next or
2470 * previous line. This is apparently needed for some fonts which are
2471 * used in a fontset. */
2472 XRectangle clip;
2473
2474 clip.x = 0;
2475 clip.y = 0;
2476 clip.height = gui.char_height;
2477 clip.width = gui.char_width * cells + 1;
2478 XSetClipRectangles(gui.dpy, gui.text_gc, FILL_X(col), FILL_Y(row),
2479 &clip, 1, Unsorted);
2480 }
2481#endif
2482
2483 if (flags & DRAW_TRANSP)
2484 {
2485#ifdef FEAT_MBYTE
2486 if (enc_utf8)
2487 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2488 TEXT_Y(row), buf, wlen);
2489 else
2490#endif
2491 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2492 TEXT_Y(row), (char *)s, len);
2493 }
2494 else if (p_linespace != 0
2495#ifdef FEAT_XFONTSET
2496 || current_fontset != NULL
2497#endif
2498 )
2499 {
2500 XSetForeground(gui.dpy, gui.text_gc, prev_bg_color);
2501 XFillRectangle(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
2502 FILL_Y(row), gui.char_width * cells, gui.char_height);
2503 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
Bram Moolenaarfb269802005-03-15 22:46:30 +00002504
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505#ifdef FEAT_MBYTE
2506 if (enc_utf8)
2507 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2508 TEXT_Y(row), buf, wlen);
2509 else
2510#endif
2511 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2512 TEXT_Y(row), (char *)s, len);
2513 }
2514 else
2515 {
2516 /* XmbDrawImageString has bug, don't use it for fontset. */
2517#ifdef FEAT_MBYTE
2518 if (enc_utf8)
2519 XDrawImageString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2520 TEXT_Y(row), buf, wlen);
2521 else
2522#endif
2523 XDrawImageString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2524 TEXT_Y(row), (char *)s, len);
2525 }
2526
2527 /* Bold trick: draw the text again with a one-pixel offset. */
2528 if (flags & DRAW_BOLD)
2529 {
2530#ifdef FEAT_MBYTE
2531 if (enc_utf8)
2532 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col) + 1,
2533 TEXT_Y(row), buf, wlen);
2534 else
2535#endif
2536 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col) + 1,
2537 TEXT_Y(row), (char *)s, len);
2538 }
2539
Bram Moolenaarfb269802005-03-15 22:46:30 +00002540 /* Undercurl: draw curl at the bottom of the character cell. */
2541 if (flags & DRAW_UNDERC)
2542 draw_curl(row, col, cells);
2543
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 /* Underline: draw a line at the bottom of the character cell. */
2545 if (flags & DRAW_UNDERL)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002546 {
2547 int y = FILL_Y(row + 1) - 1;
2548
2549 /* When p_linespace is 0, overwrite the bottom row of pixels.
2550 * Otherwise put the line just below the character. */
2551 if (p_linespace > 1)
2552 y -= p_linespace - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 XDrawLine(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
Bram Moolenaarfb269802005-03-15 22:46:30 +00002554 y, FILL_X(col + cells) - 1, y);
2555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002557 if (flags & DRAW_STRIKE)
2558 {
2559 int y = FILL_Y(row + 1) - gui.char_height/2;
2560
2561 XSetForeground(gui.dpy, gui.text_gc, prev_sp_color);
2562 XDrawLine(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
2563 y, FILL_X(col + cells) - 1, y);
2564 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
2565 }
2566
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567#ifdef FEAT_XFONTSET
2568 if (current_fontset != NULL)
2569 XSetClipMask(gui.dpy, gui.text_gc, None);
2570#endif
2571}
2572
2573/*
2574 * Return OK if the key with the termcap name "name" is supported.
2575 */
2576 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002577gui_mch_haskey(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578{
2579 int i;
2580
2581 for (i = 0; special_keys[i].key_sym != (KeySym)0; i++)
2582 if (name[0] == special_keys[i].vim_code0 &&
2583 name[1] == special_keys[i].vim_code1)
2584 return OK;
2585 return FAIL;
2586}
2587
2588/*
2589 * Return the text window-id and display. Only required for X-based GUI's
2590 */
2591 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002592gui_get_x11_windis(Window *win, Display **dis)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593{
2594 *win = XtWindow(vimShell);
2595 *dis = gui.dpy;
2596 return OK;
2597}
2598
2599 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002600gui_mch_beep(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601{
2602 XBell(gui.dpy, 0);
2603}
2604
2605 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002606gui_mch_flash(int msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607{
2608 /* Do a visual beep by reversing the foreground and background colors */
2609 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc, 0, 0,
2610 FILL_X((int)Columns) + gui.border_offset,
2611 FILL_Y((int)Rows) + gui.border_offset);
2612 XSync(gui.dpy, False);
2613 ui_delay((long)msec, TRUE); /* wait for a few msec */
2614 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc, 0, 0,
2615 FILL_X((int)Columns) + gui.border_offset,
2616 FILL_Y((int)Rows) + gui.border_offset);
2617}
2618
2619/*
2620 * Invert a rectangle from row r, column c, for nr rows and nc columns.
2621 */
2622 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002623gui_mch_invert_rectangle(
2624 int r,
2625 int c,
2626 int nr,
2627 int nc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628{
2629 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc,
2630 FILL_X(c), FILL_Y(r), (nc) * gui.char_width, (nr) * gui.char_height);
2631}
2632
2633/*
2634 * Iconify the GUI window.
2635 */
2636 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002637gui_mch_iconify(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638{
2639 XIconifyWindow(gui.dpy, XtWindow(vimShell), DefaultScreen(gui.dpy));
2640}
2641
2642#if defined(FEAT_EVAL) || defined(PROTO)
2643/*
2644 * Bring the Vim window to the foreground.
2645 */
2646 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002647gui_mch_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648{
2649 XMapRaised(gui.dpy, XtWindow(vimShell));
2650}
2651#endif
2652
2653/*
2654 * Draw a cursor without focus.
2655 */
2656 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002657gui_mch_draw_hollow_cursor(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658{
2659 int w = 1;
2660
2661#ifdef FEAT_MBYTE
2662 if (mb_lefthalve(gui.row, gui.col))
2663 w = 2;
2664#endif
2665 gui_mch_set_fg_color(color);
2666 XDrawRectangle(gui.dpy, gui.wid, gui.text_gc, FILL_X(gui.col),
2667 FILL_Y(gui.row), w * gui.char_width - 1, gui.char_height - 1);
2668}
2669
2670/*
2671 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
2672 * color "color".
2673 */
2674 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002675gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676{
2677 gui_mch_set_fg_color(color);
2678
2679 XFillRectangle(gui.dpy, gui.wid, gui.text_gc,
2680#ifdef FEAT_RIGHTLEFT
2681 /* vertical line should be on the right of current point */
2682 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
2683#endif
2684 FILL_X(gui.col),
2685 FILL_Y(gui.row) + gui.char_height - h,
2686 w, h);
2687}
2688
2689/*
2690 * Catch up with any queued X events. This may put keyboard input into the
2691 * input buffer, call resize call-backs, trigger timers etc. If there is
2692 * nothing in the X event queue (& no timers pending), then we return
2693 * immediately.
2694 */
2695 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002696gui_mch_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697{
2698 XtInputMask mask, desired;
2699
2700#ifdef ALT_X_INPUT
2701 if (suppress_alternate_input)
2702 desired = (XtIMXEvent | XtIMTimer);
2703 else
2704#endif
2705 desired = (XtIMAll);
2706 while ((mask = XtAppPending(app_context)) && (mask & desired)
2707 && !vim_is_input_buf_full())
2708 XtAppProcessEvent(app_context, desired);
2709}
2710
2711/*
2712 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2713 * from the keyboard.
2714 * wtime == -1 Wait forever.
2715 * wtime == 0 This should never happen.
2716 * wtime > 0 Wait wtime milliseconds for a character.
2717 * Returns OK if a character was found to be available within the given time,
2718 * or FAIL otherwise.
2719 */
2720 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002721gui_mch_wait_for_chars(long wtime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722{
Bram Moolenaar4231da42016-06-02 14:30:04 +02002723 int focus;
2724 int retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
2726 /*
2727 * Make this static, in case gui_x11_timer_cb is called after leaving
2728 * this function (otherwise a random value on the stack may be changed).
2729 */
2730 static int timed_out;
2731 XtIntervalId timer = (XtIntervalId)0;
2732 XtInputMask desired;
Bram Moolenaar1dccf632017-08-27 17:38:27 +02002733#ifdef FEAT_JOB_CHANNEL
2734 XtIntervalId channel_timer = (XtIntervalId)0;
2735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
2737 timed_out = FALSE;
2738
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 if (wtime > 0)
2740 timer = XtAppAddTimeOut(app_context, (long_u)wtime, gui_x11_timer_cb,
2741 &timed_out);
Bram Moolenaar1dccf632017-08-27 17:38:27 +02002742#ifdef FEAT_JOB_CHANNEL
2743 /* If there is a channel with the keep_open flag we need to poll for input
2744 * on them. */
2745 if (channel_any_keep_open())
2746 channel_timer = XtAppAddTimeOut(app_context, (long_u)20,
2747 channel_poll_cb, (XtPointer)&channel_timer);
2748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749
2750 focus = gui.in_focus;
2751#ifdef ALT_X_INPUT
2752 if (suppress_alternate_input)
2753 desired = (XtIMXEvent | XtIMTimer);
2754 else
2755#endif
2756 desired = (XtIMAll);
2757 while (!timed_out)
2758 {
2759 /* Stop or start blinking when focus changes */
2760 if (gui.in_focus != focus)
2761 {
2762 if (gui.in_focus)
2763 gui_mch_start_blink();
2764 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002765 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 focus = gui.in_focus;
2767 }
2768
Bram Moolenaar93c88e02015-09-15 14:12:05 +02002769#ifdef MESSAGE_QUEUE
Bram Moolenaar4231da42016-06-02 14:30:04 +02002770# ifdef FEAT_TIMERS
2771 did_add_timer = FALSE;
2772# endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02002773 parse_queued_messages();
Bram Moolenaar4231da42016-06-02 14:30:04 +02002774# ifdef FEAT_TIMERS
2775 if (did_add_timer)
2776 /* Need to recompute the waiting time. */
2777 break;
2778# endif
Bram Moolenaar03531f72010-11-16 15:04:57 +01002779#endif
2780
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 /*
2782 * Don't use gui_mch_update() because then we will spin-lock until a
2783 * char arrives, instead we use XtAppProcessEvent() to hang until an
2784 * event arrives. No need to check for input_buf_full because we are
2785 * returning as soon as it contains a single char. Note that
2786 * XtAppNextEvent() may not be used because it will not return after a
2787 * timer event has arrived -- webb
2788 */
2789 XtAppProcessEvent(app_context, desired);
2790
2791 if (input_available())
2792 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002793 retval = OK;
2794 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 }
2796 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002797
2798 if (timer != (XtIntervalId)0 && !timed_out)
2799 XtRemoveTimeOut(timer);
Bram Moolenaar1dccf632017-08-27 17:38:27 +02002800#ifdef FEAT_JOB_CHANNEL
2801 if (channel_timer != (XtIntervalId)0)
2802 XtRemoveTimeOut(channel_timer);
2803#endif
Bram Moolenaar4231da42016-06-02 14:30:04 +02002804
2805 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806}
2807
2808/*
2809 * Output routines.
2810 */
2811
2812/* Flush any output to the screen */
2813 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002814gui_mch_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815{
2816 XFlush(gui.dpy);
2817}
2818
2819/*
2820 * Clear a rectangular region of the screen from text pos (row1, col1) to
2821 * (row2, col2) inclusive.
2822 */
2823 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002824gui_mch_clear_block(
2825 int row1,
2826 int col1,
2827 int row2,
2828 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829{
2830 int x;
2831
2832 x = FILL_X(col1);
2833
2834 /* Clear one extra pixel at the far right, for when bold characters have
2835 * spilled over to the next column. */
2836 XFillRectangle(gui.dpy, gui.wid, gui.back_gc, x, FILL_Y(row1),
2837 (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1),
2838 (row2 - row1 + 1) * gui.char_height);
2839}
2840
2841 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002842gui_mch_clear_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843{
2844 XClearArea(gui.dpy, gui.wid, 0, 0, 0, 0, False);
2845}
2846
2847/*
2848 * Delete the given number of lines from the given row, scrolling up any
2849 * text further down within the scroll region.
2850 */
2851 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002852gui_mch_delete_lines(int row, int num_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853{
2854 if (gui.visibility == VisibilityFullyObscured)
2855 return; /* Can't see the window */
2856
2857 /* copy one extra pixel at the far right, for when bold has spilled
2858 * over */
2859 XCopyArea(gui.dpy, gui.wid, gui.wid, gui.text_gc,
2860 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
2861 gui.char_width * (gui.scroll_region_right - gui.scroll_region_left + 1)
2862 + (gui.scroll_region_right == Columns - 1),
2863 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1),
2864 FILL_X(gui.scroll_region_left), FILL_Y(row));
2865
2866 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
2867 gui.scroll_region_left,
2868 gui.scroll_region_bot, gui.scroll_region_right);
2869 gui_x11_check_copy_area();
2870}
2871
2872/*
2873 * Insert the given number of lines before the given row, scrolling down any
2874 * following text within the scroll region.
2875 */
2876 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002877gui_mch_insert_lines(int row, int num_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878{
2879 if (gui.visibility == VisibilityFullyObscured)
2880 return; /* Can't see the window */
2881
2882 /* copy one extra pixel at the far right, for when bold has spilled
2883 * over */
2884 XCopyArea(gui.dpy, gui.wid, gui.wid, gui.text_gc,
2885 FILL_X(gui.scroll_region_left), FILL_Y(row),
2886 gui.char_width * (gui.scroll_region_right - gui.scroll_region_left + 1)
2887 + (gui.scroll_region_right == Columns - 1),
2888 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1),
2889 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines));
2890
2891 gui_clear_block(row, gui.scroll_region_left,
2892 row + num_lines - 1, gui.scroll_region_right);
2893 gui_x11_check_copy_area();
2894}
2895
2896/*
2897 * Update the region revealed by scrolling up/down.
2898 */
2899 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002900gui_x11_check_copy_area(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901{
2902 XEvent event;
2903 XGraphicsExposeEvent *gevent;
2904
2905 if (gui.visibility != VisibilityPartiallyObscured)
2906 return;
2907
2908 XFlush(gui.dpy);
2909
2910 /* Wait to check whether the scroll worked or not */
2911 for (;;)
2912 {
2913 if (XCheckTypedEvent(gui.dpy, NoExpose, &event))
2914 return; /* The scroll worked. */
2915
2916 if (XCheckTypedEvent(gui.dpy, GraphicsExpose, &event))
2917 {
2918 gevent = (XGraphicsExposeEvent *)&event;
2919 gui_redraw(gevent->x, gevent->y, gevent->width, gevent->height);
2920 if (gevent->count == 0)
2921 return; /* This was the last expose event */
2922 }
2923 XSync(gui.dpy, False);
2924 }
2925}
2926
2927/*
2928 * X Selection stuff, for cutting and pasting text to other windows.
2929 */
2930
2931 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002932clip_mch_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933{
2934 clip_x11_lose_selection(vimShell, cbd);
2935}
2936
2937 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002938clip_mch_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939{
2940 return clip_x11_own_selection(vimShell, cbd);
2941}
2942
2943 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002944clip_mch_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945{
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002946 clip_x11_request_selection(vimShell, gui.dpy, cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947}
2948
2949 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002950clip_mch_set_selection(
2951 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952{
2953 clip_x11_set_selection(cbd);
2954}
2955
2956#if defined(FEAT_MENU) || defined(PROTO)
2957/*
2958 * Menu stuff.
2959 */
2960
2961/*
2962 * Make a menu either grey or not grey.
2963 */
2964 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002965gui_mch_menu_grey(vimmenu_T *menu, int grey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966{
2967 if (menu->id != (Widget)0)
2968 {
2969 gui_mch_menu_hidden(menu, False);
2970 if (grey
2971#ifdef FEAT_GUI_MOTIF
2972 || !menu->sensitive
2973#endif
2974 )
2975 XtSetSensitive(menu->id, False);
2976 else
2977 XtSetSensitive(menu->id, True);
2978 }
2979}
2980
2981/*
2982 * Make menu item hidden or not hidden
2983 */
2984 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002985gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986{
2987 if (menu->id != (Widget)0)
2988 {
2989 if (hidden)
2990 XtUnmanageChild(menu->id);
2991 else
2992 XtManageChild(menu->id);
2993 }
2994}
2995
2996/*
2997 * This is called after setting all the menus to grey/hidden or not.
2998 */
2999 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003000gui_mch_draw_menubar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001{
3002 /* Nothing to do in X */
3003}
3004
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003006gui_x11_menu_cb(
3007 Widget w UNUSED,
3008 XtPointer client_data,
3009 XtPointer call_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010{
3011 gui_menu_cb((vimmenu_T *)client_data);
3012}
3013
3014#endif /* FEAT_MENU */
3015
3016
3017
3018/*
3019 * Function called when window closed. Works like ":qa".
3020 * Should put up a requester!
3021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003023gui_x11_wm_protocol_handler(
3024 Widget w UNUSED,
3025 XtPointer client_data UNUSED,
3026 XEvent *event,
3027 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028{
3029 /*
3030 * Only deal with Client messages.
3031 */
3032 if (event->type != ClientMessage)
3033 return;
3034
3035 /*
3036 * The WM_SAVE_YOURSELF event arrives when the window manager wants to
3037 * exit. That can be cancelled though, thus Vim shouldn't exit here.
3038 * Just sync our swap files.
3039 */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003040 if ((Atom)((XClientMessageEvent *)event)->data.l[0] ==
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 wm_atoms[SAVE_YOURSELF_IDX])
3042 {
3043 out_flush();
3044 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
3045
3046 /* Set the window's WM_COMMAND property, to let the window manager
3047 * know we are done saving ourselves. We don't want to be restarted,
3048 * thus set argv to NULL. */
3049 XSetCommand(gui.dpy, XtWindow(vimShell), NULL, 0);
3050 return;
3051 }
3052
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003053 if ((Atom)((XClientMessageEvent *)event)->data.l[0] !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 wm_atoms[DELETE_WINDOW_IDX])
3055 return;
3056
3057 gui_shell_closed();
3058}
3059
3060#ifdef FEAT_CLIENTSERVER
3061/*
3062 * Function called when property changed. Check for incoming commands
3063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003065gui_x11_send_event_handler(
3066 Widget w UNUSED,
3067 XtPointer client_data UNUSED,
3068 XEvent *event,
3069 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070{
3071 XPropertyEvent *e = (XPropertyEvent *) event;
3072
3073 if (e->type == PropertyNotify && e->window == commWindow
3074 && e->atom == commProperty && e->state == PropertyNewValue)
3075 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003076 serverEventProc(gui.dpy, event, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 }
3078}
3079#endif
3080
3081/*
3082 * Cursor blink functions.
3083 *
3084 * This is a simple state machine:
3085 * BLINK_NONE not blinking at all
3086 * BLINK_OFF blinking, cursor is not shown
3087 * BLINK_ON blinking, cursor is shown
3088 */
3089
3090#define BLINK_NONE 0
3091#define BLINK_OFF 1
3092#define BLINK_ON 2
3093
3094static int blink_state = BLINK_NONE;
3095static long_u blink_waittime = 700;
3096static long_u blink_ontime = 400;
3097static long_u blink_offtime = 250;
3098static XtIntervalId blink_timer = (XtIntervalId)0;
3099
Bram Moolenaar703a8042016-06-04 16:24:32 +02003100 int
3101gui_mch_is_blinking(void)
3102{
3103 return blink_state != BLINK_NONE;
3104}
3105
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +02003106 int
3107gui_mch_is_blink_off(void)
3108{
3109 return blink_state == BLINK_OFF;
3110}
3111
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 void
Bram Moolenaar02fdaea2016-01-30 18:13:55 +01003113gui_mch_set_blinking(long waittime, long on, long off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114{
3115 blink_waittime = waittime;
3116 blink_ontime = on;
3117 blink_offtime = off;
3118}
3119
3120/*
3121 * Stop the cursor blinking. Show the cursor if it wasn't shown.
3122 */
3123 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01003124gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125{
3126 if (blink_timer != (XtIntervalId)0)
3127 {
3128 XtRemoveTimeOut(blink_timer);
3129 blink_timer = (XtIntervalId)0;
3130 }
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01003131 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 gui_update_cursor(TRUE, FALSE);
3133 blink_state = BLINK_NONE;
3134}
3135
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003137gui_x11_blink_cb(
3138 XtPointer timed_out UNUSED,
3139 XtIntervalId *interval_id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140{
3141 if (blink_state == BLINK_ON)
3142 {
3143 gui_undraw_cursor();
3144 blink_state = BLINK_OFF;
3145 blink_timer = XtAppAddTimeOut(app_context, blink_offtime,
3146 gui_x11_blink_cb, NULL);
3147 }
3148 else
3149 {
3150 gui_update_cursor(TRUE, FALSE);
3151 blink_state = BLINK_ON;
3152 blink_timer = XtAppAddTimeOut(app_context, blink_ontime,
3153 gui_x11_blink_cb, NULL);
3154 }
3155}
3156
3157/*
Bram Moolenaar1dccf632017-08-27 17:38:27 +02003158 * Start the cursor blinking. If it was already blinking, this restarts the
3159 * waiting time and shows the cursor.
3160 */
3161 void
3162gui_mch_start_blink(void)
3163{
3164 if (blink_timer != (XtIntervalId)0)
3165 XtRemoveTimeOut(blink_timer);
3166 /* Only switch blinking on if none of the times is zero */
3167 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
3168 {
3169 blink_timer = XtAppAddTimeOut(app_context, blink_waittime,
3170 gui_x11_blink_cb, NULL);
3171 blink_state = BLINK_ON;
3172 gui_update_cursor(TRUE, FALSE);
3173 }
3174}
3175
3176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 * Return the RGB value of a pixel as a long.
3178 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003179 guicolor_T
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003180gui_mch_get_rgb(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181{
3182 XColor xc;
3183 Colormap colormap;
3184
3185 colormap = DefaultColormap(gui.dpy, XDefaultScreen(gui.dpy));
3186 xc.pixel = pixel;
3187 XQueryColor(gui.dpy, colormap, &xc);
3188
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003189 return (guicolor_T)(((xc.red & 0xff00) << 8) + (xc.green & 0xff00)
3190 + ((unsigned)xc.blue >> 8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191}
3192
3193/*
3194 * Add the callback functions.
3195 */
3196 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003197gui_x11_callbacks(Widget textArea, Widget vimForm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198{
3199 XtAddEventHandler(textArea, VisibilityChangeMask, FALSE,
3200 gui_x11_visibility_cb, (XtPointer)0);
3201
3202 XtAddEventHandler(textArea, ExposureMask, FALSE, gui_x11_expose_cb,
3203 (XtPointer)0);
3204
3205 XtAddEventHandler(vimShell, StructureNotifyMask, FALSE,
3206 gui_x11_resize_window_cb, (XtPointer)0);
3207
3208 XtAddEventHandler(vimShell, FocusChangeMask, FALSE, gui_x11_focus_change_cb,
3209 (XtPointer)0);
3210 /*
3211 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3212 * Only needed for some window managers.
3213 */
3214 if (vim_strchr(p_go, GO_POINTER) != NULL)
3215 {
3216 XtAddEventHandler(vimShell, LeaveWindowMask, FALSE, gui_x11_leave_cb,
3217 (XtPointer)0);
3218 XtAddEventHandler(textArea, LeaveWindowMask, FALSE, gui_x11_leave_cb,
3219 (XtPointer)0);
3220 XtAddEventHandler(textArea, EnterWindowMask, FALSE, gui_x11_enter_cb,
3221 (XtPointer)0);
3222 XtAddEventHandler(vimShell, EnterWindowMask, FALSE, gui_x11_enter_cb,
3223 (XtPointer)0);
3224 }
3225
3226 XtAddEventHandler(vimForm, KeyPressMask, FALSE, gui_x11_key_hit_cb,
3227 (XtPointer)0);
3228 XtAddEventHandler(textArea, KeyPressMask, FALSE, gui_x11_key_hit_cb,
3229 (XtPointer)0);
3230
3231 /* get pointer moved events from scrollbar, needed for 'mousefocus' */
3232 XtAddEventHandler(vimForm, PointerMotionMask,
3233 FALSE, gui_x11_mouse_cb, (XtPointer)1);
3234 XtAddEventHandler(textArea, ButtonPressMask | ButtonReleaseMask |
3235 ButtonMotionMask | PointerMotionMask,
3236 FALSE, gui_x11_mouse_cb, (XtPointer)0);
3237}
3238
3239/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003240 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003242 void
3243gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244{
3245 int rootx, rooty, winx, winy;
3246 Window root, child;
3247 unsigned int mask;
3248
3249 if (gui.wid && XQueryPointer(gui.dpy, gui.wid, &root, &child,
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003250 &rootx, &rooty, &winx, &winy, &mask)) {
3251 *x = winx;
3252 *y = winy;
3253 } else {
3254 *x = -1;
3255 *y = -1;
3256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257}
3258
3259 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003260gui_mch_setmouse(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261{
3262 if (gui.wid)
3263 XWarpPointer(gui.dpy, (Window)0, gui.wid, 0, 0, 0, 0, x, y);
3264}
3265
3266#if (defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)) || defined(PROTO)
3267 XButtonPressedEvent *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003268gui_x11_get_last_mouse_event(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269{
3270 return &last_mouse_event;
3271}
3272#endif
3273
3274#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
3275
3276/* Signs are currently always 2 chars wide. Hopefully the font is big enough
3277 * to provide room for the bitmap! */
3278# define SIGN_WIDTH (gui.char_width * 2)
3279
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003281gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282{
3283 XImage *sign;
3284
3285 if (gui.in_use && (sign = (XImage *)sign_get_image(typenr)) != NULL)
3286 {
3287 XClearArea(gui.dpy, gui.wid, TEXT_X(col), TEXT_Y(row) - sign->height,
3288 SIGN_WIDTH, gui.char_height, FALSE);
3289 XPutImage(gui.dpy, gui.wid, gui.text_gc, sign, 0, 0,
3290 TEXT_X(col) + (SIGN_WIDTH - sign->width) / 2,
3291 TEXT_Y(row) - sign->height,
3292 sign->width, sign->height);
3293 }
3294}
3295
3296 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003297gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298{
3299 XpmAttributes attrs;
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003300 XImage *sign = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 int status;
3302
3303 /*
3304 * Setup the color substitution table.
3305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 if (signfile[0] != NUL && signfile[0] != '-')
3307 {
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003308 XpmColorSymbol color[5] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 {
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003310 {"none", NULL, 0},
3311 {"iconColor1", NULL, 0},
3312 {"bottomShadowColor", NULL, 0},
3313 {"topShadowColor", NULL, 0},
3314 {"selectColor", NULL, 0}
3315 };
3316 attrs.valuemask = XpmColorSymbols;
3317 attrs.numsymbols = 2;
3318 attrs.colorsymbols = color;
3319 attrs.colorsymbols[0].pixel = gui.back_pixel;
3320 attrs.colorsymbols[1].pixel = gui.norm_pixel;
3321 status = XpmReadFileToImage(gui.dpy, (char *)signfile,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 &sign, NULL, &attrs);
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003323 if (status == 0)
3324 {
3325 /* Sign width is fixed at two columns now.
3326 if (sign->width > gui.sign_width)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003327 gui.sign_width = sign->width + 8; */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 }
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003329 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003330 emsg(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 }
3332
3333 return (void *)sign;
3334}
3335
3336 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003337gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338{
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003339 XDestroyImage((XImage*)sign);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340}
3341#endif
3342
3343
3344#ifdef FEAT_MOUSESHAPE
3345/* The last set mouse pointer shape is remembered, to be used when it goes
3346 * from hidden to not hidden. */
3347static int last_shape = 0;
3348#endif
3349
3350/*
3351 * Use the blank mouse pointer or not.
3352 */
3353 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003354gui_mch_mousehide(
3355 int hide) /* TRUE = use blank ptr, FALSE = use parent ptr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356{
3357 if (gui.pointer_hidden != hide)
3358 {
3359 gui.pointer_hidden = hide;
3360 if (hide)
3361 XDefineCursor(gui.dpy, gui.wid, gui.blank_pointer);
3362 else
3363#ifdef FEAT_MOUSESHAPE
3364 mch_set_mouse_shape(last_shape);
3365#else
3366 XUndefineCursor(gui.dpy, gui.wid);
3367#endif
3368 }
3369}
3370
3371#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3372
3373/* Table for shape IDs. Keep in sync with the mshape_names[] table in
3374 * misc2.c! */
3375static int mshape_ids[] =
3376{
3377 XC_left_ptr, /* arrow */
3378 0, /* blank */
3379 XC_xterm, /* beam */
3380 XC_sb_v_double_arrow, /* updown */
3381 XC_sizing, /* udsizing */
3382 XC_sb_h_double_arrow, /* leftright */
3383 XC_sizing, /* lrsizing */
3384 XC_watch, /* busy */
3385 XC_X_cursor, /* no */
3386 XC_crosshair, /* crosshair */
3387 XC_hand1, /* hand1 */
3388 XC_hand2, /* hand2 */
3389 XC_pencil, /* pencil */
3390 XC_question_arrow, /* question */
3391 XC_right_ptr, /* right-arrow */
3392 XC_center_ptr, /* up-arrow */
3393 XC_left_ptr /* last one */
3394};
3395
3396 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003397mch_set_mouse_shape(int shape)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398{
3399 int id;
3400
3401 if (!gui.in_use)
3402 return;
3403
3404 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
3405 XDefineCursor(gui.dpy, gui.wid, gui.blank_pointer);
3406 else
3407 {
3408 if (shape >= MSHAPE_NUMBERED)
3409 {
3410 id = shape - MSHAPE_NUMBERED;
3411 if (id >= XC_num_glyphs)
3412 id = XC_left_ptr;
3413 else
3414 id &= ~1; /* they are always even (why?) */
3415 }
3416 else
3417 id = mshape_ids[shape];
3418
3419 XDefineCursor(gui.dpy, gui.wid, XCreateFontCursor(gui.dpy, id));
3420 }
3421 if (shape != MSHAPE_HIDE)
3422 last_shape = shape;
3423}
3424#endif
3425
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003426#if (defined(FEAT_TOOLBAR) && defined(FEAT_BEVAL_GUI)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427/*
3428 * Set the balloon-eval used for the tooltip of a toolbar menu item.
3429 * The check for a non-toolbar item was added, because there is a crash when
3430 * passing a normal menu item here. Can't explain that, but better avoid it.
3431 */
3432 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003433gui_mch_menu_set_tip(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434{
3435 if (menu->id != NULL && menu->parent != NULL
3436 && menu_is_toolbar(menu->parent->name))
3437 {
3438 /* Always destroy and create the balloon, in case the string was
3439 * changed. */
3440 if (menu->tip != NULL)
3441 {
3442 gui_mch_destroy_beval_area(menu->tip);
3443 menu->tip = NULL;
3444 }
3445 if (menu->strings[MENU_INDEX_TIP] != NULL)
3446 menu->tip = gui_mch_create_beval_area(
3447 menu->id,
3448 menu->strings[MENU_INDEX_TIP],
3449 NULL,
3450 NULL);
3451 }
3452}
3453#endif