blob: 9d8ea7e4a6360668f50047add09737b3d58c0858 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI/Motif support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10/*
11 * Common code for the Motif and Athena GUI.
12 * Not used for GTK.
13 */
14
Bram Moolenaarf7506ca2017-02-25 16:01:49 +010015#include "vim.h"
16
Bram Moolenaar071d4272004-06-13 20:20:40 +000017#include <X11/keysym.h>
18#include <X11/Xatom.h>
19#include <X11/StringDefs.h>
20#include <X11/Intrinsic.h>
21#include <X11/Shell.h>
22#include <X11/cursorfont.h>
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaarbb1969b2019-01-17 15:45:25 +010025 * XpmP.h is preferred, because it makes the signs drawn with a transparent
26 * background instead of black.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027 */
28#if defined(HAVE_XM_XPMP_H) && defined(FEAT_GUI_MOTIF) \
Bram Moolenaarbb1969b2019-01-17 15:45:25 +010029 && !defined(HAVE_X11_XPM_H)
Bram Moolenaar071d4272004-06-13 20:20:40 +000030# include <Xm/XpmP.h>
31#else
32# ifdef HAVE_X11_XPM_H
33# include <X11/xpm.h>
34# endif
35#endif
36
37#ifdef FEAT_XFONTSET
38# ifdef X_LOCALE
39# include <X11/Xlocale.h>
40# else
41# include <locale.h>
42# endif
43#endif
44
45#ifdef HAVE_X11_SUNKEYSYM_H
46# include <X11/Sunkeysym.h>
47#endif
48
49#ifdef HAVE_X11_XMU_EDITRES_H
50# include <X11/Xmu/Editres.h>
51#endif
52
Bram Moolenaar071d4272004-06-13 20:20:40 +000053#define VIM_NAME "vim"
54#define VIM_CLASS "Vim"
55
56/* Default resource values */
57#define DFLT_FONT "7x13"
58#ifdef FONTSET_ALWAYS
59# define DFLT_MENU_FONT XtDefaultFontSet
60#else
61# define DFLT_MENU_FONT XtDefaultFont
62#endif
63#define DFLT_TOOLTIP_FONT XtDefaultFontSet
64
65#ifdef FEAT_GUI_ATHENA
66# define DFLT_MENU_BG_COLOR "gray77"
67# define DFLT_MENU_FG_COLOR "black"
68# define DFLT_SCROLL_BG_COLOR "gray60"
69# define DFLT_SCROLL_FG_COLOR "gray77"
Bram Moolenaar46582282016-07-23 14:35:12 +020070# define DFLT_TOOLTIP_BG_COLOR "#ffff91"
71# define DFLT_TOOLTIP_FG_COLOR "#000000"
Bram Moolenaar071d4272004-06-13 20:20:40 +000072#else
73/* use the default (CDE) colors */
74# define DFLT_MENU_BG_COLOR ""
75# define DFLT_MENU_FG_COLOR ""
76# define DFLT_SCROLL_BG_COLOR ""
77# define DFLT_SCROLL_FG_COLOR ""
Bram Moolenaar46582282016-07-23 14:35:12 +020078# define DFLT_TOOLTIP_BG_COLOR "#ffff91"
79# define DFLT_TOOLTIP_FG_COLOR "#000000"
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#endif
81
82Widget vimShell = (Widget)0;
83
84static Atom wm_atoms[2]; /* Window Manager Atoms */
85#define DELETE_WINDOW_IDX 0 /* index in wm_atoms[] for WM_DELETE_WINDOW */
86#define SAVE_YOURSELF_IDX 1 /* index in wm_atoms[] for WM_SAVE_YOURSELF */
87
88#ifdef FEAT_XFONTSET
89/*
90 * We either draw with a fontset (when current_fontset != NULL) or with a
91 * normal font (current_fontset == NULL, use gui.text_gc and gui.back_gc).
92 */
93static XFontSet current_fontset = NULL;
94
95#define XDrawString(dpy, win, gc, x, y, str, n) \
96 do \
97 { \
98 if (current_fontset != NULL) \
99 XmbDrawString(dpy, win, current_fontset, gc, x, y, str, n); \
100 else \
101 XDrawString(dpy, win, gc, x, y, str, n); \
102 } while (0)
103
104#define XDrawString16(dpy, win, gc, x, y, str, n) \
105 do \
106 { \
107 if (current_fontset != NULL) \
108 XwcDrawString(dpy, win, current_fontset, gc, x, y, (wchar_t *)str, n); \
109 else \
Bram Moolenaara3227e22006-03-08 21:32:40 +0000110 XDrawString16(dpy, win, gc, x, y, (XChar2b *)str, n); \
111 } while (0)
112
113#define XDrawImageString16(dpy, win, gc, x, y, str, n) \
114 do \
115 { \
116 if (current_fontset != NULL) \
117 XwcDrawImageString(dpy, win, current_fontset, gc, x, y, (wchar_t *)str, n); \
118 else \
119 XDrawImageString16(dpy, win, gc, x, y, (XChar2b *)str, n); \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 } while (0)
121
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100122static int check_fontset_sanity(XFontSet fs);
123static int fontset_width(XFontSet fs);
124static int fontset_ascent(XFontSet fs);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#endif
126
127static guicolor_T prev_fg_color = INVALCOLOR;
128static guicolor_T prev_bg_color = INVALCOLOR;
Bram Moolenaarfb269802005-03-15 22:46:30 +0000129static guicolor_T prev_sp_color = INVALCOLOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130
131#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
132static XButtonPressedEvent last_mouse_event;
133#endif
134
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100135static void gui_x11_check_copy_area(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136#ifdef FEAT_CLIENTSERVER
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100137static void gui_x11_send_event_handler(Widget, XtPointer, XEvent *, Boolean *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100139static void gui_x11_wm_protocol_handler(Widget, XtPointer, XEvent *, Boolean *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100140static Cursor gui_x11_create_blank_mouse(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141
142
143/*
144 * Keycodes recognized by vim.
145 * NOTE: when changing this, the table in gui_gtk_x11.c probably needs the
146 * same change!
147 */
148static struct specialkey
149{
150 KeySym key_sym;
151 char_u vim_code0;
152 char_u vim_code1;
153} special_keys[] =
154{
155 {XK_Up, 'k', 'u'},
156 {XK_Down, 'k', 'd'},
157 {XK_Left, 'k', 'l'},
158 {XK_Right, 'k', 'r'},
159
160 {XK_F1, 'k', '1'},
161 {XK_F2, 'k', '2'},
162 {XK_F3, 'k', '3'},
163 {XK_F4, 'k', '4'},
164 {XK_F5, 'k', '5'},
165 {XK_F6, 'k', '6'},
166 {XK_F7, 'k', '7'},
167 {XK_F8, 'k', '8'},
168 {XK_F9, 'k', '9'},
169 {XK_F10, 'k', ';'},
170
171 {XK_F11, 'F', '1'},
172 {XK_F12, 'F', '2'},
173 {XK_F13, 'F', '3'},
174 {XK_F14, 'F', '4'},
175 {XK_F15, 'F', '5'},
176 {XK_F16, 'F', '6'},
177 {XK_F17, 'F', '7'},
178 {XK_F18, 'F', '8'},
179 {XK_F19, 'F', '9'},
180 {XK_F20, 'F', 'A'},
181
182 {XK_F21, 'F', 'B'},
183 {XK_F22, 'F', 'C'},
184 {XK_F23, 'F', 'D'},
185 {XK_F24, 'F', 'E'},
186 {XK_F25, 'F', 'F'},
187 {XK_F26, 'F', 'G'},
188 {XK_F27, 'F', 'H'},
189 {XK_F28, 'F', 'I'},
190 {XK_F29, 'F', 'J'},
191 {XK_F30, 'F', 'K'},
192
193 {XK_F31, 'F', 'L'},
194 {XK_F32, 'F', 'M'},
195 {XK_F33, 'F', 'N'},
196 {XK_F34, 'F', 'O'},
197 {XK_F35, 'F', 'P'}, /* keysymdef.h defines up to F35 */
198#ifdef SunXK_F36
199 {SunXK_F36, 'F', 'Q'},
200 {SunXK_F37, 'F', 'R'},
201#endif
202
203 {XK_Help, '%', '1'},
204 {XK_Undo, '&', '8'},
205 {XK_BackSpace, 'k', 'b'},
206 {XK_Insert, 'k', 'I'},
207 {XK_Delete, 'k', 'D'},
208 {XK_Home, 'k', 'h'},
209 {XK_End, '@', '7'},
210 {XK_Prior, 'k', 'P'},
211 {XK_Next, 'k', 'N'},
212 {XK_Print, '%', '9'},
213
214 /* Keypad keys: */
215#ifdef XK_KP_Left
216 {XK_KP_Left, 'k', 'l'},
217 {XK_KP_Right, 'k', 'r'},
218 {XK_KP_Up, 'k', 'u'},
219 {XK_KP_Down, 'k', 'd'},
220 {XK_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
221 {XK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
222 {XK_KP_Home, 'K', '1'},
223 {XK_KP_End, 'K', '4'},
224 {XK_KP_Prior, 'K', '3'},
225 {XK_KP_Next, 'K', '5'},
226
227 {XK_KP_Add, 'K', '6'},
228 {XK_KP_Subtract, 'K', '7'},
229 {XK_KP_Divide, 'K', '8'},
230 {XK_KP_Multiply, 'K', '9'},
231 {XK_KP_Enter, 'K', 'A'},
232 {XK_KP_Decimal, 'K', 'B'},
233
234 {XK_KP_0, 'K', 'C'},
235 {XK_KP_1, 'K', 'D'},
236 {XK_KP_2, 'K', 'E'},
237 {XK_KP_3, 'K', 'F'},
238 {XK_KP_4, 'K', 'G'},
239 {XK_KP_5, 'K', 'H'},
240 {XK_KP_6, 'K', 'I'},
241 {XK_KP_7, 'K', 'J'},
242 {XK_KP_8, 'K', 'K'},
243 {XK_KP_9, 'K', 'L'},
244#endif
245
246 /* End of list marker: */
247 {(KeySym)0, 0, 0}
248};
249
250#define XtNboldFont "boldFont"
251#define XtCBoldFont "BoldFont"
252#define XtNitalicFont "italicFont"
253#define XtCItalicFont "ItalicFont"
254#define XtNboldItalicFont "boldItalicFont"
255#define XtCBoldItalicFont "BoldItalicFont"
256#define XtNscrollbarWidth "scrollbarWidth"
257#define XtCScrollbarWidth "ScrollbarWidth"
258#define XtNmenuHeight "menuHeight"
259#define XtCMenuHeight "MenuHeight"
260#define XtNmenuFont "menuFont"
261#define XtCMenuFont "MenuFont"
262#define XtNmenuFontSet "menuFontSet"
263#define XtCMenuFontSet "MenuFontSet"
264
265
266/* Resources for setting the foreground and background colors of menus */
267#define XtNmenuBackground "menuBackground"
268#define XtCMenuBackground "MenuBackground"
269#define XtNmenuForeground "menuForeground"
270#define XtCMenuForeground "MenuForeground"
271
272/* Resources for setting the foreground and background colors of scrollbars */
273#define XtNscrollBackground "scrollBackground"
274#define XtCScrollBackground "ScrollBackground"
275#define XtNscrollForeground "scrollForeground"
276#define XtCScrollForeground "ScrollForeground"
277
278/* Resources for setting the foreground and background colors of tooltip */
279#define XtNtooltipBackground "tooltipBackground"
280#define XtCTooltipBackground "TooltipBackground"
281#define XtNtooltipForeground "tooltipForeground"
282#define XtCTooltipForeground "TooltipForeground"
283#define XtNtooltipFont "tooltipFont"
284#define XtCTooltipFont "TooltipFont"
285
286/*
287 * X Resources:
288 */
289static XtResource vim_resources[] =
290{
291 {
292 XtNforeground,
293 XtCForeground,
294 XtRPixel,
295 sizeof(Pixel),
296 XtOffsetOf(gui_T, def_norm_pixel),
297 XtRString,
298 XtDefaultForeground
299 },
300 {
301 XtNbackground,
302 XtCBackground,
303 XtRPixel,
304 sizeof(Pixel),
305 XtOffsetOf(gui_T, def_back_pixel),
306 XtRString,
307 XtDefaultBackground
308 },
309 {
310 XtNfont,
311 XtCFont,
312 XtRString,
313 sizeof(String *),
314 XtOffsetOf(gui_T, rsrc_font_name),
315 XtRImmediate,
316 XtDefaultFont
317 },
318 {
319 XtNboldFont,
320 XtCBoldFont,
321 XtRString,
322 sizeof(String *),
323 XtOffsetOf(gui_T, rsrc_bold_font_name),
324 XtRImmediate,
325 ""
326 },
327 {
328 XtNitalicFont,
329 XtCItalicFont,
330 XtRString,
331 sizeof(String *),
332 XtOffsetOf(gui_T, rsrc_ital_font_name),
333 XtRImmediate,
334 ""
335 },
336 {
337 XtNboldItalicFont,
338 XtCBoldItalicFont,
339 XtRString,
340 sizeof(String *),
341 XtOffsetOf(gui_T, rsrc_boldital_font_name),
342 XtRImmediate,
343 ""
344 },
345 {
346 XtNgeometry,
347 XtCGeometry,
348 XtRString,
349 sizeof(String *),
350 XtOffsetOf(gui_T, geom),
351 XtRImmediate,
352 ""
353 },
354 {
355 XtNreverseVideo,
356 XtCReverseVideo,
357 XtRBool,
358 sizeof(Bool),
359 XtOffsetOf(gui_T, rsrc_rev_video),
360 XtRImmediate,
361 (XtPointer)False
362 },
363 {
364 XtNborderWidth,
365 XtCBorderWidth,
366 XtRInt,
367 sizeof(int),
368 XtOffsetOf(gui_T, border_width),
369 XtRImmediate,
370 (XtPointer)2
371 },
372 {
373 XtNscrollbarWidth,
374 XtCScrollbarWidth,
375 XtRInt,
376 sizeof(int),
377 XtOffsetOf(gui_T, scrollbar_width),
378 XtRImmediate,
379 (XtPointer)SB_DEFAULT_WIDTH
380 },
381#ifdef FEAT_MENU
382# ifdef FEAT_GUI_ATHENA /* with Motif the height is always computed */
383 {
384 XtNmenuHeight,
385 XtCMenuHeight,
386 XtRInt,
387 sizeof(int),
388 XtOffsetOf(gui_T, menu_height),
389 XtRImmediate,
390 (XtPointer)MENU_DEFAULT_HEIGHT /* Should figure out at run time */
391 },
392# endif
393 {
394# ifdef FONTSET_ALWAYS
395 XtNmenuFontSet,
396 XtCMenuFontSet,
397#else
398 XtNmenuFont,
399 XtCMenuFont,
400#endif
401 XtRString,
402 sizeof(char *),
403 XtOffsetOf(gui_T, rsrc_menu_font_name),
404 XtRString,
405 DFLT_MENU_FONT
406 },
407#endif
408 {
409 XtNmenuForeground,
410 XtCMenuForeground,
411 XtRString,
412 sizeof(char *),
413 XtOffsetOf(gui_T, rsrc_menu_fg_name),
414 XtRString,
415 DFLT_MENU_FG_COLOR
416 },
417 {
418 XtNmenuBackground,
419 XtCMenuBackground,
420 XtRString,
421 sizeof(char *),
422 XtOffsetOf(gui_T, rsrc_menu_bg_name),
423 XtRString,
424 DFLT_MENU_BG_COLOR
425 },
426 {
427 XtNscrollForeground,
428 XtCScrollForeground,
429 XtRString,
430 sizeof(char *),
431 XtOffsetOf(gui_T, rsrc_scroll_fg_name),
432 XtRString,
433 DFLT_SCROLL_FG_COLOR
434 },
435 {
436 XtNscrollBackground,
437 XtCScrollBackground,
438 XtRString,
439 sizeof(char *),
440 XtOffsetOf(gui_T, rsrc_scroll_bg_name),
441 XtRString,
442 DFLT_SCROLL_BG_COLOR
443 },
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100444#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 {
446 XtNtooltipForeground,
447 XtCTooltipForeground,
448 XtRString,
449 sizeof(char *),
450 XtOffsetOf(gui_T, rsrc_tooltip_fg_name),
451 XtRString,
452 DFLT_TOOLTIP_FG_COLOR
453 },
454 {
455 XtNtooltipBackground,
456 XtCTooltipBackground,
457 XtRString,
458 sizeof(char *),
459 XtOffsetOf(gui_T, rsrc_tooltip_bg_name),
460 XtRString,
461 DFLT_TOOLTIP_BG_COLOR
462 },
463 {
464 XtNtooltipFont,
465 XtCTooltipFont,
466 XtRString,
467 sizeof(char *),
468 XtOffsetOf(gui_T, rsrc_tooltip_font_name),
469 XtRString,
470 DFLT_TOOLTIP_FONT
471 },
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100472 /* This one may not be really needed? */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 {
474 "balloonEvalFontSet",
475 XtCFontSet,
476 XtRFontSet,
477 sizeof(XFontSet),
478 XtOffsetOf(gui_T, tooltip_fontset),
479 XtRImmediate,
480 (XtPointer)NOFONTSET
481 },
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100482#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483#ifdef FEAT_XIM
484 {
485 "preeditType",
486 "PreeditType",
487 XtRString,
488 sizeof(char*),
489 XtOffsetOf(gui_T, rsrc_preedit_type_name),
490 XtRString,
491 (XtPointer)"OverTheSpot,OffTheSpot,Root"
492 },
493 {
494 "inputMethod",
495 "InputMethod",
496 XtRString,
497 sizeof(char*),
498 XtOffsetOf(gui_T, rsrc_input_method),
499 XtRString,
500 NULL
501 },
502#endif /* FEAT_XIM */
503};
504
505/*
506 * This table holds all the X GUI command line options allowed. This includes
507 * the standard ones so that we can skip them when vim is started without the
508 * GUI (but the GUI might start up later).
509 * When changing this, also update doc/vim_gui.txt and the usage message!!!
510 */
511static XrmOptionDescRec cmdline_options[] =
512{
513 /* We handle these options ourselves */
514 {"-bg", ".background", XrmoptionSepArg, NULL},
515 {"-background", ".background", XrmoptionSepArg, NULL},
516 {"-fg", ".foreground", XrmoptionSepArg, NULL},
517 {"-foreground", ".foreground", XrmoptionSepArg, NULL},
518 {"-fn", ".font", XrmoptionSepArg, NULL},
519 {"-font", ".font", XrmoptionSepArg, NULL},
520 {"-boldfont", ".boldFont", XrmoptionSepArg, NULL},
521 {"-italicfont", ".italicFont", XrmoptionSepArg, NULL},
522 {"-geom", ".geometry", XrmoptionSepArg, NULL},
523 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
524 {"-reverse", "*reverseVideo", XrmoptionNoArg, "True"},
525 {"-rv", "*reverseVideo", XrmoptionNoArg, "True"},
526 {"+reverse", "*reverseVideo", XrmoptionNoArg, "False"},
527 {"+rv", "*reverseVideo", XrmoptionNoArg, "False"},
528 {"-display", ".display", XrmoptionSepArg, NULL},
Bram Moolenaara5792f52005-11-23 21:25:05 +0000529 {"-iconic", ".iconic", XrmoptionNoArg, "True"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 {"-name", ".name", XrmoptionSepArg, NULL},
531 {"-bw", ".borderWidth", XrmoptionSepArg, NULL},
532 {"-borderwidth", ".borderWidth", XrmoptionSepArg, NULL},
533 {"-sw", ".scrollbarWidth", XrmoptionSepArg, NULL},
534 {"-scrollbarwidth", ".scrollbarWidth", XrmoptionSepArg, NULL},
535 {"-mh", ".menuHeight", XrmoptionSepArg, NULL},
536 {"-menuheight", ".menuHeight", XrmoptionSepArg, NULL},
537#ifdef FONTSET_ALWAYS
538 {"-mf", ".menuFontSet", XrmoptionSepArg, NULL},
539 {"-menufont", ".menuFontSet", XrmoptionSepArg, NULL},
540 {"-menufontset", ".menuFontSet", XrmoptionSepArg, NULL},
541#else
542 {"-mf", ".menuFont", XrmoptionSepArg, NULL},
543 {"-menufont", ".menuFont", XrmoptionSepArg, NULL},
544#endif
545 {"-xrm", NULL, XrmoptionResArg, NULL}
546};
547
548static int gui_argc = 0;
549static char **gui_argv = NULL;
550
551/*
552 * Call-back routines.
553 */
554
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100556gui_x11_timer_cb(
557 XtPointer timed_out,
558 XtIntervalId *interval_id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559{
560 *((int *)timed_out) = TRUE;
561}
562
Bram Moolenaar1dccf632017-08-27 17:38:27 +0200563#ifdef FEAT_JOB_CHANNEL
564 static void
565channel_poll_cb(
566 XtPointer client_data,
567 XtIntervalId *interval_id UNUSED)
568{
569 XtIntervalId *channel_timer = (XtIntervalId *)client_data;
570
571 /* Using an event handler for a channel that may be disconnected does
572 * not work, it hangs. Instead poll for messages. */
573 channel_handle_events(TRUE);
574 parse_queued_messages();
575
576 /* repeat */
577 *channel_timer = XtAppAddTimeOut(app_context, (long_u)20,
578 channel_poll_cb, client_data);
579}
580#endif
581
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100583gui_x11_visibility_cb(
584 Widget w UNUSED,
585 XtPointer dud UNUSED,
586 XEvent *event,
587 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
589 if (event->type != VisibilityNotify)
590 return;
591
592 gui.visibility = event->xvisibility.state;
593
594 /*
595 * When we do an XCopyArea(), and the window is partially obscured, we want
596 * to receive an event to tell us whether it worked or not.
597 */
598 XSetGraphicsExposures(gui.dpy, gui.text_gc,
599 gui.visibility != VisibilityUnobscured);
600
601 /* This is needed for when redrawing is slow. */
602 gui_mch_update();
603}
604
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100606gui_x11_expose_cb(
607 Widget w UNUSED,
608 XtPointer dud UNUSED,
609 XEvent *event,
610 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611{
612 XExposeEvent *gevent;
613 int new_x;
614
615 if (event->type != Expose)
616 return;
617
618 out_flush(); /* make sure all output has been processed */
619
620 gevent = (XExposeEvent *)event;
621 gui_redraw(gevent->x, gevent->y, gevent->width, gevent->height);
622
623 new_x = FILL_X(0);
624
625 /* Clear the border areas if needed */
626 if (gevent->x < new_x)
627 XClearArea(gui.dpy, gui.wid, 0, 0, new_x, 0, False);
628 if (gevent->y < FILL_Y(0))
629 XClearArea(gui.dpy, gui.wid, 0, 0, 0, FILL_Y(0), False);
630 if (gevent->x > FILL_X(Columns))
631 XClearArea(gui.dpy, gui.wid, FILL_X((int)Columns), 0, 0, 0, False);
632 if (gevent->y > FILL_Y(Rows))
633 XClearArea(gui.dpy, gui.wid, 0, FILL_Y((int)Rows), 0, 0, False);
634
635 /* This is needed for when redrawing is slow. */
636 gui_mch_update();
637}
638
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100639#if (defined(FEAT_NETBEANS_INTG) && defined(FEAT_GUI_MOTIF)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640/*
Bram Moolenaar231334e2005-07-25 20:46:57 +0000641 * This function fills in the XRectangle object with the current x,y
642 * coordinates and height, width so that an XtVaSetValues to the same shell of
Bram Moolenaar49325942007-05-10 19:19:59 +0000643 * those resources will restore the window to its former position and
Bram Moolenaar231334e2005-07-25 20:46:57 +0000644 * dimensions.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 *
Bram Moolenaar231334e2005-07-25 20:46:57 +0000646 * Note: This function may fail, in which case the XRectangle will be
647 * unchanged. Be sure to have the XRectangle set with the proper values for a
648 * failed condition prior to calling this function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 */
650 static void
651shellRectangle(Widget shell, XRectangle *r)
652{
653 Window rootw, shellw, child, parentw;
654 int absx, absy;
655 XWindowAttributes a;
656 Window *children;
657 unsigned int childrenCount;
658
659 shellw = XtWindow(shell);
660 if (shellw == 0)
661 return;
662 for (;;)
663 {
664 XQueryTree(XtDisplay(shell), shellw, &rootw, &parentw,
665 &children, &childrenCount);
666 XFree(children);
667 if (parentw == rootw)
668 break;
669 shellw = parentw;
670 }
671 XGetWindowAttributes(XtDisplay(shell), shellw, &a);
672 XTranslateCoordinates(XtDisplay(shell), shellw, a.root, 0, 0,
673 &absx, &absy, &child);
674 r->x = absx;
675 r->y = absy;
676 XtVaGetValues(shell, XmNheight, &r->height, XmNwidth, &r->width, NULL);
677}
678#endif
679
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100681gui_x11_resize_window_cb(
682 Widget w UNUSED,
683 XtPointer dud UNUSED,
684 XEvent *event,
685 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686{
687 static int lastWidth, lastHeight;
688
689 if (event->type != ConfigureNotify)
690 return;
691
692 if (event->xconfigure.width != lastWidth
693 || event->xconfigure.height != lastHeight)
694 {
695 lastWidth = event->xconfigure.width;
696 lastHeight = event->xconfigure.height;
697 gui_resize_shell(event->xconfigure.width, event->xconfigure.height
698#ifdef FEAT_XIM
699 - xim_get_status_area_height()
700#endif
701 );
702 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200703#if defined(FEAT_NETBEANS_INTG) && defined(FEAT_GUI_MOTIF)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200704 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {
706 XRectangle rec;
707
708 shellRectangle(w, &rec);
709 netbeans_frame_moved(rec.x, rec.y);
710 }
711#endif
712#ifdef FEAT_XIM
713 xim_set_preedit();
714#endif
715}
716
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100718gui_x11_focus_change_cb(
719 Widget w UNUSED,
720 XtPointer data UNUSED,
721 XEvent *event,
722 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723{
724 gui_focus_change(event->type == FocusIn);
725}
726
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100728gui_x11_enter_cb(
729 Widget w UNUSED,
730 XtPointer data UNUSED,
731 XEvent *event UNUSED,
732 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733{
734 gui_focus_change(TRUE);
735}
736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100738gui_x11_leave_cb(
739 Widget w UNUSED,
740 XtPointer data UNUSED,
741 XEvent *event UNUSED,
742 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743{
744 gui_focus_change(FALSE);
745}
746
747#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
748# if X_HAVE_UTF8_STRING
749# define USE_UTF8LOOKUP
750# endif
751#endif
752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100754gui_x11_key_hit_cb(
755 Widget w UNUSED,
756 XtPointer dud UNUSED,
757 XEvent *event,
758 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759{
760 XKeyPressedEvent *ev_press;
761#ifdef FEAT_XIM
762 char_u string2[256];
763 char_u string_shortbuf[256];
764 char_u *string = string_shortbuf;
765 Boolean string_alloced = False;
766 Status status;
767#else
768 char_u string[4], string2[3];
769#endif
770 KeySym key_sym, key_sym2;
771 int len, len2;
772 int i;
773 int modifiers;
774 int key;
775
776 ev_press = (XKeyPressedEvent *)event;
777
778#ifdef FEAT_XIM
779 if (xic)
780 {
781# ifdef USE_UTF8LOOKUP
782 /* XFree86 4.0.2 or newer: Be able to get UTF-8 characters even when
783 * the locale isn't utf-8. */
784 if (enc_utf8)
785 len = Xutf8LookupString(xic, ev_press, (char *)string,
786 sizeof(string_shortbuf), &key_sym, &status);
787 else
788# endif
789 len = XmbLookupString(xic, ev_press, (char *)string,
790 sizeof(string_shortbuf), &key_sym, &status);
791 if (status == XBufferOverflow)
792 {
793 string = (char_u *)XtMalloc(len + 1);
794 string_alloced = True;
795# ifdef USE_UTF8LOOKUP
796 /* XFree86 4.0.2 or newer: Be able to get UTF-8 characters even
797 * when the locale isn't utf-8. */
798 if (enc_utf8)
799 len = Xutf8LookupString(xic, ev_press, (char *)string,
800 len, &key_sym, &status);
801 else
802# endif
803 len = XmbLookupString(xic, ev_press, (char *)string,
804 len, &key_sym, &status);
805 }
806 if (status == XLookupNone || status == XLookupChars)
807 key_sym = XK_VoidSymbol;
808
809# ifdef FEAT_MBYTE
810 /* Do conversion from 'termencoding' to 'encoding'. When using
811 * Xutf8LookupString() it has already been done. */
812 if (len > 0 && input_conv.vc_type != CONV_NONE
813# ifdef USE_UTF8LOOKUP
814 && !enc_utf8
815# endif
816 )
817 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100818 int maxlen = len * 4 + 40; /* guessed */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 char_u *p = (char_u *)XtMalloc(maxlen);
820
821 mch_memmove(p, string, len);
822 if (string_alloced)
823 XtFree((char *)string);
824 string = p;
825 string_alloced = True;
826 len = convert_input(p, len, maxlen);
827 }
828# endif
829
830 /* Translate CSI to K_CSI, otherwise it could be recognized as the
831 * start of a special key. */
832 for (i = 0; i < len; ++i)
833 if (string[i] == CSI)
834 {
835 char_u *p = (char_u *)XtMalloc(len + 3);
836
837 mch_memmove(p, string, i + 1);
838 p[i + 1] = KS_EXTRA;
839 p[i + 2] = (int)KE_CSI;
840 mch_memmove(p + i + 3, string + i + 1, len - i);
841 if (string_alloced)
842 XtFree((char *)string);
843 string = p;
844 string_alloced = True;
845 i += 2;
846 len += 2;
847 }
848 }
849 else
850#endif
851 len = XLookupString(ev_press, (char *)string, sizeof(string),
852 &key_sym, NULL);
853
854#ifdef SunXK_F36
855 /*
856 * These keys have bogus lookup strings, and trapping them here is
857 * easier than trying to XRebindKeysym() on them with every possible
858 * combination of modifiers.
859 */
860 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
861 len = 0;
862#endif
863
864#ifdef FEAT_HANGULIN
865 if ((key_sym == XK_space) && (ev_press->state & ShiftMask))
866 {
867 hangul_input_state_toggle();
868 goto theend;
869 }
870#endif
871
872 if (key_sym == XK_space)
873 string[0] = ' '; /* Otherwise Ctrl-Space doesn't work */
874
875 /*
876 * Only on some machines ^_ requires Ctrl+Shift+minus. For consistency,
877 * allow just Ctrl+minus too.
878 */
879 if (key_sym == XK_minus && (ev_press->state & ControlMask))
880 string[0] = Ctrl__;
881
882#ifdef XK_ISO_Left_Tab
883 /* why do we get XK_ISO_Left_Tab instead of XK_Tab for shift-tab? */
884 if (key_sym == XK_ISO_Left_Tab)
885 {
886 key_sym = XK_Tab;
887 string[0] = TAB;
888 len = 1;
889 }
890#endif
891
892 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
893 * that already has the 8th bit set. And not when using a double-byte
894 * encoding, setting the 8th bit may make it the lead byte of a
895 * double-byte character. */
896 if (len == 1
897 && (ev_press->state & Mod1Mask)
898 && !(key_sym == XK_BackSpace || key_sym == XK_Delete)
899 && (string[0] & 0x80) == 0
900#ifdef FEAT_MBYTE
901 && !enc_dbcs
902#endif
903 )
904 {
905#if defined(FEAT_MENU) && defined(FEAT_GUI_MOTIF)
906 /* Ignore ALT keys when they are used for the menu only */
907 if (gui.menu_is_active
908 && (p_wak[0] == 'y'
909 || (p_wak[0] == 'm' && gui_is_menu_shortcut(string[0]))))
910 goto theend;
911#endif
912 /*
913 * Before we set the 8th bit, check to make sure the user doesn't
914 * already have a mapping defined for this sequence. We determine this
915 * by checking to see if the input would be the same without the
916 * Alt/Meta key.
917 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
918 */
919 ev_press->state &= ~Mod1Mask;
920 len2 = XLookupString(ev_press, (char *)string2, sizeof(string2),
921 &key_sym2, NULL);
922 if (key_sym2 == XK_space)
923 string2[0] = ' '; /* Otherwise Meta-Ctrl-Space doesn't work */
924 if ( len2 == 1
925 && string[0] == string2[0]
926 && !(key_sym == XK_Tab && (ev_press->state & ShiftMask)))
927 {
928 string[0] |= 0x80;
929#ifdef FEAT_MBYTE
930 if (enc_utf8) /* convert to utf-8 */
931 {
932 string[1] = string[0] & 0xbf;
933 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
934 if (string[1] == CSI)
935 {
936 string[2] = KS_EXTRA;
937 string[3] = (int)KE_CSI;
938 len = 4;
939 }
940 else
941 len = 2;
942 }
943#endif
944 }
945 else
946 ev_press->state |= Mod1Mask;
947 }
948
949 if (len == 1 && string[0] == CSI)
950 {
951 string[1] = KS_EXTRA;
952 string[2] = (int)KE_CSI;
953 len = -3;
954 }
955
956 /* Check for special keys. Also do this when len == 1 (key has an ASCII
957 * value) to detect backspace, delete and keypad keys. */
958 if (len == 0 || len == 1)
959 {
960 for (i = 0; special_keys[i].key_sym != (KeySym)0; i++)
961 {
962 if (special_keys[i].key_sym == key_sym)
963 {
964 string[0] = CSI;
965 string[1] = special_keys[i].vim_code0;
966 string[2] = special_keys[i].vim_code1;
967 len = -3;
968 break;
969 }
970 }
971 }
972
973 /* Unrecognised key is ignored. */
974 if (len == 0)
975 goto theend;
976
977 /* Special keys (and a few others) may have modifiers. Also when using a
978 * double-byte encoding (can't set the 8th bit). */
979 if (len == -3 || key_sym == XK_space || key_sym == XK_Tab
980 || key_sym == XK_Return || key_sym == XK_Linefeed
981 || key_sym == XK_Escape
982#ifdef FEAT_MBYTE
983 || (enc_dbcs && len == 1 && (ev_press->state & Mod1Mask))
984#endif
985 )
986 {
987 modifiers = 0;
988 if (ev_press->state & ShiftMask)
989 modifiers |= MOD_MASK_SHIFT;
990 if (ev_press->state & ControlMask)
991 modifiers |= MOD_MASK_CTRL;
992 if (ev_press->state & Mod1Mask)
993 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000994 if (ev_press->state & Mod4Mask)
995 modifiers |= MOD_MASK_META;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996
997 /*
998 * For some keys a shift modifier is translated into another key
999 * code.
1000 */
1001 if (len == -3)
1002 key = TO_SPECIAL(string[1], string[2]);
1003 else
1004 key = string[0];
1005 key = simplify_key(key, &modifiers);
1006 if (key == CSI)
1007 key = K_CSI;
1008 if (IS_SPECIAL(key))
1009 {
1010 string[0] = CSI;
1011 string[1] = K_SECOND(key);
1012 string[2] = K_THIRD(key);
1013 len = 3;
1014 }
1015 else
1016 {
1017 string[0] = key;
1018 len = 1;
1019 }
1020
1021 if (modifiers != 0)
1022 {
1023 string2[0] = CSI;
1024 string2[1] = KS_MODIFIER;
1025 string2[2] = modifiers;
1026 add_to_input_buf(string2, 3);
1027 }
1028 }
1029
1030 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1031#ifdef UNIX
1032 || (intr_char != 0 && string[0] == intr_char)
1033#endif
1034 ))
1035 {
1036 trash_input_buf();
1037 got_int = TRUE;
1038 }
1039
1040 add_to_input_buf(string, len);
1041
1042 /*
1043 * blank out the pointer if necessary
1044 */
1045 if (p_mh)
1046 gui_mch_mousehide(TRUE);
1047
1048#if defined(FEAT_BEVAL_TIP)
1049 {
1050 BalloonEval *be;
1051
1052 if ((be = gui_mch_currently_showing_beval()) != NULL)
1053 gui_mch_unpost_balloon(be);
1054 }
1055#endif
1056theend:
1057 {} /* some compilers need a statement here */
1058#ifdef FEAT_XIM
1059 if (string_alloced)
1060 XtFree((char *)string);
1061#endif
1062}
1063
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001065gui_x11_mouse_cb(
1066 Widget w UNUSED,
1067 XtPointer dud UNUSED,
1068 XEvent *event,
1069 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070{
1071 static XtIntervalId timer = (XtIntervalId)0;
1072 static int timed_out = TRUE;
1073
1074 int button;
1075 int repeated_click = FALSE;
1076 int x, y;
1077 int_u x_modifiers;
1078 int_u vim_modifiers;
1079
1080 if (event->type == MotionNotify)
1081 {
1082 /* Get the latest position, avoids lagging behind on a drag. */
1083 x = event->xmotion.x;
1084 y = event->xmotion.y;
1085 x_modifiers = event->xmotion.state;
1086 button = (x_modifiers & (Button1Mask | Button2Mask | Button3Mask))
1087 ? MOUSE_DRAG : ' ';
1088
1089 /*
1090 * if our pointer is currently hidden, then we should show it.
1091 */
1092 gui_mch_mousehide(FALSE);
1093
1094 if (button != MOUSE_DRAG) /* just moving the rodent */
1095 {
1096#ifdef FEAT_MENU
1097 if (dud) /* moved in vimForm */
1098 y -= gui.menu_height;
1099#endif
1100 gui_mouse_moved(x, y);
1101 return;
1102 }
1103 }
1104 else
1105 {
1106 x = event->xbutton.x;
1107 y = event->xbutton.y;
1108 if (event->type == ButtonPress)
1109 {
1110 /* Handle multiple clicks */
1111 if (!timed_out)
1112 {
1113 XtRemoveTimeOut(timer);
1114 repeated_click = TRUE;
1115 }
1116 timed_out = FALSE;
1117 timer = XtAppAddTimeOut(app_context, (long_u)p_mouset,
1118 gui_x11_timer_cb, &timed_out);
1119 switch (event->xbutton.button)
1120 {
Bram Moolenaar88e484b2015-11-24 15:38:44 +01001121 /* keep in sync with gui_gtk_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 case Button1: button = MOUSE_LEFT; break;
1123 case Button2: button = MOUSE_MIDDLE; break;
1124 case Button3: button = MOUSE_RIGHT; break;
1125 case Button4: button = MOUSE_4; break;
1126 case Button5: button = MOUSE_5; break;
Bram Moolenaar88e484b2015-11-24 15:38:44 +01001127 case 6: button = MOUSE_7; break;
1128 case 7: button = MOUSE_6; break;
1129 case 8: button = MOUSE_X1; break;
1130 case 9: button = MOUSE_X2; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 default:
1132 return; /* Unknown button */
1133 }
1134 }
1135 else if (event->type == ButtonRelease)
1136 button = MOUSE_RELEASE;
1137 else
1138 return; /* Unknown mouse event type */
1139
1140 x_modifiers = event->xbutton.state;
1141#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1142 last_mouse_event = event->xbutton;
1143#endif
1144 }
1145
1146 vim_modifiers = 0x0;
1147 if (x_modifiers & ShiftMask)
1148 vim_modifiers |= MOUSE_SHIFT;
1149 if (x_modifiers & ControlMask)
1150 vim_modifiers |= MOUSE_CTRL;
1151 if (x_modifiers & Mod1Mask) /* Alt or Meta key */
1152 vim_modifiers |= MOUSE_ALT;
1153
1154 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
1155}
1156
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157/*
1158 * End of call-back routines
1159 */
1160
1161/*
1162 * Parse the GUI related command-line arguments. Any arguments used are
1163 * deleted from argv, and *argc is decremented accordingly. This is called
1164 * when vim is started, whether or not the GUI has been started.
1165 */
1166 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001167gui_mch_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168{
1169 int arg;
1170 int i;
1171
1172 /*
1173 * Move all the entries in argv which are relevant to X into gui_argv.
1174 */
1175 gui_argc = 0;
1176 gui_argv = (char **)lalloc((long_u)(*argc * sizeof(char *)), FALSE);
1177 if (gui_argv == NULL)
1178 return;
1179 gui_argv[gui_argc++] = argv[0];
1180 arg = 1;
1181 while (arg < *argc)
1182 {
1183 /* Look for argv[arg] in cmdline_options[] table */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001184 for (i = 0; i < (int)XtNumber(cmdline_options); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 if (strcmp(argv[arg], cmdline_options[i].option) == 0)
1186 break;
1187
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001188 if (i < (int)XtNumber(cmdline_options))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 {
1190 /* Remember finding "-rv" or "-reverse" */
1191 if (strcmp("-rv", argv[arg]) == 0
1192 || strcmp("-reverse", argv[arg]) == 0)
1193 found_reverse_arg = TRUE;
1194 else if ((strcmp("-fn", argv[arg]) == 0
1195 || strcmp("-font", argv[arg]) == 0)
1196 && arg + 1 < *argc)
1197 font_argument = argv[arg + 1];
1198
1199 /* Found match in table, so move it into gui_argv */
1200 gui_argv[gui_argc++] = argv[arg];
1201 if (--*argc > arg)
1202 {
1203 mch_memmove(&argv[arg], &argv[arg + 1], (*argc - arg)
1204 * sizeof(char *));
1205 if (cmdline_options[i].argKind != XrmoptionNoArg)
1206 {
1207 /* Move the options argument as well */
1208 gui_argv[gui_argc++] = argv[arg];
1209 if (--*argc > arg)
1210 mch_memmove(&argv[arg], &argv[arg + 1], (*argc - arg)
1211 * sizeof(char *));
1212 }
1213 }
1214 argv[*argc] = NULL;
1215 }
1216 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217#ifdef FEAT_NETBEANS_INTG
1218 if (strncmp("-nb", argv[arg], 3) == 0)
1219 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 gui.dofork = FALSE; /* don't fork() when starting GUI */
1221 netbeansArg = argv[arg];
1222 mch_memmove(&argv[arg], &argv[arg + 1],
1223 (--*argc - arg) * sizeof(char *));
1224 argv[*argc] = NULL;
1225 }
1226 else
1227#endif
1228 arg++;
1229 }
1230}
1231
1232#ifndef XtSpecificationRelease
1233# define CARDINAL (Cardinal *)
1234#else
1235# if XtSpecificationRelease == 4
1236# define CARDINAL (Cardinal *)
1237# else
1238# define CARDINAL (int *)
1239# endif
1240#endif
1241
1242/*
1243 * Check if the GUI can be started. Called before gvimrc is sourced.
1244 * Return OK or FAIL.
1245 */
1246 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001247gui_mch_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248{
1249#ifdef FEAT_XIM
1250 XtSetLanguageProc(NULL, NULL, NULL);
1251#endif
1252 open_app_context();
1253 if (app_context != NULL)
1254 gui.dpy = XtOpenDisplay(app_context, 0, VIM_NAME, VIM_CLASS,
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00001255 cmdline_options, XtNumber(cmdline_options),
1256 CARDINAL &gui_argc, gui_argv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257
Bram Moolenaar889fe2c2018-05-13 16:23:40 +02001258# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1259 {
1260 /* The call to XtOpenDisplay() may have set the locale from the
1261 * environment. Set LC_NUMERIC to "C" to make sure that strtod() uses a
1262 * decimal point, not a comma. */
1263 char *p = setlocale(LC_NUMERIC, NULL);
1264
1265 if (p == NULL || strcmp(p, "C") != 0)
1266 setlocale(LC_NUMERIC, "C");
1267 }
1268# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 if (app_context == NULL || gui.dpy == NULL)
1270 {
1271 gui.dying = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001272 emsg(_(e_opendisp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 return FAIL;
1274 }
1275 return OK;
1276}
1277
1278
1279#ifdef USE_XSMP
1280/*
1281 * Handle XSMP processing, de-registering the attachment upon error
1282 */
1283static XtInputId _xsmp_xtinputid;
1284
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001286local_xsmp_handle_requests(
1287 XtPointer c UNUSED,
1288 int *s UNUSED,
1289 XtInputId *i UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290{
1291 if (xsmp_handle_requests() == FAIL)
1292 XtRemoveInput(_xsmp_xtinputid);
1293}
1294#endif
1295
1296
1297/*
1298 * Initialise the X GUI. Create all the windows, set up all the call-backs etc.
1299 * Returns OK for success, FAIL when the GUI can't be started.
1300 */
1301 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001302gui_mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303{
1304 XtGCMask gc_mask;
1305 XGCValues gc_vals;
1306 int x, y, mask;
1307 unsigned w, h;
1308
1309#if 0
1310 /* Uncomment this to enable synchronous mode for debugging */
1311 XSynchronize(gui.dpy, True);
1312#endif
1313
1314 vimShell = XtVaAppCreateShell(VIM_NAME, VIM_CLASS,
1315 applicationShellWidgetClass, gui.dpy, NULL);
1316
1317 /*
1318 * Get the application resources
1319 */
1320 XtVaGetApplicationResources(vimShell, (XtPointer)&gui,
1321 vim_resources, XtNumber(vim_resources), NULL);
1322
1323 gui.scrollbar_height = gui.scrollbar_width;
1324
1325 /*
1326 * Get the colors ourselves. Using the automatic conversion doesn't
1327 * handle looking for approximate colors.
1328 */
1329 /* NOTE: These next few lines are an exact duplicate of gui_athena.c's
1330 * gui_mch_def_colors(). Why?
1331 */
1332 gui.menu_fg_pixel = gui_get_color((char_u *)gui.rsrc_menu_fg_name);
1333 gui.menu_bg_pixel = gui_get_color((char_u *)gui.rsrc_menu_bg_name);
1334 gui.scroll_fg_pixel = gui_get_color((char_u *)gui.rsrc_scroll_fg_name);
1335 gui.scroll_bg_pixel = gui_get_color((char_u *)gui.rsrc_scroll_bg_name);
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001336#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 gui.tooltip_fg_pixel = gui_get_color((char_u *)gui.rsrc_tooltip_fg_name);
1338 gui.tooltip_bg_pixel = gui_get_color((char_u *)gui.rsrc_tooltip_bg_name);
1339#endif
1340
1341#if defined(FEAT_MENU) && defined(FEAT_GUI_ATHENA)
1342 /* If the menu height was set, don't change it at runtime */
1343 if (gui.menu_height != MENU_DEFAULT_HEIGHT)
1344 gui.menu_height_fixed = TRUE;
1345#endif
1346
1347 /* Set default foreground and background colours */
1348 gui.norm_pixel = gui.def_norm_pixel;
1349 gui.back_pixel = gui.def_back_pixel;
1350
1351 /* Check if reverse video needs to be applied (on Sun it's done by X) */
1352 if (gui.rsrc_rev_video && gui_get_lightness(gui.back_pixel)
1353 > gui_get_lightness(gui.norm_pixel))
1354 {
1355 gui.norm_pixel = gui.def_back_pixel;
1356 gui.back_pixel = gui.def_norm_pixel;
1357 gui.def_norm_pixel = gui.norm_pixel;
1358 gui.def_back_pixel = gui.back_pixel;
1359 }
1360
1361 /* Get the colors from the "Normal", "Tooltip", "Scrollbar" and "Menu"
1362 * group (set in syntax.c or in a vimrc file) */
1363 set_normal_colors();
1364
1365 /*
1366 * Check that none of the colors are the same as the background color
1367 */
1368 gui_check_colors();
1369
1370 /*
1371 * Set up the GCs. The font attributes will be set in gui_init_font().
1372 */
1373 gc_mask = GCForeground | GCBackground;
1374 gc_vals.foreground = gui.norm_pixel;
1375 gc_vals.background = gui.back_pixel;
1376 gui.text_gc = XtGetGC(vimShell, gc_mask, &gc_vals);
1377
1378 gc_vals.foreground = gui.back_pixel;
1379 gc_vals.background = gui.norm_pixel;
1380 gui.back_gc = XtGetGC(vimShell, gc_mask, &gc_vals);
1381
1382 gc_mask |= GCFunction;
1383 gc_vals.foreground = gui.norm_pixel ^ gui.back_pixel;
1384 gc_vals.background = gui.norm_pixel ^ gui.back_pixel;
1385 gc_vals.function = GXxor;
1386 gui.invert_gc = XtGetGC(vimShell, gc_mask, &gc_vals);
1387
1388 gui.visibility = VisibilityUnobscured;
1389 x11_setup_atoms(gui.dpy);
1390
1391 if (gui_win_x != -1 && gui_win_y != -1)
1392 gui_mch_set_winpos(gui_win_x, gui_win_y);
1393
1394 /* Now adapt the supplied(?) geometry-settings */
1395 /* Added by Kjetil Jacobsen <kjetilja@stud.cs.uit.no> */
1396 if (gui.geom != NULL && *gui.geom != NUL)
1397 {
1398 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
1399 if (mask & WidthValue)
1400 Columns = w;
1401 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00001402 {
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001403 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00001404 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00001406 }
Bram Moolenaarc33916a2013-07-01 21:43:08 +02001407 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 /*
1409 * Set the (x,y) position of the main window only if specified in the
1410 * users geometry, so we get good defaults when they don't. This needs
1411 * to be done before the shell is popped up.
1412 */
1413 if (mask & (XValue|YValue))
1414 XtVaSetValues(vimShell, XtNgeometry, gui.geom, NULL);
1415 }
1416
1417 gui_x11_create_widgets();
1418
1419 /*
1420 * Add an icon to Vim (Marcel Douben: 11 May 1998).
1421 */
1422 if (vim_strchr(p_go, GO_ICON) != NULL)
1423 {
1424#ifndef HAVE_XPM
1425# include "vim_icon.xbm"
1426# include "vim_mask.xbm"
1427
1428 Arg arg[2];
1429
1430 XtSetArg(arg[0], XtNiconPixmap,
1431 XCreateBitmapFromData(gui.dpy,
1432 DefaultRootWindow(gui.dpy),
1433 (char *)vim_icon_bits,
1434 vim_icon_width,
1435 vim_icon_height));
1436 XtSetArg(arg[1], XtNiconMask,
1437 XCreateBitmapFromData(gui.dpy,
1438 DefaultRootWindow(gui.dpy),
1439 (char *)vim_mask_icon_bits,
1440 vim_mask_icon_width,
1441 vim_mask_icon_height));
1442 XtSetValues(vimShell, arg, (Cardinal)2);
1443#else
1444/* Use Pixmaps, looking much nicer. */
1445
1446/* If you get an error message here, you still need to unpack the runtime
1447 * archive! */
1448# ifdef magick
1449# undef magick
1450# endif
1451# define magick vim32x32
1452# include "../runtime/vim32x32.xpm"
1453# undef magick
1454# define magick vim16x16
1455# include "../runtime/vim16x16.xpm"
1456# undef magick
1457# define magick vim48x48
1458# include "../runtime/vim48x48.xpm"
1459# undef magick
1460
1461 static Pixmap icon = 0;
1462 static Pixmap icon_mask = 0;
1463 static char **magick = vim32x32;
1464 Window root_window;
1465 XIconSize *size;
1466 int number_sizes;
1467 Display *dsp;
1468 Screen *scr;
1469 XpmAttributes attr;
1470 Colormap cmap;
1471
1472 /*
1473 * Adjust the icon to the preferences of the actual window manager.
1474 */
1475 root_window = XRootWindowOfScreen(XtScreen(vimShell));
1476 if (XGetIconSizes(XtDisplay(vimShell), root_window,
1477 &size, &number_sizes) != 0)
1478 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 if (number_sizes > 0)
1480 {
Bram Moolenaar6f292662013-07-14 15:06:50 +02001481 if (size->max_height >= 48 && size->max_width >= 48)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 magick = vim48x48;
Bram Moolenaar6f292662013-07-14 15:06:50 +02001483 else if (size->max_height >= 32 && size->max_width >= 32)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 magick = vim32x32;
Bram Moolenaar6f292662013-07-14 15:06:50 +02001485 else if (size->max_height >= 16 && size->max_width >= 16)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 magick = vim16x16;
1487 }
1488 }
1489
1490 dsp = XtDisplay(vimShell);
1491 scr = XtScreen(vimShell);
1492
1493 cmap = DefaultColormap(dsp, DefaultScreen(dsp));
1494 XtVaSetValues(vimShell, XtNcolormap, cmap, NULL);
1495
1496 attr.valuemask = 0L;
1497 attr.valuemask = XpmCloseness | XpmReturnPixels | XpmColormap | XpmDepth;
1498 attr.closeness = 65535; /* accuracy isn't crucial */
1499 attr.colormap = cmap;
1500 attr.depth = DefaultDepthOfScreen(scr);
1501
1502 if (!icon)
Bram Moolenaare8208012008-06-20 09:59:25 +00001503 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 XpmCreatePixmapFromData(dsp, root_window, magick, &icon,
1505 &icon_mask, &attr);
Bram Moolenaare8208012008-06-20 09:59:25 +00001506 XpmFreeAttributes(&attr);
1507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508
1509# ifdef FEAT_GUI_ATHENA
1510 XtVaSetValues(vimShell, XtNiconPixmap, icon, XtNiconMask, icon_mask, NULL);
1511# else
1512 XtVaSetValues(vimShell, XmNiconPixmap, icon, XmNiconMask, icon_mask, NULL);
1513# endif
1514#endif
1515 }
1516
1517 if (gui.color_approx)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001518 emsg(_("Vim E458: Cannot allocate colormap entry, some colors may be incorrect"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001520#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 gui_init_tooltip_font();
1522#endif
1523#ifdef FEAT_MENU
1524 gui_init_menu_font();
1525#endif
1526
1527#ifdef USE_XSMP
1528 /* Attach listener on ICE connection */
1529 if (-1 != xsmp_icefd)
1530 _xsmp_xtinputid = XtAppAddInput(app_context, xsmp_icefd,
1531 (XtPointer)XtInputReadMask, local_xsmp_handle_requests, NULL);
1532#endif
1533
1534 return OK;
1535}
1536
1537/*
1538 * Called when starting the GUI fails after calling gui_mch_init().
1539 */
1540 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001541gui_mch_uninit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542{
1543 gui_x11_destroy_widgets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 XtCloseDisplay(gui.dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 gui.dpy = NULL;
1546 vimShell = (Widget)0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001547 VIM_CLEAR(gui_argv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548}
1549
1550/*
1551 * Called when the foreground or background color has been changed.
1552 */
1553 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001554gui_mch_new_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555{
1556 long_u gc_mask;
1557 XGCValues gc_vals;
1558
1559 gc_mask = GCForeground | GCBackground;
1560 gc_vals.foreground = gui.norm_pixel;
1561 gc_vals.background = gui.back_pixel;
1562 if (gui.text_gc != NULL)
1563 XChangeGC(gui.dpy, gui.text_gc, gc_mask, &gc_vals);
1564
1565 gc_vals.foreground = gui.back_pixel;
1566 gc_vals.background = gui.norm_pixel;
1567 if (gui.back_gc != NULL)
1568 XChangeGC(gui.dpy, gui.back_gc, gc_mask, &gc_vals);
1569
1570 gc_mask |= GCFunction;
1571 gc_vals.foreground = gui.norm_pixel ^ gui.back_pixel;
1572 gc_vals.background = gui.norm_pixel ^ gui.back_pixel;
1573 gc_vals.function = GXxor;
1574 if (gui.invert_gc != NULL)
1575 XChangeGC(gui.dpy, gui.invert_gc, gc_mask, &gc_vals);
1576
1577 gui_x11_set_back_color();
1578}
1579
1580/*
1581 * Open the GUI window which was created by a call to gui_mch_init().
1582 */
1583 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001584gui_mch_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
1586 /* Actually open the window */
Bram Moolenaara5792f52005-11-23 21:25:05 +00001587 XtRealizeWidget(vimShell);
1588 XtManageChild(XtNameToWidget(vimShell, "*vimForm"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
1590 gui.wid = gui_x11_get_wid();
1591 gui.blank_pointer = gui_x11_create_blank_mouse();
1592
1593 /*
1594 * Add a callback for the Close item on the window managers menu, and the
1595 * save-yourself event.
1596 */
1597 wm_atoms[SAVE_YOURSELF_IDX] =
1598 XInternAtom(gui.dpy, "WM_SAVE_YOURSELF", False);
1599 wm_atoms[DELETE_WINDOW_IDX] =
1600 XInternAtom(gui.dpy, "WM_DELETE_WINDOW", False);
1601 XSetWMProtocols(gui.dpy, XtWindow(vimShell), wm_atoms, 2);
1602 XtAddEventHandler(vimShell, NoEventMask, True, gui_x11_wm_protocol_handler,
1603 NULL);
1604#ifdef HAVE_X11_XMU_EDITRES_H
1605 /*
1606 * Enable editres protocol (see "man editres").
1607 * Usually will need to add -lXmu to the linker line as well.
1608 */
1609 XtAddEventHandler(vimShell, (EventMask)0, True, _XEditResCheckMessages,
1610 (XtPointer)NULL);
1611#endif
1612
1613#ifdef FEAT_CLIENTSERVER
1614 if (serverName == NULL && serverDelayedStartName != NULL)
1615 {
1616 /* This is a :gui command in a plain vim with no previous server */
1617 commWindow = XtWindow(vimShell);
1618 (void)serverRegisterName(gui.dpy, serverDelayedStartName);
1619 }
1620 else
1621 {
1622 /*
1623 * Cannot handle "widget-less" windows with XtProcessEvent() we'll
1624 * have to change the "server" registration to that of the main window
1625 * If we have not registered a name yet, remember the window
1626 */
1627 serverChangeRegisteredWindow(gui.dpy, XtWindow(vimShell));
1628 }
1629 XtAddEventHandler(vimShell, PropertyChangeMask, False,
1630 gui_x11_send_event_handler, NULL);
1631#endif
1632
1633
1634#if defined(FEAT_MENU) && defined(FEAT_GUI_ATHENA)
1635 /* The Athena GUI needs this again after opening the window */
1636 gui_position_menu();
1637# ifdef FEAT_TOOLBAR
1638 gui_mch_set_toolbar_pos(0, gui.menu_height, gui.menu_width,
1639 gui.toolbar_height);
1640# endif
1641#endif
1642
1643 /* Get the colors for the highlight groups (gui_check_colors() might have
1644 * changed them) */
1645 highlight_gui_started(); /* re-init colors and fonts */
1646
1647#ifdef FEAT_HANGULIN
1648 hangul_keyboard_set();
1649#endif
1650#ifdef FEAT_XIM
1651 xim_init();
1652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653
1654 return OK;
1655}
1656
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001657#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658/*
1659 * Convert the tooltip fontset name to an XFontSet.
1660 */
1661 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001662gui_init_tooltip_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663{
1664 XrmValue from, to;
1665
1666 from.addr = (char *)gui.rsrc_tooltip_font_name;
1667 from.size = strlen(from.addr);
1668 to.addr = (XtPointer)&gui.tooltip_fontset;
1669 to.size = sizeof(XFontSet);
1670
1671 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontSet, &to) == False)
1672 {
1673 /* Failed. What to do? */
1674 }
1675}
1676#endif
1677
1678#if defined(FEAT_MENU) || defined(PROTO)
1679/* Convert the menu font/fontset name to an XFontStruct/XFontset */
1680 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001681gui_init_menu_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682{
1683 XrmValue from, to;
1684
1685#ifdef FONTSET_ALWAYS
1686 from.addr = (char *)gui.rsrc_menu_font_name;
1687 from.size = strlen(from.addr);
1688 to.addr = (XtPointer)&gui.menu_fontset;
1689 to.size = sizeof(GuiFontset);
1690
1691 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontSet, &to) == False)
1692 {
1693 /* Failed. What to do? */
1694 }
1695#else
1696 from.addr = (char *)gui.rsrc_menu_font_name;
1697 from.size = strlen(from.addr);
1698 to.addr = (XtPointer)&gui.menu_font;
1699 to.size = sizeof(GuiFont);
1700
1701 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontStruct, &to) == False)
1702 {
1703 /* Failed. What to do? */
1704 }
1705#endif
1706}
1707#endif
1708
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001710gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711{
1712#if 0
1713 /* Lesstif gives an error message here, and so does Solaris. The man page
1714 * says that this isn't needed when exiting, so just skip it. */
1715 XtCloseDisplay(gui.dpy);
1716#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01001717 VIM_CLEAR(gui_argv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718}
1719
1720/*
1721 * Get the position of the top left corner of the window.
1722 */
1723 int
Bram Moolenaar02fdaea2016-01-30 18:13:55 +01001724gui_mch_get_winpos(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725{
1726 Dimension xpos, ypos;
1727
1728 XtVaGetValues(vimShell,
1729 XtNx, &xpos,
1730 XtNy, &ypos,
1731 NULL);
1732 *x = xpos;
1733 *y = ypos;
1734 return OK;
1735}
1736
1737/*
1738 * Set the position of the top left corner of the window to the given
1739 * coordinates.
1740 */
1741 void
Bram Moolenaar02fdaea2016-01-30 18:13:55 +01001742gui_mch_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743{
1744 XtVaSetValues(vimShell,
1745 XtNx, x,
1746 XtNy, y,
1747 NULL);
1748}
1749
1750 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001751gui_mch_set_shellsize(
1752 int width,
1753 int height,
1754 int min_width,
1755 int min_height,
1756 int base_width,
1757 int base_height,
1758 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759{
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00001760#ifdef FEAT_XIM
1761 height += xim_get_status_area_height(),
1762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 XtVaSetValues(vimShell,
1764 XtNwidthInc, gui.char_width,
1765 XtNheightInc, gui.char_height,
1766#if defined(XtSpecificationRelease) && XtSpecificationRelease >= 4
1767 XtNbaseWidth, base_width,
1768 XtNbaseHeight, base_height,
1769#endif
1770 XtNminWidth, min_width,
1771 XtNminHeight, min_height,
1772 XtNwidth, width,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 XtNheight, height,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 NULL);
1775}
1776
1777/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00001778 * Allow 10 pixels for horizontal borders, 'guiheadroom' for vertical borders.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 * Is there no way in X to find out how wide the borders really are?
1780 */
1781 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001782gui_mch_get_screen_dimensions(
1783 int *screen_w,
1784 int *screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
1786 *screen_w = DisplayWidth(gui.dpy, DefaultScreen(gui.dpy)) - 10;
1787 *screen_h = DisplayHeight(gui.dpy, DefaultScreen(gui.dpy)) - p_ghr;
1788}
1789
1790/*
1791 * Initialise vim to use the font "font_name". If it's NULL, pick a default
1792 * font.
1793 * If "fontset" is TRUE, load the "font_name" as a fontset.
1794 * Return FAIL if the font could not be loaded, OK otherwise.
1795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001797gui_mch_init_font(
1798 char_u *font_name,
1799 int do_fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800{
1801 XFontStruct *font = NULL;
1802
1803#ifdef FEAT_XFONTSET
1804 XFontSet fontset = NULL;
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001807#ifdef FEAT_GUI_MOTIF
1808 /* A font name equal "*" is indicating, that we should activate the font
1809 * selection dialogue to get a new font name. So let us do it here. */
1810 if (font_name != NULL && STRCMP(font_name, "*") == 0)
1811 font_name = gui_xm_select_font(hl_get_font_name());
1812#endif
1813
1814#ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 if (do_fontset)
1816 {
1817 /* If 'guifontset' is set, VIM treats all font specifications as if
1818 * they were fontsets, and 'guifontset' becomes the default. */
1819 if (font_name != NULL)
1820 {
1821 fontset = (XFontSet)gui_mch_get_fontset(font_name, FALSE, TRUE);
1822 if (fontset == NULL)
1823 return FAIL;
1824 }
1825 }
1826 else
1827#endif
1828 {
1829 if (font_name == NULL)
1830 {
1831 /*
1832 * If none of the fonts in 'font' could be loaded, try the one set
1833 * in the X resource, and finally just try using DFLT_FONT, which
1834 * will hopefully always be there.
1835 */
1836 font_name = gui.rsrc_font_name;
1837 font = (XFontStruct *)gui_mch_get_font(font_name, FALSE);
1838 if (font == NULL)
1839 font_name = (char_u *)DFLT_FONT;
1840 }
1841 if (font == NULL)
1842 font = (XFontStruct *)gui_mch_get_font(font_name, FALSE);
1843 if (font == NULL)
1844 return FAIL;
1845 }
1846
1847 gui_mch_free_font(gui.norm_font);
1848#ifdef FEAT_XFONTSET
1849 gui_mch_free_fontset(gui.fontset);
1850
1851 if (fontset != NULL)
1852 {
1853 gui.norm_font = NOFONT;
1854 gui.fontset = (GuiFontset)fontset;
1855 gui.char_width = fontset_width(fontset);
1856 gui.char_height = fontset_height(fontset) + p_linespace;
1857 gui.char_ascent = fontset_ascent(fontset) + p_linespace / 2;
1858 }
1859 else
1860#endif
1861 {
1862 gui.norm_font = (GuiFont)font;
1863#ifdef FEAT_XFONTSET
1864 gui.fontset = NOFONTSET;
1865#endif
1866 gui.char_width = font->max_bounds.width;
1867 gui.char_height = font->ascent + font->descent + p_linespace;
1868 gui.char_ascent = font->ascent + p_linespace / 2;
1869 }
1870
1871 hl_set_font_name(font_name);
1872
1873 /*
1874 * Try to load other fonts for bold, italic, and bold-italic.
1875 * We should also try to work out what font to use for these when they are
1876 * not specified by X resources, but we don't yet.
1877 */
1878 if (font_name == gui.rsrc_font_name)
1879 {
1880 if (gui.bold_font == NOFONT
1881 && gui.rsrc_bold_font_name != NULL
1882 && *gui.rsrc_bold_font_name != NUL)
1883 gui.bold_font = gui_mch_get_font(gui.rsrc_bold_font_name, FALSE);
1884 if (gui.ital_font == NOFONT
1885 && gui.rsrc_ital_font_name != NULL
1886 && *gui.rsrc_ital_font_name != NUL)
1887 gui.ital_font = gui_mch_get_font(gui.rsrc_ital_font_name, FALSE);
1888 if (gui.boldital_font == NOFONT
1889 && gui.rsrc_boldital_font_name != NULL
1890 && *gui.rsrc_boldital_font_name != NUL)
1891 gui.boldital_font = gui_mch_get_font(gui.rsrc_boldital_font_name,
1892 FALSE);
1893 }
1894 else
1895 {
1896 /* When not using the font specified by the resources, also don't use
1897 * the bold/italic fonts, otherwise setting 'guifont' will look very
1898 * strange. */
1899 if (gui.bold_font != NOFONT)
1900 {
1901 XFreeFont(gui.dpy, (XFontStruct *)gui.bold_font);
1902 gui.bold_font = NOFONT;
1903 }
1904 if (gui.ital_font != NOFONT)
1905 {
1906 XFreeFont(gui.dpy, (XFontStruct *)gui.ital_font);
1907 gui.ital_font = NOFONT;
1908 }
1909 if (gui.boldital_font != NOFONT)
1910 {
1911 XFreeFont(gui.dpy, (XFontStruct *)gui.boldital_font);
1912 gui.boldital_font = NOFONT;
1913 }
1914 }
1915
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001916#ifdef FEAT_GUI_MOTIF
1917 gui_motif_synch_fonts();
1918#endif
1919
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 return OK;
1921}
1922
1923/*
1924 * Get a font structure for highlighting.
1925 */
1926 GuiFont
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001927gui_mch_get_font(char_u *name, int giveErrorIfMissing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928{
1929 XFontStruct *font;
1930
Bram Moolenaard23a8232018-02-10 18:45:26 +01001931 if (!gui.in_use || name == NULL) /* can't do this when GUI not running */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 return NOFONT;
1933
1934 font = XLoadQueryFont(gui.dpy, (char *)name);
1935
1936 if (font == NULL)
1937 {
1938 if (giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001939 semsg(_(e_font), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 return NOFONT;
1941 }
1942
1943#ifdef DEBUG
1944 printf("Font Information for '%s':\n", name);
1945 printf(" w = %d, h = %d, ascent = %d, descent = %d\n",
1946 font->max_bounds.width, font->ascent + font->descent,
1947 font->ascent, font->descent);
1948 printf(" max ascent = %d, max descent = %d, max h = %d\n",
1949 font->max_bounds.ascent, font->max_bounds.descent,
1950 font->max_bounds.ascent + font->max_bounds.descent);
1951 printf(" min lbearing = %d, min rbearing = %d\n",
1952 font->min_bounds.lbearing, font->min_bounds.rbearing);
1953 printf(" max lbearing = %d, max rbearing = %d\n",
1954 font->max_bounds.lbearing, font->max_bounds.rbearing);
1955 printf(" leftink = %d, rightink = %d\n",
1956 (font->min_bounds.lbearing < 0),
1957 (font->max_bounds.rbearing > font->max_bounds.width));
1958 printf("\n");
1959#endif
1960
1961 if (font->max_bounds.width != font->min_bounds.width)
1962 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001963 semsg(_(e_fontwidth), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 XFreeFont(gui.dpy, font);
1965 return NOFONT;
1966 }
1967 return (GuiFont)font;
1968}
1969
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001970#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00001971/*
1972 * Return the name of font "font" in allocated memory.
Bram Moolenaar46c9c732004-12-12 11:37:09 +00001973 */
1974 char_u *
Bram Moolenaar87748452017-03-12 17:10:33 +01001975gui_mch_get_fontname(GuiFont font, char_u *name)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00001976{
Bram Moolenaar87748452017-03-12 17:10:33 +01001977 char_u *ret = NULL;
1978
1979 if (name != NULL && font == NULL)
1980 {
1981 /* In this case, there's no way other than doing this. */
1982 ret = vim_strsave(name);
1983 }
1984 else if (font != NULL)
1985 {
1986 /* In this case, try to retrieve the XLFD corresponding to 'font'->fid;
1987 * if failed, use 'name' unless it's NULL. */
1988 unsigned long value = 0L;
1989
1990 if (XGetFontProperty(font, XA_FONT, &value))
1991 {
1992 char *xa_font_name = NULL;
1993
1994 xa_font_name = XGetAtomName(gui.dpy, value);
1995 if (xa_font_name != NULL)
1996 {
1997 ret = vim_strsave((char_u *)xa_font_name);
1998 XFree(xa_font_name);
1999 }
2000 else if (name != NULL)
2001 ret = vim_strsave(name);
2002 }
2003 else if (name != NULL)
2004 ret = vim_strsave(name);
2005 }
2006 return ret;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002007}
Bram Moolenaar567e4de2004-12-31 21:01:02 +00002008#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002009
Bram Moolenaar231334e2005-07-25 20:46:57 +00002010/*
2011 * Adjust gui.char_height (after 'linespace' was changed).
2012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002014gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015{
2016#ifdef FEAT_XFONTSET
2017 if (gui.fontset != NOFONTSET)
2018 {
2019 gui.char_height = fontset_height((XFontSet)gui.fontset) + p_linespace;
2020 gui.char_ascent = fontset_ascent((XFontSet)gui.fontset)
2021 + p_linespace / 2;
2022 }
2023 else
2024#endif
2025 {
2026 XFontStruct *font = (XFontStruct *)gui.norm_font;
2027
2028 gui.char_height = font->ascent + font->descent + p_linespace;
2029 gui.char_ascent = font->ascent + p_linespace / 2;
2030 }
2031 return OK;
2032}
2033
2034/*
2035 * Set the current text font.
2036 */
2037 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002038gui_mch_set_font(GuiFont font)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039{
2040 static Font prev_font = (Font)-1;
2041 Font fid = ((XFontStruct *)font)->fid;
2042
2043 if (fid != prev_font)
2044 {
2045 XSetFont(gui.dpy, gui.text_gc, fid);
2046 XSetFont(gui.dpy, gui.back_gc, fid);
2047 prev_font = fid;
2048 gui.char_ascent = ((XFontStruct *)font)->ascent + p_linespace / 2;
2049 }
2050#ifdef FEAT_XFONTSET
2051 current_fontset = (XFontSet)NULL;
2052#endif
2053}
2054
2055#if defined(FEAT_XFONTSET) || defined(PROTO)
2056/*
2057 * Set the current text fontset.
2058 * Adjust the ascent, in case it's different.
2059 */
2060 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002061gui_mch_set_fontset(GuiFontset fontset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062{
2063 current_fontset = (XFontSet)fontset;
2064 gui.char_ascent = fontset_ascent(current_fontset) + p_linespace / 2;
2065}
2066#endif
2067
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068/*
2069 * If a font is not going to be used, free its structure.
2070 */
2071 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002072gui_mch_free_font(GuiFont font)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073{
2074 if (font != NOFONT)
2075 XFreeFont(gui.dpy, (XFontStruct *)font);
2076}
2077
2078#if defined(FEAT_XFONTSET) || defined(PROTO)
2079/*
2080 * If a fontset is not going to be used, free its structure.
2081 */
2082 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002083gui_mch_free_fontset(GuiFontset fontset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084{
2085 if (fontset != NOFONTSET)
2086 XFreeFontSet(gui.dpy, (XFontSet)fontset);
2087}
2088
2089/*
2090 * Load the fontset "name".
2091 * Return a reference to the fontset, or NOFONTSET when failing.
2092 */
2093 GuiFontset
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002094gui_mch_get_fontset(
2095 char_u *name,
2096 int giveErrorIfMissing,
2097 int fixed_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098{
2099 XFontSet fontset;
2100 char **missing, *def_str;
2101 int num_missing;
2102
2103 if (!gui.in_use || name == NULL)
2104 return NOFONTSET;
2105
2106 fontset = XCreateFontSet(gui.dpy, (char *)name, &missing, &num_missing,
2107 &def_str);
2108 if (num_missing > 0)
2109 {
2110 int i;
2111
2112 if (giveErrorIfMissing)
2113 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002114 semsg(_("E250: Fonts for the following charsets are missing in fontset %s:"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 for (i = 0; i < num_missing; i++)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002116 semsg("%s", missing[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 }
2118 XFreeStringList(missing);
2119 }
2120
2121 if (fontset == NULL)
2122 {
2123 if (giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002124 semsg(_(e_fontset), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 return NOFONTSET;
2126 }
2127
2128 if (fixed_width && check_fontset_sanity(fontset) == FAIL)
2129 {
2130 XFreeFontSet(gui.dpy, fontset);
2131 return NOFONTSET;
2132 }
2133 return (GuiFontset)fontset;
2134}
2135
2136/*
2137 * Check if fontset "fs" is fixed width.
2138 */
2139 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002140check_fontset_sanity(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141{
2142 XFontStruct **xfs;
2143 char **font_name;
2144 int fn;
2145 char *base_name;
2146 int i;
2147 int min_width;
2148 int min_font_idx = 0;
2149
2150 base_name = XBaseFontNameListOfFontSet(fs);
2151 fn = XFontsOfFontSet(fs, &xfs, &font_name);
2152 for (i = 0; i < fn; i++)
2153 {
2154 if (xfs[i]->max_bounds.width != xfs[i]->min_bounds.width)
2155 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002156 semsg(_("E252: Fontset name: %s"), base_name);
2157 semsg(_("Font '%s' is not fixed-width"), font_name[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 return FAIL;
2159 }
2160 }
2161 /* scan base font width */
2162 min_width = 32767;
2163 for (i = 0; i < fn; i++)
2164 {
2165 if (xfs[i]->max_bounds.width<min_width)
2166 {
2167 min_width = xfs[i]->max_bounds.width;
2168 min_font_idx = i;
2169 }
2170 }
2171 for (i = 0; i < fn; i++)
2172 {
2173 if ( xfs[i]->max_bounds.width != 2 * min_width
2174 && xfs[i]->max_bounds.width != min_width)
2175 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002176 semsg(_("E253: Fontset name: %s"), base_name);
2177 semsg(_("Font0: %s"), font_name[min_font_idx]);
Bram Moolenaar500f3612019-01-16 22:15:11 +01002178 semsg(_("Font%d: %s"), i, font_name[i]);
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01002179 semsg(_("Font%d width is not twice that of font0"), i);
2180 semsg(_("Font0 width: %d"),
2181 (int)xfs[min_font_idx]->max_bounds.width);
2182 semsg(_("Font%d width: %d"), i, (int)xfs[i]->max_bounds.width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 return FAIL;
2184 }
2185 }
2186 /* it seems ok. Good Luck!! */
2187 return OK;
2188}
2189
2190 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002191fontset_width(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192{
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002193 return XmbTextEscapement(fs, "Vim", 3) / 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194}
2195
2196 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002197fontset_height(
2198 XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199{
2200 XFontSetExtents *extents;
2201
2202 extents = XExtentsOfFontSet(fs);
2203 return extents->max_logical_extent.height;
2204}
2205
2206#if (defined(FONTSET_ALWAYS) && defined(FEAT_GUI_ATHENA) \
2207 && defined(FEAT_MENU)) || defined(PROTO)
2208/*
2209 * Returns the bounding box height around the actual glyph image of all
2210 * characters in all fonts of the fontset.
2211 */
2212 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002213fontset_height2(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214{
2215 XFontSetExtents *extents;
2216
2217 extents = XExtentsOfFontSet(fs);
2218 return extents->max_ink_extent.height;
2219}
2220#endif
2221
2222/* NOT USED YET
2223 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002224fontset_descent(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225{
2226 XFontSetExtents *extents;
2227
2228 extents = XExtentsOfFontSet (fs);
2229 return extents->max_logical_extent.height + extents->max_logical_extent.y;
2230}
2231*/
2232
2233 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002234fontset_ascent(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235{
2236 XFontSetExtents *extents;
2237
2238 extents = XExtentsOfFontSet(fs);
2239 return -extents->max_logical_extent.y;
2240}
2241
2242#endif /* FEAT_XFONTSET */
2243
2244/*
2245 * Return the Pixel value (color) for the given color name.
2246 * Return INVALCOLOR for error.
2247 */
2248 guicolor_T
Bram Moolenaar46582282016-07-23 14:35:12 +02002249gui_mch_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250{
Bram Moolenaard23a8232018-02-10 18:45:26 +01002251 guicolor_T requested;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
2253 /* can't do this when GUI not running */
Bram Moolenaar46582282016-07-23 14:35:12 +02002254 if (!gui.in_use || name == NULL || *name == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 return INVALCOLOR;
2256
Bram Moolenaar46582282016-07-23 14:35:12 +02002257 requested = gui_get_color_cmn(name);
2258 if (requested == INVALCOLOR)
2259 return INVALCOLOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002261 return gui_mch_get_rgb_color(
Bram Moolenaar46582282016-07-23 14:35:12 +02002262 (requested & 0xff0000) >> 16,
2263 (requested & 0xff00) >> 8,
2264 requested & 0xff);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002265}
2266
2267/*
2268 * Return the Pixel value (color) for the given RGB values.
2269 * Return INVALCOLOR for error.
2270 */
2271 guicolor_T
2272gui_mch_get_rgb_color(int r, int g, int b)
2273{
Bram Moolenaard23a8232018-02-10 18:45:26 +01002274 XColor available;
Bram Moolenaard60547b2017-07-24 20:15:30 +02002275 Colormap colormap;
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002276
Bram Moolenaar778df2a2018-05-06 19:19:36 +02002277/* Using XParseColor() is very slow, put rgb in XColor directly.
2278
2279 char spec[8]; // space enough to hold "#RRGGBB"
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002280 vim_snprintf(spec, sizeof(spec), "#%.2x%.2x%.2x", r, g, b);
Bram Moolenaar46582282016-07-23 14:35:12 +02002281 if (XParseColor(gui.dpy, colormap, (char *)spec, &available) != 0
2282 && XAllocColor(gui.dpy, colormap, &available) != 0)
2283 return (guicolor_T)available.pixel;
Bram Moolenaar778df2a2018-05-06 19:19:36 +02002284*/
2285 colormap = DefaultColormap(gui.dpy, DefaultScreen(gui.dpy));
2286 vim_memset(&available, 0, sizeof(XColor));
2287 available.red = r << 8;
2288 available.green = g << 8;
2289 available.blue = b << 8;
2290 if (XAllocColor(gui.dpy, colormap, &available) != 0)
2291 return (guicolor_T)available.pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292
2293 return INVALCOLOR;
2294}
2295
2296/*
Bram Moolenaarfb269802005-03-15 22:46:30 +00002297 * Set the current text foreground color.
2298 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002300gui_mch_set_fg_color(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301{
2302 if (color != prev_fg_color)
2303 {
2304 XSetForeground(gui.dpy, gui.text_gc, (Pixel)color);
2305 prev_fg_color = color;
2306 }
2307}
2308
2309/*
2310 * Set the current text background color.
2311 */
2312 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002313gui_mch_set_bg_color(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314{
2315 if (color != prev_bg_color)
2316 {
2317 XSetBackground(gui.dpy, gui.text_gc, (Pixel)color);
2318 prev_bg_color = color;
2319 }
2320}
2321
2322/*
Bram Moolenaarfb269802005-03-15 22:46:30 +00002323 * Set the current text special color.
2324 */
2325 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002326gui_mch_set_sp_color(guicolor_T color)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002327{
2328 prev_sp_color = color;
2329}
2330
2331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 * create a mouse pointer that is blank
2333 */
2334 static Cursor
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002335gui_x11_create_blank_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336{
2337 Pixmap blank_pixmap = XCreatePixmap(gui.dpy, gui.wid, 1, 1, 1);
2338 GC gc = XCreateGC(gui.dpy, blank_pixmap, (unsigned long)0, (XGCValues*)0);
2339 XDrawPoint(gui.dpy, blank_pixmap, gc, 0, 0);
2340 XFreeGC(gui.dpy, gc);
2341 return XCreatePixmapCursor(gui.dpy, blank_pixmap, blank_pixmap,
2342 (XColor*)&gui.norm_pixel, (XColor*)&gui.norm_pixel, 0, 0);
2343}
2344
Bram Moolenaarfb269802005-03-15 22:46:30 +00002345/*
2346 * Draw a curled line at the bottom of the character cell.
2347 */
2348 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002349draw_curl(int row, int col, int cells)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002350{
2351 int i;
2352 int offset;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002353 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarfb269802005-03-15 22:46:30 +00002354
2355 XSetForeground(gui.dpy, gui.text_gc, prev_sp_color);
2356 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
2357 {
2358 offset = val[i % 8];
2359 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, i,
2360 FILL_Y(row + 1) - 1 - offset);
2361 }
2362 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
2363}
2364
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002366gui_mch_draw_string(
2367 int row,
2368 int col,
2369 char_u *s,
2370 int len,
2371 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372{
2373 int cells = len;
2374#ifdef FEAT_MBYTE
Bram Moolenaara3227e22006-03-08 21:32:40 +00002375 static void *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 static int buflen = 0;
2377 char_u *p;
2378 int wlen = 0;
2379 int c;
2380
2381 if (enc_utf8)
2382 {
2383 /* Convert UTF-8 byte sequence to 16 bit characters for the X
2384 * functions. Need a buffer for the 16 bit characters. Keep it
2385 * between calls, because allocating it each time is slow. */
2386 if (buflen < len)
2387 {
2388 XtFree((char *)buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002389 buf = (void *)XtMalloc(len * (sizeof(XChar2b) < sizeof(wchar_t)
2390 ? sizeof(wchar_t) : sizeof(XChar2b)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 buflen = len;
2392 }
2393 p = s;
2394 cells = 0;
2395 while (p < s + len)
2396 {
2397 c = utf_ptr2char(p);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002398# ifdef FEAT_XFONTSET
2399 if (current_fontset != NULL)
2400 {
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002401# ifdef SMALL_WCHAR_T
2402 if (c >= 0x10000)
Bram Moolenaara3227e22006-03-08 21:32:40 +00002403 c = 0xbf; /* show chars > 0xffff as ? */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002404# endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00002405 ((wchar_t *)buf)[wlen] = c;
2406 }
2407 else
2408# endif
2409 {
2410 if (c >= 0x10000)
2411 c = 0xbf; /* show chars > 0xffff as ? */
2412 ((XChar2b *)buf)[wlen].byte1 = (unsigned)c >> 8;
2413 ((XChar2b *)buf)[wlen].byte2 = c;
2414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 ++wlen;
2416 cells += utf_char2cells(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002417 p += utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 }
2419 }
2420 else if (has_mbyte)
2421 {
2422 cells = 0;
2423 for (p = s; p < s + len; )
2424 {
2425 cells += ptr2cells(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002426 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 }
2428 }
2429
2430#endif
2431
2432#ifdef FEAT_XFONTSET
2433 if (current_fontset != NULL)
2434 {
2435 /* Setup a clip rectangle to avoid spilling over in the next or
2436 * previous line. This is apparently needed for some fonts which are
2437 * used in a fontset. */
2438 XRectangle clip;
2439
2440 clip.x = 0;
2441 clip.y = 0;
2442 clip.height = gui.char_height;
2443 clip.width = gui.char_width * cells + 1;
2444 XSetClipRectangles(gui.dpy, gui.text_gc, FILL_X(col), FILL_Y(row),
2445 &clip, 1, Unsorted);
2446 }
2447#endif
2448
2449 if (flags & DRAW_TRANSP)
2450 {
2451#ifdef FEAT_MBYTE
2452 if (enc_utf8)
2453 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2454 TEXT_Y(row), buf, wlen);
2455 else
2456#endif
2457 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2458 TEXT_Y(row), (char *)s, len);
2459 }
2460 else if (p_linespace != 0
2461#ifdef FEAT_XFONTSET
2462 || current_fontset != NULL
2463#endif
2464 )
2465 {
2466 XSetForeground(gui.dpy, gui.text_gc, prev_bg_color);
2467 XFillRectangle(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
2468 FILL_Y(row), gui.char_width * cells, gui.char_height);
2469 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
Bram Moolenaarfb269802005-03-15 22:46:30 +00002470
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471#ifdef FEAT_MBYTE
2472 if (enc_utf8)
2473 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2474 TEXT_Y(row), buf, wlen);
2475 else
2476#endif
2477 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2478 TEXT_Y(row), (char *)s, len);
2479 }
2480 else
2481 {
2482 /* XmbDrawImageString has bug, don't use it for fontset. */
2483#ifdef FEAT_MBYTE
2484 if (enc_utf8)
2485 XDrawImageString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2486 TEXT_Y(row), buf, wlen);
2487 else
2488#endif
2489 XDrawImageString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2490 TEXT_Y(row), (char *)s, len);
2491 }
2492
2493 /* Bold trick: draw the text again with a one-pixel offset. */
2494 if (flags & DRAW_BOLD)
2495 {
2496#ifdef FEAT_MBYTE
2497 if (enc_utf8)
2498 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col) + 1,
2499 TEXT_Y(row), buf, wlen);
2500 else
2501#endif
2502 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col) + 1,
2503 TEXT_Y(row), (char *)s, len);
2504 }
2505
Bram Moolenaarfb269802005-03-15 22:46:30 +00002506 /* Undercurl: draw curl at the bottom of the character cell. */
2507 if (flags & DRAW_UNDERC)
2508 draw_curl(row, col, cells);
2509
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 /* Underline: draw a line at the bottom of the character cell. */
2511 if (flags & DRAW_UNDERL)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002512 {
2513 int y = FILL_Y(row + 1) - 1;
2514
2515 /* When p_linespace is 0, overwrite the bottom row of pixels.
2516 * Otherwise put the line just below the character. */
2517 if (p_linespace > 1)
2518 y -= p_linespace - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 XDrawLine(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
Bram Moolenaarfb269802005-03-15 22:46:30 +00002520 y, FILL_X(col + cells) - 1, y);
2521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002523 if (flags & DRAW_STRIKE)
2524 {
2525 int y = FILL_Y(row + 1) - gui.char_height/2;
2526
2527 XSetForeground(gui.dpy, gui.text_gc, prev_sp_color);
2528 XDrawLine(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
2529 y, FILL_X(col + cells) - 1, y);
2530 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
2531 }
2532
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533#ifdef FEAT_XFONTSET
2534 if (current_fontset != NULL)
2535 XSetClipMask(gui.dpy, gui.text_gc, None);
2536#endif
2537}
2538
2539/*
2540 * Return OK if the key with the termcap name "name" is supported.
2541 */
2542 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002543gui_mch_haskey(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544{
2545 int i;
2546
2547 for (i = 0; special_keys[i].key_sym != (KeySym)0; i++)
2548 if (name[0] == special_keys[i].vim_code0 &&
2549 name[1] == special_keys[i].vim_code1)
2550 return OK;
2551 return FAIL;
2552}
2553
2554/*
2555 * Return the text window-id and display. Only required for X-based GUI's
2556 */
2557 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002558gui_get_x11_windis(Window *win, Display **dis)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559{
2560 *win = XtWindow(vimShell);
2561 *dis = gui.dpy;
2562 return OK;
2563}
2564
2565 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002566gui_mch_beep(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567{
2568 XBell(gui.dpy, 0);
2569}
2570
2571 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002572gui_mch_flash(int msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573{
2574 /* Do a visual beep by reversing the foreground and background colors */
2575 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc, 0, 0,
2576 FILL_X((int)Columns) + gui.border_offset,
2577 FILL_Y((int)Rows) + gui.border_offset);
2578 XSync(gui.dpy, False);
2579 ui_delay((long)msec, TRUE); /* wait for a few msec */
2580 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc, 0, 0,
2581 FILL_X((int)Columns) + gui.border_offset,
2582 FILL_Y((int)Rows) + gui.border_offset);
2583}
2584
2585/*
2586 * Invert a rectangle from row r, column c, for nr rows and nc columns.
2587 */
2588 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002589gui_mch_invert_rectangle(
2590 int r,
2591 int c,
2592 int nr,
2593 int nc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594{
2595 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc,
2596 FILL_X(c), FILL_Y(r), (nc) * gui.char_width, (nr) * gui.char_height);
2597}
2598
2599/*
2600 * Iconify the GUI window.
2601 */
2602 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002603gui_mch_iconify(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604{
2605 XIconifyWindow(gui.dpy, XtWindow(vimShell), DefaultScreen(gui.dpy));
2606}
2607
2608#if defined(FEAT_EVAL) || defined(PROTO)
2609/*
2610 * Bring the Vim window to the foreground.
2611 */
2612 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002613gui_mch_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614{
2615 XMapRaised(gui.dpy, XtWindow(vimShell));
2616}
2617#endif
2618
2619/*
2620 * Draw a cursor without focus.
2621 */
2622 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002623gui_mch_draw_hollow_cursor(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624{
2625 int w = 1;
2626
2627#ifdef FEAT_MBYTE
2628 if (mb_lefthalve(gui.row, gui.col))
2629 w = 2;
2630#endif
2631 gui_mch_set_fg_color(color);
2632 XDrawRectangle(gui.dpy, gui.wid, gui.text_gc, FILL_X(gui.col),
2633 FILL_Y(gui.row), w * gui.char_width - 1, gui.char_height - 1);
2634}
2635
2636/*
2637 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
2638 * color "color".
2639 */
2640 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002641gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642{
2643 gui_mch_set_fg_color(color);
2644
2645 XFillRectangle(gui.dpy, gui.wid, gui.text_gc,
2646#ifdef FEAT_RIGHTLEFT
2647 /* vertical line should be on the right of current point */
2648 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
2649#endif
2650 FILL_X(gui.col),
2651 FILL_Y(gui.row) + gui.char_height - h,
2652 w, h);
2653}
2654
2655/*
2656 * Catch up with any queued X events. This may put keyboard input into the
2657 * input buffer, call resize call-backs, trigger timers etc. If there is
2658 * nothing in the X event queue (& no timers pending), then we return
2659 * immediately.
2660 */
2661 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002662gui_mch_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663{
2664 XtInputMask mask, desired;
2665
2666#ifdef ALT_X_INPUT
2667 if (suppress_alternate_input)
2668 desired = (XtIMXEvent | XtIMTimer);
2669 else
2670#endif
2671 desired = (XtIMAll);
2672 while ((mask = XtAppPending(app_context)) && (mask & desired)
2673 && !vim_is_input_buf_full())
2674 XtAppProcessEvent(app_context, desired);
2675}
2676
2677/*
2678 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2679 * from the keyboard.
2680 * wtime == -1 Wait forever.
2681 * wtime == 0 This should never happen.
2682 * wtime > 0 Wait wtime milliseconds for a character.
2683 * Returns OK if a character was found to be available within the given time,
2684 * or FAIL otherwise.
2685 */
2686 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002687gui_mch_wait_for_chars(long wtime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688{
Bram Moolenaar4231da42016-06-02 14:30:04 +02002689 int focus;
2690 int retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691
2692 /*
2693 * Make this static, in case gui_x11_timer_cb is called after leaving
2694 * this function (otherwise a random value on the stack may be changed).
2695 */
2696 static int timed_out;
2697 XtIntervalId timer = (XtIntervalId)0;
2698 XtInputMask desired;
Bram Moolenaar1dccf632017-08-27 17:38:27 +02002699#ifdef FEAT_JOB_CHANNEL
2700 XtIntervalId channel_timer = (XtIntervalId)0;
2701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
2703 timed_out = FALSE;
2704
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 if (wtime > 0)
2706 timer = XtAppAddTimeOut(app_context, (long_u)wtime, gui_x11_timer_cb,
2707 &timed_out);
Bram Moolenaar1dccf632017-08-27 17:38:27 +02002708#ifdef FEAT_JOB_CHANNEL
2709 /* If there is a channel with the keep_open flag we need to poll for input
2710 * on them. */
2711 if (channel_any_keep_open())
2712 channel_timer = XtAppAddTimeOut(app_context, (long_u)20,
2713 channel_poll_cb, (XtPointer)&channel_timer);
2714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715
2716 focus = gui.in_focus;
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01002717 desired = (XtIMAll);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 while (!timed_out)
2719 {
2720 /* Stop or start blinking when focus changes */
2721 if (gui.in_focus != focus)
2722 {
2723 if (gui.in_focus)
2724 gui_mch_start_blink();
2725 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002726 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 focus = gui.in_focus;
2728 }
2729
Bram Moolenaar93c88e02015-09-15 14:12:05 +02002730#ifdef MESSAGE_QUEUE
Bram Moolenaar4231da42016-06-02 14:30:04 +02002731# ifdef FEAT_TIMERS
2732 did_add_timer = FALSE;
2733# endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02002734 parse_queued_messages();
Bram Moolenaar4231da42016-06-02 14:30:04 +02002735# ifdef FEAT_TIMERS
2736 if (did_add_timer)
2737 /* Need to recompute the waiting time. */
2738 break;
2739# endif
Bram Moolenaar03531f72010-11-16 15:04:57 +01002740#endif
2741
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 /*
2743 * Don't use gui_mch_update() because then we will spin-lock until a
2744 * char arrives, instead we use XtAppProcessEvent() to hang until an
2745 * event arrives. No need to check for input_buf_full because we are
2746 * returning as soon as it contains a single char. Note that
2747 * XtAppNextEvent() may not be used because it will not return after a
2748 * timer event has arrived -- webb
2749 */
2750 XtAppProcessEvent(app_context, desired);
2751
2752 if (input_available())
2753 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002754 retval = OK;
2755 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 }
2757 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002758
2759 if (timer != (XtIntervalId)0 && !timed_out)
2760 XtRemoveTimeOut(timer);
Bram Moolenaar1dccf632017-08-27 17:38:27 +02002761#ifdef FEAT_JOB_CHANNEL
2762 if (channel_timer != (XtIntervalId)0)
2763 XtRemoveTimeOut(channel_timer);
2764#endif
Bram Moolenaar4231da42016-06-02 14:30:04 +02002765
2766 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767}
2768
2769/*
2770 * Output routines.
2771 */
2772
2773/* Flush any output to the screen */
2774 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002775gui_mch_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776{
2777 XFlush(gui.dpy);
2778}
2779
2780/*
2781 * Clear a rectangular region of the screen from text pos (row1, col1) to
2782 * (row2, col2) inclusive.
2783 */
2784 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002785gui_mch_clear_block(
2786 int row1,
2787 int col1,
2788 int row2,
2789 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790{
2791 int x;
2792
2793 x = FILL_X(col1);
2794
2795 /* Clear one extra pixel at the far right, for when bold characters have
2796 * spilled over to the next column. */
2797 XFillRectangle(gui.dpy, gui.wid, gui.back_gc, x, FILL_Y(row1),
2798 (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1),
2799 (row2 - row1 + 1) * gui.char_height);
2800}
2801
2802 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002803gui_mch_clear_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804{
2805 XClearArea(gui.dpy, gui.wid, 0, 0, 0, 0, False);
2806}
2807
2808/*
2809 * Delete the given number of lines from the given row, scrolling up any
2810 * text further down within the scroll region.
2811 */
2812 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002813gui_mch_delete_lines(int row, int num_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814{
2815 if (gui.visibility == VisibilityFullyObscured)
2816 return; /* Can't see the window */
2817
2818 /* copy one extra pixel at the far right, for when bold has spilled
2819 * over */
2820 XCopyArea(gui.dpy, gui.wid, gui.wid, gui.text_gc,
2821 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
2822 gui.char_width * (gui.scroll_region_right - gui.scroll_region_left + 1)
2823 + (gui.scroll_region_right == Columns - 1),
2824 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1),
2825 FILL_X(gui.scroll_region_left), FILL_Y(row));
2826
2827 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
2828 gui.scroll_region_left,
2829 gui.scroll_region_bot, gui.scroll_region_right);
2830 gui_x11_check_copy_area();
2831}
2832
2833/*
2834 * Insert the given number of lines before the given row, scrolling down any
2835 * following text within the scroll region.
2836 */
2837 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002838gui_mch_insert_lines(int row, int num_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839{
2840 if (gui.visibility == VisibilityFullyObscured)
2841 return; /* Can't see the window */
2842
2843 /* copy one extra pixel at the far right, for when bold has spilled
2844 * over */
2845 XCopyArea(gui.dpy, gui.wid, gui.wid, gui.text_gc,
2846 FILL_X(gui.scroll_region_left), FILL_Y(row),
2847 gui.char_width * (gui.scroll_region_right - gui.scroll_region_left + 1)
2848 + (gui.scroll_region_right == Columns - 1),
2849 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1),
2850 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines));
2851
2852 gui_clear_block(row, gui.scroll_region_left,
2853 row + num_lines - 1, gui.scroll_region_right);
2854 gui_x11_check_copy_area();
2855}
2856
2857/*
2858 * Update the region revealed by scrolling up/down.
2859 */
2860 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002861gui_x11_check_copy_area(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862{
2863 XEvent event;
2864 XGraphicsExposeEvent *gevent;
2865
2866 if (gui.visibility != VisibilityPartiallyObscured)
2867 return;
2868
2869 XFlush(gui.dpy);
2870
2871 /* Wait to check whether the scroll worked or not */
2872 for (;;)
2873 {
2874 if (XCheckTypedEvent(gui.dpy, NoExpose, &event))
2875 return; /* The scroll worked. */
2876
2877 if (XCheckTypedEvent(gui.dpy, GraphicsExpose, &event))
2878 {
2879 gevent = (XGraphicsExposeEvent *)&event;
2880 gui_redraw(gevent->x, gevent->y, gevent->width, gevent->height);
2881 if (gevent->count == 0)
2882 return; /* This was the last expose event */
2883 }
2884 XSync(gui.dpy, False);
2885 }
2886}
2887
2888/*
2889 * X Selection stuff, for cutting and pasting text to other windows.
2890 */
2891
2892 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002893clip_mch_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894{
2895 clip_x11_lose_selection(vimShell, cbd);
2896}
2897
2898 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002899clip_mch_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900{
2901 return clip_x11_own_selection(vimShell, cbd);
2902}
2903
2904 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002905clip_mch_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906{
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002907 clip_x11_request_selection(vimShell, gui.dpy, cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908}
2909
2910 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002911clip_mch_set_selection(
2912 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913{
2914 clip_x11_set_selection(cbd);
2915}
2916
2917#if defined(FEAT_MENU) || defined(PROTO)
2918/*
2919 * Menu stuff.
2920 */
2921
2922/*
2923 * Make a menu either grey or not grey.
2924 */
2925 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002926gui_mch_menu_grey(vimmenu_T *menu, int grey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927{
2928 if (menu->id != (Widget)0)
2929 {
2930 gui_mch_menu_hidden(menu, False);
2931 if (grey
2932#ifdef FEAT_GUI_MOTIF
2933 || !menu->sensitive
2934#endif
2935 )
2936 XtSetSensitive(menu->id, False);
2937 else
2938 XtSetSensitive(menu->id, True);
2939 }
2940}
2941
2942/*
2943 * Make menu item hidden or not hidden
2944 */
2945 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002946gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947{
2948 if (menu->id != (Widget)0)
2949 {
2950 if (hidden)
2951 XtUnmanageChild(menu->id);
2952 else
2953 XtManageChild(menu->id);
2954 }
2955}
2956
2957/*
2958 * This is called after setting all the menus to grey/hidden or not.
2959 */
2960 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002961gui_mch_draw_menubar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962{
2963 /* Nothing to do in X */
2964}
2965
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002967gui_x11_menu_cb(
2968 Widget w UNUSED,
2969 XtPointer client_data,
2970 XtPointer call_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971{
2972 gui_menu_cb((vimmenu_T *)client_data);
2973}
2974
2975#endif /* FEAT_MENU */
2976
2977
2978
2979/*
2980 * Function called when window closed. Works like ":qa".
2981 * Should put up a requester!
2982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002984gui_x11_wm_protocol_handler(
2985 Widget w UNUSED,
2986 XtPointer client_data UNUSED,
2987 XEvent *event,
2988 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989{
2990 /*
2991 * Only deal with Client messages.
2992 */
2993 if (event->type != ClientMessage)
2994 return;
2995
2996 /*
2997 * The WM_SAVE_YOURSELF event arrives when the window manager wants to
2998 * exit. That can be cancelled though, thus Vim shouldn't exit here.
2999 * Just sync our swap files.
3000 */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003001 if ((Atom)((XClientMessageEvent *)event)->data.l[0] ==
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 wm_atoms[SAVE_YOURSELF_IDX])
3003 {
3004 out_flush();
3005 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
3006
3007 /* Set the window's WM_COMMAND property, to let the window manager
3008 * know we are done saving ourselves. We don't want to be restarted,
3009 * thus set argv to NULL. */
3010 XSetCommand(gui.dpy, XtWindow(vimShell), NULL, 0);
3011 return;
3012 }
3013
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003014 if ((Atom)((XClientMessageEvent *)event)->data.l[0] !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 wm_atoms[DELETE_WINDOW_IDX])
3016 return;
3017
3018 gui_shell_closed();
3019}
3020
3021#ifdef FEAT_CLIENTSERVER
3022/*
3023 * Function called when property changed. Check for incoming commands
3024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003026gui_x11_send_event_handler(
3027 Widget w UNUSED,
3028 XtPointer client_data UNUSED,
3029 XEvent *event,
3030 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031{
3032 XPropertyEvent *e = (XPropertyEvent *) event;
3033
3034 if (e->type == PropertyNotify && e->window == commWindow
3035 && e->atom == commProperty && e->state == PropertyNewValue)
3036 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003037 serverEventProc(gui.dpy, event, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 }
3039}
3040#endif
3041
3042/*
3043 * Cursor blink functions.
3044 *
3045 * This is a simple state machine:
3046 * BLINK_NONE not blinking at all
3047 * BLINK_OFF blinking, cursor is not shown
3048 * BLINK_ON blinking, cursor is shown
3049 */
3050
3051#define BLINK_NONE 0
3052#define BLINK_OFF 1
3053#define BLINK_ON 2
3054
3055static int blink_state = BLINK_NONE;
3056static long_u blink_waittime = 700;
3057static long_u blink_ontime = 400;
3058static long_u blink_offtime = 250;
3059static XtIntervalId blink_timer = (XtIntervalId)0;
3060
Bram Moolenaar703a8042016-06-04 16:24:32 +02003061 int
3062gui_mch_is_blinking(void)
3063{
3064 return blink_state != BLINK_NONE;
3065}
3066
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +02003067 int
3068gui_mch_is_blink_off(void)
3069{
3070 return blink_state == BLINK_OFF;
3071}
3072
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 void
Bram Moolenaar02fdaea2016-01-30 18:13:55 +01003074gui_mch_set_blinking(long waittime, long on, long off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075{
3076 blink_waittime = waittime;
3077 blink_ontime = on;
3078 blink_offtime = off;
3079}
3080
3081/*
3082 * Stop the cursor blinking. Show the cursor if it wasn't shown.
3083 */
3084 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01003085gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086{
3087 if (blink_timer != (XtIntervalId)0)
3088 {
3089 XtRemoveTimeOut(blink_timer);
3090 blink_timer = (XtIntervalId)0;
3091 }
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01003092 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 gui_update_cursor(TRUE, FALSE);
3094 blink_state = BLINK_NONE;
3095}
3096
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003098gui_x11_blink_cb(
3099 XtPointer timed_out UNUSED,
3100 XtIntervalId *interval_id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101{
3102 if (blink_state == BLINK_ON)
3103 {
3104 gui_undraw_cursor();
3105 blink_state = BLINK_OFF;
3106 blink_timer = XtAppAddTimeOut(app_context, blink_offtime,
3107 gui_x11_blink_cb, NULL);
3108 }
3109 else
3110 {
3111 gui_update_cursor(TRUE, FALSE);
3112 blink_state = BLINK_ON;
3113 blink_timer = XtAppAddTimeOut(app_context, blink_ontime,
3114 gui_x11_blink_cb, NULL);
3115 }
3116}
3117
3118/*
Bram Moolenaar1dccf632017-08-27 17:38:27 +02003119 * Start the cursor blinking. If it was already blinking, this restarts the
3120 * waiting time and shows the cursor.
3121 */
3122 void
3123gui_mch_start_blink(void)
3124{
3125 if (blink_timer != (XtIntervalId)0)
3126 XtRemoveTimeOut(blink_timer);
3127 /* Only switch blinking on if none of the times is zero */
3128 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
3129 {
3130 blink_timer = XtAppAddTimeOut(app_context, blink_waittime,
3131 gui_x11_blink_cb, NULL);
3132 blink_state = BLINK_ON;
3133 gui_update_cursor(TRUE, FALSE);
3134 }
3135}
3136
3137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 * Return the RGB value of a pixel as a long.
3139 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003140 guicolor_T
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003141gui_mch_get_rgb(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
3143 XColor xc;
3144 Colormap colormap;
3145
3146 colormap = DefaultColormap(gui.dpy, XDefaultScreen(gui.dpy));
3147 xc.pixel = pixel;
3148 XQueryColor(gui.dpy, colormap, &xc);
3149
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003150 return (guicolor_T)(((xc.red & 0xff00) << 8) + (xc.green & 0xff00)
3151 + ((unsigned)xc.blue >> 8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152}
3153
3154/*
3155 * Add the callback functions.
3156 */
3157 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003158gui_x11_callbacks(Widget textArea, Widget vimForm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159{
3160 XtAddEventHandler(textArea, VisibilityChangeMask, FALSE,
3161 gui_x11_visibility_cb, (XtPointer)0);
3162
3163 XtAddEventHandler(textArea, ExposureMask, FALSE, gui_x11_expose_cb,
3164 (XtPointer)0);
3165
3166 XtAddEventHandler(vimShell, StructureNotifyMask, FALSE,
3167 gui_x11_resize_window_cb, (XtPointer)0);
3168
3169 XtAddEventHandler(vimShell, FocusChangeMask, FALSE, gui_x11_focus_change_cb,
3170 (XtPointer)0);
3171 /*
3172 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3173 * Only needed for some window managers.
3174 */
3175 if (vim_strchr(p_go, GO_POINTER) != NULL)
3176 {
3177 XtAddEventHandler(vimShell, LeaveWindowMask, FALSE, gui_x11_leave_cb,
3178 (XtPointer)0);
3179 XtAddEventHandler(textArea, LeaveWindowMask, FALSE, gui_x11_leave_cb,
3180 (XtPointer)0);
3181 XtAddEventHandler(textArea, EnterWindowMask, FALSE, gui_x11_enter_cb,
3182 (XtPointer)0);
3183 XtAddEventHandler(vimShell, EnterWindowMask, FALSE, gui_x11_enter_cb,
3184 (XtPointer)0);
3185 }
3186
3187 XtAddEventHandler(vimForm, KeyPressMask, FALSE, gui_x11_key_hit_cb,
3188 (XtPointer)0);
3189 XtAddEventHandler(textArea, KeyPressMask, FALSE, gui_x11_key_hit_cb,
3190 (XtPointer)0);
3191
3192 /* get pointer moved events from scrollbar, needed for 'mousefocus' */
3193 XtAddEventHandler(vimForm, PointerMotionMask,
3194 FALSE, gui_x11_mouse_cb, (XtPointer)1);
3195 XtAddEventHandler(textArea, ButtonPressMask | ButtonReleaseMask |
3196 ButtonMotionMask | PointerMotionMask,
3197 FALSE, gui_x11_mouse_cb, (XtPointer)0);
3198}
3199
3200/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003201 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003203 void
3204gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205{
3206 int rootx, rooty, winx, winy;
3207 Window root, child;
3208 unsigned int mask;
3209
3210 if (gui.wid && XQueryPointer(gui.dpy, gui.wid, &root, &child,
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003211 &rootx, &rooty, &winx, &winy, &mask)) {
3212 *x = winx;
3213 *y = winy;
3214 } else {
3215 *x = -1;
3216 *y = -1;
3217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218}
3219
3220 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003221gui_mch_setmouse(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
3223 if (gui.wid)
3224 XWarpPointer(gui.dpy, (Window)0, gui.wid, 0, 0, 0, 0, x, y);
3225}
3226
3227#if (defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)) || defined(PROTO)
3228 XButtonPressedEvent *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003229gui_x11_get_last_mouse_event(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230{
3231 return &last_mouse_event;
3232}
3233#endif
3234
3235#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
3236
3237/* Signs are currently always 2 chars wide. Hopefully the font is big enough
3238 * to provide room for the bitmap! */
3239# define SIGN_WIDTH (gui.char_width * 2)
3240
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003242gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243{
3244 XImage *sign;
3245
3246 if (gui.in_use && (sign = (XImage *)sign_get_image(typenr)) != NULL)
3247 {
3248 XClearArea(gui.dpy, gui.wid, TEXT_X(col), TEXT_Y(row) - sign->height,
3249 SIGN_WIDTH, gui.char_height, FALSE);
3250 XPutImage(gui.dpy, gui.wid, gui.text_gc, sign, 0, 0,
3251 TEXT_X(col) + (SIGN_WIDTH - sign->width) / 2,
3252 TEXT_Y(row) - sign->height,
3253 sign->width, sign->height);
3254 }
3255}
3256
3257 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003258gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259{
3260 XpmAttributes attrs;
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003261 XImage *sign = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 int status;
3263
3264 /*
3265 * Setup the color substitution table.
3266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 if (signfile[0] != NUL && signfile[0] != '-')
3268 {
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003269 XpmColorSymbol color[5] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003271 {"none", NULL, 0},
3272 {"iconColor1", NULL, 0},
3273 {"bottomShadowColor", NULL, 0},
3274 {"topShadowColor", NULL, 0},
3275 {"selectColor", NULL, 0}
3276 };
3277 attrs.valuemask = XpmColorSymbols;
3278 attrs.numsymbols = 2;
3279 attrs.colorsymbols = color;
3280 attrs.colorsymbols[0].pixel = gui.back_pixel;
3281 attrs.colorsymbols[1].pixel = gui.norm_pixel;
3282 status = XpmReadFileToImage(gui.dpy, (char *)signfile,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 &sign, NULL, &attrs);
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003284 if (status == 0)
3285 {
3286 /* Sign width is fixed at two columns now.
3287 if (sign->width > gui.sign_width)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003288 gui.sign_width = sign->width + 8; */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 }
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003290 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003291 emsg(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 }
3293
3294 return (void *)sign;
3295}
3296
3297 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003298gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299{
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003300 XDestroyImage((XImage*)sign);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301}
3302#endif
3303
3304
3305#ifdef FEAT_MOUSESHAPE
3306/* The last set mouse pointer shape is remembered, to be used when it goes
3307 * from hidden to not hidden. */
3308static int last_shape = 0;
3309#endif
3310
3311/*
3312 * Use the blank mouse pointer or not.
3313 */
3314 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003315gui_mch_mousehide(
3316 int hide) /* TRUE = use blank ptr, FALSE = use parent ptr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317{
3318 if (gui.pointer_hidden != hide)
3319 {
3320 gui.pointer_hidden = hide;
3321 if (hide)
3322 XDefineCursor(gui.dpy, gui.wid, gui.blank_pointer);
3323 else
3324#ifdef FEAT_MOUSESHAPE
3325 mch_set_mouse_shape(last_shape);
3326#else
3327 XUndefineCursor(gui.dpy, gui.wid);
3328#endif
3329 }
3330}
3331
3332#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3333
3334/* Table for shape IDs. Keep in sync with the mshape_names[] table in
3335 * misc2.c! */
3336static int mshape_ids[] =
3337{
3338 XC_left_ptr, /* arrow */
3339 0, /* blank */
3340 XC_xterm, /* beam */
3341 XC_sb_v_double_arrow, /* updown */
3342 XC_sizing, /* udsizing */
3343 XC_sb_h_double_arrow, /* leftright */
3344 XC_sizing, /* lrsizing */
3345 XC_watch, /* busy */
3346 XC_X_cursor, /* no */
3347 XC_crosshair, /* crosshair */
3348 XC_hand1, /* hand1 */
3349 XC_hand2, /* hand2 */
3350 XC_pencil, /* pencil */
3351 XC_question_arrow, /* question */
3352 XC_right_ptr, /* right-arrow */
3353 XC_center_ptr, /* up-arrow */
3354 XC_left_ptr /* last one */
3355};
3356
3357 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003358mch_set_mouse_shape(int shape)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359{
3360 int id;
3361
3362 if (!gui.in_use)
3363 return;
3364
3365 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
3366 XDefineCursor(gui.dpy, gui.wid, gui.blank_pointer);
3367 else
3368 {
3369 if (shape >= MSHAPE_NUMBERED)
3370 {
3371 id = shape - MSHAPE_NUMBERED;
3372 if (id >= XC_num_glyphs)
3373 id = XC_left_ptr;
3374 else
3375 id &= ~1; /* they are always even (why?) */
3376 }
3377 else
3378 id = mshape_ids[shape];
3379
3380 XDefineCursor(gui.dpy, gui.wid, XCreateFontCursor(gui.dpy, id));
3381 }
3382 if (shape != MSHAPE_HIDE)
3383 last_shape = shape;
3384}
3385#endif
3386
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003387#if (defined(FEAT_TOOLBAR) && defined(FEAT_BEVAL_GUI)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388/*
3389 * Set the balloon-eval used for the tooltip of a toolbar menu item.
3390 * The check for a non-toolbar item was added, because there is a crash when
3391 * passing a normal menu item here. Can't explain that, but better avoid it.
3392 */
3393 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003394gui_mch_menu_set_tip(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395{
3396 if (menu->id != NULL && menu->parent != NULL
3397 && menu_is_toolbar(menu->parent->name))
3398 {
3399 /* Always destroy and create the balloon, in case the string was
3400 * changed. */
3401 if (menu->tip != NULL)
3402 {
3403 gui_mch_destroy_beval_area(menu->tip);
3404 menu->tip = NULL;
3405 }
3406 if (menu->strings[MENU_INDEX_TIP] != NULL)
3407 menu->tip = gui_mch_create_beval_area(
3408 menu->id,
3409 menu->strings[MENU_INDEX_TIP],
3410 NULL,
3411 NULL);
3412 }
3413}
3414#endif