blob: 7a5cae03ec6374d8b21e415ab0d7023babafeb00 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI/Motif support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10/*
11 * Common code for the Motif and Athena GUI.
12 * Not used for GTK.
13 */
14
Bram Moolenaarf7506ca2017-02-25 16:01:49 +010015#include "vim.h"
16
Bram Moolenaar071d4272004-06-13 20:20:40 +000017#include <X11/keysym.h>
18#include <X11/Xatom.h>
19#include <X11/StringDefs.h>
20#include <X11/Intrinsic.h>
21#include <X11/Shell.h>
22#include <X11/cursorfont.h>
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
25 * For Workshop XpmP.h is preferred, because it makes the signs drawn with a
26 * transparent background instead of black.
27 */
28#if defined(HAVE_XM_XPMP_H) && defined(FEAT_GUI_MOTIF) \
29 && (!defined(HAVE_X11_XPM_H) || defined(FEAT_SUN_WORKSHOP))
30# include <Xm/XpmP.h>
31#else
32# ifdef HAVE_X11_XPM_H
33# include <X11/xpm.h>
34# endif
35#endif
36
37#ifdef FEAT_XFONTSET
38# ifdef X_LOCALE
39# include <X11/Xlocale.h>
40# else
41# include <locale.h>
42# endif
43#endif
44
45#ifdef HAVE_X11_SUNKEYSYM_H
46# include <X11/Sunkeysym.h>
47#endif
48
49#ifdef HAVE_X11_XMU_EDITRES_H
50# include <X11/Xmu/Editres.h>
51#endif
52
Bram Moolenaar071d4272004-06-13 20:20:40 +000053#define VIM_NAME "vim"
54#define VIM_CLASS "Vim"
55
56/* Default resource values */
57#define DFLT_FONT "7x13"
58#ifdef FONTSET_ALWAYS
59# define DFLT_MENU_FONT XtDefaultFontSet
60#else
61# define DFLT_MENU_FONT XtDefaultFont
62#endif
63#define DFLT_TOOLTIP_FONT XtDefaultFontSet
64
65#ifdef FEAT_GUI_ATHENA
66# define DFLT_MENU_BG_COLOR "gray77"
67# define DFLT_MENU_FG_COLOR "black"
68# define DFLT_SCROLL_BG_COLOR "gray60"
69# define DFLT_SCROLL_FG_COLOR "gray77"
Bram Moolenaar46582282016-07-23 14:35:12 +020070# define DFLT_TOOLTIP_BG_COLOR "#ffff91"
71# define DFLT_TOOLTIP_FG_COLOR "#000000"
Bram Moolenaar071d4272004-06-13 20:20:40 +000072#else
73/* use the default (CDE) colors */
74# define DFLT_MENU_BG_COLOR ""
75# define DFLT_MENU_FG_COLOR ""
76# define DFLT_SCROLL_BG_COLOR ""
77# define DFLT_SCROLL_FG_COLOR ""
Bram Moolenaar46582282016-07-23 14:35:12 +020078# define DFLT_TOOLTIP_BG_COLOR "#ffff91"
79# define DFLT_TOOLTIP_FG_COLOR "#000000"
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#endif
81
82Widget vimShell = (Widget)0;
83
84static Atom wm_atoms[2]; /* Window Manager Atoms */
85#define DELETE_WINDOW_IDX 0 /* index in wm_atoms[] for WM_DELETE_WINDOW */
86#define SAVE_YOURSELF_IDX 1 /* index in wm_atoms[] for WM_SAVE_YOURSELF */
87
88#ifdef FEAT_XFONTSET
89/*
90 * We either draw with a fontset (when current_fontset != NULL) or with a
91 * normal font (current_fontset == NULL, use gui.text_gc and gui.back_gc).
92 */
93static XFontSet current_fontset = NULL;
94
95#define XDrawString(dpy, win, gc, x, y, str, n) \
96 do \
97 { \
98 if (current_fontset != NULL) \
99 XmbDrawString(dpy, win, current_fontset, gc, x, y, str, n); \
100 else \
101 XDrawString(dpy, win, gc, x, y, str, n); \
102 } while (0)
103
104#define XDrawString16(dpy, win, gc, x, y, str, n) \
105 do \
106 { \
107 if (current_fontset != NULL) \
108 XwcDrawString(dpy, win, current_fontset, gc, x, y, (wchar_t *)str, n); \
109 else \
Bram Moolenaara3227e22006-03-08 21:32:40 +0000110 XDrawString16(dpy, win, gc, x, y, (XChar2b *)str, n); \
111 } while (0)
112
113#define XDrawImageString16(dpy, win, gc, x, y, str, n) \
114 do \
115 { \
116 if (current_fontset != NULL) \
117 XwcDrawImageString(dpy, win, current_fontset, gc, x, y, (wchar_t *)str, n); \
118 else \
119 XDrawImageString16(dpy, win, gc, x, y, (XChar2b *)str, n); \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 } while (0)
121
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100122static int check_fontset_sanity(XFontSet fs);
123static int fontset_width(XFontSet fs);
124static int fontset_ascent(XFontSet fs);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#endif
126
127static guicolor_T prev_fg_color = INVALCOLOR;
128static guicolor_T prev_bg_color = INVALCOLOR;
Bram Moolenaarfb269802005-03-15 22:46:30 +0000129static guicolor_T prev_sp_color = INVALCOLOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130
131#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
132static XButtonPressedEvent last_mouse_event;
133#endif
134
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100135static void gui_x11_check_copy_area(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136#ifdef FEAT_CLIENTSERVER
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100137static void gui_x11_send_event_handler(Widget, XtPointer, XEvent *, Boolean *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100139static void gui_x11_wm_protocol_handler(Widget, XtPointer, XEvent *, Boolean *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100140static Cursor gui_x11_create_blank_mouse(void);
141static void draw_curl(int row, int col, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142
143
144/*
145 * Keycodes recognized by vim.
146 * NOTE: when changing this, the table in gui_gtk_x11.c probably needs the
147 * same change!
148 */
149static struct specialkey
150{
151 KeySym key_sym;
152 char_u vim_code0;
153 char_u vim_code1;
154} special_keys[] =
155{
156 {XK_Up, 'k', 'u'},
157 {XK_Down, 'k', 'd'},
158 {XK_Left, 'k', 'l'},
159 {XK_Right, 'k', 'r'},
160
161 {XK_F1, 'k', '1'},
162 {XK_F2, 'k', '2'},
163 {XK_F3, 'k', '3'},
164 {XK_F4, 'k', '4'},
165 {XK_F5, 'k', '5'},
166 {XK_F6, 'k', '6'},
167 {XK_F7, 'k', '7'},
168 {XK_F8, 'k', '8'},
169 {XK_F9, 'k', '9'},
170 {XK_F10, 'k', ';'},
171
172 {XK_F11, 'F', '1'},
173 {XK_F12, 'F', '2'},
174 {XK_F13, 'F', '3'},
175 {XK_F14, 'F', '4'},
176 {XK_F15, 'F', '5'},
177 {XK_F16, 'F', '6'},
178 {XK_F17, 'F', '7'},
179 {XK_F18, 'F', '8'},
180 {XK_F19, 'F', '9'},
181 {XK_F20, 'F', 'A'},
182
183 {XK_F21, 'F', 'B'},
184 {XK_F22, 'F', 'C'},
185 {XK_F23, 'F', 'D'},
186 {XK_F24, 'F', 'E'},
187 {XK_F25, 'F', 'F'},
188 {XK_F26, 'F', 'G'},
189 {XK_F27, 'F', 'H'},
190 {XK_F28, 'F', 'I'},
191 {XK_F29, 'F', 'J'},
192 {XK_F30, 'F', 'K'},
193
194 {XK_F31, 'F', 'L'},
195 {XK_F32, 'F', 'M'},
196 {XK_F33, 'F', 'N'},
197 {XK_F34, 'F', 'O'},
198 {XK_F35, 'F', 'P'}, /* keysymdef.h defines up to F35 */
199#ifdef SunXK_F36
200 {SunXK_F36, 'F', 'Q'},
201 {SunXK_F37, 'F', 'R'},
202#endif
203
204 {XK_Help, '%', '1'},
205 {XK_Undo, '&', '8'},
206 {XK_BackSpace, 'k', 'b'},
207 {XK_Insert, 'k', 'I'},
208 {XK_Delete, 'k', 'D'},
209 {XK_Home, 'k', 'h'},
210 {XK_End, '@', '7'},
211 {XK_Prior, 'k', 'P'},
212 {XK_Next, 'k', 'N'},
213 {XK_Print, '%', '9'},
214
215 /* Keypad keys: */
216#ifdef XK_KP_Left
217 {XK_KP_Left, 'k', 'l'},
218 {XK_KP_Right, 'k', 'r'},
219 {XK_KP_Up, 'k', 'u'},
220 {XK_KP_Down, 'k', 'd'},
221 {XK_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
222 {XK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
223 {XK_KP_Home, 'K', '1'},
224 {XK_KP_End, 'K', '4'},
225 {XK_KP_Prior, 'K', '3'},
226 {XK_KP_Next, 'K', '5'},
227
228 {XK_KP_Add, 'K', '6'},
229 {XK_KP_Subtract, 'K', '7'},
230 {XK_KP_Divide, 'K', '8'},
231 {XK_KP_Multiply, 'K', '9'},
232 {XK_KP_Enter, 'K', 'A'},
233 {XK_KP_Decimal, 'K', 'B'},
234
235 {XK_KP_0, 'K', 'C'},
236 {XK_KP_1, 'K', 'D'},
237 {XK_KP_2, 'K', 'E'},
238 {XK_KP_3, 'K', 'F'},
239 {XK_KP_4, 'K', 'G'},
240 {XK_KP_5, 'K', 'H'},
241 {XK_KP_6, 'K', 'I'},
242 {XK_KP_7, 'K', 'J'},
243 {XK_KP_8, 'K', 'K'},
244 {XK_KP_9, 'K', 'L'},
245#endif
246
247 /* End of list marker: */
248 {(KeySym)0, 0, 0}
249};
250
251#define XtNboldFont "boldFont"
252#define XtCBoldFont "BoldFont"
253#define XtNitalicFont "italicFont"
254#define XtCItalicFont "ItalicFont"
255#define XtNboldItalicFont "boldItalicFont"
256#define XtCBoldItalicFont "BoldItalicFont"
257#define XtNscrollbarWidth "scrollbarWidth"
258#define XtCScrollbarWidth "ScrollbarWidth"
259#define XtNmenuHeight "menuHeight"
260#define XtCMenuHeight "MenuHeight"
261#define XtNmenuFont "menuFont"
262#define XtCMenuFont "MenuFont"
263#define XtNmenuFontSet "menuFontSet"
264#define XtCMenuFontSet "MenuFontSet"
265
266
267/* Resources for setting the foreground and background colors of menus */
268#define XtNmenuBackground "menuBackground"
269#define XtCMenuBackground "MenuBackground"
270#define XtNmenuForeground "menuForeground"
271#define XtCMenuForeground "MenuForeground"
272
273/* Resources for setting the foreground and background colors of scrollbars */
274#define XtNscrollBackground "scrollBackground"
275#define XtCScrollBackground "ScrollBackground"
276#define XtNscrollForeground "scrollForeground"
277#define XtCScrollForeground "ScrollForeground"
278
279/* Resources for setting the foreground and background colors of tooltip */
280#define XtNtooltipBackground "tooltipBackground"
281#define XtCTooltipBackground "TooltipBackground"
282#define XtNtooltipForeground "tooltipForeground"
283#define XtCTooltipForeground "TooltipForeground"
284#define XtNtooltipFont "tooltipFont"
285#define XtCTooltipFont "TooltipFont"
286
287/*
288 * X Resources:
289 */
290static XtResource vim_resources[] =
291{
292 {
293 XtNforeground,
294 XtCForeground,
295 XtRPixel,
296 sizeof(Pixel),
297 XtOffsetOf(gui_T, def_norm_pixel),
298 XtRString,
299 XtDefaultForeground
300 },
301 {
302 XtNbackground,
303 XtCBackground,
304 XtRPixel,
305 sizeof(Pixel),
306 XtOffsetOf(gui_T, def_back_pixel),
307 XtRString,
308 XtDefaultBackground
309 },
310 {
311 XtNfont,
312 XtCFont,
313 XtRString,
314 sizeof(String *),
315 XtOffsetOf(gui_T, rsrc_font_name),
316 XtRImmediate,
317 XtDefaultFont
318 },
319 {
320 XtNboldFont,
321 XtCBoldFont,
322 XtRString,
323 sizeof(String *),
324 XtOffsetOf(gui_T, rsrc_bold_font_name),
325 XtRImmediate,
326 ""
327 },
328 {
329 XtNitalicFont,
330 XtCItalicFont,
331 XtRString,
332 sizeof(String *),
333 XtOffsetOf(gui_T, rsrc_ital_font_name),
334 XtRImmediate,
335 ""
336 },
337 {
338 XtNboldItalicFont,
339 XtCBoldItalicFont,
340 XtRString,
341 sizeof(String *),
342 XtOffsetOf(gui_T, rsrc_boldital_font_name),
343 XtRImmediate,
344 ""
345 },
346 {
347 XtNgeometry,
348 XtCGeometry,
349 XtRString,
350 sizeof(String *),
351 XtOffsetOf(gui_T, geom),
352 XtRImmediate,
353 ""
354 },
355 {
356 XtNreverseVideo,
357 XtCReverseVideo,
358 XtRBool,
359 sizeof(Bool),
360 XtOffsetOf(gui_T, rsrc_rev_video),
361 XtRImmediate,
362 (XtPointer)False
363 },
364 {
365 XtNborderWidth,
366 XtCBorderWidth,
367 XtRInt,
368 sizeof(int),
369 XtOffsetOf(gui_T, border_width),
370 XtRImmediate,
371 (XtPointer)2
372 },
373 {
374 XtNscrollbarWidth,
375 XtCScrollbarWidth,
376 XtRInt,
377 sizeof(int),
378 XtOffsetOf(gui_T, scrollbar_width),
379 XtRImmediate,
380 (XtPointer)SB_DEFAULT_WIDTH
381 },
382#ifdef FEAT_MENU
383# ifdef FEAT_GUI_ATHENA /* with Motif the height is always computed */
384 {
385 XtNmenuHeight,
386 XtCMenuHeight,
387 XtRInt,
388 sizeof(int),
389 XtOffsetOf(gui_T, menu_height),
390 XtRImmediate,
391 (XtPointer)MENU_DEFAULT_HEIGHT /* Should figure out at run time */
392 },
393# endif
394 {
395# ifdef FONTSET_ALWAYS
396 XtNmenuFontSet,
397 XtCMenuFontSet,
398#else
399 XtNmenuFont,
400 XtCMenuFont,
401#endif
402 XtRString,
403 sizeof(char *),
404 XtOffsetOf(gui_T, rsrc_menu_font_name),
405 XtRString,
406 DFLT_MENU_FONT
407 },
408#endif
409 {
410 XtNmenuForeground,
411 XtCMenuForeground,
412 XtRString,
413 sizeof(char *),
414 XtOffsetOf(gui_T, rsrc_menu_fg_name),
415 XtRString,
416 DFLT_MENU_FG_COLOR
417 },
418 {
419 XtNmenuBackground,
420 XtCMenuBackground,
421 XtRString,
422 sizeof(char *),
423 XtOffsetOf(gui_T, rsrc_menu_bg_name),
424 XtRString,
425 DFLT_MENU_BG_COLOR
426 },
427 {
428 XtNscrollForeground,
429 XtCScrollForeground,
430 XtRString,
431 sizeof(char *),
432 XtOffsetOf(gui_T, rsrc_scroll_fg_name),
433 XtRString,
434 DFLT_SCROLL_FG_COLOR
435 },
436 {
437 XtNscrollBackground,
438 XtCScrollBackground,
439 XtRString,
440 sizeof(char *),
441 XtOffsetOf(gui_T, rsrc_scroll_bg_name),
442 XtRString,
443 DFLT_SCROLL_BG_COLOR
444 },
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100445#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 {
447 XtNtooltipForeground,
448 XtCTooltipForeground,
449 XtRString,
450 sizeof(char *),
451 XtOffsetOf(gui_T, rsrc_tooltip_fg_name),
452 XtRString,
453 DFLT_TOOLTIP_FG_COLOR
454 },
455 {
456 XtNtooltipBackground,
457 XtCTooltipBackground,
458 XtRString,
459 sizeof(char *),
460 XtOffsetOf(gui_T, rsrc_tooltip_bg_name),
461 XtRString,
462 DFLT_TOOLTIP_BG_COLOR
463 },
464 {
465 XtNtooltipFont,
466 XtCTooltipFont,
467 XtRString,
468 sizeof(char *),
469 XtOffsetOf(gui_T, rsrc_tooltip_font_name),
470 XtRString,
471 DFLT_TOOLTIP_FONT
472 },
473 /* This one isn't really needed, keep for Sun Workshop? */
474 {
475 "balloonEvalFontSet",
476 XtCFontSet,
477 XtRFontSet,
478 sizeof(XFontSet),
479 XtOffsetOf(gui_T, tooltip_fontset),
480 XtRImmediate,
481 (XtPointer)NOFONTSET
482 },
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100483#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484#ifdef FEAT_XIM
485 {
486 "preeditType",
487 "PreeditType",
488 XtRString,
489 sizeof(char*),
490 XtOffsetOf(gui_T, rsrc_preedit_type_name),
491 XtRString,
492 (XtPointer)"OverTheSpot,OffTheSpot,Root"
493 },
494 {
495 "inputMethod",
496 "InputMethod",
497 XtRString,
498 sizeof(char*),
499 XtOffsetOf(gui_T, rsrc_input_method),
500 XtRString,
501 NULL
502 },
503#endif /* FEAT_XIM */
504};
505
506/*
507 * This table holds all the X GUI command line options allowed. This includes
508 * the standard ones so that we can skip them when vim is started without the
509 * GUI (but the GUI might start up later).
510 * When changing this, also update doc/vim_gui.txt and the usage message!!!
511 */
512static XrmOptionDescRec cmdline_options[] =
513{
514 /* We handle these options ourselves */
515 {"-bg", ".background", XrmoptionSepArg, NULL},
516 {"-background", ".background", XrmoptionSepArg, NULL},
517 {"-fg", ".foreground", XrmoptionSepArg, NULL},
518 {"-foreground", ".foreground", XrmoptionSepArg, NULL},
519 {"-fn", ".font", XrmoptionSepArg, NULL},
520 {"-font", ".font", XrmoptionSepArg, NULL},
521 {"-boldfont", ".boldFont", XrmoptionSepArg, NULL},
522 {"-italicfont", ".italicFont", XrmoptionSepArg, NULL},
523 {"-geom", ".geometry", XrmoptionSepArg, NULL},
524 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
525 {"-reverse", "*reverseVideo", XrmoptionNoArg, "True"},
526 {"-rv", "*reverseVideo", XrmoptionNoArg, "True"},
527 {"+reverse", "*reverseVideo", XrmoptionNoArg, "False"},
528 {"+rv", "*reverseVideo", XrmoptionNoArg, "False"},
529 {"-display", ".display", XrmoptionSepArg, NULL},
Bram Moolenaara5792f52005-11-23 21:25:05 +0000530 {"-iconic", ".iconic", XrmoptionNoArg, "True"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 {"-name", ".name", XrmoptionSepArg, NULL},
532 {"-bw", ".borderWidth", XrmoptionSepArg, NULL},
533 {"-borderwidth", ".borderWidth", XrmoptionSepArg, NULL},
534 {"-sw", ".scrollbarWidth", XrmoptionSepArg, NULL},
535 {"-scrollbarwidth", ".scrollbarWidth", XrmoptionSepArg, NULL},
536 {"-mh", ".menuHeight", XrmoptionSepArg, NULL},
537 {"-menuheight", ".menuHeight", XrmoptionSepArg, NULL},
538#ifdef FONTSET_ALWAYS
539 {"-mf", ".menuFontSet", XrmoptionSepArg, NULL},
540 {"-menufont", ".menuFontSet", XrmoptionSepArg, NULL},
541 {"-menufontset", ".menuFontSet", XrmoptionSepArg, NULL},
542#else
543 {"-mf", ".menuFont", XrmoptionSepArg, NULL},
544 {"-menufont", ".menuFont", XrmoptionSepArg, NULL},
545#endif
546 {"-xrm", NULL, XrmoptionResArg, NULL}
547};
548
549static int gui_argc = 0;
550static char **gui_argv = NULL;
551
552/*
553 * Call-back routines.
554 */
555
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100557gui_x11_timer_cb(
558 XtPointer timed_out,
559 XtIntervalId *interval_id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
561 *((int *)timed_out) = TRUE;
562}
563
Bram Moolenaar1dccf632017-08-27 17:38:27 +0200564#ifdef FEAT_JOB_CHANNEL
565 static void
566channel_poll_cb(
567 XtPointer client_data,
568 XtIntervalId *interval_id UNUSED)
569{
570 XtIntervalId *channel_timer = (XtIntervalId *)client_data;
571
572 /* Using an event handler for a channel that may be disconnected does
573 * not work, it hangs. Instead poll for messages. */
574 channel_handle_events(TRUE);
575 parse_queued_messages();
576
577 /* repeat */
578 *channel_timer = XtAppAddTimeOut(app_context, (long_u)20,
579 channel_poll_cb, client_data);
580}
581#endif
582
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100584gui_x11_visibility_cb(
585 Widget w UNUSED,
586 XtPointer dud UNUSED,
587 XEvent *event,
588 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589{
590 if (event->type != VisibilityNotify)
591 return;
592
593 gui.visibility = event->xvisibility.state;
594
595 /*
596 * When we do an XCopyArea(), and the window is partially obscured, we want
597 * to receive an event to tell us whether it worked or not.
598 */
599 XSetGraphicsExposures(gui.dpy, gui.text_gc,
600 gui.visibility != VisibilityUnobscured);
601
602 /* This is needed for when redrawing is slow. */
603 gui_mch_update();
604}
605
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100607gui_x11_expose_cb(
608 Widget w UNUSED,
609 XtPointer dud UNUSED,
610 XEvent *event,
611 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612{
613 XExposeEvent *gevent;
614 int new_x;
615
616 if (event->type != Expose)
617 return;
618
619 out_flush(); /* make sure all output has been processed */
620
621 gevent = (XExposeEvent *)event;
622 gui_redraw(gevent->x, gevent->y, gevent->width, gevent->height);
623
624 new_x = FILL_X(0);
625
626 /* Clear the border areas if needed */
627 if (gevent->x < new_x)
628 XClearArea(gui.dpy, gui.wid, 0, 0, new_x, 0, False);
629 if (gevent->y < FILL_Y(0))
630 XClearArea(gui.dpy, gui.wid, 0, 0, 0, FILL_Y(0), False);
631 if (gevent->x > FILL_X(Columns))
632 XClearArea(gui.dpy, gui.wid, FILL_X((int)Columns), 0, 0, 0, False);
633 if (gevent->y > FILL_Y(Rows))
634 XClearArea(gui.dpy, gui.wid, 0, FILL_Y((int)Rows), 0, 0, False);
635
636 /* This is needed for when redrawing is slow. */
637 gui_mch_update();
638}
639
Bram Moolenaar67c53842010-05-22 18:28:27 +0200640#if ((defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)) \
641 && defined(FEAT_GUI_MOTIF)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642/*
Bram Moolenaar231334e2005-07-25 20:46:57 +0000643 * This function fills in the XRectangle object with the current x,y
644 * coordinates and height, width so that an XtVaSetValues to the same shell of
Bram Moolenaar49325942007-05-10 19:19:59 +0000645 * those resources will restore the window to its former position and
Bram Moolenaar231334e2005-07-25 20:46:57 +0000646 * dimensions.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 *
Bram Moolenaar231334e2005-07-25 20:46:57 +0000648 * Note: This function may fail, in which case the XRectangle will be
649 * unchanged. Be sure to have the XRectangle set with the proper values for a
650 * failed condition prior to calling this function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 */
652 static void
653shellRectangle(Widget shell, XRectangle *r)
654{
655 Window rootw, shellw, child, parentw;
656 int absx, absy;
657 XWindowAttributes a;
658 Window *children;
659 unsigned int childrenCount;
660
661 shellw = XtWindow(shell);
662 if (shellw == 0)
663 return;
664 for (;;)
665 {
666 XQueryTree(XtDisplay(shell), shellw, &rootw, &parentw,
667 &children, &childrenCount);
668 XFree(children);
669 if (parentw == rootw)
670 break;
671 shellw = parentw;
672 }
673 XGetWindowAttributes(XtDisplay(shell), shellw, &a);
674 XTranslateCoordinates(XtDisplay(shell), shellw, a.root, 0, 0,
675 &absx, &absy, &child);
676 r->x = absx;
677 r->y = absy;
678 XtVaGetValues(shell, XmNheight, &r->height, XmNwidth, &r->width, NULL);
679}
680#endif
681
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100683gui_x11_resize_window_cb(
684 Widget w UNUSED,
685 XtPointer dud UNUSED,
686 XEvent *event,
687 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
689 static int lastWidth, lastHeight;
690
691 if (event->type != ConfigureNotify)
692 return;
693
694 if (event->xconfigure.width != lastWidth
695 || event->xconfigure.height != lastHeight)
696 {
697 lastWidth = event->xconfigure.width;
698 lastHeight = event->xconfigure.height;
699 gui_resize_shell(event->xconfigure.width, event->xconfigure.height
700#ifdef FEAT_XIM
701 - xim_get_status_area_height()
702#endif
703 );
704 }
705#ifdef FEAT_SUN_WORKSHOP
706 if (usingSunWorkShop)
707 {
708 XRectangle rec;
709
710 shellRectangle(w, &rec);
711 workshop_frame_moved(rec.x, rec.y, rec.width, rec.height);
712 }
713#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +0200714#if defined(FEAT_NETBEANS_INTG) && defined(FEAT_GUI_MOTIF)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200715 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 {
717 XRectangle rec;
718
719 shellRectangle(w, &rec);
720 netbeans_frame_moved(rec.x, rec.y);
721 }
722#endif
723#ifdef FEAT_XIM
724 xim_set_preedit();
725#endif
726}
727
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100729gui_x11_focus_change_cb(
730 Widget w UNUSED,
731 XtPointer data UNUSED,
732 XEvent *event,
733 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734{
735 gui_focus_change(event->type == FocusIn);
736}
737
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100739gui_x11_enter_cb(
740 Widget w UNUSED,
741 XtPointer data UNUSED,
742 XEvent *event UNUSED,
743 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
745 gui_focus_change(TRUE);
746}
747
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100749gui_x11_leave_cb(
750 Widget w UNUSED,
751 XtPointer data UNUSED,
752 XEvent *event UNUSED,
753 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754{
755 gui_focus_change(FALSE);
756}
757
758#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
759# if X_HAVE_UTF8_STRING
760# define USE_UTF8LOOKUP
761# endif
762#endif
763
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100765gui_x11_key_hit_cb(
766 Widget w UNUSED,
767 XtPointer dud UNUSED,
768 XEvent *event,
769 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770{
771 XKeyPressedEvent *ev_press;
772#ifdef FEAT_XIM
773 char_u string2[256];
774 char_u string_shortbuf[256];
775 char_u *string = string_shortbuf;
776 Boolean string_alloced = False;
777 Status status;
778#else
779 char_u string[4], string2[3];
780#endif
781 KeySym key_sym, key_sym2;
782 int len, len2;
783 int i;
784 int modifiers;
785 int key;
786
787 ev_press = (XKeyPressedEvent *)event;
788
789#ifdef FEAT_XIM
790 if (xic)
791 {
792# ifdef USE_UTF8LOOKUP
793 /* XFree86 4.0.2 or newer: Be able to get UTF-8 characters even when
794 * the locale isn't utf-8. */
795 if (enc_utf8)
796 len = Xutf8LookupString(xic, ev_press, (char *)string,
797 sizeof(string_shortbuf), &key_sym, &status);
798 else
799# endif
800 len = XmbLookupString(xic, ev_press, (char *)string,
801 sizeof(string_shortbuf), &key_sym, &status);
802 if (status == XBufferOverflow)
803 {
804 string = (char_u *)XtMalloc(len + 1);
805 string_alloced = True;
806# ifdef USE_UTF8LOOKUP
807 /* XFree86 4.0.2 or newer: Be able to get UTF-8 characters even
808 * when the locale isn't utf-8. */
809 if (enc_utf8)
810 len = Xutf8LookupString(xic, ev_press, (char *)string,
811 len, &key_sym, &status);
812 else
813# endif
814 len = XmbLookupString(xic, ev_press, (char *)string,
815 len, &key_sym, &status);
816 }
817 if (status == XLookupNone || status == XLookupChars)
818 key_sym = XK_VoidSymbol;
819
820# ifdef FEAT_MBYTE
821 /* Do conversion from 'termencoding' to 'encoding'. When using
822 * Xutf8LookupString() it has already been done. */
823 if (len > 0 && input_conv.vc_type != CONV_NONE
824# ifdef USE_UTF8LOOKUP
825 && !enc_utf8
826# endif
827 )
828 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100829 int maxlen = len * 4 + 40; /* guessed */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 char_u *p = (char_u *)XtMalloc(maxlen);
831
832 mch_memmove(p, string, len);
833 if (string_alloced)
834 XtFree((char *)string);
835 string = p;
836 string_alloced = True;
837 len = convert_input(p, len, maxlen);
838 }
839# endif
840
841 /* Translate CSI to K_CSI, otherwise it could be recognized as the
842 * start of a special key. */
843 for (i = 0; i < len; ++i)
844 if (string[i] == CSI)
845 {
846 char_u *p = (char_u *)XtMalloc(len + 3);
847
848 mch_memmove(p, string, i + 1);
849 p[i + 1] = KS_EXTRA;
850 p[i + 2] = (int)KE_CSI;
851 mch_memmove(p + i + 3, string + i + 1, len - i);
852 if (string_alloced)
853 XtFree((char *)string);
854 string = p;
855 string_alloced = True;
856 i += 2;
857 len += 2;
858 }
859 }
860 else
861#endif
862 len = XLookupString(ev_press, (char *)string, sizeof(string),
863 &key_sym, NULL);
864
865#ifdef SunXK_F36
866 /*
867 * These keys have bogus lookup strings, and trapping them here is
868 * easier than trying to XRebindKeysym() on them with every possible
869 * combination of modifiers.
870 */
871 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
872 len = 0;
873#endif
874
875#ifdef FEAT_HANGULIN
876 if ((key_sym == XK_space) && (ev_press->state & ShiftMask))
877 {
878 hangul_input_state_toggle();
879 goto theend;
880 }
881#endif
882
883 if (key_sym == XK_space)
884 string[0] = ' '; /* Otherwise Ctrl-Space doesn't work */
885
886 /*
887 * Only on some machines ^_ requires Ctrl+Shift+minus. For consistency,
888 * allow just Ctrl+minus too.
889 */
890 if (key_sym == XK_minus && (ev_press->state & ControlMask))
891 string[0] = Ctrl__;
892
893#ifdef XK_ISO_Left_Tab
894 /* why do we get XK_ISO_Left_Tab instead of XK_Tab for shift-tab? */
895 if (key_sym == XK_ISO_Left_Tab)
896 {
897 key_sym = XK_Tab;
898 string[0] = TAB;
899 len = 1;
900 }
901#endif
902
903 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
904 * that already has the 8th bit set. And not when using a double-byte
905 * encoding, setting the 8th bit may make it the lead byte of a
906 * double-byte character. */
907 if (len == 1
908 && (ev_press->state & Mod1Mask)
909 && !(key_sym == XK_BackSpace || key_sym == XK_Delete)
910 && (string[0] & 0x80) == 0
911#ifdef FEAT_MBYTE
912 && !enc_dbcs
913#endif
914 )
915 {
916#if defined(FEAT_MENU) && defined(FEAT_GUI_MOTIF)
917 /* Ignore ALT keys when they are used for the menu only */
918 if (gui.menu_is_active
919 && (p_wak[0] == 'y'
920 || (p_wak[0] == 'm' && gui_is_menu_shortcut(string[0]))))
921 goto theend;
922#endif
923 /*
924 * Before we set the 8th bit, check to make sure the user doesn't
925 * already have a mapping defined for this sequence. We determine this
926 * by checking to see if the input would be the same without the
927 * Alt/Meta key.
928 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
929 */
930 ev_press->state &= ~Mod1Mask;
931 len2 = XLookupString(ev_press, (char *)string2, sizeof(string2),
932 &key_sym2, NULL);
933 if (key_sym2 == XK_space)
934 string2[0] = ' '; /* Otherwise Meta-Ctrl-Space doesn't work */
935 if ( len2 == 1
936 && string[0] == string2[0]
937 && !(key_sym == XK_Tab && (ev_press->state & ShiftMask)))
938 {
939 string[0] |= 0x80;
940#ifdef FEAT_MBYTE
941 if (enc_utf8) /* convert to utf-8 */
942 {
943 string[1] = string[0] & 0xbf;
944 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
945 if (string[1] == CSI)
946 {
947 string[2] = KS_EXTRA;
948 string[3] = (int)KE_CSI;
949 len = 4;
950 }
951 else
952 len = 2;
953 }
954#endif
955 }
956 else
957 ev_press->state |= Mod1Mask;
958 }
959
960 if (len == 1 && string[0] == CSI)
961 {
962 string[1] = KS_EXTRA;
963 string[2] = (int)KE_CSI;
964 len = -3;
965 }
966
967 /* Check for special keys. Also do this when len == 1 (key has an ASCII
968 * value) to detect backspace, delete and keypad keys. */
969 if (len == 0 || len == 1)
970 {
971 for (i = 0; special_keys[i].key_sym != (KeySym)0; i++)
972 {
973 if (special_keys[i].key_sym == key_sym)
974 {
975 string[0] = CSI;
976 string[1] = special_keys[i].vim_code0;
977 string[2] = special_keys[i].vim_code1;
978 len = -3;
979 break;
980 }
981 }
982 }
983
984 /* Unrecognised key is ignored. */
985 if (len == 0)
986 goto theend;
987
988 /* Special keys (and a few others) may have modifiers. Also when using a
989 * double-byte encoding (can't set the 8th bit). */
990 if (len == -3 || key_sym == XK_space || key_sym == XK_Tab
991 || key_sym == XK_Return || key_sym == XK_Linefeed
992 || key_sym == XK_Escape
993#ifdef FEAT_MBYTE
994 || (enc_dbcs && len == 1 && (ev_press->state & Mod1Mask))
995#endif
996 )
997 {
998 modifiers = 0;
999 if (ev_press->state & ShiftMask)
1000 modifiers |= MOD_MASK_SHIFT;
1001 if (ev_press->state & ControlMask)
1002 modifiers |= MOD_MASK_CTRL;
1003 if (ev_press->state & Mod1Mask)
1004 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001005 if (ev_press->state & Mod4Mask)
1006 modifiers |= MOD_MASK_META;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007
1008 /*
1009 * For some keys a shift modifier is translated into another key
1010 * code.
1011 */
1012 if (len == -3)
1013 key = TO_SPECIAL(string[1], string[2]);
1014 else
1015 key = string[0];
1016 key = simplify_key(key, &modifiers);
1017 if (key == CSI)
1018 key = K_CSI;
1019 if (IS_SPECIAL(key))
1020 {
1021 string[0] = CSI;
1022 string[1] = K_SECOND(key);
1023 string[2] = K_THIRD(key);
1024 len = 3;
1025 }
1026 else
1027 {
1028 string[0] = key;
1029 len = 1;
1030 }
1031
1032 if (modifiers != 0)
1033 {
1034 string2[0] = CSI;
1035 string2[1] = KS_MODIFIER;
1036 string2[2] = modifiers;
1037 add_to_input_buf(string2, 3);
1038 }
1039 }
1040
1041 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1042#ifdef UNIX
1043 || (intr_char != 0 && string[0] == intr_char)
1044#endif
1045 ))
1046 {
1047 trash_input_buf();
1048 got_int = TRUE;
1049 }
1050
1051 add_to_input_buf(string, len);
1052
1053 /*
1054 * blank out the pointer if necessary
1055 */
1056 if (p_mh)
1057 gui_mch_mousehide(TRUE);
1058
1059#if defined(FEAT_BEVAL_TIP)
1060 {
1061 BalloonEval *be;
1062
1063 if ((be = gui_mch_currently_showing_beval()) != NULL)
1064 gui_mch_unpost_balloon(be);
1065 }
1066#endif
1067theend:
1068 {} /* some compilers need a statement here */
1069#ifdef FEAT_XIM
1070 if (string_alloced)
1071 XtFree((char *)string);
1072#endif
1073}
1074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001076gui_x11_mouse_cb(
1077 Widget w UNUSED,
1078 XtPointer dud UNUSED,
1079 XEvent *event,
1080 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081{
1082 static XtIntervalId timer = (XtIntervalId)0;
1083 static int timed_out = TRUE;
1084
1085 int button;
1086 int repeated_click = FALSE;
1087 int x, y;
1088 int_u x_modifiers;
1089 int_u vim_modifiers;
1090
1091 if (event->type == MotionNotify)
1092 {
1093 /* Get the latest position, avoids lagging behind on a drag. */
1094 x = event->xmotion.x;
1095 y = event->xmotion.y;
1096 x_modifiers = event->xmotion.state;
1097 button = (x_modifiers & (Button1Mask | Button2Mask | Button3Mask))
1098 ? MOUSE_DRAG : ' ';
1099
1100 /*
1101 * if our pointer is currently hidden, then we should show it.
1102 */
1103 gui_mch_mousehide(FALSE);
1104
1105 if (button != MOUSE_DRAG) /* just moving the rodent */
1106 {
1107#ifdef FEAT_MENU
1108 if (dud) /* moved in vimForm */
1109 y -= gui.menu_height;
1110#endif
1111 gui_mouse_moved(x, y);
1112 return;
1113 }
1114 }
1115 else
1116 {
1117 x = event->xbutton.x;
1118 y = event->xbutton.y;
1119 if (event->type == ButtonPress)
1120 {
1121 /* Handle multiple clicks */
1122 if (!timed_out)
1123 {
1124 XtRemoveTimeOut(timer);
1125 repeated_click = TRUE;
1126 }
1127 timed_out = FALSE;
1128 timer = XtAppAddTimeOut(app_context, (long_u)p_mouset,
1129 gui_x11_timer_cb, &timed_out);
1130 switch (event->xbutton.button)
1131 {
Bram Moolenaar88e484b2015-11-24 15:38:44 +01001132 /* keep in sync with gui_gtk_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 case Button1: button = MOUSE_LEFT; break;
1134 case Button2: button = MOUSE_MIDDLE; break;
1135 case Button3: button = MOUSE_RIGHT; break;
1136 case Button4: button = MOUSE_4; break;
1137 case Button5: button = MOUSE_5; break;
Bram Moolenaar88e484b2015-11-24 15:38:44 +01001138 case 6: button = MOUSE_7; break;
1139 case 7: button = MOUSE_6; break;
1140 case 8: button = MOUSE_X1; break;
1141 case 9: button = MOUSE_X2; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 default:
1143 return; /* Unknown button */
1144 }
1145 }
1146 else if (event->type == ButtonRelease)
1147 button = MOUSE_RELEASE;
1148 else
1149 return; /* Unknown mouse event type */
1150
1151 x_modifiers = event->xbutton.state;
1152#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1153 last_mouse_event = event->xbutton;
1154#endif
1155 }
1156
1157 vim_modifiers = 0x0;
1158 if (x_modifiers & ShiftMask)
1159 vim_modifiers |= MOUSE_SHIFT;
1160 if (x_modifiers & ControlMask)
1161 vim_modifiers |= MOUSE_CTRL;
1162 if (x_modifiers & Mod1Mask) /* Alt or Meta key */
1163 vim_modifiers |= MOUSE_ALT;
1164
1165 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
1166}
1167
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168/*
1169 * End of call-back routines
1170 */
1171
1172/*
1173 * Parse the GUI related command-line arguments. Any arguments used are
1174 * deleted from argv, and *argc is decremented accordingly. This is called
1175 * when vim is started, whether or not the GUI has been started.
1176 */
1177 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001178gui_mch_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179{
1180 int arg;
1181 int i;
1182
1183 /*
1184 * Move all the entries in argv which are relevant to X into gui_argv.
1185 */
1186 gui_argc = 0;
1187 gui_argv = (char **)lalloc((long_u)(*argc * sizeof(char *)), FALSE);
1188 if (gui_argv == NULL)
1189 return;
1190 gui_argv[gui_argc++] = argv[0];
1191 arg = 1;
1192 while (arg < *argc)
1193 {
1194 /* Look for argv[arg] in cmdline_options[] table */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001195 for (i = 0; i < (int)XtNumber(cmdline_options); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 if (strcmp(argv[arg], cmdline_options[i].option) == 0)
1197 break;
1198
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001199 if (i < (int)XtNumber(cmdline_options))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 {
1201 /* Remember finding "-rv" or "-reverse" */
1202 if (strcmp("-rv", argv[arg]) == 0
1203 || strcmp("-reverse", argv[arg]) == 0)
1204 found_reverse_arg = TRUE;
1205 else if ((strcmp("-fn", argv[arg]) == 0
1206 || strcmp("-font", argv[arg]) == 0)
1207 && arg + 1 < *argc)
1208 font_argument = argv[arg + 1];
1209
1210 /* Found match in table, so move it into gui_argv */
1211 gui_argv[gui_argc++] = argv[arg];
1212 if (--*argc > arg)
1213 {
1214 mch_memmove(&argv[arg], &argv[arg + 1], (*argc - arg)
1215 * sizeof(char *));
1216 if (cmdline_options[i].argKind != XrmoptionNoArg)
1217 {
1218 /* Move the options argument as well */
1219 gui_argv[gui_argc++] = argv[arg];
1220 if (--*argc > arg)
1221 mch_memmove(&argv[arg], &argv[arg + 1], (*argc - arg)
1222 * sizeof(char *));
1223 }
1224 }
1225 argv[*argc] = NULL;
1226 }
1227 else
1228#ifdef FEAT_SUN_WORKSHOP
1229 if (strcmp("-ws", argv[arg]) == 0)
1230 {
1231 usingSunWorkShop++;
1232 p_acd = TRUE;
1233 gui.dofork = FALSE; /* don't fork() when starting GUI */
1234 mch_memmove(&argv[arg], &argv[arg + 1],
1235 (--*argc - arg) * sizeof(char *));
1236 argv[*argc] = NULL;
1237# ifdef WSDEBUG
1238 wsdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
1239 wsdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
1240# endif
1241 }
1242 else
1243#endif
1244#ifdef FEAT_NETBEANS_INTG
1245 if (strncmp("-nb", argv[arg], 3) == 0)
1246 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 gui.dofork = FALSE; /* don't fork() when starting GUI */
1248 netbeansArg = argv[arg];
1249 mch_memmove(&argv[arg], &argv[arg + 1],
1250 (--*argc - arg) * sizeof(char *));
1251 argv[*argc] = NULL;
1252 }
1253 else
1254#endif
1255 arg++;
1256 }
1257}
1258
1259#ifndef XtSpecificationRelease
1260# define CARDINAL (Cardinal *)
1261#else
1262# if XtSpecificationRelease == 4
1263# define CARDINAL (Cardinal *)
1264# else
1265# define CARDINAL (int *)
1266# endif
1267#endif
1268
1269/*
1270 * Check if the GUI can be started. Called before gvimrc is sourced.
1271 * Return OK or FAIL.
1272 */
1273 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001274gui_mch_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275{
1276#ifdef FEAT_XIM
1277 XtSetLanguageProc(NULL, NULL, NULL);
1278#endif
1279 open_app_context();
1280 if (app_context != NULL)
1281 gui.dpy = XtOpenDisplay(app_context, 0, VIM_NAME, VIM_CLASS,
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00001282 cmdline_options, XtNumber(cmdline_options),
1283 CARDINAL &gui_argc, gui_argv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284
Bram Moolenaar889fe2c2018-05-13 16:23:40 +02001285# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1286 {
1287 /* The call to XtOpenDisplay() may have set the locale from the
1288 * environment. Set LC_NUMERIC to "C" to make sure that strtod() uses a
1289 * decimal point, not a comma. */
1290 char *p = setlocale(LC_NUMERIC, NULL);
1291
1292 if (p == NULL || strcmp(p, "C") != 0)
1293 setlocale(LC_NUMERIC, "C");
1294 }
1295# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 if (app_context == NULL || gui.dpy == NULL)
1297 {
1298 gui.dying = TRUE;
1299 EMSG(_(e_opendisp));
1300 return FAIL;
1301 }
1302 return OK;
1303}
1304
1305
1306#ifdef USE_XSMP
1307/*
1308 * Handle XSMP processing, de-registering the attachment upon error
1309 */
1310static XtInputId _xsmp_xtinputid;
1311
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001312static void local_xsmp_handle_requests(XtPointer c, int *s, XtInputId *i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001315local_xsmp_handle_requests(
1316 XtPointer c UNUSED,
1317 int *s UNUSED,
1318 XtInputId *i UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319{
1320 if (xsmp_handle_requests() == FAIL)
1321 XtRemoveInput(_xsmp_xtinputid);
1322}
1323#endif
1324
1325
1326/*
1327 * Initialise the X GUI. Create all the windows, set up all the call-backs etc.
1328 * Returns OK for success, FAIL when the GUI can't be started.
1329 */
1330 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001331gui_mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332{
1333 XtGCMask gc_mask;
1334 XGCValues gc_vals;
1335 int x, y, mask;
1336 unsigned w, h;
1337
1338#if 0
1339 /* Uncomment this to enable synchronous mode for debugging */
1340 XSynchronize(gui.dpy, True);
1341#endif
1342
1343 vimShell = XtVaAppCreateShell(VIM_NAME, VIM_CLASS,
1344 applicationShellWidgetClass, gui.dpy, NULL);
1345
1346 /*
1347 * Get the application resources
1348 */
1349 XtVaGetApplicationResources(vimShell, (XtPointer)&gui,
1350 vim_resources, XtNumber(vim_resources), NULL);
1351
1352 gui.scrollbar_height = gui.scrollbar_width;
1353
1354 /*
1355 * Get the colors ourselves. Using the automatic conversion doesn't
1356 * handle looking for approximate colors.
1357 */
1358 /* NOTE: These next few lines are an exact duplicate of gui_athena.c's
1359 * gui_mch_def_colors(). Why?
1360 */
1361 gui.menu_fg_pixel = gui_get_color((char_u *)gui.rsrc_menu_fg_name);
1362 gui.menu_bg_pixel = gui_get_color((char_u *)gui.rsrc_menu_bg_name);
1363 gui.scroll_fg_pixel = gui_get_color((char_u *)gui.rsrc_scroll_fg_name);
1364 gui.scroll_bg_pixel = gui_get_color((char_u *)gui.rsrc_scroll_bg_name);
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001365#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 gui.tooltip_fg_pixel = gui_get_color((char_u *)gui.rsrc_tooltip_fg_name);
1367 gui.tooltip_bg_pixel = gui_get_color((char_u *)gui.rsrc_tooltip_bg_name);
1368#endif
1369
1370#if defined(FEAT_MENU) && defined(FEAT_GUI_ATHENA)
1371 /* If the menu height was set, don't change it at runtime */
1372 if (gui.menu_height != MENU_DEFAULT_HEIGHT)
1373 gui.menu_height_fixed = TRUE;
1374#endif
1375
1376 /* Set default foreground and background colours */
1377 gui.norm_pixel = gui.def_norm_pixel;
1378 gui.back_pixel = gui.def_back_pixel;
1379
1380 /* Check if reverse video needs to be applied (on Sun it's done by X) */
1381 if (gui.rsrc_rev_video && gui_get_lightness(gui.back_pixel)
1382 > gui_get_lightness(gui.norm_pixel))
1383 {
1384 gui.norm_pixel = gui.def_back_pixel;
1385 gui.back_pixel = gui.def_norm_pixel;
1386 gui.def_norm_pixel = gui.norm_pixel;
1387 gui.def_back_pixel = gui.back_pixel;
1388 }
1389
1390 /* Get the colors from the "Normal", "Tooltip", "Scrollbar" and "Menu"
1391 * group (set in syntax.c or in a vimrc file) */
1392 set_normal_colors();
1393
1394 /*
1395 * Check that none of the colors are the same as the background color
1396 */
1397 gui_check_colors();
1398
1399 /*
1400 * Set up the GCs. The font attributes will be set in gui_init_font().
1401 */
1402 gc_mask = GCForeground | GCBackground;
1403 gc_vals.foreground = gui.norm_pixel;
1404 gc_vals.background = gui.back_pixel;
1405 gui.text_gc = XtGetGC(vimShell, gc_mask, &gc_vals);
1406
1407 gc_vals.foreground = gui.back_pixel;
1408 gc_vals.background = gui.norm_pixel;
1409 gui.back_gc = XtGetGC(vimShell, gc_mask, &gc_vals);
1410
1411 gc_mask |= GCFunction;
1412 gc_vals.foreground = gui.norm_pixel ^ gui.back_pixel;
1413 gc_vals.background = gui.norm_pixel ^ gui.back_pixel;
1414 gc_vals.function = GXxor;
1415 gui.invert_gc = XtGetGC(vimShell, gc_mask, &gc_vals);
1416
1417 gui.visibility = VisibilityUnobscured;
1418 x11_setup_atoms(gui.dpy);
1419
1420 if (gui_win_x != -1 && gui_win_y != -1)
1421 gui_mch_set_winpos(gui_win_x, gui_win_y);
1422
1423 /* Now adapt the supplied(?) geometry-settings */
1424 /* Added by Kjetil Jacobsen <kjetilja@stud.cs.uit.no> */
1425 if (gui.geom != NULL && *gui.geom != NUL)
1426 {
1427 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
1428 if (mask & WidthValue)
1429 Columns = w;
1430 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00001431 {
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001432 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00001433 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00001435 }
Bram Moolenaarc33916a2013-07-01 21:43:08 +02001436 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 /*
1438 * Set the (x,y) position of the main window only if specified in the
1439 * users geometry, so we get good defaults when they don't. This needs
1440 * to be done before the shell is popped up.
1441 */
1442 if (mask & (XValue|YValue))
1443 XtVaSetValues(vimShell, XtNgeometry, gui.geom, NULL);
1444 }
1445
1446 gui_x11_create_widgets();
1447
1448 /*
1449 * Add an icon to Vim (Marcel Douben: 11 May 1998).
1450 */
1451 if (vim_strchr(p_go, GO_ICON) != NULL)
1452 {
1453#ifndef HAVE_XPM
1454# include "vim_icon.xbm"
1455# include "vim_mask.xbm"
1456
1457 Arg arg[2];
1458
1459 XtSetArg(arg[0], XtNiconPixmap,
1460 XCreateBitmapFromData(gui.dpy,
1461 DefaultRootWindow(gui.dpy),
1462 (char *)vim_icon_bits,
1463 vim_icon_width,
1464 vim_icon_height));
1465 XtSetArg(arg[1], XtNiconMask,
1466 XCreateBitmapFromData(gui.dpy,
1467 DefaultRootWindow(gui.dpy),
1468 (char *)vim_mask_icon_bits,
1469 vim_mask_icon_width,
1470 vim_mask_icon_height));
1471 XtSetValues(vimShell, arg, (Cardinal)2);
1472#else
1473/* Use Pixmaps, looking much nicer. */
1474
1475/* If you get an error message here, you still need to unpack the runtime
1476 * archive! */
1477# ifdef magick
1478# undef magick
1479# endif
1480# define magick vim32x32
1481# include "../runtime/vim32x32.xpm"
1482# undef magick
1483# define magick vim16x16
1484# include "../runtime/vim16x16.xpm"
1485# undef magick
1486# define magick vim48x48
1487# include "../runtime/vim48x48.xpm"
1488# undef magick
1489
1490 static Pixmap icon = 0;
1491 static Pixmap icon_mask = 0;
1492 static char **magick = vim32x32;
1493 Window root_window;
1494 XIconSize *size;
1495 int number_sizes;
1496 Display *dsp;
1497 Screen *scr;
1498 XpmAttributes attr;
1499 Colormap cmap;
1500
1501 /*
1502 * Adjust the icon to the preferences of the actual window manager.
1503 */
1504 root_window = XRootWindowOfScreen(XtScreen(vimShell));
1505 if (XGetIconSizes(XtDisplay(vimShell), root_window,
1506 &size, &number_sizes) != 0)
1507 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 if (number_sizes > 0)
1509 {
Bram Moolenaar6f292662013-07-14 15:06:50 +02001510 if (size->max_height >= 48 && size->max_width >= 48)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 magick = vim48x48;
Bram Moolenaar6f292662013-07-14 15:06:50 +02001512 else if (size->max_height >= 32 && size->max_width >= 32)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 magick = vim32x32;
Bram Moolenaar6f292662013-07-14 15:06:50 +02001514 else if (size->max_height >= 16 && size->max_width >= 16)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 magick = vim16x16;
1516 }
1517 }
1518
1519 dsp = XtDisplay(vimShell);
1520 scr = XtScreen(vimShell);
1521
1522 cmap = DefaultColormap(dsp, DefaultScreen(dsp));
1523 XtVaSetValues(vimShell, XtNcolormap, cmap, NULL);
1524
1525 attr.valuemask = 0L;
1526 attr.valuemask = XpmCloseness | XpmReturnPixels | XpmColormap | XpmDepth;
1527 attr.closeness = 65535; /* accuracy isn't crucial */
1528 attr.colormap = cmap;
1529 attr.depth = DefaultDepthOfScreen(scr);
1530
1531 if (!icon)
Bram Moolenaare8208012008-06-20 09:59:25 +00001532 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 XpmCreatePixmapFromData(dsp, root_window, magick, &icon,
1534 &icon_mask, &attr);
Bram Moolenaare8208012008-06-20 09:59:25 +00001535 XpmFreeAttributes(&attr);
1536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537
1538# ifdef FEAT_GUI_ATHENA
1539 XtVaSetValues(vimShell, XtNiconPixmap, icon, XtNiconMask, icon_mask, NULL);
1540# else
1541 XtVaSetValues(vimShell, XmNiconPixmap, icon, XmNiconMask, icon_mask, NULL);
1542# endif
1543#endif
1544 }
1545
1546 if (gui.color_approx)
1547 EMSG(_("Vim E458: Cannot allocate colormap entry, some colors may be incorrect"));
1548
1549#ifdef FEAT_SUN_WORKSHOP
1550 if (usingSunWorkShop)
1551 workshop_connect(app_context);
1552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001554#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 gui_init_tooltip_font();
1556#endif
1557#ifdef FEAT_MENU
1558 gui_init_menu_font();
1559#endif
1560
1561#ifdef USE_XSMP
1562 /* Attach listener on ICE connection */
1563 if (-1 != xsmp_icefd)
1564 _xsmp_xtinputid = XtAppAddInput(app_context, xsmp_icefd,
1565 (XtPointer)XtInputReadMask, local_xsmp_handle_requests, NULL);
1566#endif
1567
1568 return OK;
1569}
1570
1571/*
1572 * Called when starting the GUI fails after calling gui_mch_init().
1573 */
1574 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001575gui_mch_uninit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576{
1577 gui_x11_destroy_widgets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 XtCloseDisplay(gui.dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 gui.dpy = NULL;
1580 vimShell = (Widget)0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001581 VIM_CLEAR(gui_argv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582}
1583
1584/*
1585 * Called when the foreground or background color has been changed.
1586 */
1587 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001588gui_mch_new_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589{
1590 long_u gc_mask;
1591 XGCValues gc_vals;
1592
1593 gc_mask = GCForeground | GCBackground;
1594 gc_vals.foreground = gui.norm_pixel;
1595 gc_vals.background = gui.back_pixel;
1596 if (gui.text_gc != NULL)
1597 XChangeGC(gui.dpy, gui.text_gc, gc_mask, &gc_vals);
1598
1599 gc_vals.foreground = gui.back_pixel;
1600 gc_vals.background = gui.norm_pixel;
1601 if (gui.back_gc != NULL)
1602 XChangeGC(gui.dpy, gui.back_gc, gc_mask, &gc_vals);
1603
1604 gc_mask |= GCFunction;
1605 gc_vals.foreground = gui.norm_pixel ^ gui.back_pixel;
1606 gc_vals.background = gui.norm_pixel ^ gui.back_pixel;
1607 gc_vals.function = GXxor;
1608 if (gui.invert_gc != NULL)
1609 XChangeGC(gui.dpy, gui.invert_gc, gc_mask, &gc_vals);
1610
1611 gui_x11_set_back_color();
1612}
1613
1614/*
1615 * Open the GUI window which was created by a call to gui_mch_init().
1616 */
1617 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001618gui_mch_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619{
1620 /* Actually open the window */
Bram Moolenaara5792f52005-11-23 21:25:05 +00001621 XtRealizeWidget(vimShell);
1622 XtManageChild(XtNameToWidget(vimShell, "*vimForm"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623
1624 gui.wid = gui_x11_get_wid();
1625 gui.blank_pointer = gui_x11_create_blank_mouse();
1626
1627 /*
1628 * Add a callback for the Close item on the window managers menu, and the
1629 * save-yourself event.
1630 */
1631 wm_atoms[SAVE_YOURSELF_IDX] =
1632 XInternAtom(gui.dpy, "WM_SAVE_YOURSELF", False);
1633 wm_atoms[DELETE_WINDOW_IDX] =
1634 XInternAtom(gui.dpy, "WM_DELETE_WINDOW", False);
1635 XSetWMProtocols(gui.dpy, XtWindow(vimShell), wm_atoms, 2);
1636 XtAddEventHandler(vimShell, NoEventMask, True, gui_x11_wm_protocol_handler,
1637 NULL);
1638#ifdef HAVE_X11_XMU_EDITRES_H
1639 /*
1640 * Enable editres protocol (see "man editres").
1641 * Usually will need to add -lXmu to the linker line as well.
1642 */
1643 XtAddEventHandler(vimShell, (EventMask)0, True, _XEditResCheckMessages,
1644 (XtPointer)NULL);
1645#endif
1646
1647#ifdef FEAT_CLIENTSERVER
1648 if (serverName == NULL && serverDelayedStartName != NULL)
1649 {
1650 /* This is a :gui command in a plain vim with no previous server */
1651 commWindow = XtWindow(vimShell);
1652 (void)serverRegisterName(gui.dpy, serverDelayedStartName);
1653 }
1654 else
1655 {
1656 /*
1657 * Cannot handle "widget-less" windows with XtProcessEvent() we'll
1658 * have to change the "server" registration to that of the main window
1659 * If we have not registered a name yet, remember the window
1660 */
1661 serverChangeRegisteredWindow(gui.dpy, XtWindow(vimShell));
1662 }
1663 XtAddEventHandler(vimShell, PropertyChangeMask, False,
1664 gui_x11_send_event_handler, NULL);
1665#endif
1666
1667
1668#if defined(FEAT_MENU) && defined(FEAT_GUI_ATHENA)
1669 /* The Athena GUI needs this again after opening the window */
1670 gui_position_menu();
1671# ifdef FEAT_TOOLBAR
1672 gui_mch_set_toolbar_pos(0, gui.menu_height, gui.menu_width,
1673 gui.toolbar_height);
1674# endif
1675#endif
1676
1677 /* Get the colors for the highlight groups (gui_check_colors() might have
1678 * changed them) */
1679 highlight_gui_started(); /* re-init colors and fonts */
1680
1681#ifdef FEAT_HANGULIN
1682 hangul_keyboard_set();
1683#endif
1684#ifdef FEAT_XIM
1685 xim_init();
1686#endif
1687#ifdef FEAT_SUN_WORKSHOP
1688 workshop_postinit();
1689#endif
1690
1691 return OK;
1692}
1693
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001694#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695/*
1696 * Convert the tooltip fontset name to an XFontSet.
1697 */
1698 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001699gui_init_tooltip_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700{
1701 XrmValue from, to;
1702
1703 from.addr = (char *)gui.rsrc_tooltip_font_name;
1704 from.size = strlen(from.addr);
1705 to.addr = (XtPointer)&gui.tooltip_fontset;
1706 to.size = sizeof(XFontSet);
1707
1708 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontSet, &to) == False)
1709 {
1710 /* Failed. What to do? */
1711 }
1712}
1713#endif
1714
1715#if defined(FEAT_MENU) || defined(PROTO)
1716/* Convert the menu font/fontset name to an XFontStruct/XFontset */
1717 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001718gui_init_menu_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719{
1720 XrmValue from, to;
1721
1722#ifdef FONTSET_ALWAYS
1723 from.addr = (char *)gui.rsrc_menu_font_name;
1724 from.size = strlen(from.addr);
1725 to.addr = (XtPointer)&gui.menu_fontset;
1726 to.size = sizeof(GuiFontset);
1727
1728 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontSet, &to) == False)
1729 {
1730 /* Failed. What to do? */
1731 }
1732#else
1733 from.addr = (char *)gui.rsrc_menu_font_name;
1734 from.size = strlen(from.addr);
1735 to.addr = (XtPointer)&gui.menu_font;
1736 to.size = sizeof(GuiFont);
1737
1738 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontStruct, &to) == False)
1739 {
1740 /* Failed. What to do? */
1741 }
1742#endif
1743}
1744#endif
1745
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001747gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748{
1749#if 0
1750 /* Lesstif gives an error message here, and so does Solaris. The man page
1751 * says that this isn't needed when exiting, so just skip it. */
1752 XtCloseDisplay(gui.dpy);
1753#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01001754 VIM_CLEAR(gui_argv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755}
1756
1757/*
1758 * Get the position of the top left corner of the window.
1759 */
1760 int
Bram Moolenaar02fdaea2016-01-30 18:13:55 +01001761gui_mch_get_winpos(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762{
1763 Dimension xpos, ypos;
1764
1765 XtVaGetValues(vimShell,
1766 XtNx, &xpos,
1767 XtNy, &ypos,
1768 NULL);
1769 *x = xpos;
1770 *y = ypos;
1771 return OK;
1772}
1773
1774/*
1775 * Set the position of the top left corner of the window to the given
1776 * coordinates.
1777 */
1778 void
Bram Moolenaar02fdaea2016-01-30 18:13:55 +01001779gui_mch_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780{
1781 XtVaSetValues(vimShell,
1782 XtNx, x,
1783 XtNy, y,
1784 NULL);
1785}
1786
1787 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001788gui_mch_set_shellsize(
1789 int width,
1790 int height,
1791 int min_width,
1792 int min_height,
1793 int base_width,
1794 int base_height,
1795 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796{
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00001797#ifdef FEAT_XIM
1798 height += xim_get_status_area_height(),
1799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 XtVaSetValues(vimShell,
1801 XtNwidthInc, gui.char_width,
1802 XtNheightInc, gui.char_height,
1803#if defined(XtSpecificationRelease) && XtSpecificationRelease >= 4
1804 XtNbaseWidth, base_width,
1805 XtNbaseHeight, base_height,
1806#endif
1807 XtNminWidth, min_width,
1808 XtNminHeight, min_height,
1809 XtNwidth, width,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 XtNheight, height,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 NULL);
1812}
1813
1814/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00001815 * Allow 10 pixels for horizontal borders, 'guiheadroom' for vertical borders.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 * Is there no way in X to find out how wide the borders really are?
1817 */
1818 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001819gui_mch_get_screen_dimensions(
1820 int *screen_w,
1821 int *screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822{
1823 *screen_w = DisplayWidth(gui.dpy, DefaultScreen(gui.dpy)) - 10;
1824 *screen_h = DisplayHeight(gui.dpy, DefaultScreen(gui.dpy)) - p_ghr;
1825}
1826
1827/*
1828 * Initialise vim to use the font "font_name". If it's NULL, pick a default
1829 * font.
1830 * If "fontset" is TRUE, load the "font_name" as a fontset.
1831 * Return FAIL if the font could not be loaded, OK otherwise.
1832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001834gui_mch_init_font(
1835 char_u *font_name,
1836 int do_fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837{
1838 XFontStruct *font = NULL;
1839
1840#ifdef FEAT_XFONTSET
1841 XFontSet fontset = NULL;
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001844#ifdef FEAT_GUI_MOTIF
1845 /* A font name equal "*" is indicating, that we should activate the font
1846 * selection dialogue to get a new font name. So let us do it here. */
1847 if (font_name != NULL && STRCMP(font_name, "*") == 0)
1848 font_name = gui_xm_select_font(hl_get_font_name());
1849#endif
1850
1851#ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 if (do_fontset)
1853 {
1854 /* If 'guifontset' is set, VIM treats all font specifications as if
1855 * they were fontsets, and 'guifontset' becomes the default. */
1856 if (font_name != NULL)
1857 {
1858 fontset = (XFontSet)gui_mch_get_fontset(font_name, FALSE, TRUE);
1859 if (fontset == NULL)
1860 return FAIL;
1861 }
1862 }
1863 else
1864#endif
1865 {
1866 if (font_name == NULL)
1867 {
1868 /*
1869 * If none of the fonts in 'font' could be loaded, try the one set
1870 * in the X resource, and finally just try using DFLT_FONT, which
1871 * will hopefully always be there.
1872 */
1873 font_name = gui.rsrc_font_name;
1874 font = (XFontStruct *)gui_mch_get_font(font_name, FALSE);
1875 if (font == NULL)
1876 font_name = (char_u *)DFLT_FONT;
1877 }
1878 if (font == NULL)
1879 font = (XFontStruct *)gui_mch_get_font(font_name, FALSE);
1880 if (font == NULL)
1881 return FAIL;
1882 }
1883
1884 gui_mch_free_font(gui.norm_font);
1885#ifdef FEAT_XFONTSET
1886 gui_mch_free_fontset(gui.fontset);
1887
1888 if (fontset != NULL)
1889 {
1890 gui.norm_font = NOFONT;
1891 gui.fontset = (GuiFontset)fontset;
1892 gui.char_width = fontset_width(fontset);
1893 gui.char_height = fontset_height(fontset) + p_linespace;
1894 gui.char_ascent = fontset_ascent(fontset) + p_linespace / 2;
1895 }
1896 else
1897#endif
1898 {
1899 gui.norm_font = (GuiFont)font;
1900#ifdef FEAT_XFONTSET
1901 gui.fontset = NOFONTSET;
1902#endif
1903 gui.char_width = font->max_bounds.width;
1904 gui.char_height = font->ascent + font->descent + p_linespace;
1905 gui.char_ascent = font->ascent + p_linespace / 2;
1906 }
1907
1908 hl_set_font_name(font_name);
1909
1910 /*
1911 * Try to load other fonts for bold, italic, and bold-italic.
1912 * We should also try to work out what font to use for these when they are
1913 * not specified by X resources, but we don't yet.
1914 */
1915 if (font_name == gui.rsrc_font_name)
1916 {
1917 if (gui.bold_font == NOFONT
1918 && gui.rsrc_bold_font_name != NULL
1919 && *gui.rsrc_bold_font_name != NUL)
1920 gui.bold_font = gui_mch_get_font(gui.rsrc_bold_font_name, FALSE);
1921 if (gui.ital_font == NOFONT
1922 && gui.rsrc_ital_font_name != NULL
1923 && *gui.rsrc_ital_font_name != NUL)
1924 gui.ital_font = gui_mch_get_font(gui.rsrc_ital_font_name, FALSE);
1925 if (gui.boldital_font == NOFONT
1926 && gui.rsrc_boldital_font_name != NULL
1927 && *gui.rsrc_boldital_font_name != NUL)
1928 gui.boldital_font = gui_mch_get_font(gui.rsrc_boldital_font_name,
1929 FALSE);
1930 }
1931 else
1932 {
1933 /* When not using the font specified by the resources, also don't use
1934 * the bold/italic fonts, otherwise setting 'guifont' will look very
1935 * strange. */
1936 if (gui.bold_font != NOFONT)
1937 {
1938 XFreeFont(gui.dpy, (XFontStruct *)gui.bold_font);
1939 gui.bold_font = NOFONT;
1940 }
1941 if (gui.ital_font != NOFONT)
1942 {
1943 XFreeFont(gui.dpy, (XFontStruct *)gui.ital_font);
1944 gui.ital_font = NOFONT;
1945 }
1946 if (gui.boldital_font != NOFONT)
1947 {
1948 XFreeFont(gui.dpy, (XFontStruct *)gui.boldital_font);
1949 gui.boldital_font = NOFONT;
1950 }
1951 }
1952
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001953#ifdef FEAT_GUI_MOTIF
1954 gui_motif_synch_fonts();
1955#endif
1956
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 return OK;
1958}
1959
1960/*
1961 * Get a font structure for highlighting.
1962 */
1963 GuiFont
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001964gui_mch_get_font(char_u *name, int giveErrorIfMissing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965{
1966 XFontStruct *font;
1967
Bram Moolenaard23a8232018-02-10 18:45:26 +01001968 if (!gui.in_use || name == NULL) /* can't do this when GUI not running */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 return NOFONT;
1970
1971 font = XLoadQueryFont(gui.dpy, (char *)name);
1972
1973 if (font == NULL)
1974 {
1975 if (giveErrorIfMissing)
1976 EMSG2(_(e_font), name);
1977 return NOFONT;
1978 }
1979
1980#ifdef DEBUG
1981 printf("Font Information for '%s':\n", name);
1982 printf(" w = %d, h = %d, ascent = %d, descent = %d\n",
1983 font->max_bounds.width, font->ascent + font->descent,
1984 font->ascent, font->descent);
1985 printf(" max ascent = %d, max descent = %d, max h = %d\n",
1986 font->max_bounds.ascent, font->max_bounds.descent,
1987 font->max_bounds.ascent + font->max_bounds.descent);
1988 printf(" min lbearing = %d, min rbearing = %d\n",
1989 font->min_bounds.lbearing, font->min_bounds.rbearing);
1990 printf(" max lbearing = %d, max rbearing = %d\n",
1991 font->max_bounds.lbearing, font->max_bounds.rbearing);
1992 printf(" leftink = %d, rightink = %d\n",
1993 (font->min_bounds.lbearing < 0),
1994 (font->max_bounds.rbearing > font->max_bounds.width));
1995 printf("\n");
1996#endif
1997
1998 if (font->max_bounds.width != font->min_bounds.width)
1999 {
2000 EMSG2(_(e_fontwidth), name);
2001 XFreeFont(gui.dpy, font);
2002 return NOFONT;
2003 }
2004 return (GuiFont)font;
2005}
2006
Bram Moolenaar567e4de2004-12-31 21:01:02 +00002007#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002008/*
2009 * Return the name of font "font" in allocated memory.
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002010 */
2011 char_u *
Bram Moolenaar87748452017-03-12 17:10:33 +01002012gui_mch_get_fontname(GuiFont font, char_u *name)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002013{
Bram Moolenaar87748452017-03-12 17:10:33 +01002014 char_u *ret = NULL;
2015
2016 if (name != NULL && font == NULL)
2017 {
2018 /* In this case, there's no way other than doing this. */
2019 ret = vim_strsave(name);
2020 }
2021 else if (font != NULL)
2022 {
2023 /* In this case, try to retrieve the XLFD corresponding to 'font'->fid;
2024 * if failed, use 'name' unless it's NULL. */
2025 unsigned long value = 0L;
2026
2027 if (XGetFontProperty(font, XA_FONT, &value))
2028 {
2029 char *xa_font_name = NULL;
2030
2031 xa_font_name = XGetAtomName(gui.dpy, value);
2032 if (xa_font_name != NULL)
2033 {
2034 ret = vim_strsave((char_u *)xa_font_name);
2035 XFree(xa_font_name);
2036 }
2037 else if (name != NULL)
2038 ret = vim_strsave(name);
2039 }
2040 else if (name != NULL)
2041 ret = vim_strsave(name);
2042 }
2043 return ret;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002044}
Bram Moolenaar567e4de2004-12-31 21:01:02 +00002045#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002046
Bram Moolenaar231334e2005-07-25 20:46:57 +00002047/*
2048 * Adjust gui.char_height (after 'linespace' was changed).
2049 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002051gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052{
2053#ifdef FEAT_XFONTSET
2054 if (gui.fontset != NOFONTSET)
2055 {
2056 gui.char_height = fontset_height((XFontSet)gui.fontset) + p_linespace;
2057 gui.char_ascent = fontset_ascent((XFontSet)gui.fontset)
2058 + p_linespace / 2;
2059 }
2060 else
2061#endif
2062 {
2063 XFontStruct *font = (XFontStruct *)gui.norm_font;
2064
2065 gui.char_height = font->ascent + font->descent + p_linespace;
2066 gui.char_ascent = font->ascent + p_linespace / 2;
2067 }
2068 return OK;
2069}
2070
2071/*
2072 * Set the current text font.
2073 */
2074 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002075gui_mch_set_font(GuiFont font)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076{
2077 static Font prev_font = (Font)-1;
2078 Font fid = ((XFontStruct *)font)->fid;
2079
2080 if (fid != prev_font)
2081 {
2082 XSetFont(gui.dpy, gui.text_gc, fid);
2083 XSetFont(gui.dpy, gui.back_gc, fid);
2084 prev_font = fid;
2085 gui.char_ascent = ((XFontStruct *)font)->ascent + p_linespace / 2;
2086 }
2087#ifdef FEAT_XFONTSET
2088 current_fontset = (XFontSet)NULL;
2089#endif
2090}
2091
2092#if defined(FEAT_XFONTSET) || defined(PROTO)
2093/*
2094 * Set the current text fontset.
2095 * Adjust the ascent, in case it's different.
2096 */
2097 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002098gui_mch_set_fontset(GuiFontset fontset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099{
2100 current_fontset = (XFontSet)fontset;
2101 gui.char_ascent = fontset_ascent(current_fontset) + p_linespace / 2;
2102}
2103#endif
2104
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105/*
2106 * If a font is not going to be used, free its structure.
2107 */
2108 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002109gui_mch_free_font(GuiFont font)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110{
2111 if (font != NOFONT)
2112 XFreeFont(gui.dpy, (XFontStruct *)font);
2113}
2114
2115#if defined(FEAT_XFONTSET) || defined(PROTO)
2116/*
2117 * If a fontset is not going to be used, free its structure.
2118 */
2119 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002120gui_mch_free_fontset(GuiFontset fontset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121{
2122 if (fontset != NOFONTSET)
2123 XFreeFontSet(gui.dpy, (XFontSet)fontset);
2124}
2125
2126/*
2127 * Load the fontset "name".
2128 * Return a reference to the fontset, or NOFONTSET when failing.
2129 */
2130 GuiFontset
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002131gui_mch_get_fontset(
2132 char_u *name,
2133 int giveErrorIfMissing,
2134 int fixed_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135{
2136 XFontSet fontset;
2137 char **missing, *def_str;
2138 int num_missing;
2139
2140 if (!gui.in_use || name == NULL)
2141 return NOFONTSET;
2142
2143 fontset = XCreateFontSet(gui.dpy, (char *)name, &missing, &num_missing,
2144 &def_str);
2145 if (num_missing > 0)
2146 {
2147 int i;
2148
2149 if (giveErrorIfMissing)
2150 {
2151 EMSG2(_("E250: Fonts for the following charsets are missing in fontset %s:"), name);
2152 for (i = 0; i < num_missing; i++)
2153 EMSG2("%s", missing[i]);
2154 }
2155 XFreeStringList(missing);
2156 }
2157
2158 if (fontset == NULL)
2159 {
2160 if (giveErrorIfMissing)
2161 EMSG2(_(e_fontset), name);
2162 return NOFONTSET;
2163 }
2164
2165 if (fixed_width && check_fontset_sanity(fontset) == FAIL)
2166 {
2167 XFreeFontSet(gui.dpy, fontset);
2168 return NOFONTSET;
2169 }
2170 return (GuiFontset)fontset;
2171}
2172
2173/*
2174 * Check if fontset "fs" is fixed width.
2175 */
2176 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002177check_fontset_sanity(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178{
2179 XFontStruct **xfs;
2180 char **font_name;
2181 int fn;
2182 char *base_name;
2183 int i;
2184 int min_width;
2185 int min_font_idx = 0;
2186
2187 base_name = XBaseFontNameListOfFontSet(fs);
2188 fn = XFontsOfFontSet(fs, &xfs, &font_name);
2189 for (i = 0; i < fn; i++)
2190 {
2191 if (xfs[i]->max_bounds.width != xfs[i]->min_bounds.width)
2192 {
2193 EMSG2(_("E252: Fontset name: %s"), base_name);
2194 EMSG2(_("Font '%s' is not fixed-width"), font_name[i]);
2195 return FAIL;
2196 }
2197 }
2198 /* scan base font width */
2199 min_width = 32767;
2200 for (i = 0; i < fn; i++)
2201 {
2202 if (xfs[i]->max_bounds.width<min_width)
2203 {
2204 min_width = xfs[i]->max_bounds.width;
2205 min_font_idx = i;
2206 }
2207 }
2208 for (i = 0; i < fn; i++)
2209 {
2210 if ( xfs[i]->max_bounds.width != 2 * min_width
2211 && xfs[i]->max_bounds.width != min_width)
2212 {
Bram Moolenaar9d438d32013-06-13 21:57:20 +02002213 EMSG2(_("E253: Fontset name: %s"), base_name);
2214 EMSG2(_("Font0: %s"), font_name[min_font_idx]);
2215 EMSG2(_("Font1: %s"), font_name[i]);
2216 EMSGN(_("Font%ld width is not twice that of font0"), i);
2217 EMSGN(_("Font0 width: %ld"), xfs[min_font_idx]->max_bounds.width);
2218 EMSGN(_("Font1 width: %ld"), xfs[i]->max_bounds.width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 return FAIL;
2220 }
2221 }
2222 /* it seems ok. Good Luck!! */
2223 return OK;
2224}
2225
2226 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002227fontset_width(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228{
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002229 return XmbTextEscapement(fs, "Vim", 3) / 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230}
2231
2232 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002233fontset_height(
2234 XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235{
2236 XFontSetExtents *extents;
2237
2238 extents = XExtentsOfFontSet(fs);
2239 return extents->max_logical_extent.height;
2240}
2241
2242#if (defined(FONTSET_ALWAYS) && defined(FEAT_GUI_ATHENA) \
2243 && defined(FEAT_MENU)) || defined(PROTO)
2244/*
2245 * Returns the bounding box height around the actual glyph image of all
2246 * characters in all fonts of the fontset.
2247 */
2248 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002249fontset_height2(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250{
2251 XFontSetExtents *extents;
2252
2253 extents = XExtentsOfFontSet(fs);
2254 return extents->max_ink_extent.height;
2255}
2256#endif
2257
2258/* NOT USED YET
2259 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002260fontset_descent(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261{
2262 XFontSetExtents *extents;
2263
2264 extents = XExtentsOfFontSet (fs);
2265 return extents->max_logical_extent.height + extents->max_logical_extent.y;
2266}
2267*/
2268
2269 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002270fontset_ascent(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271{
2272 XFontSetExtents *extents;
2273
2274 extents = XExtentsOfFontSet(fs);
2275 return -extents->max_logical_extent.y;
2276}
2277
2278#endif /* FEAT_XFONTSET */
2279
2280/*
2281 * Return the Pixel value (color) for the given color name.
2282 * Return INVALCOLOR for error.
2283 */
2284 guicolor_T
Bram Moolenaar46582282016-07-23 14:35:12 +02002285gui_mch_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286{
Bram Moolenaard23a8232018-02-10 18:45:26 +01002287 guicolor_T requested;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288
2289 /* can't do this when GUI not running */
Bram Moolenaar46582282016-07-23 14:35:12 +02002290 if (!gui.in_use || name == NULL || *name == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 return INVALCOLOR;
2292
Bram Moolenaar46582282016-07-23 14:35:12 +02002293 requested = gui_get_color_cmn(name);
2294 if (requested == INVALCOLOR)
2295 return INVALCOLOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002297 return gui_mch_get_rgb_color(
Bram Moolenaar46582282016-07-23 14:35:12 +02002298 (requested & 0xff0000) >> 16,
2299 (requested & 0xff00) >> 8,
2300 requested & 0xff);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002301}
2302
2303/*
2304 * Return the Pixel value (color) for the given RGB values.
2305 * Return INVALCOLOR for error.
2306 */
2307 guicolor_T
2308gui_mch_get_rgb_color(int r, int g, int b)
2309{
Bram Moolenaard23a8232018-02-10 18:45:26 +01002310 XColor available;
Bram Moolenaard60547b2017-07-24 20:15:30 +02002311 Colormap colormap;
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002312
Bram Moolenaar778df2a2018-05-06 19:19:36 +02002313/* Using XParseColor() is very slow, put rgb in XColor directly.
2314
2315 char spec[8]; // space enough to hold "#RRGGBB"
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002316 vim_snprintf(spec, sizeof(spec), "#%.2x%.2x%.2x", r, g, b);
Bram Moolenaar46582282016-07-23 14:35:12 +02002317 if (XParseColor(gui.dpy, colormap, (char *)spec, &available) != 0
2318 && XAllocColor(gui.dpy, colormap, &available) != 0)
2319 return (guicolor_T)available.pixel;
Bram Moolenaar778df2a2018-05-06 19:19:36 +02002320*/
2321 colormap = DefaultColormap(gui.dpy, DefaultScreen(gui.dpy));
2322 vim_memset(&available, 0, sizeof(XColor));
2323 available.red = r << 8;
2324 available.green = g << 8;
2325 available.blue = b << 8;
2326 if (XAllocColor(gui.dpy, colormap, &available) != 0)
2327 return (guicolor_T)available.pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328
2329 return INVALCOLOR;
2330}
2331
2332/*
Bram Moolenaarfb269802005-03-15 22:46:30 +00002333 * Set the current text foreground color.
2334 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002336gui_mch_set_fg_color(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337{
2338 if (color != prev_fg_color)
2339 {
2340 XSetForeground(gui.dpy, gui.text_gc, (Pixel)color);
2341 prev_fg_color = color;
2342 }
2343}
2344
2345/*
2346 * Set the current text background color.
2347 */
2348 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002349gui_mch_set_bg_color(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350{
2351 if (color != prev_bg_color)
2352 {
2353 XSetBackground(gui.dpy, gui.text_gc, (Pixel)color);
2354 prev_bg_color = color;
2355 }
2356}
2357
2358/*
Bram Moolenaarfb269802005-03-15 22:46:30 +00002359 * Set the current text special color.
2360 */
2361 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002362gui_mch_set_sp_color(guicolor_T color)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002363{
2364 prev_sp_color = color;
2365}
2366
2367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 * create a mouse pointer that is blank
2369 */
2370 static Cursor
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002371gui_x11_create_blank_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372{
2373 Pixmap blank_pixmap = XCreatePixmap(gui.dpy, gui.wid, 1, 1, 1);
2374 GC gc = XCreateGC(gui.dpy, blank_pixmap, (unsigned long)0, (XGCValues*)0);
2375 XDrawPoint(gui.dpy, blank_pixmap, gc, 0, 0);
2376 XFreeGC(gui.dpy, gc);
2377 return XCreatePixmapCursor(gui.dpy, blank_pixmap, blank_pixmap,
2378 (XColor*)&gui.norm_pixel, (XColor*)&gui.norm_pixel, 0, 0);
2379}
2380
Bram Moolenaarfb269802005-03-15 22:46:30 +00002381/*
2382 * Draw a curled line at the bottom of the character cell.
2383 */
2384 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002385draw_curl(int row, int col, int cells)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002386{
2387 int i;
2388 int offset;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002389 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarfb269802005-03-15 22:46:30 +00002390
2391 XSetForeground(gui.dpy, gui.text_gc, prev_sp_color);
2392 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
2393 {
2394 offset = val[i % 8];
2395 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, i,
2396 FILL_Y(row + 1) - 1 - offset);
2397 }
2398 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
2399}
2400
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002402gui_mch_draw_string(
2403 int row,
2404 int col,
2405 char_u *s,
2406 int len,
2407 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408{
2409 int cells = len;
2410#ifdef FEAT_MBYTE
Bram Moolenaara3227e22006-03-08 21:32:40 +00002411 static void *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 static int buflen = 0;
2413 char_u *p;
2414 int wlen = 0;
2415 int c;
2416
2417 if (enc_utf8)
2418 {
2419 /* Convert UTF-8 byte sequence to 16 bit characters for the X
2420 * functions. Need a buffer for the 16 bit characters. Keep it
2421 * between calls, because allocating it each time is slow. */
2422 if (buflen < len)
2423 {
2424 XtFree((char *)buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002425 buf = (void *)XtMalloc(len * (sizeof(XChar2b) < sizeof(wchar_t)
2426 ? sizeof(wchar_t) : sizeof(XChar2b)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 buflen = len;
2428 }
2429 p = s;
2430 cells = 0;
2431 while (p < s + len)
2432 {
2433 c = utf_ptr2char(p);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002434# ifdef FEAT_XFONTSET
2435 if (current_fontset != NULL)
2436 {
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002437# ifdef SMALL_WCHAR_T
2438 if (c >= 0x10000)
Bram Moolenaara3227e22006-03-08 21:32:40 +00002439 c = 0xbf; /* show chars > 0xffff as ? */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002440# endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00002441 ((wchar_t *)buf)[wlen] = c;
2442 }
2443 else
2444# endif
2445 {
2446 if (c >= 0x10000)
2447 c = 0xbf; /* show chars > 0xffff as ? */
2448 ((XChar2b *)buf)[wlen].byte1 = (unsigned)c >> 8;
2449 ((XChar2b *)buf)[wlen].byte2 = c;
2450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 ++wlen;
2452 cells += utf_char2cells(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002453 p += utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 }
2455 }
2456 else if (has_mbyte)
2457 {
2458 cells = 0;
2459 for (p = s; p < s + len; )
2460 {
2461 cells += ptr2cells(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002462 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 }
2464 }
2465
2466#endif
2467
2468#ifdef FEAT_XFONTSET
2469 if (current_fontset != NULL)
2470 {
2471 /* Setup a clip rectangle to avoid spilling over in the next or
2472 * previous line. This is apparently needed for some fonts which are
2473 * used in a fontset. */
2474 XRectangle clip;
2475
2476 clip.x = 0;
2477 clip.y = 0;
2478 clip.height = gui.char_height;
2479 clip.width = gui.char_width * cells + 1;
2480 XSetClipRectangles(gui.dpy, gui.text_gc, FILL_X(col), FILL_Y(row),
2481 &clip, 1, Unsorted);
2482 }
2483#endif
2484
2485 if (flags & DRAW_TRANSP)
2486 {
2487#ifdef FEAT_MBYTE
2488 if (enc_utf8)
2489 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2490 TEXT_Y(row), buf, wlen);
2491 else
2492#endif
2493 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2494 TEXT_Y(row), (char *)s, len);
2495 }
2496 else if (p_linespace != 0
2497#ifdef FEAT_XFONTSET
2498 || current_fontset != NULL
2499#endif
2500 )
2501 {
2502 XSetForeground(gui.dpy, gui.text_gc, prev_bg_color);
2503 XFillRectangle(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
2504 FILL_Y(row), gui.char_width * cells, gui.char_height);
2505 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
Bram Moolenaarfb269802005-03-15 22:46:30 +00002506
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507#ifdef FEAT_MBYTE
2508 if (enc_utf8)
2509 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2510 TEXT_Y(row), buf, wlen);
2511 else
2512#endif
2513 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2514 TEXT_Y(row), (char *)s, len);
2515 }
2516 else
2517 {
2518 /* XmbDrawImageString has bug, don't use it for fontset. */
2519#ifdef FEAT_MBYTE
2520 if (enc_utf8)
2521 XDrawImageString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2522 TEXT_Y(row), buf, wlen);
2523 else
2524#endif
2525 XDrawImageString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2526 TEXT_Y(row), (char *)s, len);
2527 }
2528
2529 /* Bold trick: draw the text again with a one-pixel offset. */
2530 if (flags & DRAW_BOLD)
2531 {
2532#ifdef FEAT_MBYTE
2533 if (enc_utf8)
2534 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col) + 1,
2535 TEXT_Y(row), buf, wlen);
2536 else
2537#endif
2538 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col) + 1,
2539 TEXT_Y(row), (char *)s, len);
2540 }
2541
Bram Moolenaarfb269802005-03-15 22:46:30 +00002542 /* Undercurl: draw curl at the bottom of the character cell. */
2543 if (flags & DRAW_UNDERC)
2544 draw_curl(row, col, cells);
2545
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 /* Underline: draw a line at the bottom of the character cell. */
2547 if (flags & DRAW_UNDERL)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002548 {
2549 int y = FILL_Y(row + 1) - 1;
2550
2551 /* When p_linespace is 0, overwrite the bottom row of pixels.
2552 * Otherwise put the line just below the character. */
2553 if (p_linespace > 1)
2554 y -= p_linespace - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 XDrawLine(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
Bram Moolenaarfb269802005-03-15 22:46:30 +00002556 y, FILL_X(col + cells) - 1, y);
2557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002559 if (flags & DRAW_STRIKE)
2560 {
2561 int y = FILL_Y(row + 1) - gui.char_height/2;
2562
2563 XSetForeground(gui.dpy, gui.text_gc, prev_sp_color);
2564 XDrawLine(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
2565 y, FILL_X(col + cells) - 1, y);
2566 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
2567 }
2568
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569#ifdef FEAT_XFONTSET
2570 if (current_fontset != NULL)
2571 XSetClipMask(gui.dpy, gui.text_gc, None);
2572#endif
2573}
2574
2575/*
2576 * Return OK if the key with the termcap name "name" is supported.
2577 */
2578 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002579gui_mch_haskey(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580{
2581 int i;
2582
2583 for (i = 0; special_keys[i].key_sym != (KeySym)0; i++)
2584 if (name[0] == special_keys[i].vim_code0 &&
2585 name[1] == special_keys[i].vim_code1)
2586 return OK;
2587 return FAIL;
2588}
2589
2590/*
2591 * Return the text window-id and display. Only required for X-based GUI's
2592 */
2593 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002594gui_get_x11_windis(Window *win, Display **dis)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595{
2596 *win = XtWindow(vimShell);
2597 *dis = gui.dpy;
2598 return OK;
2599}
2600
2601 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002602gui_mch_beep(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603{
2604 XBell(gui.dpy, 0);
2605}
2606
2607 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002608gui_mch_flash(int msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609{
2610 /* Do a visual beep by reversing the foreground and background colors */
2611 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc, 0, 0,
2612 FILL_X((int)Columns) + gui.border_offset,
2613 FILL_Y((int)Rows) + gui.border_offset);
2614 XSync(gui.dpy, False);
2615 ui_delay((long)msec, TRUE); /* wait for a few msec */
2616 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc, 0, 0,
2617 FILL_X((int)Columns) + gui.border_offset,
2618 FILL_Y((int)Rows) + gui.border_offset);
2619}
2620
2621/*
2622 * Invert a rectangle from row r, column c, for nr rows and nc columns.
2623 */
2624 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002625gui_mch_invert_rectangle(
2626 int r,
2627 int c,
2628 int nr,
2629 int nc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630{
2631 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc,
2632 FILL_X(c), FILL_Y(r), (nc) * gui.char_width, (nr) * gui.char_height);
2633}
2634
2635/*
2636 * Iconify the GUI window.
2637 */
2638 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002639gui_mch_iconify(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
2641 XIconifyWindow(gui.dpy, XtWindow(vimShell), DefaultScreen(gui.dpy));
2642}
2643
2644#if defined(FEAT_EVAL) || defined(PROTO)
2645/*
2646 * Bring the Vim window to the foreground.
2647 */
2648 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002649gui_mch_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650{
2651 XMapRaised(gui.dpy, XtWindow(vimShell));
2652}
2653#endif
2654
2655/*
2656 * Draw a cursor without focus.
2657 */
2658 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002659gui_mch_draw_hollow_cursor(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660{
2661 int w = 1;
2662
2663#ifdef FEAT_MBYTE
2664 if (mb_lefthalve(gui.row, gui.col))
2665 w = 2;
2666#endif
2667 gui_mch_set_fg_color(color);
2668 XDrawRectangle(gui.dpy, gui.wid, gui.text_gc, FILL_X(gui.col),
2669 FILL_Y(gui.row), w * gui.char_width - 1, gui.char_height - 1);
2670}
2671
2672/*
2673 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
2674 * color "color".
2675 */
2676 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002677gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678{
2679 gui_mch_set_fg_color(color);
2680
2681 XFillRectangle(gui.dpy, gui.wid, gui.text_gc,
2682#ifdef FEAT_RIGHTLEFT
2683 /* vertical line should be on the right of current point */
2684 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
2685#endif
2686 FILL_X(gui.col),
2687 FILL_Y(gui.row) + gui.char_height - h,
2688 w, h);
2689}
2690
2691/*
2692 * Catch up with any queued X events. This may put keyboard input into the
2693 * input buffer, call resize call-backs, trigger timers etc. If there is
2694 * nothing in the X event queue (& no timers pending), then we return
2695 * immediately.
2696 */
2697 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002698gui_mch_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699{
2700 XtInputMask mask, desired;
2701
2702#ifdef ALT_X_INPUT
2703 if (suppress_alternate_input)
2704 desired = (XtIMXEvent | XtIMTimer);
2705 else
2706#endif
2707 desired = (XtIMAll);
2708 while ((mask = XtAppPending(app_context)) && (mask & desired)
2709 && !vim_is_input_buf_full())
2710 XtAppProcessEvent(app_context, desired);
2711}
2712
2713/*
2714 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2715 * from the keyboard.
2716 * wtime == -1 Wait forever.
2717 * wtime == 0 This should never happen.
2718 * wtime > 0 Wait wtime milliseconds for a character.
2719 * Returns OK if a character was found to be available within the given time,
2720 * or FAIL otherwise.
2721 */
2722 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002723gui_mch_wait_for_chars(long wtime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724{
Bram Moolenaar4231da42016-06-02 14:30:04 +02002725 int focus;
2726 int retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
2728 /*
2729 * Make this static, in case gui_x11_timer_cb is called after leaving
2730 * this function (otherwise a random value on the stack may be changed).
2731 */
2732 static int timed_out;
2733 XtIntervalId timer = (XtIntervalId)0;
2734 XtInputMask desired;
Bram Moolenaar1dccf632017-08-27 17:38:27 +02002735#ifdef FEAT_JOB_CHANNEL
2736 XtIntervalId channel_timer = (XtIntervalId)0;
2737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738
2739 timed_out = FALSE;
2740
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 if (wtime > 0)
2742 timer = XtAppAddTimeOut(app_context, (long_u)wtime, gui_x11_timer_cb,
2743 &timed_out);
Bram Moolenaar1dccf632017-08-27 17:38:27 +02002744#ifdef FEAT_JOB_CHANNEL
2745 /* If there is a channel with the keep_open flag we need to poll for input
2746 * on them. */
2747 if (channel_any_keep_open())
2748 channel_timer = XtAppAddTimeOut(app_context, (long_u)20,
2749 channel_poll_cb, (XtPointer)&channel_timer);
2750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751
2752 focus = gui.in_focus;
2753#ifdef ALT_X_INPUT
2754 if (suppress_alternate_input)
2755 desired = (XtIMXEvent | XtIMTimer);
2756 else
2757#endif
2758 desired = (XtIMAll);
2759 while (!timed_out)
2760 {
2761 /* Stop or start blinking when focus changes */
2762 if (gui.in_focus != focus)
2763 {
2764 if (gui.in_focus)
2765 gui_mch_start_blink();
2766 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002767 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 focus = gui.in_focus;
2769 }
2770
Bram Moolenaar93c88e02015-09-15 14:12:05 +02002771#ifdef MESSAGE_QUEUE
Bram Moolenaar4231da42016-06-02 14:30:04 +02002772# ifdef FEAT_TIMERS
2773 did_add_timer = FALSE;
2774# endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02002775 parse_queued_messages();
Bram Moolenaar4231da42016-06-02 14:30:04 +02002776# ifdef FEAT_TIMERS
2777 if (did_add_timer)
2778 /* Need to recompute the waiting time. */
2779 break;
2780# endif
Bram Moolenaar03531f72010-11-16 15:04:57 +01002781#endif
2782
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 /*
2784 * Don't use gui_mch_update() because then we will spin-lock until a
2785 * char arrives, instead we use XtAppProcessEvent() to hang until an
2786 * event arrives. No need to check for input_buf_full because we are
2787 * returning as soon as it contains a single char. Note that
2788 * XtAppNextEvent() may not be used because it will not return after a
2789 * timer event has arrived -- webb
2790 */
2791 XtAppProcessEvent(app_context, desired);
2792
2793 if (input_available())
2794 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002795 retval = OK;
2796 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 }
2798 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002799
2800 if (timer != (XtIntervalId)0 && !timed_out)
2801 XtRemoveTimeOut(timer);
Bram Moolenaar1dccf632017-08-27 17:38:27 +02002802#ifdef FEAT_JOB_CHANNEL
2803 if (channel_timer != (XtIntervalId)0)
2804 XtRemoveTimeOut(channel_timer);
2805#endif
Bram Moolenaar4231da42016-06-02 14:30:04 +02002806
2807 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808}
2809
2810/*
2811 * Output routines.
2812 */
2813
2814/* Flush any output to the screen */
2815 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002816gui_mch_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817{
2818 XFlush(gui.dpy);
2819}
2820
2821/*
2822 * Clear a rectangular region of the screen from text pos (row1, col1) to
2823 * (row2, col2) inclusive.
2824 */
2825 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002826gui_mch_clear_block(
2827 int row1,
2828 int col1,
2829 int row2,
2830 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831{
2832 int x;
2833
2834 x = FILL_X(col1);
2835
2836 /* Clear one extra pixel at the far right, for when bold characters have
2837 * spilled over to the next column. */
2838 XFillRectangle(gui.dpy, gui.wid, gui.back_gc, x, FILL_Y(row1),
2839 (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1),
2840 (row2 - row1 + 1) * gui.char_height);
2841}
2842
2843 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002844gui_mch_clear_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845{
2846 XClearArea(gui.dpy, gui.wid, 0, 0, 0, 0, False);
2847}
2848
2849/*
2850 * Delete the given number of lines from the given row, scrolling up any
2851 * text further down within the scroll region.
2852 */
2853 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002854gui_mch_delete_lines(int row, int num_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855{
2856 if (gui.visibility == VisibilityFullyObscured)
2857 return; /* Can't see the window */
2858
2859 /* copy one extra pixel at the far right, for when bold has spilled
2860 * over */
2861 XCopyArea(gui.dpy, gui.wid, gui.wid, gui.text_gc,
2862 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
2863 gui.char_width * (gui.scroll_region_right - gui.scroll_region_left + 1)
2864 + (gui.scroll_region_right == Columns - 1),
2865 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1),
2866 FILL_X(gui.scroll_region_left), FILL_Y(row));
2867
2868 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
2869 gui.scroll_region_left,
2870 gui.scroll_region_bot, gui.scroll_region_right);
2871 gui_x11_check_copy_area();
2872}
2873
2874/*
2875 * Insert the given number of lines before the given row, scrolling down any
2876 * following text within the scroll region.
2877 */
2878 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002879gui_mch_insert_lines(int row, int num_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880{
2881 if (gui.visibility == VisibilityFullyObscured)
2882 return; /* Can't see the window */
2883
2884 /* copy one extra pixel at the far right, for when bold has spilled
2885 * over */
2886 XCopyArea(gui.dpy, gui.wid, gui.wid, gui.text_gc,
2887 FILL_X(gui.scroll_region_left), FILL_Y(row),
2888 gui.char_width * (gui.scroll_region_right - gui.scroll_region_left + 1)
2889 + (gui.scroll_region_right == Columns - 1),
2890 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1),
2891 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines));
2892
2893 gui_clear_block(row, gui.scroll_region_left,
2894 row + num_lines - 1, gui.scroll_region_right);
2895 gui_x11_check_copy_area();
2896}
2897
2898/*
2899 * Update the region revealed by scrolling up/down.
2900 */
2901 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002902gui_x11_check_copy_area(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903{
2904 XEvent event;
2905 XGraphicsExposeEvent *gevent;
2906
2907 if (gui.visibility != VisibilityPartiallyObscured)
2908 return;
2909
2910 XFlush(gui.dpy);
2911
2912 /* Wait to check whether the scroll worked or not */
2913 for (;;)
2914 {
2915 if (XCheckTypedEvent(gui.dpy, NoExpose, &event))
2916 return; /* The scroll worked. */
2917
2918 if (XCheckTypedEvent(gui.dpy, GraphicsExpose, &event))
2919 {
2920 gevent = (XGraphicsExposeEvent *)&event;
2921 gui_redraw(gevent->x, gevent->y, gevent->width, gevent->height);
2922 if (gevent->count == 0)
2923 return; /* This was the last expose event */
2924 }
2925 XSync(gui.dpy, False);
2926 }
2927}
2928
2929/*
2930 * X Selection stuff, for cutting and pasting text to other windows.
2931 */
2932
2933 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002934clip_mch_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935{
2936 clip_x11_lose_selection(vimShell, cbd);
2937}
2938
2939 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002940clip_mch_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941{
2942 return clip_x11_own_selection(vimShell, cbd);
2943}
2944
2945 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002946clip_mch_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947{
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002948 clip_x11_request_selection(vimShell, gui.dpy, cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949}
2950
2951 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002952clip_mch_set_selection(
2953 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
2955 clip_x11_set_selection(cbd);
2956}
2957
2958#if defined(FEAT_MENU) || defined(PROTO)
2959/*
2960 * Menu stuff.
2961 */
2962
2963/*
2964 * Make a menu either grey or not grey.
2965 */
2966 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002967gui_mch_menu_grey(vimmenu_T *menu, int grey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968{
2969 if (menu->id != (Widget)0)
2970 {
2971 gui_mch_menu_hidden(menu, False);
2972 if (grey
2973#ifdef FEAT_GUI_MOTIF
2974 || !menu->sensitive
2975#endif
2976 )
2977 XtSetSensitive(menu->id, False);
2978 else
2979 XtSetSensitive(menu->id, True);
2980 }
2981}
2982
2983/*
2984 * Make menu item hidden or not hidden
2985 */
2986 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002987gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988{
2989 if (menu->id != (Widget)0)
2990 {
2991 if (hidden)
2992 XtUnmanageChild(menu->id);
2993 else
2994 XtManageChild(menu->id);
2995 }
2996}
2997
2998/*
2999 * This is called after setting all the menus to grey/hidden or not.
3000 */
3001 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003002gui_mch_draw_menubar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003{
3004 /* Nothing to do in X */
3005}
3006
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003008gui_x11_menu_cb(
3009 Widget w UNUSED,
3010 XtPointer client_data,
3011 XtPointer call_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012{
3013 gui_menu_cb((vimmenu_T *)client_data);
3014}
3015
3016#endif /* FEAT_MENU */
3017
3018
3019
3020/*
3021 * Function called when window closed. Works like ":qa".
3022 * Should put up a requester!
3023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003025gui_x11_wm_protocol_handler(
3026 Widget w UNUSED,
3027 XtPointer client_data UNUSED,
3028 XEvent *event,
3029 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030{
3031 /*
3032 * Only deal with Client messages.
3033 */
3034 if (event->type != ClientMessage)
3035 return;
3036
3037 /*
3038 * The WM_SAVE_YOURSELF event arrives when the window manager wants to
3039 * exit. That can be cancelled though, thus Vim shouldn't exit here.
3040 * Just sync our swap files.
3041 */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003042 if ((Atom)((XClientMessageEvent *)event)->data.l[0] ==
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 wm_atoms[SAVE_YOURSELF_IDX])
3044 {
3045 out_flush();
3046 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
3047
3048 /* Set the window's WM_COMMAND property, to let the window manager
3049 * know we are done saving ourselves. We don't want to be restarted,
3050 * thus set argv to NULL. */
3051 XSetCommand(gui.dpy, XtWindow(vimShell), NULL, 0);
3052 return;
3053 }
3054
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003055 if ((Atom)((XClientMessageEvent *)event)->data.l[0] !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 wm_atoms[DELETE_WINDOW_IDX])
3057 return;
3058
3059 gui_shell_closed();
3060}
3061
3062#ifdef FEAT_CLIENTSERVER
3063/*
3064 * Function called when property changed. Check for incoming commands
3065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003067gui_x11_send_event_handler(
3068 Widget w UNUSED,
3069 XtPointer client_data UNUSED,
3070 XEvent *event,
3071 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072{
3073 XPropertyEvent *e = (XPropertyEvent *) event;
3074
3075 if (e->type == PropertyNotify && e->window == commWindow
3076 && e->atom == commProperty && e->state == PropertyNewValue)
3077 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003078 serverEventProc(gui.dpy, event, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 }
3080}
3081#endif
3082
3083/*
3084 * Cursor blink functions.
3085 *
3086 * This is a simple state machine:
3087 * BLINK_NONE not blinking at all
3088 * BLINK_OFF blinking, cursor is not shown
3089 * BLINK_ON blinking, cursor is shown
3090 */
3091
3092#define BLINK_NONE 0
3093#define BLINK_OFF 1
3094#define BLINK_ON 2
3095
3096static int blink_state = BLINK_NONE;
3097static long_u blink_waittime = 700;
3098static long_u blink_ontime = 400;
3099static long_u blink_offtime = 250;
3100static XtIntervalId blink_timer = (XtIntervalId)0;
3101
Bram Moolenaar703a8042016-06-04 16:24:32 +02003102 int
3103gui_mch_is_blinking(void)
3104{
3105 return blink_state != BLINK_NONE;
3106}
3107
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +02003108 int
3109gui_mch_is_blink_off(void)
3110{
3111 return blink_state == BLINK_OFF;
3112}
3113
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 void
Bram Moolenaar02fdaea2016-01-30 18:13:55 +01003115gui_mch_set_blinking(long waittime, long on, long off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116{
3117 blink_waittime = waittime;
3118 blink_ontime = on;
3119 blink_offtime = off;
3120}
3121
3122/*
3123 * Stop the cursor blinking. Show the cursor if it wasn't shown.
3124 */
3125 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01003126gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127{
3128 if (blink_timer != (XtIntervalId)0)
3129 {
3130 XtRemoveTimeOut(blink_timer);
3131 blink_timer = (XtIntervalId)0;
3132 }
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01003133 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 gui_update_cursor(TRUE, FALSE);
3135 blink_state = BLINK_NONE;
3136}
3137
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003139gui_x11_blink_cb(
3140 XtPointer timed_out UNUSED,
3141 XtIntervalId *interval_id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
3143 if (blink_state == BLINK_ON)
3144 {
3145 gui_undraw_cursor();
3146 blink_state = BLINK_OFF;
3147 blink_timer = XtAppAddTimeOut(app_context, blink_offtime,
3148 gui_x11_blink_cb, NULL);
3149 }
3150 else
3151 {
3152 gui_update_cursor(TRUE, FALSE);
3153 blink_state = BLINK_ON;
3154 blink_timer = XtAppAddTimeOut(app_context, blink_ontime,
3155 gui_x11_blink_cb, NULL);
3156 }
3157}
3158
3159/*
Bram Moolenaar1dccf632017-08-27 17:38:27 +02003160 * Start the cursor blinking. If it was already blinking, this restarts the
3161 * waiting time and shows the cursor.
3162 */
3163 void
3164gui_mch_start_blink(void)
3165{
3166 if (blink_timer != (XtIntervalId)0)
3167 XtRemoveTimeOut(blink_timer);
3168 /* Only switch blinking on if none of the times is zero */
3169 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
3170 {
3171 blink_timer = XtAppAddTimeOut(app_context, blink_waittime,
3172 gui_x11_blink_cb, NULL);
3173 blink_state = BLINK_ON;
3174 gui_update_cursor(TRUE, FALSE);
3175 }
3176}
3177
3178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 * Return the RGB value of a pixel as a long.
3180 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003181 guicolor_T
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003182gui_mch_get_rgb(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183{
3184 XColor xc;
3185 Colormap colormap;
3186
3187 colormap = DefaultColormap(gui.dpy, XDefaultScreen(gui.dpy));
3188 xc.pixel = pixel;
3189 XQueryColor(gui.dpy, colormap, &xc);
3190
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003191 return (guicolor_T)(((xc.red & 0xff00) << 8) + (xc.green & 0xff00)
3192 + ((unsigned)xc.blue >> 8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193}
3194
3195/*
3196 * Add the callback functions.
3197 */
3198 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003199gui_x11_callbacks(Widget textArea, Widget vimForm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200{
3201 XtAddEventHandler(textArea, VisibilityChangeMask, FALSE,
3202 gui_x11_visibility_cb, (XtPointer)0);
3203
3204 XtAddEventHandler(textArea, ExposureMask, FALSE, gui_x11_expose_cb,
3205 (XtPointer)0);
3206
3207 XtAddEventHandler(vimShell, StructureNotifyMask, FALSE,
3208 gui_x11_resize_window_cb, (XtPointer)0);
3209
3210 XtAddEventHandler(vimShell, FocusChangeMask, FALSE, gui_x11_focus_change_cb,
3211 (XtPointer)0);
3212 /*
3213 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3214 * Only needed for some window managers.
3215 */
3216 if (vim_strchr(p_go, GO_POINTER) != NULL)
3217 {
3218 XtAddEventHandler(vimShell, LeaveWindowMask, FALSE, gui_x11_leave_cb,
3219 (XtPointer)0);
3220 XtAddEventHandler(textArea, LeaveWindowMask, FALSE, gui_x11_leave_cb,
3221 (XtPointer)0);
3222 XtAddEventHandler(textArea, EnterWindowMask, FALSE, gui_x11_enter_cb,
3223 (XtPointer)0);
3224 XtAddEventHandler(vimShell, EnterWindowMask, FALSE, gui_x11_enter_cb,
3225 (XtPointer)0);
3226 }
3227
3228 XtAddEventHandler(vimForm, KeyPressMask, FALSE, gui_x11_key_hit_cb,
3229 (XtPointer)0);
3230 XtAddEventHandler(textArea, KeyPressMask, FALSE, gui_x11_key_hit_cb,
3231 (XtPointer)0);
3232
3233 /* get pointer moved events from scrollbar, needed for 'mousefocus' */
3234 XtAddEventHandler(vimForm, PointerMotionMask,
3235 FALSE, gui_x11_mouse_cb, (XtPointer)1);
3236 XtAddEventHandler(textArea, ButtonPressMask | ButtonReleaseMask |
3237 ButtonMotionMask | PointerMotionMask,
3238 FALSE, gui_x11_mouse_cb, (XtPointer)0);
3239}
3240
3241/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003242 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003244 void
3245gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246{
3247 int rootx, rooty, winx, winy;
3248 Window root, child;
3249 unsigned int mask;
3250
3251 if (gui.wid && XQueryPointer(gui.dpy, gui.wid, &root, &child,
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003252 &rootx, &rooty, &winx, &winy, &mask)) {
3253 *x = winx;
3254 *y = winy;
3255 } else {
3256 *x = -1;
3257 *y = -1;
3258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259}
3260
3261 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003262gui_mch_setmouse(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263{
3264 if (gui.wid)
3265 XWarpPointer(gui.dpy, (Window)0, gui.wid, 0, 0, 0, 0, x, y);
3266}
3267
3268#if (defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)) || defined(PROTO)
3269 XButtonPressedEvent *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003270gui_x11_get_last_mouse_event(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271{
3272 return &last_mouse_event;
3273}
3274#endif
3275
3276#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
3277
3278/* Signs are currently always 2 chars wide. Hopefully the font is big enough
3279 * to provide room for the bitmap! */
3280# define SIGN_WIDTH (gui.char_width * 2)
3281
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003283gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284{
3285 XImage *sign;
3286
3287 if (gui.in_use && (sign = (XImage *)sign_get_image(typenr)) != NULL)
3288 {
3289 XClearArea(gui.dpy, gui.wid, TEXT_X(col), TEXT_Y(row) - sign->height,
3290 SIGN_WIDTH, gui.char_height, FALSE);
3291 XPutImage(gui.dpy, gui.wid, gui.text_gc, sign, 0, 0,
3292 TEXT_X(col) + (SIGN_WIDTH - sign->width) / 2,
3293 TEXT_Y(row) - sign->height,
3294 sign->width, sign->height);
3295 }
3296}
3297
3298 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003299gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300{
3301 XpmAttributes attrs;
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003302 XImage *sign = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 int status;
3304
3305 /*
3306 * Setup the color substitution table.
3307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 if (signfile[0] != NUL && signfile[0] != '-')
3309 {
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003310 XpmColorSymbol color[5] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003312 {"none", NULL, 0},
3313 {"iconColor1", NULL, 0},
3314 {"bottomShadowColor", NULL, 0},
3315 {"topShadowColor", NULL, 0},
3316 {"selectColor", NULL, 0}
3317 };
3318 attrs.valuemask = XpmColorSymbols;
3319 attrs.numsymbols = 2;
3320 attrs.colorsymbols = color;
3321 attrs.colorsymbols[0].pixel = gui.back_pixel;
3322 attrs.colorsymbols[1].pixel = gui.norm_pixel;
3323 status = XpmReadFileToImage(gui.dpy, (char *)signfile,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 &sign, NULL, &attrs);
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003325 if (status == 0)
3326 {
3327 /* Sign width is fixed at two columns now.
3328 if (sign->width > gui.sign_width)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003329 gui.sign_width = sign->width + 8; */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 }
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003331 else
3332 EMSG(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 }
3334
3335 return (void *)sign;
3336}
3337
3338 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003339gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340{
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003341 XDestroyImage((XImage*)sign);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342}
3343#endif
3344
3345
3346#ifdef FEAT_MOUSESHAPE
3347/* The last set mouse pointer shape is remembered, to be used when it goes
3348 * from hidden to not hidden. */
3349static int last_shape = 0;
3350#endif
3351
3352/*
3353 * Use the blank mouse pointer or not.
3354 */
3355 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003356gui_mch_mousehide(
3357 int hide) /* TRUE = use blank ptr, FALSE = use parent ptr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358{
3359 if (gui.pointer_hidden != hide)
3360 {
3361 gui.pointer_hidden = hide;
3362 if (hide)
3363 XDefineCursor(gui.dpy, gui.wid, gui.blank_pointer);
3364 else
3365#ifdef FEAT_MOUSESHAPE
3366 mch_set_mouse_shape(last_shape);
3367#else
3368 XUndefineCursor(gui.dpy, gui.wid);
3369#endif
3370 }
3371}
3372
3373#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3374
3375/* Table for shape IDs. Keep in sync with the mshape_names[] table in
3376 * misc2.c! */
3377static int mshape_ids[] =
3378{
3379 XC_left_ptr, /* arrow */
3380 0, /* blank */
3381 XC_xterm, /* beam */
3382 XC_sb_v_double_arrow, /* updown */
3383 XC_sizing, /* udsizing */
3384 XC_sb_h_double_arrow, /* leftright */
3385 XC_sizing, /* lrsizing */
3386 XC_watch, /* busy */
3387 XC_X_cursor, /* no */
3388 XC_crosshair, /* crosshair */
3389 XC_hand1, /* hand1 */
3390 XC_hand2, /* hand2 */
3391 XC_pencil, /* pencil */
3392 XC_question_arrow, /* question */
3393 XC_right_ptr, /* right-arrow */
3394 XC_center_ptr, /* up-arrow */
3395 XC_left_ptr /* last one */
3396};
3397
3398 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003399mch_set_mouse_shape(int shape)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
3401 int id;
3402
3403 if (!gui.in_use)
3404 return;
3405
3406 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
3407 XDefineCursor(gui.dpy, gui.wid, gui.blank_pointer);
3408 else
3409 {
3410 if (shape >= MSHAPE_NUMBERED)
3411 {
3412 id = shape - MSHAPE_NUMBERED;
3413 if (id >= XC_num_glyphs)
3414 id = XC_left_ptr;
3415 else
3416 id &= ~1; /* they are always even (why?) */
3417 }
3418 else
3419 id = mshape_ids[shape];
3420
3421 XDefineCursor(gui.dpy, gui.wid, XCreateFontCursor(gui.dpy, id));
3422 }
3423 if (shape != MSHAPE_HIDE)
3424 last_shape = shape;
3425}
3426#endif
3427
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003428#if (defined(FEAT_TOOLBAR) && defined(FEAT_BEVAL_GUI)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429/*
3430 * Set the balloon-eval used for the tooltip of a toolbar menu item.
3431 * The check for a non-toolbar item was added, because there is a crash when
3432 * passing a normal menu item here. Can't explain that, but better avoid it.
3433 */
3434 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003435gui_mch_menu_set_tip(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
3437 if (menu->id != NULL && menu->parent != NULL
3438 && menu_is_toolbar(menu->parent->name))
3439 {
3440 /* Always destroy and create the balloon, in case the string was
3441 * changed. */
3442 if (menu->tip != NULL)
3443 {
3444 gui_mch_destroy_beval_area(menu->tip);
3445 menu->tip = NULL;
3446 }
3447 if (menu->strings[MENU_INDEX_TIP] != NULL)
3448 menu->tip = gui_mch_create_beval_area(
3449 menu->id,
3450 menu->strings[MENU_INDEX_TIP],
3451 NULL,
3452 NULL);
3453 }
3454}
3455#endif