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