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