blob: 7f09880cd01b5080a37ab38f77fcfc0778bdb126 [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"
Bram Moolenaar46582282016-07-23 14:35:12 +020074# define DFLT_TOOLTIP_BG_COLOR "#ffff91"
75# define DFLT_TOOLTIP_FG_COLOR "#000000"
Bram Moolenaar071d4272004-06-13 20:20:40 +000076#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 ""
Bram Moolenaar46582282016-07-23 14:35:12 +020082# define DFLT_TOOLTIP_BG_COLOR "#ffff91"
83# define DFLT_TOOLTIP_FG_COLOR "#000000"
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#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 \
Bram Moolenaara3227e22006-03-08 21:32:40 +0000114 XDrawString16(dpy, win, gc, x, y, (XChar2b *)str, n); \
115 } while (0)
116
117#define XDrawImageString16(dpy, win, gc, x, y, str, n) \
118 do \
119 { \
120 if (current_fontset != NULL) \
121 XwcDrawImageString(dpy, win, current_fontset, gc, x, y, (wchar_t *)str, n); \
122 else \
123 XDrawImageString16(dpy, win, gc, x, y, (XChar2b *)str, n); \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 } while (0)
125
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100126static int check_fontset_sanity(XFontSet fs);
127static int fontset_width(XFontSet fs);
128static int fontset_ascent(XFontSet fs);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#endif
130
131static guicolor_T prev_fg_color = INVALCOLOR;
132static guicolor_T prev_bg_color = INVALCOLOR;
Bram Moolenaarfb269802005-03-15 22:46:30 +0000133static guicolor_T prev_sp_color = INVALCOLOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
135#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
136static XButtonPressedEvent last_mouse_event;
137#endif
138
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100139static void gui_x11_timer_cb(XtPointer timed_out, XtIntervalId *interval_id);
140static void gui_x11_visibility_cb(Widget w, XtPointer dud, XEvent *event, Boolean *dum);
141static void gui_x11_expose_cb(Widget w, XtPointer dud, XEvent *event, Boolean *dum);
142static void gui_x11_resize_window_cb(Widget w, XtPointer dud, XEvent *event, Boolean *dum);
143static void gui_x11_focus_change_cb(Widget w, XtPointer data, XEvent *event, Boolean *dum);
144static void gui_x11_enter_cb(Widget w, XtPointer data, XEvent *event, Boolean *dum);
145static void gui_x11_leave_cb(Widget w, XtPointer data, XEvent *event, Boolean *dum);
146static void gui_x11_mouse_cb(Widget w, XtPointer data, XEvent *event, Boolean *dum);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100147static void gui_x11_check_copy_area(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#ifdef FEAT_CLIENTSERVER
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100149static void gui_x11_send_event_handler(Widget, XtPointer, XEvent *, Boolean *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100151static void gui_x11_wm_protocol_handler(Widget, XtPointer, XEvent *, Boolean *);
152static void gui_x11_blink_cb(XtPointer timed_out, XtIntervalId *interval_id);
153static Cursor gui_x11_create_blank_mouse(void);
154static void draw_curl(int row, int col, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
156
157/*
158 * Keycodes recognized by vim.
159 * NOTE: when changing this, the table in gui_gtk_x11.c probably needs the
160 * same change!
161 */
162static struct specialkey
163{
164 KeySym key_sym;
165 char_u vim_code0;
166 char_u vim_code1;
167} special_keys[] =
168{
169 {XK_Up, 'k', 'u'},
170 {XK_Down, 'k', 'd'},
171 {XK_Left, 'k', 'l'},
172 {XK_Right, 'k', 'r'},
173
174 {XK_F1, 'k', '1'},
175 {XK_F2, 'k', '2'},
176 {XK_F3, 'k', '3'},
177 {XK_F4, 'k', '4'},
178 {XK_F5, 'k', '5'},
179 {XK_F6, 'k', '6'},
180 {XK_F7, 'k', '7'},
181 {XK_F8, 'k', '8'},
182 {XK_F9, 'k', '9'},
183 {XK_F10, 'k', ';'},
184
185 {XK_F11, 'F', '1'},
186 {XK_F12, 'F', '2'},
187 {XK_F13, 'F', '3'},
188 {XK_F14, 'F', '4'},
189 {XK_F15, 'F', '5'},
190 {XK_F16, 'F', '6'},
191 {XK_F17, 'F', '7'},
192 {XK_F18, 'F', '8'},
193 {XK_F19, 'F', '9'},
194 {XK_F20, 'F', 'A'},
195
196 {XK_F21, 'F', 'B'},
197 {XK_F22, 'F', 'C'},
198 {XK_F23, 'F', 'D'},
199 {XK_F24, 'F', 'E'},
200 {XK_F25, 'F', 'F'},
201 {XK_F26, 'F', 'G'},
202 {XK_F27, 'F', 'H'},
203 {XK_F28, 'F', 'I'},
204 {XK_F29, 'F', 'J'},
205 {XK_F30, 'F', 'K'},
206
207 {XK_F31, 'F', 'L'},
208 {XK_F32, 'F', 'M'},
209 {XK_F33, 'F', 'N'},
210 {XK_F34, 'F', 'O'},
211 {XK_F35, 'F', 'P'}, /* keysymdef.h defines up to F35 */
212#ifdef SunXK_F36
213 {SunXK_F36, 'F', 'Q'},
214 {SunXK_F37, 'F', 'R'},
215#endif
216
217 {XK_Help, '%', '1'},
218 {XK_Undo, '&', '8'},
219 {XK_BackSpace, 'k', 'b'},
220 {XK_Insert, 'k', 'I'},
221 {XK_Delete, 'k', 'D'},
222 {XK_Home, 'k', 'h'},
223 {XK_End, '@', '7'},
224 {XK_Prior, 'k', 'P'},
225 {XK_Next, 'k', 'N'},
226 {XK_Print, '%', '9'},
227
228 /* Keypad keys: */
229#ifdef XK_KP_Left
230 {XK_KP_Left, 'k', 'l'},
231 {XK_KP_Right, 'k', 'r'},
232 {XK_KP_Up, 'k', 'u'},
233 {XK_KP_Down, 'k', 'd'},
234 {XK_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
235 {XK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
236 {XK_KP_Home, 'K', '1'},
237 {XK_KP_End, 'K', '4'},
238 {XK_KP_Prior, 'K', '3'},
239 {XK_KP_Next, 'K', '5'},
240
241 {XK_KP_Add, 'K', '6'},
242 {XK_KP_Subtract, 'K', '7'},
243 {XK_KP_Divide, 'K', '8'},
244 {XK_KP_Multiply, 'K', '9'},
245 {XK_KP_Enter, 'K', 'A'},
246 {XK_KP_Decimal, 'K', 'B'},
247
248 {XK_KP_0, 'K', 'C'},
249 {XK_KP_1, 'K', 'D'},
250 {XK_KP_2, 'K', 'E'},
251 {XK_KP_3, 'K', 'F'},
252 {XK_KP_4, 'K', 'G'},
253 {XK_KP_5, 'K', 'H'},
254 {XK_KP_6, 'K', 'I'},
255 {XK_KP_7, 'K', 'J'},
256 {XK_KP_8, 'K', 'K'},
257 {XK_KP_9, 'K', 'L'},
258#endif
259
260 /* End of list marker: */
261 {(KeySym)0, 0, 0}
262};
263
264#define XtNboldFont "boldFont"
265#define XtCBoldFont "BoldFont"
266#define XtNitalicFont "italicFont"
267#define XtCItalicFont "ItalicFont"
268#define XtNboldItalicFont "boldItalicFont"
269#define XtCBoldItalicFont "BoldItalicFont"
270#define XtNscrollbarWidth "scrollbarWidth"
271#define XtCScrollbarWidth "ScrollbarWidth"
272#define XtNmenuHeight "menuHeight"
273#define XtCMenuHeight "MenuHeight"
274#define XtNmenuFont "menuFont"
275#define XtCMenuFont "MenuFont"
276#define XtNmenuFontSet "menuFontSet"
277#define XtCMenuFontSet "MenuFontSet"
278
279
280/* Resources for setting the foreground and background colors of menus */
281#define XtNmenuBackground "menuBackground"
282#define XtCMenuBackground "MenuBackground"
283#define XtNmenuForeground "menuForeground"
284#define XtCMenuForeground "MenuForeground"
285
286/* Resources for setting the foreground and background colors of scrollbars */
287#define XtNscrollBackground "scrollBackground"
288#define XtCScrollBackground "ScrollBackground"
289#define XtNscrollForeground "scrollForeground"
290#define XtCScrollForeground "ScrollForeground"
291
292/* Resources for setting the foreground and background colors of tooltip */
293#define XtNtooltipBackground "tooltipBackground"
294#define XtCTooltipBackground "TooltipBackground"
295#define XtNtooltipForeground "tooltipForeground"
296#define XtCTooltipForeground "TooltipForeground"
297#define XtNtooltipFont "tooltipFont"
298#define XtCTooltipFont "TooltipFont"
299
300/*
301 * X Resources:
302 */
303static XtResource vim_resources[] =
304{
305 {
306 XtNforeground,
307 XtCForeground,
308 XtRPixel,
309 sizeof(Pixel),
310 XtOffsetOf(gui_T, def_norm_pixel),
311 XtRString,
312 XtDefaultForeground
313 },
314 {
315 XtNbackground,
316 XtCBackground,
317 XtRPixel,
318 sizeof(Pixel),
319 XtOffsetOf(gui_T, def_back_pixel),
320 XtRString,
321 XtDefaultBackground
322 },
323 {
324 XtNfont,
325 XtCFont,
326 XtRString,
327 sizeof(String *),
328 XtOffsetOf(gui_T, rsrc_font_name),
329 XtRImmediate,
330 XtDefaultFont
331 },
332 {
333 XtNboldFont,
334 XtCBoldFont,
335 XtRString,
336 sizeof(String *),
337 XtOffsetOf(gui_T, rsrc_bold_font_name),
338 XtRImmediate,
339 ""
340 },
341 {
342 XtNitalicFont,
343 XtCItalicFont,
344 XtRString,
345 sizeof(String *),
346 XtOffsetOf(gui_T, rsrc_ital_font_name),
347 XtRImmediate,
348 ""
349 },
350 {
351 XtNboldItalicFont,
352 XtCBoldItalicFont,
353 XtRString,
354 sizeof(String *),
355 XtOffsetOf(gui_T, rsrc_boldital_font_name),
356 XtRImmediate,
357 ""
358 },
359 {
360 XtNgeometry,
361 XtCGeometry,
362 XtRString,
363 sizeof(String *),
364 XtOffsetOf(gui_T, geom),
365 XtRImmediate,
366 ""
367 },
368 {
369 XtNreverseVideo,
370 XtCReverseVideo,
371 XtRBool,
372 sizeof(Bool),
373 XtOffsetOf(gui_T, rsrc_rev_video),
374 XtRImmediate,
375 (XtPointer)False
376 },
377 {
378 XtNborderWidth,
379 XtCBorderWidth,
380 XtRInt,
381 sizeof(int),
382 XtOffsetOf(gui_T, border_width),
383 XtRImmediate,
384 (XtPointer)2
385 },
386 {
387 XtNscrollbarWidth,
388 XtCScrollbarWidth,
389 XtRInt,
390 sizeof(int),
391 XtOffsetOf(gui_T, scrollbar_width),
392 XtRImmediate,
393 (XtPointer)SB_DEFAULT_WIDTH
394 },
395#ifdef FEAT_MENU
396# ifdef FEAT_GUI_ATHENA /* with Motif the height is always computed */
397 {
398 XtNmenuHeight,
399 XtCMenuHeight,
400 XtRInt,
401 sizeof(int),
402 XtOffsetOf(gui_T, menu_height),
403 XtRImmediate,
404 (XtPointer)MENU_DEFAULT_HEIGHT /* Should figure out at run time */
405 },
406# endif
407 {
408# ifdef FONTSET_ALWAYS
409 XtNmenuFontSet,
410 XtCMenuFontSet,
411#else
412 XtNmenuFont,
413 XtCMenuFont,
414#endif
415 XtRString,
416 sizeof(char *),
417 XtOffsetOf(gui_T, rsrc_menu_font_name),
418 XtRString,
419 DFLT_MENU_FONT
420 },
421#endif
422 {
423 XtNmenuForeground,
424 XtCMenuForeground,
425 XtRString,
426 sizeof(char *),
427 XtOffsetOf(gui_T, rsrc_menu_fg_name),
428 XtRString,
429 DFLT_MENU_FG_COLOR
430 },
431 {
432 XtNmenuBackground,
433 XtCMenuBackground,
434 XtRString,
435 sizeof(char *),
436 XtOffsetOf(gui_T, rsrc_menu_bg_name),
437 XtRString,
438 DFLT_MENU_BG_COLOR
439 },
440 {
441 XtNscrollForeground,
442 XtCScrollForeground,
443 XtRString,
444 sizeof(char *),
445 XtOffsetOf(gui_T, rsrc_scroll_fg_name),
446 XtRString,
447 DFLT_SCROLL_FG_COLOR
448 },
449 {
450 XtNscrollBackground,
451 XtCScrollBackground,
452 XtRString,
453 sizeof(char *),
454 XtOffsetOf(gui_T, rsrc_scroll_bg_name),
455 XtRString,
456 DFLT_SCROLL_BG_COLOR
457 },
458#ifdef FEAT_BEVAL
459 {
460 XtNtooltipForeground,
461 XtCTooltipForeground,
462 XtRString,
463 sizeof(char *),
464 XtOffsetOf(gui_T, rsrc_tooltip_fg_name),
465 XtRString,
466 DFLT_TOOLTIP_FG_COLOR
467 },
468 {
469 XtNtooltipBackground,
470 XtCTooltipBackground,
471 XtRString,
472 sizeof(char *),
473 XtOffsetOf(gui_T, rsrc_tooltip_bg_name),
474 XtRString,
475 DFLT_TOOLTIP_BG_COLOR
476 },
477 {
478 XtNtooltipFont,
479 XtCTooltipFont,
480 XtRString,
481 sizeof(char *),
482 XtOffsetOf(gui_T, rsrc_tooltip_font_name),
483 XtRString,
484 DFLT_TOOLTIP_FONT
485 },
486 /* This one isn't really needed, keep for Sun Workshop? */
487 {
488 "balloonEvalFontSet",
489 XtCFontSet,
490 XtRFontSet,
491 sizeof(XFontSet),
492 XtOffsetOf(gui_T, tooltip_fontset),
493 XtRImmediate,
494 (XtPointer)NOFONTSET
495 },
496#endif /* FEAT_BEVAL */
497#ifdef FEAT_XIM
498 {
499 "preeditType",
500 "PreeditType",
501 XtRString,
502 sizeof(char*),
503 XtOffsetOf(gui_T, rsrc_preedit_type_name),
504 XtRString,
505 (XtPointer)"OverTheSpot,OffTheSpot,Root"
506 },
507 {
508 "inputMethod",
509 "InputMethod",
510 XtRString,
511 sizeof(char*),
512 XtOffsetOf(gui_T, rsrc_input_method),
513 XtRString,
514 NULL
515 },
516#endif /* FEAT_XIM */
517};
518
519/*
520 * This table holds all the X GUI command line options allowed. This includes
521 * the standard ones so that we can skip them when vim is started without the
522 * GUI (but the GUI might start up later).
523 * When changing this, also update doc/vim_gui.txt and the usage message!!!
524 */
525static XrmOptionDescRec cmdline_options[] =
526{
527 /* We handle these options ourselves */
528 {"-bg", ".background", XrmoptionSepArg, NULL},
529 {"-background", ".background", XrmoptionSepArg, NULL},
530 {"-fg", ".foreground", XrmoptionSepArg, NULL},
531 {"-foreground", ".foreground", XrmoptionSepArg, NULL},
532 {"-fn", ".font", XrmoptionSepArg, NULL},
533 {"-font", ".font", XrmoptionSepArg, NULL},
534 {"-boldfont", ".boldFont", XrmoptionSepArg, NULL},
535 {"-italicfont", ".italicFont", XrmoptionSepArg, NULL},
536 {"-geom", ".geometry", XrmoptionSepArg, NULL},
537 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
538 {"-reverse", "*reverseVideo", XrmoptionNoArg, "True"},
539 {"-rv", "*reverseVideo", XrmoptionNoArg, "True"},
540 {"+reverse", "*reverseVideo", XrmoptionNoArg, "False"},
541 {"+rv", "*reverseVideo", XrmoptionNoArg, "False"},
542 {"-display", ".display", XrmoptionSepArg, NULL},
Bram Moolenaara5792f52005-11-23 21:25:05 +0000543 {"-iconic", ".iconic", XrmoptionNoArg, "True"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 {"-name", ".name", XrmoptionSepArg, NULL},
545 {"-bw", ".borderWidth", XrmoptionSepArg, NULL},
546 {"-borderwidth", ".borderWidth", XrmoptionSepArg, NULL},
547 {"-sw", ".scrollbarWidth", XrmoptionSepArg, NULL},
548 {"-scrollbarwidth", ".scrollbarWidth", XrmoptionSepArg, NULL},
549 {"-mh", ".menuHeight", XrmoptionSepArg, NULL},
550 {"-menuheight", ".menuHeight", XrmoptionSepArg, NULL},
551#ifdef FONTSET_ALWAYS
552 {"-mf", ".menuFontSet", XrmoptionSepArg, NULL},
553 {"-menufont", ".menuFontSet", XrmoptionSepArg, NULL},
554 {"-menufontset", ".menuFontSet", XrmoptionSepArg, NULL},
555#else
556 {"-mf", ".menuFont", XrmoptionSepArg, NULL},
557 {"-menufont", ".menuFont", XrmoptionSepArg, NULL},
558#endif
559 {"-xrm", NULL, XrmoptionResArg, NULL}
560};
561
562static int gui_argc = 0;
563static char **gui_argv = NULL;
564
565/*
566 * Call-back routines.
567 */
568
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100570gui_x11_timer_cb(
571 XtPointer timed_out,
572 XtIntervalId *interval_id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573{
574 *((int *)timed_out) = TRUE;
575}
576
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100578gui_x11_visibility_cb(
579 Widget w UNUSED,
580 XtPointer dud UNUSED,
581 XEvent *event,
582 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583{
584 if (event->type != VisibilityNotify)
585 return;
586
587 gui.visibility = event->xvisibility.state;
588
589 /*
590 * When we do an XCopyArea(), and the window is partially obscured, we want
591 * to receive an event to tell us whether it worked or not.
592 */
593 XSetGraphicsExposures(gui.dpy, gui.text_gc,
594 gui.visibility != VisibilityUnobscured);
595
596 /* This is needed for when redrawing is slow. */
597 gui_mch_update();
598}
599
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100601gui_x11_expose_cb(
602 Widget w UNUSED,
603 XtPointer dud UNUSED,
604 XEvent *event,
605 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606{
607 XExposeEvent *gevent;
608 int new_x;
609
610 if (event->type != Expose)
611 return;
612
613 out_flush(); /* make sure all output has been processed */
614
615 gevent = (XExposeEvent *)event;
616 gui_redraw(gevent->x, gevent->y, gevent->width, gevent->height);
617
618 new_x = FILL_X(0);
619
620 /* Clear the border areas if needed */
621 if (gevent->x < new_x)
622 XClearArea(gui.dpy, gui.wid, 0, 0, new_x, 0, False);
623 if (gevent->y < FILL_Y(0))
624 XClearArea(gui.dpy, gui.wid, 0, 0, 0, FILL_Y(0), False);
625 if (gevent->x > FILL_X(Columns))
626 XClearArea(gui.dpy, gui.wid, FILL_X((int)Columns), 0, 0, 0, False);
627 if (gevent->y > FILL_Y(Rows))
628 XClearArea(gui.dpy, gui.wid, 0, FILL_Y((int)Rows), 0, 0, False);
629
630 /* This is needed for when redrawing is slow. */
631 gui_mch_update();
632}
633
Bram Moolenaar67c53842010-05-22 18:28:27 +0200634#if ((defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)) \
635 && defined(FEAT_GUI_MOTIF)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636/*
Bram Moolenaar231334e2005-07-25 20:46:57 +0000637 * This function fills in the XRectangle object with the current x,y
638 * coordinates and height, width so that an XtVaSetValues to the same shell of
Bram Moolenaar49325942007-05-10 19:19:59 +0000639 * those resources will restore the window to its former position and
Bram Moolenaar231334e2005-07-25 20:46:57 +0000640 * dimensions.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 *
Bram Moolenaar231334e2005-07-25 20:46:57 +0000642 * Note: This function may fail, in which case the XRectangle will be
643 * unchanged. Be sure to have the XRectangle set with the proper values for a
644 * failed condition prior to calling this function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 */
646 static void
647shellRectangle(Widget shell, XRectangle *r)
648{
649 Window rootw, shellw, child, parentw;
650 int absx, absy;
651 XWindowAttributes a;
652 Window *children;
653 unsigned int childrenCount;
654
655 shellw = XtWindow(shell);
656 if (shellw == 0)
657 return;
658 for (;;)
659 {
660 XQueryTree(XtDisplay(shell), shellw, &rootw, &parentw,
661 &children, &childrenCount);
662 XFree(children);
663 if (parentw == rootw)
664 break;
665 shellw = parentw;
666 }
667 XGetWindowAttributes(XtDisplay(shell), shellw, &a);
668 XTranslateCoordinates(XtDisplay(shell), shellw, a.root, 0, 0,
669 &absx, &absy, &child);
670 r->x = absx;
671 r->y = absy;
672 XtVaGetValues(shell, XmNheight, &r->height, XmNwidth, &r->width, NULL);
673}
674#endif
675
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100677gui_x11_resize_window_cb(
678 Widget w UNUSED,
679 XtPointer dud UNUSED,
680 XEvent *event,
681 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682{
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
Bram Moolenaar67c53842010-05-22 18:28:27 +0200708#if defined(FEAT_NETBEANS_INTG) && defined(FEAT_GUI_MOTIF)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200709 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 {
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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100723gui_x11_focus_change_cb(
724 Widget w UNUSED,
725 XtPointer data UNUSED,
726 XEvent *event,
727 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728{
729 gui_focus_change(event->type == FocusIn);
730}
731
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100733gui_x11_enter_cb(
734 Widget w UNUSED,
735 XtPointer data UNUSED,
736 XEvent *event UNUSED,
737 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738{
739 gui_focus_change(TRUE);
740}
741
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100743gui_x11_leave_cb(
744 Widget w UNUSED,
745 XtPointer data UNUSED,
746 XEvent *event UNUSED,
747 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748{
749 gui_focus_change(FALSE);
750}
751
752#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
753# if X_HAVE_UTF8_STRING
754# define USE_UTF8LOOKUP
755# endif
756#endif
757
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100759gui_x11_key_hit_cb(
760 Widget w UNUSED,
761 XtPointer dud UNUSED,
762 XEvent *event,
763 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764{
765 XKeyPressedEvent *ev_press;
766#ifdef FEAT_XIM
767 char_u string2[256];
768 char_u string_shortbuf[256];
769 char_u *string = string_shortbuf;
770 Boolean string_alloced = False;
771 Status status;
772#else
773 char_u string[4], string2[3];
774#endif
775 KeySym key_sym, key_sym2;
776 int len, len2;
777 int i;
778 int modifiers;
779 int key;
780
781 ev_press = (XKeyPressedEvent *)event;
782
783#ifdef FEAT_XIM
784 if (xic)
785 {
786# ifdef USE_UTF8LOOKUP
787 /* XFree86 4.0.2 or newer: Be able to get UTF-8 characters even when
788 * the locale isn't utf-8. */
789 if (enc_utf8)
790 len = Xutf8LookupString(xic, ev_press, (char *)string,
791 sizeof(string_shortbuf), &key_sym, &status);
792 else
793# endif
794 len = XmbLookupString(xic, ev_press, (char *)string,
795 sizeof(string_shortbuf), &key_sym, &status);
796 if (status == XBufferOverflow)
797 {
798 string = (char_u *)XtMalloc(len + 1);
799 string_alloced = True;
800# ifdef USE_UTF8LOOKUP
801 /* XFree86 4.0.2 or newer: Be able to get UTF-8 characters even
802 * when the locale isn't utf-8. */
803 if (enc_utf8)
804 len = Xutf8LookupString(xic, ev_press, (char *)string,
805 len, &key_sym, &status);
806 else
807# endif
808 len = XmbLookupString(xic, ev_press, (char *)string,
809 len, &key_sym, &status);
810 }
811 if (status == XLookupNone || status == XLookupChars)
812 key_sym = XK_VoidSymbol;
813
814# ifdef FEAT_MBYTE
815 /* Do conversion from 'termencoding' to 'encoding'. When using
816 * Xutf8LookupString() it has already been done. */
817 if (len > 0 && input_conv.vc_type != CONV_NONE
818# ifdef USE_UTF8LOOKUP
819 && !enc_utf8
820# endif
821 )
822 {
823 int maxlen = len * 4 + 40; /* guessed */
824 char_u *p = (char_u *)XtMalloc(maxlen);
825
826 mch_memmove(p, string, len);
827 if (string_alloced)
828 XtFree((char *)string);
829 string = p;
830 string_alloced = True;
831 len = convert_input(p, len, maxlen);
832 }
833# endif
834
835 /* Translate CSI to K_CSI, otherwise it could be recognized as the
836 * start of a special key. */
837 for (i = 0; i < len; ++i)
838 if (string[i] == CSI)
839 {
840 char_u *p = (char_u *)XtMalloc(len + 3);
841
842 mch_memmove(p, string, i + 1);
843 p[i + 1] = KS_EXTRA;
844 p[i + 2] = (int)KE_CSI;
845 mch_memmove(p + i + 3, string + i + 1, len - i);
846 if (string_alloced)
847 XtFree((char *)string);
848 string = p;
849 string_alloced = True;
850 i += 2;
851 len += 2;
852 }
853 }
854 else
855#endif
856 len = XLookupString(ev_press, (char *)string, sizeof(string),
857 &key_sym, NULL);
858
859#ifdef SunXK_F36
860 /*
861 * These keys have bogus lookup strings, and trapping them here is
862 * easier than trying to XRebindKeysym() on them with every possible
863 * combination of modifiers.
864 */
865 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
866 len = 0;
867#endif
868
869#ifdef FEAT_HANGULIN
870 if ((key_sym == XK_space) && (ev_press->state & ShiftMask))
871 {
872 hangul_input_state_toggle();
873 goto theend;
874 }
875#endif
876
877 if (key_sym == XK_space)
878 string[0] = ' '; /* Otherwise Ctrl-Space doesn't work */
879
880 /*
881 * Only on some machines ^_ requires Ctrl+Shift+minus. For consistency,
882 * allow just Ctrl+minus too.
883 */
884 if (key_sym == XK_minus && (ev_press->state & ControlMask))
885 string[0] = Ctrl__;
886
887#ifdef XK_ISO_Left_Tab
888 /* why do we get XK_ISO_Left_Tab instead of XK_Tab for shift-tab? */
889 if (key_sym == XK_ISO_Left_Tab)
890 {
891 key_sym = XK_Tab;
892 string[0] = TAB;
893 len = 1;
894 }
895#endif
896
897 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
898 * that already has the 8th bit set. And not when using a double-byte
899 * encoding, setting the 8th bit may make it the lead byte of a
900 * double-byte character. */
901 if (len == 1
902 && (ev_press->state & Mod1Mask)
903 && !(key_sym == XK_BackSpace || key_sym == XK_Delete)
904 && (string[0] & 0x80) == 0
905#ifdef FEAT_MBYTE
906 && !enc_dbcs
907#endif
908 )
909 {
910#if defined(FEAT_MENU) && defined(FEAT_GUI_MOTIF)
911 /* Ignore ALT keys when they are used for the menu only */
912 if (gui.menu_is_active
913 && (p_wak[0] == 'y'
914 || (p_wak[0] == 'm' && gui_is_menu_shortcut(string[0]))))
915 goto theend;
916#endif
917 /*
918 * Before we set the 8th bit, check to make sure the user doesn't
919 * already have a mapping defined for this sequence. We determine this
920 * by checking to see if the input would be the same without the
921 * Alt/Meta key.
922 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
923 */
924 ev_press->state &= ~Mod1Mask;
925 len2 = XLookupString(ev_press, (char *)string2, sizeof(string2),
926 &key_sym2, NULL);
927 if (key_sym2 == XK_space)
928 string2[0] = ' '; /* Otherwise Meta-Ctrl-Space doesn't work */
929 if ( len2 == 1
930 && string[0] == string2[0]
931 && !(key_sym == XK_Tab && (ev_press->state & ShiftMask)))
932 {
933 string[0] |= 0x80;
934#ifdef FEAT_MBYTE
935 if (enc_utf8) /* convert to utf-8 */
936 {
937 string[1] = string[0] & 0xbf;
938 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
939 if (string[1] == CSI)
940 {
941 string[2] = KS_EXTRA;
942 string[3] = (int)KE_CSI;
943 len = 4;
944 }
945 else
946 len = 2;
947 }
948#endif
949 }
950 else
951 ev_press->state |= Mod1Mask;
952 }
953
954 if (len == 1 && string[0] == CSI)
955 {
956 string[1] = KS_EXTRA;
957 string[2] = (int)KE_CSI;
958 len = -3;
959 }
960
961 /* Check for special keys. Also do this when len == 1 (key has an ASCII
962 * value) to detect backspace, delete and keypad keys. */
963 if (len == 0 || len == 1)
964 {
965 for (i = 0; special_keys[i].key_sym != (KeySym)0; i++)
966 {
967 if (special_keys[i].key_sym == key_sym)
968 {
969 string[0] = CSI;
970 string[1] = special_keys[i].vim_code0;
971 string[2] = special_keys[i].vim_code1;
972 len = -3;
973 break;
974 }
975 }
976 }
977
978 /* Unrecognised key is ignored. */
979 if (len == 0)
980 goto theend;
981
982 /* Special keys (and a few others) may have modifiers. Also when using a
983 * double-byte encoding (can't set the 8th bit). */
984 if (len == -3 || key_sym == XK_space || key_sym == XK_Tab
985 || key_sym == XK_Return || key_sym == XK_Linefeed
986 || key_sym == XK_Escape
987#ifdef FEAT_MBYTE
988 || (enc_dbcs && len == 1 && (ev_press->state & Mod1Mask))
989#endif
990 )
991 {
992 modifiers = 0;
993 if (ev_press->state & ShiftMask)
994 modifiers |= MOD_MASK_SHIFT;
995 if (ev_press->state & ControlMask)
996 modifiers |= MOD_MASK_CTRL;
997 if (ev_press->state & Mod1Mask)
998 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000999 if (ev_press->state & Mod4Mask)
1000 modifiers |= MOD_MASK_META;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001
1002 /*
1003 * For some keys a shift modifier is translated into another key
1004 * code.
1005 */
1006 if (len == -3)
1007 key = TO_SPECIAL(string[1], string[2]);
1008 else
1009 key = string[0];
1010 key = simplify_key(key, &modifiers);
1011 if (key == CSI)
1012 key = K_CSI;
1013 if (IS_SPECIAL(key))
1014 {
1015 string[0] = CSI;
1016 string[1] = K_SECOND(key);
1017 string[2] = K_THIRD(key);
1018 len = 3;
1019 }
1020 else
1021 {
1022 string[0] = key;
1023 len = 1;
1024 }
1025
1026 if (modifiers != 0)
1027 {
1028 string2[0] = CSI;
1029 string2[1] = KS_MODIFIER;
1030 string2[2] = modifiers;
1031 add_to_input_buf(string2, 3);
1032 }
1033 }
1034
1035 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1036#ifdef UNIX
1037 || (intr_char != 0 && string[0] == intr_char)
1038#endif
1039 ))
1040 {
1041 trash_input_buf();
1042 got_int = TRUE;
1043 }
1044
1045 add_to_input_buf(string, len);
1046
1047 /*
1048 * blank out the pointer if necessary
1049 */
1050 if (p_mh)
1051 gui_mch_mousehide(TRUE);
1052
1053#if defined(FEAT_BEVAL_TIP)
1054 {
1055 BalloonEval *be;
1056
1057 if ((be = gui_mch_currently_showing_beval()) != NULL)
1058 gui_mch_unpost_balloon(be);
1059 }
1060#endif
1061theend:
1062 {} /* some compilers need a statement here */
1063#ifdef FEAT_XIM
1064 if (string_alloced)
1065 XtFree((char *)string);
1066#endif
1067}
1068
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001070gui_x11_mouse_cb(
1071 Widget w UNUSED,
1072 XtPointer dud UNUSED,
1073 XEvent *event,
1074 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075{
1076 static XtIntervalId timer = (XtIntervalId)0;
1077 static int timed_out = TRUE;
1078
1079 int button;
1080 int repeated_click = FALSE;
1081 int x, y;
1082 int_u x_modifiers;
1083 int_u vim_modifiers;
1084
1085 if (event->type == MotionNotify)
1086 {
1087 /* Get the latest position, avoids lagging behind on a drag. */
1088 x = event->xmotion.x;
1089 y = event->xmotion.y;
1090 x_modifiers = event->xmotion.state;
1091 button = (x_modifiers & (Button1Mask | Button2Mask | Button3Mask))
1092 ? MOUSE_DRAG : ' ';
1093
1094 /*
1095 * if our pointer is currently hidden, then we should show it.
1096 */
1097 gui_mch_mousehide(FALSE);
1098
1099 if (button != MOUSE_DRAG) /* just moving the rodent */
1100 {
1101#ifdef FEAT_MENU
1102 if (dud) /* moved in vimForm */
1103 y -= gui.menu_height;
1104#endif
1105 gui_mouse_moved(x, y);
1106 return;
1107 }
1108 }
1109 else
1110 {
1111 x = event->xbutton.x;
1112 y = event->xbutton.y;
1113 if (event->type == ButtonPress)
1114 {
1115 /* Handle multiple clicks */
1116 if (!timed_out)
1117 {
1118 XtRemoveTimeOut(timer);
1119 repeated_click = TRUE;
1120 }
1121 timed_out = FALSE;
1122 timer = XtAppAddTimeOut(app_context, (long_u)p_mouset,
1123 gui_x11_timer_cb, &timed_out);
1124 switch (event->xbutton.button)
1125 {
Bram Moolenaar88e484b2015-11-24 15:38:44 +01001126 /* keep in sync with gui_gtk_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 case Button1: button = MOUSE_LEFT; break;
1128 case Button2: button = MOUSE_MIDDLE; break;
1129 case Button3: button = MOUSE_RIGHT; break;
1130 case Button4: button = MOUSE_4; break;
1131 case Button5: button = MOUSE_5; break;
Bram Moolenaar88e484b2015-11-24 15:38:44 +01001132 case 6: button = MOUSE_7; break;
1133 case 7: button = MOUSE_6; break;
1134 case 8: button = MOUSE_X1; break;
1135 case 9: button = MOUSE_X2; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 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
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162/*
1163 * End of call-back routines
1164 */
1165
1166/*
1167 * Parse the GUI related command-line arguments. Any arguments used are
1168 * deleted from argv, and *argc is decremented accordingly. This is called
1169 * when vim is started, whether or not the GUI has been started.
1170 */
1171 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001172gui_mch_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173{
1174 int arg;
1175 int i;
1176
1177 /*
1178 * Move all the entries in argv which are relevant to X into gui_argv.
1179 */
1180 gui_argc = 0;
1181 gui_argv = (char **)lalloc((long_u)(*argc * sizeof(char *)), FALSE);
1182 if (gui_argv == NULL)
1183 return;
1184 gui_argv[gui_argc++] = argv[0];
1185 arg = 1;
1186 while (arg < *argc)
1187 {
1188 /* Look for argv[arg] in cmdline_options[] table */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001189 for (i = 0; i < (int)XtNumber(cmdline_options); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (strcmp(argv[arg], cmdline_options[i].option) == 0)
1191 break;
1192
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001193 if (i < (int)XtNumber(cmdline_options))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 {
1195 /* Remember finding "-rv" or "-reverse" */
1196 if (strcmp("-rv", argv[arg]) == 0
1197 || strcmp("-reverse", argv[arg]) == 0)
1198 found_reverse_arg = TRUE;
1199 else if ((strcmp("-fn", argv[arg]) == 0
1200 || strcmp("-font", argv[arg]) == 0)
1201 && arg + 1 < *argc)
1202 font_argument = argv[arg + 1];
1203
1204 /* Found match in table, so move it into gui_argv */
1205 gui_argv[gui_argc++] = argv[arg];
1206 if (--*argc > arg)
1207 {
1208 mch_memmove(&argv[arg], &argv[arg + 1], (*argc - arg)
1209 * sizeof(char *));
1210 if (cmdline_options[i].argKind != XrmoptionNoArg)
1211 {
1212 /* Move the options argument as well */
1213 gui_argv[gui_argc++] = argv[arg];
1214 if (--*argc > arg)
1215 mch_memmove(&argv[arg], &argv[arg + 1], (*argc - arg)
1216 * sizeof(char *));
1217 }
1218 }
1219 argv[*argc] = NULL;
1220 }
1221 else
1222#ifdef FEAT_SUN_WORKSHOP
1223 if (strcmp("-ws", argv[arg]) == 0)
1224 {
1225 usingSunWorkShop++;
1226 p_acd = TRUE;
1227 gui.dofork = FALSE; /* don't fork() when starting GUI */
1228 mch_memmove(&argv[arg], &argv[arg + 1],
1229 (--*argc - arg) * sizeof(char *));
1230 argv[*argc] = NULL;
1231# ifdef WSDEBUG
1232 wsdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
1233 wsdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
1234# endif
1235 }
1236 else
1237#endif
1238#ifdef FEAT_NETBEANS_INTG
1239 if (strncmp("-nb", argv[arg], 3) == 0)
1240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 gui.dofork = FALSE; /* don't fork() when starting GUI */
1242 netbeansArg = argv[arg];
1243 mch_memmove(&argv[arg], &argv[arg + 1],
1244 (--*argc - arg) * sizeof(char *));
1245 argv[*argc] = NULL;
1246 }
1247 else
1248#endif
1249 arg++;
1250 }
1251}
1252
1253#ifndef XtSpecificationRelease
1254# define CARDINAL (Cardinal *)
1255#else
1256# if XtSpecificationRelease == 4
1257# define CARDINAL (Cardinal *)
1258# else
1259# define CARDINAL (int *)
1260# endif
1261#endif
1262
1263/*
1264 * Check if the GUI can be started. Called before gvimrc is sourced.
1265 * Return OK or FAIL.
1266 */
1267 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001268gui_mch_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269{
1270#ifdef FEAT_XIM
1271 XtSetLanguageProc(NULL, NULL, NULL);
1272#endif
1273 open_app_context();
1274 if (app_context != NULL)
1275 gui.dpy = XtOpenDisplay(app_context, 0, VIM_NAME, VIM_CLASS,
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00001276 cmdline_options, XtNumber(cmdline_options),
1277 CARDINAL &gui_argc, gui_argv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278
1279 if (app_context == NULL || gui.dpy == NULL)
1280 {
1281 gui.dying = TRUE;
1282 EMSG(_(e_opendisp));
1283 return FAIL;
1284 }
1285 return OK;
1286}
1287
1288
1289#ifdef USE_XSMP
1290/*
1291 * Handle XSMP processing, de-registering the attachment upon error
1292 */
1293static XtInputId _xsmp_xtinputid;
1294
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001295static void local_xsmp_handle_requests(XtPointer c, int *s, XtInputId *i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001298local_xsmp_handle_requests(
1299 XtPointer c UNUSED,
1300 int *s UNUSED,
1301 XtInputId *i UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302{
1303 if (xsmp_handle_requests() == FAIL)
1304 XtRemoveInput(_xsmp_xtinputid);
1305}
1306#endif
1307
1308
1309/*
1310 * Initialise the X GUI. Create all the windows, set up all the call-backs etc.
1311 * Returns OK for success, FAIL when the GUI can't be started.
1312 */
1313 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001314gui_mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315{
1316 XtGCMask gc_mask;
1317 XGCValues gc_vals;
1318 int x, y, mask;
1319 unsigned w, h;
1320
1321#if 0
1322 /* Uncomment this to enable synchronous mode for debugging */
1323 XSynchronize(gui.dpy, True);
1324#endif
1325
1326 vimShell = XtVaAppCreateShell(VIM_NAME, VIM_CLASS,
1327 applicationShellWidgetClass, gui.dpy, NULL);
1328
1329 /*
1330 * Get the application resources
1331 */
1332 XtVaGetApplicationResources(vimShell, (XtPointer)&gui,
1333 vim_resources, XtNumber(vim_resources), NULL);
1334
1335 gui.scrollbar_height = gui.scrollbar_width;
1336
1337 /*
1338 * Get the colors ourselves. Using the automatic conversion doesn't
1339 * handle looking for approximate colors.
1340 */
1341 /* NOTE: These next few lines are an exact duplicate of gui_athena.c's
1342 * gui_mch_def_colors(). Why?
1343 */
1344 gui.menu_fg_pixel = gui_get_color((char_u *)gui.rsrc_menu_fg_name);
1345 gui.menu_bg_pixel = gui_get_color((char_u *)gui.rsrc_menu_bg_name);
1346 gui.scroll_fg_pixel = gui_get_color((char_u *)gui.rsrc_scroll_fg_name);
1347 gui.scroll_bg_pixel = gui_get_color((char_u *)gui.rsrc_scroll_bg_name);
1348#ifdef FEAT_BEVAL
1349 gui.tooltip_fg_pixel = gui_get_color((char_u *)gui.rsrc_tooltip_fg_name);
1350 gui.tooltip_bg_pixel = gui_get_color((char_u *)gui.rsrc_tooltip_bg_name);
1351#endif
1352
1353#if defined(FEAT_MENU) && defined(FEAT_GUI_ATHENA)
1354 /* If the menu height was set, don't change it at runtime */
1355 if (gui.menu_height != MENU_DEFAULT_HEIGHT)
1356 gui.menu_height_fixed = TRUE;
1357#endif
1358
1359 /* Set default foreground and background colours */
1360 gui.norm_pixel = gui.def_norm_pixel;
1361 gui.back_pixel = gui.def_back_pixel;
1362
1363 /* Check if reverse video needs to be applied (on Sun it's done by X) */
1364 if (gui.rsrc_rev_video && gui_get_lightness(gui.back_pixel)
1365 > gui_get_lightness(gui.norm_pixel))
1366 {
1367 gui.norm_pixel = gui.def_back_pixel;
1368 gui.back_pixel = gui.def_norm_pixel;
1369 gui.def_norm_pixel = gui.norm_pixel;
1370 gui.def_back_pixel = gui.back_pixel;
1371 }
1372
1373 /* Get the colors from the "Normal", "Tooltip", "Scrollbar" and "Menu"
1374 * group (set in syntax.c or in a vimrc file) */
1375 set_normal_colors();
1376
1377 /*
1378 * Check that none of the colors are the same as the background color
1379 */
1380 gui_check_colors();
1381
1382 /*
1383 * Set up the GCs. The font attributes will be set in gui_init_font().
1384 */
1385 gc_mask = GCForeground | GCBackground;
1386 gc_vals.foreground = gui.norm_pixel;
1387 gc_vals.background = gui.back_pixel;
1388 gui.text_gc = XtGetGC(vimShell, gc_mask, &gc_vals);
1389
1390 gc_vals.foreground = gui.back_pixel;
1391 gc_vals.background = gui.norm_pixel;
1392 gui.back_gc = XtGetGC(vimShell, gc_mask, &gc_vals);
1393
1394 gc_mask |= GCFunction;
1395 gc_vals.foreground = gui.norm_pixel ^ gui.back_pixel;
1396 gc_vals.background = gui.norm_pixel ^ gui.back_pixel;
1397 gc_vals.function = GXxor;
1398 gui.invert_gc = XtGetGC(vimShell, gc_mask, &gc_vals);
1399
1400 gui.visibility = VisibilityUnobscured;
1401 x11_setup_atoms(gui.dpy);
1402
1403 if (gui_win_x != -1 && gui_win_y != -1)
1404 gui_mch_set_winpos(gui_win_x, gui_win_y);
1405
1406 /* Now adapt the supplied(?) geometry-settings */
1407 /* Added by Kjetil Jacobsen <kjetilja@stud.cs.uit.no> */
1408 if (gui.geom != NULL && *gui.geom != NUL)
1409 {
1410 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
1411 if (mask & WidthValue)
1412 Columns = w;
1413 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00001414 {
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001415 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00001416 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00001418 }
Bram Moolenaarc33916a2013-07-01 21:43:08 +02001419 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 /*
1421 * Set the (x,y) position of the main window only if specified in the
1422 * users geometry, so we get good defaults when they don't. This needs
1423 * to be done before the shell is popped up.
1424 */
1425 if (mask & (XValue|YValue))
1426 XtVaSetValues(vimShell, XtNgeometry, gui.geom, NULL);
1427 }
1428
1429 gui_x11_create_widgets();
1430
1431 /*
1432 * Add an icon to Vim (Marcel Douben: 11 May 1998).
1433 */
1434 if (vim_strchr(p_go, GO_ICON) != NULL)
1435 {
1436#ifndef HAVE_XPM
1437# include "vim_icon.xbm"
1438# include "vim_mask.xbm"
1439
1440 Arg arg[2];
1441
1442 XtSetArg(arg[0], XtNiconPixmap,
1443 XCreateBitmapFromData(gui.dpy,
1444 DefaultRootWindow(gui.dpy),
1445 (char *)vim_icon_bits,
1446 vim_icon_width,
1447 vim_icon_height));
1448 XtSetArg(arg[1], XtNiconMask,
1449 XCreateBitmapFromData(gui.dpy,
1450 DefaultRootWindow(gui.dpy),
1451 (char *)vim_mask_icon_bits,
1452 vim_mask_icon_width,
1453 vim_mask_icon_height));
1454 XtSetValues(vimShell, arg, (Cardinal)2);
1455#else
1456/* Use Pixmaps, looking much nicer. */
1457
1458/* If you get an error message here, you still need to unpack the runtime
1459 * archive! */
1460# ifdef magick
1461# undef magick
1462# endif
1463# define magick vim32x32
1464# include "../runtime/vim32x32.xpm"
1465# undef magick
1466# define magick vim16x16
1467# include "../runtime/vim16x16.xpm"
1468# undef magick
1469# define magick vim48x48
1470# include "../runtime/vim48x48.xpm"
1471# undef magick
1472
1473 static Pixmap icon = 0;
1474 static Pixmap icon_mask = 0;
1475 static char **magick = vim32x32;
1476 Window root_window;
1477 XIconSize *size;
1478 int number_sizes;
1479 Display *dsp;
1480 Screen *scr;
1481 XpmAttributes attr;
1482 Colormap cmap;
1483
1484 /*
1485 * Adjust the icon to the preferences of the actual window manager.
1486 */
1487 root_window = XRootWindowOfScreen(XtScreen(vimShell));
1488 if (XGetIconSizes(XtDisplay(vimShell), root_window,
1489 &size, &number_sizes) != 0)
1490 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 if (number_sizes > 0)
1492 {
Bram Moolenaar6f292662013-07-14 15:06:50 +02001493 if (size->max_height >= 48 && size->max_width >= 48)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 magick = vim48x48;
Bram Moolenaar6f292662013-07-14 15:06:50 +02001495 else if (size->max_height >= 32 && size->max_width >= 32)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 magick = vim32x32;
Bram Moolenaar6f292662013-07-14 15:06:50 +02001497 else if (size->max_height >= 16 && size->max_width >= 16)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 magick = vim16x16;
1499 }
1500 }
1501
1502 dsp = XtDisplay(vimShell);
1503 scr = XtScreen(vimShell);
1504
1505 cmap = DefaultColormap(dsp, DefaultScreen(dsp));
1506 XtVaSetValues(vimShell, XtNcolormap, cmap, NULL);
1507
1508 attr.valuemask = 0L;
1509 attr.valuemask = XpmCloseness | XpmReturnPixels | XpmColormap | XpmDepth;
1510 attr.closeness = 65535; /* accuracy isn't crucial */
1511 attr.colormap = cmap;
1512 attr.depth = DefaultDepthOfScreen(scr);
1513
1514 if (!icon)
Bram Moolenaare8208012008-06-20 09:59:25 +00001515 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 XpmCreatePixmapFromData(dsp, root_window, magick, &icon,
1517 &icon_mask, &attr);
Bram Moolenaare8208012008-06-20 09:59:25 +00001518 XpmFreeAttributes(&attr);
1519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520
1521# ifdef FEAT_GUI_ATHENA
1522 XtVaSetValues(vimShell, XtNiconPixmap, icon, XtNiconMask, icon_mask, NULL);
1523# else
1524 XtVaSetValues(vimShell, XmNiconPixmap, icon, XmNiconMask, icon_mask, NULL);
1525# endif
1526#endif
1527 }
1528
1529 if (gui.color_approx)
1530 EMSG(_("Vim E458: Cannot allocate colormap entry, some colors may be incorrect"));
1531
1532#ifdef FEAT_SUN_WORKSHOP
1533 if (usingSunWorkShop)
1534 workshop_connect(app_context);
1535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536
1537#ifdef FEAT_BEVAL
1538 gui_init_tooltip_font();
1539#endif
1540#ifdef FEAT_MENU
1541 gui_init_menu_font();
1542#endif
1543
1544#ifdef USE_XSMP
1545 /* Attach listener on ICE connection */
1546 if (-1 != xsmp_icefd)
1547 _xsmp_xtinputid = XtAppAddInput(app_context, xsmp_icefd,
1548 (XtPointer)XtInputReadMask, local_xsmp_handle_requests, NULL);
1549#endif
1550
1551 return OK;
1552}
1553
1554/*
1555 * Called when starting the GUI fails after calling gui_mch_init().
1556 */
1557 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001558gui_mch_uninit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559{
1560 gui_x11_destroy_widgets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 XtCloseDisplay(gui.dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 gui.dpy = NULL;
1563 vimShell = (Widget)0;
Bram Moolenaare4bfca82009-02-24 03:12:40 +00001564 vim_free(gui_argv);
1565 gui_argv = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566}
1567
1568/*
1569 * Called when the foreground or background color has been changed.
1570 */
1571 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001572gui_mch_new_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573{
1574 long_u gc_mask;
1575 XGCValues gc_vals;
1576
1577 gc_mask = GCForeground | GCBackground;
1578 gc_vals.foreground = gui.norm_pixel;
1579 gc_vals.background = gui.back_pixel;
1580 if (gui.text_gc != NULL)
1581 XChangeGC(gui.dpy, gui.text_gc, gc_mask, &gc_vals);
1582
1583 gc_vals.foreground = gui.back_pixel;
1584 gc_vals.background = gui.norm_pixel;
1585 if (gui.back_gc != NULL)
1586 XChangeGC(gui.dpy, gui.back_gc, gc_mask, &gc_vals);
1587
1588 gc_mask |= GCFunction;
1589 gc_vals.foreground = gui.norm_pixel ^ gui.back_pixel;
1590 gc_vals.background = gui.norm_pixel ^ gui.back_pixel;
1591 gc_vals.function = GXxor;
1592 if (gui.invert_gc != NULL)
1593 XChangeGC(gui.dpy, gui.invert_gc, gc_mask, &gc_vals);
1594
1595 gui_x11_set_back_color();
1596}
1597
1598/*
1599 * Open the GUI window which was created by a call to gui_mch_init().
1600 */
1601 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001602gui_mch_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
1604 /* Actually open the window */
Bram Moolenaara5792f52005-11-23 21:25:05 +00001605 XtRealizeWidget(vimShell);
1606 XtManageChild(XtNameToWidget(vimShell, "*vimForm"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607
1608 gui.wid = gui_x11_get_wid();
1609 gui.blank_pointer = gui_x11_create_blank_mouse();
1610
1611 /*
1612 * Add a callback for the Close item on the window managers menu, and the
1613 * save-yourself event.
1614 */
1615 wm_atoms[SAVE_YOURSELF_IDX] =
1616 XInternAtom(gui.dpy, "WM_SAVE_YOURSELF", False);
1617 wm_atoms[DELETE_WINDOW_IDX] =
1618 XInternAtom(gui.dpy, "WM_DELETE_WINDOW", False);
1619 XSetWMProtocols(gui.dpy, XtWindow(vimShell), wm_atoms, 2);
1620 XtAddEventHandler(vimShell, NoEventMask, True, gui_x11_wm_protocol_handler,
1621 NULL);
1622#ifdef HAVE_X11_XMU_EDITRES_H
1623 /*
1624 * Enable editres protocol (see "man editres").
1625 * Usually will need to add -lXmu to the linker line as well.
1626 */
1627 XtAddEventHandler(vimShell, (EventMask)0, True, _XEditResCheckMessages,
1628 (XtPointer)NULL);
1629#endif
1630
1631#ifdef FEAT_CLIENTSERVER
1632 if (serverName == NULL && serverDelayedStartName != NULL)
1633 {
1634 /* This is a :gui command in a plain vim with no previous server */
1635 commWindow = XtWindow(vimShell);
1636 (void)serverRegisterName(gui.dpy, serverDelayedStartName);
1637 }
1638 else
1639 {
1640 /*
1641 * Cannot handle "widget-less" windows with XtProcessEvent() we'll
1642 * have to change the "server" registration to that of the main window
1643 * If we have not registered a name yet, remember the window
1644 */
1645 serverChangeRegisteredWindow(gui.dpy, XtWindow(vimShell));
1646 }
1647 XtAddEventHandler(vimShell, PropertyChangeMask, False,
1648 gui_x11_send_event_handler, NULL);
1649#endif
1650
1651
1652#if defined(FEAT_MENU) && defined(FEAT_GUI_ATHENA)
1653 /* The Athena GUI needs this again after opening the window */
1654 gui_position_menu();
1655# ifdef FEAT_TOOLBAR
1656 gui_mch_set_toolbar_pos(0, gui.menu_height, gui.menu_width,
1657 gui.toolbar_height);
1658# endif
1659#endif
1660
1661 /* Get the colors for the highlight groups (gui_check_colors() might have
1662 * changed them) */
1663 highlight_gui_started(); /* re-init colors and fonts */
1664
1665#ifdef FEAT_HANGULIN
1666 hangul_keyboard_set();
1667#endif
1668#ifdef FEAT_XIM
1669 xim_init();
1670#endif
1671#ifdef FEAT_SUN_WORKSHOP
1672 workshop_postinit();
1673#endif
1674
1675 return OK;
1676}
1677
1678#if defined(FEAT_BEVAL) || defined(PROTO)
1679/*
1680 * Convert the tooltip fontset name to an XFontSet.
1681 */
1682 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001683gui_init_tooltip_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684{
1685 XrmValue from, to;
1686
1687 from.addr = (char *)gui.rsrc_tooltip_font_name;
1688 from.size = strlen(from.addr);
1689 to.addr = (XtPointer)&gui.tooltip_fontset;
1690 to.size = sizeof(XFontSet);
1691
1692 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontSet, &to) == False)
1693 {
1694 /* Failed. What to do? */
1695 }
1696}
1697#endif
1698
1699#if defined(FEAT_MENU) || defined(PROTO)
1700/* Convert the menu font/fontset name to an XFontStruct/XFontset */
1701 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001702gui_init_menu_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703{
1704 XrmValue from, to;
1705
1706#ifdef FONTSET_ALWAYS
1707 from.addr = (char *)gui.rsrc_menu_font_name;
1708 from.size = strlen(from.addr);
1709 to.addr = (XtPointer)&gui.menu_fontset;
1710 to.size = sizeof(GuiFontset);
1711
1712 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontSet, &to) == False)
1713 {
1714 /* Failed. What to do? */
1715 }
1716#else
1717 from.addr = (char *)gui.rsrc_menu_font_name;
1718 from.size = strlen(from.addr);
1719 to.addr = (XtPointer)&gui.menu_font;
1720 to.size = sizeof(GuiFont);
1721
1722 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontStruct, &to) == False)
1723 {
1724 /* Failed. What to do? */
1725 }
1726#endif
1727}
1728#endif
1729
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001731gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732{
1733#if 0
1734 /* Lesstif gives an error message here, and so does Solaris. The man page
1735 * says that this isn't needed when exiting, so just skip it. */
1736 XtCloseDisplay(gui.dpy);
1737#endif
Bram Moolenaare4bfca82009-02-24 03:12:40 +00001738 vim_free(gui_argv);
1739 gui_argv = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740}
1741
1742/*
1743 * Get the position of the top left corner of the window.
1744 */
1745 int
Bram Moolenaar02fdaea2016-01-30 18:13:55 +01001746gui_mch_get_winpos(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747{
1748 Dimension xpos, ypos;
1749
1750 XtVaGetValues(vimShell,
1751 XtNx, &xpos,
1752 XtNy, &ypos,
1753 NULL);
1754 *x = xpos;
1755 *y = ypos;
1756 return OK;
1757}
1758
1759/*
1760 * Set the position of the top left corner of the window to the given
1761 * coordinates.
1762 */
1763 void
Bram Moolenaar02fdaea2016-01-30 18:13:55 +01001764gui_mch_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765{
1766 XtVaSetValues(vimShell,
1767 XtNx, x,
1768 XtNy, y,
1769 NULL);
1770}
1771
1772 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001773gui_mch_set_shellsize(
1774 int width,
1775 int height,
1776 int min_width,
1777 int min_height,
1778 int base_width,
1779 int base_height,
1780 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00001782#ifdef FEAT_XIM
1783 height += xim_get_status_area_height(),
1784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 XtVaSetValues(vimShell,
1786 XtNwidthInc, gui.char_width,
1787 XtNheightInc, gui.char_height,
1788#if defined(XtSpecificationRelease) && XtSpecificationRelease >= 4
1789 XtNbaseWidth, base_width,
1790 XtNbaseHeight, base_height,
1791#endif
1792 XtNminWidth, min_width,
1793 XtNminHeight, min_height,
1794 XtNwidth, width,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 XtNheight, height,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 NULL);
1797}
1798
1799/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00001800 * Allow 10 pixels for horizontal borders, 'guiheadroom' for vertical borders.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 * Is there no way in X to find out how wide the borders really are?
1802 */
1803 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001804gui_mch_get_screen_dimensions(
1805 int *screen_w,
1806 int *screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807{
1808 *screen_w = DisplayWidth(gui.dpy, DefaultScreen(gui.dpy)) - 10;
1809 *screen_h = DisplayHeight(gui.dpy, DefaultScreen(gui.dpy)) - p_ghr;
1810}
1811
1812/*
1813 * Initialise vim to use the font "font_name". If it's NULL, pick a default
1814 * font.
1815 * If "fontset" is TRUE, load the "font_name" as a fontset.
1816 * Return FAIL if the font could not be loaded, OK otherwise.
1817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001819gui_mch_init_font(
1820 char_u *font_name,
1821 int do_fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822{
1823 XFontStruct *font = NULL;
1824
1825#ifdef FEAT_XFONTSET
1826 XFontSet fontset = NULL;
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001829#ifdef FEAT_GUI_MOTIF
1830 /* A font name equal "*" is indicating, that we should activate the font
1831 * selection dialogue to get a new font name. So let us do it here. */
1832 if (font_name != NULL && STRCMP(font_name, "*") == 0)
1833 font_name = gui_xm_select_font(hl_get_font_name());
1834#endif
1835
1836#ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 if (do_fontset)
1838 {
1839 /* If 'guifontset' is set, VIM treats all font specifications as if
1840 * they were fontsets, and 'guifontset' becomes the default. */
1841 if (font_name != NULL)
1842 {
1843 fontset = (XFontSet)gui_mch_get_fontset(font_name, FALSE, TRUE);
1844 if (fontset == NULL)
1845 return FAIL;
1846 }
1847 }
1848 else
1849#endif
1850 {
1851 if (font_name == NULL)
1852 {
1853 /*
1854 * If none of the fonts in 'font' could be loaded, try the one set
1855 * in the X resource, and finally just try using DFLT_FONT, which
1856 * will hopefully always be there.
1857 */
1858 font_name = gui.rsrc_font_name;
1859 font = (XFontStruct *)gui_mch_get_font(font_name, FALSE);
1860 if (font == NULL)
1861 font_name = (char_u *)DFLT_FONT;
1862 }
1863 if (font == NULL)
1864 font = (XFontStruct *)gui_mch_get_font(font_name, FALSE);
1865 if (font == NULL)
1866 return FAIL;
1867 }
1868
1869 gui_mch_free_font(gui.norm_font);
1870#ifdef FEAT_XFONTSET
1871 gui_mch_free_fontset(gui.fontset);
1872
1873 if (fontset != NULL)
1874 {
1875 gui.norm_font = NOFONT;
1876 gui.fontset = (GuiFontset)fontset;
1877 gui.char_width = fontset_width(fontset);
1878 gui.char_height = fontset_height(fontset) + p_linespace;
1879 gui.char_ascent = fontset_ascent(fontset) + p_linespace / 2;
1880 }
1881 else
1882#endif
1883 {
1884 gui.norm_font = (GuiFont)font;
1885#ifdef FEAT_XFONTSET
1886 gui.fontset = NOFONTSET;
1887#endif
1888 gui.char_width = font->max_bounds.width;
1889 gui.char_height = font->ascent + font->descent + p_linespace;
1890 gui.char_ascent = font->ascent + p_linespace / 2;
1891 }
1892
1893 hl_set_font_name(font_name);
1894
1895 /*
1896 * Try to load other fonts for bold, italic, and bold-italic.
1897 * We should also try to work out what font to use for these when they are
1898 * not specified by X resources, but we don't yet.
1899 */
1900 if (font_name == gui.rsrc_font_name)
1901 {
1902 if (gui.bold_font == NOFONT
1903 && gui.rsrc_bold_font_name != NULL
1904 && *gui.rsrc_bold_font_name != NUL)
1905 gui.bold_font = gui_mch_get_font(gui.rsrc_bold_font_name, FALSE);
1906 if (gui.ital_font == NOFONT
1907 && gui.rsrc_ital_font_name != NULL
1908 && *gui.rsrc_ital_font_name != NUL)
1909 gui.ital_font = gui_mch_get_font(gui.rsrc_ital_font_name, FALSE);
1910 if (gui.boldital_font == NOFONT
1911 && gui.rsrc_boldital_font_name != NULL
1912 && *gui.rsrc_boldital_font_name != NUL)
1913 gui.boldital_font = gui_mch_get_font(gui.rsrc_boldital_font_name,
1914 FALSE);
1915 }
1916 else
1917 {
1918 /* When not using the font specified by the resources, also don't use
1919 * the bold/italic fonts, otherwise setting 'guifont' will look very
1920 * strange. */
1921 if (gui.bold_font != NOFONT)
1922 {
1923 XFreeFont(gui.dpy, (XFontStruct *)gui.bold_font);
1924 gui.bold_font = NOFONT;
1925 }
1926 if (gui.ital_font != NOFONT)
1927 {
1928 XFreeFont(gui.dpy, (XFontStruct *)gui.ital_font);
1929 gui.ital_font = NOFONT;
1930 }
1931 if (gui.boldital_font != NOFONT)
1932 {
1933 XFreeFont(gui.dpy, (XFontStruct *)gui.boldital_font);
1934 gui.boldital_font = NOFONT;
1935 }
1936 }
1937
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001938#ifdef FEAT_GUI_MOTIF
1939 gui_motif_synch_fonts();
1940#endif
1941
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 return OK;
1943}
1944
1945/*
1946 * Get a font structure for highlighting.
1947 */
1948 GuiFont
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001949gui_mch_get_font(char_u *name, int giveErrorIfMissing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950{
1951 XFontStruct *font;
1952
1953 if (!gui.in_use || name == NULL) /* can't do this when GUI not running */
1954 return NOFONT;
1955
1956 font = XLoadQueryFont(gui.dpy, (char *)name);
1957
1958 if (font == NULL)
1959 {
1960 if (giveErrorIfMissing)
1961 EMSG2(_(e_font), name);
1962 return NOFONT;
1963 }
1964
1965#ifdef DEBUG
1966 printf("Font Information for '%s':\n", name);
1967 printf(" w = %d, h = %d, ascent = %d, descent = %d\n",
1968 font->max_bounds.width, font->ascent + font->descent,
1969 font->ascent, font->descent);
1970 printf(" max ascent = %d, max descent = %d, max h = %d\n",
1971 font->max_bounds.ascent, font->max_bounds.descent,
1972 font->max_bounds.ascent + font->max_bounds.descent);
1973 printf(" min lbearing = %d, min rbearing = %d\n",
1974 font->min_bounds.lbearing, font->min_bounds.rbearing);
1975 printf(" max lbearing = %d, max rbearing = %d\n",
1976 font->max_bounds.lbearing, font->max_bounds.rbearing);
1977 printf(" leftink = %d, rightink = %d\n",
1978 (font->min_bounds.lbearing < 0),
1979 (font->max_bounds.rbearing > font->max_bounds.width));
1980 printf("\n");
1981#endif
1982
1983 if (font->max_bounds.width != font->min_bounds.width)
1984 {
1985 EMSG2(_(e_fontwidth), name);
1986 XFreeFont(gui.dpy, font);
1987 return NOFONT;
1988 }
1989 return (GuiFont)font;
1990}
1991
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001992#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00001993/*
1994 * Return the name of font "font" in allocated memory.
1995 * Don't know how to get the actual name, thus use the provided name.
1996 */
1997 char_u *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001998gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00001999{
2000 if (name == NULL)
2001 return NULL;
2002 return vim_strsave(name);
2003}
Bram Moolenaar567e4de2004-12-31 21:01:02 +00002004#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002005
Bram Moolenaar231334e2005-07-25 20:46:57 +00002006/*
2007 * Adjust gui.char_height (after 'linespace' was changed).
2008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002010gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011{
2012#ifdef FEAT_XFONTSET
2013 if (gui.fontset != NOFONTSET)
2014 {
2015 gui.char_height = fontset_height((XFontSet)gui.fontset) + p_linespace;
2016 gui.char_ascent = fontset_ascent((XFontSet)gui.fontset)
2017 + p_linespace / 2;
2018 }
2019 else
2020#endif
2021 {
2022 XFontStruct *font = (XFontStruct *)gui.norm_font;
2023
2024 gui.char_height = font->ascent + font->descent + p_linespace;
2025 gui.char_ascent = font->ascent + p_linespace / 2;
2026 }
2027 return OK;
2028}
2029
2030/*
2031 * Set the current text font.
2032 */
2033 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002034gui_mch_set_font(GuiFont font)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035{
2036 static Font prev_font = (Font)-1;
2037 Font fid = ((XFontStruct *)font)->fid;
2038
2039 if (fid != prev_font)
2040 {
2041 XSetFont(gui.dpy, gui.text_gc, fid);
2042 XSetFont(gui.dpy, gui.back_gc, fid);
2043 prev_font = fid;
2044 gui.char_ascent = ((XFontStruct *)font)->ascent + p_linespace / 2;
2045 }
2046#ifdef FEAT_XFONTSET
2047 current_fontset = (XFontSet)NULL;
2048#endif
2049}
2050
2051#if defined(FEAT_XFONTSET) || defined(PROTO)
2052/*
2053 * Set the current text fontset.
2054 * Adjust the ascent, in case it's different.
2055 */
2056 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002057gui_mch_set_fontset(GuiFontset fontset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058{
2059 current_fontset = (XFontSet)fontset;
2060 gui.char_ascent = fontset_ascent(current_fontset) + p_linespace / 2;
2061}
2062#endif
2063
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064/*
2065 * If a font is not going to be used, free its structure.
2066 */
2067 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002068gui_mch_free_font(GuiFont font)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069{
2070 if (font != NOFONT)
2071 XFreeFont(gui.dpy, (XFontStruct *)font);
2072}
2073
2074#if defined(FEAT_XFONTSET) || defined(PROTO)
2075/*
2076 * If a fontset is not going to be used, free its structure.
2077 */
2078 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002079gui_mch_free_fontset(GuiFontset fontset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080{
2081 if (fontset != NOFONTSET)
2082 XFreeFontSet(gui.dpy, (XFontSet)fontset);
2083}
2084
2085/*
2086 * Load the fontset "name".
2087 * Return a reference to the fontset, or NOFONTSET when failing.
2088 */
2089 GuiFontset
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002090gui_mch_get_fontset(
2091 char_u *name,
2092 int giveErrorIfMissing,
2093 int fixed_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094{
2095 XFontSet fontset;
2096 char **missing, *def_str;
2097 int num_missing;
2098
2099 if (!gui.in_use || name == NULL)
2100 return NOFONTSET;
2101
2102 fontset = XCreateFontSet(gui.dpy, (char *)name, &missing, &num_missing,
2103 &def_str);
2104 if (num_missing > 0)
2105 {
2106 int i;
2107
2108 if (giveErrorIfMissing)
2109 {
2110 EMSG2(_("E250: Fonts for the following charsets are missing in fontset %s:"), name);
2111 for (i = 0; i < num_missing; i++)
2112 EMSG2("%s", missing[i]);
2113 }
2114 XFreeStringList(missing);
2115 }
2116
2117 if (fontset == NULL)
2118 {
2119 if (giveErrorIfMissing)
2120 EMSG2(_(e_fontset), name);
2121 return NOFONTSET;
2122 }
2123
2124 if (fixed_width && check_fontset_sanity(fontset) == FAIL)
2125 {
2126 XFreeFontSet(gui.dpy, fontset);
2127 return NOFONTSET;
2128 }
2129 return (GuiFontset)fontset;
2130}
2131
2132/*
2133 * Check if fontset "fs" is fixed width.
2134 */
2135 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002136check_fontset_sanity(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137{
2138 XFontStruct **xfs;
2139 char **font_name;
2140 int fn;
2141 char *base_name;
2142 int i;
2143 int min_width;
2144 int min_font_idx = 0;
2145
2146 base_name = XBaseFontNameListOfFontSet(fs);
2147 fn = XFontsOfFontSet(fs, &xfs, &font_name);
2148 for (i = 0; i < fn; i++)
2149 {
2150 if (xfs[i]->max_bounds.width != xfs[i]->min_bounds.width)
2151 {
2152 EMSG2(_("E252: Fontset name: %s"), base_name);
2153 EMSG2(_("Font '%s' is not fixed-width"), font_name[i]);
2154 return FAIL;
2155 }
2156 }
2157 /* scan base font width */
2158 min_width = 32767;
2159 for (i = 0; i < fn; i++)
2160 {
2161 if (xfs[i]->max_bounds.width<min_width)
2162 {
2163 min_width = xfs[i]->max_bounds.width;
2164 min_font_idx = i;
2165 }
2166 }
2167 for (i = 0; i < fn; i++)
2168 {
2169 if ( xfs[i]->max_bounds.width != 2 * min_width
2170 && xfs[i]->max_bounds.width != min_width)
2171 {
Bram Moolenaar9d438d32013-06-13 21:57:20 +02002172 EMSG2(_("E253: Fontset name: %s"), base_name);
2173 EMSG2(_("Font0: %s"), font_name[min_font_idx]);
2174 EMSG2(_("Font1: %s"), font_name[i]);
2175 EMSGN(_("Font%ld width is not twice that of font0"), i);
2176 EMSGN(_("Font0 width: %ld"), xfs[min_font_idx]->max_bounds.width);
2177 EMSGN(_("Font1 width: %ld"), xfs[i]->max_bounds.width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 return FAIL;
2179 }
2180 }
2181 /* it seems ok. Good Luck!! */
2182 return OK;
2183}
2184
2185 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002186fontset_width(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187{
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002188 return XmbTextEscapement(fs, "Vim", 3) / 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189}
2190
2191 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002192fontset_height(
2193 XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194{
2195 XFontSetExtents *extents;
2196
2197 extents = XExtentsOfFontSet(fs);
2198 return extents->max_logical_extent.height;
2199}
2200
2201#if (defined(FONTSET_ALWAYS) && defined(FEAT_GUI_ATHENA) \
2202 && defined(FEAT_MENU)) || defined(PROTO)
2203/*
2204 * Returns the bounding box height around the actual glyph image of all
2205 * characters in all fonts of the fontset.
2206 */
2207 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002208fontset_height2(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209{
2210 XFontSetExtents *extents;
2211
2212 extents = XExtentsOfFontSet(fs);
2213 return extents->max_ink_extent.height;
2214}
2215#endif
2216
2217/* NOT USED YET
2218 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002219fontset_descent(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220{
2221 XFontSetExtents *extents;
2222
2223 extents = XExtentsOfFontSet (fs);
2224 return extents->max_logical_extent.height + extents->max_logical_extent.y;
2225}
2226*/
2227
2228 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002229fontset_ascent(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230{
2231 XFontSetExtents *extents;
2232
2233 extents = XExtentsOfFontSet(fs);
2234 return -extents->max_logical_extent.y;
2235}
2236
2237#endif /* FEAT_XFONTSET */
2238
2239/*
2240 * Return the Pixel value (color) for the given color name.
2241 * Return INVALCOLOR for error.
2242 */
2243 guicolor_T
Bram Moolenaar46582282016-07-23 14:35:12 +02002244gui_mch_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245{
Bram Moolenaar46582282016-07-23 14:35:12 +02002246 guicolor_T requested;
2247 XColor available;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 Colormap colormap;
Bram Moolenaar46582282016-07-23 14:35:12 +02002249#define COLORSPECBUFSIZE 8 /* space enough to hold "#RRGGBB" */
2250 char spec[COLORSPECBUFSIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251
2252 /* can't do this when GUI not running */
Bram Moolenaar46582282016-07-23 14:35:12 +02002253 if (!gui.in_use || name == NULL || *name == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 return INVALCOLOR;
2255
Bram Moolenaar46582282016-07-23 14:35:12 +02002256 requested = gui_get_color_cmn(name);
2257 if (requested == INVALCOLOR)
2258 return INVALCOLOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259
Bram Moolenaar46582282016-07-23 14:35:12 +02002260 vim_snprintf(spec, COLORSPECBUFSIZE, "#%.2x%.2x%.2x",
2261 (requested & 0xff0000) >> 16,
2262 (requested & 0xff00) >> 8,
2263 requested & 0xff);
2264#undef COLORSPECBUFSIZE
2265 colormap = DefaultColormap(gui.dpy, DefaultScreen(gui.dpy));
2266 if (XParseColor(gui.dpy, colormap, (char *)spec, &available) != 0
2267 && XAllocColor(gui.dpy, colormap, &available) != 0)
2268 return (guicolor_T)available.pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269
2270 return INVALCOLOR;
2271}
2272
2273/*
Bram Moolenaarfb269802005-03-15 22:46:30 +00002274 * Set the current text foreground color.
2275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002277gui_mch_set_fg_color(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278{
2279 if (color != prev_fg_color)
2280 {
2281 XSetForeground(gui.dpy, gui.text_gc, (Pixel)color);
2282 prev_fg_color = color;
2283 }
2284}
2285
2286/*
2287 * Set the current text background color.
2288 */
2289 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002290gui_mch_set_bg_color(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291{
2292 if (color != prev_bg_color)
2293 {
2294 XSetBackground(gui.dpy, gui.text_gc, (Pixel)color);
2295 prev_bg_color = color;
2296 }
2297}
2298
2299/*
Bram Moolenaarfb269802005-03-15 22:46:30 +00002300 * Set the current text special color.
2301 */
2302 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002303gui_mch_set_sp_color(guicolor_T color)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002304{
2305 prev_sp_color = color;
2306}
2307
2308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 * create a mouse pointer that is blank
2310 */
2311 static Cursor
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002312gui_x11_create_blank_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313{
2314 Pixmap blank_pixmap = XCreatePixmap(gui.dpy, gui.wid, 1, 1, 1);
2315 GC gc = XCreateGC(gui.dpy, blank_pixmap, (unsigned long)0, (XGCValues*)0);
2316 XDrawPoint(gui.dpy, blank_pixmap, gc, 0, 0);
2317 XFreeGC(gui.dpy, gc);
2318 return XCreatePixmapCursor(gui.dpy, blank_pixmap, blank_pixmap,
2319 (XColor*)&gui.norm_pixel, (XColor*)&gui.norm_pixel, 0, 0);
2320}
2321
Bram Moolenaarfb269802005-03-15 22:46:30 +00002322/*
2323 * Draw a curled line at the bottom of the character cell.
2324 */
2325 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002326draw_curl(int row, int col, int cells)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002327{
2328 int i;
2329 int offset;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002330 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarfb269802005-03-15 22:46:30 +00002331
2332 XSetForeground(gui.dpy, gui.text_gc, prev_sp_color);
2333 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
2334 {
2335 offset = val[i % 8];
2336 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, i,
2337 FILL_Y(row + 1) - 1 - offset);
2338 }
2339 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
2340}
2341
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002343gui_mch_draw_string(
2344 int row,
2345 int col,
2346 char_u *s,
2347 int len,
2348 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349{
2350 int cells = len;
2351#ifdef FEAT_MBYTE
Bram Moolenaara3227e22006-03-08 21:32:40 +00002352 static void *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 static int buflen = 0;
2354 char_u *p;
2355 int wlen = 0;
2356 int c;
2357
2358 if (enc_utf8)
2359 {
2360 /* Convert UTF-8 byte sequence to 16 bit characters for the X
2361 * functions. Need a buffer for the 16 bit characters. Keep it
2362 * between calls, because allocating it each time is slow. */
2363 if (buflen < len)
2364 {
2365 XtFree((char *)buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002366 buf = (void *)XtMalloc(len * (sizeof(XChar2b) < sizeof(wchar_t)
2367 ? sizeof(wchar_t) : sizeof(XChar2b)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 buflen = len;
2369 }
2370 p = s;
2371 cells = 0;
2372 while (p < s + len)
2373 {
2374 c = utf_ptr2char(p);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002375# ifdef FEAT_XFONTSET
2376 if (current_fontset != NULL)
2377 {
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002378# ifdef SMALL_WCHAR_T
2379 if (c >= 0x10000)
Bram Moolenaara3227e22006-03-08 21:32:40 +00002380 c = 0xbf; /* show chars > 0xffff as ? */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002381# endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00002382 ((wchar_t *)buf)[wlen] = c;
2383 }
2384 else
2385# endif
2386 {
2387 if (c >= 0x10000)
2388 c = 0xbf; /* show chars > 0xffff as ? */
2389 ((XChar2b *)buf)[wlen].byte1 = (unsigned)c >> 8;
2390 ((XChar2b *)buf)[wlen].byte2 = c;
2391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 ++wlen;
2393 cells += utf_char2cells(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002394 p += utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 }
2396 }
2397 else if (has_mbyte)
2398 {
2399 cells = 0;
2400 for (p = s; p < s + len; )
2401 {
2402 cells += ptr2cells(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002403 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 }
2405 }
2406
2407#endif
2408
2409#ifdef FEAT_XFONTSET
2410 if (current_fontset != NULL)
2411 {
2412 /* Setup a clip rectangle to avoid spilling over in the next or
2413 * previous line. This is apparently needed for some fonts which are
2414 * used in a fontset. */
2415 XRectangle clip;
2416
2417 clip.x = 0;
2418 clip.y = 0;
2419 clip.height = gui.char_height;
2420 clip.width = gui.char_width * cells + 1;
2421 XSetClipRectangles(gui.dpy, gui.text_gc, FILL_X(col), FILL_Y(row),
2422 &clip, 1, Unsorted);
2423 }
2424#endif
2425
2426 if (flags & DRAW_TRANSP)
2427 {
2428#ifdef FEAT_MBYTE
2429 if (enc_utf8)
2430 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2431 TEXT_Y(row), buf, wlen);
2432 else
2433#endif
2434 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2435 TEXT_Y(row), (char *)s, len);
2436 }
2437 else if (p_linespace != 0
2438#ifdef FEAT_XFONTSET
2439 || current_fontset != NULL
2440#endif
2441 )
2442 {
2443 XSetForeground(gui.dpy, gui.text_gc, prev_bg_color);
2444 XFillRectangle(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
2445 FILL_Y(row), gui.char_width * cells, gui.char_height);
2446 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
Bram Moolenaarfb269802005-03-15 22:46:30 +00002447
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448#ifdef FEAT_MBYTE
2449 if (enc_utf8)
2450 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2451 TEXT_Y(row), buf, wlen);
2452 else
2453#endif
2454 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2455 TEXT_Y(row), (char *)s, len);
2456 }
2457 else
2458 {
2459 /* XmbDrawImageString has bug, don't use it for fontset. */
2460#ifdef FEAT_MBYTE
2461 if (enc_utf8)
2462 XDrawImageString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2463 TEXT_Y(row), buf, wlen);
2464 else
2465#endif
2466 XDrawImageString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2467 TEXT_Y(row), (char *)s, len);
2468 }
2469
2470 /* Bold trick: draw the text again with a one-pixel offset. */
2471 if (flags & DRAW_BOLD)
2472 {
2473#ifdef FEAT_MBYTE
2474 if (enc_utf8)
2475 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col) + 1,
2476 TEXT_Y(row), buf, wlen);
2477 else
2478#endif
2479 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col) + 1,
2480 TEXT_Y(row), (char *)s, len);
2481 }
2482
Bram Moolenaarfb269802005-03-15 22:46:30 +00002483 /* Undercurl: draw curl at the bottom of the character cell. */
2484 if (flags & DRAW_UNDERC)
2485 draw_curl(row, col, cells);
2486
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 /* Underline: draw a line at the bottom of the character cell. */
2488 if (flags & DRAW_UNDERL)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002489 {
2490 int y = FILL_Y(row + 1) - 1;
2491
2492 /* When p_linespace is 0, overwrite the bottom row of pixels.
2493 * Otherwise put the line just below the character. */
2494 if (p_linespace > 1)
2495 y -= p_linespace - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 XDrawLine(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
Bram Moolenaarfb269802005-03-15 22:46:30 +00002497 y, FILL_X(col + cells) - 1, y);
2498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499
2500#ifdef FEAT_XFONTSET
2501 if (current_fontset != NULL)
2502 XSetClipMask(gui.dpy, gui.text_gc, None);
2503#endif
2504}
2505
2506/*
2507 * Return OK if the key with the termcap name "name" is supported.
2508 */
2509 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002510gui_mch_haskey(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511{
2512 int i;
2513
2514 for (i = 0; special_keys[i].key_sym != (KeySym)0; i++)
2515 if (name[0] == special_keys[i].vim_code0 &&
2516 name[1] == special_keys[i].vim_code1)
2517 return OK;
2518 return FAIL;
2519}
2520
2521/*
2522 * Return the text window-id and display. Only required for X-based GUI's
2523 */
2524 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002525gui_get_x11_windis(Window *win, Display **dis)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526{
2527 *win = XtWindow(vimShell);
2528 *dis = gui.dpy;
2529 return OK;
2530}
2531
2532 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002533gui_mch_beep(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534{
2535 XBell(gui.dpy, 0);
2536}
2537
2538 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002539gui_mch_flash(int msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
2541 /* Do a visual beep by reversing the foreground and background colors */
2542 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc, 0, 0,
2543 FILL_X((int)Columns) + gui.border_offset,
2544 FILL_Y((int)Rows) + gui.border_offset);
2545 XSync(gui.dpy, False);
2546 ui_delay((long)msec, TRUE); /* wait for a few msec */
2547 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc, 0, 0,
2548 FILL_X((int)Columns) + gui.border_offset,
2549 FILL_Y((int)Rows) + gui.border_offset);
2550}
2551
2552/*
2553 * Invert a rectangle from row r, column c, for nr rows and nc columns.
2554 */
2555 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002556gui_mch_invert_rectangle(
2557 int r,
2558 int c,
2559 int nr,
2560 int nc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561{
2562 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc,
2563 FILL_X(c), FILL_Y(r), (nc) * gui.char_width, (nr) * gui.char_height);
2564}
2565
2566/*
2567 * Iconify the GUI window.
2568 */
2569 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002570gui_mch_iconify(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571{
2572 XIconifyWindow(gui.dpy, XtWindow(vimShell), DefaultScreen(gui.dpy));
2573}
2574
2575#if defined(FEAT_EVAL) || defined(PROTO)
2576/*
2577 * Bring the Vim window to the foreground.
2578 */
2579 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002580gui_mch_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581{
2582 XMapRaised(gui.dpy, XtWindow(vimShell));
2583}
2584#endif
2585
2586/*
2587 * Draw a cursor without focus.
2588 */
2589 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002590gui_mch_draw_hollow_cursor(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591{
2592 int w = 1;
2593
2594#ifdef FEAT_MBYTE
2595 if (mb_lefthalve(gui.row, gui.col))
2596 w = 2;
2597#endif
2598 gui_mch_set_fg_color(color);
2599 XDrawRectangle(gui.dpy, gui.wid, gui.text_gc, FILL_X(gui.col),
2600 FILL_Y(gui.row), w * gui.char_width - 1, gui.char_height - 1);
2601}
2602
2603/*
2604 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
2605 * color "color".
2606 */
2607 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002608gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609{
2610 gui_mch_set_fg_color(color);
2611
2612 XFillRectangle(gui.dpy, gui.wid, gui.text_gc,
2613#ifdef FEAT_RIGHTLEFT
2614 /* vertical line should be on the right of current point */
2615 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
2616#endif
2617 FILL_X(gui.col),
2618 FILL_Y(gui.row) + gui.char_height - h,
2619 w, h);
2620}
2621
2622/*
2623 * Catch up with any queued X events. This may put keyboard input into the
2624 * input buffer, call resize call-backs, trigger timers etc. If there is
2625 * nothing in the X event queue (& no timers pending), then we return
2626 * immediately.
2627 */
2628 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002629gui_mch_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630{
2631 XtInputMask mask, desired;
2632
2633#ifdef ALT_X_INPUT
2634 if (suppress_alternate_input)
2635 desired = (XtIMXEvent | XtIMTimer);
2636 else
2637#endif
2638 desired = (XtIMAll);
2639 while ((mask = XtAppPending(app_context)) && (mask & desired)
2640 && !vim_is_input_buf_full())
2641 XtAppProcessEvent(app_context, desired);
2642}
2643
2644/*
2645 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2646 * from the keyboard.
2647 * wtime == -1 Wait forever.
2648 * wtime == 0 This should never happen.
2649 * wtime > 0 Wait wtime milliseconds for a character.
2650 * Returns OK if a character was found to be available within the given time,
2651 * or FAIL otherwise.
2652 */
2653 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002654gui_mch_wait_for_chars(long wtime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655{
Bram Moolenaar4231da42016-06-02 14:30:04 +02002656 int focus;
2657 int retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658
2659 /*
2660 * Make this static, in case gui_x11_timer_cb is called after leaving
2661 * this function (otherwise a random value on the stack may be changed).
2662 */
2663 static int timed_out;
2664 XtIntervalId timer = (XtIntervalId)0;
2665 XtInputMask desired;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666
2667 timed_out = FALSE;
2668
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 if (wtime > 0)
2670 timer = XtAppAddTimeOut(app_context, (long_u)wtime, gui_x11_timer_cb,
2671 &timed_out);
2672
2673 focus = gui.in_focus;
2674#ifdef ALT_X_INPUT
2675 if (suppress_alternate_input)
2676 desired = (XtIMXEvent | XtIMTimer);
2677 else
2678#endif
2679 desired = (XtIMAll);
2680 while (!timed_out)
2681 {
2682 /* Stop or start blinking when focus changes */
2683 if (gui.in_focus != focus)
2684 {
2685 if (gui.in_focus)
2686 gui_mch_start_blink();
2687 else
2688 gui_mch_stop_blink();
2689 focus = gui.in_focus;
2690 }
2691
Bram Moolenaar93c88e02015-09-15 14:12:05 +02002692#ifdef MESSAGE_QUEUE
Bram Moolenaar4231da42016-06-02 14:30:04 +02002693# ifdef FEAT_TIMERS
2694 did_add_timer = FALSE;
2695# endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02002696 parse_queued_messages();
Bram Moolenaar4231da42016-06-02 14:30:04 +02002697# ifdef FEAT_TIMERS
2698 if (did_add_timer)
2699 /* Need to recompute the waiting time. */
2700 break;
2701# endif
Bram Moolenaar03531f72010-11-16 15:04:57 +01002702#endif
2703
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 /*
2705 * Don't use gui_mch_update() because then we will spin-lock until a
2706 * char arrives, instead we use XtAppProcessEvent() to hang until an
2707 * event arrives. No need to check for input_buf_full because we are
2708 * returning as soon as it contains a single char. Note that
2709 * XtAppNextEvent() may not be used because it will not return after a
2710 * timer event has arrived -- webb
2711 */
2712 XtAppProcessEvent(app_context, desired);
2713
2714 if (input_available())
2715 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002716 retval = OK;
2717 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 }
2719 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002720
2721 if (timer != (XtIntervalId)0 && !timed_out)
2722 XtRemoveTimeOut(timer);
2723
2724 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725}
2726
2727/*
2728 * Output routines.
2729 */
2730
2731/* Flush any output to the screen */
2732 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002733gui_mch_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734{
2735 XFlush(gui.dpy);
2736}
2737
2738/*
2739 * Clear a rectangular region of the screen from text pos (row1, col1) to
2740 * (row2, col2) inclusive.
2741 */
2742 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002743gui_mch_clear_block(
2744 int row1,
2745 int col1,
2746 int row2,
2747 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748{
2749 int x;
2750
2751 x = FILL_X(col1);
2752
2753 /* Clear one extra pixel at the far right, for when bold characters have
2754 * spilled over to the next column. */
2755 XFillRectangle(gui.dpy, gui.wid, gui.back_gc, x, FILL_Y(row1),
2756 (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1),
2757 (row2 - row1 + 1) * gui.char_height);
2758}
2759
2760 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002761gui_mch_clear_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762{
2763 XClearArea(gui.dpy, gui.wid, 0, 0, 0, 0, False);
2764}
2765
2766/*
2767 * Delete the given number of lines from the given row, scrolling up any
2768 * text further down within the scroll region.
2769 */
2770 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002771gui_mch_delete_lines(int row, int num_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772{
2773 if (gui.visibility == VisibilityFullyObscured)
2774 return; /* Can't see the window */
2775
2776 /* copy one extra pixel at the far right, for when bold has spilled
2777 * over */
2778 XCopyArea(gui.dpy, gui.wid, gui.wid, gui.text_gc,
2779 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
2780 gui.char_width * (gui.scroll_region_right - gui.scroll_region_left + 1)
2781 + (gui.scroll_region_right == Columns - 1),
2782 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1),
2783 FILL_X(gui.scroll_region_left), FILL_Y(row));
2784
2785 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
2786 gui.scroll_region_left,
2787 gui.scroll_region_bot, gui.scroll_region_right);
2788 gui_x11_check_copy_area();
2789}
2790
2791/*
2792 * Insert the given number of lines before the given row, scrolling down any
2793 * following text within the scroll region.
2794 */
2795 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002796gui_mch_insert_lines(int row, int num_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797{
2798 if (gui.visibility == VisibilityFullyObscured)
2799 return; /* Can't see the window */
2800
2801 /* copy one extra pixel at the far right, for when bold has spilled
2802 * over */
2803 XCopyArea(gui.dpy, gui.wid, gui.wid, gui.text_gc,
2804 FILL_X(gui.scroll_region_left), FILL_Y(row),
2805 gui.char_width * (gui.scroll_region_right - gui.scroll_region_left + 1)
2806 + (gui.scroll_region_right == Columns - 1),
2807 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1),
2808 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines));
2809
2810 gui_clear_block(row, gui.scroll_region_left,
2811 row + num_lines - 1, gui.scroll_region_right);
2812 gui_x11_check_copy_area();
2813}
2814
2815/*
2816 * Update the region revealed by scrolling up/down.
2817 */
2818 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002819gui_x11_check_copy_area(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820{
2821 XEvent event;
2822 XGraphicsExposeEvent *gevent;
2823
2824 if (gui.visibility != VisibilityPartiallyObscured)
2825 return;
2826
2827 XFlush(gui.dpy);
2828
2829 /* Wait to check whether the scroll worked or not */
2830 for (;;)
2831 {
2832 if (XCheckTypedEvent(gui.dpy, NoExpose, &event))
2833 return; /* The scroll worked. */
2834
2835 if (XCheckTypedEvent(gui.dpy, GraphicsExpose, &event))
2836 {
2837 gevent = (XGraphicsExposeEvent *)&event;
2838 gui_redraw(gevent->x, gevent->y, gevent->width, gevent->height);
2839 if (gevent->count == 0)
2840 return; /* This was the last expose event */
2841 }
2842 XSync(gui.dpy, False);
2843 }
2844}
2845
2846/*
2847 * X Selection stuff, for cutting and pasting text to other windows.
2848 */
2849
2850 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002851clip_mch_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
2853 clip_x11_lose_selection(vimShell, cbd);
2854}
2855
2856 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002857clip_mch_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858{
2859 return clip_x11_own_selection(vimShell, cbd);
2860}
2861
2862 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002863clip_mch_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864{
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002865 clip_x11_request_selection(vimShell, gui.dpy, cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866}
2867
2868 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002869clip_mch_set_selection(
2870 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871{
2872 clip_x11_set_selection(cbd);
2873}
2874
2875#if defined(FEAT_MENU) || defined(PROTO)
2876/*
2877 * Menu stuff.
2878 */
2879
2880/*
2881 * Make a menu either grey or not grey.
2882 */
2883 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002884gui_mch_menu_grey(vimmenu_T *menu, int grey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885{
2886 if (menu->id != (Widget)0)
2887 {
2888 gui_mch_menu_hidden(menu, False);
2889 if (grey
2890#ifdef FEAT_GUI_MOTIF
2891 || !menu->sensitive
2892#endif
2893 )
2894 XtSetSensitive(menu->id, False);
2895 else
2896 XtSetSensitive(menu->id, True);
2897 }
2898}
2899
2900/*
2901 * Make menu item hidden or not hidden
2902 */
2903 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002904gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905{
2906 if (menu->id != (Widget)0)
2907 {
2908 if (hidden)
2909 XtUnmanageChild(menu->id);
2910 else
2911 XtManageChild(menu->id);
2912 }
2913}
2914
2915/*
2916 * This is called after setting all the menus to grey/hidden or not.
2917 */
2918 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002919gui_mch_draw_menubar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920{
2921 /* Nothing to do in X */
2922}
2923
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002925gui_x11_menu_cb(
2926 Widget w UNUSED,
2927 XtPointer client_data,
2928 XtPointer call_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929{
2930 gui_menu_cb((vimmenu_T *)client_data);
2931}
2932
2933#endif /* FEAT_MENU */
2934
2935
2936
2937/*
2938 * Function called when window closed. Works like ":qa".
2939 * Should put up a requester!
2940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002942gui_x11_wm_protocol_handler(
2943 Widget w UNUSED,
2944 XtPointer client_data UNUSED,
2945 XEvent *event,
2946 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947{
2948 /*
2949 * Only deal with Client messages.
2950 */
2951 if (event->type != ClientMessage)
2952 return;
2953
2954 /*
2955 * The WM_SAVE_YOURSELF event arrives when the window manager wants to
2956 * exit. That can be cancelled though, thus Vim shouldn't exit here.
2957 * Just sync our swap files.
2958 */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002959 if ((Atom)((XClientMessageEvent *)event)->data.l[0] ==
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 wm_atoms[SAVE_YOURSELF_IDX])
2961 {
2962 out_flush();
2963 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2964
2965 /* Set the window's WM_COMMAND property, to let the window manager
2966 * know we are done saving ourselves. We don't want to be restarted,
2967 * thus set argv to NULL. */
2968 XSetCommand(gui.dpy, XtWindow(vimShell), NULL, 0);
2969 return;
2970 }
2971
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002972 if ((Atom)((XClientMessageEvent *)event)->data.l[0] !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 wm_atoms[DELETE_WINDOW_IDX])
2974 return;
2975
2976 gui_shell_closed();
2977}
2978
2979#ifdef FEAT_CLIENTSERVER
2980/*
2981 * Function called when property changed. Check for incoming commands
2982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002984gui_x11_send_event_handler(
2985 Widget w UNUSED,
2986 XtPointer client_data UNUSED,
2987 XEvent *event,
2988 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989{
2990 XPropertyEvent *e = (XPropertyEvent *) event;
2991
2992 if (e->type == PropertyNotify && e->window == commWindow
2993 && e->atom == commProperty && e->state == PropertyNewValue)
2994 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02002995 serverEventProc(gui.dpy, event, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 }
2997}
2998#endif
2999
3000/*
3001 * Cursor blink functions.
3002 *
3003 * This is a simple state machine:
3004 * BLINK_NONE not blinking at all
3005 * BLINK_OFF blinking, cursor is not shown
3006 * BLINK_ON blinking, cursor is shown
3007 */
3008
3009#define BLINK_NONE 0
3010#define BLINK_OFF 1
3011#define BLINK_ON 2
3012
3013static int blink_state = BLINK_NONE;
3014static long_u blink_waittime = 700;
3015static long_u blink_ontime = 400;
3016static long_u blink_offtime = 250;
3017static XtIntervalId blink_timer = (XtIntervalId)0;
3018
Bram Moolenaar703a8042016-06-04 16:24:32 +02003019 int
3020gui_mch_is_blinking(void)
3021{
3022 return blink_state != BLINK_NONE;
3023}
3024
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +02003025 int
3026gui_mch_is_blink_off(void)
3027{
3028 return blink_state == BLINK_OFF;
3029}
3030
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 void
Bram Moolenaar02fdaea2016-01-30 18:13:55 +01003032gui_mch_set_blinking(long waittime, long on, long off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033{
3034 blink_waittime = waittime;
3035 blink_ontime = on;
3036 blink_offtime = off;
3037}
3038
3039/*
3040 * Stop the cursor blinking. Show the cursor if it wasn't shown.
3041 */
3042 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003043gui_mch_stop_blink(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044{
3045 if (blink_timer != (XtIntervalId)0)
3046 {
3047 XtRemoveTimeOut(blink_timer);
3048 blink_timer = (XtIntervalId)0;
3049 }
3050 if (blink_state == BLINK_OFF)
3051 gui_update_cursor(TRUE, FALSE);
3052 blink_state = BLINK_NONE;
3053}
3054
3055/*
3056 * Start the cursor blinking. If it was already blinking, this restarts the
3057 * waiting time and shows the cursor.
3058 */
3059 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003060gui_mch_start_blink(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061{
3062 if (blink_timer != (XtIntervalId)0)
3063 XtRemoveTimeOut(blink_timer);
3064 /* Only switch blinking on if none of the times is zero */
3065 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
3066 {
3067 blink_timer = XtAppAddTimeOut(app_context, blink_waittime,
3068 gui_x11_blink_cb, NULL);
3069 blink_state = BLINK_ON;
3070 gui_update_cursor(TRUE, FALSE);
3071 }
3072}
3073
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003075gui_x11_blink_cb(
3076 XtPointer timed_out UNUSED,
3077 XtIntervalId *interval_id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078{
3079 if (blink_state == BLINK_ON)
3080 {
3081 gui_undraw_cursor();
3082 blink_state = BLINK_OFF;
3083 blink_timer = XtAppAddTimeOut(app_context, blink_offtime,
3084 gui_x11_blink_cb, NULL);
3085 }
3086 else
3087 {
3088 gui_update_cursor(TRUE, FALSE);
3089 blink_state = BLINK_ON;
3090 blink_timer = XtAppAddTimeOut(app_context, blink_ontime,
3091 gui_x11_blink_cb, NULL);
3092 }
3093}
3094
3095/*
3096 * Return the RGB value of a pixel as a long.
3097 */
3098 long_u
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003099gui_mch_get_rgb(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100{
3101 XColor xc;
3102 Colormap colormap;
3103
3104 colormap = DefaultColormap(gui.dpy, XDefaultScreen(gui.dpy));
3105 xc.pixel = pixel;
3106 XQueryColor(gui.dpy, colormap, &xc);
3107
3108 return ((xc.red & 0xff00) << 8) + (xc.green & 0xff00)
3109 + ((unsigned)xc.blue >> 8);
3110}
3111
3112/*
3113 * Add the callback functions.
3114 */
3115 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003116gui_x11_callbacks(Widget textArea, Widget vimForm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
3118 XtAddEventHandler(textArea, VisibilityChangeMask, FALSE,
3119 gui_x11_visibility_cb, (XtPointer)0);
3120
3121 XtAddEventHandler(textArea, ExposureMask, FALSE, gui_x11_expose_cb,
3122 (XtPointer)0);
3123
3124 XtAddEventHandler(vimShell, StructureNotifyMask, FALSE,
3125 gui_x11_resize_window_cb, (XtPointer)0);
3126
3127 XtAddEventHandler(vimShell, FocusChangeMask, FALSE, gui_x11_focus_change_cb,
3128 (XtPointer)0);
3129 /*
3130 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3131 * Only needed for some window managers.
3132 */
3133 if (vim_strchr(p_go, GO_POINTER) != NULL)
3134 {
3135 XtAddEventHandler(vimShell, LeaveWindowMask, FALSE, gui_x11_leave_cb,
3136 (XtPointer)0);
3137 XtAddEventHandler(textArea, LeaveWindowMask, FALSE, gui_x11_leave_cb,
3138 (XtPointer)0);
3139 XtAddEventHandler(textArea, EnterWindowMask, FALSE, gui_x11_enter_cb,
3140 (XtPointer)0);
3141 XtAddEventHandler(vimShell, EnterWindowMask, FALSE, gui_x11_enter_cb,
3142 (XtPointer)0);
3143 }
3144
3145 XtAddEventHandler(vimForm, KeyPressMask, FALSE, gui_x11_key_hit_cb,
3146 (XtPointer)0);
3147 XtAddEventHandler(textArea, KeyPressMask, FALSE, gui_x11_key_hit_cb,
3148 (XtPointer)0);
3149
3150 /* get pointer moved events from scrollbar, needed for 'mousefocus' */
3151 XtAddEventHandler(vimForm, PointerMotionMask,
3152 FALSE, gui_x11_mouse_cb, (XtPointer)1);
3153 XtAddEventHandler(textArea, ButtonPressMask | ButtonReleaseMask |
3154 ButtonMotionMask | PointerMotionMask,
3155 FALSE, gui_x11_mouse_cb, (XtPointer)0);
3156}
3157
3158/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003159 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003161 void
3162gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163{
3164 int rootx, rooty, winx, winy;
3165 Window root, child;
3166 unsigned int mask;
3167
3168 if (gui.wid && XQueryPointer(gui.dpy, gui.wid, &root, &child,
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003169 &rootx, &rooty, &winx, &winy, &mask)) {
3170 *x = winx;
3171 *y = winy;
3172 } else {
3173 *x = -1;
3174 *y = -1;
3175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176}
3177
3178 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003179gui_mch_setmouse(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180{
3181 if (gui.wid)
3182 XWarpPointer(gui.dpy, (Window)0, gui.wid, 0, 0, 0, 0, x, y);
3183}
3184
3185#if (defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)) || defined(PROTO)
3186 XButtonPressedEvent *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003187gui_x11_get_last_mouse_event(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188{
3189 return &last_mouse_event;
3190}
3191#endif
3192
3193#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
3194
3195/* Signs are currently always 2 chars wide. Hopefully the font is big enough
3196 * to provide room for the bitmap! */
3197# define SIGN_WIDTH (gui.char_width * 2)
3198
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003200gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201{
3202 XImage *sign;
3203
3204 if (gui.in_use && (sign = (XImage *)sign_get_image(typenr)) != NULL)
3205 {
3206 XClearArea(gui.dpy, gui.wid, TEXT_X(col), TEXT_Y(row) - sign->height,
3207 SIGN_WIDTH, gui.char_height, FALSE);
3208 XPutImage(gui.dpy, gui.wid, gui.text_gc, sign, 0, 0,
3209 TEXT_X(col) + (SIGN_WIDTH - sign->width) / 2,
3210 TEXT_Y(row) - sign->height,
3211 sign->width, sign->height);
3212 }
3213}
3214
3215 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003216gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217{
3218 XpmAttributes attrs;
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003219 XImage *sign = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 int status;
3221
3222 /*
3223 * Setup the color substitution table.
3224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 if (signfile[0] != NUL && signfile[0] != '-')
3226 {
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003227 XpmColorSymbol color[5] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 {
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003229 {"none", NULL, 0},
3230 {"iconColor1", NULL, 0},
3231 {"bottomShadowColor", NULL, 0},
3232 {"topShadowColor", NULL, 0},
3233 {"selectColor", NULL, 0}
3234 };
3235 attrs.valuemask = XpmColorSymbols;
3236 attrs.numsymbols = 2;
3237 attrs.colorsymbols = color;
3238 attrs.colorsymbols[0].pixel = gui.back_pixel;
3239 attrs.colorsymbols[1].pixel = gui.norm_pixel;
3240 status = XpmReadFileToImage(gui.dpy, (char *)signfile,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 &sign, NULL, &attrs);
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003242 if (status == 0)
3243 {
3244 /* Sign width is fixed at two columns now.
3245 if (sign->width > gui.sign_width)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003246 gui.sign_width = sign->width + 8; */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 }
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003248 else
3249 EMSG(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 }
3251
3252 return (void *)sign;
3253}
3254
3255 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003256gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257{
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003258 XDestroyImage((XImage*)sign);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259}
3260#endif
3261
3262
3263#ifdef FEAT_MOUSESHAPE
3264/* The last set mouse pointer shape is remembered, to be used when it goes
3265 * from hidden to not hidden. */
3266static int last_shape = 0;
3267#endif
3268
3269/*
3270 * Use the blank mouse pointer or not.
3271 */
3272 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003273gui_mch_mousehide(
3274 int hide) /* TRUE = use blank ptr, FALSE = use parent ptr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275{
3276 if (gui.pointer_hidden != hide)
3277 {
3278 gui.pointer_hidden = hide;
3279 if (hide)
3280 XDefineCursor(gui.dpy, gui.wid, gui.blank_pointer);
3281 else
3282#ifdef FEAT_MOUSESHAPE
3283 mch_set_mouse_shape(last_shape);
3284#else
3285 XUndefineCursor(gui.dpy, gui.wid);
3286#endif
3287 }
3288}
3289
3290#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3291
3292/* Table for shape IDs. Keep in sync with the mshape_names[] table in
3293 * misc2.c! */
3294static int mshape_ids[] =
3295{
3296 XC_left_ptr, /* arrow */
3297 0, /* blank */
3298 XC_xterm, /* beam */
3299 XC_sb_v_double_arrow, /* updown */
3300 XC_sizing, /* udsizing */
3301 XC_sb_h_double_arrow, /* leftright */
3302 XC_sizing, /* lrsizing */
3303 XC_watch, /* busy */
3304 XC_X_cursor, /* no */
3305 XC_crosshair, /* crosshair */
3306 XC_hand1, /* hand1 */
3307 XC_hand2, /* hand2 */
3308 XC_pencil, /* pencil */
3309 XC_question_arrow, /* question */
3310 XC_right_ptr, /* right-arrow */
3311 XC_center_ptr, /* up-arrow */
3312 XC_left_ptr /* last one */
3313};
3314
3315 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003316mch_set_mouse_shape(int shape)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317{
3318 int id;
3319
3320 if (!gui.in_use)
3321 return;
3322
3323 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
3324 XDefineCursor(gui.dpy, gui.wid, gui.blank_pointer);
3325 else
3326 {
3327 if (shape >= MSHAPE_NUMBERED)
3328 {
3329 id = shape - MSHAPE_NUMBERED;
3330 if (id >= XC_num_glyphs)
3331 id = XC_left_ptr;
3332 else
3333 id &= ~1; /* they are always even (why?) */
3334 }
3335 else
3336 id = mshape_ids[shape];
3337
3338 XDefineCursor(gui.dpy, gui.wid, XCreateFontCursor(gui.dpy, id));
3339 }
3340 if (shape != MSHAPE_HIDE)
3341 last_shape = shape;
3342}
3343#endif
3344
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345#if (defined(FEAT_TOOLBAR) && defined(FEAT_BEVAL)) || defined(PROTO)
3346/*
3347 * Set the balloon-eval used for the tooltip of a toolbar menu item.
3348 * The check for a non-toolbar item was added, because there is a crash when
3349 * passing a normal menu item here. Can't explain that, but better avoid it.
3350 */
3351 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003352gui_mch_menu_set_tip(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
3354 if (menu->id != NULL && menu->parent != NULL
3355 && menu_is_toolbar(menu->parent->name))
3356 {
3357 /* Always destroy and create the balloon, in case the string was
3358 * changed. */
3359 if (menu->tip != NULL)
3360 {
3361 gui_mch_destroy_beval_area(menu->tip);
3362 menu->tip = NULL;
3363 }
3364 if (menu->strings[MENU_INDEX_TIP] != NULL)
3365 menu->tip = gui_mch_create_beval_area(
3366 menu->id,
3367 menu->strings[MENU_INDEX_TIP],
3368 NULL,
3369 NULL);
3370 }
3371}
3372#endif