1 /* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10 /*
11 * Porting to GTK+ was done by:
12 *
13 * (C) 1998,1999,2000 by Marcin Dalecki <[email protected]>
14 *
15 * With GREAT support and continuous encouragements by Andy Kahn and of
16 * course Bram Moolenaar!
17 *
18 * Support for GTK+ 2 was added by:
19 *
20 * (C) 2002,2003 Jason Hildebrand <[email protected]>
21 * Daniel Elstner <[email protected]>
22 *
23 * Support for GTK+ 3 was added by:
24 *
25 * 2016 Kazunobu Kuriyama <[email protected]>
26 */
27
28 #include "vim.h"
29 #ifdef USE_GRESOURCE
30 #include "auto/gui_gtk_gresources.h"
31 #endif
32
33 #ifdef FEAT_GUI_GNOME
34 // Gnome redefines _() and N_(). Grrr...
35 # ifdef _
36 # undef _
37 # endif
38 # ifdef N_
39 # undef N_
40 # endif
41 # ifdef textdomain
42 # undef textdomain
43 # endif
44 # ifdef bindtextdomain
45 # undef bindtextdomain
46 # endif
47 # ifdef bind_textdomain_codeset
48 # undef bind_textdomain_codeset
49 # endif
50 # if defined(FEAT_GETTEXT) && !defined(ENABLE_NLS)
51 # define ENABLE_NLS // so the texts in the dialog boxes are translated
52 # endif
53 # include <gnome.h>
54 # include "version.h"
55 // missing prototype in bonobo-dock-item.h
56 extern void bonobo_dock_item_set_behavior(BonoboDockItem *dock_item, BonoboDockItemBehavior beh);
57 #endif
58
59 #if !defined(FEAT_GUI_GTK) && defined(PROTO)
60 // When generating prototypes we don't want syntax errors.
61 # define GdkAtom int
62 # define GdkEventExpose int
63 # define GdkEventFocus int
64 # define GdkEventVisibility int
65 # define GdkEventProperty int
66 # define GtkContainer int
67 # define GtkTargetEntry int
68 # define GtkType int
69 # define GtkWidget int
70 # define gint int
71 # define gpointer int
72 # define guint int
73 # define GdkEventKey int
74 # define GdkEventSelection int
75 # define GtkSelectionData int
76 # define GdkEventMotion int
77 # define GdkEventButton int
78 # define GdkDragContext int
79 # define GdkEventConfigure int
80 # define GdkEventClient int
81 #else
82 # if GTK_CHECK_VERSION(3,0,0)
83 # include <gdk/gdkkeysyms-compat.h>
84 # include <gtk/gtkx.h>
85 # else
86 # include <gdk/gdkkeysyms.h>
87 # endif
88 # include <gdk/gdk.h>
89 # ifdef MSWIN
90 # include <gdk/gdkwin32.h>
91 # else
92 # include <gdk/gdkx.h>
93 # endif
94 # include <gtk/gtk.h>
95 # include "gui_gtk_f.h"
96 #endif
97
98 #ifdef HAVE_X11_SUNKEYSYM_H
99 # include <X11/Sunkeysym.h>
100 #endif
101
102 /*
103 * Easy-to-use macro for multihead support.
104 */
105 #define GET_X_ATOM(atom) gdk_x11_atom_to_xatom_for_display( \
106 gtk_widget_get_display(gui.mainwin), atom)
107
108 // Selection type distinguishers
109 enum
110 {
111 TARGET_TYPE_NONE,
112 TARGET_UTF8_STRING,
113 TARGET_STRING,
114 TARGET_COMPOUND_TEXT,
115 TARGET_HTML,
116 TARGET_TEXT,
117 TARGET_TEXT_URI_LIST,
118 TARGET_TEXT_PLAIN,
119 TARGET_VIM,
120 TARGET_VIMENC
121 };
122
123 /*
124 * Table of selection targets supported by Vim.
125 * Note: Order matters, preferred types should come first.
126 */
127 static const GtkTargetEntry selection_targets[] =
128 {
129 {VIMENC_ATOM_NAME, 0, TARGET_VIMENC},
130 {VIM_ATOM_NAME, 0, TARGET_VIM},
131 {"text/html", 0, TARGET_HTML},
132 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
133 {"COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT},
134 {"TEXT", 0, TARGET_TEXT},
135 {"STRING", 0, TARGET_STRING}
136 };
137 #define N_SELECTION_TARGETS ARRAY_LENGTH(selection_targets)
138
139 #ifdef FEAT_DND
140 /*
141 * Table of DnD targets supported by Vim.
142 * Note: Order matters, preferred types should come first.
143 */
144 static const GtkTargetEntry dnd_targets[] =
145 {
146 {"text/uri-list", 0, TARGET_TEXT_URI_LIST},
147 {"text/html", 0, TARGET_HTML},
148 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
149 {"STRING", 0, TARGET_STRING},
150 {"text/plain", 0, TARGET_TEXT_PLAIN}
151 };
152 # define N_DND_TARGETS ARRAY_LENGTH(dnd_targets)
153 #endif
154
155
156 /*
157 * "Monospace" is a standard font alias that should be present
158 * on all proper Pango/fontconfig installations.
159 */
160 # define DEFAULT_FONT "Monospace 10"
161
162 #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
163 # define USE_GNOME_SESSION
164 #endif
165
166 #if !defined(FEAT_GUI_GNOME)
167 /*
168 * Atoms used to communicate save-yourself from the X11 session manager. There
169 * is no need to move them into the GUI struct, since they should be constant.
170 */
171 static GdkAtom wm_protocols_atom = GDK_NONE;
172 static GdkAtom save_yourself_atom = GDK_NONE;
173 #endif
174
175 /*
176 * Atoms used to control/reference X11 selections.
177 */
178 static GdkAtom html_atom = GDK_NONE;
179 static GdkAtom utf8_string_atom = GDK_NONE;
180 static GdkAtom vim_atom = GDK_NONE; // Vim's own special selection format
181 static GdkAtom vimenc_atom = GDK_NONE; // Vim's extended selection format
182
183 /*
184 * Keycodes recognized by vim.
185 * NOTE: when changing this, the table in gui_x11.c probably needs the same
186 * change!
187 */
188 static struct special_key
189 {
190 guint key_sym;
191 char_u code0;
192 char_u code1;
193 }
194 const special_keys[] =
195 {
196 {GDK_Up, 'k', 'u'},
197 {GDK_Down, 'k', 'd'},
198 {GDK_Left, 'k', 'l'},
199 {GDK_Right, 'k', 'r'},
200 {GDK_F1, 'k', '1'},
201 {GDK_F2, 'k', '2'},
202 {GDK_F3, 'k', '3'},
203 {GDK_F4, 'k', '4'},
204 {GDK_F5, 'k', '5'},
205 {GDK_F6, 'k', '6'},
206 {GDK_F7, 'k', '7'},
207 {GDK_F8, 'k', '8'},
208 {GDK_F9, 'k', '9'},
209 {GDK_F10, 'k', ';'},
210 {GDK_F11, 'F', '1'},
211 {GDK_F12, 'F', '2'},
212 {GDK_F13, 'F', '3'},
213 {GDK_F14, 'F', '4'},
214 {GDK_F15, 'F', '5'},
215 {GDK_F16, 'F', '6'},
216 {GDK_F17, 'F', '7'},
217 {GDK_F18, 'F', '8'},
218 {GDK_F19, 'F', '9'},
219 {GDK_F20, 'F', 'A'},
220 {GDK_F21, 'F', 'B'},
221 {GDK_Pause, 'F', 'B'}, // Pause == F21 according to netbeans.txt
222 {GDK_F22, 'F', 'C'},
223 {GDK_F23, 'F', 'D'},
224 {GDK_F24, 'F', 'E'},
225 {GDK_F25, 'F', 'F'},
226 {GDK_F26, 'F', 'G'},
227 {GDK_F27, 'F', 'H'},
228 {GDK_F28, 'F', 'I'},
229 {GDK_F29, 'F', 'J'},
230 {GDK_F30, 'F', 'K'},
231 {GDK_F31, 'F', 'L'},
232 {GDK_F32, 'F', 'M'},
233 {GDK_F33, 'F', 'N'},
234 {GDK_F34, 'F', 'O'},
235 {GDK_F35, 'F', 'P'},
236 #ifdef SunXK_F36
237 {SunXK_F36, 'F', 'Q'},
238 {SunXK_F37, 'F', 'R'},
239 #endif
240 {GDK_Help, '%', '1'},
241 {GDK_Undo, '&', '8'},
242 {GDK_BackSpace, 'k', 'b'},
243 {GDK_Insert, 'k', 'I'},
244 {GDK_Delete, 'k', 'D'},
245 {GDK_3270_BackTab, 'k', 'B'},
246 {GDK_Clear, 'k', 'C'},
247 {GDK_Home, 'k', 'h'},
248 {GDK_End, '@', '7'},
249 {GDK_Prior, 'k', 'P'},
250 {GDK_Next, 'k', 'N'},
251 {GDK_Print, '%', '9'},
252 // Keypad keys:
253 {GDK_KP_Left, 'k', 'l'},
254 {GDK_KP_Right, 'k', 'r'},
255 {GDK_KP_Up, 'k', 'u'},
256 {GDK_KP_Down, 'k', 'd'},
257 {GDK_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
258 {GDK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
259 {GDK_KP_Home, 'K', '1'},
260 {GDK_KP_End, 'K', '4'},
261 {GDK_KP_Prior, 'K', '3'}, // page up
262 {GDK_KP_Next, 'K', '5'}, // page down
263
264 {GDK_KP_Add, 'K', '6'},
265 {GDK_KP_Subtract, 'K', '7'},
266 {GDK_KP_Divide, 'K', '8'},
267 {GDK_KP_Multiply, 'K', '9'},
268 {GDK_KP_Enter, 'K', 'A'},
269 {GDK_KP_Decimal, 'K', 'B'},
270
271 {GDK_KP_0, 'K', 'C'},
272 {GDK_KP_1, 'K', 'D'},
273 {GDK_KP_2, 'K', 'E'},
274 {GDK_KP_3, 'K', 'F'},
275 {GDK_KP_4, 'K', 'G'},
276 {GDK_KP_5, 'K', 'H'},
277 {GDK_KP_6, 'K', 'I'},
278 {GDK_KP_7, 'K', 'J'},
279 {GDK_KP_8, 'K', 'K'},
280 {GDK_KP_9, 'K', 'L'},
281
282 // End of list marker:
283 {0, 0, 0}
284 };
285
286 /*
287 * Flags for command line options table below.
288 */
289 #define ARG_FONT 1
290 #define ARG_GEOMETRY 2
291 #define ARG_REVERSE 3
292 #define ARG_NOREVERSE 4
293 #define ARG_BACKGROUND 5
294 #define ARG_FOREGROUND 6
295 #define ARG_ICONIC 7
296 #define ARG_ROLE 8
297 #define ARG_NETBEANS 9
298 #define ARG_XRM 10 // ignored
299 #define ARG_MENUFONT 11 // ignored
300 #define ARG_INDEX_MASK 0x00ff
301 #define ARG_HAS_VALUE 0x0100 // a value is expected after the argument
302 #define ARG_NEEDS_GUI 0x0200 // need to initialize the GUI for this
303 #define ARG_FOR_GTK 0x0400 // argument is handled by GTK+ or GNOME
304 #define ARG_COMPAT_LONG 0x0800 // accept -foo but substitute with --foo
305 #define ARG_KEEP 0x1000 // don't remove argument from argv[]
306
307 /*
308 * This table holds all the X GUI command line options allowed. This includes
309 * the standard ones so that we can skip them when Vim is started without the
310 * GUI (but the GUI might start up later).
311 *
312 * When changing this, also update doc/gui_x11.txt and the usage message!!!
313 */
314 typedef struct
315 {
316 const char *name;
317 unsigned int flags;
318 }
319 cmdline_option_T;
320
321 static const cmdline_option_T cmdline_options[] =
322 {
323 // We handle these options ourselves
324 {"-fn", ARG_FONT|ARG_HAS_VALUE},
325 {"-font", ARG_FONT|ARG_HAS_VALUE},
326 {"-geom", ARG_GEOMETRY|ARG_HAS_VALUE},
327 {"-geometry", ARG_GEOMETRY|ARG_HAS_VALUE},
328 {"-rv", ARG_REVERSE},
329 {"-reverse", ARG_REVERSE},
330 {"+rv", ARG_NOREVERSE},
331 {"+reverse", ARG_NOREVERSE},
332 {"-bg", ARG_BACKGROUND|ARG_HAS_VALUE},
333 {"-background", ARG_BACKGROUND|ARG_HAS_VALUE},
334 {"-fg", ARG_FOREGROUND|ARG_HAS_VALUE},
335 {"-foreground", ARG_FOREGROUND|ARG_HAS_VALUE},
336 {"-iconic", ARG_ICONIC},
337 {"--role", ARG_ROLE|ARG_HAS_VALUE},
338 #ifdef FEAT_NETBEANS_INTG
339 {"-nb", ARG_NETBEANS}, // non-standard value format
340 {"-xrm", ARG_XRM|ARG_HAS_VALUE}, // not implemented
341 {"-mf", ARG_MENUFONT|ARG_HAS_VALUE}, // not implemented
342 {"-menufont", ARG_MENUFONT|ARG_HAS_VALUE}, // not implemented
343 #endif
344 // Arguments handled by GTK (and GNOME) internally.
345 {"--g-fatal-warnings", ARG_FOR_GTK},
346 {"--gdk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
347 {"--gdk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
348 {"--gtk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
349 {"--gtk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
350 {"--gtk-module", ARG_FOR_GTK|ARG_HAS_VALUE},
351 {"--sync", ARG_FOR_GTK},
352 {"--display", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
353 {"--name", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
354 {"--class", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
355 {"--screen", ARG_FOR_GTK|ARG_HAS_VALUE},
356 {"--gxid-host", ARG_FOR_GTK|ARG_HAS_VALUE},
357 {"--gxid-port", ARG_FOR_GTK|ARG_HAS_VALUE},
358 #ifdef FEAT_GUI_GNOME
359 {"--load-modules", ARG_FOR_GTK|ARG_HAS_VALUE},
360 {"--sm-client-id", ARG_FOR_GTK|ARG_HAS_VALUE},
361 {"--sm-config-prefix", ARG_FOR_GTK|ARG_HAS_VALUE},
362 {"--sm-disable", ARG_FOR_GTK},
363 {"--oaf-ior-fd", ARG_FOR_GTK|ARG_HAS_VALUE},
364 {"--oaf-activate-iid", ARG_FOR_GTK|ARG_HAS_VALUE},
365 {"--oaf-private", ARG_FOR_GTK},
366 {"--enable-sound", ARG_FOR_GTK},
367 {"--disable-sound", ARG_FOR_GTK},
368 {"--espeaker", ARG_FOR_GTK|ARG_HAS_VALUE},
369 {"-?", ARG_FOR_GTK|ARG_NEEDS_GUI},
370 {"--help", ARG_FOR_GTK|ARG_NEEDS_GUI|ARG_KEEP},
371 {"--usage", ARG_FOR_GTK|ARG_NEEDS_GUI},
372 # if 0 // conflicts with Vim's own --version argument
373 {"--version", ARG_FOR_GTK|ARG_NEEDS_GUI},
374 # endif
375 {"--disable-crash-dialog", ARG_FOR_GTK},
376 #endif
377 {NULL, 0}
378 };
379
380 static int gui_argc = 0;
381 static char **gui_argv = NULL;
382
383 static const char *role_argument = NULL;
384 #if defined(USE_GNOME_SESSION)
385 static const char *restart_command = NULL;
386 static char *abs_restart_command = NULL;
387 #endif
388 static int found_iconic_arg = FALSE;
389
390 #ifdef FEAT_GUI_GNOME
391 /*
392 * Can't use Gnome if --socketid given
393 */
394 static int using_gnome = 0;
395 #else
396 # define using_gnome 0
397 #endif
398
399 /*
400 * Parse the GUI related command-line arguments. Any arguments used are
401 * deleted from argv, and *argc is decremented accordingly. This is called
402 * when vim is started, whether or not the GUI has been started.
403 */
404 void
gui_mch_prepare(int * argc,char ** argv)405 gui_mch_prepare(int *argc, char **argv)
406 {
407 const cmdline_option_T *option;
408 int i = 0;
409 int len = 0;
410
411 #if defined(USE_GNOME_SESSION)
412 /*
413 * Determine the command used to invoke Vim, to be passed as restart
414 * command to the session manager. If argv[0] contains any directory
415 * components try building an absolute path, otherwise leave it as is.
416 */
417 restart_command = argv[0];
418
419 if (strchr(argv[0], G_DIR_SEPARATOR) != NULL)
420 {
421 char_u buf[MAXPATHL];
422
423 if (mch_FullName((char_u *)argv[0], buf, (int)sizeof(buf), TRUE) == OK)
424 {
425 abs_restart_command = (char *)vim_strsave(buf);
426 restart_command = abs_restart_command;
427 }
428 }
429 #endif
430
431 /*
432 * Move all the entries in argv which are relevant to GTK+ and GNOME
433 * into gui_argv. Freed later in gui_mch_init().
434 */
435 gui_argc = 0;
436 gui_argv = ALLOC_MULT(char *, *argc + 1);
437
438 g_return_if_fail(gui_argv != NULL);
439
440 gui_argv[gui_argc++] = argv[i++];
441
442 while (i < *argc)
443 {
444 // Don't waste CPU cycles on non-option arguments.
445 if (argv[i][0] != '-' && argv[i][0] != '+')
446 {
447 ++i;
448 continue;
449 }
450
451 // Look for argv[i] in cmdline_options[] table.
452 for (option = &cmdline_options[0]; option->name != NULL; ++option)
453 {
454 len = strlen(option->name);
455
456 if (strncmp(argv[i], option->name, len) == 0)
457 {
458 if (argv[i][len] == '\0')
459 break;
460 // allow --foo=bar style
461 if (argv[i][len] == '=' && (option->flags & ARG_HAS_VALUE))
462 break;
463 #ifdef FEAT_NETBEANS_INTG
464 // darn, -nb has non-standard syntax
465 if (vim_strchr((char_u *)":=", argv[i][len]) != NULL
466 && (option->flags & ARG_INDEX_MASK) == ARG_NETBEANS)
467 break;
468 #endif
469 }
470 else if ((option->flags & ARG_COMPAT_LONG)
471 && strcmp(argv[i], option->name + 1) == 0)
472 {
473 // Replace the standard X arguments "-name" and "-display"
474 // with their GNU-style long option counterparts.
475 argv[i] = (char *)option->name;
476 break;
477 }
478 }
479 if (option->name == NULL) // no match
480 {
481 ++i;
482 continue;
483 }
484
485 if (option->flags & ARG_FOR_GTK)
486 {
487 // Move the argument into gui_argv, which
488 // will later be passed to gtk_init_check()
489 gui_argv[gui_argc++] = argv[i];
490 }
491 else
492 {
493 char *value = NULL;
494
495 // Extract the option's value if there is one.
496 // Accept both "--foo bar" and "--foo=bar" style.
497 if (option->flags & ARG_HAS_VALUE)
498 {
499 if (argv[i][len] == '=')
500 value = &argv[i][len + 1];
501 else if (i + 1 < *argc && strcmp(argv[i + 1], "--") != 0)
502 value = argv[i + 1];
503 }
504
505 // Check for options handled by Vim itself
506 switch (option->flags & ARG_INDEX_MASK)
507 {
508 case ARG_REVERSE:
509 found_reverse_arg = TRUE;
510 break;
511 case ARG_NOREVERSE:
512 found_reverse_arg = FALSE;
513 break;
514 case ARG_FONT:
515 font_argument = value;
516 break;
517 case ARG_GEOMETRY:
518 if (value != NULL)
519 gui.geom = vim_strsave((char_u *)value);
520 break;
521 case ARG_BACKGROUND:
522 background_argument = value;
523 break;
524 case ARG_FOREGROUND:
525 foreground_argument = value;
526 break;
527 case ARG_ICONIC:
528 found_iconic_arg = TRUE;
529 break;
530 case ARG_ROLE:
531 role_argument = value; // used later in gui_mch_open()
532 break;
533 #ifdef FEAT_NETBEANS_INTG
534 case ARG_NETBEANS:
535 gui.dofork = FALSE; // don't fork() when starting GUI
536 netbeansArg = argv[i];
537 break;
538 #endif
539 default:
540 break;
541 }
542 }
543
544 // These arguments make gnome_program_init() print a message and exit.
545 // Must start the GUI for this, otherwise ":gui" will exit later!
546 // Only when the GUI can start.
547 if ((option->flags & ARG_NEEDS_GUI)
548 && gui_mch_early_init_check(FALSE) == OK)
549 gui.starting = TRUE;
550
551 if (option->flags & ARG_KEEP)
552 ++i;
553 else
554 {
555 // Remove the flag from the argument vector.
556 if (--*argc > i)
557 {
558 int n_strip = 1;
559
560 // Move the argument's value as well, if there is one.
561 if ((option->flags & ARG_HAS_VALUE)
562 && argv[i][len] != '='
563 && strcmp(argv[i + 1], "--") != 0)
564 {
565 ++n_strip;
566 --*argc;
567 if (option->flags & ARG_FOR_GTK)
568 gui_argv[gui_argc++] = argv[i + 1];
569 }
570
571 if (*argc > i)
572 mch_memmove(&argv[i], &argv[i + n_strip],
573 (*argc - i) * sizeof(char *));
574 }
575 argv[*argc] = NULL;
576 }
577 }
578
579 gui_argv[gui_argc] = NULL;
580 }
581
582 #if defined(EXITFREE) || defined(PROTO)
583 void
gui_mch_free_all(void)584 gui_mch_free_all(void)
585 {
586 vim_free(gui_argv);
587 #if defined(USE_GNOME_SESSION)
588 vim_free(abs_restart_command);
589 #endif
590 }
591 #endif
592
593 #if !GTK_CHECK_VERSION(3,0,0)
594 /*
595 * This should be maybe completely removed.
596 * Doesn't seem possible, since check_copy_area() relies on
597 * this information. --danielk
598 */
599 static gint
visibility_event(GtkWidget * widget UNUSED,GdkEventVisibility * event,gpointer data UNUSED)600 visibility_event(GtkWidget *widget UNUSED,
601 GdkEventVisibility *event,
602 gpointer data UNUSED)
603 {
604 gui.visibility = event->state;
605 /*
606 * When we do an gdk_window_copy_area(), and the window is partially
607 * obscured, we want to receive an event to tell us whether it worked
608 * or not.
609 */
610 if (gui.text_gc != NULL)
611 gdk_gc_set_exposures(gui.text_gc,
612 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
613 return FALSE;
614 }
615 #endif // !GTK_CHECK_VERSION(3,0,0)
616
617 /*
618 * Redraw the corresponding portions of the screen.
619 */
620 #if GTK_CHECK_VERSION(3,0,0)
621 static gboolean
draw_event(GtkWidget * widget UNUSED,cairo_t * cr,gpointer user_data UNUSED)622 draw_event(GtkWidget *widget UNUSED,
623 cairo_t *cr,
624 gpointer user_data UNUSED)
625 {
626 // Skip this when the GUI isn't set up yet, will redraw later.
627 if (gui.starting)
628 return FALSE;
629
630 out_flush(); // make sure all output has been processed
631 // for GTK+ 3, may induce other draw events.
632
633 cairo_set_source_surface(cr, gui.surface, 0, 0);
634
635 {
636 cairo_rectangle_list_t *list = NULL;
637
638 list = cairo_copy_clip_rectangle_list(cr);
639 if (list->status != CAIRO_STATUS_CLIP_NOT_REPRESENTABLE)
640 {
641 int i;
642
643 for (i = 0; i < list->num_rectangles; i++)
644 {
645 const cairo_rectangle_t *rect = &list->rectangles[i];
646 cairo_rectangle(cr, rect->x, rect->y, rect->width, rect->height);
647 cairo_fill(cr);
648 }
649 }
650 cairo_rectangle_list_destroy(list);
651 }
652
653 return FALSE;
654 }
655 #else // !GTK_CHECK_VERSION(3,0,0)
656 static gint
expose_event(GtkWidget * widget UNUSED,GdkEventExpose * event,gpointer data UNUSED)657 expose_event(GtkWidget *widget UNUSED,
658 GdkEventExpose *event,
659 gpointer data UNUSED)
660 {
661 // Skip this when the GUI isn't set up yet, will redraw later.
662 if (gui.starting)
663 return FALSE;
664
665 out_flush(); // make sure all output has been processed
666 gui_redraw(event->area.x, event->area.y,
667 event->area.width, event->area.height);
668
669 // Clear the border areas if needed
670 if (event->area.x < FILL_X(0))
671 gdk_window_clear_area(gui.drawarea->window, 0, 0, FILL_X(0), 0);
672 if (event->area.y < FILL_Y(0))
673 gdk_window_clear_area(gui.drawarea->window, 0, 0, 0, FILL_Y(0));
674 if (event->area.x > FILL_X(Columns))
675 gdk_window_clear_area(gui.drawarea->window,
676 FILL_X((int)Columns), 0, 0, 0);
677 if (event->area.y > FILL_Y(Rows))
678 gdk_window_clear_area(gui.drawarea->window, 0, FILL_Y((int)Rows), 0, 0);
679
680 return FALSE;
681 }
682 #endif // !GTK_CHECK_VERSION(3,0,0)
683
684 #ifdef FEAT_CLIENTSERVER
685 /*
686 * Handle changes to the "Comm" property
687 */
688 static gint
property_event(GtkWidget * widget,GdkEventProperty * event,gpointer data UNUSED)689 property_event(GtkWidget *widget,
690 GdkEventProperty *event,
691 gpointer data UNUSED)
692 {
693 if (event->type == GDK_PROPERTY_NOTIFY
694 && event->state == (int)GDK_PROPERTY_NEW_VALUE
695 && GDK_WINDOW_XID(event->window) == commWindow
696 && GET_X_ATOM(event->atom) == commProperty)
697 {
698 XEvent xev;
699
700 // Translate to XLib
701 xev.xproperty.type = PropertyNotify;
702 xev.xproperty.atom = commProperty;
703 xev.xproperty.window = commWindow;
704 xev.xproperty.state = PropertyNewValue;
705 serverEventProc(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(widget)),
706 &xev, 0);
707 }
708 return FALSE;
709 }
710 #endif // defined(FEAT_CLIENTSERVER)
711
712 /*
713 * Handle changes to the "Xft/DPI" setting
714 */
715 static void
gtk_settings_xft_dpi_changed_cb(GtkSettings * gtk_settings UNUSED,GParamSpec * pspec UNUSED,gpointer data UNUSED)716 gtk_settings_xft_dpi_changed_cb(GtkSettings *gtk_settings UNUSED,
717 GParamSpec *pspec UNUSED,
718 gpointer data UNUSED)
719 {
720 // Create a new PangoContext for this screen, and initialize it
721 // with the current font if necessary.
722 if (gui.text_context != NULL)
723 g_object_unref(gui.text_context);
724
725 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
726 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
727
728 if (gui.norm_font != NULL)
729 {
730 // force default font
731 gui_mch_init_font(*p_guifont == NUL ? NULL : p_guifont, FALSE);
732 gui_set_shellsize(TRUE, FALSE, RESIZE_BOTH);
733 }
734 }
735
736 typedef gboolean timeout_cb_type;
737
738 /*
739 * Start a timer that will invoke the specified callback.
740 * Returns the ID of the timer.
741 */
742 static guint
timeout_add(int time,timeout_cb_type (* callback)(gpointer),int * flagp)743 timeout_add(int time, timeout_cb_type (*callback)(gpointer), int *flagp)
744 {
745 return g_timeout_add((guint)time, (GSourceFunc)callback, flagp);
746 }
747
748 static void
timeout_remove(guint timer)749 timeout_remove(guint timer)
750 {
751 g_source_remove(timer);
752 }
753
754
755 /////////////////////////////////////////////////////////////////////////////
756 // Focus handlers:
757
758
759 /*
760 * This is a simple state machine:
761 * BLINK_NONE not blinking at all
762 * BLINK_OFF blinking, cursor is not shown
763 * BLINK_ON blinking, cursor is shown
764 */
765
766 #define BLINK_NONE 0
767 #define BLINK_OFF 1
768 #define BLINK_ON 2
769
770 static int blink_state = BLINK_NONE;
771 static long_u blink_waittime = 700;
772 static long_u blink_ontime = 400;
773 static long_u blink_offtime = 250;
774 static guint blink_timer = 0;
775
776 int
gui_mch_is_blinking(void)777 gui_mch_is_blinking(void)
778 {
779 return blink_state != BLINK_NONE;
780 }
781
782 int
gui_mch_is_blink_off(void)783 gui_mch_is_blink_off(void)
784 {
785 return blink_state == BLINK_OFF;
786 }
787
788 void
gui_mch_set_blinking(long waittime,long on,long off)789 gui_mch_set_blinking(long waittime, long on, long off)
790 {
791 blink_waittime = waittime;
792 blink_ontime = on;
793 blink_offtime = off;
794 }
795
796 /*
797 * Stop the cursor blinking. Show the cursor if it wasn't shown.
798 */
799 void
gui_mch_stop_blink(int may_call_gui_update_cursor)800 gui_mch_stop_blink(int may_call_gui_update_cursor)
801 {
802 if (blink_timer)
803 {
804 timeout_remove(blink_timer);
805 blink_timer = 0;
806 }
807 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
808 {
809 gui_update_cursor(TRUE, FALSE);
810 #if !GTK_CHECK_VERSION(3,0,0)
811 gui_mch_flush();
812 #endif
813 }
814 blink_state = BLINK_NONE;
815 }
816
817 static timeout_cb_type
blink_cb(gpointer data UNUSED)818 blink_cb(gpointer data UNUSED)
819 {
820 if (blink_state == BLINK_ON)
821 {
822 gui_undraw_cursor();
823 blink_state = BLINK_OFF;
824 blink_timer = timeout_add(blink_offtime, blink_cb, NULL);
825 }
826 else
827 {
828 gui_update_cursor(TRUE, FALSE);
829 blink_state = BLINK_ON;
830 blink_timer = timeout_add(blink_ontime, blink_cb, NULL);
831 }
832 #if !GTK_CHECK_VERSION(3,0,0)
833 gui_mch_flush();
834 #endif
835
836 return FALSE; // don't happen again
837 }
838
839 /*
840 * Start the cursor blinking. If it was already blinking, this restarts the
841 * waiting time and shows the cursor.
842 */
843 void
gui_mch_start_blink(void)844 gui_mch_start_blink(void)
845 {
846 if (blink_timer)
847 {
848 timeout_remove(blink_timer);
849 blink_timer = 0;
850 }
851 // Only switch blinking on if none of the times is zero
852 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
853 {
854 blink_timer = timeout_add(blink_waittime, blink_cb, NULL);
855 blink_state = BLINK_ON;
856 gui_update_cursor(TRUE, FALSE);
857 #if !GTK_CHECK_VERSION(3,0,0)
858 gui_mch_flush();
859 #endif
860 }
861 }
862
863 static gint
enter_notify_event(GtkWidget * widget UNUSED,GdkEventCrossing * event UNUSED,gpointer data UNUSED)864 enter_notify_event(GtkWidget *widget UNUSED,
865 GdkEventCrossing *event UNUSED,
866 gpointer data UNUSED)
867 {
868 if (blink_state == BLINK_NONE)
869 gui_mch_start_blink();
870
871 // make sure keyboard input goes there
872 if (gtk_socket_id == 0 || !gtk_widget_has_focus(gui.drawarea))
873 gtk_widget_grab_focus(gui.drawarea);
874
875 return FALSE;
876 }
877
878 static gint
leave_notify_event(GtkWidget * widget UNUSED,GdkEventCrossing * event UNUSED,gpointer data UNUSED)879 leave_notify_event(GtkWidget *widget UNUSED,
880 GdkEventCrossing *event UNUSED,
881 gpointer data UNUSED)
882 {
883 if (blink_state != BLINK_NONE)
884 gui_mch_stop_blink(TRUE);
885
886 return FALSE;
887 }
888
889 static gint
focus_in_event(GtkWidget * widget,GdkEventFocus * event UNUSED,gpointer data UNUSED)890 focus_in_event(GtkWidget *widget,
891 GdkEventFocus *event UNUSED,
892 gpointer data UNUSED)
893 {
894 gui_focus_change(TRUE);
895
896 if (blink_state == BLINK_NONE)
897 gui_mch_start_blink();
898
899 // make sure keyboard input goes to the draw area (if this is focus for a
900 // window)
901 if (widget != gui.drawarea)
902 gtk_widget_grab_focus(gui.drawarea);
903
904 return TRUE;
905 }
906
907 static gint
focus_out_event(GtkWidget * widget UNUSED,GdkEventFocus * event UNUSED,gpointer data UNUSED)908 focus_out_event(GtkWidget *widget UNUSED,
909 GdkEventFocus *event UNUSED,
910 gpointer data UNUSED)
911 {
912 gui_focus_change(FALSE);
913
914 if (blink_state != BLINK_NONE)
915 gui_mch_stop_blink(TRUE);
916
917 return TRUE;
918 }
919
920
921 /*
922 * Translate a GDK key value to UTF-8 independently of the current locale.
923 * The output is written to string, which must have room for at least 6 bytes
924 * plus the NUL terminator. Returns the length in bytes.
925 *
926 * event->string is evil; see here why:
927 * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey
928 */
929 static int
keyval_to_string(unsigned int keyval,char_u * string)930 keyval_to_string(unsigned int keyval, char_u *string)
931 {
932 int len;
933 guint32 uc;
934
935 uc = gdk_keyval_to_unicode(keyval);
936 if (uc != 0)
937 {
938 // Translate a normal key to UTF-8. This doesn't work for dead
939 // keys of course, you _have_ to use an input method for that.
940 len = utf_char2bytes((int)uc, string);
941 }
942 else
943 {
944 // Translate keys which are represented by ASCII control codes in Vim.
945 // There are only a few of those; most control keys are translated to
946 // special terminal-like control sequences.
947 len = 1;
948 switch (keyval)
949 {
950 case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab:
951 string[0] = TAB;
952 break;
953 case GDK_Linefeed:
954 string[0] = NL;
955 break;
956 case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter:
957 string[0] = CAR;
958 break;
959 case GDK_Escape:
960 string[0] = ESC;
961 break;
962 default:
963 len = 0;
964 break;
965 }
966 }
967 string[len] = NUL;
968
969 return len;
970 }
971
972 static int
modifiers_gdk2vim(guint state)973 modifiers_gdk2vim(guint state)
974 {
975 int modifiers = 0;
976
977 if (state & GDK_SHIFT_MASK)
978 modifiers |= MOD_MASK_SHIFT;
979 if (state & GDK_CONTROL_MASK)
980 modifiers |= MOD_MASK_CTRL;
981 if (state & GDK_MOD1_MASK)
982 modifiers |= MOD_MASK_ALT;
983 #if GTK_CHECK_VERSION(2,10,0)
984 if (state & GDK_SUPER_MASK)
985 modifiers |= MOD_MASK_META;
986 #endif
987 if (state & GDK_MOD4_MASK)
988 modifiers |= MOD_MASK_META;
989
990 return modifiers;
991 }
992
993 static int
modifiers_gdk2mouse(guint state)994 modifiers_gdk2mouse(guint state)
995 {
996 int modifiers = 0;
997
998 if (state & GDK_SHIFT_MASK)
999 modifiers |= MOUSE_SHIFT;
1000 if (state & GDK_CONTROL_MASK)
1001 modifiers |= MOUSE_CTRL;
1002 if (state & GDK_MOD1_MASK)
1003 modifiers |= MOUSE_ALT;
1004
1005 return modifiers;
1006 }
1007
1008 /*
1009 * Main keyboard handler:
1010 */
1011 static gint
key_press_event(GtkWidget * widget UNUSED,GdkEventKey * event,gpointer data UNUSED)1012 key_press_event(GtkWidget *widget UNUSED,
1013 GdkEventKey *event,
1014 gpointer data UNUSED)
1015 {
1016 // For GTK+ 2 we know for sure how large the string might get.
1017 // (That is, up to 6 bytes + NUL + CSI escapes + safety measure.)
1018 char_u string[32], string2[32];
1019 guint key_sym;
1020 int len;
1021 int i;
1022 int modifiers;
1023 int key;
1024 guint state;
1025 char_u *s, *d;
1026
1027 gui.event_time = event->time;
1028 key_sym = event->keyval;
1029 state = event->state;
1030
1031 #ifdef FEAT_XIM
1032 if (xim_queue_key_press_event(event, TRUE))
1033 return TRUE;
1034 #endif
1035
1036 #ifdef SunXK_F36
1037 /*
1038 * These keys have bogus lookup strings, and trapping them here is
1039 * easier than trying to XRebindKeysym() on them with every possible
1040 * combination of modifiers.
1041 */
1042 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
1043 len = 0;
1044 else
1045 #endif
1046 {
1047 len = keyval_to_string(key_sym, string2);
1048
1049 // Careful: convert_input() doesn't handle the NUL character.
1050 // No need to convert pure ASCII anyway, thus the len > 1 check.
1051 if (len > 1 && input_conv.vc_type != CONV_NONE)
1052 len = convert_input(string2, len, sizeof(string2));
1053
1054 s = string2;
1055 d = string;
1056 for (i = 0; i < len; ++i)
1057 {
1058 *d++ = s[i];
1059 if (d[-1] == CSI && d + 2 < string + sizeof(string))
1060 {
1061 // Turn CSI into K_CSI.
1062 *d++ = KS_EXTRA;
1063 *d++ = (int)KE_CSI;
1064 }
1065 }
1066 len = d - string;
1067 }
1068
1069 // Shift-Tab results in Left_Tab, but we want <S-Tab>
1070 if (key_sym == GDK_ISO_Left_Tab)
1071 {
1072 key_sym = GDK_Tab;
1073 state |= GDK_SHIFT_MASK;
1074 }
1075
1076 #ifdef FEAT_MENU
1077 // If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key
1078 // is a menu shortcut, we ignore everything with the ALT modifier.
1079 if ((state & GDK_MOD1_MASK)
1080 && gui.menu_is_active
1081 && (*p_wak == 'y'
1082 || (*p_wak == 'm'
1083 && len == 1
1084 && gui_is_menu_shortcut(string[0]))))
1085 // For GTK2 we return false to signify that we haven't handled the
1086 // keypress, so that gtk will handle the mnemonic or accelerator.
1087 return FALSE;
1088 #endif
1089
1090 // We used to apply Alt/Meta to the key here (Mod1Mask), but that is now
1091 // done later, the same as it happens for the terminal. Hopefully that
1092 // works for everybody...
1093
1094 // Check for special keys. Also do this when len == 1 (key has an ASCII
1095 // value) to detect backspace, delete and keypad keys.
1096 if (len == 0 || len == 1)
1097 {
1098 for (i = 0; special_keys[i].key_sym != 0; i++)
1099 {
1100 if (special_keys[i].key_sym == key_sym)
1101 {
1102 string[0] = CSI;
1103 string[1] = special_keys[i].code0;
1104 string[2] = special_keys[i].code1;
1105 len = -3;
1106 break;
1107 }
1108 }
1109 }
1110
1111 if (len == 0) // Unrecognized key
1112 return TRUE;
1113
1114 // For some keys a shift modifier is translated into another key code.
1115 if (len == -3)
1116 key = TO_SPECIAL(string[1], string[2]);
1117 else
1118 {
1119 string[len] = NUL;
1120 key = mb_ptr2char(string);
1121 }
1122
1123 // Handle modifiers.
1124 modifiers = modifiers_gdk2vim(state);
1125
1126 // Recognize special keys.
1127 key = simplify_key(key, &modifiers);
1128 if (key == CSI)
1129 key = K_CSI;
1130 if (IS_SPECIAL(key))
1131 {
1132 string[0] = CSI;
1133 string[1] = K_SECOND(key);
1134 string[2] = K_THIRD(key);
1135 len = 3;
1136 }
1137 else
1138 {
1139 // Some keys need adjustment when the Ctrl modifier is used.
1140 key = may_adjust_key_for_ctrl(modifiers, key);
1141
1142 // May remove the Shift modifier if it's included in the key.
1143 modifiers = may_remove_shift_modifier(modifiers, key);
1144
1145 len = mb_char2bytes(key, string);
1146 }
1147
1148 if (modifiers != 0)
1149 {
1150 string2[0] = CSI;
1151 string2[1] = KS_MODIFIER;
1152 string2[2] = modifiers;
1153 add_to_input_buf(string2, 3);
1154 }
1155
1156 // Check if the key interrupts.
1157 {
1158 int int_ch = check_for_interrupt(key, modifiers);
1159
1160 if (int_ch != NUL)
1161 {
1162 trash_input_buf();
1163 string[0] = int_ch;
1164 len = 1;
1165 }
1166 }
1167
1168 add_to_input_buf(string, len);
1169
1170 // blank out the pointer if necessary
1171 if (p_mh)
1172 gui_mch_mousehide(TRUE);
1173
1174 return TRUE;
1175 }
1176
1177 #if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0)
1178 static gboolean
key_release_event(GtkWidget * widget UNUSED,GdkEventKey * event,gpointer data UNUSED)1179 key_release_event(GtkWidget *widget UNUSED,
1180 GdkEventKey *event,
1181 gpointer data UNUSED)
1182 {
1183 # if defined(FEAT_XIM)
1184 gui.event_time = event->time;
1185 /*
1186 * GTK+ 2 input methods may do fancy stuff on key release events too.
1187 * With the default IM for instance, you can enter any UCS code point
1188 * by holding down CTRL-SHIFT and typing hexadecimal digits.
1189 */
1190 return xim_queue_key_press_event(event, FALSE);
1191 # else
1192 return TRUE;
1193 # endif
1194 }
1195 #endif
1196
1197
1198 /////////////////////////////////////////////////////////////////////////////
1199 // Selection handlers:
1200
1201 // Remember when clip_lose_selection was called from here, we must not call
1202 // gtk_selection_owner_set() then.
1203 static int in_selection_clear_event = FALSE;
1204
1205 static gint
selection_clear_event(GtkWidget * widget UNUSED,GdkEventSelection * event,gpointer user_data UNUSED)1206 selection_clear_event(GtkWidget *widget UNUSED,
1207 GdkEventSelection *event,
1208 gpointer user_data UNUSED)
1209 {
1210 in_selection_clear_event = TRUE;
1211 if (event->selection == clip_plus.gtk_sel_atom)
1212 clip_lose_selection(&clip_plus);
1213 else
1214 clip_lose_selection(&clip_star);
1215 in_selection_clear_event = FALSE;
1216
1217 return TRUE;
1218 }
1219
1220 #define RS_NONE 0 // selection_received_cb() not called yet
1221 #define RS_OK 1 // selection_received_cb() called and OK
1222 #define RS_FAIL 2 // selection_received_cb() called and failed
1223 static int received_selection = RS_NONE;
1224
1225 static void
selection_received_cb(GtkWidget * widget UNUSED,GtkSelectionData * data,guint time_ UNUSED,gpointer user_data UNUSED)1226 selection_received_cb(GtkWidget *widget UNUSED,
1227 GtkSelectionData *data,
1228 guint time_ UNUSED,
1229 gpointer user_data UNUSED)
1230 {
1231 Clipboard_T *cbd;
1232 char_u *text;
1233 char_u *tmpbuf = NULL;
1234 guchar *tmpbuf_utf8 = NULL;
1235 int len;
1236 int motion_type = MAUTO;
1237
1238 if (gtk_selection_data_get_selection(data) == clip_plus.gtk_sel_atom)
1239 cbd = &clip_plus;
1240 else
1241 cbd = &clip_star;
1242
1243 text = (char_u *)gtk_selection_data_get_data(data);
1244 len = gtk_selection_data_get_length(data);
1245
1246 if (text == NULL || len <= 0)
1247 {
1248 received_selection = RS_FAIL;
1249 // clip_free_selection(cbd); ???
1250
1251 return;
1252 }
1253
1254 if (gtk_selection_data_get_data_type(data) == vim_atom)
1255 {
1256 motion_type = *text++;
1257 --len;
1258 }
1259 else if (gtk_selection_data_get_data_type(data) == vimenc_atom)
1260 {
1261 char_u *enc;
1262 vimconv_T conv;
1263
1264 motion_type = *text++;
1265 --len;
1266
1267 enc = text;
1268 text += STRLEN(text) + 1;
1269 len -= text - enc;
1270
1271 // If the encoding of the text is different from 'encoding', attempt
1272 // converting it.
1273 conv.vc_type = CONV_NONE;
1274 convert_setup(&conv, enc, p_enc);
1275 if (conv.vc_type != CONV_NONE)
1276 {
1277 tmpbuf = string_convert(&conv, text, &len);
1278 if (tmpbuf != NULL)
1279 text = tmpbuf;
1280 convert_setup(&conv, NULL, NULL);
1281 }
1282 }
1283
1284 // gtk_selection_data_get_text() handles all the nasty details
1285 // and targets and encodings etc. This rocks so hard.
1286 else
1287 {
1288 tmpbuf_utf8 = gtk_selection_data_get_text(data);
1289 if (tmpbuf_utf8 != NULL)
1290 {
1291 len = STRLEN(tmpbuf_utf8);
1292 if (input_conv.vc_type != CONV_NONE)
1293 {
1294 tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len);
1295 if (tmpbuf != NULL)
1296 text = tmpbuf;
1297 }
1298 else
1299 text = tmpbuf_utf8;
1300 }
1301 else if (len >= 2 && text[0] == 0xff && text[1] == 0xfe)
1302 {
1303 vimconv_T conv;
1304
1305 // UTF-16, we get this for HTML
1306 conv.vc_type = CONV_NONE;
1307 convert_setup_ext(&conv, (char_u *)"utf-16le", FALSE, p_enc, TRUE);
1308
1309 if (conv.vc_type != CONV_NONE)
1310 {
1311 text += 2;
1312 len -= 2;
1313 tmpbuf = string_convert(&conv, text, &len);
1314 convert_setup(&conv, NULL, NULL);
1315 }
1316 if (tmpbuf != NULL)
1317 text = tmpbuf;
1318 }
1319 }
1320
1321 // Chop off any trailing NUL bytes. OpenOffice sends these.
1322 while (len > 0 && text[len - 1] == NUL)
1323 --len;
1324
1325 clip_yank_selection(motion_type, text, (long)len, cbd);
1326 received_selection = RS_OK;
1327 vim_free(tmpbuf);
1328 g_free(tmpbuf_utf8);
1329 }
1330
1331 /*
1332 * Prepare our selection data for passing it to the external selection
1333 * client.
1334 */
1335 static void
selection_get_cb(GtkWidget * widget UNUSED,GtkSelectionData * selection_data,guint info,guint time_ UNUSED,gpointer user_data UNUSED)1336 selection_get_cb(GtkWidget *widget UNUSED,
1337 GtkSelectionData *selection_data,
1338 guint info,
1339 guint time_ UNUSED,
1340 gpointer user_data UNUSED)
1341 {
1342 char_u *string;
1343 char_u *tmpbuf;
1344 long_u tmplen;
1345 int length;
1346 int motion_type;
1347 GdkAtom type;
1348 Clipboard_T *cbd;
1349
1350 if (gtk_selection_data_get_selection(selection_data)
1351 == clip_plus.gtk_sel_atom)
1352 cbd = &clip_plus;
1353 else
1354 cbd = &clip_star;
1355
1356 if (!cbd->owned)
1357 return; // Shouldn't ever happen
1358
1359 if (info != (guint)TARGET_STRING
1360 && (!clip_html || info != (guint)TARGET_HTML)
1361 && info != (guint)TARGET_UTF8_STRING
1362 && info != (guint)TARGET_VIMENC
1363 && info != (guint)TARGET_VIM
1364 && info != (guint)TARGET_COMPOUND_TEXT
1365 && info != (guint)TARGET_TEXT)
1366 return;
1367
1368 // get the selection from the '*'/'+' register
1369 clip_get_selection(cbd);
1370
1371 motion_type = clip_convert_selection(&string, &tmplen, cbd);
1372 if (motion_type < 0 || string == NULL)
1373 return;
1374 // Due to int arguments we can't handle more than G_MAXINT. Also
1375 // reserve one extra byte for NUL or the motion type; just in case.
1376 // (Not that pasting 2G of text is ever going to work, but... ;-)
1377 length = MIN(tmplen, (long_u)(G_MAXINT - 1));
1378
1379 if (info == (guint)TARGET_VIM)
1380 {
1381 tmpbuf = alloc(length + 1);
1382 if (tmpbuf != NULL)
1383 {
1384 tmpbuf[0] = motion_type;
1385 mch_memmove(tmpbuf + 1, string, (size_t)length);
1386 }
1387 // For our own format, the first byte contains the motion type
1388 ++length;
1389 vim_free(string);
1390 string = tmpbuf;
1391 type = vim_atom;
1392 }
1393
1394 else if (info == (guint)TARGET_HTML)
1395 {
1396 vimconv_T conv;
1397
1398 // Since we get utf-16, we probably should set it as well.
1399 conv.vc_type = CONV_NONE;
1400 convert_setup_ext(&conv, p_enc, TRUE, (char_u *)"utf-16le", FALSE);
1401 if (conv.vc_type != CONV_NONE)
1402 {
1403 tmpbuf = string_convert(&conv, string, &length);
1404 convert_setup(&conv, NULL, NULL);
1405 vim_free(string);
1406 string = tmpbuf;
1407 }
1408
1409 // Prepend the BOM: "fffe"
1410 if (string != NULL)
1411 {
1412 tmpbuf = alloc(length + 2);
1413 if (tmpbuf != NULL)
1414 {
1415 tmpbuf[0] = 0xff;
1416 tmpbuf[1] = 0xfe;
1417 mch_memmove(tmpbuf + 2, string, (size_t)length);
1418 vim_free(string);
1419 string = tmpbuf;
1420 length += 2;
1421 }
1422
1423 #if !GTK_CHECK_VERSION(3,0,0)
1424 // Looks redundant even for GTK2 because these values are
1425 // overwritten by gtk_selection_data_set() that follows.
1426 selection_data->type = selection_data->target;
1427 selection_data->format = 16; // 16 bits per char
1428 #endif
1429 gtk_selection_data_set(selection_data, html_atom, 16,
1430 string, length);
1431 vim_free(string);
1432 }
1433 return;
1434 }
1435 else if (info == (guint)TARGET_VIMENC)
1436 {
1437 int l = STRLEN(p_enc);
1438
1439 // contents: motion_type 'encoding' NUL text
1440 tmpbuf = alloc(length + l + 2);
1441 if (tmpbuf != NULL)
1442 {
1443 tmpbuf[0] = motion_type;
1444 STRCPY(tmpbuf + 1, p_enc);
1445 mch_memmove(tmpbuf + l + 2, string, (size_t)length);
1446 length += l + 2;
1447 vim_free(string);
1448 string = tmpbuf;
1449 }
1450 type = vimenc_atom;
1451 }
1452
1453 // gtk_selection_data_set_text() handles everything for us. This is
1454 // so easy and simple and cool, it'd be insane not to use it.
1455 else
1456 {
1457 if (output_conv.vc_type != CONV_NONE)
1458 {
1459 tmpbuf = string_convert(&output_conv, string, &length);
1460 vim_free(string);
1461 if (tmpbuf == NULL)
1462 return;
1463 string = tmpbuf;
1464 }
1465 // Validate the string to avoid runtime warnings
1466 if (g_utf8_validate((const char *)string, (gssize)length, NULL))
1467 {
1468 gtk_selection_data_set_text(selection_data,
1469 (const char *)string, length);
1470 }
1471 vim_free(string);
1472 return;
1473 }
1474
1475 if (string != NULL)
1476 {
1477 #if !GTK_CHECK_VERSION(3,0,0)
1478 // Looks redundant even for GTK2 because these values are
1479 // overwritten by gtk_selection_data_set() that follows.
1480 selection_data->type = selection_data->target;
1481 selection_data->format = 8; // 8 bits per char
1482 #endif
1483 gtk_selection_data_set(selection_data, type, 8, string, length);
1484 vim_free(string);
1485 }
1486 }
1487
1488 /*
1489 * Check if the GUI can be started. Called before gvimrc is sourced and
1490 * before fork().
1491 * Return OK or FAIL.
1492 */
1493 int
gui_mch_early_init_check(int give_message)1494 gui_mch_early_init_check(int give_message)
1495 {
1496 char_u *p;
1497
1498 // Guess that when $DISPLAY isn't set the GUI can't start.
1499 p = mch_getenv((char_u *)"DISPLAY");
1500 if (p == NULL || *p == NUL)
1501 {
1502 gui.dying = TRUE;
1503 if (give_message)
1504 emsg(_((char *)e_opendisp));
1505 return FAIL;
1506 }
1507 return OK;
1508 }
1509
1510 /*
1511 * Check if the GUI can be started. Called before gvimrc is sourced but after
1512 * fork().
1513 * Return OK or FAIL.
1514 */
1515 int
gui_mch_init_check(void)1516 gui_mch_init_check(void)
1517 {
1518 #ifdef USE_GRESOURCE
1519 static int res_registered = FALSE;
1520
1521 if (!res_registered)
1522 {
1523 // Call this function in the GUI process; otherwise, the resources
1524 // won't be available. Don't call it twice.
1525 res_registered = TRUE;
1526 gui_gtk_register_resource();
1527 }
1528 #endif
1529
1530 #if GTK_CHECK_VERSION(3,10,0)
1531 // Vim currently assumes that Gtk means X11, so it cannot use native Gtk
1532 // support for other backends such as Wayland.
1533 gdk_set_allowed_backends ("x11");
1534 #endif
1535
1536 #ifdef FEAT_GUI_GNOME
1537 if (gtk_socket_id == 0)
1538 using_gnome = 1;
1539 #endif
1540
1541 // This defaults to argv[0], but we want it to match the name of the
1542 // shipped gvim.desktop so that Vim's windows can be associated with this
1543 // file.
1544 g_set_prgname("gvim");
1545
1546 // Don't use gtk_init() or gnome_init(), it exits on failure.
1547 if (!gtk_init_check(&gui_argc, &gui_argv))
1548 {
1549 gui.dying = TRUE;
1550 emsg(_((char *)e_opendisp));
1551 return FAIL;
1552 }
1553
1554 return OK;
1555 }
1556
1557 /////////////////////////////////////////////////////////////////////////////
1558 // Mouse handling callbacks
1559
1560
1561 static guint mouse_click_timer = 0;
1562 static int mouse_timed_out = TRUE;
1563
1564 /*
1565 * Timer used to recognize multiple clicks of the mouse button
1566 */
1567 static timeout_cb_type
mouse_click_timer_cb(gpointer data)1568 mouse_click_timer_cb(gpointer data)
1569 {
1570 // we don't use this information currently
1571 int *timed_out = (int *) data;
1572
1573 *timed_out = TRUE;
1574 return FALSE; // don't happen again
1575 }
1576
1577 static guint motion_repeat_timer = 0;
1578 static int motion_repeat_offset = FALSE;
1579 static timeout_cb_type motion_repeat_timer_cb(gpointer);
1580
1581 static void
process_motion_notify(int x,int y,GdkModifierType state)1582 process_motion_notify(int x, int y, GdkModifierType state)
1583 {
1584 int button;
1585 int_u vim_modifiers;
1586 GtkAllocation allocation;
1587
1588 button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1589 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1590 GDK_BUTTON5_MASK))
1591 ? MOUSE_DRAG : ' ';
1592
1593 // If our pointer is currently hidden, then we should show it.
1594 gui_mch_mousehide(FALSE);
1595
1596 // Just moving the rodent above the drawing area without any button
1597 // being pressed.
1598 if (button != MOUSE_DRAG)
1599 {
1600 gui_mouse_moved(x, y);
1601 return;
1602 }
1603
1604 // translate modifier coding between the main engine and GTK
1605 vim_modifiers = modifiers_gdk2mouse(state);
1606
1607 // inform the editor engine about the occurrence of this event
1608 gui_send_mouse_event(button, x, y, FALSE, vim_modifiers);
1609
1610 /*
1611 * Auto repeat timer handling.
1612 */
1613 gtk_widget_get_allocation(gui.drawarea, &allocation);
1614
1615 if (x < 0 || y < 0
1616 || x >= allocation.width
1617 || y >= allocation.height)
1618 {
1619
1620 int dx;
1621 int dy;
1622 int offshoot;
1623 int delay = 10;
1624
1625 // Calculate the maximal distance of the cursor from the drawing area.
1626 // (offshoot can't become negative here!).
1627 dx = x < 0 ? -x : x - allocation.width;
1628 dy = y < 0 ? -y : y - allocation.height;
1629
1630 offshoot = dx > dy ? dx : dy;
1631
1632 // Make a linearly decaying timer delay with a threshold of 5 at a
1633 // distance of 127 pixels from the main window.
1634 //
1635 // One could think endlessly about the most ergonomic variant here.
1636 // For example it could make sense to calculate the distance from the
1637 // drags start instead...
1638 //
1639 // Maybe a parabolic interpolation would suite us better here too...
1640 if (offshoot > 127)
1641 {
1642 // 5 appears to be somehow near to my perceptual limits :-).
1643 delay = 5;
1644 }
1645 else
1646 {
1647 delay = (130 * (127 - offshoot)) / 127 + 5;
1648 }
1649
1650 // shoot again
1651 if (!motion_repeat_timer)
1652 motion_repeat_timer = timeout_add(delay, motion_repeat_timer_cb,
1653 NULL);
1654 }
1655 }
1656
1657 #if GTK_CHECK_VERSION(3,0,0)
1658 static GdkDevice *
gui_gtk_get_pointer_device(GtkWidget * widget)1659 gui_gtk_get_pointer_device(GtkWidget *widget)
1660 {
1661 GdkWindow * const win = gtk_widget_get_window(widget);
1662 GdkDisplay * const dpy = gdk_window_get_display(win);
1663 # if GTK_CHECK_VERSION(3,20,0)
1664 GdkSeat * const seat = gdk_display_get_default_seat(dpy);
1665 return gdk_seat_get_pointer(seat);
1666 # else
1667 GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy);
1668 return gdk_device_manager_get_client_pointer(mngr);
1669 # endif
1670 }
1671
1672 static GdkWindow *
gui_gtk_get_pointer(GtkWidget * widget,gint * x,gint * y,GdkModifierType * state)1673 gui_gtk_get_pointer(GtkWidget *widget,
1674 gint *x,
1675 gint *y,
1676 GdkModifierType *state)
1677 {
1678 GdkWindow * const win = gtk_widget_get_window(widget);
1679 GdkDevice * const dev = gui_gtk_get_pointer_device(widget);
1680 return gdk_window_get_device_position(win, dev , x, y, state);
1681 }
1682
1683 # if defined(FEAT_GUI_TABLINE) || defined(PROTO)
1684 static GdkWindow *
gui_gtk_window_at_position(GtkWidget * widget,gint * x,gint * y)1685 gui_gtk_window_at_position(GtkWidget *widget,
1686 gint *x,
1687 gint *y)
1688 {
1689 GdkDevice * const dev = gui_gtk_get_pointer_device(widget);
1690 return gdk_device_get_window_at_position(dev, x, y);
1691 }
1692 # endif
1693 #else // !GTK_CHECK_VERSION(3,0,0)
1694 # define gui_gtk_get_pointer(wid, x, y, s) \
1695 gdk_window_get_pointer((wid)->window, x, y, s)
1696 # define gui_gtk_window_at_position(wid, x, y) gdk_window_at_pointer(x, y)
1697 #endif
1698
1699 /*
1700 * Timer used to recognize multiple clicks of the mouse button.
1701 */
1702 static timeout_cb_type
motion_repeat_timer_cb(gpointer data UNUSED)1703 motion_repeat_timer_cb(gpointer data UNUSED)
1704 {
1705 int x;
1706 int y;
1707 GdkModifierType state;
1708
1709 gui_gtk_get_pointer(gui.drawarea, &x, &y, &state);
1710
1711 if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1712 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1713 GDK_BUTTON5_MASK)))
1714 {
1715 motion_repeat_timer = 0;
1716 return FALSE;
1717 }
1718
1719 // If there already is a mouse click in the input buffer, wait another
1720 // time (otherwise we would create a backlog of clicks)
1721 if (vim_used_in_input_buf() > 10)
1722 return TRUE;
1723
1724 motion_repeat_timer = 0;
1725
1726 /*
1727 * Fake a motion event.
1728 * Trick: Pretend the mouse moved to the next character on every other
1729 * event, otherwise drag events will be discarded, because they are still
1730 * in the same character.
1731 */
1732 if (motion_repeat_offset)
1733 x += gui.char_width;
1734
1735 motion_repeat_offset = !motion_repeat_offset;
1736 process_motion_notify(x, y, state);
1737
1738 // Don't happen again. We will get reinstalled in the synthetic event
1739 // if needed -- thus repeating should still work.
1740 return FALSE;
1741 }
1742
1743 static gint
motion_notify_event(GtkWidget * widget,GdkEventMotion * event,gpointer data UNUSED)1744 motion_notify_event(GtkWidget *widget,
1745 GdkEventMotion *event,
1746 gpointer data UNUSED)
1747 {
1748 if (event->is_hint)
1749 {
1750 int x;
1751 int y;
1752 GdkModifierType state;
1753
1754 gui_gtk_get_pointer(widget, &x, &y, &state);
1755 process_motion_notify(x, y, state);
1756 }
1757 else
1758 {
1759 process_motion_notify((int)event->x, (int)event->y,
1760 (GdkModifierType)event->state);
1761 }
1762
1763 return TRUE; // handled
1764 }
1765
1766
1767 /*
1768 * Mouse button handling. Note please that we are capturing multiple click's
1769 * by our own timeout mechanism instead of the one provided by GTK+ itself.
1770 * This is due to the way the generic VIM code is recognizing multiple clicks.
1771 */
1772 static gint
button_press_event(GtkWidget * widget,GdkEventButton * event,gpointer data UNUSED)1773 button_press_event(GtkWidget *widget,
1774 GdkEventButton *event,
1775 gpointer data UNUSED)
1776 {
1777 int button;
1778 int repeated_click = FALSE;
1779 int x, y;
1780 int_u vim_modifiers;
1781
1782 gui.event_time = event->time;
1783
1784 // Make sure we have focus now we've been selected
1785 if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget))
1786 gtk_widget_grab_focus(widget);
1787
1788 /*
1789 * Don't let additional events about multiple clicks send by GTK to us
1790 * after the initial button press event confuse us.
1791 */
1792 if (event->type != GDK_BUTTON_PRESS)
1793 return FALSE;
1794
1795 x = event->x;
1796 y = event->y;
1797
1798 // Handle multiple clicks
1799 if (!mouse_timed_out && mouse_click_timer)
1800 {
1801 timeout_remove(mouse_click_timer);
1802 mouse_click_timer = 0;
1803 repeated_click = TRUE;
1804 }
1805
1806 mouse_timed_out = FALSE;
1807 mouse_click_timer = timeout_add(p_mouset, mouse_click_timer_cb,
1808 &mouse_timed_out);
1809
1810 switch (event->button)
1811 {
1812 // Keep in sync with gui_x11.c.
1813 // Buttons 4-7 are handled in scroll_event()
1814 case 1: button = MOUSE_LEFT; break;
1815 case 2: button = MOUSE_MIDDLE; break;
1816 case 3: button = MOUSE_RIGHT; break;
1817 case 8: button = MOUSE_X1; break;
1818 case 9: button = MOUSE_X2; break;
1819 default:
1820 return FALSE; // Unknown button
1821 }
1822
1823 #ifdef FEAT_XIM
1824 // cancel any preediting
1825 if (im_is_preediting())
1826 xim_reset();
1827 #endif
1828
1829 vim_modifiers = modifiers_gdk2mouse(event->state);
1830
1831 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
1832
1833 return TRUE;
1834 }
1835
1836 /*
1837 * GTK+ 2 abstracts scrolling via the GdkEventScroll.
1838 */
1839 static gboolean
scroll_event(GtkWidget * widget,GdkEventScroll * event,gpointer data UNUSED)1840 scroll_event(GtkWidget *widget,
1841 GdkEventScroll *event,
1842 gpointer data UNUSED)
1843 {
1844 int button;
1845 int_u vim_modifiers;
1846
1847 if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget))
1848 gtk_widget_grab_focus(widget);
1849
1850 switch (event->direction)
1851 {
1852 case GDK_SCROLL_UP:
1853 button = MOUSE_4;
1854 break;
1855 case GDK_SCROLL_DOWN:
1856 button = MOUSE_5;
1857 break;
1858 case GDK_SCROLL_LEFT:
1859 button = MOUSE_7;
1860 break;
1861 case GDK_SCROLL_RIGHT:
1862 button = MOUSE_6;
1863 break;
1864 default: // This shouldn't happen
1865 return FALSE;
1866 }
1867
1868 # ifdef FEAT_XIM
1869 // cancel any preediting
1870 if (im_is_preediting())
1871 xim_reset();
1872 # endif
1873
1874 vim_modifiers = modifiers_gdk2mouse(event->state);
1875
1876 gui_send_mouse_event(button, (int)event->x, (int)event->y,
1877 FALSE, vim_modifiers);
1878
1879 return TRUE;
1880 }
1881
1882
1883 static gint
button_release_event(GtkWidget * widget UNUSED,GdkEventButton * event,gpointer data UNUSED)1884 button_release_event(GtkWidget *widget UNUSED,
1885 GdkEventButton *event,
1886 gpointer data UNUSED)
1887 {
1888 int x, y;
1889 int_u vim_modifiers;
1890
1891 gui.event_time = event->time;
1892
1893 // Remove any motion "machine gun" timers used for automatic further
1894 // extension of allocation areas if outside of the applications window
1895 // area .
1896 if (motion_repeat_timer)
1897 {
1898 timeout_remove(motion_repeat_timer);
1899 motion_repeat_timer = 0;
1900 }
1901
1902 x = event->x;
1903 y = event->y;
1904
1905 vim_modifiers = modifiers_gdk2mouse(event->state);
1906
1907 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers);
1908
1909 return TRUE;
1910 }
1911
1912
1913 #ifdef FEAT_DND
1914 /////////////////////////////////////////////////////////////////////////////
1915 // Drag aNd Drop support handlers.
1916
1917 /*
1918 * Count how many items there may be and separate them with a NUL.
1919 * Apparently the items are separated with \r\n. This is not documented,
1920 * thus be careful not to go past the end. Also allow separation with
1921 * NUL characters.
1922 */
1923 static int
count_and_decode_uri_list(char_u * out,char_u * raw,int len)1924 count_and_decode_uri_list(char_u *out, char_u *raw, int len)
1925 {
1926 int i;
1927 char_u *p = out;
1928 int count = 0;
1929
1930 for (i = 0; i < len; ++i)
1931 {
1932 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r')
1933 {
1934 if (p > out && p[-1] != NUL)
1935 {
1936 ++count;
1937 *p++ = NUL;
1938 }
1939 }
1940 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0)
1941 {
1942 *p++ = hexhex2nr(raw + i + 1);
1943 i += 2;
1944 }
1945 else
1946 *p++ = raw[i];
1947 }
1948 if (p > out && p[-1] != NUL)
1949 {
1950 *p = NUL; // last item didn't have \r or \n
1951 ++count;
1952 }
1953 return count;
1954 }
1955
1956 /*
1957 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
1958 * this process, URI which protocol is not "file:" are removed. Return
1959 * length of array (less than "max").
1960 */
1961 static int
filter_uri_list(char_u ** outlist,int max,char_u * src)1962 filter_uri_list(char_u **outlist, int max, char_u *src)
1963 {
1964 int i, j;
1965
1966 for (i = j = 0; i < max; ++i)
1967 {
1968 outlist[i] = NULL;
1969 if (STRNCMP(src, "file:", 5) == 0)
1970 {
1971 src += 5;
1972 if (STRNCMP(src, "//localhost", 11) == 0)
1973 src += 11;
1974 while (src[0] == '/' && src[1] == '/')
1975 ++src;
1976 outlist[j++] = vim_strsave(src);
1977 }
1978 src += STRLEN(src) + 1;
1979 }
1980 return j;
1981 }
1982
1983 static char_u **
parse_uri_list(int * count,char_u * data,int len)1984 parse_uri_list(int *count, char_u *data, int len)
1985 {
1986 int n = 0;
1987 char_u *tmp = NULL;
1988 char_u **array = NULL;
1989
1990 if (data != NULL && len > 0 && (tmp = alloc(len + 1)) != NULL)
1991 {
1992 n = count_and_decode_uri_list(tmp, data, len);
1993 if (n > 0 && (array = ALLOC_MULT(char_u *, n)) != NULL)
1994 n = filter_uri_list(array, n, tmp);
1995 }
1996 vim_free(tmp);
1997 *count = n;
1998 return array;
1999 }
2000
2001 static void
drag_handle_uri_list(GdkDragContext * context,GtkSelectionData * data,guint time_,GdkModifierType state,gint x,gint y)2002 drag_handle_uri_list(GdkDragContext *context,
2003 GtkSelectionData *data,
2004 guint time_,
2005 GdkModifierType state,
2006 gint x,
2007 gint y)
2008 {
2009 char_u **fnames;
2010 int nfiles = 0;
2011
2012 fnames = parse_uri_list(&nfiles,
2013 (char_u *)gtk_selection_data_get_data(data),
2014 gtk_selection_data_get_length(data));
2015
2016 if (fnames != NULL && nfiles > 0)
2017 {
2018 int_u modifiers;
2019
2020 gtk_drag_finish(context, TRUE, FALSE, time_); // accept
2021
2022 modifiers = modifiers_gdk2mouse(state);
2023
2024 gui_handle_drop(x, y, modifiers, fnames, nfiles);
2025 }
2026 else
2027 vim_free(fnames);
2028 }
2029
2030 static void
drag_handle_text(GdkDragContext * context,GtkSelectionData * data,guint time_,GdkModifierType state)2031 drag_handle_text(GdkDragContext *context,
2032 GtkSelectionData *data,
2033 guint time_,
2034 GdkModifierType state)
2035 {
2036 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP};
2037 char_u *text;
2038 int len;
2039 char_u *tmpbuf = NULL;
2040
2041 text = (char_u *)gtk_selection_data_get_data(data);
2042 len = gtk_selection_data_get_length(data);
2043
2044 if (gtk_selection_data_get_data_type(data) == utf8_string_atom)
2045 {
2046 if (input_conv.vc_type != CONV_NONE)
2047 tmpbuf = string_convert(&input_conv, text, &len);
2048 if (tmpbuf != NULL)
2049 text = tmpbuf;
2050 }
2051
2052 dnd_yank_drag_data(text, (long)len);
2053 gtk_drag_finish(context, TRUE, FALSE, time_); // accept
2054 vim_free(tmpbuf);
2055
2056 dropkey[2] = modifiers_gdk2vim(state);
2057
2058 if (dropkey[2] != 0)
2059 add_to_input_buf(dropkey, (int)sizeof(dropkey));
2060 else
2061 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3));
2062 }
2063
2064 /*
2065 * DND receiver.
2066 */
2067 static void
drag_data_received_cb(GtkWidget * widget,GdkDragContext * context,gint x,gint y,GtkSelectionData * data,guint info,guint time_,gpointer user_data UNUSED)2068 drag_data_received_cb(GtkWidget *widget,
2069 GdkDragContext *context,
2070 gint x,
2071 gint y,
2072 GtkSelectionData *data,
2073 guint info,
2074 guint time_,
2075 gpointer user_data UNUSED)
2076 {
2077 GdkModifierType state;
2078
2079 // Guard against trash
2080 const guchar * const data_data = gtk_selection_data_get_data(data);
2081 const gint data_length = gtk_selection_data_get_length(data);
2082 const gint data_format = gtk_selection_data_get_format(data);
2083
2084 if (data_data == NULL
2085 || data_length <= 0
2086 || data_format != 8
2087 || data_data[data_length] != '\0')
2088 {
2089 gtk_drag_finish(context, FALSE, FALSE, time_);
2090 return;
2091 }
2092
2093 // Get the current modifier state for proper distinguishment between
2094 // different operations later.
2095 gui_gtk_get_pointer(widget, NULL, NULL, &state);
2096
2097 // Not sure about the role of "text/plain" here...
2098 if (info == (guint)TARGET_TEXT_URI_LIST)
2099 drag_handle_uri_list(context, data, time_, state, x, y);
2100 else
2101 drag_handle_text(context, data, time_, state);
2102
2103 }
2104 #endif // FEAT_DND
2105
2106
2107 #if defined(USE_GNOME_SESSION)
2108 /*
2109 * GnomeClient interact callback. Check for unsaved buffers that cannot
2110 * be abandoned and pop up a dialog asking the user for confirmation if
2111 * necessary.
2112 */
2113 static void
sm_client_check_changed_any(GnomeClient * client UNUSED,gint key,GnomeDialogType type UNUSED,gpointer data UNUSED)2114 sm_client_check_changed_any(GnomeClient *client UNUSED,
2115 gint key,
2116 GnomeDialogType type UNUSED,
2117 gpointer data UNUSED)
2118 {
2119 cmdmod_T save_cmdmod;
2120 gboolean shutdown_cancelled;
2121
2122 save_cmdmod = cmdmod;
2123
2124 # ifdef FEAT_BROWSE
2125 cmdmod.cmod_flags |= CMOD_BROWSE;
2126 # endif
2127 # if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2128 cmdmod.cmod_flags |= CMOD_CONFIRM;
2129 # endif
2130 /*
2131 * If there are changed buffers, present the user with
2132 * a dialog if possible, otherwise give an error message.
2133 */
2134 shutdown_cancelled = check_changed_any(FALSE, FALSE);
2135
2136 exiting = FALSE;
2137 cmdmod = save_cmdmod;
2138 setcursor(); // position the cursor
2139 out_flush();
2140 /*
2141 * If the user hit the [Cancel] button the whole shutdown
2142 * will be cancelled. Wow, quite powerful feature (:
2143 */
2144 gnome_interaction_key_return(key, shutdown_cancelled);
2145 }
2146
2147 /*
2148 * "save_yourself" signal handler. Initiate an interaction to ask the user
2149 * for confirmation if necessary. Save the current editing session and tell
2150 * the session manager how to restart Vim.
2151 */
2152 static gboolean
sm_client_save_yourself(GnomeClient * client,gint phase UNUSED,GnomeSaveStyle save_style UNUSED,gboolean shutdown UNUSED,GnomeInteractStyle interact_style,gboolean fast UNUSED,gpointer data UNUSED)2153 sm_client_save_yourself(GnomeClient *client,
2154 gint phase UNUSED,
2155 GnomeSaveStyle save_style UNUSED,
2156 gboolean shutdown UNUSED,
2157 GnomeInteractStyle interact_style,
2158 gboolean fast UNUSED,
2159 gpointer data UNUSED)
2160 {
2161 static const char suffix[] = "-session.vim";
2162 char *session_file;
2163 unsigned int len;
2164 gboolean success;
2165
2166 // Always request an interaction if possible. check_changed_any()
2167 // won't actually show a dialog unless any buffers have been modified.
2168 // There doesn't seem to be an obvious way to check that without
2169 // automatically firing the dialog. Anyway, it works just fine.
2170 if (interact_style == GNOME_INTERACT_ANY)
2171 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL,
2172 &sm_client_check_changed_any,
2173 NULL);
2174 out_flush();
2175 ml_sync_all(FALSE, FALSE); // preserve all swap files
2176
2177 // The path is unique for each session save. We do neither know nor care
2178 // which session script will actually be used later. This decision is in
2179 // the domain of the session manager.
2180 session_file = gnome_config_get_real_path(
2181 gnome_client_get_config_prefix(client));
2182 len = strlen(session_file);
2183
2184 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR)
2185 --len; // get rid of the superfluous trailing '/'
2186
2187 session_file = g_renew(char, session_file, len + sizeof(suffix));
2188 memcpy(session_file + len, suffix, sizeof(suffix));
2189
2190 success = write_session_file((char_u *)session_file);
2191
2192 if (success)
2193 {
2194 const char *argv[8];
2195 int i;
2196
2197 // Tell the session manager how to wipe out the stored session data.
2198 // This isn't as dangerous as it looks, don't worry :) session_file
2199 // is a unique absolute filename. Usually it'll be something like
2200 // `/home/user/.gnome2/vim-XXXXXX-session.vim'.
2201 i = 0;
2202 argv[i++] = "rm";
2203 argv[i++] = session_file;
2204 argv[i] = NULL;
2205
2206 gnome_client_set_discard_command(client, i, (char **)argv);
2207
2208 // Tell the session manager how to restore the just saved session.
2209 // This is easily done thanks to Vim's -S option. Pass the -f flag
2210 // since there's no need to fork -- it might even cause confusion.
2211 // Also pass the window role to give the WM something to match on.
2212 // The role is set in gui_mch_open(), thus should _never_ be NULL.
2213 i = 0;
2214 argv[i++] = restart_command;
2215 argv[i++] = "-f";
2216 argv[i++] = "-g";
2217 argv[i++] = "--role";
2218 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin));
2219 argv[i++] = "-S";
2220 argv[i++] = session_file;
2221 argv[i] = NULL;
2222
2223 gnome_client_set_restart_command(client, i, (char **)argv);
2224 gnome_client_set_clone_command(client, 0, NULL);
2225 }
2226
2227 g_free(session_file);
2228
2229 return success;
2230 }
2231
2232 /*
2233 * Called when the session manager wants us to die. There isn't much to save
2234 * here since "save_yourself" has been emitted before (unless serious trouble
2235 * is happening).
2236 */
2237 static void
sm_client_die(GnomeClient * client UNUSED,gpointer data UNUSED)2238 sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED)
2239 {
2240 // Don't write messages to the GUI anymore
2241 full_screen = FALSE;
2242
2243 vim_strncpy(IObuff, (char_u *)
2244 _("Vim: Received \"die\" request from session manager\n"),
2245 IOSIZE - 1);
2246 preserve_exit();
2247 }
2248
2249 /*
2250 * Connect our signal handlers to be notified on session save and shutdown.
2251 */
2252 static void
setup_save_yourself(void)2253 setup_save_yourself(void)
2254 {
2255 GnomeClient *client;
2256
2257 client = gnome_master_client();
2258
2259 if (client != NULL)
2260 {
2261 // Must use the deprecated gtk_signal_connect() for compatibility
2262 // with GNOME 1. Arrgh, zombies!
2263 gtk_signal_connect(GTK_OBJECT(client), "save_yourself",
2264 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL);
2265 gtk_signal_connect(GTK_OBJECT(client), "die",
2266 GTK_SIGNAL_FUNC(&sm_client_die), NULL);
2267 }
2268 }
2269
2270 #else // !USE_GNOME_SESSION
2271
2272 # ifdef USE_XSMP
2273 /*
2274 * GTK tells us that XSMP needs attention
2275 */
2276 static gboolean
local_xsmp_handle_requests(GIOChannel * source UNUSED,GIOCondition condition,gpointer data)2277 local_xsmp_handle_requests(
2278 GIOChannel *source UNUSED,
2279 GIOCondition condition,
2280 gpointer data)
2281 {
2282 if (condition == G_IO_IN)
2283 {
2284 // Do stuff; maybe close connection
2285 if (xsmp_handle_requests() == FAIL)
2286 g_io_channel_unref((GIOChannel *)data);
2287 return TRUE;
2288 }
2289 // Error
2290 g_io_channel_unref((GIOChannel *)data);
2291 xsmp_close();
2292 return TRUE;
2293 }
2294 # endif // USE_XSMP
2295
2296 /*
2297 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2298 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2299 */
2300 static void
setup_save_yourself(void)2301 setup_save_yourself(void)
2302 {
2303 Atom *existing_atoms = NULL;
2304 int count = 0;
2305
2306 # ifdef USE_XSMP
2307 if (xsmp_icefd != -1)
2308 {
2309 /*
2310 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2311 * set up GTK IO monitor
2312 */
2313 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd);
2314
2315 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP,
2316 local_xsmp_handle_requests, (gpointer)g_io);
2317 g_io_channel_unref(g_io);
2318 }
2319 else
2320 # endif
2321 {
2322 // Fall back to old method
2323
2324 // first get the existing value
2325 GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin);
2326
2327 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win),
2328 GDK_WINDOW_XID(mainwin_win),
2329 &existing_atoms, &count))
2330 {
2331 Atom *new_atoms;
2332 Atom save_yourself_xatom;
2333 int i;
2334
2335 save_yourself_xatom = GET_X_ATOM(save_yourself_atom);
2336
2337 // check if WM_SAVE_YOURSELF isn't there yet
2338 for (i = 0; i < count; ++i)
2339 if (existing_atoms[i] == save_yourself_xatom)
2340 break;
2341
2342 if (i == count)
2343 {
2344 // allocate an Atoms array which is one item longer
2345 new_atoms = ALLOC_MULT(Atom, count + 1);
2346 if (new_atoms != NULL)
2347 {
2348 memcpy(new_atoms, existing_atoms, count * sizeof(Atom));
2349 new_atoms[count] = save_yourself_xatom;
2350 XSetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win),
2351 GDK_WINDOW_XID(mainwin_win),
2352 new_atoms, count + 1);
2353 vim_free(new_atoms);
2354 }
2355 }
2356 XFree(existing_atoms);
2357 }
2358 }
2359 }
2360
2361 /*
2362 * Installing a global event filter seems to be the only way to catch
2363 * client messages of type WM_PROTOCOLS without overriding GDK's own
2364 * client message event filter. Well, that's still better than trying
2365 * to guess what the GDK filter had done if it had been invoked instead
2366 *
2367 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2368 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2369 * I have the nasty feeling modern session managers just don't send this
2370 * deprecated message anymore. Addition: confirmed by several people.
2371 *
2372 * The GNOME session support is much cooler anyway. Unlike this ugly
2373 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2374 * it should work with KDE as well.
2375 */
2376 static GdkFilterReturn
global_event_filter(GdkXEvent * xev,GdkEvent * event UNUSED,gpointer data UNUSED)2377 global_event_filter(GdkXEvent *xev,
2378 GdkEvent *event UNUSED,
2379 gpointer data UNUSED)
2380 {
2381 XEvent *xevent = (XEvent *)xev;
2382
2383 if (xevent != NULL
2384 && xevent->type == ClientMessage
2385 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom)
2386 && (long_u)xevent->xclient.data.l[0]
2387 == GET_X_ATOM(save_yourself_atom))
2388 {
2389 out_flush();
2390 ml_sync_all(FALSE, FALSE); // preserve all swap files
2391 /*
2392 * Set the window's WM_COMMAND property, to let the window manager
2393 * know we are done saving ourselves. We don't want to be
2394 * restarted, thus set argv to NULL.
2395 */
2396 XSetCommand(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)),
2397 GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin)),
2398 NULL, 0);
2399 return GDK_FILTER_REMOVE;
2400 }
2401
2402 return GDK_FILTER_CONTINUE;
2403 }
2404 #endif // !USE_GNOME_SESSION
2405
2406
2407 /*
2408 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2409 */
2410 static void
mainwin_realize(GtkWidget * widget UNUSED,gpointer data UNUSED)2411 mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED)
2412 {
2413 // If you get an error message here, you still need to unpack the runtime
2414 // archive!
2415 #ifdef magick
2416 # undef magick
2417 #endif
2418 // A bit hackish, but avoids casting later and allows optimization
2419 # define static static const
2420 #define magick vim32x32
2421 #include "../runtime/vim32x32.xpm"
2422 #undef magick
2423 #define magick vim16x16
2424 #include "../runtime/vim16x16.xpm"
2425 #undef magick
2426 #define magick vim48x48
2427 #include "../runtime/vim48x48.xpm"
2428 #undef magick
2429 # undef static
2430
2431 GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin);
2432
2433 // When started with "--echo-wid" argument, write window ID on stdout.
2434 if (echo_wid_arg)
2435 {
2436 printf("WID: %ld\n", (long)GDK_WINDOW_XID(mainwin_win));
2437 fflush(stdout);
2438 }
2439
2440 if (vim_strchr(p_go, GO_ICON) != NULL)
2441 {
2442 /*
2443 * Add an icon to the main window. For fun and convenience of the user.
2444 */
2445 GList *icons = NULL;
2446
2447 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16));
2448 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32));
2449 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48));
2450
2451 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons);
2452
2453 // TODO: is this type cast OK?
2454 g_list_foreach(icons, (GFunc)(void *)&g_object_unref, NULL);
2455 g_list_free(icons);
2456 }
2457
2458 #if !defined(USE_GNOME_SESSION)
2459 // Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F
2460 gdk_window_add_filter(NULL, &global_event_filter, NULL);
2461 #endif
2462 // Setup to indicate to the window manager that we want to catch the
2463 // WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2464 // manager instead.
2465 #if defined(USE_GNOME_SESSION)
2466 if (using_gnome)
2467 #endif
2468 setup_save_yourself();
2469
2470 #ifdef FEAT_CLIENTSERVER
2471 if (serverName == NULL && serverDelayedStartName != NULL)
2472 {
2473 // This is a :gui command in a plain vim with no previous server
2474 commWindow = GDK_WINDOW_XID(mainwin_win);
2475
2476 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(mainwin_win),
2477 serverDelayedStartName);
2478 }
2479 else
2480 {
2481 /*
2482 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2483 * have to change the "server" registration to that of the main window
2484 * If we have not registered a name yet, remember the window.
2485 */
2486 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(mainwin_win),
2487 GDK_WINDOW_XID(mainwin_win));
2488 }
2489 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK);
2490 g_signal_connect(G_OBJECT(gui.mainwin), "property-notify-event",
2491 G_CALLBACK(property_event), NULL);
2492 #endif
2493 }
2494
2495 static GdkCursor *
create_blank_pointer(void)2496 create_blank_pointer(void)
2497 {
2498 GdkWindow *root_window = NULL;
2499 #if GTK_CHECK_VERSION(3,0,0)
2500 GdkPixbuf *blank_mask;
2501 #else
2502 GdkPixmap *blank_mask;
2503 #endif
2504 GdkCursor *cursor;
2505 #if GTK_CHECK_VERSION(3,0,0)
2506 GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
2507 #else
2508 GdkColor color = { 0, 0, 0, 0 };
2509 char blank_data[] = { 0x0 };
2510 #endif
2511
2512 #if GTK_CHECK_VERSION(3,12,0)
2513 {
2514 GdkWindow * const win = gtk_widget_get_window(gui.mainwin);
2515 GdkScreen * const scrn = gdk_window_get_screen(win);
2516 root_window = gdk_screen_get_root_window(scrn);
2517 }
2518 #else
2519 root_window = gtk_widget_get_root_window(gui.mainwin);
2520 #endif
2521
2522 // Create a pseudo blank pointer, which is in fact one pixel by one pixel
2523 // in size.
2524 #if GTK_CHECK_VERSION(3,0,0)
2525 {
2526 cairo_surface_t *surf;
2527 cairo_t *cr;
2528
2529 surf = cairo_image_surface_create(CAIRO_FORMAT_A1, 1, 1);
2530 cr = cairo_create(surf);
2531
2532 cairo_set_source_rgba(cr,
2533 color.red,
2534 color.green,
2535 color.blue,
2536 color.alpha);
2537 cairo_rectangle(cr, 0, 0, 1, 1);
2538 cairo_fill(cr);
2539 cairo_destroy(cr);
2540
2541 blank_mask = gdk_pixbuf_get_from_surface(surf, 0, 0, 1, 1);
2542 cairo_surface_destroy(surf);
2543
2544 cursor = gdk_cursor_new_from_pixbuf(gdk_window_get_display(root_window),
2545 blank_mask, 0, 0);
2546 g_object_unref(blank_mask);
2547 }
2548 #else
2549 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1);
2550 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask,
2551 &color, &color, 0, 0);
2552 gdk_bitmap_unref(blank_mask);
2553 #endif
2554
2555 return cursor;
2556 }
2557
2558 static void
mainwin_screen_changed_cb(GtkWidget * widget,GdkScreen * previous_screen UNUSED,gpointer data UNUSED)2559 mainwin_screen_changed_cb(GtkWidget *widget,
2560 GdkScreen *previous_screen UNUSED,
2561 gpointer data UNUSED)
2562 {
2563 if (!gtk_widget_has_screen(widget))
2564 return;
2565
2566 /*
2567 * Recreate the invisible mouse cursor.
2568 */
2569 if (gui.blank_pointer != NULL)
2570 #if GTK_CHECK_VERSION(3,0,0)
2571 g_object_unref(G_OBJECT(gui.blank_pointer));
2572 #else
2573 gdk_cursor_unref(gui.blank_pointer);
2574 #endif
2575
2576 gui.blank_pointer = create_blank_pointer();
2577
2578 if (gui.pointer_hidden && gtk_widget_get_window(gui.drawarea) != NULL)
2579 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
2580 gui.blank_pointer);
2581
2582 /*
2583 * Create a new PangoContext for this screen, and initialize it
2584 * with the current font if necessary.
2585 */
2586 if (gui.text_context != NULL)
2587 g_object_unref(gui.text_context);
2588
2589 gui.text_context = gtk_widget_create_pango_context(widget);
2590 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
2591
2592 if (gui.norm_font != NULL)
2593 {
2594 gui_mch_init_font(p_guifont, FALSE);
2595 gui_set_shellsize(TRUE, FALSE, RESIZE_BOTH);
2596 }
2597 }
2598
2599 /*
2600 * After the drawing area comes up, we calculate all colors and create the
2601 * dummy blank cursor.
2602 *
2603 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2604 * fact that the main VIM engine doesn't take them into account anywhere.
2605 */
2606 static void
drawarea_realize_cb(GtkWidget * widget,gpointer data UNUSED)2607 drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED)
2608 {
2609 GtkWidget *sbar;
2610 GtkAllocation allocation;
2611
2612 #ifdef FEAT_XIM
2613 xim_init();
2614 #endif
2615 gui_mch_new_colors();
2616 #if GTK_CHECK_VERSION(3,0,0)
2617 gui.surface = gdk_window_create_similar_surface(
2618 gtk_widget_get_window(widget),
2619 CAIRO_CONTENT_COLOR_ALPHA,
2620 gtk_widget_get_allocated_width(widget),
2621 gtk_widget_get_allocated_height(widget));
2622 #else
2623 gui.text_gc = gdk_gc_new(gui.drawarea->window);
2624 #endif
2625
2626 gui.blank_pointer = create_blank_pointer();
2627 if (gui.pointer_hidden)
2628 gdk_window_set_cursor(gtk_widget_get_window(widget), gui.blank_pointer);
2629
2630 // get the actual size of the scrollbars, if they are realized
2631 sbar = firstwin->w_scrollbars[SBAR_LEFT].id;
2632 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT]
2633 && firstwin->w_scrollbars[SBAR_RIGHT].id))
2634 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id;
2635 gtk_widget_get_allocation(sbar, &allocation);
2636 if (sbar && gtk_widget_get_realized(sbar) && allocation.width)
2637 gui.scrollbar_width = allocation.width;
2638
2639 sbar = gui.bottom_sbar.id;
2640 if (sbar && gtk_widget_get_realized(sbar) && allocation.height)
2641 gui.scrollbar_height = allocation.height;
2642 }
2643
2644 /*
2645 * Properly clean up on shutdown.
2646 */
2647 static void
drawarea_unrealize_cb(GtkWidget * widget UNUSED,gpointer data UNUSED)2648 drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED)
2649 {
2650 // Don't write messages to the GUI anymore
2651 full_screen = FALSE;
2652
2653 #ifdef FEAT_XIM
2654 im_shutdown();
2655 #endif
2656 if (gui.ascii_glyphs != NULL)
2657 {
2658 pango_glyph_string_free(gui.ascii_glyphs);
2659 gui.ascii_glyphs = NULL;
2660 }
2661 if (gui.ascii_font != NULL)
2662 {
2663 g_object_unref(gui.ascii_font);
2664 gui.ascii_font = NULL;
2665 }
2666 g_object_unref(gui.text_context);
2667 gui.text_context = NULL;
2668
2669 #if GTK_CHECK_VERSION(3,0,0)
2670 if (gui.surface != NULL)
2671 {
2672 cairo_surface_destroy(gui.surface);
2673 gui.surface = NULL;
2674 }
2675 #else
2676 g_object_unref(gui.text_gc);
2677 gui.text_gc = NULL;
2678 #endif
2679
2680 #if GTK_CHECK_VERSION(3,0,0)
2681 g_object_unref(G_OBJECT(gui.blank_pointer));
2682 #else
2683 gdk_cursor_unref(gui.blank_pointer);
2684 #endif
2685 gui.blank_pointer = NULL;
2686 }
2687
2688 #if GTK_CHECK_VERSION(3,22,2)
2689 static void
drawarea_style_updated_cb(GtkWidget * widget UNUSED,gpointer data UNUSED)2690 drawarea_style_updated_cb(GtkWidget *widget UNUSED,
2691 gpointer data UNUSED)
2692 #else
2693 static void
2694 drawarea_style_set_cb(GtkWidget *widget UNUSED,
2695 GtkStyle *previous_style UNUSED,
2696 gpointer data UNUSED)
2697 #endif
2698 {
2699 gui_mch_new_colors();
2700 }
2701
2702 #if GTK_CHECK_VERSION(3,0,0)
2703 static gboolean
drawarea_configure_event_cb(GtkWidget * widget,GdkEventConfigure * event,gpointer data UNUSED)2704 drawarea_configure_event_cb(GtkWidget *widget,
2705 GdkEventConfigure *event,
2706 gpointer data UNUSED)
2707 {
2708 static int cur_width = 0;
2709 static int cur_height = 0;
2710
2711 g_return_val_if_fail(event
2712 && event->width >= 1 && event->height >= 1, TRUE);
2713
2714 # if GTK_CHECK_VERSION(3,22,2) && !GTK_CHECK_VERSION(3,22,4)
2715 // As of 3.22.2, GdkWindows have started distributing configure events to
2716 // their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0).
2717 //
2718 // As can be seen from the implementation of move_native_children() and
2719 // configure_native_child() in gdkwindow.c, those functions actually
2720 // propagate configure events to every child, failing to distinguish
2721 // "native" one from non-native one.
2722 //
2723 // Naturally, configure events propagated to here like that are fallacious
2724 // and, as a matter of fact, they trigger a geometric collapse of
2725 // gui.drawarea in fullscreen and maximized modes.
2726 //
2727 // To filter out such nuisance events, we are making use of the fact that
2728 // the field send_event of such GdkEventConfigures is set to FALSE in
2729 // configure_native_child().
2730 //
2731 // Obviously, this is a terrible hack making GVim depend on GTK's
2732 // implementation details. Therefore, watch out any relevant internal
2733 // changes happening in GTK in the feature (sigh).
2734 //
2735 // Follow-up
2736 // After a few weeks later, the GdkWindow change mentioned above was
2737 // reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155).
2738 // The corresponding official release is 3.22.4.
2739 if (event->send_event == FALSE)
2740 return TRUE;
2741 # endif
2742
2743 if (event->width == cur_width && event->height == cur_height)
2744 return TRUE;
2745
2746 cur_width = event->width;
2747 cur_height = event->height;
2748
2749 if (gui.surface != NULL)
2750 cairo_surface_destroy(gui.surface);
2751
2752 gui.surface = gdk_window_create_similar_surface(
2753 gtk_widget_get_window(widget),
2754 CAIRO_CONTENT_COLOR_ALPHA,
2755 event->width, event->height);
2756
2757 gtk_widget_queue_draw(widget);
2758
2759 return TRUE;
2760 }
2761 #endif
2762
2763 /*
2764 * Callback routine for the "delete_event" signal on the toplevel window.
2765 * Tries to vim gracefully, or refuses to exit with changed buffers.
2766 */
2767 static gint
delete_event_cb(GtkWidget * widget UNUSED,GdkEventAny * event UNUSED,gpointer data UNUSED)2768 delete_event_cb(GtkWidget *widget UNUSED,
2769 GdkEventAny *event UNUSED,
2770 gpointer data UNUSED)
2771 {
2772 gui_shell_closed();
2773 return TRUE;
2774 }
2775
2776 #if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
2777 static int
get_item_dimensions(GtkWidget * widget,GtkOrientation orientation)2778 get_item_dimensions(GtkWidget *widget, GtkOrientation orientation)
2779 {
2780 # ifdef FEAT_GUI_GNOME
2781 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL;
2782
2783 if (using_gnome && widget != NULL)
2784 {
2785 GtkWidget *parent;
2786 BonoboDockItem *dockitem;
2787
2788 parent = gtk_widget_get_parent(widget);
2789 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM)
2790 {
2791 // Only menu & toolbar are dock items. Could tabline be?
2792 // Seem to be only the 2 defined in GNOME
2793 widget = parent;
2794 dockitem = BONOBO_DOCK_ITEM(widget);
2795
2796 if (dockitem == NULL || dockitem->is_floating)
2797 return 0;
2798 item_orientation = bonobo_dock_item_get_orientation(dockitem);
2799 }
2800 }
2801 # else
2802 # define item_orientation GTK_ORIENTATION_HORIZONTAL
2803 # endif
2804
2805 if (widget != NULL
2806 && item_orientation == orientation
2807 && gtk_widget_get_realized(widget)
2808 && gtk_widget_get_visible(widget))
2809 {
2810 # if GTK_CHECK_VERSION(3,0,0) || !defined(FEAT_GUI_GNOME)
2811 GtkAllocation allocation;
2812
2813 gtk_widget_get_allocation(widget, &allocation);
2814 return allocation.height;
2815 # else
2816 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2817 return widget->allocation.height;
2818 else
2819 return widget->allocation.width;
2820 # endif
2821 }
2822 return 0;
2823 }
2824 #endif
2825
2826 static int
get_menu_tool_width(void)2827 get_menu_tool_width(void)
2828 {
2829 int width = 0;
2830
2831 #ifdef FEAT_GUI_GNOME // these are never vertical without GNOME
2832 # ifdef FEAT_MENU
2833 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL);
2834 # endif
2835 # ifdef FEAT_TOOLBAR
2836 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL);
2837 # endif
2838 # ifdef FEAT_GUI_TABLINE
2839 if (gui.tabline != NULL)
2840 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL);
2841 # endif
2842 #endif
2843
2844 return width;
2845 }
2846
2847 static int
get_menu_tool_height(void)2848 get_menu_tool_height(void)
2849 {
2850 int height = 0;
2851
2852 #ifdef FEAT_MENU
2853 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL);
2854 #endif
2855 #ifdef FEAT_TOOLBAR
2856 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL);
2857 #endif
2858 #ifdef FEAT_GUI_TABLINE
2859 if (gui.tabline != NULL)
2860 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL);
2861 #endif
2862
2863 return height;
2864 }
2865
2866 // This controls whether we can set the real window hints at
2867 // start-up when in a GtkPlug.
2868 // 0 = normal processing (default)
2869 // 1 = init. hints set, no-one's tried to reset since last check
2870 // 2 = init. hints set, attempt made to change hints
2871 static int init_window_hints_state = 0;
2872
2873 static void
update_window_manager_hints(int force_width,int force_height)2874 update_window_manager_hints(int force_width, int force_height)
2875 {
2876 static int old_width = 0;
2877 static int old_height = 0;
2878 static int old_min_width = 0;
2879 static int old_min_height = 0;
2880 static int old_char_width = 0;
2881 static int old_char_height = 0;
2882
2883 int width;
2884 int height;
2885 int min_width;
2886 int min_height;
2887
2888 // At start-up, don't try to set the hints until the initial
2889 // values have been used (those that dictate our initial size)
2890 // Let forced (i.e., correct) values through always.
2891 if (!(force_width && force_height) && init_window_hints_state > 0)
2892 {
2893 // Don't do it!
2894 init_window_hints_state = 2;
2895 return;
2896 }
2897
2898 // This also needs to be done when the main window isn't there yet,
2899 // otherwise the hints don't work.
2900 width = gui_get_base_width();
2901 height = gui_get_base_height();
2902 # ifdef FEAT_MENU
2903 height += tabline_height() * gui.char_height;
2904 # endif
2905 width += get_menu_tool_width();
2906 height += get_menu_tool_height();
2907
2908 // GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
2909 // their actual widget size. When we set our size ourselves (e.g.,
2910 // 'set columns=' or init. -geom) we briefly set the min. to the size
2911 // we wish to be instead of the legitimate minimum so that we actually
2912 // resize correctly.
2913 if (force_width && force_height)
2914 {
2915 min_width = force_width;
2916 min_height = force_height;
2917 }
2918 else
2919 {
2920 min_width = width + MIN_COLUMNS * gui.char_width;
2921 min_height = height + MIN_LINES * gui.char_height;
2922 }
2923
2924 // Avoid an expose event when the size didn't change.
2925 if (width != old_width
2926 || height != old_height
2927 || min_width != old_min_width
2928 || min_height != old_min_height
2929 || gui.char_width != old_char_width
2930 || gui.char_height != old_char_height)
2931 {
2932 GdkGeometry geometry;
2933 GdkWindowHints geometry_mask;
2934
2935 geometry.width_inc = gui.char_width;
2936 geometry.height_inc = gui.char_height;
2937 geometry.base_width = width;
2938 geometry.base_height = height;
2939 geometry.min_width = min_width;
2940 geometry.min_height = min_height;
2941 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC
2942 |GDK_HINT_MIN_SIZE;
2943 // Using gui.formwin as geometry widget doesn't work as expected
2944 // with GTK+ 2 -- dunno why. Presumably all the resizing hacks
2945 // in Vim confuse GTK+.
2946 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin,
2947 &geometry, geometry_mask);
2948 old_width = width;
2949 old_height = height;
2950 old_min_width = min_width;
2951 old_min_height = min_height;
2952 old_char_width = gui.char_width;
2953 old_char_height = gui.char_height;
2954 }
2955 }
2956
2957 #if defined(FEAT_GUI_DARKTHEME) || defined(PROTO)
2958 void
gui_mch_set_dark_theme(int dark)2959 gui_mch_set_dark_theme(int dark)
2960 {
2961 # if GTK_CHECK_VERSION(3,0,0)
2962 GtkSettings *gtk_settings;
2963
2964 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
2965 g_object_set(gtk_settings, "gtk-application-prefer-dark-theme", (gboolean)dark, NULL);
2966 # endif
2967 }
2968 #endif // FEAT_GUI_DARKTHEME
2969
2970 #ifdef FEAT_TOOLBAR
2971
2972 /*
2973 * This extra effort wouldn't be necessary if we only used stock icons in the
2974 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
2975 * shouldn't be treated differently, thus we do need this.
2976 */
2977 static void
icon_size_changed_foreach(GtkWidget * widget,gpointer user_data)2978 icon_size_changed_foreach(GtkWidget *widget, gpointer user_data)
2979 {
2980 if (GTK_IS_IMAGE(widget))
2981 {
2982 GtkImage *image = (GtkImage *)widget;
2983
2984 # if GTK_CHECK_VERSION(3,10,0)
2985 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_NAME)
2986 {
2987 const GtkIconSize icon_size = GPOINTER_TO_INT(user_data);
2988 const gchar *icon_name;
2989
2990 gtk_image_get_icon_name(image, &icon_name, NULL);
2991 image = (GtkImage *)gtk_image_new_from_icon_name(
2992 icon_name, icon_size);
2993 }
2994 # else
2995 // User-defined icons are stored in a GtkIconSet
2996 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET)
2997 {
2998 GtkIconSet *icon_set;
2999 GtkIconSize icon_size;
3000
3001 gtk_image_get_icon_set(image, &icon_set, &icon_size);
3002 icon_size = (GtkIconSize)(long)user_data;
3003
3004 gtk_icon_set_ref(icon_set);
3005 gtk_image_set_from_icon_set(image, icon_set, icon_size);
3006 gtk_icon_set_unref(icon_set);
3007 }
3008 # endif
3009 }
3010 else if (GTK_IS_CONTAINER(widget))
3011 {
3012 gtk_container_foreach((GtkContainer *)widget,
3013 &icon_size_changed_foreach,
3014 user_data);
3015 }
3016 }
3017
3018 static void
set_toolbar_style(GtkToolbar * toolbar)3019 set_toolbar_style(GtkToolbar *toolbar)
3020 {
3021 GtkToolbarStyle style;
3022 GtkIconSize size;
3023 GtkIconSize oldsize;
3024
3025 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3026 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3027 style = GTK_TOOLBAR_BOTH_HORIZ;
3028 else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))
3029 == (TOOLBAR_TEXT | TOOLBAR_ICONS))
3030 style = GTK_TOOLBAR_BOTH;
3031 else if (toolbar_flags & TOOLBAR_TEXT)
3032 style = GTK_TOOLBAR_TEXT;
3033 else
3034 style = GTK_TOOLBAR_ICONS;
3035
3036 gtk_toolbar_set_style(toolbar, style);
3037 # if !GTK_CHECK_VERSION(3,0,0)
3038 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0);
3039 # endif
3040
3041 switch (tbis_flags)
3042 {
3043 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break;
3044 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break;
3045 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break;
3046 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
3047 case TBIS_HUGE: size = GTK_ICON_SIZE_DND; break;
3048 case TBIS_GIANT: size = GTK_ICON_SIZE_DIALOG; break;
3049 default: size = GTK_ICON_SIZE_INVALID; break;
3050 }
3051 oldsize = gtk_toolbar_get_icon_size(toolbar);
3052
3053 if (size == GTK_ICON_SIZE_INVALID)
3054 {
3055 // Let global user preferences decide the icon size.
3056 gtk_toolbar_unset_icon_size(toolbar);
3057 size = gtk_toolbar_get_icon_size(toolbar);
3058 }
3059 if (size != oldsize)
3060 {
3061 gtk_container_foreach(GTK_CONTAINER(toolbar),
3062 &icon_size_changed_foreach,
3063 GINT_TO_POINTER((int)size));
3064 }
3065 gtk_toolbar_set_icon_size(toolbar, size);
3066 }
3067
3068 #endif // FEAT_TOOLBAR
3069
3070 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3071 static int ignore_tabline_evt = FALSE;
3072 static GtkWidget *tabline_menu;
3073 # if !GTK_CHECK_VERSION(3,0,0)
3074 static GtkTooltips *tabline_tooltip;
3075 # endif
3076 static int clicked_page; // page clicked in tab line
3077
3078 /*
3079 * Handle selecting an item in the tab line popup menu.
3080 */
3081 static void
tabline_menu_handler(GtkMenuItem * item UNUSED,gpointer user_data)3082 tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data)
3083 {
3084 // Add the string cmd into input buffer
3085 send_tabline_menu_event(clicked_page, (int)(long)user_data);
3086 }
3087
3088 static void
add_tabline_menu_item(GtkWidget * menu,char_u * text,int resp)3089 add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp)
3090 {
3091 GtkWidget *item;
3092 char_u *utf_text;
3093
3094 utf_text = CONVERT_TO_UTF8(text);
3095 item = gtk_menu_item_new_with_label((const char *)utf_text);
3096 gtk_widget_show(item);
3097 CONVERT_TO_UTF8_FREE(utf_text);
3098
3099 gtk_container_add(GTK_CONTAINER(menu), item);
3100 g_signal_connect(G_OBJECT(item), "activate",
3101 G_CALLBACK(tabline_menu_handler),
3102 GINT_TO_POINTER(resp));
3103 }
3104
3105 /*
3106 * Create a menu for the tab line.
3107 */
3108 static GtkWidget *
create_tabline_menu(void)3109 create_tabline_menu(void)
3110 {
3111 GtkWidget *menu;
3112
3113 menu = gtk_menu_new();
3114 add_tabline_menu_item(menu, (char_u *)_("Close tab"), TABLINE_MENU_CLOSE);
3115 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW);
3116 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN);
3117
3118 return menu;
3119 }
3120
3121 static gboolean
on_tabline_menu(GtkWidget * widget,GdkEvent * event)3122 on_tabline_menu(GtkWidget *widget, GdkEvent *event)
3123 {
3124 // Was this button press event ?
3125 if (event->type == GDK_BUTTON_PRESS)
3126 {
3127 GdkEventButton *bevent = (GdkEventButton *)event;
3128 int x = bevent->x;
3129 int y = bevent->y;
3130 GtkWidget *tabwidget;
3131 GdkWindow *tabwin;
3132
3133 // When ignoring events return TRUE so that the selected page doesn't
3134 // change.
3135 if (hold_gui_events
3136 # ifdef FEAT_CMDWIN
3137 || cmdwin_type != 0
3138 # endif
3139 )
3140 return TRUE;
3141
3142 tabwin = gui_gtk_window_at_position(gui.mainwin, &x, &y);
3143
3144 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget);
3145 clicked_page = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(tabwidget),
3146 "tab_num"));
3147
3148 // If the event was generated for 3rd button popup the menu.
3149 if (bevent->button == 3)
3150 {
3151 # if GTK_CHECK_VERSION(3,22,2)
3152 gtk_menu_popup_at_pointer(GTK_MENU(widget), event);
3153 # else
3154 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
3155 bevent->button, bevent->time);
3156 # endif
3157 // We handled the event.
3158 return TRUE;
3159 }
3160 else if (bevent->button == 1)
3161 {
3162 if (clicked_page == 0)
3163 {
3164 // Click after all tabs moves to next tab page. When "x" is
3165 // small guess it's the left button.
3166 send_tabline_event(x < 50 ? -1 : 0);
3167 }
3168 }
3169 }
3170
3171 // We didn't handle the event.
3172 return FALSE;
3173 }
3174
3175 /*
3176 * Handle selecting one of the tabs.
3177 */
3178 static void
on_select_tab(GtkNotebook * notebook UNUSED,gpointer * page UNUSED,gint idx,gpointer data UNUSED)3179 on_select_tab(
3180 GtkNotebook *notebook UNUSED,
3181 gpointer *page UNUSED,
3182 gint idx,
3183 gpointer data UNUSED)
3184 {
3185 if (!ignore_tabline_evt)
3186 send_tabline_event(idx + 1);
3187 }
3188
3189 # if GTK_CHECK_VERSION(2,10,0)
3190 /*
3191 * Handle reordering the tabs (using D&D).
3192 */
3193 static void
on_tab_reordered(GtkNotebook * notebook UNUSED,gpointer * page UNUSED,gint idx,gpointer data UNUSED)3194 on_tab_reordered(
3195 GtkNotebook *notebook UNUSED,
3196 gpointer *page UNUSED,
3197 gint idx,
3198 gpointer data UNUSED)
3199 {
3200 if (!ignore_tabline_evt)
3201 {
3202 if ((tabpage_index(curtab) - 1) < idx)
3203 tabpage_move(idx + 1);
3204 else
3205 tabpage_move(idx);
3206 }
3207 }
3208 # endif
3209
3210 /*
3211 * Show or hide the tabline.
3212 */
3213 void
gui_mch_show_tabline(int showit)3214 gui_mch_show_tabline(int showit)
3215 {
3216 if (gui.tabline == NULL)
3217 return;
3218
3219 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)))
3220 {
3221 // Note: this may cause a resize event
3222 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit);
3223 update_window_manager_hints(0, 0);
3224 if (showit)
3225 gtk_widget_set_can_focus(GTK_WIDGET(gui.tabline), FALSE);
3226 }
3227
3228 gui_mch_update();
3229 }
3230
3231 /*
3232 * Return TRUE when tabline is displayed.
3233 */
3234 int
gui_mch_showing_tabline(void)3235 gui_mch_showing_tabline(void)
3236 {
3237 return gui.tabline != NULL
3238 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline));
3239 }
3240
3241 /*
3242 * Update the labels of the tabline.
3243 */
3244 void
gui_mch_update_tabline(void)3245 gui_mch_update_tabline(void)
3246 {
3247 GtkWidget *page;
3248 GtkWidget *event_box;
3249 GtkWidget *label;
3250 tabpage_T *tp;
3251 int nr = 0;
3252 int tab_num;
3253 int curtabidx = 0;
3254 char_u *labeltext;
3255
3256 if (gui.tabline == NULL)
3257 return;
3258
3259 ignore_tabline_evt = TRUE;
3260
3261 // Add a label for each tab page. They all contain the same text area.
3262 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3263 {
3264 if (tp == curtab)
3265 curtabidx = nr;
3266
3267 tab_num = nr + 1;
3268
3269 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr);
3270 if (page == NULL)
3271 {
3272 // Add notebook page
3273 # if GTK_CHECK_VERSION(3,2,0)
3274 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3275 gtk_box_set_homogeneous(GTK_BOX(page), FALSE);
3276 # else
3277 page = gtk_vbox_new(FALSE, 0);
3278 # endif
3279 gtk_widget_show(page);
3280 event_box = gtk_event_box_new();
3281 gtk_widget_show(event_box);
3282 label = gtk_label_new("-Empty-");
3283 # if !GTK_CHECK_VERSION(3,14,0)
3284 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
3285 # endif
3286 gtk_container_add(GTK_CONTAINER(event_box), label);
3287 gtk_widget_show(label);
3288 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline),
3289 page,
3290 event_box,
3291 nr++);
3292 # if GTK_CHECK_VERSION(2,10,0)
3293 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(gui.tabline),
3294 page,
3295 TRUE);
3296 # endif
3297 }
3298
3299 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page);
3300 g_object_set_data(G_OBJECT(event_box), "tab_num",
3301 GINT_TO_POINTER(tab_num));
3302 label = gtk_bin_get_child(GTK_BIN(event_box));
3303 get_tabline_label(tp, FALSE);
3304 labeltext = CONVERT_TO_UTF8(NameBuff);
3305 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext);
3306 CONVERT_TO_UTF8_FREE(labeltext);
3307
3308 get_tabline_label(tp, TRUE);
3309 labeltext = CONVERT_TO_UTF8(NameBuff);
3310 # if GTK_CHECK_VERSION(3,0,0)
3311 gtk_widget_set_tooltip_text(event_box, (const gchar *)labeltext);
3312 # else
3313 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box,
3314 (const char *)labeltext, NULL);
3315 # endif
3316 CONVERT_TO_UTF8_FREE(labeltext);
3317 }
3318
3319 // Remove any old labels.
3320 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL)
3321 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr);
3322
3323 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
3324 gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
3325
3326 // Make sure everything is in place before drawing text.
3327 gui_mch_update();
3328
3329 ignore_tabline_evt = FALSE;
3330 }
3331
3332 /*
3333 * Set the current tab to "nr". First tab is 1.
3334 */
3335 void
gui_mch_set_curtab(int nr)3336 gui_mch_set_curtab(int nr)
3337 {
3338 if (gui.tabline == NULL)
3339 return;
3340
3341 ignore_tabline_evt = TRUE;
3342 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
3343 gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
3344 ignore_tabline_evt = FALSE;
3345 }
3346
3347 #endif // FEAT_GUI_TABLINE
3348
3349 /*
3350 * Add selection targets for PRIMARY and CLIPBOARD selections.
3351 */
3352 void
gui_gtk_set_selection_targets(void)3353 gui_gtk_set_selection_targets(void)
3354 {
3355 int i, j = 0;
3356 int n_targets = N_SELECTION_TARGETS;
3357 GtkTargetEntry targets[N_SELECTION_TARGETS];
3358
3359 for (i = 0; i < (int)N_SELECTION_TARGETS; ++i)
3360 {
3361 // OpenOffice tries to use TARGET_HTML and fails when we don't
3362 // return something, instead of trying another target. Therefore only
3363 // offer TARGET_HTML when it works.
3364 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3365 n_targets--;
3366 else
3367 targets[j++] = selection_targets[i];
3368 }
3369
3370 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)GDK_SELECTION_PRIMARY);
3371 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)clip_plus.gtk_sel_atom);
3372 gtk_selection_add_targets(gui.drawarea,
3373 (GdkAtom)GDK_SELECTION_PRIMARY,
3374 targets, n_targets);
3375 gtk_selection_add_targets(gui.drawarea,
3376 (GdkAtom)clip_plus.gtk_sel_atom,
3377 targets, n_targets);
3378 }
3379
3380 /*
3381 * Set up for receiving DND items.
3382 */
3383 void
gui_gtk_set_dnd_targets(void)3384 gui_gtk_set_dnd_targets(void)
3385 {
3386 int i, j = 0;
3387 int n_targets = N_DND_TARGETS;
3388 GtkTargetEntry targets[N_DND_TARGETS];
3389
3390 for (i = 0; i < (int)N_DND_TARGETS; ++i)
3391 {
3392 if (!clip_html && dnd_targets[i].info == TARGET_HTML)
3393 n_targets--;
3394 else
3395 targets[j++] = dnd_targets[i];
3396 }
3397
3398 gtk_drag_dest_unset(gui.drawarea);
3399 gtk_drag_dest_set(gui.drawarea,
3400 GTK_DEST_DEFAULT_ALL,
3401 targets, n_targets,
3402 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3403 }
3404
3405 /*
3406 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3407 * Returns OK for success, FAIL when the GUI can't be started.
3408 */
3409 int
gui_mch_init(void)3410 gui_mch_init(void)
3411 {
3412 GtkWidget *vbox;
3413
3414 #ifdef FEAT_GUI_GNOME
3415 // Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3416 // exits on failure, but that's a non-issue because we already called
3417 // gtk_init_check() in gui_mch_init_check().
3418 if (using_gnome)
3419 {
3420 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT,
3421 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL);
3422 # if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
3423 {
3424 char *p = setlocale(LC_NUMERIC, NULL);
3425
3426 // Make sure strtod() uses a decimal point, not a comma. Gnome
3427 // init may change it.
3428 if (p == NULL || strcmp(p, "C") != 0)
3429 setlocale(LC_NUMERIC, "C");
3430 }
3431 # endif
3432 }
3433 #endif
3434 VIM_CLEAR(gui_argv);
3435
3436 #if GLIB_CHECK_VERSION(2,1,3)
3437 // Set the human-readable application name
3438 g_set_application_name("Vim");
3439 #endif
3440 /*
3441 * Force UTF-8 output no matter what the value of 'encoding' is.
3442 * did_set_string_option() in option.c prohibits changing 'termencoding'
3443 * to something else than UTF-8 if the GUI is in use.
3444 */
3445 set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0);
3446
3447 #ifdef FEAT_TOOLBAR
3448 gui_gtk_register_stock_icons();
3449 #endif
3450 // FIXME: Need to install the classic icons and a gtkrc.classic file.
3451 // The hard part is deciding install locations and the Makefile magic.
3452 #if !GTK_CHECK_VERSION(3,0,0)
3453 # if 0
3454 gtk_rc_parse("gtkrc");
3455 # endif
3456 #endif
3457
3458 // Initialize values
3459 gui.border_width = 2;
3460 gui.scrollbar_width = SB_DEFAULT_WIDTH;
3461 gui.scrollbar_height = SB_DEFAULT_WIDTH;
3462 #if GTK_CHECK_VERSION(3,0,0)
3463 gui.fgcolor = g_new(GdkRGBA, 1);
3464 gui.bgcolor = g_new(GdkRGBA, 1);
3465 gui.spcolor = g_new(GdkRGBA, 1);
3466 #else
3467 // LINTED: avoid warning: conversion to 'unsigned long'
3468 gui.fgcolor = g_new0(GdkColor, 1);
3469 // LINTED: avoid warning: conversion to 'unsigned long'
3470 gui.bgcolor = g_new0(GdkColor, 1);
3471 // LINTED: avoid warning: conversion to 'unsigned long'
3472 gui.spcolor = g_new0(GdkColor, 1);
3473 #endif
3474
3475 // Initialise atoms
3476 html_atom = gdk_atom_intern("text/html", FALSE);
3477 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
3478
3479 // Set default foreground and background colors.
3480 gui.norm_pixel = gui.def_norm_pixel;
3481 gui.back_pixel = gui.def_back_pixel;
3482
3483 if (gtk_socket_id != 0)
3484 {
3485 GtkWidget *plug;
3486
3487 // Use GtkSocket from another app.
3488 plug = gtk_plug_new_for_display(gdk_display_get_default(),
3489 gtk_socket_id);
3490 if (plug != NULL && gtk_plug_get_socket_window(GTK_PLUG(plug)) != NULL)
3491 {
3492 gui.mainwin = plug;
3493 }
3494 else
3495 {
3496 g_warning("Connection to GTK+ socket (ID %u) failed",
3497 (unsigned int)gtk_socket_id);
3498 // Pretend we never wanted it if it failed (get own window)
3499 gtk_socket_id = 0;
3500 }
3501 }
3502
3503 if (gtk_socket_id == 0)
3504 {
3505 #ifdef FEAT_GUI_GNOME
3506 if (using_gnome)
3507 {
3508 gui.mainwin = gnome_app_new("Vim", NULL);
3509 # ifdef USE_XSMP
3510 // Use the GNOME save-yourself functionality now.
3511 xsmp_close();
3512 # endif
3513 }
3514 else
3515 #endif
3516 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3517 }
3518
3519 gtk_widget_set_name(gui.mainwin, "vim-main-window");
3520
3521 // Create the PangoContext used for drawing all text.
3522 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
3523 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
3524
3525 gtk_container_set_border_width(GTK_CONTAINER(gui.mainwin), 0);
3526 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK);
3527
3528 g_signal_connect(G_OBJECT(gui.mainwin), "delete-event",
3529 G_CALLBACK(&delete_event_cb), NULL);
3530
3531 g_signal_connect(G_OBJECT(gui.mainwin), "realize",
3532 G_CALLBACK(&mainwin_realize), NULL);
3533
3534 g_signal_connect(G_OBJECT(gui.mainwin), "screen-changed",
3535 G_CALLBACK(&mainwin_screen_changed_cb), NULL);
3536
3537 gui.accel_group = gtk_accel_group_new();
3538 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group);
3539
3540 // A vertical box holds the menubar, toolbar and main text window.
3541 #if GTK_CHECK_VERSION(3,2,0)
3542 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3543 gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE);
3544 #else
3545 vbox = gtk_vbox_new(FALSE, 0);
3546 #endif
3547
3548 #ifdef FEAT_GUI_GNOME
3549 if (using_gnome)
3550 {
3551 # if defined(FEAT_MENU)
3552 // automagically restore menubar/toolbar placement
3553 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE);
3554 # endif
3555 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox);
3556 }
3557 else
3558 #endif
3559 {
3560 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox);
3561 gtk_widget_show(vbox);
3562 }
3563
3564 #ifdef FEAT_MENU
3565 /*
3566 * Create the menubar and handle
3567 */
3568 gui.menubar = gtk_menu_bar_new();
3569 gtk_widget_set_name(gui.menubar, "vim-menubar");
3570
3571 // Avoid that GTK takes <F10> away from us.
3572 {
3573 GtkSettings *gtk_settings;
3574
3575 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
3576 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
3577 }
3578
3579
3580 # ifdef FEAT_GUI_GNOME
3581 if (using_gnome)
3582 {
3583 BonoboDockItem *dockitem;
3584
3585 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar));
3586 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3587 GNOME_APP_MENUBAR_NAME);
3588 // We don't want the menu to float.
3589 bonobo_dock_item_set_behavior(dockitem,
3590 bonobo_dock_item_get_behavior(dockitem)
3591 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
3592 gui.menubar_h = GTK_WIDGET(dockitem);
3593 }
3594 else
3595 # endif // FEAT_GUI_GNOME
3596 {
3597 // Always show the menubar, otherwise <F10> doesn't work. It may be
3598 // disabled in gui_init() later.
3599 gtk_widget_show(gui.menubar);
3600 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0);
3601 }
3602 #endif // FEAT_MENU
3603
3604 #ifdef FEAT_TOOLBAR
3605 /*
3606 * Create the toolbar and handle
3607 */
3608 // some aesthetics on the toolbar
3609 # ifdef USE_GTK3
3610 // TODO: Add GTK+ 3 code here using GtkCssProvider if necessary.
3611 // N.B. Since the default value of GtkToolbar::button-relief is
3612 // GTK_RELIEF_NONE, there's no need to specify that, probably.
3613 # else
3614 gtk_rc_parse_string(
3615 "style \"vim-toolbar-style\" {\n"
3616 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
3617 "}\n"
3618 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
3619 # endif
3620 gui.toolbar = gtk_toolbar_new();
3621 gtk_widget_set_name(gui.toolbar, "vim-toolbar");
3622 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
3623
3624 # ifdef FEAT_GUI_GNOME
3625 if (using_gnome)
3626 {
3627 BonoboDockItem *dockitem;
3628
3629 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar));
3630 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3631 GNOME_APP_TOOLBAR_NAME);
3632 gui.toolbar_h = GTK_WIDGET(dockitem);
3633 // When the toolbar is floating it gets stuck. So long as that isn't
3634 // fixed let's disallow floating.
3635 bonobo_dock_item_set_behavior(dockitem,
3636 bonobo_dock_item_get_behavior(dockitem)
3637 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
3638 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0);
3639 }
3640 else
3641 # endif // FEAT_GUI_GNOME
3642 {
3643 if (vim_strchr(p_go, GO_TOOLBAR) != NULL
3644 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3645 gtk_widget_show(gui.toolbar);
3646 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0);
3647 }
3648 #endif // FEAT_TOOLBAR
3649
3650 #ifdef FEAT_GUI_TABLINE
3651 /*
3652 * Use a Notebook for the tab pages labels. The labels are hidden by
3653 * default.
3654 */
3655 gui.tabline = gtk_notebook_new();
3656 gtk_widget_show(gui.tabline);
3657 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0);
3658 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
3659 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE);
3660 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
3661 # if !GTK_CHECK_VERSION(3,0,0)
3662 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE);
3663 # endif
3664
3665 # if !GTK_CHECK_VERSION(3,0,0)
3666 tabline_tooltip = gtk_tooltips_new();
3667 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip));
3668 # endif
3669
3670 {
3671 GtkWidget *page, *label, *event_box;
3672
3673 // Add the first tab.
3674 # if GTK_CHECK_VERSION(3,2,0)
3675 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3676 gtk_box_set_homogeneous(GTK_BOX(page), FALSE);
3677 # else
3678 page = gtk_vbox_new(FALSE, 0);
3679 # endif
3680 gtk_widget_show(page);
3681 gtk_container_add(GTK_CONTAINER(gui.tabline), page);
3682 label = gtk_label_new("-Empty-");
3683 gtk_widget_show(label);
3684 event_box = gtk_event_box_new();
3685 gtk_widget_show(event_box);
3686 g_object_set_data(G_OBJECT(event_box), "tab_num", GINT_TO_POINTER(1L));
3687 # if !GTK_CHECK_VERSION(3,14,0)
3688 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
3689 # endif
3690 gtk_container_add(GTK_CONTAINER(event_box), label);
3691 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box);
3692 # if GTK_CHECK_VERSION(2,10,0)
3693 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(gui.tabline), page, TRUE);
3694 # endif
3695 }
3696
3697 g_signal_connect(G_OBJECT(gui.tabline), "switch-page",
3698 G_CALLBACK(on_select_tab), NULL);
3699 # if GTK_CHECK_VERSION(2,10,0)
3700 g_signal_connect(G_OBJECT(gui.tabline), "page-reordered",
3701 G_CALLBACK(on_tab_reordered), NULL);
3702 # endif
3703
3704 // Create a popup menu for the tab line and connect it.
3705 tabline_menu = create_tabline_menu();
3706 g_signal_connect_swapped(G_OBJECT(gui.tabline), "button-press-event",
3707 G_CALLBACK(on_tabline_menu), G_OBJECT(tabline_menu));
3708 #endif // FEAT_GUI_TABLINE
3709
3710 gui.formwin = gui_gtk_form_new();
3711 gtk_container_set_border_width(GTK_CONTAINER(gui.formwin), 0);
3712 #if !GTK_CHECK_VERSION(3,0,0)
3713 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK);
3714 #endif
3715 #if GTK_CHECK_VERSION(3,22,2)
3716 gtk_widget_set_name(gui.formwin, "vim-gtk-form");
3717 #endif
3718
3719 gui.drawarea = gtk_drawing_area_new();
3720 #if GTK_CHECK_VERSION(3,0,0)
3721 gui.surface = NULL;
3722 #endif
3723
3724 // Determine which events we will filter.
3725 gtk_widget_set_events(gui.drawarea,
3726 GDK_EXPOSURE_MASK |
3727 GDK_ENTER_NOTIFY_MASK |
3728 GDK_LEAVE_NOTIFY_MASK |
3729 GDK_BUTTON_PRESS_MASK |
3730 GDK_BUTTON_RELEASE_MASK |
3731 GDK_SCROLL_MASK |
3732 GDK_KEY_PRESS_MASK |
3733 GDK_KEY_RELEASE_MASK |
3734 GDK_POINTER_MOTION_MASK |
3735 GDK_POINTER_MOTION_HINT_MASK);
3736
3737 gtk_widget_show(gui.drawarea);
3738 gui_gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0);
3739 gtk_widget_show(gui.formwin);
3740 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0);
3741
3742 // For GtkSockets, key-presses must go to the focus widget (drawarea)
3743 // and not the window.
3744 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
3745 : G_OBJECT(gui.drawarea),
3746 "key-press-event",
3747 G_CALLBACK(key_press_event), NULL);
3748 #if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0)
3749 // Also forward key release events for the benefit of GTK+ 2 input
3750 // modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point.
3751 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
3752 : G_OBJECT(gui.drawarea),
3753 "key-release-event",
3754 G_CALLBACK(&key_release_event), NULL);
3755 #endif
3756 g_signal_connect(G_OBJECT(gui.drawarea), "realize",
3757 G_CALLBACK(drawarea_realize_cb), NULL);
3758 g_signal_connect(G_OBJECT(gui.drawarea), "unrealize",
3759 G_CALLBACK(drawarea_unrealize_cb), NULL);
3760 #if GTK_CHECK_VERSION(3,0,0)
3761 g_signal_connect(G_OBJECT(gui.drawarea), "configure-event",
3762 G_CALLBACK(drawarea_configure_event_cb), NULL);
3763 #endif
3764 #if GTK_CHECK_VERSION(3,22,2)
3765 g_signal_connect_after(G_OBJECT(gui.drawarea), "style-updated",
3766 G_CALLBACK(&drawarea_style_updated_cb), NULL);
3767 #else
3768 g_signal_connect_after(G_OBJECT(gui.drawarea), "style-set",
3769 G_CALLBACK(&drawarea_style_set_cb), NULL);
3770 #endif
3771
3772 #if !GTK_CHECK_VERSION(3,0,0)
3773 gui.visibility = GDK_VISIBILITY_UNOBSCURED;
3774 #endif
3775
3776 #if !defined(USE_GNOME_SESSION)
3777 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE);
3778 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE);
3779 #endif
3780
3781 if (gtk_socket_id != 0)
3782 // make sure keyboard input can go to the drawarea
3783 gtk_widget_set_can_focus(gui.drawarea, TRUE);
3784
3785 /*
3786 * Set clipboard specific atoms
3787 */
3788 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE);
3789 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE);
3790 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY;
3791 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE);
3792
3793 /*
3794 * Start out by adding the configured border width into the border offset.
3795 */
3796 gui.border_offset = gui.border_width;
3797
3798 #if GTK_CHECK_VERSION(3,0,0)
3799 g_signal_connect(G_OBJECT(gui.drawarea), "draw",
3800 G_CALLBACK(draw_event), NULL);
3801 #else
3802 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event",
3803 GTK_SIGNAL_FUNC(visibility_event), NULL);
3804 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event",
3805 GTK_SIGNAL_FUNC(expose_event), NULL);
3806 #endif
3807
3808 /*
3809 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3810 * Only needed for some window managers.
3811 */
3812 if (vim_strchr(p_go, GO_POINTER) != NULL)
3813 {
3814 g_signal_connect(G_OBJECT(gui.drawarea), "leave-notify-event",
3815 G_CALLBACK(leave_notify_event), NULL);
3816 g_signal_connect(G_OBJECT(gui.drawarea), "enter-notify-event",
3817 G_CALLBACK(enter_notify_event), NULL);
3818 }
3819
3820 // Real windows can get focus ... GtkPlug, being a mere container can't,
3821 // only its widgets. Arguably, this could be common code and we not use
3822 // the window focus at all, but let's be safe.
3823 if (gtk_socket_id == 0)
3824 {
3825 g_signal_connect(G_OBJECT(gui.mainwin), "focus-out-event",
3826 G_CALLBACK(focus_out_event), NULL);
3827 g_signal_connect(G_OBJECT(gui.mainwin), "focus-in-event",
3828 G_CALLBACK(focus_in_event), NULL);
3829 }
3830 else
3831 {
3832 g_signal_connect(G_OBJECT(gui.drawarea), "focus-out-event",
3833 G_CALLBACK(focus_out_event), NULL);
3834 g_signal_connect(G_OBJECT(gui.drawarea), "focus-in-event",
3835 G_CALLBACK(focus_in_event), NULL);
3836 #ifdef FEAT_GUI_TABLINE
3837 g_signal_connect(G_OBJECT(gui.tabline), "focus-out-event",
3838 G_CALLBACK(focus_out_event), NULL);
3839 g_signal_connect(G_OBJECT(gui.tabline), "focus-in-event",
3840 G_CALLBACK(focus_in_event), NULL);
3841 #endif // FEAT_GUI_TABLINE
3842 }
3843
3844 g_signal_connect(G_OBJECT(gui.drawarea), "motion-notify-event",
3845 G_CALLBACK(motion_notify_event), NULL);
3846 g_signal_connect(G_OBJECT(gui.drawarea), "button-press-event",
3847 G_CALLBACK(button_press_event), NULL);
3848 g_signal_connect(G_OBJECT(gui.drawarea), "button-release-event",
3849 G_CALLBACK(button_release_event), NULL);
3850 g_signal_connect(G_OBJECT(gui.drawarea), "scroll-event",
3851 G_CALLBACK(&scroll_event), NULL);
3852
3853 /*
3854 * Add selection handler functions.
3855 */
3856 g_signal_connect(G_OBJECT(gui.drawarea), "selection-clear-event",
3857 G_CALLBACK(selection_clear_event), NULL);
3858 g_signal_connect(G_OBJECT(gui.drawarea), "selection-received",
3859 G_CALLBACK(selection_received_cb), NULL);
3860
3861 gui_gtk_set_selection_targets();
3862
3863 g_signal_connect(G_OBJECT(gui.drawarea), "selection-get",
3864 G_CALLBACK(selection_get_cb), NULL);
3865
3866 // Pretend we don't have input focus, we will get an event if we do.
3867 gui.in_focus = FALSE;
3868
3869 // Handle changes to the "Xft/DPI" setting.
3870 {
3871 GtkSettings *gtk_settings =
3872 gtk_settings_get_for_screen(gdk_screen_get_default());
3873
3874 g_signal_connect(gtk_settings, "notify::gtk-xft-dpi",
3875 G_CALLBACK(gtk_settings_xft_dpi_changed_cb), NULL);
3876 }
3877
3878 return OK;
3879 }
3880
3881 #if defined(USE_GNOME_SESSION) || defined(PROTO)
3882 /*
3883 * This is called from gui_start() after a fork() has been done.
3884 * We have to tell the session manager our new PID.
3885 */
3886 void
gui_mch_forked(void)3887 gui_mch_forked(void)
3888 {
3889 if (using_gnome)
3890 {
3891 GnomeClient *client;
3892
3893 client = gnome_master_client();
3894
3895 if (client != NULL)
3896 gnome_client_set_process_id(client, getpid());
3897 }
3898 }
3899 #endif // USE_GNOME_SESSION
3900
3901 #if GTK_CHECK_VERSION(3,0,0)
3902 static GdkRGBA
color_to_rgba(guicolor_T color)3903 color_to_rgba(guicolor_T color)
3904 {
3905 GdkRGBA rgba;
3906 rgba.red = ((color & 0xff0000) >> 16) / 255.0;
3907 rgba.green = ((color & 0xff00) >> 8) / 255.0;
3908 rgba.blue = ((color & 0xff)) / 255.0;
3909 rgba.alpha = 1.0;
3910 return rgba;
3911 }
3912
3913 static void
set_cairo_source_rgba_from_color(cairo_t * cr,guicolor_T color)3914 set_cairo_source_rgba_from_color(cairo_t *cr, guicolor_T color)
3915 {
3916 const GdkRGBA rgba = color_to_rgba(color);
3917 cairo_set_source_rgba(cr, rgba.red, rgba.green, rgba.blue, rgba.alpha);
3918 }
3919 #endif // GTK_CHECK_VERSION(3,0,0)
3920
3921 /*
3922 * Called when the foreground or background color has been changed.
3923 * This used to change the graphics contexts directly but we are
3924 * currently manipulating them where desired.
3925 */
3926 void
gui_mch_new_colors(void)3927 gui_mch_new_colors(void)
3928 {
3929 if (gui.drawarea != NULL
3930 #if GTK_CHECK_VERSION(3,22,2)
3931 && gui.formwin != NULL
3932 #endif
3933 && gtk_widget_get_window(gui.drawarea) != NULL)
3934 {
3935 #if !GTK_CHECK_VERSION(3,22,2)
3936 GdkWindow * const da_win = gtk_widget_get_window(gui.drawarea);
3937 #endif
3938 #if GTK_CHECK_VERSION(3,22,2)
3939 GtkStyleContext * const context =
3940 gtk_widget_get_style_context(gui.formwin);
3941 GtkCssProvider * const provider = gtk_css_provider_new();
3942 gchar * const css = g_strdup_printf(
3943 "widget#vim-gtk-form {\n"
3944 " background-color: #%.2lx%.2lx%.2lx;\n"
3945 "}\n",
3946 (gui.back_pixel >> 16) & 0xff,
3947 (gui.back_pixel >> 8) & 0xff,
3948 gui.back_pixel & 0xff);
3949
3950 gtk_css_provider_load_from_data(provider, css, -1, NULL);
3951 gtk_style_context_add_provider(context,
3952 GTK_STYLE_PROVIDER(provider), G_MAXUINT);
3953
3954 g_free(css);
3955 g_object_unref(provider);
3956 #elif GTK_CHECK_VERSION(3,4,0) // !GTK_CHECK_VERSION(3,22,2)
3957 GdkRGBA rgba;
3958
3959 rgba = color_to_rgba(gui.back_pixel);
3960 {
3961 cairo_pattern_t * const pat = cairo_pattern_create_rgba(
3962 rgba.red, rgba.green, rgba.blue, rgba.alpha);
3963 if (pat != NULL)
3964 {
3965 gdk_window_set_background_pattern(da_win, pat);
3966 cairo_pattern_destroy(pat);
3967 }
3968 else
3969 gdk_window_set_background_rgba(da_win, &rgba);
3970 }
3971 #else // !GTK_CHECK_VERSION(3,4,0)
3972 GdkColor color = { 0, 0, 0, 0 };
3973
3974 color.pixel = gui.back_pixel;
3975 gdk_window_set_background(da_win, &color);
3976 #endif // !GTK_CHECK_VERSION(3,22,2)
3977 }
3978 }
3979
3980 /*
3981 * This signal informs us about the need to rearrange our sub-widgets.
3982 */
3983 static gint
form_configure_event(GtkWidget * widget UNUSED,GdkEventConfigure * event,gpointer data UNUSED)3984 form_configure_event(GtkWidget *widget UNUSED,
3985 GdkEventConfigure *event,
3986 gpointer data UNUSED)
3987 {
3988 int usable_height = event->height;
3989
3990 #if GTK_CHECK_VERSION(3,22,2) && !GTK_CHECK_VERSION(3,22,4)
3991 // As of 3.22.2, GdkWindows have started distributing configure events to
3992 // their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0).
3993 //
3994 // As can be seen from the implementation of move_native_children() and
3995 // configure_native_child() in gdkwindow.c, those functions actually
3996 // propagate configure events to every child, failing to distinguish
3997 // "native" one from non-native one.
3998 //
3999 // Naturally, configure events propagated to here like that are fallacious
4000 // and, as a matter of fact, they trigger a geometric collapse of
4001 // gui.formwin.
4002 //
4003 // To filter out such fallacious events, check if the given event is the
4004 // one that was sent out to the right place. Ignore it if not.
4005 //
4006 // Follow-up
4007 // After a few weeks later, the GdkWindow change mentioned above was
4008 // reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155).
4009 // The corresponding official release is 3.22.4.
4010 if (event->window != gtk_widget_get_window(gui.formwin))
4011 return TRUE;
4012 #endif
4013
4014 // When in a GtkPlug, we can't guarantee valid heights (as a round
4015 // no. of char-heights), so we have to manually sanitise them.
4016 // Widths seem to sort themselves out, don't ask me why.
4017 if (gtk_socket_id != 0)
4018 usable_height -= (gui.char_height - (gui.char_height/2)); // sic.
4019
4020 gui_gtk_form_freeze(GTK_FORM(gui.formwin));
4021 gui_resize_shell(event->width, usable_height);
4022 gui_gtk_form_thaw(GTK_FORM(gui.formwin));
4023
4024 return TRUE;
4025 }
4026
4027 /*
4028 * Function called when window already closed.
4029 * We can't do much more here than to trying to preserve what had been done,
4030 * since the window is already inevitably going away.
4031 */
4032 static void
mainwin_destroy_cb(GObject * object UNUSED,gpointer data UNUSED)4033 mainwin_destroy_cb(GObject *object UNUSED, gpointer data UNUSED)
4034 {
4035 // Don't write messages to the GUI anymore
4036 full_screen = FALSE;
4037
4038 gui.mainwin = NULL;
4039 gui.drawarea = NULL;
4040
4041 if (!exiting) // only do anything if the destroy was unexpected
4042 {
4043 vim_strncpy(IObuff,
4044 (char_u *)_("Vim: Main window unexpectedly destroyed\n"),
4045 IOSIZE - 1);
4046 preserve_exit();
4047 }
4048 #ifdef USE_GRESOURCE
4049 gui_gtk_unregister_resource();
4050 #endif
4051 }
4052
4053 void
gui_gtk_get_screen_geom_of_win(GtkWidget * wid,int point_x,int point_y,int * screen_x,int * screen_y,int * width,int * height)4054 gui_gtk_get_screen_geom_of_win(
4055 GtkWidget *wid,
4056 int point_x, // x position of window if not initialized
4057 int point_y, // y position of window if not initialized
4058 int *screen_x,
4059 int *screen_y,
4060 int *width,
4061 int *height)
4062 {
4063 GdkRectangle geometry;
4064 GdkWindow *win = gtk_widget_get_window(wid);
4065 #if GTK_CHECK_VERSION(3,22,0)
4066 GdkDisplay *dpy;
4067 GdkMonitor *monitor;
4068
4069 if (wid != NULL && gtk_widget_get_realized(wid))
4070 dpy = gtk_widget_get_display(wid);
4071 else
4072 dpy = gdk_display_get_default();
4073 if (win != NULL)
4074 monitor = gdk_display_get_monitor_at_window(dpy, win);
4075 else
4076 monitor = gdk_display_get_monitor_at_point(dpy, point_x, point_y);
4077 gdk_monitor_get_geometry(monitor, &geometry);
4078 #else
4079 GdkScreen* screen;
4080 int monitor;
4081
4082 if (wid != NULL && gtk_widget_has_screen(wid))
4083 screen = gtk_widget_get_screen(wid);
4084 else
4085 screen = gdk_screen_get_default();
4086 if (win != NULL)
4087 monitor = gdk_screen_get_monitor_at_window(screen, win);
4088 else
4089 monitor = gdk_screen_get_monitor_at_point(screen, point_x, point_y);
4090 gdk_screen_get_monitor_geometry(screen, monitor, &geometry);
4091 #endif
4092 *screen_x = geometry.x;
4093 *screen_y = geometry.y;
4094 *width = geometry.width;
4095 *height = geometry.height;
4096 }
4097
4098 /*
4099 * The screen size is used to make sure the initial window doesn't get bigger
4100 * than the screen. This subtracts some room for menubar, toolbar and window
4101 * decorations.
4102 */
4103 static void
gui_gtk_get_screen_dimensions(int point_x,int point_y,int * screen_w,int * screen_h)4104 gui_gtk_get_screen_dimensions(
4105 int point_x,
4106 int point_y,
4107 int *screen_w,
4108 int *screen_h)
4109 {
4110 int x, y;
4111
4112 gui_gtk_get_screen_geom_of_win(gui.mainwin, point_x, point_y,
4113 &x, &y, screen_w, screen_h);
4114
4115 // Subtract 'guiheadroom' from the height to allow some room for the
4116 // window manager (task list and window title bar).
4117 *screen_h -= p_ghr;
4118
4119 /*
4120 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
4121 * the toolbar and menubar for GTK, we subtract them from the screen
4122 * height, so that the window size can be made to fit on the screen.
4123 * This should be completely changed later.
4124 */
4125 *screen_w -= get_menu_tool_width();
4126 *screen_h -= get_menu_tool_height();
4127 }
4128
4129 void
gui_mch_get_screen_dimensions(int * screen_w,int * screen_h)4130 gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
4131 {
4132 gui_gtk_get_screen_dimensions(0, 0, screen_w, screen_h);
4133 }
4134
4135
4136 /*
4137 * Bit of a hack to ensure we start GtkPlug windows with the correct window
4138 * hints (and thus the required size from -geom), but that after that we
4139 * put the hints back to normal (the actual minimum size) so we may
4140 * subsequently be resized smaller. GtkSocket (the parent end) uses the
4141 * plug's window 'min hints to set *its* minimum size, but that's also the
4142 * only way we have of making ourselves bigger (by set lines/columns).
4143 * Thus set hints at start-up to ensure correct init. size, then a
4144 * second after the final attempt to reset the real minimum hints (done by
4145 * scrollbar init.), actually do the standard hints and stop the timer.
4146 * We'll not let the default hints be set while this timer's active.
4147 */
4148 static timeout_cb_type
check_startup_plug_hints(gpointer data UNUSED)4149 check_startup_plug_hints(gpointer data UNUSED)
4150 {
4151 if (init_window_hints_state == 1)
4152 {
4153 // Safe to use normal hints now
4154 init_window_hints_state = 0;
4155 update_window_manager_hints(0, 0);
4156 return FALSE; // stop timer
4157 }
4158
4159 // Keep on trying
4160 init_window_hints_state = 1;
4161 return TRUE;
4162 }
4163
4164 /*
4165 * Open the GUI window which was created by a call to gui_mch_init().
4166 */
4167 int
gui_mch_open(void)4168 gui_mch_open(void)
4169 {
4170 guicolor_T fg_pixel = INVALCOLOR;
4171 guicolor_T bg_pixel = INVALCOLOR;
4172 guint pixel_width;
4173 guint pixel_height;
4174
4175 /*
4176 * Allow setting a window role on the command line, or invent one
4177 * if none was specified. This is mainly useful for GNOME session
4178 * support; allowing the WM to restore window placement.
4179 */
4180 if (role_argument != NULL)
4181 {
4182 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument);
4183 }
4184 else
4185 {
4186 char *role;
4187
4188 // Invent a unique-enough ID string for the role
4189 role = g_strdup_printf("vim-%u-%u-%u",
4190 (unsigned)mch_get_pid(),
4191 (unsigned)g_random_int(),
4192 (unsigned)time(NULL));
4193
4194 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role);
4195 g_free(role);
4196 }
4197
4198 if (gui_win_x != -1 && gui_win_y != -1)
4199 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y);
4200
4201 // Determine user specified geometry, if present.
4202 if (gui.geom != NULL)
4203 {
4204 int mask;
4205 unsigned int w, h;
4206 int x = 0;
4207 int y = 0;
4208
4209 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
4210
4211 if (mask & WidthValue)
4212 Columns = w;
4213 if (mask & HeightValue)
4214 {
4215 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
4216 p_window = h - 1;
4217 Rows = h;
4218 }
4219 limit_screen_size();
4220
4221 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4222 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
4223
4224 pixel_width += get_menu_tool_width();
4225 pixel_height += get_menu_tool_height();
4226
4227 if (mask & (XValue | YValue))
4228 {
4229 int ww, hh;
4230
4231 #ifdef FEAT_GUI_GTK
4232 gui_gtk_get_screen_dimensions(x, y, &ww, &hh);
4233 #else
4234 gui_mch_get_screen_dimensions(&ww, &hh);
4235 #endif
4236 hh += p_ghr + get_menu_tool_height();
4237 ww += get_menu_tool_width();
4238 if (mask & XNegative)
4239 x += ww - pixel_width;
4240 if (mask & YNegative)
4241 y += hh - pixel_height;
4242 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
4243 }
4244 VIM_CLEAR(gui.geom);
4245
4246 // From now until everyone's stopped trying to set the window hints
4247 // to their correct minimum values, stop them being set as we need
4248 // them to remain at our required size for the parent GtkSocket to
4249 // give us the right initial size.
4250 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue))
4251 {
4252 update_window_manager_hints(pixel_width, pixel_height);
4253 init_window_hints_state = 1;
4254 timeout_add(1000, check_startup_plug_hints, NULL);
4255 }
4256 }
4257
4258 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4259 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
4260 // For GTK2 changing the size of the form widget doesn't cause window
4261 // resizing.
4262 if (gtk_socket_id == 0)
4263 gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height);
4264 update_window_manager_hints(0, 0);
4265
4266 if (foreground_argument != NULL)
4267 fg_pixel = gui_get_color((char_u *)foreground_argument);
4268 if (fg_pixel == INVALCOLOR)
4269 fg_pixel = gui_get_color((char_u *)"Black");
4270
4271 if (background_argument != NULL)
4272 bg_pixel = gui_get_color((char_u *)background_argument);
4273 if (bg_pixel == INVALCOLOR)
4274 bg_pixel = gui_get_color((char_u *)"White");
4275
4276 if (found_reverse_arg)
4277 {
4278 gui.def_norm_pixel = bg_pixel;
4279 gui.def_back_pixel = fg_pixel;
4280 }
4281 else
4282 {
4283 gui.def_norm_pixel = fg_pixel;
4284 gui.def_back_pixel = bg_pixel;
4285 }
4286
4287 // Get the colors from the "Normal" and "Menu" group (set in syntax.c or
4288 // in a vimrc file)
4289 set_normal_colors();
4290
4291 // Check that none of the colors are the same as the background color
4292 gui_check_colors();
4293
4294 // Get the colors for the highlight groups (gui_check_colors() might have
4295 // changed them).
4296 highlight_gui_started(); // re-init colors and fonts
4297
4298 g_signal_connect(G_OBJECT(gui.mainwin), "destroy",
4299 G_CALLBACK(mainwin_destroy_cb), NULL);
4300
4301 /*
4302 * Notify the fixed area about the need to resize the contents of the
4303 * gui.formwin, which we use for random positioning of the included
4304 * components.
4305 *
4306 * We connect this signal deferred finally after anything is in place,
4307 * since this is intended to handle resizements coming from the window
4308 * manager upon us and should not interfere with what VIM is requesting
4309 * upon startup.
4310 */
4311 g_signal_connect(G_OBJECT(gui.formwin), "configure-event",
4312 G_CALLBACK(form_configure_event), NULL);
4313
4314 #ifdef FEAT_DND
4315 // Set up for receiving DND items.
4316 gui_gtk_set_dnd_targets();
4317
4318 g_signal_connect(G_OBJECT(gui.drawarea), "drag-data-received",
4319 G_CALLBACK(drag_data_received_cb), NULL);
4320 #endif
4321
4322 // With GTK+ 2, we need to iconify the window before calling show()
4323 // to avoid mapping the window for a short time.
4324 if (found_iconic_arg && gtk_socket_id == 0)
4325 gui_mch_iconify();
4326
4327 {
4328 #if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
4329 unsigned long menu_handler = 0;
4330 # ifdef FEAT_TOOLBAR
4331 unsigned long tool_handler = 0;
4332 # endif
4333 /*
4334 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
4335 * show when restoring the saved layout configuration. We can't just
4336 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
4337 * a toplevel window and thus will be realized immediately. Instead,
4338 * connect signal handlers to hide the widgets just after they've been
4339 * marked visible, but before the main window is realized.
4340 */
4341 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL)
4342 menu_handler = g_signal_connect_after(gui.menubar_h, "show",
4343 G_CALLBACK(>k_widget_hide),
4344 NULL);
4345 # ifdef FEAT_TOOLBAR
4346 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL
4347 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
4348 tool_handler = g_signal_connect_after(gui.toolbar_h, "show",
4349 G_CALLBACK(>k_widget_hide),
4350 NULL);
4351 # endif
4352 #endif
4353 gtk_widget_show(gui.mainwin);
4354
4355 #if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
4356 if (menu_handler != 0)
4357 g_signal_handler_disconnect(gui.menubar_h, menu_handler);
4358 # ifdef FEAT_TOOLBAR
4359 if (tool_handler != 0)
4360 g_signal_handler_disconnect(gui.toolbar_h, tool_handler);
4361 # endif
4362 #endif
4363 }
4364
4365 return OK;
4366 }
4367
4368 /*
4369 * Clean up for when exiting Vim.
4370 */
4371 void
gui_mch_exit(int rc UNUSED)4372 gui_mch_exit(int rc UNUSED)
4373 {
4374 // Clean up, unless we don't want to invoke free().
4375 if (gui.mainwin != NULL && !really_exiting)
4376 gtk_widget_destroy(gui.mainwin);
4377 }
4378
4379 /*
4380 * Get the position of the top left corner of the window.
4381 */
4382 int
gui_mch_get_winpos(int * x,int * y)4383 gui_mch_get_winpos(int *x, int *y)
4384 {
4385 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y);
4386 return OK;
4387 }
4388
4389 /*
4390 * Set the position of the top left corner of the window to the given
4391 * coordinates.
4392 */
4393 void
gui_mch_set_winpos(int x,int y)4394 gui_mch_set_winpos(int x, int y)
4395 {
4396 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
4397 }
4398
4399 #if !GTK_CHECK_VERSION(3,0,0)
4400 # if 0
4401 static int resize_idle_installed = FALSE;
4402 /*
4403 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
4404 * the shell size doesn't exceed the window size, i.e. if the window manager
4405 * ignored our size request. Usually this happens if the window is maximized.
4406 *
4407 * FIXME: It'd be nice if we could find a little more orthodox solution.
4408 * See also the remark below in gui_mch_set_shellsize().
4409 *
4410 * DISABLED: When doing ":set lines+=1" this function would first invoke
4411 * gui_resize_shell() with the old size, then the normal callback would
4412 * report the new size through form_configure_event(). That caused the window
4413 * layout to be messed up.
4414 */
4415 static gboolean
4416 force_shell_resize_idle(gpointer data)
4417 {
4418 if (gui.mainwin != NULL
4419 && GTK_WIDGET_REALIZED(gui.mainwin)
4420 && GTK_WIDGET_VISIBLE(gui.mainwin))
4421 {
4422 int width;
4423 int height;
4424
4425 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height);
4426
4427 width -= get_menu_tool_width();
4428 height -= get_menu_tool_height();
4429
4430 gui_resize_shell(width, height);
4431 }
4432
4433 resize_idle_installed = FALSE;
4434 return FALSE; // don't call me again
4435 }
4436 # endif
4437 #endif // !GTK_CHECK_VERSION(3,0,0)
4438
4439 /*
4440 * Return TRUE if the main window is maximized.
4441 */
4442 int
gui_mch_maximized(void)4443 gui_mch_maximized(void)
4444 {
4445 return (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL
4446 && (gdk_window_get_state(gtk_widget_get_window(gui.mainwin))
4447 & GDK_WINDOW_STATE_MAXIMIZED));
4448 }
4449
4450 /*
4451 * Unmaximize the main window
4452 */
4453 void
gui_mch_unmaximize(void)4454 gui_mch_unmaximize(void)
4455 {
4456 if (gui.mainwin != NULL)
4457 gtk_window_unmaximize(GTK_WINDOW(gui.mainwin));
4458 }
4459
4460 /*
4461 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
4462 * is set. Compute the new Rows and Columns. This is like resizing the
4463 * window.
4464 */
4465 void
gui_mch_newfont(void)4466 gui_mch_newfont(void)
4467 {
4468 int w, h;
4469
4470 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
4471 w -= get_menu_tool_width();
4472 h -= get_menu_tool_height();
4473 gui_resize_shell(w, h);
4474 }
4475
4476 /*
4477 * Set the windows size.
4478 */
4479 void
gui_mch_set_shellsize(int width,int height,int min_width UNUSED,int min_height UNUSED,int base_width UNUSED,int base_height UNUSED,int direction UNUSED)4480 gui_mch_set_shellsize(int width, int height,
4481 int min_width UNUSED, int min_height UNUSED,
4482 int base_width UNUSED, int base_height UNUSED,
4483 int direction UNUSED)
4484 {
4485 // give GTK+ a chance to put all widget's into place
4486 gui_mch_update();
4487
4488 // this will cause the proper resizement to happen too
4489 if (gtk_socket_id == 0)
4490 update_window_manager_hints(0, 0);
4491
4492 // With GTK+ 2, changing the size of the form widget doesn't resize
4493 // the window. So let's do it the other way around and resize the
4494 // main window instead.
4495 width += get_menu_tool_width();
4496 height += get_menu_tool_height();
4497
4498 if (gtk_socket_id == 0)
4499 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height);
4500 else
4501 update_window_manager_hints(width, height);
4502
4503 # if !GTK_CHECK_VERSION(3,0,0)
4504 # if 0
4505 if (!resize_idle_installed)
4506 {
4507 g_idle_add_full(GDK_PRIORITY_EVENTS + 10,
4508 &force_shell_resize_idle, NULL, NULL);
4509 resize_idle_installed = TRUE;
4510 }
4511 # endif
4512 # endif // !GTK_CHECK_VERSION(3,0,0)
4513 /*
4514 * Wait until all events are processed to prevent a crash because the
4515 * real size of the drawing area doesn't reflect Vim's internal ideas.
4516 *
4517 * This is a bit of a hack, since Vim is a terminal application with a GUI
4518 * on top, while the GUI expects to be the boss.
4519 */
4520 gui_mch_update();
4521 }
4522
4523 #if defined(FEAT_TITLE) || defined(PROTO)
4524 void
gui_mch_settitle(char_u * title,char_u * icon UNUSED)4525 gui_mch_settitle(char_u *title, char_u *icon UNUSED)
4526 {
4527 if (title != NULL && output_conv.vc_type != CONV_NONE)
4528 title = string_convert(&output_conv, title, NULL);
4529
4530 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
4531
4532 if (output_conv.vc_type != CONV_NONE)
4533 vim_free(title);
4534 }
4535 #endif // FEAT_TITLE
4536
4537 #if defined(FEAT_MENU) || defined(PROTO)
4538 void
gui_mch_enable_menu(int showit)4539 gui_mch_enable_menu(int showit)
4540 {
4541 GtkWidget *widget;
4542
4543 # ifdef FEAT_GUI_GNOME
4544 if (using_gnome)
4545 widget = gui.menubar_h;
4546 else
4547 # endif
4548 widget = gui.menubar;
4549
4550 // Do not disable the menu while starting up, otherwise F10 doesn't work.
4551 if (!showit != !gtk_widget_get_visible(widget) && !gui.starting)
4552 {
4553 if (showit)
4554 gtk_widget_show(widget);
4555 else
4556 gtk_widget_hide(widget);
4557
4558 update_window_manager_hints(0, 0);
4559 }
4560 }
4561 #endif // FEAT_MENU
4562
4563 #if defined(FEAT_TOOLBAR) || defined(PROTO)
4564 void
gui_mch_show_toolbar(int showit)4565 gui_mch_show_toolbar(int showit)
4566 {
4567 GtkWidget *widget;
4568
4569 if (gui.toolbar == NULL)
4570 return;
4571
4572 # ifdef FEAT_GUI_GNOME
4573 if (using_gnome)
4574 widget = gui.toolbar_h;
4575 else
4576 # endif
4577 widget = gui.toolbar;
4578
4579 if (showit)
4580 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
4581
4582 if (!showit != !gtk_widget_get_visible(widget))
4583 {
4584 if (showit)
4585 gtk_widget_show(widget);
4586 else
4587 gtk_widget_hide(widget);
4588
4589 update_window_manager_hints(0, 0);
4590 }
4591 }
4592 #endif // FEAT_TOOLBAR
4593
4594 /*
4595 * Check if a given font is a CJK font. This is done in a very crude manner. It
4596 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
4597 * font. Consequently, this function cannot be used as a general purpose check
4598 * for CJK-ness for which fontconfig APIs should be used. This is only used by
4599 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
4600 */
4601 static int
is_cjk_font(PangoFontDescription * font_desc)4602 is_cjk_font(PangoFontDescription *font_desc)
4603 {
4604 static const char * const cjk_langs[] =
4605 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
4606
4607 PangoFont *font;
4608 unsigned i;
4609 int is_cjk = FALSE;
4610
4611 font = pango_context_load_font(gui.text_context, font_desc);
4612
4613 if (font == NULL)
4614 return FALSE;
4615
4616 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i)
4617 {
4618 PangoCoverage *coverage;
4619 gunichar uc;
4620
4621 // Valgrind reports a leak for pango_language_from_string(), but the
4622 // documentation says "This is owned by Pango and should not be freed".
4623 coverage = pango_font_get_coverage(
4624 font, pango_language_from_string(cjk_langs[i]));
4625
4626 if (coverage != NULL)
4627 {
4628 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00;
4629 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT);
4630 pango_coverage_unref(coverage);
4631 }
4632 }
4633
4634 g_object_unref(font);
4635
4636 return is_cjk;
4637 }
4638
4639 /*
4640 * Adjust gui.char_height (after 'linespace' was changed).
4641 */
4642 int
gui_mch_adjust_charheight(void)4643 gui_mch_adjust_charheight(void)
4644 {
4645 PangoFontMetrics *metrics;
4646 int ascent;
4647 int descent;
4648
4649 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font,
4650 pango_context_get_language(gui.text_context));
4651 ascent = pango_font_metrics_get_ascent(metrics);
4652 descent = pango_font_metrics_get_descent(metrics);
4653
4654 pango_font_metrics_unref(metrics);
4655
4656 // Round up when the value is more than about 1/16 of a pixel above a whole
4657 // pixel (12.0624 becomes 12, 12.07 becomes 13). Then add 'linespace'.
4658 gui.char_height = (ascent + descent + (PANGO_SCALE * 15) / 16)
4659 / PANGO_SCALE + p_linespace;
4660 // LINTED: avoid warning: bitwise operation on signed value
4661 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2);
4662
4663 // A not-positive value of char_height may crash Vim. Only happens
4664 // if 'linespace' is negative (which does make sense sometimes).
4665 gui.char_ascent = MAX(gui.char_ascent, 0);
4666 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
4667
4668 return OK;
4669 }
4670
4671 #if GTK_CHECK_VERSION(3,0,0)
4672 // Callback function used in gui_mch_font_dialog()
4673 static gboolean
font_filter(const PangoFontFamily * family,const PangoFontFace * face UNUSED,gpointer data UNUSED)4674 font_filter(const PangoFontFamily *family,
4675 const PangoFontFace *face UNUSED,
4676 gpointer data UNUSED)
4677 {
4678 return pango_font_family_is_monospace((PangoFontFamily *)family);
4679 }
4680 #endif
4681
4682 /*
4683 * Put up a font dialog and return the selected font name in allocated memory.
4684 * "oldval" is the previous value. Return NULL when cancelled.
4685 * This should probably go into gui_gtk.c. Hmm.
4686 * FIXME:
4687 * The GTK2 font selection dialog has no filtering API. So we could either
4688 * a) implement our own (possibly copying the code from somewhere else) or
4689 * b) just live with it.
4690 */
4691 char_u *
gui_mch_font_dialog(char_u * oldval)4692 gui_mch_font_dialog(char_u *oldval)
4693 {
4694 GtkWidget *dialog;
4695 int response;
4696 char_u *fontname = NULL;
4697 char_u *oldname;
4698
4699 #if GTK_CHECK_VERSION(3,2,0)
4700 dialog = gtk_font_chooser_dialog_new(NULL, NULL);
4701 gtk_font_chooser_set_filter_func(GTK_FONT_CHOOSER(dialog), font_filter,
4702 NULL, NULL);
4703 #else
4704 dialog = gtk_font_selection_dialog_new(NULL);
4705 #endif
4706
4707 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
4708 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
4709
4710 if (oldval != NULL && oldval[0] != NUL)
4711 {
4712 if (output_conv.vc_type != CONV_NONE)
4713 oldname = string_convert(&output_conv, oldval, NULL);
4714 else
4715 oldname = oldval;
4716
4717 // Annoying bug in GTK (or Pango): if the font name does not include a
4718 // size, zero is used. Use default point size ten.
4719 if (!vim_isdigit(oldname[STRLEN(oldname) - 1]))
4720 {
4721 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3);
4722
4723 if (p != NULL)
4724 {
4725 STRCPY(p + STRLEN(p), " 10");
4726 if (oldname != oldval)
4727 vim_free(oldname);
4728 oldname = p;
4729 }
4730 }
4731
4732 #if GTK_CHECK_VERSION(3,2,0)
4733 gtk_font_chooser_set_font(
4734 GTK_FONT_CHOOSER(dialog), (const gchar *)oldname);
4735 #else
4736 gtk_font_selection_dialog_set_font_name(
4737 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname);
4738 #endif
4739
4740 if (oldname != oldval)
4741 vim_free(oldname);
4742 }
4743 else
4744 #if GTK_CHECK_VERSION(3,2,0)
4745 gtk_font_chooser_set_font(
4746 GTK_FONT_CHOOSER(dialog), DEFAULT_FONT);
4747 #else
4748 gtk_font_selection_dialog_set_font_name(
4749 GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT);
4750 #endif
4751
4752 response = gtk_dialog_run(GTK_DIALOG(dialog));
4753
4754 if (response == GTK_RESPONSE_OK)
4755 {
4756 char *name;
4757
4758 #if GTK_CHECK_VERSION(3,2,0)
4759 name = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(dialog));
4760 #else
4761 name = gtk_font_selection_dialog_get_font_name(
4762 GTK_FONT_SELECTION_DIALOG(dialog));
4763 #endif
4764 if (name != NULL)
4765 {
4766 char_u *p;
4767
4768 // Apparently some font names include a comma, need to escape
4769 // that, because in 'guifont' it separates names.
4770 p = vim_strsave_escaped((char_u *)name, (char_u *)",");
4771 g_free(name);
4772 if (p != NULL && input_conv.vc_type != CONV_NONE)
4773 {
4774 fontname = string_convert(&input_conv, p, NULL);
4775 vim_free(p);
4776 }
4777 else
4778 fontname = p;
4779 }
4780 }
4781
4782 if (response != GTK_RESPONSE_NONE)
4783 gtk_widget_destroy(dialog);
4784
4785 return fontname;
4786 }
4787
4788 /*
4789 * Some monospace fonts don't support a bold weight, and fall back
4790 * silently to the regular weight. But this is no good since our text
4791 * drawing function can emulate bold by overstriking. So let's try
4792 * to detect whether bold weight is actually available and emulate it
4793 * otherwise.
4794 *
4795 * Note that we don't need to check for italic style since Xft can
4796 * emulate italic on its own, provided you have a proper fontconfig
4797 * setup. We wouldn't be able to emulate it in Vim anyway.
4798 */
4799 static void
get_styled_font_variants(void)4800 get_styled_font_variants(void)
4801 {
4802 PangoFontDescription *bold_font_desc;
4803 PangoFont *plain_font;
4804 PangoFont *bold_font;
4805
4806 gui.font_can_bold = FALSE;
4807
4808 plain_font = pango_context_load_font(gui.text_context, gui.norm_font);
4809
4810 if (plain_font == NULL)
4811 return;
4812
4813 bold_font_desc = pango_font_description_copy_static(gui.norm_font);
4814 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD);
4815
4816 bold_font = pango_context_load_font(gui.text_context, bold_font_desc);
4817 /*
4818 * The comparison relies on the unique handle nature of a PangoFont*,
4819 * i.e. it's assumed that a different PangoFont* won't refer to the
4820 * same font. Seems to work, and failing here isn't critical anyway.
4821 */
4822 if (bold_font != NULL)
4823 {
4824 gui.font_can_bold = (bold_font != plain_font);
4825 g_object_unref(bold_font);
4826 }
4827
4828 pango_font_description_free(bold_font_desc);
4829 g_object_unref(plain_font);
4830 }
4831
4832 static PangoEngineShape *default_shape_engine = NULL;
4833
4834 /*
4835 * Create a map from ASCII characters in the range [32,126] to glyphs
4836 * of the current font. This is used by gui_gtk2_draw_string() to skip
4837 * the itemize and shaping process for the most common case.
4838 */
4839 static void
ascii_glyph_table_init(void)4840 ascii_glyph_table_init(void)
4841 {
4842 char_u ascii_chars[2 * 128];
4843 PangoAttrList *attr_list;
4844 GList *item_list;
4845 int i;
4846
4847 if (gui.ascii_glyphs != NULL)
4848 pango_glyph_string_free(gui.ascii_glyphs);
4849 if (gui.ascii_font != NULL)
4850 g_object_unref(gui.ascii_font);
4851
4852 gui.ascii_glyphs = NULL;
4853 gui.ascii_font = NULL;
4854
4855 // For safety, fill in question marks for the control characters.
4856 // Put a space between characters to avoid shaping.
4857 for (i = 0; i < 128; ++i)
4858 {
4859 if (i >= 32 && i < 127)
4860 ascii_chars[2 * i] = i;
4861 else
4862 ascii_chars[2 * i] = '?';
4863 ascii_chars[2 * i + 1] = ' ';
4864 }
4865
4866 attr_list = pango_attr_list_new();
4867 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars,
4868 0, sizeof(ascii_chars), attr_list, NULL);
4869
4870 if (item_list != NULL && item_list->next == NULL) // play safe
4871 {
4872 PangoItem *item;
4873 int width;
4874
4875 item = (PangoItem *)item_list->data;
4876 width = gui.char_width * PANGO_SCALE;
4877
4878 // Remember the shape engine used for ASCII.
4879 default_shape_engine = item->analysis.shape_engine;
4880
4881 gui.ascii_font = item->analysis.font;
4882 g_object_ref(gui.ascii_font);
4883
4884 gui.ascii_glyphs = pango_glyph_string_new();
4885
4886 pango_shape((const char *)ascii_chars, sizeof(ascii_chars),
4887 &item->analysis, gui.ascii_glyphs);
4888
4889 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars));
4890
4891 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i)
4892 {
4893 PangoGlyphGeometry *geom;
4894
4895 geom = &gui.ascii_glyphs->glyphs[i].geometry;
4896 geom->x_offset += MAX(0, width - geom->width) / 2;
4897 geom->width = width;
4898 }
4899 }
4900
4901 // TODO: is this type cast OK?
4902 g_list_foreach(item_list, (GFunc)(void *)&pango_item_free, NULL);
4903 g_list_free(item_list);
4904 pango_attr_list_unref(attr_list);
4905 }
4906
4907 /*
4908 * Initialize Vim to use the font or fontset with the given name.
4909 * Return FAIL if the font could not be loaded, OK otherwise.
4910 */
4911 int
gui_mch_init_font(char_u * font_name,int fontset UNUSED)4912 gui_mch_init_font(char_u *font_name, int fontset UNUSED)
4913 {
4914 PangoFontDescription *font_desc;
4915 PangoLayout *layout;
4916 int width;
4917
4918 // If font_name is NULL, this means to use the default, which should
4919 // be present on all proper Pango/fontconfig installations.
4920 if (font_name == NULL)
4921 font_name = (char_u *)DEFAULT_FONT;
4922
4923 font_desc = gui_mch_get_font(font_name, FALSE);
4924
4925 if (font_desc == NULL)
4926 return FAIL;
4927
4928 gui_mch_free_font(gui.norm_font);
4929 gui.norm_font = font_desc;
4930
4931 pango_context_set_font_description(gui.text_context, font_desc);
4932
4933 layout = pango_layout_new(gui.text_context);
4934 pango_layout_set_text(layout, "MW", 2);
4935 pango_layout_get_size(layout, &width, NULL);
4936 /*
4937 * Set char_width to half the width obtained from pango_layout_get_size()
4938 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
4939 * Pango to use the same width for both non-CJK characters (e.g. Latin
4940 * letters and numbers) and CJK characters. This results in 's p a c e d
4941 * o u t' rendering when a CJK 'fixed width' font is used. To work around
4942 * that, divide the width returned by Pango by 2 if cjk_width is equal to
4943 * width for CJK fonts.
4944 *
4945 * For related bugs, see:
4946 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
4947 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
4948 *
4949 * With this, for all four of the following cases, Vim works fine:
4950 * guifont=CJK_fixed_width_font
4951 * guifont=Non_CJK_fixed_font
4952 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
4953 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
4954 */
4955 if (is_cjk_font(gui.norm_font))
4956 {
4957 int cjk_width;
4958
4959 // Measure the text extent of U+4E00 and U+4E8C
4960 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1);
4961 pango_layout_get_size(layout, &cjk_width, NULL);
4962
4963 if (width == cjk_width) // Xft not patched
4964 width /= 2;
4965 }
4966 g_object_unref(layout);
4967
4968 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE;
4969
4970 // A zero width may cause a crash. Happens for semi-invalid fontsets.
4971 if (gui.char_width <= 0)
4972 gui.char_width = 8;
4973
4974 gui_mch_adjust_charheight();
4975
4976 // Set the fontname, which will be used for information purposes
4977 hl_set_font_name(font_name);
4978
4979 get_styled_font_variants();
4980 ascii_glyph_table_init();
4981
4982 // Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'.
4983 if (gui.wide_font != NULL
4984 && pango_font_description_equal(gui.norm_font, gui.wide_font))
4985 {
4986 pango_font_description_free(gui.wide_font);
4987 gui.wide_font = NULL;
4988 }
4989
4990 if (gui_mch_maximized())
4991 {
4992 // Update lines and columns in accordance with the new font, keep the
4993 // window maximized.
4994 gui_mch_newfont();
4995 }
4996 else
4997 {
4998 // Preserve the logical dimensions of the screen.
4999 update_window_manager_hints(0, 0);
5000 }
5001
5002 return OK;
5003 }
5004
5005 /*
5006 * Get a reference to the font "name".
5007 * Return zero for failure.
5008 */
5009 GuiFont
gui_mch_get_font(char_u * name,int report_error)5010 gui_mch_get_font(char_u *name, int report_error)
5011 {
5012 PangoFontDescription *font;
5013
5014 // can't do this when GUI is not running
5015 if (!gui.in_use || name == NULL)
5016 return NULL;
5017
5018 if (output_conv.vc_type != CONV_NONE)
5019 {
5020 char_u *buf;
5021
5022 buf = string_convert(&output_conv, name, NULL);
5023 if (buf != NULL)
5024 {
5025 font = pango_font_description_from_string((const char *)buf);
5026 vim_free(buf);
5027 }
5028 else
5029 font = NULL;
5030 }
5031 else
5032 font = pango_font_description_from_string((const char *)name);
5033
5034 if (font != NULL)
5035 {
5036 PangoFont *real_font;
5037
5038 // pango_context_load_font() bails out if no font size is set
5039 if (pango_font_description_get_size(font) <= 0)
5040 pango_font_description_set_size(font, 10 * PANGO_SCALE);
5041
5042 real_font = pango_context_load_font(gui.text_context, font);
5043
5044 if (real_font == NULL)
5045 {
5046 pango_font_description_free(font);
5047 font = NULL;
5048 }
5049 else
5050 g_object_unref(real_font);
5051 }
5052
5053 if (font == NULL)
5054 {
5055 if (report_error)
5056 semsg(_((char *)e_font), name);
5057 return NULL;
5058 }
5059
5060 return font;
5061 }
5062
5063 #if defined(FEAT_EVAL) || defined(PROTO)
5064 /*
5065 * Return the name of font "font" in allocated memory.
5066 */
5067 char_u *
gui_mch_get_fontname(GuiFont font,char_u * name UNUSED)5068 gui_mch_get_fontname(GuiFont font, char_u *name UNUSED)
5069 {
5070 if (font != NOFONT)
5071 {
5072 char *pangoname = pango_font_description_to_string(font);
5073
5074 if (pangoname != NULL)
5075 {
5076 char_u *s = vim_strsave((char_u *)pangoname);
5077
5078 g_free(pangoname);
5079 return s;
5080 }
5081 }
5082 return NULL;
5083 }
5084 #endif
5085
5086 /*
5087 * If a font is not going to be used, free its structure.
5088 */
5089 void
gui_mch_free_font(GuiFont font)5090 gui_mch_free_font(GuiFont font)
5091 {
5092 if (font != NOFONT)
5093 pango_font_description_free(font);
5094 }
5095
5096 /*
5097 * Return the Pixel value (color) for the given color name.
5098 *
5099 * Return INVALCOLOR for error.
5100 */
5101 guicolor_T
gui_mch_get_color(char_u * name)5102 gui_mch_get_color(char_u *name)
5103 {
5104 guicolor_T color = INVALCOLOR;
5105
5106 if (!gui.in_use) // can't do this when GUI not running
5107 return color;
5108
5109 if (name != NULL)
5110 color = gui_get_color_cmn(name);
5111
5112 #if GTK_CHECK_VERSION(3,0,0)
5113 return color;
5114 #else
5115 if (color == INVALCOLOR)
5116 return INVALCOLOR;
5117
5118 return gui_mch_get_rgb_color(
5119 (color & 0xff0000) >> 16,
5120 (color & 0xff00) >> 8,
5121 color & 0xff);
5122 #endif
5123 }
5124
5125 /*
5126 * Return the Pixel value (color) for the given RGB values.
5127 * Return INVALCOLOR for error.
5128 */
5129 guicolor_T
gui_mch_get_rgb_color(int r,int g,int b)5130 gui_mch_get_rgb_color(int r, int g, int b)
5131 {
5132 #if GTK_CHECK_VERSION(3,0,0)
5133 return gui_get_rgb_color_cmn(r, g, b);
5134 #else
5135 GdkColor gcolor;
5136 int ret;
5137
5138 gcolor.red = (guint16)(r / 255.0 * 65535 + 0.5);
5139 gcolor.green = (guint16)(g / 255.0 * 65535 + 0.5);
5140 gcolor.blue = (guint16)(b / 255.0 * 65535 + 0.5);
5141
5142 ret = gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea),
5143 &gcolor, FALSE, TRUE);
5144
5145 return ret != 0 ? (guicolor_T)gcolor.pixel : INVALCOLOR;
5146 #endif
5147 }
5148
5149 /*
5150 * Set the current text foreground color.
5151 */
5152 void
gui_mch_set_fg_color(guicolor_T color)5153 gui_mch_set_fg_color(guicolor_T color)
5154 {
5155 #if GTK_CHECK_VERSION(3,0,0)
5156 *gui.fgcolor = color_to_rgba(color);
5157 #else
5158 gui.fgcolor->pixel = (unsigned long)color;
5159 #endif
5160 }
5161
5162 /*
5163 * Set the current text background color.
5164 */
5165 void
gui_mch_set_bg_color(guicolor_T color)5166 gui_mch_set_bg_color(guicolor_T color)
5167 {
5168 #if GTK_CHECK_VERSION(3,0,0)
5169 *gui.bgcolor = color_to_rgba(color);
5170 #else
5171 gui.bgcolor->pixel = (unsigned long)color;
5172 #endif
5173 }
5174
5175 /*
5176 * Set the current text special color.
5177 */
5178 void
gui_mch_set_sp_color(guicolor_T color)5179 gui_mch_set_sp_color(guicolor_T color)
5180 {
5181 #if GTK_CHECK_VERSION(3,0,0)
5182 *gui.spcolor = color_to_rgba(color);
5183 #else
5184 gui.spcolor->pixel = (unsigned long)color;
5185 #endif
5186 }
5187
5188 /*
5189 * Function-like convenience macro for the sake of efficiency.
5190 */
5191 #define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
5192 G_STMT_START{ \
5193 PangoAttribute *tmp_attr_; \
5194 tmp_attr_ = (Attribute); \
5195 tmp_attr_->start_index = (Start); \
5196 tmp_attr_->end_index = (End); \
5197 pango_attr_list_insert((AttrList), tmp_attr_); \
5198 }G_STMT_END
5199
5200 static void
apply_wide_font_attr(char_u * s,int len,PangoAttrList * attr_list)5201 apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list)
5202 {
5203 char_u *start = NULL;
5204 char_u *p;
5205 int uc;
5206
5207 for (p = s; p < s + len; p += utf_byte2len(*p))
5208 {
5209 uc = utf_ptr2char(p);
5210
5211 if (start == NULL)
5212 {
5213 if (uc >= 0x80 && utf_char2cells(uc) == 2)
5214 start = p;
5215 }
5216 else if (uc < 0x80 // optimization shortcut
5217 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc)))
5218 {
5219 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5220 attr_list, start - s, p - s);
5221 start = NULL;
5222 }
5223 }
5224
5225 if (start != NULL)
5226 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5227 attr_list, start - s, len);
5228 }
5229
5230 static int
count_cluster_cells(char_u * s,PangoItem * item,PangoGlyphString * glyphs,int i,int * cluster_width,int * last_glyph_rbearing)5231 count_cluster_cells(char_u *s, PangoItem *item,
5232 PangoGlyphString* glyphs, int i,
5233 int *cluster_width,
5234 int *last_glyph_rbearing)
5235 {
5236 char_u *p;
5237 int next; // glyph start index of next cluster
5238 int start, end; // string segment of current cluster
5239 int width; // real cluster width in Pango units
5240 int uc;
5241 int cellcount = 0;
5242
5243 width = glyphs->glyphs[i].geometry.width;
5244
5245 for (next = i + 1; next < glyphs->num_glyphs; ++next)
5246 {
5247 if (glyphs->glyphs[next].attr.is_cluster_start)
5248 break;
5249 else if (glyphs->glyphs[next].geometry.width > width)
5250 width = glyphs->glyphs[next].geometry.width;
5251 }
5252
5253 start = item->offset + glyphs->log_clusters[i];
5254 end = item->offset + ((next < glyphs->num_glyphs) ?
5255 glyphs->log_clusters[next] : item->length);
5256
5257 for (p = s + start; p < s + end; p += utf_byte2len(*p))
5258 {
5259 uc = utf_ptr2char(p);
5260 if (uc < 0x80)
5261 ++cellcount;
5262 else if (!utf_iscomposing(uc))
5263 cellcount += utf_char2cells(uc);
5264 }
5265
5266 if (last_glyph_rbearing != NULL
5267 && cellcount > 0 && next == glyphs->num_glyphs)
5268 {
5269 PangoRectangle ink_rect;
5270 /*
5271 * If a certain combining mark had to be taken from a non-monospace
5272 * font, we have to compensate manually by adapting x_offset according
5273 * to the ink extents of the previous glyph.
5274 */
5275 pango_font_get_glyph_extents(item->analysis.font,
5276 glyphs->glyphs[i].glyph,
5277 &ink_rect, NULL);
5278
5279 if (PANGO_RBEARING(ink_rect) > 0)
5280 *last_glyph_rbearing = PANGO_RBEARING(ink_rect);
5281 }
5282
5283 if (cellcount > 0)
5284 *cluster_width = width;
5285
5286 return cellcount;
5287 }
5288
5289 /*
5290 * If there are only combining characters in the cluster, we cannot just
5291 * change the width of the previous glyph since there is none. Therefore
5292 * some guesswork is needed.
5293 *
5294 * If ink_rect.x is negative Pango apparently has taken care of the composing
5295 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
5296 * to problems with composing from different fonts we still need to fine-tune
5297 * x_offset to avoid ugliness.
5298 *
5299 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
5300 * the position of the previous glyph. Apparently this happens only with old
5301 * X fonts which don't provide the special combining information needed by
5302 * Pango.
5303 */
5304 static void
setup_zero_width_cluster(PangoItem * item,PangoGlyphInfo * glyph,int last_cellcount,int last_cluster_width,int last_glyph_rbearing)5305 setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph,
5306 int last_cellcount, int last_cluster_width,
5307 int last_glyph_rbearing)
5308 {
5309 PangoRectangle ink_rect;
5310 PangoRectangle logical_rect;
5311 int width;
5312
5313 width = last_cellcount * gui.char_width * PANGO_SCALE;
5314 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2;
5315 glyph->geometry.width = 0;
5316
5317 pango_font_get_glyph_extents(item->analysis.font,
5318 glyph->glyph,
5319 &ink_rect, &logical_rect);
5320 if (ink_rect.x < 0)
5321 {
5322 glyph->geometry.x_offset += last_glyph_rbearing;
5323 glyph->geometry.y_offset = logical_rect.height
5324 - (gui.char_height - p_linespace) * PANGO_SCALE;
5325 }
5326 else
5327 // If the accent width is smaller than the cluster width, position it
5328 // in the middle.
5329 glyph->geometry.x_offset = -width + MAX(0, width - ink_rect.width) / 2;
5330 }
5331
5332 #if GTK_CHECK_VERSION(3,0,0)
5333 static void
draw_glyph_string(int row,int col,int num_cells,int flags,PangoFont * font,PangoGlyphString * glyphs,cairo_t * cr)5334 draw_glyph_string(int row, int col, int num_cells, int flags,
5335 PangoFont *font, PangoGlyphString *glyphs,
5336 cairo_t *cr)
5337 #else
5338 static void
5339 draw_glyph_string(int row, int col, int num_cells, int flags,
5340 PangoFont *font, PangoGlyphString *glyphs)
5341 #endif
5342 {
5343 if (!(flags & DRAW_TRANSP))
5344 {
5345 #if GTK_CHECK_VERSION(3,0,0)
5346 cairo_set_source_rgba(cr,
5347 gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue,
5348 gui.bgcolor->alpha);
5349 cairo_rectangle(cr,
5350 FILL_X(col), FILL_Y(row),
5351 num_cells * gui.char_width, gui.char_height);
5352 cairo_fill(cr);
5353 #else
5354 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
5355
5356 gdk_draw_rectangle(gui.drawarea->window,
5357 gui.text_gc,
5358 TRUE,
5359 FILL_X(col),
5360 FILL_Y(row),
5361 num_cells * gui.char_width,
5362 gui.char_height);
5363 #endif
5364 }
5365
5366 #if GTK_CHECK_VERSION(3,0,0)
5367 cairo_set_source_rgba(cr,
5368 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5369 gui.fgcolor->alpha);
5370 cairo_move_to(cr, TEXT_X(col), TEXT_Y(row));
5371 pango_cairo_show_glyph_string(cr, font, glyphs);
5372 #else
5373 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5374
5375 gdk_draw_glyphs(gui.drawarea->window,
5376 gui.text_gc,
5377 font,
5378 TEXT_X(col),
5379 TEXT_Y(row),
5380 glyphs);
5381 #endif
5382
5383 // redraw the contents with an offset of 1 to emulate bold
5384 if ((flags & DRAW_BOLD) && !gui.font_can_bold)
5385 #if GTK_CHECK_VERSION(3,0,0)
5386 {
5387 cairo_set_source_rgba(cr,
5388 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5389 gui.fgcolor->alpha);
5390 cairo_move_to(cr, TEXT_X(col) + 1, TEXT_Y(row));
5391 pango_cairo_show_glyph_string(cr, font, glyphs);
5392 }
5393 #else
5394 gdk_draw_glyphs(gui.drawarea->window,
5395 gui.text_gc,
5396 font,
5397 TEXT_X(col) + 1,
5398 TEXT_Y(row),
5399 glyphs);
5400 #endif
5401 }
5402
5403 /*
5404 * Draw underline and undercurl at the bottom of the character cell.
5405 */
5406 #if GTK_CHECK_VERSION(3,0,0)
5407 static void
draw_under(int flags,int row,int col,int cells,cairo_t * cr)5408 draw_under(int flags, int row, int col, int cells, cairo_t *cr)
5409 #else
5410 static void
5411 draw_under(int flags, int row, int col, int cells)
5412 #endif
5413 {
5414 int i;
5415 int offset;
5416 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
5417 int y = FILL_Y(row + 1) - 1;
5418
5419 // Undercurl: draw curl at the bottom of the character cell.
5420 if (flags & DRAW_UNDERC)
5421 {
5422 #if GTK_CHECK_VERSION(3,0,0)
5423 cairo_set_line_width(cr, 1.0);
5424 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
5425 cairo_set_source_rgba(cr,
5426 gui.spcolor->red, gui.spcolor->green, gui.spcolor->blue,
5427 gui.spcolor->alpha);
5428 cairo_move_to(cr, FILL_X(col) + 1, y - 2 + 0.5);
5429 for (i = FILL_X(col) + 1; i < FILL_X(col + cells); ++i)
5430 {
5431 offset = val[i % 8];
5432 cairo_line_to(cr, i, y - offset + 0.5);
5433 }
5434 cairo_stroke(cr);
5435 #else
5436 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
5437 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
5438 {
5439 offset = val[i % 8];
5440 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset);
5441 }
5442 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5443 #endif
5444 }
5445
5446 // Draw a strikethrough line
5447 if (flags & DRAW_STRIKE)
5448 {
5449 #if GTK_CHECK_VERSION(3,0,0)
5450 cairo_set_line_width(cr, 1.0);
5451 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
5452 cairo_set_source_rgba(cr,
5453 gui.spcolor->red, gui.spcolor->green, gui.spcolor->blue,
5454 gui.spcolor->alpha);
5455 cairo_move_to(cr, FILL_X(col), y + 1 - gui.char_height/2 + 0.5);
5456 cairo_line_to(cr, FILL_X(col + cells), y + 1 - gui.char_height/2 + 0.5);
5457 cairo_stroke(cr);
5458 #else
5459 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
5460 gdk_draw_line(gui.drawarea->window, gui.text_gc,
5461 FILL_X(col), y + 1 - gui.char_height/2,
5462 FILL_X(col + cells), y + 1 - gui.char_height/2);
5463 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5464 #endif
5465 }
5466
5467 // Underline: draw a line at the bottom of the character cell.
5468 if (flags & DRAW_UNDERL)
5469 {
5470 // When p_linespace is 0, overwrite the bottom row of pixels.
5471 // Otherwise put the line just below the character.
5472 if (p_linespace > 1)
5473 y -= p_linespace - 1;
5474 #if GTK_CHECK_VERSION(3,0,0)
5475 cairo_set_line_width(cr, 1.0);
5476 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
5477 cairo_set_source_rgba(cr,
5478 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5479 gui.fgcolor->alpha);
5480 cairo_move_to(cr, FILL_X(col), y + 0.5);
5481 cairo_line_to(cr, FILL_X(col + cells), y + 0.5);
5482 cairo_stroke(cr);
5483 #else
5484 gdk_draw_line(gui.drawarea->window, gui.text_gc,
5485 FILL_X(col), y,
5486 FILL_X(col + cells) - 1, y);
5487 #endif
5488 }
5489 }
5490
5491 int
gui_gtk2_draw_string(int row,int col,char_u * s,int len,int flags)5492 gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
5493 {
5494 char_u *conv_buf = NULL; // result of UTF-8 conversion
5495 char_u *new_conv_buf;
5496 int convlen;
5497 char_u *sp, *bp;
5498 int plen;
5499 int len_sum; // return value needs to add up since we are
5500 // printing substrings
5501 int byte_sum; // byte position in string
5502 char_u *cs; // current *s pointer
5503 int needs_pango; // look ahead, 0=ascii 1=unicode/ligatures
5504 int should_need_pango = FALSE;
5505 int slen;
5506 int is_ligature;
5507 int is_utf8;
5508 char_u backup_ch;
5509
5510 if (gui.text_context == NULL || gtk_widget_get_window(gui.drawarea) == NULL)
5511 return len;
5512
5513 if (output_conv.vc_type != CONV_NONE)
5514 {
5515 /*
5516 * Convert characters from 'encoding' to 'termencoding', which is set
5517 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
5518 * prohibits changing this to something else than UTF-8 if the GUI is
5519 * in use.
5520 */
5521 convlen = len;
5522 conv_buf = string_convert(&output_conv, s, &convlen);
5523 g_return_val_if_fail(conv_buf != NULL, len);
5524
5525 // Correct for differences in char width: some chars are
5526 // double-wide in 'encoding' but single-wide in utf-8. Add a space to
5527 // compensate for that.
5528 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; )
5529 {
5530 plen = utf_ptr2len(bp);
5531 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1)
5532 {
5533 new_conv_buf = alloc(convlen + 2);
5534 if (new_conv_buf == NULL)
5535 return len;
5536 plen += bp - conv_buf;
5537 mch_memmove(new_conv_buf, conv_buf, plen);
5538 new_conv_buf[plen] = ' ';
5539 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen,
5540 convlen - plen + 1);
5541 vim_free(conv_buf);
5542 conv_buf = new_conv_buf;
5543 ++convlen;
5544 bp = conv_buf + plen;
5545 plen = 1;
5546 }
5547 sp += (*mb_ptr2len)(sp);
5548 bp += plen;
5549 }
5550 s = conv_buf;
5551 len = convlen;
5552 }
5553
5554 /*
5555 * Ligature support and complex utf-8 char optimization:
5556 * String received to output to screen can print using pre-cached glyphs
5557 * (fast) or Pango (slow). Ligatures and multibype utf-8 must use Pango.
5558 * Since we receive mixed content string, split it into logical segments
5559 * that are guaranteed to go trough glyphs as much as possible. Since
5560 * single ligature char prints as ascii, print it that way.
5561 */
5562 len_sum = 0; // return value needs to add up since we are printing
5563 // substrings
5564 byte_sum = 0;
5565 cs = s;
5566 // First char decides starting needs_pango mode, 0=ascii 1=utf8/ligatures.
5567 // Even if it is ligature char, two chars or more make ligature.
5568 // Ascii followed by utf8 is also going trough pango.
5569 is_utf8 = (*cs & 0x80);
5570 is_ligature = gui.ligatures_map[*cs] && (len > 1);
5571 if (is_ligature)
5572 is_ligature = gui.ligatures_map[*(cs + 1)];
5573 if (!is_utf8 && len > 1)
5574 is_utf8 = (*(cs + 1) & 0x80) != 0;
5575 needs_pango = is_utf8 || is_ligature;
5576
5577 // split string into ascii and non-ascii (ligatures + utf-8) substrings,
5578 // print glyphs or use Pango
5579 while (cs < s + len)
5580 {
5581 slen = 0;
5582 while (slen < (len - byte_sum))
5583 {
5584 is_ligature = gui.ligatures_map[*(cs + slen)];
5585 // look ahead, single ligature char between ascii is ascii
5586 if (is_ligature && !needs_pango)
5587 {
5588 if ((slen + 1) < (len - byte_sum))
5589 is_ligature = gui.ligatures_map[*(cs + slen + 1)];
5590 else
5591 is_ligature = 0;
5592 }
5593 is_utf8 = *(cs + slen) & 0x80;
5594 // ascii followed by utf8 could be combining
5595 // if so send it trough pango
5596 if ((!is_utf8) && ((slen + 1) < (len - byte_sum)))
5597 is_utf8 = (*(cs + slen + 1) & 0x80);
5598 should_need_pango = (is_ligature || is_utf8);
5599 if (needs_pango != should_need_pango) // mode switch
5600 break;
5601 if (needs_pango)
5602 {
5603 if (is_ligature)
5604 {
5605 slen++; // ligature char by char
5606 }
5607 else if (is_utf8)
5608 {
5609 if ((*(cs + slen) & 0xC0) == 0x80)
5610 {
5611 // a continuation, find next 0xC0 != 0x80 but don't
5612 // include it
5613 while ((slen < (len - byte_sum))
5614 && ((*(cs + slen) & 0xC0) == 0x80))
5615 {
5616 slen++;
5617 }
5618 }
5619 else if ((*(cs + slen) & 0xE0) == 0xC0)
5620 {
5621 // + one byte utf8
5622 slen++;
5623 }
5624 else if ((*(cs + slen) & 0xF0) == 0xE0)
5625 {
5626 // + two bytes utf8
5627 slen += 2;
5628 }
5629 else if ((*(cs + slen) & 0xF8) == 0xF0)
5630 {
5631 // + three bytes utf8
5632 slen += 3;
5633 }
5634 else
5635 {
5636 // this should not happen, try moving forward, Pango
5637 // will catch it
5638 slen++;
5639 }
5640 }
5641 else
5642 {
5643 slen++;
5644 }
5645 }
5646 else
5647 {
5648 slen++; // ascii
5649 }
5650 }
5651
5652 if (slen < len)
5653 {
5654 // temporarily zero terminate substring, print, restore char, wrap
5655 backup_ch = *(cs + slen);
5656 *(cs + slen) = NUL;
5657 }
5658 len_sum += gui_gtk2_draw_string_ext(row, col + len_sum,
5659 cs, slen, flags, needs_pango);
5660 if (slen < len)
5661 *(cs + slen) = backup_ch;
5662 cs += slen;
5663 byte_sum += slen;
5664 needs_pango = should_need_pango;
5665 }
5666 vim_free(conv_buf);
5667 return len_sum;
5668 }
5669
5670 int
gui_gtk2_draw_string_ext(int row,int col,char_u * s,int len,int flags,int force_pango)5671 gui_gtk2_draw_string_ext(
5672 int row,
5673 int col,
5674 char_u *s,
5675 int len,
5676 int flags,
5677 int force_pango)
5678 {
5679 GdkRectangle area; // area for clip mask
5680 PangoGlyphString *glyphs; // glyphs of current item
5681 int column_offset = 0; // column offset in cells
5682 int i;
5683 #if GTK_CHECK_VERSION(3,0,0)
5684 cairo_t *cr;
5685 #endif
5686
5687 /*
5688 * Restrict all drawing to the current screen line in order to prevent
5689 * fuzzy font lookups from messing up the screen.
5690 */
5691 area.x = gui.border_offset;
5692 area.y = FILL_Y(row);
5693 area.width = gui.num_cols * gui.char_width;
5694 area.height = gui.char_height;
5695
5696 #if GTK_CHECK_VERSION(3,0,0)
5697 cr = cairo_create(gui.surface);
5698 cairo_rectangle(cr, area.x, area.y, area.width, area.height);
5699 cairo_clip(cr);
5700 #else
5701 gdk_gc_set_clip_origin(gui.text_gc, 0, 0);
5702 gdk_gc_set_clip_rectangle(gui.text_gc, &area);
5703 #endif
5704
5705 glyphs = pango_glyph_string_new();
5706
5707 /*
5708 * Optimization hack: If possible, skip the itemize and shaping process
5709 * for pure ASCII strings. This optimization is particularly effective
5710 * because Vim draws space characters to clear parts of the screen.
5711 */
5712 if (!(flags & DRAW_ITALIC)
5713 && !((flags & DRAW_BOLD) && gui.font_can_bold)
5714 && gui.ascii_glyphs != NULL
5715 && !force_pango)
5716 {
5717 char_u *p;
5718
5719 for (p = s; p < s + len; ++p)
5720 if (*p & 0x80)
5721 goto not_ascii;
5722
5723 pango_glyph_string_set_size(glyphs, len);
5724
5725 for (i = 0; i < len; ++i)
5726 {
5727 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[2 * s[i]];
5728 glyphs->log_clusters[i] = i;
5729 }
5730
5731 #if GTK_CHECK_VERSION(3,0,0)
5732 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs, cr);
5733 #else
5734 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs);
5735 #endif
5736
5737 column_offset = len;
5738 }
5739 else
5740 not_ascii:
5741 {
5742 PangoAttrList *attr_list;
5743 GList *item_list;
5744 int cluster_width;
5745 int last_glyph_rbearing;
5746 int cells = 0; // cells occupied by current cluster
5747
5748 // Safety check: pango crashes when invoked with invalid utf-8
5749 // characters.
5750 if (!utf_valid_string(s, s + len))
5751 {
5752 column_offset = len;
5753 goto skipitall;
5754 }
5755
5756 // original width of the current cluster
5757 cluster_width = PANGO_SCALE * gui.char_width;
5758
5759 // right bearing of the last non-composing glyph
5760 last_glyph_rbearing = PANGO_SCALE * gui.char_width;
5761
5762 attr_list = pango_attr_list_new();
5763
5764 // If 'guifontwide' is set then use that for double-width characters.
5765 // Otherwise just go with 'guifont' and let Pango do its thing.
5766 if (gui.wide_font != NULL)
5767 apply_wide_font_attr(s, len, attr_list);
5768
5769 if ((flags & DRAW_BOLD) && gui.font_can_bold)
5770 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD),
5771 attr_list, 0, len);
5772 if (flags & DRAW_ITALIC)
5773 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC),
5774 attr_list, 0, len);
5775 /*
5776 * Break the text into segments with consistent directional level
5777 * and shaping engine. Pure Latin text needs only a single segment,
5778 * so there's no need to worry about the loop's efficiency. Better
5779 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
5780 */
5781 item_list = pango_itemize(gui.text_context,
5782 (const char *)s, 0, len, attr_list, NULL);
5783
5784 while (item_list != NULL)
5785 {
5786 PangoItem *item;
5787 int item_cells = 0; // item length in cells
5788
5789 item = (PangoItem *)item_list->data;
5790 item_list = g_list_delete_link(item_list, item_list);
5791 /*
5792 * Increment the bidirectional embedding level by 1 if it is not
5793 * even. An odd number means the output will be RTL, but we don't
5794 * want that since Vim handles right-to-left text on its own. It
5795 * would probably be sufficient to just set level = 0, but you can
5796 * never know :)
5797 *
5798 * Unfortunately we can't take advantage of Pango's ability to
5799 * render both LTR and RTL at the same time. In order to support
5800 * that, Vim's main screen engine would have to make use of Pango
5801 * functionality.
5802 */
5803 item->analysis.level = (item->analysis.level + 1) & (~1U);
5804
5805 // HACK: Overrule the shape engine, we don't want shaping to be
5806 // done, because drawing the cursor would change the display.
5807 item->analysis.shape_engine = default_shape_engine;
5808
5809 #ifdef HAVE_PANGO_SHAPE_FULL
5810 pango_shape_full((const char *)s + item->offset, item->length,
5811 (const char *)s, len, &item->analysis, glyphs);
5812 #else
5813 pango_shape((const char *)s + item->offset, item->length,
5814 &item->analysis, glyphs);
5815 #endif
5816 /*
5817 * Fixed-width hack: iterate over the array and assign a fixed
5818 * width to each glyph, thus overriding the choice made by the
5819 * shaping engine. We use utf_char2cells() to determine the
5820 * number of cells needed.
5821 *
5822 * Also perform all kind of dark magic to get composing
5823 * characters right (and pretty too of course).
5824 */
5825 for (i = 0; i < glyphs->num_glyphs; ++i)
5826 {
5827 PangoGlyphInfo *glyph;
5828
5829 glyph = &glyphs->glyphs[i];
5830
5831 if (glyph->attr.is_cluster_start)
5832 {
5833 int cellcount;
5834
5835 cellcount = count_cluster_cells(
5836 s, item, glyphs, i, &cluster_width,
5837 (item_list != NULL) ? &last_glyph_rbearing : NULL);
5838
5839 if (cellcount > 0)
5840 {
5841 int width;
5842
5843 width = cellcount * gui.char_width * PANGO_SCALE;
5844 glyph->geometry.x_offset +=
5845 MAX(0, width - cluster_width) / 2;
5846 glyph->geometry.width = width;
5847 }
5848 else
5849 {
5850 // If there are only combining characters in the
5851 // cluster, we cannot just change the width of the
5852 // previous glyph since there is none. Therefore
5853 // some guesswork is needed.
5854 setup_zero_width_cluster(item, glyph, cells,
5855 cluster_width,
5856 last_glyph_rbearing);
5857 }
5858
5859 item_cells += cellcount;
5860 cells = cellcount;
5861 }
5862 else if (i > 0)
5863 {
5864 int width;
5865
5866 // There is a previous glyph, so we deal with combining
5867 // characters the canonical way.
5868 // In some circumstances Pango uses a positive x_offset,
5869 // then use the width of the previous glyph for this one
5870 // and set the previous width to zero.
5871 // Otherwise we get a negative x_offset, Pango has already
5872 // positioned the combining char, keep the widths as they
5873 // are.
5874 // For both adjust the x_offset to position the glyph in
5875 // the middle.
5876 if (glyph->geometry.x_offset >= 0)
5877 {
5878 glyphs->glyphs[i].geometry.width =
5879 glyphs->glyphs[i - 1].geometry.width;
5880 glyphs->glyphs[i - 1].geometry.width = 0;
5881 }
5882 width = cells * gui.char_width * PANGO_SCALE;
5883 glyph->geometry.x_offset +=
5884 MAX(0, width - cluster_width) / 2;
5885 }
5886 else // i == 0 "cannot happen"
5887 {
5888 glyph->geometry.width = 0;
5889 }
5890 }
5891
5892 //// Aaaaand action! **
5893 #if GTK_CHECK_VERSION(3,0,0)
5894 draw_glyph_string(row, col + column_offset, item_cells,
5895 flags, item->analysis.font, glyphs,
5896 cr);
5897 #else
5898 draw_glyph_string(row, col + column_offset, item_cells,
5899 flags, item->analysis.font, glyphs);
5900 #endif
5901
5902 pango_item_free(item);
5903
5904 column_offset += item_cells;
5905 }
5906
5907 pango_attr_list_unref(attr_list);
5908 }
5909
5910 skipitall:
5911 // Draw underline and undercurl.
5912 #if GTK_CHECK_VERSION(3,0,0)
5913 draw_under(flags, row, col, column_offset, cr);
5914 #else
5915 draw_under(flags, row, col, column_offset);
5916 #endif
5917
5918 pango_glyph_string_free(glyphs);
5919
5920 #if GTK_CHECK_VERSION(3,0,0)
5921 cairo_destroy(cr);
5922 gtk_widget_queue_draw_area(gui.drawarea, area.x, area.y,
5923 area.width, area.height);
5924 #else
5925 gdk_gc_set_clip_rectangle(gui.text_gc, NULL);
5926 #endif
5927
5928 return column_offset;
5929 }
5930
5931 /*
5932 * Return OK if the key with the termcap name "name" is supported.
5933 */
5934 int
gui_mch_haskey(char_u * name)5935 gui_mch_haskey(char_u *name)
5936 {
5937 int i;
5938
5939 for (i = 0; special_keys[i].key_sym != 0; i++)
5940 if (name[0] == special_keys[i].code0
5941 && name[1] == special_keys[i].code1)
5942 return OK;
5943 return FAIL;
5944 }
5945
5946 #if defined(FEAT_TITLE) || defined(FEAT_EVAL) || defined(PROTO)
5947 /*
5948 * Return the text window-id and display. Only required for X-based GUI's
5949 */
5950 int
gui_get_x11_windis(Window * win,Display ** dis)5951 gui_get_x11_windis(Window *win, Display **dis)
5952 {
5953 if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL)
5954 {
5955 *dis = GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin));
5956 *win = GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin));
5957 return OK;
5958 }
5959
5960 *dis = NULL;
5961 *win = 0;
5962 return FAIL;
5963 }
5964 #endif
5965
5966 #if defined(FEAT_CLIENTSERVER) \
5967 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
5968
5969 Display *
gui_mch_get_display(void)5970 gui_mch_get_display(void)
5971 {
5972 if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL)
5973 return GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin));
5974 else
5975 return NULL;
5976 }
5977 #endif
5978
5979 void
gui_mch_beep(void)5980 gui_mch_beep(void)
5981 {
5982 GdkDisplay *display;
5983
5984 if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin))
5985 display = gtk_widget_get_display(gui.mainwin);
5986 else
5987 display = gdk_display_get_default();
5988
5989 if (display != NULL)
5990 gdk_display_beep(display);
5991 }
5992
5993 void
gui_mch_flash(int msec)5994 gui_mch_flash(int msec)
5995 {
5996 #if GTK_CHECK_VERSION(3,0,0)
5997 // TODO Replace GdkGC with Cairo
5998 (void)msec;
5999 #else
6000 GdkGCValues values;
6001 GdkGC *invert_gc;
6002
6003 if (gui.drawarea->window == NULL)
6004 return;
6005
6006 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6007 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
6008 values.function = GDK_XOR;
6009 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6010 &values,
6011 GDK_GC_FOREGROUND |
6012 GDK_GC_BACKGROUND |
6013 GDK_GC_FUNCTION);
6014 gdk_gc_set_exposures(invert_gc,
6015 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
6016 /*
6017 * Do a visual beep by changing back and forth in some undetermined way,
6018 * the foreground and background colors. This is due to the fact that
6019 * there can't be really any prediction about the effects of XOR on
6020 * arbitrary X11 servers. However this seems to be enough for what we
6021 * intend it to do.
6022 */
6023 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6024 TRUE,
6025 0, 0,
6026 FILL_X((int)Columns) + gui.border_offset,
6027 FILL_Y((int)Rows) + gui.border_offset);
6028
6029 gui_mch_flush();
6030 ui_delay((long)msec, TRUE); // wait so many msec
6031
6032 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6033 TRUE,
6034 0, 0,
6035 FILL_X((int)Columns) + gui.border_offset,
6036 FILL_Y((int)Rows) + gui.border_offset);
6037
6038 gdk_gc_destroy(invert_gc);
6039 #endif
6040 }
6041
6042 /*
6043 * Invert a rectangle from row r, column c, for nr rows and nc columns.
6044 */
6045 void
gui_mch_invert_rectangle(int r,int c,int nr,int nc)6046 gui_mch_invert_rectangle(int r, int c, int nr, int nc)
6047 {
6048 #if GTK_CHECK_VERSION(3,0,0)
6049 const GdkRectangle rect = {
6050 FILL_X(c), FILL_Y(r), nc * gui.char_width, nr * gui.char_height
6051 };
6052 cairo_t * const cr = cairo_create(gui.surface);
6053
6054 set_cairo_source_rgba_from_color(cr, gui.norm_pixel ^ gui.back_pixel);
6055 # if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,9,2)
6056 cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE);
6057 # else
6058 // Give an implementation for older cairo versions if necessary.
6059 # endif
6060 cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height);
6061 cairo_fill(cr);
6062
6063 cairo_destroy(cr);
6064
6065 gtk_widget_queue_draw_area(gui.drawarea, rect.x, rect.y,
6066 rect.width, rect.height);
6067 #else
6068 GdkGCValues values;
6069 GdkGC *invert_gc;
6070
6071 if (gui.drawarea->window == NULL)
6072 return;
6073
6074 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6075 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
6076 values.function = GDK_XOR;
6077 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6078 &values,
6079 GDK_GC_FOREGROUND |
6080 GDK_GC_BACKGROUND |
6081 GDK_GC_FUNCTION);
6082 gdk_gc_set_exposures(invert_gc, gui.visibility !=
6083 GDK_VISIBILITY_UNOBSCURED);
6084 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6085 TRUE,
6086 FILL_X(c), FILL_Y(r),
6087 (nc) * gui.char_width, (nr) * gui.char_height);
6088 gdk_gc_destroy(invert_gc);
6089 #endif
6090 }
6091
6092 /*
6093 * Iconify the GUI window.
6094 */
6095 void
gui_mch_iconify(void)6096 gui_mch_iconify(void)
6097 {
6098 gtk_window_iconify(GTK_WINDOW(gui.mainwin));
6099 }
6100
6101 #if defined(FEAT_EVAL) || defined(PROTO)
6102 /*
6103 * Bring the Vim window to the foreground.
6104 */
6105 void
gui_mch_set_foreground(void)6106 gui_mch_set_foreground(void)
6107 {
6108 gtk_window_present(GTK_WINDOW(gui.mainwin));
6109 }
6110 #endif
6111
6112 /*
6113 * Draw a cursor without focus.
6114 */
6115 void
gui_mch_draw_hollow_cursor(guicolor_T color)6116 gui_mch_draw_hollow_cursor(guicolor_T color)
6117 {
6118 int i = 1;
6119 #if GTK_CHECK_VERSION(3,0,0)
6120 cairo_t *cr;
6121 #endif
6122
6123 if (gtk_widget_get_window(gui.drawarea) == NULL)
6124 return;
6125
6126 #if GTK_CHECK_VERSION(3,0,0)
6127 cr = cairo_create(gui.surface);
6128 #endif
6129
6130 gui_mch_set_fg_color(color);
6131
6132 #if GTK_CHECK_VERSION(3,0,0)
6133 cairo_set_source_rgba(cr,
6134 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
6135 gui.fgcolor->alpha);
6136 #else
6137 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6138 #endif
6139 if (mb_lefthalve(gui.row, gui.col))
6140 i = 2;
6141 #if GTK_CHECK_VERSION(3,0,0)
6142 cairo_set_line_width(cr, 1.0);
6143 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
6144 cairo_rectangle(cr,
6145 FILL_X(gui.col) + 0.5, FILL_Y(gui.row) + 0.5,
6146 i * gui.char_width - 1, gui.char_height - 1);
6147 cairo_stroke(cr);
6148 cairo_destroy(cr);
6149 #else
6150 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6151 FALSE,
6152 FILL_X(gui.col), FILL_Y(gui.row),
6153 i * gui.char_width - 1, gui.char_height - 1);
6154 #endif
6155 }
6156
6157 /*
6158 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
6159 * color "color".
6160 */
6161 void
gui_mch_draw_part_cursor(int w,int h,guicolor_T color)6162 gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
6163 {
6164 if (gtk_widget_get_window(gui.drawarea) == NULL)
6165 return;
6166
6167 gui_mch_set_fg_color(color);
6168
6169 #if GTK_CHECK_VERSION(3,0,0)
6170 {
6171 cairo_t *cr;
6172
6173 cr = cairo_create(gui.surface);
6174 cairo_set_source_rgba(cr,
6175 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
6176 gui.fgcolor->alpha);
6177 cairo_rectangle(cr,
6178 # ifdef FEAT_RIGHTLEFT
6179 // vertical line should be on the right of current point
6180 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
6181 # endif
6182 FILL_X(gui.col), FILL_Y(gui.row) + gui.char_height - h,
6183 w, h);
6184 cairo_fill(cr);
6185 cairo_destroy(cr);
6186 }
6187 #else // !GTK_CHECK_VERSION(3,0,0)
6188 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6189 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6190 TRUE,
6191 # ifdef FEAT_RIGHTLEFT
6192 // vertical line should be on the right of current point
6193 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
6194 # endif
6195 FILL_X(gui.col),
6196 FILL_Y(gui.row) + gui.char_height - h,
6197 w, h);
6198 #endif // !GTK_CHECK_VERSION(3,0,0)
6199 }
6200
6201
6202 /*
6203 * Catch up with any queued X11 events. This may put keyboard input into the
6204 * input buffer, call resize call-backs, trigger timers etc. If there is
6205 * nothing in the X11 event queue (& no timers pending), then we return
6206 * immediately.
6207 */
6208 void
gui_mch_update(void)6209 gui_mch_update(void)
6210 {
6211 while (g_main_context_pending(NULL) && !vim_is_input_buf_full())
6212 g_main_context_iteration(NULL, TRUE);
6213 }
6214
6215 static timeout_cb_type
input_timer_cb(gpointer data)6216 input_timer_cb(gpointer data)
6217 {
6218 int *timed_out = (int *) data;
6219
6220 // Just inform the caller about the occurrence of it
6221 *timed_out = TRUE;
6222
6223 return FALSE; // don't happen again
6224 }
6225
6226 #ifdef FEAT_JOB_CHANNEL
6227 static timeout_cb_type
channel_poll_cb(gpointer data UNUSED)6228 channel_poll_cb(gpointer data UNUSED)
6229 {
6230 // Using an event handler for a channel that may be disconnected does
6231 // not work, it hangs. Instead poll for messages.
6232 channel_handle_events(TRUE);
6233 parse_queued_messages();
6234
6235 return TRUE; // repeat
6236 }
6237 #endif
6238
6239 /*
6240 * GUI input routine called by gui_wait_for_chars(). Waits for a character
6241 * from the keyboard.
6242 * wtime == -1 Wait forever.
6243 * wtime == 0 This should never happen.
6244 * wtime > 0 Wait wtime milliseconds for a character.
6245 * Returns OK if a character was found to be available within the given time,
6246 * or FAIL otherwise.
6247 */
6248 int
gui_mch_wait_for_chars(long wtime)6249 gui_mch_wait_for_chars(long wtime)
6250 {
6251 int focus;
6252 guint timer;
6253 static int timed_out;
6254 int retval = FAIL;
6255 #ifdef FEAT_JOB_CHANNEL
6256 guint channel_timer = 0;
6257 #endif
6258
6259 timed_out = FALSE;
6260
6261 // This timeout makes sure that we will return if no characters arrived in
6262 // time. If "wtime" is zero just use one.
6263 if (wtime >= 0)
6264 timer = timeout_add(wtime == 0 ? 1L : wtime,
6265 input_timer_cb, &timed_out);
6266 else
6267 timer = 0;
6268
6269 #ifdef FEAT_JOB_CHANNEL
6270 // If there is a channel with the keep_open flag we need to poll for input
6271 // on them.
6272 if (channel_any_keep_open())
6273 channel_timer = timeout_add(20, channel_poll_cb, NULL);
6274 #endif
6275
6276 focus = gui.in_focus;
6277
6278 do
6279 {
6280 // Stop or start blinking when focus changes
6281 if (gui.in_focus != focus)
6282 {
6283 if (gui.in_focus)
6284 gui_mch_start_blink();
6285 else
6286 gui_mch_stop_blink(TRUE);
6287 focus = gui.in_focus;
6288 }
6289
6290 #ifdef MESSAGE_QUEUE
6291 # ifdef FEAT_TIMERS
6292 did_add_timer = FALSE;
6293 # endif
6294 parse_queued_messages();
6295 # ifdef FEAT_TIMERS
6296 if (did_add_timer)
6297 // Need to recompute the waiting time.
6298 goto theend;
6299 # endif
6300 #endif
6301
6302 /*
6303 * Loop in GTK+ processing until a timeout or input occurs.
6304 * Skip this if input is available anyway (can happen in rare
6305 * situations, sort of race condition).
6306 */
6307 if (!input_available())
6308 g_main_context_iteration(NULL, TRUE);
6309
6310 // Got char, return immediately
6311 if (input_available())
6312 {
6313 retval = OK;
6314 goto theend;
6315 }
6316 } while (wtime < 0 || !timed_out);
6317
6318 /*
6319 * Flush all eventually pending (drawing) events.
6320 */
6321 gui_mch_update();
6322
6323 theend:
6324 if (timer != 0 && !timed_out)
6325 timeout_remove(timer);
6326 #ifdef FEAT_JOB_CHANNEL
6327 if (channel_timer != 0)
6328 timeout_remove(channel_timer);
6329 #endif
6330
6331 return retval;
6332 }
6333
6334
6335 /////////////////////////////////////////////////////////////////////////////
6336 // Output drawing routines.
6337 //
6338
6339
6340 // Flush any output to the screen
6341 void
gui_mch_flush(void)6342 gui_mch_flush(void)
6343 {
6344 if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin))
6345 #if GTK_CHECK_VERSION(2,4,0)
6346 gdk_display_flush(gtk_widget_get_display(gui.mainwin));
6347 #else
6348 gdk_display_sync(gtk_widget_get_display(gui.mainwin));
6349 #endif
6350 }
6351
6352 /*
6353 * Clear a rectangular region of the screen from text pos (row1, col1) to
6354 * (row2, col2) inclusive.
6355 */
6356 void
gui_mch_clear_block(int row1arg,int col1arg,int row2arg,int col2arg)6357 gui_mch_clear_block(int row1arg, int col1arg, int row2arg, int col2arg)
6358 {
6359
6360 int col1 = check_col(col1arg);
6361 int col2 = check_col(col2arg);
6362 int row1 = check_row(row1arg);
6363 int row2 = check_row(row2arg);
6364
6365 #if GTK_CHECK_VERSION(3,0,0)
6366 if (gtk_widget_get_window(gui.drawarea) == NULL)
6367 return;
6368 #else
6369 GdkColor color;
6370
6371 if (gui.drawarea->window == NULL)
6372 return;
6373
6374 color.pixel = gui.back_pixel;
6375 #endif
6376
6377 #if GTK_CHECK_VERSION(3,0,0)
6378 {
6379 // Add one pixel to the far right column in case a double-stroked
6380 // bold glyph may sit there.
6381 const GdkRectangle rect = {
6382 FILL_X(col1), FILL_Y(row1),
6383 (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1),
6384 (row2 - row1 + 1) * gui.char_height
6385 };
6386 cairo_t * const cr = cairo_create(gui.surface);
6387 # if GTK_CHECK_VERSION(3,22,2)
6388 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
6389 # else
6390 GdkWindow * const win = gtk_widget_get_window(gui.drawarea);
6391 cairo_pattern_t * const pat = gdk_window_get_background_pattern(win);
6392 if (pat != NULL)
6393 cairo_set_source(cr, pat);
6394 else
6395 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
6396 # endif
6397 cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height);
6398 cairo_fill(cr);
6399 cairo_destroy(cr);
6400
6401 gtk_widget_queue_draw_area(gui.drawarea,
6402 rect.x, rect.y, rect.width, rect.height);
6403 }
6404 #else // !GTK_CHECK_VERSION(3,0,0)
6405 gdk_gc_set_foreground(gui.text_gc, &color);
6406
6407 // Clear one extra pixel at the far right, for when bold characters have
6408 // spilled over to the window border.
6409 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE,
6410 FILL_X(col1), FILL_Y(row1),
6411 (col2 - col1 + 1) * gui.char_width
6412 + (col2 == Columns - 1),
6413 (row2 - row1 + 1) * gui.char_height);
6414 #endif // !GTK_CHECK_VERSION(3,0,0)
6415 }
6416
6417 #if GTK_CHECK_VERSION(3,0,0)
6418 static void
gui_gtk_window_clear(GdkWindow * win)6419 gui_gtk_window_clear(GdkWindow *win)
6420 {
6421 const GdkRectangle rect = {
6422 0, 0, gdk_window_get_width(win), gdk_window_get_height(win)
6423 };
6424 cairo_t * const cr = cairo_create(gui.surface);
6425 # if GTK_CHECK_VERSION(3,22,2)
6426 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
6427 # else
6428 cairo_pattern_t * const pat = gdk_window_get_background_pattern(win);
6429 if (pat != NULL)
6430 cairo_set_source(cr, pat);
6431 else
6432 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
6433 # endif
6434 cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height);
6435 cairo_fill(cr);
6436 cairo_destroy(cr);
6437
6438 gtk_widget_queue_draw_area(gui.drawarea,
6439 rect.x, rect.y, rect.width, rect.height);
6440 }
6441 #else
6442 # define gui_gtk_window_clear(win) gdk_window_clear(win)
6443 #endif
6444
6445 void
gui_mch_clear_all(void)6446 gui_mch_clear_all(void)
6447 {
6448 if (gtk_widget_get_window(gui.drawarea) != NULL)
6449 gui_gtk_window_clear(gtk_widget_get_window(gui.drawarea));
6450 }
6451
6452 #if !GTK_CHECK_VERSION(3,0,0)
6453 /*
6454 * Redraw any text revealed by scrolling up/down.
6455 */
6456 static void
check_copy_area(void)6457 check_copy_area(void)
6458 {
6459 GdkEvent *event;
6460 int expose_count;
6461
6462 if (gui.visibility != GDK_VISIBILITY_PARTIAL)
6463 return;
6464
6465 // Avoid redrawing the cursor while scrolling or it'll end up where
6466 // we don't want it to be. I'm not sure if it's correct to call
6467 // gui_dont_update_cursor() at this point but it works as a quick
6468 // fix for now.
6469 gui_dont_update_cursor(TRUE);
6470
6471 do
6472 {
6473 // Wait to check whether the scroll worked or not.
6474 event = gdk_event_get_graphics_expose(gui.drawarea->window);
6475
6476 if (event == NULL)
6477 break; // received NoExpose event
6478
6479 gui_redraw(event->expose.area.x, event->expose.area.y,
6480 event->expose.area.width, event->expose.area.height);
6481
6482 expose_count = event->expose.count;
6483 gdk_event_free(event);
6484 }
6485 while (expose_count > 0); // more events follow
6486
6487 gui_can_update_cursor();
6488 }
6489 #endif // !GTK_CHECK_VERSION(3,0,0)
6490
6491 #if GTK_CHECK_VERSION(3,0,0)
6492 static void
gui_gtk_surface_copy_rect(int dest_x,int dest_y,int src_x,int src_y,int width,int height)6493 gui_gtk_surface_copy_rect(int dest_x, int dest_y,
6494 int src_x, int src_y,
6495 int width, int height)
6496 {
6497 cairo_t * const cr = cairo_create(gui.surface);
6498
6499 cairo_rectangle(cr, dest_x, dest_y, width, height);
6500 cairo_clip(cr);
6501 cairo_push_group(cr);
6502 cairo_set_source_surface(cr, gui.surface, dest_x - src_x, dest_y - src_y);
6503 cairo_paint(cr);
6504 cairo_pop_group_to_source(cr);
6505 cairo_paint(cr);
6506
6507 cairo_destroy(cr);
6508 }
6509 #endif
6510
6511 /*
6512 * Delete the given number of lines from the given row, scrolling up any
6513 * text further down within the scroll region.
6514 */
6515 void
gui_mch_delete_lines(int row,int num_lines)6516 gui_mch_delete_lines(int row, int num_lines)
6517 {
6518 #if GTK_CHECK_VERSION(3,0,0)
6519 const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1;
6520 const int nrows = gui.scroll_region_bot - row + 1;
6521 const int src_nrows = nrows - num_lines;
6522
6523 gui_gtk_surface_copy_rect(
6524 FILL_X(gui.scroll_region_left), FILL_Y(row),
6525 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6526 gui.char_width * ncols + 1, gui.char_height * src_nrows);
6527 gui_clear_block(
6528 gui.scroll_region_bot - num_lines + 1, gui.scroll_region_left,
6529 gui.scroll_region_bot, gui.scroll_region_right);
6530 gtk_widget_queue_draw_area(gui.drawarea,
6531 FILL_X(gui.scroll_region_left), FILL_Y(row),
6532 gui.char_width * ncols + 1, gui.char_height * nrows);
6533 #else
6534 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
6535 return; // Can't see the window
6536
6537 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6538 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6539
6540 // copy one extra pixel, for when bold has spilled over
6541 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6542 FILL_X(gui.scroll_region_left), FILL_Y(row),
6543 gui.drawarea->window,
6544 FILL_X(gui.scroll_region_left),
6545 FILL_Y(row + num_lines),
6546 gui.char_width * (gui.scroll_region_right
6547 - gui.scroll_region_left + 1) + 1,
6548 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6549
6550 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
6551 gui.scroll_region_left,
6552 gui.scroll_region_bot, gui.scroll_region_right);
6553 check_copy_area();
6554 #endif // !GTK_CHECK_VERSION(3,0,0)
6555 }
6556
6557 /*
6558 * Insert the given number of lines before the given row, scrolling down any
6559 * following text within the scroll region.
6560 */
6561 void
gui_mch_insert_lines(int row,int num_lines)6562 gui_mch_insert_lines(int row, int num_lines)
6563 {
6564 #if GTK_CHECK_VERSION(3,0,0)
6565 const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1;
6566 const int nrows = gui.scroll_region_bot - row + 1;
6567 const int src_nrows = nrows - num_lines;
6568
6569 gui_gtk_surface_copy_rect(
6570 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6571 FILL_X(gui.scroll_region_left), FILL_Y(row),
6572 gui.char_width * ncols + 1, gui.char_height * src_nrows);
6573 gui_clear_block(
6574 row, gui.scroll_region_left,
6575 row + num_lines - 1, gui.scroll_region_right);
6576 gtk_widget_queue_draw_area(gui.drawarea,
6577 FILL_X(gui.scroll_region_left), FILL_Y(row),
6578 gui.char_width * ncols + 1, gui.char_height * nrows);
6579 #else
6580 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
6581 return; // Can't see the window
6582
6583 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6584 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6585
6586 // copy one extra pixel, for when bold has spilled over
6587 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6588 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6589 gui.drawarea->window,
6590 FILL_X(gui.scroll_region_left), FILL_Y(row),
6591 gui.char_width * (gui.scroll_region_right
6592 - gui.scroll_region_left + 1) + 1,
6593 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6594
6595 gui_clear_block(row, gui.scroll_region_left,
6596 row + num_lines - 1, gui.scroll_region_right);
6597 check_copy_area();
6598 #endif // !GTK_CHECK_VERSION(3,0,0)
6599 }
6600
6601 /*
6602 * X Selection stuff, for cutting and pasting text to other windows.
6603 */
6604 void
clip_mch_request_selection(Clipboard_T * cbd)6605 clip_mch_request_selection(Clipboard_T *cbd)
6606 {
6607 GdkAtom target;
6608 unsigned i;
6609 time_t start;
6610
6611 for (i = 0; i < N_SELECTION_TARGETS; ++i)
6612 {
6613 if (!clip_html && selection_targets[i].info == TARGET_HTML)
6614 continue;
6615 received_selection = RS_NONE;
6616 target = gdk_atom_intern(selection_targets[i].target, FALSE);
6617
6618 gtk_selection_convert(gui.drawarea,
6619 cbd->gtk_sel_atom, target,
6620 (guint32)GDK_CURRENT_TIME);
6621
6622 // Hack: Wait up to three seconds for the selection. A hang was
6623 // noticed here when using the netrw plugin combined with ":gui"
6624 // during the FocusGained event.
6625 start = time(NULL);
6626 while (received_selection == RS_NONE && time(NULL) < start + 3)
6627 g_main_context_iteration(NULL, TRUE); // wait for selection_received_cb
6628
6629 if (received_selection != RS_FAIL)
6630 return;
6631 }
6632
6633 // Final fallback position - use the X CUT_BUFFER0 store
6634 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)),
6635 cbd);
6636 }
6637
6638 /*
6639 * Disown the selection.
6640 */
6641 void
clip_mch_lose_selection(Clipboard_T * cbd UNUSED)6642 clip_mch_lose_selection(Clipboard_T *cbd UNUSED)
6643 {
6644 if (!in_selection_clear_event)
6645 {
6646 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, gui.event_time);
6647 gui_mch_update();
6648 }
6649 }
6650
6651 /*
6652 * Own the selection and return OK if it worked.
6653 */
6654 int
clip_mch_own_selection(Clipboard_T * cbd)6655 clip_mch_own_selection(Clipboard_T *cbd)
6656 {
6657 int success;
6658
6659 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom,
6660 gui.event_time);
6661 gui_mch_update();
6662 return (success) ? OK : FAIL;
6663 }
6664
6665 /*
6666 * Send the current selection to the clipboard. Do nothing for X because we
6667 * will fill in the selection only when requested by another app.
6668 */
6669 void
clip_mch_set_selection(Clipboard_T * cbd UNUSED)6670 clip_mch_set_selection(Clipboard_T *cbd UNUSED)
6671 {
6672 }
6673
6674 #if (defined(FEAT_XCLIPBOARD) && defined(USE_SYSTEM)) || defined(PROTO)
6675 int
clip_gtk_owner_exists(Clipboard_T * cbd)6676 clip_gtk_owner_exists(Clipboard_T *cbd)
6677 {
6678 return gdk_selection_owner_get(cbd->gtk_sel_atom) != NULL;
6679 }
6680 #endif
6681
6682
6683 #if defined(FEAT_MENU) || defined(PROTO)
6684 /*
6685 * Make a menu item appear either active or not active (grey or not grey).
6686 */
6687 void
gui_mch_menu_grey(vimmenu_T * menu,int grey)6688 gui_mch_menu_grey(vimmenu_T *menu, int grey)
6689 {
6690 if (menu->id == NULL)
6691 return;
6692
6693 if (menu_is_separator(menu->name))
6694 grey = TRUE;
6695
6696 gui_mch_menu_hidden(menu, FALSE);
6697 // Be clever about bitfields versus true booleans here!
6698 if (!gtk_widget_get_sensitive(menu->id) == !grey)
6699 {
6700 gtk_widget_set_sensitive(menu->id, !grey);
6701 gui_mch_update();
6702 }
6703 }
6704
6705 /*
6706 * Make menu item hidden or not hidden.
6707 */
6708 void
gui_mch_menu_hidden(vimmenu_T * menu,int hidden)6709 gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
6710 {
6711 if (menu->id == 0)
6712 return;
6713
6714 if (hidden)
6715 {
6716 if (gtk_widget_get_visible(menu->id))
6717 {
6718 gtk_widget_hide(menu->id);
6719 gui_mch_update();
6720 }
6721 }
6722 else
6723 {
6724 if (!gtk_widget_get_visible(menu->id))
6725 {
6726 gtk_widget_show(menu->id);
6727 gui_mch_update();
6728 }
6729 }
6730 }
6731
6732 /*
6733 * This is called after setting all the menus to grey/hidden or not.
6734 */
6735 void
gui_mch_draw_menubar(void)6736 gui_mch_draw_menubar(void)
6737 {
6738 // just make sure that the visual changes get effect immediately
6739 gui_mch_update();
6740 }
6741 #endif // FEAT_MENU
6742
6743 /*
6744 * Scrollbar stuff.
6745 */
6746 void
gui_mch_enable_scrollbar(scrollbar_T * sb,int flag)6747 gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
6748 {
6749 if (sb->id == NULL)
6750 return;
6751
6752 gtk_widget_set_visible(sb->id, flag);
6753 update_window_manager_hints(0, 0);
6754 }
6755
6756
6757 /*
6758 * Return the RGB value of a pixel as long.
6759 */
6760 guicolor_T
gui_mch_get_rgb(guicolor_T pixel)6761 gui_mch_get_rgb(guicolor_T pixel)
6762 {
6763 #if GTK_CHECK_VERSION(3,0,0)
6764 return (long_u)pixel;
6765 #else
6766 GdkColor color;
6767
6768 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
6769 (unsigned long)pixel, &color);
6770
6771 return (guicolor_T)(
6772 (((unsigned)color.red & 0xff00) << 8)
6773 | ((unsigned)color.green & 0xff00)
6774 | (((unsigned)color.blue & 0xff00) >> 8));
6775 #endif
6776 }
6777
6778 /*
6779 * Get current mouse coordinates in text window.
6780 */
6781 void
gui_mch_getmouse(int * x,int * y)6782 gui_mch_getmouse(int *x, int *y)
6783 {
6784 gui_gtk_get_pointer(gui.drawarea, x, y, NULL);
6785 }
6786
6787 void
gui_mch_setmouse(int x,int y)6788 gui_mch_setmouse(int x, int y)
6789 {
6790 // Sorry for the Xlib call, but we can't avoid it, since there is no
6791 // internal GDK mechanism present to accomplish this. (and for good
6792 // reason...)
6793 XWarpPointer(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.drawarea)),
6794 (Window)0, GDK_WINDOW_XID(gtk_widget_get_window(gui.drawarea)),
6795 0, 0, 0U, 0U, x, y);
6796 }
6797
6798
6799 #ifdef FEAT_MOUSESHAPE
6800 // The last set mouse pointer shape is remembered, to be used when it goes
6801 // from hidden to not hidden.
6802 static int last_shape = 0;
6803 #endif
6804
6805 /*
6806 * Use the blank mouse pointer or not.
6807 *
6808 * hide: TRUE = use blank ptr, FALSE = use parent ptr
6809 */
6810 void
gui_mch_mousehide(int hide)6811 gui_mch_mousehide(int hide)
6812 {
6813 if (gui.pointer_hidden != hide)
6814 {
6815 gui.pointer_hidden = hide;
6816 if (gtk_widget_get_window(gui.drawarea) && gui.blank_pointer != NULL)
6817 {
6818 if (hide)
6819 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
6820 gui.blank_pointer);
6821 else
6822 #ifdef FEAT_MOUSESHAPE
6823 mch_set_mouse_shape(last_shape);
6824 #else
6825 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), NULL);
6826 #endif
6827 }
6828 }
6829 }
6830
6831 #if defined(FEAT_MOUSESHAPE) || defined(PROTO)
6832
6833 // Table for shape IDs. Keep in sync with the mshape_names[] table in
6834 // misc2.c!
6835 static const int mshape_ids[] =
6836 {
6837 GDK_LEFT_PTR, // arrow
6838 GDK_CURSOR_IS_PIXMAP, // blank
6839 GDK_XTERM, // beam
6840 GDK_SB_V_DOUBLE_ARROW, // updown
6841 GDK_SIZING, // udsizing
6842 GDK_SB_H_DOUBLE_ARROW, // leftright
6843 GDK_SIZING, // lrsizing
6844 GDK_WATCH, // busy
6845 GDK_X_CURSOR, // no
6846 GDK_CROSSHAIR, // crosshair
6847 GDK_HAND1, // hand1
6848 GDK_HAND2, // hand2
6849 GDK_PENCIL, // pencil
6850 GDK_QUESTION_ARROW, // question
6851 GDK_RIGHT_PTR, // right-arrow
6852 GDK_CENTER_PTR, // up-arrow
6853 GDK_LEFT_PTR // last one
6854 };
6855
6856 void
mch_set_mouse_shape(int shape)6857 mch_set_mouse_shape(int shape)
6858 {
6859 int id;
6860 GdkCursor *c;
6861
6862 if (gtk_widget_get_window(gui.drawarea) == NULL)
6863 return;
6864
6865 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
6866 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
6867 gui.blank_pointer);
6868 else
6869 {
6870 if (shape >= MSHAPE_NUMBERED)
6871 {
6872 id = shape - MSHAPE_NUMBERED;
6873 if (id >= GDK_LAST_CURSOR)
6874 id = GDK_LEFT_PTR;
6875 else
6876 id &= ~1; // they are always even (why?)
6877 }
6878 else if (shape < (int)ARRAY_LENGTH(mshape_ids))
6879 id = mshape_ids[shape];
6880 else
6881 return;
6882 c = gdk_cursor_new_for_display(
6883 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id);
6884 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), c);
6885 # if GTK_CHECK_VERSION(3,0,0)
6886 g_object_unref(G_OBJECT(c));
6887 # else
6888 gdk_cursor_destroy(c); // Unref, actually. Bloody GTK+ 1.
6889 # endif
6890 }
6891 if (shape != MSHAPE_HIDE)
6892 last_shape = shape;
6893 }
6894 #endif // FEAT_MOUSESHAPE
6895
6896
6897 #if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6898 /*
6899 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
6900 * scaled down if the current font is not big enough, or scaled up if the image
6901 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
6902 * will be cut off if the current font is not big enough, or centered if it's
6903 * too small.
6904 */
6905 # define SIGN_WIDTH (2 * gui.char_width)
6906 # define SIGN_HEIGHT (gui.char_height)
6907 # define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
6908
6909 void
gui_mch_drawsign(int row,int col,int typenr)6910 gui_mch_drawsign(int row, int col, int typenr)
6911 {
6912 GdkPixbuf *sign;
6913
6914 sign = (GdkPixbuf *)sign_get_image(typenr);
6915
6916 if (sign != NULL && gui.drawarea != NULL
6917 && gtk_widget_get_window(gui.drawarea) != NULL)
6918 {
6919 int width;
6920 int height;
6921 int xoffset;
6922 int yoffset;
6923 int need_scale;
6924
6925 width = gdk_pixbuf_get_width(sign);
6926 height = gdk_pixbuf_get_height(sign);
6927 /*
6928 * Decide whether we need to scale. Allow one pixel of border
6929 * width to be cut off, in order to avoid excessive scaling for
6930 * tiny differences in font size.
6931 * Do scale to fit the height to avoid gaps because of linespacing.
6932 */
6933 need_scale = (width > SIGN_WIDTH + 2
6934 || height != SIGN_HEIGHT
6935 || (width < 3 * SIGN_WIDTH / 4
6936 && height < 3 * SIGN_HEIGHT / 4));
6937 if (need_scale)
6938 {
6939 double aspect;
6940 int w = width;
6941 int h = height;
6942
6943 // Keep the original aspect ratio
6944 aspect = (double)height / (double)width;
6945 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect;
6946 width = MIN(width, SIGN_WIDTH);
6947 if (((double)(MAX(height, SIGN_HEIGHT)) /
6948 (double)(MIN(height, SIGN_HEIGHT))) < 1.15)
6949 {
6950 // Change the aspect ratio by at most 15% to fill the
6951 // available space completely.
6952 height = (double)SIGN_HEIGHT * SIGN_ASPECT / aspect;
6953 height = MIN(height, SIGN_HEIGHT);
6954 }
6955 else
6956 height = (double)width * aspect;
6957
6958 if (w == width && h == height)
6959 {
6960 // no change in dimensions; don't decrease reference counter
6961 // (below)
6962 need_scale = FALSE;
6963 }
6964 else
6965 {
6966 // This doesn't seem to be worth caching, and doing so would
6967 // complicate the code quite a bit.
6968 sign = gdk_pixbuf_scale_simple(sign, width, height,
6969 GDK_INTERP_BILINEAR);
6970 if (sign == NULL)
6971 return; // out of memory
6972 }
6973 }
6974
6975 // The origin is the upper-left corner of the pixmap. Therefore
6976 // these offset may become negative if the pixmap is smaller than
6977 // the 2x1 cells reserved for the sign icon.
6978 xoffset = (width - SIGN_WIDTH) / 2;
6979 yoffset = (height - SIGN_HEIGHT) / 2;
6980
6981 # if GTK_CHECK_VERSION(3,0,0)
6982 {
6983 cairo_t *cr;
6984 cairo_surface_t *bg_surf;
6985 cairo_t *bg_cr;
6986 cairo_surface_t *sign_surf;
6987 cairo_t *sign_cr;
6988
6989 cr = cairo_create(gui.surface);
6990
6991 bg_surf = cairo_surface_create_similar(gui.surface,
6992 cairo_surface_get_content(gui.surface),
6993 SIGN_WIDTH, SIGN_HEIGHT);
6994 bg_cr = cairo_create(bg_surf);
6995 cairo_set_source_rgba(bg_cr,
6996 gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue,
6997 gui.bgcolor->alpha);
6998 cairo_paint(bg_cr);
6999
7000 sign_surf = cairo_surface_create_similar(gui.surface,
7001 cairo_surface_get_content(gui.surface),
7002 SIGN_WIDTH, SIGN_HEIGHT);
7003 sign_cr = cairo_create(sign_surf);
7004 gdk_cairo_set_source_pixbuf(sign_cr, sign, -xoffset, -yoffset);
7005 cairo_paint(sign_cr);
7006
7007 cairo_set_operator(sign_cr, CAIRO_OPERATOR_DEST_OVER);
7008 cairo_set_source_surface(sign_cr, bg_surf, 0, 0);
7009 cairo_paint(sign_cr);
7010
7011 cairo_set_source_surface(cr, sign_surf, FILL_X(col), FILL_Y(row));
7012 cairo_paint(cr);
7013
7014 cairo_destroy(sign_cr);
7015 cairo_surface_destroy(sign_surf);
7016 cairo_destroy(bg_cr);
7017 cairo_surface_destroy(bg_surf);
7018 cairo_destroy(cr);
7019
7020 gtk_widget_queue_draw_area(gui.drawarea,
7021 FILL_X(col), FILL_Y(col), width, height);
7022
7023 }
7024 # else // !GTK_CHECK_VERSION(3,0,0)
7025 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
7026
7027 gdk_draw_rectangle(gui.drawarea->window,
7028 gui.text_gc,
7029 TRUE,
7030 FILL_X(col),
7031 FILL_Y(row),
7032 SIGN_WIDTH,
7033 SIGN_HEIGHT);
7034
7035 gdk_pixbuf_render_to_drawable_alpha(sign,
7036 gui.drawarea->window,
7037 MAX(0, xoffset),
7038 MAX(0, yoffset),
7039 FILL_X(col) - MIN(0, xoffset),
7040 FILL_Y(row) - MIN(0, yoffset),
7041 MIN(width, SIGN_WIDTH),
7042 MIN(height, SIGN_HEIGHT),
7043 GDK_PIXBUF_ALPHA_BILEVEL,
7044 127,
7045 GDK_RGB_DITHER_NORMAL,
7046 0, 0);
7047 # endif // !GTK_CHECK_VERSION(3,0,0)
7048 if (need_scale)
7049 g_object_unref(sign);
7050 }
7051 }
7052
7053 void *
gui_mch_register_sign(char_u * signfile)7054 gui_mch_register_sign(char_u *signfile)
7055 {
7056 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use)
7057 {
7058 GdkPixbuf *sign;
7059 GError *error = NULL;
7060 char_u *message;
7061
7062 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error);
7063
7064 if (error == NULL)
7065 return sign;
7066
7067 message = (char_u *)error->message;
7068
7069 if (message != NULL && input_conv.vc_type != CONV_NONE)
7070 message = string_convert(&input_conv, message, NULL);
7071
7072 if (message != NULL)
7073 {
7074 // The error message is already translated and will be more
7075 // descriptive than anything we could possibly do ourselves.
7076 semsg("E255: %s", message);
7077
7078 if (input_conv.vc_type != CONV_NONE)
7079 vim_free(message);
7080 }
7081 g_error_free(error);
7082 }
7083
7084 return NULL;
7085 }
7086
7087 void
gui_mch_destroy_sign(void * sign)7088 gui_mch_destroy_sign(void *sign)
7089 {
7090 if (sign != NULL)
7091 g_object_unref(sign);
7092 }
7093
7094 #endif // FEAT_SIGN_ICONS
7095