blob: be93cc5b2c22009a524a132233634d863c1c03ba [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},
Bram Moolenaara5792f52005-11-23 21:25:05 +0000538 {"-iconic", ".iconic", XrmoptionNoArg, "True"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 {"-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 */
Bram Moolenaara5792f52005-11-23 21:25:05 +00001613 XtRealizeWidget(vimShell);
1614 XtManageChild(XtNameToWidget(vimShell, "*vimForm"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
1616 gui.wid = gui_x11_get_wid();
1617 gui.blank_pointer = gui_x11_create_blank_mouse();
1618
1619 /*
1620 * Add a callback for the Close item on the window managers menu, and the
1621 * save-yourself event.
1622 */
1623 wm_atoms[SAVE_YOURSELF_IDX] =
1624 XInternAtom(gui.dpy, "WM_SAVE_YOURSELF", False);
1625 wm_atoms[DELETE_WINDOW_IDX] =
1626 XInternAtom(gui.dpy, "WM_DELETE_WINDOW", False);
1627 XSetWMProtocols(gui.dpy, XtWindow(vimShell), wm_atoms, 2);
1628 XtAddEventHandler(vimShell, NoEventMask, True, gui_x11_wm_protocol_handler,
1629 NULL);
1630#ifdef HAVE_X11_XMU_EDITRES_H
1631 /*
1632 * Enable editres protocol (see "man editres").
1633 * Usually will need to add -lXmu to the linker line as well.
1634 */
1635 XtAddEventHandler(vimShell, (EventMask)0, True, _XEditResCheckMessages,
1636 (XtPointer)NULL);
1637#endif
1638
1639#ifdef FEAT_CLIENTSERVER
1640 if (serverName == NULL && serverDelayedStartName != NULL)
1641 {
1642 /* This is a :gui command in a plain vim with no previous server */
1643 commWindow = XtWindow(vimShell);
1644 (void)serverRegisterName(gui.dpy, serverDelayedStartName);
1645 }
1646 else
1647 {
1648 /*
1649 * Cannot handle "widget-less" windows with XtProcessEvent() we'll
1650 * have to change the "server" registration to that of the main window
1651 * If we have not registered a name yet, remember the window
1652 */
1653 serverChangeRegisteredWindow(gui.dpy, XtWindow(vimShell));
1654 }
1655 XtAddEventHandler(vimShell, PropertyChangeMask, False,
1656 gui_x11_send_event_handler, NULL);
1657#endif
1658
1659
1660#if defined(FEAT_MENU) && defined(FEAT_GUI_ATHENA)
1661 /* The Athena GUI needs this again after opening the window */
1662 gui_position_menu();
1663# ifdef FEAT_TOOLBAR
1664 gui_mch_set_toolbar_pos(0, gui.menu_height, gui.menu_width,
1665 gui.toolbar_height);
1666# endif
1667#endif
1668
1669 /* Get the colors for the highlight groups (gui_check_colors() might have
1670 * changed them) */
1671 highlight_gui_started(); /* re-init colors and fonts */
1672
1673#ifdef FEAT_HANGULIN
1674 hangul_keyboard_set();
1675#endif
1676#ifdef FEAT_XIM
1677 xim_init();
1678#endif
1679#ifdef FEAT_SUN_WORKSHOP
1680 workshop_postinit();
1681#endif
1682
1683 return OK;
1684}
1685
1686#if defined(FEAT_BEVAL) || defined(PROTO)
1687/*
1688 * Convert the tooltip fontset name to an XFontSet.
1689 */
1690 void
1691gui_init_tooltip_font()
1692{
1693 XrmValue from, to;
1694
1695 from.addr = (char *)gui.rsrc_tooltip_font_name;
1696 from.size = strlen(from.addr);
1697 to.addr = (XtPointer)&gui.tooltip_fontset;
1698 to.size = sizeof(XFontSet);
1699
1700 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontSet, &to) == False)
1701 {
1702 /* Failed. What to do? */
1703 }
1704}
1705#endif
1706
1707#if defined(FEAT_MENU) || defined(PROTO)
1708/* Convert the menu font/fontset name to an XFontStruct/XFontset */
1709 void
1710gui_init_menu_font()
1711{
1712 XrmValue from, to;
1713
1714#ifdef FONTSET_ALWAYS
1715 from.addr = (char *)gui.rsrc_menu_font_name;
1716 from.size = strlen(from.addr);
1717 to.addr = (XtPointer)&gui.menu_fontset;
1718 to.size = sizeof(GuiFontset);
1719
1720 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontSet, &to) == False)
1721 {
1722 /* Failed. What to do? */
1723 }
1724#else
1725 from.addr = (char *)gui.rsrc_menu_font_name;
1726 from.size = strlen(from.addr);
1727 to.addr = (XtPointer)&gui.menu_font;
1728 to.size = sizeof(GuiFont);
1729
1730 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontStruct, &to) == False)
1731 {
1732 /* Failed. What to do? */
1733 }
1734#endif
1735}
1736#endif
1737
1738/*ARGSUSED*/
1739 void
1740gui_mch_exit(rc)
1741 int rc;
1742{
1743#if 0
1744 /* Lesstif gives an error message here, and so does Solaris. The man page
1745 * says that this isn't needed when exiting, so just skip it. */
1746 XtCloseDisplay(gui.dpy);
1747#endif
1748}
1749
1750/*
1751 * Get the position of the top left corner of the window.
1752 */
1753 int
1754gui_mch_get_winpos(x, y)
1755 int *x, *y;
1756{
1757 Dimension xpos, ypos;
1758
1759 XtVaGetValues(vimShell,
1760 XtNx, &xpos,
1761 XtNy, &ypos,
1762 NULL);
1763 *x = xpos;
1764 *y = ypos;
1765 return OK;
1766}
1767
1768/*
1769 * Set the position of the top left corner of the window to the given
1770 * coordinates.
1771 */
1772 void
1773gui_mch_set_winpos(x, y)
1774 int x, y;
1775{
1776 XtVaSetValues(vimShell,
1777 XtNx, x,
1778 XtNy, y,
1779 NULL);
1780}
1781
1782 void
1783gui_mch_set_shellsize(width, height, min_width, min_height,
1784 base_width, base_height)
1785 int width;
1786 int height;
1787 int min_width;
1788 int min_height;
1789 int base_width;
1790 int base_height;
1791{
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00001792#ifdef FEAT_XIM
1793 height += xim_get_status_area_height(),
1794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 XtVaSetValues(vimShell,
1796 XtNwidthInc, gui.char_width,
1797 XtNheightInc, gui.char_height,
1798#if defined(XtSpecificationRelease) && XtSpecificationRelease >= 4
1799 XtNbaseWidth, base_width,
1800 XtNbaseHeight, base_height,
1801#endif
1802 XtNminWidth, min_width,
1803 XtNminHeight, min_height,
1804 XtNwidth, width,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 XtNheight, height,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 NULL);
1807}
1808
1809/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00001810 * Allow 10 pixels for horizontal borders, 'guiheadroom' for vertical borders.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 * Is there no way in X to find out how wide the borders really are?
1812 */
1813 void
1814gui_mch_get_screen_dimensions(screen_w, screen_h)
1815 int *screen_w;
1816 int *screen_h;
1817{
1818 *screen_w = DisplayWidth(gui.dpy, DefaultScreen(gui.dpy)) - 10;
1819 *screen_h = DisplayHeight(gui.dpy, DefaultScreen(gui.dpy)) - p_ghr;
1820}
1821
1822/*
1823 * Initialise vim to use the font "font_name". If it's NULL, pick a default
1824 * font.
1825 * If "fontset" is TRUE, load the "font_name" as a fontset.
1826 * Return FAIL if the font could not be loaded, OK otherwise.
1827 */
1828/*ARGSUSED*/
1829 int
1830gui_mch_init_font(font_name, do_fontset)
1831 char_u *font_name;
1832 int do_fontset;
1833{
1834 XFontStruct *font = NULL;
1835
1836#ifdef FEAT_XFONTSET
1837 XFontSet fontset = NULL;
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001840#ifdef FEAT_GUI_MOTIF
1841 /* A font name equal "*" is indicating, that we should activate the font
1842 * selection dialogue to get a new font name. So let us do it here. */
1843 if (font_name != NULL && STRCMP(font_name, "*") == 0)
1844 font_name = gui_xm_select_font(hl_get_font_name());
1845#endif
1846
1847#ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 if (do_fontset)
1849 {
1850 /* If 'guifontset' is set, VIM treats all font specifications as if
1851 * they were fontsets, and 'guifontset' becomes the default. */
1852 if (font_name != NULL)
1853 {
1854 fontset = (XFontSet)gui_mch_get_fontset(font_name, FALSE, TRUE);
1855 if (fontset == NULL)
1856 return FAIL;
1857 }
1858 }
1859 else
1860#endif
1861 {
1862 if (font_name == NULL)
1863 {
1864 /*
1865 * If none of the fonts in 'font' could be loaded, try the one set
1866 * in the X resource, and finally just try using DFLT_FONT, which
1867 * will hopefully always be there.
1868 */
1869 font_name = gui.rsrc_font_name;
1870 font = (XFontStruct *)gui_mch_get_font(font_name, FALSE);
1871 if (font == NULL)
1872 font_name = (char_u *)DFLT_FONT;
1873 }
1874 if (font == NULL)
1875 font = (XFontStruct *)gui_mch_get_font(font_name, FALSE);
1876 if (font == NULL)
1877 return FAIL;
1878 }
1879
1880 gui_mch_free_font(gui.norm_font);
1881#ifdef FEAT_XFONTSET
1882 gui_mch_free_fontset(gui.fontset);
1883
1884 if (fontset != NULL)
1885 {
1886 gui.norm_font = NOFONT;
1887 gui.fontset = (GuiFontset)fontset;
1888 gui.char_width = fontset_width(fontset);
1889 gui.char_height = fontset_height(fontset) + p_linespace;
1890 gui.char_ascent = fontset_ascent(fontset) + p_linespace / 2;
1891 }
1892 else
1893#endif
1894 {
1895 gui.norm_font = (GuiFont)font;
1896#ifdef FEAT_XFONTSET
1897 gui.fontset = NOFONTSET;
1898#endif
1899 gui.char_width = font->max_bounds.width;
1900 gui.char_height = font->ascent + font->descent + p_linespace;
1901 gui.char_ascent = font->ascent + p_linespace / 2;
1902 }
1903
1904 hl_set_font_name(font_name);
1905
1906 /*
1907 * Try to load other fonts for bold, italic, and bold-italic.
1908 * We should also try to work out what font to use for these when they are
1909 * not specified by X resources, but we don't yet.
1910 */
1911 if (font_name == gui.rsrc_font_name)
1912 {
1913 if (gui.bold_font == NOFONT
1914 && gui.rsrc_bold_font_name != NULL
1915 && *gui.rsrc_bold_font_name != NUL)
1916 gui.bold_font = gui_mch_get_font(gui.rsrc_bold_font_name, FALSE);
1917 if (gui.ital_font == NOFONT
1918 && gui.rsrc_ital_font_name != NULL
1919 && *gui.rsrc_ital_font_name != NUL)
1920 gui.ital_font = gui_mch_get_font(gui.rsrc_ital_font_name, FALSE);
1921 if (gui.boldital_font == NOFONT
1922 && gui.rsrc_boldital_font_name != NULL
1923 && *gui.rsrc_boldital_font_name != NUL)
1924 gui.boldital_font = gui_mch_get_font(gui.rsrc_boldital_font_name,
1925 FALSE);
1926 }
1927 else
1928 {
1929 /* When not using the font specified by the resources, also don't use
1930 * the bold/italic fonts, otherwise setting 'guifont' will look very
1931 * strange. */
1932 if (gui.bold_font != NOFONT)
1933 {
1934 XFreeFont(gui.dpy, (XFontStruct *)gui.bold_font);
1935 gui.bold_font = NOFONT;
1936 }
1937 if (gui.ital_font != NOFONT)
1938 {
1939 XFreeFont(gui.dpy, (XFontStruct *)gui.ital_font);
1940 gui.ital_font = NOFONT;
1941 }
1942 if (gui.boldital_font != NOFONT)
1943 {
1944 XFreeFont(gui.dpy, (XFontStruct *)gui.boldital_font);
1945 gui.boldital_font = NOFONT;
1946 }
1947 }
1948
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001949#ifdef FEAT_GUI_MOTIF
1950 gui_motif_synch_fonts();
1951#endif
1952
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 return OK;
1954}
1955
1956/*
1957 * Get a font structure for highlighting.
1958 */
1959 GuiFont
1960gui_mch_get_font(name, giveErrorIfMissing)
1961 char_u *name;
1962 int giveErrorIfMissing;
1963{
1964 XFontStruct *font;
1965
1966 if (!gui.in_use || name == NULL) /* can't do this when GUI not running */
1967 return NOFONT;
1968
1969 font = XLoadQueryFont(gui.dpy, (char *)name);
1970
1971 if (font == NULL)
1972 {
1973 if (giveErrorIfMissing)
1974 EMSG2(_(e_font), name);
1975 return NOFONT;
1976 }
1977
1978#ifdef DEBUG
1979 printf("Font Information for '%s':\n", name);
1980 printf(" w = %d, h = %d, ascent = %d, descent = %d\n",
1981 font->max_bounds.width, font->ascent + font->descent,
1982 font->ascent, font->descent);
1983 printf(" max ascent = %d, max descent = %d, max h = %d\n",
1984 font->max_bounds.ascent, font->max_bounds.descent,
1985 font->max_bounds.ascent + font->max_bounds.descent);
1986 printf(" min lbearing = %d, min rbearing = %d\n",
1987 font->min_bounds.lbearing, font->min_bounds.rbearing);
1988 printf(" max lbearing = %d, max rbearing = %d\n",
1989 font->max_bounds.lbearing, font->max_bounds.rbearing);
1990 printf(" leftink = %d, rightink = %d\n",
1991 (font->min_bounds.lbearing < 0),
1992 (font->max_bounds.rbearing > font->max_bounds.width));
1993 printf("\n");
1994#endif
1995
1996 if (font->max_bounds.width != font->min_bounds.width)
1997 {
1998 EMSG2(_(e_fontwidth), name);
1999 XFreeFont(gui.dpy, font);
2000 return NOFONT;
2001 }
2002 return (GuiFont)font;
2003}
2004
Bram Moolenaar567e4de2004-12-31 21:01:02 +00002005#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002006/*
2007 * Return the name of font "font" in allocated memory.
2008 * Don't know how to get the actual name, thus use the provided name.
2009 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002010/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002011 char_u *
2012gui_mch_get_fontname(font, name)
2013 GuiFont font;
2014 char_u *name;
2015{
2016 if (name == NULL)
2017 return NULL;
2018 return vim_strsave(name);
2019}
Bram Moolenaar567e4de2004-12-31 21:01:02 +00002020#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002021
Bram Moolenaar231334e2005-07-25 20:46:57 +00002022/*
2023 * Adjust gui.char_height (after 'linespace' was changed).
2024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 int
Bram Moolenaar231334e2005-07-25 20:46:57 +00002026gui_mch_adjust_charheight()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027{
2028#ifdef FEAT_XFONTSET
2029 if (gui.fontset != NOFONTSET)
2030 {
2031 gui.char_height = fontset_height((XFontSet)gui.fontset) + p_linespace;
2032 gui.char_ascent = fontset_ascent((XFontSet)gui.fontset)
2033 + p_linespace / 2;
2034 }
2035 else
2036#endif
2037 {
2038 XFontStruct *font = (XFontStruct *)gui.norm_font;
2039
2040 gui.char_height = font->ascent + font->descent + p_linespace;
2041 gui.char_ascent = font->ascent + p_linespace / 2;
2042 }
2043 return OK;
2044}
2045
2046/*
2047 * Set the current text font.
2048 */
2049 void
2050gui_mch_set_font(font)
2051 GuiFont font;
2052{
2053 static Font prev_font = (Font)-1;
2054 Font fid = ((XFontStruct *)font)->fid;
2055
2056 if (fid != prev_font)
2057 {
2058 XSetFont(gui.dpy, gui.text_gc, fid);
2059 XSetFont(gui.dpy, gui.back_gc, fid);
2060 prev_font = fid;
2061 gui.char_ascent = ((XFontStruct *)font)->ascent + p_linespace / 2;
2062 }
2063#ifdef FEAT_XFONTSET
2064 current_fontset = (XFontSet)NULL;
2065#endif
2066}
2067
2068#if defined(FEAT_XFONTSET) || defined(PROTO)
2069/*
2070 * Set the current text fontset.
2071 * Adjust the ascent, in case it's different.
2072 */
2073 void
2074gui_mch_set_fontset(fontset)
2075 GuiFontset fontset;
2076{
2077 current_fontset = (XFontSet)fontset;
2078 gui.char_ascent = fontset_ascent(current_fontset) + p_linespace / 2;
2079}
2080#endif
2081
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082/*
2083 * If a font is not going to be used, free its structure.
2084 */
2085 void
2086gui_mch_free_font(font)
2087 GuiFont font;
2088{
2089 if (font != NOFONT)
2090 XFreeFont(gui.dpy, (XFontStruct *)font);
2091}
2092
2093#if defined(FEAT_XFONTSET) || defined(PROTO)
2094/*
2095 * If a fontset is not going to be used, free its structure.
2096 */
2097 void
2098gui_mch_free_fontset(fontset)
2099 GuiFontset fontset;
2100{
2101 if (fontset != NOFONTSET)
2102 XFreeFontSet(gui.dpy, (XFontSet)fontset);
2103}
2104
2105/*
2106 * Load the fontset "name".
2107 * Return a reference to the fontset, or NOFONTSET when failing.
2108 */
2109 GuiFontset
2110gui_mch_get_fontset(name, giveErrorIfMissing, fixed_width)
2111 char_u *name;
2112 int giveErrorIfMissing;
2113 int fixed_width;
2114{
2115 XFontSet fontset;
2116 char **missing, *def_str;
2117 int num_missing;
2118
2119 if (!gui.in_use || name == NULL)
2120 return NOFONTSET;
2121
2122 fontset = XCreateFontSet(gui.dpy, (char *)name, &missing, &num_missing,
2123 &def_str);
2124 if (num_missing > 0)
2125 {
2126 int i;
2127
2128 if (giveErrorIfMissing)
2129 {
2130 EMSG2(_("E250: Fonts for the following charsets are missing in fontset %s:"), name);
2131 for (i = 0; i < num_missing; i++)
2132 EMSG2("%s", missing[i]);
2133 }
2134 XFreeStringList(missing);
2135 }
2136
2137 if (fontset == NULL)
2138 {
2139 if (giveErrorIfMissing)
2140 EMSG2(_(e_fontset), name);
2141 return NOFONTSET;
2142 }
2143
2144 if (fixed_width && check_fontset_sanity(fontset) == FAIL)
2145 {
2146 XFreeFontSet(gui.dpy, fontset);
2147 return NOFONTSET;
2148 }
2149 return (GuiFontset)fontset;
2150}
2151
2152/*
2153 * Check if fontset "fs" is fixed width.
2154 */
2155 static int
2156check_fontset_sanity(fs)
2157 XFontSet fs;
2158{
2159 XFontStruct **xfs;
2160 char **font_name;
2161 int fn;
2162 char *base_name;
2163 int i;
2164 int min_width;
2165 int min_font_idx = 0;
2166
2167 base_name = XBaseFontNameListOfFontSet(fs);
2168 fn = XFontsOfFontSet(fs, &xfs, &font_name);
2169 for (i = 0; i < fn; i++)
2170 {
2171 if (xfs[i]->max_bounds.width != xfs[i]->min_bounds.width)
2172 {
2173 EMSG2(_("E252: Fontset name: %s"), base_name);
2174 EMSG2(_("Font '%s' is not fixed-width"), font_name[i]);
2175 return FAIL;
2176 }
2177 }
2178 /* scan base font width */
2179 min_width = 32767;
2180 for (i = 0; i < fn; i++)
2181 {
2182 if (xfs[i]->max_bounds.width<min_width)
2183 {
2184 min_width = xfs[i]->max_bounds.width;
2185 min_font_idx = i;
2186 }
2187 }
2188 for (i = 0; i < fn; i++)
2189 {
2190 if ( xfs[i]->max_bounds.width != 2 * min_width
2191 && xfs[i]->max_bounds.width != min_width)
2192 {
2193 EMSG2(_("E253: Fontset name: %s\n"), base_name);
2194 EMSG2(_("Font0: %s\n"), font_name[min_font_idx]);
2195 EMSG2(_("Font1: %s\n"), font_name[i]);
2196 EMSGN(_("Font%ld width is not twice that of font0\n"), i);
2197 EMSGN(_("Font0 width: %ld\n"), xfs[min_font_idx]->max_bounds.width);
2198 EMSGN(_("Font1 width: %ld\n\n"), xfs[i]->max_bounds.width);
2199 return FAIL;
2200 }
2201 }
2202 /* it seems ok. Good Luck!! */
2203 return OK;
2204}
2205
2206 static int
2207fontset_width(fs)
2208 XFontSet fs;
2209{
2210 return XmbTextEscapement(fs, "Vim", 3) / 3;
2211}
2212
2213 int
2214fontset_height(fs)
2215 XFontSet fs;
2216{
2217 XFontSetExtents *extents;
2218
2219 extents = XExtentsOfFontSet(fs);
2220 return extents->max_logical_extent.height;
2221}
2222
2223#if (defined(FONTSET_ALWAYS) && defined(FEAT_GUI_ATHENA) \
2224 && defined(FEAT_MENU)) || defined(PROTO)
2225/*
2226 * Returns the bounding box height around the actual glyph image of all
2227 * characters in all fonts of the fontset.
2228 */
2229 int
2230fontset_height2(fs)
2231 XFontSet fs;
2232{
2233 XFontSetExtents *extents;
2234
2235 extents = XExtentsOfFontSet(fs);
2236 return extents->max_ink_extent.height;
2237}
2238#endif
2239
2240/* NOT USED YET
2241 static int
2242fontset_descent(fs)
2243 XFontSet fs;
2244{
2245 XFontSetExtents *extents;
2246
2247 extents = XExtentsOfFontSet (fs);
2248 return extents->max_logical_extent.height + extents->max_logical_extent.y;
2249}
2250*/
2251
2252 static int
2253fontset_ascent(fs)
2254 XFontSet fs;
2255{
2256 XFontSetExtents *extents;
2257
2258 extents = XExtentsOfFontSet(fs);
2259 return -extents->max_logical_extent.y;
2260}
2261
2262#endif /* FEAT_XFONTSET */
2263
2264/*
2265 * Return the Pixel value (color) for the given color name.
2266 * Return INVALCOLOR for error.
2267 */
2268 guicolor_T
2269gui_mch_get_color(reqname)
2270 char_u *reqname;
2271{
2272 int i;
2273 char_u *name = reqname;
2274 Colormap colormap;
2275 XColor color;
2276 static char *(vimnames[][2]) =
2277 {
2278 /* A number of colors that some X11 systems don't have */
2279 {"LightRed", "#FFBBBB"},
2280 {"LightGreen", "#88FF88"},
2281 {"LightMagenta","#FFBBFF"},
2282 {"DarkCyan", "#008888"},
2283 {"DarkBlue", "#0000BB"},
2284 {"DarkRed", "#BB0000"},
2285 {"DarkMagenta", "#BB00BB"},
2286 {"DarkGrey", "#BBBBBB"},
2287 {"DarkYellow", "#BBBB00"},
2288 {NULL, NULL}
2289 };
2290
2291 /* can't do this when GUI not running */
2292 if (!gui.in_use || *reqname == NUL)
2293 return INVALCOLOR;
2294
2295 colormap = DefaultColormap(gui.dpy, XDefaultScreen(gui.dpy));
2296
2297 /* Do this twice if the name isn't recognized. */
2298 while (name != NULL)
2299 {
2300 i = XParseColor(gui.dpy, colormap, (char *)name, &color);
2301
2302#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2303 if (i == 0)
2304 {
2305 char *old;
2306
2307 /* The X11 system is trying to resolve named colors only by names
2308 * corresponding to the current locale language. But Vim scripts
2309 * usually contain the English color names. Therefore we have to
2310 * try a second time here with the native "C" locale set.
2311 * Hopefully, restoring the old locale this way works on all
2312 * systems...
2313 */
2314 old = setlocale(LC_ALL, NULL);
2315 if (old != NULL && STRCMP(old, "C") != 0)
2316 {
2317 old = (char *)vim_strsave((char_u *)old);
2318 setlocale(LC_ALL, "C");
2319 i = XParseColor(gui.dpy, colormap, (char *)name, &color);
2320 setlocale(LC_ALL, old);
2321 vim_free(old);
2322 }
2323 }
2324#endif
2325 if (i != 0 && (XAllocColor(gui.dpy, colormap, &color) != 0
2326 || find_closest_color(colormap, &color) == OK))
2327 return (guicolor_T)color.pixel;
2328
2329 /* check for a few builtin names */
2330 for (i = 0; ; ++i)
2331 {
2332 if (vimnames[i][0] == NULL)
2333 {
2334 name = NULL;
2335 break;
2336 }
2337 if (STRICMP(name, vimnames[i][0]) == 0)
2338 {
2339 name = (char_u *)vimnames[i][1];
2340 break;
2341 }
2342 }
2343 }
2344
2345 return INVALCOLOR;
2346}
2347
2348/*
2349 * Find closest color for "colorPtr" in "colormap". set "colorPtr" to the
2350 * resulting color.
2351 * Based on a similar function in TCL.
2352 * Return FAIL if not able to find or allocate a color.
2353 */
2354 static int
2355find_closest_color(colormap, colorPtr)
2356 Colormap colormap;
2357 XColor *colorPtr;
2358{
2359 double tmp, distance, closestDistance;
2360 int i, closest, numFound, cmap_size;
2361 XColor *colortable;
2362 XVisualInfo template, *visInfoPtr;
2363
2364 template.visualid = XVisualIDFromVisual(DefaultVisual(gui.dpy,
2365 XDefaultScreen(gui.dpy)));
2366 visInfoPtr = XGetVisualInfo(gui.dpy, (long)VisualIDMask,
2367 &template, &numFound);
2368 if (numFound < 1)
2369 /* FindClosestColor couldn't lookup visual */
2370 return FAIL;
2371
2372 cmap_size = visInfoPtr->colormap_size;
2373 XFree((char *)visInfoPtr);
2374 colortable = (XColor *)alloc((unsigned)(cmap_size * sizeof(XColor)));
2375 if (!colortable)
2376 return FAIL; /* out of memory */
2377
2378 for (i = 0; i < cmap_size; i++)
2379 colortable[i].pixel = (unsigned long)i;
2380 XQueryColors (gui.dpy, colormap, colortable, cmap_size);
2381
2382 /*
2383 * Find the color that best approximates the desired one, then
2384 * try to allocate that color. If that fails, it must mean that
2385 * the color was read-write (so we can't use it, since it's owner
2386 * might change it) or else it was already freed. Try again,
2387 * over and over again, until something succeeds.
2388 */
2389 closestDistance = 1e30;
2390 closest = 0;
2391 for (i = 0; i < cmap_size; i++)
2392 {
2393 /*
2394 * Use Euclidean distance in RGB space, weighted by Y (of YIQ)
2395 * as the objective function; this accounts for differences
2396 * in the color sensitivity of the eye.
2397 */
2398 tmp = .30 * (((int)colorPtr->red) - (int)colortable[i].red);
2399 distance = tmp * tmp;
2400 tmp = .61 * (((int)colorPtr->green) - (int)colortable[i].green);
2401 distance += tmp * tmp;
2402 tmp = .11 * (((int)colorPtr->blue) - (int)colortable[i].blue);
2403 distance += tmp * tmp;
2404 if (distance < closestDistance)
2405 {
2406 closest = i;
2407 closestDistance = distance;
2408 }
2409 }
2410
2411 if (XAllocColor(gui.dpy, colormap, &colortable[closest]) != 0)
2412 {
2413 gui.color_approx = TRUE;
2414 *colorPtr = colortable[closest];
2415 }
2416
2417 free(colortable);
2418 return OK;
2419}
2420
Bram Moolenaarfb269802005-03-15 22:46:30 +00002421/*
2422 * Set the current text foreground color.
2423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 void
2425gui_mch_set_fg_color(color)
2426 guicolor_T color;
2427{
2428 if (color != prev_fg_color)
2429 {
2430 XSetForeground(gui.dpy, gui.text_gc, (Pixel)color);
2431 prev_fg_color = color;
2432 }
2433}
2434
2435/*
2436 * Set the current text background color.
2437 */
2438 void
2439gui_mch_set_bg_color(color)
2440 guicolor_T color;
2441{
2442 if (color != prev_bg_color)
2443 {
2444 XSetBackground(gui.dpy, gui.text_gc, (Pixel)color);
2445 prev_bg_color = color;
2446 }
2447}
2448
2449/*
Bram Moolenaarfb269802005-03-15 22:46:30 +00002450 * Set the current text special color.
2451 */
2452 void
2453gui_mch_set_sp_color(color)
2454 guicolor_T color;
2455{
2456 prev_sp_color = color;
2457}
2458
2459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 * create a mouse pointer that is blank
2461 */
2462 static Cursor
2463gui_x11_create_blank_mouse()
2464{
2465 Pixmap blank_pixmap = XCreatePixmap(gui.dpy, gui.wid, 1, 1, 1);
2466 GC gc = XCreateGC(gui.dpy, blank_pixmap, (unsigned long)0, (XGCValues*)0);
2467 XDrawPoint(gui.dpy, blank_pixmap, gc, 0, 0);
2468 XFreeGC(gui.dpy, gc);
2469 return XCreatePixmapCursor(gui.dpy, blank_pixmap, blank_pixmap,
2470 (XColor*)&gui.norm_pixel, (XColor*)&gui.norm_pixel, 0, 0);
2471}
2472
Bram Moolenaarfb269802005-03-15 22:46:30 +00002473/*
2474 * Draw a curled line at the bottom of the character cell.
2475 */
2476 static void
2477draw_curl(row, col, cells)
2478 int row;
2479 int col;
2480 int cells;
2481{
2482 int i;
2483 int offset;
2484 const static int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
2485
2486 XSetForeground(gui.dpy, gui.text_gc, prev_sp_color);
2487 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
2488 {
2489 offset = val[i % 8];
2490 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, i,
2491 FILL_Y(row + 1) - 1 - offset);
2492 }
2493 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
2494}
2495
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 void
2497gui_mch_draw_string(row, col, s, len, flags)
2498 int row;
2499 int col;
2500 char_u *s;
2501 int len;
2502 int flags;
2503{
2504 int cells = len;
2505#ifdef FEAT_MBYTE
2506 static XChar2b *buf = NULL;
2507 static int buflen = 0;
2508 char_u *p;
2509 int wlen = 0;
2510 int c;
2511
2512 if (enc_utf8)
2513 {
2514 /* Convert UTF-8 byte sequence to 16 bit characters for the X
2515 * functions. Need a buffer for the 16 bit characters. Keep it
2516 * between calls, because allocating it each time is slow. */
2517 if (buflen < len)
2518 {
2519 XtFree((char *)buf);
2520 buf = (XChar2b *)XtMalloc(len * sizeof(XChar2b));
2521 buflen = len;
2522 }
2523 p = s;
2524 cells = 0;
2525 while (p < s + len)
2526 {
2527 c = utf_ptr2char(p);
2528 if (c >= 0x10000) /* show chars > 0xffff as ? */
2529 c = 0xbf;
2530 buf[wlen].byte1 = (unsigned)c >> 8;
2531 buf[wlen].byte2 = c;
2532 ++wlen;
2533 cells += utf_char2cells(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002534 p += utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 }
2536 }
2537 else if (has_mbyte)
2538 {
2539 cells = 0;
2540 for (p = s; p < s + len; )
2541 {
2542 cells += ptr2cells(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002543 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 }
2545 }
2546
2547#endif
2548
2549#ifdef FEAT_XFONTSET
2550 if (current_fontset != NULL)
2551 {
2552 /* Setup a clip rectangle to avoid spilling over in the next or
2553 * previous line. This is apparently needed for some fonts which are
2554 * used in a fontset. */
2555 XRectangle clip;
2556
2557 clip.x = 0;
2558 clip.y = 0;
2559 clip.height = gui.char_height;
2560 clip.width = gui.char_width * cells + 1;
2561 XSetClipRectangles(gui.dpy, gui.text_gc, FILL_X(col), FILL_Y(row),
2562 &clip, 1, Unsorted);
2563 }
2564#endif
2565
2566 if (flags & DRAW_TRANSP)
2567 {
2568#ifdef FEAT_MBYTE
2569 if (enc_utf8)
2570 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2571 TEXT_Y(row), buf, wlen);
2572 else
2573#endif
2574 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2575 TEXT_Y(row), (char *)s, len);
2576 }
2577 else if (p_linespace != 0
2578#ifdef FEAT_XFONTSET
2579 || current_fontset != NULL
2580#endif
2581 )
2582 {
2583 XSetForeground(gui.dpy, gui.text_gc, prev_bg_color);
2584 XFillRectangle(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
2585 FILL_Y(row), gui.char_width * cells, gui.char_height);
2586 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
Bram Moolenaarfb269802005-03-15 22:46:30 +00002587
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588#ifdef FEAT_MBYTE
2589 if (enc_utf8)
2590 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2591 TEXT_Y(row), buf, wlen);
2592 else
2593#endif
2594 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2595 TEXT_Y(row), (char *)s, len);
2596 }
2597 else
2598 {
2599 /* XmbDrawImageString has bug, don't use it for fontset. */
2600#ifdef FEAT_MBYTE
2601 if (enc_utf8)
2602 XDrawImageString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2603 TEXT_Y(row), buf, wlen);
2604 else
2605#endif
2606 XDrawImageString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2607 TEXT_Y(row), (char *)s, len);
2608 }
2609
2610 /* Bold trick: draw the text again with a one-pixel offset. */
2611 if (flags & DRAW_BOLD)
2612 {
2613#ifdef FEAT_MBYTE
2614 if (enc_utf8)
2615 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col) + 1,
2616 TEXT_Y(row), buf, wlen);
2617 else
2618#endif
2619 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col) + 1,
2620 TEXT_Y(row), (char *)s, len);
2621 }
2622
Bram Moolenaarfb269802005-03-15 22:46:30 +00002623 /* Undercurl: draw curl at the bottom of the character cell. */
2624 if (flags & DRAW_UNDERC)
2625 draw_curl(row, col, cells);
2626
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 /* Underline: draw a line at the bottom of the character cell. */
2628 if (flags & DRAW_UNDERL)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002629 {
2630 int y = FILL_Y(row + 1) - 1;
2631
2632 /* When p_linespace is 0, overwrite the bottom row of pixels.
2633 * Otherwise put the line just below the character. */
2634 if (p_linespace > 1)
2635 y -= p_linespace - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 XDrawLine(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
Bram Moolenaarfb269802005-03-15 22:46:30 +00002637 y, FILL_X(col + cells) - 1, y);
2638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639
2640#ifdef FEAT_XFONTSET
2641 if (current_fontset != NULL)
2642 XSetClipMask(gui.dpy, gui.text_gc, None);
2643#endif
2644}
2645
2646/*
2647 * Return OK if the key with the termcap name "name" is supported.
2648 */
2649 int
2650gui_mch_haskey(name)
2651 char_u *name;
2652{
2653 int i;
2654
2655 for (i = 0; special_keys[i].key_sym != (KeySym)0; i++)
2656 if (name[0] == special_keys[i].vim_code0 &&
2657 name[1] == special_keys[i].vim_code1)
2658 return OK;
2659 return FAIL;
2660}
2661
2662/*
2663 * Return the text window-id and display. Only required for X-based GUI's
2664 */
2665 int
2666gui_get_x11_windis(win, dis)
2667 Window *win;
2668 Display **dis;
2669{
2670 *win = XtWindow(vimShell);
2671 *dis = gui.dpy;
2672 return OK;
2673}
2674
2675 void
2676gui_mch_beep()
2677{
2678 XBell(gui.dpy, 0);
2679}
2680
2681 void
2682gui_mch_flash(msec)
2683 int msec;
2684{
2685 /* Do a visual beep by reversing the foreground and background colors */
2686 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc, 0, 0,
2687 FILL_X((int)Columns) + gui.border_offset,
2688 FILL_Y((int)Rows) + gui.border_offset);
2689 XSync(gui.dpy, False);
2690 ui_delay((long)msec, TRUE); /* wait for a few msec */
2691 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc, 0, 0,
2692 FILL_X((int)Columns) + gui.border_offset,
2693 FILL_Y((int)Rows) + gui.border_offset);
2694}
2695
2696/*
2697 * Invert a rectangle from row r, column c, for nr rows and nc columns.
2698 */
2699 void
2700gui_mch_invert_rectangle(r, c, nr, nc)
2701 int r;
2702 int c;
2703 int nr;
2704 int nc;
2705{
2706 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc,
2707 FILL_X(c), FILL_Y(r), (nc) * gui.char_width, (nr) * gui.char_height);
2708}
2709
2710/*
2711 * Iconify the GUI window.
2712 */
2713 void
2714gui_mch_iconify()
2715{
2716 XIconifyWindow(gui.dpy, XtWindow(vimShell), DefaultScreen(gui.dpy));
2717}
2718
2719#if defined(FEAT_EVAL) || defined(PROTO)
2720/*
2721 * Bring the Vim window to the foreground.
2722 */
2723 void
2724gui_mch_set_foreground()
2725{
2726 XMapRaised(gui.dpy, XtWindow(vimShell));
2727}
2728#endif
2729
2730/*
2731 * Draw a cursor without focus.
2732 */
2733 void
2734gui_mch_draw_hollow_cursor(color)
2735 guicolor_T color;
2736{
2737 int w = 1;
2738
2739#ifdef FEAT_MBYTE
2740 if (mb_lefthalve(gui.row, gui.col))
2741 w = 2;
2742#endif
2743 gui_mch_set_fg_color(color);
2744 XDrawRectangle(gui.dpy, gui.wid, gui.text_gc, FILL_X(gui.col),
2745 FILL_Y(gui.row), w * gui.char_width - 1, gui.char_height - 1);
2746}
2747
2748/*
2749 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
2750 * color "color".
2751 */
2752 void
2753gui_mch_draw_part_cursor(w, h, color)
2754 int w;
2755 int h;
2756 guicolor_T color;
2757{
2758 gui_mch_set_fg_color(color);
2759
2760 XFillRectangle(gui.dpy, gui.wid, gui.text_gc,
2761#ifdef FEAT_RIGHTLEFT
2762 /* vertical line should be on the right of current point */
2763 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
2764#endif
2765 FILL_X(gui.col),
2766 FILL_Y(gui.row) + gui.char_height - h,
2767 w, h);
2768}
2769
2770/*
2771 * Catch up with any queued X events. This may put keyboard input into the
2772 * input buffer, call resize call-backs, trigger timers etc. If there is
2773 * nothing in the X event queue (& no timers pending), then we return
2774 * immediately.
2775 */
2776 void
2777gui_mch_update()
2778{
2779 XtInputMask mask, desired;
2780
2781#ifdef ALT_X_INPUT
2782 if (suppress_alternate_input)
2783 desired = (XtIMXEvent | XtIMTimer);
2784 else
2785#endif
2786 desired = (XtIMAll);
2787 while ((mask = XtAppPending(app_context)) && (mask & desired)
2788 && !vim_is_input_buf_full())
2789 XtAppProcessEvent(app_context, desired);
2790}
2791
2792/*
2793 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2794 * from the keyboard.
2795 * wtime == -1 Wait forever.
2796 * wtime == 0 This should never happen.
2797 * wtime > 0 Wait wtime milliseconds for a character.
2798 * Returns OK if a character was found to be available within the given time,
2799 * or FAIL otherwise.
2800 */
2801 int
2802gui_mch_wait_for_chars(wtime)
2803 long wtime;
2804{
2805 int focus;
2806
2807 /*
2808 * Make this static, in case gui_x11_timer_cb is called after leaving
2809 * this function (otherwise a random value on the stack may be changed).
2810 */
2811 static int timed_out;
2812 XtIntervalId timer = (XtIntervalId)0;
2813 XtInputMask desired;
2814#ifdef FEAT_SNIFF
2815 static int sniff_on = 0;
2816 static XtInputId sniff_input_id = 0;
2817#endif
2818
2819 timed_out = FALSE;
2820
2821#ifdef FEAT_SNIFF
2822 if (sniff_on && !want_sniff_request)
2823 {
2824 if (sniff_input_id)
2825 XtRemoveInput(sniff_input_id);
2826 sniff_on = 0;
2827 }
2828 else if (!sniff_on && want_sniff_request)
2829 {
2830 sniff_input_id = XtAppAddInput(app_context, fd_from_sniff,
2831 (XtPointer)XtInputReadMask, gui_x11_sniff_request_cb, 0);
2832 sniff_on = 1;
2833 }
2834#endif
2835
2836 if (wtime > 0)
2837 timer = XtAppAddTimeOut(app_context, (long_u)wtime, gui_x11_timer_cb,
2838 &timed_out);
2839
2840 focus = gui.in_focus;
2841#ifdef ALT_X_INPUT
2842 if (suppress_alternate_input)
2843 desired = (XtIMXEvent | XtIMTimer);
2844 else
2845#endif
2846 desired = (XtIMAll);
2847 while (!timed_out)
2848 {
2849 /* Stop or start blinking when focus changes */
2850 if (gui.in_focus != focus)
2851 {
2852 if (gui.in_focus)
2853 gui_mch_start_blink();
2854 else
2855 gui_mch_stop_blink();
2856 focus = gui.in_focus;
2857 }
2858
2859 /*
2860 * Don't use gui_mch_update() because then we will spin-lock until a
2861 * char arrives, instead we use XtAppProcessEvent() to hang until an
2862 * event arrives. No need to check for input_buf_full because we are
2863 * returning as soon as it contains a single char. Note that
2864 * XtAppNextEvent() may not be used because it will not return after a
2865 * timer event has arrived -- webb
2866 */
2867 XtAppProcessEvent(app_context, desired);
2868
2869 if (input_available())
2870 {
2871 if (timer != (XtIntervalId)0 && !timed_out)
2872 XtRemoveTimeOut(timer);
2873 return OK;
2874 }
2875 }
2876 return FAIL;
2877}
2878
2879/*
2880 * Output routines.
2881 */
2882
2883/* Flush any output to the screen */
2884 void
2885gui_mch_flush()
2886{
2887 XFlush(gui.dpy);
2888}
2889
2890/*
2891 * Clear a rectangular region of the screen from text pos (row1, col1) to
2892 * (row2, col2) inclusive.
2893 */
2894 void
2895gui_mch_clear_block(row1, col1, row2, col2)
2896 int row1;
2897 int col1;
2898 int row2;
2899 int col2;
2900{
2901 int x;
2902
2903 x = FILL_X(col1);
2904
2905 /* Clear one extra pixel at the far right, for when bold characters have
2906 * spilled over to the next column. */
2907 XFillRectangle(gui.dpy, gui.wid, gui.back_gc, x, FILL_Y(row1),
2908 (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1),
2909 (row2 - row1 + 1) * gui.char_height);
2910}
2911
2912 void
2913gui_mch_clear_all()
2914{
2915 XClearArea(gui.dpy, gui.wid, 0, 0, 0, 0, False);
2916}
2917
2918/*
2919 * Delete the given number of lines from the given row, scrolling up any
2920 * text further down within the scroll region.
2921 */
2922 void
2923gui_mch_delete_lines(row, num_lines)
2924 int row;
2925 int num_lines;
2926{
2927 if (gui.visibility == VisibilityFullyObscured)
2928 return; /* Can't see the window */
2929
2930 /* copy one extra pixel at the far right, for when bold has spilled
2931 * over */
2932 XCopyArea(gui.dpy, gui.wid, gui.wid, gui.text_gc,
2933 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
2934 gui.char_width * (gui.scroll_region_right - gui.scroll_region_left + 1)
2935 + (gui.scroll_region_right == Columns - 1),
2936 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1),
2937 FILL_X(gui.scroll_region_left), FILL_Y(row));
2938
2939 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
2940 gui.scroll_region_left,
2941 gui.scroll_region_bot, gui.scroll_region_right);
2942 gui_x11_check_copy_area();
2943}
2944
2945/*
2946 * Insert the given number of lines before the given row, scrolling down any
2947 * following text within the scroll region.
2948 */
2949 void
2950gui_mch_insert_lines(row, num_lines)
2951 int row;
2952 int num_lines;
2953{
2954 if (gui.visibility == VisibilityFullyObscured)
2955 return; /* Can't see the window */
2956
2957 /* copy one extra pixel at the far right, for when bold has spilled
2958 * over */
2959 XCopyArea(gui.dpy, gui.wid, gui.wid, gui.text_gc,
2960 FILL_X(gui.scroll_region_left), FILL_Y(row),
2961 gui.char_width * (gui.scroll_region_right - gui.scroll_region_left + 1)
2962 + (gui.scroll_region_right == Columns - 1),
2963 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1),
2964 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines));
2965
2966 gui_clear_block(row, gui.scroll_region_left,
2967 row + num_lines - 1, gui.scroll_region_right);
2968 gui_x11_check_copy_area();
2969}
2970
2971/*
2972 * Update the region revealed by scrolling up/down.
2973 */
2974 static void
2975gui_x11_check_copy_area()
2976{
2977 XEvent event;
2978 XGraphicsExposeEvent *gevent;
2979
2980 if (gui.visibility != VisibilityPartiallyObscured)
2981 return;
2982
2983 XFlush(gui.dpy);
2984
2985 /* Wait to check whether the scroll worked or not */
2986 for (;;)
2987 {
2988 if (XCheckTypedEvent(gui.dpy, NoExpose, &event))
2989 return; /* The scroll worked. */
2990
2991 if (XCheckTypedEvent(gui.dpy, GraphicsExpose, &event))
2992 {
2993 gevent = (XGraphicsExposeEvent *)&event;
2994 gui_redraw(gevent->x, gevent->y, gevent->width, gevent->height);
2995 if (gevent->count == 0)
2996 return; /* This was the last expose event */
2997 }
2998 XSync(gui.dpy, False);
2999 }
3000}
3001
3002/*
3003 * X Selection stuff, for cutting and pasting text to other windows.
3004 */
3005
3006 void
3007clip_mch_lose_selection(cbd)
3008 VimClipboard *cbd;
3009{
3010 clip_x11_lose_selection(vimShell, cbd);
3011}
3012
3013 int
3014clip_mch_own_selection(cbd)
3015 VimClipboard *cbd;
3016{
3017 return clip_x11_own_selection(vimShell, cbd);
3018}
3019
3020 void
3021clip_mch_request_selection(cbd)
3022 VimClipboard *cbd;
3023{
3024 clip_x11_request_selection(vimShell, gui.dpy, cbd);
3025}
3026
3027 void
3028clip_mch_set_selection(cbd)
3029 VimClipboard *cbd;
3030{
3031 clip_x11_set_selection(cbd);
3032}
3033
3034#if defined(FEAT_MENU) || defined(PROTO)
3035/*
3036 * Menu stuff.
3037 */
3038
3039/*
3040 * Make a menu either grey or not grey.
3041 */
3042 void
3043gui_mch_menu_grey(menu, grey)
3044 vimmenu_T *menu;
3045 int grey;
3046{
3047 if (menu->id != (Widget)0)
3048 {
3049 gui_mch_menu_hidden(menu, False);
3050 if (grey
3051#ifdef FEAT_GUI_MOTIF
3052 || !menu->sensitive
3053#endif
3054 )
3055 XtSetSensitive(menu->id, False);
3056 else
3057 XtSetSensitive(menu->id, True);
3058 }
3059}
3060
3061/*
3062 * Make menu item hidden or not hidden
3063 */
3064 void
3065gui_mch_menu_hidden(menu, hidden)
3066 vimmenu_T *menu;
3067 int hidden;
3068{
3069 if (menu->id != (Widget)0)
3070 {
3071 if (hidden)
3072 XtUnmanageChild(menu->id);
3073 else
3074 XtManageChild(menu->id);
3075 }
3076}
3077
3078/*
3079 * This is called after setting all the menus to grey/hidden or not.
3080 */
3081 void
3082gui_mch_draw_menubar()
3083{
3084 /* Nothing to do in X */
3085}
3086
3087/* ARGSUSED */
3088 void
3089gui_x11_menu_cb(w, client_data, call_data)
3090 Widget w;
3091 XtPointer client_data, call_data;
3092{
3093 gui_menu_cb((vimmenu_T *)client_data);
3094}
3095
3096#endif /* FEAT_MENU */
3097
3098
3099
3100/*
3101 * Function called when window closed. Works like ":qa".
3102 * Should put up a requester!
3103 */
3104/*ARGSUSED*/
3105 static void
3106gui_x11_wm_protocol_handler(w, client_data, event, dum)
3107 Widget w;
3108 XtPointer client_data;
3109 XEvent *event;
3110 Boolean *dum;
3111{
3112 /*
3113 * Only deal with Client messages.
3114 */
3115 if (event->type != ClientMessage)
3116 return;
3117
3118 /*
3119 * The WM_SAVE_YOURSELF event arrives when the window manager wants to
3120 * exit. That can be cancelled though, thus Vim shouldn't exit here.
3121 * Just sync our swap files.
3122 */
3123 if (((XClientMessageEvent *)event)->data.l[0] ==
3124 wm_atoms[SAVE_YOURSELF_IDX])
3125 {
3126 out_flush();
3127 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
3128
3129 /* Set the window's WM_COMMAND property, to let the window manager
3130 * know we are done saving ourselves. We don't want to be restarted,
3131 * thus set argv to NULL. */
3132 XSetCommand(gui.dpy, XtWindow(vimShell), NULL, 0);
3133 return;
3134 }
3135
3136 if (((XClientMessageEvent *)event)->data.l[0] !=
3137 wm_atoms[DELETE_WINDOW_IDX])
3138 return;
3139
3140 gui_shell_closed();
3141}
3142
3143#ifdef FEAT_CLIENTSERVER
3144/*
3145 * Function called when property changed. Check for incoming commands
3146 */
3147/*ARGSUSED*/
3148 static void
3149gui_x11_send_event_handler(w, client_data, event, dum)
3150 Widget w;
3151 XtPointer client_data;
3152 XEvent *event;
3153 Boolean *dum;
3154{
3155 XPropertyEvent *e = (XPropertyEvent *) event;
3156
3157 if (e->type == PropertyNotify && e->window == commWindow
3158 && e->atom == commProperty && e->state == PropertyNewValue)
3159 {
3160 serverEventProc(gui.dpy, event);
3161 }
3162}
3163#endif
3164
3165/*
3166 * Cursor blink functions.
3167 *
3168 * This is a simple state machine:
3169 * BLINK_NONE not blinking at all
3170 * BLINK_OFF blinking, cursor is not shown
3171 * BLINK_ON blinking, cursor is shown
3172 */
3173
3174#define BLINK_NONE 0
3175#define BLINK_OFF 1
3176#define BLINK_ON 2
3177
3178static int blink_state = BLINK_NONE;
3179static long_u blink_waittime = 700;
3180static long_u blink_ontime = 400;
3181static long_u blink_offtime = 250;
3182static XtIntervalId blink_timer = (XtIntervalId)0;
3183
3184 void
3185gui_mch_set_blinking(waittime, on, off)
3186 long waittime, on, off;
3187{
3188 blink_waittime = waittime;
3189 blink_ontime = on;
3190 blink_offtime = off;
3191}
3192
3193/*
3194 * Stop the cursor blinking. Show the cursor if it wasn't shown.
3195 */
3196 void
3197gui_mch_stop_blink()
3198{
3199 if (blink_timer != (XtIntervalId)0)
3200 {
3201 XtRemoveTimeOut(blink_timer);
3202 blink_timer = (XtIntervalId)0;
3203 }
3204 if (blink_state == BLINK_OFF)
3205 gui_update_cursor(TRUE, FALSE);
3206 blink_state = BLINK_NONE;
3207}
3208
3209/*
3210 * Start the cursor blinking. If it was already blinking, this restarts the
3211 * waiting time and shows the cursor.
3212 */
3213 void
3214gui_mch_start_blink()
3215{
3216 if (blink_timer != (XtIntervalId)0)
3217 XtRemoveTimeOut(blink_timer);
3218 /* Only switch blinking on if none of the times is zero */
3219 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
3220 {
3221 blink_timer = XtAppAddTimeOut(app_context, blink_waittime,
3222 gui_x11_blink_cb, NULL);
3223 blink_state = BLINK_ON;
3224 gui_update_cursor(TRUE, FALSE);
3225 }
3226}
3227
3228/* ARGSUSED */
3229 static void
3230gui_x11_blink_cb(timed_out, interval_id)
3231 XtPointer timed_out;
3232 XtIntervalId *interval_id;
3233{
3234 if (blink_state == BLINK_ON)
3235 {
3236 gui_undraw_cursor();
3237 blink_state = BLINK_OFF;
3238 blink_timer = XtAppAddTimeOut(app_context, blink_offtime,
3239 gui_x11_blink_cb, NULL);
3240 }
3241 else
3242 {
3243 gui_update_cursor(TRUE, FALSE);
3244 blink_state = BLINK_ON;
3245 blink_timer = XtAppAddTimeOut(app_context, blink_ontime,
3246 gui_x11_blink_cb, NULL);
3247 }
3248}
3249
3250/*
3251 * Return the RGB value of a pixel as a long.
3252 */
3253 long_u
3254gui_mch_get_rgb(pixel)
3255 guicolor_T pixel;
3256{
3257 XColor xc;
3258 Colormap colormap;
3259
3260 colormap = DefaultColormap(gui.dpy, XDefaultScreen(gui.dpy));
3261 xc.pixel = pixel;
3262 XQueryColor(gui.dpy, colormap, &xc);
3263
3264 return ((xc.red & 0xff00) << 8) + (xc.green & 0xff00)
3265 + ((unsigned)xc.blue >> 8);
3266}
3267
3268/*
3269 * Add the callback functions.
3270 */
3271 void
3272gui_x11_callbacks(textArea, vimForm)
3273 Widget textArea;
3274 Widget vimForm;
3275{
3276 XtAddEventHandler(textArea, VisibilityChangeMask, FALSE,
3277 gui_x11_visibility_cb, (XtPointer)0);
3278
3279 XtAddEventHandler(textArea, ExposureMask, FALSE, gui_x11_expose_cb,
3280 (XtPointer)0);
3281
3282 XtAddEventHandler(vimShell, StructureNotifyMask, FALSE,
3283 gui_x11_resize_window_cb, (XtPointer)0);
3284
3285 XtAddEventHandler(vimShell, FocusChangeMask, FALSE, gui_x11_focus_change_cb,
3286 (XtPointer)0);
3287 /*
3288 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3289 * Only needed for some window managers.
3290 */
3291 if (vim_strchr(p_go, GO_POINTER) != NULL)
3292 {
3293 XtAddEventHandler(vimShell, LeaveWindowMask, FALSE, gui_x11_leave_cb,
3294 (XtPointer)0);
3295 XtAddEventHandler(textArea, LeaveWindowMask, FALSE, gui_x11_leave_cb,
3296 (XtPointer)0);
3297 XtAddEventHandler(textArea, EnterWindowMask, FALSE, gui_x11_enter_cb,
3298 (XtPointer)0);
3299 XtAddEventHandler(vimShell, EnterWindowMask, FALSE, gui_x11_enter_cb,
3300 (XtPointer)0);
3301 }
3302
3303 XtAddEventHandler(vimForm, KeyPressMask, FALSE, gui_x11_key_hit_cb,
3304 (XtPointer)0);
3305 XtAddEventHandler(textArea, KeyPressMask, FALSE, gui_x11_key_hit_cb,
3306 (XtPointer)0);
3307
3308 /* get pointer moved events from scrollbar, needed for 'mousefocus' */
3309 XtAddEventHandler(vimForm, PointerMotionMask,
3310 FALSE, gui_x11_mouse_cb, (XtPointer)1);
3311 XtAddEventHandler(textArea, ButtonPressMask | ButtonReleaseMask |
3312 ButtonMotionMask | PointerMotionMask,
3313 FALSE, gui_x11_mouse_cb, (XtPointer)0);
3314}
3315
3316/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003317 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003319 void
3320gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321{
3322 int rootx, rooty, winx, winy;
3323 Window root, child;
3324 unsigned int mask;
3325
3326 if (gui.wid && XQueryPointer(gui.dpy, gui.wid, &root, &child,
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003327 &rootx, &rooty, &winx, &winy, &mask)) {
3328 *x = winx;
3329 *y = winy;
3330 } else {
3331 *x = -1;
3332 *y = -1;
3333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334}
3335
3336 void
3337gui_mch_setmouse(x, y)
3338 int x;
3339 int y;
3340{
3341 if (gui.wid)
3342 XWarpPointer(gui.dpy, (Window)0, gui.wid, 0, 0, 0, 0, x, y);
3343}
3344
3345#if (defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)) || defined(PROTO)
3346 XButtonPressedEvent *
3347gui_x11_get_last_mouse_event()
3348{
3349 return &last_mouse_event;
3350}
3351#endif
3352
3353#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
3354
3355/* Signs are currently always 2 chars wide. Hopefully the font is big enough
3356 * to provide room for the bitmap! */
3357# define SIGN_WIDTH (gui.char_width * 2)
3358
3359#if 0 /* not used */
3360 void
3361gui_mch_clearsign(row)
3362 int row;
3363{
3364 if (gui.in_use)
3365 XClearArea(gui.dpy, gui.wid, 0, TEXT_Y(row) - gui.char_height,
3366 SIGN_WIDTH, gui.char_height, FALSE);
3367}
3368#endif
3369
3370 void
3371gui_mch_drawsign(row, col, typenr)
3372 int row;
3373 int col;
3374 int typenr;
3375{
3376 XImage *sign;
3377
3378 if (gui.in_use && (sign = (XImage *)sign_get_image(typenr)) != NULL)
3379 {
3380 XClearArea(gui.dpy, gui.wid, TEXT_X(col), TEXT_Y(row) - sign->height,
3381 SIGN_WIDTH, gui.char_height, FALSE);
3382 XPutImage(gui.dpy, gui.wid, gui.text_gc, sign, 0, 0,
3383 TEXT_X(col) + (SIGN_WIDTH - sign->width) / 2,
3384 TEXT_Y(row) - sign->height,
3385 sign->width, sign->height);
3386 }
3387}
3388
3389 void *
3390gui_mch_register_sign(signfile)
3391 char_u *signfile;
3392{
3393 XpmAttributes attrs;
3394 XImage *sign;
3395 int status;
3396
3397 /*
3398 * Setup the color substitution table.
3399 */
3400 sign = NULL;
3401 if (signfile[0] != NUL && signfile[0] != '-')
3402 {
3403 sign = (XImage *)alloc(sizeof(XImage));
3404 if (sign != NULL)
3405 {
3406 XpmColorSymbol color[5] =
3407 {
3408 {"none", NULL, 0},
3409 {"iconColor1", NULL, 0},
3410 {"bottomShadowColor", NULL, 0},
3411 {"topShadowColor", NULL, 0},
3412 {"selectColor", NULL, 0}
3413 };
3414 attrs.valuemask = XpmColorSymbols;
3415 attrs.numsymbols = 2;
3416 attrs.colorsymbols = color;
3417 attrs.colorsymbols[0].pixel = gui.back_pixel;
3418 attrs.colorsymbols[1].pixel = gui.norm_pixel;
3419 status = XpmReadFileToImage(gui.dpy, (char *)signfile,
3420 &sign, NULL, &attrs);
3421
3422 if (status == 0)
3423 {
3424 /* Sign width is fixed at two columns now.
3425 if (sign->width > gui.sign_width)
3426 gui.sign_width = sign->width + 8; */
3427 }
3428 else
3429 {
3430 vim_free(sign);
3431 sign = NULL;
3432 EMSG(_(e_signdata));
3433 }
3434 }
3435 }
3436
3437 return (void *)sign;
3438}
3439
3440 void
3441gui_mch_destroy_sign(sign)
3442 void *sign;
3443{
3444 XFree(((XImage *)sign)->data);
3445 vim_free(sign);
3446}
3447#endif
3448
3449
3450#ifdef FEAT_MOUSESHAPE
3451/* The last set mouse pointer shape is remembered, to be used when it goes
3452 * from hidden to not hidden. */
3453static int last_shape = 0;
3454#endif
3455
3456/*
3457 * Use the blank mouse pointer or not.
3458 */
3459 void
3460gui_mch_mousehide(hide)
3461 int hide; /* TRUE = use blank ptr, FALSE = use parent ptr */
3462{
3463 if (gui.pointer_hidden != hide)
3464 {
3465 gui.pointer_hidden = hide;
3466 if (hide)
3467 XDefineCursor(gui.dpy, gui.wid, gui.blank_pointer);
3468 else
3469#ifdef FEAT_MOUSESHAPE
3470 mch_set_mouse_shape(last_shape);
3471#else
3472 XUndefineCursor(gui.dpy, gui.wid);
3473#endif
3474 }
3475}
3476
3477#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3478
3479/* Table for shape IDs. Keep in sync with the mshape_names[] table in
3480 * misc2.c! */
3481static int mshape_ids[] =
3482{
3483 XC_left_ptr, /* arrow */
3484 0, /* blank */
3485 XC_xterm, /* beam */
3486 XC_sb_v_double_arrow, /* updown */
3487 XC_sizing, /* udsizing */
3488 XC_sb_h_double_arrow, /* leftright */
3489 XC_sizing, /* lrsizing */
3490 XC_watch, /* busy */
3491 XC_X_cursor, /* no */
3492 XC_crosshair, /* crosshair */
3493 XC_hand1, /* hand1 */
3494 XC_hand2, /* hand2 */
3495 XC_pencil, /* pencil */
3496 XC_question_arrow, /* question */
3497 XC_right_ptr, /* right-arrow */
3498 XC_center_ptr, /* up-arrow */
3499 XC_left_ptr /* last one */
3500};
3501
3502 void
3503mch_set_mouse_shape(shape)
3504 int shape;
3505{
3506 int id;
3507
3508 if (!gui.in_use)
3509 return;
3510
3511 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
3512 XDefineCursor(gui.dpy, gui.wid, gui.blank_pointer);
3513 else
3514 {
3515 if (shape >= MSHAPE_NUMBERED)
3516 {
3517 id = shape - MSHAPE_NUMBERED;
3518 if (id >= XC_num_glyphs)
3519 id = XC_left_ptr;
3520 else
3521 id &= ~1; /* they are always even (why?) */
3522 }
3523 else
3524 id = mshape_ids[shape];
3525
3526 XDefineCursor(gui.dpy, gui.wid, XCreateFontCursor(gui.dpy, id));
3527 }
3528 if (shape != MSHAPE_HIDE)
3529 last_shape = shape;
3530}
3531#endif
3532
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533#if (defined(FEAT_TOOLBAR) && defined(FEAT_BEVAL)) || defined(PROTO)
3534/*
3535 * Set the balloon-eval used for the tooltip of a toolbar menu item.
3536 * The check for a non-toolbar item was added, because there is a crash when
3537 * passing a normal menu item here. Can't explain that, but better avoid it.
3538 */
3539 void
3540gui_mch_menu_set_tip(menu)
3541 vimmenu_T *menu;
3542{
3543 if (menu->id != NULL && menu->parent != NULL
3544 && menu_is_toolbar(menu->parent->name))
3545 {
3546 /* Always destroy and create the balloon, in case the string was
3547 * changed. */
3548 if (menu->tip != NULL)
3549 {
3550 gui_mch_destroy_beval_area(menu->tip);
3551 menu->tip = NULL;
3552 }
3553 if (menu->strings[MENU_INDEX_TIP] != NULL)
3554 menu->tip = gui_mch_create_beval_area(
3555 menu->id,
3556 menu->strings[MENU_INDEX_TIP],
3557 NULL,
3558 NULL);
3559 }
3560}
3561#endif