blob: 1c55805d3b58d4a0e1ec0693f9aa9b3cad0aec53 [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/*
Bram Moolenaarbb1969b2019-01-17 15:45:25 +010025 * XpmP.h is preferred, because it makes the signs drawn with a transparent
26 * background instead of black.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027 */
28#if defined(HAVE_XM_XPMP_H) && defined(FEAT_GUI_MOTIF) \
Bram Moolenaarbb1969b2019-01-17 15:45:25 +010029 && !defined(HAVE_X11_XPM_H)
Bram Moolenaar071d4272004-06-13 20:20:40 +000030# include <Xm/XpmP.h>
31#else
32# ifdef HAVE_X11_XPM_H
Bram Moolenaar88c86eb2019-01-17 17:13:30 +010033# ifdef VMS
34# include <xpm.h>
35# else
36# include <X11/xpm.h>
37# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000038# endif
39#endif
40
41#ifdef FEAT_XFONTSET
42# ifdef X_LOCALE
43# include <X11/Xlocale.h>
44# else
45# include <locale.h>
46# endif
47#endif
48
49#ifdef HAVE_X11_SUNKEYSYM_H
50# include <X11/Sunkeysym.h>
51#endif
52
53#ifdef HAVE_X11_XMU_EDITRES_H
54# include <X11/Xmu/Editres.h>
55#endif
56
Bram Moolenaar071d4272004-06-13 20:20:40 +000057#define VIM_NAME "vim"
58#define VIM_CLASS "Vim"
59
60/* Default resource values */
61#define DFLT_FONT "7x13"
62#ifdef FONTSET_ALWAYS
63# define DFLT_MENU_FONT XtDefaultFontSet
64#else
65# define DFLT_MENU_FONT XtDefaultFont
66#endif
67#define DFLT_TOOLTIP_FONT XtDefaultFontSet
68
69#ifdef FEAT_GUI_ATHENA
70# define DFLT_MENU_BG_COLOR "gray77"
71# define DFLT_MENU_FG_COLOR "black"
72# define DFLT_SCROLL_BG_COLOR "gray60"
73# define DFLT_SCROLL_FG_COLOR "gray77"
Bram Moolenaar46582282016-07-23 14:35:12 +020074# define DFLT_TOOLTIP_BG_COLOR "#ffff91"
75# define DFLT_TOOLTIP_FG_COLOR "#000000"
Bram Moolenaar071d4272004-06-13 20:20:40 +000076#else
77/* use the default (CDE) colors */
78# define DFLT_MENU_BG_COLOR ""
79# define DFLT_MENU_FG_COLOR ""
80# define DFLT_SCROLL_BG_COLOR ""
81# define DFLT_SCROLL_FG_COLOR ""
Bram Moolenaar46582282016-07-23 14:35:12 +020082# define DFLT_TOOLTIP_BG_COLOR "#ffff91"
83# define DFLT_TOOLTIP_FG_COLOR "#000000"
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#endif
85
86Widget vimShell = (Widget)0;
87
88static Atom wm_atoms[2]; /* Window Manager Atoms */
89#define DELETE_WINDOW_IDX 0 /* index in wm_atoms[] for WM_DELETE_WINDOW */
90#define SAVE_YOURSELF_IDX 1 /* index in wm_atoms[] for WM_SAVE_YOURSELF */
91
92#ifdef FEAT_XFONTSET
93/*
94 * We either draw with a fontset (when current_fontset != NULL) or with a
95 * normal font (current_fontset == NULL, use gui.text_gc and gui.back_gc).
96 */
97static XFontSet current_fontset = NULL;
98
99#define XDrawString(dpy, win, gc, x, y, str, n) \
100 do \
101 { \
102 if (current_fontset != NULL) \
103 XmbDrawString(dpy, win, current_fontset, gc, x, y, str, n); \
104 else \
105 XDrawString(dpy, win, gc, x, y, str, n); \
106 } while (0)
107
108#define XDrawString16(dpy, win, gc, x, y, str, n) \
109 do \
110 { \
111 if (current_fontset != NULL) \
112 XwcDrawString(dpy, win, current_fontset, gc, x, y, (wchar_t *)str, n); \
113 else \
Bram Moolenaara3227e22006-03-08 21:32:40 +0000114 XDrawString16(dpy, win, gc, x, y, (XChar2b *)str, n); \
115 } while (0)
116
117#define XDrawImageString16(dpy, win, gc, x, y, str, n) \
118 do \
119 { \
120 if (current_fontset != NULL) \
121 XwcDrawImageString(dpy, win, current_fontset, gc, x, y, (wchar_t *)str, n); \
122 else \
123 XDrawImageString16(dpy, win, gc, x, y, (XChar2b *)str, n); \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 } while (0)
125
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100126static int check_fontset_sanity(XFontSet fs);
127static int fontset_width(XFontSet fs);
128static int fontset_ascent(XFontSet fs);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#endif
130
131static guicolor_T prev_fg_color = INVALCOLOR;
132static guicolor_T prev_bg_color = INVALCOLOR;
Bram Moolenaarfb269802005-03-15 22:46:30 +0000133static guicolor_T prev_sp_color = INVALCOLOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
135#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
136static XButtonPressedEvent last_mouse_event;
137#endif
138
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100139static void gui_x11_check_copy_area(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#ifdef FEAT_CLIENTSERVER
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100141static void gui_x11_send_event_handler(Widget, XtPointer, XEvent *, Boolean *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100143static void gui_x11_wm_protocol_handler(Widget, XtPointer, XEvent *, Boolean *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100144static Cursor gui_x11_create_blank_mouse(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
146
147/*
148 * Keycodes recognized by vim.
149 * NOTE: when changing this, the table in gui_gtk_x11.c probably needs the
150 * same change!
151 */
152static struct specialkey
153{
154 KeySym key_sym;
155 char_u vim_code0;
156 char_u vim_code1;
157} special_keys[] =
158{
159 {XK_Up, 'k', 'u'},
160 {XK_Down, 'k', 'd'},
161 {XK_Left, 'k', 'l'},
162 {XK_Right, 'k', 'r'},
163
164 {XK_F1, 'k', '1'},
165 {XK_F2, 'k', '2'},
166 {XK_F3, 'k', '3'},
167 {XK_F4, 'k', '4'},
168 {XK_F5, 'k', '5'},
169 {XK_F6, 'k', '6'},
170 {XK_F7, 'k', '7'},
171 {XK_F8, 'k', '8'},
172 {XK_F9, 'k', '9'},
173 {XK_F10, 'k', ';'},
174
175 {XK_F11, 'F', '1'},
176 {XK_F12, 'F', '2'},
177 {XK_F13, 'F', '3'},
178 {XK_F14, 'F', '4'},
179 {XK_F15, 'F', '5'},
180 {XK_F16, 'F', '6'},
181 {XK_F17, 'F', '7'},
182 {XK_F18, 'F', '8'},
183 {XK_F19, 'F', '9'},
184 {XK_F20, 'F', 'A'},
185
186 {XK_F21, 'F', 'B'},
187 {XK_F22, 'F', 'C'},
188 {XK_F23, 'F', 'D'},
189 {XK_F24, 'F', 'E'},
190 {XK_F25, 'F', 'F'},
191 {XK_F26, 'F', 'G'},
192 {XK_F27, 'F', 'H'},
193 {XK_F28, 'F', 'I'},
194 {XK_F29, 'F', 'J'},
195 {XK_F30, 'F', 'K'},
196
197 {XK_F31, 'F', 'L'},
198 {XK_F32, 'F', 'M'},
199 {XK_F33, 'F', 'N'},
200 {XK_F34, 'F', 'O'},
201 {XK_F35, 'F', 'P'}, /* keysymdef.h defines up to F35 */
202#ifdef SunXK_F36
203 {SunXK_F36, 'F', 'Q'},
204 {SunXK_F37, 'F', 'R'},
205#endif
206
207 {XK_Help, '%', '1'},
208 {XK_Undo, '&', '8'},
209 {XK_BackSpace, 'k', 'b'},
210 {XK_Insert, 'k', 'I'},
211 {XK_Delete, 'k', 'D'},
212 {XK_Home, 'k', 'h'},
213 {XK_End, '@', '7'},
214 {XK_Prior, 'k', 'P'},
215 {XK_Next, 'k', 'N'},
216 {XK_Print, '%', '9'},
217
218 /* Keypad keys: */
219#ifdef XK_KP_Left
220 {XK_KP_Left, 'k', 'l'},
221 {XK_KP_Right, 'k', 'r'},
222 {XK_KP_Up, 'k', 'u'},
223 {XK_KP_Down, 'k', 'd'},
224 {XK_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
225 {XK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
226 {XK_KP_Home, 'K', '1'},
227 {XK_KP_End, 'K', '4'},
228 {XK_KP_Prior, 'K', '3'},
229 {XK_KP_Next, 'K', '5'},
230
231 {XK_KP_Add, 'K', '6'},
232 {XK_KP_Subtract, 'K', '7'},
233 {XK_KP_Divide, 'K', '8'},
234 {XK_KP_Multiply, 'K', '9'},
235 {XK_KP_Enter, 'K', 'A'},
236 {XK_KP_Decimal, 'K', 'B'},
237
238 {XK_KP_0, 'K', 'C'},
239 {XK_KP_1, 'K', 'D'},
240 {XK_KP_2, 'K', 'E'},
241 {XK_KP_3, 'K', 'F'},
242 {XK_KP_4, 'K', 'G'},
243 {XK_KP_5, 'K', 'H'},
244 {XK_KP_6, 'K', 'I'},
245 {XK_KP_7, 'K', 'J'},
246 {XK_KP_8, 'K', 'K'},
247 {XK_KP_9, 'K', 'L'},
248#endif
249
250 /* End of list marker: */
251 {(KeySym)0, 0, 0}
252};
253
254#define XtNboldFont "boldFont"
255#define XtCBoldFont "BoldFont"
256#define XtNitalicFont "italicFont"
257#define XtCItalicFont "ItalicFont"
258#define XtNboldItalicFont "boldItalicFont"
259#define XtCBoldItalicFont "BoldItalicFont"
260#define XtNscrollbarWidth "scrollbarWidth"
261#define XtCScrollbarWidth "ScrollbarWidth"
262#define XtNmenuHeight "menuHeight"
263#define XtCMenuHeight "MenuHeight"
264#define XtNmenuFont "menuFont"
265#define XtCMenuFont "MenuFont"
266#define XtNmenuFontSet "menuFontSet"
267#define XtCMenuFontSet "MenuFontSet"
268
269
270/* Resources for setting the foreground and background colors of menus */
271#define XtNmenuBackground "menuBackground"
272#define XtCMenuBackground "MenuBackground"
273#define XtNmenuForeground "menuForeground"
274#define XtCMenuForeground "MenuForeground"
275
276/* Resources for setting the foreground and background colors of scrollbars */
277#define XtNscrollBackground "scrollBackground"
278#define XtCScrollBackground "ScrollBackground"
279#define XtNscrollForeground "scrollForeground"
280#define XtCScrollForeground "ScrollForeground"
281
282/* Resources for setting the foreground and background colors of tooltip */
283#define XtNtooltipBackground "tooltipBackground"
284#define XtCTooltipBackground "TooltipBackground"
285#define XtNtooltipForeground "tooltipForeground"
286#define XtCTooltipForeground "TooltipForeground"
287#define XtNtooltipFont "tooltipFont"
288#define XtCTooltipFont "TooltipFont"
289
290/*
291 * X Resources:
292 */
293static XtResource vim_resources[] =
294{
295 {
296 XtNforeground,
297 XtCForeground,
298 XtRPixel,
299 sizeof(Pixel),
300 XtOffsetOf(gui_T, def_norm_pixel),
301 XtRString,
302 XtDefaultForeground
303 },
304 {
305 XtNbackground,
306 XtCBackground,
307 XtRPixel,
308 sizeof(Pixel),
309 XtOffsetOf(gui_T, def_back_pixel),
310 XtRString,
311 XtDefaultBackground
312 },
313 {
314 XtNfont,
315 XtCFont,
316 XtRString,
317 sizeof(String *),
318 XtOffsetOf(gui_T, rsrc_font_name),
319 XtRImmediate,
320 XtDefaultFont
321 },
322 {
323 XtNboldFont,
324 XtCBoldFont,
325 XtRString,
326 sizeof(String *),
327 XtOffsetOf(gui_T, rsrc_bold_font_name),
328 XtRImmediate,
329 ""
330 },
331 {
332 XtNitalicFont,
333 XtCItalicFont,
334 XtRString,
335 sizeof(String *),
336 XtOffsetOf(gui_T, rsrc_ital_font_name),
337 XtRImmediate,
338 ""
339 },
340 {
341 XtNboldItalicFont,
342 XtCBoldItalicFont,
343 XtRString,
344 sizeof(String *),
345 XtOffsetOf(gui_T, rsrc_boldital_font_name),
346 XtRImmediate,
347 ""
348 },
349 {
350 XtNgeometry,
351 XtCGeometry,
352 XtRString,
353 sizeof(String *),
354 XtOffsetOf(gui_T, geom),
355 XtRImmediate,
356 ""
357 },
358 {
359 XtNreverseVideo,
360 XtCReverseVideo,
361 XtRBool,
362 sizeof(Bool),
363 XtOffsetOf(gui_T, rsrc_rev_video),
364 XtRImmediate,
365 (XtPointer)False
366 },
367 {
368 XtNborderWidth,
369 XtCBorderWidth,
370 XtRInt,
371 sizeof(int),
372 XtOffsetOf(gui_T, border_width),
373 XtRImmediate,
374 (XtPointer)2
375 },
376 {
377 XtNscrollbarWidth,
378 XtCScrollbarWidth,
379 XtRInt,
380 sizeof(int),
381 XtOffsetOf(gui_T, scrollbar_width),
382 XtRImmediate,
383 (XtPointer)SB_DEFAULT_WIDTH
384 },
385#ifdef FEAT_MENU
386# ifdef FEAT_GUI_ATHENA /* with Motif the height is always computed */
387 {
388 XtNmenuHeight,
389 XtCMenuHeight,
390 XtRInt,
391 sizeof(int),
392 XtOffsetOf(gui_T, menu_height),
393 XtRImmediate,
394 (XtPointer)MENU_DEFAULT_HEIGHT /* Should figure out at run time */
395 },
396# endif
397 {
398# ifdef FONTSET_ALWAYS
399 XtNmenuFontSet,
400 XtCMenuFontSet,
401#else
402 XtNmenuFont,
403 XtCMenuFont,
404#endif
405 XtRString,
406 sizeof(char *),
407 XtOffsetOf(gui_T, rsrc_menu_font_name),
408 XtRString,
409 DFLT_MENU_FONT
410 },
411#endif
412 {
413 XtNmenuForeground,
414 XtCMenuForeground,
415 XtRString,
416 sizeof(char *),
417 XtOffsetOf(gui_T, rsrc_menu_fg_name),
418 XtRString,
419 DFLT_MENU_FG_COLOR
420 },
421 {
422 XtNmenuBackground,
423 XtCMenuBackground,
424 XtRString,
425 sizeof(char *),
426 XtOffsetOf(gui_T, rsrc_menu_bg_name),
427 XtRString,
428 DFLT_MENU_BG_COLOR
429 },
430 {
431 XtNscrollForeground,
432 XtCScrollForeground,
433 XtRString,
434 sizeof(char *),
435 XtOffsetOf(gui_T, rsrc_scroll_fg_name),
436 XtRString,
437 DFLT_SCROLL_FG_COLOR
438 },
439 {
440 XtNscrollBackground,
441 XtCScrollBackground,
442 XtRString,
443 sizeof(char *),
444 XtOffsetOf(gui_T, rsrc_scroll_bg_name),
445 XtRString,
446 DFLT_SCROLL_BG_COLOR
447 },
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100448#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 {
450 XtNtooltipForeground,
451 XtCTooltipForeground,
452 XtRString,
453 sizeof(char *),
454 XtOffsetOf(gui_T, rsrc_tooltip_fg_name),
455 XtRString,
456 DFLT_TOOLTIP_FG_COLOR
457 },
458 {
459 XtNtooltipBackground,
460 XtCTooltipBackground,
461 XtRString,
462 sizeof(char *),
463 XtOffsetOf(gui_T, rsrc_tooltip_bg_name),
464 XtRString,
465 DFLT_TOOLTIP_BG_COLOR
466 },
467 {
468 XtNtooltipFont,
469 XtCTooltipFont,
470 XtRString,
471 sizeof(char *),
472 XtOffsetOf(gui_T, rsrc_tooltip_font_name),
473 XtRString,
474 DFLT_TOOLTIP_FONT
475 },
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100476 /* This one may not be really needed? */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 {
478 "balloonEvalFontSet",
479 XtCFontSet,
480 XtRFontSet,
481 sizeof(XFontSet),
482 XtOffsetOf(gui_T, tooltip_fontset),
483 XtRImmediate,
484 (XtPointer)NOFONTSET
485 },
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100486#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487#ifdef FEAT_XIM
488 {
489 "preeditType",
490 "PreeditType",
491 XtRString,
492 sizeof(char*),
493 XtOffsetOf(gui_T, rsrc_preedit_type_name),
494 XtRString,
495 (XtPointer)"OverTheSpot,OffTheSpot,Root"
496 },
497 {
498 "inputMethod",
499 "InputMethod",
500 XtRString,
501 sizeof(char*),
502 XtOffsetOf(gui_T, rsrc_input_method),
503 XtRString,
504 NULL
505 },
506#endif /* FEAT_XIM */
507};
508
509/*
510 * This table holds all the X GUI command line options allowed. This includes
511 * the standard ones so that we can skip them when vim is started without the
512 * GUI (but the GUI might start up later).
513 * When changing this, also update doc/vim_gui.txt and the usage message!!!
514 */
515static XrmOptionDescRec cmdline_options[] =
516{
517 /* We handle these options ourselves */
518 {"-bg", ".background", XrmoptionSepArg, NULL},
519 {"-background", ".background", XrmoptionSepArg, NULL},
520 {"-fg", ".foreground", XrmoptionSepArg, NULL},
521 {"-foreground", ".foreground", XrmoptionSepArg, NULL},
522 {"-fn", ".font", XrmoptionSepArg, NULL},
523 {"-font", ".font", XrmoptionSepArg, NULL},
524 {"-boldfont", ".boldFont", XrmoptionSepArg, NULL},
525 {"-italicfont", ".italicFont", XrmoptionSepArg, NULL},
526 {"-geom", ".geometry", XrmoptionSepArg, NULL},
527 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
528 {"-reverse", "*reverseVideo", XrmoptionNoArg, "True"},
529 {"-rv", "*reverseVideo", XrmoptionNoArg, "True"},
530 {"+reverse", "*reverseVideo", XrmoptionNoArg, "False"},
531 {"+rv", "*reverseVideo", XrmoptionNoArg, "False"},
532 {"-display", ".display", XrmoptionSepArg, NULL},
Bram Moolenaara5792f52005-11-23 21:25:05 +0000533 {"-iconic", ".iconic", XrmoptionNoArg, "True"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 {"-name", ".name", XrmoptionSepArg, NULL},
535 {"-bw", ".borderWidth", XrmoptionSepArg, NULL},
536 {"-borderwidth", ".borderWidth", XrmoptionSepArg, NULL},
537 {"-sw", ".scrollbarWidth", XrmoptionSepArg, NULL},
538 {"-scrollbarwidth", ".scrollbarWidth", XrmoptionSepArg, NULL},
539 {"-mh", ".menuHeight", XrmoptionSepArg, NULL},
540 {"-menuheight", ".menuHeight", XrmoptionSepArg, NULL},
541#ifdef FONTSET_ALWAYS
542 {"-mf", ".menuFontSet", XrmoptionSepArg, NULL},
543 {"-menufont", ".menuFontSet", XrmoptionSepArg, NULL},
544 {"-menufontset", ".menuFontSet", XrmoptionSepArg, NULL},
545#else
546 {"-mf", ".menuFont", XrmoptionSepArg, NULL},
547 {"-menufont", ".menuFont", XrmoptionSepArg, NULL},
548#endif
549 {"-xrm", NULL, XrmoptionResArg, NULL}
550};
551
552static int gui_argc = 0;
553static char **gui_argv = NULL;
554
555/*
556 * Call-back routines.
557 */
558
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100560gui_x11_timer_cb(
561 XtPointer timed_out,
562 XtIntervalId *interval_id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563{
564 *((int *)timed_out) = TRUE;
565}
566
Bram Moolenaar1dccf632017-08-27 17:38:27 +0200567#ifdef FEAT_JOB_CHANNEL
568 static void
569channel_poll_cb(
570 XtPointer client_data,
571 XtIntervalId *interval_id UNUSED)
572{
573 XtIntervalId *channel_timer = (XtIntervalId *)client_data;
574
575 /* Using an event handler for a channel that may be disconnected does
576 * not work, it hangs. Instead poll for messages. */
577 channel_handle_events(TRUE);
578 parse_queued_messages();
579
580 /* repeat */
581 *channel_timer = XtAppAddTimeOut(app_context, (long_u)20,
582 channel_poll_cb, client_data);
583}
584#endif
585
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100587gui_x11_visibility_cb(
588 Widget w UNUSED,
589 XtPointer dud UNUSED,
590 XEvent *event,
591 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592{
593 if (event->type != VisibilityNotify)
594 return;
595
596 gui.visibility = event->xvisibility.state;
597
598 /*
599 * When we do an XCopyArea(), and the window is partially obscured, we want
600 * to receive an event to tell us whether it worked or not.
601 */
602 XSetGraphicsExposures(gui.dpy, gui.text_gc,
603 gui.visibility != VisibilityUnobscured);
604
605 /* This is needed for when redrawing is slow. */
606 gui_mch_update();
607}
608
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100610gui_x11_expose_cb(
611 Widget w UNUSED,
612 XtPointer dud UNUSED,
613 XEvent *event,
614 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615{
616 XExposeEvent *gevent;
617 int new_x;
618
619 if (event->type != Expose)
620 return;
621
622 out_flush(); /* make sure all output has been processed */
623
624 gevent = (XExposeEvent *)event;
625 gui_redraw(gevent->x, gevent->y, gevent->width, gevent->height);
626
627 new_x = FILL_X(0);
628
629 /* Clear the border areas if needed */
630 if (gevent->x < new_x)
631 XClearArea(gui.dpy, gui.wid, 0, 0, new_x, 0, False);
632 if (gevent->y < FILL_Y(0))
633 XClearArea(gui.dpy, gui.wid, 0, 0, 0, FILL_Y(0), False);
634 if (gevent->x > FILL_X(Columns))
635 XClearArea(gui.dpy, gui.wid, FILL_X((int)Columns), 0, 0, 0, False);
636 if (gevent->y > FILL_Y(Rows))
637 XClearArea(gui.dpy, gui.wid, 0, FILL_Y((int)Rows), 0, 0, False);
638
639 /* This is needed for when redrawing is slow. */
640 gui_mch_update();
641}
642
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100643#if (defined(FEAT_NETBEANS_INTG) && defined(FEAT_GUI_MOTIF)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644/*
Bram Moolenaar231334e2005-07-25 20:46:57 +0000645 * This function fills in the XRectangle object with the current x,y
646 * coordinates and height, width so that an XtVaSetValues to the same shell of
Bram Moolenaar49325942007-05-10 19:19:59 +0000647 * those resources will restore the window to its former position and
Bram Moolenaar231334e2005-07-25 20:46:57 +0000648 * dimensions.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 *
Bram Moolenaar231334e2005-07-25 20:46:57 +0000650 * Note: This function may fail, in which case the XRectangle will be
651 * unchanged. Be sure to have the XRectangle set with the proper values for a
652 * failed condition prior to calling this function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 */
654 static void
655shellRectangle(Widget shell, XRectangle *r)
656{
657 Window rootw, shellw, child, parentw;
658 int absx, absy;
659 XWindowAttributes a;
660 Window *children;
661 unsigned int childrenCount;
662
663 shellw = XtWindow(shell);
664 if (shellw == 0)
665 return;
666 for (;;)
667 {
668 XQueryTree(XtDisplay(shell), shellw, &rootw, &parentw,
669 &children, &childrenCount);
670 XFree(children);
671 if (parentw == rootw)
672 break;
673 shellw = parentw;
674 }
675 XGetWindowAttributes(XtDisplay(shell), shellw, &a);
676 XTranslateCoordinates(XtDisplay(shell), shellw, a.root, 0, 0,
677 &absx, &absy, &child);
678 r->x = absx;
679 r->y = absy;
680 XtVaGetValues(shell, XmNheight, &r->height, XmNwidth, &r->width, NULL);
681}
682#endif
683
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100685gui_x11_resize_window_cb(
686 Widget w UNUSED,
687 XtPointer dud UNUSED,
688 XEvent *event,
689 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690{
691 static int lastWidth, lastHeight;
692
693 if (event->type != ConfigureNotify)
694 return;
695
696 if (event->xconfigure.width != lastWidth
697 || event->xconfigure.height != lastHeight)
698 {
699 lastWidth = event->xconfigure.width;
700 lastHeight = event->xconfigure.height;
701 gui_resize_shell(event->xconfigure.width, event->xconfigure.height
702#ifdef FEAT_XIM
703 - xim_get_status_area_height()
704#endif
705 );
706 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200707#if defined(FEAT_NETBEANS_INTG) && defined(FEAT_GUI_MOTIF)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200708 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {
710 XRectangle rec;
711
712 shellRectangle(w, &rec);
713 netbeans_frame_moved(rec.x, rec.y);
714 }
715#endif
716#ifdef FEAT_XIM
717 xim_set_preedit();
718#endif
719}
720
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100722gui_x11_focus_change_cb(
723 Widget w UNUSED,
724 XtPointer data UNUSED,
725 XEvent *event,
726 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727{
728 gui_focus_change(event->type == FocusIn);
729}
730
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100732gui_x11_enter_cb(
733 Widget w UNUSED,
734 XtPointer data UNUSED,
735 XEvent *event UNUSED,
736 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737{
738 gui_focus_change(TRUE);
739}
740
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100742gui_x11_leave_cb(
743 Widget w UNUSED,
744 XtPointer data UNUSED,
745 XEvent *event UNUSED,
746 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747{
748 gui_focus_change(FALSE);
749}
750
751#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
752# if X_HAVE_UTF8_STRING
753# define USE_UTF8LOOKUP
754# endif
755#endif
756
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100758gui_x11_key_hit_cb(
759 Widget w UNUSED,
760 XtPointer dud UNUSED,
761 XEvent *event,
762 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763{
764 XKeyPressedEvent *ev_press;
765#ifdef FEAT_XIM
766 char_u string2[256];
767 char_u string_shortbuf[256];
768 char_u *string = string_shortbuf;
769 Boolean string_alloced = False;
770 Status status;
771#else
772 char_u string[4], string2[3];
773#endif
774 KeySym key_sym, key_sym2;
775 int len, len2;
776 int i;
777 int modifiers;
778 int key;
779
780 ev_press = (XKeyPressedEvent *)event;
781
782#ifdef FEAT_XIM
783 if (xic)
784 {
785# ifdef USE_UTF8LOOKUP
786 /* XFree86 4.0.2 or newer: Be able to get UTF-8 characters even when
787 * the locale isn't utf-8. */
788 if (enc_utf8)
789 len = Xutf8LookupString(xic, ev_press, (char *)string,
790 sizeof(string_shortbuf), &key_sym, &status);
791 else
792# endif
793 len = XmbLookupString(xic, ev_press, (char *)string,
794 sizeof(string_shortbuf), &key_sym, &status);
795 if (status == XBufferOverflow)
796 {
797 string = (char_u *)XtMalloc(len + 1);
798 string_alloced = True;
799# ifdef USE_UTF8LOOKUP
800 /* XFree86 4.0.2 or newer: Be able to get UTF-8 characters even
801 * when the locale isn't utf-8. */
802 if (enc_utf8)
803 len = Xutf8LookupString(xic, ev_press, (char *)string,
804 len, &key_sym, &status);
805 else
806# endif
807 len = XmbLookupString(xic, ev_press, (char *)string,
808 len, &key_sym, &status);
809 }
810 if (status == XLookupNone || status == XLookupChars)
811 key_sym = XK_VoidSymbol;
812
813# ifdef FEAT_MBYTE
814 /* Do conversion from 'termencoding' to 'encoding'. When using
815 * Xutf8LookupString() it has already been done. */
816 if (len > 0 && input_conv.vc_type != CONV_NONE
817# ifdef USE_UTF8LOOKUP
818 && !enc_utf8
819# endif
820 )
821 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100822 int maxlen = len * 4 + 40; /* guessed */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 char_u *p = (char_u *)XtMalloc(maxlen);
824
825 mch_memmove(p, string, len);
826 if (string_alloced)
827 XtFree((char *)string);
828 string = p;
829 string_alloced = True;
830 len = convert_input(p, len, maxlen);
831 }
832# endif
833
834 /* Translate CSI to K_CSI, otherwise it could be recognized as the
835 * start of a special key. */
836 for (i = 0; i < len; ++i)
837 if (string[i] == CSI)
838 {
839 char_u *p = (char_u *)XtMalloc(len + 3);
840
841 mch_memmove(p, string, i + 1);
842 p[i + 1] = KS_EXTRA;
843 p[i + 2] = (int)KE_CSI;
844 mch_memmove(p + i + 3, string + i + 1, len - i);
845 if (string_alloced)
846 XtFree((char *)string);
847 string = p;
848 string_alloced = True;
849 i += 2;
850 len += 2;
851 }
852 }
853 else
854#endif
855 len = XLookupString(ev_press, (char *)string, sizeof(string),
856 &key_sym, NULL);
857
858#ifdef SunXK_F36
859 /*
860 * These keys have bogus lookup strings, and trapping them here is
861 * easier than trying to XRebindKeysym() on them with every possible
862 * combination of modifiers.
863 */
864 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
865 len = 0;
866#endif
867
868#ifdef FEAT_HANGULIN
869 if ((key_sym == XK_space) && (ev_press->state & ShiftMask))
870 {
871 hangul_input_state_toggle();
872 goto theend;
873 }
874#endif
875
876 if (key_sym == XK_space)
877 string[0] = ' '; /* Otherwise Ctrl-Space doesn't work */
878
879 /*
880 * Only on some machines ^_ requires Ctrl+Shift+minus. For consistency,
881 * allow just Ctrl+minus too.
882 */
883 if (key_sym == XK_minus && (ev_press->state & ControlMask))
884 string[0] = Ctrl__;
885
886#ifdef XK_ISO_Left_Tab
887 /* why do we get XK_ISO_Left_Tab instead of XK_Tab for shift-tab? */
888 if (key_sym == XK_ISO_Left_Tab)
889 {
890 key_sym = XK_Tab;
891 string[0] = TAB;
892 len = 1;
893 }
894#endif
895
896 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
897 * that already has the 8th bit set. And not when using a double-byte
898 * encoding, setting the 8th bit may make it the lead byte of a
899 * double-byte character. */
900 if (len == 1
901 && (ev_press->state & Mod1Mask)
902 && !(key_sym == XK_BackSpace || key_sym == XK_Delete)
903 && (string[0] & 0x80) == 0
904#ifdef FEAT_MBYTE
905 && !enc_dbcs
906#endif
907 )
908 {
909#if defined(FEAT_MENU) && defined(FEAT_GUI_MOTIF)
910 /* Ignore ALT keys when they are used for the menu only */
911 if (gui.menu_is_active
912 && (p_wak[0] == 'y'
913 || (p_wak[0] == 'm' && gui_is_menu_shortcut(string[0]))))
914 goto theend;
915#endif
916 /*
917 * Before we set the 8th bit, check to make sure the user doesn't
918 * already have a mapping defined for this sequence. We determine this
919 * by checking to see if the input would be the same without the
920 * Alt/Meta key.
921 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
922 */
923 ev_press->state &= ~Mod1Mask;
924 len2 = XLookupString(ev_press, (char *)string2, sizeof(string2),
925 &key_sym2, NULL);
926 if (key_sym2 == XK_space)
927 string2[0] = ' '; /* Otherwise Meta-Ctrl-Space doesn't work */
928 if ( len2 == 1
929 && string[0] == string2[0]
930 && !(key_sym == XK_Tab && (ev_press->state & ShiftMask)))
931 {
932 string[0] |= 0x80;
933#ifdef FEAT_MBYTE
934 if (enc_utf8) /* convert to utf-8 */
935 {
936 string[1] = string[0] & 0xbf;
937 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
938 if (string[1] == CSI)
939 {
940 string[2] = KS_EXTRA;
941 string[3] = (int)KE_CSI;
942 len = 4;
943 }
944 else
945 len = 2;
946 }
947#endif
948 }
949 else
950 ev_press->state |= Mod1Mask;
951 }
952
953 if (len == 1 && string[0] == CSI)
954 {
955 string[1] = KS_EXTRA;
956 string[2] = (int)KE_CSI;
957 len = -3;
958 }
959
960 /* Check for special keys. Also do this when len == 1 (key has an ASCII
961 * value) to detect backspace, delete and keypad keys. */
962 if (len == 0 || len == 1)
963 {
964 for (i = 0; special_keys[i].key_sym != (KeySym)0; i++)
965 {
966 if (special_keys[i].key_sym == key_sym)
967 {
968 string[0] = CSI;
969 string[1] = special_keys[i].vim_code0;
970 string[2] = special_keys[i].vim_code1;
971 len = -3;
972 break;
973 }
974 }
975 }
976
977 /* Unrecognised key is ignored. */
978 if (len == 0)
979 goto theend;
980
981 /* Special keys (and a few others) may have modifiers. Also when using a
982 * double-byte encoding (can't set the 8th bit). */
983 if (len == -3 || key_sym == XK_space || key_sym == XK_Tab
984 || key_sym == XK_Return || key_sym == XK_Linefeed
985 || key_sym == XK_Escape
986#ifdef FEAT_MBYTE
987 || (enc_dbcs && len == 1 && (ev_press->state & Mod1Mask))
988#endif
989 )
990 {
991 modifiers = 0;
992 if (ev_press->state & ShiftMask)
993 modifiers |= MOD_MASK_SHIFT;
994 if (ev_press->state & ControlMask)
995 modifiers |= MOD_MASK_CTRL;
996 if (ev_press->state & Mod1Mask)
997 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000998 if (ev_press->state & Mod4Mask)
999 modifiers |= MOD_MASK_META;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000
1001 /*
1002 * For some keys a shift modifier is translated into another key
1003 * code.
1004 */
1005 if (len == -3)
1006 key = TO_SPECIAL(string[1], string[2]);
1007 else
1008 key = string[0];
1009 key = simplify_key(key, &modifiers);
1010 if (key == CSI)
1011 key = K_CSI;
1012 if (IS_SPECIAL(key))
1013 {
1014 string[0] = CSI;
1015 string[1] = K_SECOND(key);
1016 string[2] = K_THIRD(key);
1017 len = 3;
1018 }
1019 else
1020 {
1021 string[0] = key;
1022 len = 1;
1023 }
1024
1025 if (modifiers != 0)
1026 {
1027 string2[0] = CSI;
1028 string2[1] = KS_MODIFIER;
1029 string2[2] = modifiers;
1030 add_to_input_buf(string2, 3);
1031 }
1032 }
1033
1034 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1035#ifdef UNIX
1036 || (intr_char != 0 && string[0] == intr_char)
1037#endif
1038 ))
1039 {
1040 trash_input_buf();
1041 got_int = TRUE;
1042 }
1043
1044 add_to_input_buf(string, len);
1045
1046 /*
1047 * blank out the pointer if necessary
1048 */
1049 if (p_mh)
1050 gui_mch_mousehide(TRUE);
1051
1052#if defined(FEAT_BEVAL_TIP)
1053 {
1054 BalloonEval *be;
1055
1056 if ((be = gui_mch_currently_showing_beval()) != NULL)
1057 gui_mch_unpost_balloon(be);
1058 }
1059#endif
1060theend:
1061 {} /* some compilers need a statement here */
1062#ifdef FEAT_XIM
1063 if (string_alloced)
1064 XtFree((char *)string);
1065#endif
1066}
1067
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001069gui_x11_mouse_cb(
1070 Widget w UNUSED,
1071 XtPointer dud UNUSED,
1072 XEvent *event,
1073 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074{
1075 static XtIntervalId timer = (XtIntervalId)0;
1076 static int timed_out = TRUE;
1077
1078 int button;
1079 int repeated_click = FALSE;
1080 int x, y;
1081 int_u x_modifiers;
1082 int_u vim_modifiers;
1083
1084 if (event->type == MotionNotify)
1085 {
1086 /* Get the latest position, avoids lagging behind on a drag. */
1087 x = event->xmotion.x;
1088 y = event->xmotion.y;
1089 x_modifiers = event->xmotion.state;
1090 button = (x_modifiers & (Button1Mask | Button2Mask | Button3Mask))
1091 ? MOUSE_DRAG : ' ';
1092
1093 /*
1094 * if our pointer is currently hidden, then we should show it.
1095 */
1096 gui_mch_mousehide(FALSE);
1097
1098 if (button != MOUSE_DRAG) /* just moving the rodent */
1099 {
1100#ifdef FEAT_MENU
1101 if (dud) /* moved in vimForm */
1102 y -= gui.menu_height;
1103#endif
1104 gui_mouse_moved(x, y);
1105 return;
1106 }
1107 }
1108 else
1109 {
1110 x = event->xbutton.x;
1111 y = event->xbutton.y;
1112 if (event->type == ButtonPress)
1113 {
1114 /* Handle multiple clicks */
1115 if (!timed_out)
1116 {
1117 XtRemoveTimeOut(timer);
1118 repeated_click = TRUE;
1119 }
1120 timed_out = FALSE;
1121 timer = XtAppAddTimeOut(app_context, (long_u)p_mouset,
1122 gui_x11_timer_cb, &timed_out);
1123 switch (event->xbutton.button)
1124 {
Bram Moolenaar88e484b2015-11-24 15:38:44 +01001125 /* keep in sync with gui_gtk_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 case Button1: button = MOUSE_LEFT; break;
1127 case Button2: button = MOUSE_MIDDLE; break;
1128 case Button3: button = MOUSE_RIGHT; break;
1129 case Button4: button = MOUSE_4; break;
1130 case Button5: button = MOUSE_5; break;
Bram Moolenaar88e484b2015-11-24 15:38:44 +01001131 case 6: button = MOUSE_7; break;
1132 case 7: button = MOUSE_6; break;
1133 case 8: button = MOUSE_X1; break;
1134 case 9: button = MOUSE_X2; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 default:
1136 return; /* Unknown button */
1137 }
1138 }
1139 else if (event->type == ButtonRelease)
1140 button = MOUSE_RELEASE;
1141 else
1142 return; /* Unknown mouse event type */
1143
1144 x_modifiers = event->xbutton.state;
1145#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1146 last_mouse_event = event->xbutton;
1147#endif
1148 }
1149
1150 vim_modifiers = 0x0;
1151 if (x_modifiers & ShiftMask)
1152 vim_modifiers |= MOUSE_SHIFT;
1153 if (x_modifiers & ControlMask)
1154 vim_modifiers |= MOUSE_CTRL;
1155 if (x_modifiers & Mod1Mask) /* Alt or Meta key */
1156 vim_modifiers |= MOUSE_ALT;
1157
1158 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
1159}
1160
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161/*
1162 * End of call-back routines
1163 */
1164
1165/*
1166 * Parse the GUI related command-line arguments. Any arguments used are
1167 * deleted from argv, and *argc is decremented accordingly. This is called
1168 * when vim is started, whether or not the GUI has been started.
1169 */
1170 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001171gui_mch_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172{
1173 int arg;
1174 int i;
1175
1176 /*
1177 * Move all the entries in argv which are relevant to X into gui_argv.
1178 */
1179 gui_argc = 0;
1180 gui_argv = (char **)lalloc((long_u)(*argc * sizeof(char *)), FALSE);
1181 if (gui_argv == NULL)
1182 return;
1183 gui_argv[gui_argc++] = argv[0];
1184 arg = 1;
1185 while (arg < *argc)
1186 {
1187 /* Look for argv[arg] in cmdline_options[] table */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001188 for (i = 0; i < (int)XtNumber(cmdline_options); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if (strcmp(argv[arg], cmdline_options[i].option) == 0)
1190 break;
1191
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001192 if (i < (int)XtNumber(cmdline_options))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 {
1194 /* Remember finding "-rv" or "-reverse" */
1195 if (strcmp("-rv", argv[arg]) == 0
1196 || strcmp("-reverse", argv[arg]) == 0)
1197 found_reverse_arg = TRUE;
1198 else if ((strcmp("-fn", argv[arg]) == 0
1199 || strcmp("-font", argv[arg]) == 0)
1200 && arg + 1 < *argc)
1201 font_argument = argv[arg + 1];
1202
1203 /* Found match in table, so move it into gui_argv */
1204 gui_argv[gui_argc++] = argv[arg];
1205 if (--*argc > arg)
1206 {
1207 mch_memmove(&argv[arg], &argv[arg + 1], (*argc - arg)
1208 * sizeof(char *));
1209 if (cmdline_options[i].argKind != XrmoptionNoArg)
1210 {
1211 /* Move the options argument as well */
1212 gui_argv[gui_argc++] = argv[arg];
1213 if (--*argc > arg)
1214 mch_memmove(&argv[arg], &argv[arg + 1], (*argc - arg)
1215 * sizeof(char *));
1216 }
1217 }
1218 argv[*argc] = NULL;
1219 }
1220 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221#ifdef FEAT_NETBEANS_INTG
1222 if (strncmp("-nb", argv[arg], 3) == 0)
1223 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 gui.dofork = FALSE; /* don't fork() when starting GUI */
1225 netbeansArg = argv[arg];
1226 mch_memmove(&argv[arg], &argv[arg + 1],
1227 (--*argc - arg) * sizeof(char *));
1228 argv[*argc] = NULL;
1229 }
1230 else
1231#endif
1232 arg++;
1233 }
1234}
1235
1236#ifndef XtSpecificationRelease
1237# define CARDINAL (Cardinal *)
1238#else
1239# if XtSpecificationRelease == 4
1240# define CARDINAL (Cardinal *)
1241# else
1242# define CARDINAL (int *)
1243# endif
1244#endif
1245
1246/*
1247 * Check if the GUI can be started. Called before gvimrc is sourced.
1248 * Return OK or FAIL.
1249 */
1250 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001251gui_mch_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252{
1253#ifdef FEAT_XIM
1254 XtSetLanguageProc(NULL, NULL, NULL);
1255#endif
1256 open_app_context();
1257 if (app_context != NULL)
1258 gui.dpy = XtOpenDisplay(app_context, 0, VIM_NAME, VIM_CLASS,
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00001259 cmdline_options, XtNumber(cmdline_options),
1260 CARDINAL &gui_argc, gui_argv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261
Bram Moolenaar889fe2c2018-05-13 16:23:40 +02001262# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1263 {
1264 /* The call to XtOpenDisplay() may have set the locale from the
1265 * environment. Set LC_NUMERIC to "C" to make sure that strtod() uses a
1266 * decimal point, not a comma. */
1267 char *p = setlocale(LC_NUMERIC, NULL);
1268
1269 if (p == NULL || strcmp(p, "C") != 0)
1270 setlocale(LC_NUMERIC, "C");
1271 }
1272# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 if (app_context == NULL || gui.dpy == NULL)
1274 {
1275 gui.dying = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001276 emsg(_(e_opendisp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 return FAIL;
1278 }
1279 return OK;
1280}
1281
1282
1283#ifdef USE_XSMP
1284/*
1285 * Handle XSMP processing, de-registering the attachment upon error
1286 */
1287static XtInputId _xsmp_xtinputid;
1288
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001290local_xsmp_handle_requests(
1291 XtPointer c UNUSED,
1292 int *s UNUSED,
1293 XtInputId *i UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294{
1295 if (xsmp_handle_requests() == FAIL)
1296 XtRemoveInput(_xsmp_xtinputid);
1297}
1298#endif
1299
1300
1301/*
1302 * Initialise the X GUI. Create all the windows, set up all the call-backs etc.
1303 * Returns OK for success, FAIL when the GUI can't be started.
1304 */
1305 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001306gui_mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307{
1308 XtGCMask gc_mask;
1309 XGCValues gc_vals;
1310 int x, y, mask;
1311 unsigned w, h;
1312
1313#if 0
1314 /* Uncomment this to enable synchronous mode for debugging */
1315 XSynchronize(gui.dpy, True);
1316#endif
1317
1318 vimShell = XtVaAppCreateShell(VIM_NAME, VIM_CLASS,
1319 applicationShellWidgetClass, gui.dpy, NULL);
1320
1321 /*
1322 * Get the application resources
1323 */
1324 XtVaGetApplicationResources(vimShell, (XtPointer)&gui,
1325 vim_resources, XtNumber(vim_resources), NULL);
1326
1327 gui.scrollbar_height = gui.scrollbar_width;
1328
1329 /*
1330 * Get the colors ourselves. Using the automatic conversion doesn't
1331 * handle looking for approximate colors.
1332 */
1333 /* NOTE: These next few lines are an exact duplicate of gui_athena.c's
1334 * gui_mch_def_colors(). Why?
1335 */
1336 gui.menu_fg_pixel = gui_get_color((char_u *)gui.rsrc_menu_fg_name);
1337 gui.menu_bg_pixel = gui_get_color((char_u *)gui.rsrc_menu_bg_name);
1338 gui.scroll_fg_pixel = gui_get_color((char_u *)gui.rsrc_scroll_fg_name);
1339 gui.scroll_bg_pixel = gui_get_color((char_u *)gui.rsrc_scroll_bg_name);
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001340#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 gui.tooltip_fg_pixel = gui_get_color((char_u *)gui.rsrc_tooltip_fg_name);
1342 gui.tooltip_bg_pixel = gui_get_color((char_u *)gui.rsrc_tooltip_bg_name);
1343#endif
1344
1345#if defined(FEAT_MENU) && defined(FEAT_GUI_ATHENA)
1346 /* If the menu height was set, don't change it at runtime */
1347 if (gui.menu_height != MENU_DEFAULT_HEIGHT)
1348 gui.menu_height_fixed = TRUE;
1349#endif
1350
1351 /* Set default foreground and background colours */
1352 gui.norm_pixel = gui.def_norm_pixel;
1353 gui.back_pixel = gui.def_back_pixel;
1354
1355 /* Check if reverse video needs to be applied (on Sun it's done by X) */
1356 if (gui.rsrc_rev_video && gui_get_lightness(gui.back_pixel)
1357 > gui_get_lightness(gui.norm_pixel))
1358 {
1359 gui.norm_pixel = gui.def_back_pixel;
1360 gui.back_pixel = gui.def_norm_pixel;
1361 gui.def_norm_pixel = gui.norm_pixel;
1362 gui.def_back_pixel = gui.back_pixel;
1363 }
1364
1365 /* Get the colors from the "Normal", "Tooltip", "Scrollbar" and "Menu"
1366 * group (set in syntax.c or in a vimrc file) */
1367 set_normal_colors();
1368
1369 /*
1370 * Check that none of the colors are the same as the background color
1371 */
1372 gui_check_colors();
1373
1374 /*
1375 * Set up the GCs. The font attributes will be set in gui_init_font().
1376 */
1377 gc_mask = GCForeground | GCBackground;
1378 gc_vals.foreground = gui.norm_pixel;
1379 gc_vals.background = gui.back_pixel;
1380 gui.text_gc = XtGetGC(vimShell, gc_mask, &gc_vals);
1381
1382 gc_vals.foreground = gui.back_pixel;
1383 gc_vals.background = gui.norm_pixel;
1384 gui.back_gc = XtGetGC(vimShell, gc_mask, &gc_vals);
1385
1386 gc_mask |= GCFunction;
1387 gc_vals.foreground = gui.norm_pixel ^ gui.back_pixel;
1388 gc_vals.background = gui.norm_pixel ^ gui.back_pixel;
1389 gc_vals.function = GXxor;
1390 gui.invert_gc = XtGetGC(vimShell, gc_mask, &gc_vals);
1391
1392 gui.visibility = VisibilityUnobscured;
1393 x11_setup_atoms(gui.dpy);
1394
1395 if (gui_win_x != -1 && gui_win_y != -1)
1396 gui_mch_set_winpos(gui_win_x, gui_win_y);
1397
1398 /* Now adapt the supplied(?) geometry-settings */
1399 /* Added by Kjetil Jacobsen <kjetilja@stud.cs.uit.no> */
1400 if (gui.geom != NULL && *gui.geom != NUL)
1401 {
1402 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
1403 if (mask & WidthValue)
1404 Columns = w;
1405 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00001406 {
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001407 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00001408 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00001410 }
Bram Moolenaarc33916a2013-07-01 21:43:08 +02001411 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 /*
1413 * Set the (x,y) position of the main window only if specified in the
1414 * users geometry, so we get good defaults when they don't. This needs
1415 * to be done before the shell is popped up.
1416 */
1417 if (mask & (XValue|YValue))
1418 XtVaSetValues(vimShell, XtNgeometry, gui.geom, NULL);
1419 }
1420
1421 gui_x11_create_widgets();
1422
1423 /*
1424 * Add an icon to Vim (Marcel Douben: 11 May 1998).
1425 */
1426 if (vim_strchr(p_go, GO_ICON) != NULL)
1427 {
1428#ifndef HAVE_XPM
1429# include "vim_icon.xbm"
1430# include "vim_mask.xbm"
1431
1432 Arg arg[2];
1433
1434 XtSetArg(arg[0], XtNiconPixmap,
1435 XCreateBitmapFromData(gui.dpy,
1436 DefaultRootWindow(gui.dpy),
1437 (char *)vim_icon_bits,
1438 vim_icon_width,
1439 vim_icon_height));
1440 XtSetArg(arg[1], XtNiconMask,
1441 XCreateBitmapFromData(gui.dpy,
1442 DefaultRootWindow(gui.dpy),
1443 (char *)vim_mask_icon_bits,
1444 vim_mask_icon_width,
1445 vim_mask_icon_height));
1446 XtSetValues(vimShell, arg, (Cardinal)2);
1447#else
1448/* Use Pixmaps, looking much nicer. */
1449
1450/* If you get an error message here, you still need to unpack the runtime
1451 * archive! */
1452# ifdef magick
1453# undef magick
1454# endif
1455# define magick vim32x32
1456# include "../runtime/vim32x32.xpm"
1457# undef magick
1458# define magick vim16x16
1459# include "../runtime/vim16x16.xpm"
1460# undef magick
1461# define magick vim48x48
1462# include "../runtime/vim48x48.xpm"
1463# undef magick
1464
1465 static Pixmap icon = 0;
1466 static Pixmap icon_mask = 0;
1467 static char **magick = vim32x32;
1468 Window root_window;
1469 XIconSize *size;
1470 int number_sizes;
1471 Display *dsp;
1472 Screen *scr;
1473 XpmAttributes attr;
1474 Colormap cmap;
1475
1476 /*
1477 * Adjust the icon to the preferences of the actual window manager.
1478 */
1479 root_window = XRootWindowOfScreen(XtScreen(vimShell));
1480 if (XGetIconSizes(XtDisplay(vimShell), root_window,
1481 &size, &number_sizes) != 0)
1482 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 if (number_sizes > 0)
1484 {
Bram Moolenaar6f292662013-07-14 15:06:50 +02001485 if (size->max_height >= 48 && size->max_width >= 48)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 magick = vim48x48;
Bram Moolenaar6f292662013-07-14 15:06:50 +02001487 else if (size->max_height >= 32 && size->max_width >= 32)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 magick = vim32x32;
Bram Moolenaar6f292662013-07-14 15:06:50 +02001489 else if (size->max_height >= 16 && size->max_width >= 16)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 magick = vim16x16;
1491 }
1492 }
1493
1494 dsp = XtDisplay(vimShell);
1495 scr = XtScreen(vimShell);
1496
1497 cmap = DefaultColormap(dsp, DefaultScreen(dsp));
1498 XtVaSetValues(vimShell, XtNcolormap, cmap, NULL);
1499
1500 attr.valuemask = 0L;
1501 attr.valuemask = XpmCloseness | XpmReturnPixels | XpmColormap | XpmDepth;
1502 attr.closeness = 65535; /* accuracy isn't crucial */
1503 attr.colormap = cmap;
1504 attr.depth = DefaultDepthOfScreen(scr);
1505
1506 if (!icon)
Bram Moolenaare8208012008-06-20 09:59:25 +00001507 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 XpmCreatePixmapFromData(dsp, root_window, magick, &icon,
1509 &icon_mask, &attr);
Bram Moolenaare8208012008-06-20 09:59:25 +00001510 XpmFreeAttributes(&attr);
1511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512
1513# ifdef FEAT_GUI_ATHENA
1514 XtVaSetValues(vimShell, XtNiconPixmap, icon, XtNiconMask, icon_mask, NULL);
1515# else
1516 XtVaSetValues(vimShell, XmNiconPixmap, icon, XmNiconMask, icon_mask, NULL);
1517# endif
1518#endif
1519 }
1520
1521 if (gui.color_approx)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001522 emsg(_("Vim E458: Cannot allocate colormap entry, some colors may be incorrect"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001524#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 gui_init_tooltip_font();
1526#endif
1527#ifdef FEAT_MENU
1528 gui_init_menu_font();
1529#endif
1530
1531#ifdef USE_XSMP
1532 /* Attach listener on ICE connection */
1533 if (-1 != xsmp_icefd)
1534 _xsmp_xtinputid = XtAppAddInput(app_context, xsmp_icefd,
1535 (XtPointer)XtInputReadMask, local_xsmp_handle_requests, NULL);
1536#endif
1537
1538 return OK;
1539}
1540
1541/*
1542 * Called when starting the GUI fails after calling gui_mch_init().
1543 */
1544 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001545gui_mch_uninit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546{
1547 gui_x11_destroy_widgets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 XtCloseDisplay(gui.dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 gui.dpy = NULL;
1550 vimShell = (Widget)0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001551 VIM_CLEAR(gui_argv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552}
1553
1554/*
1555 * Called when the foreground or background color has been changed.
1556 */
1557 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001558gui_mch_new_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559{
1560 long_u gc_mask;
1561 XGCValues gc_vals;
1562
1563 gc_mask = GCForeground | GCBackground;
1564 gc_vals.foreground = gui.norm_pixel;
1565 gc_vals.background = gui.back_pixel;
1566 if (gui.text_gc != NULL)
1567 XChangeGC(gui.dpy, gui.text_gc, gc_mask, &gc_vals);
1568
1569 gc_vals.foreground = gui.back_pixel;
1570 gc_vals.background = gui.norm_pixel;
1571 if (gui.back_gc != NULL)
1572 XChangeGC(gui.dpy, gui.back_gc, gc_mask, &gc_vals);
1573
1574 gc_mask |= GCFunction;
1575 gc_vals.foreground = gui.norm_pixel ^ gui.back_pixel;
1576 gc_vals.background = gui.norm_pixel ^ gui.back_pixel;
1577 gc_vals.function = GXxor;
1578 if (gui.invert_gc != NULL)
1579 XChangeGC(gui.dpy, gui.invert_gc, gc_mask, &gc_vals);
1580
1581 gui_x11_set_back_color();
1582}
1583
1584/*
1585 * Open the GUI window which was created by a call to gui_mch_init().
1586 */
1587 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001588gui_mch_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589{
1590 /* Actually open the window */
Bram Moolenaara5792f52005-11-23 21:25:05 +00001591 XtRealizeWidget(vimShell);
1592 XtManageChild(XtNameToWidget(vimShell, "*vimForm"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
1594 gui.wid = gui_x11_get_wid();
1595 gui.blank_pointer = gui_x11_create_blank_mouse();
1596
1597 /*
1598 * Add a callback for the Close item on the window managers menu, and the
1599 * save-yourself event.
1600 */
1601 wm_atoms[SAVE_YOURSELF_IDX] =
1602 XInternAtom(gui.dpy, "WM_SAVE_YOURSELF", False);
1603 wm_atoms[DELETE_WINDOW_IDX] =
1604 XInternAtom(gui.dpy, "WM_DELETE_WINDOW", False);
1605 XSetWMProtocols(gui.dpy, XtWindow(vimShell), wm_atoms, 2);
1606 XtAddEventHandler(vimShell, NoEventMask, True, gui_x11_wm_protocol_handler,
1607 NULL);
1608#ifdef HAVE_X11_XMU_EDITRES_H
1609 /*
1610 * Enable editres protocol (see "man editres").
1611 * Usually will need to add -lXmu to the linker line as well.
1612 */
1613 XtAddEventHandler(vimShell, (EventMask)0, True, _XEditResCheckMessages,
1614 (XtPointer)NULL);
1615#endif
1616
1617#ifdef FEAT_CLIENTSERVER
1618 if (serverName == NULL && serverDelayedStartName != NULL)
1619 {
1620 /* This is a :gui command in a plain vim with no previous server */
1621 commWindow = XtWindow(vimShell);
1622 (void)serverRegisterName(gui.dpy, serverDelayedStartName);
1623 }
1624 else
1625 {
1626 /*
1627 * Cannot handle "widget-less" windows with XtProcessEvent() we'll
1628 * have to change the "server" registration to that of the main window
1629 * If we have not registered a name yet, remember the window
1630 */
1631 serverChangeRegisteredWindow(gui.dpy, XtWindow(vimShell));
1632 }
1633 XtAddEventHandler(vimShell, PropertyChangeMask, False,
1634 gui_x11_send_event_handler, NULL);
1635#endif
1636
1637
1638#if defined(FEAT_MENU) && defined(FEAT_GUI_ATHENA)
1639 /* The Athena GUI needs this again after opening the window */
1640 gui_position_menu();
1641# ifdef FEAT_TOOLBAR
1642 gui_mch_set_toolbar_pos(0, gui.menu_height, gui.menu_width,
1643 gui.toolbar_height);
1644# endif
1645#endif
1646
1647 /* Get the colors for the highlight groups (gui_check_colors() might have
1648 * changed them) */
1649 highlight_gui_started(); /* re-init colors and fonts */
1650
1651#ifdef FEAT_HANGULIN
1652 hangul_keyboard_set();
1653#endif
1654#ifdef FEAT_XIM
1655 xim_init();
1656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657
1658 return OK;
1659}
1660
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001661#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662/*
1663 * Convert the tooltip fontset name to an XFontSet.
1664 */
1665 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001666gui_init_tooltip_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667{
1668 XrmValue from, to;
1669
1670 from.addr = (char *)gui.rsrc_tooltip_font_name;
1671 from.size = strlen(from.addr);
1672 to.addr = (XtPointer)&gui.tooltip_fontset;
1673 to.size = sizeof(XFontSet);
1674
1675 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontSet, &to) == False)
1676 {
1677 /* Failed. What to do? */
1678 }
1679}
1680#endif
1681
1682#if defined(FEAT_MENU) || defined(PROTO)
1683/* Convert the menu font/fontset name to an XFontStruct/XFontset */
1684 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001685gui_init_menu_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686{
1687 XrmValue from, to;
1688
1689#ifdef FONTSET_ALWAYS
1690 from.addr = (char *)gui.rsrc_menu_font_name;
1691 from.size = strlen(from.addr);
1692 to.addr = (XtPointer)&gui.menu_fontset;
1693 to.size = sizeof(GuiFontset);
1694
1695 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontSet, &to) == False)
1696 {
1697 /* Failed. What to do? */
1698 }
1699#else
1700 from.addr = (char *)gui.rsrc_menu_font_name;
1701 from.size = strlen(from.addr);
1702 to.addr = (XtPointer)&gui.menu_font;
1703 to.size = sizeof(GuiFont);
1704
1705 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontStruct, &to) == False)
1706 {
1707 /* Failed. What to do? */
1708 }
1709#endif
1710}
1711#endif
1712
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001714gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715{
1716#if 0
1717 /* Lesstif gives an error message here, and so does Solaris. The man page
1718 * says that this isn't needed when exiting, so just skip it. */
1719 XtCloseDisplay(gui.dpy);
1720#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01001721 VIM_CLEAR(gui_argv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722}
1723
1724/*
1725 * Get the position of the top left corner of the window.
1726 */
1727 int
Bram Moolenaar02fdaea2016-01-30 18:13:55 +01001728gui_mch_get_winpos(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729{
1730 Dimension xpos, ypos;
1731
1732 XtVaGetValues(vimShell,
1733 XtNx, &xpos,
1734 XtNy, &ypos,
1735 NULL);
1736 *x = xpos;
1737 *y = ypos;
1738 return OK;
1739}
1740
1741/*
1742 * Set the position of the top left corner of the window to the given
1743 * coordinates.
1744 */
1745 void
Bram Moolenaar02fdaea2016-01-30 18:13:55 +01001746gui_mch_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747{
1748 XtVaSetValues(vimShell,
1749 XtNx, x,
1750 XtNy, y,
1751 NULL);
1752}
1753
1754 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001755gui_mch_set_shellsize(
1756 int width,
1757 int height,
1758 int min_width,
1759 int min_height,
1760 int base_width,
1761 int base_height,
1762 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763{
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00001764#ifdef FEAT_XIM
1765 height += xim_get_status_area_height(),
1766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 XtVaSetValues(vimShell,
1768 XtNwidthInc, gui.char_width,
1769 XtNheightInc, gui.char_height,
1770#if defined(XtSpecificationRelease) && XtSpecificationRelease >= 4
1771 XtNbaseWidth, base_width,
1772 XtNbaseHeight, base_height,
1773#endif
1774 XtNminWidth, min_width,
1775 XtNminHeight, min_height,
1776 XtNwidth, width,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 XtNheight, height,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 NULL);
1779}
1780
1781/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00001782 * Allow 10 pixels for horizontal borders, 'guiheadroom' for vertical borders.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 * Is there no way in X to find out how wide the borders really are?
1784 */
1785 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001786gui_mch_get_screen_dimensions(
1787 int *screen_w,
1788 int *screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
1790 *screen_w = DisplayWidth(gui.dpy, DefaultScreen(gui.dpy)) - 10;
1791 *screen_h = DisplayHeight(gui.dpy, DefaultScreen(gui.dpy)) - p_ghr;
1792}
1793
1794/*
1795 * Initialise vim to use the font "font_name". If it's NULL, pick a default
1796 * font.
1797 * If "fontset" is TRUE, load the "font_name" as a fontset.
1798 * Return FAIL if the font could not be loaded, OK otherwise.
1799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001801gui_mch_init_font(
1802 char_u *font_name,
1803 int do_fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804{
1805 XFontStruct *font = NULL;
1806
1807#ifdef FEAT_XFONTSET
1808 XFontSet fontset = NULL;
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001811#ifdef FEAT_GUI_MOTIF
1812 /* A font name equal "*" is indicating, that we should activate the font
1813 * selection dialogue to get a new font name. So let us do it here. */
1814 if (font_name != NULL && STRCMP(font_name, "*") == 0)
1815 font_name = gui_xm_select_font(hl_get_font_name());
1816#endif
1817
1818#ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 if (do_fontset)
1820 {
1821 /* If 'guifontset' is set, VIM treats all font specifications as if
1822 * they were fontsets, and 'guifontset' becomes the default. */
1823 if (font_name != NULL)
1824 {
1825 fontset = (XFontSet)gui_mch_get_fontset(font_name, FALSE, TRUE);
1826 if (fontset == NULL)
1827 return FAIL;
1828 }
1829 }
1830 else
1831#endif
1832 {
1833 if (font_name == NULL)
1834 {
1835 /*
1836 * If none of the fonts in 'font' could be loaded, try the one set
1837 * in the X resource, and finally just try using DFLT_FONT, which
1838 * will hopefully always be there.
1839 */
1840 font_name = gui.rsrc_font_name;
1841 font = (XFontStruct *)gui_mch_get_font(font_name, FALSE);
1842 if (font == NULL)
1843 font_name = (char_u *)DFLT_FONT;
1844 }
1845 if (font == NULL)
1846 font = (XFontStruct *)gui_mch_get_font(font_name, FALSE);
1847 if (font == NULL)
1848 return FAIL;
1849 }
1850
1851 gui_mch_free_font(gui.norm_font);
1852#ifdef FEAT_XFONTSET
1853 gui_mch_free_fontset(gui.fontset);
1854
1855 if (fontset != NULL)
1856 {
1857 gui.norm_font = NOFONT;
1858 gui.fontset = (GuiFontset)fontset;
1859 gui.char_width = fontset_width(fontset);
1860 gui.char_height = fontset_height(fontset) + p_linespace;
1861 gui.char_ascent = fontset_ascent(fontset) + p_linespace / 2;
1862 }
1863 else
1864#endif
1865 {
1866 gui.norm_font = (GuiFont)font;
1867#ifdef FEAT_XFONTSET
1868 gui.fontset = NOFONTSET;
1869#endif
1870 gui.char_width = font->max_bounds.width;
1871 gui.char_height = font->ascent + font->descent + p_linespace;
1872 gui.char_ascent = font->ascent + p_linespace / 2;
1873 }
1874
1875 hl_set_font_name(font_name);
1876
1877 /*
1878 * Try to load other fonts for bold, italic, and bold-italic.
1879 * We should also try to work out what font to use for these when they are
1880 * not specified by X resources, but we don't yet.
1881 */
1882 if (font_name == gui.rsrc_font_name)
1883 {
1884 if (gui.bold_font == NOFONT
1885 && gui.rsrc_bold_font_name != NULL
1886 && *gui.rsrc_bold_font_name != NUL)
1887 gui.bold_font = gui_mch_get_font(gui.rsrc_bold_font_name, FALSE);
1888 if (gui.ital_font == NOFONT
1889 && gui.rsrc_ital_font_name != NULL
1890 && *gui.rsrc_ital_font_name != NUL)
1891 gui.ital_font = gui_mch_get_font(gui.rsrc_ital_font_name, FALSE);
1892 if (gui.boldital_font == NOFONT
1893 && gui.rsrc_boldital_font_name != NULL
1894 && *gui.rsrc_boldital_font_name != NUL)
1895 gui.boldital_font = gui_mch_get_font(gui.rsrc_boldital_font_name,
1896 FALSE);
1897 }
1898 else
1899 {
1900 /* When not using the font specified by the resources, also don't use
1901 * the bold/italic fonts, otherwise setting 'guifont' will look very
1902 * strange. */
1903 if (gui.bold_font != NOFONT)
1904 {
1905 XFreeFont(gui.dpy, (XFontStruct *)gui.bold_font);
1906 gui.bold_font = NOFONT;
1907 }
1908 if (gui.ital_font != NOFONT)
1909 {
1910 XFreeFont(gui.dpy, (XFontStruct *)gui.ital_font);
1911 gui.ital_font = NOFONT;
1912 }
1913 if (gui.boldital_font != NOFONT)
1914 {
1915 XFreeFont(gui.dpy, (XFontStruct *)gui.boldital_font);
1916 gui.boldital_font = NOFONT;
1917 }
1918 }
1919
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001920#ifdef FEAT_GUI_MOTIF
1921 gui_motif_synch_fonts();
1922#endif
1923
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 return OK;
1925}
1926
1927/*
1928 * Get a font structure for highlighting.
1929 */
1930 GuiFont
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001931gui_mch_get_font(char_u *name, int giveErrorIfMissing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932{
1933 XFontStruct *font;
1934
Bram Moolenaard23a8232018-02-10 18:45:26 +01001935 if (!gui.in_use || name == NULL) /* can't do this when GUI not running */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 return NOFONT;
1937
1938 font = XLoadQueryFont(gui.dpy, (char *)name);
1939
1940 if (font == NULL)
1941 {
1942 if (giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001943 semsg(_(e_font), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 return NOFONT;
1945 }
1946
1947#ifdef DEBUG
1948 printf("Font Information for '%s':\n", name);
1949 printf(" w = %d, h = %d, ascent = %d, descent = %d\n",
1950 font->max_bounds.width, font->ascent + font->descent,
1951 font->ascent, font->descent);
1952 printf(" max ascent = %d, max descent = %d, max h = %d\n",
1953 font->max_bounds.ascent, font->max_bounds.descent,
1954 font->max_bounds.ascent + font->max_bounds.descent);
1955 printf(" min lbearing = %d, min rbearing = %d\n",
1956 font->min_bounds.lbearing, font->min_bounds.rbearing);
1957 printf(" max lbearing = %d, max rbearing = %d\n",
1958 font->max_bounds.lbearing, font->max_bounds.rbearing);
1959 printf(" leftink = %d, rightink = %d\n",
1960 (font->min_bounds.lbearing < 0),
1961 (font->max_bounds.rbearing > font->max_bounds.width));
1962 printf("\n");
1963#endif
1964
1965 if (font->max_bounds.width != font->min_bounds.width)
1966 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001967 semsg(_(e_fontwidth), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 XFreeFont(gui.dpy, font);
1969 return NOFONT;
1970 }
1971 return (GuiFont)font;
1972}
1973
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001974#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00001975/*
1976 * Return the name of font "font" in allocated memory.
Bram Moolenaar46c9c732004-12-12 11:37:09 +00001977 */
1978 char_u *
Bram Moolenaar87748452017-03-12 17:10:33 +01001979gui_mch_get_fontname(GuiFont font, char_u *name)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00001980{
Bram Moolenaar87748452017-03-12 17:10:33 +01001981 char_u *ret = NULL;
1982
1983 if (name != NULL && font == NULL)
1984 {
1985 /* In this case, there's no way other than doing this. */
1986 ret = vim_strsave(name);
1987 }
1988 else if (font != NULL)
1989 {
1990 /* In this case, try to retrieve the XLFD corresponding to 'font'->fid;
1991 * if failed, use 'name' unless it's NULL. */
1992 unsigned long value = 0L;
1993
1994 if (XGetFontProperty(font, XA_FONT, &value))
1995 {
1996 char *xa_font_name = NULL;
1997
1998 xa_font_name = XGetAtomName(gui.dpy, value);
1999 if (xa_font_name != NULL)
2000 {
2001 ret = vim_strsave((char_u *)xa_font_name);
2002 XFree(xa_font_name);
2003 }
2004 else if (name != NULL)
2005 ret = vim_strsave(name);
2006 }
2007 else if (name != NULL)
2008 ret = vim_strsave(name);
2009 }
2010 return ret;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002011}
Bram Moolenaar567e4de2004-12-31 21:01:02 +00002012#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002013
Bram Moolenaar231334e2005-07-25 20:46:57 +00002014/*
2015 * Adjust gui.char_height (after 'linespace' was changed).
2016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002018gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019{
2020#ifdef FEAT_XFONTSET
2021 if (gui.fontset != NOFONTSET)
2022 {
2023 gui.char_height = fontset_height((XFontSet)gui.fontset) + p_linespace;
2024 gui.char_ascent = fontset_ascent((XFontSet)gui.fontset)
2025 + p_linespace / 2;
2026 }
2027 else
2028#endif
2029 {
2030 XFontStruct *font = (XFontStruct *)gui.norm_font;
2031
2032 gui.char_height = font->ascent + font->descent + p_linespace;
2033 gui.char_ascent = font->ascent + p_linespace / 2;
2034 }
2035 return OK;
2036}
2037
2038/*
2039 * Set the current text font.
2040 */
2041 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002042gui_mch_set_font(GuiFont font)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043{
2044 static Font prev_font = (Font)-1;
2045 Font fid = ((XFontStruct *)font)->fid;
2046
2047 if (fid != prev_font)
2048 {
2049 XSetFont(gui.dpy, gui.text_gc, fid);
2050 XSetFont(gui.dpy, gui.back_gc, fid);
2051 prev_font = fid;
2052 gui.char_ascent = ((XFontStruct *)font)->ascent + p_linespace / 2;
2053 }
2054#ifdef FEAT_XFONTSET
2055 current_fontset = (XFontSet)NULL;
2056#endif
2057}
2058
2059#if defined(FEAT_XFONTSET) || defined(PROTO)
2060/*
2061 * Set the current text fontset.
2062 * Adjust the ascent, in case it's different.
2063 */
2064 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002065gui_mch_set_fontset(GuiFontset fontset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066{
2067 current_fontset = (XFontSet)fontset;
2068 gui.char_ascent = fontset_ascent(current_fontset) + p_linespace / 2;
2069}
2070#endif
2071
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072/*
2073 * If a font is not going to be used, free its structure.
2074 */
2075 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002076gui_mch_free_font(GuiFont font)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077{
2078 if (font != NOFONT)
2079 XFreeFont(gui.dpy, (XFontStruct *)font);
2080}
2081
2082#if defined(FEAT_XFONTSET) || defined(PROTO)
2083/*
2084 * If a fontset is not going to be used, free its structure.
2085 */
2086 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002087gui_mch_free_fontset(GuiFontset fontset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088{
2089 if (fontset != NOFONTSET)
2090 XFreeFontSet(gui.dpy, (XFontSet)fontset);
2091}
2092
2093/*
2094 * Load the fontset "name".
2095 * Return a reference to the fontset, or NOFONTSET when failing.
2096 */
2097 GuiFontset
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002098gui_mch_get_fontset(
2099 char_u *name,
2100 int giveErrorIfMissing,
2101 int fixed_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102{
2103 XFontSet fontset;
2104 char **missing, *def_str;
2105 int num_missing;
2106
2107 if (!gui.in_use || name == NULL)
2108 return NOFONTSET;
2109
2110 fontset = XCreateFontSet(gui.dpy, (char *)name, &missing, &num_missing,
2111 &def_str);
2112 if (num_missing > 0)
2113 {
2114 int i;
2115
2116 if (giveErrorIfMissing)
2117 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002118 semsg(_("E250: Fonts for the following charsets are missing in fontset %s:"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 for (i = 0; i < num_missing; i++)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002120 semsg("%s", missing[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 }
2122 XFreeStringList(missing);
2123 }
2124
2125 if (fontset == NULL)
2126 {
2127 if (giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002128 semsg(_(e_fontset), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 return NOFONTSET;
2130 }
2131
2132 if (fixed_width && check_fontset_sanity(fontset) == FAIL)
2133 {
2134 XFreeFontSet(gui.dpy, fontset);
2135 return NOFONTSET;
2136 }
2137 return (GuiFontset)fontset;
2138}
2139
2140/*
2141 * Check if fontset "fs" is fixed width.
2142 */
2143 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002144check_fontset_sanity(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145{
2146 XFontStruct **xfs;
2147 char **font_name;
2148 int fn;
2149 char *base_name;
2150 int i;
2151 int min_width;
2152 int min_font_idx = 0;
2153
2154 base_name = XBaseFontNameListOfFontSet(fs);
2155 fn = XFontsOfFontSet(fs, &xfs, &font_name);
2156 for (i = 0; i < fn; i++)
2157 {
2158 if (xfs[i]->max_bounds.width != xfs[i]->min_bounds.width)
2159 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002160 semsg(_("E252: Fontset name: %s"), base_name);
2161 semsg(_("Font '%s' is not fixed-width"), font_name[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 return FAIL;
2163 }
2164 }
2165 /* scan base font width */
2166 min_width = 32767;
2167 for (i = 0; i < fn; i++)
2168 {
2169 if (xfs[i]->max_bounds.width<min_width)
2170 {
2171 min_width = xfs[i]->max_bounds.width;
2172 min_font_idx = i;
2173 }
2174 }
2175 for (i = 0; i < fn; i++)
2176 {
2177 if ( xfs[i]->max_bounds.width != 2 * min_width
2178 && xfs[i]->max_bounds.width != min_width)
2179 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002180 semsg(_("E253: Fontset name: %s"), base_name);
2181 semsg(_("Font0: %s"), font_name[min_font_idx]);
Bram Moolenaar500f3612019-01-16 22:15:11 +01002182 semsg(_("Font%d: %s"), i, font_name[i]);
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01002183 semsg(_("Font%d width is not twice that of font0"), i);
2184 semsg(_("Font0 width: %d"),
2185 (int)xfs[min_font_idx]->max_bounds.width);
2186 semsg(_("Font%d width: %d"), i, (int)xfs[i]->max_bounds.width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 return FAIL;
2188 }
2189 }
2190 /* it seems ok. Good Luck!! */
2191 return OK;
2192}
2193
2194 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002195fontset_width(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196{
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002197 return XmbTextEscapement(fs, "Vim", 3) / 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198}
2199
2200 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002201fontset_height(
2202 XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203{
2204 XFontSetExtents *extents;
2205
2206 extents = XExtentsOfFontSet(fs);
2207 return extents->max_logical_extent.height;
2208}
2209
2210#if (defined(FONTSET_ALWAYS) && defined(FEAT_GUI_ATHENA) \
2211 && defined(FEAT_MENU)) || defined(PROTO)
2212/*
2213 * Returns the bounding box height around the actual glyph image of all
2214 * characters in all fonts of the fontset.
2215 */
2216 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002217fontset_height2(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218{
2219 XFontSetExtents *extents;
2220
2221 extents = XExtentsOfFontSet(fs);
2222 return extents->max_ink_extent.height;
2223}
2224#endif
2225
2226/* NOT USED YET
2227 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002228fontset_descent(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229{
2230 XFontSetExtents *extents;
2231
2232 extents = XExtentsOfFontSet (fs);
2233 return extents->max_logical_extent.height + extents->max_logical_extent.y;
2234}
2235*/
2236
2237 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002238fontset_ascent(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239{
2240 XFontSetExtents *extents;
2241
2242 extents = XExtentsOfFontSet(fs);
2243 return -extents->max_logical_extent.y;
2244}
2245
2246#endif /* FEAT_XFONTSET */
2247
2248/*
2249 * Return the Pixel value (color) for the given color name.
2250 * Return INVALCOLOR for error.
2251 */
2252 guicolor_T
Bram Moolenaar46582282016-07-23 14:35:12 +02002253gui_mch_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254{
Bram Moolenaard23a8232018-02-10 18:45:26 +01002255 guicolor_T requested;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256
2257 /* can't do this when GUI not running */
Bram Moolenaar46582282016-07-23 14:35:12 +02002258 if (!gui.in_use || name == NULL || *name == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 return INVALCOLOR;
2260
Bram Moolenaar46582282016-07-23 14:35:12 +02002261 requested = gui_get_color_cmn(name);
2262 if (requested == INVALCOLOR)
2263 return INVALCOLOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002265 return gui_mch_get_rgb_color(
Bram Moolenaar46582282016-07-23 14:35:12 +02002266 (requested & 0xff0000) >> 16,
2267 (requested & 0xff00) >> 8,
2268 requested & 0xff);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002269}
2270
2271/*
2272 * Return the Pixel value (color) for the given RGB values.
2273 * Return INVALCOLOR for error.
2274 */
2275 guicolor_T
2276gui_mch_get_rgb_color(int r, int g, int b)
2277{
Bram Moolenaard23a8232018-02-10 18:45:26 +01002278 XColor available;
Bram Moolenaard60547b2017-07-24 20:15:30 +02002279 Colormap colormap;
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002280
Bram Moolenaar778df2a2018-05-06 19:19:36 +02002281/* Using XParseColor() is very slow, put rgb in XColor directly.
2282
2283 char spec[8]; // space enough to hold "#RRGGBB"
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002284 vim_snprintf(spec, sizeof(spec), "#%.2x%.2x%.2x", r, g, b);
Bram Moolenaar46582282016-07-23 14:35:12 +02002285 if (XParseColor(gui.dpy, colormap, (char *)spec, &available) != 0
2286 && XAllocColor(gui.dpy, colormap, &available) != 0)
2287 return (guicolor_T)available.pixel;
Bram Moolenaar778df2a2018-05-06 19:19:36 +02002288*/
2289 colormap = DefaultColormap(gui.dpy, DefaultScreen(gui.dpy));
2290 vim_memset(&available, 0, sizeof(XColor));
2291 available.red = r << 8;
2292 available.green = g << 8;
2293 available.blue = b << 8;
2294 if (XAllocColor(gui.dpy, colormap, &available) != 0)
2295 return (guicolor_T)available.pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296
2297 return INVALCOLOR;
2298}
2299
2300/*
Bram Moolenaarfb269802005-03-15 22:46:30 +00002301 * Set the current text foreground color.
2302 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002304gui_mch_set_fg_color(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305{
2306 if (color != prev_fg_color)
2307 {
2308 XSetForeground(gui.dpy, gui.text_gc, (Pixel)color);
2309 prev_fg_color = color;
2310 }
2311}
2312
2313/*
2314 * Set the current text background color.
2315 */
2316 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002317gui_mch_set_bg_color(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318{
2319 if (color != prev_bg_color)
2320 {
2321 XSetBackground(gui.dpy, gui.text_gc, (Pixel)color);
2322 prev_bg_color = color;
2323 }
2324}
2325
2326/*
Bram Moolenaarfb269802005-03-15 22:46:30 +00002327 * Set the current text special color.
2328 */
2329 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002330gui_mch_set_sp_color(guicolor_T color)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002331{
2332 prev_sp_color = color;
2333}
2334
2335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 * create a mouse pointer that is blank
2337 */
2338 static Cursor
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002339gui_x11_create_blank_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340{
2341 Pixmap blank_pixmap = XCreatePixmap(gui.dpy, gui.wid, 1, 1, 1);
2342 GC gc = XCreateGC(gui.dpy, blank_pixmap, (unsigned long)0, (XGCValues*)0);
2343 XDrawPoint(gui.dpy, blank_pixmap, gc, 0, 0);
2344 XFreeGC(gui.dpy, gc);
2345 return XCreatePixmapCursor(gui.dpy, blank_pixmap, blank_pixmap,
2346 (XColor*)&gui.norm_pixel, (XColor*)&gui.norm_pixel, 0, 0);
2347}
2348
Bram Moolenaarfb269802005-03-15 22:46:30 +00002349/*
2350 * Draw a curled line at the bottom of the character cell.
2351 */
2352 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002353draw_curl(int row, int col, int cells)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002354{
2355 int i;
2356 int offset;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002357 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarfb269802005-03-15 22:46:30 +00002358
2359 XSetForeground(gui.dpy, gui.text_gc, prev_sp_color);
2360 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
2361 {
2362 offset = val[i % 8];
2363 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, i,
2364 FILL_Y(row + 1) - 1 - offset);
2365 }
2366 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
2367}
2368
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002370gui_mch_draw_string(
2371 int row,
2372 int col,
2373 char_u *s,
2374 int len,
2375 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376{
2377 int cells = len;
2378#ifdef FEAT_MBYTE
Bram Moolenaara3227e22006-03-08 21:32:40 +00002379 static void *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 static int buflen = 0;
2381 char_u *p;
2382 int wlen = 0;
2383 int c;
2384
2385 if (enc_utf8)
2386 {
2387 /* Convert UTF-8 byte sequence to 16 bit characters for the X
2388 * functions. Need a buffer for the 16 bit characters. Keep it
2389 * between calls, because allocating it each time is slow. */
2390 if (buflen < len)
2391 {
2392 XtFree((char *)buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002393 buf = (void *)XtMalloc(len * (sizeof(XChar2b) < sizeof(wchar_t)
2394 ? sizeof(wchar_t) : sizeof(XChar2b)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 buflen = len;
2396 }
2397 p = s;
2398 cells = 0;
2399 while (p < s + len)
2400 {
2401 c = utf_ptr2char(p);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002402# ifdef FEAT_XFONTSET
2403 if (current_fontset != NULL)
2404 {
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002405# ifdef SMALL_WCHAR_T
2406 if (c >= 0x10000)
Bram Moolenaara3227e22006-03-08 21:32:40 +00002407 c = 0xbf; /* show chars > 0xffff as ? */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002408# endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00002409 ((wchar_t *)buf)[wlen] = c;
2410 }
2411 else
2412# endif
2413 {
2414 if (c >= 0x10000)
2415 c = 0xbf; /* show chars > 0xffff as ? */
2416 ((XChar2b *)buf)[wlen].byte1 = (unsigned)c >> 8;
2417 ((XChar2b *)buf)[wlen].byte2 = c;
2418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 ++wlen;
2420 cells += utf_char2cells(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002421 p += utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 }
2423 }
2424 else if (has_mbyte)
2425 {
2426 cells = 0;
2427 for (p = s; p < s + len; )
2428 {
2429 cells += ptr2cells(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002430 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 }
2432 }
2433
2434#endif
2435
2436#ifdef FEAT_XFONTSET
2437 if (current_fontset != NULL)
2438 {
2439 /* Setup a clip rectangle to avoid spilling over in the next or
2440 * previous line. This is apparently needed for some fonts which are
2441 * used in a fontset. */
2442 XRectangle clip;
2443
2444 clip.x = 0;
2445 clip.y = 0;
2446 clip.height = gui.char_height;
2447 clip.width = gui.char_width * cells + 1;
2448 XSetClipRectangles(gui.dpy, gui.text_gc, FILL_X(col), FILL_Y(row),
2449 &clip, 1, Unsorted);
2450 }
2451#endif
2452
2453 if (flags & DRAW_TRANSP)
2454 {
2455#ifdef FEAT_MBYTE
2456 if (enc_utf8)
2457 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2458 TEXT_Y(row), buf, wlen);
2459 else
2460#endif
2461 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2462 TEXT_Y(row), (char *)s, len);
2463 }
2464 else if (p_linespace != 0
2465#ifdef FEAT_XFONTSET
2466 || current_fontset != NULL
2467#endif
2468 )
2469 {
2470 XSetForeground(gui.dpy, gui.text_gc, prev_bg_color);
2471 XFillRectangle(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
2472 FILL_Y(row), gui.char_width * cells, gui.char_height);
2473 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
Bram Moolenaarfb269802005-03-15 22:46:30 +00002474
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475#ifdef FEAT_MBYTE
2476 if (enc_utf8)
2477 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2478 TEXT_Y(row), buf, wlen);
2479 else
2480#endif
2481 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2482 TEXT_Y(row), (char *)s, len);
2483 }
2484 else
2485 {
2486 /* XmbDrawImageString has bug, don't use it for fontset. */
2487#ifdef FEAT_MBYTE
2488 if (enc_utf8)
2489 XDrawImageString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2490 TEXT_Y(row), buf, wlen);
2491 else
2492#endif
2493 XDrawImageString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2494 TEXT_Y(row), (char *)s, len);
2495 }
2496
2497 /* Bold trick: draw the text again with a one-pixel offset. */
2498 if (flags & DRAW_BOLD)
2499 {
2500#ifdef FEAT_MBYTE
2501 if (enc_utf8)
2502 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col) + 1,
2503 TEXT_Y(row), buf, wlen);
2504 else
2505#endif
2506 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col) + 1,
2507 TEXT_Y(row), (char *)s, len);
2508 }
2509
Bram Moolenaarfb269802005-03-15 22:46:30 +00002510 /* Undercurl: draw curl at the bottom of the character cell. */
2511 if (flags & DRAW_UNDERC)
2512 draw_curl(row, col, cells);
2513
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 /* Underline: draw a line at the bottom of the character cell. */
2515 if (flags & DRAW_UNDERL)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002516 {
2517 int y = FILL_Y(row + 1) - 1;
2518
2519 /* When p_linespace is 0, overwrite the bottom row of pixels.
2520 * Otherwise put the line just below the character. */
2521 if (p_linespace > 1)
2522 y -= p_linespace - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 XDrawLine(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
Bram Moolenaarfb269802005-03-15 22:46:30 +00002524 y, FILL_X(col + cells) - 1, y);
2525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002527 if (flags & DRAW_STRIKE)
2528 {
2529 int y = FILL_Y(row + 1) - gui.char_height/2;
2530
2531 XSetForeground(gui.dpy, gui.text_gc, prev_sp_color);
2532 XDrawLine(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
2533 y, FILL_X(col + cells) - 1, y);
2534 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
2535 }
2536
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537#ifdef FEAT_XFONTSET
2538 if (current_fontset != NULL)
2539 XSetClipMask(gui.dpy, gui.text_gc, None);
2540#endif
2541}
2542
2543/*
2544 * Return OK if the key with the termcap name "name" is supported.
2545 */
2546 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002547gui_mch_haskey(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548{
2549 int i;
2550
2551 for (i = 0; special_keys[i].key_sym != (KeySym)0; i++)
2552 if (name[0] == special_keys[i].vim_code0 &&
2553 name[1] == special_keys[i].vim_code1)
2554 return OK;
2555 return FAIL;
2556}
2557
2558/*
2559 * Return the text window-id and display. Only required for X-based GUI's
2560 */
2561 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002562gui_get_x11_windis(Window *win, Display **dis)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563{
2564 *win = XtWindow(vimShell);
2565 *dis = gui.dpy;
2566 return OK;
2567}
2568
2569 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002570gui_mch_beep(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571{
2572 XBell(gui.dpy, 0);
2573}
2574
2575 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002576gui_mch_flash(int msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577{
2578 /* Do a visual beep by reversing the foreground and background colors */
2579 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc, 0, 0,
2580 FILL_X((int)Columns) + gui.border_offset,
2581 FILL_Y((int)Rows) + gui.border_offset);
2582 XSync(gui.dpy, False);
2583 ui_delay((long)msec, TRUE); /* wait for a few msec */
2584 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc, 0, 0,
2585 FILL_X((int)Columns) + gui.border_offset,
2586 FILL_Y((int)Rows) + gui.border_offset);
2587}
2588
2589/*
2590 * Invert a rectangle from row r, column c, for nr rows and nc columns.
2591 */
2592 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002593gui_mch_invert_rectangle(
2594 int r,
2595 int c,
2596 int nr,
2597 int nc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598{
2599 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc,
2600 FILL_X(c), FILL_Y(r), (nc) * gui.char_width, (nr) * gui.char_height);
2601}
2602
2603/*
2604 * Iconify the GUI window.
2605 */
2606 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002607gui_mch_iconify(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608{
2609 XIconifyWindow(gui.dpy, XtWindow(vimShell), DefaultScreen(gui.dpy));
2610}
2611
2612#if defined(FEAT_EVAL) || defined(PROTO)
2613/*
2614 * Bring the Vim window to the foreground.
2615 */
2616 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002617gui_mch_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618{
2619 XMapRaised(gui.dpy, XtWindow(vimShell));
2620}
2621#endif
2622
2623/*
2624 * Draw a cursor without focus.
2625 */
2626 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002627gui_mch_draw_hollow_cursor(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628{
2629 int w = 1;
2630
2631#ifdef FEAT_MBYTE
2632 if (mb_lefthalve(gui.row, gui.col))
2633 w = 2;
2634#endif
2635 gui_mch_set_fg_color(color);
2636 XDrawRectangle(gui.dpy, gui.wid, gui.text_gc, FILL_X(gui.col),
2637 FILL_Y(gui.row), w * gui.char_width - 1, gui.char_height - 1);
2638}
2639
2640/*
2641 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
2642 * color "color".
2643 */
2644 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002645gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646{
2647 gui_mch_set_fg_color(color);
2648
2649 XFillRectangle(gui.dpy, gui.wid, gui.text_gc,
2650#ifdef FEAT_RIGHTLEFT
2651 /* vertical line should be on the right of current point */
2652 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
2653#endif
2654 FILL_X(gui.col),
2655 FILL_Y(gui.row) + gui.char_height - h,
2656 w, h);
2657}
2658
2659/*
2660 * Catch up with any queued X events. This may put keyboard input into the
2661 * input buffer, call resize call-backs, trigger timers etc. If there is
2662 * nothing in the X event queue (& no timers pending), then we return
2663 * immediately.
2664 */
2665 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002666gui_mch_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667{
2668 XtInputMask mask, desired;
2669
2670#ifdef ALT_X_INPUT
2671 if (suppress_alternate_input)
2672 desired = (XtIMXEvent | XtIMTimer);
2673 else
2674#endif
2675 desired = (XtIMAll);
2676 while ((mask = XtAppPending(app_context)) && (mask & desired)
2677 && !vim_is_input_buf_full())
2678 XtAppProcessEvent(app_context, desired);
2679}
2680
2681/*
2682 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2683 * from the keyboard.
2684 * wtime == -1 Wait forever.
2685 * wtime == 0 This should never happen.
2686 * wtime > 0 Wait wtime milliseconds for a character.
2687 * Returns OK if a character was found to be available within the given time,
2688 * or FAIL otherwise.
2689 */
2690 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002691gui_mch_wait_for_chars(long wtime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692{
Bram Moolenaar4231da42016-06-02 14:30:04 +02002693 int focus;
2694 int retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695
2696 /*
2697 * Make this static, in case gui_x11_timer_cb is called after leaving
2698 * this function (otherwise a random value on the stack may be changed).
2699 */
2700 static int timed_out;
2701 XtIntervalId timer = (XtIntervalId)0;
2702 XtInputMask desired;
Bram Moolenaar1dccf632017-08-27 17:38:27 +02002703#ifdef FEAT_JOB_CHANNEL
2704 XtIntervalId channel_timer = (XtIntervalId)0;
2705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706
2707 timed_out = FALSE;
2708
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 if (wtime > 0)
2710 timer = XtAppAddTimeOut(app_context, (long_u)wtime, gui_x11_timer_cb,
2711 &timed_out);
Bram Moolenaar1dccf632017-08-27 17:38:27 +02002712#ifdef FEAT_JOB_CHANNEL
2713 /* If there is a channel with the keep_open flag we need to poll for input
2714 * on them. */
2715 if (channel_any_keep_open())
2716 channel_timer = XtAppAddTimeOut(app_context, (long_u)20,
2717 channel_poll_cb, (XtPointer)&channel_timer);
2718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
2720 focus = gui.in_focus;
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01002721 desired = (XtIMAll);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 while (!timed_out)
2723 {
2724 /* Stop or start blinking when focus changes */
2725 if (gui.in_focus != focus)
2726 {
2727 if (gui.in_focus)
2728 gui_mch_start_blink();
2729 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002730 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 focus = gui.in_focus;
2732 }
2733
Bram Moolenaar93c88e02015-09-15 14:12:05 +02002734#ifdef MESSAGE_QUEUE
Bram Moolenaar4231da42016-06-02 14:30:04 +02002735# ifdef FEAT_TIMERS
2736 did_add_timer = FALSE;
2737# endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02002738 parse_queued_messages();
Bram Moolenaar4231da42016-06-02 14:30:04 +02002739# ifdef FEAT_TIMERS
2740 if (did_add_timer)
2741 /* Need to recompute the waiting time. */
2742 break;
2743# endif
Bram Moolenaar03531f72010-11-16 15:04:57 +01002744#endif
2745
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 /*
2747 * Don't use gui_mch_update() because then we will spin-lock until a
2748 * char arrives, instead we use XtAppProcessEvent() to hang until an
2749 * event arrives. No need to check for input_buf_full because we are
2750 * returning as soon as it contains a single char. Note that
2751 * XtAppNextEvent() may not be used because it will not return after a
2752 * timer event has arrived -- webb
2753 */
2754 XtAppProcessEvent(app_context, desired);
2755
2756 if (input_available())
2757 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002758 retval = OK;
2759 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
2761 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002762
2763 if (timer != (XtIntervalId)0 && !timed_out)
2764 XtRemoveTimeOut(timer);
Bram Moolenaar1dccf632017-08-27 17:38:27 +02002765#ifdef FEAT_JOB_CHANNEL
2766 if (channel_timer != (XtIntervalId)0)
2767 XtRemoveTimeOut(channel_timer);
2768#endif
Bram Moolenaar4231da42016-06-02 14:30:04 +02002769
2770 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771}
2772
2773/*
2774 * Output routines.
2775 */
2776
2777/* Flush any output to the screen */
2778 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002779gui_mch_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780{
2781 XFlush(gui.dpy);
2782}
2783
2784/*
2785 * Clear a rectangular region of the screen from text pos (row1, col1) to
2786 * (row2, col2) inclusive.
2787 */
2788 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002789gui_mch_clear_block(
2790 int row1,
2791 int col1,
2792 int row2,
2793 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794{
2795 int x;
2796
2797 x = FILL_X(col1);
2798
2799 /* Clear one extra pixel at the far right, for when bold characters have
2800 * spilled over to the next column. */
2801 XFillRectangle(gui.dpy, gui.wid, gui.back_gc, x, FILL_Y(row1),
2802 (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1),
2803 (row2 - row1 + 1) * gui.char_height);
2804}
2805
2806 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002807gui_mch_clear_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808{
2809 XClearArea(gui.dpy, gui.wid, 0, 0, 0, 0, False);
2810}
2811
2812/*
2813 * Delete the given number of lines from the given row, scrolling up any
2814 * text further down within the scroll region.
2815 */
2816 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002817gui_mch_delete_lines(int row, int num_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818{
2819 if (gui.visibility == VisibilityFullyObscured)
2820 return; /* Can't see the window */
2821
2822 /* copy one extra pixel at the far right, for when bold has spilled
2823 * over */
2824 XCopyArea(gui.dpy, gui.wid, gui.wid, gui.text_gc,
2825 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
2826 gui.char_width * (gui.scroll_region_right - gui.scroll_region_left + 1)
2827 + (gui.scroll_region_right == Columns - 1),
2828 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1),
2829 FILL_X(gui.scroll_region_left), FILL_Y(row));
2830
2831 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
2832 gui.scroll_region_left,
2833 gui.scroll_region_bot, gui.scroll_region_right);
2834 gui_x11_check_copy_area();
2835}
2836
2837/*
2838 * Insert the given number of lines before the given row, scrolling down any
2839 * following text within the scroll region.
2840 */
2841 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002842gui_mch_insert_lines(int row, int num_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843{
2844 if (gui.visibility == VisibilityFullyObscured)
2845 return; /* Can't see the window */
2846
2847 /* copy one extra pixel at the far right, for when bold has spilled
2848 * over */
2849 XCopyArea(gui.dpy, gui.wid, gui.wid, gui.text_gc,
2850 FILL_X(gui.scroll_region_left), FILL_Y(row),
2851 gui.char_width * (gui.scroll_region_right - gui.scroll_region_left + 1)
2852 + (gui.scroll_region_right == Columns - 1),
2853 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1),
2854 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines));
2855
2856 gui_clear_block(row, gui.scroll_region_left,
2857 row + num_lines - 1, gui.scroll_region_right);
2858 gui_x11_check_copy_area();
2859}
2860
2861/*
2862 * Update the region revealed by scrolling up/down.
2863 */
2864 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002865gui_x11_check_copy_area(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866{
2867 XEvent event;
2868 XGraphicsExposeEvent *gevent;
2869
2870 if (gui.visibility != VisibilityPartiallyObscured)
2871 return;
2872
2873 XFlush(gui.dpy);
2874
2875 /* Wait to check whether the scroll worked or not */
2876 for (;;)
2877 {
2878 if (XCheckTypedEvent(gui.dpy, NoExpose, &event))
2879 return; /* The scroll worked. */
2880
2881 if (XCheckTypedEvent(gui.dpy, GraphicsExpose, &event))
2882 {
2883 gevent = (XGraphicsExposeEvent *)&event;
2884 gui_redraw(gevent->x, gevent->y, gevent->width, gevent->height);
2885 if (gevent->count == 0)
2886 return; /* This was the last expose event */
2887 }
2888 XSync(gui.dpy, False);
2889 }
2890}
2891
2892/*
2893 * X Selection stuff, for cutting and pasting text to other windows.
2894 */
2895
2896 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002897clip_mch_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898{
2899 clip_x11_lose_selection(vimShell, cbd);
2900}
2901
2902 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002903clip_mch_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904{
2905 return clip_x11_own_selection(vimShell, cbd);
2906}
2907
2908 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002909clip_mch_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910{
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002911 clip_x11_request_selection(vimShell, gui.dpy, cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912}
2913
2914 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002915clip_mch_set_selection(
2916 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917{
2918 clip_x11_set_selection(cbd);
2919}
2920
2921#if defined(FEAT_MENU) || defined(PROTO)
2922/*
2923 * Menu stuff.
2924 */
2925
2926/*
2927 * Make a menu either grey or not grey.
2928 */
2929 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002930gui_mch_menu_grey(vimmenu_T *menu, int grey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931{
2932 if (menu->id != (Widget)0)
2933 {
2934 gui_mch_menu_hidden(menu, False);
2935 if (grey
2936#ifdef FEAT_GUI_MOTIF
2937 || !menu->sensitive
2938#endif
2939 )
2940 XtSetSensitive(menu->id, False);
2941 else
2942 XtSetSensitive(menu->id, True);
2943 }
2944}
2945
2946/*
2947 * Make menu item hidden or not hidden
2948 */
2949 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002950gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951{
2952 if (menu->id != (Widget)0)
2953 {
2954 if (hidden)
2955 XtUnmanageChild(menu->id);
2956 else
2957 XtManageChild(menu->id);
2958 }
2959}
2960
2961/*
2962 * This is called after setting all the menus to grey/hidden or not.
2963 */
2964 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002965gui_mch_draw_menubar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966{
2967 /* Nothing to do in X */
2968}
2969
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002971gui_x11_menu_cb(
2972 Widget w UNUSED,
2973 XtPointer client_data,
2974 XtPointer call_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975{
2976 gui_menu_cb((vimmenu_T *)client_data);
2977}
2978
2979#endif /* FEAT_MENU */
2980
2981
2982
2983/*
2984 * Function called when window closed. Works like ":qa".
2985 * Should put up a requester!
2986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002988gui_x11_wm_protocol_handler(
2989 Widget w UNUSED,
2990 XtPointer client_data UNUSED,
2991 XEvent *event,
2992 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993{
2994 /*
2995 * Only deal with Client messages.
2996 */
2997 if (event->type != ClientMessage)
2998 return;
2999
3000 /*
3001 * The WM_SAVE_YOURSELF event arrives when the window manager wants to
3002 * exit. That can be cancelled though, thus Vim shouldn't exit here.
3003 * Just sync our swap files.
3004 */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003005 if ((Atom)((XClientMessageEvent *)event)->data.l[0] ==
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 wm_atoms[SAVE_YOURSELF_IDX])
3007 {
3008 out_flush();
3009 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
3010
3011 /* Set the window's WM_COMMAND property, to let the window manager
3012 * know we are done saving ourselves. We don't want to be restarted,
3013 * thus set argv to NULL. */
3014 XSetCommand(gui.dpy, XtWindow(vimShell), NULL, 0);
3015 return;
3016 }
3017
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003018 if ((Atom)((XClientMessageEvent *)event)->data.l[0] !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 wm_atoms[DELETE_WINDOW_IDX])
3020 return;
3021
3022 gui_shell_closed();
3023}
3024
3025#ifdef FEAT_CLIENTSERVER
3026/*
3027 * Function called when property changed. Check for incoming commands
3028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003030gui_x11_send_event_handler(
3031 Widget w UNUSED,
3032 XtPointer client_data UNUSED,
3033 XEvent *event,
3034 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035{
3036 XPropertyEvent *e = (XPropertyEvent *) event;
3037
3038 if (e->type == PropertyNotify && e->window == commWindow
3039 && e->atom == commProperty && e->state == PropertyNewValue)
3040 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003041 serverEventProc(gui.dpy, event, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 }
3043}
3044#endif
3045
3046/*
3047 * Cursor blink functions.
3048 *
3049 * This is a simple state machine:
3050 * BLINK_NONE not blinking at all
3051 * BLINK_OFF blinking, cursor is not shown
3052 * BLINK_ON blinking, cursor is shown
3053 */
3054
3055#define BLINK_NONE 0
3056#define BLINK_OFF 1
3057#define BLINK_ON 2
3058
3059static int blink_state = BLINK_NONE;
3060static long_u blink_waittime = 700;
3061static long_u blink_ontime = 400;
3062static long_u blink_offtime = 250;
3063static XtIntervalId blink_timer = (XtIntervalId)0;
3064
Bram Moolenaar703a8042016-06-04 16:24:32 +02003065 int
3066gui_mch_is_blinking(void)
3067{
3068 return blink_state != BLINK_NONE;
3069}
3070
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +02003071 int
3072gui_mch_is_blink_off(void)
3073{
3074 return blink_state == BLINK_OFF;
3075}
3076
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 void
Bram Moolenaar02fdaea2016-01-30 18:13:55 +01003078gui_mch_set_blinking(long waittime, long on, long off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079{
3080 blink_waittime = waittime;
3081 blink_ontime = on;
3082 blink_offtime = off;
3083}
3084
3085/*
3086 * Stop the cursor blinking. Show the cursor if it wasn't shown.
3087 */
3088 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01003089gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
3091 if (blink_timer != (XtIntervalId)0)
3092 {
3093 XtRemoveTimeOut(blink_timer);
3094 blink_timer = (XtIntervalId)0;
3095 }
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01003096 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 gui_update_cursor(TRUE, FALSE);
3098 blink_state = BLINK_NONE;
3099}
3100
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003102gui_x11_blink_cb(
3103 XtPointer timed_out UNUSED,
3104 XtIntervalId *interval_id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
3106 if (blink_state == BLINK_ON)
3107 {
3108 gui_undraw_cursor();
3109 blink_state = BLINK_OFF;
3110 blink_timer = XtAppAddTimeOut(app_context, blink_offtime,
3111 gui_x11_blink_cb, NULL);
3112 }
3113 else
3114 {
3115 gui_update_cursor(TRUE, FALSE);
3116 blink_state = BLINK_ON;
3117 blink_timer = XtAppAddTimeOut(app_context, blink_ontime,
3118 gui_x11_blink_cb, NULL);
3119 }
3120}
3121
3122/*
Bram Moolenaar1dccf632017-08-27 17:38:27 +02003123 * Start the cursor blinking. If it was already blinking, this restarts the
3124 * waiting time and shows the cursor.
3125 */
3126 void
3127gui_mch_start_blink(void)
3128{
3129 if (blink_timer != (XtIntervalId)0)
3130 XtRemoveTimeOut(blink_timer);
3131 /* Only switch blinking on if none of the times is zero */
3132 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
3133 {
3134 blink_timer = XtAppAddTimeOut(app_context, blink_waittime,
3135 gui_x11_blink_cb, NULL);
3136 blink_state = BLINK_ON;
3137 gui_update_cursor(TRUE, FALSE);
3138 }
3139}
3140
3141/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 * Return the RGB value of a pixel as a long.
3143 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003144 guicolor_T
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003145gui_mch_get_rgb(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146{
3147 XColor xc;
3148 Colormap colormap;
3149
3150 colormap = DefaultColormap(gui.dpy, XDefaultScreen(gui.dpy));
3151 xc.pixel = pixel;
3152 XQueryColor(gui.dpy, colormap, &xc);
3153
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003154 return (guicolor_T)(((xc.red & 0xff00) << 8) + (xc.green & 0xff00)
3155 + ((unsigned)xc.blue >> 8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156}
3157
3158/*
3159 * Add the callback functions.
3160 */
3161 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003162gui_x11_callbacks(Widget textArea, Widget vimForm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163{
3164 XtAddEventHandler(textArea, VisibilityChangeMask, FALSE,
3165 gui_x11_visibility_cb, (XtPointer)0);
3166
3167 XtAddEventHandler(textArea, ExposureMask, FALSE, gui_x11_expose_cb,
3168 (XtPointer)0);
3169
3170 XtAddEventHandler(vimShell, StructureNotifyMask, FALSE,
3171 gui_x11_resize_window_cb, (XtPointer)0);
3172
3173 XtAddEventHandler(vimShell, FocusChangeMask, FALSE, gui_x11_focus_change_cb,
3174 (XtPointer)0);
3175 /*
3176 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3177 * Only needed for some window managers.
3178 */
3179 if (vim_strchr(p_go, GO_POINTER) != NULL)
3180 {
3181 XtAddEventHandler(vimShell, LeaveWindowMask, FALSE, gui_x11_leave_cb,
3182 (XtPointer)0);
3183 XtAddEventHandler(textArea, LeaveWindowMask, FALSE, gui_x11_leave_cb,
3184 (XtPointer)0);
3185 XtAddEventHandler(textArea, EnterWindowMask, FALSE, gui_x11_enter_cb,
3186 (XtPointer)0);
3187 XtAddEventHandler(vimShell, EnterWindowMask, FALSE, gui_x11_enter_cb,
3188 (XtPointer)0);
3189 }
3190
3191 XtAddEventHandler(vimForm, KeyPressMask, FALSE, gui_x11_key_hit_cb,
3192 (XtPointer)0);
3193 XtAddEventHandler(textArea, KeyPressMask, FALSE, gui_x11_key_hit_cb,
3194 (XtPointer)0);
3195
3196 /* get pointer moved events from scrollbar, needed for 'mousefocus' */
3197 XtAddEventHandler(vimForm, PointerMotionMask,
3198 FALSE, gui_x11_mouse_cb, (XtPointer)1);
3199 XtAddEventHandler(textArea, ButtonPressMask | ButtonReleaseMask |
3200 ButtonMotionMask | PointerMotionMask,
3201 FALSE, gui_x11_mouse_cb, (XtPointer)0);
3202}
3203
3204/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003205 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003207 void
3208gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209{
3210 int rootx, rooty, winx, winy;
3211 Window root, child;
3212 unsigned int mask;
3213
3214 if (gui.wid && XQueryPointer(gui.dpy, gui.wid, &root, &child,
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003215 &rootx, &rooty, &winx, &winy, &mask)) {
3216 *x = winx;
3217 *y = winy;
3218 } else {
3219 *x = -1;
3220 *y = -1;
3221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222}
3223
3224 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003225gui_mch_setmouse(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226{
3227 if (gui.wid)
3228 XWarpPointer(gui.dpy, (Window)0, gui.wid, 0, 0, 0, 0, x, y);
3229}
3230
3231#if (defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)) || defined(PROTO)
3232 XButtonPressedEvent *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003233gui_x11_get_last_mouse_event(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234{
3235 return &last_mouse_event;
3236}
3237#endif
3238
3239#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
3240
3241/* Signs are currently always 2 chars wide. Hopefully the font is big enough
3242 * to provide room for the bitmap! */
3243# define SIGN_WIDTH (gui.char_width * 2)
3244
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003246gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247{
3248 XImage *sign;
3249
3250 if (gui.in_use && (sign = (XImage *)sign_get_image(typenr)) != NULL)
3251 {
3252 XClearArea(gui.dpy, gui.wid, TEXT_X(col), TEXT_Y(row) - sign->height,
3253 SIGN_WIDTH, gui.char_height, FALSE);
3254 XPutImage(gui.dpy, gui.wid, gui.text_gc, sign, 0, 0,
3255 TEXT_X(col) + (SIGN_WIDTH - sign->width) / 2,
3256 TEXT_Y(row) - sign->height,
3257 sign->width, sign->height);
3258 }
3259}
3260
3261 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003262gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263{
3264 XpmAttributes attrs;
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003265 XImage *sign = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 int status;
3267
3268 /*
3269 * Setup the color substitution table.
3270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 if (signfile[0] != NUL && signfile[0] != '-')
3272 {
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003273 XpmColorSymbol color[5] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 {
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003275 {"none", NULL, 0},
3276 {"iconColor1", NULL, 0},
3277 {"bottomShadowColor", NULL, 0},
3278 {"topShadowColor", NULL, 0},
3279 {"selectColor", NULL, 0}
3280 };
3281 attrs.valuemask = XpmColorSymbols;
3282 attrs.numsymbols = 2;
3283 attrs.colorsymbols = color;
3284 attrs.colorsymbols[0].pixel = gui.back_pixel;
3285 attrs.colorsymbols[1].pixel = gui.norm_pixel;
3286 status = XpmReadFileToImage(gui.dpy, (char *)signfile,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 &sign, NULL, &attrs);
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003288 if (status == 0)
3289 {
3290 /* Sign width is fixed at two columns now.
3291 if (sign->width > gui.sign_width)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003292 gui.sign_width = sign->width + 8; */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 }
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003294 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003295 emsg(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 }
3297
3298 return (void *)sign;
3299}
3300
3301 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003302gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303{
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003304 XDestroyImage((XImage*)sign);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305}
3306#endif
3307
3308
3309#ifdef FEAT_MOUSESHAPE
3310/* The last set mouse pointer shape is remembered, to be used when it goes
3311 * from hidden to not hidden. */
3312static int last_shape = 0;
3313#endif
3314
3315/*
3316 * Use the blank mouse pointer or not.
3317 */
3318 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003319gui_mch_mousehide(
3320 int hide) /* TRUE = use blank ptr, FALSE = use parent ptr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321{
3322 if (gui.pointer_hidden != hide)
3323 {
3324 gui.pointer_hidden = hide;
3325 if (hide)
3326 XDefineCursor(gui.dpy, gui.wid, gui.blank_pointer);
3327 else
3328#ifdef FEAT_MOUSESHAPE
3329 mch_set_mouse_shape(last_shape);
3330#else
3331 XUndefineCursor(gui.dpy, gui.wid);
3332#endif
3333 }
3334}
3335
3336#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3337
3338/* Table for shape IDs. Keep in sync with the mshape_names[] table in
3339 * misc2.c! */
3340static int mshape_ids[] =
3341{
3342 XC_left_ptr, /* arrow */
3343 0, /* blank */
3344 XC_xterm, /* beam */
3345 XC_sb_v_double_arrow, /* updown */
3346 XC_sizing, /* udsizing */
3347 XC_sb_h_double_arrow, /* leftright */
3348 XC_sizing, /* lrsizing */
3349 XC_watch, /* busy */
3350 XC_X_cursor, /* no */
3351 XC_crosshair, /* crosshair */
3352 XC_hand1, /* hand1 */
3353 XC_hand2, /* hand2 */
3354 XC_pencil, /* pencil */
3355 XC_question_arrow, /* question */
3356 XC_right_ptr, /* right-arrow */
3357 XC_center_ptr, /* up-arrow */
3358 XC_left_ptr /* last one */
3359};
3360
3361 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003362mch_set_mouse_shape(int shape)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363{
3364 int id;
3365
3366 if (!gui.in_use)
3367 return;
3368
3369 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
3370 XDefineCursor(gui.dpy, gui.wid, gui.blank_pointer);
3371 else
3372 {
3373 if (shape >= MSHAPE_NUMBERED)
3374 {
3375 id = shape - MSHAPE_NUMBERED;
3376 if (id >= XC_num_glyphs)
3377 id = XC_left_ptr;
3378 else
3379 id &= ~1; /* they are always even (why?) */
3380 }
3381 else
3382 id = mshape_ids[shape];
3383
3384 XDefineCursor(gui.dpy, gui.wid, XCreateFontCursor(gui.dpy, id));
3385 }
3386 if (shape != MSHAPE_HIDE)
3387 last_shape = shape;
3388}
3389#endif
3390
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003391#if (defined(FEAT_TOOLBAR) && defined(FEAT_BEVAL_GUI)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392/*
3393 * Set the balloon-eval used for the tooltip of a toolbar menu item.
3394 * The check for a non-toolbar item was added, because there is a crash when
3395 * passing a normal menu item here. Can't explain that, but better avoid it.
3396 */
3397 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003398gui_mch_menu_set_tip(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399{
3400 if (menu->id != NULL && menu->parent != NULL
3401 && menu_is_toolbar(menu->parent->name))
3402 {
3403 /* Always destroy and create the balloon, in case the string was
3404 * changed. */
3405 if (menu->tip != NULL)
3406 {
3407 gui_mch_destroy_beval_area(menu->tip);
3408 menu->tip = NULL;
3409 }
3410 if (menu->strings[MENU_INDEX_TIP] != NULL)
3411 menu->tip = gui_mch_create_beval_area(
3412 menu->id,
3413 menu->strings[MENU_INDEX_TIP],
3414 NULL,
3415 NULL);
3416 }
3417}
3418#endif