blob: 34d584e78eabc6d50343c39f6b92772bc9c1dcb2 [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
Bram Moolenaar071d4272004-06-13 20:20:40 +000053#define VIM_NAME "vim"
54#define VIM_CLASS "Vim"
55
56/* Default resource values */
57#define DFLT_FONT "7x13"
58#ifdef FONTSET_ALWAYS
59# define DFLT_MENU_FONT XtDefaultFontSet
60#else
61# define DFLT_MENU_FONT XtDefaultFont
62#endif
63#define DFLT_TOOLTIP_FONT XtDefaultFontSet
64
65#ifdef FEAT_GUI_ATHENA
66# define DFLT_MENU_BG_COLOR "gray77"
67# define DFLT_MENU_FG_COLOR "black"
68# define DFLT_SCROLL_BG_COLOR "gray60"
69# define DFLT_SCROLL_FG_COLOR "gray77"
Bram Moolenaar46582282016-07-23 14:35:12 +020070# define DFLT_TOOLTIP_BG_COLOR "#ffff91"
71# define DFLT_TOOLTIP_FG_COLOR "#000000"
Bram Moolenaar071d4272004-06-13 20:20:40 +000072#else
73/* use the default (CDE) colors */
74# define DFLT_MENU_BG_COLOR ""
75# define DFLT_MENU_FG_COLOR ""
76# define DFLT_SCROLL_BG_COLOR ""
77# define DFLT_SCROLL_FG_COLOR ""
Bram Moolenaar46582282016-07-23 14:35:12 +020078# define DFLT_TOOLTIP_BG_COLOR "#ffff91"
79# define DFLT_TOOLTIP_FG_COLOR "#000000"
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#endif
81
82Widget vimShell = (Widget)0;
83
84static Atom wm_atoms[2]; /* Window Manager Atoms */
85#define DELETE_WINDOW_IDX 0 /* index in wm_atoms[] for WM_DELETE_WINDOW */
86#define SAVE_YOURSELF_IDX 1 /* index in wm_atoms[] for WM_SAVE_YOURSELF */
87
88#ifdef FEAT_XFONTSET
89/*
90 * We either draw with a fontset (when current_fontset != NULL) or with a
91 * normal font (current_fontset == NULL, use gui.text_gc and gui.back_gc).
92 */
93static XFontSet current_fontset = NULL;
94
95#define XDrawString(dpy, win, gc, x, y, str, n) \
96 do \
97 { \
98 if (current_fontset != NULL) \
99 XmbDrawString(dpy, win, current_fontset, gc, x, y, str, n); \
100 else \
101 XDrawString(dpy, win, gc, x, y, str, n); \
102 } while (0)
103
104#define XDrawString16(dpy, win, gc, x, y, str, n) \
105 do \
106 { \
107 if (current_fontset != NULL) \
108 XwcDrawString(dpy, win, current_fontset, gc, x, y, (wchar_t *)str, n); \
109 else \
Bram Moolenaara3227e22006-03-08 21:32:40 +0000110 XDrawString16(dpy, win, gc, x, y, (XChar2b *)str, n); \
111 } while (0)
112
113#define XDrawImageString16(dpy, win, gc, x, y, str, n) \
114 do \
115 { \
116 if (current_fontset != NULL) \
117 XwcDrawImageString(dpy, win, current_fontset, gc, x, y, (wchar_t *)str, n); \
118 else \
119 XDrawImageString16(dpy, win, gc, x, y, (XChar2b *)str, n); \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 } while (0)
121
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100122static int check_fontset_sanity(XFontSet fs);
123static int fontset_width(XFontSet fs);
124static int fontset_ascent(XFontSet fs);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#endif
126
127static guicolor_T prev_fg_color = INVALCOLOR;
128static guicolor_T prev_bg_color = INVALCOLOR;
Bram Moolenaarfb269802005-03-15 22:46:30 +0000129static guicolor_T prev_sp_color = INVALCOLOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130
131#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
132static XButtonPressedEvent last_mouse_event;
133#endif
134
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100135static void gui_x11_check_copy_area(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136#ifdef FEAT_CLIENTSERVER
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100137static void gui_x11_send_event_handler(Widget, XtPointer, XEvent *, Boolean *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100139static void gui_x11_wm_protocol_handler(Widget, XtPointer, XEvent *, Boolean *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100140static Cursor gui_x11_create_blank_mouse(void);
141static void draw_curl(int row, int col, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142
143
144/*
145 * Keycodes recognized by vim.
146 * NOTE: when changing this, the table in gui_gtk_x11.c probably needs the
147 * same change!
148 */
149static struct specialkey
150{
151 KeySym key_sym;
152 char_u vim_code0;
153 char_u vim_code1;
154} special_keys[] =
155{
156 {XK_Up, 'k', 'u'},
157 {XK_Down, 'k', 'd'},
158 {XK_Left, 'k', 'l'},
159 {XK_Right, 'k', 'r'},
160
161 {XK_F1, 'k', '1'},
162 {XK_F2, 'k', '2'},
163 {XK_F3, 'k', '3'},
164 {XK_F4, 'k', '4'},
165 {XK_F5, 'k', '5'},
166 {XK_F6, 'k', '6'},
167 {XK_F7, 'k', '7'},
168 {XK_F8, 'k', '8'},
169 {XK_F9, 'k', '9'},
170 {XK_F10, 'k', ';'},
171
172 {XK_F11, 'F', '1'},
173 {XK_F12, 'F', '2'},
174 {XK_F13, 'F', '3'},
175 {XK_F14, 'F', '4'},
176 {XK_F15, 'F', '5'},
177 {XK_F16, 'F', '6'},
178 {XK_F17, 'F', '7'},
179 {XK_F18, 'F', '8'},
180 {XK_F19, 'F', '9'},
181 {XK_F20, 'F', 'A'},
182
183 {XK_F21, 'F', 'B'},
184 {XK_F22, 'F', 'C'},
185 {XK_F23, 'F', 'D'},
186 {XK_F24, 'F', 'E'},
187 {XK_F25, 'F', 'F'},
188 {XK_F26, 'F', 'G'},
189 {XK_F27, 'F', 'H'},
190 {XK_F28, 'F', 'I'},
191 {XK_F29, 'F', 'J'},
192 {XK_F30, 'F', 'K'},
193
194 {XK_F31, 'F', 'L'},
195 {XK_F32, 'F', 'M'},
196 {XK_F33, 'F', 'N'},
197 {XK_F34, 'F', 'O'},
198 {XK_F35, 'F', 'P'}, /* keysymdef.h defines up to F35 */
199#ifdef SunXK_F36
200 {SunXK_F36, 'F', 'Q'},
201 {SunXK_F37, 'F', 'R'},
202#endif
203
204 {XK_Help, '%', '1'},
205 {XK_Undo, '&', '8'},
206 {XK_BackSpace, 'k', 'b'},
207 {XK_Insert, 'k', 'I'},
208 {XK_Delete, 'k', 'D'},
209 {XK_Home, 'k', 'h'},
210 {XK_End, '@', '7'},
211 {XK_Prior, 'k', 'P'},
212 {XK_Next, 'k', 'N'},
213 {XK_Print, '%', '9'},
214
215 /* Keypad keys: */
216#ifdef XK_KP_Left
217 {XK_KP_Left, 'k', 'l'},
218 {XK_KP_Right, 'k', 'r'},
219 {XK_KP_Up, 'k', 'u'},
220 {XK_KP_Down, 'k', 'd'},
221 {XK_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
222 {XK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
223 {XK_KP_Home, 'K', '1'},
224 {XK_KP_End, 'K', '4'},
225 {XK_KP_Prior, 'K', '3'},
226 {XK_KP_Next, 'K', '5'},
227
228 {XK_KP_Add, 'K', '6'},
229 {XK_KP_Subtract, 'K', '7'},
230 {XK_KP_Divide, 'K', '8'},
231 {XK_KP_Multiply, 'K', '9'},
232 {XK_KP_Enter, 'K', 'A'},
233 {XK_KP_Decimal, 'K', 'B'},
234
235 {XK_KP_0, 'K', 'C'},
236 {XK_KP_1, 'K', 'D'},
237 {XK_KP_2, 'K', 'E'},
238 {XK_KP_3, 'K', 'F'},
239 {XK_KP_4, 'K', 'G'},
240 {XK_KP_5, 'K', 'H'},
241 {XK_KP_6, 'K', 'I'},
242 {XK_KP_7, 'K', 'J'},
243 {XK_KP_8, 'K', 'K'},
244 {XK_KP_9, 'K', 'L'},
245#endif
246
247 /* End of list marker: */
248 {(KeySym)0, 0, 0}
249};
250
251#define XtNboldFont "boldFont"
252#define XtCBoldFont "BoldFont"
253#define XtNitalicFont "italicFont"
254#define XtCItalicFont "ItalicFont"
255#define XtNboldItalicFont "boldItalicFont"
256#define XtCBoldItalicFont "BoldItalicFont"
257#define XtNscrollbarWidth "scrollbarWidth"
258#define XtCScrollbarWidth "ScrollbarWidth"
259#define XtNmenuHeight "menuHeight"
260#define XtCMenuHeight "MenuHeight"
261#define XtNmenuFont "menuFont"
262#define XtCMenuFont "MenuFont"
263#define XtNmenuFontSet "menuFontSet"
264#define XtCMenuFontSet "MenuFontSet"
265
266
267/* Resources for setting the foreground and background colors of menus */
268#define XtNmenuBackground "menuBackground"
269#define XtCMenuBackground "MenuBackground"
270#define XtNmenuForeground "menuForeground"
271#define XtCMenuForeground "MenuForeground"
272
273/* Resources for setting the foreground and background colors of scrollbars */
274#define XtNscrollBackground "scrollBackground"
275#define XtCScrollBackground "ScrollBackground"
276#define XtNscrollForeground "scrollForeground"
277#define XtCScrollForeground "ScrollForeground"
278
279/* Resources for setting the foreground and background colors of tooltip */
280#define XtNtooltipBackground "tooltipBackground"
281#define XtCTooltipBackground "TooltipBackground"
282#define XtNtooltipForeground "tooltipForeground"
283#define XtCTooltipForeground "TooltipForeground"
284#define XtNtooltipFont "tooltipFont"
285#define XtCTooltipFont "TooltipFont"
286
287/*
288 * X Resources:
289 */
290static XtResource vim_resources[] =
291{
292 {
293 XtNforeground,
294 XtCForeground,
295 XtRPixel,
296 sizeof(Pixel),
297 XtOffsetOf(gui_T, def_norm_pixel),
298 XtRString,
299 XtDefaultForeground
300 },
301 {
302 XtNbackground,
303 XtCBackground,
304 XtRPixel,
305 sizeof(Pixel),
306 XtOffsetOf(gui_T, def_back_pixel),
307 XtRString,
308 XtDefaultBackground
309 },
310 {
311 XtNfont,
312 XtCFont,
313 XtRString,
314 sizeof(String *),
315 XtOffsetOf(gui_T, rsrc_font_name),
316 XtRImmediate,
317 XtDefaultFont
318 },
319 {
320 XtNboldFont,
321 XtCBoldFont,
322 XtRString,
323 sizeof(String *),
324 XtOffsetOf(gui_T, rsrc_bold_font_name),
325 XtRImmediate,
326 ""
327 },
328 {
329 XtNitalicFont,
330 XtCItalicFont,
331 XtRString,
332 sizeof(String *),
333 XtOffsetOf(gui_T, rsrc_ital_font_name),
334 XtRImmediate,
335 ""
336 },
337 {
338 XtNboldItalicFont,
339 XtCBoldItalicFont,
340 XtRString,
341 sizeof(String *),
342 XtOffsetOf(gui_T, rsrc_boldital_font_name),
343 XtRImmediate,
344 ""
345 },
346 {
347 XtNgeometry,
348 XtCGeometry,
349 XtRString,
350 sizeof(String *),
351 XtOffsetOf(gui_T, geom),
352 XtRImmediate,
353 ""
354 },
355 {
356 XtNreverseVideo,
357 XtCReverseVideo,
358 XtRBool,
359 sizeof(Bool),
360 XtOffsetOf(gui_T, rsrc_rev_video),
361 XtRImmediate,
362 (XtPointer)False
363 },
364 {
365 XtNborderWidth,
366 XtCBorderWidth,
367 XtRInt,
368 sizeof(int),
369 XtOffsetOf(gui_T, border_width),
370 XtRImmediate,
371 (XtPointer)2
372 },
373 {
374 XtNscrollbarWidth,
375 XtCScrollbarWidth,
376 XtRInt,
377 sizeof(int),
378 XtOffsetOf(gui_T, scrollbar_width),
379 XtRImmediate,
380 (XtPointer)SB_DEFAULT_WIDTH
381 },
382#ifdef FEAT_MENU
383# ifdef FEAT_GUI_ATHENA /* with Motif the height is always computed */
384 {
385 XtNmenuHeight,
386 XtCMenuHeight,
387 XtRInt,
388 sizeof(int),
389 XtOffsetOf(gui_T, menu_height),
390 XtRImmediate,
391 (XtPointer)MENU_DEFAULT_HEIGHT /* Should figure out at run time */
392 },
393# endif
394 {
395# ifdef FONTSET_ALWAYS
396 XtNmenuFontSet,
397 XtCMenuFontSet,
398#else
399 XtNmenuFont,
400 XtCMenuFont,
401#endif
402 XtRString,
403 sizeof(char *),
404 XtOffsetOf(gui_T, rsrc_menu_font_name),
405 XtRString,
406 DFLT_MENU_FONT
407 },
408#endif
409 {
410 XtNmenuForeground,
411 XtCMenuForeground,
412 XtRString,
413 sizeof(char *),
414 XtOffsetOf(gui_T, rsrc_menu_fg_name),
415 XtRString,
416 DFLT_MENU_FG_COLOR
417 },
418 {
419 XtNmenuBackground,
420 XtCMenuBackground,
421 XtRString,
422 sizeof(char *),
423 XtOffsetOf(gui_T, rsrc_menu_bg_name),
424 XtRString,
425 DFLT_MENU_BG_COLOR
426 },
427 {
428 XtNscrollForeground,
429 XtCScrollForeground,
430 XtRString,
431 sizeof(char *),
432 XtOffsetOf(gui_T, rsrc_scroll_fg_name),
433 XtRString,
434 DFLT_SCROLL_FG_COLOR
435 },
436 {
437 XtNscrollBackground,
438 XtCScrollBackground,
439 XtRString,
440 sizeof(char *),
441 XtOffsetOf(gui_T, rsrc_scroll_bg_name),
442 XtRString,
443 DFLT_SCROLL_BG_COLOR
444 },
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100445#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 {
447 XtNtooltipForeground,
448 XtCTooltipForeground,
449 XtRString,
450 sizeof(char *),
451 XtOffsetOf(gui_T, rsrc_tooltip_fg_name),
452 XtRString,
453 DFLT_TOOLTIP_FG_COLOR
454 },
455 {
456 XtNtooltipBackground,
457 XtCTooltipBackground,
458 XtRString,
459 sizeof(char *),
460 XtOffsetOf(gui_T, rsrc_tooltip_bg_name),
461 XtRString,
462 DFLT_TOOLTIP_BG_COLOR
463 },
464 {
465 XtNtooltipFont,
466 XtCTooltipFont,
467 XtRString,
468 sizeof(char *),
469 XtOffsetOf(gui_T, rsrc_tooltip_font_name),
470 XtRString,
471 DFLT_TOOLTIP_FONT
472 },
473 /* This one isn't really needed, keep for Sun Workshop? */
474 {
475 "balloonEvalFontSet",
476 XtCFontSet,
477 XtRFontSet,
478 sizeof(XFontSet),
479 XtOffsetOf(gui_T, tooltip_fontset),
480 XtRImmediate,
481 (XtPointer)NOFONTSET
482 },
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100483#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484#ifdef FEAT_XIM
485 {
486 "preeditType",
487 "PreeditType",
488 XtRString,
489 sizeof(char*),
490 XtOffsetOf(gui_T, rsrc_preedit_type_name),
491 XtRString,
492 (XtPointer)"OverTheSpot,OffTheSpot,Root"
493 },
494 {
495 "inputMethod",
496 "InputMethod",
497 XtRString,
498 sizeof(char*),
499 XtOffsetOf(gui_T, rsrc_input_method),
500 XtRString,
501 NULL
502 },
503#endif /* FEAT_XIM */
504};
505
506/*
507 * This table holds all the X GUI command line options allowed. This includes
508 * the standard ones so that we can skip them when vim is started without the
509 * GUI (but the GUI might start up later).
510 * When changing this, also update doc/vim_gui.txt and the usage message!!!
511 */
512static XrmOptionDescRec cmdline_options[] =
513{
514 /* We handle these options ourselves */
515 {"-bg", ".background", XrmoptionSepArg, NULL},
516 {"-background", ".background", XrmoptionSepArg, NULL},
517 {"-fg", ".foreground", XrmoptionSepArg, NULL},
518 {"-foreground", ".foreground", XrmoptionSepArg, NULL},
519 {"-fn", ".font", XrmoptionSepArg, NULL},
520 {"-font", ".font", XrmoptionSepArg, NULL},
521 {"-boldfont", ".boldFont", XrmoptionSepArg, NULL},
522 {"-italicfont", ".italicFont", XrmoptionSepArg, NULL},
523 {"-geom", ".geometry", XrmoptionSepArg, NULL},
524 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
525 {"-reverse", "*reverseVideo", XrmoptionNoArg, "True"},
526 {"-rv", "*reverseVideo", XrmoptionNoArg, "True"},
527 {"+reverse", "*reverseVideo", XrmoptionNoArg, "False"},
528 {"+rv", "*reverseVideo", XrmoptionNoArg, "False"},
529 {"-display", ".display", XrmoptionSepArg, NULL},
Bram Moolenaara5792f52005-11-23 21:25:05 +0000530 {"-iconic", ".iconic", XrmoptionNoArg, "True"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 {"-name", ".name", XrmoptionSepArg, NULL},
532 {"-bw", ".borderWidth", XrmoptionSepArg, NULL},
533 {"-borderwidth", ".borderWidth", XrmoptionSepArg, NULL},
534 {"-sw", ".scrollbarWidth", XrmoptionSepArg, NULL},
535 {"-scrollbarwidth", ".scrollbarWidth", XrmoptionSepArg, NULL},
536 {"-mh", ".menuHeight", XrmoptionSepArg, NULL},
537 {"-menuheight", ".menuHeight", XrmoptionSepArg, NULL},
538#ifdef FONTSET_ALWAYS
539 {"-mf", ".menuFontSet", XrmoptionSepArg, NULL},
540 {"-menufont", ".menuFontSet", XrmoptionSepArg, NULL},
541 {"-menufontset", ".menuFontSet", XrmoptionSepArg, NULL},
542#else
543 {"-mf", ".menuFont", XrmoptionSepArg, NULL},
544 {"-menufont", ".menuFont", XrmoptionSepArg, NULL},
545#endif
546 {"-xrm", NULL, XrmoptionResArg, NULL}
547};
548
549static int gui_argc = 0;
550static char **gui_argv = NULL;
551
552/*
553 * Call-back routines.
554 */
555
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100557gui_x11_timer_cb(
558 XtPointer timed_out,
559 XtIntervalId *interval_id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
561 *((int *)timed_out) = TRUE;
562}
563
Bram Moolenaar1dccf632017-08-27 17:38:27 +0200564#ifdef FEAT_JOB_CHANNEL
565 static void
566channel_poll_cb(
567 XtPointer client_data,
568 XtIntervalId *interval_id UNUSED)
569{
570 XtIntervalId *channel_timer = (XtIntervalId *)client_data;
571
572 /* Using an event handler for a channel that may be disconnected does
573 * not work, it hangs. Instead poll for messages. */
574 channel_handle_events(TRUE);
575 parse_queued_messages();
576
577 /* repeat */
578 *channel_timer = XtAppAddTimeOut(app_context, (long_u)20,
579 channel_poll_cb, client_data);
580}
581#endif
582
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100584gui_x11_visibility_cb(
585 Widget w UNUSED,
586 XtPointer dud UNUSED,
587 XEvent *event,
588 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589{
590 if (event->type != VisibilityNotify)
591 return;
592
593 gui.visibility = event->xvisibility.state;
594
595 /*
596 * When we do an XCopyArea(), and the window is partially obscured, we want
597 * to receive an event to tell us whether it worked or not.
598 */
599 XSetGraphicsExposures(gui.dpy, gui.text_gc,
600 gui.visibility != VisibilityUnobscured);
601
602 /* This is needed for when redrawing is slow. */
603 gui_mch_update();
604}
605
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100607gui_x11_expose_cb(
608 Widget w UNUSED,
609 XtPointer dud UNUSED,
610 XEvent *event,
611 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612{
613 XExposeEvent *gevent;
614 int new_x;
615
616 if (event->type != Expose)
617 return;
618
619 out_flush(); /* make sure all output has been processed */
620
621 gevent = (XExposeEvent *)event;
622 gui_redraw(gevent->x, gevent->y, gevent->width, gevent->height);
623
624 new_x = FILL_X(0);
625
626 /* Clear the border areas if needed */
627 if (gevent->x < new_x)
628 XClearArea(gui.dpy, gui.wid, 0, 0, new_x, 0, False);
629 if (gevent->y < FILL_Y(0))
630 XClearArea(gui.dpy, gui.wid, 0, 0, 0, FILL_Y(0), False);
631 if (gevent->x > FILL_X(Columns))
632 XClearArea(gui.dpy, gui.wid, FILL_X((int)Columns), 0, 0, 0, False);
633 if (gevent->y > FILL_Y(Rows))
634 XClearArea(gui.dpy, gui.wid, 0, FILL_Y((int)Rows), 0, 0, False);
635
636 /* This is needed for when redrawing is slow. */
637 gui_mch_update();
638}
639
Bram Moolenaar67c53842010-05-22 18:28:27 +0200640#if ((defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)) \
641 && defined(FEAT_GUI_MOTIF)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642/*
Bram Moolenaar231334e2005-07-25 20:46:57 +0000643 * This function fills in the XRectangle object with the current x,y
644 * coordinates and height, width so that an XtVaSetValues to the same shell of
Bram Moolenaar49325942007-05-10 19:19:59 +0000645 * those resources will restore the window to its former position and
Bram Moolenaar231334e2005-07-25 20:46:57 +0000646 * dimensions.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 *
Bram Moolenaar231334e2005-07-25 20:46:57 +0000648 * Note: This function may fail, in which case the XRectangle will be
649 * unchanged. Be sure to have the XRectangle set with the proper values for a
650 * failed condition prior to calling this function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 */
652 static void
653shellRectangle(Widget shell, XRectangle *r)
654{
655 Window rootw, shellw, child, parentw;
656 int absx, absy;
657 XWindowAttributes a;
658 Window *children;
659 unsigned int childrenCount;
660
661 shellw = XtWindow(shell);
662 if (shellw == 0)
663 return;
664 for (;;)
665 {
666 XQueryTree(XtDisplay(shell), shellw, &rootw, &parentw,
667 &children, &childrenCount);
668 XFree(children);
669 if (parentw == rootw)
670 break;
671 shellw = parentw;
672 }
673 XGetWindowAttributes(XtDisplay(shell), shellw, &a);
674 XTranslateCoordinates(XtDisplay(shell), shellw, a.root, 0, 0,
675 &absx, &absy, &child);
676 r->x = absx;
677 r->y = absy;
678 XtVaGetValues(shell, XmNheight, &r->height, XmNwidth, &r->width, NULL);
679}
680#endif
681
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100683gui_x11_resize_window_cb(
684 Widget w UNUSED,
685 XtPointer dud UNUSED,
686 XEvent *event,
687 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
689 static int lastWidth, lastHeight;
690
691 if (event->type != ConfigureNotify)
692 return;
693
694 if (event->xconfigure.width != lastWidth
695 || event->xconfigure.height != lastHeight)
696 {
697 lastWidth = event->xconfigure.width;
698 lastHeight = event->xconfigure.height;
699 gui_resize_shell(event->xconfigure.width, event->xconfigure.height
700#ifdef FEAT_XIM
701 - xim_get_status_area_height()
702#endif
703 );
704 }
705#ifdef FEAT_SUN_WORKSHOP
706 if (usingSunWorkShop)
707 {
708 XRectangle rec;
709
710 shellRectangle(w, &rec);
711 workshop_frame_moved(rec.x, rec.y, rec.width, rec.height);
712 }
713#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +0200714#if defined(FEAT_NETBEANS_INTG) && defined(FEAT_GUI_MOTIF)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200715 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 {
717 XRectangle rec;
718
719 shellRectangle(w, &rec);
720 netbeans_frame_moved(rec.x, rec.y);
721 }
722#endif
723#ifdef FEAT_XIM
724 xim_set_preedit();
725#endif
726}
727
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100729gui_x11_focus_change_cb(
730 Widget w UNUSED,
731 XtPointer data UNUSED,
732 XEvent *event,
733 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734{
735 gui_focus_change(event->type == FocusIn);
736}
737
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100739gui_x11_enter_cb(
740 Widget w UNUSED,
741 XtPointer data UNUSED,
742 XEvent *event UNUSED,
743 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
745 gui_focus_change(TRUE);
746}
747
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100749gui_x11_leave_cb(
750 Widget w UNUSED,
751 XtPointer data UNUSED,
752 XEvent *event UNUSED,
753 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754{
755 gui_focus_change(FALSE);
756}
757
758#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
759# if X_HAVE_UTF8_STRING
760# define USE_UTF8LOOKUP
761# endif
762#endif
763
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100765gui_x11_key_hit_cb(
766 Widget w UNUSED,
767 XtPointer dud UNUSED,
768 XEvent *event,
769 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770{
771 XKeyPressedEvent *ev_press;
772#ifdef FEAT_XIM
773 char_u string2[256];
774 char_u string_shortbuf[256];
775 char_u *string = string_shortbuf;
776 Boolean string_alloced = False;
777 Status status;
778#else
779 char_u string[4], string2[3];
780#endif
781 KeySym key_sym, key_sym2;
782 int len, len2;
783 int i;
784 int modifiers;
785 int key;
786
787 ev_press = (XKeyPressedEvent *)event;
788
789#ifdef FEAT_XIM
790 if (xic)
791 {
792# ifdef USE_UTF8LOOKUP
793 /* XFree86 4.0.2 or newer: Be able to get UTF-8 characters even when
794 * the locale isn't utf-8. */
795 if (enc_utf8)
796 len = Xutf8LookupString(xic, ev_press, (char *)string,
797 sizeof(string_shortbuf), &key_sym, &status);
798 else
799# endif
800 len = XmbLookupString(xic, ev_press, (char *)string,
801 sizeof(string_shortbuf), &key_sym, &status);
802 if (status == XBufferOverflow)
803 {
804 string = (char_u *)XtMalloc(len + 1);
805 string_alloced = True;
806# ifdef USE_UTF8LOOKUP
807 /* XFree86 4.0.2 or newer: Be able to get UTF-8 characters even
808 * when the locale isn't utf-8. */
809 if (enc_utf8)
810 len = Xutf8LookupString(xic, ev_press, (char *)string,
811 len, &key_sym, &status);
812 else
813# endif
814 len = XmbLookupString(xic, ev_press, (char *)string,
815 len, &key_sym, &status);
816 }
817 if (status == XLookupNone || status == XLookupChars)
818 key_sym = XK_VoidSymbol;
819
820# ifdef FEAT_MBYTE
821 /* Do conversion from 'termencoding' to 'encoding'. When using
822 * Xutf8LookupString() it has already been done. */
823 if (len > 0 && input_conv.vc_type != CONV_NONE
824# ifdef USE_UTF8LOOKUP
825 && !enc_utf8
826# endif
827 )
828 {
829 int maxlen = len * 4 + 40; /* guessed */
830 char_u *p = (char_u *)XtMalloc(maxlen);
831
832 mch_memmove(p, string, len);
833 if (string_alloced)
834 XtFree((char *)string);
835 string = p;
836 string_alloced = True;
837 len = convert_input(p, len, maxlen);
838 }
839# endif
840
841 /* Translate CSI to K_CSI, otherwise it could be recognized as the
842 * start of a special key. */
843 for (i = 0; i < len; ++i)
844 if (string[i] == CSI)
845 {
846 char_u *p = (char_u *)XtMalloc(len + 3);
847
848 mch_memmove(p, string, i + 1);
849 p[i + 1] = KS_EXTRA;
850 p[i + 2] = (int)KE_CSI;
851 mch_memmove(p + i + 3, string + i + 1, len - i);
852 if (string_alloced)
853 XtFree((char *)string);
854 string = p;
855 string_alloced = True;
856 i += 2;
857 len += 2;
858 }
859 }
860 else
861#endif
862 len = XLookupString(ev_press, (char *)string, sizeof(string),
863 &key_sym, NULL);
864
865#ifdef SunXK_F36
866 /*
867 * These keys have bogus lookup strings, and trapping them here is
868 * easier than trying to XRebindKeysym() on them with every possible
869 * combination of modifiers.
870 */
871 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
872 len = 0;
873#endif
874
875#ifdef FEAT_HANGULIN
876 if ((key_sym == XK_space) && (ev_press->state & ShiftMask))
877 {
878 hangul_input_state_toggle();
879 goto theend;
880 }
881#endif
882
883 if (key_sym == XK_space)
884 string[0] = ' '; /* Otherwise Ctrl-Space doesn't work */
885
886 /*
887 * Only on some machines ^_ requires Ctrl+Shift+minus. For consistency,
888 * allow just Ctrl+minus too.
889 */
890 if (key_sym == XK_minus && (ev_press->state & ControlMask))
891 string[0] = Ctrl__;
892
893#ifdef XK_ISO_Left_Tab
894 /* why do we get XK_ISO_Left_Tab instead of XK_Tab for shift-tab? */
895 if (key_sym == XK_ISO_Left_Tab)
896 {
897 key_sym = XK_Tab;
898 string[0] = TAB;
899 len = 1;
900 }
901#endif
902
903 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
904 * that already has the 8th bit set. And not when using a double-byte
905 * encoding, setting the 8th bit may make it the lead byte of a
906 * double-byte character. */
907 if (len == 1
908 && (ev_press->state & Mod1Mask)
909 && !(key_sym == XK_BackSpace || key_sym == XK_Delete)
910 && (string[0] & 0x80) == 0
911#ifdef FEAT_MBYTE
912 && !enc_dbcs
913#endif
914 )
915 {
916#if defined(FEAT_MENU) && defined(FEAT_GUI_MOTIF)
917 /* Ignore ALT keys when they are used for the menu only */
918 if (gui.menu_is_active
919 && (p_wak[0] == 'y'
920 || (p_wak[0] == 'm' && gui_is_menu_shortcut(string[0]))))
921 goto theend;
922#endif
923 /*
924 * Before we set the 8th bit, check to make sure the user doesn't
925 * already have a mapping defined for this sequence. We determine this
926 * by checking to see if the input would be the same without the
927 * Alt/Meta key.
928 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
929 */
930 ev_press->state &= ~Mod1Mask;
931 len2 = XLookupString(ev_press, (char *)string2, sizeof(string2),
932 &key_sym2, NULL);
933 if (key_sym2 == XK_space)
934 string2[0] = ' '; /* Otherwise Meta-Ctrl-Space doesn't work */
935 if ( len2 == 1
936 && string[0] == string2[0]
937 && !(key_sym == XK_Tab && (ev_press->state & ShiftMask)))
938 {
939 string[0] |= 0x80;
940#ifdef FEAT_MBYTE
941 if (enc_utf8) /* convert to utf-8 */
942 {
943 string[1] = string[0] & 0xbf;
944 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
945 if (string[1] == CSI)
946 {
947 string[2] = KS_EXTRA;
948 string[3] = (int)KE_CSI;
949 len = 4;
950 }
951 else
952 len = 2;
953 }
954#endif
955 }
956 else
957 ev_press->state |= Mod1Mask;
958 }
959
960 if (len == 1 && string[0] == CSI)
961 {
962 string[1] = KS_EXTRA;
963 string[2] = (int)KE_CSI;
964 len = -3;
965 }
966
967 /* Check for special keys. Also do this when len == 1 (key has an ASCII
968 * value) to detect backspace, delete and keypad keys. */
969 if (len == 0 || len == 1)
970 {
971 for (i = 0; special_keys[i].key_sym != (KeySym)0; i++)
972 {
973 if (special_keys[i].key_sym == key_sym)
974 {
975 string[0] = CSI;
976 string[1] = special_keys[i].vim_code0;
977 string[2] = special_keys[i].vim_code1;
978 len = -3;
979 break;
980 }
981 }
982 }
983
984 /* Unrecognised key is ignored. */
985 if (len == 0)
986 goto theend;
987
988 /* Special keys (and a few others) may have modifiers. Also when using a
989 * double-byte encoding (can't set the 8th bit). */
990 if (len == -3 || key_sym == XK_space || key_sym == XK_Tab
991 || key_sym == XK_Return || key_sym == XK_Linefeed
992 || key_sym == XK_Escape
993#ifdef FEAT_MBYTE
994 || (enc_dbcs && len == 1 && (ev_press->state & Mod1Mask))
995#endif
996 )
997 {
998 modifiers = 0;
999 if (ev_press->state & ShiftMask)
1000 modifiers |= MOD_MASK_SHIFT;
1001 if (ev_press->state & ControlMask)
1002 modifiers |= MOD_MASK_CTRL;
1003 if (ev_press->state & Mod1Mask)
1004 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001005 if (ev_press->state & Mod4Mask)
1006 modifiers |= MOD_MASK_META;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007
1008 /*
1009 * For some keys a shift modifier is translated into another key
1010 * code.
1011 */
1012 if (len == -3)
1013 key = TO_SPECIAL(string[1], string[2]);
1014 else
1015 key = string[0];
1016 key = simplify_key(key, &modifiers);
1017 if (key == CSI)
1018 key = K_CSI;
1019 if (IS_SPECIAL(key))
1020 {
1021 string[0] = CSI;
1022 string[1] = K_SECOND(key);
1023 string[2] = K_THIRD(key);
1024 len = 3;
1025 }
1026 else
1027 {
1028 string[0] = key;
1029 len = 1;
1030 }
1031
1032 if (modifiers != 0)
1033 {
1034 string2[0] = CSI;
1035 string2[1] = KS_MODIFIER;
1036 string2[2] = modifiers;
1037 add_to_input_buf(string2, 3);
1038 }
1039 }
1040
1041 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1042#ifdef UNIX
1043 || (intr_char != 0 && string[0] == intr_char)
1044#endif
1045 ))
1046 {
1047 trash_input_buf();
1048 got_int = TRUE;
1049 }
1050
1051 add_to_input_buf(string, len);
1052
1053 /*
1054 * blank out the pointer if necessary
1055 */
1056 if (p_mh)
1057 gui_mch_mousehide(TRUE);
1058
1059#if defined(FEAT_BEVAL_TIP)
1060 {
1061 BalloonEval *be;
1062
1063 if ((be = gui_mch_currently_showing_beval()) != NULL)
1064 gui_mch_unpost_balloon(be);
1065 }
1066#endif
1067theend:
1068 {} /* some compilers need a statement here */
1069#ifdef FEAT_XIM
1070 if (string_alloced)
1071 XtFree((char *)string);
1072#endif
1073}
1074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001076gui_x11_mouse_cb(
1077 Widget w UNUSED,
1078 XtPointer dud UNUSED,
1079 XEvent *event,
1080 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081{
1082 static XtIntervalId timer = (XtIntervalId)0;
1083 static int timed_out = TRUE;
1084
1085 int button;
1086 int repeated_click = FALSE;
1087 int x, y;
1088 int_u x_modifiers;
1089 int_u vim_modifiers;
1090
1091 if (event->type == MotionNotify)
1092 {
1093 /* Get the latest position, avoids lagging behind on a drag. */
1094 x = event->xmotion.x;
1095 y = event->xmotion.y;
1096 x_modifiers = event->xmotion.state;
1097 button = (x_modifiers & (Button1Mask | Button2Mask | Button3Mask))
1098 ? MOUSE_DRAG : ' ';
1099
1100 /*
1101 * if our pointer is currently hidden, then we should show it.
1102 */
1103 gui_mch_mousehide(FALSE);
1104
1105 if (button != MOUSE_DRAG) /* just moving the rodent */
1106 {
1107#ifdef FEAT_MENU
1108 if (dud) /* moved in vimForm */
1109 y -= gui.menu_height;
1110#endif
1111 gui_mouse_moved(x, y);
1112 return;
1113 }
1114 }
1115 else
1116 {
1117 x = event->xbutton.x;
1118 y = event->xbutton.y;
1119 if (event->type == ButtonPress)
1120 {
1121 /* Handle multiple clicks */
1122 if (!timed_out)
1123 {
1124 XtRemoveTimeOut(timer);
1125 repeated_click = TRUE;
1126 }
1127 timed_out = FALSE;
1128 timer = XtAppAddTimeOut(app_context, (long_u)p_mouset,
1129 gui_x11_timer_cb, &timed_out);
1130 switch (event->xbutton.button)
1131 {
Bram Moolenaar88e484b2015-11-24 15:38:44 +01001132 /* keep in sync with gui_gtk_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 case Button1: button = MOUSE_LEFT; break;
1134 case Button2: button = MOUSE_MIDDLE; break;
1135 case Button3: button = MOUSE_RIGHT; break;
1136 case Button4: button = MOUSE_4; break;
1137 case Button5: button = MOUSE_5; break;
Bram Moolenaar88e484b2015-11-24 15:38:44 +01001138 case 6: button = MOUSE_7; break;
1139 case 7: button = MOUSE_6; break;
1140 case 8: button = MOUSE_X1; break;
1141 case 9: button = MOUSE_X2; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 default:
1143 return; /* Unknown button */
1144 }
1145 }
1146 else if (event->type == ButtonRelease)
1147 button = MOUSE_RELEASE;
1148 else
1149 return; /* Unknown mouse event type */
1150
1151 x_modifiers = event->xbutton.state;
1152#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1153 last_mouse_event = event->xbutton;
1154#endif
1155 }
1156
1157 vim_modifiers = 0x0;
1158 if (x_modifiers & ShiftMask)
1159 vim_modifiers |= MOUSE_SHIFT;
1160 if (x_modifiers & ControlMask)
1161 vim_modifiers |= MOUSE_CTRL;
1162 if (x_modifiers & Mod1Mask) /* Alt or Meta key */
1163 vim_modifiers |= MOUSE_ALT;
1164
1165 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
1166}
1167
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168/*
1169 * End of call-back routines
1170 */
1171
1172/*
1173 * Parse the GUI related command-line arguments. Any arguments used are
1174 * deleted from argv, and *argc is decremented accordingly. This is called
1175 * when vim is started, whether or not the GUI has been started.
1176 */
1177 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001178gui_mch_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179{
1180 int arg;
1181 int i;
1182
1183 /*
1184 * Move all the entries in argv which are relevant to X into gui_argv.
1185 */
1186 gui_argc = 0;
1187 gui_argv = (char **)lalloc((long_u)(*argc * sizeof(char *)), FALSE);
1188 if (gui_argv == NULL)
1189 return;
1190 gui_argv[gui_argc++] = argv[0];
1191 arg = 1;
1192 while (arg < *argc)
1193 {
1194 /* Look for argv[arg] in cmdline_options[] table */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001195 for (i = 0; i < (int)XtNumber(cmdline_options); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 if (strcmp(argv[arg], cmdline_options[i].option) == 0)
1197 break;
1198
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001199 if (i < (int)XtNumber(cmdline_options))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 {
1201 /* Remember finding "-rv" or "-reverse" */
1202 if (strcmp("-rv", argv[arg]) == 0
1203 || strcmp("-reverse", argv[arg]) == 0)
1204 found_reverse_arg = TRUE;
1205 else if ((strcmp("-fn", argv[arg]) == 0
1206 || strcmp("-font", argv[arg]) == 0)
1207 && arg + 1 < *argc)
1208 font_argument = argv[arg + 1];
1209
1210 /* Found match in table, so move it into gui_argv */
1211 gui_argv[gui_argc++] = argv[arg];
1212 if (--*argc > arg)
1213 {
1214 mch_memmove(&argv[arg], &argv[arg + 1], (*argc - arg)
1215 * sizeof(char *));
1216 if (cmdline_options[i].argKind != XrmoptionNoArg)
1217 {
1218 /* Move the options argument as well */
1219 gui_argv[gui_argc++] = argv[arg];
1220 if (--*argc > arg)
1221 mch_memmove(&argv[arg], &argv[arg + 1], (*argc - arg)
1222 * sizeof(char *));
1223 }
1224 }
1225 argv[*argc] = NULL;
1226 }
1227 else
1228#ifdef FEAT_SUN_WORKSHOP
1229 if (strcmp("-ws", argv[arg]) == 0)
1230 {
1231 usingSunWorkShop++;
1232 p_acd = TRUE;
1233 gui.dofork = FALSE; /* don't fork() when starting GUI */
1234 mch_memmove(&argv[arg], &argv[arg + 1],
1235 (--*argc - arg) * sizeof(char *));
1236 argv[*argc] = NULL;
1237# ifdef WSDEBUG
1238 wsdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
1239 wsdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
1240# endif
1241 }
1242 else
1243#endif
1244#ifdef FEAT_NETBEANS_INTG
1245 if (strncmp("-nb", argv[arg], 3) == 0)
1246 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 gui.dofork = FALSE; /* don't fork() when starting GUI */
1248 netbeansArg = argv[arg];
1249 mch_memmove(&argv[arg], &argv[arg + 1],
1250 (--*argc - arg) * sizeof(char *));
1251 argv[*argc] = NULL;
1252 }
1253 else
1254#endif
1255 arg++;
1256 }
1257}
1258
1259#ifndef XtSpecificationRelease
1260# define CARDINAL (Cardinal *)
1261#else
1262# if XtSpecificationRelease == 4
1263# define CARDINAL (Cardinal *)
1264# else
1265# define CARDINAL (int *)
1266# endif
1267#endif
1268
1269/*
1270 * Check if the GUI can be started. Called before gvimrc is sourced.
1271 * Return OK or FAIL.
1272 */
1273 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001274gui_mch_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275{
1276#ifdef FEAT_XIM
1277 XtSetLanguageProc(NULL, NULL, NULL);
1278#endif
1279 open_app_context();
1280 if (app_context != NULL)
1281 gui.dpy = XtOpenDisplay(app_context, 0, VIM_NAME, VIM_CLASS,
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00001282 cmdline_options, XtNumber(cmdline_options),
1283 CARDINAL &gui_argc, gui_argv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284
1285 if (app_context == NULL || gui.dpy == NULL)
1286 {
1287 gui.dying = TRUE;
1288 EMSG(_(e_opendisp));
1289 return FAIL;
1290 }
1291 return OK;
1292}
1293
1294
1295#ifdef USE_XSMP
1296/*
1297 * Handle XSMP processing, de-registering the attachment upon error
1298 */
1299static XtInputId _xsmp_xtinputid;
1300
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001301static void local_xsmp_handle_requests(XtPointer c, int *s, XtInputId *i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001304local_xsmp_handle_requests(
1305 XtPointer c UNUSED,
1306 int *s UNUSED,
1307 XtInputId *i UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308{
1309 if (xsmp_handle_requests() == FAIL)
1310 XtRemoveInput(_xsmp_xtinputid);
1311}
1312#endif
1313
1314
1315/*
1316 * Initialise the X GUI. Create all the windows, set up all the call-backs etc.
1317 * Returns OK for success, FAIL when the GUI can't be started.
1318 */
1319 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001320gui_mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321{
1322 XtGCMask gc_mask;
1323 XGCValues gc_vals;
1324 int x, y, mask;
1325 unsigned w, h;
1326
1327#if 0
1328 /* Uncomment this to enable synchronous mode for debugging */
1329 XSynchronize(gui.dpy, True);
1330#endif
1331
1332 vimShell = XtVaAppCreateShell(VIM_NAME, VIM_CLASS,
1333 applicationShellWidgetClass, gui.dpy, NULL);
1334
1335 /*
1336 * Get the application resources
1337 */
1338 XtVaGetApplicationResources(vimShell, (XtPointer)&gui,
1339 vim_resources, XtNumber(vim_resources), NULL);
1340
1341 gui.scrollbar_height = gui.scrollbar_width;
1342
1343 /*
1344 * Get the colors ourselves. Using the automatic conversion doesn't
1345 * handle looking for approximate colors.
1346 */
1347 /* NOTE: These next few lines are an exact duplicate of gui_athena.c's
1348 * gui_mch_def_colors(). Why?
1349 */
1350 gui.menu_fg_pixel = gui_get_color((char_u *)gui.rsrc_menu_fg_name);
1351 gui.menu_bg_pixel = gui_get_color((char_u *)gui.rsrc_menu_bg_name);
1352 gui.scroll_fg_pixel = gui_get_color((char_u *)gui.rsrc_scroll_fg_name);
1353 gui.scroll_bg_pixel = gui_get_color((char_u *)gui.rsrc_scroll_bg_name);
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001354#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 gui.tooltip_fg_pixel = gui_get_color((char_u *)gui.rsrc_tooltip_fg_name);
1356 gui.tooltip_bg_pixel = gui_get_color((char_u *)gui.rsrc_tooltip_bg_name);
1357#endif
1358
1359#if defined(FEAT_MENU) && defined(FEAT_GUI_ATHENA)
1360 /* If the menu height was set, don't change it at runtime */
1361 if (gui.menu_height != MENU_DEFAULT_HEIGHT)
1362 gui.menu_height_fixed = TRUE;
1363#endif
1364
1365 /* Set default foreground and background colours */
1366 gui.norm_pixel = gui.def_norm_pixel;
1367 gui.back_pixel = gui.def_back_pixel;
1368
1369 /* Check if reverse video needs to be applied (on Sun it's done by X) */
1370 if (gui.rsrc_rev_video && gui_get_lightness(gui.back_pixel)
1371 > gui_get_lightness(gui.norm_pixel))
1372 {
1373 gui.norm_pixel = gui.def_back_pixel;
1374 gui.back_pixel = gui.def_norm_pixel;
1375 gui.def_norm_pixel = gui.norm_pixel;
1376 gui.def_back_pixel = gui.back_pixel;
1377 }
1378
1379 /* Get the colors from the "Normal", "Tooltip", "Scrollbar" and "Menu"
1380 * group (set in syntax.c or in a vimrc file) */
1381 set_normal_colors();
1382
1383 /*
1384 * Check that none of the colors are the same as the background color
1385 */
1386 gui_check_colors();
1387
1388 /*
1389 * Set up the GCs. The font attributes will be set in gui_init_font().
1390 */
1391 gc_mask = GCForeground | GCBackground;
1392 gc_vals.foreground = gui.norm_pixel;
1393 gc_vals.background = gui.back_pixel;
1394 gui.text_gc = XtGetGC(vimShell, gc_mask, &gc_vals);
1395
1396 gc_vals.foreground = gui.back_pixel;
1397 gc_vals.background = gui.norm_pixel;
1398 gui.back_gc = XtGetGC(vimShell, gc_mask, &gc_vals);
1399
1400 gc_mask |= GCFunction;
1401 gc_vals.foreground = gui.norm_pixel ^ gui.back_pixel;
1402 gc_vals.background = gui.norm_pixel ^ gui.back_pixel;
1403 gc_vals.function = GXxor;
1404 gui.invert_gc = XtGetGC(vimShell, gc_mask, &gc_vals);
1405
1406 gui.visibility = VisibilityUnobscured;
1407 x11_setup_atoms(gui.dpy);
1408
1409 if (gui_win_x != -1 && gui_win_y != -1)
1410 gui_mch_set_winpos(gui_win_x, gui_win_y);
1411
1412 /* Now adapt the supplied(?) geometry-settings */
1413 /* Added by Kjetil Jacobsen <kjetilja@stud.cs.uit.no> */
1414 if (gui.geom != NULL && *gui.geom != NUL)
1415 {
1416 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
1417 if (mask & WidthValue)
1418 Columns = w;
1419 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00001420 {
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001421 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00001422 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00001424 }
Bram Moolenaarc33916a2013-07-01 21:43:08 +02001425 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 /*
1427 * Set the (x,y) position of the main window only if specified in the
1428 * users geometry, so we get good defaults when they don't. This needs
1429 * to be done before the shell is popped up.
1430 */
1431 if (mask & (XValue|YValue))
1432 XtVaSetValues(vimShell, XtNgeometry, gui.geom, NULL);
1433 }
1434
1435 gui_x11_create_widgets();
1436
1437 /*
1438 * Add an icon to Vim (Marcel Douben: 11 May 1998).
1439 */
1440 if (vim_strchr(p_go, GO_ICON) != NULL)
1441 {
1442#ifndef HAVE_XPM
1443# include "vim_icon.xbm"
1444# include "vim_mask.xbm"
1445
1446 Arg arg[2];
1447
1448 XtSetArg(arg[0], XtNiconPixmap,
1449 XCreateBitmapFromData(gui.dpy,
1450 DefaultRootWindow(gui.dpy),
1451 (char *)vim_icon_bits,
1452 vim_icon_width,
1453 vim_icon_height));
1454 XtSetArg(arg[1], XtNiconMask,
1455 XCreateBitmapFromData(gui.dpy,
1456 DefaultRootWindow(gui.dpy),
1457 (char *)vim_mask_icon_bits,
1458 vim_mask_icon_width,
1459 vim_mask_icon_height));
1460 XtSetValues(vimShell, arg, (Cardinal)2);
1461#else
1462/* Use Pixmaps, looking much nicer. */
1463
1464/* If you get an error message here, you still need to unpack the runtime
1465 * archive! */
1466# ifdef magick
1467# undef magick
1468# endif
1469# define magick vim32x32
1470# include "../runtime/vim32x32.xpm"
1471# undef magick
1472# define magick vim16x16
1473# include "../runtime/vim16x16.xpm"
1474# undef magick
1475# define magick vim48x48
1476# include "../runtime/vim48x48.xpm"
1477# undef magick
1478
1479 static Pixmap icon = 0;
1480 static Pixmap icon_mask = 0;
1481 static char **magick = vim32x32;
1482 Window root_window;
1483 XIconSize *size;
1484 int number_sizes;
1485 Display *dsp;
1486 Screen *scr;
1487 XpmAttributes attr;
1488 Colormap cmap;
1489
1490 /*
1491 * Adjust the icon to the preferences of the actual window manager.
1492 */
1493 root_window = XRootWindowOfScreen(XtScreen(vimShell));
1494 if (XGetIconSizes(XtDisplay(vimShell), root_window,
1495 &size, &number_sizes) != 0)
1496 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 if (number_sizes > 0)
1498 {
Bram Moolenaar6f292662013-07-14 15:06:50 +02001499 if (size->max_height >= 48 && size->max_width >= 48)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 magick = vim48x48;
Bram Moolenaar6f292662013-07-14 15:06:50 +02001501 else if (size->max_height >= 32 && size->max_width >= 32)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 magick = vim32x32;
Bram Moolenaar6f292662013-07-14 15:06:50 +02001503 else if (size->max_height >= 16 && size->max_width >= 16)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 magick = vim16x16;
1505 }
1506 }
1507
1508 dsp = XtDisplay(vimShell);
1509 scr = XtScreen(vimShell);
1510
1511 cmap = DefaultColormap(dsp, DefaultScreen(dsp));
1512 XtVaSetValues(vimShell, XtNcolormap, cmap, NULL);
1513
1514 attr.valuemask = 0L;
1515 attr.valuemask = XpmCloseness | XpmReturnPixels | XpmColormap | XpmDepth;
1516 attr.closeness = 65535; /* accuracy isn't crucial */
1517 attr.colormap = cmap;
1518 attr.depth = DefaultDepthOfScreen(scr);
1519
1520 if (!icon)
Bram Moolenaare8208012008-06-20 09:59:25 +00001521 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 XpmCreatePixmapFromData(dsp, root_window, magick, &icon,
1523 &icon_mask, &attr);
Bram Moolenaare8208012008-06-20 09:59:25 +00001524 XpmFreeAttributes(&attr);
1525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
1527# ifdef FEAT_GUI_ATHENA
1528 XtVaSetValues(vimShell, XtNiconPixmap, icon, XtNiconMask, icon_mask, NULL);
1529# else
1530 XtVaSetValues(vimShell, XmNiconPixmap, icon, XmNiconMask, icon_mask, NULL);
1531# endif
1532#endif
1533 }
1534
1535 if (gui.color_approx)
1536 EMSG(_("Vim E458: Cannot allocate colormap entry, some colors may be incorrect"));
1537
1538#ifdef FEAT_SUN_WORKSHOP
1539 if (usingSunWorkShop)
1540 workshop_connect(app_context);
1541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001543#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 gui_init_tooltip_font();
1545#endif
1546#ifdef FEAT_MENU
1547 gui_init_menu_font();
1548#endif
1549
1550#ifdef USE_XSMP
1551 /* Attach listener on ICE connection */
1552 if (-1 != xsmp_icefd)
1553 _xsmp_xtinputid = XtAppAddInput(app_context, xsmp_icefd,
1554 (XtPointer)XtInputReadMask, local_xsmp_handle_requests, NULL);
1555#endif
1556
1557 return OK;
1558}
1559
1560/*
1561 * Called when starting the GUI fails after calling gui_mch_init().
1562 */
1563 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001564gui_mch_uninit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565{
1566 gui_x11_destroy_widgets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 XtCloseDisplay(gui.dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 gui.dpy = NULL;
1569 vimShell = (Widget)0;
Bram Moolenaare4bfca82009-02-24 03:12:40 +00001570 vim_free(gui_argv);
1571 gui_argv = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572}
1573
1574/*
1575 * Called when the foreground or background color has been changed.
1576 */
1577 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001578gui_mch_new_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579{
1580 long_u gc_mask;
1581 XGCValues gc_vals;
1582
1583 gc_mask = GCForeground | GCBackground;
1584 gc_vals.foreground = gui.norm_pixel;
1585 gc_vals.background = gui.back_pixel;
1586 if (gui.text_gc != NULL)
1587 XChangeGC(gui.dpy, gui.text_gc, gc_mask, &gc_vals);
1588
1589 gc_vals.foreground = gui.back_pixel;
1590 gc_vals.background = gui.norm_pixel;
1591 if (gui.back_gc != NULL)
1592 XChangeGC(gui.dpy, gui.back_gc, gc_mask, &gc_vals);
1593
1594 gc_mask |= GCFunction;
1595 gc_vals.foreground = gui.norm_pixel ^ gui.back_pixel;
1596 gc_vals.background = gui.norm_pixel ^ gui.back_pixel;
1597 gc_vals.function = GXxor;
1598 if (gui.invert_gc != NULL)
1599 XChangeGC(gui.dpy, gui.invert_gc, gc_mask, &gc_vals);
1600
1601 gui_x11_set_back_color();
1602}
1603
1604/*
1605 * Open the GUI window which was created by a call to gui_mch_init().
1606 */
1607 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001608gui_mch_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609{
1610 /* Actually open the window */
Bram Moolenaara5792f52005-11-23 21:25:05 +00001611 XtRealizeWidget(vimShell);
1612 XtManageChild(XtNameToWidget(vimShell, "*vimForm"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
1614 gui.wid = gui_x11_get_wid();
1615 gui.blank_pointer = gui_x11_create_blank_mouse();
1616
1617 /*
1618 * Add a callback for the Close item on the window managers menu, and the
1619 * save-yourself event.
1620 */
1621 wm_atoms[SAVE_YOURSELF_IDX] =
1622 XInternAtom(gui.dpy, "WM_SAVE_YOURSELF", False);
1623 wm_atoms[DELETE_WINDOW_IDX] =
1624 XInternAtom(gui.dpy, "WM_DELETE_WINDOW", False);
1625 XSetWMProtocols(gui.dpy, XtWindow(vimShell), wm_atoms, 2);
1626 XtAddEventHandler(vimShell, NoEventMask, True, gui_x11_wm_protocol_handler,
1627 NULL);
1628#ifdef HAVE_X11_XMU_EDITRES_H
1629 /*
1630 * Enable editres protocol (see "man editres").
1631 * Usually will need to add -lXmu to the linker line as well.
1632 */
1633 XtAddEventHandler(vimShell, (EventMask)0, True, _XEditResCheckMessages,
1634 (XtPointer)NULL);
1635#endif
1636
1637#ifdef FEAT_CLIENTSERVER
1638 if (serverName == NULL && serverDelayedStartName != NULL)
1639 {
1640 /* This is a :gui command in a plain vim with no previous server */
1641 commWindow = XtWindow(vimShell);
1642 (void)serverRegisterName(gui.dpy, serverDelayedStartName);
1643 }
1644 else
1645 {
1646 /*
1647 * Cannot handle "widget-less" windows with XtProcessEvent() we'll
1648 * have to change the "server" registration to that of the main window
1649 * If we have not registered a name yet, remember the window
1650 */
1651 serverChangeRegisteredWindow(gui.dpy, XtWindow(vimShell));
1652 }
1653 XtAddEventHandler(vimShell, PropertyChangeMask, False,
1654 gui_x11_send_event_handler, NULL);
1655#endif
1656
1657
1658#if defined(FEAT_MENU) && defined(FEAT_GUI_ATHENA)
1659 /* The Athena GUI needs this again after opening the window */
1660 gui_position_menu();
1661# ifdef FEAT_TOOLBAR
1662 gui_mch_set_toolbar_pos(0, gui.menu_height, gui.menu_width,
1663 gui.toolbar_height);
1664# endif
1665#endif
1666
1667 /* Get the colors for the highlight groups (gui_check_colors() might have
1668 * changed them) */
1669 highlight_gui_started(); /* re-init colors and fonts */
1670
1671#ifdef FEAT_HANGULIN
1672 hangul_keyboard_set();
1673#endif
1674#ifdef FEAT_XIM
1675 xim_init();
1676#endif
1677#ifdef FEAT_SUN_WORKSHOP
1678 workshop_postinit();
1679#endif
1680
1681 return OK;
1682}
1683
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001684#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685/*
1686 * Convert the tooltip fontset name to an XFontSet.
1687 */
1688 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001689gui_init_tooltip_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690{
1691 XrmValue from, to;
1692
1693 from.addr = (char *)gui.rsrc_tooltip_font_name;
1694 from.size = strlen(from.addr);
1695 to.addr = (XtPointer)&gui.tooltip_fontset;
1696 to.size = sizeof(XFontSet);
1697
1698 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontSet, &to) == False)
1699 {
1700 /* Failed. What to do? */
1701 }
1702}
1703#endif
1704
1705#if defined(FEAT_MENU) || defined(PROTO)
1706/* Convert the menu font/fontset name to an XFontStruct/XFontset */
1707 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001708gui_init_menu_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709{
1710 XrmValue from, to;
1711
1712#ifdef FONTSET_ALWAYS
1713 from.addr = (char *)gui.rsrc_menu_font_name;
1714 from.size = strlen(from.addr);
1715 to.addr = (XtPointer)&gui.menu_fontset;
1716 to.size = sizeof(GuiFontset);
1717
1718 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontSet, &to) == False)
1719 {
1720 /* Failed. What to do? */
1721 }
1722#else
1723 from.addr = (char *)gui.rsrc_menu_font_name;
1724 from.size = strlen(from.addr);
1725 to.addr = (XtPointer)&gui.menu_font;
1726 to.size = sizeof(GuiFont);
1727
1728 if (XtConvertAndStore(vimShell, XtRString, &from, XtRFontStruct, &to) == False)
1729 {
1730 /* Failed. What to do? */
1731 }
1732#endif
1733}
1734#endif
1735
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001737gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
1739#if 0
1740 /* Lesstif gives an error message here, and so does Solaris. The man page
1741 * says that this isn't needed when exiting, so just skip it. */
1742 XtCloseDisplay(gui.dpy);
1743#endif
Bram Moolenaare4bfca82009-02-24 03:12:40 +00001744 vim_free(gui_argv);
1745 gui_argv = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746}
1747
1748/*
1749 * Get the position of the top left corner of the window.
1750 */
1751 int
Bram Moolenaar02fdaea2016-01-30 18:13:55 +01001752gui_mch_get_winpos(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
1754 Dimension xpos, ypos;
1755
1756 XtVaGetValues(vimShell,
1757 XtNx, &xpos,
1758 XtNy, &ypos,
1759 NULL);
1760 *x = xpos;
1761 *y = ypos;
1762 return OK;
1763}
1764
1765/*
1766 * Set the position of the top left corner of the window to the given
1767 * coordinates.
1768 */
1769 void
Bram Moolenaar02fdaea2016-01-30 18:13:55 +01001770gui_mch_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771{
1772 XtVaSetValues(vimShell,
1773 XtNx, x,
1774 XtNy, y,
1775 NULL);
1776}
1777
1778 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001779gui_mch_set_shellsize(
1780 int width,
1781 int height,
1782 int min_width,
1783 int min_height,
1784 int base_width,
1785 int base_height,
1786 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00001788#ifdef FEAT_XIM
1789 height += xim_get_status_area_height(),
1790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 XtVaSetValues(vimShell,
1792 XtNwidthInc, gui.char_width,
1793 XtNheightInc, gui.char_height,
1794#if defined(XtSpecificationRelease) && XtSpecificationRelease >= 4
1795 XtNbaseWidth, base_width,
1796 XtNbaseHeight, base_height,
1797#endif
1798 XtNminWidth, min_width,
1799 XtNminHeight, min_height,
1800 XtNwidth, width,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 XtNheight, height,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 NULL);
1803}
1804
1805/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00001806 * Allow 10 pixels for horizontal borders, 'guiheadroom' for vertical borders.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 * Is there no way in X to find out how wide the borders really are?
1808 */
1809 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001810gui_mch_get_screen_dimensions(
1811 int *screen_w,
1812 int *screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813{
1814 *screen_w = DisplayWidth(gui.dpy, DefaultScreen(gui.dpy)) - 10;
1815 *screen_h = DisplayHeight(gui.dpy, DefaultScreen(gui.dpy)) - p_ghr;
1816}
1817
1818/*
1819 * Initialise vim to use the font "font_name". If it's NULL, pick a default
1820 * font.
1821 * If "fontset" is TRUE, load the "font_name" as a fontset.
1822 * Return FAIL if the font could not be loaded, OK otherwise.
1823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001825gui_mch_init_font(
1826 char_u *font_name,
1827 int do_fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
1829 XFontStruct *font = NULL;
1830
1831#ifdef FEAT_XFONTSET
1832 XFontSet fontset = NULL;
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001835#ifdef FEAT_GUI_MOTIF
1836 /* A font name equal "*" is indicating, that we should activate the font
1837 * selection dialogue to get a new font name. So let us do it here. */
1838 if (font_name != NULL && STRCMP(font_name, "*") == 0)
1839 font_name = gui_xm_select_font(hl_get_font_name());
1840#endif
1841
1842#ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 if (do_fontset)
1844 {
1845 /* If 'guifontset' is set, VIM treats all font specifications as if
1846 * they were fontsets, and 'guifontset' becomes the default. */
1847 if (font_name != NULL)
1848 {
1849 fontset = (XFontSet)gui_mch_get_fontset(font_name, FALSE, TRUE);
1850 if (fontset == NULL)
1851 return FAIL;
1852 }
1853 }
1854 else
1855#endif
1856 {
1857 if (font_name == NULL)
1858 {
1859 /*
1860 * If none of the fonts in 'font' could be loaded, try the one set
1861 * in the X resource, and finally just try using DFLT_FONT, which
1862 * will hopefully always be there.
1863 */
1864 font_name = gui.rsrc_font_name;
1865 font = (XFontStruct *)gui_mch_get_font(font_name, FALSE);
1866 if (font == NULL)
1867 font_name = (char_u *)DFLT_FONT;
1868 }
1869 if (font == NULL)
1870 font = (XFontStruct *)gui_mch_get_font(font_name, FALSE);
1871 if (font == NULL)
1872 return FAIL;
1873 }
1874
1875 gui_mch_free_font(gui.norm_font);
1876#ifdef FEAT_XFONTSET
1877 gui_mch_free_fontset(gui.fontset);
1878
1879 if (fontset != NULL)
1880 {
1881 gui.norm_font = NOFONT;
1882 gui.fontset = (GuiFontset)fontset;
1883 gui.char_width = fontset_width(fontset);
1884 gui.char_height = fontset_height(fontset) + p_linespace;
1885 gui.char_ascent = fontset_ascent(fontset) + p_linespace / 2;
1886 }
1887 else
1888#endif
1889 {
1890 gui.norm_font = (GuiFont)font;
1891#ifdef FEAT_XFONTSET
1892 gui.fontset = NOFONTSET;
1893#endif
1894 gui.char_width = font->max_bounds.width;
1895 gui.char_height = font->ascent + font->descent + p_linespace;
1896 gui.char_ascent = font->ascent + p_linespace / 2;
1897 }
1898
1899 hl_set_font_name(font_name);
1900
1901 /*
1902 * Try to load other fonts for bold, italic, and bold-italic.
1903 * We should also try to work out what font to use for these when they are
1904 * not specified by X resources, but we don't yet.
1905 */
1906 if (font_name == gui.rsrc_font_name)
1907 {
1908 if (gui.bold_font == NOFONT
1909 && gui.rsrc_bold_font_name != NULL
1910 && *gui.rsrc_bold_font_name != NUL)
1911 gui.bold_font = gui_mch_get_font(gui.rsrc_bold_font_name, FALSE);
1912 if (gui.ital_font == NOFONT
1913 && gui.rsrc_ital_font_name != NULL
1914 && *gui.rsrc_ital_font_name != NUL)
1915 gui.ital_font = gui_mch_get_font(gui.rsrc_ital_font_name, FALSE);
1916 if (gui.boldital_font == NOFONT
1917 && gui.rsrc_boldital_font_name != NULL
1918 && *gui.rsrc_boldital_font_name != NUL)
1919 gui.boldital_font = gui_mch_get_font(gui.rsrc_boldital_font_name,
1920 FALSE);
1921 }
1922 else
1923 {
1924 /* When not using the font specified by the resources, also don't use
1925 * the bold/italic fonts, otherwise setting 'guifont' will look very
1926 * strange. */
1927 if (gui.bold_font != NOFONT)
1928 {
1929 XFreeFont(gui.dpy, (XFontStruct *)gui.bold_font);
1930 gui.bold_font = NOFONT;
1931 }
1932 if (gui.ital_font != NOFONT)
1933 {
1934 XFreeFont(gui.dpy, (XFontStruct *)gui.ital_font);
1935 gui.ital_font = NOFONT;
1936 }
1937 if (gui.boldital_font != NOFONT)
1938 {
1939 XFreeFont(gui.dpy, (XFontStruct *)gui.boldital_font);
1940 gui.boldital_font = NOFONT;
1941 }
1942 }
1943
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001944#ifdef FEAT_GUI_MOTIF
1945 gui_motif_synch_fonts();
1946#endif
1947
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 return OK;
1949}
1950
1951/*
1952 * Get a font structure for highlighting.
1953 */
1954 GuiFont
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001955gui_mch_get_font(char_u *name, int giveErrorIfMissing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956{
1957 XFontStruct *font;
1958
1959 if (!gui.in_use || name == NULL) /* can't do this when GUI not running */
1960 return NOFONT;
1961
1962 font = XLoadQueryFont(gui.dpy, (char *)name);
1963
1964 if (font == NULL)
1965 {
1966 if (giveErrorIfMissing)
1967 EMSG2(_(e_font), name);
1968 return NOFONT;
1969 }
1970
1971#ifdef DEBUG
1972 printf("Font Information for '%s':\n", name);
1973 printf(" w = %d, h = %d, ascent = %d, descent = %d\n",
1974 font->max_bounds.width, font->ascent + font->descent,
1975 font->ascent, font->descent);
1976 printf(" max ascent = %d, max descent = %d, max h = %d\n",
1977 font->max_bounds.ascent, font->max_bounds.descent,
1978 font->max_bounds.ascent + font->max_bounds.descent);
1979 printf(" min lbearing = %d, min rbearing = %d\n",
1980 font->min_bounds.lbearing, font->min_bounds.rbearing);
1981 printf(" max lbearing = %d, max rbearing = %d\n",
1982 font->max_bounds.lbearing, font->max_bounds.rbearing);
1983 printf(" leftink = %d, rightink = %d\n",
1984 (font->min_bounds.lbearing < 0),
1985 (font->max_bounds.rbearing > font->max_bounds.width));
1986 printf("\n");
1987#endif
1988
1989 if (font->max_bounds.width != font->min_bounds.width)
1990 {
1991 EMSG2(_(e_fontwidth), name);
1992 XFreeFont(gui.dpy, font);
1993 return NOFONT;
1994 }
1995 return (GuiFont)font;
1996}
1997
Bram Moolenaar567e4de2004-12-31 21:01:02 +00001998#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00001999/*
2000 * Return the name of font "font" in allocated memory.
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002001 */
2002 char_u *
Bram Moolenaar87748452017-03-12 17:10:33 +01002003gui_mch_get_fontname(GuiFont font, char_u *name)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002004{
Bram Moolenaar87748452017-03-12 17:10:33 +01002005 char_u *ret = NULL;
2006
2007 if (name != NULL && font == NULL)
2008 {
2009 /* In this case, there's no way other than doing this. */
2010 ret = vim_strsave(name);
2011 }
2012 else if (font != NULL)
2013 {
2014 /* In this case, try to retrieve the XLFD corresponding to 'font'->fid;
2015 * if failed, use 'name' unless it's NULL. */
2016 unsigned long value = 0L;
2017
2018 if (XGetFontProperty(font, XA_FONT, &value))
2019 {
2020 char *xa_font_name = NULL;
2021
2022 xa_font_name = XGetAtomName(gui.dpy, value);
2023 if (xa_font_name != NULL)
2024 {
2025 ret = vim_strsave((char_u *)xa_font_name);
2026 XFree(xa_font_name);
2027 }
2028 else if (name != NULL)
2029 ret = vim_strsave(name);
2030 }
2031 else if (name != NULL)
2032 ret = vim_strsave(name);
2033 }
2034 return ret;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002035}
Bram Moolenaar567e4de2004-12-31 21:01:02 +00002036#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002037
Bram Moolenaar231334e2005-07-25 20:46:57 +00002038/*
2039 * Adjust gui.char_height (after 'linespace' was changed).
2040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002042gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043{
2044#ifdef FEAT_XFONTSET
2045 if (gui.fontset != NOFONTSET)
2046 {
2047 gui.char_height = fontset_height((XFontSet)gui.fontset) + p_linespace;
2048 gui.char_ascent = fontset_ascent((XFontSet)gui.fontset)
2049 + p_linespace / 2;
2050 }
2051 else
2052#endif
2053 {
2054 XFontStruct *font = (XFontStruct *)gui.norm_font;
2055
2056 gui.char_height = font->ascent + font->descent + p_linespace;
2057 gui.char_ascent = font->ascent + p_linespace / 2;
2058 }
2059 return OK;
2060}
2061
2062/*
2063 * Set the current text font.
2064 */
2065 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002066gui_mch_set_font(GuiFont font)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067{
2068 static Font prev_font = (Font)-1;
2069 Font fid = ((XFontStruct *)font)->fid;
2070
2071 if (fid != prev_font)
2072 {
2073 XSetFont(gui.dpy, gui.text_gc, fid);
2074 XSetFont(gui.dpy, gui.back_gc, fid);
2075 prev_font = fid;
2076 gui.char_ascent = ((XFontStruct *)font)->ascent + p_linespace / 2;
2077 }
2078#ifdef FEAT_XFONTSET
2079 current_fontset = (XFontSet)NULL;
2080#endif
2081}
2082
2083#if defined(FEAT_XFONTSET) || defined(PROTO)
2084/*
2085 * Set the current text fontset.
2086 * Adjust the ascent, in case it's different.
2087 */
2088 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002089gui_mch_set_fontset(GuiFontset fontset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090{
2091 current_fontset = (XFontSet)fontset;
2092 gui.char_ascent = fontset_ascent(current_fontset) + p_linespace / 2;
2093}
2094#endif
2095
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096/*
2097 * If a font is not going to be used, free its structure.
2098 */
2099 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002100gui_mch_free_font(GuiFont font)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101{
2102 if (font != NOFONT)
2103 XFreeFont(gui.dpy, (XFontStruct *)font);
2104}
2105
2106#if defined(FEAT_XFONTSET) || defined(PROTO)
2107/*
2108 * If a fontset is not going to be used, free its structure.
2109 */
2110 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002111gui_mch_free_fontset(GuiFontset fontset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112{
2113 if (fontset != NOFONTSET)
2114 XFreeFontSet(gui.dpy, (XFontSet)fontset);
2115}
2116
2117/*
2118 * Load the fontset "name".
2119 * Return a reference to the fontset, or NOFONTSET when failing.
2120 */
2121 GuiFontset
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002122gui_mch_get_fontset(
2123 char_u *name,
2124 int giveErrorIfMissing,
2125 int fixed_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126{
2127 XFontSet fontset;
2128 char **missing, *def_str;
2129 int num_missing;
2130
2131 if (!gui.in_use || name == NULL)
2132 return NOFONTSET;
2133
2134 fontset = XCreateFontSet(gui.dpy, (char *)name, &missing, &num_missing,
2135 &def_str);
2136 if (num_missing > 0)
2137 {
2138 int i;
2139
2140 if (giveErrorIfMissing)
2141 {
2142 EMSG2(_("E250: Fonts for the following charsets are missing in fontset %s:"), name);
2143 for (i = 0; i < num_missing; i++)
2144 EMSG2("%s", missing[i]);
2145 }
2146 XFreeStringList(missing);
2147 }
2148
2149 if (fontset == NULL)
2150 {
2151 if (giveErrorIfMissing)
2152 EMSG2(_(e_fontset), name);
2153 return NOFONTSET;
2154 }
2155
2156 if (fixed_width && check_fontset_sanity(fontset) == FAIL)
2157 {
2158 XFreeFontSet(gui.dpy, fontset);
2159 return NOFONTSET;
2160 }
2161 return (GuiFontset)fontset;
2162}
2163
2164/*
2165 * Check if fontset "fs" is fixed width.
2166 */
2167 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002168check_fontset_sanity(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169{
2170 XFontStruct **xfs;
2171 char **font_name;
2172 int fn;
2173 char *base_name;
2174 int i;
2175 int min_width;
2176 int min_font_idx = 0;
2177
2178 base_name = XBaseFontNameListOfFontSet(fs);
2179 fn = XFontsOfFontSet(fs, &xfs, &font_name);
2180 for (i = 0; i < fn; i++)
2181 {
2182 if (xfs[i]->max_bounds.width != xfs[i]->min_bounds.width)
2183 {
2184 EMSG2(_("E252: Fontset name: %s"), base_name);
2185 EMSG2(_("Font '%s' is not fixed-width"), font_name[i]);
2186 return FAIL;
2187 }
2188 }
2189 /* scan base font width */
2190 min_width = 32767;
2191 for (i = 0; i < fn; i++)
2192 {
2193 if (xfs[i]->max_bounds.width<min_width)
2194 {
2195 min_width = xfs[i]->max_bounds.width;
2196 min_font_idx = i;
2197 }
2198 }
2199 for (i = 0; i < fn; i++)
2200 {
2201 if ( xfs[i]->max_bounds.width != 2 * min_width
2202 && xfs[i]->max_bounds.width != min_width)
2203 {
Bram Moolenaar9d438d32013-06-13 21:57:20 +02002204 EMSG2(_("E253: Fontset name: %s"), base_name);
2205 EMSG2(_("Font0: %s"), font_name[min_font_idx]);
2206 EMSG2(_("Font1: %s"), font_name[i]);
2207 EMSGN(_("Font%ld width is not twice that of font0"), i);
2208 EMSGN(_("Font0 width: %ld"), xfs[min_font_idx]->max_bounds.width);
2209 EMSGN(_("Font1 width: %ld"), xfs[i]->max_bounds.width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 return FAIL;
2211 }
2212 }
2213 /* it seems ok. Good Luck!! */
2214 return OK;
2215}
2216
2217 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002218fontset_width(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219{
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002220 return XmbTextEscapement(fs, "Vim", 3) / 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221}
2222
2223 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002224fontset_height(
2225 XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226{
2227 XFontSetExtents *extents;
2228
2229 extents = XExtentsOfFontSet(fs);
2230 return extents->max_logical_extent.height;
2231}
2232
2233#if (defined(FONTSET_ALWAYS) && defined(FEAT_GUI_ATHENA) \
2234 && defined(FEAT_MENU)) || defined(PROTO)
2235/*
2236 * Returns the bounding box height around the actual glyph image of all
2237 * characters in all fonts of the fontset.
2238 */
2239 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002240fontset_height2(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241{
2242 XFontSetExtents *extents;
2243
2244 extents = XExtentsOfFontSet(fs);
2245 return extents->max_ink_extent.height;
2246}
2247#endif
2248
2249/* NOT USED YET
2250 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002251fontset_descent(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252{
2253 XFontSetExtents *extents;
2254
2255 extents = XExtentsOfFontSet (fs);
2256 return extents->max_logical_extent.height + extents->max_logical_extent.y;
2257}
2258*/
2259
2260 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002261fontset_ascent(XFontSet fs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262{
2263 XFontSetExtents *extents;
2264
2265 extents = XExtentsOfFontSet(fs);
2266 return -extents->max_logical_extent.y;
2267}
2268
2269#endif /* FEAT_XFONTSET */
2270
2271/*
2272 * Return the Pixel value (color) for the given color name.
2273 * Return INVALCOLOR for error.
2274 */
2275 guicolor_T
Bram Moolenaar46582282016-07-23 14:35:12 +02002276gui_mch_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277{
Bram Moolenaar46582282016-07-23 14:35:12 +02002278 guicolor_T requested;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279
2280 /* can't do this when GUI not running */
Bram Moolenaar46582282016-07-23 14:35:12 +02002281 if (!gui.in_use || name == NULL || *name == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 return INVALCOLOR;
2283
Bram Moolenaar46582282016-07-23 14:35:12 +02002284 requested = gui_get_color_cmn(name);
2285 if (requested == INVALCOLOR)
2286 return INVALCOLOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002288 return gui_mch_get_rgb_color(
Bram Moolenaar46582282016-07-23 14:35:12 +02002289 (requested & 0xff0000) >> 16,
2290 (requested & 0xff00) >> 8,
2291 requested & 0xff);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002292}
2293
2294/*
2295 * Return the Pixel value (color) for the given RGB values.
2296 * Return INVALCOLOR for error.
2297 */
2298 guicolor_T
2299gui_mch_get_rgb_color(int r, int g, int b)
2300{
2301 char spec[8]; /* space enough to hold "#RRGGBB" */
Bram Moolenaard60547b2017-07-24 20:15:30 +02002302 XColor available;
2303 Colormap colormap;
Bram Moolenaar26af85d2017-07-23 16:45:10 +02002304
2305 vim_snprintf(spec, sizeof(spec), "#%.2x%.2x%.2x", r, g, b);
Bram Moolenaar46582282016-07-23 14:35:12 +02002306 colormap = DefaultColormap(gui.dpy, DefaultScreen(gui.dpy));
2307 if (XParseColor(gui.dpy, colormap, (char *)spec, &available) != 0
2308 && XAllocColor(gui.dpy, colormap, &available) != 0)
2309 return (guicolor_T)available.pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310
2311 return INVALCOLOR;
2312}
2313
2314/*
Bram Moolenaarfb269802005-03-15 22:46:30 +00002315 * Set the current text foreground color.
2316 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002318gui_mch_set_fg_color(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319{
2320 if (color != prev_fg_color)
2321 {
2322 XSetForeground(gui.dpy, gui.text_gc, (Pixel)color);
2323 prev_fg_color = color;
2324 }
2325}
2326
2327/*
2328 * Set the current text background color.
2329 */
2330 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002331gui_mch_set_bg_color(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332{
2333 if (color != prev_bg_color)
2334 {
2335 XSetBackground(gui.dpy, gui.text_gc, (Pixel)color);
2336 prev_bg_color = color;
2337 }
2338}
2339
2340/*
Bram Moolenaarfb269802005-03-15 22:46:30 +00002341 * Set the current text special color.
2342 */
2343 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002344gui_mch_set_sp_color(guicolor_T color)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002345{
2346 prev_sp_color = color;
2347}
2348
2349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 * create a mouse pointer that is blank
2351 */
2352 static Cursor
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002353gui_x11_create_blank_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354{
2355 Pixmap blank_pixmap = XCreatePixmap(gui.dpy, gui.wid, 1, 1, 1);
2356 GC gc = XCreateGC(gui.dpy, blank_pixmap, (unsigned long)0, (XGCValues*)0);
2357 XDrawPoint(gui.dpy, blank_pixmap, gc, 0, 0);
2358 XFreeGC(gui.dpy, gc);
2359 return XCreatePixmapCursor(gui.dpy, blank_pixmap, blank_pixmap,
2360 (XColor*)&gui.norm_pixel, (XColor*)&gui.norm_pixel, 0, 0);
2361}
2362
Bram Moolenaarfb269802005-03-15 22:46:30 +00002363/*
2364 * Draw a curled line at the bottom of the character cell.
2365 */
2366 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002367draw_curl(int row, int col, int cells)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002368{
2369 int i;
2370 int offset;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002371 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarfb269802005-03-15 22:46:30 +00002372
2373 XSetForeground(gui.dpy, gui.text_gc, prev_sp_color);
2374 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
2375 {
2376 offset = val[i % 8];
2377 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, i,
2378 FILL_Y(row + 1) - 1 - offset);
2379 }
2380 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
2381}
2382
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002384gui_mch_draw_string(
2385 int row,
2386 int col,
2387 char_u *s,
2388 int len,
2389 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390{
2391 int cells = len;
2392#ifdef FEAT_MBYTE
Bram Moolenaara3227e22006-03-08 21:32:40 +00002393 static void *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 static int buflen = 0;
2395 char_u *p;
2396 int wlen = 0;
2397 int c;
2398
2399 if (enc_utf8)
2400 {
2401 /* Convert UTF-8 byte sequence to 16 bit characters for the X
2402 * functions. Need a buffer for the 16 bit characters. Keep it
2403 * between calls, because allocating it each time is slow. */
2404 if (buflen < len)
2405 {
2406 XtFree((char *)buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002407 buf = (void *)XtMalloc(len * (sizeof(XChar2b) < sizeof(wchar_t)
2408 ? sizeof(wchar_t) : sizeof(XChar2b)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 buflen = len;
2410 }
2411 p = s;
2412 cells = 0;
2413 while (p < s + len)
2414 {
2415 c = utf_ptr2char(p);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002416# ifdef FEAT_XFONTSET
2417 if (current_fontset != NULL)
2418 {
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002419# ifdef SMALL_WCHAR_T
2420 if (c >= 0x10000)
Bram Moolenaara3227e22006-03-08 21:32:40 +00002421 c = 0xbf; /* show chars > 0xffff as ? */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002422# endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00002423 ((wchar_t *)buf)[wlen] = c;
2424 }
2425 else
2426# endif
2427 {
2428 if (c >= 0x10000)
2429 c = 0xbf; /* show chars > 0xffff as ? */
2430 ((XChar2b *)buf)[wlen].byte1 = (unsigned)c >> 8;
2431 ((XChar2b *)buf)[wlen].byte2 = c;
2432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 ++wlen;
2434 cells += utf_char2cells(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002435 p += utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
2437 }
2438 else if (has_mbyte)
2439 {
2440 cells = 0;
2441 for (p = s; p < s + len; )
2442 {
2443 cells += ptr2cells(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002444 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 }
2446 }
2447
2448#endif
2449
2450#ifdef FEAT_XFONTSET
2451 if (current_fontset != NULL)
2452 {
2453 /* Setup a clip rectangle to avoid spilling over in the next or
2454 * previous line. This is apparently needed for some fonts which are
2455 * used in a fontset. */
2456 XRectangle clip;
2457
2458 clip.x = 0;
2459 clip.y = 0;
2460 clip.height = gui.char_height;
2461 clip.width = gui.char_width * cells + 1;
2462 XSetClipRectangles(gui.dpy, gui.text_gc, FILL_X(col), FILL_Y(row),
2463 &clip, 1, Unsorted);
2464 }
2465#endif
2466
2467 if (flags & DRAW_TRANSP)
2468 {
2469#ifdef FEAT_MBYTE
2470 if (enc_utf8)
2471 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2472 TEXT_Y(row), buf, wlen);
2473 else
2474#endif
2475 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2476 TEXT_Y(row), (char *)s, len);
2477 }
2478 else if (p_linespace != 0
2479#ifdef FEAT_XFONTSET
2480 || current_fontset != NULL
2481#endif
2482 )
2483 {
2484 XSetForeground(gui.dpy, gui.text_gc, prev_bg_color);
2485 XFillRectangle(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
2486 FILL_Y(row), gui.char_width * cells, gui.char_height);
2487 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
Bram Moolenaarfb269802005-03-15 22:46:30 +00002488
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489#ifdef FEAT_MBYTE
2490 if (enc_utf8)
2491 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2492 TEXT_Y(row), buf, wlen);
2493 else
2494#endif
2495 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2496 TEXT_Y(row), (char *)s, len);
2497 }
2498 else
2499 {
2500 /* XmbDrawImageString has bug, don't use it for fontset. */
2501#ifdef FEAT_MBYTE
2502 if (enc_utf8)
2503 XDrawImageString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2504 TEXT_Y(row), buf, wlen);
2505 else
2506#endif
2507 XDrawImageString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col),
2508 TEXT_Y(row), (char *)s, len);
2509 }
2510
2511 /* Bold trick: draw the text again with a one-pixel offset. */
2512 if (flags & DRAW_BOLD)
2513 {
2514#ifdef FEAT_MBYTE
2515 if (enc_utf8)
2516 XDrawString16(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col) + 1,
2517 TEXT_Y(row), buf, wlen);
2518 else
2519#endif
2520 XDrawString(gui.dpy, gui.wid, gui.text_gc, TEXT_X(col) + 1,
2521 TEXT_Y(row), (char *)s, len);
2522 }
2523
Bram Moolenaarfb269802005-03-15 22:46:30 +00002524 /* Undercurl: draw curl at the bottom of the character cell. */
2525 if (flags & DRAW_UNDERC)
2526 draw_curl(row, col, cells);
2527
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 /* Underline: draw a line at the bottom of the character cell. */
2529 if (flags & DRAW_UNDERL)
Bram Moolenaarfb269802005-03-15 22:46:30 +00002530 {
2531 int y = FILL_Y(row + 1) - 1;
2532
2533 /* When p_linespace is 0, overwrite the bottom row of pixels.
2534 * Otherwise put the line just below the character. */
2535 if (p_linespace > 1)
2536 y -= p_linespace - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 XDrawLine(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
Bram Moolenaarfb269802005-03-15 22:46:30 +00002538 y, FILL_X(col + cells) - 1, y);
2539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002541 if (flags & DRAW_STRIKE)
2542 {
2543 int y = FILL_Y(row + 1) - gui.char_height/2;
2544
2545 XSetForeground(gui.dpy, gui.text_gc, prev_sp_color);
2546 XDrawLine(gui.dpy, gui.wid, gui.text_gc, FILL_X(col),
2547 y, FILL_X(col + cells) - 1, y);
2548 XSetForeground(gui.dpy, gui.text_gc, prev_fg_color);
2549 }
2550
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551#ifdef FEAT_XFONTSET
2552 if (current_fontset != NULL)
2553 XSetClipMask(gui.dpy, gui.text_gc, None);
2554#endif
2555}
2556
2557/*
2558 * Return OK if the key with the termcap name "name" is supported.
2559 */
2560 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002561gui_mch_haskey(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562{
2563 int i;
2564
2565 for (i = 0; special_keys[i].key_sym != (KeySym)0; i++)
2566 if (name[0] == special_keys[i].vim_code0 &&
2567 name[1] == special_keys[i].vim_code1)
2568 return OK;
2569 return FAIL;
2570}
2571
2572/*
2573 * Return the text window-id and display. Only required for X-based GUI's
2574 */
2575 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002576gui_get_x11_windis(Window *win, Display **dis)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577{
2578 *win = XtWindow(vimShell);
2579 *dis = gui.dpy;
2580 return OK;
2581}
2582
2583 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002584gui_mch_beep(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585{
2586 XBell(gui.dpy, 0);
2587}
2588
2589 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002590gui_mch_flash(int msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591{
2592 /* Do a visual beep by reversing the foreground and background colors */
2593 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc, 0, 0,
2594 FILL_X((int)Columns) + gui.border_offset,
2595 FILL_Y((int)Rows) + gui.border_offset);
2596 XSync(gui.dpy, False);
2597 ui_delay((long)msec, TRUE); /* wait for a few msec */
2598 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc, 0, 0,
2599 FILL_X((int)Columns) + gui.border_offset,
2600 FILL_Y((int)Rows) + gui.border_offset);
2601}
2602
2603/*
2604 * Invert a rectangle from row r, column c, for nr rows and nc columns.
2605 */
2606 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002607gui_mch_invert_rectangle(
2608 int r,
2609 int c,
2610 int nr,
2611 int nc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612{
2613 XFillRectangle(gui.dpy, gui.wid, gui.invert_gc,
2614 FILL_X(c), FILL_Y(r), (nc) * gui.char_width, (nr) * gui.char_height);
2615}
2616
2617/*
2618 * Iconify the GUI window.
2619 */
2620 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002621gui_mch_iconify(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622{
2623 XIconifyWindow(gui.dpy, XtWindow(vimShell), DefaultScreen(gui.dpy));
2624}
2625
2626#if defined(FEAT_EVAL) || defined(PROTO)
2627/*
2628 * Bring the Vim window to the foreground.
2629 */
2630 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002631gui_mch_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632{
2633 XMapRaised(gui.dpy, XtWindow(vimShell));
2634}
2635#endif
2636
2637/*
2638 * Draw a cursor without focus.
2639 */
2640 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002641gui_mch_draw_hollow_cursor(guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642{
2643 int w = 1;
2644
2645#ifdef FEAT_MBYTE
2646 if (mb_lefthalve(gui.row, gui.col))
2647 w = 2;
2648#endif
2649 gui_mch_set_fg_color(color);
2650 XDrawRectangle(gui.dpy, gui.wid, gui.text_gc, FILL_X(gui.col),
2651 FILL_Y(gui.row), w * gui.char_width - 1, gui.char_height - 1);
2652}
2653
2654/*
2655 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
2656 * color "color".
2657 */
2658 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002659gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660{
2661 gui_mch_set_fg_color(color);
2662
2663 XFillRectangle(gui.dpy, gui.wid, gui.text_gc,
2664#ifdef FEAT_RIGHTLEFT
2665 /* vertical line should be on the right of current point */
2666 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
2667#endif
2668 FILL_X(gui.col),
2669 FILL_Y(gui.row) + gui.char_height - h,
2670 w, h);
2671}
2672
2673/*
2674 * Catch up with any queued X events. This may put keyboard input into the
2675 * input buffer, call resize call-backs, trigger timers etc. If there is
2676 * nothing in the X event queue (& no timers pending), then we return
2677 * immediately.
2678 */
2679 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002680gui_mch_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681{
2682 XtInputMask mask, desired;
2683
2684#ifdef ALT_X_INPUT
2685 if (suppress_alternate_input)
2686 desired = (XtIMXEvent | XtIMTimer);
2687 else
2688#endif
2689 desired = (XtIMAll);
2690 while ((mask = XtAppPending(app_context)) && (mask & desired)
2691 && !vim_is_input_buf_full())
2692 XtAppProcessEvent(app_context, desired);
2693}
2694
2695/*
2696 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2697 * from the keyboard.
2698 * wtime == -1 Wait forever.
2699 * wtime == 0 This should never happen.
2700 * wtime > 0 Wait wtime milliseconds for a character.
2701 * Returns OK if a character was found to be available within the given time,
2702 * or FAIL otherwise.
2703 */
2704 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002705gui_mch_wait_for_chars(long wtime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706{
Bram Moolenaar4231da42016-06-02 14:30:04 +02002707 int focus;
2708 int retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
2710 /*
2711 * Make this static, in case gui_x11_timer_cb is called after leaving
2712 * this function (otherwise a random value on the stack may be changed).
2713 */
2714 static int timed_out;
2715 XtIntervalId timer = (XtIntervalId)0;
2716 XtInputMask desired;
Bram Moolenaar1dccf632017-08-27 17:38:27 +02002717#ifdef FEAT_JOB_CHANNEL
2718 XtIntervalId channel_timer = (XtIntervalId)0;
2719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
2721 timed_out = FALSE;
2722
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 if (wtime > 0)
2724 timer = XtAppAddTimeOut(app_context, (long_u)wtime, gui_x11_timer_cb,
2725 &timed_out);
Bram Moolenaar1dccf632017-08-27 17:38:27 +02002726#ifdef FEAT_JOB_CHANNEL
2727 /* If there is a channel with the keep_open flag we need to poll for input
2728 * on them. */
2729 if (channel_any_keep_open())
2730 channel_timer = XtAppAddTimeOut(app_context, (long_u)20,
2731 channel_poll_cb, (XtPointer)&channel_timer);
2732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733
2734 focus = gui.in_focus;
2735#ifdef ALT_X_INPUT
2736 if (suppress_alternate_input)
2737 desired = (XtIMXEvent | XtIMTimer);
2738 else
2739#endif
2740 desired = (XtIMAll);
2741 while (!timed_out)
2742 {
2743 /* Stop or start blinking when focus changes */
2744 if (gui.in_focus != focus)
2745 {
2746 if (gui.in_focus)
2747 gui_mch_start_blink();
2748 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002749 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 focus = gui.in_focus;
2751 }
2752
Bram Moolenaar93c88e02015-09-15 14:12:05 +02002753#ifdef MESSAGE_QUEUE
Bram Moolenaar4231da42016-06-02 14:30:04 +02002754# ifdef FEAT_TIMERS
2755 did_add_timer = FALSE;
2756# endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02002757 parse_queued_messages();
Bram Moolenaar4231da42016-06-02 14:30:04 +02002758# ifdef FEAT_TIMERS
2759 if (did_add_timer)
2760 /* Need to recompute the waiting time. */
2761 break;
2762# endif
Bram Moolenaar03531f72010-11-16 15:04:57 +01002763#endif
2764
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 /*
2766 * Don't use gui_mch_update() because then we will spin-lock until a
2767 * char arrives, instead we use XtAppProcessEvent() to hang until an
2768 * event arrives. No need to check for input_buf_full because we are
2769 * returning as soon as it contains a single char. Note that
2770 * XtAppNextEvent() may not be used because it will not return after a
2771 * timer event has arrived -- webb
2772 */
2773 XtAppProcessEvent(app_context, desired);
2774
2775 if (input_available())
2776 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002777 retval = OK;
2778 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 }
2780 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002781
2782 if (timer != (XtIntervalId)0 && !timed_out)
2783 XtRemoveTimeOut(timer);
Bram Moolenaar1dccf632017-08-27 17:38:27 +02002784#ifdef FEAT_JOB_CHANNEL
2785 if (channel_timer != (XtIntervalId)0)
2786 XtRemoveTimeOut(channel_timer);
2787#endif
Bram Moolenaar4231da42016-06-02 14:30:04 +02002788
2789 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790}
2791
2792/*
2793 * Output routines.
2794 */
2795
2796/* Flush any output to the screen */
2797 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002798gui_mch_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799{
2800 XFlush(gui.dpy);
2801}
2802
2803/*
2804 * Clear a rectangular region of the screen from text pos (row1, col1) to
2805 * (row2, col2) inclusive.
2806 */
2807 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002808gui_mch_clear_block(
2809 int row1,
2810 int col1,
2811 int row2,
2812 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813{
2814 int x;
2815
2816 x = FILL_X(col1);
2817
2818 /* Clear one extra pixel at the far right, for when bold characters have
2819 * spilled over to the next column. */
2820 XFillRectangle(gui.dpy, gui.wid, gui.back_gc, x, FILL_Y(row1),
2821 (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1),
2822 (row2 - row1 + 1) * gui.char_height);
2823}
2824
2825 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002826gui_mch_clear_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827{
2828 XClearArea(gui.dpy, gui.wid, 0, 0, 0, 0, False);
2829}
2830
2831/*
2832 * Delete the given number of lines from the given row, scrolling up any
2833 * text further down within the scroll region.
2834 */
2835 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002836gui_mch_delete_lines(int row, int num_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837{
2838 if (gui.visibility == VisibilityFullyObscured)
2839 return; /* Can't see the window */
2840
2841 /* copy one extra pixel at the far right, for when bold has spilled
2842 * over */
2843 XCopyArea(gui.dpy, gui.wid, gui.wid, gui.text_gc,
2844 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
2845 gui.char_width * (gui.scroll_region_right - gui.scroll_region_left + 1)
2846 + (gui.scroll_region_right == Columns - 1),
2847 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1),
2848 FILL_X(gui.scroll_region_left), FILL_Y(row));
2849
2850 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
2851 gui.scroll_region_left,
2852 gui.scroll_region_bot, gui.scroll_region_right);
2853 gui_x11_check_copy_area();
2854}
2855
2856/*
2857 * Insert the given number of lines before the given row, scrolling down any
2858 * following text within the scroll region.
2859 */
2860 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002861gui_mch_insert_lines(int row, int num_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862{
2863 if (gui.visibility == VisibilityFullyObscured)
2864 return; /* Can't see the window */
2865
2866 /* copy one extra pixel at the far right, for when bold has spilled
2867 * over */
2868 XCopyArea(gui.dpy, gui.wid, gui.wid, gui.text_gc,
2869 FILL_X(gui.scroll_region_left), FILL_Y(row),
2870 gui.char_width * (gui.scroll_region_right - gui.scroll_region_left + 1)
2871 + (gui.scroll_region_right == Columns - 1),
2872 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1),
2873 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines));
2874
2875 gui_clear_block(row, gui.scroll_region_left,
2876 row + num_lines - 1, gui.scroll_region_right);
2877 gui_x11_check_copy_area();
2878}
2879
2880/*
2881 * Update the region revealed by scrolling up/down.
2882 */
2883 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002884gui_x11_check_copy_area(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885{
2886 XEvent event;
2887 XGraphicsExposeEvent *gevent;
2888
2889 if (gui.visibility != VisibilityPartiallyObscured)
2890 return;
2891
2892 XFlush(gui.dpy);
2893
2894 /* Wait to check whether the scroll worked or not */
2895 for (;;)
2896 {
2897 if (XCheckTypedEvent(gui.dpy, NoExpose, &event))
2898 return; /* The scroll worked. */
2899
2900 if (XCheckTypedEvent(gui.dpy, GraphicsExpose, &event))
2901 {
2902 gevent = (XGraphicsExposeEvent *)&event;
2903 gui_redraw(gevent->x, gevent->y, gevent->width, gevent->height);
2904 if (gevent->count == 0)
2905 return; /* This was the last expose event */
2906 }
2907 XSync(gui.dpy, False);
2908 }
2909}
2910
2911/*
2912 * X Selection stuff, for cutting and pasting text to other windows.
2913 */
2914
2915 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002916clip_mch_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917{
2918 clip_x11_lose_selection(vimShell, cbd);
2919}
2920
2921 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002922clip_mch_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923{
2924 return clip_x11_own_selection(vimShell, cbd);
2925}
2926
2927 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002928clip_mch_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929{
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002930 clip_x11_request_selection(vimShell, gui.dpy, cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931}
2932
2933 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002934clip_mch_set_selection(
2935 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936{
2937 clip_x11_set_selection(cbd);
2938}
2939
2940#if defined(FEAT_MENU) || defined(PROTO)
2941/*
2942 * Menu stuff.
2943 */
2944
2945/*
2946 * Make a menu either grey or not grey.
2947 */
2948 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002949gui_mch_menu_grey(vimmenu_T *menu, int grey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950{
2951 if (menu->id != (Widget)0)
2952 {
2953 gui_mch_menu_hidden(menu, False);
2954 if (grey
2955#ifdef FEAT_GUI_MOTIF
2956 || !menu->sensitive
2957#endif
2958 )
2959 XtSetSensitive(menu->id, False);
2960 else
2961 XtSetSensitive(menu->id, True);
2962 }
2963}
2964
2965/*
2966 * Make menu item hidden or not hidden
2967 */
2968 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002969gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970{
2971 if (menu->id != (Widget)0)
2972 {
2973 if (hidden)
2974 XtUnmanageChild(menu->id);
2975 else
2976 XtManageChild(menu->id);
2977 }
2978}
2979
2980/*
2981 * This is called after setting all the menus to grey/hidden or not.
2982 */
2983 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002984gui_mch_draw_menubar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985{
2986 /* Nothing to do in X */
2987}
2988
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002990gui_x11_menu_cb(
2991 Widget w UNUSED,
2992 XtPointer client_data,
2993 XtPointer call_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994{
2995 gui_menu_cb((vimmenu_T *)client_data);
2996}
2997
2998#endif /* FEAT_MENU */
2999
3000
3001
3002/*
3003 * Function called when window closed. Works like ":qa".
3004 * Should put up a requester!
3005 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003007gui_x11_wm_protocol_handler(
3008 Widget w UNUSED,
3009 XtPointer client_data UNUSED,
3010 XEvent *event,
3011 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012{
3013 /*
3014 * Only deal with Client messages.
3015 */
3016 if (event->type != ClientMessage)
3017 return;
3018
3019 /*
3020 * The WM_SAVE_YOURSELF event arrives when the window manager wants to
3021 * exit. That can be cancelled though, thus Vim shouldn't exit here.
3022 * Just sync our swap files.
3023 */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003024 if ((Atom)((XClientMessageEvent *)event)->data.l[0] ==
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 wm_atoms[SAVE_YOURSELF_IDX])
3026 {
3027 out_flush();
3028 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
3029
3030 /* Set the window's WM_COMMAND property, to let the window manager
3031 * know we are done saving ourselves. We don't want to be restarted,
3032 * thus set argv to NULL. */
3033 XSetCommand(gui.dpy, XtWindow(vimShell), NULL, 0);
3034 return;
3035 }
3036
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003037 if ((Atom)((XClientMessageEvent *)event)->data.l[0] !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 wm_atoms[DELETE_WINDOW_IDX])
3039 return;
3040
3041 gui_shell_closed();
3042}
3043
3044#ifdef FEAT_CLIENTSERVER
3045/*
3046 * Function called when property changed. Check for incoming commands
3047 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003049gui_x11_send_event_handler(
3050 Widget w UNUSED,
3051 XtPointer client_data UNUSED,
3052 XEvent *event,
3053 Boolean *dum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054{
3055 XPropertyEvent *e = (XPropertyEvent *) event;
3056
3057 if (e->type == PropertyNotify && e->window == commWindow
3058 && e->atom == commProperty && e->state == PropertyNewValue)
3059 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003060 serverEventProc(gui.dpy, event, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 }
3062}
3063#endif
3064
3065/*
3066 * Cursor blink functions.
3067 *
3068 * This is a simple state machine:
3069 * BLINK_NONE not blinking at all
3070 * BLINK_OFF blinking, cursor is not shown
3071 * BLINK_ON blinking, cursor is shown
3072 */
3073
3074#define BLINK_NONE 0
3075#define BLINK_OFF 1
3076#define BLINK_ON 2
3077
3078static int blink_state = BLINK_NONE;
3079static long_u blink_waittime = 700;
3080static long_u blink_ontime = 400;
3081static long_u blink_offtime = 250;
3082static XtIntervalId blink_timer = (XtIntervalId)0;
3083
Bram Moolenaar703a8042016-06-04 16:24:32 +02003084 int
3085gui_mch_is_blinking(void)
3086{
3087 return blink_state != BLINK_NONE;
3088}
3089
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +02003090 int
3091gui_mch_is_blink_off(void)
3092{
3093 return blink_state == BLINK_OFF;
3094}
3095
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 void
Bram Moolenaar02fdaea2016-01-30 18:13:55 +01003097gui_mch_set_blinking(long waittime, long on, long off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098{
3099 blink_waittime = waittime;
3100 blink_ontime = on;
3101 blink_offtime = off;
3102}
3103
3104/*
3105 * Stop the cursor blinking. Show the cursor if it wasn't shown.
3106 */
3107 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01003108gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109{
3110 if (blink_timer != (XtIntervalId)0)
3111 {
3112 XtRemoveTimeOut(blink_timer);
3113 blink_timer = (XtIntervalId)0;
3114 }
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01003115 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 gui_update_cursor(TRUE, FALSE);
3117 blink_state = BLINK_NONE;
3118}
3119
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003121gui_x11_blink_cb(
3122 XtPointer timed_out UNUSED,
3123 XtIntervalId *interval_id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124{
3125 if (blink_state == BLINK_ON)
3126 {
3127 gui_undraw_cursor();
3128 blink_state = BLINK_OFF;
3129 blink_timer = XtAppAddTimeOut(app_context, blink_offtime,
3130 gui_x11_blink_cb, NULL);
3131 }
3132 else
3133 {
3134 gui_update_cursor(TRUE, FALSE);
3135 blink_state = BLINK_ON;
3136 blink_timer = XtAppAddTimeOut(app_context, blink_ontime,
3137 gui_x11_blink_cb, NULL);
3138 }
3139}
3140
3141/*
Bram Moolenaar1dccf632017-08-27 17:38:27 +02003142 * Start the cursor blinking. If it was already blinking, this restarts the
3143 * waiting time and shows the cursor.
3144 */
3145 void
3146gui_mch_start_blink(void)
3147{
3148 if (blink_timer != (XtIntervalId)0)
3149 XtRemoveTimeOut(blink_timer);
3150 /* Only switch blinking on if none of the times is zero */
3151 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
3152 {
3153 blink_timer = XtAppAddTimeOut(app_context, blink_waittime,
3154 gui_x11_blink_cb, NULL);
3155 blink_state = BLINK_ON;
3156 gui_update_cursor(TRUE, FALSE);
3157 }
3158}
3159
3160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 * Return the RGB value of a pixel as a long.
3162 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003163 guicolor_T
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003164gui_mch_get_rgb(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165{
3166 XColor xc;
3167 Colormap colormap;
3168
3169 colormap = DefaultColormap(gui.dpy, XDefaultScreen(gui.dpy));
3170 xc.pixel = pixel;
3171 XQueryColor(gui.dpy, colormap, &xc);
3172
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003173 return (guicolor_T)(((xc.red & 0xff00) << 8) + (xc.green & 0xff00)
3174 + ((unsigned)xc.blue >> 8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175}
3176
3177/*
3178 * Add the callback functions.
3179 */
3180 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003181gui_x11_callbacks(Widget textArea, Widget vimForm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182{
3183 XtAddEventHandler(textArea, VisibilityChangeMask, FALSE,
3184 gui_x11_visibility_cb, (XtPointer)0);
3185
3186 XtAddEventHandler(textArea, ExposureMask, FALSE, gui_x11_expose_cb,
3187 (XtPointer)0);
3188
3189 XtAddEventHandler(vimShell, StructureNotifyMask, FALSE,
3190 gui_x11_resize_window_cb, (XtPointer)0);
3191
3192 XtAddEventHandler(vimShell, FocusChangeMask, FALSE, gui_x11_focus_change_cb,
3193 (XtPointer)0);
3194 /*
3195 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3196 * Only needed for some window managers.
3197 */
3198 if (vim_strchr(p_go, GO_POINTER) != NULL)
3199 {
3200 XtAddEventHandler(vimShell, LeaveWindowMask, FALSE, gui_x11_leave_cb,
3201 (XtPointer)0);
3202 XtAddEventHandler(textArea, LeaveWindowMask, FALSE, gui_x11_leave_cb,
3203 (XtPointer)0);
3204 XtAddEventHandler(textArea, EnterWindowMask, FALSE, gui_x11_enter_cb,
3205 (XtPointer)0);
3206 XtAddEventHandler(vimShell, EnterWindowMask, FALSE, gui_x11_enter_cb,
3207 (XtPointer)0);
3208 }
3209
3210 XtAddEventHandler(vimForm, KeyPressMask, FALSE, gui_x11_key_hit_cb,
3211 (XtPointer)0);
3212 XtAddEventHandler(textArea, KeyPressMask, FALSE, gui_x11_key_hit_cb,
3213 (XtPointer)0);
3214
3215 /* get pointer moved events from scrollbar, needed for 'mousefocus' */
3216 XtAddEventHandler(vimForm, PointerMotionMask,
3217 FALSE, gui_x11_mouse_cb, (XtPointer)1);
3218 XtAddEventHandler(textArea, ButtonPressMask | ButtonReleaseMask |
3219 ButtonMotionMask | PointerMotionMask,
3220 FALSE, gui_x11_mouse_cb, (XtPointer)0);
3221}
3222
3223/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003224 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003226 void
3227gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228{
3229 int rootx, rooty, winx, winy;
3230 Window root, child;
3231 unsigned int mask;
3232
3233 if (gui.wid && XQueryPointer(gui.dpy, gui.wid, &root, &child,
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003234 &rootx, &rooty, &winx, &winy, &mask)) {
3235 *x = winx;
3236 *y = winy;
3237 } else {
3238 *x = -1;
3239 *y = -1;
3240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241}
3242
3243 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003244gui_mch_setmouse(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245{
3246 if (gui.wid)
3247 XWarpPointer(gui.dpy, (Window)0, gui.wid, 0, 0, 0, 0, x, y);
3248}
3249
3250#if (defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)) || defined(PROTO)
3251 XButtonPressedEvent *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003252gui_x11_get_last_mouse_event(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253{
3254 return &last_mouse_event;
3255}
3256#endif
3257
3258#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
3259
3260/* Signs are currently always 2 chars wide. Hopefully the font is big enough
3261 * to provide room for the bitmap! */
3262# define SIGN_WIDTH (gui.char_width * 2)
3263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003265gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266{
3267 XImage *sign;
3268
3269 if (gui.in_use && (sign = (XImage *)sign_get_image(typenr)) != NULL)
3270 {
3271 XClearArea(gui.dpy, gui.wid, TEXT_X(col), TEXT_Y(row) - sign->height,
3272 SIGN_WIDTH, gui.char_height, FALSE);
3273 XPutImage(gui.dpy, gui.wid, gui.text_gc, sign, 0, 0,
3274 TEXT_X(col) + (SIGN_WIDTH - sign->width) / 2,
3275 TEXT_Y(row) - sign->height,
3276 sign->width, sign->height);
3277 }
3278}
3279
3280 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003281gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282{
3283 XpmAttributes attrs;
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003284 XImage *sign = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 int status;
3286
3287 /*
3288 * Setup the color substitution table.
3289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 if (signfile[0] != NUL && signfile[0] != '-')
3291 {
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003292 XpmColorSymbol color[5] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 {
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003294 {"none", NULL, 0},
3295 {"iconColor1", NULL, 0},
3296 {"bottomShadowColor", NULL, 0},
3297 {"topShadowColor", NULL, 0},
3298 {"selectColor", NULL, 0}
3299 };
3300 attrs.valuemask = XpmColorSymbols;
3301 attrs.numsymbols = 2;
3302 attrs.colorsymbols = color;
3303 attrs.colorsymbols[0].pixel = gui.back_pixel;
3304 attrs.colorsymbols[1].pixel = gui.norm_pixel;
3305 status = XpmReadFileToImage(gui.dpy, (char *)signfile,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 &sign, NULL, &attrs);
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003307 if (status == 0)
3308 {
3309 /* Sign width is fixed at two columns now.
3310 if (sign->width > gui.sign_width)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003311 gui.sign_width = sign->width + 8; */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 }
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003313 else
3314 EMSG(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 }
3316
3317 return (void *)sign;
3318}
3319
3320 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003321gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322{
Bram Moolenaare4bfca82009-02-24 03:12:40 +00003323 XDestroyImage((XImage*)sign);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324}
3325#endif
3326
3327
3328#ifdef FEAT_MOUSESHAPE
3329/* The last set mouse pointer shape is remembered, to be used when it goes
3330 * from hidden to not hidden. */
3331static int last_shape = 0;
3332#endif
3333
3334/*
3335 * Use the blank mouse pointer or not.
3336 */
3337 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003338gui_mch_mousehide(
3339 int hide) /* TRUE = use blank ptr, FALSE = use parent ptr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340{
3341 if (gui.pointer_hidden != hide)
3342 {
3343 gui.pointer_hidden = hide;
3344 if (hide)
3345 XDefineCursor(gui.dpy, gui.wid, gui.blank_pointer);
3346 else
3347#ifdef FEAT_MOUSESHAPE
3348 mch_set_mouse_shape(last_shape);
3349#else
3350 XUndefineCursor(gui.dpy, gui.wid);
3351#endif
3352 }
3353}
3354
3355#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3356
3357/* Table for shape IDs. Keep in sync with the mshape_names[] table in
3358 * misc2.c! */
3359static int mshape_ids[] =
3360{
3361 XC_left_ptr, /* arrow */
3362 0, /* blank */
3363 XC_xterm, /* beam */
3364 XC_sb_v_double_arrow, /* updown */
3365 XC_sizing, /* udsizing */
3366 XC_sb_h_double_arrow, /* leftright */
3367 XC_sizing, /* lrsizing */
3368 XC_watch, /* busy */
3369 XC_X_cursor, /* no */
3370 XC_crosshair, /* crosshair */
3371 XC_hand1, /* hand1 */
3372 XC_hand2, /* hand2 */
3373 XC_pencil, /* pencil */
3374 XC_question_arrow, /* question */
3375 XC_right_ptr, /* right-arrow */
3376 XC_center_ptr, /* up-arrow */
3377 XC_left_ptr /* last one */
3378};
3379
3380 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003381mch_set_mouse_shape(int shape)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
3383 int id;
3384
3385 if (!gui.in_use)
3386 return;
3387
3388 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
3389 XDefineCursor(gui.dpy, gui.wid, gui.blank_pointer);
3390 else
3391 {
3392 if (shape >= MSHAPE_NUMBERED)
3393 {
3394 id = shape - MSHAPE_NUMBERED;
3395 if (id >= XC_num_glyphs)
3396 id = XC_left_ptr;
3397 else
3398 id &= ~1; /* they are always even (why?) */
3399 }
3400 else
3401 id = mshape_ids[shape];
3402
3403 XDefineCursor(gui.dpy, gui.wid, XCreateFontCursor(gui.dpy, id));
3404 }
3405 if (shape != MSHAPE_HIDE)
3406 last_shape = shape;
3407}
3408#endif
3409
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003410#if (defined(FEAT_TOOLBAR) && defined(FEAT_BEVAL_GUI)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411/*
3412 * Set the balloon-eval used for the tooltip of a toolbar menu item.
3413 * The check for a non-toolbar item was added, because there is a crash when
3414 * passing a normal menu item here. Can't explain that, but better avoid it.
3415 */
3416 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003417gui_mch_menu_set_tip(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418{
3419 if (menu->id != NULL && menu->parent != NULL
3420 && menu_is_toolbar(menu->parent->name))
3421 {
3422 /* Always destroy and create the balloon, in case the string was
3423 * changed. */
3424 if (menu->tip != NULL)
3425 {
3426 gui_mch_destroy_beval_area(menu->tip);
3427 menu->tip = NULL;
3428 }
3429 if (menu->strings[MENU_INDEX_TIP] != NULL)
3430 menu->tip = gui_mch_create_beval_area(
3431 menu->id,
3432 menu->strings[MENU_INDEX_TIP],
3433 NULL,
3434 NULL);
3435 }
3436}
3437#endif