blob: e0b91e65cdc14e9cef6abb859971a89de8a985da [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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
Bram Moolenaarf7506ca2017-02-25 16:01:49 +010015#include "vim.h"
16
Bram Moolenaar071d4272004-06-13 20:20:40 +000017#include <X11/keysym.h>
18#include <X11/Xatom.h>
19#include <X11/StringDefs.h>
20#include <X11/Intrinsic.h>
21#include <X11/Shell.h>
22#include <X11/cursorfont.h>
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
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.
Bram Moolenaar46c9c732004-12-12 11:37:09 +00001995 */
1996 char_u *
Bram Moolenaar87748452017-03-12 17:10:33 +01001997gui_mch_get_fontname(GuiFont font, char_u *name)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00001998{
Bram Moolenaar87748452017-03-12 17:10:33 +01001999 char_u *ret = NULL;
2000
2001 if (name != NULL && font == NULL)
2002 {
2003 /* In this case, there's no way other than doing this. */
2004 ret = vim_strsave(name);
2005 }
2006 else if (font != NULL)
2007 {
2008 /* In this case, try to retrieve the XLFD corresponding to 'font'->fid;
2009 * if failed, use 'name' unless it's NULL. */
2010 unsigned long value = 0L;
2011
2012 if (XGetFontProperty(font, XA_FONT, &value))
2013 {
2014 char *xa_font_name = NULL;
2015
2016 xa_font_name = XGetAtomName(gui.dpy, value);
2017 if (xa_font_name != NULL)
2018 {
2019 ret = vim_strsave((char_u *)xa_font_name);
2020 XFree(xa_font_name);
2021 }
2022 else if (name != NULL)
2023 ret = vim_strsave(name);
2024 }
2025 else if (name != NULL)
2026 ret = vim_strsave(name);
2027 }
2028 return ret;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002029}
Bram Moolenaar567e4de2004-12-31 21:01:02 +00002030#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002031
Bram Moolenaar231334e2005-07-25 20:46:57 +00002032/*
2033 * Adjust gui.char_height (after 'linespace' was changed).
2034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002036gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037{
2038#ifdef FEAT_XFONTSET
2039 if (gui.fontset != NOFONTSET)
2040 {
2041 gui.char_height = fontset_height((XFontSet)gui.fontset) + p_linespace;
2042 gui.char_ascent = fontset_ascent((XFontSet)gui.fontset)
2043 + p_linespace / 2;
2044 }
2045 else
2046#endif
2047 {
2048 XFontStruct *font = (XFontStruct *)gui.norm_font;
2049
2050 gui.char_height = font->ascent + font->descent + p_linespace;
2051 gui.char_ascent = font->ascent + p_linespace / 2;
2052 }
2053 return OK;
2054}
2055
2056/*
2057 * Set the current text font.
2058 */
2059 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002060gui_mch_set_font(GuiFont font)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061{
2062 static Font prev_font = (Font)-1;
2063 Font fid = ((XFontStruct *)font)->fid;
2064
2065 if (fid != prev_font)
2066 {
2067 XSetFont(gui.dpy, gui.text_gc, fid);
2068 XSetFont(gui.dpy, gui.back_gc, fid);
2069 prev_font = fid;
2070 gui.char_ascent = ((XFontStruct *)font)->ascent + p_linespace / 2;
2071 }
2072#ifdef FEAT_XFONTSET
2073 current_fontset = (XFontSet)NULL;
2074#endif
2075}
2076
2077#if defined(FEAT_XFONTSET) || defined(PROTO)
2078/*
2079 * Set the current text fontset.
2080 * Adjust the ascent, in case it's different.
2081 */
2082 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002083gui_mch_set_fontset(GuiFontset fontset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084{
2085 current_fontset = (XFontSet)fontset;
2086 gui.char_ascent = fontset_ascent(current_fontset) + p_linespace / 2;
2087}
2088#endif
2089
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090/*
2091 * If a font is not going to be used, free its structure.
2092 */
2093 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002094gui_mch_free_font(GuiFont font)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095{
2096 if (font != NOFONT)
2097 XFreeFont(gui.dpy, (XFontStruct *)font);
2098}
2099
2100#if defined(FEAT_XFONTSET) || defined(PROTO)
2101/*
2102 * If a fontset is not going to be used, free its structure.
2103 */
2104 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002105gui_mch_free_fontset(GuiFontset fontset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106{
2107 if (fontset != NOFONTSET)
2108 XFreeFontSet(gui.dpy, (XFontSet)fontset);
2109}
2110
2111/*
2112 * Load the fontset "name".
2113 * Return a reference to the fontset, or NOFONTSET when failing.
2114 */
2115 GuiFontset
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002116gui_mch_get_fontset(
2117 char_u *name,
2118 int giveErrorIfMissing,
2119 int fixed_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120{
2121 XFontSet fontset;
2122 char **missing, *def_str;
2123 int num_missing;
2124
2125 if (!gui.in_use || name == NULL)
2126 return NOFONTSET;
2127
2128 fontset = XCreateFontSet(gui.dpy, (char *)name, &missing, &num_missing,
2129 &def_str);
2130 if (num_missing > 0)
2131 {
2132 int i;
2133
2134 if (giveErrorIfMissing)
2135 {
2136 EMSG2(_("E250: Fonts for the following charsets are missing in fontset %s:"), name);
2137 for (i = 0; i < num_missing; i++)
2138 EMSG2("%s", missing[i]);
2139 }
2140 XFreeStringList(missing);
2141 }
2142
2143 if (fontset == NULL)
2144 {
2145 if (giveErrorIfMissing)
2146 EMSG2(_(e_fontset), name);
2147 return NOFONTSET;
2148 }
2149
2150 if (fixed_width && check_fontset_sanity(fontset) == FAIL)
2151 {
2152 XFreeFontSet(gui.dpy, fontset);
2153 return NOFONTSET;
2154 }
2155 return (GuiFontset)fontset;
2156}
2157
2158/*
2159 * Check if fontset "fs" is fixed width.
2160 */
2161 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002162check_fontset_sanity(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163{
2164 XFontStruct **xfs;
2165 char **font_name;
2166 int fn;
2167 char *base_name;
2168 int i;
2169 int min_width;
2170 int min_font_idx = 0;
2171
2172 base_name = XBaseFontNameListOfFontSet(fs);
2173 fn = XFontsOfFontSet(fs, &xfs, &font_name);
2174 for (i = 0; i < fn; i++)
2175 {
2176 if (xfs[i]->max_bounds.width != xfs[i]->min_bounds.width)
2177 {
2178 EMSG2(_("E252: Fontset name: %s"), base_name);
2179 EMSG2(_("Font '%s' is not fixed-width"), font_name[i]);
2180 return FAIL;
2181 }
2182 }
2183 /* scan base font width */
2184 min_width = 32767;
2185 for (i = 0; i < fn; i++)
2186 {
2187 if (xfs[i]->max_bounds.width<min_width)
2188 {
2189 min_width = xfs[i]->max_bounds.width;
2190 min_font_idx = i;
2191 }
2192 }
2193 for (i = 0; i < fn; i++)
2194 {
2195 if ( xfs[i]->max_bounds.width != 2 * min_width
2196 && xfs[i]->max_bounds.width != min_width)
2197 {
Bram Moolenaar9d438d32013-06-13 21:57:20 +02002198 EMSG2(_("E253: Fontset name: %s"), base_name);
2199 EMSG2(_("Font0: %s"), font_name[min_font_idx]);
2200 EMSG2(_("Font1: %s"), font_name[i]);
2201 EMSGN(_("Font%ld width is not twice that of font0"), i);
2202 EMSGN(_("Font0 width: %ld"), xfs[min_font_idx]->max_bounds.width);
2203 EMSGN(_("Font1 width: %ld"), xfs[i]->max_bounds.width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 return FAIL;
2205 }
2206 }
2207 /* it seems ok. Good Luck!! */
2208 return OK;
2209}
2210
2211 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002212fontset_width(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213{
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002214 return XmbTextEscapement(fs, "Vim", 3) / 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215}
2216
2217 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002218fontset_height(
2219 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;
2225}
2226
2227#if (defined(FONTSET_ALWAYS) && defined(FEAT_GUI_ATHENA) \
2228 && defined(FEAT_MENU)) || defined(PROTO)
2229/*
2230 * Returns the bounding box height around the actual glyph image of all
2231 * characters in all fonts of the fontset.
2232 */
2233 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002234fontset_height2(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235{
2236 XFontSetExtents *extents;
2237
2238 extents = XExtentsOfFontSet(fs);
2239 return extents->max_ink_extent.height;
2240}
2241#endif
2242
2243/* NOT USED YET
2244 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002245fontset_descent(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246{
2247 XFontSetExtents *extents;
2248
2249 extents = XExtentsOfFontSet (fs);
2250 return extents->max_logical_extent.height + extents->max_logical_extent.y;
2251}
2252*/
2253
2254 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002255fontset_ascent(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256{
2257 XFontSetExtents *extents;
2258
2259 extents = XExtentsOfFontSet(fs);
2260 return -extents->max_logical_extent.y;
2261}
2262
2263#endif /* FEAT_XFONTSET */
2264
2265/*
2266 * Return the Pixel value (color) for the given color name.
2267 * Return INVALCOLOR for error.
2268 */
2269 guicolor_T
Bram Moolenaar46582282016-07-23 14:35:12 +02002270gui_mch_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271{
Bram Moolenaar46582282016-07-23 14:35:12 +02002272 guicolor_T requested;
2273 XColor available;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 Colormap colormap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275
2276 /* can't do this when GUI not running */
Bram Moolenaar46582282016-07-23 14:35:12 +02002277 if (!gui.in_use || name == NULL || *name == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 return INVALCOLOR;
2279
Bram Moolenaar46582282016-07-23 14:35:12 +02002280 requested = gui_get_color_cmn(name);
2281 if (requested == INVALCOLOR)
2282 return INVALCOLOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002284 return gui_mch_get_rgb_color(
Bram Moolenaar46582282016-07-23 14:35:12 +02002285 (requested & 0xff0000) >> 16,
2286 (requested & 0xff00) >> 8,
2287 requested & 0xff);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002288}
2289
2290/*
2291 * Return the Pixel value (color) for the given RGB values.
2292 * Return INVALCOLOR for error.
2293 */
2294 guicolor_T
2295gui_mch_get_rgb_color(int r, int g, int b)
2296{
2297 char spec[8]; /* space enough to hold "#RRGGBB" */
2298
2299 vim_snprintf(spec, sizeof(spec), "#%.2x%.2x%.2x", r, g, b);
Bram Moolenaar46582282016-07-23 14:35:12 +02002300 colormap = DefaultColormap(gui.dpy, DefaultScreen(gui.dpy));
2301 if (XParseColor(gui.dpy, colormap, (char *)spec, &available) != 0
2302 && XAllocColor(gui.dpy, colormap, &available) != 0)
2303 return (guicolor_T)available.pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
2305 return INVALCOLOR;
2306}
2307
2308/*
Bram Moolenaarfb269802005-03-15 22:46:30 +00002309 * Set the current text foreground color.
2310 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002312gui_mch_set_fg_color(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313{
2314 if (color != prev_fg_color)
2315 {
2316 XSetForeground(gui.dpy, gui.text_gc, (Pixel)color);
2317 prev_fg_color = color;
2318 }
2319}
2320
2321/*
2322 * Set the current text background color.
2323 */
2324 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002325gui_mch_set_bg_color(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326{
2327 if (color != prev_bg_color)
2328 {
2329 XSetBackground(gui.dpy, gui.text_gc, (Pixel)color);
2330 prev_bg_color = color;
2331 }
2332}
2333
2334/*
Bram Moolenaarfb269802005-03-15 22:46:30 +00002335 * Set the current text special color.
2336 */
2337 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002338gui_mch_set_sp_color(guicolor_T color)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002339{
2340 prev_sp_color = color;
2341}
2342
2343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 * create a mouse pointer that is blank
2345 */
2346 static Cursor
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002347gui_x11_create_blank_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348{
2349 Pixmap blank_pixmap = XCreatePixmap(gui.dpy, gui.wid, 1, 1, 1);
2350 GC gc = XCreateGC(gui.dpy, blank_pixmap, (unsigned long)0, (XGCValues*)0);
2351 XDrawPoint(gui.dpy, blank_pixmap, gc, 0, 0);
2352 XFreeGC(gui.dpy, gc);
2353 return XCreatePixmapCursor(gui.dpy, blank_pixmap, blank_pixmap,
2354 (XColor*)&gui.norm_pixel, (XColor*)&gui.norm_pixel, 0, 0);
2355}
2356
Bram Moolenaarfb269802005-03-15 22:46:30 +00002357/*
2358 * Draw a curled line at the bottom of the character cell.
2359 */
2360 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002361draw_curl(int row, int col, int cells)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002362{
2363 int i;
2364 int offset;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002365 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarfb269802005-03-15 22:46:30 +00002366
2367 XSetForeground(gui.dpy, gui.text_gc, prev_sp_color);
2368 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
2369 {
2370 offset = val[i % 8];
2371 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, i,
2372 FILL_Y(row + 1) - 1 - offset);
2373 }
2374 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
2375}
2376
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002378gui_mch_draw_string(
2379 int row,
2380 int col,
2381 char_u *s,
2382 int len,
2383 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384{
2385 int cells = len;
2386#ifdef FEAT_MBYTE
Bram Moolenaara3227e22006-03-08 21:32:40 +00002387 static void *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 static int buflen = 0;
2389 char_u *p;
2390 int wlen = 0;
2391 int c;
2392
2393 if (enc_utf8)
2394 {
2395 /* Convert UTF-8 byte sequence to 16 bit characters for the X
2396 * functions. Need a buffer for the 16 bit characters. Keep it
2397 * between calls, because allocating it each time is slow. */
2398 if (buflen < len)
2399 {
2400 XtFree((char *)buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002401 buf = (void *)XtMalloc(len * (sizeof(XChar2b) < sizeof(wchar_t)
2402 ? sizeof(wchar_t) : sizeof(XChar2b)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 buflen = len;
2404 }
2405 p = s;
2406 cells = 0;
2407 while (p < s + len)
2408 {
2409 c = utf_ptr2char(p);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002410# ifdef FEAT_XFONTSET
2411 if (current_fontset != NULL)
2412 {
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002413# ifdef SMALL_WCHAR_T
2414 if (c >= 0x10000)
Bram Moolenaara3227e22006-03-08 21:32:40 +00002415 c = 0xbf; /* show chars > 0xffff as ? */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002416# endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00002417 ((wchar_t *)buf)[wlen] = c;
2418 }
2419 else
2420# endif
2421 {
2422 if (c >= 0x10000)
2423 c = 0xbf; /* show chars > 0xffff as ? */
2424 ((XChar2b *)buf)[wlen].byte1 = (unsigned)c >> 8;
2425 ((XChar2b *)buf)[wlen].byte2 = c;
2426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 ++wlen;
2428 cells += utf_char2cells(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002429 p += utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 }
2431 }
2432 else if (has_mbyte)
2433 {
2434 cells = 0;
2435 for (p = s; p < s + len; )
2436 {
2437 cells += ptr2cells(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002438 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 }
2440 }
2441
2442#endif
2443
2444#ifdef FEAT_XFONTSET
2445 if (current_fontset != NULL)
2446 {
2447 /* Setup a clip rectangle to avoid spilling over in the next or
2448 * previous line. This is apparently needed for some fonts which are
2449 * used in a fontset. */
2450 XRectangle clip;
2451
2452 clip.x = 0;
2453 clip.y = 0;
2454 clip.height = gui.char_height;
2455 clip.width = gui.char_width * cells + 1;
2456 XSetClipRectangles(gui.dpy, gui.text_gc, FILL_X(col), FILL_Y(row),
2457 &clip, 1, Unsorted);
2458 }
2459#endif
2460
2461 if (flags & DRAW_TRANSP)
2462 {
2463#ifdef FEAT_MBYTE
2464 if (enc_utf8)
2465 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2466 TEXT_Y(row), buf, wlen);
2467 else
2468#endif
2469 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2470 TEXT_Y(row), (char *)s, len);
2471 }
2472 else if (p_linespace != 0
2473#ifdef FEAT_XFONTSET
2474 || current_fontset != NULL
2475#endif
2476 )
2477 {
2478 XSetForeground(gui.dpy, gui.text_gc, prev_bg_color);
2479 XFillRectangle(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
2480 FILL_Y(row), gui.char_width * cells, gui.char_height);
2481 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
Bram Moolenaarfb269802005-03-15 22:46:30 +00002482
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483#ifdef FEAT_MBYTE
2484 if (enc_utf8)
2485 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2486 TEXT_Y(row), buf, wlen);
2487 else
2488#endif
2489 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2490 TEXT_Y(row), (char *)s, len);
2491 }
2492 else
2493 {
2494 /* XmbDrawImageString has bug, don't use it for fontset. */
2495#ifdef FEAT_MBYTE
2496 if (enc_utf8)
2497 XDrawImageString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2498 TEXT_Y(row), buf, wlen);
2499 else
2500#endif
2501 XDrawImageString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2502 TEXT_Y(row), (char *)s, len);
2503 }
2504
2505 /* Bold trick: draw the text again with a one-pixel offset. */
2506 if (flags & DRAW_BOLD)
2507 {
2508#ifdef FEAT_MBYTE
2509 if (enc_utf8)
2510 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col) + 1,
2511 TEXT_Y(row), buf, wlen);
2512 else
2513#endif
2514 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col) + 1,
2515 TEXT_Y(row), (char *)s, len);
2516 }
2517
Bram Moolenaarfb269802005-03-15 22:46:30 +00002518 /* Undercurl: draw curl at the bottom of the character cell. */
2519 if (flags & DRAW_UNDERC)
2520 draw_curl(row, col, cells);
2521
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 /* Underline: draw a line at the bottom of the character cell. */
2523 if (flags & DRAW_UNDERL)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002524 {
2525 int y = FILL_Y(row + 1) - 1;
2526
2527 /* When p_linespace is 0, overwrite the bottom row of pixels.
2528 * Otherwise put the line just below the character. */
2529 if (p_linespace > 1)
2530 y -= p_linespace - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 XDrawLine(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
Bram Moolenaarfb269802005-03-15 22:46:30 +00002532 y, FILL_X(col + cells) - 1, y);
2533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
2535#ifdef FEAT_XFONTSET
2536 if (current_fontset != NULL)
2537 XSetClipMask(gui.dpy, gui.text_gc, None);
2538#endif
2539}
2540
2541/*
2542 * Return OK if the key with the termcap name "name" is supported.
2543 */
2544 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002545gui_mch_haskey(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546{
2547 int i;
2548
2549 for (i = 0; special_keys[i].key_sym != (KeySym)0; i++)
2550 if (name[0] == special_keys[i].vim_code0 &&
2551 name[1] == special_keys[i].vim_code1)
2552 return OK;
2553 return FAIL;
2554}
2555
2556/*
2557 * Return the text window-id and display. Only required for X-based GUI's
2558 */
2559 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002560gui_get_x11_windis(Window *win, Display **dis)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561{
2562 *win = XtWindow(vimShell);
2563 *dis = gui.dpy;
2564 return OK;
2565}
2566
2567 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002568gui_mch_beep(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569{
2570 XBell(gui.dpy, 0);
2571}
2572
2573 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002574gui_mch_flash(int msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575{
2576 /* Do a visual beep by reversing the foreground and background colors */
2577 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc, 0, 0,
2578 FILL_X((int)Columns) + gui.border_offset,
2579 FILL_Y((int)Rows) + gui.border_offset);
2580 XSync(gui.dpy, False);
2581 ui_delay((long)msec, TRUE); /* wait for a few msec */
2582 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc, 0, 0,
2583 FILL_X((int)Columns) + gui.border_offset,
2584 FILL_Y((int)Rows) + gui.border_offset);
2585}
2586
2587/*
2588 * Invert a rectangle from row r, column c, for nr rows and nc columns.
2589 */
2590 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002591gui_mch_invert_rectangle(
2592 int r,
2593 int c,
2594 int nr,
2595 int nc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596{
2597 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc,
2598 FILL_X(c), FILL_Y(r), (nc) * gui.char_width, (nr) * gui.char_height);
2599}
2600
2601/*
2602 * Iconify the GUI window.
2603 */
2604 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002605gui_mch_iconify(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606{
2607 XIconifyWindow(gui.dpy, XtWindow(vimShell), DefaultScreen(gui.dpy));
2608}
2609
2610#if defined(FEAT_EVAL) || defined(PROTO)
2611/*
2612 * Bring the Vim window to the foreground.
2613 */
2614 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002615gui_mch_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616{
2617 XMapRaised(gui.dpy, XtWindow(vimShell));
2618}
2619#endif
2620
2621/*
2622 * Draw a cursor without focus.
2623 */
2624 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002625gui_mch_draw_hollow_cursor(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626{
2627 int w = 1;
2628
2629#ifdef FEAT_MBYTE
2630 if (mb_lefthalve(gui.row, gui.col))
2631 w = 2;
2632#endif
2633 gui_mch_set_fg_color(color);
2634 XDrawRectangle(gui.dpy, gui.wid, gui.text_gc, FILL_X(gui.col),
2635 FILL_Y(gui.row), w * gui.char_width - 1, gui.char_height - 1);
2636}
2637
2638/*
2639 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
2640 * color "color".
2641 */
2642 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002643gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644{
2645 gui_mch_set_fg_color(color);
2646
2647 XFillRectangle(gui.dpy, gui.wid, gui.text_gc,
2648#ifdef FEAT_RIGHTLEFT
2649 /* vertical line should be on the right of current point */
2650 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
2651#endif
2652 FILL_X(gui.col),
2653 FILL_Y(gui.row) + gui.char_height - h,
2654 w, h);
2655}
2656
2657/*
2658 * Catch up with any queued X events. This may put keyboard input into the
2659 * input buffer, call resize call-backs, trigger timers etc. If there is
2660 * nothing in the X event queue (& no timers pending), then we return
2661 * immediately.
2662 */
2663 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002664gui_mch_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665{
2666 XtInputMask mask, desired;
2667
2668#ifdef ALT_X_INPUT
2669 if (suppress_alternate_input)
2670 desired = (XtIMXEvent | XtIMTimer);
2671 else
2672#endif
2673 desired = (XtIMAll);
2674 while ((mask = XtAppPending(app_context)) && (mask & desired)
2675 && !vim_is_input_buf_full())
2676 XtAppProcessEvent(app_context, desired);
2677}
2678
2679/*
2680 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2681 * from the keyboard.
2682 * wtime == -1 Wait forever.
2683 * wtime == 0 This should never happen.
2684 * wtime > 0 Wait wtime milliseconds for a character.
2685 * Returns OK if a character was found to be available within the given time,
2686 * or FAIL otherwise.
2687 */
2688 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002689gui_mch_wait_for_chars(long wtime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690{
Bram Moolenaar4231da42016-06-02 14:30:04 +02002691 int focus;
2692 int retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
2694 /*
2695 * Make this static, in case gui_x11_timer_cb is called after leaving
2696 * this function (otherwise a random value on the stack may be changed).
2697 */
2698 static int timed_out;
2699 XtIntervalId timer = (XtIntervalId)0;
2700 XtInputMask desired;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701
2702 timed_out = FALSE;
2703
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 if (wtime > 0)
2705 timer = XtAppAddTimeOut(app_context, (long_u)wtime, gui_x11_timer_cb,
2706 &timed_out);
2707
2708 focus = gui.in_focus;
2709#ifdef ALT_X_INPUT
2710 if (suppress_alternate_input)
2711 desired = (XtIMXEvent | XtIMTimer);
2712 else
2713#endif
2714 desired = (XtIMAll);
2715 while (!timed_out)
2716 {
2717 /* Stop or start blinking when focus changes */
2718 if (gui.in_focus != focus)
2719 {
2720 if (gui.in_focus)
2721 gui_mch_start_blink();
2722 else
2723 gui_mch_stop_blink();
2724 focus = gui.in_focus;
2725 }
2726
Bram Moolenaar93c88e02015-09-15 14:12:05 +02002727#ifdef MESSAGE_QUEUE
Bram Moolenaar4231da42016-06-02 14:30:04 +02002728# ifdef FEAT_TIMERS
2729 did_add_timer = FALSE;
2730# endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02002731 parse_queued_messages();
Bram Moolenaar4231da42016-06-02 14:30:04 +02002732# ifdef FEAT_TIMERS
2733 if (did_add_timer)
2734 /* Need to recompute the waiting time. */
2735 break;
2736# endif
Bram Moolenaar03531f72010-11-16 15:04:57 +01002737#endif
2738
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 /*
2740 * Don't use gui_mch_update() because then we will spin-lock until a
2741 * char arrives, instead we use XtAppProcessEvent() to hang until an
2742 * event arrives. No need to check for input_buf_full because we are
2743 * returning as soon as it contains a single char. Note that
2744 * XtAppNextEvent() may not be used because it will not return after a
2745 * timer event has arrived -- webb
2746 */
2747 XtAppProcessEvent(app_context, desired);
2748
2749 if (input_available())
2750 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002751 retval = OK;
2752 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 }
2754 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002755
2756 if (timer != (XtIntervalId)0 && !timed_out)
2757 XtRemoveTimeOut(timer);
2758
2759 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760}
2761
2762/*
2763 * Output routines.
2764 */
2765
2766/* Flush any output to the screen */
2767 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002768gui_mch_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769{
2770 XFlush(gui.dpy);
2771}
2772
2773/*
2774 * Clear a rectangular region of the screen from text pos (row1, col1) to
2775 * (row2, col2) inclusive.
2776 */
2777 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002778gui_mch_clear_block(
2779 int row1,
2780 int col1,
2781 int row2,
2782 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783{
2784 int x;
2785
2786 x = FILL_X(col1);
2787
2788 /* Clear one extra pixel at the far right, for when bold characters have
2789 * spilled over to the next column. */
2790 XFillRectangle(gui.dpy, gui.wid, gui.back_gc, x, FILL_Y(row1),
2791 (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1),
2792 (row2 - row1 + 1) * gui.char_height);
2793}
2794
2795 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002796gui_mch_clear_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797{
2798 XClearArea(gui.dpy, gui.wid, 0, 0, 0, 0, False);
2799}
2800
2801/*
2802 * Delete the given number of lines from the given row, scrolling up any
2803 * text further down within the scroll region.
2804 */
2805 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002806gui_mch_delete_lines(int row, int num_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807{
2808 if (gui.visibility == VisibilityFullyObscured)
2809 return; /* Can't see the window */
2810
2811 /* copy one extra pixel at the far right, for when bold has spilled
2812 * over */
2813 XCopyArea(gui.dpy, gui.wid, gui.wid, gui.text_gc,
2814 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
2815 gui.char_width * (gui.scroll_region_right - gui.scroll_region_left + 1)
2816 + (gui.scroll_region_right == Columns - 1),
2817 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1),
2818 FILL_X(gui.scroll_region_left), FILL_Y(row));
2819
2820 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
2821 gui.scroll_region_left,
2822 gui.scroll_region_bot, gui.scroll_region_right);
2823 gui_x11_check_copy_area();
2824}
2825
2826/*
2827 * Insert the given number of lines before the given row, scrolling down any
2828 * following text within the scroll region.
2829 */
2830 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002831gui_mch_insert_lines(int row, int num_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832{
2833 if (gui.visibility == VisibilityFullyObscured)
2834 return; /* Can't see the window */
2835
2836 /* copy one extra pixel at the far right, for when bold has spilled
2837 * over */
2838 XCopyArea(gui.dpy, gui.wid, gui.wid, gui.text_gc,
2839 FILL_X(gui.scroll_region_left), FILL_Y(row),
2840 gui.char_width * (gui.scroll_region_right - gui.scroll_region_left + 1)
2841 + (gui.scroll_region_right == Columns - 1),
2842 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1),
2843 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines));
2844
2845 gui_clear_block(row, gui.scroll_region_left,
2846 row + num_lines - 1, gui.scroll_region_right);
2847 gui_x11_check_copy_area();
2848}
2849
2850/*
2851 * Update the region revealed by scrolling up/down.
2852 */
2853 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002854gui_x11_check_copy_area(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855{
2856 XEvent event;
2857 XGraphicsExposeEvent *gevent;
2858
2859 if (gui.visibility != VisibilityPartiallyObscured)
2860 return;
2861
2862 XFlush(gui.dpy);
2863
2864 /* Wait to check whether the scroll worked or not */
2865 for (;;)
2866 {
2867 if (XCheckTypedEvent(gui.dpy, NoExpose, &event))
2868 return; /* The scroll worked. */
2869
2870 if (XCheckTypedEvent(gui.dpy, GraphicsExpose, &event))
2871 {
2872 gevent = (XGraphicsExposeEvent *)&event;
2873 gui_redraw(gevent->x, gevent->y, gevent->width, gevent->height);
2874 if (gevent->count == 0)
2875 return; /* This was the last expose event */
2876 }
2877 XSync(gui.dpy, False);
2878 }
2879}
2880
2881/*
2882 * X Selection stuff, for cutting and pasting text to other windows.
2883 */
2884
2885 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002886clip_mch_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887{
2888 clip_x11_lose_selection(vimShell, cbd);
2889}
2890
2891 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002892clip_mch_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893{
2894 return clip_x11_own_selection(vimShell, cbd);
2895}
2896
2897 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002898clip_mch_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899{
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002900 clip_x11_request_selection(vimShell, gui.dpy, cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901}
2902
2903 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002904clip_mch_set_selection(
2905 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906{
2907 clip_x11_set_selection(cbd);
2908}
2909
2910#if defined(FEAT_MENU) || defined(PROTO)
2911/*
2912 * Menu stuff.
2913 */
2914
2915/*
2916 * Make a menu either grey or not grey.
2917 */
2918 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002919gui_mch_menu_grey(vimmenu_T *menu, int grey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920{
2921 if (menu->id != (Widget)0)
2922 {
2923 gui_mch_menu_hidden(menu, False);
2924 if (grey
2925#ifdef FEAT_GUI_MOTIF
2926 || !menu->sensitive
2927#endif
2928 )
2929 XtSetSensitive(menu->id, False);
2930 else
2931 XtSetSensitive(menu->id, True);
2932 }
2933}
2934
2935/*
2936 * Make menu item hidden or not hidden
2937 */
2938 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002939gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940{
2941 if (menu->id != (Widget)0)
2942 {
2943 if (hidden)
2944 XtUnmanageChild(menu->id);
2945 else
2946 XtManageChild(menu->id);
2947 }
2948}
2949
2950/*
2951 * This is called after setting all the menus to grey/hidden or not.
2952 */
2953 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002954gui_mch_draw_menubar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955{
2956 /* Nothing to do in X */
2957}
2958
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002960gui_x11_menu_cb(
2961 Widget w UNUSED,
2962 XtPointer client_data,
2963 XtPointer call_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964{
2965 gui_menu_cb((vimmenu_T *)client_data);
2966}
2967
2968#endif /* FEAT_MENU */
2969
2970
2971
2972/*
2973 * Function called when window closed. Works like ":qa".
2974 * Should put up a requester!
2975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002977gui_x11_wm_protocol_handler(
2978 Widget w UNUSED,
2979 XtPointer client_data UNUSED,
2980 XEvent *event,
2981 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982{
2983 /*
2984 * Only deal with Client messages.
2985 */
2986 if (event->type != ClientMessage)
2987 return;
2988
2989 /*
2990 * The WM_SAVE_YOURSELF event arrives when the window manager wants to
2991 * exit. That can be cancelled though, thus Vim shouldn't exit here.
2992 * Just sync our swap files.
2993 */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002994 if ((Atom)((XClientMessageEvent *)event)->data.l[0] ==
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 wm_atoms[SAVE_YOURSELF_IDX])
2996 {
2997 out_flush();
2998 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2999
3000 /* Set the window's WM_COMMAND property, to let the window manager
3001 * know we are done saving ourselves. We don't want to be restarted,
3002 * thus set argv to NULL. */
3003 XSetCommand(gui.dpy, XtWindow(vimShell), NULL, 0);
3004 return;
3005 }
3006
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003007 if ((Atom)((XClientMessageEvent *)event)->data.l[0] !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 wm_atoms[DELETE_WINDOW_IDX])
3009 return;
3010
3011 gui_shell_closed();
3012}
3013
3014#ifdef FEAT_CLIENTSERVER
3015/*
3016 * Function called when property changed. Check for incoming commands
3017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003019gui_x11_send_event_handler(
3020 Widget w UNUSED,
3021 XtPointer client_data UNUSED,
3022 XEvent *event,
3023 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024{
3025 XPropertyEvent *e = (XPropertyEvent *) event;
3026
3027 if (e->type == PropertyNotify && e->window == commWindow
3028 && e->atom == commProperty && e->state == PropertyNewValue)
3029 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003030 serverEventProc(gui.dpy, event, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 }
3032}
3033#endif
3034
3035/*
3036 * Cursor blink functions.
3037 *
3038 * This is a simple state machine:
3039 * BLINK_NONE not blinking at all
3040 * BLINK_OFF blinking, cursor is not shown
3041 * BLINK_ON blinking, cursor is shown
3042 */
3043
3044#define BLINK_NONE 0
3045#define BLINK_OFF 1
3046#define BLINK_ON 2
3047
3048static int blink_state = BLINK_NONE;
3049static long_u blink_waittime = 700;
3050static long_u blink_ontime = 400;
3051static long_u blink_offtime = 250;
3052static XtIntervalId blink_timer = (XtIntervalId)0;
3053
Bram Moolenaar703a8042016-06-04 16:24:32 +02003054 int
3055gui_mch_is_blinking(void)
3056{
3057 return blink_state != BLINK_NONE;
3058}
3059
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +02003060 int
3061gui_mch_is_blink_off(void)
3062{
3063 return blink_state == BLINK_OFF;
3064}
3065
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 void
Bram Moolenaar02fdaea2016-01-30 18:13:55 +01003067gui_mch_set_blinking(long waittime, long on, long off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068{
3069 blink_waittime = waittime;
3070 blink_ontime = on;
3071 blink_offtime = off;
3072}
3073
3074/*
3075 * Stop the cursor blinking. Show the cursor if it wasn't shown.
3076 */
3077 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003078gui_mch_stop_blink(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079{
3080 if (blink_timer != (XtIntervalId)0)
3081 {
3082 XtRemoveTimeOut(blink_timer);
3083 blink_timer = (XtIntervalId)0;
3084 }
3085 if (blink_state == BLINK_OFF)
3086 gui_update_cursor(TRUE, FALSE);
3087 blink_state = BLINK_NONE;
3088}
3089
3090/*
3091 * Start the cursor blinking. If it was already blinking, this restarts the
3092 * waiting time and shows the cursor.
3093 */
3094 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003095gui_mch_start_blink(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096{
3097 if (blink_timer != (XtIntervalId)0)
3098 XtRemoveTimeOut(blink_timer);
3099 /* Only switch blinking on if none of the times is zero */
3100 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
3101 {
3102 blink_timer = XtAppAddTimeOut(app_context, blink_waittime,
3103 gui_x11_blink_cb, NULL);
3104 blink_state = BLINK_ON;
3105 gui_update_cursor(TRUE, FALSE);
3106 }
3107}
3108
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003110gui_x11_blink_cb(
3111 XtPointer timed_out UNUSED,
3112 XtIntervalId *interval_id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
3114 if (blink_state == BLINK_ON)
3115 {
3116 gui_undraw_cursor();
3117 blink_state = BLINK_OFF;
3118 blink_timer = XtAppAddTimeOut(app_context, blink_offtime,
3119 gui_x11_blink_cb, NULL);
3120 }
3121 else
3122 {
3123 gui_update_cursor(TRUE, FALSE);
3124 blink_state = BLINK_ON;
3125 blink_timer = XtAppAddTimeOut(app_context, blink_ontime,
3126 gui_x11_blink_cb, NULL);
3127 }
3128}
3129
3130/*
3131 * Return the RGB value of a pixel as a long.
3132 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003133 guicolor_T
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003134gui_mch_get_rgb(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135{
3136 XColor xc;
3137 Colormap colormap;
3138
3139 colormap = DefaultColormap(gui.dpy, XDefaultScreen(gui.dpy));
3140 xc.pixel = pixel;
3141 XQueryColor(gui.dpy, colormap, &xc);
3142
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003143 return (guicolor_T)(((xc.red & 0xff00) << 8) + (xc.green & 0xff00)
3144 + ((unsigned)xc.blue >> 8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145}
3146
3147/*
3148 * Add the callback functions.
3149 */
3150 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003151gui_x11_callbacks(Widget textArea, Widget vimForm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152{
3153 XtAddEventHandler(textArea, VisibilityChangeMask, FALSE,
3154 gui_x11_visibility_cb, (XtPointer)0);
3155
3156 XtAddEventHandler(textArea, ExposureMask, FALSE, gui_x11_expose_cb,
3157 (XtPointer)0);
3158
3159 XtAddEventHandler(vimShell, StructureNotifyMask, FALSE,
3160 gui_x11_resize_window_cb, (XtPointer)0);
3161
3162 XtAddEventHandler(vimShell, FocusChangeMask, FALSE, gui_x11_focus_change_cb,
3163 (XtPointer)0);
3164 /*
3165 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3166 * Only needed for some window managers.
3167 */
3168 if (vim_strchr(p_go, GO_POINTER) != NULL)
3169 {
3170 XtAddEventHandler(vimShell, LeaveWindowMask, FALSE, gui_x11_leave_cb,
3171 (XtPointer)0);
3172 XtAddEventHandler(textArea, LeaveWindowMask, FALSE, gui_x11_leave_cb,
3173 (XtPointer)0);
3174 XtAddEventHandler(textArea, EnterWindowMask, FALSE, gui_x11_enter_cb,
3175 (XtPointer)0);
3176 XtAddEventHandler(vimShell, EnterWindowMask, FALSE, gui_x11_enter_cb,
3177 (XtPointer)0);
3178 }
3179
3180 XtAddEventHandler(vimForm, KeyPressMask, FALSE, gui_x11_key_hit_cb,
3181 (XtPointer)0);
3182 XtAddEventHandler(textArea, KeyPressMask, FALSE, gui_x11_key_hit_cb,
3183 (XtPointer)0);
3184
3185 /* get pointer moved events from scrollbar, needed for 'mousefocus' */
3186 XtAddEventHandler(vimForm, PointerMotionMask,
3187 FALSE, gui_x11_mouse_cb, (XtPointer)1);
3188 XtAddEventHandler(textArea, ButtonPressMask | ButtonReleaseMask |
3189 ButtonMotionMask | PointerMotionMask,
3190 FALSE, gui_x11_mouse_cb, (XtPointer)0);
3191}
3192
3193/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003194 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003196 void
3197gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198{
3199 int rootx, rooty, winx, winy;
3200 Window root, child;
3201 unsigned int mask;
3202
3203 if (gui.wid && XQueryPointer(gui.dpy, gui.wid, &root, &child,
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003204 &rootx, &rooty, &winx, &winy, &mask)) {
3205 *x = winx;
3206 *y = winy;
3207 } else {
3208 *x = -1;
3209 *y = -1;
3210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211}
3212
3213 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003214gui_mch_setmouse(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215{
3216 if (gui.wid)
3217 XWarpPointer(gui.dpy, (Window)0, gui.wid, 0, 0, 0, 0, x, y);
3218}
3219
3220#if (defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)) || defined(PROTO)
3221 XButtonPressedEvent *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003222gui_x11_get_last_mouse_event(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
3224 return &last_mouse_event;
3225}
3226#endif
3227
3228#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
3229
3230/* Signs are currently always 2 chars wide. Hopefully the font is big enough
3231 * to provide room for the bitmap! */
3232# define SIGN_WIDTH (gui.char_width * 2)
3233
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003235gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236{
3237 XImage *sign;
3238
3239 if (gui.in_use && (sign = (XImage *)sign_get_image(typenr)) != NULL)
3240 {
3241 XClearArea(gui.dpy, gui.wid, TEXT_X(col), TEXT_Y(row) - sign->height,
3242 SIGN_WIDTH, gui.char_height, FALSE);
3243 XPutImage(gui.dpy, gui.wid, gui.text_gc, sign, 0, 0,
3244 TEXT_X(col) + (SIGN_WIDTH - sign->width) / 2,
3245 TEXT_Y(row) - sign->height,
3246 sign->width, sign->height);
3247 }
3248}
3249
3250 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003251gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252{
3253 XpmAttributes attrs;
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003254 XImage *sign = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 int status;
3256
3257 /*
3258 * Setup the color substitution table.
3259 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 if (signfile[0] != NUL && signfile[0] != '-')
3261 {
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003262 XpmColorSymbol color[5] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 {
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003264 {"none", NULL, 0},
3265 {"iconColor1", NULL, 0},
3266 {"bottomShadowColor", NULL, 0},
3267 {"topShadowColor", NULL, 0},
3268 {"selectColor", NULL, 0}
3269 };
3270 attrs.valuemask = XpmColorSymbols;
3271 attrs.numsymbols = 2;
3272 attrs.colorsymbols = color;
3273 attrs.colorsymbols[0].pixel = gui.back_pixel;
3274 attrs.colorsymbols[1].pixel = gui.norm_pixel;
3275 status = XpmReadFileToImage(gui.dpy, (char *)signfile,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 &sign, NULL, &attrs);
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003277 if (status == 0)
3278 {
3279 /* Sign width is fixed at two columns now.
3280 if (sign->width > gui.sign_width)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003281 gui.sign_width = sign->width + 8; */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 }
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003283 else
3284 EMSG(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 }
3286
3287 return (void *)sign;
3288}
3289
3290 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003291gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003293 XDestroyImage((XImage*)sign);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294}
3295#endif
3296
3297
3298#ifdef FEAT_MOUSESHAPE
3299/* The last set mouse pointer shape is remembered, to be used when it goes
3300 * from hidden to not hidden. */
3301static int last_shape = 0;
3302#endif
3303
3304/*
3305 * Use the blank mouse pointer or not.
3306 */
3307 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003308gui_mch_mousehide(
3309 int hide) /* TRUE = use blank ptr, FALSE = use parent ptr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310{
3311 if (gui.pointer_hidden != hide)
3312 {
3313 gui.pointer_hidden = hide;
3314 if (hide)
3315 XDefineCursor(gui.dpy, gui.wid, gui.blank_pointer);
3316 else
3317#ifdef FEAT_MOUSESHAPE
3318 mch_set_mouse_shape(last_shape);
3319#else
3320 XUndefineCursor(gui.dpy, gui.wid);
3321#endif
3322 }
3323}
3324
3325#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3326
3327/* Table for shape IDs. Keep in sync with the mshape_names[] table in
3328 * misc2.c! */
3329static int mshape_ids[] =
3330{
3331 XC_left_ptr, /* arrow */
3332 0, /* blank */
3333 XC_xterm, /* beam */
3334 XC_sb_v_double_arrow, /* updown */
3335 XC_sizing, /* udsizing */
3336 XC_sb_h_double_arrow, /* leftright */
3337 XC_sizing, /* lrsizing */
3338 XC_watch, /* busy */
3339 XC_X_cursor, /* no */
3340 XC_crosshair, /* crosshair */
3341 XC_hand1, /* hand1 */
3342 XC_hand2, /* hand2 */
3343 XC_pencil, /* pencil */
3344 XC_question_arrow, /* question */
3345 XC_right_ptr, /* right-arrow */
3346 XC_center_ptr, /* up-arrow */
3347 XC_left_ptr /* last one */
3348};
3349
3350 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003351mch_set_mouse_shape(int shape)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352{
3353 int id;
3354
3355 if (!gui.in_use)
3356 return;
3357
3358 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
3359 XDefineCursor(gui.dpy, gui.wid, gui.blank_pointer);
3360 else
3361 {
3362 if (shape >= MSHAPE_NUMBERED)
3363 {
3364 id = shape - MSHAPE_NUMBERED;
3365 if (id >= XC_num_glyphs)
3366 id = XC_left_ptr;
3367 else
3368 id &= ~1; /* they are always even (why?) */
3369 }
3370 else
3371 id = mshape_ids[shape];
3372
3373 XDefineCursor(gui.dpy, gui.wid, XCreateFontCursor(gui.dpy, id));
3374 }
3375 if (shape != MSHAPE_HIDE)
3376 last_shape = shape;
3377}
3378#endif
3379
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380#if (defined(FEAT_TOOLBAR) && defined(FEAT_BEVAL)) || defined(PROTO)
3381/*
3382 * Set the balloon-eval used for the tooltip of a toolbar menu item.
3383 * The check for a non-toolbar item was added, because there is a crash when
3384 * passing a normal menu item here. Can't explain that, but better avoid it.
3385 */
3386 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003387gui_mch_menu_set_tip(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388{
3389 if (menu->id != NULL && menu->parent != NULL
3390 && menu_is_toolbar(menu->parent->name))
3391 {
3392 /* Always destroy and create the balloon, in case the string was
3393 * changed. */
3394 if (menu->tip != NULL)
3395 {
3396 gui_mch_destroy_beval_area(menu->tip);
3397 menu->tip = NULL;
3398 }
3399 if (menu->strings[MENU_INDEX_TIP] != NULL)
3400 menu->tip = gui_mch_create_beval_area(
3401 menu->id,
3402 menu->strings[MENU_INDEX_TIP],
3403 NULL,
3404 NULL);
3405 }
3406}
3407#endif