blob: 4bcdc50d193d4b94e74ee281fe414d31d95ef130 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
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 *
Bram Moolenaar0a56cb82005-01-04 21:45:14 +000013 * (C) 1998,1999,2000 by Marcin Dalecki <martin@dalecki.de>
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
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 <jason@peaceworks.ca>
21 * Daniel Elstner <daniel.elstner@gmx.net>
Bram Moolenaar98921892016-02-23 17:14:37 +010022 *
23 * Support for GTK+ 3 was added by:
24 *
25 * 2016 Kazunobu Kuriyama <kazunobu.kuriyama@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
27
28#include "vim.h"
Bram Moolenaar36e294c2015-12-29 18:55:46 +010029#ifdef USE_GRESOURCE
30#include "auto/gui_gtk_gresources.h"
31#endif
Bram Moolenaarc93e7912008-07-08 10:46:08 +000032
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef FEAT_GUI_GNOME
34/* Gnome redefines _() and N_(). Grrr... */
35# ifdef _
36# undef _
37# endif
Bram Moolenaar009c7b22017-01-09 20:30:27 +010038# ifdef ngettext
39# undef ngettext
40# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000041# ifdef N_
42# undef N_
43# endif
44# ifdef textdomain
45# undef textdomain
46# endif
47# ifdef bindtextdomain
48# undef bindtextdomain
49# endif
Bram Moolenaara2dd9002007-05-14 17:38:30 +000050# ifdef bind_textdomain_codeset
51# undef bind_textdomain_codeset
Bram Moolenaar49325942007-05-10 19:19:59 +000052# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000053# if defined(FEAT_GETTEXT) && !defined(ENABLE_NLS)
54# define ENABLE_NLS /* so the texts in the dialog boxes are translated */
55# endif
56# include <gnome.h>
57# include "version.h"
Bram Moolenaardb552d602006-03-23 22:59:57 +000058/* missing prototype in bonobo-dock-item.h */
59extern void bonobo_dock_item_set_behavior(BonoboDockItem *dock_item, BonoboDockItemBehavior beh);
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#endif
61
62#if !defined(FEAT_GUI_GTK) && defined(PROTO)
63/* When generating prototypes we don't want syntax errors. */
64# define GdkAtom int
65# define GdkEventExpose int
66# define GdkEventFocus int
67# define GdkEventVisibility int
68# define GdkEventProperty int
69# define GtkContainer int
70# define GtkTargetEntry int
71# define GtkType int
72# define GtkWidget int
73# define gint int
74# define gpointer int
75# define guint int
76# define GdkEventKey int
77# define GdkEventSelection int
78# define GtkSelectionData int
79# define GdkEventMotion int
80# define GdkEventButton int
81# define GdkDragContext int
82# define GdkEventConfigure int
83# define GdkEventClient int
84#else
Bram Moolenaar98921892016-02-23 17:14:37 +010085# if GTK_CHECK_VERSION(3,0,0)
86# include <gdk/gdkkeysyms-compat.h>
87# include <gtk/gtkx.h>
88# else
89# include <gdk/gdkkeysyms.h>
90# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000091# include <gdk/gdk.h>
92# ifdef WIN3264
93# include <gdk/gdkwin32.h>
94# else
95# include <gdk/gdkx.h>
96# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000097# include <gtk/gtk.h>
98# include "gui_gtk_f.h"
99#endif
100
101#ifdef HAVE_X11_SUNKEYSYM_H
102# include <X11/Sunkeysym.h>
103#endif
104
105/*
106 * Easy-to-use macro for multihead support.
107 */
108#ifdef HAVE_GTK_MULTIHEAD
109# define GET_X_ATOM(atom) gdk_x11_atom_to_xatom_for_display( \
110 gtk_widget_get_display(gui.mainwin), atom)
111#else
112# define GET_X_ATOM(atom) ((Atom)(atom))
113#endif
114
115/* Selection type distinguishers */
116enum
117{
118 TARGET_TYPE_NONE,
119 TARGET_UTF8_STRING,
120 TARGET_STRING,
121 TARGET_COMPOUND_TEXT,
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000122 TARGET_HTML,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123 TARGET_TEXT,
124 TARGET_TEXT_URI_LIST,
125 TARGET_TEXT_PLAIN,
126 TARGET_VIM,
127 TARGET_VIMENC
128};
129
130/*
131 * Table of selection targets supported by Vim.
132 * Note: Order matters, preferred types should come first.
133 */
134static const GtkTargetEntry selection_targets[] =
135{
136 {VIMENC_ATOM_NAME, 0, TARGET_VIMENC},
137 {VIM_ATOM_NAME, 0, TARGET_VIM},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000138 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 {"COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT},
141 {"TEXT", 0, TARGET_TEXT},
142 {"STRING", 0, TARGET_STRING}
143};
144#define N_SELECTION_TARGETS (sizeof(selection_targets) / sizeof(selection_targets[0]))
145
146#ifdef FEAT_DND
147/*
148 * Table of DnD targets supported by Vim.
149 * Note: Order matters, preferred types should come first.
150 */
151static const GtkTargetEntry dnd_targets[] =
152{
153 {"text/uri-list", 0, TARGET_TEXT_URI_LIST},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000154 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 {"STRING", 0, TARGET_STRING},
157 {"text/plain", 0, TARGET_TEXT_PLAIN}
158};
159# define N_DND_TARGETS (sizeof(dnd_targets) / sizeof(dnd_targets[0]))
160#endif
161
162
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163/*
164 * "Monospace" is a standard font alias that should be present
165 * on all proper Pango/fontconfig installations.
166 */
167# define DEFAULT_FONT "Monospace 10"
168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
170/*
171 * Atoms used to communicate save-yourself from the X11 session manager. There
172 * is no need to move them into the GUI struct, since they should be constant.
173 */
174static GdkAtom wm_protocols_atom = GDK_NONE;
175static GdkAtom save_yourself_atom = GDK_NONE;
176#endif
177
178/*
179 * Atoms used to control/reference X11 selections.
180 */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000181static GdkAtom html_atom = GDK_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182static GdkAtom utf8_string_atom = GDK_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183static GdkAtom vim_atom = GDK_NONE; /* Vim's own special selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184static GdkAtom vimenc_atom = GDK_NONE; /* Vim's extended selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
186/*
187 * Keycodes recognized by vim.
188 * NOTE: when changing this, the table in gui_x11.c probably needs the same
189 * change!
190 */
191static struct special_key
192{
193 guint key_sym;
194 char_u code0;
195 char_u code1;
196}
197const special_keys[] =
198{
199 {GDK_Up, 'k', 'u'},
200 {GDK_Down, 'k', 'd'},
201 {GDK_Left, 'k', 'l'},
202 {GDK_Right, 'k', 'r'},
203 {GDK_F1, 'k', '1'},
204 {GDK_F2, 'k', '2'},
205 {GDK_F3, 'k', '3'},
206 {GDK_F4, 'k', '4'},
207 {GDK_F5, 'k', '5'},
208 {GDK_F6, 'k', '6'},
209 {GDK_F7, 'k', '7'},
210 {GDK_F8, 'k', '8'},
211 {GDK_F9, 'k', '9'},
212 {GDK_F10, 'k', ';'},
213 {GDK_F11, 'F', '1'},
214 {GDK_F12, 'F', '2'},
215 {GDK_F13, 'F', '3'},
216 {GDK_F14, 'F', '4'},
217 {GDK_F15, 'F', '5'},
218 {GDK_F16, 'F', '6'},
219 {GDK_F17, 'F', '7'},
220 {GDK_F18, 'F', '8'},
221 {GDK_F19, 'F', '9'},
222 {GDK_F20, 'F', 'A'},
223 {GDK_F21, 'F', 'B'},
224 {GDK_Pause, 'F', 'B'}, /* Pause == F21 according to netbeans.txt */
225 {GDK_F22, 'F', 'C'},
226 {GDK_F23, 'F', 'D'},
227 {GDK_F24, 'F', 'E'},
228 {GDK_F25, 'F', 'F'},
229 {GDK_F26, 'F', 'G'},
230 {GDK_F27, 'F', 'H'},
231 {GDK_F28, 'F', 'I'},
232 {GDK_F29, 'F', 'J'},
233 {GDK_F30, 'F', 'K'},
234 {GDK_F31, 'F', 'L'},
235 {GDK_F32, 'F', 'M'},
236 {GDK_F33, 'F', 'N'},
237 {GDK_F34, 'F', 'O'},
238 {GDK_F35, 'F', 'P'},
239#ifdef SunXK_F36
240 {SunXK_F36, 'F', 'Q'},
241 {SunXK_F37, 'F', 'R'},
242#endif
243 {GDK_Help, '%', '1'},
244 {GDK_Undo, '&', '8'},
245 {GDK_BackSpace, 'k', 'b'},
246 {GDK_Insert, 'k', 'I'},
247 {GDK_Delete, 'k', 'D'},
248 {GDK_3270_BackTab, 'k', 'B'},
249 {GDK_Clear, 'k', 'C'},
250 {GDK_Home, 'k', 'h'},
251 {GDK_End, '@', '7'},
252 {GDK_Prior, 'k', 'P'},
253 {GDK_Next, 'k', 'N'},
254 {GDK_Print, '%', '9'},
255 /* Keypad keys: */
256 {GDK_KP_Left, 'k', 'l'},
257 {GDK_KP_Right, 'k', 'r'},
258 {GDK_KP_Up, 'k', 'u'},
259 {GDK_KP_Down, 'k', 'd'},
260 {GDK_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
261 {GDK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
262 {GDK_KP_Home, 'K', '1'},
263 {GDK_KP_End, 'K', '4'},
264 {GDK_KP_Prior, 'K', '3'}, /* page up */
265 {GDK_KP_Next, 'K', '5'}, /* page down */
266
267 {GDK_KP_Add, 'K', '6'},
268 {GDK_KP_Subtract, 'K', '7'},
269 {GDK_KP_Divide, 'K', '8'},
270 {GDK_KP_Multiply, 'K', '9'},
271 {GDK_KP_Enter, 'K', 'A'},
272 {GDK_KP_Decimal, 'K', 'B'},
273
274 {GDK_KP_0, 'K', 'C'},
275 {GDK_KP_1, 'K', 'D'},
276 {GDK_KP_2, 'K', 'E'},
277 {GDK_KP_3, 'K', 'F'},
278 {GDK_KP_4, 'K', 'G'},
279 {GDK_KP_5, 'K', 'H'},
280 {GDK_KP_6, 'K', 'I'},
281 {GDK_KP_7, 'K', 'J'},
282 {GDK_KP_8, 'K', 'K'},
283 {GDK_KP_9, 'K', 'L'},
284
285 /* End of list marker: */
286 {0, 0, 0}
287};
288
289/*
290 * Flags for command line options table below.
291 */
292#define ARG_FONT 1
293#define ARG_GEOMETRY 2
294#define ARG_REVERSE 3
295#define ARG_NOREVERSE 4
296#define ARG_BACKGROUND 5
297#define ARG_FOREGROUND 6
298#define ARG_ICONIC 7
299#define ARG_ROLE 8
300#define ARG_NETBEANS 9
301#define ARG_XRM 10 /* ignored */
302#define ARG_MENUFONT 11 /* ignored */
303#define ARG_INDEX_MASK 0x00ff
304#define ARG_HAS_VALUE 0x0100 /* a value is expected after the argument */
305#define ARG_NEEDS_GUI 0x0200 /* need to initialize the GUI for this */
306#define ARG_FOR_GTK 0x0400 /* argument is handled by GTK+ or GNOME */
307#define ARG_COMPAT_LONG 0x0800 /* accept -foo but substitute with --foo */
308#define ARG_KEEP 0x1000 /* don't remove argument from argv[] */
309
310/*
311 * This table holds all the X GUI command line options allowed. This includes
312 * the standard ones so that we can skip them when Vim is started without the
313 * GUI (but the GUI might start up later).
314 *
315 * When changing this, also update doc/gui_x11.txt and the usage message!!!
316 */
317typedef struct
318{
319 const char *name;
320 unsigned int flags;
321}
322cmdline_option_T;
323
324static const cmdline_option_T cmdline_options[] =
325{
326 /* We handle these options ourselves */
327 {"-fn", ARG_FONT|ARG_HAS_VALUE},
328 {"-font", ARG_FONT|ARG_HAS_VALUE},
329 {"-geom", ARG_GEOMETRY|ARG_HAS_VALUE},
330 {"-geometry", ARG_GEOMETRY|ARG_HAS_VALUE},
331 {"-rv", ARG_REVERSE},
332 {"-reverse", ARG_REVERSE},
333 {"+rv", ARG_NOREVERSE},
334 {"+reverse", ARG_NOREVERSE},
335 {"-bg", ARG_BACKGROUND|ARG_HAS_VALUE},
336 {"-background", ARG_BACKGROUND|ARG_HAS_VALUE},
337 {"-fg", ARG_FOREGROUND|ARG_HAS_VALUE},
338 {"-foreground", ARG_FOREGROUND|ARG_HAS_VALUE},
339 {"-iconic", ARG_ICONIC},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 {"--role", ARG_ROLE|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#ifdef FEAT_NETBEANS_INTG
342 {"-nb", ARG_NETBEANS}, /* non-standard value format */
343 {"-xrm", ARG_XRM|ARG_HAS_VALUE}, /* not implemented */
344 {"-mf", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
345 {"-menufont", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 /* Arguments handled by GTK (and GNOME) internally. */
348 {"--g-fatal-warnings", ARG_FOR_GTK},
349 {"--gdk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
350 {"--gdk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
351 {"--gtk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
352 {"--gtk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
353 {"--gtk-module", ARG_FOR_GTK|ARG_HAS_VALUE},
354 {"--sync", ARG_FOR_GTK},
355 {"--display", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
356 {"--name", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
357 {"--class", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 {"--screen", ARG_FOR_GTK|ARG_HAS_VALUE},
359 {"--gxid-host", ARG_FOR_GTK|ARG_HAS_VALUE},
360 {"--gxid-port", ARG_FOR_GTK|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361#ifdef FEAT_GUI_GNOME
362 {"--load-modules", ARG_FOR_GTK|ARG_HAS_VALUE},
363 {"--sm-client-id", ARG_FOR_GTK|ARG_HAS_VALUE},
364 {"--sm-config-prefix", ARG_FOR_GTK|ARG_HAS_VALUE},
365 {"--sm-disable", ARG_FOR_GTK},
366 {"--oaf-ior-fd", ARG_FOR_GTK|ARG_HAS_VALUE},
367 {"--oaf-activate-iid", ARG_FOR_GTK|ARG_HAS_VALUE},
368 {"--oaf-private", ARG_FOR_GTK},
369 {"--enable-sound", ARG_FOR_GTK},
370 {"--disable-sound", ARG_FOR_GTK},
371 {"--espeaker", ARG_FOR_GTK|ARG_HAS_VALUE},
372 {"-?", ARG_FOR_GTK|ARG_NEEDS_GUI},
373 {"--help", ARG_FOR_GTK|ARG_NEEDS_GUI|ARG_KEEP},
374 {"--usage", ARG_FOR_GTK|ARG_NEEDS_GUI},
375# if 0 /* conflicts with Vim's own --version argument */
376 {"--version", ARG_FOR_GTK|ARG_NEEDS_GUI},
377# endif
378 {"--disable-crash-dialog", ARG_FOR_GTK},
379#endif
380 {NULL, 0}
381};
382
383static int gui_argc = 0;
384static char **gui_argv = NULL;
385
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386static const char *role_argument = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
388static const char *restart_command = NULL;
Bram Moolenaar9085f802009-06-03 14:20:21 +0000389static char *abs_restart_command = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390#endif
391static int found_iconic_arg = FALSE;
392
393#ifdef FEAT_GUI_GNOME
394/*
395 * Can't use Gnome if --socketid given
396 */
397static int using_gnome = 0;
398#else
399# define using_gnome 0
400#endif
401
402/*
403 * Parse the GUI related command-line arguments. Any arguments used are
404 * deleted from argv, and *argc is decremented accordingly. This is called
405 * when vim is started, whether or not the GUI has been started.
406 */
407 void
408gui_mch_prepare(int *argc, char **argv)
409{
410 const cmdline_option_T *option;
411 int i = 0;
412 int len = 0;
413
414#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
415 /*
416 * Determine the command used to invoke Vim, to be passed as restart
417 * command to the session manager. If argv[0] contains any directory
418 * components try building an absolute path, otherwise leave it as is.
419 */
420 restart_command = argv[0];
421
422 if (strchr(argv[0], G_DIR_SEPARATOR) != NULL)
423 {
424 char_u buf[MAXPATHL];
425
426 if (mch_FullName((char_u *)argv[0], buf, (int)sizeof(buf), TRUE) == OK)
Bram Moolenaar9085f802009-06-03 14:20:21 +0000427 {
428 abs_restart_command = (char *)vim_strsave(buf);
429 restart_command = abs_restart_command;
430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 }
432#endif
433
434 /*
435 * Move all the entries in argv which are relevant to GTK+ and GNOME
436 * into gui_argv. Freed later in gui_mch_init().
437 */
438 gui_argc = 0;
439 gui_argv = (char **)alloc((unsigned)((*argc + 1) * sizeof(char *)));
440
441 g_return_if_fail(gui_argv != NULL);
442
443 gui_argv[gui_argc++] = argv[i++];
444
445 while (i < *argc)
446 {
447 /* Don't waste CPU cycles on non-option arguments. */
448 if (argv[i][0] != '-' && argv[i][0] != '+')
449 {
450 ++i;
451 continue;
452 }
453
454 /* Look for argv[i] in cmdline_options[] table. */
455 for (option = &cmdline_options[0]; option->name != NULL; ++option)
456 {
457 len = strlen(option->name);
458
459 if (strncmp(argv[i], option->name, len) == 0)
460 {
461 if (argv[i][len] == '\0')
462 break;
463 /* allow --foo=bar style */
464 if (argv[i][len] == '=' && (option->flags & ARG_HAS_VALUE))
465 break;
466#ifdef FEAT_NETBEANS_INTG
467 /* darn, -nb has non-standard syntax */
468 if (vim_strchr((char_u *)":=", argv[i][len]) != NULL
469 && (option->flags & ARG_INDEX_MASK) == ARG_NETBEANS)
470 break;
471#endif
472 }
473 else if ((option->flags & ARG_COMPAT_LONG)
474 && strcmp(argv[i], option->name + 1) == 0)
475 {
476 /* Replace the standard X arguments "-name" and "-display"
477 * with their GNU-style long option counterparts. */
478 argv[i] = (char *)option->name;
479 break;
480 }
481 }
482 if (option->name == NULL) /* no match */
483 {
484 ++i;
485 continue;
486 }
487
488 if (option->flags & ARG_FOR_GTK)
489 {
490 /* Move the argument into gui_argv, which
491 * will later be passed to gtk_init_check() */
492 gui_argv[gui_argc++] = argv[i];
493 }
494 else
495 {
496 char *value = NULL;
497
498 /* Extract the option's value if there is one.
499 * Accept both "--foo bar" and "--foo=bar" style. */
500 if (option->flags & ARG_HAS_VALUE)
501 {
502 if (argv[i][len] == '=')
503 value = &argv[i][len + 1];
504 else if (i + 1 < *argc && strcmp(argv[i + 1], "--") != 0)
505 value = argv[i + 1];
506 }
507
508 /* Check for options handled by Vim itself */
509 switch (option->flags & ARG_INDEX_MASK)
510 {
511 case ARG_REVERSE:
512 found_reverse_arg = TRUE;
513 break;
514 case ARG_NOREVERSE:
515 found_reverse_arg = FALSE;
516 break;
517 case ARG_FONT:
518 font_argument = value;
519 break;
520 case ARG_GEOMETRY:
521 if (value != NULL)
522 gui.geom = vim_strsave((char_u *)value);
523 break;
524 case ARG_BACKGROUND:
525 background_argument = value;
526 break;
527 case ARG_FOREGROUND:
528 foreground_argument = value;
529 break;
530 case ARG_ICONIC:
531 found_iconic_arg = TRUE;
532 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 case ARG_ROLE:
534 role_argument = value; /* used later in gui_mch_open() */
535 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536#ifdef FEAT_NETBEANS_INTG
537 case ARG_NETBEANS:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 gui.dofork = FALSE; /* don't fork() when starting GUI */
539 netbeansArg = argv[i];
540 break;
541#endif
542 default:
543 break;
544 }
545 }
546
547 /* These arguments make gnome_program_init() print a message and exit.
Bram Moolenaar717e1962016-08-10 21:28:44 +0200548 * Must start the GUI for this, otherwise ":gui" will exit later!
549 * Only when the GUI can start. */
550 if ((option->flags & ARG_NEEDS_GUI)
551 && gui_mch_early_init_check(FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 gui.starting = TRUE;
553
554 if (option->flags & ARG_KEEP)
555 ++i;
556 else
557 {
558 /* Remove the flag from the argument vector. */
559 if (--*argc > i)
560 {
561 int n_strip = 1;
562
563 /* Move the argument's value as well, if there is one. */
564 if ((option->flags & ARG_HAS_VALUE)
565 && argv[i][len] != '='
566 && strcmp(argv[i + 1], "--") != 0)
567 {
568 ++n_strip;
569 --*argc;
570 if (option->flags & ARG_FOR_GTK)
571 gui_argv[gui_argc++] = argv[i + 1];
572 }
573
574 if (*argc > i)
575 mch_memmove(&argv[i], &argv[i + n_strip],
576 (*argc - i) * sizeof(char *));
577 }
578 argv[*argc] = NULL;
579 }
580 }
581
582 gui_argv[gui_argc] = NULL;
583}
584
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000585#if defined(EXITFREE) || defined(PROTO)
586 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100587gui_mch_free_all(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000588{
589 vim_free(gui_argv);
Bram Moolenaar9085f802009-06-03 14:20:21 +0000590#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
591 vim_free(abs_restart_command);
592#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000593}
594#endif
595
Bram Moolenaar98921892016-02-23 17:14:37 +0100596#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597/*
598 * This should be maybe completely removed.
599 * Doesn't seem possible, since check_copy_area() relies on
600 * this information. --danielk
601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000603visibility_event(GtkWidget *widget UNUSED,
604 GdkEventVisibility *event,
605 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606{
607 gui.visibility = event->state;
608 /*
609 * When we do an gdk_window_copy_area(), and the window is partially
610 * obscured, we want to receive an event to tell us whether it worked
611 * or not.
612 */
613 if (gui.text_gc != NULL)
614 gdk_gc_set_exposures(gui.text_gc,
615 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
616 return FALSE;
617}
Bram Moolenaar98921892016-02-23 17:14:37 +0100618#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619
620/*
621 * Redraw the corresponding portions of the screen.
622 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100623#if GTK_CHECK_VERSION(3,0,0)
624static gboolean is_key_pressed = FALSE;
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100625static gboolean blink_mode = TRUE;
Bram Moolenaar98921892016-02-23 17:14:37 +0100626
627static gboolean gui_gtk_is_blink_on(void);
Bram Moolenaar98921892016-02-23 17:14:37 +0100628static void gui_gtk_window_clear(GdkWindow *win);
629
630 static void
631gui_gtk3_redraw(int x, int y, int width, int height)
632{
Bram Moolenaarfe344a92017-02-23 12:20:35 +0100633 /* Range checks are left to gui_redraw_block() */
Bram Moolenaar98921892016-02-23 17:14:37 +0100634 gui_redraw_block(Y_2_ROW(y), X_2_COL(x),
635 Y_2_ROW(y + height - 1), X_2_COL(x + width - 1),
636 GUI_MON_NOCLEAR);
637}
638
639 static void
640gui_gtk3_update_cursor(cairo_t *cr)
641{
642 if (gui.row == gui.cursor_row)
643 {
644 gui.by_signal = TRUE;
Bram Moolenaar4fc563b2016-03-12 12:40:58 +0100645 if (State & CMDLINE)
646 gui_update_cursor(TRUE, FALSE);
647 else
648 gui_update_cursor(TRUE, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +0100649 gui.by_signal = FALSE;
650 cairo_paint(cr);
651 }
652}
653
654 static gboolean
655gui_gtk3_should_draw_cursor(void)
656{
657 unsigned int cond = 0;
658 cond |= gui_gtk_is_blink_on();
Bram Moolenaarb4ebf9a2016-03-12 16:28:18 +0100659 if (gui.cursor_col >= gui.col)
660 cond |= is_key_pressed;
Bram Moolenaar98921892016-02-23 17:14:37 +0100661 cond |= gui.in_focus == FALSE;
Bram Moolenaar98921892016-02-23 17:14:37 +0100662 return cond;
663}
664
665 static gboolean
Bram Moolenaar8e31fd52016-06-04 22:18:13 +0200666draw_event(GtkWidget *widget UNUSED,
Bram Moolenaar98921892016-02-23 17:14:37 +0100667 cairo_t *cr,
668 gpointer user_data UNUSED)
669{
670 /* Skip this when the GUI isn't set up yet, will redraw later. */
671 if (gui.starting)
672 return FALSE;
673
674 out_flush(); /* make sure all output has been processed */
675 /* for GTK+ 3, may induce other draw events. */
676
677 cairo_set_source_surface(cr, gui.surface, 0, 0);
678
679 /* Draw the window without the cursor. */
680 gui.by_signal = TRUE;
681 {
682 cairo_rectangle_list_t *list = NULL;
683
Bram Moolenaar98921892016-02-23 17:14:37 +0100684 list = cairo_copy_clip_rectangle_list(cr);
685 if (list->status != CAIRO_STATUS_CLIP_NOT_REPRESENTABLE)
686 {
687 int i;
Bram Moolenaarfe344a92017-02-23 12:20:35 +0100688
689 /* First clear all the blocks and then redraw them. Just in case
690 * some blocks overlap. */
Bram Moolenaar98921892016-02-23 17:14:37 +0100691 for (i = 0; i < list->num_rectangles; i++)
692 {
693 const cairo_rectangle_t rect = list->rectangles[i];
Bram Moolenaar8e31fd52016-06-04 22:18:13 +0200694
Bram Moolenaarfe344a92017-02-23 12:20:35 +0100695 gui_mch_clear_block(Y_2_ROW((int)rect.y), 0,
696 Y_2_ROW((int)(rect.y + rect.height)) - 1, Columns - 1);
697 }
698
699 for (i = 0; i < list->num_rectangles; i++)
700 {
701 const cairo_rectangle_t rect = list->rectangles[i];
Bram Moolenaar8e31fd52016-06-04 22:18:13 +0200702
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100703 if (blink_mode)
704 gui_gtk3_redraw(rect.x, rect.y, rect.width, rect.height);
705 else
Bram Moolenaarb4ebf9a2016-03-12 16:28:18 +0100706 {
707 if (get_real_state() & VISUAL)
708 gui_gtk3_redraw(rect.x, rect.y,
709 rect.width, rect.height);
710 else
711 gui_redraw(rect.x, rect.y, rect.width, rect.height);
712 }
Bram Moolenaar98921892016-02-23 17:14:37 +0100713 }
714 }
715 cairo_rectangle_list_destroy(list);
716
Bram Moolenaarb4ebf9a2016-03-12 16:28:18 +0100717 if (get_real_state() & VISUAL)
718 {
719 if (gui.cursor_row == gui.row && gui.cursor_col >= gui.col)
720 gui_update_cursor(TRUE, TRUE);
721 }
722
Bram Moolenaar98921892016-02-23 17:14:37 +0100723 cairo_paint(cr);
724 }
725 gui.by_signal = FALSE;
726
727 /* Add the cursor to the window if necessary.*/
Bram Moolenaarb4ebf9a2016-03-12 16:28:18 +0100728 if (gui_gtk3_should_draw_cursor() && blink_mode)
Bram Moolenaar98921892016-02-23 17:14:37 +0100729 gui_gtk3_update_cursor(cr);
730
731 return FALSE;
732}
733#else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000735expose_event(GtkWidget *widget UNUSED,
736 GdkEventExpose *event,
737 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738{
739 /* Skip this when the GUI isn't set up yet, will redraw later. */
740 if (gui.starting)
741 return FALSE;
742
743 out_flush(); /* make sure all output has been processed */
744 gui_redraw(event->area.x, event->area.y,
745 event->area.width, event->area.height);
746
747 /* Clear the border areas if needed */
748 if (event->area.x < FILL_X(0))
749 gdk_window_clear_area(gui.drawarea->window, 0, 0, FILL_X(0), 0);
750 if (event->area.y < FILL_Y(0))
751 gdk_window_clear_area(gui.drawarea->window, 0, 0, 0, FILL_Y(0));
752 if (event->area.x > FILL_X(Columns))
753 gdk_window_clear_area(gui.drawarea->window,
754 FILL_X((int)Columns), 0, 0, 0);
755 if (event->area.y > FILL_Y(Rows))
756 gdk_window_clear_area(gui.drawarea->window, 0, FILL_Y((int)Rows), 0, 0);
757
758 return FALSE;
759}
Bram Moolenaar98921892016-02-23 17:14:37 +0100760#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761
762#ifdef FEAT_CLIENTSERVER
763/*
764 * Handle changes to the "Comm" property
765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000767property_event(GtkWidget *widget,
768 GdkEventProperty *event,
769 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770{
771 if (event->type == GDK_PROPERTY_NOTIFY
772 && event->state == (int)GDK_PROPERTY_NEW_VALUE
Bram Moolenaar98921892016-02-23 17:14:37 +0100773# if GTK_CHECK_VERSION(3,0,0)
774 && GDK_WINDOW_XID(event->window) == commWindow
775# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 && GDK_WINDOW_XWINDOW(event->window) == commWindow
Bram Moolenaar98921892016-02-23 17:14:37 +0100777# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 && GET_X_ATOM(event->atom) == commProperty)
779 {
780 XEvent xev;
781
782 /* Translate to XLib */
783 xev.xproperty.type = PropertyNotify;
784 xev.xproperty.atom = commProperty;
785 xev.xproperty.window = commWindow;
786 xev.xproperty.state = PropertyNewValue;
Bram Moolenaar98921892016-02-23 17:14:37 +0100787# if GTK_CHECK_VERSION(3,0,0)
788 serverEventProc(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(widget)),
789 &xev, 0);
790# else
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200791 serverEventProc(GDK_WINDOW_XDISPLAY(widget->window), &xev, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +0100792# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 }
794 return FALSE;
795}
Bram Moolenaar98921892016-02-23 17:14:37 +0100796#endif /* defined(FEAT_CLIENTSERVER) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797
798
799/****************************************************************************
800 * Focus handlers:
801 */
802
803
804/*
805 * This is a simple state machine:
806 * BLINK_NONE not blinking at all
807 * BLINK_OFF blinking, cursor is not shown
808 * BLINK_ON blinking, cursor is shown
809 */
810
811#define BLINK_NONE 0
812#define BLINK_OFF 1
813#define BLINK_ON 2
814
815static int blink_state = BLINK_NONE;
816static long_u blink_waittime = 700;
817static long_u blink_ontime = 400;
818static long_u blink_offtime = 250;
819static guint blink_timer = 0;
820
Bram Moolenaar98921892016-02-23 17:14:37 +0100821#if GTK_CHECK_VERSION(3,0,0)
822 static gboolean
823gui_gtk_is_blink_on(void)
824{
825 return blink_state == BLINK_ON;
826}
Bram Moolenaar98921892016-02-23 17:14:37 +0100827#endif
828
Bram Moolenaar703a8042016-06-04 16:24:32 +0200829 int
830gui_mch_is_blinking(void)
831{
832 return blink_state != BLINK_NONE;
833}
834
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200835 int
836gui_mch_is_blink_off(void)
837{
838 return blink_state == BLINK_OFF;
839}
840
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 void
842gui_mch_set_blinking(long waittime, long on, long off)
843{
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100844#if GTK_CHECK_VERSION(3,0,0)
845 if (waittime == 0 || on == 0 || off == 0)
846 {
847 blink_mode = FALSE;
848
849 blink_waittime = 700;
850 blink_ontime = 400;
851 blink_offtime = 250;
852 }
853 else
854 {
855 blink_mode = TRUE;
856
857 blink_waittime = waittime;
858 blink_ontime = on;
859 blink_offtime = off;
860 }
861#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 blink_waittime = waittime;
863 blink_ontime = on;
864 blink_offtime = off;
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866}
867
868/*
869 * Stop the cursor blinking. Show the cursor if it wasn't shown.
870 */
871 void
872gui_mch_stop_blink(void)
873{
874 if (blink_timer)
875 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100876#if GTK_CHECK_VERSION(3,0,0)
877 g_source_remove(blink_timer);
878#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 gtk_timeout_remove(blink_timer);
Bram Moolenaar98921892016-02-23 17:14:37 +0100880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 blink_timer = 0;
882 }
883 if (blink_state == BLINK_OFF)
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200884 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200886 gui_mch_flush();
887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 blink_state = BLINK_NONE;
889}
890
Bram Moolenaar98921892016-02-23 17:14:37 +0100891#if GTK_CHECK_VERSION(3,0,0)
892 static gboolean
893#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +0100895#endif
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000896blink_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897{
898 if (blink_state == BLINK_ON)
899 {
900 gui_undraw_cursor();
901 blink_state = BLINK_OFF;
Bram Moolenaar98921892016-02-23 17:14:37 +0100902#if GTK_CHECK_VERSION(3,0,0)
903 blink_timer = g_timeout_add((guint)blink_offtime,
904 (GSourceFunc) blink_cb, NULL);
905#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 blink_timer = gtk_timeout_add((guint32)blink_offtime,
907 (GtkFunction) blink_cb, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +0100908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 }
910 else
911 {
912 gui_update_cursor(TRUE, FALSE);
913 blink_state = BLINK_ON;
Bram Moolenaar98921892016-02-23 17:14:37 +0100914#if GTK_CHECK_VERSION(3,0,0)
915 blink_timer = g_timeout_add((guint)blink_ontime,
916 (GSourceFunc) blink_cb, NULL);
917#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 blink_timer = gtk_timeout_add((guint32)blink_ontime,
919 (GtkFunction) blink_cb, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +0100920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 }
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200922 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923
924 return FALSE; /* don't happen again */
925}
926
927/*
928 * Start the cursor blinking. If it was already blinking, this restarts the
929 * waiting time and shows the cursor.
930 */
931 void
932gui_mch_start_blink(void)
933{
934 if (blink_timer)
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +0200935 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100936#if GTK_CHECK_VERSION(3,0,0)
937 g_source_remove(blink_timer);
938#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 gtk_timeout_remove(blink_timer);
Bram Moolenaar98921892016-02-23 17:14:37 +0100940#endif
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +0200941 blink_timer = 0;
942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 /* Only switch blinking on if none of the times is zero */
944 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
945 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100946#if GTK_CHECK_VERSION(3,0,0)
947 blink_timer = g_timeout_add((guint)blink_waittime,
948 (GSourceFunc) blink_cb, NULL);
949#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 blink_timer = gtk_timeout_add((guint32)blink_waittime,
951 (GtkFunction) blink_cb, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +0100952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 blink_state = BLINK_ON;
954 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200955 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 }
957}
958
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000960enter_notify_event(GtkWidget *widget UNUSED,
961 GdkEventCrossing *event UNUSED,
962 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963{
964 if (blink_state == BLINK_NONE)
965 gui_mch_start_blink();
966
967 /* make sure keyboard input goes there */
Bram Moolenaar98921892016-02-23 17:14:37 +0100968#if GTK_CHECK_VERSION(3,0,0)
969 if (gtk_socket_id == 0 || !gtk_widget_has_focus(gui.drawarea))
970#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 if (gtk_socket_id == 0 || !GTK_WIDGET_HAS_FOCUS(gui.drawarea))
Bram Moolenaar98921892016-02-23 17:14:37 +0100972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 gtk_widget_grab_focus(gui.drawarea);
974
975 return FALSE;
976}
977
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000979leave_notify_event(GtkWidget *widget UNUSED,
980 GdkEventCrossing *event UNUSED,
981 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982{
983 if (blink_state != BLINK_NONE)
984 gui_mch_stop_blink();
985
986 return FALSE;
987}
988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000990focus_in_event(GtkWidget *widget,
991 GdkEventFocus *event UNUSED,
992 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993{
994 gui_focus_change(TRUE);
995
996 if (blink_state == BLINK_NONE)
997 gui_mch_start_blink();
998
Bram Moolenaar9c8791f2007-09-05 19:47:23 +0000999 /* make sure keyboard input goes to the draw area (if this is focus for a
1000 * window) */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001001 if (widget != gui.drawarea)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001002 gtk_widget_grab_focus(gui.drawarea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
1004 return TRUE;
1005}
1006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001008focus_out_event(GtkWidget *widget UNUSED,
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001009 GdkEventFocus *event UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001010 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011{
1012 gui_focus_change(FALSE);
1013
1014 if (blink_state != BLINK_NONE)
1015 gui_mch_stop_blink();
1016
1017 return TRUE;
1018}
1019
1020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021/*
1022 * Translate a GDK key value to UTF-8 independently of the current locale.
1023 * The output is written to string, which must have room for at least 6 bytes
1024 * plus the NUL terminator. Returns the length in bytes.
1025 *
1026 * This function is used in the GTK+ 2 GUI only. The GTK+ 1 code makes use
1027 * of GdkEventKey::string instead. But event->string is evil; see here why:
1028 * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey
1029 */
1030 static int
1031keyval_to_string(unsigned int keyval, unsigned int state, char_u *string)
1032{
1033 int len;
1034 guint32 uc;
1035
1036 uc = gdk_keyval_to_unicode(keyval);
1037 if (uc != 0)
1038 {
1039 /* Check for CTRL-foo */
1040 if ((state & GDK_CONTROL_MASK) && uc >= 0x20 && uc < 0x80)
1041 {
1042 /* These mappings look arbitrary at the first glance, but in fact
1043 * resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1044 * machine. The only difference is BS vs. DEL for CTRL-8 (makes
1045 * more sense and is consistent with usual terminal behaviour). */
1046 if (uc >= '@')
1047 string[0] = uc & 0x1F;
1048 else if (uc == '2')
1049 string[0] = NUL;
1050 else if (uc >= '3' && uc <= '7')
1051 string[0] = uc ^ 0x28;
1052 else if (uc == '8')
1053 string[0] = BS;
1054 else if (uc == '?')
1055 string[0] = DEL;
1056 else
1057 string[0] = uc;
1058 len = 1;
1059 }
1060 else
1061 {
1062 /* Translate a normal key to UTF-8. This doesn't work for dead
1063 * keys of course, you _have_ to use an input method for that. */
1064 len = utf_char2bytes((int)uc, string);
1065 }
1066 }
1067 else
1068 {
1069 /* Translate keys which are represented by ASCII control codes in Vim.
1070 * There are only a few of those; most control keys are translated to
1071 * special terminal-like control sequences. */
1072 len = 1;
1073 switch (keyval)
1074 {
1075 case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab:
1076 string[0] = TAB;
1077 break;
1078 case GDK_Linefeed:
1079 string[0] = NL;
1080 break;
1081 case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter:
1082 string[0] = CAR;
1083 break;
1084 case GDK_Escape:
1085 string[0] = ESC;
1086 break;
1087 default:
1088 len = 0;
1089 break;
1090 }
1091 }
1092 string[len] = NUL;
1093
1094 return len;
1095}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001097 static int
1098modifiers_gdk2vim(guint state)
1099{
1100 int modifiers = 0;
1101
1102 if (state & GDK_SHIFT_MASK)
1103 modifiers |= MOD_MASK_SHIFT;
1104 if (state & GDK_CONTROL_MASK)
1105 modifiers |= MOD_MASK_CTRL;
1106 if (state & GDK_MOD1_MASK)
1107 modifiers |= MOD_MASK_ALT;
Bram Moolenaar580061a2010-08-13 13:57:13 +02001108#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001109 if (state & GDK_SUPER_MASK)
1110 modifiers |= MOD_MASK_META;
1111#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001112 if (state & GDK_MOD4_MASK)
1113 modifiers |= MOD_MASK_META;
1114
1115 return modifiers;
1116}
1117
1118 static int
1119modifiers_gdk2mouse(guint state)
1120{
1121 int modifiers = 0;
1122
1123 if (state & GDK_SHIFT_MASK)
1124 modifiers |= MOUSE_SHIFT;
1125 if (state & GDK_CONTROL_MASK)
1126 modifiers |= MOUSE_CTRL;
1127 if (state & GDK_MOD1_MASK)
1128 modifiers |= MOUSE_ALT;
1129
1130 return modifiers;
1131}
1132
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133/*
1134 * Main keyboard handler:
1135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001137key_press_event(GtkWidget *widget UNUSED,
1138 GdkEventKey *event,
1139 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140{
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001141 /* For GTK+ 2 we know for sure how large the string might get.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 * (That is, up to 6 bytes + NUL + CSI escapes + safety measure.) */
1143 char_u string[32], string2[32];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 guint key_sym;
1145 int len;
1146 int i;
1147 int modifiers;
1148 int key;
1149 guint state;
1150 char_u *s, *d;
1151
Bram Moolenaar98921892016-02-23 17:14:37 +01001152#if GTK_CHECK_VERSION(3,0,0)
1153 is_key_pressed = TRUE;
1154 gui_mch_stop_blink();
1155#endif
1156
Bram Moolenaar20892c12011-06-26 04:49:00 +02001157 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 key_sym = event->keyval;
1159 state = event->state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160
1161#ifdef FEAT_XIM
1162 if (xim_queue_key_press_event(event, TRUE))
1163 return TRUE;
1164#endif
1165
1166#ifdef FEAT_HANGULIN
1167 if (key_sym == GDK_space && (state & GDK_SHIFT_MASK))
1168 {
1169 hangul_input_state_toggle();
1170 return TRUE;
1171 }
1172#endif
1173
1174#ifdef SunXK_F36
1175 /*
1176 * These keys have bogus lookup strings, and trapping them here is
1177 * easier than trying to XRebindKeysym() on them with every possible
1178 * combination of modifiers.
1179 */
1180 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
1181 len = 0;
1182 else
1183#endif
1184 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 len = keyval_to_string(key_sym, state, string2);
1186
1187 /* Careful: convert_input() doesn't handle the NUL character.
1188 * No need to convert pure ASCII anyway, thus the len > 1 check. */
1189 if (len > 1 && input_conv.vc_type != CONV_NONE)
1190 len = convert_input(string2, len, sizeof(string2));
1191
1192 s = string2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 d = string;
1194 for (i = 0; i < len; ++i)
1195 {
1196 *d++ = s[i];
1197 if (d[-1] == CSI && d + 2 < string + sizeof(string))
1198 {
1199 /* Turn CSI into K_CSI. */
1200 *d++ = KS_EXTRA;
1201 *d++ = (int)KE_CSI;
1202 }
1203 }
1204 len = d - string;
1205 }
1206
1207 /* Shift-Tab results in Left_Tab, but we want <S-Tab> */
1208 if (key_sym == GDK_ISO_Left_Tab)
1209 {
1210 key_sym = GDK_Tab;
1211 state |= GDK_SHIFT_MASK;
1212 }
1213
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214#ifdef FEAT_MENU
1215 /* If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key
1216 * is a menu shortcut, we ignore everything with the ALT modifier. */
1217 if ((state & GDK_MOD1_MASK)
1218 && gui.menu_is_active
1219 && (*p_wak == 'y'
1220 || (*p_wak == 'm'
1221 && len == 1
1222 && gui_is_menu_shortcut(string[0]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 /* For GTK2 we return false to signify that we haven't handled the
1224 * keypress, so that gtk will handle the mnemonic or accelerator. */
1225 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226#endif
1227
1228 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
1229 * that already has the 8th bit set.
1230 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
1231 * Don't do this for double-byte encodings, it turns the char into a lead
1232 * byte. */
1233 if (len == 1
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001234 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001235#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001236 || (state & GDK_SUPER_MASK)
1237#endif
1238 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 && !(key_sym == GDK_BackSpace || key_sym == GDK_Delete)
1240 && (string[0] & 0x80) == 0
1241 && !(key_sym == GDK_Tab && (state & GDK_SHIFT_MASK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 )
1244 {
1245 string[0] |= 0x80;
1246 state &= ~GDK_MOD1_MASK; /* don't use it again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 if (enc_utf8) /* convert to utf-8 */
1248 {
1249 string[1] = string[0] & 0xbf;
1250 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
1251 if (string[1] == CSI)
1252 {
1253 string[2] = KS_EXTRA;
1254 string[3] = (int)KE_CSI;
1255 len = 4;
1256 }
1257 else
1258 len = 2;
1259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
1261
1262 /* Check for special keys. Also do this when len == 1 (key has an ASCII
1263 * value) to detect backspace, delete and keypad keys. */
1264 if (len == 0 || len == 1)
1265 {
1266 for (i = 0; special_keys[i].key_sym != 0; i++)
1267 {
1268 if (special_keys[i].key_sym == key_sym)
1269 {
1270 string[0] = CSI;
1271 string[1] = special_keys[i].code0;
1272 string[2] = special_keys[i].code1;
1273 len = -3;
1274 break;
1275 }
1276 }
1277 }
1278
1279 if (len == 0) /* Unrecognized key */
1280 return TRUE;
1281
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 /* Special keys (and a few others) may have modifiers. Also when using a
1283 * double-byte encoding (can't set the 8th bit). */
1284 if (len == -3 || key_sym == GDK_space || key_sym == GDK_Tab
1285 || key_sym == GDK_Return || key_sym == GDK_Linefeed
1286 || key_sym == GDK_Escape || key_sym == GDK_KP_Tab
1287 || key_sym == GDK_ISO_Enter || key_sym == GDK_3270_Enter
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001288 || (enc_dbcs && len == 1 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001289#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001290 || (state & GDK_SUPER_MASK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291#endif
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001292 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001294 modifiers = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295
1296 /*
1297 * For some keys a shift modifier is translated into another key
1298 * code.
1299 */
1300 if (len == -3)
1301 key = TO_SPECIAL(string[1], string[2]);
1302 else
1303 key = string[0];
1304
1305 key = simplify_key(key, &modifiers);
1306 if (key == CSI)
1307 key = K_CSI;
1308 if (IS_SPECIAL(key))
1309 {
1310 string[0] = CSI;
1311 string[1] = K_SECOND(key);
1312 string[2] = K_THIRD(key);
1313 len = 3;
1314 }
1315 else
1316 {
1317 string[0] = key;
1318 len = 1;
1319 }
1320
1321 if (modifiers != 0)
1322 {
1323 string2[0] = CSI;
1324 string2[1] = KS_MODIFIER;
1325 string2[2] = modifiers;
1326 add_to_input_buf(string2, 3);
1327 }
1328 }
1329
1330 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1331 || (string[0] == intr_char && intr_char != Ctrl_C)))
1332 {
1333 trash_input_buf();
1334 got_int = TRUE;
1335 }
1336
1337 add_to_input_buf(string, len);
1338
1339 /* blank out the pointer if necessary */
1340 if (p_mh)
1341 gui_mch_mousehide(TRUE);
1342
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 return TRUE;
1344}
1345
Bram Moolenaar98921892016-02-23 17:14:37 +01001346#if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001348key_release_event(GtkWidget *widget UNUSED,
1349 GdkEventKey *event,
1350 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351{
Bram Moolenaar98921892016-02-23 17:14:37 +01001352# if GTK_CHECK_VERSION(3,0,0)
1353 is_key_pressed = FALSE;
1354 gui_mch_start_blink();
1355# endif
1356# if defined(FEAT_XIM)
Bram Moolenaar20892c12011-06-26 04:49:00 +02001357 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 /*
1359 * GTK+ 2 input methods may do fancy stuff on key release events too.
1360 * With the default IM for instance, you can enter any UCS code point
1361 * by holding down CTRL-SHIFT and typing hexadecimal digits.
1362 */
1363 return xim_queue_key_press_event(event, FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001364# else
1365 return TRUE;
1366# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367}
1368#endif
1369
1370
1371/****************************************************************************
1372 * Selection handlers:
1373 */
1374
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001376selection_clear_event(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 GdkEventSelection *event,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001378 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379{
1380 if (event->selection == clip_plus.gtk_sel_atom)
1381 clip_lose_selection(&clip_plus);
1382 else
1383 clip_lose_selection(&clip_star);
1384
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 return TRUE;
1386}
1387
1388#define RS_NONE 0 /* selection_received_cb() not called yet */
1389#define RS_OK 1 /* selection_received_cb() called and OK */
1390#define RS_FAIL 2 /* selection_received_cb() called and failed */
1391static int received_selection = RS_NONE;
1392
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001394selection_received_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 GtkSelectionData *data,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001396 guint time_ UNUSED,
1397 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398{
1399 VimClipboard *cbd;
1400 char_u *text;
1401 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 guchar *tmpbuf_utf8 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 int len;
Bram Moolenaard44347f2011-06-19 01:14:29 +02001404 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405
Bram Moolenaar98921892016-02-23 17:14:37 +01001406#if GTK_CHECK_VERSION(3,0,0)
1407 if (gtk_selection_data_get_selection(data) == clip_plus.gtk_sel_atom)
1408#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 if (data->selection == clip_plus.gtk_sel_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 cbd = &clip_plus;
1412 else
1413 cbd = &clip_star;
1414
Bram Moolenaar98921892016-02-23 17:14:37 +01001415#if GTK_CHECK_VERSION(3,0,0)
1416 text = (char_u *)gtk_selection_data_get_data(data);
1417 len = gtk_selection_data_get_length(data);
1418#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 text = (char_u *)data->data;
1420 len = data->length;
Bram Moolenaar98921892016-02-23 17:14:37 +01001421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422
1423 if (text == NULL || len <= 0)
1424 {
1425 received_selection = RS_FAIL;
1426 /* clip_free_selection(cbd); ??? */
1427
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 return;
1429 }
1430
Bram Moolenaar98921892016-02-23 17:14:37 +01001431#if GTK_CHECK_VERSION(3,0,0)
1432 if (gtk_selection_data_get_data_type(data) == vim_atom)
1433#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 if (data->type == vim_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 {
1437 motion_type = *text++;
1438 --len;
1439 }
Bram Moolenaar98921892016-02-23 17:14:37 +01001440#if GTK_CHECK_VERSION(3,0,0)
1441 else if (gtk_selection_data_get_data_type(data) == vimenc_atom)
1442#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 else if (data->type == vimenc_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 {
1446 char_u *enc;
1447 vimconv_T conv;
1448
1449 motion_type = *text++;
1450 --len;
1451
1452 enc = text;
1453 text += STRLEN(text) + 1;
1454 len -= text - enc;
1455
1456 /* If the encoding of the text is different from 'encoding', attempt
1457 * converting it. */
1458 conv.vc_type = CONV_NONE;
1459 convert_setup(&conv, enc, p_enc);
1460 if (conv.vc_type != CONV_NONE)
1461 {
1462 tmpbuf = string_convert(&conv, text, &len);
1463 if (tmpbuf != NULL)
1464 text = tmpbuf;
1465 convert_setup(&conv, NULL, NULL);
1466 }
1467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 /* gtk_selection_data_get_text() handles all the nasty details
1470 * and targets and encodings etc. This rocks so hard. */
1471 else
1472 {
1473 tmpbuf_utf8 = gtk_selection_data_get_text(data);
1474 if (tmpbuf_utf8 != NULL)
1475 {
1476 len = STRLEN(tmpbuf_utf8);
1477 if (input_conv.vc_type != CONV_NONE)
1478 {
1479 tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len);
1480 if (tmpbuf != NULL)
1481 text = tmpbuf;
1482 }
1483 else
1484 text = tmpbuf_utf8;
1485 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001486 else if (len >= 2 && text[0] == 0xff && text[1] == 0xfe)
1487 {
1488 vimconv_T conv;
1489
1490 /* UTF-16, we get this for HTML */
1491 conv.vc_type = CONV_NONE;
1492 convert_setup_ext(&conv, (char_u *)"utf-16le", FALSE, p_enc, TRUE);
1493
1494 if (conv.vc_type != CONV_NONE)
1495 {
1496 text += 2;
1497 len -= 2;
1498 tmpbuf = string_convert(&conv, text, &len);
1499 convert_setup(&conv, NULL, NULL);
1500 }
1501 if (tmpbuf != NULL)
1502 text = tmpbuf;
1503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001506 /* Chop off any trailing NUL bytes. OpenOffice sends these. */
Bram Moolenaara76638f2010-06-05 12:49:46 +02001507 while (len > 0 && text[len - 1] == NUL)
1508 --len;
1509
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 clip_yank_selection(motion_type, text, (long)len, cbd);
1511 received_selection = RS_OK;
1512 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 g_free(tmpbuf_utf8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514}
1515
1516/*
1517 * Prepare our selection data for passing it to the external selection
1518 * client.
1519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001521selection_get_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 GtkSelectionData *selection_data,
1523 guint info,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001524 guint time_ UNUSED,
1525 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526{
1527 char_u *string;
1528 char_u *tmpbuf;
1529 long_u tmplen;
1530 int length;
1531 int motion_type;
1532 GdkAtom type;
1533 VimClipboard *cbd;
1534
Bram Moolenaar98921892016-02-23 17:14:37 +01001535#if GTK_CHECK_VERSION(3,0,0)
1536 if (gtk_selection_data_get_selection(selection_data)
1537 == clip_plus.gtk_sel_atom)
1538#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 if (selection_data->selection == clip_plus.gtk_sel_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 cbd = &clip_plus;
1542 else
1543 cbd = &clip_star;
1544
1545 if (!cbd->owned)
1546 return; /* Shouldn't ever happen */
1547
1548 if (info != (guint)TARGET_STRING
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001549 && (!clip_html || info != (guint)TARGET_HTML)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 && info != (guint)TARGET_UTF8_STRING
1551 && info != (guint)TARGET_VIMENC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 && info != (guint)TARGET_VIM
1553 && info != (guint)TARGET_COMPOUND_TEXT
1554 && info != (guint)TARGET_TEXT)
1555 return;
1556
1557 /* get the selection from the '*'/'+' register */
1558 clip_get_selection(cbd);
1559
1560 motion_type = clip_convert_selection(&string, &tmplen, cbd);
1561 if (motion_type < 0 || string == NULL)
1562 return;
1563 /* Due to int arguments we can't handle more than G_MAXINT. Also
1564 * reserve one extra byte for NUL or the motion type; just in case.
1565 * (Not that pasting 2G of text is ever going to work, but... ;-) */
1566 length = MIN(tmplen, (long_u)(G_MAXINT - 1));
1567
1568 if (info == (guint)TARGET_VIM)
1569 {
1570 tmpbuf = alloc((unsigned)length + 1);
1571 if (tmpbuf != NULL)
1572 {
1573 tmpbuf[0] = motion_type;
1574 mch_memmove(tmpbuf + 1, string, (size_t)length);
1575 }
1576 /* For our own format, the first byte contains the motion type */
1577 ++length;
1578 vim_free(string);
1579 string = tmpbuf;
1580 type = vim_atom;
1581 }
1582
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001583 else if (info == (guint)TARGET_HTML)
1584 {
1585 vimconv_T conv;
1586
1587 /* Since we get utf-16, we probably should set it as well. */
1588 conv.vc_type = CONV_NONE;
1589 convert_setup_ext(&conv, p_enc, TRUE, (char_u *)"utf-16le", FALSE);
1590 if (conv.vc_type != CONV_NONE)
1591 {
1592 tmpbuf = string_convert(&conv, string, &length);
1593 convert_setup(&conv, NULL, NULL);
1594 vim_free(string);
1595 string = tmpbuf;
1596 }
1597
1598 /* Prepend the BOM: "fffe" */
1599 if (string != NULL)
1600 {
1601 tmpbuf = alloc(length + 2);
1602 tmpbuf[0] = 0xff;
1603 tmpbuf[1] = 0xfe;
1604 mch_memmove(tmpbuf + 2, string, (size_t)length);
1605 vim_free(string);
1606 string = tmpbuf;
1607 length += 2;
1608
Bram Moolenaar98921892016-02-23 17:14:37 +01001609#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01001610 /* Looks redundant even for GTK2 because these values are
Bram Moolenaar98921892016-02-23 17:14:37 +01001611 * overwritten by gtk_selection_data_set() that follows. */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001612 selection_data->type = selection_data->target;
1613 selection_data->format = 16; /* 16 bits per char */
Bram Moolenaar98921892016-02-23 17:14:37 +01001614#endif
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001615 gtk_selection_data_set(selection_data, html_atom, 16,
1616 string, length);
1617 vim_free(string);
1618 }
1619 return;
1620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 else if (info == (guint)TARGET_VIMENC)
1622 {
1623 int l = STRLEN(p_enc);
1624
1625 /* contents: motion_type 'encoding' NUL text */
1626 tmpbuf = alloc((unsigned)length + l + 2);
1627 if (tmpbuf != NULL)
1628 {
1629 tmpbuf[0] = motion_type;
1630 STRCPY(tmpbuf + 1, p_enc);
1631 mch_memmove(tmpbuf + l + 2, string, (size_t)length);
1632 }
1633 length += l + 2;
1634 vim_free(string);
1635 string = tmpbuf;
1636 type = vimenc_atom;
1637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 /* gtk_selection_data_set_text() handles everything for us. This is
1640 * so easy and simple and cool, it'd be insane not to use it. */
1641 else
1642 {
1643 if (output_conv.vc_type != CONV_NONE)
1644 {
1645 tmpbuf = string_convert(&output_conv, string, &length);
1646 vim_free(string);
1647 if (tmpbuf == NULL)
1648 return;
1649 string = tmpbuf;
1650 }
1651 /* Validate the string to avoid runtime warnings */
1652 if (g_utf8_validate((const char *)string, (gssize)length, NULL))
1653 {
1654 gtk_selection_data_set_text(selection_data,
1655 (const char *)string, length);
1656 }
1657 vim_free(string);
1658 return;
1659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660
1661 if (string != NULL)
1662 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001663#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01001664 /* Looks redundant even for GTK2 because these values are
Bram Moolenaar98921892016-02-23 17:14:37 +01001665 * overwritten by gtk_selection_data_set() that follows. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 selection_data->type = selection_data->target;
1667 selection_data->format = 8; /* 8 bits per char */
Bram Moolenaar98921892016-02-23 17:14:37 +01001668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 gtk_selection_data_set(selection_data, type, 8, string, length);
1670 vim_free(string);
1671 }
1672}
1673
1674/*
Bram Moolenaar29695702012-05-18 17:03:18 +02001675 * Check if the GUI can be started. Called before gvimrc is sourced and
1676 * before fork().
1677 * Return OK or FAIL.
1678 */
1679 int
Bram Moolenaar717e1962016-08-10 21:28:44 +02001680gui_mch_early_init_check(int give_message)
Bram Moolenaar29695702012-05-18 17:03:18 +02001681{
1682 char_u *p;
1683
1684 /* Guess that when $DISPLAY isn't set the GUI can't start. */
1685 p = mch_getenv((char_u *)"DISPLAY");
1686 if (p == NULL || *p == NUL)
1687 {
1688 gui.dying = TRUE;
Bram Moolenaar717e1962016-08-10 21:28:44 +02001689 if (give_message)
1690 EMSG(_((char *)e_opendisp));
Bram Moolenaar29695702012-05-18 17:03:18 +02001691 return FAIL;
1692 }
1693 return OK;
1694}
1695
1696/*
1697 * Check if the GUI can be started. Called before gvimrc is sourced but after
1698 * fork().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 * Return OK or FAIL.
1700 */
1701 int
1702gui_mch_init_check(void)
1703{
Bram Moolenaar3a466a82016-01-19 17:47:25 +01001704#ifdef USE_GRESOURCE
1705 static int res_registered = FALSE;
1706
1707 if (!res_registered)
1708 {
1709 /* Call this function in the GUI process; otherwise, the resources
1710 * won't be available. Don't call it twice. */
1711 res_registered = TRUE;
1712 gui_gtk_register_resource();
1713 }
1714#endif
1715
Bram Moolenaarf80663f2016-04-05 21:56:06 +02001716#if GTK_CHECK_VERSION(3,10,0)
1717 /* Vim currently assumes that Gtk means X11, so it cannot use native Gtk
1718 * support for other backends such as Wayland. */
1719 gdk_set_allowed_backends ("x11");
1720#endif
1721
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722#ifdef FEAT_GUI_GNOME
1723 if (gtk_socket_id == 0)
1724 using_gnome = 1;
1725#endif
1726
Bram Moolenaar8dd79012013-05-21 12:52:04 +02001727 /* This defaults to argv[0], but we want it to match the name of the
1728 * shipped gvim.desktop so that Vim's windows can be associated with this
1729 * file. */
1730 g_set_prgname("gvim");
1731
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 /* Don't use gtk_init() or gnome_init(), it exits on failure. */
1733 if (!gtk_init_check(&gui_argc, &gui_argv))
1734 {
1735 gui.dying = TRUE;
Bram Moolenaarc93e7912008-07-08 10:46:08 +00001736 EMSG(_((char *)e_opendisp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 return FAIL;
1738 }
1739
1740 return OK;
1741}
1742
1743
1744/****************************************************************************
1745 * Mouse handling callbacks
1746 */
1747
1748
1749static guint mouse_click_timer = 0;
1750static int mouse_timed_out = TRUE;
1751
1752/*
1753 * Timer used to recognize multiple clicks of the mouse button
1754 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001755#if GTK_CHECK_VERSION(3,0,0)
1756 static gboolean
1757#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +01001759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760mouse_click_timer_cb(gpointer data)
1761{
1762 /* we don't use this information currently */
1763 int *timed_out = (int *) data;
1764
1765 *timed_out = TRUE;
1766 return FALSE; /* don't happen again */
1767}
1768
1769static guint motion_repeat_timer = 0;
1770static int motion_repeat_offset = FALSE;
Bram Moolenaar98921892016-02-23 17:14:37 +01001771#ifdef GTK_DEST_DEFAULT_ALL
1772static gboolean motion_repeat_timer_cb(gpointer);
1773#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774static gint motion_repeat_timer_cb(gpointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01001775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
1777 static void
1778process_motion_notify(int x, int y, GdkModifierType state)
1779{
1780 int button;
1781 int_u vim_modifiers;
Bram Moolenaar98921892016-02-23 17:14:37 +01001782#if GTK_CHECK_VERSION(3,0,0)
1783 GtkAllocation allocation;
1784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
1786 button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1787 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1788 GDK_BUTTON5_MASK))
1789 ? MOUSE_DRAG : ' ';
1790
1791 /* If our pointer is currently hidden, then we should show it. */
1792 gui_mch_mousehide(FALSE);
1793
1794 /* Just moving the rodent above the drawing area without any button
1795 * being pressed. */
1796 if (button != MOUSE_DRAG)
1797 {
1798 gui_mouse_moved(x, y);
1799 return;
1800 }
1801
1802 /* translate modifier coding between the main engine and GTK */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001803 vim_modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804
Bram Moolenaar49325942007-05-10 19:19:59 +00001805 /* inform the editor engine about the occurrence of this event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 gui_send_mouse_event(button, x, y, FALSE, vim_modifiers);
1807
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 /*
1809 * Auto repeat timer handling.
1810 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001811#if GTK_CHECK_VERSION(3,0,0)
1812 gtk_widget_get_allocation(gui.drawarea, &allocation);
1813
1814 if (x < 0 || y < 0
1815 || x >= allocation.width
1816 || y >= allocation.height)
1817#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 if (x < 0 || y < 0
1819 || x >= gui.drawarea->allocation.width
1820 || y >= gui.drawarea->allocation.height)
Bram Moolenaar98921892016-02-23 17:14:37 +01001821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 {
1823
1824 int dx;
1825 int dy;
1826 int offshoot;
1827 int delay = 10;
1828
1829 /* Calculate the maximal distance of the cursor from the drawing area.
1830 * (offshoot can't become negative here!).
1831 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001832#if GTK_CHECK_VERSION(3,0,0)
1833 dx = x < 0 ? -x : x - allocation.width;
1834 dy = y < 0 ? -y : y - allocation.height;
1835#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 dx = x < 0 ? -x : x - gui.drawarea->allocation.width;
1837 dy = y < 0 ? -y : y - gui.drawarea->allocation.height;
Bram Moolenaar98921892016-02-23 17:14:37 +01001838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
1840 offshoot = dx > dy ? dx : dy;
1841
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001842 /* Make a linearly decaying timer delay with a threshold of 5 at a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 * distance of 127 pixels from the main window.
1844 *
1845 * One could think endlessly about the most ergonomic variant here.
1846 * For example it could make sense to calculate the distance from the
1847 * drags start instead...
1848 *
1849 * Maybe a parabolic interpolation would suite us better here too...
1850 */
1851 if (offshoot > 127)
1852 {
1853 /* 5 appears to be somehow near to my perceptual limits :-). */
1854 delay = 5;
1855 }
1856 else
1857 {
1858 delay = (130 * (127 - offshoot)) / 127 + 5;
1859 }
1860
1861 /* shoot again */
1862 if (!motion_repeat_timer)
Bram Moolenaar98921892016-02-23 17:14:37 +01001863#if GTK_CHECK_VERSION(3,0,0)
1864 motion_repeat_timer = g_timeout_add((guint)delay,
1865 motion_repeat_timer_cb, NULL);
1866#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 motion_repeat_timer = gtk_timeout_add((guint32)delay,
1868 motion_repeat_timer_cb, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01001869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 }
1871}
1872
Bram Moolenaar98921892016-02-23 17:14:37 +01001873#if GTK_CHECK_VERSION(3,0,0)
1874 static GdkDevice *
1875gui_gtk_get_pointer_device(GtkWidget *widget)
1876{
1877 GdkWindow * const win = gtk_widget_get_window(widget);
1878 GdkDisplay * const dpy = gdk_window_get_display(win);
Bram Moolenaar30e12d22016-04-17 20:49:53 +02001879# if GTK_CHECK_VERSION(3,20,0)
1880 GdkSeat * const seat = gdk_display_get_default_seat(dpy);
1881 return gdk_seat_get_pointer(seat);
1882# else
Bram Moolenaar98921892016-02-23 17:14:37 +01001883 GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy);
1884 return gdk_device_manager_get_client_pointer(mngr);
Bram Moolenaar30e12d22016-04-17 20:49:53 +02001885# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01001886}
1887
1888 static GdkWindow *
1889gui_gtk_get_pointer(GtkWidget *widget,
1890 gint *x,
1891 gint *y,
1892 GdkModifierType *state)
1893{
1894 GdkWindow * const win = gtk_widget_get_window(widget);
1895 GdkDevice * const dev = gui_gtk_get_pointer_device(widget);
1896 return gdk_window_get_device_position(win, dev , x, y, state);
1897}
1898
Bram Moolenaar2369c152016-03-04 23:08:25 +01001899# if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar98921892016-02-23 17:14:37 +01001900 static GdkWindow *
1901gui_gtk_window_at_position(GtkWidget *widget,
1902 gint *x,
1903 gint *y)
1904{
1905 GdkDevice * const dev = gui_gtk_get_pointer_device(widget);
1906 return gdk_device_get_window_at_position(dev, x, y);
1907}
Bram Moolenaar2369c152016-03-04 23:08:25 +01001908# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01001909#endif
1910
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911/*
1912 * Timer used to recognize multiple clicks of the mouse button.
1913 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001914#if GTK_CHECK_VERSION(3,0,0)
1915 static gboolean
1916#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +01001918#endif
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001919motion_repeat_timer_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920{
1921 int x;
1922 int y;
1923 GdkModifierType state;
1924
Bram Moolenaar98921892016-02-23 17:14:37 +01001925#if GTK_CHECK_VERSION(3,0,0)
1926 gui_gtk_get_pointer(gui.drawarea, &x, &y, &state);
1927#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 gdk_window_get_pointer(gui.drawarea->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +01001929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930
1931 if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1932 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1933 GDK_BUTTON5_MASK)))
1934 {
1935 motion_repeat_timer = 0;
1936 return FALSE;
1937 }
1938
1939 /* If there already is a mouse click in the input buffer, wait another
1940 * time (otherwise we would create a backlog of clicks) */
1941 if (vim_used_in_input_buf() > 10)
1942 return TRUE;
1943
1944 motion_repeat_timer = 0;
1945
1946 /*
1947 * Fake a motion event.
1948 * Trick: Pretend the mouse moved to the next character on every other
1949 * event, otherwise drag events will be discarded, because they are still
1950 * in the same character.
1951 */
1952 if (motion_repeat_offset)
1953 x += gui.char_width;
1954
1955 motion_repeat_offset = !motion_repeat_offset;
1956 process_motion_notify(x, y, state);
1957
1958 /* Don't happen again. We will get reinstalled in the synthetic event
1959 * if needed -- thus repeating should still work. */
1960 return FALSE;
1961}
1962
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001964motion_notify_event(GtkWidget *widget,
1965 GdkEventMotion *event,
1966 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967{
1968 if (event->is_hint)
1969 {
1970 int x;
1971 int y;
1972 GdkModifierType state;
1973
Bram Moolenaar98921892016-02-23 17:14:37 +01001974#if GTK_CHECK_VERSION(3,0,0)
1975 gui_gtk_get_pointer(widget, &x, &y, &state);
1976#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 gdk_window_get_pointer(widget->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +01001978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 process_motion_notify(x, y, state);
1980 }
1981 else
1982 {
1983 process_motion_notify((int)event->x, (int)event->y,
1984 (GdkModifierType)event->state);
1985 }
1986
1987 return TRUE; /* handled */
1988}
1989
1990
1991/*
1992 * Mouse button handling. Note please that we are capturing multiple click's
1993 * by our own timeout mechanism instead of the one provided by GTK+ itself.
1994 * This is due to the way the generic VIM code is recognizing multiple clicks.
1995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001997button_press_event(GtkWidget *widget,
1998 GdkEventButton *event,
1999 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000{
2001 int button;
2002 int repeated_click = FALSE;
2003 int x, y;
2004 int_u vim_modifiers;
2005
Bram Moolenaar20892c12011-06-26 04:49:00 +02002006 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002007
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 /* Make sure we have focus now we've been selected */
Bram Moolenaar98921892016-02-23 17:14:37 +01002009#if GTK_CHECK_VERSION(3,0,0)
2010 if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget))
2011#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01002013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 gtk_widget_grab_focus(widget);
2015
2016 /*
2017 * Don't let additional events about multiple clicks send by GTK to us
2018 * after the initial button press event confuse us.
2019 */
2020 if (event->type != GDK_BUTTON_PRESS)
2021 return FALSE;
2022
2023 x = event->x;
2024 y = event->y;
2025
2026 /* Handle multiple clicks */
2027 if (!mouse_timed_out && mouse_click_timer)
2028 {
Bram Moolenaar98921892016-02-23 17:14:37 +01002029#if GTK_CHECK_VERSION(3,0,0)
2030 g_source_remove(mouse_click_timer);
2031#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 gtk_timeout_remove(mouse_click_timer);
Bram Moolenaar98921892016-02-23 17:14:37 +01002033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 mouse_click_timer = 0;
2035 repeated_click = TRUE;
2036 }
2037
2038 mouse_timed_out = FALSE;
Bram Moolenaar98921892016-02-23 17:14:37 +01002039#if GTK_CHECK_VERSION(3,0,0)
2040 mouse_click_timer = g_timeout_add((guint)p_mouset,
2041 mouse_click_timer_cb, &mouse_timed_out);
2042#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 mouse_click_timer = gtk_timeout_add((guint32)p_mouset,
2044 mouse_click_timer_cb, &mouse_timed_out);
Bram Moolenaar98921892016-02-23 17:14:37 +01002045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
2047 switch (event->button)
2048 {
Bram Moolenaar88e484b2015-11-24 15:38:44 +01002049 /* Keep in sync with gui_x11.c.
2050 * Buttons 4-7 are handled in scroll_event() */
2051 case 1: button = MOUSE_LEFT; break;
2052 case 2: button = MOUSE_MIDDLE; break;
2053 case 3: button = MOUSE_RIGHT; break;
2054 case 8: button = MOUSE_X1; break;
2055 case 9: button = MOUSE_X2; break;
2056 default:
2057 return FALSE; /* Unknown button */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 }
2059
2060#ifdef FEAT_XIM
2061 /* cancel any preediting */
2062 if (im_is_preediting())
2063 xim_reset();
2064#endif
2065
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002066 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
2068 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
2070 return TRUE;
2071}
2072
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073/*
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002074 * GTK+ 2 abstracts scrolling via the GdkEventScroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002077scroll_event(GtkWidget *widget,
2078 GdkEventScroll *event,
2079 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080{
2081 int button;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002082 int_u vim_modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083
Bram Moolenaar98921892016-02-23 17:14:37 +01002084#if GTK_CHECK_VERSION(3,0,0)
2085 if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget))
2086#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01002088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 gtk_widget_grab_focus(widget);
2090
2091 switch (event->direction)
2092 {
2093 case GDK_SCROLL_UP:
2094 button = MOUSE_4;
2095 break;
2096 case GDK_SCROLL_DOWN:
2097 button = MOUSE_5;
2098 break;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002099 case GDK_SCROLL_LEFT:
2100 button = MOUSE_7;
2101 break;
2102 case GDK_SCROLL_RIGHT:
2103 button = MOUSE_6;
2104 break;
2105 default: /* This shouldn't happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 return FALSE;
2107 }
2108
2109# ifdef FEAT_XIM
2110 /* cancel any preediting */
2111 if (im_is_preediting())
2112 xim_reset();
2113# endif
2114
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002115 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
2117 gui_send_mouse_event(button, (int)event->x, (int)event->y,
2118 FALSE, vim_modifiers);
2119
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 return TRUE;
2121}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122
2123
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002125button_release_event(GtkWidget *widget UNUSED,
2126 GdkEventButton *event,
2127 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128{
2129 int x, y;
2130 int_u vim_modifiers;
2131
Bram Moolenaar20892c12011-06-26 04:49:00 +02002132 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002133
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 /* Remove any motion "machine gun" timers used for automatic further
2135 extension of allocation areas if outside of the applications window
2136 area .*/
2137 if (motion_repeat_timer)
2138 {
Bram Moolenaar98921892016-02-23 17:14:37 +01002139#if GTK_CHECK_VERSION(3,0,0)
2140 g_source_remove(motion_repeat_timer);
2141#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 gtk_timeout_remove(motion_repeat_timer);
Bram Moolenaar98921892016-02-23 17:14:37 +01002143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 motion_repeat_timer = 0;
2145 }
2146
2147 x = event->x;
2148 y = event->y;
2149
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002150 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151
2152 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
2154 return TRUE;
2155}
2156
2157
2158#ifdef FEAT_DND
2159/****************************************************************************
2160 * Drag aNd Drop support handlers.
2161 */
2162
2163/*
2164 * Count how many items there may be and separate them with a NUL.
2165 * Apparently the items are separated with \r\n. This is not documented,
2166 * thus be careful not to go past the end. Also allow separation with
2167 * NUL characters.
2168 */
2169 static int
2170count_and_decode_uri_list(char_u *out, char_u *raw, int len)
2171{
2172 int i;
2173 char_u *p = out;
2174 int count = 0;
2175
2176 for (i = 0; i < len; ++i)
2177 {
2178 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r')
2179 {
2180 if (p > out && p[-1] != NUL)
2181 {
2182 ++count;
2183 *p++ = NUL;
2184 }
2185 }
2186 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0)
2187 {
2188 *p++ = hexhex2nr(raw + i + 1);
2189 i += 2;
2190 }
2191 else
2192 *p++ = raw[i];
2193 }
2194 if (p > out && p[-1] != NUL)
2195 {
2196 *p = NUL; /* last item didn't have \r or \n */
2197 ++count;
2198 }
2199 return count;
2200}
2201
2202/*
2203 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
2204 * this process, URI which protocol is not "file:" are removed. Return
2205 * length of array (less than "max").
2206 */
2207 static int
2208filter_uri_list(char_u **outlist, int max, char_u *src)
2209{
2210 int i, j;
2211
2212 for (i = j = 0; i < max; ++i)
2213 {
2214 outlist[i] = NULL;
2215 if (STRNCMP(src, "file:", 5) == 0)
2216 {
2217 src += 5;
2218 if (STRNCMP(src, "//localhost", 11) == 0)
2219 src += 11;
2220 while (src[0] == '/' && src[1] == '/')
2221 ++src;
2222 outlist[j++] = vim_strsave(src);
2223 }
2224 src += STRLEN(src) + 1;
2225 }
2226 return j;
2227}
2228
2229 static char_u **
2230parse_uri_list(int *count, char_u *data, int len)
2231{
2232 int n = 0;
2233 char_u *tmp = NULL;
Bram Moolenaar945ec092016-06-08 21:17:43 +02002234 char_u **array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235
2236 if (data != NULL && len > 0 && (tmp = (char_u *)alloc(len + 1)) != NULL)
2237 {
2238 n = count_and_decode_uri_list(tmp, data, len);
2239 if (n > 0 && (array = (char_u **)alloc(n * sizeof(char_u *))) != NULL)
2240 n = filter_uri_list(array, n, tmp);
2241 }
2242 vim_free(tmp);
2243 *count = n;
2244 return array;
2245}
2246
2247 static void
2248drag_handle_uri_list(GdkDragContext *context,
2249 GtkSelectionData *data,
2250 guint time_,
2251 GdkModifierType state,
2252 gint x,
2253 gint y)
2254{
2255 char_u **fnames;
2256 int nfiles = 0;
2257
Bram Moolenaar98921892016-02-23 17:14:37 +01002258# if GTK_CHECK_VERSION(3,0,0)
2259 fnames = parse_uri_list(&nfiles,
2260 (char_u *)gtk_selection_data_get_data(data),
2261 gtk_selection_data_get_length(data));
2262# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 fnames = parse_uri_list(&nfiles, data->data, data->length);
Bram Moolenaar98921892016-02-23 17:14:37 +01002264# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265
2266 if (fnames != NULL && nfiles > 0)
2267 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002268 int_u modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269
2270 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
2271
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002272 modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273
2274 gui_handle_drop(x, y, modifiers, fnames, nfiles);
2275 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002276 else
2277 vim_free(fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278}
2279
2280 static void
2281drag_handle_text(GdkDragContext *context,
2282 GtkSelectionData *data,
2283 guint time_,
2284 GdkModifierType state)
2285{
2286 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP};
2287 char_u *text;
2288 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290
Bram Moolenaar98921892016-02-23 17:14:37 +01002291# if GTK_CHECK_VERSION(3,0,0)
2292 text = (char_u *)gtk_selection_data_get_data(data);
2293 len = gtk_selection_data_get_length(data);
2294# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 text = data->data;
2296 len = data->length;
Bram Moolenaar98921892016-02-23 17:14:37 +01002297# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
Bram Moolenaar98921892016-02-23 17:14:37 +01002299# if GTK_CHECK_VERSION(3,0,0)
2300 if (gtk_selection_data_get_data_type(data) == utf8_string_atom)
2301# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 if (data->type == utf8_string_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01002303# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 if (input_conv.vc_type != CONV_NONE)
2306 tmpbuf = string_convert(&input_conv, text, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 if (tmpbuf != NULL)
2308 text = tmpbuf;
2309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310
2311 dnd_yank_drag_data(text, (long)len);
2312 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002315 dropkey[2] = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316
2317 if (dropkey[2] != 0)
2318 add_to_input_buf(dropkey, (int)sizeof(dropkey));
2319 else
2320 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321}
2322
2323/*
2324 * DND receiver.
2325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 static void
2327drag_data_received_cb(GtkWidget *widget,
2328 GdkDragContext *context,
2329 gint x,
2330 gint y,
2331 GtkSelectionData *data,
2332 guint info,
2333 guint time_,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002334 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335{
2336 GdkModifierType state;
2337
2338 /* Guard against trash */
Bram Moolenaar98921892016-02-23 17:14:37 +01002339# if GTK_CHECK_VERSION(3,0,0)
2340 const guchar * const data_data = gtk_selection_data_get_data(data);
2341 const gint data_length = gtk_selection_data_get_length(data);
2342 const gint data_format = gtk_selection_data_get_format(data);
2343
2344 if (data_data == NULL
2345 || data_length <= 0
2346 || data_format != 8
2347 || data_data[data_length] != '\0')
2348# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 if (data->data == NULL
2350 || data->length <= 0
2351 || data->format != 8
2352 || data->data[data->length] != '\0')
Bram Moolenaar98921892016-02-23 17:14:37 +01002353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 {
2355 gtk_drag_finish(context, FALSE, FALSE, time_);
2356 return;
2357 }
2358
2359 /* Get the current modifier state for proper distinguishment between
2360 * different operations later. */
Bram Moolenaar98921892016-02-23 17:14:37 +01002361#if GTK_CHECK_VERSION(3,0,0)
2362 gui_gtk_get_pointer(widget, NULL, NULL, &state);
2363# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 gdk_window_get_pointer(widget->window, NULL, NULL, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +01002365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366
2367 /* Not sure about the role of "text/plain" here... */
2368 if (info == (guint)TARGET_TEXT_URI_LIST)
2369 drag_handle_uri_list(context, data, time_, state, x, y);
2370 else
2371 drag_handle_text(context, data, time_, state);
2372
2373}
2374#endif /* FEAT_DND */
2375
2376
2377#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2378/*
2379 * GnomeClient interact callback. Check for unsaved buffers that cannot
2380 * be abandoned and pop up a dialog asking the user for confirmation if
2381 * necessary.
2382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002384sm_client_check_changed_any(GnomeClient *client UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 gint key,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002386 GnomeDialogType type UNUSED,
2387 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388{
2389 cmdmod_T save_cmdmod;
2390 gboolean shutdown_cancelled;
2391
2392 save_cmdmod = cmdmod;
2393
2394# ifdef FEAT_BROWSE
2395 cmdmod.browse = TRUE;
2396# endif
2397# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2398 cmdmod.confirm = TRUE;
2399# endif
2400 /*
2401 * If there are changed buffers, present the user with
2402 * a dialog if possible, otherwise give an error message.
2403 */
Bram Moolenaar027387f2016-01-02 22:25:52 +01002404 shutdown_cancelled = check_changed_any(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406 exiting = FALSE;
2407 cmdmod = save_cmdmod;
2408 setcursor(); /* position the cursor */
2409 out_flush();
2410 /*
2411 * If the user hit the [Cancel] button the whole shutdown
2412 * will be cancelled. Wow, quite powerful feature (:
2413 */
2414 gnome_interaction_key_return(key, shutdown_cancelled);
2415}
2416
2417/*
2418 * Generate a script that can be used to restore the current editing session.
2419 * Save the value of v:this_session before running :mksession in order to make
2420 * automagic session save fully transparent. Return TRUE on success.
2421 */
2422 static int
2423write_session_file(char_u *filename)
2424{
2425 char_u *escaped_filename;
2426 char *mksession_cmdline;
2427 unsigned int save_ssop_flags;
2428 int failed;
2429
2430 /*
2431 * Build an ex command line to create a script that restores the current
2432 * session if executed. Escape the filename to avoid nasty surprises.
2433 */
2434 escaped_filename = vim_strsave_escaped(filename, escape_chars);
2435 if (escaped_filename == NULL)
2436 return FALSE;
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002437 mksession_cmdline = g_strconcat("mksession ", (char *)escaped_filename,
2438 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 vim_free(escaped_filename);
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002440
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 /*
2442 * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
2443 * unpredictable effects when the session is saved automatically. Also,
2444 * we definitely need SSOP_GLOBALS to be able to restore v:this_session.
2445 * Don't use SSOP_BUFFERS to prevent the buffer list from becoming
2446 * enormously large if the GNOME session feature is used regularly.
2447 */
2448 save_ssop_flags = ssop_flags;
2449 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002450 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451
2452 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
2453 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
2454 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
Bram Moolenaar485db9b2005-01-31 19:23:41 +00002455 do_unlet((char_u *)"Save_VV_this_session", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
2457 ssop_flags = save_ssop_flags;
2458 g_free(mksession_cmdline);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002459
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 /*
2461 * Reopen the file and append a command to restore v:this_session,
2462 * as if this save never happened. This is to avoid conflicts with
2463 * the user's own sessions. FIXME: It's probably less hackish to add
2464 * a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
2465 */
2466 if (!failed)
2467 {
2468 FILE *fd;
2469
2470 fd = open_exfile(filename, TRUE, APPENDBIN);
2471
2472 failed = (fd == NULL
2473 || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL
2474 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
2475
2476 if (fd != NULL && fclose(fd) != 0)
2477 failed = TRUE;
2478
2479 if (failed)
2480 mch_remove(filename);
2481 }
2482
2483 return !failed;
2484}
2485
2486/*
2487 * "save_yourself" signal handler. Initiate an interaction to ask the user
2488 * for confirmation if necessary. Save the current editing session and tell
2489 * the session manager how to restart Vim.
2490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 static gboolean
2492sm_client_save_yourself(GnomeClient *client,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002493 gint phase UNUSED,
2494 GnomeSaveStyle save_style UNUSED,
2495 gboolean shutdown UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 GnomeInteractStyle interact_style,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002497 gboolean fast UNUSED,
2498 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499{
2500 static const char suffix[] = "-session.vim";
2501 char *session_file;
2502 unsigned int len;
2503 gboolean success;
2504
2505 /* Always request an interaction if possible. check_changed_any()
2506 * won't actually show a dialog unless any buffers have been modified.
2507 * There doesn't seem to be an obvious way to check that without
2508 * automatically firing the dialog. Anyway, it works just fine. */
2509 if (interact_style == GNOME_INTERACT_ANY)
2510 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL,
2511 &sm_client_check_changed_any,
2512 NULL);
2513 out_flush();
2514 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2515
2516 /* The path is unique for each session save. We do neither know nor care
2517 * which session script will actually be used later. This decision is in
2518 * the domain of the session manager. */
2519 session_file = gnome_config_get_real_path(
2520 gnome_client_get_config_prefix(client));
2521 len = strlen(session_file);
2522
2523 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR)
2524 --len; /* get rid of the superfluous trailing '/' */
2525
2526 session_file = g_renew(char, session_file, len + sizeof(suffix));
2527 memcpy(session_file + len, suffix, sizeof(suffix));
2528
2529 success = write_session_file((char_u *)session_file);
2530
2531 if (success)
2532 {
2533 const char *argv[8];
2534 int i;
2535
2536 /* Tell the session manager how to wipe out the stored session data.
2537 * This isn't as dangerous as it looks, don't worry :) session_file
2538 * is a unique absolute filename. Usually it'll be something like
2539 * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */
2540 i = 0;
2541 argv[i++] = "rm";
2542 argv[i++] = session_file;
2543 argv[i] = NULL;
2544
2545 gnome_client_set_discard_command(client, i, (char **)argv);
2546
2547 /* Tell the session manager how to restore the just saved session.
2548 * This is easily done thanks to Vim's -S option. Pass the -f flag
2549 * since there's no need to fork -- it might even cause confusion.
2550 * Also pass the window role to give the WM something to match on.
2551 * The role is set in gui_mch_open(), thus should _never_ be NULL. */
2552 i = 0;
2553 argv[i++] = restart_command;
2554 argv[i++] = "-f";
2555 argv[i++] = "-g";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 argv[i++] = "--role";
2557 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 argv[i++] = "-S";
2559 argv[i++] = session_file;
2560 argv[i] = NULL;
2561
2562 gnome_client_set_restart_command(client, i, (char **)argv);
2563 gnome_client_set_clone_command(client, 0, NULL);
2564 }
2565
2566 g_free(session_file);
2567
2568 return success;
2569}
2570
2571/*
2572 * Called when the session manager wants us to die. There isn't much to save
2573 * here since "save_yourself" has been emitted before (unless serious trouble
2574 * is happening).
2575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002577sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578{
2579 /* Don't write messages to the GUI anymore */
2580 full_screen = FALSE;
2581
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002582 vim_strncpy(IObuff, (char_u *)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002583 _("Vim: Received \"die\" request from session manager\n"),
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002584 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 preserve_exit();
2586}
2587
2588/*
2589 * Connect our signal handlers to be notified on session save and shutdown.
2590 */
2591 static void
2592setup_save_yourself(void)
2593{
2594 GnomeClient *client;
2595
2596 client = gnome_master_client();
2597
2598 if (client != NULL)
2599 {
2600 /* Must use the deprecated gtk_signal_connect() for compatibility
2601 * with GNOME 1. Arrgh, zombies! */
2602 gtk_signal_connect(GTK_OBJECT(client), "save_yourself",
2603 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL);
2604 gtk_signal_connect(GTK_OBJECT(client), "die",
2605 GTK_SIGNAL_FUNC(&sm_client_die), NULL);
2606 }
2607}
2608
2609#else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2610
2611# ifdef USE_XSMP
2612/*
2613 * GTK tells us that XSMP needs attention
2614 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 static gboolean
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002616local_xsmp_handle_requests(
2617 GIOChannel *source UNUSED,
2618 GIOCondition condition,
2619 gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620{
2621 if (condition == G_IO_IN)
2622 {
2623 /* Do stuff; maybe close connection */
2624 if (xsmp_handle_requests() == FAIL)
2625 g_io_channel_unref((GIOChannel *)data);
2626 return TRUE;
2627 }
2628 /* Error */
2629 g_io_channel_unref((GIOChannel *)data);
2630 xsmp_close();
2631 return TRUE;
2632}
2633# endif /* USE_XSMP */
2634
2635/*
2636 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2637 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2638 */
2639 static void
2640setup_save_yourself(void)
2641{
2642 Atom *existing_atoms = NULL;
2643 int count = 0;
2644
Bram Moolenaar98921892016-02-23 17:14:37 +01002645# ifdef USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 if (xsmp_icefd != -1)
2647 {
2648 /*
2649 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2650 * set up GTK IO monitor
2651 */
2652 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd);
2653
2654 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP,
2655 local_xsmp_handle_requests, (gpointer)g_io);
Bram Moolenaar98921892016-02-23 17:14:37 +01002656 g_io_channel_unref(g_io);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 }
2658 else
Bram Moolenaar98921892016-02-23 17:14:37 +01002659# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 {
2661 /* Fall back to old method */
2662
2663 /* first get the existing value */
Bram Moolenaar98921892016-02-23 17:14:37 +01002664# if GTK_CHECK_VERSION(3,0,0)
2665 GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin);
2666
2667 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win),
2668 GDK_WINDOW_XID(mainwin_win),
2669 &existing_atoms, &count))
2670# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2672 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2673 &existing_atoms, &count))
Bram Moolenaar98921892016-02-23 17:14:37 +01002674# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 {
2676 Atom *new_atoms;
2677 Atom save_yourself_xatom;
2678 int i;
2679
2680 save_yourself_xatom = GET_X_ATOM(save_yourself_atom);
2681
2682 /* check if WM_SAVE_YOURSELF isn't there yet */
2683 for (i = 0; i < count; ++i)
2684 if (existing_atoms[i] == save_yourself_xatom)
2685 break;
2686
2687 if (i == count)
2688 {
2689 /* allocate an Atoms array which is one item longer */
2690 new_atoms = (Atom *)alloc((unsigned)((count + 1)
2691 * sizeof(Atom)));
2692 if (new_atoms != NULL)
2693 {
2694 memcpy(new_atoms, existing_atoms, count * sizeof(Atom));
2695 new_atoms[count] = save_yourself_xatom;
Bram Moolenaar98921892016-02-23 17:14:37 +01002696# if GTK_CHECK_VERSION(3,0,0)
2697 XSetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win),
2698 GDK_WINDOW_XID(mainwin_win),
2699# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 XSetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2701 GDK_WINDOW_XWINDOW(gui.mainwin->window),
Bram Moolenaar98921892016-02-23 17:14:37 +01002702# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 new_atoms, count + 1);
2704 vim_free(new_atoms);
2705 }
2706 }
2707 XFree(existing_atoms);
2708 }
2709 }
2710}
2711
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712/*
2713 * Installing a global event filter seems to be the only way to catch
2714 * client messages of type WM_PROTOCOLS without overriding GDK's own
2715 * client message event filter. Well, that's still better than trying
2716 * to guess what the GDK filter had done if it had been invoked instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 *
2718 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2719 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2720 * I have the nasty feeling modern session managers just don't send this
2721 * deprecated message anymore. Addition: confirmed by several people.
2722 *
2723 * The GNOME session support is much cooler anyway. Unlike this ugly
2724 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2725 * it should work with KDE as well.
2726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 static GdkFilterReturn
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002728global_event_filter(GdkXEvent *xev,
2729 GdkEvent *event UNUSED,
2730 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731{
2732 XEvent *xevent = (XEvent *)xev;
2733
2734 if (xevent != NULL
2735 && xevent->type == ClientMessage
2736 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002737 && (long_u)xevent->xclient.data.l[0]
2738 == GET_X_ATOM(save_yourself_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 {
2740 out_flush();
2741 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2742 /*
2743 * Set the window's WM_COMMAND property, to let the window manager
2744 * know we are done saving ourselves. We don't want to be
2745 * restarted, thus set argv to NULL.
2746 */
Bram Moolenaar98921892016-02-23 17:14:37 +01002747# if GTK_CHECK_VERSION(3,0,0)
2748 XSetCommand(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)),
2749 GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin)),
2750# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2752 GDK_WINDOW_XWINDOW(gui.mainwin->window),
Bram Moolenaar98921892016-02-23 17:14:37 +01002753# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 NULL, 0);
2755 return GDK_FILTER_REMOVE;
2756 }
2757
2758 return GDK_FILTER_CONTINUE;
2759}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760#endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2761
2762
2763/*
2764 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002767mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768{
2769/* If you get an error message here, you still need to unpack the runtime
2770 * archive! */
2771#ifdef magick
2772# undef magick
2773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 /* A bit hackish, but avoids casting later and allows optimization */
2775# define static static const
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776#define magick vim32x32
2777#include "../runtime/vim32x32.xpm"
2778#undef magick
2779#define magick vim16x16
2780#include "../runtime/vim16x16.xpm"
2781#undef magick
2782#define magick vim48x48
2783#include "../runtime/vim48x48.xpm"
2784#undef magick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785# undef static
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786
Bram Moolenaar98921892016-02-23 17:14:37 +01002787#if GTK_CHECK_VERSION(3,0,0)
2788 GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin);
2789#endif
2790
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 /* When started with "--echo-wid" argument, write window ID on stdout. */
2792 if (echo_wid_arg)
2793 {
Bram Moolenaar98921892016-02-23 17:14:37 +01002794#if GTK_CHECK_VERSION(3,0,0)
2795 printf("WID: %ld\n", (long)GDK_WINDOW_XID(mainwin_win));
2796#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 printf("WID: %ld\n", (long)GDK_WINDOW_XWINDOW(gui.mainwin->window));
Bram Moolenaar98921892016-02-23 17:14:37 +01002798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 fflush(stdout);
2800 }
2801
2802 if (vim_strchr(p_go, GO_ICON) != NULL)
2803 {
2804 /*
2805 * Add an icon to the main window. For fun and convenience of the user.
2806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 GList *icons = NULL;
2808
2809 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16));
2810 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32));
2811 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48));
2812
2813 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons);
2814
2815 g_list_foreach(icons, (GFunc)&g_object_unref, NULL);
2816 g_list_free(icons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 }
2818
2819#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
2820 /* Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 gdk_window_add_filter(NULL, &global_event_filter, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822#endif
2823 /* Setup to indicate to the window manager that we want to catch the
2824 * WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2825 * manager instead. */
2826#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2827 if (using_gnome)
2828#endif
2829 setup_save_yourself();
2830
2831#ifdef FEAT_CLIENTSERVER
2832 if (serverName == NULL && serverDelayedStartName != NULL)
2833 {
2834 /* This is a :gui command in a plain vim with no previous server */
Bram Moolenaar98921892016-02-23 17:14:37 +01002835# if GTK_CHECK_VERSION(3,0,0)
2836 commWindow = GDK_WINDOW_XID(mainwin_win);
2837
2838 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(mainwin_win),
2839 serverDelayedStartName);
2840# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 commWindow = GDK_WINDOW_XWINDOW(gui.mainwin->window);
2842
2843 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2844 serverDelayedStartName);
Bram Moolenaar98921892016-02-23 17:14:37 +01002845# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 }
2847 else
2848 {
2849 /*
2850 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2851 * have to change the "server" registration to that of the main window
2852 * If we have not registered a name yet, remember the window
2853 */
Bram Moolenaar98921892016-02-23 17:14:37 +01002854# if GTK_CHECK_VERSION(3,0,0)
2855 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(mainwin_win),
2856 GDK_WINDOW_XID(mainwin_win));
2857# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2859 GDK_WINDOW_XWINDOW(gui.mainwin->window));
Bram Moolenaar98921892016-02-23 17:14:37 +01002860# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 }
2862 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK);
Bram Moolenaar98921892016-02-23 17:14:37 +01002863# if GTK_CHECK_VERSION(3,0,0)
2864 g_signal_connect(G_OBJECT(gui.mainwin), "property-notify-event",
2865 G_CALLBACK(property_event), NULL);
2866# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "property_notify_event",
2868 GTK_SIGNAL_FUNC(property_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01002869# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870#endif
2871}
2872
2873 static GdkCursor *
2874create_blank_pointer(void)
2875{
2876 GdkWindow *root_window = NULL;
Bram Moolenaar98921892016-02-23 17:14:37 +01002877#if GTK_CHECK_VERSION(3,0,0)
2878 GdkPixbuf *blank_mask;
2879#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 GdkPixmap *blank_mask;
Bram Moolenaar98921892016-02-23 17:14:37 +01002881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 GdkCursor *cursor;
Bram Moolenaar36edf062016-07-21 22:10:12 +02002883#if GTK_CHECK_VERSION(3,0,0)
2884 GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
2885#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 GdkColor color = { 0, 0, 0, 0 };
2887 char blank_data[] = { 0x0 };
Bram Moolenaar98921892016-02-23 17:14:37 +01002888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889
2890#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar98921892016-02-23 17:14:37 +01002891# if GTK_CHECK_VERSION(3,12,0)
2892 {
2893 GdkWindow * const win = gtk_widget_get_window(gui.mainwin);
2894 GdkScreen * const scrn = gdk_window_get_screen(win);
2895 root_window = gdk_screen_get_root_window(scrn);
2896 }
2897# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 root_window = gtk_widget_get_root_window(gui.mainwin);
Bram Moolenaar98921892016-02-23 17:14:37 +01002899# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900#endif
2901
2902 /* Create a pseudo blank pointer, which is in fact one pixel by one pixel
2903 * in size. */
Bram Moolenaar98921892016-02-23 17:14:37 +01002904#if GTK_CHECK_VERSION(3,0,0)
2905 {
2906 cairo_surface_t *surf;
2907 cairo_t *cr;
2908
2909 surf = cairo_image_surface_create(CAIRO_FORMAT_A1, 1, 1);
2910 cr = cairo_create(surf);
2911
Bram Moolenaar36edf062016-07-21 22:10:12 +02002912 cairo_set_source_rgba(cr,
2913 color.red,
2914 color.green,
2915 color.blue,
2916 color.alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01002917 cairo_rectangle(cr, 0, 0, 1, 1);
2918 cairo_fill(cr);
2919 cairo_destroy(cr);
2920
2921 blank_mask = gdk_pixbuf_get_from_surface(surf, 0, 0, 1, 1);
2922 cairo_surface_destroy(surf);
2923
2924 cursor = gdk_cursor_new_from_pixbuf(gdk_window_get_display(root_window),
2925 blank_mask, 0, 0);
2926 g_object_unref(blank_mask);
2927 }
2928#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1);
2930 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask,
2931 &color, &color, 0, 0);
2932 gdk_bitmap_unref(blank_mask);
Bram Moolenaar98921892016-02-23 17:14:37 +01002933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934
2935 return cursor;
2936}
2937
2938#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 static void
2940mainwin_screen_changed_cb(GtkWidget *widget,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002941 GdkScreen *previous_screen UNUSED,
2942 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943{
2944 if (!gtk_widget_has_screen(widget))
2945 return;
2946
2947 /*
Bram Moolenaar49325942007-05-10 19:19:59 +00002948 * Recreate the invisible mouse cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 */
2950 if (gui.blank_pointer != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01002951# if GTK_CHECK_VERSION(3,0,0)
2952 g_object_unref(G_OBJECT(gui.blank_pointer));
2953# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 gdk_cursor_unref(gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01002955# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956
2957 gui.blank_pointer = create_blank_pointer();
2958
Bram Moolenaar98921892016-02-23 17:14:37 +01002959# if GTK_CHECK_VERSION(3,0,0)
2960 if (gui.pointer_hidden && gtk_widget_get_window(gui.drawarea) != NULL)
2961 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
2962 gui.blank_pointer);
2963# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 if (gui.pointer_hidden && gui.drawarea->window != NULL)
2965 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01002966# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967
2968 /*
2969 * Create a new PangoContext for this screen, and initialize it
2970 * with the current font if necessary.
2971 */
2972 if (gui.text_context != NULL)
2973 g_object_unref(gui.text_context);
2974
2975 gui.text_context = gtk_widget_create_pango_context(widget);
2976 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
2977
2978 if (gui.norm_font != NULL)
2979 {
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002980 gui_mch_init_font(p_guifont, FALSE);
Bram Moolenaar04a9d452006-03-27 21:03:26 +00002981 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 }
2983}
2984#endif /* HAVE_GTK_MULTIHEAD */
2985
2986/*
2987 * After the drawing area comes up, we calculate all colors and create the
2988 * dummy blank cursor.
2989 *
2990 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2991 * fact that the main VIM engine doesn't take them into account anywhere.
2992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002994drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995{
2996 GtkWidget *sbar;
Bram Moolenaar98921892016-02-23 17:14:37 +01002997#if GTK_CHECK_VERSION(3,0,0)
2998 GtkAllocation allocation;
2999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000
3001#ifdef FEAT_XIM
3002 xim_init();
3003#endif
3004 gui_mch_new_colors();
Bram Moolenaar98921892016-02-23 17:14:37 +01003005#if GTK_CHECK_VERSION(3,0,0)
3006 gui.surface = gdk_window_create_similar_surface(
3007 gtk_widget_get_window(widget),
3008 CAIRO_CONTENT_COLOR_ALPHA,
3009 gtk_widget_get_allocated_width(widget),
3010 gtk_widget_get_allocated_height(widget));
3011#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 gui.text_gc = gdk_gc_new(gui.drawarea->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01003013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014
3015 gui.blank_pointer = create_blank_pointer();
3016 if (gui.pointer_hidden)
Bram Moolenaar98921892016-02-23 17:14:37 +01003017#if GTK_CHECK_VERSION(3,0,0)
3018 gdk_window_set_cursor(gtk_widget_get_window(widget), gui.blank_pointer);
3019#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 gdk_window_set_cursor(widget->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01003021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022
3023 /* get the actual size of the scrollbars, if they are realized */
3024 sbar = firstwin->w_scrollbars[SBAR_LEFT].id;
3025 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT]
3026 && firstwin->w_scrollbars[SBAR_RIGHT].id))
3027 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id;
Bram Moolenaar98921892016-02-23 17:14:37 +01003028#if GTK_CHECK_VERSION(3,0,0)
3029 gtk_widget_get_allocation(sbar, &allocation);
3030 if (sbar && gtk_widget_get_realized(sbar) && allocation.width)
3031 gui.scrollbar_width = allocation.width;
3032#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.width)
3034 gui.scrollbar_width = sbar->allocation.width;
Bram Moolenaar98921892016-02-23 17:14:37 +01003035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036
3037 sbar = gui.bottom_sbar.id;
Bram Moolenaar98921892016-02-23 17:14:37 +01003038#if GTK_CHECK_VERSION(3,0,0)
3039 if (sbar && gtk_widget_get_realized(sbar) && allocation.height)
3040 gui.scrollbar_height = allocation.height;
3041#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.height)
3043 gui.scrollbar_height = sbar->allocation.height;
Bram Moolenaar98921892016-02-23 17:14:37 +01003044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045}
3046
3047/*
3048 * Properly clean up on shutdown.
3049 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003051drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
3053 /* Don't write messages to the GUI anymore */
3054 full_screen = FALSE;
3055
3056#ifdef FEAT_XIM
3057 im_shutdown();
3058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 if (gui.ascii_glyphs != NULL)
3060 {
3061 pango_glyph_string_free(gui.ascii_glyphs);
3062 gui.ascii_glyphs = NULL;
3063 }
3064 if (gui.ascii_font != NULL)
3065 {
3066 g_object_unref(gui.ascii_font);
3067 gui.ascii_font = NULL;
3068 }
3069 g_object_unref(gui.text_context);
3070 gui.text_context = NULL;
3071
Bram Moolenaar98921892016-02-23 17:14:37 +01003072#if GTK_CHECK_VERSION(3,0,0)
3073 if (gui.surface != NULL)
3074 {
3075 cairo_surface_destroy(gui.surface);
3076 gui.surface = NULL;
3077 }
3078#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 g_object_unref(gui.text_gc);
3080 gui.text_gc = NULL;
Bram Moolenaar98921892016-02-23 17:14:37 +01003081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082
Bram Moolenaar98921892016-02-23 17:14:37 +01003083#if GTK_CHECK_VERSION(3,0,0)
3084 g_object_unref(G_OBJECT(gui.blank_pointer));
3085#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 gdk_cursor_unref(gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01003087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 gui.blank_pointer = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089}
3090
Bram Moolenaara859f042016-11-17 19:11:55 +01003091#if GTK_CHECK_VERSION(3,22,2)
3092 static void
3093drawarea_style_updated_cb(GtkWidget *widget UNUSED,
3094 gpointer data UNUSED)
3095#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003097drawarea_style_set_cb(GtkWidget *widget UNUSED,
3098 GtkStyle *previous_style UNUSED,
3099 gpointer data UNUSED)
Bram Moolenaara859f042016-11-17 19:11:55 +01003100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101{
3102 gui_mch_new_colors();
3103}
3104
Bram Moolenaar98921892016-02-23 17:14:37 +01003105#if GTK_CHECK_VERSION(3,0,0)
3106 static gboolean
3107drawarea_configure_event_cb(GtkWidget *widget,
3108 GdkEventConfigure *event,
3109 gpointer data UNUSED)
3110{
3111 static int cur_width = 0;
3112 static int cur_height = 0;
3113
3114 g_return_val_if_fail(event
3115 && event->width >= 1 && event->height >= 1, TRUE);
3116
Bram Moolenaar182707a2016-11-21 20:55:58 +01003117# if GTK_CHECK_VERSION(3,22,2) && !GTK_CHECK_VERSION(3,22,4)
Bram Moolenaara859f042016-11-17 19:11:55 +01003118 /* As of 3.22.2, GdkWindows have started distributing configure events to
3119 * their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0).
3120 *
3121 * As can be seen from the implementation of move_native_children() and
3122 * configure_native_child() in gdkwindow.c, those functions actually
3123 * propagate configure events to every child, failing to distinguish
3124 * "native" one from non-native one.
3125 *
3126 * Naturally, configure events propagated to here like that are fallacious
3127 * and, as a matter of fact, they trigger a geometric collapse of
3128 * gui.drawarea in fullscreen and miximized modes.
3129 *
3130 * To filter out such nuisance events, we are making use of the fact that
3131 * the field send_event of such GdkEventConfigures is set to FALSE in
3132 * configure_native_child().
3133 *
3134 * Obviously, this is a terrible hack making GVim depend on GTK's
3135 * implementation details. Therefore, watch out any relevant internal
3136 * changes happening in GTK in the feature (sigh).
3137 */
Bram Moolenaar182707a2016-11-21 20:55:58 +01003138 /* Follow-up
3139 * After a few weeks later, the GdkWindow change mentioned above was
3140 * reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155).
3141 * The corresponding official release is 3.22.4. */
Bram Moolenaara859f042016-11-17 19:11:55 +01003142 if (event->send_event == FALSE)
3143 return TRUE;
3144# endif
3145
Bram Moolenaar98921892016-02-23 17:14:37 +01003146 if (event->width == cur_width && event->height == cur_height)
3147 return TRUE;
3148
3149 cur_width = event->width;
3150 cur_height = event->height;
3151
3152 if (gui.surface != NULL)
3153 cairo_surface_destroy(gui.surface);
3154
3155 gui.surface = gdk_window_create_similar_surface(
3156 gtk_widget_get_window(widget),
3157 CAIRO_CONTENT_COLOR_ALPHA,
3158 event->width, event->height);
3159
3160 gtk_widget_queue_draw(widget);
3161
3162 return TRUE;
3163}
3164#endif
3165
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166/*
3167 * Callback routine for the "delete_event" signal on the toplevel window.
3168 * Tries to vim gracefully, or refuses to exit with changed buffers.
3169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003171delete_event_cb(GtkWidget *widget UNUSED,
3172 GdkEventAny *event UNUSED,
3173 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174{
3175 gui_shell_closed();
3176 return TRUE;
3177}
3178
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003179#if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
3180 static int
3181get_item_dimensions(GtkWidget *widget, GtkOrientation orientation)
3182{
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003183# ifdef FEAT_GUI_GNOME
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003184 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL;
3185
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003186 if (using_gnome && widget != NULL)
3187 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003188 GtkWidget *parent;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003189 BonoboDockItem *dockitem;
3190
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003191 parent = gtk_widget_get_parent(widget);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003192 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM)
3193 {
3194 /* Only menu & toolbar are dock items. Could tabline be?
3195 * Seem to be only the 2 defined in GNOME */
3196 widget = parent;
3197 dockitem = BONOBO_DOCK_ITEM(widget);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003198
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003199 if (dockitem == NULL || dockitem->is_floating)
3200 return 0;
3201 item_orientation = bonobo_dock_item_get_orientation(dockitem);
3202 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003203 }
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003204# else
3205# define item_orientation GTK_ORIENTATION_HORIZONTAL
Bram Moolenaar98921892016-02-23 17:14:37 +01003206# endif
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003207
Bram Moolenaar98921892016-02-23 17:14:37 +01003208# if GTK_CHECK_VERSION(3,0,0)
3209 if (widget != NULL
3210 && item_orientation == orientation
3211 && gtk_widget_get_realized(widget)
3212 && gtk_widget_get_visible(widget))
3213# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003214 if (widget != NULL
3215 && item_orientation == orientation
3216 && GTK_WIDGET_REALIZED(widget)
3217 && GTK_WIDGET_VISIBLE(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01003218# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003219 {
Bram Moolenaar98921892016-02-23 17:14:37 +01003220# if GTK_CHECK_VERSION(3,0,0)
3221 GtkAllocation allocation;
3222
3223 gtk_widget_get_allocation(widget, &allocation);
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003224 return allocation.height;
Bram Moolenaar98921892016-02-23 17:14:37 +01003225# else
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003226# ifdef FEAT_GUI_GNOME
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003227 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3228 return widget->allocation.height;
3229 else
3230 return widget->allocation.width;
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003231# else
3232 return widget->allocation.height;
3233# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01003234# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003235 }
3236 return 0;
3237}
3238#endif
3239
3240 static int
3241get_menu_tool_width(void)
3242{
3243 int width = 0;
3244
3245#ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */
3246# ifdef FEAT_MENU
3247 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL);
3248# endif
3249# ifdef FEAT_TOOLBAR
3250 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL);
3251# endif
3252# ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003253 if (gui.tabline != NULL)
3254 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003255# endif
3256#endif
3257
3258 return width;
3259}
3260
3261 static int
3262get_menu_tool_height(void)
3263{
3264 int height = 0;
3265
3266#ifdef FEAT_MENU
3267 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL);
3268#endif
3269#ifdef FEAT_TOOLBAR
3270 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL);
3271#endif
3272#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003273 if (gui.tabline != NULL)
3274 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003275#endif
3276
3277 return height;
3278}
3279
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003280/* This controls whether we can set the real window hints at
3281 * start-up when in a GtkPlug.
3282 * 0 = normal processing (default)
3283 * 1 = init. hints set, no-one's tried to reset since last check
3284 * 2 = init. hints set, attempt made to change hints
3285 */
3286static int init_window_hints_state = 0;
3287
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003288 static void
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003289update_window_manager_hints(int force_width, int force_height)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003290{
3291 static int old_width = 0;
3292 static int old_height = 0;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003293 static int old_min_width = 0;
3294 static int old_min_height = 0;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003295 static int old_char_width = 0;
3296 static int old_char_height = 0;
3297
3298 int width;
3299 int height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003300 int min_width;
3301 int min_height;
3302
3303 /* At start-up, don't try to set the hints until the initial
3304 * values have been used (those that dictate our initial size)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003305 * Let forced (i.e., correct) values through always.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003306 */
3307 if (!(force_width && force_height) && init_window_hints_state > 0)
3308 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003309 /* Don't do it! */
3310 init_window_hints_state = 2;
3311 return;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003312 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003313
3314 /* This also needs to be done when the main window isn't there yet,
3315 * otherwise the hints don't work. */
3316 width = gui_get_base_width();
3317 height = gui_get_base_height();
3318# ifdef FEAT_MENU
3319 height += tabline_height() * gui.char_height;
3320# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003321 width += get_menu_tool_width();
3322 height += get_menu_tool_height();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003323
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003324 /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
Bram Moolenaar49325942007-05-10 19:19:59 +00003325 * their actual widget size. When we set our size ourselves (e.g.,
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003326 * 'set columns=' or init. -geom) we briefly set the min. to the size
3327 * we wish to be instead of the legitimate minimum so that we actually
3328 * resize correctly.
3329 */
3330 if (force_width && force_height)
3331 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003332 min_width = force_width;
3333 min_height = force_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003334 }
3335 else
3336 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003337 min_width = width + MIN_COLUMNS * gui.char_width;
3338 min_height = height + MIN_LINES * gui.char_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003339 }
3340
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003341 /* Avoid an expose event when the size didn't change. */
3342 if (width != old_width
3343 || height != old_height
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003344 || min_width != old_min_width
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003345 || min_height != old_min_height
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003346 || gui.char_width != old_char_width
3347 || gui.char_height != old_char_height)
3348 {
3349 GdkGeometry geometry;
3350 GdkWindowHints geometry_mask;
3351
3352 geometry.width_inc = gui.char_width;
3353 geometry.height_inc = gui.char_height;
3354 geometry.base_width = width;
3355 geometry.base_height = height;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003356 geometry.min_width = min_width;
3357 geometry.min_height = min_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003358 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC
3359 |GDK_HINT_MIN_SIZE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003360 /* Using gui.formwin as geometry widget doesn't work as expected
3361 * with GTK+ 2 -- dunno why. Presumably all the resizing hacks
3362 * in Vim confuse GTK+. */
3363 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin,
3364 &geometry, geometry_mask);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003365 old_width = width;
3366 old_height = height;
3367 old_min_width = min_width;
3368 old_min_height = min_height;
3369 old_char_width = gui.char_width;
3370 old_char_height = gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003371 }
3372}
3373
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374#ifdef FEAT_TOOLBAR
3375
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376/*
3377 * This extra effort wouldn't be necessary if we only used stock icons in the
3378 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
3379 * shouldn't be treated differently, thus we do need this.
3380 */
3381 static void
3382icon_size_changed_foreach(GtkWidget *widget, gpointer user_data)
3383{
3384 if (GTK_IS_IMAGE(widget))
3385 {
3386 GtkImage *image = (GtkImage *)widget;
3387
Bram Moolenaar98921892016-02-23 17:14:37 +01003388# if GTK_CHECK_VERSION(3,10,0)
3389 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_NAME)
3390 {
3391 const GtkIconSize icon_size = GPOINTER_TO_INT(user_data);
3392 const gchar *icon_name;
3393
3394 gtk_image_get_icon_name(image, &icon_name, NULL);
3395
3396 gtk_image_set_from_icon_name(image, icon_name, icon_size);
3397 }
3398# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 /* User-defined icons are stored in a GtkIconSet */
3400 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET)
3401 {
3402 GtkIconSet *icon_set;
3403 GtkIconSize icon_size;
3404
3405 gtk_image_get_icon_set(image, &icon_set, &icon_size);
3406 icon_size = (GtkIconSize)(long)user_data;
3407
3408 gtk_icon_set_ref(icon_set);
3409 gtk_image_set_from_icon_set(image, icon_set, icon_size);
3410 gtk_icon_set_unref(icon_set);
3411 }
Bram Moolenaar98921892016-02-23 17:14:37 +01003412# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 }
3414 else if (GTK_IS_CONTAINER(widget))
3415 {
3416 gtk_container_foreach((GtkContainer *)widget,
3417 &icon_size_changed_foreach,
3418 user_data);
3419 }
3420}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
3422 static void
3423set_toolbar_style(GtkToolbar *toolbar)
3424{
3425 GtkToolbarStyle style;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 GtkIconSize size;
3427 GtkIconSize oldsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3430 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3431 style = GTK_TOOLBAR_BOTH_HORIZ;
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003432 else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 == (TOOLBAR_TEXT | TOOLBAR_ICONS))
3434 style = GTK_TOOLBAR_BOTH;
3435 else if (toolbar_flags & TOOLBAR_TEXT)
3436 style = GTK_TOOLBAR_TEXT;
3437 else
3438 style = GTK_TOOLBAR_ICONS;
3439
3440 gtk_toolbar_set_style(toolbar, style);
Bram Moolenaar98921892016-02-23 17:14:37 +01003441# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 switch (tbis_flags)
3446 {
3447 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break;
3448 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break;
3449 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break;
3450 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
Bram Moolenaarbeb003b2016-03-08 22:47:17 +01003451 case TBIS_HUGE: size = GTK_ICON_SIZE_DND; break;
3452 case TBIS_GIANT: size = GTK_ICON_SIZE_DIALOG; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 default: size = GTK_ICON_SIZE_INVALID; break;
3454 }
3455 oldsize = gtk_toolbar_get_icon_size(toolbar);
3456
3457 if (size == GTK_ICON_SIZE_INVALID)
3458 {
3459 /* Let global user preferences decide the icon size. */
3460 gtk_toolbar_unset_icon_size(toolbar);
3461 size = gtk_toolbar_get_icon_size(toolbar);
3462 }
3463 if (size != oldsize)
3464 {
3465 gtk_container_foreach(GTK_CONTAINER(toolbar),
3466 &icon_size_changed_foreach,
3467 GINT_TO_POINTER((int)size));
3468 }
3469 gtk_toolbar_set_icon_size(toolbar, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470}
3471
3472#endif /* FEAT_TOOLBAR */
3473
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003474#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3475static int ignore_tabline_evt = FALSE;
Bram Moolenaara5621492006-02-25 21:55:24 +00003476static GtkWidget *tabline_menu;
Bram Moolenaar98921892016-02-23 17:14:37 +01003477# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003478static GtkTooltips *tabline_tooltip;
Bram Moolenaar98921892016-02-23 17:14:37 +01003479# endif
Bram Moolenaara5621492006-02-25 21:55:24 +00003480static int clicked_page; /* page clicked in tab line */
3481
3482/*
3483 * Handle selecting an item in the tab line popup menu.
3484 */
Bram Moolenaara5621492006-02-25 21:55:24 +00003485 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003486tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data)
Bram Moolenaara5621492006-02-25 21:55:24 +00003487{
Bram Moolenaara5621492006-02-25 21:55:24 +00003488 /* Add the string cmd into input buffer */
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003489 send_tabline_menu_event(clicked_page, (int)(long)user_data);
Bram Moolenaara5621492006-02-25 21:55:24 +00003490}
3491
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003492 static void
3493add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp)
3494{
3495 GtkWidget *item;
3496 char_u *utf_text;
3497
3498 utf_text = CONVERT_TO_UTF8(text);
3499 item = gtk_menu_item_new_with_label((const char *)utf_text);
3500 gtk_widget_show(item);
3501 CONVERT_TO_UTF8_FREE(utf_text);
3502
3503 gtk_container_add(GTK_CONTAINER(menu), item);
Bram Moolenaar98921892016-02-23 17:14:37 +01003504# if GTK_CHECK_VERSION(3,0,0)
3505 g_signal_connect(G_OBJECT(item), "activate",
3506 G_CALLBACK(tabline_menu_handler),
3507 GINT_TO_POINTER(resp));
3508# else
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003509 gtk_signal_connect(GTK_OBJECT(item), "activate",
3510 GTK_SIGNAL_FUNC(tabline_menu_handler),
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003511 (gpointer)(long)resp);
Bram Moolenaar98921892016-02-23 17:14:37 +01003512# endif
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003513}
3514
Bram Moolenaara5621492006-02-25 21:55:24 +00003515/*
Bram Moolenaara5621492006-02-25 21:55:24 +00003516 * Create a menu for the tab line.
3517 */
3518 static GtkWidget *
3519create_tabline_menu(void)
3520{
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003521 GtkWidget *menu;
Bram Moolenaara5621492006-02-25 21:55:24 +00003522
3523 menu = gtk_menu_new();
Bram Moolenaar7098ee52015-06-09 19:14:24 +02003524 if (first_tabpage->tp_next != NULL)
3525 add_tabline_menu_item(menu, (char_u *)_("Close tab"),
3526 TABLINE_MENU_CLOSE);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003527 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW);
3528 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN);
Bram Moolenaara5621492006-02-25 21:55:24 +00003529
3530 return menu;
3531}
3532
3533 static gboolean
3534on_tabline_menu(GtkWidget *widget, GdkEvent *event)
3535{
3536 /* Was this button press event ? */
3537 if (event->type == GDK_BUTTON_PRESS)
3538 {
3539 GdkEventButton *bevent = (GdkEventButton *)event;
3540 int x = bevent->x;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003541 int y = bevent->y;
3542 GtkWidget *tabwidget;
3543 GdkWindow *tabwin;
Bram Moolenaara5621492006-02-25 21:55:24 +00003544
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003545 /* When ignoring events return TRUE so that the selected page doesn't
3546 * change. */
3547 if (hold_gui_events
3548# ifdef FEAT_CMDWIN
3549 || cmdwin_type != 0
3550# endif
3551 )
3552 return TRUE;
3553
Bram Moolenaar98921892016-02-23 17:14:37 +01003554# if GTK_CHECK_VERSION(3,0,0)
3555 tabwin = gui_gtk_window_at_position(gui.mainwin, &x, &y);
3556# else
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003557 tabwin = gdk_window_at_pointer(&x, &y);
Bram Moolenaar98921892016-02-23 17:14:37 +01003558# endif
3559
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003560 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget);
Bram Moolenaar98921892016-02-23 17:14:37 +01003561# if GTK_CHECK_VERSION(3,0,0)
3562 clicked_page = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(tabwidget),
3563 "tab_num"));
3564# else
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003565 clicked_page = (int)(long)gtk_object_get_user_data(
3566 GTK_OBJECT(tabwidget));
Bram Moolenaar98921892016-02-23 17:14:37 +01003567# endif
Bram Moolenaara5621492006-02-25 21:55:24 +00003568
3569 /* If the event was generated for 3rd button popup the menu. */
3570 if (bevent->button == 3)
3571 {
Bram Moolenaara859f042016-11-17 19:11:55 +01003572# if GTK_CHECK_VERSION(3,22,2)
3573 gtk_menu_popup_at_pointer(GTK_MENU(widget), event);
3574# else
Bram Moolenaara5621492006-02-25 21:55:24 +00003575 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
3576 bevent->button, bevent->time);
Bram Moolenaara859f042016-11-17 19:11:55 +01003577# endif
Bram Moolenaara5621492006-02-25 21:55:24 +00003578 /* We handled the event. */
3579 return TRUE;
3580 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003581 else if (bevent->button == 1)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003582 {
Bram Moolenaar96351572006-05-05 21:16:59 +00003583 if (clicked_page == 0)
3584 {
Bram Moolenaar07354542007-09-15 12:07:46 +00003585 /* Click after all tabs moves to next tab page. When "x" is
3586 * small guess it's the left button. */
Bram Moolenaara3f41662010-07-11 19:01:06 +02003587 send_tabline_event(x < 50 ? -1 : 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00003588 }
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003589 }
Bram Moolenaara5621492006-02-25 21:55:24 +00003590 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003591
Bram Moolenaara5621492006-02-25 21:55:24 +00003592 /* We didn't handle the event. */
3593 return FALSE;
3594}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003595
3596/*
3597 * Handle selecting one of the tabs.
3598 */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003599 static void
3600on_select_tab(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003601 GtkNotebook *notebook UNUSED,
Bram Moolenaar98921892016-02-23 17:14:37 +01003602# if GTK_CHECK_VERSION(3,0,0)
3603 gpointer *page UNUSED,
3604# else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003605 GtkNotebookPage *page UNUSED,
Bram Moolenaar98921892016-02-23 17:14:37 +01003606# endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00003607 gint idx,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003608 gpointer data UNUSED)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003609{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003610 if (!ignore_tabline_evt)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003611 {
Bram Moolenaara3f41662010-07-11 19:01:06 +02003612 send_tabline_event(idx + 1);
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003613 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003614}
3615
3616/*
3617 * Show or hide the tabline.
3618 */
3619 void
3620gui_mch_show_tabline(int showit)
3621{
3622 if (gui.tabline == NULL)
3623 return;
3624
3625 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)))
3626 {
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003627 /* Note: this may cause a resize event */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003628 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003629 update_window_manager_hints(0, 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00003630 if (showit)
Bram Moolenaar98921892016-02-23 17:14:37 +01003631# if GTK_CHECK_VERSION(3,0,0)
3632 gtk_widget_set_can_focus(GTK_WIDGET(gui.tabline), FALSE);
3633# else
Bram Moolenaar96351572006-05-05 21:16:59 +00003634 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui.tabline), GTK_CAN_FOCUS);
Bram Moolenaar98921892016-02-23 17:14:37 +01003635# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003636 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003637
3638 gui_mch_update();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003639}
3640
3641/*
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003642 * Return TRUE when tabline is displayed.
3643 */
3644 int
3645gui_mch_showing_tabline(void)
3646{
3647 return gui.tabline != NULL
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003648 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline));
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003649}
3650
3651/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003652 * Update the labels of the tabline.
3653 */
3654 void
3655gui_mch_update_tabline(void)
3656{
3657 GtkWidget *page;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003658 GtkWidget *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003659 GtkWidget *label;
3660 tabpage_T *tp;
3661 int nr = 0;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003662 int tab_num;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003663 int curtabidx = 0;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003664 char_u *labeltext;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003665
3666 if (gui.tabline == NULL)
3667 return;
3668
3669 ignore_tabline_evt = TRUE;
3670
3671 /* Add a label for each tab page. They all contain the same text area. */
3672 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3673 {
3674 if (tp == curtab)
3675 curtabidx = nr;
3676
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003677 tab_num = nr + 1;
3678
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003679 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr);
3680 if (page == NULL)
3681 {
3682 /* Add notebook page */
Bram Moolenaar98921892016-02-23 17:14:37 +01003683# if GTK_CHECK_VERSION(3,2,0)
3684 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3685 gtk_box_set_homogeneous(GTK_BOX(page), FALSE);
3686# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003687 page = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003688# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003689 gtk_widget_show(page);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003690 event_box = gtk_event_box_new();
3691 gtk_widget_show(event_box);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003692 label = gtk_label_new("-Empty-");
Bram Moolenaar98921892016-02-23 17:14:37 +01003693# if !GTK_CHECK_VERSION(3,14,0)
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003694 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01003695# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003696 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003697 gtk_widget_show(label);
3698 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline),
3699 page,
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003700 event_box,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003701 nr++);
3702 }
3703
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003704 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page);
Bram Moolenaar98921892016-02-23 17:14:37 +01003705# if GTK_CHECK_VERSION(3,0,0)
3706 g_object_set_data(G_OBJECT(event_box), "tab_num",
3707 GINT_TO_POINTER(tab_num));
3708# else
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003709 gtk_object_set_user_data(GTK_OBJECT(event_box),
3710 (gpointer)(long)tab_num);
Bram Moolenaar98921892016-02-23 17:14:37 +01003711# endif
3712# if GTK_CHECK_VERSION(3,0,0)
3713 label = gtk_bin_get_child(GTK_BIN(event_box));
3714# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003715 label = GTK_BIN(event_box)->child;
Bram Moolenaar98921892016-02-23 17:14:37 +01003716# endif
Bram Moolenaar57657d82006-04-21 22:12:41 +00003717 get_tabline_label(tp, FALSE);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003718 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003719 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003720 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003721
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003722 get_tabline_label(tp, TRUE);
3723 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar98921892016-02-23 17:14:37 +01003724# if GTK_CHECK_VERSION(3,0,0)
3725 gtk_widget_set_tooltip_text(event_box, (const gchar *)labeltext);
3726# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003727 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box,
3728 (const char *)labeltext, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01003729# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003730 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003731 }
3732
3733 /* Remove any old labels. */
3734 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL)
3735 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr);
3736
Bram Moolenaar98921892016-02-23 17:14:37 +01003737# if GTK_CHECK_VERSION(3,0,0)
3738 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
3739 gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
3740# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003741 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003742 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
Bram Moolenaar98921892016-02-23 17:14:37 +01003743# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003744
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003745 /* Make sure everything is in place before drawing text. */
3746 gui_mch_update();
3747
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003748 ignore_tabline_evt = FALSE;
3749}
3750
3751/*
3752 * Set the current tab to "nr". First tab is 1.
3753 */
3754 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003755gui_mch_set_curtab(int nr)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003756{
3757 if (gui.tabline == NULL)
3758 return;
3759
3760 ignore_tabline_evt = TRUE;
Bram Moolenaar98921892016-02-23 17:14:37 +01003761# if GTK_CHECK_VERSION(3,0,0)
3762 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
3763 gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
3764# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003765 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003766 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
Bram Moolenaar98921892016-02-23 17:14:37 +01003767# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003768 ignore_tabline_evt = FALSE;
3769}
3770
3771#endif /* FEAT_GUI_TABLINE */
3772
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773/*
Bram Moolenaara76638f2010-06-05 12:49:46 +02003774 * Add selection targets for PRIMARY and CLIPBOARD selections.
3775 */
3776 void
3777gui_gtk_set_selection_targets(void)
3778{
3779 int i, j = 0;
3780 int n_targets = N_SELECTION_TARGETS;
3781 GtkTargetEntry targets[N_SELECTION_TARGETS];
3782
3783 for (i = 0; i < (int)N_SELECTION_TARGETS; ++i)
3784 {
Bram Moolenaar29695702012-05-18 17:03:18 +02003785 /* OpenOffice tries to use TARGET_HTML and fails when we don't
Bram Moolenaara76638f2010-06-05 12:49:46 +02003786 * return something, instead of trying another target. Therefore only
3787 * offer TARGET_HTML when it works. */
3788 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3789 n_targets--;
3790 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003791 targets[j++] = selection_targets[i];
3792 }
3793
3794 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)GDK_SELECTION_PRIMARY);
3795 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)clip_plus.gtk_sel_atom);
3796 gtk_selection_add_targets(gui.drawarea,
3797 (GdkAtom)GDK_SELECTION_PRIMARY,
3798 targets, n_targets);
3799 gtk_selection_add_targets(gui.drawarea,
3800 (GdkAtom)clip_plus.gtk_sel_atom,
3801 targets, n_targets);
3802}
3803
3804/*
3805 * Set up for receiving DND items.
3806 */
3807 void
3808gui_gtk_set_dnd_targets(void)
3809{
3810 int i, j = 0;
3811 int n_targets = N_DND_TARGETS;
3812 GtkTargetEntry targets[N_DND_TARGETS];
3813
3814 for (i = 0; i < (int)N_DND_TARGETS; ++i)
3815 {
Bram Moolenaarb931d742011-10-26 11:36:25 +02003816 if (!clip_html && dnd_targets[i].info == TARGET_HTML)
Bram Moolenaara76638f2010-06-05 12:49:46 +02003817 n_targets--;
3818 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003819 targets[j++] = dnd_targets[i];
3820 }
3821
3822 gtk_drag_dest_unset(gui.drawarea);
3823 gtk_drag_dest_set(gui.drawarea,
3824 GTK_DEST_DEFAULT_ALL,
3825 targets, n_targets,
Bram Moolenaarba7cc9f2011-02-25 17:10:27 +01003826 GDK_ACTION_COPY | GDK_ACTION_MOVE);
Bram Moolenaara76638f2010-06-05 12:49:46 +02003827}
3828
3829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3831 * Returns OK for success, FAIL when the GUI can't be started.
3832 */
3833 int
3834gui_mch_init(void)
3835{
3836 GtkWidget *vbox;
3837
3838#ifdef FEAT_GUI_GNOME
3839 /* Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3840 * exits on failure, but that's a non-issue because we already called
3841 * gtk_init_check() in gui_mch_init_check(). */
3842 if (using_gnome)
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003843 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT,
3845 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL);
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003846# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
Bram Moolenaar1ff32c52014-04-29 15:11:43 +02003847 {
3848 char *p = setlocale(LC_NUMERIC, NULL);
3849
3850 /* Make sure strtod() uses a decimal point, not a comma. Gnome
3851 * init may change it. */
3852 if (p == NULL || strcmp(p, "C") != 0)
3853 setlocale(LC_NUMERIC, "C");
3854 }
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003855# endif
3856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857#endif
3858 vim_free(gui_argv);
3859 gui_argv = NULL;
3860
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003861#if GLIB_CHECK_VERSION(2,1,3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 /* Set the human-readable application name */
3863 g_set_application_name("Vim");
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 /*
3866 * Force UTF-8 output no matter what the value of 'encoding' is.
3867 * did_set_string_option() in option.c prohibits changing 'termencoding'
3868 * to something else than UTF-8 if the GUI is in use.
3869 */
3870 set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0);
3871
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003872#ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 gui_gtk_register_stock_icons();
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 /* FIXME: Need to install the classic icons and a gtkrc.classic file.
3876 * The hard part is deciding install locations and the Makefile magic. */
Bram Moolenaar98921892016-02-23 17:14:37 +01003877#if !GTK_CHECK_VERSION(3,0,0)
3878# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 gtk_rc_parse("gtkrc");
Bram Moolenaar98921892016-02-23 17:14:37 +01003880# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881#endif
3882
3883 /* Initialize values */
3884 gui.border_width = 2;
3885 gui.scrollbar_width = SB_DEFAULT_WIDTH;
3886 gui.scrollbar_height = SB_DEFAULT_WIDTH;
Bram Moolenaar36edf062016-07-21 22:10:12 +02003887#if GTK_CHECK_VERSION(3,0,0)
3888 gui.fgcolor = g_new(GdkRGBA, 1);
3889 gui.bgcolor = g_new(GdkRGBA, 1);
3890 gui.spcolor = g_new(GdkRGBA, 1);
3891#else
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003892 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 gui.fgcolor = g_new0(GdkColor, 1);
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003894 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 gui.bgcolor = g_new0(GdkColor, 1);
Bram Moolenaarf36d3692005-03-15 22:48:14 +00003896 /* LINTED: avoid warning: conversion to 'unsigned long' */
3897 gui.spcolor = g_new0(GdkColor, 1);
Bram Moolenaar36edf062016-07-21 22:10:12 +02003898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899
3900 /* Initialise atoms */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00003901 html_atom = gdk_atom_intern("text/html", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903
3904 /* Set default foreground and background colors. */
3905 gui.norm_pixel = gui.def_norm_pixel;
3906 gui.back_pixel = gui.def_back_pixel;
3907
3908 if (gtk_socket_id != 0)
3909 {
3910 GtkWidget *plug;
3911
3912 /* Use GtkSocket from another app. */
3913#ifdef HAVE_GTK_MULTIHEAD
3914 plug = gtk_plug_new_for_display(gdk_display_get_default(),
3915 gtk_socket_id);
3916#else
3917 plug = gtk_plug_new(gtk_socket_id);
3918#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01003919#if GTK_CHECK_VERSION(3,0,0)
3920 if (plug != NULL && gtk_plug_get_socket_window(GTK_PLUG(plug)) != NULL)
3921#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 if (plug != NULL && GTK_PLUG(plug)->socket_window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01003923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 {
3925 gui.mainwin = plug;
3926 }
3927 else
3928 {
3929 g_warning("Connection to GTK+ socket (ID %u) failed",
3930 (unsigned int)gtk_socket_id);
3931 /* Pretend we never wanted it if it failed (get own window) */
3932 gtk_socket_id = 0;
3933 }
3934 }
3935
3936 if (gtk_socket_id == 0)
3937 {
3938#ifdef FEAT_GUI_GNOME
3939 if (using_gnome)
3940 {
3941 gui.mainwin = gnome_app_new("Vim", NULL);
3942# ifdef USE_XSMP
3943 /* Use the GNOME save-yourself functionality now. */
3944 xsmp_close();
3945# endif
3946 }
3947 else
3948#endif
3949 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3950 }
3951
3952 gtk_widget_set_name(gui.mainwin, "vim-main-window");
3953
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 /* Create the PangoContext used for drawing all text. */
3955 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
3956 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957
Bram Moolenaar98921892016-02-23 17:14:37 +01003958#if GTK_CHECK_VERSION(3,0,0)
3959 gtk_container_set_border_width(GTK_CONTAINER(gui.mainwin), 0);
3960#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 gtk_container_border_width(GTK_CONTAINER(gui.mainwin), 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK);
3964
Bram Moolenaar98921892016-02-23 17:14:37 +01003965#if GTK_CHECK_VERSION(3,0,0)
3966 g_signal_connect(G_OBJECT(gui.mainwin), "delete-event",
3967 G_CALLBACK(&delete_event_cb), NULL);
3968
3969 g_signal_connect(G_OBJECT(gui.mainwin), "realize",
3970 G_CALLBACK(&mainwin_realize), NULL);
3971#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "delete_event",
3973 GTK_SIGNAL_FUNC(&delete_event_cb), NULL);
3974
3975 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "realize",
3976 GTK_SIGNAL_FUNC(&mainwin_realize), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01003977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978#ifdef HAVE_GTK_MULTIHEAD
3979 g_signal_connect(G_OBJECT(gui.mainwin), "screen_changed",
3980 G_CALLBACK(&mainwin_screen_changed_cb), NULL);
3981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 gui.accel_group = gtk_accel_group_new();
3983 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003985 /* A vertical box holds the menubar, toolbar and main text window. */
Bram Moolenaar98921892016-02-23 17:14:37 +01003986#if GTK_CHECK_VERSION(3,2,0)
3987 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3988 gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE);
3989#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 vbox = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992
3993#ifdef FEAT_GUI_GNOME
3994 if (using_gnome)
3995 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003996# if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 /* automagically restore menubar/toolbar placement */
3998 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE);
3999# endif
4000 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox);
4001 }
4002 else
4003#endif
4004 {
4005 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox);
4006 gtk_widget_show(vbox);
4007 }
4008
4009#ifdef FEAT_MENU
4010 /*
4011 * Create the menubar and handle
4012 */
4013 gui.menubar = gtk_menu_bar_new();
4014 gtk_widget_set_name(gui.menubar, "vim-menubar");
4015
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004016 /* Avoid that GTK takes <F10> away from us. */
4017 {
4018 GtkSettings *gtk_settings;
4019
4020 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
4021 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
4022 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004023
4024
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025# ifdef FEAT_GUI_GNOME
4026 if (using_gnome)
4027 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 BonoboDockItem *dockitem;
4029
4030 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar));
4031 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
4032 GNOME_APP_MENUBAR_NAME);
Bram Moolenaardb552d602006-03-23 22:59:57 +00004033 /* We don't want the menu to float. */
4034 bonobo_dock_item_set_behavior(dockitem,
4035 bonobo_dock_item_get_behavior(dockitem)
4036 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 gui.menubar_h = GTK_WIDGET(dockitem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
4039 else
4040# endif /* FEAT_GUI_GNOME */
4041 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00004042 /* Always show the menubar, otherwise <F10> doesn't work. It may be
4043 * disabled in gui_init() later. */
4044 gtk_widget_show(gui.menubar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0);
4046 }
4047#endif /* FEAT_MENU */
4048
4049#ifdef FEAT_TOOLBAR
4050 /*
4051 * Create the toolbar and handle
4052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 /* some aesthetics on the toolbar */
Bram Moolenaar98921892016-02-23 17:14:37 +01004054# ifdef USE_GTK3
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01004055 /* TODO: Add GTK+ 3 code here using GtkCssProvider if necessary. */
Bram Moolenaar98921892016-02-23 17:14:37 +01004056 /* N.B. Since the default value of GtkToolbar::button-relief is
4057 * GTK_RELIEF_NONE, there's no need to specify that, probably. */
4058# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 gtk_rc_parse_string(
4060 "style \"vim-toolbar-style\" {\n"
4061 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
4062 "}\n"
4063 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
Bram Moolenaar98921892016-02-23 17:14:37 +01004064# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 gui.toolbar = gtk_toolbar_new();
4066 gtk_widget_set_name(gui.toolbar, "vim-toolbar");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
4068
4069# ifdef FEAT_GUI_GNOME
4070 if (using_gnome)
4071 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 BonoboDockItem *dockitem;
4073
4074 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar));
4075 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
4076 GNOME_APP_TOOLBAR_NAME);
4077 gui.toolbar_h = GTK_WIDGET(dockitem);
Bram Moolenaare580b0c2006-03-21 21:33:03 +00004078 /* When the toolbar is floating it gets stuck. So long as that isn't
Bram Moolenaardb552d602006-03-23 22:59:57 +00004079 * fixed let's disallow floating. */
Bram Moolenaare580b0c2006-03-21 21:33:03 +00004080 bonobo_dock_item_set_behavior(dockitem,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004081 bonobo_dock_item_get_behavior(dockitem)
4082 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 }
4085 else
4086# endif /* FEAT_GUI_GNOME */
4087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 if (vim_strchr(p_go, GO_TOOLBAR) != NULL
4089 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
4090 gtk_widget_show(gui.toolbar);
4091 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0);
4092 }
4093#endif /* FEAT_TOOLBAR */
4094
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004095#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004096 /*
4097 * Use a Notebook for the tab pages labels. The labels are hidden by
4098 * default.
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004099 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004100 gui.tabline = gtk_notebook_new();
4101 gtk_widget_show(gui.tabline);
4102 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0);
4103 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
4104 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004105 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01004106# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar96351572006-05-05 21:16:59 +00004107 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01004108# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004109
Bram Moolenaar98921892016-02-23 17:14:37 +01004110# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004111 tabline_tooltip = gtk_tooltips_new();
4112 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip));
Bram Moolenaar98921892016-02-23 17:14:37 +01004113# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004114
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004115 {
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004116 GtkWidget *page, *label, *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004117
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004118 /* Add the first tab. */
Bram Moolenaar98921892016-02-23 17:14:37 +01004119# if GTK_CHECK_VERSION(3,2,0)
4120 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
4121 gtk_box_set_homogeneous(GTK_BOX(page), FALSE);
4122# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004123 page = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01004124# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004125 gtk_widget_show(page);
4126 gtk_container_add(GTK_CONTAINER(gui.tabline), page);
4127 label = gtk_label_new("-Empty-");
4128 gtk_widget_show(label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004129 event_box = gtk_event_box_new();
4130 gtk_widget_show(event_box);
Bram Moolenaar98921892016-02-23 17:14:37 +01004131# if GTK_CHECK_VERSION(3,0,0)
4132 g_object_set_data(G_OBJECT(event_box), "tab_num", GINT_TO_POINTER(1L));
4133# else
Bram Moolenaar47b46d72008-07-02 19:05:48 +00004134 gtk_object_set_user_data(GTK_OBJECT(event_box), (gpointer)1L);
Bram Moolenaar98921892016-02-23 17:14:37 +01004135# endif
4136# if !GTK_CHECK_VERSION(3,14,0)
Bram Moolenaar8ea91232006-04-28 22:41:43 +00004137 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01004138# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004139 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004140 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004141 }
Bram Moolenaar54a709e2006-05-04 21:57:11 +00004142
Bram Moolenaar98921892016-02-23 17:14:37 +01004143# if GTK_CHECK_VERSION(3,0,0)
4144 g_signal_connect(G_OBJECT(gui.tabline), "switch-page",
4145 G_CALLBACK(on_select_tab), NULL);
4146# else
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004147 gtk_signal_connect(GTK_OBJECT(gui.tabline), "switch_page",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004148 GTK_SIGNAL_FUNC(on_select_tab), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004149# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004150
4151 /* Create a popup menu for the tab line and connect it. */
4152 tabline_menu = create_tabline_menu();
Bram Moolenaar98921892016-02-23 17:14:37 +01004153# if GTK_CHECK_VERSION(3,0,0)
4154 g_signal_connect_swapped(G_OBJECT(gui.tabline), "button-press-event",
4155 G_CALLBACK(on_tabline_menu), G_OBJECT(tabline_menu));
4156# else
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004157 gtk_signal_connect_object(GTK_OBJECT(gui.tabline), "button_press_event",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004158 GTK_SIGNAL_FUNC(on_tabline_menu), GTK_OBJECT(tabline_menu));
Bram Moolenaar98921892016-02-23 17:14:37 +01004159# endif
4160#endif /* FEAT_GUI_TABLINE */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004161
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 gui.formwin = gtk_form_new();
Bram Moolenaar98921892016-02-23 17:14:37 +01004163#if GTK_CHECK_VERSION(3,0,0)
4164 gtk_container_set_border_width(GTK_CONTAINER(gui.formwin), 0);
4165#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 gtk_container_border_width(GTK_CONTAINER(gui.formwin), 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01004167#endif
4168#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK);
Bram Moolenaar98921892016-02-23 17:14:37 +01004170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171
4172 gui.drawarea = gtk_drawing_area_new();
Bram Moolenaara859f042016-11-17 19:11:55 +01004173#if GTK_CHECK_VERSION(3,22,2)
4174 gtk_widget_set_name(gui.drawarea, "vim-gui-drawarea");
4175#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004176#if GTK_CHECK_VERSION(3,0,0)
4177 gui.surface = NULL;
4178 gui.by_signal = FALSE;
4179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180
4181 /* Determine which events we will filter. */
4182 gtk_widget_set_events(gui.drawarea,
4183 GDK_EXPOSURE_MASK |
4184 GDK_ENTER_NOTIFY_MASK |
4185 GDK_LEAVE_NOTIFY_MASK |
4186 GDK_BUTTON_PRESS_MASK |
4187 GDK_BUTTON_RELEASE_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 GDK_SCROLL_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 GDK_KEY_PRESS_MASK |
4190 GDK_KEY_RELEASE_MASK |
4191 GDK_POINTER_MOTION_MASK |
4192 GDK_POINTER_MOTION_HINT_MASK);
4193
4194 gtk_widget_show(gui.drawarea);
4195 gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0);
4196 gtk_widget_show(gui.formwin);
4197 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0);
4198
4199 /* For GtkSockets, key-presses must go to the focus widget (drawarea)
4200 * and not the window. */
Bram Moolenaar98921892016-02-23 17:14:37 +01004201#if GTK_CHECK_VERSION(3,0,0)
4202 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
4203 : G_OBJECT(gui.drawarea),
4204 "key-press-event",
4205 G_CALLBACK(key_press_event), NULL);
4206#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 gtk_signal_connect((gtk_socket_id == 0) ? GTK_OBJECT(gui.mainwin)
4208 : GTK_OBJECT(gui.drawarea),
4209 "key_press_event",
4210 GTK_SIGNAL_FUNC(key_press_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004211#endif
4212#if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 /* Also forward key release events for the benefit of GTK+ 2 input
4214 * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */
4215 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
4216 : G_OBJECT(gui.drawarea),
Bram Moolenaar98921892016-02-23 17:14:37 +01004217 "key-release-event",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 G_CALLBACK(&key_release_event), NULL);
4219#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004220#if GTK_CHECK_VERSION(3,0,0)
4221 g_signal_connect(G_OBJECT(gui.drawarea), "realize",
4222 G_CALLBACK(drawarea_realize_cb), NULL);
4223 g_signal_connect(G_OBJECT(gui.drawarea), "unrealize",
4224 G_CALLBACK(drawarea_unrealize_cb), NULL);
4225 g_signal_connect(G_OBJECT(gui.drawarea), "configure-event",
4226 G_CALLBACK(drawarea_configure_event_cb), NULL);
Bram Moolenaara859f042016-11-17 19:11:55 +01004227# if GTK_CHECK_VERSION(3,22,2)
4228 g_signal_connect_after(G_OBJECT(gui.drawarea), "style-updated",
4229 G_CALLBACK(&drawarea_style_updated_cb), NULL);
4230# else
Bram Moolenaar98921892016-02-23 17:14:37 +01004231 g_signal_connect_after(G_OBJECT(gui.drawarea), "style-set",
4232 G_CALLBACK(&drawarea_style_set_cb), NULL);
Bram Moolenaara859f042016-11-17 19:11:55 +01004233# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004234#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "realize",
4236 GTK_SIGNAL_FUNC(drawarea_realize_cb), NULL);
4237 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "unrealize",
4238 GTK_SIGNAL_FUNC(drawarea_unrealize_cb), NULL);
4239
4240 gtk_signal_connect_after(GTK_OBJECT(gui.drawarea), "style_set",
4241 GTK_SIGNAL_FUNC(&drawarea_style_set_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
Bram Moolenaar98921892016-02-23 17:14:37 +01004244#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 gui.visibility = GDK_VISIBILITY_UNOBSCURED;
Bram Moolenaar98921892016-02-23 17:14:37 +01004246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247
4248#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
4249 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE);
4250 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE);
4251#endif
4252
4253 if (gtk_socket_id != 0)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004254 /* make sure keyboard input can go to the drawarea */
Bram Moolenaar98921892016-02-23 17:14:37 +01004255#if GTK_CHECK_VERSION(3,0,0)
4256 gtk_widget_set_can_focus(gui.drawarea, TRUE);
4257#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 GTK_WIDGET_SET_FLAGS(gui.drawarea, GTK_CAN_FOCUS);
Bram Moolenaar98921892016-02-23 17:14:37 +01004259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260
4261 /*
4262 * Set clipboard specific atoms
4263 */
4264 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY;
4267 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE);
4268
4269 /*
4270 * Start out by adding the configured border width into the border offset.
4271 */
4272 gui.border_offset = gui.border_width;
4273
Bram Moolenaar98921892016-02-23 17:14:37 +01004274#if GTK_CHECK_VERSION(3,0,0)
4275 g_signal_connect(G_OBJECT(gui.drawarea), "draw",
4276 G_CALLBACK(draw_event), NULL);
4277#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event",
4279 GTK_SIGNAL_FUNC(visibility_event), NULL);
4280 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event",
4281 GTK_SIGNAL_FUNC(expose_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283
4284 /*
4285 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
4286 * Only needed for some window managers.
4287 */
4288 if (vim_strchr(p_go, GO_POINTER) != NULL)
4289 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004290#if GTK_CHECK_VERSION(3,0,0)
4291 g_signal_connect(G_OBJECT(gui.drawarea), "leave-notify-event",
4292 G_CALLBACK(leave_notify_event), NULL);
4293 g_signal_connect(G_OBJECT(gui.drawarea), "enter-notify-event",
4294 G_CALLBACK(enter_notify_event), NULL);
4295#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "leave_notify_event",
4297 GTK_SIGNAL_FUNC(leave_notify_event), NULL);
4298 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "enter_notify_event",
4299 GTK_SIGNAL_FUNC(enter_notify_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004303 /* Real windows can get focus ... GtkPlug, being a mere container can't,
4304 * only its widgets. Arguably, this could be common code and we not use
4305 * the window focus at all, but let's be safe.
4306 */
4307 if (gtk_socket_id == 0)
4308 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004309#if GTK_CHECK_VERSION(3,0,0)
4310 g_signal_connect(G_OBJECT(gui.mainwin), "focus-out-event",
4311 G_CALLBACK(focus_out_event), NULL);
4312 g_signal_connect(G_OBJECT(gui.mainwin), "focus-in-event",
4313 G_CALLBACK(focus_in_event), NULL);
4314#else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004315 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_out_event",
4316 GTK_SIGNAL_FUNC(focus_out_event), NULL);
4317 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_in_event",
4318 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004319#endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004320 }
4321 else
4322 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004323#if GTK_CHECK_VERSION(3,0,0)
4324 g_signal_connect(G_OBJECT(gui.drawarea), "focus-out-event",
4325 G_CALLBACK(focus_out_event), NULL);
4326 g_signal_connect(G_OBJECT(gui.drawarea), "focus-in-event",
4327 G_CALLBACK(focus_in_event), NULL);
4328#else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004329 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_out_event",
4330 GTK_SIGNAL_FUNC(focus_out_event), NULL);
4331 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_in_event",
4332 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004333#endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004334#ifdef FEAT_GUI_TABLINE
Bram Moolenaar98921892016-02-23 17:14:37 +01004335# if GTK_CHECK_VERSION(3,0,0)
4336 g_signal_connect(G_OBJECT(gui.tabline), "focus-out-event",
4337 G_CALLBACK(focus_out_event), NULL);
4338 g_signal_connect(G_OBJECT(gui.tabline), "focus-in-event",
4339 G_CALLBACK(focus_in_event), NULL);
4340# else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004341 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_out_event",
4342 GTK_SIGNAL_FUNC(focus_out_event), NULL);
4343 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_in_event",
4344 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004345# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004346#endif /* FEAT_GUI_TABLINE */
4347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348
Bram Moolenaar98921892016-02-23 17:14:37 +01004349#if GTK_CHECK_VERSION(3,0,0)
4350 g_signal_connect(G_OBJECT(gui.drawarea), "motion-notify-event",
4351 G_CALLBACK(motion_notify_event), NULL);
4352 g_signal_connect(G_OBJECT(gui.drawarea), "button-press-event",
4353 G_CALLBACK(button_press_event), NULL);
4354 g_signal_connect(G_OBJECT(gui.drawarea), "button-release-event",
4355 G_CALLBACK(button_release_event), NULL);
4356 g_signal_connect(G_OBJECT(gui.drawarea), "scroll-event",
4357 G_CALLBACK(&scroll_event), NULL);
4358#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "motion_notify_event",
4360 GTK_SIGNAL_FUNC(motion_notify_event), NULL);
4361 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_press_event",
4362 GTK_SIGNAL_FUNC(button_press_event), NULL);
4363 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_release_event",
4364 GTK_SIGNAL_FUNC(button_release_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 g_signal_connect(G_OBJECT(gui.drawarea), "scroll_event",
4366 G_CALLBACK(&scroll_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368
4369 /*
4370 * Add selection handler functions.
4371 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004372#if GTK_CHECK_VERSION(3,0,0)
4373 g_signal_connect(G_OBJECT(gui.drawarea), "selection-clear-event",
4374 G_CALLBACK(selection_clear_event), NULL);
4375 g_signal_connect(G_OBJECT(gui.drawarea), "selection-received",
4376 G_CALLBACK(selection_received_cb), NULL);
4377#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_clear_event",
4379 GTK_SIGNAL_FUNC(selection_clear_event), NULL);
4380 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_received",
4381 GTK_SIGNAL_FUNC(selection_received_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383
Bram Moolenaara76638f2010-06-05 12:49:46 +02004384 gui_gtk_set_selection_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385
Bram Moolenaar98921892016-02-23 17:14:37 +01004386#if GTK_CHECK_VERSION(3,0,0)
4387 g_signal_connect(G_OBJECT(gui.drawarea), "selection-get",
4388 G_CALLBACK(selection_get_cb), NULL);
4389#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_get",
4391 GTK_SIGNAL_FUNC(selection_get_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393
4394 /* Pretend we don't have input focus, we will get an event if we do. */
4395 gui.in_focus = FALSE;
4396
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 return OK;
4398}
4399
4400#if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO)
4401/*
4402 * This is called from gui_start() after a fork() has been done.
4403 * We have to tell the session manager our new PID.
4404 */
4405 void
4406gui_mch_forked(void)
4407{
4408 if (using_gnome)
4409 {
4410 GnomeClient *client;
4411
4412 client = gnome_master_client();
4413
4414 if (client != NULL)
4415 gnome_client_set_process_id(client, getpid());
4416 }
4417}
4418#endif /* FEAT_GUI_GNOME && FEAT_SESSION */
4419
Bram Moolenaar98921892016-02-23 17:14:37 +01004420#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02004421 static GdkRGBA
4422color_to_rgba(guicolor_T color)
Bram Moolenaar98921892016-02-23 17:14:37 +01004423{
Bram Moolenaar36edf062016-07-21 22:10:12 +02004424 GdkRGBA rgba;
4425 rgba.red = ((color & 0xff0000) >> 16) / 255.0;
4426 rgba.green = ((color & 0xff00) >> 8) / 255.0;
4427 rgba.blue = ((color & 0xff)) / 255.0;
4428 rgba.alpha = 1.0;
4429 return rgba;
Bram Moolenaar98921892016-02-23 17:14:37 +01004430}
4431
4432 static void
Bram Moolenaar36edf062016-07-21 22:10:12 +02004433set_cairo_source_rgba_from_color(cairo_t *cr, guicolor_T color)
Bram Moolenaar98921892016-02-23 17:14:37 +01004434{
Bram Moolenaar36edf062016-07-21 22:10:12 +02004435 const GdkRGBA rgba = color_to_rgba(color);
4436 cairo_set_source_rgba(cr, rgba.red, rgba.green, rgba.blue, rgba.alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01004437}
4438#endif /* GTK_CHECK_VERSION(3,0,0) */
4439
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440/*
4441 * Called when the foreground or background color has been changed.
4442 * This used to change the graphics contexts directly but we are
4443 * currently manipulating them where desired.
4444 */
4445 void
4446gui_mch_new_colors(void)
4447{
Bram Moolenaar98921892016-02-23 17:14:37 +01004448#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaara859f042016-11-17 19:11:55 +01004449# if !GTK_CHECK_VERSION(3,22,2)
Bram Moolenaar98921892016-02-23 17:14:37 +01004450 GdkWindow * const da_win = gtk_widget_get_window(gui.drawarea);
Bram Moolenaara859f042016-11-17 19:11:55 +01004451# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004452
4453 if (gui.drawarea != NULL && gtk_widget_get_window(gui.drawarea) != NULL)
4454#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01004456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 {
Bram Moolenaara859f042016-11-17 19:11:55 +01004458#if GTK_CHECK_VERSION(3,22,2)
4459 GtkStyleContext * const context
4460 = gtk_widget_get_style_context(gui.drawarea);
4461 GtkCssProvider * const provider = gtk_css_provider_new();
4462 gchar * const css = g_strdup_printf(
4463 "widget#vim-gui-drawarea {\n"
4464 " background-color: #%.2lx%.2lx%.2lx;\n"
4465 "}\n",
4466 (gui.back_pixel >> 16) & 0xff,
4467 (gui.back_pixel >> 8) & 0xff,
4468 gui.back_pixel & 0xff);
4469
4470 gtk_css_provider_load_from_data(provider, css, -1, NULL);
4471 gtk_style_context_add_provider(context,
4472 GTK_STYLE_PROVIDER(provider), G_MAXUINT);
4473
4474 g_free(css);
4475 g_object_unref(provider);
4476#elif GTK_CHECK_VERSION(3,4,0) /* !GTK_CHECK_VERSION(3,22,2) */
Bram Moolenaar36edf062016-07-21 22:10:12 +02004477 GdkRGBA rgba;
Bram Moolenaar98921892016-02-23 17:14:37 +01004478
Bram Moolenaar36edf062016-07-21 22:10:12 +02004479 rgba = color_to_rgba(gui.back_pixel);
Bram Moolenaar98921892016-02-23 17:14:37 +01004480 {
4481 cairo_pattern_t * const pat = cairo_pattern_create_rgba(
Bram Moolenaar36edf062016-07-21 22:10:12 +02004482 rgba.red, rgba.green, rgba.blue, rgba.alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01004483 if (pat != NULL)
4484 {
4485 gdk_window_set_background_pattern(da_win, pat);
4486 cairo_pattern_destroy(pat);
4487 }
4488 else
Bram Moolenaar36edf062016-07-21 22:10:12 +02004489 gdk_window_set_background_rgba(da_win, &rgba);
Bram Moolenaar98921892016-02-23 17:14:37 +01004490 }
4491#else /* !GTK_CHECK_VERSION(3,4,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 GdkColor color = { 0, 0, 0, 0 };
4493
4494 color.pixel = gui.back_pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01004495# if GTK_CHECK_VERSION(3,0,0)
4496 gdk_window_set_background(da_win, &color);
4497# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 gdk_window_set_background(gui.drawarea->window, &color);
Bram Moolenaar98921892016-02-23 17:14:37 +01004499# endif
Bram Moolenaara859f042016-11-17 19:11:55 +01004500#endif /* !GTK_CHECK_VERSION(3,22,2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 }
4502}
4503
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504/*
4505 * This signal informs us about the need to rearrange our sub-widgets.
4506 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004508form_configure_event(GtkWidget *widget UNUSED,
4509 GdkEventConfigure *event,
4510 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511{
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004512 int usable_height = event->height;
4513
Bram Moolenaar182707a2016-11-21 20:55:58 +01004514#if GTK_CHECK_VERSION(3,22,2) && !GTK_CHECK_VERSION(3,22,4)
Bram Moolenaara859f042016-11-17 19:11:55 +01004515 /* As of 3.22.2, GdkWindows have started distributing configure events to
4516 * their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0).
4517 *
4518 * As can be seen from the implementation of move_native_children() and
4519 * configure_native_child() in gdkwindow.c, those functions actually
4520 * propagate configure events to every child, failing to distinguish
4521 * "native" one from non-native one.
4522 *
4523 * Naturally, configure events propagated to here like that are fallacious
4524 * and, as a matter of fact, they trigger a geometric collapse of
4525 * gui.formwin.
4526 *
4527 * To filter out such fallacious events, check if the given event is the
4528 * one that was sent out to the right place. Ignore it if not.
4529 */
Bram Moolenaar182707a2016-11-21 20:55:58 +01004530 /* Follow-up
4531 * After a few weeks later, the GdkWindow change mentioned above was
4532 * reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155).
4533 * The corresponding official release is 3.22.4. */
Bram Moolenaara859f042016-11-17 19:11:55 +01004534 if (event->window != gtk_widget_get_window(gui.formwin))
4535 return TRUE;
4536#endif
4537
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004538 /* When in a GtkPlug, we can't guarantee valid heights (as a round
4539 * no. of char-heights), so we have to manually sanitise them.
4540 * Widths seem to sort themselves out, don't ask me why.
4541 */
4542 if (gtk_socket_id != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004543 usable_height -= (gui.char_height - (gui.char_height/2)); /* sic. */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004544
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 gtk_form_freeze(GTK_FORM(gui.formwin));
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004546 gui_resize_shell(event->width, usable_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 gtk_form_thaw(GTK_FORM(gui.formwin));
4548
4549 return TRUE;
4550}
4551
4552/*
4553 * Function called when window already closed.
4554 * We can't do much more here than to trying to preserve what had been done,
4555 * since the window is already inevitably going away.
4556 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004557#if GTK_CHECK_VERSION(3,0,0)
4558 static void
4559mainwin_destroy_cb(GObject *object UNUSED, gpointer data UNUSED)
4560#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004562mainwin_destroy_cb(GtkObject *object UNUSED, gpointer data UNUSED)
Bram Moolenaar98921892016-02-23 17:14:37 +01004563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564{
4565 /* Don't write messages to the GUI anymore */
4566 full_screen = FALSE;
4567
4568 gui.mainwin = NULL;
4569 gui.drawarea = NULL;
4570
4571 if (!exiting) /* only do anything if the destroy was unexpected */
4572 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004573 vim_strncpy(IObuff,
4574 (char_u *)_("Vim: Main window unexpectedly destroyed\n"),
4575 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 preserve_exit();
4577 }
Bram Moolenaar36e294c2015-12-29 18:55:46 +01004578#ifdef USE_GRESOURCE
4579 gui_gtk_unregister_resource();
4580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581}
4582
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004583
4584/*
4585 * Bit of a hack to ensure we start GtkPlug windows with the correct window
4586 * hints (and thus the required size from -geom), but that after that we
4587 * put the hints back to normal (the actual minimum size) so we may
4588 * subsequently be resized smaller. GtkSocket (the parent end) uses the
Bram Moolenaar49325942007-05-10 19:19:59 +00004589 * plug's window 'min hints to set *it's* minimum size, but that's also the
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004590 * only way we have of making ourselves bigger (by set lines/columns).
4591 * Thus set hints at start-up to ensure correct init. size, then a
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01004592 * second after the final attempt to reset the real minimum hints (done by
4593 * scrollbar init.), actually do the standard hints and stop the timer.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004594 * We'll not let the default hints be set while this timer's active.
4595 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004596 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004597check_startup_plug_hints(gpointer data UNUSED)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004598{
4599 if (init_window_hints_state == 1)
4600 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004601 /* Safe to use normal hints now */
4602 init_window_hints_state = 0;
4603 update_window_manager_hints(0, 0);
4604 return FALSE; /* stop timer */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004605 }
4606
4607 /* Keep on trying */
4608 init_window_hints_state = 1;
4609 return TRUE;
4610}
4611
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612/*
4613 * Open the GUI window which was created by a call to gui_mch_init().
4614 */
4615 int
4616gui_mch_open(void)
4617{
4618 guicolor_T fg_pixel = INVALCOLOR;
4619 guicolor_T bg_pixel = INVALCOLOR;
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004620 guint pixel_width;
4621 guint pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 /*
4624 * Allow setting a window role on the command line, or invent one
4625 * if none was specified. This is mainly useful for GNOME session
4626 * support; allowing the WM to restore window placement.
4627 */
4628 if (role_argument != NULL)
4629 {
4630 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument);
4631 }
4632 else
4633 {
4634 char *role;
4635
4636 /* Invent a unique-enough ID string for the role */
4637 role = g_strdup_printf("vim-%u-%u-%u",
4638 (unsigned)mch_get_pid(),
4639 (unsigned)g_random_int(),
4640 (unsigned)time(NULL));
4641
4642 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role);
4643 g_free(role);
4644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645
4646 if (gui_win_x != -1 && gui_win_y != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648
4649 /* Determine user specified geometry, if present. */
4650 if (gui.geom != NULL)
4651 {
4652 int mask;
4653 unsigned int w, h;
4654 int x = 0;
4655 int y = 0;
4656
4657 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
4658
4659 if (mask & WidthValue)
4660 Columns = w;
4661 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00004662 {
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004663 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00004664 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00004666 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004667 limit_screen_size();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004668
4669 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4670 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
4671
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004672 pixel_width += get_menu_tool_width();
4673 pixel_height += get_menu_tool_height();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004674
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 if (mask & (XValue | YValue))
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004676 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004677 int ww, hh;
4678 gui_mch_get_screen_dimensions(&ww, &hh);
4679 hh += p_ghr + get_menu_tool_height();
4680 ww += get_menu_tool_width();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004681 if (mask & XNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004682 x += ww - pixel_width;
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004683 if (mask & YNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004684 y += hh - pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 vim_free(gui.geom);
4688 gui.geom = NULL;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004689
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004690 /* From now until everyone's stopped trying to set the window hints
4691 * to their correct minimum values, stop them being set as we need
4692 * them to remain at our required size for the parent GtkSocket to
4693 * give us the right initial size.
4694 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004695 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004696 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004697 update_window_manager_hints(pixel_width, pixel_height);
4698 init_window_hints_state = 1;
4699 g_timeout_add(1000, check_startup_plug_hints, NULL);
4700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 }
4702
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004703 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4704 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004705 /* For GTK2 changing the size of the form widget doesn't cause window
4706 * resizing. */
Bram Moolenaardebe25a2010-06-06 17:41:24 +02004707 if (gtk_socket_id == 0)
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004708 gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004709 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710
4711 if (foreground_argument != NULL)
4712 fg_pixel = gui_get_color((char_u *)foreground_argument);
4713 if (fg_pixel == INVALCOLOR)
4714 fg_pixel = gui_get_color((char_u *)"Black");
4715
4716 if (background_argument != NULL)
4717 bg_pixel = gui_get_color((char_u *)background_argument);
4718 if (bg_pixel == INVALCOLOR)
4719 bg_pixel = gui_get_color((char_u *)"White");
4720
4721 if (found_reverse_arg)
4722 {
4723 gui.def_norm_pixel = bg_pixel;
4724 gui.def_back_pixel = fg_pixel;
4725 }
4726 else
4727 {
4728 gui.def_norm_pixel = fg_pixel;
4729 gui.def_back_pixel = bg_pixel;
4730 }
4731
4732 /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or
4733 * in a vimrc file) */
4734 set_normal_colors();
4735
4736 /* Check that none of the colors are the same as the background color */
4737 gui_check_colors();
4738
4739 /* Get the colors for the highlight groups (gui_check_colors() might have
4740 * changed them). */
4741 highlight_gui_started(); /* re-init colors and fonts */
4742
Bram Moolenaar98921892016-02-23 17:14:37 +01004743#if GTK_CHECK_VERSION(3,0,0)
4744 g_signal_connect(G_OBJECT(gui.mainwin), "destroy",
4745 G_CALLBACK(mainwin_destroy_cb), NULL);
4746#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "destroy",
4748 GTK_SIGNAL_FUNC(mainwin_destroy_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750
4751#ifdef FEAT_HANGULIN
4752 hangul_keyboard_set();
4753#endif
4754
4755 /*
4756 * Notify the fixed area about the need to resize the contents of the
4757 * gui.formwin, which we use for random positioning of the included
4758 * components.
4759 *
4760 * We connect this signal deferred finally after anything is in place,
4761 * since this is intended to handle resizements coming from the window
4762 * manager upon us and should not interfere with what VIM is requesting
4763 * upon startup.
4764 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004765#if GTK_CHECK_VERSION(3,0,0)
4766 g_signal_connect(G_OBJECT(gui.formwin), "configure-event",
4767 G_CALLBACK(form_configure_event), NULL);
4768#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 gtk_signal_connect(GTK_OBJECT(gui.formwin), "configure_event",
4770 GTK_SIGNAL_FUNC(form_configure_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772
4773#ifdef FEAT_DND
Bram Moolenaara76638f2010-06-05 12:49:46 +02004774 /* Set up for receiving DND items. */
4775 gui_gtk_set_dnd_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776
Bram Moolenaar98921892016-02-23 17:14:37 +01004777# if GTK_CHECK_VERSION(3,0,0)
4778 g_signal_connect(G_OBJECT(gui.drawarea), "drag-data-received",
4779 G_CALLBACK(drag_data_received_cb), NULL);
4780# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "drag_data_received",
4782 GTK_SIGNAL_FUNC(drag_data_received_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784#endif
4785
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 /* With GTK+ 2, we need to iconify the window before calling show()
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004787 * to avoid mapping the window for a short time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 if (found_iconic_arg && gtk_socket_id == 0)
4789 gui_mch_iconify();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790
4791 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004792#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 unsigned long menu_handler = 0;
4794# ifdef FEAT_TOOLBAR
4795 unsigned long tool_handler = 0;
4796# endif
4797 /*
4798 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
4799 * show when restoring the saved layout configuration. We can't just
4800 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
4801 * a toplevel window and thus will be realized immediately. Instead,
4802 * connect signal handlers to hide the widgets just after they've been
4803 * marked visible, but before the main window is realized.
4804 */
4805 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL)
4806 menu_handler = g_signal_connect_after(gui.menubar_h, "show",
4807 G_CALLBACK(&gtk_widget_hide),
4808 NULL);
4809# ifdef FEAT_TOOLBAR
4810 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL
4811 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
4812 tool_handler = g_signal_connect_after(gui.toolbar_h, "show",
4813 G_CALLBACK(&gtk_widget_hide),
4814 NULL);
4815# endif
4816#endif
4817 gtk_widget_show(gui.mainwin);
4818
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004819#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 if (menu_handler != 0)
4821 g_signal_handler_disconnect(gui.menubar_h, menu_handler);
4822# ifdef FEAT_TOOLBAR
4823 if (tool_handler != 0)
4824 g_signal_handler_disconnect(gui.toolbar_h, tool_handler);
4825# endif
4826#endif
4827 }
4828
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 return OK;
4830}
4831
4832
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004834gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835{
4836 if (gui.mainwin != NULL)
4837 gtk_widget_destroy(gui.mainwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838}
4839
4840/*
4841 * Get the position of the top left corner of the window.
4842 */
4843 int
4844gui_mch_get_winpos(int *x, int *y)
4845{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 return OK;
4848}
4849
4850/*
4851 * Set the position of the top left corner of the window to the given
4852 * coordinates.
4853 */
4854 void
4855gui_mch_set_winpos(int x, int y)
4856{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858}
4859
Bram Moolenaar98921892016-02-23 17:14:37 +01004860#if !GTK_CHECK_VERSION(3,0,0)
4861# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862static int resize_idle_installed = FALSE;
4863/*
4864 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
4865 * the shell size doesn't exceed the window size, i.e. if the window manager
4866 * ignored our size request. Usually this happens if the window is maximized.
4867 *
4868 * FIXME: It'd be nice if we could find a little more orthodox solution.
4869 * See also the remark below in gui_mch_set_shellsize().
4870 *
4871 * DISABLED: When doing ":set lines+=1" this function would first invoke
4872 * gui_resize_shell() with the old size, then the normal callback would
4873 * report the new size through form_configure_event(). That caused the window
4874 * layout to be messed up.
4875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 static gboolean
4877force_shell_resize_idle(gpointer data)
4878{
4879 if (gui.mainwin != NULL
4880 && GTK_WIDGET_REALIZED(gui.mainwin)
4881 && GTK_WIDGET_VISIBLE(gui.mainwin))
4882 {
4883 int width;
4884 int height;
4885
4886 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height);
4887
4888 width -= get_menu_tool_width();
4889 height -= get_menu_tool_height();
4890
4891 gui_resize_shell(width, height);
4892 }
4893
4894 resize_idle_installed = FALSE;
4895 return FALSE; /* don't call me again */
4896}
Bram Moolenaar98921892016-02-23 17:14:37 +01004897# endif
4898#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899
Bram Moolenaar09736232009-09-23 16:14:49 +00004900/*
4901 * Return TRUE if the main window is maximized.
4902 */
4903 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004904gui_mch_maximized(void)
Bram Moolenaar09736232009-09-23 16:14:49 +00004905{
Bram Moolenaar98921892016-02-23 17:14:37 +01004906#if GTK_CHECK_VERSION(3,0,0)
4907 return (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL
4908 && (gdk_window_get_state(gtk_widget_get_window(gui.mainwin))
4909 & GDK_WINDOW_STATE_MAXIMIZED));
4910#else
Bram Moolenaar09736232009-09-23 16:14:49 +00004911 return (gui.mainwin != NULL && gui.mainwin->window != NULL
4912 && (gdk_window_get_state(gui.mainwin->window)
4913 & GDK_WINDOW_STATE_MAXIMIZED));
Bram Moolenaar98921892016-02-23 17:14:37 +01004914#endif
Bram Moolenaar09736232009-09-23 16:14:49 +00004915}
4916
4917/*
4918 * Unmaximize the main window
4919 */
4920 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004921gui_mch_unmaximize(void)
Bram Moolenaar09736232009-09-23 16:14:49 +00004922{
4923 if (gui.mainwin != NULL)
4924 gtk_window_unmaximize(GTK_WINDOW(gui.mainwin));
4925}
Bram Moolenaar09736232009-09-23 16:14:49 +00004926
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927/*
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004928 * Called when the font changed while the window is maximized. Compute the
4929 * new Rows and Columns. This is like resizing the window.
4930 */
4931 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004932gui_mch_newfont(void)
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004933{
4934 int w, h;
4935
4936 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
4937 w -= get_menu_tool_width();
4938 h -= get_menu_tool_height();
4939 gui_resize_shell(w, h);
4940}
4941
4942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 * Set the windows size.
4944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 void
4946gui_mch_set_shellsize(int width, int height,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004947 int min_width UNUSED, int min_height UNUSED,
4948 int base_width UNUSED, int base_height UNUSED,
4949 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 /* give GTK+ a chance to put all widget's into place */
4952 gui_mch_update();
4953
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004954 /* this will cause the proper resizement to happen too */
4955 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004956 update_window_manager_hints(0, 0);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004957
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 /* With GTK+ 2, changing the size of the form widget doesn't resize
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004959 * the window. So let's do it the other way around and resize the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 * main window instead. */
4961 width += get_menu_tool_width();
4962 height += get_menu_tool_height();
4963
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004964 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004965 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004966 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004967 update_window_manager_hints(width, height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968
Bram Moolenaar98921892016-02-23 17:14:37 +01004969# if !GTK_CHECK_VERSION(3,0,0)
4970# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 if (!resize_idle_installed)
4972 {
4973 g_idle_add_full(GDK_PRIORITY_EVENTS + 10,
4974 &force_shell_resize_idle, NULL, NULL);
4975 resize_idle_installed = TRUE;
4976 }
Bram Moolenaar98921892016-02-23 17:14:37 +01004977# endif
4978# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 /*
4980 * Wait until all events are processed to prevent a crash because the
4981 * real size of the drawing area doesn't reflect Vim's internal ideas.
4982 *
4983 * This is a bit of a hack, since Vim is a terminal application with a GUI
4984 * on top, while the GUI expects to be the boss.
4985 */
4986 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987}
4988
4989
4990/*
4991 * The screen size is used to make sure the initial window doesn't get bigger
4992 * than the screen. This subtracts some room for menubar, toolbar and window
4993 * decorations.
4994 */
4995 void
4996gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
4997{
4998#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaara859f042016-11-17 19:11:55 +01004999# if GTK_CHECK_VERSION(3,22,2)
5000 GdkRectangle rect;
5001 GdkMonitor * const mon = gdk_display_get_monitor_at_window(
5002 gtk_widget_get_display(gui.mainwin),
5003 gtk_widget_get_window(gui.mainwin));
5004 gdk_monitor_get_geometry(mon, &rect);
5005
5006 *screen_w = rect.width;
5007 *screen_h = rect.height - p_ghr;
5008# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 GdkScreen* screen;
5010
5011 if (gui.mainwin != NULL && gtk_widget_has_screen(gui.mainwin))
5012 screen = gtk_widget_get_screen(gui.mainwin);
5013 else
5014 screen = gdk_screen_get_default();
5015
5016 *screen_w = gdk_screen_get_width(screen);
5017 *screen_h = gdk_screen_get_height(screen) - p_ghr;
Bram Moolenaara859f042016-11-17 19:11:55 +01005018# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019#else
5020 *screen_w = gdk_screen_width();
5021 /* Subtract 'guiheadroom' from the height to allow some room for the
5022 * window manager (task list and window title bar). */
5023 *screen_h = gdk_screen_height() - p_ghr;
5024#endif
5025
5026 /*
5027 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
5028 * the toolbar and menubar for GTK, we subtract them from the screen
Bram Moolenaar64404472010-06-26 06:24:45 +02005029 * height, so that the window size can be made to fit on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 * This should be completely changed later.
5031 */
5032 *screen_w -= get_menu_tool_width();
5033 *screen_h -= get_menu_tool_height();
5034}
5035
5036#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005038gui_mch_settitle(char_u *title, char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 if (title != NULL && output_conv.vc_type != CONV_NONE)
5041 title = string_convert(&output_conv, title, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042
5043 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
5044
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 if (output_conv.vc_type != CONV_NONE)
5046 vim_free(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047}
5048#endif /* FEAT_TITLE */
5049
5050#if defined(FEAT_MENU) || defined(PROTO)
5051 void
5052gui_mch_enable_menu(int showit)
5053{
5054 GtkWidget *widget;
5055
5056# ifdef FEAT_GUI_GNOME
5057 if (using_gnome)
5058 widget = gui.menubar_h;
5059 else
5060# endif
5061 widget = gui.menubar;
5062
Bram Moolenaar18144c82006-04-12 21:52:12 +00005063 /* Do not disable the menu while starting up, otherwise F10 doesn't work. */
Bram Moolenaar98921892016-02-23 17:14:37 +01005064# if GTK_CHECK_VERSION(3,0,0)
5065 if (!showit != !gtk_widget_get_visible(widget) && !gui.starting)
5066# else
Bram Moolenaar18144c82006-04-12 21:52:12 +00005067 if (!showit != !GTK_WIDGET_VISIBLE(widget) && !gui.starting)
Bram Moolenaar98921892016-02-23 17:14:37 +01005068# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 {
5070 if (showit)
5071 gtk_widget_show(widget);
5072 else
5073 gtk_widget_hide(widget);
5074
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00005075 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 }
5077}
5078#endif /* FEAT_MENU */
5079
5080#if defined(FEAT_TOOLBAR) || defined(PROTO)
5081 void
5082gui_mch_show_toolbar(int showit)
5083{
5084 GtkWidget *widget;
5085
5086 if (gui.toolbar == NULL)
5087 return;
5088
5089# ifdef FEAT_GUI_GNOME
5090 if (using_gnome)
5091 widget = gui.toolbar_h;
5092 else
5093# endif
5094 widget = gui.toolbar;
5095
5096 if (showit)
5097 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
5098
Bram Moolenaar98921892016-02-23 17:14:37 +01005099# if GTK_CHECK_VERSION(3,0,0)
5100 if (!showit != !gtk_widget_get_visible(widget))
5101# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 if (!showit != !GTK_WIDGET_VISIBLE(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01005103# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
5105 if (showit)
5106 gtk_widget_show(widget);
5107 else
5108 gtk_widget_hide(widget);
5109
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00005110 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 }
5112}
5113#endif /* FEAT_TOOLBAR */
5114
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115/*
5116 * Check if a given font is a CJK font. This is done in a very crude manner. It
5117 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
5118 * font. Consequently, this function cannot be used as a general purpose check
5119 * for CJK-ness for which fontconfig APIs should be used. This is only used by
5120 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
5121 */
5122 static int
5123is_cjk_font(PangoFontDescription *font_desc)
5124{
5125 static const char * const cjk_langs[] =
5126 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
5127
5128 PangoFont *font;
5129 unsigned i;
5130 int is_cjk = FALSE;
5131
5132 font = pango_context_load_font(gui.text_context, font_desc);
5133
5134 if (font == NULL)
5135 return FALSE;
5136
5137 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i)
5138 {
5139 PangoCoverage *coverage;
5140 gunichar uc;
5141
5142 coverage = pango_font_get_coverage(
5143 font, pango_language_from_string(cjk_langs[i]));
5144
5145 if (coverage != NULL)
5146 {
5147 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00;
5148 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT);
5149 pango_coverage_unref(coverage);
5150 }
5151 }
5152
5153 g_object_unref(font);
5154
5155 return is_cjk;
5156}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157
Bram Moolenaar231334e2005-07-25 20:46:57 +00005158/*
5159 * Adjust gui.char_height (after 'linespace' was changed).
5160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 int
Bram Moolenaar231334e2005-07-25 20:46:57 +00005162gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 PangoFontMetrics *metrics;
5165 int ascent;
5166 int descent;
5167
5168 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font,
5169 pango_context_get_language(gui.text_context));
5170 ascent = pango_font_metrics_get_ascent(metrics);
5171 descent = pango_font_metrics_get_descent(metrics);
5172
5173 pango_font_metrics_unref(metrics);
5174
5175 gui.char_height = (ascent + descent + PANGO_SCALE - 1) / PANGO_SCALE
Bram Moolenaar231334e2005-07-25 20:46:57 +00005176 + p_linespace;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005177 /* LINTED: avoid warning: bitwise operation on signed value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2);
5179
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 /* A not-positive value of char_height may crash Vim. Only happens
5181 * if 'linespace' is negative (which does make sense sometimes). */
5182 gui.char_ascent = MAX(gui.char_ascent, 0);
5183 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
5184
5185 return OK;
5186}
5187
Bram Moolenaar98921892016-02-23 17:14:37 +01005188#if GTK_CHECK_VERSION(3,0,0)
5189/* Callback function used in gui_mch_font_dialog() */
5190 static gboolean
5191font_filter(const PangoFontFamily *family,
5192 const PangoFontFace *face UNUSED,
5193 gpointer data UNUSED)
5194{
5195 return pango_font_family_is_monospace((PangoFontFamily *)family);
5196}
5197#endif
5198
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199/*
5200 * Put up a font dialog and return the selected font name in allocated memory.
5201 * "oldval" is the previous value. Return NULL when cancelled.
5202 * This should probably go into gui_gtk.c. Hmm.
5203 * FIXME:
5204 * The GTK2 font selection dialog has no filtering API. So we could either
5205 * a) implement our own (possibly copying the code from somewhere else) or
5206 * b) just live with it.
5207 */
5208 char_u *
5209gui_mch_font_dialog(char_u *oldval)
5210{
5211 GtkWidget *dialog;
5212 int response;
5213 char_u *fontname = NULL;
5214 char_u *oldname;
5215
Bram Moolenaar98921892016-02-23 17:14:37 +01005216#if GTK_CHECK_VERSION(3,2,0)
5217 dialog = gtk_font_chooser_dialog_new(NULL, NULL);
5218 gtk_font_chooser_set_filter_func(GTK_FONT_CHOOSER(dialog), font_filter,
5219 NULL, NULL);
5220#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 dialog = gtk_font_selection_dialog_new(NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01005222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223
5224 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
5225 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
5226
5227 if (oldval != NULL && oldval[0] != NUL)
5228 {
5229 if (output_conv.vc_type != CONV_NONE)
5230 oldname = string_convert(&output_conv, oldval, NULL);
5231 else
5232 oldname = oldval;
5233
5234 /* Annoying bug in GTK (or Pango): if the font name does not include a
5235 * size, zero is used. Use default point size ten. */
5236 if (!vim_isdigit(oldname[STRLEN(oldname) - 1]))
5237 {
5238 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3);
5239
5240 if (p != NULL)
5241 {
5242 STRCPY(p + STRLEN(p), " 10");
5243 if (oldname != oldval)
5244 vim_free(oldname);
5245 oldname = p;
5246 }
5247 }
5248
Bram Moolenaar98921892016-02-23 17:14:37 +01005249#if GTK_CHECK_VERSION(3,2,0)
5250 gtk_font_chooser_set_font(
5251 GTK_FONT_CHOOSER(dialog), (const gchar *)oldname);
5252#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 gtk_font_selection_dialog_set_font_name(
5254 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname);
Bram Moolenaar98921892016-02-23 17:14:37 +01005255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256
5257 if (oldname != oldval)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005258 vim_free(oldname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 }
Bram Moolenaarbef9d832009-09-11 13:46:41 +00005260 else
Bram Moolenaar98921892016-02-23 17:14:37 +01005261#if GTK_CHECK_VERSION(3,2,0)
5262 gtk_font_chooser_set_font(
5263 GTK_FONT_CHOOSER(dialog), DEFAULT_FONT);
5264#else
Bram Moolenaarbef9d832009-09-11 13:46:41 +00005265 gtk_font_selection_dialog_set_font_name(
5266 GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT);
Bram Moolenaar98921892016-02-23 17:14:37 +01005267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268
5269 response = gtk_dialog_run(GTK_DIALOG(dialog));
5270
5271 if (response == GTK_RESPONSE_OK)
5272 {
5273 char *name;
5274
Bram Moolenaar98921892016-02-23 17:14:37 +01005275#if GTK_CHECK_VERSION(3,2,0)
5276 name = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(dialog));
5277#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 name = gtk_font_selection_dialog_get_font_name(
5279 GTK_FONT_SELECTION_DIALOG(dialog));
Bram Moolenaar98921892016-02-23 17:14:37 +01005280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 if (name != NULL)
5282 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005283 char_u *p;
5284
5285 /* Apparently some font names include a comma, need to escape
5286 * that, because in 'guifont' it separates names. */
5287 p = vim_strsave_escaped((char_u *)name, (char_u *)",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 g_free(name);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005289 if (p != NULL && input_conv.vc_type != CONV_NONE)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005290 {
5291 fontname = string_convert(&input_conv, p, NULL);
5292 vim_free(p);
5293 }
5294 else
5295 fontname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 }
5297 }
5298
5299 if (response != GTK_RESPONSE_NONE)
5300 gtk_widget_destroy(dialog);
5301
5302 return fontname;
5303}
5304
5305/*
5306 * Some monospace fonts don't support a bold weight, and fall back
5307 * silently to the regular weight. But this is no good since our text
5308 * drawing function can emulate bold by overstriking. So let's try
5309 * to detect whether bold weight is actually available and emulate it
5310 * otherwise.
5311 *
5312 * Note that we don't need to check for italic style since Xft can
5313 * emulate italic on its own, provided you have a proper fontconfig
5314 * setup. We wouldn't be able to emulate it in Vim anyway.
5315 */
5316 static void
5317get_styled_font_variants(void)
5318{
5319 PangoFontDescription *bold_font_desc;
5320 PangoFont *plain_font;
5321 PangoFont *bold_font;
5322
5323 gui.font_can_bold = FALSE;
5324
5325 plain_font = pango_context_load_font(gui.text_context, gui.norm_font);
5326
5327 if (plain_font == NULL)
5328 return;
5329
5330 bold_font_desc = pango_font_description_copy_static(gui.norm_font);
5331 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD);
5332
5333 bold_font = pango_context_load_font(gui.text_context, bold_font_desc);
5334 /*
5335 * The comparison relies on the unique handle nature of a PangoFont*,
5336 * i.e. it's assumed that a different PangoFont* won't refer to the
5337 * same font. Seems to work, and failing here isn't critical anyway.
5338 */
5339 if (bold_font != NULL)
5340 {
5341 gui.font_can_bold = (bold_font != plain_font);
5342 g_object_unref(bold_font);
5343 }
5344
5345 pango_font_description_free(bold_font_desc);
5346 g_object_unref(plain_font);
5347}
5348
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349static PangoEngineShape *default_shape_engine = NULL;
5350
5351/*
5352 * Create a map from ASCII characters in the range [32,126] to glyphs
5353 * of the current font. This is used by gui_gtk2_draw_string() to skip
5354 * the itemize and shaping process for the most common case.
5355 */
5356 static void
5357ascii_glyph_table_init(void)
5358{
Bram Moolenaar16350cb2016-08-14 20:27:34 +02005359 char_u ascii_chars[2 * 128];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 PangoAttrList *attr_list;
5361 GList *item_list;
5362 int i;
5363
5364 if (gui.ascii_glyphs != NULL)
5365 pango_glyph_string_free(gui.ascii_glyphs);
5366 if (gui.ascii_font != NULL)
5367 g_object_unref(gui.ascii_font);
5368
5369 gui.ascii_glyphs = NULL;
5370 gui.ascii_font = NULL;
5371
Bram Moolenaar16350cb2016-08-14 20:27:34 +02005372 /* For safety, fill in question marks for the control characters.
5373 * Put a space between characters to avoid shaping. */
5374 for (i = 0; i < 128; ++i)
5375 {
5376 if (i >= 32 && i < 127)
5377 ascii_chars[2 * i] = i;
5378 else
5379 ascii_chars[2 * i] = '?';
5380 ascii_chars[2 * i + 1] = ' ';
5381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
5383 attr_list = pango_attr_list_new();
5384 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars,
5385 0, sizeof(ascii_chars), attr_list, NULL);
5386
5387 if (item_list != NULL && item_list->next == NULL) /* play safe */
5388 {
5389 PangoItem *item;
5390 int width;
5391
5392 item = (PangoItem *)item_list->data;
5393 width = gui.char_width * PANGO_SCALE;
5394
5395 /* Remember the shape engine used for ASCII. */
5396 default_shape_engine = item->analysis.shape_engine;
5397
5398 gui.ascii_font = item->analysis.font;
5399 g_object_ref(gui.ascii_font);
5400
5401 gui.ascii_glyphs = pango_glyph_string_new();
5402
5403 pango_shape((const char *)ascii_chars, sizeof(ascii_chars),
5404 &item->analysis, gui.ascii_glyphs);
5405
5406 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars));
5407
5408 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i)
5409 {
5410 PangoGlyphGeometry *geom;
5411
5412 geom = &gui.ascii_glyphs->glyphs[i].geometry;
5413 geom->x_offset += MAX(0, width - geom->width) / 2;
5414 geom->width = width;
5415 }
5416 }
5417
5418 g_list_foreach(item_list, (GFunc)&pango_item_free, NULL);
5419 g_list_free(item_list);
5420 pango_attr_list_unref(attr_list);
5421}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422
5423/*
5424 * Initialize Vim to use the font or fontset with the given name.
5425 * Return FAIL if the font could not be loaded, OK otherwise.
5426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 int
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005428gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 PangoFontDescription *font_desc;
5431 PangoLayout *layout;
5432 int width;
5433
5434 /* If font_name is NULL, this means to use the default, which should
5435 * be present on all proper Pango/fontconfig installations. */
5436 if (font_name == NULL)
5437 font_name = (char_u *)DEFAULT_FONT;
5438
5439 font_desc = gui_mch_get_font(font_name, FALSE);
5440
5441 if (font_desc == NULL)
5442 return FAIL;
5443
5444 gui_mch_free_font(gui.norm_font);
5445 gui.norm_font = font_desc;
5446
5447 pango_context_set_font_description(gui.text_context, font_desc);
5448
5449 layout = pango_layout_new(gui.text_context);
5450 pango_layout_set_text(layout, "MW", 2);
5451 pango_layout_get_size(layout, &width, NULL);
5452 /*
5453 * Set char_width to half the width obtained from pango_layout_get_size()
5454 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
5455 * Pango to use the same width for both non-CJK characters (e.g. Latin
5456 * letters and numbers) and CJK characters. This results in 's p a c e d
5457 * o u t' rendering when a CJK 'fixed width' font is used. To work around
5458 * that, divide the width returned by Pango by 2 if cjk_width is equal to
5459 * width for CJK fonts.
5460 *
5461 * For related bugs, see:
5462 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
5463 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
5464 *
5465 * With this, for all four of the following cases, Vim works fine:
5466 * guifont=CJK_fixed_width_font
5467 * guifont=Non_CJK_fixed_font
5468 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
5469 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
5470 */
5471 if (is_cjk_font(gui.norm_font))
5472 {
5473 int cjk_width;
5474
5475 /* Measure the text extent of U+4E00 and U+4E8C */
5476 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1);
5477 pango_layout_get_size(layout, &cjk_width, NULL);
5478
5479 if (width == cjk_width) /* Xft not patched */
5480 width /= 2;
5481 }
5482 g_object_unref(layout);
5483
5484 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE;
5485
5486 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
5487 if (gui.char_width <= 0)
5488 gui.char_width = 8;
5489
Bram Moolenaar231334e2005-07-25 20:46:57 +00005490 gui_mch_adjust_charheight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491
5492 /* Set the fontname, which will be used for information purposes */
5493 hl_set_font_name(font_name);
5494
5495 get_styled_font_variants();
5496 ascii_glyph_table_init();
5497
5498 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
5499 if (gui.wide_font != NULL
5500 && pango_font_description_equal(gui.norm_font, gui.wide_font))
5501 {
5502 pango_font_description_free(gui.wide_font);
5503 gui.wide_font = NULL;
5504 }
5505
Bram Moolenaare161c792009-11-03 17:13:59 +00005506 if (gui_mch_maximized())
5507 {
Bram Moolenaare161c792009-11-03 17:13:59 +00005508 /* Update lines and columns in accordance with the new font, keep the
5509 * window maximized. */
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02005510 gui_mch_newfont();
Bram Moolenaare161c792009-11-03 17:13:59 +00005511 }
5512 else
Bram Moolenaare161c792009-11-03 17:13:59 +00005513 {
5514 /* Preserve the logical dimensions of the screen. */
5515 update_window_manager_hints(0, 0);
5516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517
5518 return OK;
5519}
5520
5521/*
5522 * Get a reference to the font "name".
5523 * Return zero for failure.
5524 */
5525 GuiFont
5526gui_mch_get_font(char_u *name, int report_error)
5527{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 PangoFontDescription *font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529
5530 /* can't do this when GUI is not running */
5531 if (!gui.in_use || name == NULL)
5532 return NULL;
5533
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 if (output_conv.vc_type != CONV_NONE)
5535 {
5536 char_u *buf;
5537
5538 buf = string_convert(&output_conv, name, NULL);
5539 if (buf != NULL)
5540 {
5541 font = pango_font_description_from_string((const char *)buf);
5542 vim_free(buf);
5543 }
5544 else
5545 font = NULL;
5546 }
5547 else
5548 font = pango_font_description_from_string((const char *)name);
5549
5550 if (font != NULL)
5551 {
5552 PangoFont *real_font;
5553
5554 /* pango_context_load_font() bails out if no font size is set */
5555 if (pango_font_description_get_size(font) <= 0)
5556 pango_font_description_set_size(font, 10 * PANGO_SCALE);
5557
5558 real_font = pango_context_load_font(gui.text_context, font);
5559
5560 if (real_font == NULL)
5561 {
5562 pango_font_description_free(font);
5563 font = NULL;
5564 }
5565 else
5566 g_object_unref(real_font);
5567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
5569 if (font == NULL)
5570 {
5571 if (report_error)
Bram Moolenaarc93e7912008-07-08 10:46:08 +00005572 EMSG2(_((char *)e_font), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 return NULL;
5574 }
5575
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 return font;
5577}
5578
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005579#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005580/*
5581 * Return the name of font "font" in allocated memory.
5582 */
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005583 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005584gui_mch_get_fontname(GuiFont font, char_u *name UNUSED)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005585{
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005586 if (font != NOFONT)
5587 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005588 char *pangoname = pango_font_description_to_string(font);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005589
Bram Moolenaar89d40322006-08-29 15:30:07 +00005590 if (pangoname != NULL)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005591 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005592 char_u *s = vim_strsave((char_u *)pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005593
Bram Moolenaar89d40322006-08-29 15:30:07 +00005594 g_free(pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005595 return s;
5596 }
5597 }
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005598 return NULL;
5599}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005600#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005601
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602/*
5603 * If a font is not going to be used, free its structure.
5604 */
5605 void
5606gui_mch_free_font(GuiFont font)
5607{
5608 if (font != NOFONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 pango_font_description_free(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610}
5611
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612/*
Bram Moolenaar36edf062016-07-21 22:10:12 +02005613 * Return the Pixel value (color) for the given color name.
5614 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 * Return INVALCOLOR for error.
5616 */
5617 guicolor_T
5618gui_mch_get_color(char_u *name)
5619{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 if (!gui.in_use) /* can't do this when GUI not running */
5621 return INVALCOLOR;
5622
Bram Moolenaar98921892016-02-23 17:14:37 +01005623#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02005624 return name != NULL ? gui_get_color_cmn(name) : INVALCOLOR;
Bram Moolenaar98921892016-02-23 17:14:37 +01005625#else
Bram Moolenaar36edf062016-07-21 22:10:12 +02005626 guicolor_T color;
5627 GdkColor gcolor;
5628 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629
Bram Moolenaar36edf062016-07-21 22:10:12 +02005630 color = (name != NULL) ? gui_get_color_cmn(name) : INVALCOLOR;
5631 if (color == INVALCOLOR)
5632 return INVALCOLOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633
Bram Moolenaar36edf062016-07-21 22:10:12 +02005634 gcolor.red = (guint16)(((color & 0xff0000) >> 16) / 255.0 * 65535 + 0.5);
5635 gcolor.green = (guint16)(((color & 0xff00) >> 8) / 255.0 * 65535 + 0.5);
5636 gcolor.blue = (guint16)((color & 0xff) / 255.0 * 65535 + 0.5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637
Bram Moolenaar36edf062016-07-21 22:10:12 +02005638 ret = gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea),
5639 &gcolor, FALSE, TRUE);
5640
5641 return ret != 0 ? (guicolor_T)gcolor.pixel : INVALCOLOR;
5642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643}
5644
5645/*
5646 * Set the current text foreground color.
5647 */
5648 void
5649gui_mch_set_fg_color(guicolor_T color)
5650{
Bram Moolenaar36edf062016-07-21 22:10:12 +02005651#if GTK_CHECK_VERSION(3,0,0)
5652 *gui.fgcolor = color_to_rgba(color);
5653#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 gui.fgcolor->pixel = (unsigned long)color;
Bram Moolenaar36edf062016-07-21 22:10:12 +02005655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656}
5657
5658/*
5659 * Set the current text background color.
5660 */
5661 void
5662gui_mch_set_bg_color(guicolor_T color)
5663{
Bram Moolenaar36edf062016-07-21 22:10:12 +02005664#if GTK_CHECK_VERSION(3,0,0)
5665 *gui.bgcolor = color_to_rgba(color);
5666#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 gui.bgcolor->pixel = (unsigned long)color;
Bram Moolenaar36edf062016-07-21 22:10:12 +02005668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669}
5670
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005671/*
5672 * Set the current text special color.
5673 */
5674 void
5675gui_mch_set_sp_color(guicolor_T color)
5676{
Bram Moolenaar36edf062016-07-21 22:10:12 +02005677#if GTK_CHECK_VERSION(3,0,0)
5678 *gui.spcolor = color_to_rgba(color);
5679#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005680 gui.spcolor->pixel = (unsigned long)color;
Bram Moolenaar36edf062016-07-21 22:10:12 +02005681#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005682}
5683
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684/*
5685 * Function-like convenience macro for the sake of efficiency.
5686 */
5687#define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
5688 G_STMT_START{ \
5689 PangoAttribute *tmp_attr_; \
5690 tmp_attr_ = (Attribute); \
5691 tmp_attr_->start_index = (Start); \
5692 tmp_attr_->end_index = (End); \
5693 pango_attr_list_insert((AttrList), tmp_attr_); \
5694 }G_STMT_END
5695
5696 static void
5697apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list)
5698{
5699 char_u *start = NULL;
5700 char_u *p;
5701 int uc;
5702
5703 for (p = s; p < s + len; p += utf_byte2len(*p))
5704 {
5705 uc = utf_ptr2char(p);
5706
5707 if (start == NULL)
5708 {
5709 if (uc >= 0x80 && utf_char2cells(uc) == 2)
5710 start = p;
5711 }
5712 else if (uc < 0x80 /* optimization shortcut */
5713 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc)))
5714 {
5715 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5716 attr_list, start - s, p - s);
5717 start = NULL;
5718 }
5719 }
5720
5721 if (start != NULL)
5722 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5723 attr_list, start - s, len);
5724}
5725
5726 static int
5727count_cluster_cells(char_u *s, PangoItem *item,
5728 PangoGlyphString* glyphs, int i,
5729 int *cluster_width,
5730 int *last_glyph_rbearing)
5731{
5732 char_u *p;
5733 int next; /* glyph start index of next cluster */
5734 int start, end; /* string segment of current cluster */
5735 int width; /* real cluster width in Pango units */
5736 int uc;
5737 int cellcount = 0;
5738
5739 width = glyphs->glyphs[i].geometry.width;
5740
5741 for (next = i + 1; next < glyphs->num_glyphs; ++next)
5742 {
5743 if (glyphs->glyphs[next].attr.is_cluster_start)
5744 break;
5745 else if (glyphs->glyphs[next].geometry.width > width)
5746 width = glyphs->glyphs[next].geometry.width;
5747 }
5748
5749 start = item->offset + glyphs->log_clusters[i];
5750 end = item->offset + ((next < glyphs->num_glyphs) ?
5751 glyphs->log_clusters[next] : item->length);
5752
5753 for (p = s + start; p < s + end; p += utf_byte2len(*p))
5754 {
5755 uc = utf_ptr2char(p);
5756 if (uc < 0x80)
5757 ++cellcount;
5758 else if (!utf_iscomposing(uc))
5759 cellcount += utf_char2cells(uc);
5760 }
5761
5762 if (last_glyph_rbearing != NULL
5763 && cellcount > 0 && next == glyphs->num_glyphs)
5764 {
5765 PangoRectangle ink_rect;
5766 /*
5767 * If a certain combining mark had to be taken from a non-monospace
5768 * font, we have to compensate manually by adapting x_offset according
5769 * to the ink extents of the previous glyph.
5770 */
5771 pango_font_get_glyph_extents(item->analysis.font,
5772 glyphs->glyphs[i].glyph,
5773 &ink_rect, NULL);
5774
5775 if (PANGO_RBEARING(ink_rect) > 0)
5776 *last_glyph_rbearing = PANGO_RBEARING(ink_rect);
5777 }
5778
5779 if (cellcount > 0)
5780 *cluster_width = width;
5781
5782 return cellcount;
5783}
5784
5785/*
5786 * If there are only combining characters in the cluster, we cannot just
5787 * change the width of the previous glyph since there is none. Therefore
5788 * some guesswork is needed.
5789 *
5790 * If ink_rect.x is negative Pango apparently has taken care of the composing
5791 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
5792 * to problems with composing from different fonts we still need to fine-tune
Bram Moolenaar64404472010-06-26 06:24:45 +02005793 * x_offset to avoid ugliness.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 *
5795 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
5796 * the position of the previous glyph. Apparently this happens only with old
5797 * X fonts which don't provide the special combining information needed by
5798 * Pango.
5799 */
5800 static void
5801setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph,
5802 int last_cellcount, int last_cluster_width,
5803 int last_glyph_rbearing)
5804{
5805 PangoRectangle ink_rect;
5806 PangoRectangle logical_rect;
5807 int width;
5808
5809 width = last_cellcount * gui.char_width * PANGO_SCALE;
5810 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2;
5811 glyph->geometry.width = 0;
5812
5813 pango_font_get_glyph_extents(item->analysis.font,
5814 glyph->glyph,
5815 &ink_rect, &logical_rect);
5816 if (ink_rect.x < 0)
5817 {
5818 glyph->geometry.x_offset += last_glyph_rbearing;
5819 glyph->geometry.y_offset = logical_rect.height
5820 - (gui.char_height - p_linespace) * PANGO_SCALE;
5821 }
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005822 else
5823 /* If the accent width is smaller than the cluster width, position it
5824 * in the middle. */
5825 glyph->geometry.x_offset = -width + MAX(0, width - ink_rect.width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826}
5827
Bram Moolenaar98921892016-02-23 17:14:37 +01005828#if GTK_CHECK_VERSION(3,0,0)
5829 static void
5830draw_glyph_string(int row, int col, int num_cells, int flags,
5831 PangoFont *font, PangoGlyphString *glyphs,
5832 cairo_t *cr)
5833#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 static void
5835draw_glyph_string(int row, int col, int num_cells, int flags,
5836 PangoFont *font, PangoGlyphString *glyphs)
Bram Moolenaar98921892016-02-23 17:14:37 +01005837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838{
5839 if (!(flags & DRAW_TRANSP))
5840 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005841#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02005842 cairo_set_source_rgba(cr,
5843 gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue,
5844 gui.bgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005845 cairo_rectangle(cr,
5846 FILL_X(col), FILL_Y(row),
5847 num_cells * gui.char_width, gui.char_height);
5848 cairo_fill(cr);
5849#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
5851
5852 gdk_draw_rectangle(gui.drawarea->window,
5853 gui.text_gc,
5854 TRUE,
5855 FILL_X(col),
5856 FILL_Y(row),
5857 num_cells * gui.char_width,
5858 gui.char_height);
Bram Moolenaar98921892016-02-23 17:14:37 +01005859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 }
5861
Bram Moolenaar98921892016-02-23 17:14:37 +01005862#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02005863 cairo_set_source_rgba(cr,
5864 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5865 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005866 cairo_move_to(cr, TEXT_X(col), TEXT_Y(row));
5867 pango_cairo_show_glyph_string(cr, font, glyphs);
5868#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5870
5871 gdk_draw_glyphs(gui.drawarea->window,
5872 gui.text_gc,
5873 font,
5874 TEXT_X(col),
5875 TEXT_Y(row),
5876 glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01005877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878
5879 /* redraw the contents with an offset of 1 to emulate bold */
5880 if ((flags & DRAW_BOLD) && !gui.font_can_bold)
Bram Moolenaar98921892016-02-23 17:14:37 +01005881#if GTK_CHECK_VERSION(3,0,0)
5882 {
Bram Moolenaar36edf062016-07-21 22:10:12 +02005883 cairo_set_source_rgba(cr,
5884 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5885 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005886 cairo_move_to(cr, TEXT_X(col) + 1, TEXT_Y(row));
5887 pango_cairo_show_glyph_string(cr, font, glyphs);
5888 }
5889#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 gdk_draw_glyphs(gui.drawarea->window,
5891 gui.text_gc,
5892 font,
5893 TEXT_X(col) + 1,
5894 TEXT_Y(row),
5895 glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01005896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897}
5898
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005899/*
5900 * Draw underline and undercurl at the bottom of the character cell.
5901 */
Bram Moolenaar98921892016-02-23 17:14:37 +01005902#if GTK_CHECK_VERSION(3,0,0)
5903 static void
5904draw_under(int flags, int row, int col, int cells, cairo_t *cr)
5905#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005906 static void
5907draw_under(int flags, int row, int col, int cells)
Bram Moolenaar98921892016-02-23 17:14:37 +01005908#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005909{
5910 int i;
5911 int offset;
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005912 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005913 int y = FILL_Y(row + 1) - 1;
5914
5915 /* Undercurl: draw curl at the bottom of the character cell. */
5916 if (flags & DRAW_UNDERC)
5917 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005918#if GTK_CHECK_VERSION(3,0,0)
5919 cairo_set_line_width(cr, 1.0);
5920 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
Bram Moolenaar36edf062016-07-21 22:10:12 +02005921 cairo_set_source_rgba(cr,
5922 gui.spcolor->red, gui.spcolor->green, gui.spcolor->blue,
5923 gui.spcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005924 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
5925 {
5926 offset = val[i % 8];
5927 cairo_line_to(cr, i, y - offset + 0.5);
5928 }
5929 cairo_stroke(cr);
5930#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005931 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
5932 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
5933 {
5934 offset = val[i % 8];
5935 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset);
5936 }
5937 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar98921892016-02-23 17:14:37 +01005938#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005939 }
5940
5941 /* Underline: draw a line at the bottom of the character cell. */
5942 if (flags & DRAW_UNDERL)
5943 {
5944 /* When p_linespace is 0, overwrite the bottom row of pixels.
5945 * Otherwise put the line just below the character. */
5946 if (p_linespace > 1)
5947 y -= p_linespace - 1;
Bram Moolenaar98921892016-02-23 17:14:37 +01005948#if GTK_CHECK_VERSION(3,0,0)
5949 {
5950 cairo_set_line_width(cr, 1.0);
5951 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
Bram Moolenaar36edf062016-07-21 22:10:12 +02005952 cairo_set_source_rgba(cr,
5953 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5954 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005955 cairo_move_to(cr, FILL_X(col), y + 0.5);
5956 cairo_line_to(cr, FILL_X(col + cells), y + 0.5);
5957 cairo_stroke(cr);
5958 }
5959#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005960 gdk_draw_line(gui.drawarea->window, gui.text_gc,
5961 FILL_X(col), y,
5962 FILL_X(col + cells) - 1, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01005963#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005964 }
5965}
5966
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 int
5968gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
5969{
5970 GdkRectangle area; /* area for clip mask */
5971 PangoGlyphString *glyphs; /* glyphs of current item */
5972 int column_offset = 0; /* column offset in cells */
5973 int i;
5974 char_u *conv_buf = NULL; /* result of UTF-8 conversion */
5975 char_u *new_conv_buf;
5976 int convlen;
5977 char_u *sp, *bp;
5978 int plen;
Bram Moolenaar98921892016-02-23 17:14:37 +01005979#if GTK_CHECK_VERSION(3,0,0)
5980 cairo_t *cr;
5981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982
Bram Moolenaar98921892016-02-23 17:14:37 +01005983#if GTK_CHECK_VERSION(3,0,0)
5984 if (gui.text_context == NULL || gtk_widget_get_window(gui.drawarea) == NULL)
5985#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 if (gui.text_context == NULL || gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01005987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 return len;
5989
5990 if (output_conv.vc_type != CONV_NONE)
5991 {
5992 /*
5993 * Convert characters from 'encoding' to 'termencoding', which is set
5994 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
5995 * prohibits changing this to something else than UTF-8 if the GUI is
5996 * in use.
5997 */
5998 convlen = len;
5999 conv_buf = string_convert(&output_conv, s, &convlen);
6000 g_return_val_if_fail(conv_buf != NULL, len);
6001
6002 /* Correct for differences in char width: some chars are
6003 * double-wide in 'encoding' but single-wide in utf-8. Add a space to
6004 * compensate for that. */
6005 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; )
6006 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006007 plen = utf_ptr2len(bp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1)
6009 {
6010 new_conv_buf = alloc(convlen + 2);
6011 if (new_conv_buf == NULL)
6012 return len;
6013 plen += bp - conv_buf;
6014 mch_memmove(new_conv_buf, conv_buf, plen);
6015 new_conv_buf[plen] = ' ';
6016 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen,
6017 convlen - plen + 1);
6018 vim_free(conv_buf);
6019 conv_buf = new_conv_buf;
6020 ++convlen;
6021 bp = conv_buf + plen;
6022 plen = 1;
6023 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006024 sp += (*mb_ptr2len)(sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 bp += plen;
6026 }
6027 s = conv_buf;
6028 len = convlen;
6029 }
6030
6031 /*
6032 * Restrict all drawing to the current screen line in order to prevent
6033 * fuzzy font lookups from messing up the screen.
6034 */
6035 area.x = gui.border_offset;
6036 area.y = FILL_Y(row);
6037 area.width = gui.num_cols * gui.char_width;
6038 area.height = gui.char_height;
6039
Bram Moolenaar98921892016-02-23 17:14:37 +01006040#if GTK_CHECK_VERSION(3,0,0)
6041 cr = cairo_create(gui.surface);
6042 cairo_rectangle(cr, area.x, area.y, area.width, area.height);
6043 cairo_clip(cr);
6044#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 gdk_gc_set_clip_origin(gui.text_gc, 0, 0);
6046 gdk_gc_set_clip_rectangle(gui.text_gc, &area);
Bram Moolenaar98921892016-02-23 17:14:37 +01006047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048
6049 glyphs = pango_glyph_string_new();
6050
6051 /*
6052 * Optimization hack: If possible, skip the itemize and shaping process
6053 * for pure ASCII strings. This optimization is particularly effective
6054 * because Vim draws space characters to clear parts of the screen.
6055 */
6056 if (!(flags & DRAW_ITALIC)
6057 && !((flags & DRAW_BOLD) && gui.font_can_bold)
6058 && gui.ascii_glyphs != NULL)
6059 {
6060 char_u *p;
6061
6062 for (p = s; p < s + len; ++p)
6063 if (*p & 0x80)
6064 goto not_ascii;
6065
6066 pango_glyph_string_set_size(glyphs, len);
6067
6068 for (i = 0; i < len; ++i)
6069 {
Bram Moolenaar16350cb2016-08-14 20:27:34 +02006070 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[2 * s[i]];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 glyphs->log_clusters[i] = i;
6072 }
6073
Bram Moolenaar98921892016-02-23 17:14:37 +01006074#if GTK_CHECK_VERSION(3,0,0)
6075 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs, cr);
6076#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01006078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079
6080 column_offset = len;
6081 }
6082 else
6083not_ascii:
6084 {
6085 PangoAttrList *attr_list;
6086 GList *item_list;
6087 int cluster_width;
6088 int last_glyph_rbearing;
6089 int cells = 0; /* cells occupied by current cluster */
6090
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006091 /* Safety check: pango crashes when invoked with invalid utf-8
6092 * characters. */
6093 if (!utf_valid_string(s, s + len))
6094 {
6095 column_offset = len;
6096 goto skipitall;
6097 }
6098
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 /* original width of the current cluster */
6100 cluster_width = PANGO_SCALE * gui.char_width;
6101
6102 /* right bearing of the last non-composing glyph */
6103 last_glyph_rbearing = PANGO_SCALE * gui.char_width;
6104
6105 attr_list = pango_attr_list_new();
6106
6107 /* If 'guifontwide' is set then use that for double-width characters.
6108 * Otherwise just go with 'guifont' and let Pango do its thing. */
6109 if (gui.wide_font != NULL)
6110 apply_wide_font_attr(s, len, attr_list);
6111
6112 if ((flags & DRAW_BOLD) && gui.font_can_bold)
6113 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD),
6114 attr_list, 0, len);
6115 if (flags & DRAW_ITALIC)
6116 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC),
6117 attr_list, 0, len);
6118 /*
6119 * Break the text into segments with consistent directional level
6120 * and shaping engine. Pure Latin text needs only a single segment,
6121 * so there's no need to worry about the loop's efficiency. Better
6122 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
6123 */
6124 item_list = pango_itemize(gui.text_context,
6125 (const char *)s, 0, len, attr_list, NULL);
6126
6127 while (item_list != NULL)
6128 {
6129 PangoItem *item;
6130 int item_cells = 0; /* item length in cells */
6131
6132 item = (PangoItem *)item_list->data;
6133 item_list = g_list_delete_link(item_list, item_list);
6134 /*
6135 * Increment the bidirectional embedding level by 1 if it is not
6136 * even. An odd number means the output will be RTL, but we don't
6137 * want that since Vim handles right-to-left text on its own. It
6138 * would probably be sufficient to just set level = 0, but you can
6139 * never know :)
6140 *
6141 * Unfortunately we can't take advantage of Pango's ability to
6142 * render both LTR and RTL at the same time. In order to support
6143 * that, Vim's main screen engine would have to make use of Pango
6144 * functionality.
6145 */
6146 item->analysis.level = (item->analysis.level + 1) & (~1U);
6147
6148 /* HACK: Overrule the shape engine, we don't want shaping to be
6149 * done, because drawing the cursor would change the display. */
6150 item->analysis.shape_engine = default_shape_engine;
6151
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02006152#ifdef HAVE_PANGO_SHAPE_FULL
Bram Moolenaar7e2ec002015-09-08 16:31:06 +02006153 pango_shape_full((const char *)s + item->offset, item->length,
6154 (const char *)s, len, &item->analysis, glyphs);
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02006155#else
6156 pango_shape((const char *)s + item->offset, item->length,
6157 &item->analysis, glyphs);
6158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 /*
6160 * Fixed-width hack: iterate over the array and assign a fixed
6161 * width to each glyph, thus overriding the choice made by the
6162 * shaping engine. We use utf_char2cells() to determine the
6163 * number of cells needed.
6164 *
6165 * Also perform all kind of dark magic to get composing
6166 * characters right (and pretty too of course).
6167 */
6168 for (i = 0; i < glyphs->num_glyphs; ++i)
6169 {
6170 PangoGlyphInfo *glyph;
6171
6172 glyph = &glyphs->glyphs[i];
6173
6174 if (glyph->attr.is_cluster_start)
6175 {
6176 int cellcount;
6177
6178 cellcount = count_cluster_cells(
6179 s, item, glyphs, i, &cluster_width,
6180 (item_list != NULL) ? &last_glyph_rbearing : NULL);
6181
6182 if (cellcount > 0)
6183 {
6184 int width;
6185
6186 width = cellcount * gui.char_width * PANGO_SCALE;
6187 glyph->geometry.x_offset +=
6188 MAX(0, width - cluster_width) / 2;
6189 glyph->geometry.width = width;
6190 }
6191 else
6192 {
6193 /* If there are only combining characters in the
6194 * cluster, we cannot just change the width of the
6195 * previous glyph since there is none. Therefore
6196 * some guesswork is needed. */
6197 setup_zero_width_cluster(item, glyph, cells,
6198 cluster_width,
6199 last_glyph_rbearing);
6200 }
6201
6202 item_cells += cellcount;
6203 cells = cellcount;
6204 }
6205 else if (i > 0)
6206 {
6207 int width;
6208
6209 /* There is a previous glyph, so we deal with combining
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006210 * characters the canonical way.
Bram Moolenaar96118f32010-08-08 14:40:37 +02006211 * In some circumstances Pango uses a positive x_offset,
6212 * then use the width of the previous glyph for this one
6213 * and set the previous width to zero.
6214 * Otherwise we get a negative x_offset, Pango has already
6215 * positioned the combining char, keep the widths as they
6216 * are.
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006217 * For both adjust the x_offset to position the glyph in
Bram Moolenaar96118f32010-08-08 14:40:37 +02006218 * the middle. */
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006219 if (glyph->geometry.x_offset >= 0)
Bram Moolenaar96118f32010-08-08 14:40:37 +02006220 {
6221 glyphs->glyphs[i].geometry.width =
6222 glyphs->glyphs[i - 1].geometry.width;
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006223 glyphs->glyphs[i - 1].geometry.width = 0;
Bram Moolenaar96118f32010-08-08 14:40:37 +02006224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 width = cells * gui.char_width * PANGO_SCALE;
6226 glyph->geometry.x_offset +=
6227 MAX(0, width - cluster_width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 }
6229 else /* i == 0 "cannot happen" */
6230 {
6231 glyph->geometry.width = 0;
6232 }
6233 }
6234
6235 /*** Aaaaand action! ***/
Bram Moolenaar98921892016-02-23 17:14:37 +01006236#if GTK_CHECK_VERSION(3,0,0)
6237 draw_glyph_string(row, col + column_offset, item_cells,
6238 flags, item->analysis.font, glyphs,
6239 cr);
6240#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 draw_glyph_string(row, col + column_offset, item_cells,
6242 flags, item->analysis.font, glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01006243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244
6245 pango_item_free(item);
6246
6247 column_offset += item_cells;
6248 }
6249
6250 pango_attr_list_unref(attr_list);
6251 }
6252
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006253skipitall:
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006254 /* Draw underline and undercurl. */
Bram Moolenaar98921892016-02-23 17:14:37 +01006255#if GTK_CHECK_VERSION(3,0,0)
6256 draw_under(flags, row, col, column_offset, cr);
6257#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006258 draw_under(flags, row, col, column_offset);
Bram Moolenaar98921892016-02-23 17:14:37 +01006259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260
6261 pango_glyph_string_free(glyphs);
6262 vim_free(conv_buf);
6263
Bram Moolenaar98921892016-02-23 17:14:37 +01006264#if GTK_CHECK_VERSION(3,0,0)
6265 cairo_destroy(cr);
6266 if (!gui.by_signal)
6267 gdk_window_invalidate_rect(gtk_widget_get_window(gui.drawarea),
6268 &area, FALSE);
6269#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 gdk_gc_set_clip_rectangle(gui.text_gc, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01006271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272
6273 return column_offset;
6274}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275
6276/*
6277 * Return OK if the key with the termcap name "name" is supported.
6278 */
6279 int
6280gui_mch_haskey(char_u *name)
6281{
6282 int i;
6283
6284 for (i = 0; special_keys[i].key_sym != 0; i++)
6285 if (name[0] == special_keys[i].code0
6286 && name[1] == special_keys[i].code1)
6287 return OK;
6288 return FAIL;
6289}
6290
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006291#if defined(FEAT_TITLE) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292/*
6293 * Return the text window-id and display. Only required for X-based GUI's
6294 */
6295 int
6296gui_get_x11_windis(Window *win, Display **dis)
6297{
Bram Moolenaar98921892016-02-23 17:14:37 +01006298#if GTK_CHECK_VERSION(3,0,0)
6299 if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL)
6300#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01006302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 {
Bram Moolenaar98921892016-02-23 17:14:37 +01006304#if GTK_CHECK_VERSION(3,0,0)
6305 *dis = GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin));
6306 *win = GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin));
6307#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 *dis = GDK_WINDOW_XDISPLAY(gui.mainwin->window);
6309 *win = GDK_WINDOW_XWINDOW(gui.mainwin->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01006310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 return OK;
6312 }
6313
6314 *dis = NULL;
6315 *win = 0;
6316 return FAIL;
6317}
6318#endif
6319
6320#if defined(FEAT_CLIENTSERVER) \
6321 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
6322
6323 Display *
6324gui_mch_get_display(void)
6325{
Bram Moolenaar98921892016-02-23 17:14:37 +01006326#if GTK_CHECK_VERSION(3,0,0)
6327 if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL)
6328 return GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin));
6329#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
6331 return GDK_WINDOW_XDISPLAY(gui.mainwin->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01006332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 else
6334 return NULL;
6335}
6336#endif
6337
6338 void
6339gui_mch_beep(void)
6340{
6341#ifdef HAVE_GTK_MULTIHEAD
6342 GdkDisplay *display;
6343
Bram Moolenaar98921892016-02-23 17:14:37 +01006344# if GTK_CHECK_VERSION(3,0,0)
6345 if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin))
6346# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
Bram Moolenaar98921892016-02-23 17:14:37 +01006348# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 display = gtk_widget_get_display(gui.mainwin);
6350 else
6351 display = gdk_display_get_default();
6352
6353 if (display != NULL)
6354 gdk_display_beep(display);
6355#else
6356 gdk_beep();
6357#endif
6358}
6359
6360 void
6361gui_mch_flash(int msec)
6362{
Bram Moolenaar98921892016-02-23 17:14:37 +01006363#if GTK_CHECK_VERSION(3,0,0)
6364 /* TODO Replace GdkGC with Cairo */
6365 (void)msec;
6366#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 GdkGCValues values;
6368 GdkGC *invert_gc;
6369
6370 if (gui.drawarea->window == NULL)
6371 return;
6372
6373 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6374 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
6375 values.function = GDK_XOR;
6376 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6377 &values,
6378 GDK_GC_FOREGROUND |
6379 GDK_GC_BACKGROUND |
6380 GDK_GC_FUNCTION);
6381 gdk_gc_set_exposures(invert_gc,
6382 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
6383 /*
6384 * Do a visual beep by changing back and forth in some undetermined way,
6385 * the foreground and background colors. This is due to the fact that
6386 * there can't be really any prediction about the effects of XOR on
6387 * arbitrary X11 servers. However this seems to be enough for what we
6388 * intend it to do.
6389 */
6390 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6391 TRUE,
6392 0, 0,
6393 FILL_X((int)Columns) + gui.border_offset,
6394 FILL_Y((int)Rows) + gui.border_offset);
6395
6396 gui_mch_flush();
6397 ui_delay((long)msec, TRUE); /* wait so many msec */
6398
6399 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6400 TRUE,
6401 0, 0,
6402 FILL_X((int)Columns) + gui.border_offset,
6403 FILL_Y((int)Rows) + gui.border_offset);
6404
6405 gdk_gc_destroy(invert_gc);
Bram Moolenaar98921892016-02-23 17:14:37 +01006406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407}
6408
6409/*
6410 * Invert a rectangle from row r, column c, for nr rows and nc columns.
6411 */
6412 void
6413gui_mch_invert_rectangle(int r, int c, int nr, int nc)
6414{
Bram Moolenaar98921892016-02-23 17:14:37 +01006415#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar4fc563b2016-03-12 12:40:58 +01006416 const GdkRectangle rect = {
6417 FILL_X(c), FILL_Y(r), nc * gui.char_width, nr * gui.char_height
6418 };
6419 cairo_t * const cr = cairo_create(gui.surface);
6420
Bram Moolenaar36edf062016-07-21 22:10:12 +02006421 set_cairo_source_rgba_from_color(cr, gui.norm_pixel ^ gui.back_pixel);
Bram Moolenaar4fc563b2016-03-12 12:40:58 +01006422# if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,9,2)
6423 cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE);
6424# else
6425 /* Give an implementation for older cairo versions if necessary. */
6426# endif
6427 gdk_cairo_rectangle(cr, &rect);
6428 cairo_fill(cr);
6429
6430 cairo_destroy(cr);
6431
6432 if (!gui.by_signal)
6433 gtk_widget_queue_draw_area(gui.drawarea, rect.x, rect.y,
6434 rect.width, rect.height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006435#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 GdkGCValues values;
6437 GdkGC *invert_gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438
6439 if (gui.drawarea->window == NULL)
6440 return;
6441
Bram Moolenaar89d40322006-08-29 15:30:07 +00006442 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6443 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 values.function = GDK_XOR;
6445 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6446 &values,
6447 GDK_GC_FOREGROUND |
6448 GDK_GC_BACKGROUND |
6449 GDK_GC_FUNCTION);
Bram Moolenaar89d40322006-08-29 15:30:07 +00006450 gdk_gc_set_exposures(invert_gc, gui.visibility !=
6451 GDK_VISIBILITY_UNOBSCURED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6453 TRUE,
6454 FILL_X(c), FILL_Y(r),
6455 (nc) * gui.char_width, (nr) * gui.char_height);
6456 gdk_gc_destroy(invert_gc);
Bram Moolenaar98921892016-02-23 17:14:37 +01006457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458}
6459
6460/*
6461 * Iconify the GUI window.
6462 */
6463 void
6464gui_mch_iconify(void)
6465{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 gtk_window_iconify(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467}
6468
6469#if defined(FEAT_EVAL) || defined(PROTO)
6470/*
6471 * Bring the Vim window to the foreground.
6472 */
6473 void
6474gui_mch_set_foreground(void)
6475{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 gtk_window_present(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477}
6478#endif
6479
6480/*
6481 * Draw a cursor without focus.
6482 */
6483 void
6484gui_mch_draw_hollow_cursor(guicolor_T color)
6485{
6486 int i = 1;
Bram Moolenaar98921892016-02-23 17:14:37 +01006487#if GTK_CHECK_VERSION(3,0,0)
6488 cairo_t *cr;
6489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490
Bram Moolenaar98921892016-02-23 17:14:37 +01006491#if GTK_CHECK_VERSION(3,0,0)
6492 if (gtk_widget_get_window(gui.drawarea) == NULL)
6493#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 if (gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01006495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 return;
6497
Bram Moolenaar98921892016-02-23 17:14:37 +01006498#if GTK_CHECK_VERSION(3,0,0)
6499 cr = cairo_create(gui.surface);
6500#endif
6501
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 gui_mch_set_fg_color(color);
6503
Bram Moolenaar98921892016-02-23 17:14:37 +01006504#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02006505 cairo_set_source_rgba(cr,
6506 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
6507 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01006508#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar98921892016-02-23 17:14:37 +01006510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 if (mb_lefthalve(gui.row, gui.col))
6512 i = 2;
Bram Moolenaar98921892016-02-23 17:14:37 +01006513#if GTK_CHECK_VERSION(3,0,0)
6514 cairo_set_line_width(cr, 1.0);
6515 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
6516 cairo_rectangle(cr,
6517 FILL_X(gui.col) + 0.5, FILL_Y(gui.row) + 0.5,
6518 i * gui.char_width - 1, gui.char_height - 1);
6519 cairo_stroke(cr);
6520 cairo_destroy(cr);
6521#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6523 FALSE,
6524 FILL_X(gui.col), FILL_Y(gui.row),
6525 i * gui.char_width - 1, gui.char_height - 1);
Bram Moolenaar98921892016-02-23 17:14:37 +01006526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527}
6528
6529/*
6530 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
6531 * color "color".
6532 */
6533 void
6534gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
6535{
Bram Moolenaar98921892016-02-23 17:14:37 +01006536#if GTK_CHECK_VERSION(3,0,0)
6537 if (gtk_widget_get_window(gui.drawarea) == NULL)
6538#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 if (gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01006540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 return;
6542
6543 gui_mch_set_fg_color(color);
6544
Bram Moolenaar98921892016-02-23 17:14:37 +01006545#if GTK_CHECK_VERSION(3,0,0)
6546 {
6547 cairo_t *cr;
6548
6549 cr = cairo_create(gui.surface);
Bram Moolenaar36edf062016-07-21 22:10:12 +02006550 cairo_set_source_rgba(cr,
6551 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
6552 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01006553 cairo_rectangle(cr,
6554# ifdef FEAT_RIGHTLEFT
6555 /* vertical line should be on the right of current point */
6556 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
6557# endif
6558 FILL_X(gui.col), FILL_Y(gui.row) + gui.char_height - h,
6559 w, h);
6560 cairo_fill(cr);
6561 cairo_destroy(cr);
6562 }
6563#else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6565 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6566 TRUE,
Bram Moolenaar98921892016-02-23 17:14:37 +01006567# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 /* vertical line should be on the right of current point */
6569 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
Bram Moolenaar98921892016-02-23 17:14:37 +01006570# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 FILL_X(gui.col),
6572 FILL_Y(gui.row) + gui.char_height - h,
6573 w, h);
Bram Moolenaar98921892016-02-23 17:14:37 +01006574#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575}
6576
6577
6578/*
6579 * Catch up with any queued X11 events. This may put keyboard input into the
6580 * input buffer, call resize call-backs, trigger timers etc. If there is
6581 * nothing in the X11 event queue (& no timers pending), then we return
6582 * immediately.
6583 */
6584 void
6585gui_mch_update(void)
6586{
Bram Moolenaara3f41662010-07-11 19:01:06 +02006587 while (g_main_context_pending(NULL) && !vim_is_input_buf_full())
6588 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589}
6590
Bram Moolenaar98921892016-02-23 17:14:37 +01006591#if GTK_CHECK_VERSION(3,0,0)
6592 static gboolean
6593#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +01006595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596input_timer_cb(gpointer data)
6597{
6598 int *timed_out = (int *) data;
6599
Bram Moolenaar49325942007-05-10 19:19:59 +00006600 /* Just inform the caller about the occurrence of it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 *timed_out = TRUE;
6602
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 return FALSE; /* don't happen again */
6604}
6605
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606/*
6607 * GUI input routine called by gui_wait_for_chars(). Waits for a character
6608 * from the keyboard.
6609 * wtime == -1 Wait forever.
6610 * wtime == 0 This should never happen.
6611 * wtime > 0 Wait wtime milliseconds for a character.
6612 * Returns OK if a character was found to be available within the given time,
6613 * or FAIL otherwise.
6614 */
6615 int
6616gui_mch_wait_for_chars(long wtime)
6617{
Bram Moolenaar4231da42016-06-02 14:30:04 +02006618 int focus;
6619 guint timer;
6620 static int timed_out;
6621 int retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622
6623 timed_out = FALSE;
6624
6625 /* this timeout makes sure that we will return if no characters arrived in
6626 * time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 if (wtime > 0)
Bram Moolenaar98921892016-02-23 17:14:37 +01006628#if GTK_CHECK_VERSION(3,0,0)
6629 timer = g_timeout_add((guint)wtime, input_timer_cb, &timed_out);
6630#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 timer = gtk_timeout_add((guint32)wtime, input_timer_cb, &timed_out);
Bram Moolenaar98921892016-02-23 17:14:37 +01006632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 else
6634 timer = 0;
6635
6636 focus = gui.in_focus;
6637
6638 do
6639 {
6640 /* Stop or start blinking when focus changes */
6641 if (gui.in_focus != focus)
6642 {
6643 if (gui.in_focus)
6644 gui_mch_start_blink();
6645 else
6646 gui_mch_stop_blink();
6647 focus = gui.in_focus;
6648 }
6649
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006650#ifdef MESSAGE_QUEUE
Bram Moolenaar4231da42016-06-02 14:30:04 +02006651# ifdef FEAT_TIMERS
6652 did_add_timer = FALSE;
6653# endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006654 parse_queued_messages();
Bram Moolenaar4231da42016-06-02 14:30:04 +02006655# ifdef FEAT_TIMERS
6656 if (did_add_timer)
6657 /* Need to recompute the waiting time. */
6658 goto theend;
6659# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006660#endif
6661
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 /*
6663 * Loop in GTK+ processing until a timeout or input occurs.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006664 * Skip this if input is available anyway (can happen in rare
6665 * situations, sort of race condition).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006667 if (!input_available())
Bram Moolenaara3f41662010-07-11 19:01:06 +02006668 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669
6670 /* Got char, return immediately */
6671 if (input_available())
6672 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02006673 retval = OK;
6674 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 }
6676 } while (wtime < 0 || !timed_out);
6677
6678 /*
6679 * Flush all eventually pending (drawing) events.
6680 */
6681 gui_mch_update();
6682
Bram Moolenaar4231da42016-06-02 14:30:04 +02006683theend:
6684 if (timer != 0 && !timed_out)
6685#if GTK_CHECK_VERSION(3,0,0)
6686 g_source_remove(timer);
6687#else
6688 gtk_timeout_remove(timer);
6689#endif
6690
6691 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692}
6693
6694
6695/****************************************************************************
6696 * Output drawing routines.
6697 ****************************************************************************/
6698
6699
6700/* Flush any output to the screen */
6701 void
6702gui_mch_flush(void)
6703{
6704#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar98921892016-02-23 17:14:37 +01006705# if GTK_CHECK_VERSION(3,0,0)
6706 if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin))
6707# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
Bram Moolenaar98921892016-02-23 17:14:37 +01006709# endif
Bram Moolenaarfdadad92016-07-15 17:49:58 +02006710 gdk_display_flush(gtk_widget_get_display(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711#else
6712 gdk_flush(); /* historical misnomer: calls XSync(), not XFlush() */
6713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714}
6715
6716/*
6717 * Clear a rectangular region of the screen from text pos (row1, col1) to
6718 * (row2, col2) inclusive.
6719 */
6720 void
Bram Moolenaarfe344a92017-02-23 12:20:35 +01006721gui_mch_clear_block(int row1arg, int col1arg, int row2arg, int col2arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722{
Bram Moolenaarfe344a92017-02-23 12:20:35 +01006723
6724 int col1 = check_col(col1arg);
6725 int col2 = check_col(col2arg);
6726 int row1 = check_row(row1arg);
6727 int row2 = check_row(row2arg);
6728
Bram Moolenaar98921892016-02-23 17:14:37 +01006729#if GTK_CHECK_VERSION(3,0,0)
6730 if (gtk_widget_get_window(gui.drawarea) == NULL)
6731 return;
6732#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 GdkColor color;
6734
6735 if (gui.drawarea->window == NULL)
6736 return;
6737
6738 color.pixel = gui.back_pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01006739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740
Bram Moolenaar98921892016-02-23 17:14:37 +01006741#if GTK_CHECK_VERSION(3,0,0)
6742 {
6743 /* Add one pixel to the far right column in case a double-stroked
6744 * bold glyph may sit there. */
6745 const GdkRectangle rect = {
6746 FILL_X(col1), FILL_Y(row1),
6747 (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1),
6748 (row2 - row1 + 1) * gui.char_height
6749 };
6750 GdkWindow * const win = gtk_widget_get_window(gui.drawarea);
6751 cairo_t * const cr = cairo_create(gui.surface);
Bram Moolenaara859f042016-11-17 19:11:55 +01006752# if GTK_CHECK_VERSION(3,22,2)
6753 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
6754# else
Bram Moolenaar98921892016-02-23 17:14:37 +01006755 cairo_pattern_t * const pat = gdk_window_get_background_pattern(win);
6756 if (pat != NULL)
6757 cairo_set_source(cr, pat);
6758 else
Bram Moolenaar36edf062016-07-21 22:10:12 +02006759 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
Bram Moolenaara859f042016-11-17 19:11:55 +01006760# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01006761 gdk_cairo_rectangle(cr, &rect);
6762 cairo_fill(cr);
6763 cairo_destroy(cr);
6764
6765 if (!gui.by_signal)
6766 gdk_window_invalidate_rect(win, &rect, FALSE);
6767 }
6768#else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 gdk_gc_set_foreground(gui.text_gc, &color);
6770
6771 /* Clear one extra pixel at the far right, for when bold characters have
6772 * spilled over to the window border. */
6773 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE,
6774 FILL_X(col1), FILL_Y(row1),
6775 (col2 - col1 + 1) * gui.char_width
6776 + (col2 == Columns - 1),
6777 (row2 - row1 + 1) * gui.char_height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006778#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779}
6780
Bram Moolenaar98921892016-02-23 17:14:37 +01006781#if GTK_CHECK_VERSION(3,0,0)
6782 static void
6783gui_gtk_window_clear(GdkWindow *win)
6784{
6785 const GdkRectangle rect = {
6786 0, 0, gdk_window_get_width(win), gdk_window_get_height(win)
6787 };
6788 cairo_t * const cr = cairo_create(gui.surface);
Bram Moolenaara859f042016-11-17 19:11:55 +01006789# if GTK_CHECK_VERSION(3,22,2)
6790 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
6791# else
Bram Moolenaar98921892016-02-23 17:14:37 +01006792 cairo_pattern_t * const pat = gdk_window_get_background_pattern(win);
6793 if (pat != NULL)
6794 cairo_set_source(cr, pat);
6795 else
Bram Moolenaar36edf062016-07-21 22:10:12 +02006796 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
Bram Moolenaara859f042016-11-17 19:11:55 +01006797# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01006798 gdk_cairo_rectangle(cr, &rect);
6799 cairo_fill(cr);
6800 cairo_destroy(cr);
6801
6802 if (!gui.by_signal)
6803 gdk_window_invalidate_rect(win, &rect, FALSE);
6804}
6805#endif
6806
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 void
6808gui_mch_clear_all(void)
6809{
Bram Moolenaar98921892016-02-23 17:14:37 +01006810#if GTK_CHECK_VERSION(3,0,0)
6811 if (gtk_widget_get_window(gui.drawarea) != NULL)
6812 gui_gtk_window_clear(gtk_widget_get_window(gui.drawarea));
6813#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 if (gui.drawarea->window != NULL)
6815 gdk_window_clear(gui.drawarea->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01006816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817}
6818
Bram Moolenaar98921892016-02-23 17:14:37 +01006819#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820/*
6821 * Redraw any text revealed by scrolling up/down.
6822 */
6823 static void
6824check_copy_area(void)
6825{
6826 GdkEvent *event;
6827 int expose_count;
6828
6829 if (gui.visibility != GDK_VISIBILITY_PARTIAL)
6830 return;
6831
6832 /* Avoid redrawing the cursor while scrolling or it'll end up where
6833 * we don't want it to be. I'm not sure if it's correct to call
6834 * gui_dont_update_cursor() at this point but it works as a quick
6835 * fix for now. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02006836 gui_dont_update_cursor(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837
6838 do
6839 {
6840 /* Wait to check whether the scroll worked or not. */
6841 event = gdk_event_get_graphics_expose(gui.drawarea->window);
6842
6843 if (event == NULL)
6844 break; /* received NoExpose event */
6845
6846 gui_redraw(event->expose.area.x, event->expose.area.y,
6847 event->expose.area.width, event->expose.area.height);
6848
6849 expose_count = event->expose.count;
6850 gdk_event_free(event);
6851 }
6852 while (expose_count > 0); /* more events follow */
6853
6854 gui_can_update_cursor();
6855}
Bram Moolenaar98921892016-02-23 17:14:37 +01006856#endif /* !GTK_CHECK_VERSION(3,0,0) */
6857
6858#if GTK_CHECK_VERSION(3,0,0)
6859 static void
6860gui_gtk_surface_copy_rect(int dest_x, int dest_y,
6861 int src_x, int src_y,
6862 int width, int height)
6863{
6864 cairo_t * const cr = cairo_create(gui.surface);
6865
6866 cairo_rectangle(cr, dest_x, dest_y, width, height);
6867 cairo_clip(cr);
6868 cairo_push_group(cr);
6869 cairo_set_source_surface(cr, gui.surface, dest_x - src_x, dest_y - src_y);
6870 cairo_paint(cr);
6871 cairo_pop_group_to_source(cr);
6872 cairo_paint(cr);
6873
6874 cairo_destroy(cr);
6875}
6876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877
6878/*
6879 * Delete the given number of lines from the given row, scrolling up any
6880 * text further down within the scroll region.
6881 */
6882 void
6883gui_mch_delete_lines(int row, int num_lines)
6884{
Bram Moolenaar98921892016-02-23 17:14:37 +01006885#if GTK_CHECK_VERSION(3,0,0)
6886 const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1;
6887 const int nrows = gui.scroll_region_bot - row + 1;
6888 const int src_nrows = nrows - num_lines;
6889
6890 gui_gtk_surface_copy_rect(
6891 FILL_X(gui.scroll_region_left), FILL_Y(row),
6892 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6893 gui.char_width * ncols + 1, gui.char_height * src_nrows);
6894 gui_clear_block(
6895 gui.scroll_region_bot - num_lines + 1, gui.scroll_region_left,
6896 gui.scroll_region_bot, gui.scroll_region_right);
6897 gui_gtk3_redraw(
6898 FILL_X(gui.scroll_region_left), FILL_Y(row),
6899 gui.char_width * ncols + 1, gui.char_height * nrows);
6900 if (!gui.by_signal)
6901 gtk_widget_queue_draw_area(gui.drawarea,
6902 FILL_X(gui.scroll_region_left), FILL_Y(row),
6903 gui.char_width * ncols + 1, gui.char_height * nrows);
6904#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
6906 return; /* Can't see the window */
6907
6908 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6909 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6910
6911 /* copy one extra pixel, for when bold has spilled over */
6912 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6913 FILL_X(gui.scroll_region_left), FILL_Y(row),
6914 gui.drawarea->window,
6915 FILL_X(gui.scroll_region_left),
6916 FILL_Y(row + num_lines),
6917 gui.char_width * (gui.scroll_region_right
6918 - gui.scroll_region_left + 1) + 1,
6919 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6920
6921 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
6922 gui.scroll_region_left,
6923 gui.scroll_region_bot, gui.scroll_region_right);
6924 check_copy_area();
Bram Moolenaar98921892016-02-23 17:14:37 +01006925#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926}
6927
6928/*
6929 * Insert the given number of lines before the given row, scrolling down any
6930 * following text within the scroll region.
6931 */
6932 void
6933gui_mch_insert_lines(int row, int num_lines)
6934{
Bram Moolenaar98921892016-02-23 17:14:37 +01006935#if GTK_CHECK_VERSION(3,0,0)
6936 const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1;
6937 const int nrows = gui.scroll_region_bot - row + 1;
6938 const int src_nrows = nrows - num_lines;
6939
6940 gui_gtk_surface_copy_rect(
6941 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6942 FILL_X(gui.scroll_region_left), FILL_Y(row),
6943 gui.char_width * ncols + 1, gui.char_height * src_nrows);
6944 gui_mch_clear_block(
6945 row, gui.scroll_region_left,
6946 row + num_lines - 1, gui.scroll_region_right);
6947 gui_gtk3_redraw(
6948 FILL_X(gui.scroll_region_left), FILL_Y(row),
6949 gui.char_width * ncols + 1, gui.char_height * nrows);
6950 if (!gui.by_signal)
6951 gtk_widget_queue_draw_area(gui.drawarea,
6952 FILL_X(gui.scroll_region_left), FILL_Y(row),
6953 gui.char_width * ncols + 1, gui.char_height * nrows);
6954#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
6956 return; /* Can't see the window */
6957
6958 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6959 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6960
6961 /* copy one extra pixel, for when bold has spilled over */
6962 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6963 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6964 gui.drawarea->window,
6965 FILL_X(gui.scroll_region_left), FILL_Y(row),
6966 gui.char_width * (gui.scroll_region_right
6967 - gui.scroll_region_left + 1) + 1,
6968 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6969
6970 gui_clear_block(row, gui.scroll_region_left,
6971 row + num_lines - 1, gui.scroll_region_right);
6972 check_copy_area();
Bram Moolenaar98921892016-02-23 17:14:37 +01006973#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974}
6975
6976/*
6977 * X Selection stuff, for cutting and pasting text to other windows.
6978 */
6979 void
6980clip_mch_request_selection(VimClipboard *cbd)
6981{
6982 GdkAtom target;
6983 unsigned i;
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00006984 time_t start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985
6986 for (i = 0; i < N_SELECTION_TARGETS; ++i)
6987 {
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006988 if (!clip_html && selection_targets[i].info == TARGET_HTML)
6989 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 received_selection = RS_NONE;
6991 target = gdk_atom_intern(selection_targets[i].target, FALSE);
6992
6993 gtk_selection_convert(gui.drawarea,
6994 cbd->gtk_sel_atom, target,
6995 (guint32)GDK_CURRENT_TIME);
6996
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00006997 /* Hack: Wait up to three seconds for the selection. A hang was
6998 * noticed here when using the netrw plugin combined with ":gui"
6999 * during the FocusGained event. */
7000 start = time(NULL);
7001 while (received_selection == RS_NONE && time(NULL) < start + 3)
Bram Moolenaara3f41662010-07-11 19:01:06 +02007002 g_main_context_iteration(NULL, TRUE); /* wait for selection_received_cb */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003
7004 if (received_selection != RS_FAIL)
7005 return;
7006 }
7007
7008 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaar98921892016-02-23 17:14:37 +01007009#if GTK_CHECK_VERSION(3,0,0)
7010 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)),
7011 cbd);
7012#else
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00007013 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gui.mainwin->window), cbd);
Bram Moolenaar98921892016-02-23 17:14:37 +01007014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015}
7016
7017/*
7018 * Disown the selection.
7019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00007021clip_mch_lose_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022{
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01007023 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025}
7026
7027/*
7028 * Own the selection and return OK if it worked.
7029 */
7030 int
7031clip_mch_own_selection(VimClipboard *cbd)
7032{
7033 int success;
7034
7035 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom,
Bram Moolenaar20892c12011-06-26 04:49:00 +02007036 gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 gui_mch_update();
7038 return (success) ? OK : FAIL;
7039}
7040
7041/*
7042 * Send the current selection to the clipboard. Do nothing for X because we
7043 * will fill in the selection only when requested by another app.
7044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00007046clip_mch_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047{
7048}
7049
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01007050 int
7051clip_gtk_owner_exists(VimClipboard *cbd)
7052{
7053 return gdk_selection_owner_get(cbd->gtk_sel_atom) != NULL;
7054}
7055
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056
7057#if defined(FEAT_MENU) || defined(PROTO)
7058/*
7059 * Make a menu item appear either active or not active (grey or not grey).
7060 */
7061 void
7062gui_mch_menu_grey(vimmenu_T *menu, int grey)
7063{
7064 if (menu->id == NULL)
7065 return;
7066
7067 if (menu_is_separator(menu->name))
7068 grey = TRUE;
7069
7070 gui_mch_menu_hidden(menu, FALSE);
7071 /* Be clever about bitfields versus true booleans here! */
Bram Moolenaar98921892016-02-23 17:14:37 +01007072# if GTK_CHECK_VERSION(3,0,0)
7073 if (!gtk_widget_get_sensitive(menu->id) == !grey)
7074# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 if (!GTK_WIDGET_SENSITIVE(menu->id) == !grey)
Bram Moolenaar98921892016-02-23 17:14:37 +01007076# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 {
7078 gtk_widget_set_sensitive(menu->id, !grey);
7079 gui_mch_update();
7080 }
7081}
7082
7083/*
7084 * Make menu item hidden or not hidden.
7085 */
7086 void
7087gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
7088{
7089 if (menu->id == 0)
7090 return;
7091
7092 if (hidden)
7093 {
Bram Moolenaar98921892016-02-23 17:14:37 +01007094# if GTK_CHECK_VERSION(3,0,0)
7095 if (gtk_widget_get_visible(menu->id))
7096# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 if (GTK_WIDGET_VISIBLE(menu->id))
Bram Moolenaar98921892016-02-23 17:14:37 +01007098# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 {
7100 gtk_widget_hide(menu->id);
7101 gui_mch_update();
7102 }
7103 }
7104 else
7105 {
Bram Moolenaar98921892016-02-23 17:14:37 +01007106# if GTK_CHECK_VERSION(3,0,0)
7107 if (!gtk_widget_get_visible(menu->id))
7108# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 if (!GTK_WIDGET_VISIBLE(menu->id))
Bram Moolenaar98921892016-02-23 17:14:37 +01007110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 {
7112 gtk_widget_show(menu->id);
7113 gui_mch_update();
7114 }
7115 }
7116}
7117
7118/*
7119 * This is called after setting all the menus to grey/hidden or not.
7120 */
7121 void
7122gui_mch_draw_menubar(void)
7123{
7124 /* just make sure that the visual changes get effect immediately */
7125 gui_mch_update();
7126}
7127#endif /* FEAT_MENU */
7128
7129/*
7130 * Scrollbar stuff.
7131 */
7132 void
7133gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
7134{
7135 if (sb->id == NULL)
7136 return;
7137
Bram Moolenaar98921892016-02-23 17:14:37 +01007138#if GTK_CHECK_VERSION(3,0,0)
7139 gtk_widget_set_visible(sb->id, flag);
7140#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 if (flag)
7142 gtk_widget_show(sb->id);
7143 else
7144 gtk_widget_hide(sb->id);
Bram Moolenaar98921892016-02-23 17:14:37 +01007145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00007147 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148}
7149
7150
7151/*
7152 * Return the RGB value of a pixel as long.
7153 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007154 guicolor_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155gui_mch_get_rgb(guicolor_T pixel)
7156{
Bram Moolenaar98921892016-02-23 17:14:37 +01007157#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02007158 return (long_u)pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01007159#else
Bram Moolenaar36edf062016-07-21 22:10:12 +02007160 GdkColor color;
7161
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
7163 (unsigned long)pixel, &color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007165 return (guicolor_T)(
7166 (((unsigned)color.red & 0xff00) << 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 | ((unsigned)color.green & 0xff00)
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007168 | (((unsigned)color.blue & 0xff00) >> 8));
Bram Moolenaar36edf062016-07-21 22:10:12 +02007169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170}
7171
7172/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007173 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007175 void
7176gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177{
Bram Moolenaar98921892016-02-23 17:14:37 +01007178#if GTK_CHECK_VERSION(3,0,0)
7179 gui_gtk_get_pointer(gui.drawarea, x, y, NULL);
7180#else
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007181 gdk_window_get_pointer(gui.drawarea->window, x, y, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01007182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183}
7184
7185 void
7186gui_mch_setmouse(int x, int y)
7187{
7188 /* Sorry for the Xlib call, but we can't avoid it, since there is no
7189 * internal GDK mechanism present to accomplish this. (and for good
7190 * reason...) */
Bram Moolenaar98921892016-02-23 17:14:37 +01007191#if GTK_CHECK_VERSION(3,0,0)
7192 XWarpPointer(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.drawarea)),
7193 (Window)0, GDK_WINDOW_XID(gtk_widget_get_window(gui.drawarea)),
7194 0, 0, 0U, 0U, x, y);
7195#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 XWarpPointer(GDK_WINDOW_XDISPLAY(gui.drawarea->window),
7197 (Window)0, GDK_WINDOW_XWINDOW(gui.drawarea->window),
7198 0, 0, 0U, 0U, x, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01007199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200}
7201
7202
7203#ifdef FEAT_MOUSESHAPE
7204/* The last set mouse pointer shape is remembered, to be used when it goes
7205 * from hidden to not hidden. */
7206static int last_shape = 0;
7207#endif
7208
7209/*
7210 * Use the blank mouse pointer or not.
7211 *
7212 * hide: TRUE = use blank ptr, FALSE = use parent ptr
7213 */
7214 void
7215gui_mch_mousehide(int hide)
7216{
7217 if (gui.pointer_hidden != hide)
7218 {
7219 gui.pointer_hidden = hide;
Bram Moolenaar98921892016-02-23 17:14:37 +01007220#if GTK_CHECK_VERSION(3,0,0)
7221 if (gtk_widget_get_window(gui.drawarea) && gui.blank_pointer != NULL)
7222#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 if (gui.drawarea->window && gui.blank_pointer != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01007224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 {
7226 if (hide)
Bram Moolenaar98921892016-02-23 17:14:37 +01007227#if GTK_CHECK_VERSION(3,0,0)
7228 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
7229 gui.blank_pointer);
7230#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01007232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 else
7234#ifdef FEAT_MOUSESHAPE
7235 mch_set_mouse_shape(last_shape);
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01007236#elif GTK_CHECK_VERSION(3,0,0)
7237 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238#else
7239 gdk_window_set_cursor(gui.drawarea->window, NULL);
7240#endif
7241 }
7242 }
7243}
7244
7245#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
7246
7247/* Table for shape IDs. Keep in sync with the mshape_names[] table in
7248 * misc2.c! */
7249static const int mshape_ids[] =
7250{
7251 GDK_LEFT_PTR, /* arrow */
7252 GDK_CURSOR_IS_PIXMAP, /* blank */
7253 GDK_XTERM, /* beam */
7254 GDK_SB_V_DOUBLE_ARROW, /* updown */
7255 GDK_SIZING, /* udsizing */
7256 GDK_SB_H_DOUBLE_ARROW, /* leftright */
7257 GDK_SIZING, /* lrsizing */
7258 GDK_WATCH, /* busy */
7259 GDK_X_CURSOR, /* no */
7260 GDK_CROSSHAIR, /* crosshair */
7261 GDK_HAND1, /* hand1 */
7262 GDK_HAND2, /* hand2 */
7263 GDK_PENCIL, /* pencil */
7264 GDK_QUESTION_ARROW, /* question */
7265 GDK_RIGHT_PTR, /* right-arrow */
7266 GDK_CENTER_PTR, /* up-arrow */
7267 GDK_LEFT_PTR /* last one */
7268};
7269
7270 void
7271mch_set_mouse_shape(int shape)
7272{
7273 int id;
7274 GdkCursor *c;
7275
Bram Moolenaar98921892016-02-23 17:14:37 +01007276# if GTK_CHECK_VERSION(3,0,0)
7277 if (gtk_widget_get_window(gui.drawarea) == NULL)
7278# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 if (gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01007280# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 return;
7282
7283 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
Bram Moolenaar98921892016-02-23 17:14:37 +01007284# if GTK_CHECK_VERSION(3,0,0)
7285 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
7286 gui.blank_pointer);
7287# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01007289# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 else
7291 {
7292 if (shape >= MSHAPE_NUMBERED)
7293 {
7294 id = shape - MSHAPE_NUMBERED;
7295 if (id >= GDK_LAST_CURSOR)
7296 id = GDK_LEFT_PTR;
7297 else
7298 id &= ~1; /* they are always even (why?) */
7299 }
Bram Moolenaarb85cb212009-05-17 14:24:23 +00007300 else if (shape < (int)(sizeof(mshape_ids) / sizeof(int)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 id = mshape_ids[shape];
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007302 else
7303 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304# ifdef HAVE_GTK_MULTIHEAD
7305 c = gdk_cursor_new_for_display(
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00007306 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307# else
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00007308 c = gdk_cursor_new((GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01007310# if GTK_CHECK_VERSION(3,0,0)
7311 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), c);
7312# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 gdk_window_set_cursor(gui.drawarea->window, c);
Bram Moolenaar98921892016-02-23 17:14:37 +01007314# endif
7315# if GTK_CHECK_VERSION(3,0,0)
7316 g_object_unref(G_OBJECT(c));
7317# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 gdk_cursor_destroy(c); /* Unref, actually. Bloody GTK+ 1. */
Bram Moolenaar98921892016-02-23 17:14:37 +01007319# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 }
7321 if (shape != MSHAPE_HIDE)
7322 last_shape = shape;
7323}
7324#endif /* FEAT_MOUSESHAPE */
7325
7326
7327#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
7328/*
7329 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
7330 * scaled down if the current font is not big enough, or scaled up if the image
7331 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
7332 * will be cut off if the current font is not big enough, or centered if it's
7333 * too small.
7334 */
7335# define SIGN_WIDTH (2 * gui.char_width)
7336# define SIGN_HEIGHT (gui.char_height)
7337# define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
7338
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 void
7340gui_mch_drawsign(int row, int col, int typenr)
7341{
7342 GdkPixbuf *sign;
7343
7344 sign = (GdkPixbuf *)sign_get_image(typenr);
7345
Bram Moolenaar98921892016-02-23 17:14:37 +01007346# if GTK_CHECK_VERSION(3,0,0)
7347 if (sign != NULL && gui.drawarea != NULL
7348 && gtk_widget_get_window(gui.drawarea) != NULL)
7349# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 if (sign != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01007351# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 {
7353 int width;
7354 int height;
7355 int xoffset;
7356 int yoffset;
7357 int need_scale;
7358
7359 width = gdk_pixbuf_get_width(sign);
7360 height = gdk_pixbuf_get_height(sign);
7361 /*
7362 * Decide whether we need to scale. Allow one pixel of border
7363 * width to be cut off, in order to avoid excessive scaling for
7364 * tiny differences in font size.
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007365 * Do scale to fit the height to avoid gaps because of linespacing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 */
7367 need_scale = (width > SIGN_WIDTH + 2
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007368 || height != SIGN_HEIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 || (width < 3 * SIGN_WIDTH / 4
7370 && height < 3 * SIGN_HEIGHT / 4));
7371 if (need_scale)
7372 {
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007373 double aspect;
7374 int w = width;
7375 int h = height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376
7377 /* Keep the original aspect ratio */
7378 aspect = (double)height / (double)width;
7379 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect;
7380 width = MIN(width, SIGN_WIDTH);
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007381 if (((double)(MAX(height, SIGN_HEIGHT)) /
7382 (double)(MIN(height, SIGN_HEIGHT))) < 1.15)
7383 {
7384 /* Change the aspect ratio by at most 15% to fill the
7385 * available space completly. */
7386 height = (double)SIGN_HEIGHT * SIGN_ASPECT / aspect;
7387 height = MIN(height, SIGN_HEIGHT);
7388 }
7389 else
7390 height = (double)width * aspect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007392 if (w == width && h == height)
7393 {
7394 /* no change in dimensions; don't decrease reference counter
7395 * (below) */
7396 need_scale = FALSE;
7397 }
7398 else
7399 {
7400 /* This doesn't seem to be worth caching, and doing so would
7401 * complicate the code quite a bit. */
7402 sign = gdk_pixbuf_scale_simple(sign, width, height,
7403 GDK_INTERP_BILINEAR);
7404 if (sign == NULL)
7405 return; /* out of memory */
7406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 }
7408
7409 /* The origin is the upper-left corner of the pixmap. Therefore
7410 * these offset may become negative if the pixmap is smaller than
7411 * the 2x1 cells reserved for the sign icon. */
7412 xoffset = (width - SIGN_WIDTH) / 2;
7413 yoffset = (height - SIGN_HEIGHT) / 2;
7414
Bram Moolenaar98921892016-02-23 17:14:37 +01007415# if GTK_CHECK_VERSION(3,0,0)
7416 {
7417 cairo_t *cr;
7418 cairo_surface_t *bg_surf;
7419 cairo_t *bg_cr;
7420 cairo_surface_t *sign_surf;
7421 cairo_t *sign_cr;
7422
7423 cr = cairo_create(gui.surface);
7424
7425 bg_surf = cairo_surface_create_similar(gui.surface,
7426 cairo_surface_get_content(gui.surface),
7427 SIGN_WIDTH, SIGN_HEIGHT);
7428 bg_cr = cairo_create(bg_surf);
Bram Moolenaar36edf062016-07-21 22:10:12 +02007429 cairo_set_source_rgba(bg_cr,
7430 gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue,
7431 gui.bgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01007432 cairo_paint(bg_cr);
7433
7434 sign_surf = cairo_surface_create_similar(gui.surface,
7435 cairo_surface_get_content(gui.surface),
7436 SIGN_WIDTH, SIGN_HEIGHT);
7437 sign_cr = cairo_create(sign_surf);
7438 gdk_cairo_set_source_pixbuf(sign_cr, sign, -xoffset, -yoffset);
7439 cairo_paint(sign_cr);
7440
7441 cairo_set_operator(sign_cr, CAIRO_OPERATOR_DEST_OVER);
7442 cairo_set_source_surface(sign_cr, bg_surf, 0, 0);
7443 cairo_paint(sign_cr);
7444
7445 cairo_set_source_surface(cr, sign_surf, FILL_X(col), FILL_Y(row));
7446 cairo_paint(cr);
7447
7448 cairo_destroy(sign_cr);
7449 cairo_surface_destroy(sign_surf);
7450 cairo_destroy(bg_cr);
7451 cairo_surface_destroy(bg_surf);
7452 cairo_destroy(cr);
7453
7454 if (!gui.by_signal)
7455 gtk_widget_queue_draw_area(gui.drawarea,
7456 FILL_X(col), FILL_Y(col), width, height);
7457
7458 }
7459# else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
7461
7462 gdk_draw_rectangle(gui.drawarea->window,
7463 gui.text_gc,
7464 TRUE,
7465 FILL_X(col),
7466 FILL_Y(row),
7467 SIGN_WIDTH,
7468 SIGN_HEIGHT);
7469
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 gdk_pixbuf_render_to_drawable_alpha(sign,
7471 gui.drawarea->window,
7472 MAX(0, xoffset),
7473 MAX(0, yoffset),
7474 FILL_X(col) - MIN(0, xoffset),
7475 FILL_Y(row) - MIN(0, yoffset),
7476 MIN(width, SIGN_WIDTH),
7477 MIN(height, SIGN_HEIGHT),
7478 GDK_PIXBUF_ALPHA_BILEVEL,
7479 127,
7480 GDK_RGB_DITHER_NORMAL,
7481 0, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01007482# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 if (need_scale)
7484 g_object_unref(sign);
7485 }
7486}
7487
7488 void *
7489gui_mch_register_sign(char_u *signfile)
7490{
7491 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use)
7492 {
7493 GdkPixbuf *sign;
7494 GError *error = NULL;
7495 char_u *message;
7496
7497 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error);
7498
7499 if (error == NULL)
7500 return sign;
7501
7502 message = (char_u *)error->message;
7503
7504 if (message != NULL && input_conv.vc_type != CONV_NONE)
7505 message = string_convert(&input_conv, message, NULL);
7506
7507 if (message != NULL)
7508 {
7509 /* The error message is already translated and will be more
7510 * descriptive than anything we could possibly do ourselves. */
7511 EMSG2("E255: %s", message);
7512
7513 if (input_conv.vc_type != CONV_NONE)
7514 vim_free(message);
7515 }
7516 g_error_free(error);
7517 }
7518
7519 return NULL;
7520}
7521
7522 void
7523gui_mch_destroy_sign(void *sign)
7524{
7525 if (sign != NULL)
7526 g_object_unref(sign);
7527}
7528
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529#endif /* FEAT_SIGN_ICONS */