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