blob: 809054f0f7d7fff4e53f393145bffb23c419b96e [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;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001001 if (ev_press->state & Mod4Mask)
1002 modifiers |= MOD_MASK_META;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
1004 /*
1005 * For some keys a shift modifier is translated into another key
1006 * code.
1007 */
1008 if (len == -3)
1009 key = TO_SPECIAL(string[1], string[2]);
1010 else
1011 key = string[0];
1012 key = simplify_key(key, &modifiers);
1013 if (key == CSI)
1014 key = K_CSI;
1015 if (IS_SPECIAL(key))
1016 {
1017 string[0] = CSI;
1018 string[1] = K_SECOND(key);
1019 string[2] = K_THIRD(key);
1020 len = 3;
1021 }
1022 else
1023 {
1024 string[0] = key;
1025 len = 1;
1026 }
1027
1028 if (modifiers != 0)
1029 {
1030 string2[0] = CSI;
1031 string2[1] = KS_MODIFIER;
1032 string2[2] = modifiers;
1033 add_to_input_buf(string2, 3);
1034 }
1035 }
1036
1037 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1038#ifdef UNIX
1039 || (intr_char != 0 && string[0] == intr_char)
1040#endif
1041 ))
1042 {
1043 trash_input_buf();
1044 got_int = TRUE;
1045 }
1046
1047 add_to_input_buf(string, len);
1048
1049 /*
1050 * blank out the pointer if necessary
1051 */
1052 if (p_mh)
1053 gui_mch_mousehide(TRUE);
1054
1055#if defined(FEAT_BEVAL_TIP)
1056 {
1057 BalloonEval *be;
1058
1059 if ((be = gui_mch_currently_showing_beval()) != NULL)
1060 gui_mch_unpost_balloon(be);
1061 }
1062#endif
1063theend:
1064 {} /* some compilers need a statement here */
1065#ifdef FEAT_XIM
1066 if (string_alloced)
1067 XtFree((char *)string);
1068#endif
1069}
1070
1071/* ARGSUSED */
1072 static void
1073gui_x11_mouse_cb(w, dud, event, dum)
1074 Widget w;
1075 XtPointer dud;
1076 XEvent *event;
1077 Boolean *dum;
1078{
1079 static XtIntervalId timer = (XtIntervalId)0;
1080 static int timed_out = TRUE;
1081
1082 int button;
1083 int repeated_click = FALSE;
1084 int x, y;
1085 int_u x_modifiers;
1086 int_u vim_modifiers;
1087
1088 if (event->type == MotionNotify)
1089 {
1090 /* Get the latest position, avoids lagging behind on a drag. */
1091 x = event->xmotion.x;
1092 y = event->xmotion.y;
1093 x_modifiers = event->xmotion.state;
1094 button = (x_modifiers & (Button1Mask | Button2Mask | Button3Mask))
1095 ? MOUSE_DRAG : ' ';
1096
1097 /*
1098 * if our pointer is currently hidden, then we should show it.
1099 */
1100 gui_mch_mousehide(FALSE);
1101
1102 if (button != MOUSE_DRAG) /* just moving the rodent */
1103 {
1104#ifdef FEAT_MENU
1105 if (dud) /* moved in vimForm */
1106 y -= gui.menu_height;
1107#endif
1108 gui_mouse_moved(x, y);
1109 return;
1110 }
1111 }
1112 else
1113 {
1114 x = event->xbutton.x;
1115 y = event->xbutton.y;
1116 if (event->type == ButtonPress)
1117 {
1118 /* Handle multiple clicks */
1119 if (!timed_out)
1120 {
1121 XtRemoveTimeOut(timer);
1122 repeated_click = TRUE;
1123 }
1124 timed_out = FALSE;
1125 timer = XtAppAddTimeOut(app_context, (long_u)p_mouset,
1126 gui_x11_timer_cb, &timed_out);
1127 switch (event->xbutton.button)
1128 {
1129 case Button1: button = MOUSE_LEFT; break;
1130 case Button2: button = MOUSE_MIDDLE; break;
1131 case Button3: button = MOUSE_RIGHT; break;
1132 case Button4: button = MOUSE_4; break;
1133 case Button5: button = MOUSE_5; break;
1134 default:
1135 return; /* Unknown button */
1136 }
1137 }
1138 else if (event->type == ButtonRelease)
1139 button = MOUSE_RELEASE;
1140 else
1141 return; /* Unknown mouse event type */
1142
1143 x_modifiers = event->xbutton.state;
1144#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1145 last_mouse_event = event->xbutton;
1146#endif
1147 }
1148
1149 vim_modifiers = 0x0;
1150 if (x_modifiers & ShiftMask)
1151 vim_modifiers |= MOUSE_SHIFT;
1152 if (x_modifiers & ControlMask)
1153 vim_modifiers |= MOUSE_CTRL;
1154 if (x_modifiers & Mod1Mask) /* Alt or Meta key */
1155 vim_modifiers |= MOUSE_ALT;
1156
1157 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
1158}
1159
1160#ifdef FEAT_SNIFF
1161/* ARGSUSED */
1162 static void
1163gui_x11_sniff_request_cb(closure, source, id)
1164 XtPointer closure;
1165 int *source;
1166 XtInputId *id;
1167{
1168 static char_u bytes[3] = {CSI, (int)KS_EXTRA, (int)KE_SNIFF};
1169
1170 add_to_input_buf(bytes, 3);
1171}
1172#endif
1173
1174/*
1175 * End of call-back routines
1176 */
1177
1178/*
1179 * Parse the GUI related command-line arguments. Any arguments used are
1180 * deleted from argv, and *argc is decremented accordingly. This is called
1181 * when vim is started, whether or not the GUI has been started.
1182 */
1183 void
1184gui_mch_prepare(argc, argv)
1185 int *argc;
1186 char **argv;
1187{
1188 int arg;
1189 int i;
1190
1191 /*
1192 * Move all the entries in argv which are relevant to X into gui_argv.
1193 */
1194 gui_argc = 0;
1195 gui_argv = (char **)lalloc((long_u)(*argc * sizeof(char *)), FALSE);
1196 if (gui_argv == NULL)
1197 return;
1198 gui_argv[gui_argc++] = argv[0];
1199 arg = 1;
1200 while (arg < *argc)
1201 {
1202 /* Look for argv[arg] in cmdline_options[] table */
1203 for (i = 0; i < XtNumber(cmdline_options); i++)
1204 if (strcmp(argv[arg], cmdline_options[i].option) == 0)
1205 break;
1206
1207 if (i < XtNumber(cmdline_options))
1208 {
1209 /* Remember finding "-rv" or "-reverse" */
1210 if (strcmp("-rv", argv[arg]) == 0
1211 || strcmp("-reverse", argv[arg]) == 0)
1212 found_reverse_arg = TRUE;
1213 else if ((strcmp("-fn", argv[arg]) == 0
1214 || strcmp("-font", argv[arg]) == 0)
1215 && arg + 1 < *argc)
1216 font_argument = argv[arg + 1];
1217
1218 /* Found match in table, so move it into gui_argv */
1219 gui_argv[gui_argc++] = argv[arg];
1220 if (--*argc > arg)
1221 {
1222 mch_memmove(&argv[arg], &argv[arg + 1], (*argc - arg)
1223 * sizeof(char *));
1224 if (cmdline_options[i].argKind != XrmoptionNoArg)
1225 {
1226 /* Move the options argument as well */
1227 gui_argv[gui_argc++] = argv[arg];
1228 if (--*argc > arg)
1229 mch_memmove(&argv[arg], &argv[arg + 1], (*argc - arg)
1230 * sizeof(char *));
1231 }
1232 }
1233 argv[*argc] = NULL;
1234 }
1235 else
1236#ifdef FEAT_SUN_WORKSHOP
1237 if (strcmp("-ws", argv[arg]) == 0)
1238 {
1239 usingSunWorkShop++;
1240 p_acd = TRUE;
1241 gui.dofork = FALSE; /* don't fork() when starting GUI */
1242 mch_memmove(&argv[arg], &argv[arg + 1],
1243 (--*argc - arg) * sizeof(char *));
1244 argv[*argc] = NULL;
1245# ifdef WSDEBUG
1246 wsdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
1247 wsdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
1248# endif
1249 }
1250 else
1251#endif
1252#ifdef FEAT_NETBEANS_INTG
1253 if (strncmp("-nb", argv[arg], 3) == 0)
1254 {
1255 usingNetbeans++;
1256 gui.dofork = FALSE; /* don't fork() when starting GUI */
1257 netbeansArg = argv[arg];
1258 mch_memmove(&argv[arg], &argv[arg + 1],
1259 (--*argc - arg) * sizeof(char *));
1260 argv[*argc] = NULL;
1261 }
1262 else
1263#endif
1264 arg++;
1265 }
1266}
1267
1268#ifndef XtSpecificationRelease
1269# define CARDINAL (Cardinal *)
1270#else
1271# if XtSpecificationRelease == 4
1272# define CARDINAL (Cardinal *)
1273# else
1274# define CARDINAL (int *)
1275# endif
1276#endif
1277
1278/*
1279 * Check if the GUI can be started. Called before gvimrc is sourced.
1280 * Return OK or FAIL.
1281 */
1282 int
1283gui_mch_init_check()
1284{
1285#ifdef FEAT_XIM
1286 XtSetLanguageProc(NULL, NULL, NULL);
1287#endif
1288 open_app_context();
1289 if (app_context != NULL)
1290 gui.dpy = XtOpenDisplay(app_context, 0, VIM_NAME, VIM_CLASS,
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00001291 cmdline_options, XtNumber(cmdline_options),
1292 CARDINAL &gui_argc, gui_argv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293
1294 if (app_context == NULL || gui.dpy == NULL)
1295 {
1296 gui.dying = TRUE;
1297 EMSG(_(e_opendisp));
1298 return FAIL;
1299 }
1300 return OK;
1301}
1302
1303
1304#ifdef USE_XSMP
1305/*
1306 * Handle XSMP processing, de-registering the attachment upon error
1307 */
1308static XtInputId _xsmp_xtinputid;
1309
1310static void local_xsmp_handle_requests __ARGS((XtPointer c, int *s, XtInputId *i));
1311
1312/*ARGSUSED*/
1313 static void
1314local_xsmp_handle_requests(c, s, i)
1315 XtPointer c;
1316 int *s;
1317 XtInputId *i;
1318{
1319 if (xsmp_handle_requests() == FAIL)
1320 XtRemoveInput(_xsmp_xtinputid);
1321}
1322#endif
1323
1324
1325/*
1326 * Initialise the X GUI. Create all the windows, set up all the call-backs etc.
1327 * Returns OK for success, FAIL when the GUI can't be started.
1328 */
1329 int
1330gui_mch_init()
1331{
1332 XtGCMask gc_mask;
1333 XGCValues gc_vals;
1334 int x, y, mask;
1335 unsigned w, h;
1336
1337#if 0
1338 /* Uncomment this to enable synchronous mode for debugging */
1339 XSynchronize(gui.dpy, True);
1340#endif
1341
1342 vimShell = XtVaAppCreateShell(VIM_NAME, VIM_CLASS,
1343 applicationShellWidgetClass, gui.dpy, NULL);
1344
1345 /*
1346 * Get the application resources
1347 */
1348 XtVaGetApplicationResources(vimShell, (XtPointer)&gui,
1349 vim_resources, XtNumber(vim_resources), NULL);
1350
1351 gui.scrollbar_height = gui.scrollbar_width;
1352
1353 /*
1354 * Get the colors ourselves. Using the automatic conversion doesn't
1355 * handle looking for approximate colors.
1356 */
1357 /* NOTE: These next few lines are an exact duplicate of gui_athena.c's
1358 * gui_mch_def_colors(). Why?
1359 */
1360 gui.menu_fg_pixel = gui_get_color((char_u *)gui.rsrc_menu_fg_name);
1361 gui.menu_bg_pixel = gui_get_color((char_u *)gui.rsrc_menu_bg_name);
1362 gui.scroll_fg_pixel = gui_get_color((char_u *)gui.rsrc_scroll_fg_name);
1363 gui.scroll_bg_pixel = gui_get_color((char_u *)gui.rsrc_scroll_bg_name);
1364#ifdef FEAT_BEVAL
1365 gui.tooltip_fg_pixel = gui_get_color((char_u *)gui.rsrc_tooltip_fg_name);
1366 gui.tooltip_bg_pixel = gui_get_color((char_u *)gui.rsrc_tooltip_bg_name);
1367#endif
1368
1369#if defined(FEAT_MENU) && defined(FEAT_GUI_ATHENA)
1370 /* If the menu height was set, don't change it at runtime */
1371 if (gui.menu_height != MENU_DEFAULT_HEIGHT)
1372 gui.menu_height_fixed = TRUE;
1373#endif
1374
1375 /* Set default foreground and background colours */
1376 gui.norm_pixel = gui.def_norm_pixel;
1377 gui.back_pixel = gui.def_back_pixel;
1378
1379 /* Check if reverse video needs to be applied (on Sun it's done by X) */
1380 if (gui.rsrc_rev_video && gui_get_lightness(gui.back_pixel)
1381 > gui_get_lightness(gui.norm_pixel))
1382 {
1383 gui.norm_pixel = gui.def_back_pixel;
1384 gui.back_pixel = gui.def_norm_pixel;
1385 gui.def_norm_pixel = gui.norm_pixel;
1386 gui.def_back_pixel = gui.back_pixel;
1387 }
1388
1389 /* Get the colors from the "Normal", "Tooltip", "Scrollbar" and "Menu"
1390 * group (set in syntax.c or in a vimrc file) */
1391 set_normal_colors();
1392
1393 /*
1394 * Check that none of the colors are the same as the background color
1395 */
1396 gui_check_colors();
1397
1398 /*
1399 * Set up the GCs. The font attributes will be set in gui_init_font().
1400 */
1401 gc_mask = GCForeground | GCBackground;
1402 gc_vals.foreground = gui.norm_pixel;
1403 gc_vals.background = gui.back_pixel;
1404 gui.text_gc = XtGetGC(vimShell, gc_mask, &gc_vals);
1405
1406 gc_vals.foreground = gui.back_pixel;
1407 gc_vals.background = gui.norm_pixel;
1408 gui.back_gc = XtGetGC(vimShell, gc_mask, &gc_vals);
1409
1410 gc_mask |= GCFunction;
1411 gc_vals.foreground = gui.norm_pixel ^ gui.back_pixel;
1412 gc_vals.background = gui.norm_pixel ^ gui.back_pixel;
1413 gc_vals.function = GXxor;
1414 gui.invert_gc = XtGetGC(vimShell, gc_mask, &gc_vals);
1415
1416 gui.visibility = VisibilityUnobscured;
1417 x11_setup_atoms(gui.dpy);
1418
1419 if (gui_win_x != -1 && gui_win_y != -1)
1420 gui_mch_set_winpos(gui_win_x, gui_win_y);
1421
1422 /* Now adapt the supplied(?) geometry-settings */
1423 /* Added by Kjetil Jacobsen <kjetilja@stud.cs.uit.no> */
1424 if (gui.geom != NULL && *gui.geom != NUL)
1425 {
1426 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
1427 if (mask & WidthValue)
1428 Columns = w;
1429 if (mask & HeightValue)
1430 Rows = h;
1431 /*
1432 * Set the (x,y) position of the main window only if specified in the
1433 * users geometry, so we get good defaults when they don't. This needs
1434 * to be done before the shell is popped up.
1435 */
1436 if (mask & (XValue|YValue))
1437 XtVaSetValues(vimShell, XtNgeometry, gui.geom, NULL);
1438 }
1439
1440 gui_x11_create_widgets();
1441
1442 /*
1443 * Add an icon to Vim (Marcel Douben: 11 May 1998).
1444 */
1445 if (vim_strchr(p_go, GO_ICON) != NULL)
1446 {
1447#ifndef HAVE_XPM
1448# include "vim_icon.xbm"
1449# include "vim_mask.xbm"
1450
1451 Arg arg[2];
1452
1453 XtSetArg(arg[0], XtNiconPixmap,
1454 XCreateBitmapFromData(gui.dpy,
1455 DefaultRootWindow(gui.dpy),
1456 (char *)vim_icon_bits,
1457 vim_icon_width,
1458 vim_icon_height));
1459 XtSetArg(arg[1], XtNiconMask,
1460 XCreateBitmapFromData(gui.dpy,
1461 DefaultRootWindow(gui.dpy),
1462 (char *)vim_mask_icon_bits,
1463 vim_mask_icon_width,
1464 vim_mask_icon_height));
1465 XtSetValues(vimShell, arg, (Cardinal)2);
1466#else
1467/* Use Pixmaps, looking much nicer. */
1468
1469/* If you get an error message here, you still need to unpack the runtime
1470 * archive! */
1471# ifdef magick
1472# undef magick
1473# endif
1474# define magick vim32x32
1475# include "../runtime/vim32x32.xpm"
1476# undef magick
1477# define magick vim16x16
1478# include "../runtime/vim16x16.xpm"
1479# undef magick
1480# define magick vim48x48
1481# include "../runtime/vim48x48.xpm"
1482# undef magick
1483
1484 static Pixmap icon = 0;
1485 static Pixmap icon_mask = 0;
1486 static char **magick = vim32x32;
1487 Window root_window;
1488 XIconSize *size;
1489 int number_sizes;
1490 Display *dsp;
1491 Screen *scr;
1492 XpmAttributes attr;
1493 Colormap cmap;
1494
1495 /*
1496 * Adjust the icon to the preferences of the actual window manager.
1497 */
1498 root_window = XRootWindowOfScreen(XtScreen(vimShell));
1499 if (XGetIconSizes(XtDisplay(vimShell), root_window,
1500 &size, &number_sizes) != 0)
1501 {
1502
1503 if (number_sizes > 0)
1504 {
1505 if (size->max_height >= 48 && size->max_height >= 48)
1506 magick = vim48x48;
1507 else if (size->max_height >= 32 && size->max_height >= 32)
1508 magick = vim32x32;
1509 else if (size->max_height >= 16 && size->max_height >= 16)
1510 magick = vim16x16;
1511 }
1512 }
1513
1514 dsp = XtDisplay(vimShell);
1515 scr = XtScreen(vimShell);
1516
1517 cmap = DefaultColormap(dsp, DefaultScreen(dsp));
1518 XtVaSetValues(vimShell, XtNcolormap, cmap, NULL);
1519
1520 attr.valuemask = 0L;
1521 attr.valuemask = XpmCloseness | XpmReturnPixels | XpmColormap | XpmDepth;
1522 attr.closeness = 65535; /* accuracy isn't crucial */
1523 attr.colormap = cmap;
1524 attr.depth = DefaultDepthOfScreen(scr);
1525
1526 if (!icon)
1527 XpmCreatePixmapFromData(dsp, root_window, magick, &icon,
1528 &icon_mask, &attr);
1529
1530# ifdef FEAT_GUI_ATHENA
1531 XtVaSetValues(vimShell, XtNiconPixmap, icon, XtNiconMask, icon_mask, NULL);
1532# else
1533 XtVaSetValues(vimShell, XmNiconPixmap, icon, XmNiconMask, icon_mask, NULL);
1534# endif
1535#endif
1536 }
1537
1538 if (gui.color_approx)
1539 EMSG(_("Vim E458: Cannot allocate colormap entry, some colors may be incorrect"));
1540
1541#ifdef FEAT_SUN_WORKSHOP
1542 if (usingSunWorkShop)
1543 workshop_connect(app_context);
1544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545
1546#ifdef FEAT_BEVAL
1547 gui_init_tooltip_font();
1548#endif
1549#ifdef FEAT_MENU
1550 gui_init_menu_font();
1551#endif
1552
1553#ifdef USE_XSMP
1554 /* Attach listener on ICE connection */
1555 if (-1 != xsmp_icefd)
1556 _xsmp_xtinputid = XtAppAddInput(app_context, xsmp_icefd,
1557 (XtPointer)XtInputReadMask, local_xsmp_handle_requests, NULL);
1558#endif
1559
1560 return OK;
1561}
1562
1563/*
1564 * Called when starting the GUI fails after calling gui_mch_init().
1565 */
1566 void
1567gui_mch_uninit()
1568{
1569 gui_x11_destroy_widgets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 XtCloseDisplay(gui.dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 gui.dpy = NULL;
1572 vimShell = (Widget)0;
1573}
1574
1575/*
1576 * Called when the foreground or background color has been changed.
1577 */
1578 void
1579gui_mch_new_colors()
1580{
1581 long_u gc_mask;
1582 XGCValues gc_vals;
1583
1584 gc_mask = GCForeground | GCBackground;
1585 gc_vals.foreground = gui.norm_pixel;
1586 gc_vals.background = gui.back_pixel;
1587 if (gui.text_gc != NULL)
1588 XChangeGC(gui.dpy, gui.text_gc, gc_mask, &gc_vals);
1589
1590 gc_vals.foreground = gui.back_pixel;
1591 gc_vals.background = gui.norm_pixel;
1592 if (gui.back_gc != NULL)
1593 XChangeGC(gui.dpy, gui.back_gc, gc_mask, &gc_vals);
1594
1595 gc_mask |= GCFunction;
1596 gc_vals.foreground = gui.norm_pixel ^ gui.back_pixel;
1597 gc_vals.background = gui.norm_pixel ^ gui.back_pixel;
1598 gc_vals.function = GXxor;
1599 if (gui.invert_gc != NULL)
1600 XChangeGC(gui.dpy, gui.invert_gc, gc_mask, &gc_vals);
1601
1602 gui_x11_set_back_color();
1603}
1604
1605/*
1606 * Open the GUI window which was created by a call to gui_mch_init().
1607 */
1608 int
1609gui_mch_open()
1610{
1611 /* Actually open the window */
1612 XtPopup(vimShell, XtGrabNone);
1613
1614 gui.wid = gui_x11_get_wid();
1615 gui.blank_pointer = gui_x11_create_blank_mouse();
1616
1617 /*
1618 * Add a callback for the Close item on the window managers menu, and the
1619 * save-yourself event.
1620 */
1621 wm_atoms[SAVE_YOURSELF_IDX] =
1622 XInternAtom(gui.dpy, "WM_SAVE_YOURSELF", False);
1623 wm_atoms[DELETE_WINDOW_IDX] =
1624 XInternAtom(gui.dpy, "WM_DELETE_WINDOW", False);
1625 XSetWMProtocols(gui.dpy, XtWindow(vimShell), wm_atoms, 2);
1626 XtAddEventHandler(vimShell, NoEventMask, True, gui_x11_wm_protocol_handler,
1627 NULL);
1628#ifdef HAVE_X11_XMU_EDITRES_H
1629 /*
1630 * Enable editres protocol (see "man editres").
1631 * Usually will need to add -lXmu to the linker line as well.
1632 */
1633 XtAddEventHandler(vimShell, (EventMask)0, True, _XEditResCheckMessages,
1634 (XtPointer)NULL);
1635#endif
1636
1637#ifdef FEAT_CLIENTSERVER
1638 if (serverName == NULL && serverDelayedStartName != NULL)
1639 {
1640 /* This is a :gui command in a plain vim with no previous server */
1641 commWindow = XtWindow(vimShell);
1642 (void)serverRegisterName(gui.dpy, serverDelayedStartName);
1643 }
1644 else
1645 {
1646 /*
1647 * Cannot handle "widget-less" windows with XtProcessEvent() we'll
1648 * have to change the "server" registration to that of the main window
1649 * If we have not registered a name yet, remember the window
1650 */
1651 serverChangeRegisteredWindow(gui.dpy, XtWindow(vimShell));
1652 }
1653 XtAddEventHandler(vimShell, PropertyChangeMask, False,
1654 gui_x11_send_event_handler, NULL);
1655#endif
1656
1657
1658#if defined(FEAT_MENU) && defined(FEAT_GUI_ATHENA)
1659 /* The Athena GUI needs this again after opening the window */
1660 gui_position_menu();
1661# ifdef FEAT_TOOLBAR
1662 gui_mch_set_toolbar_pos(0, gui.menu_height, gui.menu_width,
1663 gui.toolbar_height);
1664# endif
1665#endif
1666
1667 /* Get the colors for the highlight groups (gui_check_colors() might have
1668 * changed them) */
1669 highlight_gui_started(); /* re-init colors and fonts */
1670
1671#ifdef FEAT_HANGULIN
1672 hangul_keyboard_set();
1673#endif
1674#ifdef FEAT_XIM
1675 xim_init();
1676#endif
1677#ifdef FEAT_SUN_WORKSHOP
1678 workshop_postinit();
1679#endif
1680
1681 return OK;
1682}
1683
1684#if defined(FEAT_BEVAL) || defined(PROTO)
1685/*
1686 * Convert the tooltip fontset name to an XFontSet.
1687 */
1688 void
1689gui_init_tooltip_font()
1690{
1691 XrmValue from, to;
1692
1693 from.addr = (char *)gui.rsrc_tooltip_font_name;
1694 from.size = strlen(from.addr);
1695 to.addr = (XtPointer)&gui.tooltip_fontset;
1696 to.size = sizeof(XFontSet);
1697
1698 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontSet, &to) == False)
1699 {
1700 /* Failed. What to do? */
1701 }
1702}
1703#endif
1704
1705#if defined(FEAT_MENU) || defined(PROTO)
1706/* Convert the menu font/fontset name to an XFontStruct/XFontset */
1707 void
1708gui_init_menu_font()
1709{
1710 XrmValue from, to;
1711
1712#ifdef FONTSET_ALWAYS
1713 from.addr = (char *)gui.rsrc_menu_font_name;
1714 from.size = strlen(from.addr);
1715 to.addr = (XtPointer)&gui.menu_fontset;
1716 to.size = sizeof(GuiFontset);
1717
1718 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontSet, &to) == False)
1719 {
1720 /* Failed. What to do? */
1721 }
1722#else
1723 from.addr = (char *)gui.rsrc_menu_font_name;
1724 from.size = strlen(from.addr);
1725 to.addr = (XtPointer)&gui.menu_font;
1726 to.size = sizeof(GuiFont);
1727
1728 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontStruct, &to) == False)
1729 {
1730 /* Failed. What to do? */
1731 }
1732#endif
1733}
1734#endif
1735
1736/*ARGSUSED*/
1737 void
1738gui_mch_exit(rc)
1739 int rc;
1740{
1741#if 0
1742 /* Lesstif gives an error message here, and so does Solaris. The man page
1743 * says that this isn't needed when exiting, so just skip it. */
1744 XtCloseDisplay(gui.dpy);
1745#endif
1746}
1747
1748/*
1749 * Get the position of the top left corner of the window.
1750 */
1751 int
1752gui_mch_get_winpos(x, y)
1753 int *x, *y;
1754{
1755 Dimension xpos, ypos;
1756
1757 XtVaGetValues(vimShell,
1758 XtNx, &xpos,
1759 XtNy, &ypos,
1760 NULL);
1761 *x = xpos;
1762 *y = ypos;
1763 return OK;
1764}
1765
1766/*
1767 * Set the position of the top left corner of the window to the given
1768 * coordinates.
1769 */
1770 void
1771gui_mch_set_winpos(x, y)
1772 int x, y;
1773{
1774 XtVaSetValues(vimShell,
1775 XtNx, x,
1776 XtNy, y,
1777 NULL);
1778}
1779
1780 void
1781gui_mch_set_shellsize(width, height, min_width, min_height,
1782 base_width, base_height)
1783 int width;
1784 int height;
1785 int min_width;
1786 int min_height;
1787 int base_width;
1788 int base_height;
1789{
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00001790#ifdef FEAT_XIM
1791 height += xim_get_status_area_height(),
1792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 XtVaSetValues(vimShell,
1794 XtNwidthInc, gui.char_width,
1795 XtNheightInc, gui.char_height,
1796#if defined(XtSpecificationRelease) && XtSpecificationRelease >= 4
1797 XtNbaseWidth, base_width,
1798 XtNbaseHeight, base_height,
1799#endif
1800 XtNminWidth, min_width,
1801 XtNminHeight, min_height,
1802 XtNwidth, width,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 XtNheight, height,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 NULL);
1805}
1806
1807/*
1808 * Allow 10 pixels for horizontal borders, 30 for vertical borders.
1809 * Is there no way in X to find out how wide the borders really are?
1810 */
1811 void
1812gui_mch_get_screen_dimensions(screen_w, screen_h)
1813 int *screen_w;
1814 int *screen_h;
1815{
1816 *screen_w = DisplayWidth(gui.dpy, DefaultScreen(gui.dpy)) - 10;
1817 *screen_h = DisplayHeight(gui.dpy, DefaultScreen(gui.dpy)) - p_ghr;
1818}
1819
1820/*
1821 * Initialise vim to use the font "font_name". If it's NULL, pick a default
1822 * font.
1823 * If "fontset" is TRUE, load the "font_name" as a fontset.
1824 * Return FAIL if the font could not be loaded, OK otherwise.
1825 */
1826/*ARGSUSED*/
1827 int
1828gui_mch_init_font(font_name, do_fontset)
1829 char_u *font_name;
1830 int do_fontset;
1831{
1832 XFontStruct *font = NULL;
1833
1834#ifdef FEAT_XFONTSET
1835 XFontSet fontset = NULL;
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001838#ifdef FEAT_GUI_MOTIF
1839 /* A font name equal "*" is indicating, that we should activate the font
1840 * selection dialogue to get a new font name. So let us do it here. */
1841 if (font_name != NULL && STRCMP(font_name, "*") == 0)
1842 font_name = gui_xm_select_font(hl_get_font_name());
1843#endif
1844
1845#ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 if (do_fontset)
1847 {
1848 /* If 'guifontset' is set, VIM treats all font specifications as if
1849 * they were fontsets, and 'guifontset' becomes the default. */
1850 if (font_name != NULL)
1851 {
1852 fontset = (XFontSet)gui_mch_get_fontset(font_name, FALSE, TRUE);
1853 if (fontset == NULL)
1854 return FAIL;
1855 }
1856 }
1857 else
1858#endif
1859 {
1860 if (font_name == NULL)
1861 {
1862 /*
1863 * If none of the fonts in 'font' could be loaded, try the one set
1864 * in the X resource, and finally just try using DFLT_FONT, which
1865 * will hopefully always be there.
1866 */
1867 font_name = gui.rsrc_font_name;
1868 font = (XFontStruct *)gui_mch_get_font(font_name, FALSE);
1869 if (font == NULL)
1870 font_name = (char_u *)DFLT_FONT;
1871 }
1872 if (font == NULL)
1873 font = (XFontStruct *)gui_mch_get_font(font_name, FALSE);
1874 if (font == NULL)
1875 return FAIL;
1876 }
1877
1878 gui_mch_free_font(gui.norm_font);
1879#ifdef FEAT_XFONTSET
1880 gui_mch_free_fontset(gui.fontset);
1881
1882 if (fontset != NULL)
1883 {
1884 gui.norm_font = NOFONT;
1885 gui.fontset = (GuiFontset)fontset;
1886 gui.char_width = fontset_width(fontset);
1887 gui.char_height = fontset_height(fontset) + p_linespace;
1888 gui.char_ascent = fontset_ascent(fontset) + p_linespace / 2;
1889 }
1890 else
1891#endif
1892 {
1893 gui.norm_font = (GuiFont)font;
1894#ifdef FEAT_XFONTSET
1895 gui.fontset = NOFONTSET;
1896#endif
1897 gui.char_width = font->max_bounds.width;
1898 gui.char_height = font->ascent + font->descent + p_linespace;
1899 gui.char_ascent = font->ascent + p_linespace / 2;
1900 }
1901
1902 hl_set_font_name(font_name);
1903
1904 /*
1905 * Try to load other fonts for bold, italic, and bold-italic.
1906 * We should also try to work out what font to use for these when they are
1907 * not specified by X resources, but we don't yet.
1908 */
1909 if (font_name == gui.rsrc_font_name)
1910 {
1911 if (gui.bold_font == NOFONT
1912 && gui.rsrc_bold_font_name != NULL
1913 && *gui.rsrc_bold_font_name != NUL)
1914 gui.bold_font = gui_mch_get_font(gui.rsrc_bold_font_name, FALSE);
1915 if (gui.ital_font == NOFONT
1916 && gui.rsrc_ital_font_name != NULL
1917 && *gui.rsrc_ital_font_name != NUL)
1918 gui.ital_font = gui_mch_get_font(gui.rsrc_ital_font_name, FALSE);
1919 if (gui.boldital_font == NOFONT
1920 && gui.rsrc_boldital_font_name != NULL
1921 && *gui.rsrc_boldital_font_name != NUL)
1922 gui.boldital_font = gui_mch_get_font(gui.rsrc_boldital_font_name,
1923 FALSE);
1924 }
1925 else
1926 {
1927 /* When not using the font specified by the resources, also don't use
1928 * the bold/italic fonts, otherwise setting 'guifont' will look very
1929 * strange. */
1930 if (gui.bold_font != NOFONT)
1931 {
1932 XFreeFont(gui.dpy, (XFontStruct *)gui.bold_font);
1933 gui.bold_font = NOFONT;
1934 }
1935 if (gui.ital_font != NOFONT)
1936 {
1937 XFreeFont(gui.dpy, (XFontStruct *)gui.ital_font);
1938 gui.ital_font = NOFONT;
1939 }
1940 if (gui.boldital_font != NOFONT)
1941 {
1942 XFreeFont(gui.dpy, (XFontStruct *)gui.boldital_font);
1943 gui.boldital_font = NOFONT;
1944 }
1945 }
1946
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001947#ifdef FEAT_GUI_MOTIF
1948 gui_motif_synch_fonts();
1949#endif
1950
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 return OK;
1952}
1953
1954/*
1955 * Get a font structure for highlighting.
1956 */
1957 GuiFont
1958gui_mch_get_font(name, giveErrorIfMissing)
1959 char_u *name;
1960 int giveErrorIfMissing;
1961{
1962 XFontStruct *font;
1963
1964 if (!gui.in_use || name == NULL) /* can't do this when GUI not running */
1965 return NOFONT;
1966
1967 font = XLoadQueryFont(gui.dpy, (char *)name);
1968
1969 if (font == NULL)
1970 {
1971 if (giveErrorIfMissing)
1972 EMSG2(_(e_font), name);
1973 return NOFONT;
1974 }
1975
1976#ifdef DEBUG
1977 printf("Font Information for '%s':\n", name);
1978 printf(" w = %d, h = %d, ascent = %d, descent = %d\n",
1979 font->max_bounds.width, font->ascent + font->descent,
1980 font->ascent, font->descent);
1981 printf(" max ascent = %d, max descent = %d, max h = %d\n",
1982 font->max_bounds.ascent, font->max_bounds.descent,
1983 font->max_bounds.ascent + font->max_bounds.descent);
1984 printf(" min lbearing = %d, min rbearing = %d\n",
1985 font->min_bounds.lbearing, font->min_bounds.rbearing);
1986 printf(" max lbearing = %d, max rbearing = %d\n",
1987 font->max_bounds.lbearing, font->max_bounds.rbearing);
1988 printf(" leftink = %d, rightink = %d\n",
1989 (font->min_bounds.lbearing < 0),
1990 (font->max_bounds.rbearing > font->max_bounds.width));
1991 printf("\n");
1992#endif
1993
1994 if (font->max_bounds.width != font->min_bounds.width)
1995 {
1996 EMSG2(_(e_fontwidth), name);
1997 XFreeFont(gui.dpy, font);
1998 return NOFONT;
1999 }
2000 return (GuiFont)font;
2001}
2002
Bram Moolenaar567e4de2004-12-31 21:01:02 +00002003#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002004/*
2005 * Return the name of font "font" in allocated memory.
2006 * Don't know how to get the actual name, thus use the provided name.
2007 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002008/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002009 char_u *
2010gui_mch_get_fontname(font, name)
2011 GuiFont font;
2012 char_u *name;
2013{
2014 if (name == NULL)
2015 return NULL;
2016 return vim_strsave(name);
2017}
Bram Moolenaar567e4de2004-12-31 21:01:02 +00002018#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002019
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 int
2021gui_mch_adjust_charsize()
2022{
2023#ifdef FEAT_XFONTSET
2024 if (gui.fontset != NOFONTSET)
2025 {
2026 gui.char_height = fontset_height((XFontSet)gui.fontset) + p_linespace;
2027 gui.char_ascent = fontset_ascent((XFontSet)gui.fontset)
2028 + p_linespace / 2;
2029 }
2030 else
2031#endif
2032 {
2033 XFontStruct *font = (XFontStruct *)gui.norm_font;
2034
2035 gui.char_height = font->ascent + font->descent + p_linespace;
2036 gui.char_ascent = font->ascent + p_linespace / 2;
2037 }
2038 return OK;
2039}
2040
2041/*
2042 * Set the current text font.
2043 */
2044 void
2045gui_mch_set_font(font)
2046 GuiFont font;
2047{
2048 static Font prev_font = (Font)-1;
2049 Font fid = ((XFontStruct *)font)->fid;
2050
2051 if (fid != prev_font)
2052 {
2053 XSetFont(gui.dpy, gui.text_gc, fid);
2054 XSetFont(gui.dpy, gui.back_gc, fid);
2055 prev_font = fid;
2056 gui.char_ascent = ((XFontStruct *)font)->ascent + p_linespace / 2;
2057 }
2058#ifdef FEAT_XFONTSET
2059 current_fontset = (XFontSet)NULL;
2060#endif
2061}
2062
2063#if defined(FEAT_XFONTSET) || defined(PROTO)
2064/*
2065 * Set the current text fontset.
2066 * Adjust the ascent, in case it's different.
2067 */
2068 void
2069gui_mch_set_fontset(fontset)
2070 GuiFontset fontset;
2071{
2072 current_fontset = (XFontSet)fontset;
2073 gui.char_ascent = fontset_ascent(current_fontset) + p_linespace / 2;
2074}
2075#endif
2076
2077#if 0 /* not used */
2078/*
2079 * Return TRUE if the two fonts given are equivalent.
2080 */
2081 int
2082gui_mch_same_font(f1, f2)
2083 GuiFont f1;
2084 GuiFont f2;
2085{
2086#ifdef FEAT_XFONTSET
2087 if (gui.fontset != NULL)
2088 return f1 == f2;
2089 else
2090#endif
2091 return ((XFontStruct *)f1)->fid == ((XFontStruct *)f2)->fid;
2092}
2093#endif
2094
2095/*
2096 * If a font is not going to be used, free its structure.
2097 */
2098 void
2099gui_mch_free_font(font)
2100 GuiFont font;
2101{
2102 if (font != NOFONT)
2103 XFreeFont(gui.dpy, (XFontStruct *)font);
2104}
2105
2106#if defined(FEAT_XFONTSET) || defined(PROTO)
2107/*
2108 * If a fontset is not going to be used, free its structure.
2109 */
2110 void
2111gui_mch_free_fontset(fontset)
2112 GuiFontset fontset;
2113{
2114 if (fontset != NOFONTSET)
2115 XFreeFontSet(gui.dpy, (XFontSet)fontset);
2116}
2117
2118/*
2119 * Load the fontset "name".
2120 * Return a reference to the fontset, or NOFONTSET when failing.
2121 */
2122 GuiFontset
2123gui_mch_get_fontset(name, giveErrorIfMissing, fixed_width)
2124 char_u *name;
2125 int giveErrorIfMissing;
2126 int fixed_width;
2127{
2128 XFontSet fontset;
2129 char **missing, *def_str;
2130 int num_missing;
2131
2132 if (!gui.in_use || name == NULL)
2133 return NOFONTSET;
2134
2135 fontset = XCreateFontSet(gui.dpy, (char *)name, &missing, &num_missing,
2136 &def_str);
2137 if (num_missing > 0)
2138 {
2139 int i;
2140
2141 if (giveErrorIfMissing)
2142 {
2143 EMSG2(_("E250: Fonts for the following charsets are missing in fontset %s:"), name);
2144 for (i = 0; i < num_missing; i++)
2145 EMSG2("%s", missing[i]);
2146 }
2147 XFreeStringList(missing);
2148 }
2149
2150 if (fontset == NULL)
2151 {
2152 if (giveErrorIfMissing)
2153 EMSG2(_(e_fontset), name);
2154 return NOFONTSET;
2155 }
2156
2157 if (fixed_width && check_fontset_sanity(fontset) == FAIL)
2158 {
2159 XFreeFontSet(gui.dpy, fontset);
2160 return NOFONTSET;
2161 }
2162 return (GuiFontset)fontset;
2163}
2164
2165/*
2166 * Check if fontset "fs" is fixed width.
2167 */
2168 static int
2169check_fontset_sanity(fs)
2170 XFontSet fs;
2171{
2172 XFontStruct **xfs;
2173 char **font_name;
2174 int fn;
2175 char *base_name;
2176 int i;
2177 int min_width;
2178 int min_font_idx = 0;
2179
2180 base_name = XBaseFontNameListOfFontSet(fs);
2181 fn = XFontsOfFontSet(fs, &xfs, &font_name);
2182 for (i = 0; i < fn; i++)
2183 {
2184 if (xfs[i]->max_bounds.width != xfs[i]->min_bounds.width)
2185 {
2186 EMSG2(_("E252: Fontset name: %s"), base_name);
2187 EMSG2(_("Font '%s' is not fixed-width"), font_name[i]);
2188 return FAIL;
2189 }
2190 }
2191 /* scan base font width */
2192 min_width = 32767;
2193 for (i = 0; i < fn; i++)
2194 {
2195 if (xfs[i]->max_bounds.width<min_width)
2196 {
2197 min_width = xfs[i]->max_bounds.width;
2198 min_font_idx = i;
2199 }
2200 }
2201 for (i = 0; i < fn; i++)
2202 {
2203 if ( xfs[i]->max_bounds.width != 2 * min_width
2204 && xfs[i]->max_bounds.width != min_width)
2205 {
2206 EMSG2(_("E253: Fontset name: %s\n"), base_name);
2207 EMSG2(_("Font0: %s\n"), font_name[min_font_idx]);
2208 EMSG2(_("Font1: %s\n"), font_name[i]);
2209 EMSGN(_("Font%ld width is not twice that of font0\n"), i);
2210 EMSGN(_("Font0 width: %ld\n"), xfs[min_font_idx]->max_bounds.width);
2211 EMSGN(_("Font1 width: %ld\n\n"), xfs[i]->max_bounds.width);
2212 return FAIL;
2213 }
2214 }
2215 /* it seems ok. Good Luck!! */
2216 return OK;
2217}
2218
2219 static int
2220fontset_width(fs)
2221 XFontSet fs;
2222{
2223 return XmbTextEscapement(fs, "Vim", 3) / 3;
2224}
2225
2226 int
2227fontset_height(fs)
2228 XFontSet fs;
2229{
2230 XFontSetExtents *extents;
2231
2232 extents = XExtentsOfFontSet(fs);
2233 return extents->max_logical_extent.height;
2234}
2235
2236#if (defined(FONTSET_ALWAYS) && defined(FEAT_GUI_ATHENA) \
2237 && defined(FEAT_MENU)) || defined(PROTO)
2238/*
2239 * Returns the bounding box height around the actual glyph image of all
2240 * characters in all fonts of the fontset.
2241 */
2242 int
2243fontset_height2(fs)
2244 XFontSet fs;
2245{
2246 XFontSetExtents *extents;
2247
2248 extents = XExtentsOfFontSet(fs);
2249 return extents->max_ink_extent.height;
2250}
2251#endif
2252
2253/* NOT USED YET
2254 static int
2255fontset_descent(fs)
2256 XFontSet fs;
2257{
2258 XFontSetExtents *extents;
2259
2260 extents = XExtentsOfFontSet (fs);
2261 return extents->max_logical_extent.height + extents->max_logical_extent.y;
2262}
2263*/
2264
2265 static int
2266fontset_ascent(fs)
2267 XFontSet fs;
2268{
2269 XFontSetExtents *extents;
2270
2271 extents = XExtentsOfFontSet(fs);
2272 return -extents->max_logical_extent.y;
2273}
2274
2275#endif /* FEAT_XFONTSET */
2276
2277/*
2278 * Return the Pixel value (color) for the given color name.
2279 * Return INVALCOLOR for error.
2280 */
2281 guicolor_T
2282gui_mch_get_color(reqname)
2283 char_u *reqname;
2284{
2285 int i;
2286 char_u *name = reqname;
2287 Colormap colormap;
2288 XColor color;
2289 static char *(vimnames[][2]) =
2290 {
2291 /* A number of colors that some X11 systems don't have */
2292 {"LightRed", "#FFBBBB"},
2293 {"LightGreen", "#88FF88"},
2294 {"LightMagenta","#FFBBFF"},
2295 {"DarkCyan", "#008888"},
2296 {"DarkBlue", "#0000BB"},
2297 {"DarkRed", "#BB0000"},
2298 {"DarkMagenta", "#BB00BB"},
2299 {"DarkGrey", "#BBBBBB"},
2300 {"DarkYellow", "#BBBB00"},
2301 {NULL, NULL}
2302 };
2303
2304 /* can't do this when GUI not running */
2305 if (!gui.in_use || *reqname == NUL)
2306 return INVALCOLOR;
2307
2308 colormap = DefaultColormap(gui.dpy, XDefaultScreen(gui.dpy));
2309
2310 /* Do this twice if the name isn't recognized. */
2311 while (name != NULL)
2312 {
2313 i = XParseColor(gui.dpy, colormap, (char *)name, &color);
2314
2315#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2316 if (i == 0)
2317 {
2318 char *old;
2319
2320 /* The X11 system is trying to resolve named colors only by names
2321 * corresponding to the current locale language. But Vim scripts
2322 * usually contain the English color names. Therefore we have to
2323 * try a second time here with the native "C" locale set.
2324 * Hopefully, restoring the old locale this way works on all
2325 * systems...
2326 */
2327 old = setlocale(LC_ALL, NULL);
2328 if (old != NULL && STRCMP(old, "C") != 0)
2329 {
2330 old = (char *)vim_strsave((char_u *)old);
2331 setlocale(LC_ALL, "C");
2332 i = XParseColor(gui.dpy, colormap, (char *)name, &color);
2333 setlocale(LC_ALL, old);
2334 vim_free(old);
2335 }
2336 }
2337#endif
2338 if (i != 0 && (XAllocColor(gui.dpy, colormap, &color) != 0
2339 || find_closest_color(colormap, &color) == OK))
2340 return (guicolor_T)color.pixel;
2341
2342 /* check for a few builtin names */
2343 for (i = 0; ; ++i)
2344 {
2345 if (vimnames[i][0] == NULL)
2346 {
2347 name = NULL;
2348 break;
2349 }
2350 if (STRICMP(name, vimnames[i][0]) == 0)
2351 {
2352 name = (char_u *)vimnames[i][1];
2353 break;
2354 }
2355 }
2356 }
2357
2358 return INVALCOLOR;
2359}
2360
2361/*
2362 * Find closest color for "colorPtr" in "colormap". set "colorPtr" to the
2363 * resulting color.
2364 * Based on a similar function in TCL.
2365 * Return FAIL if not able to find or allocate a color.
2366 */
2367 static int
2368find_closest_color(colormap, colorPtr)
2369 Colormap colormap;
2370 XColor *colorPtr;
2371{
2372 double tmp, distance, closestDistance;
2373 int i, closest, numFound, cmap_size;
2374 XColor *colortable;
2375 XVisualInfo template, *visInfoPtr;
2376
2377 template.visualid = XVisualIDFromVisual(DefaultVisual(gui.dpy,
2378 XDefaultScreen(gui.dpy)));
2379 visInfoPtr = XGetVisualInfo(gui.dpy, (long)VisualIDMask,
2380 &template, &numFound);
2381 if (numFound < 1)
2382 /* FindClosestColor couldn't lookup visual */
2383 return FAIL;
2384
2385 cmap_size = visInfoPtr->colormap_size;
2386 XFree((char *)visInfoPtr);
2387 colortable = (XColor *)alloc((unsigned)(cmap_size * sizeof(XColor)));
2388 if (!colortable)
2389 return FAIL; /* out of memory */
2390
2391 for (i = 0; i < cmap_size; i++)
2392 colortable[i].pixel = (unsigned long)i;
2393 XQueryColors (gui.dpy, colormap, colortable, cmap_size);
2394
2395 /*
2396 * Find the color that best approximates the desired one, then
2397 * try to allocate that color. If that fails, it must mean that
2398 * the color was read-write (so we can't use it, since it's owner
2399 * might change it) or else it was already freed. Try again,
2400 * over and over again, until something succeeds.
2401 */
2402 closestDistance = 1e30;
2403 closest = 0;
2404 for (i = 0; i < cmap_size; i++)
2405 {
2406 /*
2407 * Use Euclidean distance in RGB space, weighted by Y (of YIQ)
2408 * as the objective function; this accounts for differences
2409 * in the color sensitivity of the eye.
2410 */
2411 tmp = .30 * (((int)colorPtr->red) - (int)colortable[i].red);
2412 distance = tmp * tmp;
2413 tmp = .61 * (((int)colorPtr->green) - (int)colortable[i].green);
2414 distance += tmp * tmp;
2415 tmp = .11 * (((int)colorPtr->blue) - (int)colortable[i].blue);
2416 distance += tmp * tmp;
2417 if (distance < closestDistance)
2418 {
2419 closest = i;
2420 closestDistance = distance;
2421 }
2422 }
2423
2424 if (XAllocColor(gui.dpy, colormap, &colortable[closest]) != 0)
2425 {
2426 gui.color_approx = TRUE;
2427 *colorPtr = colortable[closest];
2428 }
2429
2430 free(colortable);
2431 return OK;
2432}
2433
2434 void
2435gui_mch_set_fg_color(color)
2436 guicolor_T color;
2437{
2438 if (color != prev_fg_color)
2439 {
2440 XSetForeground(gui.dpy, gui.text_gc, (Pixel)color);
2441 prev_fg_color = color;
2442 }
2443}
2444
2445/*
2446 * Set the current text background color.
2447 */
2448 void
2449gui_mch_set_bg_color(color)
2450 guicolor_T color;
2451{
2452 if (color != prev_bg_color)
2453 {
2454 XSetBackground(gui.dpy, gui.text_gc, (Pixel)color);
2455 prev_bg_color = color;
2456 }
2457}
2458
2459/*
2460 * create a mouse pointer that is blank
2461 */
2462 static Cursor
2463gui_x11_create_blank_mouse()
2464{
2465 Pixmap blank_pixmap = XCreatePixmap(gui.dpy, gui.wid, 1, 1, 1);
2466 GC gc = XCreateGC(gui.dpy, blank_pixmap, (unsigned long)0, (XGCValues*)0);
2467 XDrawPoint(gui.dpy, blank_pixmap, gc, 0, 0);
2468 XFreeGC(gui.dpy, gc);
2469 return XCreatePixmapCursor(gui.dpy, blank_pixmap, blank_pixmap,
2470 (XColor*)&gui.norm_pixel, (XColor*)&gui.norm_pixel, 0, 0);
2471}
2472
2473 void
2474gui_mch_draw_string(row, col, s, len, flags)
2475 int row;
2476 int col;
2477 char_u *s;
2478 int len;
2479 int flags;
2480{
2481 int cells = len;
2482#ifdef FEAT_MBYTE
2483 static XChar2b *buf = NULL;
2484 static int buflen = 0;
2485 char_u *p;
2486 int wlen = 0;
2487 int c;
2488
2489 if (enc_utf8)
2490 {
2491 /* Convert UTF-8 byte sequence to 16 bit characters for the X
2492 * functions. Need a buffer for the 16 bit characters. Keep it
2493 * between calls, because allocating it each time is slow. */
2494 if (buflen < len)
2495 {
2496 XtFree((char *)buf);
2497 buf = (XChar2b *)XtMalloc(len * sizeof(XChar2b));
2498 buflen = len;
2499 }
2500 p = s;
2501 cells = 0;
2502 while (p < s + len)
2503 {
2504 c = utf_ptr2char(p);
2505 if (c >= 0x10000) /* show chars > 0xffff as ? */
2506 c = 0xbf;
2507 buf[wlen].byte1 = (unsigned)c >> 8;
2508 buf[wlen].byte2 = c;
2509 ++wlen;
2510 cells += utf_char2cells(c);
2511 p += utf_ptr2len_check(p);
2512 }
2513 }
2514 else if (has_mbyte)
2515 {
2516 cells = 0;
2517 for (p = s; p < s + len; )
2518 {
2519 cells += ptr2cells(p);
2520 p += (*mb_ptr2len_check)(p);
2521 }
2522 }
2523
2524#endif
2525
2526#ifdef FEAT_XFONTSET
2527 if (current_fontset != NULL)
2528 {
2529 /* Setup a clip rectangle to avoid spilling over in the next or
2530 * previous line. This is apparently needed for some fonts which are
2531 * used in a fontset. */
2532 XRectangle clip;
2533
2534 clip.x = 0;
2535 clip.y = 0;
2536 clip.height = gui.char_height;
2537 clip.width = gui.char_width * cells + 1;
2538 XSetClipRectangles(gui.dpy, gui.text_gc, FILL_X(col), FILL_Y(row),
2539 &clip, 1, Unsorted);
2540 }
2541#endif
2542
2543 if (flags & DRAW_TRANSP)
2544 {
2545#ifdef FEAT_MBYTE
2546 if (enc_utf8)
2547 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2548 TEXT_Y(row), buf, wlen);
2549 else
2550#endif
2551 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2552 TEXT_Y(row), (char *)s, len);
2553 }
2554 else if (p_linespace != 0
2555#ifdef FEAT_XFONTSET
2556 || current_fontset != NULL
2557#endif
2558 )
2559 {
2560 XSetForeground(gui.dpy, gui.text_gc, prev_bg_color);
2561 XFillRectangle(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
2562 FILL_Y(row), gui.char_width * cells, gui.char_height);
2563 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
2564#ifdef FEAT_MBYTE
2565 if (enc_utf8)
2566 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2567 TEXT_Y(row), buf, wlen);
2568 else
2569#endif
2570 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2571 TEXT_Y(row), (char *)s, len);
2572 }
2573 else
2574 {
2575 /* XmbDrawImageString has bug, don't use it for fontset. */
2576#ifdef FEAT_MBYTE
2577 if (enc_utf8)
2578 XDrawImageString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2579 TEXT_Y(row), buf, wlen);
2580 else
2581#endif
2582 XDrawImageString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2583 TEXT_Y(row), (char *)s, len);
2584 }
2585
2586 /* Bold trick: draw the text again with a one-pixel offset. */
2587 if (flags & DRAW_BOLD)
2588 {
2589#ifdef FEAT_MBYTE
2590 if (enc_utf8)
2591 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col) + 1,
2592 TEXT_Y(row), buf, wlen);
2593 else
2594#endif
2595 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col) + 1,
2596 TEXT_Y(row), (char *)s, len);
2597 }
2598
2599 /* Underline: draw a line at the bottom of the character cell. */
2600 if (flags & DRAW_UNDERL)
2601 XDrawLine(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
2602 FILL_Y(row + 1) - 1, FILL_X(col + cells) - 1, FILL_Y(row + 1) - 1);
2603
2604#ifdef FEAT_XFONTSET
2605 if (current_fontset != NULL)
2606 XSetClipMask(gui.dpy, gui.text_gc, None);
2607#endif
2608}
2609
2610/*
2611 * Return OK if the key with the termcap name "name" is supported.
2612 */
2613 int
2614gui_mch_haskey(name)
2615 char_u *name;
2616{
2617 int i;
2618
2619 for (i = 0; special_keys[i].key_sym != (KeySym)0; i++)
2620 if (name[0] == special_keys[i].vim_code0 &&
2621 name[1] == special_keys[i].vim_code1)
2622 return OK;
2623 return FAIL;
2624}
2625
2626/*
2627 * Return the text window-id and display. Only required for X-based GUI's
2628 */
2629 int
2630gui_get_x11_windis(win, dis)
2631 Window *win;
2632 Display **dis;
2633{
2634 *win = XtWindow(vimShell);
2635 *dis = gui.dpy;
2636 return OK;
2637}
2638
2639 void
2640gui_mch_beep()
2641{
2642 XBell(gui.dpy, 0);
2643}
2644
2645 void
2646gui_mch_flash(msec)
2647 int msec;
2648{
2649 /* Do a visual beep by reversing the foreground and background colors */
2650 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc, 0, 0,
2651 FILL_X((int)Columns) + gui.border_offset,
2652 FILL_Y((int)Rows) + gui.border_offset);
2653 XSync(gui.dpy, False);
2654 ui_delay((long)msec, TRUE); /* wait for a few msec */
2655 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc, 0, 0,
2656 FILL_X((int)Columns) + gui.border_offset,
2657 FILL_Y((int)Rows) + gui.border_offset);
2658}
2659
2660/*
2661 * Invert a rectangle from row r, column c, for nr rows and nc columns.
2662 */
2663 void
2664gui_mch_invert_rectangle(r, c, nr, nc)
2665 int r;
2666 int c;
2667 int nr;
2668 int nc;
2669{
2670 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc,
2671 FILL_X(c), FILL_Y(r), (nc) * gui.char_width, (nr) * gui.char_height);
2672}
2673
2674/*
2675 * Iconify the GUI window.
2676 */
2677 void
2678gui_mch_iconify()
2679{
2680 XIconifyWindow(gui.dpy, XtWindow(vimShell), DefaultScreen(gui.dpy));
2681}
2682
2683#if defined(FEAT_EVAL) || defined(PROTO)
2684/*
2685 * Bring the Vim window to the foreground.
2686 */
2687 void
2688gui_mch_set_foreground()
2689{
2690 XMapRaised(gui.dpy, XtWindow(vimShell));
2691}
2692#endif
2693
2694/*
2695 * Draw a cursor without focus.
2696 */
2697 void
2698gui_mch_draw_hollow_cursor(color)
2699 guicolor_T color;
2700{
2701 int w = 1;
2702
2703#ifdef FEAT_MBYTE
2704 if (mb_lefthalve(gui.row, gui.col))
2705 w = 2;
2706#endif
2707 gui_mch_set_fg_color(color);
2708 XDrawRectangle(gui.dpy, gui.wid, gui.text_gc, FILL_X(gui.col),
2709 FILL_Y(gui.row), w * gui.char_width - 1, gui.char_height - 1);
2710}
2711
2712/*
2713 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
2714 * color "color".
2715 */
2716 void
2717gui_mch_draw_part_cursor(w, h, color)
2718 int w;
2719 int h;
2720 guicolor_T color;
2721{
2722 gui_mch_set_fg_color(color);
2723
2724 XFillRectangle(gui.dpy, gui.wid, gui.text_gc,
2725#ifdef FEAT_RIGHTLEFT
2726 /* vertical line should be on the right of current point */
2727 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
2728#endif
2729 FILL_X(gui.col),
2730 FILL_Y(gui.row) + gui.char_height - h,
2731 w, h);
2732}
2733
2734/*
2735 * Catch up with any queued X events. This may put keyboard input into the
2736 * input buffer, call resize call-backs, trigger timers etc. If there is
2737 * nothing in the X event queue (& no timers pending), then we return
2738 * immediately.
2739 */
2740 void
2741gui_mch_update()
2742{
2743 XtInputMask mask, desired;
2744
2745#ifdef ALT_X_INPUT
2746 if (suppress_alternate_input)
2747 desired = (XtIMXEvent | XtIMTimer);
2748 else
2749#endif
2750 desired = (XtIMAll);
2751 while ((mask = XtAppPending(app_context)) && (mask & desired)
2752 && !vim_is_input_buf_full())
2753 XtAppProcessEvent(app_context, desired);
2754}
2755
2756/*
2757 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2758 * from the keyboard.
2759 * wtime == -1 Wait forever.
2760 * wtime == 0 This should never happen.
2761 * wtime > 0 Wait wtime milliseconds for a character.
2762 * Returns OK if a character was found to be available within the given time,
2763 * or FAIL otherwise.
2764 */
2765 int
2766gui_mch_wait_for_chars(wtime)
2767 long wtime;
2768{
2769 int focus;
2770
2771 /*
2772 * Make this static, in case gui_x11_timer_cb is called after leaving
2773 * this function (otherwise a random value on the stack may be changed).
2774 */
2775 static int timed_out;
2776 XtIntervalId timer = (XtIntervalId)0;
2777 XtInputMask desired;
2778#ifdef FEAT_SNIFF
2779 static int sniff_on = 0;
2780 static XtInputId sniff_input_id = 0;
2781#endif
2782
2783 timed_out = FALSE;
2784
2785#ifdef FEAT_SNIFF
2786 if (sniff_on && !want_sniff_request)
2787 {
2788 if (sniff_input_id)
2789 XtRemoveInput(sniff_input_id);
2790 sniff_on = 0;
2791 }
2792 else if (!sniff_on && want_sniff_request)
2793 {
2794 sniff_input_id = XtAppAddInput(app_context, fd_from_sniff,
2795 (XtPointer)XtInputReadMask, gui_x11_sniff_request_cb, 0);
2796 sniff_on = 1;
2797 }
2798#endif
2799
2800 if (wtime > 0)
2801 timer = XtAppAddTimeOut(app_context, (long_u)wtime, gui_x11_timer_cb,
2802 &timed_out);
2803
2804 focus = gui.in_focus;
2805#ifdef ALT_X_INPUT
2806 if (suppress_alternate_input)
2807 desired = (XtIMXEvent | XtIMTimer);
2808 else
2809#endif
2810 desired = (XtIMAll);
2811 while (!timed_out)
2812 {
2813 /* Stop or start blinking when focus changes */
2814 if (gui.in_focus != focus)
2815 {
2816 if (gui.in_focus)
2817 gui_mch_start_blink();
2818 else
2819 gui_mch_stop_blink();
2820 focus = gui.in_focus;
2821 }
2822
2823 /*
2824 * Don't use gui_mch_update() because then we will spin-lock until a
2825 * char arrives, instead we use XtAppProcessEvent() to hang until an
2826 * event arrives. No need to check for input_buf_full because we are
2827 * returning as soon as it contains a single char. Note that
2828 * XtAppNextEvent() may not be used because it will not return after a
2829 * timer event has arrived -- webb
2830 */
2831 XtAppProcessEvent(app_context, desired);
2832
2833 if (input_available())
2834 {
2835 if (timer != (XtIntervalId)0 && !timed_out)
2836 XtRemoveTimeOut(timer);
2837 return OK;
2838 }
2839 }
2840 return FAIL;
2841}
2842
2843/*
2844 * Output routines.
2845 */
2846
2847/* Flush any output to the screen */
2848 void
2849gui_mch_flush()
2850{
2851 XFlush(gui.dpy);
2852}
2853
2854/*
2855 * Clear a rectangular region of the screen from text pos (row1, col1) to
2856 * (row2, col2) inclusive.
2857 */
2858 void
2859gui_mch_clear_block(row1, col1, row2, col2)
2860 int row1;
2861 int col1;
2862 int row2;
2863 int col2;
2864{
2865 int x;
2866
2867 x = FILL_X(col1);
2868
2869 /* Clear one extra pixel at the far right, for when bold characters have
2870 * spilled over to the next column. */
2871 XFillRectangle(gui.dpy, gui.wid, gui.back_gc, x, FILL_Y(row1),
2872 (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1),
2873 (row2 - row1 + 1) * gui.char_height);
2874}
2875
2876 void
2877gui_mch_clear_all()
2878{
2879 XClearArea(gui.dpy, gui.wid, 0, 0, 0, 0, False);
2880}
2881
2882/*
2883 * Delete the given number of lines from the given row, scrolling up any
2884 * text further down within the scroll region.
2885 */
2886 void
2887gui_mch_delete_lines(row, num_lines)
2888 int row;
2889 int num_lines;
2890{
2891 if (gui.visibility == VisibilityFullyObscured)
2892 return; /* Can't see the window */
2893
2894 /* copy one extra pixel at the far right, for when bold has spilled
2895 * over */
2896 XCopyArea(gui.dpy, gui.wid, gui.wid, gui.text_gc,
2897 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
2898 gui.char_width * (gui.scroll_region_right - gui.scroll_region_left + 1)
2899 + (gui.scroll_region_right == Columns - 1),
2900 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1),
2901 FILL_X(gui.scroll_region_left), FILL_Y(row));
2902
2903 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
2904 gui.scroll_region_left,
2905 gui.scroll_region_bot, gui.scroll_region_right);
2906 gui_x11_check_copy_area();
2907}
2908
2909/*
2910 * Insert the given number of lines before the given row, scrolling down any
2911 * following text within the scroll region.
2912 */
2913 void
2914gui_mch_insert_lines(row, num_lines)
2915 int row;
2916 int num_lines;
2917{
2918 if (gui.visibility == VisibilityFullyObscured)
2919 return; /* Can't see the window */
2920
2921 /* copy one extra pixel at the far right, for when bold has spilled
2922 * over */
2923 XCopyArea(gui.dpy, gui.wid, gui.wid, gui.text_gc,
2924 FILL_X(gui.scroll_region_left), FILL_Y(row),
2925 gui.char_width * (gui.scroll_region_right - gui.scroll_region_left + 1)
2926 + (gui.scroll_region_right == Columns - 1),
2927 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1),
2928 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines));
2929
2930 gui_clear_block(row, gui.scroll_region_left,
2931 row + num_lines - 1, gui.scroll_region_right);
2932 gui_x11_check_copy_area();
2933}
2934
2935/*
2936 * Update the region revealed by scrolling up/down.
2937 */
2938 static void
2939gui_x11_check_copy_area()
2940{
2941 XEvent event;
2942 XGraphicsExposeEvent *gevent;
2943
2944 if (gui.visibility != VisibilityPartiallyObscured)
2945 return;
2946
2947 XFlush(gui.dpy);
2948
2949 /* Wait to check whether the scroll worked or not */
2950 for (;;)
2951 {
2952 if (XCheckTypedEvent(gui.dpy, NoExpose, &event))
2953 return; /* The scroll worked. */
2954
2955 if (XCheckTypedEvent(gui.dpy, GraphicsExpose, &event))
2956 {
2957 gevent = (XGraphicsExposeEvent *)&event;
2958 gui_redraw(gevent->x, gevent->y, gevent->width, gevent->height);
2959 if (gevent->count == 0)
2960 return; /* This was the last expose event */
2961 }
2962 XSync(gui.dpy, False);
2963 }
2964}
2965
2966/*
2967 * X Selection stuff, for cutting and pasting text to other windows.
2968 */
2969
2970 void
2971clip_mch_lose_selection(cbd)
2972 VimClipboard *cbd;
2973{
2974 clip_x11_lose_selection(vimShell, cbd);
2975}
2976
2977 int
2978clip_mch_own_selection(cbd)
2979 VimClipboard *cbd;
2980{
2981 return clip_x11_own_selection(vimShell, cbd);
2982}
2983
2984 void
2985clip_mch_request_selection(cbd)
2986 VimClipboard *cbd;
2987{
2988 clip_x11_request_selection(vimShell, gui.dpy, cbd);
2989}
2990
2991 void
2992clip_mch_set_selection(cbd)
2993 VimClipboard *cbd;
2994{
2995 clip_x11_set_selection(cbd);
2996}
2997
2998#if defined(FEAT_MENU) || defined(PROTO)
2999/*
3000 * Menu stuff.
3001 */
3002
3003/*
3004 * Make a menu either grey or not grey.
3005 */
3006 void
3007gui_mch_menu_grey(menu, grey)
3008 vimmenu_T *menu;
3009 int grey;
3010{
3011 if (menu->id != (Widget)0)
3012 {
3013 gui_mch_menu_hidden(menu, False);
3014 if (grey
3015#ifdef FEAT_GUI_MOTIF
3016 || !menu->sensitive
3017#endif
3018 )
3019 XtSetSensitive(menu->id, False);
3020 else
3021 XtSetSensitive(menu->id, True);
3022 }
3023}
3024
3025/*
3026 * Make menu item hidden or not hidden
3027 */
3028 void
3029gui_mch_menu_hidden(menu, hidden)
3030 vimmenu_T *menu;
3031 int hidden;
3032{
3033 if (menu->id != (Widget)0)
3034 {
3035 if (hidden)
3036 XtUnmanageChild(menu->id);
3037 else
3038 XtManageChild(menu->id);
3039 }
3040}
3041
3042/*
3043 * This is called after setting all the menus to grey/hidden or not.
3044 */
3045 void
3046gui_mch_draw_menubar()
3047{
3048 /* Nothing to do in X */
3049}
3050
3051/* ARGSUSED */
3052 void
3053gui_x11_menu_cb(w, client_data, call_data)
3054 Widget w;
3055 XtPointer client_data, call_data;
3056{
3057 gui_menu_cb((vimmenu_T *)client_data);
3058}
3059
3060#endif /* FEAT_MENU */
3061
3062
3063
3064/*
3065 * Function called when window closed. Works like ":qa".
3066 * Should put up a requester!
3067 */
3068/*ARGSUSED*/
3069 static void
3070gui_x11_wm_protocol_handler(w, client_data, event, dum)
3071 Widget w;
3072 XtPointer client_data;
3073 XEvent *event;
3074 Boolean *dum;
3075{
3076 /*
3077 * Only deal with Client messages.
3078 */
3079 if (event->type != ClientMessage)
3080 return;
3081
3082 /*
3083 * The WM_SAVE_YOURSELF event arrives when the window manager wants to
3084 * exit. That can be cancelled though, thus Vim shouldn't exit here.
3085 * Just sync our swap files.
3086 */
3087 if (((XClientMessageEvent *)event)->data.l[0] ==
3088 wm_atoms[SAVE_YOURSELF_IDX])
3089 {
3090 out_flush();
3091 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
3092
3093 /* Set the window's WM_COMMAND property, to let the window manager
3094 * know we are done saving ourselves. We don't want to be restarted,
3095 * thus set argv to NULL. */
3096 XSetCommand(gui.dpy, XtWindow(vimShell), NULL, 0);
3097 return;
3098 }
3099
3100 if (((XClientMessageEvent *)event)->data.l[0] !=
3101 wm_atoms[DELETE_WINDOW_IDX])
3102 return;
3103
3104 gui_shell_closed();
3105}
3106
3107#ifdef FEAT_CLIENTSERVER
3108/*
3109 * Function called when property changed. Check for incoming commands
3110 */
3111/*ARGSUSED*/
3112 static void
3113gui_x11_send_event_handler(w, client_data, event, dum)
3114 Widget w;
3115 XtPointer client_data;
3116 XEvent *event;
3117 Boolean *dum;
3118{
3119 XPropertyEvent *e = (XPropertyEvent *) event;
3120
3121 if (e->type == PropertyNotify && e->window == commWindow
3122 && e->atom == commProperty && e->state == PropertyNewValue)
3123 {
3124 serverEventProc(gui.dpy, event);
3125 }
3126}
3127#endif
3128
3129/*
3130 * Cursor blink functions.
3131 *
3132 * This is a simple state machine:
3133 * BLINK_NONE not blinking at all
3134 * BLINK_OFF blinking, cursor is not shown
3135 * BLINK_ON blinking, cursor is shown
3136 */
3137
3138#define BLINK_NONE 0
3139#define BLINK_OFF 1
3140#define BLINK_ON 2
3141
3142static int blink_state = BLINK_NONE;
3143static long_u blink_waittime = 700;
3144static long_u blink_ontime = 400;
3145static long_u blink_offtime = 250;
3146static XtIntervalId blink_timer = (XtIntervalId)0;
3147
3148 void
3149gui_mch_set_blinking(waittime, on, off)
3150 long waittime, on, off;
3151{
3152 blink_waittime = waittime;
3153 blink_ontime = on;
3154 blink_offtime = off;
3155}
3156
3157/*
3158 * Stop the cursor blinking. Show the cursor if it wasn't shown.
3159 */
3160 void
3161gui_mch_stop_blink()
3162{
3163 if (blink_timer != (XtIntervalId)0)
3164 {
3165 XtRemoveTimeOut(blink_timer);
3166 blink_timer = (XtIntervalId)0;
3167 }
3168 if (blink_state == BLINK_OFF)
3169 gui_update_cursor(TRUE, FALSE);
3170 blink_state = BLINK_NONE;
3171}
3172
3173/*
3174 * Start the cursor blinking. If it was already blinking, this restarts the
3175 * waiting time and shows the cursor.
3176 */
3177 void
3178gui_mch_start_blink()
3179{
3180 if (blink_timer != (XtIntervalId)0)
3181 XtRemoveTimeOut(blink_timer);
3182 /* Only switch blinking on if none of the times is zero */
3183 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
3184 {
3185 blink_timer = XtAppAddTimeOut(app_context, blink_waittime,
3186 gui_x11_blink_cb, NULL);
3187 blink_state = BLINK_ON;
3188 gui_update_cursor(TRUE, FALSE);
3189 }
3190}
3191
3192/* ARGSUSED */
3193 static void
3194gui_x11_blink_cb(timed_out, interval_id)
3195 XtPointer timed_out;
3196 XtIntervalId *interval_id;
3197{
3198 if (blink_state == BLINK_ON)
3199 {
3200 gui_undraw_cursor();
3201 blink_state = BLINK_OFF;
3202 blink_timer = XtAppAddTimeOut(app_context, blink_offtime,
3203 gui_x11_blink_cb, NULL);
3204 }
3205 else
3206 {
3207 gui_update_cursor(TRUE, FALSE);
3208 blink_state = BLINK_ON;
3209 blink_timer = XtAppAddTimeOut(app_context, blink_ontime,
3210 gui_x11_blink_cb, NULL);
3211 }
3212}
3213
3214/*
3215 * Return the RGB value of a pixel as a long.
3216 */
3217 long_u
3218gui_mch_get_rgb(pixel)
3219 guicolor_T pixel;
3220{
3221 XColor xc;
3222 Colormap colormap;
3223
3224 colormap = DefaultColormap(gui.dpy, XDefaultScreen(gui.dpy));
3225 xc.pixel = pixel;
3226 XQueryColor(gui.dpy, colormap, &xc);
3227
3228 return ((xc.red & 0xff00) << 8) + (xc.green & 0xff00)
3229 + ((unsigned)xc.blue >> 8);
3230}
3231
3232/*
3233 * Add the callback functions.
3234 */
3235 void
3236gui_x11_callbacks(textArea, vimForm)
3237 Widget textArea;
3238 Widget vimForm;
3239{
3240 XtAddEventHandler(textArea, VisibilityChangeMask, FALSE,
3241 gui_x11_visibility_cb, (XtPointer)0);
3242
3243 XtAddEventHandler(textArea, ExposureMask, FALSE, gui_x11_expose_cb,
3244 (XtPointer)0);
3245
3246 XtAddEventHandler(vimShell, StructureNotifyMask, FALSE,
3247 gui_x11_resize_window_cb, (XtPointer)0);
3248
3249 XtAddEventHandler(vimShell, FocusChangeMask, FALSE, gui_x11_focus_change_cb,
3250 (XtPointer)0);
3251 /*
3252 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3253 * Only needed for some window managers.
3254 */
3255 if (vim_strchr(p_go, GO_POINTER) != NULL)
3256 {
3257 XtAddEventHandler(vimShell, LeaveWindowMask, FALSE, gui_x11_leave_cb,
3258 (XtPointer)0);
3259 XtAddEventHandler(textArea, LeaveWindowMask, FALSE, gui_x11_leave_cb,
3260 (XtPointer)0);
3261 XtAddEventHandler(textArea, EnterWindowMask, FALSE, gui_x11_enter_cb,
3262 (XtPointer)0);
3263 XtAddEventHandler(vimShell, EnterWindowMask, FALSE, gui_x11_enter_cb,
3264 (XtPointer)0);
3265 }
3266
3267 XtAddEventHandler(vimForm, KeyPressMask, FALSE, gui_x11_key_hit_cb,
3268 (XtPointer)0);
3269 XtAddEventHandler(textArea, KeyPressMask, FALSE, gui_x11_key_hit_cb,
3270 (XtPointer)0);
3271
3272 /* get pointer moved events from scrollbar, needed for 'mousefocus' */
3273 XtAddEventHandler(vimForm, PointerMotionMask,
3274 FALSE, gui_x11_mouse_cb, (XtPointer)1);
3275 XtAddEventHandler(textArea, ButtonPressMask | ButtonReleaseMask |
3276 ButtonMotionMask | PointerMotionMask,
3277 FALSE, gui_x11_mouse_cb, (XtPointer)0);
3278}
3279
3280/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003281 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003283 void
3284gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285{
3286 int rootx, rooty, winx, winy;
3287 Window root, child;
3288 unsigned int mask;
3289
3290 if (gui.wid && XQueryPointer(gui.dpy, gui.wid, &root, &child,
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003291 &rootx, &rooty, &winx, &winy, &mask)) {
3292 *x = winx;
3293 *y = winy;
3294 } else {
3295 *x = -1;
3296 *y = -1;
3297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298}
3299
3300 void
3301gui_mch_setmouse(x, y)
3302 int x;
3303 int y;
3304{
3305 if (gui.wid)
3306 XWarpPointer(gui.dpy, (Window)0, gui.wid, 0, 0, 0, 0, x, y);
3307}
3308
3309#if (defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)) || defined(PROTO)
3310 XButtonPressedEvent *
3311gui_x11_get_last_mouse_event()
3312{
3313 return &last_mouse_event;
3314}
3315#endif
3316
3317#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
3318
3319/* Signs are currently always 2 chars wide. Hopefully the font is big enough
3320 * to provide room for the bitmap! */
3321# define SIGN_WIDTH (gui.char_width * 2)
3322
3323#if 0 /* not used */
3324 void
3325gui_mch_clearsign(row)
3326 int row;
3327{
3328 if (gui.in_use)
3329 XClearArea(gui.dpy, gui.wid, 0, TEXT_Y(row) - gui.char_height,
3330 SIGN_WIDTH, gui.char_height, FALSE);
3331}
3332#endif
3333
3334 void
3335gui_mch_drawsign(row, col, typenr)
3336 int row;
3337 int col;
3338 int typenr;
3339{
3340 XImage *sign;
3341
3342 if (gui.in_use && (sign = (XImage *)sign_get_image(typenr)) != NULL)
3343 {
3344 XClearArea(gui.dpy, gui.wid, TEXT_X(col), TEXT_Y(row) - sign->height,
3345 SIGN_WIDTH, gui.char_height, FALSE);
3346 XPutImage(gui.dpy, gui.wid, gui.text_gc, sign, 0, 0,
3347 TEXT_X(col) + (SIGN_WIDTH - sign->width) / 2,
3348 TEXT_Y(row) - sign->height,
3349 sign->width, sign->height);
3350 }
3351}
3352
3353 void *
3354gui_mch_register_sign(signfile)
3355 char_u *signfile;
3356{
3357 XpmAttributes attrs;
3358 XImage *sign;
3359 int status;
3360
3361 /*
3362 * Setup the color substitution table.
3363 */
3364 sign = NULL;
3365 if (signfile[0] != NUL && signfile[0] != '-')
3366 {
3367 sign = (XImage *)alloc(sizeof(XImage));
3368 if (sign != NULL)
3369 {
3370 XpmColorSymbol color[5] =
3371 {
3372 {"none", NULL, 0},
3373 {"iconColor1", NULL, 0},
3374 {"bottomShadowColor", NULL, 0},
3375 {"topShadowColor", NULL, 0},
3376 {"selectColor", NULL, 0}
3377 };
3378 attrs.valuemask = XpmColorSymbols;
3379 attrs.numsymbols = 2;
3380 attrs.colorsymbols = color;
3381 attrs.colorsymbols[0].pixel = gui.back_pixel;
3382 attrs.colorsymbols[1].pixel = gui.norm_pixel;
3383 status = XpmReadFileToImage(gui.dpy, (char *)signfile,
3384 &sign, NULL, &attrs);
3385
3386 if (status == 0)
3387 {
3388 /* Sign width is fixed at two columns now.
3389 if (sign->width > gui.sign_width)
3390 gui.sign_width = sign->width + 8; */
3391 }
3392 else
3393 {
3394 vim_free(sign);
3395 sign = NULL;
3396 EMSG(_(e_signdata));
3397 }
3398 }
3399 }
3400
3401 return (void *)sign;
3402}
3403
3404 void
3405gui_mch_destroy_sign(sign)
3406 void *sign;
3407{
3408 XFree(((XImage *)sign)->data);
3409 vim_free(sign);
3410}
3411#endif
3412
3413
3414#ifdef FEAT_MOUSESHAPE
3415/* The last set mouse pointer shape is remembered, to be used when it goes
3416 * from hidden to not hidden. */
3417static int last_shape = 0;
3418#endif
3419
3420/*
3421 * Use the blank mouse pointer or not.
3422 */
3423 void
3424gui_mch_mousehide(hide)
3425 int hide; /* TRUE = use blank ptr, FALSE = use parent ptr */
3426{
3427 if (gui.pointer_hidden != hide)
3428 {
3429 gui.pointer_hidden = hide;
3430 if (hide)
3431 XDefineCursor(gui.dpy, gui.wid, gui.blank_pointer);
3432 else
3433#ifdef FEAT_MOUSESHAPE
3434 mch_set_mouse_shape(last_shape);
3435#else
3436 XUndefineCursor(gui.dpy, gui.wid);
3437#endif
3438 }
3439}
3440
3441#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3442
3443/* Table for shape IDs. Keep in sync with the mshape_names[] table in
3444 * misc2.c! */
3445static int mshape_ids[] =
3446{
3447 XC_left_ptr, /* arrow */
3448 0, /* blank */
3449 XC_xterm, /* beam */
3450 XC_sb_v_double_arrow, /* updown */
3451 XC_sizing, /* udsizing */
3452 XC_sb_h_double_arrow, /* leftright */
3453 XC_sizing, /* lrsizing */
3454 XC_watch, /* busy */
3455 XC_X_cursor, /* no */
3456 XC_crosshair, /* crosshair */
3457 XC_hand1, /* hand1 */
3458 XC_hand2, /* hand2 */
3459 XC_pencil, /* pencil */
3460 XC_question_arrow, /* question */
3461 XC_right_ptr, /* right-arrow */
3462 XC_center_ptr, /* up-arrow */
3463 XC_left_ptr /* last one */
3464};
3465
3466 void
3467mch_set_mouse_shape(shape)
3468 int shape;
3469{
3470 int id;
3471
3472 if (!gui.in_use)
3473 return;
3474
3475 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
3476 XDefineCursor(gui.dpy, gui.wid, gui.blank_pointer);
3477 else
3478 {
3479 if (shape >= MSHAPE_NUMBERED)
3480 {
3481 id = shape - MSHAPE_NUMBERED;
3482 if (id >= XC_num_glyphs)
3483 id = XC_left_ptr;
3484 else
3485 id &= ~1; /* they are always even (why?) */
3486 }
3487 else
3488 id = mshape_ids[shape];
3489
3490 XDefineCursor(gui.dpy, gui.wid, XCreateFontCursor(gui.dpy, id));
3491 }
3492 if (shape != MSHAPE_HIDE)
3493 last_shape = shape;
3494}
3495#endif
3496
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497#if (defined(FEAT_TOOLBAR) && defined(FEAT_BEVAL)) || defined(PROTO)
3498/*
3499 * Set the balloon-eval used for the tooltip of a toolbar menu item.
3500 * The check for a non-toolbar item was added, because there is a crash when
3501 * passing a normal menu item here. Can't explain that, but better avoid it.
3502 */
3503 void
3504gui_mch_menu_set_tip(menu)
3505 vimmenu_T *menu;
3506{
3507 if (menu->id != NULL && menu->parent != NULL
3508 && menu_is_toolbar(menu->parent->name))
3509 {
3510 /* Always destroy and create the balloon, in case the string was
3511 * changed. */
3512 if (menu->tip != NULL)
3513 {
3514 gui_mch_destroy_beval_area(menu->tip);
3515 menu->tip = NULL;
3516 }
3517 if (menu->strings[MENU_INDEX_TIP] != NULL)
3518 menu->tip = gui_mch_create_beval_area(
3519 menu->id,
3520 menu->strings[MENU_INDEX_TIP],
3521 NULL,
3522 NULL);
3523 }
3524}
3525#endif