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