blob: c00b3d6c49525fb791b38627fc0c995dc5568b59 [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
38# ifdef N_
39# undef N_
40# endif
41# ifdef textdomain
42# undef textdomain
43# endif
44# ifdef bindtextdomain
45# undef bindtextdomain
46# endif
Bram Moolenaara2dd9002007-05-14 17:38:30 +000047# ifdef bind_textdomain_codeset
48# undef bind_textdomain_codeset
Bram Moolenaar49325942007-05-10 19:19:59 +000049# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000050# if defined(FEAT_GETTEXT) && !defined(ENABLE_NLS)
51# define ENABLE_NLS /* so the texts in the dialog boxes are translated */
52# endif
53# include <gnome.h>
54# include "version.h"
Bram Moolenaardb552d602006-03-23 22:59:57 +000055/* missing prototype in bonobo-dock-item.h */
56extern void bonobo_dock_item_set_behavior(BonoboDockItem *dock_item, BonoboDockItemBehavior beh);
Bram Moolenaar071d4272004-06-13 20:20:40 +000057#endif
58
59#if !defined(FEAT_GUI_GTK) && defined(PROTO)
60/* When generating prototypes we don't want syntax errors. */
61# define GdkAtom int
62# define GdkEventExpose int
63# define GdkEventFocus int
64# define GdkEventVisibility int
65# define GdkEventProperty int
66# define GtkContainer int
67# define GtkTargetEntry int
68# define GtkType int
69# define GtkWidget int
70# define gint int
71# define gpointer int
72# define guint int
73# define GdkEventKey int
74# define GdkEventSelection int
75# define GtkSelectionData int
76# define GdkEventMotion int
77# define GdkEventButton int
78# define GdkDragContext int
79# define GdkEventConfigure int
80# define GdkEventClient int
81#else
Bram Moolenaar98921892016-02-23 17:14:37 +010082# if GTK_CHECK_VERSION(3,0,0)
83# include <gdk/gdkkeysyms-compat.h>
84# include <gtk/gtkx.h>
85# else
86# include <gdk/gdkkeysyms.h>
87# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000088# include <gdk/gdk.h>
89# ifdef WIN3264
90# include <gdk/gdkwin32.h>
91# else
92# include <gdk/gdkx.h>
93# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000094# include <gtk/gtk.h>
95# include "gui_gtk_f.h"
96#endif
97
98#ifdef HAVE_X11_SUNKEYSYM_H
99# include <X11/Sunkeysym.h>
100#endif
101
102/*
103 * Easy-to-use macro for multihead support.
104 */
105#ifdef HAVE_GTK_MULTIHEAD
106# define GET_X_ATOM(atom) gdk_x11_atom_to_xatom_for_display( \
107 gtk_widget_get_display(gui.mainwin), atom)
108#else
109# define GET_X_ATOM(atom) ((Atom)(atom))
110#endif
111
112/* Selection type distinguishers */
113enum
114{
115 TARGET_TYPE_NONE,
116 TARGET_UTF8_STRING,
117 TARGET_STRING,
118 TARGET_COMPOUND_TEXT,
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000119 TARGET_HTML,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 TARGET_TEXT,
121 TARGET_TEXT_URI_LIST,
122 TARGET_TEXT_PLAIN,
123 TARGET_VIM,
124 TARGET_VIMENC
125};
126
127/*
128 * Table of selection targets supported by Vim.
129 * Note: Order matters, preferred types should come first.
130 */
131static const GtkTargetEntry selection_targets[] =
132{
133 {VIMENC_ATOM_NAME, 0, TARGET_VIMENC},
134 {VIM_ATOM_NAME, 0, TARGET_VIM},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000135 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 {"COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT},
138 {"TEXT", 0, TARGET_TEXT},
139 {"STRING", 0, TARGET_STRING}
140};
141#define N_SELECTION_TARGETS (sizeof(selection_targets) / sizeof(selection_targets[0]))
142
143#ifdef FEAT_DND
144/*
145 * Table of DnD targets supported by Vim.
146 * Note: Order matters, preferred types should come first.
147 */
148static const GtkTargetEntry dnd_targets[] =
149{
150 {"text/uri-list", 0, TARGET_TEXT_URI_LIST},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000151 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 {"STRING", 0, TARGET_STRING},
154 {"text/plain", 0, TARGET_TEXT_PLAIN}
155};
156# define N_DND_TARGETS (sizeof(dnd_targets) / sizeof(dnd_targets[0]))
157#endif
158
159
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160/*
161 * "Monospace" is a standard font alias that should be present
162 * on all proper Pango/fontconfig installations.
163 */
164# define DEFAULT_FONT "Monospace 10"
165
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
167/*
168 * Atoms used to communicate save-yourself from the X11 session manager. There
169 * is no need to move them into the GUI struct, since they should be constant.
170 */
171static GdkAtom wm_protocols_atom = GDK_NONE;
172static GdkAtom save_yourself_atom = GDK_NONE;
173#endif
174
175/*
176 * Atoms used to control/reference X11 selections.
177 */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000178static GdkAtom html_atom = GDK_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179static GdkAtom utf8_string_atom = GDK_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180static GdkAtom vim_atom = GDK_NONE; /* Vim's own special selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181static GdkAtom vimenc_atom = GDK_NONE; /* Vim's extended selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182
183/*
184 * Keycodes recognized by vim.
185 * NOTE: when changing this, the table in gui_x11.c probably needs the same
186 * change!
187 */
188static struct special_key
189{
190 guint key_sym;
191 char_u code0;
192 char_u code1;
193}
194const special_keys[] =
195{
196 {GDK_Up, 'k', 'u'},
197 {GDK_Down, 'k', 'd'},
198 {GDK_Left, 'k', 'l'},
199 {GDK_Right, 'k', 'r'},
200 {GDK_F1, 'k', '1'},
201 {GDK_F2, 'k', '2'},
202 {GDK_F3, 'k', '3'},
203 {GDK_F4, 'k', '4'},
204 {GDK_F5, 'k', '5'},
205 {GDK_F6, 'k', '6'},
206 {GDK_F7, 'k', '7'},
207 {GDK_F8, 'k', '8'},
208 {GDK_F9, 'k', '9'},
209 {GDK_F10, 'k', ';'},
210 {GDK_F11, 'F', '1'},
211 {GDK_F12, 'F', '2'},
212 {GDK_F13, 'F', '3'},
213 {GDK_F14, 'F', '4'},
214 {GDK_F15, 'F', '5'},
215 {GDK_F16, 'F', '6'},
216 {GDK_F17, 'F', '7'},
217 {GDK_F18, 'F', '8'},
218 {GDK_F19, 'F', '9'},
219 {GDK_F20, 'F', 'A'},
220 {GDK_F21, 'F', 'B'},
221 {GDK_Pause, 'F', 'B'}, /* Pause == F21 according to netbeans.txt */
222 {GDK_F22, 'F', 'C'},
223 {GDK_F23, 'F', 'D'},
224 {GDK_F24, 'F', 'E'},
225 {GDK_F25, 'F', 'F'},
226 {GDK_F26, 'F', 'G'},
227 {GDK_F27, 'F', 'H'},
228 {GDK_F28, 'F', 'I'},
229 {GDK_F29, 'F', 'J'},
230 {GDK_F30, 'F', 'K'},
231 {GDK_F31, 'F', 'L'},
232 {GDK_F32, 'F', 'M'},
233 {GDK_F33, 'F', 'N'},
234 {GDK_F34, 'F', 'O'},
235 {GDK_F35, 'F', 'P'},
236#ifdef SunXK_F36
237 {SunXK_F36, 'F', 'Q'},
238 {SunXK_F37, 'F', 'R'},
239#endif
240 {GDK_Help, '%', '1'},
241 {GDK_Undo, '&', '8'},
242 {GDK_BackSpace, 'k', 'b'},
243 {GDK_Insert, 'k', 'I'},
244 {GDK_Delete, 'k', 'D'},
245 {GDK_3270_BackTab, 'k', 'B'},
246 {GDK_Clear, 'k', 'C'},
247 {GDK_Home, 'k', 'h'},
248 {GDK_End, '@', '7'},
249 {GDK_Prior, 'k', 'P'},
250 {GDK_Next, 'k', 'N'},
251 {GDK_Print, '%', '9'},
252 /* Keypad keys: */
253 {GDK_KP_Left, 'k', 'l'},
254 {GDK_KP_Right, 'k', 'r'},
255 {GDK_KP_Up, 'k', 'u'},
256 {GDK_KP_Down, 'k', 'd'},
257 {GDK_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
258 {GDK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
259 {GDK_KP_Home, 'K', '1'},
260 {GDK_KP_End, 'K', '4'},
261 {GDK_KP_Prior, 'K', '3'}, /* page up */
262 {GDK_KP_Next, 'K', '5'}, /* page down */
263
264 {GDK_KP_Add, 'K', '6'},
265 {GDK_KP_Subtract, 'K', '7'},
266 {GDK_KP_Divide, 'K', '8'},
267 {GDK_KP_Multiply, 'K', '9'},
268 {GDK_KP_Enter, 'K', 'A'},
269 {GDK_KP_Decimal, 'K', 'B'},
270
271 {GDK_KP_0, 'K', 'C'},
272 {GDK_KP_1, 'K', 'D'},
273 {GDK_KP_2, 'K', 'E'},
274 {GDK_KP_3, 'K', 'F'},
275 {GDK_KP_4, 'K', 'G'},
276 {GDK_KP_5, 'K', 'H'},
277 {GDK_KP_6, 'K', 'I'},
278 {GDK_KP_7, 'K', 'J'},
279 {GDK_KP_8, 'K', 'K'},
280 {GDK_KP_9, 'K', 'L'},
281
282 /* End of list marker: */
283 {0, 0, 0}
284};
285
286/*
287 * Flags for command line options table below.
288 */
289#define ARG_FONT 1
290#define ARG_GEOMETRY 2
291#define ARG_REVERSE 3
292#define ARG_NOREVERSE 4
293#define ARG_BACKGROUND 5
294#define ARG_FOREGROUND 6
295#define ARG_ICONIC 7
296#define ARG_ROLE 8
297#define ARG_NETBEANS 9
298#define ARG_XRM 10 /* ignored */
299#define ARG_MENUFONT 11 /* ignored */
300#define ARG_INDEX_MASK 0x00ff
301#define ARG_HAS_VALUE 0x0100 /* a value is expected after the argument */
302#define ARG_NEEDS_GUI 0x0200 /* need to initialize the GUI for this */
303#define ARG_FOR_GTK 0x0400 /* argument is handled by GTK+ or GNOME */
304#define ARG_COMPAT_LONG 0x0800 /* accept -foo but substitute with --foo */
305#define ARG_KEEP 0x1000 /* don't remove argument from argv[] */
306
307/*
308 * This table holds all the X GUI command line options allowed. This includes
309 * the standard ones so that we can skip them when Vim is started without the
310 * GUI (but the GUI might start up later).
311 *
312 * When changing this, also update doc/gui_x11.txt and the usage message!!!
313 */
314typedef struct
315{
316 const char *name;
317 unsigned int flags;
318}
319cmdline_option_T;
320
321static const cmdline_option_T cmdline_options[] =
322{
323 /* We handle these options ourselves */
324 {"-fn", ARG_FONT|ARG_HAS_VALUE},
325 {"-font", ARG_FONT|ARG_HAS_VALUE},
326 {"-geom", ARG_GEOMETRY|ARG_HAS_VALUE},
327 {"-geometry", ARG_GEOMETRY|ARG_HAS_VALUE},
328 {"-rv", ARG_REVERSE},
329 {"-reverse", ARG_REVERSE},
330 {"+rv", ARG_NOREVERSE},
331 {"+reverse", ARG_NOREVERSE},
332 {"-bg", ARG_BACKGROUND|ARG_HAS_VALUE},
333 {"-background", ARG_BACKGROUND|ARG_HAS_VALUE},
334 {"-fg", ARG_FOREGROUND|ARG_HAS_VALUE},
335 {"-foreground", ARG_FOREGROUND|ARG_HAS_VALUE},
336 {"-iconic", ARG_ICONIC},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 {"--role", ARG_ROLE|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338#ifdef FEAT_NETBEANS_INTG
339 {"-nb", ARG_NETBEANS}, /* non-standard value format */
340 {"-xrm", ARG_XRM|ARG_HAS_VALUE}, /* not implemented */
341 {"-mf", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
342 {"-menufont", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 /* Arguments handled by GTK (and GNOME) internally. */
345 {"--g-fatal-warnings", ARG_FOR_GTK},
346 {"--gdk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
347 {"--gdk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
348 {"--gtk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
349 {"--gtk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
350 {"--gtk-module", ARG_FOR_GTK|ARG_HAS_VALUE},
351 {"--sync", ARG_FOR_GTK},
352 {"--display", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
353 {"--name", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
354 {"--class", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 {"--screen", ARG_FOR_GTK|ARG_HAS_VALUE},
356 {"--gxid-host", ARG_FOR_GTK|ARG_HAS_VALUE},
357 {"--gxid-port", ARG_FOR_GTK|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifdef FEAT_GUI_GNOME
359 {"--load-modules", ARG_FOR_GTK|ARG_HAS_VALUE},
360 {"--sm-client-id", ARG_FOR_GTK|ARG_HAS_VALUE},
361 {"--sm-config-prefix", ARG_FOR_GTK|ARG_HAS_VALUE},
362 {"--sm-disable", ARG_FOR_GTK},
363 {"--oaf-ior-fd", ARG_FOR_GTK|ARG_HAS_VALUE},
364 {"--oaf-activate-iid", ARG_FOR_GTK|ARG_HAS_VALUE},
365 {"--oaf-private", ARG_FOR_GTK},
366 {"--enable-sound", ARG_FOR_GTK},
367 {"--disable-sound", ARG_FOR_GTK},
368 {"--espeaker", ARG_FOR_GTK|ARG_HAS_VALUE},
369 {"-?", ARG_FOR_GTK|ARG_NEEDS_GUI},
370 {"--help", ARG_FOR_GTK|ARG_NEEDS_GUI|ARG_KEEP},
371 {"--usage", ARG_FOR_GTK|ARG_NEEDS_GUI},
372# if 0 /* conflicts with Vim's own --version argument */
373 {"--version", ARG_FOR_GTK|ARG_NEEDS_GUI},
374# endif
375 {"--disable-crash-dialog", ARG_FOR_GTK},
376#endif
377 {NULL, 0}
378};
379
380static int gui_argc = 0;
381static char **gui_argv = NULL;
382
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383static const char *role_argument = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
385static const char *restart_command = NULL;
Bram Moolenaar9085f802009-06-03 14:20:21 +0000386static char *abs_restart_command = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387#endif
388static int found_iconic_arg = FALSE;
389
390#ifdef FEAT_GUI_GNOME
391/*
392 * Can't use Gnome if --socketid given
393 */
394static int using_gnome = 0;
395#else
396# define using_gnome 0
397#endif
398
399/*
400 * Parse the GUI related command-line arguments. Any arguments used are
401 * deleted from argv, and *argc is decremented accordingly. This is called
402 * when vim is started, whether or not the GUI has been started.
403 */
404 void
405gui_mch_prepare(int *argc, char **argv)
406{
407 const cmdline_option_T *option;
408 int i = 0;
409 int len = 0;
410
411#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
412 /*
413 * Determine the command used to invoke Vim, to be passed as restart
414 * command to the session manager. If argv[0] contains any directory
415 * components try building an absolute path, otherwise leave it as is.
416 */
417 restart_command = argv[0];
418
419 if (strchr(argv[0], G_DIR_SEPARATOR) != NULL)
420 {
421 char_u buf[MAXPATHL];
422
423 if (mch_FullName((char_u *)argv[0], buf, (int)sizeof(buf), TRUE) == OK)
Bram Moolenaar9085f802009-06-03 14:20:21 +0000424 {
425 abs_restart_command = (char *)vim_strsave(buf);
426 restart_command = abs_restart_command;
427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 }
429#endif
430
431 /*
432 * Move all the entries in argv which are relevant to GTK+ and GNOME
433 * into gui_argv. Freed later in gui_mch_init().
434 */
435 gui_argc = 0;
436 gui_argv = (char **)alloc((unsigned)((*argc + 1) * sizeof(char *)));
437
438 g_return_if_fail(gui_argv != NULL);
439
440 gui_argv[gui_argc++] = argv[i++];
441
442 while (i < *argc)
443 {
444 /* Don't waste CPU cycles on non-option arguments. */
445 if (argv[i][0] != '-' && argv[i][0] != '+')
446 {
447 ++i;
448 continue;
449 }
450
451 /* Look for argv[i] in cmdline_options[] table. */
452 for (option = &cmdline_options[0]; option->name != NULL; ++option)
453 {
454 len = strlen(option->name);
455
456 if (strncmp(argv[i], option->name, len) == 0)
457 {
458 if (argv[i][len] == '\0')
459 break;
460 /* allow --foo=bar style */
461 if (argv[i][len] == '=' && (option->flags & ARG_HAS_VALUE))
462 break;
463#ifdef FEAT_NETBEANS_INTG
464 /* darn, -nb has non-standard syntax */
465 if (vim_strchr((char_u *)":=", argv[i][len]) != NULL
466 && (option->flags & ARG_INDEX_MASK) == ARG_NETBEANS)
467 break;
468#endif
469 }
470 else if ((option->flags & ARG_COMPAT_LONG)
471 && strcmp(argv[i], option->name + 1) == 0)
472 {
473 /* Replace the standard X arguments "-name" and "-display"
474 * with their GNU-style long option counterparts. */
475 argv[i] = (char *)option->name;
476 break;
477 }
478 }
479 if (option->name == NULL) /* no match */
480 {
481 ++i;
482 continue;
483 }
484
485 if (option->flags & ARG_FOR_GTK)
486 {
487 /* Move the argument into gui_argv, which
488 * will later be passed to gtk_init_check() */
489 gui_argv[gui_argc++] = argv[i];
490 }
491 else
492 {
493 char *value = NULL;
494
495 /* Extract the option's value if there is one.
496 * Accept both "--foo bar" and "--foo=bar" style. */
497 if (option->flags & ARG_HAS_VALUE)
498 {
499 if (argv[i][len] == '=')
500 value = &argv[i][len + 1];
501 else if (i + 1 < *argc && strcmp(argv[i + 1], "--") != 0)
502 value = argv[i + 1];
503 }
504
505 /* Check for options handled by Vim itself */
506 switch (option->flags & ARG_INDEX_MASK)
507 {
508 case ARG_REVERSE:
509 found_reverse_arg = TRUE;
510 break;
511 case ARG_NOREVERSE:
512 found_reverse_arg = FALSE;
513 break;
514 case ARG_FONT:
515 font_argument = value;
516 break;
517 case ARG_GEOMETRY:
518 if (value != NULL)
519 gui.geom = vim_strsave((char_u *)value);
520 break;
521 case ARG_BACKGROUND:
522 background_argument = value;
523 break;
524 case ARG_FOREGROUND:
525 foreground_argument = value;
526 break;
527 case ARG_ICONIC:
528 found_iconic_arg = TRUE;
529 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 case ARG_ROLE:
531 role_argument = value; /* used later in gui_mch_open() */
532 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533#ifdef FEAT_NETBEANS_INTG
534 case ARG_NETBEANS:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 gui.dofork = FALSE; /* don't fork() when starting GUI */
536 netbeansArg = argv[i];
537 break;
538#endif
539 default:
540 break;
541 }
542 }
543
544 /* These arguments make gnome_program_init() print a message and exit.
Bram Moolenaar717e1962016-08-10 21:28:44 +0200545 * Must start the GUI for this, otherwise ":gui" will exit later!
546 * Only when the GUI can start. */
547 if ((option->flags & ARG_NEEDS_GUI)
548 && gui_mch_early_init_check(FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 gui.starting = TRUE;
550
551 if (option->flags & ARG_KEEP)
552 ++i;
553 else
554 {
555 /* Remove the flag from the argument vector. */
556 if (--*argc > i)
557 {
558 int n_strip = 1;
559
560 /* Move the argument's value as well, if there is one. */
561 if ((option->flags & ARG_HAS_VALUE)
562 && argv[i][len] != '='
563 && strcmp(argv[i + 1], "--") != 0)
564 {
565 ++n_strip;
566 --*argc;
567 if (option->flags & ARG_FOR_GTK)
568 gui_argv[gui_argc++] = argv[i + 1];
569 }
570
571 if (*argc > i)
572 mch_memmove(&argv[i], &argv[i + n_strip],
573 (*argc - i) * sizeof(char *));
574 }
575 argv[*argc] = NULL;
576 }
577 }
578
579 gui_argv[gui_argc] = NULL;
580}
581
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000582#if defined(EXITFREE) || defined(PROTO)
583 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100584gui_mch_free_all(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000585{
586 vim_free(gui_argv);
Bram Moolenaar9085f802009-06-03 14:20:21 +0000587#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
588 vim_free(abs_restart_command);
589#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000590}
591#endif
592
Bram Moolenaar98921892016-02-23 17:14:37 +0100593#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594/*
595 * This should be maybe completely removed.
596 * Doesn't seem possible, since check_copy_area() relies on
597 * this information. --danielk
598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000600visibility_event(GtkWidget *widget UNUSED,
601 GdkEventVisibility *event,
602 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603{
604 gui.visibility = event->state;
605 /*
606 * When we do an gdk_window_copy_area(), and the window is partially
607 * obscured, we want to receive an event to tell us whether it worked
608 * or not.
609 */
610 if (gui.text_gc != NULL)
611 gdk_gc_set_exposures(gui.text_gc,
612 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
613 return FALSE;
614}
Bram Moolenaar98921892016-02-23 17:14:37 +0100615#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
617/*
618 * Redraw the corresponding portions of the screen.
619 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100620#if GTK_CHECK_VERSION(3,0,0)
621static gboolean is_key_pressed = FALSE;
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100622static gboolean blink_mode = TRUE;
Bram Moolenaar98921892016-02-23 17:14:37 +0100623
624static gboolean gui_gtk_is_blink_on(void);
Bram Moolenaar98921892016-02-23 17:14:37 +0100625static void gui_gtk_window_clear(GdkWindow *win);
626
627 static void
628gui_gtk3_redraw(int x, int y, int width, int height)
629{
Bram Moolenaarfe344a92017-02-23 12:20:35 +0100630 /* Range checks are left to gui_redraw_block() */
Bram Moolenaar98921892016-02-23 17:14:37 +0100631 gui_redraw_block(Y_2_ROW(y), X_2_COL(x),
632 Y_2_ROW(y + height - 1), X_2_COL(x + width - 1),
633 GUI_MON_NOCLEAR);
634}
635
636 static void
637gui_gtk3_update_cursor(cairo_t *cr)
638{
639 if (gui.row == gui.cursor_row)
640 {
641 gui.by_signal = TRUE;
Bram Moolenaar4fc563b2016-03-12 12:40:58 +0100642 if (State & CMDLINE)
643 gui_update_cursor(TRUE, FALSE);
644 else
645 gui_update_cursor(TRUE, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +0100646 gui.by_signal = FALSE;
647 cairo_paint(cr);
648 }
649}
650
651 static gboolean
652gui_gtk3_should_draw_cursor(void)
653{
654 unsigned int cond = 0;
655 cond |= gui_gtk_is_blink_on();
Bram Moolenaarb4ebf9a2016-03-12 16:28:18 +0100656 if (gui.cursor_col >= gui.col)
657 cond |= is_key_pressed;
Bram Moolenaar98921892016-02-23 17:14:37 +0100658 cond |= gui.in_focus == FALSE;
Bram Moolenaar98921892016-02-23 17:14:37 +0100659 return cond;
660}
661
662 static gboolean
Bram Moolenaar8e31fd52016-06-04 22:18:13 +0200663draw_event(GtkWidget *widget UNUSED,
Bram Moolenaar98921892016-02-23 17:14:37 +0100664 cairo_t *cr,
665 gpointer user_data UNUSED)
666{
667 /* Skip this when the GUI isn't set up yet, will redraw later. */
668 if (gui.starting)
669 return FALSE;
670
671 out_flush(); /* make sure all output has been processed */
672 /* for GTK+ 3, may induce other draw events. */
673
674 cairo_set_source_surface(cr, gui.surface, 0, 0);
675
676 /* Draw the window without the cursor. */
677 gui.by_signal = TRUE;
678 {
679 cairo_rectangle_list_t *list = NULL;
680
Bram Moolenaar98921892016-02-23 17:14:37 +0100681 list = cairo_copy_clip_rectangle_list(cr);
682 if (list->status != CAIRO_STATUS_CLIP_NOT_REPRESENTABLE)
683 {
684 int i;
Bram Moolenaarfe344a92017-02-23 12:20:35 +0100685
686 /* First clear all the blocks and then redraw them. Just in case
687 * some blocks overlap. */
Bram Moolenaar98921892016-02-23 17:14:37 +0100688 for (i = 0; i < list->num_rectangles; i++)
689 {
690 const cairo_rectangle_t rect = list->rectangles[i];
Bram Moolenaar8e31fd52016-06-04 22:18:13 +0200691
Bram Moolenaarfe344a92017-02-23 12:20:35 +0100692 gui_mch_clear_block(Y_2_ROW((int)rect.y), 0,
693 Y_2_ROW((int)(rect.y + rect.height)) - 1, Columns - 1);
694 }
695
696 for (i = 0; i < list->num_rectangles; i++)
697 {
698 const cairo_rectangle_t rect = list->rectangles[i];
Bram Moolenaar8e31fd52016-06-04 22:18:13 +0200699
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100700 if (blink_mode)
701 gui_gtk3_redraw(rect.x, rect.y, rect.width, rect.height);
702 else
Bram Moolenaarb4ebf9a2016-03-12 16:28:18 +0100703 {
704 if (get_real_state() & VISUAL)
705 gui_gtk3_redraw(rect.x, rect.y,
706 rect.width, rect.height);
707 else
708 gui_redraw(rect.x, rect.y, rect.width, rect.height);
709 }
Bram Moolenaar98921892016-02-23 17:14:37 +0100710 }
711 }
712 cairo_rectangle_list_destroy(list);
713
Bram Moolenaarb4ebf9a2016-03-12 16:28:18 +0100714 if (get_real_state() & VISUAL)
715 {
716 if (gui.cursor_row == gui.row && gui.cursor_col >= gui.col)
717 gui_update_cursor(TRUE, TRUE);
718 }
719
Bram Moolenaar98921892016-02-23 17:14:37 +0100720 cairo_paint(cr);
721 }
722 gui.by_signal = FALSE;
723
724 /* Add the cursor to the window if necessary.*/
Bram Moolenaarb4ebf9a2016-03-12 16:28:18 +0100725 if (gui_gtk3_should_draw_cursor() && blink_mode)
Bram Moolenaar98921892016-02-23 17:14:37 +0100726 gui_gtk3_update_cursor(cr);
727
728 return FALSE;
729}
730#else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000732expose_event(GtkWidget *widget UNUSED,
733 GdkEventExpose *event,
734 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735{
736 /* Skip this when the GUI isn't set up yet, will redraw later. */
737 if (gui.starting)
738 return FALSE;
739
740 out_flush(); /* make sure all output has been processed */
741 gui_redraw(event->area.x, event->area.y,
742 event->area.width, event->area.height);
743
744 /* Clear the border areas if needed */
745 if (event->area.x < FILL_X(0))
746 gdk_window_clear_area(gui.drawarea->window, 0, 0, FILL_X(0), 0);
747 if (event->area.y < FILL_Y(0))
748 gdk_window_clear_area(gui.drawarea->window, 0, 0, 0, FILL_Y(0));
749 if (event->area.x > FILL_X(Columns))
750 gdk_window_clear_area(gui.drawarea->window,
751 FILL_X((int)Columns), 0, 0, 0);
752 if (event->area.y > FILL_Y(Rows))
753 gdk_window_clear_area(gui.drawarea->window, 0, FILL_Y((int)Rows), 0, 0);
754
755 return FALSE;
756}
Bram Moolenaar98921892016-02-23 17:14:37 +0100757#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758
759#ifdef FEAT_CLIENTSERVER
760/*
761 * Handle changes to the "Comm" property
762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000764property_event(GtkWidget *widget,
765 GdkEventProperty *event,
766 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767{
768 if (event->type == GDK_PROPERTY_NOTIFY
769 && event->state == (int)GDK_PROPERTY_NEW_VALUE
Bram Moolenaar98921892016-02-23 17:14:37 +0100770# if GTK_CHECK_VERSION(3,0,0)
771 && GDK_WINDOW_XID(event->window) == commWindow
772# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 && GDK_WINDOW_XWINDOW(event->window) == commWindow
Bram Moolenaar98921892016-02-23 17:14:37 +0100774# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 && GET_X_ATOM(event->atom) == commProperty)
776 {
777 XEvent xev;
778
779 /* Translate to XLib */
780 xev.xproperty.type = PropertyNotify;
781 xev.xproperty.atom = commProperty;
782 xev.xproperty.window = commWindow;
783 xev.xproperty.state = PropertyNewValue;
Bram Moolenaar98921892016-02-23 17:14:37 +0100784# if GTK_CHECK_VERSION(3,0,0)
785 serverEventProc(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(widget)),
786 &xev, 0);
787# else
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200788 serverEventProc(GDK_WINDOW_XDISPLAY(widget->window), &xev, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +0100789# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 }
791 return FALSE;
792}
Bram Moolenaar98921892016-02-23 17:14:37 +0100793#endif /* defined(FEAT_CLIENTSERVER) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
795
796/****************************************************************************
797 * Focus handlers:
798 */
799
800
801/*
802 * This is a simple state machine:
803 * BLINK_NONE not blinking at all
804 * BLINK_OFF blinking, cursor is not shown
805 * BLINK_ON blinking, cursor is shown
806 */
807
808#define BLINK_NONE 0
809#define BLINK_OFF 1
810#define BLINK_ON 2
811
812static int blink_state = BLINK_NONE;
813static long_u blink_waittime = 700;
814static long_u blink_ontime = 400;
815static long_u blink_offtime = 250;
816static guint blink_timer = 0;
817
Bram Moolenaar98921892016-02-23 17:14:37 +0100818#if GTK_CHECK_VERSION(3,0,0)
819 static gboolean
820gui_gtk_is_blink_on(void)
821{
822 return blink_state == BLINK_ON;
823}
Bram Moolenaar98921892016-02-23 17:14:37 +0100824#endif
825
Bram Moolenaar703a8042016-06-04 16:24:32 +0200826 int
827gui_mch_is_blinking(void)
828{
829 return blink_state != BLINK_NONE;
830}
831
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200832 int
833gui_mch_is_blink_off(void)
834{
835 return blink_state == BLINK_OFF;
836}
837
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 void
839gui_mch_set_blinking(long waittime, long on, long off)
840{
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100841#if GTK_CHECK_VERSION(3,0,0)
842 if (waittime == 0 || on == 0 || off == 0)
843 {
844 blink_mode = FALSE;
845
846 blink_waittime = 700;
847 blink_ontime = 400;
848 blink_offtime = 250;
849 }
850 else
851 {
852 blink_mode = TRUE;
853
854 blink_waittime = waittime;
855 blink_ontime = on;
856 blink_offtime = off;
857 }
858#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 blink_waittime = waittime;
860 blink_ontime = on;
861 blink_offtime = off;
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863}
864
865/*
866 * Stop the cursor blinking. Show the cursor if it wasn't shown.
867 */
868 void
869gui_mch_stop_blink(void)
870{
871 if (blink_timer)
872 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100873#if GTK_CHECK_VERSION(3,0,0)
874 g_source_remove(blink_timer);
875#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 gtk_timeout_remove(blink_timer);
Bram Moolenaar98921892016-02-23 17:14:37 +0100877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 blink_timer = 0;
879 }
880 if (blink_state == BLINK_OFF)
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200881 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200883 gui_mch_flush();
884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 blink_state = BLINK_NONE;
886}
887
Bram Moolenaar98921892016-02-23 17:14:37 +0100888#if GTK_CHECK_VERSION(3,0,0)
889 static gboolean
890#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +0100892#endif
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000893blink_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894{
895 if (blink_state == BLINK_ON)
896 {
897 gui_undraw_cursor();
898 blink_state = BLINK_OFF;
Bram Moolenaar98921892016-02-23 17:14:37 +0100899#if GTK_CHECK_VERSION(3,0,0)
900 blink_timer = g_timeout_add((guint)blink_offtime,
901 (GSourceFunc) blink_cb, NULL);
902#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 blink_timer = gtk_timeout_add((guint32)blink_offtime,
904 (GtkFunction) blink_cb, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +0100905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 }
907 else
908 {
909 gui_update_cursor(TRUE, FALSE);
910 blink_state = BLINK_ON;
Bram Moolenaar98921892016-02-23 17:14:37 +0100911#if GTK_CHECK_VERSION(3,0,0)
912 blink_timer = g_timeout_add((guint)blink_ontime,
913 (GSourceFunc) blink_cb, NULL);
914#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 blink_timer = gtk_timeout_add((guint32)blink_ontime,
916 (GtkFunction) blink_cb, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +0100917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 }
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200919 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920
921 return FALSE; /* don't happen again */
922}
923
924/*
925 * Start the cursor blinking. If it was already blinking, this restarts the
926 * waiting time and shows the cursor.
927 */
928 void
929gui_mch_start_blink(void)
930{
931 if (blink_timer)
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +0200932 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100933#if GTK_CHECK_VERSION(3,0,0)
934 g_source_remove(blink_timer);
935#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 gtk_timeout_remove(blink_timer);
Bram Moolenaar98921892016-02-23 17:14:37 +0100937#endif
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +0200938 blink_timer = 0;
939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 /* Only switch blinking on if none of the times is zero */
941 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
942 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100943#if GTK_CHECK_VERSION(3,0,0)
944 blink_timer = g_timeout_add((guint)blink_waittime,
945 (GSourceFunc) blink_cb, NULL);
946#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 blink_timer = gtk_timeout_add((guint32)blink_waittime,
948 (GtkFunction) blink_cb, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +0100949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 blink_state = BLINK_ON;
951 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200952 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 }
954}
955
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000957enter_notify_event(GtkWidget *widget UNUSED,
958 GdkEventCrossing *event UNUSED,
959 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960{
961 if (blink_state == BLINK_NONE)
962 gui_mch_start_blink();
963
964 /* make sure keyboard input goes there */
Bram Moolenaar98921892016-02-23 17:14:37 +0100965#if GTK_CHECK_VERSION(3,0,0)
966 if (gtk_socket_id == 0 || !gtk_widget_has_focus(gui.drawarea))
967#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 if (gtk_socket_id == 0 || !GTK_WIDGET_HAS_FOCUS(gui.drawarea))
Bram Moolenaar98921892016-02-23 17:14:37 +0100969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 gtk_widget_grab_focus(gui.drawarea);
971
972 return FALSE;
973}
974
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000976leave_notify_event(GtkWidget *widget UNUSED,
977 GdkEventCrossing *event UNUSED,
978 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979{
980 if (blink_state != BLINK_NONE)
981 gui_mch_stop_blink();
982
983 return FALSE;
984}
985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000987focus_in_event(GtkWidget *widget,
988 GdkEventFocus *event UNUSED,
989 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990{
991 gui_focus_change(TRUE);
992
993 if (blink_state == BLINK_NONE)
994 gui_mch_start_blink();
995
Bram Moolenaar9c8791f2007-09-05 19:47:23 +0000996 /* make sure keyboard input goes to the draw area (if this is focus for a
997 * window) */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000998 if (widget != gui.drawarea)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000999 gtk_widget_grab_focus(gui.drawarea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000
1001 return TRUE;
1002}
1003
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001005focus_out_event(GtkWidget *widget UNUSED,
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001006 GdkEventFocus *event UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001007 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008{
1009 gui_focus_change(FALSE);
1010
1011 if (blink_state != BLINK_NONE)
1012 gui_mch_stop_blink();
1013
1014 return TRUE;
1015}
1016
1017
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018/*
1019 * Translate a GDK key value to UTF-8 independently of the current locale.
1020 * The output is written to string, which must have room for at least 6 bytes
1021 * plus the NUL terminator. Returns the length in bytes.
1022 *
1023 * This function is used in the GTK+ 2 GUI only. The GTK+ 1 code makes use
1024 * of GdkEventKey::string instead. But event->string is evil; see here why:
1025 * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey
1026 */
1027 static int
1028keyval_to_string(unsigned int keyval, unsigned int state, char_u *string)
1029{
1030 int len;
1031 guint32 uc;
1032
1033 uc = gdk_keyval_to_unicode(keyval);
1034 if (uc != 0)
1035 {
1036 /* Check for CTRL-foo */
1037 if ((state & GDK_CONTROL_MASK) && uc >= 0x20 && uc < 0x80)
1038 {
1039 /* These mappings look arbitrary at the first glance, but in fact
1040 * resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1041 * machine. The only difference is BS vs. DEL for CTRL-8 (makes
1042 * more sense and is consistent with usual terminal behaviour). */
1043 if (uc >= '@')
1044 string[0] = uc & 0x1F;
1045 else if (uc == '2')
1046 string[0] = NUL;
1047 else if (uc >= '3' && uc <= '7')
1048 string[0] = uc ^ 0x28;
1049 else if (uc == '8')
1050 string[0] = BS;
1051 else if (uc == '?')
1052 string[0] = DEL;
1053 else
1054 string[0] = uc;
1055 len = 1;
1056 }
1057 else
1058 {
1059 /* Translate a normal key to UTF-8. This doesn't work for dead
1060 * keys of course, you _have_ to use an input method for that. */
1061 len = utf_char2bytes((int)uc, string);
1062 }
1063 }
1064 else
1065 {
1066 /* Translate keys which are represented by ASCII control codes in Vim.
1067 * There are only a few of those; most control keys are translated to
1068 * special terminal-like control sequences. */
1069 len = 1;
1070 switch (keyval)
1071 {
1072 case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab:
1073 string[0] = TAB;
1074 break;
1075 case GDK_Linefeed:
1076 string[0] = NL;
1077 break;
1078 case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter:
1079 string[0] = CAR;
1080 break;
1081 case GDK_Escape:
1082 string[0] = ESC;
1083 break;
1084 default:
1085 len = 0;
1086 break;
1087 }
1088 }
1089 string[len] = NUL;
1090
1091 return len;
1092}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001094 static int
1095modifiers_gdk2vim(guint state)
1096{
1097 int modifiers = 0;
1098
1099 if (state & GDK_SHIFT_MASK)
1100 modifiers |= MOD_MASK_SHIFT;
1101 if (state & GDK_CONTROL_MASK)
1102 modifiers |= MOD_MASK_CTRL;
1103 if (state & GDK_MOD1_MASK)
1104 modifiers |= MOD_MASK_ALT;
Bram Moolenaar580061a2010-08-13 13:57:13 +02001105#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001106 if (state & GDK_SUPER_MASK)
1107 modifiers |= MOD_MASK_META;
1108#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001109 if (state & GDK_MOD4_MASK)
1110 modifiers |= MOD_MASK_META;
1111
1112 return modifiers;
1113}
1114
1115 static int
1116modifiers_gdk2mouse(guint state)
1117{
1118 int modifiers = 0;
1119
1120 if (state & GDK_SHIFT_MASK)
1121 modifiers |= MOUSE_SHIFT;
1122 if (state & GDK_CONTROL_MASK)
1123 modifiers |= MOUSE_CTRL;
1124 if (state & GDK_MOD1_MASK)
1125 modifiers |= MOUSE_ALT;
1126
1127 return modifiers;
1128}
1129
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130/*
1131 * Main keyboard handler:
1132 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001134key_press_event(GtkWidget *widget UNUSED,
1135 GdkEventKey *event,
1136 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137{
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001138 /* For GTK+ 2 we know for sure how large the string might get.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 * (That is, up to 6 bytes + NUL + CSI escapes + safety measure.) */
1140 char_u string[32], string2[32];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 guint key_sym;
1142 int len;
1143 int i;
1144 int modifiers;
1145 int key;
1146 guint state;
1147 char_u *s, *d;
1148
Bram Moolenaar98921892016-02-23 17:14:37 +01001149#if GTK_CHECK_VERSION(3,0,0)
1150 is_key_pressed = TRUE;
1151 gui_mch_stop_blink();
1152#endif
1153
Bram Moolenaar20892c12011-06-26 04:49:00 +02001154 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 key_sym = event->keyval;
1156 state = event->state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
1158#ifdef FEAT_XIM
1159 if (xim_queue_key_press_event(event, TRUE))
1160 return TRUE;
1161#endif
1162
1163#ifdef FEAT_HANGULIN
1164 if (key_sym == GDK_space && (state & GDK_SHIFT_MASK))
1165 {
1166 hangul_input_state_toggle();
1167 return TRUE;
1168 }
1169#endif
1170
1171#ifdef SunXK_F36
1172 /*
1173 * These keys have bogus lookup strings, and trapping them here is
1174 * easier than trying to XRebindKeysym() on them with every possible
1175 * combination of modifiers.
1176 */
1177 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
1178 len = 0;
1179 else
1180#endif
1181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 len = keyval_to_string(key_sym, state, string2);
1183
1184 /* Careful: convert_input() doesn't handle the NUL character.
1185 * No need to convert pure ASCII anyway, thus the len > 1 check. */
1186 if (len > 1 && input_conv.vc_type != CONV_NONE)
1187 len = convert_input(string2, len, sizeof(string2));
1188
1189 s = string2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 d = string;
1191 for (i = 0; i < len; ++i)
1192 {
1193 *d++ = s[i];
1194 if (d[-1] == CSI && d + 2 < string + sizeof(string))
1195 {
1196 /* Turn CSI into K_CSI. */
1197 *d++ = KS_EXTRA;
1198 *d++ = (int)KE_CSI;
1199 }
1200 }
1201 len = d - string;
1202 }
1203
1204 /* Shift-Tab results in Left_Tab, but we want <S-Tab> */
1205 if (key_sym == GDK_ISO_Left_Tab)
1206 {
1207 key_sym = GDK_Tab;
1208 state |= GDK_SHIFT_MASK;
1209 }
1210
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211#ifdef FEAT_MENU
1212 /* If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key
1213 * is a menu shortcut, we ignore everything with the ALT modifier. */
1214 if ((state & GDK_MOD1_MASK)
1215 && gui.menu_is_active
1216 && (*p_wak == 'y'
1217 || (*p_wak == 'm'
1218 && len == 1
1219 && gui_is_menu_shortcut(string[0]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 /* For GTK2 we return false to signify that we haven't handled the
1221 * keypress, so that gtk will handle the mnemonic or accelerator. */
1222 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223#endif
1224
1225 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
1226 * that already has the 8th bit set.
1227 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
1228 * Don't do this for double-byte encodings, it turns the char into a lead
1229 * byte. */
1230 if (len == 1
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001231 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001232#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001233 || (state & GDK_SUPER_MASK)
1234#endif
1235 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 && !(key_sym == GDK_BackSpace || key_sym == GDK_Delete)
1237 && (string[0] & 0x80) == 0
1238 && !(key_sym == GDK_Tab && (state & GDK_SHIFT_MASK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 )
1241 {
1242 string[0] |= 0x80;
1243 state &= ~GDK_MOD1_MASK; /* don't use it again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 if (enc_utf8) /* convert to utf-8 */
1245 {
1246 string[1] = string[0] & 0xbf;
1247 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
1248 if (string[1] == CSI)
1249 {
1250 string[2] = KS_EXTRA;
1251 string[3] = (int)KE_CSI;
1252 len = 4;
1253 }
1254 else
1255 len = 2;
1256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 }
1258
1259 /* Check for special keys. Also do this when len == 1 (key has an ASCII
1260 * value) to detect backspace, delete and keypad keys. */
1261 if (len == 0 || len == 1)
1262 {
1263 for (i = 0; special_keys[i].key_sym != 0; i++)
1264 {
1265 if (special_keys[i].key_sym == key_sym)
1266 {
1267 string[0] = CSI;
1268 string[1] = special_keys[i].code0;
1269 string[2] = special_keys[i].code1;
1270 len = -3;
1271 break;
1272 }
1273 }
1274 }
1275
1276 if (len == 0) /* Unrecognized key */
1277 return TRUE;
1278
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 /* Special keys (and a few others) may have modifiers. Also when using a
1280 * double-byte encoding (can't set the 8th bit). */
1281 if (len == -3 || key_sym == GDK_space || key_sym == GDK_Tab
1282 || key_sym == GDK_Return || key_sym == GDK_Linefeed
1283 || key_sym == GDK_Escape || key_sym == GDK_KP_Tab
1284 || key_sym == GDK_ISO_Enter || key_sym == GDK_3270_Enter
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001285 || (enc_dbcs && len == 1 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001286#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001287 || (state & GDK_SUPER_MASK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288#endif
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001289 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001291 modifiers = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292
1293 /*
1294 * For some keys a shift modifier is translated into another key
1295 * code.
1296 */
1297 if (len == -3)
1298 key = TO_SPECIAL(string[1], string[2]);
1299 else
1300 key = string[0];
1301
1302 key = simplify_key(key, &modifiers);
1303 if (key == CSI)
1304 key = K_CSI;
1305 if (IS_SPECIAL(key))
1306 {
1307 string[0] = CSI;
1308 string[1] = K_SECOND(key);
1309 string[2] = K_THIRD(key);
1310 len = 3;
1311 }
1312 else
1313 {
1314 string[0] = key;
1315 len = 1;
1316 }
1317
1318 if (modifiers != 0)
1319 {
1320 string2[0] = CSI;
1321 string2[1] = KS_MODIFIER;
1322 string2[2] = modifiers;
1323 add_to_input_buf(string2, 3);
1324 }
1325 }
1326
1327 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1328 || (string[0] == intr_char && intr_char != Ctrl_C)))
1329 {
1330 trash_input_buf();
1331 got_int = TRUE;
1332 }
1333
1334 add_to_input_buf(string, len);
1335
1336 /* blank out the pointer if necessary */
1337 if (p_mh)
1338 gui_mch_mousehide(TRUE);
1339
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 return TRUE;
1341}
1342
Bram Moolenaar98921892016-02-23 17:14:37 +01001343#if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001345key_release_event(GtkWidget *widget UNUSED,
1346 GdkEventKey *event,
1347 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
Bram Moolenaar98921892016-02-23 17:14:37 +01001349# if GTK_CHECK_VERSION(3,0,0)
1350 is_key_pressed = FALSE;
1351 gui_mch_start_blink();
1352# endif
1353# if defined(FEAT_XIM)
Bram Moolenaar20892c12011-06-26 04:49:00 +02001354 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 /*
1356 * GTK+ 2 input methods may do fancy stuff on key release events too.
1357 * With the default IM for instance, you can enter any UCS code point
1358 * by holding down CTRL-SHIFT and typing hexadecimal digits.
1359 */
1360 return xim_queue_key_press_event(event, FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001361# else
1362 return TRUE;
1363# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364}
1365#endif
1366
1367
1368/****************************************************************************
1369 * Selection handlers:
1370 */
1371
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001373selection_clear_event(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 GdkEventSelection *event,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001375 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
1377 if (event->selection == clip_plus.gtk_sel_atom)
1378 clip_lose_selection(&clip_plus);
1379 else
1380 clip_lose_selection(&clip_star);
1381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 return TRUE;
1383}
1384
1385#define RS_NONE 0 /* selection_received_cb() not called yet */
1386#define RS_OK 1 /* selection_received_cb() called and OK */
1387#define RS_FAIL 2 /* selection_received_cb() called and failed */
1388static int received_selection = RS_NONE;
1389
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001391selection_received_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 GtkSelectionData *data,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001393 guint time_ UNUSED,
1394 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395{
1396 VimClipboard *cbd;
1397 char_u *text;
1398 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 guchar *tmpbuf_utf8 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 int len;
Bram Moolenaard44347f2011-06-19 01:14:29 +02001401 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402
Bram Moolenaar98921892016-02-23 17:14:37 +01001403#if GTK_CHECK_VERSION(3,0,0)
1404 if (gtk_selection_data_get_selection(data) == clip_plus.gtk_sel_atom)
1405#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if (data->selection == clip_plus.gtk_sel_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 cbd = &clip_plus;
1409 else
1410 cbd = &clip_star;
1411
Bram Moolenaar98921892016-02-23 17:14:37 +01001412#if GTK_CHECK_VERSION(3,0,0)
1413 text = (char_u *)gtk_selection_data_get_data(data);
1414 len = gtk_selection_data_get_length(data);
1415#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 text = (char_u *)data->data;
1417 len = data->length;
Bram Moolenaar98921892016-02-23 17:14:37 +01001418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419
1420 if (text == NULL || len <= 0)
1421 {
1422 received_selection = RS_FAIL;
1423 /* clip_free_selection(cbd); ??? */
1424
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 return;
1426 }
1427
Bram Moolenaar98921892016-02-23 17:14:37 +01001428#if GTK_CHECK_VERSION(3,0,0)
1429 if (gtk_selection_data_get_data_type(data) == vim_atom)
1430#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 if (data->type == vim_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 {
1434 motion_type = *text++;
1435 --len;
1436 }
Bram Moolenaar98921892016-02-23 17:14:37 +01001437#if GTK_CHECK_VERSION(3,0,0)
1438 else if (gtk_selection_data_get_data_type(data) == vimenc_atom)
1439#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 else if (data->type == vimenc_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 {
1443 char_u *enc;
1444 vimconv_T conv;
1445
1446 motion_type = *text++;
1447 --len;
1448
1449 enc = text;
1450 text += STRLEN(text) + 1;
1451 len -= text - enc;
1452
1453 /* If the encoding of the text is different from 'encoding', attempt
1454 * converting it. */
1455 conv.vc_type = CONV_NONE;
1456 convert_setup(&conv, enc, p_enc);
1457 if (conv.vc_type != CONV_NONE)
1458 {
1459 tmpbuf = string_convert(&conv, text, &len);
1460 if (tmpbuf != NULL)
1461 text = tmpbuf;
1462 convert_setup(&conv, NULL, NULL);
1463 }
1464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 /* gtk_selection_data_get_text() handles all the nasty details
1467 * and targets and encodings etc. This rocks so hard. */
1468 else
1469 {
1470 tmpbuf_utf8 = gtk_selection_data_get_text(data);
1471 if (tmpbuf_utf8 != NULL)
1472 {
1473 len = STRLEN(tmpbuf_utf8);
1474 if (input_conv.vc_type != CONV_NONE)
1475 {
1476 tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len);
1477 if (tmpbuf != NULL)
1478 text = tmpbuf;
1479 }
1480 else
1481 text = tmpbuf_utf8;
1482 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001483 else if (len >= 2 && text[0] == 0xff && text[1] == 0xfe)
1484 {
1485 vimconv_T conv;
1486
1487 /* UTF-16, we get this for HTML */
1488 conv.vc_type = CONV_NONE;
1489 convert_setup_ext(&conv, (char_u *)"utf-16le", FALSE, p_enc, TRUE);
1490
1491 if (conv.vc_type != CONV_NONE)
1492 {
1493 text += 2;
1494 len -= 2;
1495 tmpbuf = string_convert(&conv, text, &len);
1496 convert_setup(&conv, NULL, NULL);
1497 }
1498 if (tmpbuf != NULL)
1499 text = tmpbuf;
1500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001503 /* Chop off any trailing NUL bytes. OpenOffice sends these. */
Bram Moolenaara76638f2010-06-05 12:49:46 +02001504 while (len > 0 && text[len - 1] == NUL)
1505 --len;
1506
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 clip_yank_selection(motion_type, text, (long)len, cbd);
1508 received_selection = RS_OK;
1509 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 g_free(tmpbuf_utf8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511}
1512
1513/*
1514 * Prepare our selection data for passing it to the external selection
1515 * client.
1516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001518selection_get_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 GtkSelectionData *selection_data,
1520 guint info,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001521 guint time_ UNUSED,
1522 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523{
1524 char_u *string;
1525 char_u *tmpbuf;
1526 long_u tmplen;
1527 int length;
1528 int motion_type;
1529 GdkAtom type;
1530 VimClipboard *cbd;
1531
Bram Moolenaar98921892016-02-23 17:14:37 +01001532#if GTK_CHECK_VERSION(3,0,0)
1533 if (gtk_selection_data_get_selection(selection_data)
1534 == clip_plus.gtk_sel_atom)
1535#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 if (selection_data->selection == clip_plus.gtk_sel_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 cbd = &clip_plus;
1539 else
1540 cbd = &clip_star;
1541
1542 if (!cbd->owned)
1543 return; /* Shouldn't ever happen */
1544
1545 if (info != (guint)TARGET_STRING
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001546 && (!clip_html || info != (guint)TARGET_HTML)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 && info != (guint)TARGET_UTF8_STRING
1548 && info != (guint)TARGET_VIMENC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 && info != (guint)TARGET_VIM
1550 && info != (guint)TARGET_COMPOUND_TEXT
1551 && info != (guint)TARGET_TEXT)
1552 return;
1553
1554 /* get the selection from the '*'/'+' register */
1555 clip_get_selection(cbd);
1556
1557 motion_type = clip_convert_selection(&string, &tmplen, cbd);
1558 if (motion_type < 0 || string == NULL)
1559 return;
1560 /* Due to int arguments we can't handle more than G_MAXINT. Also
1561 * reserve one extra byte for NUL or the motion type; just in case.
1562 * (Not that pasting 2G of text is ever going to work, but... ;-) */
1563 length = MIN(tmplen, (long_u)(G_MAXINT - 1));
1564
1565 if (info == (guint)TARGET_VIM)
1566 {
1567 tmpbuf = alloc((unsigned)length + 1);
1568 if (tmpbuf != NULL)
1569 {
1570 tmpbuf[0] = motion_type;
1571 mch_memmove(tmpbuf + 1, string, (size_t)length);
1572 }
1573 /* For our own format, the first byte contains the motion type */
1574 ++length;
1575 vim_free(string);
1576 string = tmpbuf;
1577 type = vim_atom;
1578 }
1579
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001580 else if (info == (guint)TARGET_HTML)
1581 {
1582 vimconv_T conv;
1583
1584 /* Since we get utf-16, we probably should set it as well. */
1585 conv.vc_type = CONV_NONE;
1586 convert_setup_ext(&conv, p_enc, TRUE, (char_u *)"utf-16le", FALSE);
1587 if (conv.vc_type != CONV_NONE)
1588 {
1589 tmpbuf = string_convert(&conv, string, &length);
1590 convert_setup(&conv, NULL, NULL);
1591 vim_free(string);
1592 string = tmpbuf;
1593 }
1594
1595 /* Prepend the BOM: "fffe" */
1596 if (string != NULL)
1597 {
1598 tmpbuf = alloc(length + 2);
1599 tmpbuf[0] = 0xff;
1600 tmpbuf[1] = 0xfe;
1601 mch_memmove(tmpbuf + 2, string, (size_t)length);
1602 vim_free(string);
1603 string = tmpbuf;
1604 length += 2;
1605
Bram Moolenaar98921892016-02-23 17:14:37 +01001606#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01001607 /* Looks redundant even for GTK2 because these values are
Bram Moolenaar98921892016-02-23 17:14:37 +01001608 * overwritten by gtk_selection_data_set() that follows. */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001609 selection_data->type = selection_data->target;
1610 selection_data->format = 16; /* 16 bits per char */
Bram Moolenaar98921892016-02-23 17:14:37 +01001611#endif
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001612 gtk_selection_data_set(selection_data, html_atom, 16,
1613 string, length);
1614 vim_free(string);
1615 }
1616 return;
1617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 else if (info == (guint)TARGET_VIMENC)
1619 {
1620 int l = STRLEN(p_enc);
1621
1622 /* contents: motion_type 'encoding' NUL text */
1623 tmpbuf = alloc((unsigned)length + l + 2);
1624 if (tmpbuf != NULL)
1625 {
1626 tmpbuf[0] = motion_type;
1627 STRCPY(tmpbuf + 1, p_enc);
1628 mch_memmove(tmpbuf + l + 2, string, (size_t)length);
1629 }
1630 length += l + 2;
1631 vim_free(string);
1632 string = tmpbuf;
1633 type = vimenc_atom;
1634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 /* gtk_selection_data_set_text() handles everything for us. This is
1637 * so easy and simple and cool, it'd be insane not to use it. */
1638 else
1639 {
1640 if (output_conv.vc_type != CONV_NONE)
1641 {
1642 tmpbuf = string_convert(&output_conv, string, &length);
1643 vim_free(string);
1644 if (tmpbuf == NULL)
1645 return;
1646 string = tmpbuf;
1647 }
1648 /* Validate the string to avoid runtime warnings */
1649 if (g_utf8_validate((const char *)string, (gssize)length, NULL))
1650 {
1651 gtk_selection_data_set_text(selection_data,
1652 (const char *)string, length);
1653 }
1654 vim_free(string);
1655 return;
1656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657
1658 if (string != NULL)
1659 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001660#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01001661 /* Looks redundant even for GTK2 because these values are
Bram Moolenaar98921892016-02-23 17:14:37 +01001662 * overwritten by gtk_selection_data_set() that follows. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 selection_data->type = selection_data->target;
1664 selection_data->format = 8; /* 8 bits per char */
Bram Moolenaar98921892016-02-23 17:14:37 +01001665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 gtk_selection_data_set(selection_data, type, 8, string, length);
1667 vim_free(string);
1668 }
1669}
1670
1671/*
Bram Moolenaar29695702012-05-18 17:03:18 +02001672 * Check if the GUI can be started. Called before gvimrc is sourced and
1673 * before fork().
1674 * Return OK or FAIL.
1675 */
1676 int
Bram Moolenaar717e1962016-08-10 21:28:44 +02001677gui_mch_early_init_check(int give_message)
Bram Moolenaar29695702012-05-18 17:03:18 +02001678{
1679 char_u *p;
1680
1681 /* Guess that when $DISPLAY isn't set the GUI can't start. */
1682 p = mch_getenv((char_u *)"DISPLAY");
1683 if (p == NULL || *p == NUL)
1684 {
1685 gui.dying = TRUE;
Bram Moolenaar717e1962016-08-10 21:28:44 +02001686 if (give_message)
1687 EMSG(_((char *)e_opendisp));
Bram Moolenaar29695702012-05-18 17:03:18 +02001688 return FAIL;
1689 }
1690 return OK;
1691}
1692
1693/*
1694 * Check if the GUI can be started. Called before gvimrc is sourced but after
1695 * fork().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 * Return OK or FAIL.
1697 */
1698 int
1699gui_mch_init_check(void)
1700{
Bram Moolenaar3a466a82016-01-19 17:47:25 +01001701#ifdef USE_GRESOURCE
1702 static int res_registered = FALSE;
1703
1704 if (!res_registered)
1705 {
1706 /* Call this function in the GUI process; otherwise, the resources
1707 * won't be available. Don't call it twice. */
1708 res_registered = TRUE;
1709 gui_gtk_register_resource();
1710 }
1711#endif
1712
Bram Moolenaarf80663f2016-04-05 21:56:06 +02001713#if GTK_CHECK_VERSION(3,10,0)
1714 /* Vim currently assumes that Gtk means X11, so it cannot use native Gtk
1715 * support for other backends such as Wayland. */
1716 gdk_set_allowed_backends ("x11");
1717#endif
1718
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719#ifdef FEAT_GUI_GNOME
1720 if (gtk_socket_id == 0)
1721 using_gnome = 1;
1722#endif
1723
Bram Moolenaar8dd79012013-05-21 12:52:04 +02001724 /* This defaults to argv[0], but we want it to match the name of the
1725 * shipped gvim.desktop so that Vim's windows can be associated with this
1726 * file. */
1727 g_set_prgname("gvim");
1728
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 /* Don't use gtk_init() or gnome_init(), it exits on failure. */
1730 if (!gtk_init_check(&gui_argc, &gui_argv))
1731 {
1732 gui.dying = TRUE;
Bram Moolenaarc93e7912008-07-08 10:46:08 +00001733 EMSG(_((char *)e_opendisp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 return FAIL;
1735 }
1736
1737 return OK;
1738}
1739
1740
1741/****************************************************************************
1742 * Mouse handling callbacks
1743 */
1744
1745
1746static guint mouse_click_timer = 0;
1747static int mouse_timed_out = TRUE;
1748
1749/*
1750 * Timer used to recognize multiple clicks of the mouse button
1751 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001752#if GTK_CHECK_VERSION(3,0,0)
1753 static gboolean
1754#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +01001756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757mouse_click_timer_cb(gpointer data)
1758{
1759 /* we don't use this information currently */
1760 int *timed_out = (int *) data;
1761
1762 *timed_out = TRUE;
1763 return FALSE; /* don't happen again */
1764}
1765
1766static guint motion_repeat_timer = 0;
1767static int motion_repeat_offset = FALSE;
Bram Moolenaar98921892016-02-23 17:14:37 +01001768#ifdef GTK_DEST_DEFAULT_ALL
1769static gboolean motion_repeat_timer_cb(gpointer);
1770#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771static gint motion_repeat_timer_cb(gpointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01001772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773
1774 static void
1775process_motion_notify(int x, int y, GdkModifierType state)
1776{
1777 int button;
1778 int_u vim_modifiers;
Bram Moolenaar98921892016-02-23 17:14:37 +01001779#if GTK_CHECK_VERSION(3,0,0)
1780 GtkAllocation allocation;
1781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783 button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1784 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1785 GDK_BUTTON5_MASK))
1786 ? MOUSE_DRAG : ' ';
1787
1788 /* If our pointer is currently hidden, then we should show it. */
1789 gui_mch_mousehide(FALSE);
1790
1791 /* Just moving the rodent above the drawing area without any button
1792 * being pressed. */
1793 if (button != MOUSE_DRAG)
1794 {
1795 gui_mouse_moved(x, y);
1796 return;
1797 }
1798
1799 /* translate modifier coding between the main engine and GTK */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001800 vim_modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
Bram Moolenaar49325942007-05-10 19:19:59 +00001802 /* inform the editor engine about the occurrence of this event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 gui_send_mouse_event(button, x, y, FALSE, vim_modifiers);
1804
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 /*
1806 * Auto repeat timer handling.
1807 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001808#if GTK_CHECK_VERSION(3,0,0)
1809 gtk_widget_get_allocation(gui.drawarea, &allocation);
1810
1811 if (x < 0 || y < 0
1812 || x >= allocation.width
1813 || y >= allocation.height)
1814#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 if (x < 0 || y < 0
1816 || x >= gui.drawarea->allocation.width
1817 || y >= gui.drawarea->allocation.height)
Bram Moolenaar98921892016-02-23 17:14:37 +01001818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 {
1820
1821 int dx;
1822 int dy;
1823 int offshoot;
1824 int delay = 10;
1825
1826 /* Calculate the maximal distance of the cursor from the drawing area.
1827 * (offshoot can't become negative here!).
1828 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001829#if GTK_CHECK_VERSION(3,0,0)
1830 dx = x < 0 ? -x : x - allocation.width;
1831 dy = y < 0 ? -y : y - allocation.height;
1832#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 dx = x < 0 ? -x : x - gui.drawarea->allocation.width;
1834 dy = y < 0 ? -y : y - gui.drawarea->allocation.height;
Bram Moolenaar98921892016-02-23 17:14:37 +01001835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836
1837 offshoot = dx > dy ? dx : dy;
1838
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001839 /* Make a linearly decaying timer delay with a threshold of 5 at a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 * distance of 127 pixels from the main window.
1841 *
1842 * One could think endlessly about the most ergonomic variant here.
1843 * For example it could make sense to calculate the distance from the
1844 * drags start instead...
1845 *
1846 * Maybe a parabolic interpolation would suite us better here too...
1847 */
1848 if (offshoot > 127)
1849 {
1850 /* 5 appears to be somehow near to my perceptual limits :-). */
1851 delay = 5;
1852 }
1853 else
1854 {
1855 delay = (130 * (127 - offshoot)) / 127 + 5;
1856 }
1857
1858 /* shoot again */
1859 if (!motion_repeat_timer)
Bram Moolenaar98921892016-02-23 17:14:37 +01001860#if GTK_CHECK_VERSION(3,0,0)
1861 motion_repeat_timer = g_timeout_add((guint)delay,
1862 motion_repeat_timer_cb, NULL);
1863#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 motion_repeat_timer = gtk_timeout_add((guint32)delay,
1865 motion_repeat_timer_cb, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01001866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868}
1869
Bram Moolenaar98921892016-02-23 17:14:37 +01001870#if GTK_CHECK_VERSION(3,0,0)
1871 static GdkDevice *
1872gui_gtk_get_pointer_device(GtkWidget *widget)
1873{
1874 GdkWindow * const win = gtk_widget_get_window(widget);
1875 GdkDisplay * const dpy = gdk_window_get_display(win);
Bram Moolenaar30e12d22016-04-17 20:49:53 +02001876# if GTK_CHECK_VERSION(3,20,0)
1877 GdkSeat * const seat = gdk_display_get_default_seat(dpy);
1878 return gdk_seat_get_pointer(seat);
1879# else
Bram Moolenaar98921892016-02-23 17:14:37 +01001880 GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy);
1881 return gdk_device_manager_get_client_pointer(mngr);
Bram Moolenaar30e12d22016-04-17 20:49:53 +02001882# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01001883}
1884
1885 static GdkWindow *
1886gui_gtk_get_pointer(GtkWidget *widget,
1887 gint *x,
1888 gint *y,
1889 GdkModifierType *state)
1890{
1891 GdkWindow * const win = gtk_widget_get_window(widget);
1892 GdkDevice * const dev = gui_gtk_get_pointer_device(widget);
1893 return gdk_window_get_device_position(win, dev , x, y, state);
1894}
1895
Bram Moolenaar2369c152016-03-04 23:08:25 +01001896# if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar98921892016-02-23 17:14:37 +01001897 static GdkWindow *
1898gui_gtk_window_at_position(GtkWidget *widget,
1899 gint *x,
1900 gint *y)
1901{
1902 GdkDevice * const dev = gui_gtk_get_pointer_device(widget);
1903 return gdk_device_get_window_at_position(dev, x, y);
1904}
Bram Moolenaar2369c152016-03-04 23:08:25 +01001905# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01001906#endif
1907
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908/*
1909 * Timer used to recognize multiple clicks of the mouse button.
1910 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001911#if GTK_CHECK_VERSION(3,0,0)
1912 static gboolean
1913#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +01001915#endif
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001916motion_repeat_timer_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917{
1918 int x;
1919 int y;
1920 GdkModifierType state;
1921
Bram Moolenaar98921892016-02-23 17:14:37 +01001922#if GTK_CHECK_VERSION(3,0,0)
1923 gui_gtk_get_pointer(gui.drawarea, &x, &y, &state);
1924#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 gdk_window_get_pointer(gui.drawarea->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +01001926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
1928 if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1929 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1930 GDK_BUTTON5_MASK)))
1931 {
1932 motion_repeat_timer = 0;
1933 return FALSE;
1934 }
1935
1936 /* If there already is a mouse click in the input buffer, wait another
1937 * time (otherwise we would create a backlog of clicks) */
1938 if (vim_used_in_input_buf() > 10)
1939 return TRUE;
1940
1941 motion_repeat_timer = 0;
1942
1943 /*
1944 * Fake a motion event.
1945 * Trick: Pretend the mouse moved to the next character on every other
1946 * event, otherwise drag events will be discarded, because they are still
1947 * in the same character.
1948 */
1949 if (motion_repeat_offset)
1950 x += gui.char_width;
1951
1952 motion_repeat_offset = !motion_repeat_offset;
1953 process_motion_notify(x, y, state);
1954
1955 /* Don't happen again. We will get reinstalled in the synthetic event
1956 * if needed -- thus repeating should still work. */
1957 return FALSE;
1958}
1959
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001961motion_notify_event(GtkWidget *widget,
1962 GdkEventMotion *event,
1963 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964{
1965 if (event->is_hint)
1966 {
1967 int x;
1968 int y;
1969 GdkModifierType state;
1970
Bram Moolenaar98921892016-02-23 17:14:37 +01001971#if GTK_CHECK_VERSION(3,0,0)
1972 gui_gtk_get_pointer(widget, &x, &y, &state);
1973#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 gdk_window_get_pointer(widget->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +01001975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 process_motion_notify(x, y, state);
1977 }
1978 else
1979 {
1980 process_motion_notify((int)event->x, (int)event->y,
1981 (GdkModifierType)event->state);
1982 }
1983
1984 return TRUE; /* handled */
1985}
1986
1987
1988/*
1989 * Mouse button handling. Note please that we are capturing multiple click's
1990 * by our own timeout mechanism instead of the one provided by GTK+ itself.
1991 * This is due to the way the generic VIM code is recognizing multiple clicks.
1992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001994button_press_event(GtkWidget *widget,
1995 GdkEventButton *event,
1996 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997{
1998 int button;
1999 int repeated_click = FALSE;
2000 int x, y;
2001 int_u vim_modifiers;
2002
Bram Moolenaar20892c12011-06-26 04:49:00 +02002003 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002004
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 /* Make sure we have focus now we've been selected */
Bram Moolenaar98921892016-02-23 17:14:37 +01002006#if GTK_CHECK_VERSION(3,0,0)
2007 if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget))
2008#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01002010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 gtk_widget_grab_focus(widget);
2012
2013 /*
2014 * Don't let additional events about multiple clicks send by GTK to us
2015 * after the initial button press event confuse us.
2016 */
2017 if (event->type != GDK_BUTTON_PRESS)
2018 return FALSE;
2019
2020 x = event->x;
2021 y = event->y;
2022
2023 /* Handle multiple clicks */
2024 if (!mouse_timed_out && mouse_click_timer)
2025 {
Bram Moolenaar98921892016-02-23 17:14:37 +01002026#if GTK_CHECK_VERSION(3,0,0)
2027 g_source_remove(mouse_click_timer);
2028#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 gtk_timeout_remove(mouse_click_timer);
Bram Moolenaar98921892016-02-23 17:14:37 +01002030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 mouse_click_timer = 0;
2032 repeated_click = TRUE;
2033 }
2034
2035 mouse_timed_out = FALSE;
Bram Moolenaar98921892016-02-23 17:14:37 +01002036#if GTK_CHECK_VERSION(3,0,0)
2037 mouse_click_timer = g_timeout_add((guint)p_mouset,
2038 mouse_click_timer_cb, &mouse_timed_out);
2039#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 mouse_click_timer = gtk_timeout_add((guint32)p_mouset,
2041 mouse_click_timer_cb, &mouse_timed_out);
Bram Moolenaar98921892016-02-23 17:14:37 +01002042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043
2044 switch (event->button)
2045 {
Bram Moolenaar88e484b2015-11-24 15:38:44 +01002046 /* Keep in sync with gui_x11.c.
2047 * Buttons 4-7 are handled in scroll_event() */
2048 case 1: button = MOUSE_LEFT; break;
2049 case 2: button = MOUSE_MIDDLE; break;
2050 case 3: button = MOUSE_RIGHT; break;
2051 case 8: button = MOUSE_X1; break;
2052 case 9: button = MOUSE_X2; break;
2053 default:
2054 return FALSE; /* Unknown button */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 }
2056
2057#ifdef FEAT_XIM
2058 /* cancel any preediting */
2059 if (im_is_preediting())
2060 xim_reset();
2061#endif
2062
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002063 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
2065 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066
2067 return TRUE;
2068}
2069
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070/*
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002071 * GTK+ 2 abstracts scrolling via the GdkEventScroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002074scroll_event(GtkWidget *widget,
2075 GdkEventScroll *event,
2076 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077{
2078 int button;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002079 int_u vim_modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080
Bram Moolenaar98921892016-02-23 17:14:37 +01002081#if GTK_CHECK_VERSION(3,0,0)
2082 if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget))
2083#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01002085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 gtk_widget_grab_focus(widget);
2087
2088 switch (event->direction)
2089 {
2090 case GDK_SCROLL_UP:
2091 button = MOUSE_4;
2092 break;
2093 case GDK_SCROLL_DOWN:
2094 button = MOUSE_5;
2095 break;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002096 case GDK_SCROLL_LEFT:
2097 button = MOUSE_7;
2098 break;
2099 case GDK_SCROLL_RIGHT:
2100 button = MOUSE_6;
2101 break;
2102 default: /* This shouldn't happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 return FALSE;
2104 }
2105
2106# ifdef FEAT_XIM
2107 /* cancel any preediting */
2108 if (im_is_preediting())
2109 xim_reset();
2110# endif
2111
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002112 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113
2114 gui_send_mouse_event(button, (int)event->x, (int)event->y,
2115 FALSE, vim_modifiers);
2116
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 return TRUE;
2118}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119
2120
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002122button_release_event(GtkWidget *widget UNUSED,
2123 GdkEventButton *event,
2124 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125{
2126 int x, y;
2127 int_u vim_modifiers;
2128
Bram Moolenaar20892c12011-06-26 04:49:00 +02002129 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002130
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 /* Remove any motion "machine gun" timers used for automatic further
2132 extension of allocation areas if outside of the applications window
2133 area .*/
2134 if (motion_repeat_timer)
2135 {
Bram Moolenaar98921892016-02-23 17:14:37 +01002136#if GTK_CHECK_VERSION(3,0,0)
2137 g_source_remove(motion_repeat_timer);
2138#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 gtk_timeout_remove(motion_repeat_timer);
Bram Moolenaar98921892016-02-23 17:14:37 +01002140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 motion_repeat_timer = 0;
2142 }
2143
2144 x = event->x;
2145 y = event->y;
2146
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002147 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148
2149 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150
2151 return TRUE;
2152}
2153
2154
2155#ifdef FEAT_DND
2156/****************************************************************************
2157 * Drag aNd Drop support handlers.
2158 */
2159
2160/*
2161 * Count how many items there may be and separate them with a NUL.
2162 * Apparently the items are separated with \r\n. This is not documented,
2163 * thus be careful not to go past the end. Also allow separation with
2164 * NUL characters.
2165 */
2166 static int
2167count_and_decode_uri_list(char_u *out, char_u *raw, int len)
2168{
2169 int i;
2170 char_u *p = out;
2171 int count = 0;
2172
2173 for (i = 0; i < len; ++i)
2174 {
2175 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r')
2176 {
2177 if (p > out && p[-1] != NUL)
2178 {
2179 ++count;
2180 *p++ = NUL;
2181 }
2182 }
2183 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0)
2184 {
2185 *p++ = hexhex2nr(raw + i + 1);
2186 i += 2;
2187 }
2188 else
2189 *p++ = raw[i];
2190 }
2191 if (p > out && p[-1] != NUL)
2192 {
2193 *p = NUL; /* last item didn't have \r or \n */
2194 ++count;
2195 }
2196 return count;
2197}
2198
2199/*
2200 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
2201 * this process, URI which protocol is not "file:" are removed. Return
2202 * length of array (less than "max").
2203 */
2204 static int
2205filter_uri_list(char_u **outlist, int max, char_u *src)
2206{
2207 int i, j;
2208
2209 for (i = j = 0; i < max; ++i)
2210 {
2211 outlist[i] = NULL;
2212 if (STRNCMP(src, "file:", 5) == 0)
2213 {
2214 src += 5;
2215 if (STRNCMP(src, "//localhost", 11) == 0)
2216 src += 11;
2217 while (src[0] == '/' && src[1] == '/')
2218 ++src;
2219 outlist[j++] = vim_strsave(src);
2220 }
2221 src += STRLEN(src) + 1;
2222 }
2223 return j;
2224}
2225
2226 static char_u **
2227parse_uri_list(int *count, char_u *data, int len)
2228{
2229 int n = 0;
2230 char_u *tmp = NULL;
Bram Moolenaar945ec092016-06-08 21:17:43 +02002231 char_u **array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232
2233 if (data != NULL && len > 0 && (tmp = (char_u *)alloc(len + 1)) != NULL)
2234 {
2235 n = count_and_decode_uri_list(tmp, data, len);
2236 if (n > 0 && (array = (char_u **)alloc(n * sizeof(char_u *))) != NULL)
2237 n = filter_uri_list(array, n, tmp);
2238 }
2239 vim_free(tmp);
2240 *count = n;
2241 return array;
2242}
2243
2244 static void
2245drag_handle_uri_list(GdkDragContext *context,
2246 GtkSelectionData *data,
2247 guint time_,
2248 GdkModifierType state,
2249 gint x,
2250 gint y)
2251{
2252 char_u **fnames;
2253 int nfiles = 0;
2254
Bram Moolenaar98921892016-02-23 17:14:37 +01002255# if GTK_CHECK_VERSION(3,0,0)
2256 fnames = parse_uri_list(&nfiles,
2257 (char_u *)gtk_selection_data_get_data(data),
2258 gtk_selection_data_get_length(data));
2259# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 fnames = parse_uri_list(&nfiles, data->data, data->length);
Bram Moolenaar98921892016-02-23 17:14:37 +01002261# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262
2263 if (fnames != NULL && nfiles > 0)
2264 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002265 int_u modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266
2267 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
2268
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002269 modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270
2271 gui_handle_drop(x, y, modifiers, fnames, nfiles);
2272 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002273 else
2274 vim_free(fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275}
2276
2277 static void
2278drag_handle_text(GdkDragContext *context,
2279 GtkSelectionData *data,
2280 guint time_,
2281 GdkModifierType state)
2282{
2283 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP};
2284 char_u *text;
2285 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287
Bram Moolenaar98921892016-02-23 17:14:37 +01002288# if GTK_CHECK_VERSION(3,0,0)
2289 text = (char_u *)gtk_selection_data_get_data(data);
2290 len = gtk_selection_data_get_length(data);
2291# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 text = data->data;
2293 len = data->length;
Bram Moolenaar98921892016-02-23 17:14:37 +01002294# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295
Bram Moolenaar98921892016-02-23 17:14:37 +01002296# if GTK_CHECK_VERSION(3,0,0)
2297 if (gtk_selection_data_get_data_type(data) == utf8_string_atom)
2298# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 if (data->type == utf8_string_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01002300# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 if (input_conv.vc_type != CONV_NONE)
2303 tmpbuf = string_convert(&input_conv, text, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if (tmpbuf != NULL)
2305 text = tmpbuf;
2306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307
2308 dnd_yank_drag_data(text, (long)len);
2309 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002312 dropkey[2] = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313
2314 if (dropkey[2] != 0)
2315 add_to_input_buf(dropkey, (int)sizeof(dropkey));
2316 else
2317 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318}
2319
2320/*
2321 * DND receiver.
2322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 static void
2324drag_data_received_cb(GtkWidget *widget,
2325 GdkDragContext *context,
2326 gint x,
2327 gint y,
2328 GtkSelectionData *data,
2329 guint info,
2330 guint time_,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002331 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332{
2333 GdkModifierType state;
2334
2335 /* Guard against trash */
Bram Moolenaar98921892016-02-23 17:14:37 +01002336# if GTK_CHECK_VERSION(3,0,0)
2337 const guchar * const data_data = gtk_selection_data_get_data(data);
2338 const gint data_length = gtk_selection_data_get_length(data);
2339 const gint data_format = gtk_selection_data_get_format(data);
2340
2341 if (data_data == NULL
2342 || data_length <= 0
2343 || data_format != 8
2344 || data_data[data_length] != '\0')
2345# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 if (data->data == NULL
2347 || data->length <= 0
2348 || data->format != 8
2349 || data->data[data->length] != '\0')
Bram Moolenaar98921892016-02-23 17:14:37 +01002350# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 {
2352 gtk_drag_finish(context, FALSE, FALSE, time_);
2353 return;
2354 }
2355
2356 /* Get the current modifier state for proper distinguishment between
2357 * different operations later. */
Bram Moolenaar98921892016-02-23 17:14:37 +01002358#if GTK_CHECK_VERSION(3,0,0)
2359 gui_gtk_get_pointer(widget, NULL, NULL, &state);
2360# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 gdk_window_get_pointer(widget->window, NULL, NULL, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +01002362# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363
2364 /* Not sure about the role of "text/plain" here... */
2365 if (info == (guint)TARGET_TEXT_URI_LIST)
2366 drag_handle_uri_list(context, data, time_, state, x, y);
2367 else
2368 drag_handle_text(context, data, time_, state);
2369
2370}
2371#endif /* FEAT_DND */
2372
2373
2374#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2375/*
2376 * GnomeClient interact callback. Check for unsaved buffers that cannot
2377 * be abandoned and pop up a dialog asking the user for confirmation if
2378 * necessary.
2379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002381sm_client_check_changed_any(GnomeClient *client UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 gint key,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002383 GnomeDialogType type UNUSED,
2384 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385{
2386 cmdmod_T save_cmdmod;
2387 gboolean shutdown_cancelled;
2388
2389 save_cmdmod = cmdmod;
2390
2391# ifdef FEAT_BROWSE
2392 cmdmod.browse = TRUE;
2393# endif
2394# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2395 cmdmod.confirm = TRUE;
2396# endif
2397 /*
2398 * If there are changed buffers, present the user with
2399 * a dialog if possible, otherwise give an error message.
2400 */
Bram Moolenaar027387f2016-01-02 22:25:52 +01002401 shutdown_cancelled = check_changed_any(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402
2403 exiting = FALSE;
2404 cmdmod = save_cmdmod;
2405 setcursor(); /* position the cursor */
2406 out_flush();
2407 /*
2408 * If the user hit the [Cancel] button the whole shutdown
2409 * will be cancelled. Wow, quite powerful feature (:
2410 */
2411 gnome_interaction_key_return(key, shutdown_cancelled);
2412}
2413
2414/*
2415 * Generate a script that can be used to restore the current editing session.
2416 * Save the value of v:this_session before running :mksession in order to make
2417 * automagic session save fully transparent. Return TRUE on success.
2418 */
2419 static int
2420write_session_file(char_u *filename)
2421{
2422 char_u *escaped_filename;
2423 char *mksession_cmdline;
2424 unsigned int save_ssop_flags;
2425 int failed;
2426
2427 /*
2428 * Build an ex command line to create a script that restores the current
2429 * session if executed. Escape the filename to avoid nasty surprises.
2430 */
2431 escaped_filename = vim_strsave_escaped(filename, escape_chars);
2432 if (escaped_filename == NULL)
2433 return FALSE;
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002434 mksession_cmdline = g_strconcat("mksession ", (char *)escaped_filename,
2435 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 vim_free(escaped_filename);
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002437
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 /*
2439 * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
2440 * unpredictable effects when the session is saved automatically. Also,
2441 * we definitely need SSOP_GLOBALS to be able to restore v:this_session.
2442 * Don't use SSOP_BUFFERS to prevent the buffer list from becoming
2443 * enormously large if the GNOME session feature is used regularly.
2444 */
2445 save_ssop_flags = ssop_flags;
2446 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002447 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
2449 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
2450 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
2451 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
Bram Moolenaar485db9b2005-01-31 19:23:41 +00002452 do_unlet((char_u *)"Save_VV_this_session", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453
2454 ssop_flags = save_ssop_flags;
2455 g_free(mksession_cmdline);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002456
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 /*
2458 * Reopen the file and append a command to restore v:this_session,
2459 * as if this save never happened. This is to avoid conflicts with
2460 * the user's own sessions. FIXME: It's probably less hackish to add
2461 * a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
2462 */
2463 if (!failed)
2464 {
2465 FILE *fd;
2466
2467 fd = open_exfile(filename, TRUE, APPENDBIN);
2468
2469 failed = (fd == NULL
2470 || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL
2471 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
2472
2473 if (fd != NULL && fclose(fd) != 0)
2474 failed = TRUE;
2475
2476 if (failed)
2477 mch_remove(filename);
2478 }
2479
2480 return !failed;
2481}
2482
2483/*
2484 * "save_yourself" signal handler. Initiate an interaction to ask the user
2485 * for confirmation if necessary. Save the current editing session and tell
2486 * the session manager how to restart Vim.
2487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 static gboolean
2489sm_client_save_yourself(GnomeClient *client,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002490 gint phase UNUSED,
2491 GnomeSaveStyle save_style UNUSED,
2492 gboolean shutdown UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 GnomeInteractStyle interact_style,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002494 gboolean fast UNUSED,
2495 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496{
2497 static const char suffix[] = "-session.vim";
2498 char *session_file;
2499 unsigned int len;
2500 gboolean success;
2501
2502 /* Always request an interaction if possible. check_changed_any()
2503 * won't actually show a dialog unless any buffers have been modified.
2504 * There doesn't seem to be an obvious way to check that without
2505 * automatically firing the dialog. Anyway, it works just fine. */
2506 if (interact_style == GNOME_INTERACT_ANY)
2507 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL,
2508 &sm_client_check_changed_any,
2509 NULL);
2510 out_flush();
2511 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2512
2513 /* The path is unique for each session save. We do neither know nor care
2514 * which session script will actually be used later. This decision is in
2515 * the domain of the session manager. */
2516 session_file = gnome_config_get_real_path(
2517 gnome_client_get_config_prefix(client));
2518 len = strlen(session_file);
2519
2520 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR)
2521 --len; /* get rid of the superfluous trailing '/' */
2522
2523 session_file = g_renew(char, session_file, len + sizeof(suffix));
2524 memcpy(session_file + len, suffix, sizeof(suffix));
2525
2526 success = write_session_file((char_u *)session_file);
2527
2528 if (success)
2529 {
2530 const char *argv[8];
2531 int i;
2532
2533 /* Tell the session manager how to wipe out the stored session data.
2534 * This isn't as dangerous as it looks, don't worry :) session_file
2535 * is a unique absolute filename. Usually it'll be something like
2536 * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */
2537 i = 0;
2538 argv[i++] = "rm";
2539 argv[i++] = session_file;
2540 argv[i] = NULL;
2541
2542 gnome_client_set_discard_command(client, i, (char **)argv);
2543
2544 /* Tell the session manager how to restore the just saved session.
2545 * This is easily done thanks to Vim's -S option. Pass the -f flag
2546 * since there's no need to fork -- it might even cause confusion.
2547 * Also pass the window role to give the WM something to match on.
2548 * The role is set in gui_mch_open(), thus should _never_ be NULL. */
2549 i = 0;
2550 argv[i++] = restart_command;
2551 argv[i++] = "-f";
2552 argv[i++] = "-g";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 argv[i++] = "--role";
2554 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 argv[i++] = "-S";
2556 argv[i++] = session_file;
2557 argv[i] = NULL;
2558
2559 gnome_client_set_restart_command(client, i, (char **)argv);
2560 gnome_client_set_clone_command(client, 0, NULL);
2561 }
2562
2563 g_free(session_file);
2564
2565 return success;
2566}
2567
2568/*
2569 * Called when the session manager wants us to die. There isn't much to save
2570 * here since "save_yourself" has been emitted before (unless serious trouble
2571 * is happening).
2572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002574sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575{
2576 /* Don't write messages to the GUI anymore */
2577 full_screen = FALSE;
2578
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002579 vim_strncpy(IObuff, (char_u *)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002580 _("Vim: Received \"die\" request from session manager\n"),
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002581 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 preserve_exit();
2583}
2584
2585/*
2586 * Connect our signal handlers to be notified on session save and shutdown.
2587 */
2588 static void
2589setup_save_yourself(void)
2590{
2591 GnomeClient *client;
2592
2593 client = gnome_master_client();
2594
2595 if (client != NULL)
2596 {
2597 /* Must use the deprecated gtk_signal_connect() for compatibility
2598 * with GNOME 1. Arrgh, zombies! */
2599 gtk_signal_connect(GTK_OBJECT(client), "save_yourself",
2600 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL);
2601 gtk_signal_connect(GTK_OBJECT(client), "die",
2602 GTK_SIGNAL_FUNC(&sm_client_die), NULL);
2603 }
2604}
2605
2606#else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2607
2608# ifdef USE_XSMP
2609/*
2610 * GTK tells us that XSMP needs attention
2611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 static gboolean
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002613local_xsmp_handle_requests(
2614 GIOChannel *source UNUSED,
2615 GIOCondition condition,
2616 gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617{
2618 if (condition == G_IO_IN)
2619 {
2620 /* Do stuff; maybe close connection */
2621 if (xsmp_handle_requests() == FAIL)
2622 g_io_channel_unref((GIOChannel *)data);
2623 return TRUE;
2624 }
2625 /* Error */
2626 g_io_channel_unref((GIOChannel *)data);
2627 xsmp_close();
2628 return TRUE;
2629}
2630# endif /* USE_XSMP */
2631
2632/*
2633 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2634 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2635 */
2636 static void
2637setup_save_yourself(void)
2638{
2639 Atom *existing_atoms = NULL;
2640 int count = 0;
2641
Bram Moolenaar98921892016-02-23 17:14:37 +01002642# ifdef USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 if (xsmp_icefd != -1)
2644 {
2645 /*
2646 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2647 * set up GTK IO monitor
2648 */
2649 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd);
2650
2651 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP,
2652 local_xsmp_handle_requests, (gpointer)g_io);
Bram Moolenaar98921892016-02-23 17:14:37 +01002653 g_io_channel_unref(g_io);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 }
2655 else
Bram Moolenaar98921892016-02-23 17:14:37 +01002656# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 {
2658 /* Fall back to old method */
2659
2660 /* first get the existing value */
Bram Moolenaar98921892016-02-23 17:14:37 +01002661# if GTK_CHECK_VERSION(3,0,0)
2662 GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin);
2663
2664 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win),
2665 GDK_WINDOW_XID(mainwin_win),
2666 &existing_atoms, &count))
2667# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2669 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2670 &existing_atoms, &count))
Bram Moolenaar98921892016-02-23 17:14:37 +01002671# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 {
2673 Atom *new_atoms;
2674 Atom save_yourself_xatom;
2675 int i;
2676
2677 save_yourself_xatom = GET_X_ATOM(save_yourself_atom);
2678
2679 /* check if WM_SAVE_YOURSELF isn't there yet */
2680 for (i = 0; i < count; ++i)
2681 if (existing_atoms[i] == save_yourself_xatom)
2682 break;
2683
2684 if (i == count)
2685 {
2686 /* allocate an Atoms array which is one item longer */
2687 new_atoms = (Atom *)alloc((unsigned)((count + 1)
2688 * sizeof(Atom)));
2689 if (new_atoms != NULL)
2690 {
2691 memcpy(new_atoms, existing_atoms, count * sizeof(Atom));
2692 new_atoms[count] = save_yourself_xatom;
Bram Moolenaar98921892016-02-23 17:14:37 +01002693# if GTK_CHECK_VERSION(3,0,0)
2694 XSetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win),
2695 GDK_WINDOW_XID(mainwin_win),
2696# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 XSetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2698 GDK_WINDOW_XWINDOW(gui.mainwin->window),
Bram Moolenaar98921892016-02-23 17:14:37 +01002699# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 new_atoms, count + 1);
2701 vim_free(new_atoms);
2702 }
2703 }
2704 XFree(existing_atoms);
2705 }
2706 }
2707}
2708
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709/*
2710 * Installing a global event filter seems to be the only way to catch
2711 * client messages of type WM_PROTOCOLS without overriding GDK's own
2712 * client message event filter. Well, that's still better than trying
2713 * to guess what the GDK filter had done if it had been invoked instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 *
2715 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2716 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2717 * I have the nasty feeling modern session managers just don't send this
2718 * deprecated message anymore. Addition: confirmed by several people.
2719 *
2720 * The GNOME session support is much cooler anyway. Unlike this ugly
2721 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2722 * it should work with KDE as well.
2723 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 static GdkFilterReturn
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002725global_event_filter(GdkXEvent *xev,
2726 GdkEvent *event UNUSED,
2727 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728{
2729 XEvent *xevent = (XEvent *)xev;
2730
2731 if (xevent != NULL
2732 && xevent->type == ClientMessage
2733 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002734 && (long_u)xevent->xclient.data.l[0]
2735 == GET_X_ATOM(save_yourself_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 {
2737 out_flush();
2738 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2739 /*
2740 * Set the window's WM_COMMAND property, to let the window manager
2741 * know we are done saving ourselves. We don't want to be
2742 * restarted, thus set argv to NULL.
2743 */
Bram Moolenaar98921892016-02-23 17:14:37 +01002744# if GTK_CHECK_VERSION(3,0,0)
2745 XSetCommand(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)),
2746 GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin)),
2747# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2749 GDK_WINDOW_XWINDOW(gui.mainwin->window),
Bram Moolenaar98921892016-02-23 17:14:37 +01002750# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 NULL, 0);
2752 return GDK_FILTER_REMOVE;
2753 }
2754
2755 return GDK_FILTER_CONTINUE;
2756}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757#endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2758
2759
2760/*
2761 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002764mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765{
2766/* If you get an error message here, you still need to unpack the runtime
2767 * archive! */
2768#ifdef magick
2769# undef magick
2770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 /* A bit hackish, but avoids casting later and allows optimization */
2772# define static static const
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773#define magick vim32x32
2774#include "../runtime/vim32x32.xpm"
2775#undef magick
2776#define magick vim16x16
2777#include "../runtime/vim16x16.xpm"
2778#undef magick
2779#define magick vim48x48
2780#include "../runtime/vim48x48.xpm"
2781#undef magick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782# undef static
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783
Bram Moolenaar98921892016-02-23 17:14:37 +01002784#if GTK_CHECK_VERSION(3,0,0)
2785 GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin);
2786#endif
2787
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 /* When started with "--echo-wid" argument, write window ID on stdout. */
2789 if (echo_wid_arg)
2790 {
Bram Moolenaar98921892016-02-23 17:14:37 +01002791#if GTK_CHECK_VERSION(3,0,0)
2792 printf("WID: %ld\n", (long)GDK_WINDOW_XID(mainwin_win));
2793#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 printf("WID: %ld\n", (long)GDK_WINDOW_XWINDOW(gui.mainwin->window));
Bram Moolenaar98921892016-02-23 17:14:37 +01002795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 fflush(stdout);
2797 }
2798
2799 if (vim_strchr(p_go, GO_ICON) != NULL)
2800 {
2801 /*
2802 * Add an icon to the main window. For fun and convenience of the user.
2803 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 GList *icons = NULL;
2805
2806 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16));
2807 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32));
2808 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48));
2809
2810 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons);
2811
2812 g_list_foreach(icons, (GFunc)&g_object_unref, NULL);
2813 g_list_free(icons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 }
2815
2816#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
2817 /* Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 gdk_window_add_filter(NULL, &global_event_filter, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819#endif
2820 /* Setup to indicate to the window manager that we want to catch the
2821 * WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2822 * manager instead. */
2823#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2824 if (using_gnome)
2825#endif
2826 setup_save_yourself();
2827
2828#ifdef FEAT_CLIENTSERVER
2829 if (serverName == NULL && serverDelayedStartName != NULL)
2830 {
2831 /* This is a :gui command in a plain vim with no previous server */
Bram Moolenaar98921892016-02-23 17:14:37 +01002832# if GTK_CHECK_VERSION(3,0,0)
2833 commWindow = GDK_WINDOW_XID(mainwin_win);
2834
2835 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(mainwin_win),
2836 serverDelayedStartName);
2837# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 commWindow = GDK_WINDOW_XWINDOW(gui.mainwin->window);
2839
2840 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2841 serverDelayedStartName);
Bram Moolenaar98921892016-02-23 17:14:37 +01002842# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 }
2844 else
2845 {
2846 /*
2847 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2848 * have to change the "server" registration to that of the main window
2849 * If we have not registered a name yet, remember the window
2850 */
Bram Moolenaar98921892016-02-23 17:14:37 +01002851# if GTK_CHECK_VERSION(3,0,0)
2852 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(mainwin_win),
2853 GDK_WINDOW_XID(mainwin_win));
2854# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2856 GDK_WINDOW_XWINDOW(gui.mainwin->window));
Bram Moolenaar98921892016-02-23 17:14:37 +01002857# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 }
2859 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK);
Bram Moolenaar98921892016-02-23 17:14:37 +01002860# if GTK_CHECK_VERSION(3,0,0)
2861 g_signal_connect(G_OBJECT(gui.mainwin), "property-notify-event",
2862 G_CALLBACK(property_event), NULL);
2863# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "property_notify_event",
2865 GTK_SIGNAL_FUNC(property_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01002866# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867#endif
2868}
2869
2870 static GdkCursor *
2871create_blank_pointer(void)
2872{
2873 GdkWindow *root_window = NULL;
Bram Moolenaar98921892016-02-23 17:14:37 +01002874#if GTK_CHECK_VERSION(3,0,0)
2875 GdkPixbuf *blank_mask;
2876#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 GdkPixmap *blank_mask;
Bram Moolenaar98921892016-02-23 17:14:37 +01002878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 GdkCursor *cursor;
Bram Moolenaar36edf062016-07-21 22:10:12 +02002880#if GTK_CHECK_VERSION(3,0,0)
2881 GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
2882#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 GdkColor color = { 0, 0, 0, 0 };
2884 char blank_data[] = { 0x0 };
Bram Moolenaar98921892016-02-23 17:14:37 +01002885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886
2887#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar98921892016-02-23 17:14:37 +01002888# if GTK_CHECK_VERSION(3,12,0)
2889 {
2890 GdkWindow * const win = gtk_widget_get_window(gui.mainwin);
2891 GdkScreen * const scrn = gdk_window_get_screen(win);
2892 root_window = gdk_screen_get_root_window(scrn);
2893 }
2894# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 root_window = gtk_widget_get_root_window(gui.mainwin);
Bram Moolenaar98921892016-02-23 17:14:37 +01002896# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897#endif
2898
2899 /* Create a pseudo blank pointer, which is in fact one pixel by one pixel
2900 * in size. */
Bram Moolenaar98921892016-02-23 17:14:37 +01002901#if GTK_CHECK_VERSION(3,0,0)
2902 {
2903 cairo_surface_t *surf;
2904 cairo_t *cr;
2905
2906 surf = cairo_image_surface_create(CAIRO_FORMAT_A1, 1, 1);
2907 cr = cairo_create(surf);
2908
Bram Moolenaar36edf062016-07-21 22:10:12 +02002909 cairo_set_source_rgba(cr,
2910 color.red,
2911 color.green,
2912 color.blue,
2913 color.alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01002914 cairo_rectangle(cr, 0, 0, 1, 1);
2915 cairo_fill(cr);
2916 cairo_destroy(cr);
2917
2918 blank_mask = gdk_pixbuf_get_from_surface(surf, 0, 0, 1, 1);
2919 cairo_surface_destroy(surf);
2920
2921 cursor = gdk_cursor_new_from_pixbuf(gdk_window_get_display(root_window),
2922 blank_mask, 0, 0);
2923 g_object_unref(blank_mask);
2924 }
2925#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1);
2927 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask,
2928 &color, &color, 0, 0);
2929 gdk_bitmap_unref(blank_mask);
Bram Moolenaar98921892016-02-23 17:14:37 +01002930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
2932 return cursor;
2933}
2934
2935#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 static void
2937mainwin_screen_changed_cb(GtkWidget *widget,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002938 GdkScreen *previous_screen UNUSED,
2939 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940{
2941 if (!gtk_widget_has_screen(widget))
2942 return;
2943
2944 /*
Bram Moolenaar49325942007-05-10 19:19:59 +00002945 * Recreate the invisible mouse cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 */
2947 if (gui.blank_pointer != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01002948# if GTK_CHECK_VERSION(3,0,0)
2949 g_object_unref(G_OBJECT(gui.blank_pointer));
2950# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 gdk_cursor_unref(gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01002952# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953
2954 gui.blank_pointer = create_blank_pointer();
2955
Bram Moolenaar98921892016-02-23 17:14:37 +01002956# if GTK_CHECK_VERSION(3,0,0)
2957 if (gui.pointer_hidden && gtk_widget_get_window(gui.drawarea) != NULL)
2958 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
2959 gui.blank_pointer);
2960# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 if (gui.pointer_hidden && gui.drawarea->window != NULL)
2962 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01002963# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964
2965 /*
2966 * Create a new PangoContext for this screen, and initialize it
2967 * with the current font if necessary.
2968 */
2969 if (gui.text_context != NULL)
2970 g_object_unref(gui.text_context);
2971
2972 gui.text_context = gtk_widget_create_pango_context(widget);
2973 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
2974
2975 if (gui.norm_font != NULL)
2976 {
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002977 gui_mch_init_font(p_guifont, FALSE);
Bram Moolenaar04a9d452006-03-27 21:03:26 +00002978 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 }
2980}
2981#endif /* HAVE_GTK_MULTIHEAD */
2982
2983/*
2984 * After the drawing area comes up, we calculate all colors and create the
2985 * dummy blank cursor.
2986 *
2987 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2988 * fact that the main VIM engine doesn't take them into account anywhere.
2989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002991drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992{
2993 GtkWidget *sbar;
Bram Moolenaar98921892016-02-23 17:14:37 +01002994#if GTK_CHECK_VERSION(3,0,0)
2995 GtkAllocation allocation;
2996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
2998#ifdef FEAT_XIM
2999 xim_init();
3000#endif
3001 gui_mch_new_colors();
Bram Moolenaar98921892016-02-23 17:14:37 +01003002#if GTK_CHECK_VERSION(3,0,0)
3003 gui.surface = gdk_window_create_similar_surface(
3004 gtk_widget_get_window(widget),
3005 CAIRO_CONTENT_COLOR_ALPHA,
3006 gtk_widget_get_allocated_width(widget),
3007 gtk_widget_get_allocated_height(widget));
3008#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 gui.text_gc = gdk_gc_new(gui.drawarea->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01003010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
3012 gui.blank_pointer = create_blank_pointer();
3013 if (gui.pointer_hidden)
Bram Moolenaar98921892016-02-23 17:14:37 +01003014#if GTK_CHECK_VERSION(3,0,0)
3015 gdk_window_set_cursor(gtk_widget_get_window(widget), gui.blank_pointer);
3016#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 gdk_window_set_cursor(widget->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01003018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019
3020 /* get the actual size of the scrollbars, if they are realized */
3021 sbar = firstwin->w_scrollbars[SBAR_LEFT].id;
3022 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT]
3023 && firstwin->w_scrollbars[SBAR_RIGHT].id))
3024 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id;
Bram Moolenaar98921892016-02-23 17:14:37 +01003025#if GTK_CHECK_VERSION(3,0,0)
3026 gtk_widget_get_allocation(sbar, &allocation);
3027 if (sbar && gtk_widget_get_realized(sbar) && allocation.width)
3028 gui.scrollbar_width = allocation.width;
3029#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.width)
3031 gui.scrollbar_width = sbar->allocation.width;
Bram Moolenaar98921892016-02-23 17:14:37 +01003032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033
3034 sbar = gui.bottom_sbar.id;
Bram Moolenaar98921892016-02-23 17:14:37 +01003035#if GTK_CHECK_VERSION(3,0,0)
3036 if (sbar && gtk_widget_get_realized(sbar) && allocation.height)
3037 gui.scrollbar_height = allocation.height;
3038#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.height)
3040 gui.scrollbar_height = sbar->allocation.height;
Bram Moolenaar98921892016-02-23 17:14:37 +01003041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042}
3043
3044/*
3045 * Properly clean up on shutdown.
3046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003048drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049{
3050 /* Don't write messages to the GUI anymore */
3051 full_screen = FALSE;
3052
3053#ifdef FEAT_XIM
3054 im_shutdown();
3055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 if (gui.ascii_glyphs != NULL)
3057 {
3058 pango_glyph_string_free(gui.ascii_glyphs);
3059 gui.ascii_glyphs = NULL;
3060 }
3061 if (gui.ascii_font != NULL)
3062 {
3063 g_object_unref(gui.ascii_font);
3064 gui.ascii_font = NULL;
3065 }
3066 g_object_unref(gui.text_context);
3067 gui.text_context = NULL;
3068
Bram Moolenaar98921892016-02-23 17:14:37 +01003069#if GTK_CHECK_VERSION(3,0,0)
3070 if (gui.surface != NULL)
3071 {
3072 cairo_surface_destroy(gui.surface);
3073 gui.surface = NULL;
3074 }
3075#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 g_object_unref(gui.text_gc);
3077 gui.text_gc = NULL;
Bram Moolenaar98921892016-02-23 17:14:37 +01003078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079
Bram Moolenaar98921892016-02-23 17:14:37 +01003080#if GTK_CHECK_VERSION(3,0,0)
3081 g_object_unref(G_OBJECT(gui.blank_pointer));
3082#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 gdk_cursor_unref(gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01003084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 gui.blank_pointer = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086}
3087
Bram Moolenaara859f042016-11-17 19:11:55 +01003088#if GTK_CHECK_VERSION(3,22,2)
3089 static void
3090drawarea_style_updated_cb(GtkWidget *widget UNUSED,
3091 gpointer data UNUSED)
3092#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003094drawarea_style_set_cb(GtkWidget *widget UNUSED,
3095 GtkStyle *previous_style UNUSED,
3096 gpointer data UNUSED)
Bram Moolenaara859f042016-11-17 19:11:55 +01003097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098{
3099 gui_mch_new_colors();
3100}
3101
Bram Moolenaar98921892016-02-23 17:14:37 +01003102#if GTK_CHECK_VERSION(3,0,0)
3103 static gboolean
3104drawarea_configure_event_cb(GtkWidget *widget,
3105 GdkEventConfigure *event,
3106 gpointer data UNUSED)
3107{
3108 static int cur_width = 0;
3109 static int cur_height = 0;
3110
3111 g_return_val_if_fail(event
3112 && event->width >= 1 && event->height >= 1, TRUE);
3113
Bram Moolenaar182707a2016-11-21 20:55:58 +01003114# if GTK_CHECK_VERSION(3,22,2) && !GTK_CHECK_VERSION(3,22,4)
Bram Moolenaara859f042016-11-17 19:11:55 +01003115 /* As of 3.22.2, GdkWindows have started distributing configure events to
3116 * their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0).
3117 *
3118 * As can be seen from the implementation of move_native_children() and
3119 * configure_native_child() in gdkwindow.c, those functions actually
3120 * propagate configure events to every child, failing to distinguish
3121 * "native" one from non-native one.
3122 *
3123 * Naturally, configure events propagated to here like that are fallacious
3124 * and, as a matter of fact, they trigger a geometric collapse of
3125 * gui.drawarea in fullscreen and miximized modes.
3126 *
3127 * To filter out such nuisance events, we are making use of the fact that
3128 * the field send_event of such GdkEventConfigures is set to FALSE in
3129 * configure_native_child().
3130 *
3131 * Obviously, this is a terrible hack making GVim depend on GTK's
3132 * implementation details. Therefore, watch out any relevant internal
3133 * changes happening in GTK in the feature (sigh).
3134 */
Bram Moolenaar182707a2016-11-21 20:55:58 +01003135 /* Follow-up
3136 * After a few weeks later, the GdkWindow change mentioned above was
3137 * reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155).
3138 * The corresponding official release is 3.22.4. */
Bram Moolenaara859f042016-11-17 19:11:55 +01003139 if (event->send_event == FALSE)
3140 return TRUE;
3141# endif
3142
Bram Moolenaar98921892016-02-23 17:14:37 +01003143 if (event->width == cur_width && event->height == cur_height)
3144 return TRUE;
3145
3146 cur_width = event->width;
3147 cur_height = event->height;
3148
3149 if (gui.surface != NULL)
3150 cairo_surface_destroy(gui.surface);
3151
3152 gui.surface = gdk_window_create_similar_surface(
3153 gtk_widget_get_window(widget),
3154 CAIRO_CONTENT_COLOR_ALPHA,
3155 event->width, event->height);
3156
3157 gtk_widget_queue_draw(widget);
3158
3159 return TRUE;
3160}
3161#endif
3162
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163/*
3164 * Callback routine for the "delete_event" signal on the toplevel window.
3165 * Tries to vim gracefully, or refuses to exit with changed buffers.
3166 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003168delete_event_cb(GtkWidget *widget UNUSED,
3169 GdkEventAny *event UNUSED,
3170 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171{
3172 gui_shell_closed();
3173 return TRUE;
3174}
3175
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003176#if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
3177 static int
3178get_item_dimensions(GtkWidget *widget, GtkOrientation orientation)
3179{
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003180# ifdef FEAT_GUI_GNOME
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003181 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL;
3182
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003183 if (using_gnome && widget != NULL)
3184 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003185 GtkWidget *parent;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003186 BonoboDockItem *dockitem;
3187
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003188 parent = gtk_widget_get_parent(widget);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003189 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM)
3190 {
3191 /* Only menu & toolbar are dock items. Could tabline be?
3192 * Seem to be only the 2 defined in GNOME */
3193 widget = parent;
3194 dockitem = BONOBO_DOCK_ITEM(widget);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003195
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003196 if (dockitem == NULL || dockitem->is_floating)
3197 return 0;
3198 item_orientation = bonobo_dock_item_get_orientation(dockitem);
3199 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003200 }
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003201# else
3202# define item_orientation GTK_ORIENTATION_HORIZONTAL
Bram Moolenaar98921892016-02-23 17:14:37 +01003203# endif
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003204
Bram Moolenaar98921892016-02-23 17:14:37 +01003205# if GTK_CHECK_VERSION(3,0,0)
3206 if (widget != NULL
3207 && item_orientation == orientation
3208 && gtk_widget_get_realized(widget)
3209 && gtk_widget_get_visible(widget))
3210# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003211 if (widget != NULL
3212 && item_orientation == orientation
3213 && GTK_WIDGET_REALIZED(widget)
3214 && GTK_WIDGET_VISIBLE(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01003215# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003216 {
Bram Moolenaar98921892016-02-23 17:14:37 +01003217# if GTK_CHECK_VERSION(3,0,0)
3218 GtkAllocation allocation;
3219
3220 gtk_widget_get_allocation(widget, &allocation);
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003221 return allocation.height;
Bram Moolenaar98921892016-02-23 17:14:37 +01003222# else
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003223# ifdef FEAT_GUI_GNOME
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003224 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3225 return widget->allocation.height;
3226 else
3227 return widget->allocation.width;
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003228# else
3229 return widget->allocation.height;
3230# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01003231# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003232 }
3233 return 0;
3234}
3235#endif
3236
3237 static int
3238get_menu_tool_width(void)
3239{
3240 int width = 0;
3241
3242#ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */
3243# ifdef FEAT_MENU
3244 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL);
3245# endif
3246# ifdef FEAT_TOOLBAR
3247 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL);
3248# endif
3249# ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003250 if (gui.tabline != NULL)
3251 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003252# endif
3253#endif
3254
3255 return width;
3256}
3257
3258 static int
3259get_menu_tool_height(void)
3260{
3261 int height = 0;
3262
3263#ifdef FEAT_MENU
3264 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL);
3265#endif
3266#ifdef FEAT_TOOLBAR
3267 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL);
3268#endif
3269#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003270 if (gui.tabline != NULL)
3271 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003272#endif
3273
3274 return height;
3275}
3276
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003277/* This controls whether we can set the real window hints at
3278 * start-up when in a GtkPlug.
3279 * 0 = normal processing (default)
3280 * 1 = init. hints set, no-one's tried to reset since last check
3281 * 2 = init. hints set, attempt made to change hints
3282 */
3283static int init_window_hints_state = 0;
3284
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003285 static void
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003286update_window_manager_hints(int force_width, int force_height)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003287{
3288 static int old_width = 0;
3289 static int old_height = 0;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003290 static int old_min_width = 0;
3291 static int old_min_height = 0;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003292 static int old_char_width = 0;
3293 static int old_char_height = 0;
3294
3295 int width;
3296 int height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003297 int min_width;
3298 int min_height;
3299
3300 /* At start-up, don't try to set the hints until the initial
3301 * values have been used (those that dictate our initial size)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003302 * Let forced (i.e., correct) values through always.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003303 */
3304 if (!(force_width && force_height) && init_window_hints_state > 0)
3305 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003306 /* Don't do it! */
3307 init_window_hints_state = 2;
3308 return;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003309 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003310
3311 /* This also needs to be done when the main window isn't there yet,
3312 * otherwise the hints don't work. */
3313 width = gui_get_base_width();
3314 height = gui_get_base_height();
3315# ifdef FEAT_MENU
3316 height += tabline_height() * gui.char_height;
3317# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003318 width += get_menu_tool_width();
3319 height += get_menu_tool_height();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003320
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003321 /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
Bram Moolenaar49325942007-05-10 19:19:59 +00003322 * their actual widget size. When we set our size ourselves (e.g.,
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003323 * 'set columns=' or init. -geom) we briefly set the min. to the size
3324 * we wish to be instead of the legitimate minimum so that we actually
3325 * resize correctly.
3326 */
3327 if (force_width && force_height)
3328 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003329 min_width = force_width;
3330 min_height = force_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003331 }
3332 else
3333 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003334 min_width = width + MIN_COLUMNS * gui.char_width;
3335 min_height = height + MIN_LINES * gui.char_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003336 }
3337
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003338 /* Avoid an expose event when the size didn't change. */
3339 if (width != old_width
3340 || height != old_height
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003341 || min_width != old_min_width
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003342 || min_height != old_min_height
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003343 || gui.char_width != old_char_width
3344 || gui.char_height != old_char_height)
3345 {
3346 GdkGeometry geometry;
3347 GdkWindowHints geometry_mask;
3348
3349 geometry.width_inc = gui.char_width;
3350 geometry.height_inc = gui.char_height;
3351 geometry.base_width = width;
3352 geometry.base_height = height;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003353 geometry.min_width = min_width;
3354 geometry.min_height = min_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003355 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC
3356 |GDK_HINT_MIN_SIZE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003357 /* Using gui.formwin as geometry widget doesn't work as expected
3358 * with GTK+ 2 -- dunno why. Presumably all the resizing hacks
3359 * in Vim confuse GTK+. */
3360 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin,
3361 &geometry, geometry_mask);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003362 old_width = width;
3363 old_height = height;
3364 old_min_width = min_width;
3365 old_min_height = min_height;
3366 old_char_width = gui.char_width;
3367 old_char_height = gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003368 }
3369}
3370
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371#ifdef FEAT_TOOLBAR
3372
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373/*
3374 * This extra effort wouldn't be necessary if we only used stock icons in the
3375 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
3376 * shouldn't be treated differently, thus we do need this.
3377 */
3378 static void
3379icon_size_changed_foreach(GtkWidget *widget, gpointer user_data)
3380{
3381 if (GTK_IS_IMAGE(widget))
3382 {
3383 GtkImage *image = (GtkImage *)widget;
3384
Bram Moolenaar98921892016-02-23 17:14:37 +01003385# if GTK_CHECK_VERSION(3,10,0)
3386 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_NAME)
3387 {
3388 const GtkIconSize icon_size = GPOINTER_TO_INT(user_data);
3389 const gchar *icon_name;
3390
3391 gtk_image_get_icon_name(image, &icon_name, NULL);
3392
3393 gtk_image_set_from_icon_name(image, icon_name, icon_size);
3394 }
3395# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 /* User-defined icons are stored in a GtkIconSet */
3397 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET)
3398 {
3399 GtkIconSet *icon_set;
3400 GtkIconSize icon_size;
3401
3402 gtk_image_get_icon_set(image, &icon_set, &icon_size);
3403 icon_size = (GtkIconSize)(long)user_data;
3404
3405 gtk_icon_set_ref(icon_set);
3406 gtk_image_set_from_icon_set(image, icon_set, icon_size);
3407 gtk_icon_set_unref(icon_set);
3408 }
Bram Moolenaar98921892016-02-23 17:14:37 +01003409# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 }
3411 else if (GTK_IS_CONTAINER(widget))
3412 {
3413 gtk_container_foreach((GtkContainer *)widget,
3414 &icon_size_changed_foreach,
3415 user_data);
3416 }
3417}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418
3419 static void
3420set_toolbar_style(GtkToolbar *toolbar)
3421{
3422 GtkToolbarStyle style;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 GtkIconSize size;
3424 GtkIconSize oldsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3427 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3428 style = GTK_TOOLBAR_BOTH_HORIZ;
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003429 else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 == (TOOLBAR_TEXT | TOOLBAR_ICONS))
3431 style = GTK_TOOLBAR_BOTH;
3432 else if (toolbar_flags & TOOLBAR_TEXT)
3433 style = GTK_TOOLBAR_TEXT;
3434 else
3435 style = GTK_TOOLBAR_ICONS;
3436
3437 gtk_toolbar_set_style(toolbar, style);
Bram Moolenaar98921892016-02-23 17:14:37 +01003438# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003440# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 switch (tbis_flags)
3443 {
3444 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break;
3445 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break;
3446 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break;
3447 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
Bram Moolenaarbeb003b2016-03-08 22:47:17 +01003448 case TBIS_HUGE: size = GTK_ICON_SIZE_DND; break;
3449 case TBIS_GIANT: size = GTK_ICON_SIZE_DIALOG; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 default: size = GTK_ICON_SIZE_INVALID; break;
3451 }
3452 oldsize = gtk_toolbar_get_icon_size(toolbar);
3453
3454 if (size == GTK_ICON_SIZE_INVALID)
3455 {
3456 /* Let global user preferences decide the icon size. */
3457 gtk_toolbar_unset_icon_size(toolbar);
3458 size = gtk_toolbar_get_icon_size(toolbar);
3459 }
3460 if (size != oldsize)
3461 {
3462 gtk_container_foreach(GTK_CONTAINER(toolbar),
3463 &icon_size_changed_foreach,
3464 GINT_TO_POINTER((int)size));
3465 }
3466 gtk_toolbar_set_icon_size(toolbar, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467}
3468
3469#endif /* FEAT_TOOLBAR */
3470
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003471#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3472static int ignore_tabline_evt = FALSE;
Bram Moolenaara5621492006-02-25 21:55:24 +00003473static GtkWidget *tabline_menu;
Bram Moolenaar98921892016-02-23 17:14:37 +01003474# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003475static GtkTooltips *tabline_tooltip;
Bram Moolenaar98921892016-02-23 17:14:37 +01003476# endif
Bram Moolenaara5621492006-02-25 21:55:24 +00003477static int clicked_page; /* page clicked in tab line */
3478
3479/*
3480 * Handle selecting an item in the tab line popup menu.
3481 */
Bram Moolenaara5621492006-02-25 21:55:24 +00003482 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003483tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data)
Bram Moolenaara5621492006-02-25 21:55:24 +00003484{
Bram Moolenaara5621492006-02-25 21:55:24 +00003485 /* Add the string cmd into input buffer */
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003486 send_tabline_menu_event(clicked_page, (int)(long)user_data);
Bram Moolenaara5621492006-02-25 21:55:24 +00003487}
3488
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003489 static void
3490add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp)
3491{
3492 GtkWidget *item;
3493 char_u *utf_text;
3494
3495 utf_text = CONVERT_TO_UTF8(text);
3496 item = gtk_menu_item_new_with_label((const char *)utf_text);
3497 gtk_widget_show(item);
3498 CONVERT_TO_UTF8_FREE(utf_text);
3499
3500 gtk_container_add(GTK_CONTAINER(menu), item);
Bram Moolenaar98921892016-02-23 17:14:37 +01003501# if GTK_CHECK_VERSION(3,0,0)
3502 g_signal_connect(G_OBJECT(item), "activate",
3503 G_CALLBACK(tabline_menu_handler),
3504 GINT_TO_POINTER(resp));
3505# else
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003506 gtk_signal_connect(GTK_OBJECT(item), "activate",
3507 GTK_SIGNAL_FUNC(tabline_menu_handler),
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003508 (gpointer)(long)resp);
Bram Moolenaar98921892016-02-23 17:14:37 +01003509# endif
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003510}
3511
Bram Moolenaara5621492006-02-25 21:55:24 +00003512/*
Bram Moolenaara5621492006-02-25 21:55:24 +00003513 * Create a menu for the tab line.
3514 */
3515 static GtkWidget *
3516create_tabline_menu(void)
3517{
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003518 GtkWidget *menu;
Bram Moolenaara5621492006-02-25 21:55:24 +00003519
3520 menu = gtk_menu_new();
Bram Moolenaar7098ee52015-06-09 19:14:24 +02003521 if (first_tabpage->tp_next != NULL)
3522 add_tabline_menu_item(menu, (char_u *)_("Close tab"),
3523 TABLINE_MENU_CLOSE);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003524 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW);
3525 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN);
Bram Moolenaara5621492006-02-25 21:55:24 +00003526
3527 return menu;
3528}
3529
3530 static gboolean
3531on_tabline_menu(GtkWidget *widget, GdkEvent *event)
3532{
3533 /* Was this button press event ? */
3534 if (event->type == GDK_BUTTON_PRESS)
3535 {
3536 GdkEventButton *bevent = (GdkEventButton *)event;
3537 int x = bevent->x;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003538 int y = bevent->y;
3539 GtkWidget *tabwidget;
3540 GdkWindow *tabwin;
Bram Moolenaara5621492006-02-25 21:55:24 +00003541
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003542 /* When ignoring events return TRUE so that the selected page doesn't
3543 * change. */
3544 if (hold_gui_events
3545# ifdef FEAT_CMDWIN
3546 || cmdwin_type != 0
3547# endif
3548 )
3549 return TRUE;
3550
Bram Moolenaar98921892016-02-23 17:14:37 +01003551# if GTK_CHECK_VERSION(3,0,0)
3552 tabwin = gui_gtk_window_at_position(gui.mainwin, &x, &y);
3553# else
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003554 tabwin = gdk_window_at_pointer(&x, &y);
Bram Moolenaar98921892016-02-23 17:14:37 +01003555# endif
3556
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003557 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget);
Bram Moolenaar98921892016-02-23 17:14:37 +01003558# if GTK_CHECK_VERSION(3,0,0)
3559 clicked_page = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(tabwidget),
3560 "tab_num"));
3561# else
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003562 clicked_page = (int)(long)gtk_object_get_user_data(
3563 GTK_OBJECT(tabwidget));
Bram Moolenaar98921892016-02-23 17:14:37 +01003564# endif
Bram Moolenaara5621492006-02-25 21:55:24 +00003565
3566 /* If the event was generated for 3rd button popup the menu. */
3567 if (bevent->button == 3)
3568 {
Bram Moolenaara859f042016-11-17 19:11:55 +01003569# if GTK_CHECK_VERSION(3,22,2)
3570 gtk_menu_popup_at_pointer(GTK_MENU(widget), event);
3571# else
Bram Moolenaara5621492006-02-25 21:55:24 +00003572 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
3573 bevent->button, bevent->time);
Bram Moolenaara859f042016-11-17 19:11:55 +01003574# endif
Bram Moolenaara5621492006-02-25 21:55:24 +00003575 /* We handled the event. */
3576 return TRUE;
3577 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003578 else if (bevent->button == 1)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003579 {
Bram Moolenaar96351572006-05-05 21:16:59 +00003580 if (clicked_page == 0)
3581 {
Bram Moolenaar07354542007-09-15 12:07:46 +00003582 /* Click after all tabs moves to next tab page. When "x" is
3583 * small guess it's the left button. */
Bram Moolenaara3f41662010-07-11 19:01:06 +02003584 send_tabline_event(x < 50 ? -1 : 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00003585 }
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003586 }
Bram Moolenaara5621492006-02-25 21:55:24 +00003587 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003588
Bram Moolenaara5621492006-02-25 21:55:24 +00003589 /* We didn't handle the event. */
3590 return FALSE;
3591}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003592
3593/*
3594 * Handle selecting one of the tabs.
3595 */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003596 static void
3597on_select_tab(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003598 GtkNotebook *notebook UNUSED,
Bram Moolenaar98921892016-02-23 17:14:37 +01003599# if GTK_CHECK_VERSION(3,0,0)
3600 gpointer *page UNUSED,
3601# else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003602 GtkNotebookPage *page UNUSED,
Bram Moolenaar98921892016-02-23 17:14:37 +01003603# endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00003604 gint idx,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003605 gpointer data UNUSED)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003606{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003607 if (!ignore_tabline_evt)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003608 {
Bram Moolenaara3f41662010-07-11 19:01:06 +02003609 send_tabline_event(idx + 1);
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003610 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003611}
3612
3613/*
3614 * Show or hide the tabline.
3615 */
3616 void
3617gui_mch_show_tabline(int showit)
3618{
3619 if (gui.tabline == NULL)
3620 return;
3621
3622 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)))
3623 {
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003624 /* Note: this may cause a resize event */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003625 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003626 update_window_manager_hints(0, 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00003627 if (showit)
Bram Moolenaar98921892016-02-23 17:14:37 +01003628# if GTK_CHECK_VERSION(3,0,0)
3629 gtk_widget_set_can_focus(GTK_WIDGET(gui.tabline), FALSE);
3630# else
Bram Moolenaar96351572006-05-05 21:16:59 +00003631 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui.tabline), GTK_CAN_FOCUS);
Bram Moolenaar98921892016-02-23 17:14:37 +01003632# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003633 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003634
3635 gui_mch_update();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003636}
3637
3638/*
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003639 * Return TRUE when tabline is displayed.
3640 */
3641 int
3642gui_mch_showing_tabline(void)
3643{
3644 return gui.tabline != NULL
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003645 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline));
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003646}
3647
3648/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003649 * Update the labels of the tabline.
3650 */
3651 void
3652gui_mch_update_tabline(void)
3653{
3654 GtkWidget *page;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003655 GtkWidget *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003656 GtkWidget *label;
3657 tabpage_T *tp;
3658 int nr = 0;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003659 int tab_num;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003660 int curtabidx = 0;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003661 char_u *labeltext;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003662
3663 if (gui.tabline == NULL)
3664 return;
3665
3666 ignore_tabline_evt = TRUE;
3667
3668 /* Add a label for each tab page. They all contain the same text area. */
3669 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3670 {
3671 if (tp == curtab)
3672 curtabidx = nr;
3673
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003674 tab_num = nr + 1;
3675
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003676 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr);
3677 if (page == NULL)
3678 {
3679 /* Add notebook page */
Bram Moolenaar98921892016-02-23 17:14:37 +01003680# if GTK_CHECK_VERSION(3,2,0)
3681 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3682 gtk_box_set_homogeneous(GTK_BOX(page), FALSE);
3683# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003684 page = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003685# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003686 gtk_widget_show(page);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003687 event_box = gtk_event_box_new();
3688 gtk_widget_show(event_box);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003689 label = gtk_label_new("-Empty-");
Bram Moolenaar98921892016-02-23 17:14:37 +01003690# if !GTK_CHECK_VERSION(3,14,0)
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003691 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01003692# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003693 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003694 gtk_widget_show(label);
3695 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline),
3696 page,
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003697 event_box,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003698 nr++);
3699 }
3700
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003701 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page);
Bram Moolenaar98921892016-02-23 17:14:37 +01003702# if GTK_CHECK_VERSION(3,0,0)
3703 g_object_set_data(G_OBJECT(event_box), "tab_num",
3704 GINT_TO_POINTER(tab_num));
3705# else
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003706 gtk_object_set_user_data(GTK_OBJECT(event_box),
3707 (gpointer)(long)tab_num);
Bram Moolenaar98921892016-02-23 17:14:37 +01003708# endif
3709# if GTK_CHECK_VERSION(3,0,0)
3710 label = gtk_bin_get_child(GTK_BIN(event_box));
3711# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003712 label = GTK_BIN(event_box)->child;
Bram Moolenaar98921892016-02-23 17:14:37 +01003713# endif
Bram Moolenaar57657d82006-04-21 22:12:41 +00003714 get_tabline_label(tp, FALSE);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003715 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003716 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003717 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003718
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003719 get_tabline_label(tp, TRUE);
3720 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar98921892016-02-23 17:14:37 +01003721# if GTK_CHECK_VERSION(3,0,0)
3722 gtk_widget_set_tooltip_text(event_box, (const gchar *)labeltext);
3723# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003724 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box,
3725 (const char *)labeltext, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01003726# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003727 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003728 }
3729
3730 /* Remove any old labels. */
3731 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL)
3732 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr);
3733
Bram Moolenaar98921892016-02-23 17:14:37 +01003734# if GTK_CHECK_VERSION(3,0,0)
3735 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
3736 gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
3737# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003738 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003739 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
Bram Moolenaar98921892016-02-23 17:14:37 +01003740# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003741
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003742 /* Make sure everything is in place before drawing text. */
3743 gui_mch_update();
3744
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003745 ignore_tabline_evt = FALSE;
3746}
3747
3748/*
3749 * Set the current tab to "nr". First tab is 1.
3750 */
3751 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003752gui_mch_set_curtab(int nr)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003753{
3754 if (gui.tabline == NULL)
3755 return;
3756
3757 ignore_tabline_evt = TRUE;
Bram Moolenaar98921892016-02-23 17:14:37 +01003758# if GTK_CHECK_VERSION(3,0,0)
3759 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
3760 gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
3761# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003762 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003763 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
Bram Moolenaar98921892016-02-23 17:14:37 +01003764# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003765 ignore_tabline_evt = FALSE;
3766}
3767
3768#endif /* FEAT_GUI_TABLINE */
3769
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770/*
Bram Moolenaara76638f2010-06-05 12:49:46 +02003771 * Add selection targets for PRIMARY and CLIPBOARD selections.
3772 */
3773 void
3774gui_gtk_set_selection_targets(void)
3775{
3776 int i, j = 0;
3777 int n_targets = N_SELECTION_TARGETS;
3778 GtkTargetEntry targets[N_SELECTION_TARGETS];
3779
3780 for (i = 0; i < (int)N_SELECTION_TARGETS; ++i)
3781 {
Bram Moolenaar29695702012-05-18 17:03:18 +02003782 /* OpenOffice tries to use TARGET_HTML and fails when we don't
Bram Moolenaara76638f2010-06-05 12:49:46 +02003783 * return something, instead of trying another target. Therefore only
3784 * offer TARGET_HTML when it works. */
3785 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3786 n_targets--;
3787 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003788 targets[j++] = selection_targets[i];
3789 }
3790
3791 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)GDK_SELECTION_PRIMARY);
3792 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)clip_plus.gtk_sel_atom);
3793 gtk_selection_add_targets(gui.drawarea,
3794 (GdkAtom)GDK_SELECTION_PRIMARY,
3795 targets, n_targets);
3796 gtk_selection_add_targets(gui.drawarea,
3797 (GdkAtom)clip_plus.gtk_sel_atom,
3798 targets, n_targets);
3799}
3800
3801/*
3802 * Set up for receiving DND items.
3803 */
3804 void
3805gui_gtk_set_dnd_targets(void)
3806{
3807 int i, j = 0;
3808 int n_targets = N_DND_TARGETS;
3809 GtkTargetEntry targets[N_DND_TARGETS];
3810
3811 for (i = 0; i < (int)N_DND_TARGETS; ++i)
3812 {
Bram Moolenaarb931d742011-10-26 11:36:25 +02003813 if (!clip_html && dnd_targets[i].info == TARGET_HTML)
Bram Moolenaara76638f2010-06-05 12:49:46 +02003814 n_targets--;
3815 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003816 targets[j++] = dnd_targets[i];
3817 }
3818
3819 gtk_drag_dest_unset(gui.drawarea);
3820 gtk_drag_dest_set(gui.drawarea,
3821 GTK_DEST_DEFAULT_ALL,
3822 targets, n_targets,
Bram Moolenaarba7cc9f2011-02-25 17:10:27 +01003823 GDK_ACTION_COPY | GDK_ACTION_MOVE);
Bram Moolenaara76638f2010-06-05 12:49:46 +02003824}
3825
3826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3828 * Returns OK for success, FAIL when the GUI can't be started.
3829 */
3830 int
3831gui_mch_init(void)
3832{
3833 GtkWidget *vbox;
3834
3835#ifdef FEAT_GUI_GNOME
3836 /* Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3837 * exits on failure, but that's a non-issue because we already called
3838 * gtk_init_check() in gui_mch_init_check(). */
3839 if (using_gnome)
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003840 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT,
3842 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL);
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003843# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
Bram Moolenaar1ff32c52014-04-29 15:11:43 +02003844 {
3845 char *p = setlocale(LC_NUMERIC, NULL);
3846
3847 /* Make sure strtod() uses a decimal point, not a comma. Gnome
3848 * init may change it. */
3849 if (p == NULL || strcmp(p, "C") != 0)
3850 setlocale(LC_NUMERIC, "C");
3851 }
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003852# endif
3853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854#endif
3855 vim_free(gui_argv);
3856 gui_argv = NULL;
3857
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003858#if GLIB_CHECK_VERSION(2,1,3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 /* Set the human-readable application name */
3860 g_set_application_name("Vim");
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 /*
3863 * Force UTF-8 output no matter what the value of 'encoding' is.
3864 * did_set_string_option() in option.c prohibits changing 'termencoding'
3865 * to something else than UTF-8 if the GUI is in use.
3866 */
3867 set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0);
3868
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003869#ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 gui_gtk_register_stock_icons();
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 /* FIXME: Need to install the classic icons and a gtkrc.classic file.
3873 * The hard part is deciding install locations and the Makefile magic. */
Bram Moolenaar98921892016-02-23 17:14:37 +01003874#if !GTK_CHECK_VERSION(3,0,0)
3875# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 gtk_rc_parse("gtkrc");
Bram Moolenaar98921892016-02-23 17:14:37 +01003877# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878#endif
3879
3880 /* Initialize values */
3881 gui.border_width = 2;
3882 gui.scrollbar_width = SB_DEFAULT_WIDTH;
3883 gui.scrollbar_height = SB_DEFAULT_WIDTH;
Bram Moolenaar36edf062016-07-21 22:10:12 +02003884#if GTK_CHECK_VERSION(3,0,0)
3885 gui.fgcolor = g_new(GdkRGBA, 1);
3886 gui.bgcolor = g_new(GdkRGBA, 1);
3887 gui.spcolor = g_new(GdkRGBA, 1);
3888#else
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003889 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 gui.fgcolor = g_new0(GdkColor, 1);
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003891 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 gui.bgcolor = g_new0(GdkColor, 1);
Bram Moolenaarf36d3692005-03-15 22:48:14 +00003893 /* LINTED: avoid warning: conversion to 'unsigned long' */
3894 gui.spcolor = g_new0(GdkColor, 1);
Bram Moolenaar36edf062016-07-21 22:10:12 +02003895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896
3897 /* Initialise atoms */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00003898 html_atom = gdk_atom_intern("text/html", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900
3901 /* Set default foreground and background colors. */
3902 gui.norm_pixel = gui.def_norm_pixel;
3903 gui.back_pixel = gui.def_back_pixel;
3904
3905 if (gtk_socket_id != 0)
3906 {
3907 GtkWidget *plug;
3908
3909 /* Use GtkSocket from another app. */
3910#ifdef HAVE_GTK_MULTIHEAD
3911 plug = gtk_plug_new_for_display(gdk_display_get_default(),
3912 gtk_socket_id);
3913#else
3914 plug = gtk_plug_new(gtk_socket_id);
3915#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01003916#if GTK_CHECK_VERSION(3,0,0)
3917 if (plug != NULL && gtk_plug_get_socket_window(GTK_PLUG(plug)) != NULL)
3918#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 if (plug != NULL && GTK_PLUG(plug)->socket_window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01003920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 {
3922 gui.mainwin = plug;
3923 }
3924 else
3925 {
3926 g_warning("Connection to GTK+ socket (ID %u) failed",
3927 (unsigned int)gtk_socket_id);
3928 /* Pretend we never wanted it if it failed (get own window) */
3929 gtk_socket_id = 0;
3930 }
3931 }
3932
3933 if (gtk_socket_id == 0)
3934 {
3935#ifdef FEAT_GUI_GNOME
3936 if (using_gnome)
3937 {
3938 gui.mainwin = gnome_app_new("Vim", NULL);
3939# ifdef USE_XSMP
3940 /* Use the GNOME save-yourself functionality now. */
3941 xsmp_close();
3942# endif
3943 }
3944 else
3945#endif
3946 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3947 }
3948
3949 gtk_widget_set_name(gui.mainwin, "vim-main-window");
3950
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 /* Create the PangoContext used for drawing all text. */
3952 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
3953 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954
Bram Moolenaar98921892016-02-23 17:14:37 +01003955#if GTK_CHECK_VERSION(3,0,0)
3956 gtk_container_set_border_width(GTK_CONTAINER(gui.mainwin), 0);
3957#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 gtk_container_border_width(GTK_CONTAINER(gui.mainwin), 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK);
3961
Bram Moolenaar98921892016-02-23 17:14:37 +01003962#if GTK_CHECK_VERSION(3,0,0)
3963 g_signal_connect(G_OBJECT(gui.mainwin), "delete-event",
3964 G_CALLBACK(&delete_event_cb), NULL);
3965
3966 g_signal_connect(G_OBJECT(gui.mainwin), "realize",
3967 G_CALLBACK(&mainwin_realize), NULL);
3968#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "delete_event",
3970 GTK_SIGNAL_FUNC(&delete_event_cb), NULL);
3971
3972 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "realize",
3973 GTK_SIGNAL_FUNC(&mainwin_realize), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01003974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975#ifdef HAVE_GTK_MULTIHEAD
3976 g_signal_connect(G_OBJECT(gui.mainwin), "screen_changed",
3977 G_CALLBACK(&mainwin_screen_changed_cb), NULL);
3978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 gui.accel_group = gtk_accel_group_new();
3980 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003982 /* A vertical box holds the menubar, toolbar and main text window. */
Bram Moolenaar98921892016-02-23 17:14:37 +01003983#if GTK_CHECK_VERSION(3,2,0)
3984 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3985 gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE);
3986#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 vbox = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989
3990#ifdef FEAT_GUI_GNOME
3991 if (using_gnome)
3992 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003993# if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 /* automagically restore menubar/toolbar placement */
3995 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE);
3996# endif
3997 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox);
3998 }
3999 else
4000#endif
4001 {
4002 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox);
4003 gtk_widget_show(vbox);
4004 }
4005
4006#ifdef FEAT_MENU
4007 /*
4008 * Create the menubar and handle
4009 */
4010 gui.menubar = gtk_menu_bar_new();
4011 gtk_widget_set_name(gui.menubar, "vim-menubar");
4012
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004013 /* Avoid that GTK takes <F10> away from us. */
4014 {
4015 GtkSettings *gtk_settings;
4016
4017 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
4018 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
4019 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004020
4021
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022# ifdef FEAT_GUI_GNOME
4023 if (using_gnome)
4024 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 BonoboDockItem *dockitem;
4026
4027 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar));
4028 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
4029 GNOME_APP_MENUBAR_NAME);
Bram Moolenaardb552d602006-03-23 22:59:57 +00004030 /* We don't want the menu to float. */
4031 bonobo_dock_item_set_behavior(dockitem,
4032 bonobo_dock_item_get_behavior(dockitem)
4033 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 gui.menubar_h = GTK_WIDGET(dockitem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 }
4036 else
4037# endif /* FEAT_GUI_GNOME */
4038 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00004039 /* Always show the menubar, otherwise <F10> doesn't work. It may be
4040 * disabled in gui_init() later. */
4041 gtk_widget_show(gui.menubar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0);
4043 }
4044#endif /* FEAT_MENU */
4045
4046#ifdef FEAT_TOOLBAR
4047 /*
4048 * Create the toolbar and handle
4049 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 /* some aesthetics on the toolbar */
Bram Moolenaar98921892016-02-23 17:14:37 +01004051# ifdef USE_GTK3
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01004052 /* TODO: Add GTK+ 3 code here using GtkCssProvider if necessary. */
Bram Moolenaar98921892016-02-23 17:14:37 +01004053 /* N.B. Since the default value of GtkToolbar::button-relief is
4054 * GTK_RELIEF_NONE, there's no need to specify that, probably. */
4055# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 gtk_rc_parse_string(
4057 "style \"vim-toolbar-style\" {\n"
4058 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
4059 "}\n"
4060 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
Bram Moolenaar98921892016-02-23 17:14:37 +01004061# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 gui.toolbar = gtk_toolbar_new();
4063 gtk_widget_set_name(gui.toolbar, "vim-toolbar");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
4065
4066# ifdef FEAT_GUI_GNOME
4067 if (using_gnome)
4068 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 BonoboDockItem *dockitem;
4070
4071 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar));
4072 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
4073 GNOME_APP_TOOLBAR_NAME);
4074 gui.toolbar_h = GTK_WIDGET(dockitem);
Bram Moolenaare580b0c2006-03-21 21:33:03 +00004075 /* When the toolbar is floating it gets stuck. So long as that isn't
Bram Moolenaardb552d602006-03-23 22:59:57 +00004076 * fixed let's disallow floating. */
Bram Moolenaare580b0c2006-03-21 21:33:03 +00004077 bonobo_dock_item_set_behavior(dockitem,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004078 bonobo_dock_item_get_behavior(dockitem)
4079 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 }
4082 else
4083# endif /* FEAT_GUI_GNOME */
4084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 if (vim_strchr(p_go, GO_TOOLBAR) != NULL
4086 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
4087 gtk_widget_show(gui.toolbar);
4088 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0);
4089 }
4090#endif /* FEAT_TOOLBAR */
4091
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004092#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004093 /*
4094 * Use a Notebook for the tab pages labels. The labels are hidden by
4095 * default.
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004096 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004097 gui.tabline = gtk_notebook_new();
4098 gtk_widget_show(gui.tabline);
4099 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0);
4100 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
4101 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004102 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01004103# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar96351572006-05-05 21:16:59 +00004104 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01004105# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004106
Bram Moolenaar98921892016-02-23 17:14:37 +01004107# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004108 tabline_tooltip = gtk_tooltips_new();
4109 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip));
Bram Moolenaar98921892016-02-23 17:14:37 +01004110# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004111
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004112 {
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004113 GtkWidget *page, *label, *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004114
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004115 /* Add the first tab. */
Bram Moolenaar98921892016-02-23 17:14:37 +01004116# if GTK_CHECK_VERSION(3,2,0)
4117 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
4118 gtk_box_set_homogeneous(GTK_BOX(page), FALSE);
4119# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004120 page = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01004121# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004122 gtk_widget_show(page);
4123 gtk_container_add(GTK_CONTAINER(gui.tabline), page);
4124 label = gtk_label_new("-Empty-");
4125 gtk_widget_show(label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004126 event_box = gtk_event_box_new();
4127 gtk_widget_show(event_box);
Bram Moolenaar98921892016-02-23 17:14:37 +01004128# if GTK_CHECK_VERSION(3,0,0)
4129 g_object_set_data(G_OBJECT(event_box), "tab_num", GINT_TO_POINTER(1L));
4130# else
Bram Moolenaar47b46d72008-07-02 19:05:48 +00004131 gtk_object_set_user_data(GTK_OBJECT(event_box), (gpointer)1L);
Bram Moolenaar98921892016-02-23 17:14:37 +01004132# endif
4133# if !GTK_CHECK_VERSION(3,14,0)
Bram Moolenaar8ea91232006-04-28 22:41:43 +00004134 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01004135# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004136 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004137 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004138 }
Bram Moolenaar54a709e2006-05-04 21:57:11 +00004139
Bram Moolenaar98921892016-02-23 17:14:37 +01004140# if GTK_CHECK_VERSION(3,0,0)
4141 g_signal_connect(G_OBJECT(gui.tabline), "switch-page",
4142 G_CALLBACK(on_select_tab), NULL);
4143# else
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004144 gtk_signal_connect(GTK_OBJECT(gui.tabline), "switch_page",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004145 GTK_SIGNAL_FUNC(on_select_tab), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004146# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004147
4148 /* Create a popup menu for the tab line and connect it. */
4149 tabline_menu = create_tabline_menu();
Bram Moolenaar98921892016-02-23 17:14:37 +01004150# if GTK_CHECK_VERSION(3,0,0)
4151 g_signal_connect_swapped(G_OBJECT(gui.tabline), "button-press-event",
4152 G_CALLBACK(on_tabline_menu), G_OBJECT(tabline_menu));
4153# else
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004154 gtk_signal_connect_object(GTK_OBJECT(gui.tabline), "button_press_event",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004155 GTK_SIGNAL_FUNC(on_tabline_menu), GTK_OBJECT(tabline_menu));
Bram Moolenaar98921892016-02-23 17:14:37 +01004156# endif
4157#endif /* FEAT_GUI_TABLINE */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004158
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 gui.formwin = gtk_form_new();
Bram Moolenaar98921892016-02-23 17:14:37 +01004160#if GTK_CHECK_VERSION(3,0,0)
4161 gtk_container_set_border_width(GTK_CONTAINER(gui.formwin), 0);
4162#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 gtk_container_border_width(GTK_CONTAINER(gui.formwin), 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01004164#endif
4165#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK);
Bram Moolenaar98921892016-02-23 17:14:37 +01004167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
4169 gui.drawarea = gtk_drawing_area_new();
Bram Moolenaara859f042016-11-17 19:11:55 +01004170#if GTK_CHECK_VERSION(3,22,2)
4171 gtk_widget_set_name(gui.drawarea, "vim-gui-drawarea");
4172#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004173#if GTK_CHECK_VERSION(3,0,0)
4174 gui.surface = NULL;
4175 gui.by_signal = FALSE;
4176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177
4178 /* Determine which events we will filter. */
4179 gtk_widget_set_events(gui.drawarea,
4180 GDK_EXPOSURE_MASK |
4181 GDK_ENTER_NOTIFY_MASK |
4182 GDK_LEAVE_NOTIFY_MASK |
4183 GDK_BUTTON_PRESS_MASK |
4184 GDK_BUTTON_RELEASE_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 GDK_SCROLL_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 GDK_KEY_PRESS_MASK |
4187 GDK_KEY_RELEASE_MASK |
4188 GDK_POINTER_MOTION_MASK |
4189 GDK_POINTER_MOTION_HINT_MASK);
4190
4191 gtk_widget_show(gui.drawarea);
4192 gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0);
4193 gtk_widget_show(gui.formwin);
4194 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0);
4195
4196 /* For GtkSockets, key-presses must go to the focus widget (drawarea)
4197 * and not the window. */
Bram Moolenaar98921892016-02-23 17:14:37 +01004198#if GTK_CHECK_VERSION(3,0,0)
4199 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
4200 : G_OBJECT(gui.drawarea),
4201 "key-press-event",
4202 G_CALLBACK(key_press_event), NULL);
4203#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 gtk_signal_connect((gtk_socket_id == 0) ? GTK_OBJECT(gui.mainwin)
4205 : GTK_OBJECT(gui.drawarea),
4206 "key_press_event",
4207 GTK_SIGNAL_FUNC(key_press_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004208#endif
4209#if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 /* Also forward key release events for the benefit of GTK+ 2 input
4211 * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */
4212 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
4213 : G_OBJECT(gui.drawarea),
Bram Moolenaar98921892016-02-23 17:14:37 +01004214 "key-release-event",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 G_CALLBACK(&key_release_event), NULL);
4216#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004217#if GTK_CHECK_VERSION(3,0,0)
4218 g_signal_connect(G_OBJECT(gui.drawarea), "realize",
4219 G_CALLBACK(drawarea_realize_cb), NULL);
4220 g_signal_connect(G_OBJECT(gui.drawarea), "unrealize",
4221 G_CALLBACK(drawarea_unrealize_cb), NULL);
4222 g_signal_connect(G_OBJECT(gui.drawarea), "configure-event",
4223 G_CALLBACK(drawarea_configure_event_cb), NULL);
Bram Moolenaara859f042016-11-17 19:11:55 +01004224# if GTK_CHECK_VERSION(3,22,2)
4225 g_signal_connect_after(G_OBJECT(gui.drawarea), "style-updated",
4226 G_CALLBACK(&drawarea_style_updated_cb), NULL);
4227# else
Bram Moolenaar98921892016-02-23 17:14:37 +01004228 g_signal_connect_after(G_OBJECT(gui.drawarea), "style-set",
4229 G_CALLBACK(&drawarea_style_set_cb), NULL);
Bram Moolenaara859f042016-11-17 19:11:55 +01004230# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004231#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "realize",
4233 GTK_SIGNAL_FUNC(drawarea_realize_cb), NULL);
4234 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "unrealize",
4235 GTK_SIGNAL_FUNC(drawarea_unrealize_cb), NULL);
4236
4237 gtk_signal_connect_after(GTK_OBJECT(gui.drawarea), "style_set",
4238 GTK_SIGNAL_FUNC(&drawarea_style_set_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240
Bram Moolenaar98921892016-02-23 17:14:37 +01004241#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 gui.visibility = GDK_VISIBILITY_UNOBSCURED;
Bram Moolenaar98921892016-02-23 17:14:37 +01004243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
4245#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
4246 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE);
4247 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE);
4248#endif
4249
4250 if (gtk_socket_id != 0)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004251 /* make sure keyboard input can go to the drawarea */
Bram Moolenaar98921892016-02-23 17:14:37 +01004252#if GTK_CHECK_VERSION(3,0,0)
4253 gtk_widget_set_can_focus(gui.drawarea, TRUE);
4254#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 GTK_WIDGET_SET_FLAGS(gui.drawarea, GTK_CAN_FOCUS);
Bram Moolenaar98921892016-02-23 17:14:37 +01004256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257
4258 /*
4259 * Set clipboard specific atoms
4260 */
4261 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY;
4264 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE);
4265
4266 /*
4267 * Start out by adding the configured border width into the border offset.
4268 */
4269 gui.border_offset = gui.border_width;
4270
Bram Moolenaar98921892016-02-23 17:14:37 +01004271#if GTK_CHECK_VERSION(3,0,0)
4272 g_signal_connect(G_OBJECT(gui.drawarea), "draw",
4273 G_CALLBACK(draw_event), NULL);
4274#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event",
4276 GTK_SIGNAL_FUNC(visibility_event), NULL);
4277 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event",
4278 GTK_SIGNAL_FUNC(expose_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280
4281 /*
4282 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
4283 * Only needed for some window managers.
4284 */
4285 if (vim_strchr(p_go, GO_POINTER) != NULL)
4286 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004287#if GTK_CHECK_VERSION(3,0,0)
4288 g_signal_connect(G_OBJECT(gui.drawarea), "leave-notify-event",
4289 G_CALLBACK(leave_notify_event), NULL);
4290 g_signal_connect(G_OBJECT(gui.drawarea), "enter-notify-event",
4291 G_CALLBACK(enter_notify_event), NULL);
4292#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "leave_notify_event",
4294 GTK_SIGNAL_FUNC(leave_notify_event), NULL);
4295 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "enter_notify_event",
4296 GTK_SIGNAL_FUNC(enter_notify_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004300 /* Real windows can get focus ... GtkPlug, being a mere container can't,
4301 * only its widgets. Arguably, this could be common code and we not use
4302 * the window focus at all, but let's be safe.
4303 */
4304 if (gtk_socket_id == 0)
4305 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004306#if GTK_CHECK_VERSION(3,0,0)
4307 g_signal_connect(G_OBJECT(gui.mainwin), "focus-out-event",
4308 G_CALLBACK(focus_out_event), NULL);
4309 g_signal_connect(G_OBJECT(gui.mainwin), "focus-in-event",
4310 G_CALLBACK(focus_in_event), NULL);
4311#else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004312 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_out_event",
4313 GTK_SIGNAL_FUNC(focus_out_event), NULL);
4314 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_in_event",
4315 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004316#endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004317 }
4318 else
4319 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004320#if GTK_CHECK_VERSION(3,0,0)
4321 g_signal_connect(G_OBJECT(gui.drawarea), "focus-out-event",
4322 G_CALLBACK(focus_out_event), NULL);
4323 g_signal_connect(G_OBJECT(gui.drawarea), "focus-in-event",
4324 G_CALLBACK(focus_in_event), NULL);
4325#else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004326 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_out_event",
4327 GTK_SIGNAL_FUNC(focus_out_event), NULL);
4328 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_in_event",
4329 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004330#endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004331#ifdef FEAT_GUI_TABLINE
Bram Moolenaar98921892016-02-23 17:14:37 +01004332# if GTK_CHECK_VERSION(3,0,0)
4333 g_signal_connect(G_OBJECT(gui.tabline), "focus-out-event",
4334 G_CALLBACK(focus_out_event), NULL);
4335 g_signal_connect(G_OBJECT(gui.tabline), "focus-in-event",
4336 G_CALLBACK(focus_in_event), NULL);
4337# else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004338 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_out_event",
4339 GTK_SIGNAL_FUNC(focus_out_event), NULL);
4340 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_in_event",
4341 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004342# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004343#endif /* FEAT_GUI_TABLINE */
4344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345
Bram Moolenaar98921892016-02-23 17:14:37 +01004346#if GTK_CHECK_VERSION(3,0,0)
4347 g_signal_connect(G_OBJECT(gui.drawarea), "motion-notify-event",
4348 G_CALLBACK(motion_notify_event), NULL);
4349 g_signal_connect(G_OBJECT(gui.drawarea), "button-press-event",
4350 G_CALLBACK(button_press_event), NULL);
4351 g_signal_connect(G_OBJECT(gui.drawarea), "button-release-event",
4352 G_CALLBACK(button_release_event), NULL);
4353 g_signal_connect(G_OBJECT(gui.drawarea), "scroll-event",
4354 G_CALLBACK(&scroll_event), NULL);
4355#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "motion_notify_event",
4357 GTK_SIGNAL_FUNC(motion_notify_event), NULL);
4358 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_press_event",
4359 GTK_SIGNAL_FUNC(button_press_event), NULL);
4360 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_release_event",
4361 GTK_SIGNAL_FUNC(button_release_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 g_signal_connect(G_OBJECT(gui.drawarea), "scroll_event",
4363 G_CALLBACK(&scroll_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365
4366 /*
4367 * Add selection handler functions.
4368 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004369#if GTK_CHECK_VERSION(3,0,0)
4370 g_signal_connect(G_OBJECT(gui.drawarea), "selection-clear-event",
4371 G_CALLBACK(selection_clear_event), NULL);
4372 g_signal_connect(G_OBJECT(gui.drawarea), "selection-received",
4373 G_CALLBACK(selection_received_cb), NULL);
4374#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_clear_event",
4376 GTK_SIGNAL_FUNC(selection_clear_event), NULL);
4377 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_received",
4378 GTK_SIGNAL_FUNC(selection_received_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380
Bram Moolenaara76638f2010-06-05 12:49:46 +02004381 gui_gtk_set_selection_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382
Bram Moolenaar98921892016-02-23 17:14:37 +01004383#if GTK_CHECK_VERSION(3,0,0)
4384 g_signal_connect(G_OBJECT(gui.drawarea), "selection-get",
4385 G_CALLBACK(selection_get_cb), NULL);
4386#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_get",
4388 GTK_SIGNAL_FUNC(selection_get_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
4391 /* Pretend we don't have input focus, we will get an event if we do. */
4392 gui.in_focus = FALSE;
4393
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 return OK;
4395}
4396
4397#if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO)
4398/*
4399 * This is called from gui_start() after a fork() has been done.
4400 * We have to tell the session manager our new PID.
4401 */
4402 void
4403gui_mch_forked(void)
4404{
4405 if (using_gnome)
4406 {
4407 GnomeClient *client;
4408
4409 client = gnome_master_client();
4410
4411 if (client != NULL)
4412 gnome_client_set_process_id(client, getpid());
4413 }
4414}
4415#endif /* FEAT_GUI_GNOME && FEAT_SESSION */
4416
Bram Moolenaar98921892016-02-23 17:14:37 +01004417#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02004418 static GdkRGBA
4419color_to_rgba(guicolor_T color)
Bram Moolenaar98921892016-02-23 17:14:37 +01004420{
Bram Moolenaar36edf062016-07-21 22:10:12 +02004421 GdkRGBA rgba;
4422 rgba.red = ((color & 0xff0000) >> 16) / 255.0;
4423 rgba.green = ((color & 0xff00) >> 8) / 255.0;
4424 rgba.blue = ((color & 0xff)) / 255.0;
4425 rgba.alpha = 1.0;
4426 return rgba;
Bram Moolenaar98921892016-02-23 17:14:37 +01004427}
4428
4429 static void
Bram Moolenaar36edf062016-07-21 22:10:12 +02004430set_cairo_source_rgba_from_color(cairo_t *cr, guicolor_T color)
Bram Moolenaar98921892016-02-23 17:14:37 +01004431{
Bram Moolenaar36edf062016-07-21 22:10:12 +02004432 const GdkRGBA rgba = color_to_rgba(color);
4433 cairo_set_source_rgba(cr, rgba.red, rgba.green, rgba.blue, rgba.alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01004434}
4435#endif /* GTK_CHECK_VERSION(3,0,0) */
4436
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437/*
4438 * Called when the foreground or background color has been changed.
4439 * This used to change the graphics contexts directly but we are
4440 * currently manipulating them where desired.
4441 */
4442 void
4443gui_mch_new_colors(void)
4444{
Bram Moolenaar98921892016-02-23 17:14:37 +01004445#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaara859f042016-11-17 19:11:55 +01004446# if !GTK_CHECK_VERSION(3,22,2)
Bram Moolenaar98921892016-02-23 17:14:37 +01004447 GdkWindow * const da_win = gtk_widget_get_window(gui.drawarea);
Bram Moolenaara859f042016-11-17 19:11:55 +01004448# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004449
4450 if (gui.drawarea != NULL && gtk_widget_get_window(gui.drawarea) != NULL)
4451#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01004453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 {
Bram Moolenaara859f042016-11-17 19:11:55 +01004455#if GTK_CHECK_VERSION(3,22,2)
4456 GtkStyleContext * const context
4457 = gtk_widget_get_style_context(gui.drawarea);
4458 GtkCssProvider * const provider = gtk_css_provider_new();
4459 gchar * const css = g_strdup_printf(
4460 "widget#vim-gui-drawarea {\n"
4461 " background-color: #%.2lx%.2lx%.2lx;\n"
4462 "}\n",
4463 (gui.back_pixel >> 16) & 0xff,
4464 (gui.back_pixel >> 8) & 0xff,
4465 gui.back_pixel & 0xff);
4466
4467 gtk_css_provider_load_from_data(provider, css, -1, NULL);
4468 gtk_style_context_add_provider(context,
4469 GTK_STYLE_PROVIDER(provider), G_MAXUINT);
4470
4471 g_free(css);
4472 g_object_unref(provider);
4473#elif GTK_CHECK_VERSION(3,4,0) /* !GTK_CHECK_VERSION(3,22,2) */
Bram Moolenaar36edf062016-07-21 22:10:12 +02004474 GdkRGBA rgba;
Bram Moolenaar98921892016-02-23 17:14:37 +01004475
Bram Moolenaar36edf062016-07-21 22:10:12 +02004476 rgba = color_to_rgba(gui.back_pixel);
Bram Moolenaar98921892016-02-23 17:14:37 +01004477 {
4478 cairo_pattern_t * const pat = cairo_pattern_create_rgba(
Bram Moolenaar36edf062016-07-21 22:10:12 +02004479 rgba.red, rgba.green, rgba.blue, rgba.alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01004480 if (pat != NULL)
4481 {
4482 gdk_window_set_background_pattern(da_win, pat);
4483 cairo_pattern_destroy(pat);
4484 }
4485 else
Bram Moolenaar36edf062016-07-21 22:10:12 +02004486 gdk_window_set_background_rgba(da_win, &rgba);
Bram Moolenaar98921892016-02-23 17:14:37 +01004487 }
4488#else /* !GTK_CHECK_VERSION(3,4,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 GdkColor color = { 0, 0, 0, 0 };
4490
4491 color.pixel = gui.back_pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01004492# if GTK_CHECK_VERSION(3,0,0)
4493 gdk_window_set_background(da_win, &color);
4494# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 gdk_window_set_background(gui.drawarea->window, &color);
Bram Moolenaar98921892016-02-23 17:14:37 +01004496# endif
Bram Moolenaara859f042016-11-17 19:11:55 +01004497#endif /* !GTK_CHECK_VERSION(3,22,2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 }
4499}
4500
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501/*
4502 * This signal informs us about the need to rearrange our sub-widgets.
4503 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004505form_configure_event(GtkWidget *widget UNUSED,
4506 GdkEventConfigure *event,
4507 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508{
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004509 int usable_height = event->height;
4510
Bram Moolenaar182707a2016-11-21 20:55:58 +01004511#if GTK_CHECK_VERSION(3,22,2) && !GTK_CHECK_VERSION(3,22,4)
Bram Moolenaara859f042016-11-17 19:11:55 +01004512 /* As of 3.22.2, GdkWindows have started distributing configure events to
4513 * their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0).
4514 *
4515 * As can be seen from the implementation of move_native_children() and
4516 * configure_native_child() in gdkwindow.c, those functions actually
4517 * propagate configure events to every child, failing to distinguish
4518 * "native" one from non-native one.
4519 *
4520 * Naturally, configure events propagated to here like that are fallacious
4521 * and, as a matter of fact, they trigger a geometric collapse of
4522 * gui.formwin.
4523 *
4524 * To filter out such fallacious events, check if the given event is the
4525 * one that was sent out to the right place. Ignore it if not.
4526 */
Bram Moolenaar182707a2016-11-21 20:55:58 +01004527 /* Follow-up
4528 * After a few weeks later, the GdkWindow change mentioned above was
4529 * reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155).
4530 * The corresponding official release is 3.22.4. */
Bram Moolenaara859f042016-11-17 19:11:55 +01004531 if (event->window != gtk_widget_get_window(gui.formwin))
4532 return TRUE;
4533#endif
4534
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004535 /* When in a GtkPlug, we can't guarantee valid heights (as a round
4536 * no. of char-heights), so we have to manually sanitise them.
4537 * Widths seem to sort themselves out, don't ask me why.
4538 */
4539 if (gtk_socket_id != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004540 usable_height -= (gui.char_height - (gui.char_height/2)); /* sic. */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004541
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 gtk_form_freeze(GTK_FORM(gui.formwin));
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004543 gui_resize_shell(event->width, usable_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 gtk_form_thaw(GTK_FORM(gui.formwin));
4545
4546 return TRUE;
4547}
4548
4549/*
4550 * Function called when window already closed.
4551 * We can't do much more here than to trying to preserve what had been done,
4552 * since the window is already inevitably going away.
4553 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004554#if GTK_CHECK_VERSION(3,0,0)
4555 static void
4556mainwin_destroy_cb(GObject *object UNUSED, gpointer data UNUSED)
4557#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004559mainwin_destroy_cb(GtkObject *object UNUSED, gpointer data UNUSED)
Bram Moolenaar98921892016-02-23 17:14:37 +01004560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561{
4562 /* Don't write messages to the GUI anymore */
4563 full_screen = FALSE;
4564
4565 gui.mainwin = NULL;
4566 gui.drawarea = NULL;
4567
4568 if (!exiting) /* only do anything if the destroy was unexpected */
4569 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004570 vim_strncpy(IObuff,
4571 (char_u *)_("Vim: Main window unexpectedly destroyed\n"),
4572 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 preserve_exit();
4574 }
Bram Moolenaar36e294c2015-12-29 18:55:46 +01004575#ifdef USE_GRESOURCE
4576 gui_gtk_unregister_resource();
4577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578}
4579
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004580
4581/*
4582 * Bit of a hack to ensure we start GtkPlug windows with the correct window
4583 * hints (and thus the required size from -geom), but that after that we
4584 * put the hints back to normal (the actual minimum size) so we may
4585 * subsequently be resized smaller. GtkSocket (the parent end) uses the
Bram Moolenaar49325942007-05-10 19:19:59 +00004586 * plug's window 'min hints to set *it's* minimum size, but that's also the
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004587 * only way we have of making ourselves bigger (by set lines/columns).
4588 * Thus set hints at start-up to ensure correct init. size, then a
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01004589 * second after the final attempt to reset the real minimum hints (done by
4590 * scrollbar init.), actually do the standard hints and stop the timer.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004591 * We'll not let the default hints be set while this timer's active.
4592 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004593 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004594check_startup_plug_hints(gpointer data UNUSED)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004595{
4596 if (init_window_hints_state == 1)
4597 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004598 /* Safe to use normal hints now */
4599 init_window_hints_state = 0;
4600 update_window_manager_hints(0, 0);
4601 return FALSE; /* stop timer */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004602 }
4603
4604 /* Keep on trying */
4605 init_window_hints_state = 1;
4606 return TRUE;
4607}
4608
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609/*
4610 * Open the GUI window which was created by a call to gui_mch_init().
4611 */
4612 int
4613gui_mch_open(void)
4614{
4615 guicolor_T fg_pixel = INVALCOLOR;
4616 guicolor_T bg_pixel = INVALCOLOR;
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004617 guint pixel_width;
4618 guint pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 /*
4621 * Allow setting a window role on the command line, or invent one
4622 * if none was specified. This is mainly useful for GNOME session
4623 * support; allowing the WM to restore window placement.
4624 */
4625 if (role_argument != NULL)
4626 {
4627 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument);
4628 }
4629 else
4630 {
4631 char *role;
4632
4633 /* Invent a unique-enough ID string for the role */
4634 role = g_strdup_printf("vim-%u-%u-%u",
4635 (unsigned)mch_get_pid(),
4636 (unsigned)g_random_int(),
4637 (unsigned)time(NULL));
4638
4639 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role);
4640 g_free(role);
4641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642
4643 if (gui_win_x != -1 && gui_win_y != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645
4646 /* Determine user specified geometry, if present. */
4647 if (gui.geom != NULL)
4648 {
4649 int mask;
4650 unsigned int w, h;
4651 int x = 0;
4652 int y = 0;
4653
4654 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
4655
4656 if (mask & WidthValue)
4657 Columns = w;
4658 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00004659 {
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004660 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00004661 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00004663 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004664 limit_screen_size();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004665
4666 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4667 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
4668
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004669 pixel_width += get_menu_tool_width();
4670 pixel_height += get_menu_tool_height();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004671
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 if (mask & (XValue | YValue))
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004673 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004674 int ww, hh;
4675 gui_mch_get_screen_dimensions(&ww, &hh);
4676 hh += p_ghr + get_menu_tool_height();
4677 ww += get_menu_tool_width();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004678 if (mask & XNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004679 x += ww - pixel_width;
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004680 if (mask & YNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004681 y += hh - pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 vim_free(gui.geom);
4685 gui.geom = NULL;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004686
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004687 /* From now until everyone's stopped trying to set the window hints
4688 * to their correct minimum values, stop them being set as we need
4689 * them to remain at our required size for the parent GtkSocket to
4690 * give us the right initial size.
4691 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004692 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004693 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004694 update_window_manager_hints(pixel_width, pixel_height);
4695 init_window_hints_state = 1;
4696 g_timeout_add(1000, check_startup_plug_hints, NULL);
4697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 }
4699
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004700 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4701 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004702 /* For GTK2 changing the size of the form widget doesn't cause window
4703 * resizing. */
Bram Moolenaardebe25a2010-06-06 17:41:24 +02004704 if (gtk_socket_id == 0)
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004705 gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004706 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707
4708 if (foreground_argument != NULL)
4709 fg_pixel = gui_get_color((char_u *)foreground_argument);
4710 if (fg_pixel == INVALCOLOR)
4711 fg_pixel = gui_get_color((char_u *)"Black");
4712
4713 if (background_argument != NULL)
4714 bg_pixel = gui_get_color((char_u *)background_argument);
4715 if (bg_pixel == INVALCOLOR)
4716 bg_pixel = gui_get_color((char_u *)"White");
4717
4718 if (found_reverse_arg)
4719 {
4720 gui.def_norm_pixel = bg_pixel;
4721 gui.def_back_pixel = fg_pixel;
4722 }
4723 else
4724 {
4725 gui.def_norm_pixel = fg_pixel;
4726 gui.def_back_pixel = bg_pixel;
4727 }
4728
4729 /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or
4730 * in a vimrc file) */
4731 set_normal_colors();
4732
4733 /* Check that none of the colors are the same as the background color */
4734 gui_check_colors();
4735
4736 /* Get the colors for the highlight groups (gui_check_colors() might have
4737 * changed them). */
4738 highlight_gui_started(); /* re-init colors and fonts */
4739
Bram Moolenaar98921892016-02-23 17:14:37 +01004740#if GTK_CHECK_VERSION(3,0,0)
4741 g_signal_connect(G_OBJECT(gui.mainwin), "destroy",
4742 G_CALLBACK(mainwin_destroy_cb), NULL);
4743#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "destroy",
4745 GTK_SIGNAL_FUNC(mainwin_destroy_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747
4748#ifdef FEAT_HANGULIN
4749 hangul_keyboard_set();
4750#endif
4751
4752 /*
4753 * Notify the fixed area about the need to resize the contents of the
4754 * gui.formwin, which we use for random positioning of the included
4755 * components.
4756 *
4757 * We connect this signal deferred finally after anything is in place,
4758 * since this is intended to handle resizements coming from the window
4759 * manager upon us and should not interfere with what VIM is requesting
4760 * upon startup.
4761 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004762#if GTK_CHECK_VERSION(3,0,0)
4763 g_signal_connect(G_OBJECT(gui.formwin), "configure-event",
4764 G_CALLBACK(form_configure_event), NULL);
4765#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 gtk_signal_connect(GTK_OBJECT(gui.formwin), "configure_event",
4767 GTK_SIGNAL_FUNC(form_configure_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769
4770#ifdef FEAT_DND
Bram Moolenaara76638f2010-06-05 12:49:46 +02004771 /* Set up for receiving DND items. */
4772 gui_gtk_set_dnd_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773
Bram Moolenaar98921892016-02-23 17:14:37 +01004774# if GTK_CHECK_VERSION(3,0,0)
4775 g_signal_connect(G_OBJECT(gui.drawarea), "drag-data-received",
4776 G_CALLBACK(drag_data_received_cb), NULL);
4777# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "drag_data_received",
4779 GTK_SIGNAL_FUNC(drag_data_received_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004780# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781#endif
4782
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 /* With GTK+ 2, we need to iconify the window before calling show()
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004784 * to avoid mapping the window for a short time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 if (found_iconic_arg && gtk_socket_id == 0)
4786 gui_mch_iconify();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787
4788 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004789#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 unsigned long menu_handler = 0;
4791# ifdef FEAT_TOOLBAR
4792 unsigned long tool_handler = 0;
4793# endif
4794 /*
4795 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
4796 * show when restoring the saved layout configuration. We can't just
4797 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
4798 * a toplevel window and thus will be realized immediately. Instead,
4799 * connect signal handlers to hide the widgets just after they've been
4800 * marked visible, but before the main window is realized.
4801 */
4802 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL)
4803 menu_handler = g_signal_connect_after(gui.menubar_h, "show",
4804 G_CALLBACK(&gtk_widget_hide),
4805 NULL);
4806# ifdef FEAT_TOOLBAR
4807 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL
4808 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
4809 tool_handler = g_signal_connect_after(gui.toolbar_h, "show",
4810 G_CALLBACK(&gtk_widget_hide),
4811 NULL);
4812# endif
4813#endif
4814 gtk_widget_show(gui.mainwin);
4815
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004816#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 if (menu_handler != 0)
4818 g_signal_handler_disconnect(gui.menubar_h, menu_handler);
4819# ifdef FEAT_TOOLBAR
4820 if (tool_handler != 0)
4821 g_signal_handler_disconnect(gui.toolbar_h, tool_handler);
4822# endif
4823#endif
4824 }
4825
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 return OK;
4827}
4828
4829
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004831gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832{
4833 if (gui.mainwin != NULL)
4834 gtk_widget_destroy(gui.mainwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835}
4836
4837/*
4838 * Get the position of the top left corner of the window.
4839 */
4840 int
4841gui_mch_get_winpos(int *x, int *y)
4842{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 return OK;
4845}
4846
4847/*
4848 * Set the position of the top left corner of the window to the given
4849 * coordinates.
4850 */
4851 void
4852gui_mch_set_winpos(int x, int y)
4853{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855}
4856
Bram Moolenaar98921892016-02-23 17:14:37 +01004857#if !GTK_CHECK_VERSION(3,0,0)
4858# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859static int resize_idle_installed = FALSE;
4860/*
4861 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
4862 * the shell size doesn't exceed the window size, i.e. if the window manager
4863 * ignored our size request. Usually this happens if the window is maximized.
4864 *
4865 * FIXME: It'd be nice if we could find a little more orthodox solution.
4866 * See also the remark below in gui_mch_set_shellsize().
4867 *
4868 * DISABLED: When doing ":set lines+=1" this function would first invoke
4869 * gui_resize_shell() with the old size, then the normal callback would
4870 * report the new size through form_configure_event(). That caused the window
4871 * layout to be messed up.
4872 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 static gboolean
4874force_shell_resize_idle(gpointer data)
4875{
4876 if (gui.mainwin != NULL
4877 && GTK_WIDGET_REALIZED(gui.mainwin)
4878 && GTK_WIDGET_VISIBLE(gui.mainwin))
4879 {
4880 int width;
4881 int height;
4882
4883 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height);
4884
4885 width -= get_menu_tool_width();
4886 height -= get_menu_tool_height();
4887
4888 gui_resize_shell(width, height);
4889 }
4890
4891 resize_idle_installed = FALSE;
4892 return FALSE; /* don't call me again */
4893}
Bram Moolenaar98921892016-02-23 17:14:37 +01004894# endif
4895#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896
Bram Moolenaar09736232009-09-23 16:14:49 +00004897/*
4898 * Return TRUE if the main window is maximized.
4899 */
4900 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004901gui_mch_maximized(void)
Bram Moolenaar09736232009-09-23 16:14:49 +00004902{
Bram Moolenaar98921892016-02-23 17:14:37 +01004903#if GTK_CHECK_VERSION(3,0,0)
4904 return (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL
4905 && (gdk_window_get_state(gtk_widget_get_window(gui.mainwin))
4906 & GDK_WINDOW_STATE_MAXIMIZED));
4907#else
Bram Moolenaar09736232009-09-23 16:14:49 +00004908 return (gui.mainwin != NULL && gui.mainwin->window != NULL
4909 && (gdk_window_get_state(gui.mainwin->window)
4910 & GDK_WINDOW_STATE_MAXIMIZED));
Bram Moolenaar98921892016-02-23 17:14:37 +01004911#endif
Bram Moolenaar09736232009-09-23 16:14:49 +00004912}
4913
4914/*
4915 * Unmaximize the main window
4916 */
4917 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004918gui_mch_unmaximize(void)
Bram Moolenaar09736232009-09-23 16:14:49 +00004919{
4920 if (gui.mainwin != NULL)
4921 gtk_window_unmaximize(GTK_WINDOW(gui.mainwin));
4922}
Bram Moolenaar09736232009-09-23 16:14:49 +00004923
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924/*
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004925 * Called when the font changed while the window is maximized. Compute the
4926 * new Rows and Columns. This is like resizing the window.
4927 */
4928 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004929gui_mch_newfont(void)
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004930{
4931 int w, h;
4932
4933 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
4934 w -= get_menu_tool_width();
4935 h -= get_menu_tool_height();
4936 gui_resize_shell(w, h);
4937}
4938
4939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 * Set the windows size.
4941 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 void
4943gui_mch_set_shellsize(int width, int height,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004944 int min_width UNUSED, int min_height UNUSED,
4945 int base_width UNUSED, int base_height UNUSED,
4946 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 /* give GTK+ a chance to put all widget's into place */
4949 gui_mch_update();
4950
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004951 /* this will cause the proper resizement to happen too */
4952 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004953 update_window_manager_hints(0, 0);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004954
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 /* With GTK+ 2, changing the size of the form widget doesn't resize
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004956 * the window. So let's do it the other way around and resize the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 * main window instead. */
4958 width += get_menu_tool_width();
4959 height += get_menu_tool_height();
4960
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004961 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004962 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004963 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004964 update_window_manager_hints(width, height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965
Bram Moolenaar98921892016-02-23 17:14:37 +01004966# if !GTK_CHECK_VERSION(3,0,0)
4967# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 if (!resize_idle_installed)
4969 {
4970 g_idle_add_full(GDK_PRIORITY_EVENTS + 10,
4971 &force_shell_resize_idle, NULL, NULL);
4972 resize_idle_installed = TRUE;
4973 }
Bram Moolenaar98921892016-02-23 17:14:37 +01004974# endif
4975# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 /*
4977 * Wait until all events are processed to prevent a crash because the
4978 * real size of the drawing area doesn't reflect Vim's internal ideas.
4979 *
4980 * This is a bit of a hack, since Vim is a terminal application with a GUI
4981 * on top, while the GUI expects to be the boss.
4982 */
4983 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984}
4985
4986
4987/*
4988 * The screen size is used to make sure the initial window doesn't get bigger
4989 * than the screen. This subtracts some room for menubar, toolbar and window
4990 * decorations.
4991 */
4992 void
4993gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
4994{
4995#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaara859f042016-11-17 19:11:55 +01004996# if GTK_CHECK_VERSION(3,22,2)
4997 GdkRectangle rect;
4998 GdkMonitor * const mon = gdk_display_get_monitor_at_window(
4999 gtk_widget_get_display(gui.mainwin),
5000 gtk_widget_get_window(gui.mainwin));
5001 gdk_monitor_get_geometry(mon, &rect);
5002
5003 *screen_w = rect.width;
5004 *screen_h = rect.height - p_ghr;
5005# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 GdkScreen* screen;
5007
5008 if (gui.mainwin != NULL && gtk_widget_has_screen(gui.mainwin))
5009 screen = gtk_widget_get_screen(gui.mainwin);
5010 else
5011 screen = gdk_screen_get_default();
5012
5013 *screen_w = gdk_screen_get_width(screen);
5014 *screen_h = gdk_screen_get_height(screen) - p_ghr;
Bram Moolenaara859f042016-11-17 19:11:55 +01005015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016#else
5017 *screen_w = gdk_screen_width();
5018 /* Subtract 'guiheadroom' from the height to allow some room for the
5019 * window manager (task list and window title bar). */
5020 *screen_h = gdk_screen_height() - p_ghr;
5021#endif
5022
5023 /*
5024 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
5025 * the toolbar and menubar for GTK, we subtract them from the screen
Bram Moolenaar64404472010-06-26 06:24:45 +02005026 * height, so that the window size can be made to fit on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 * This should be completely changed later.
5028 */
5029 *screen_w -= get_menu_tool_width();
5030 *screen_h -= get_menu_tool_height();
5031}
5032
5033#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005035gui_mch_settitle(char_u *title, char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 if (title != NULL && output_conv.vc_type != CONV_NONE)
5038 title = string_convert(&output_conv, title, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039
5040 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
5041
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 if (output_conv.vc_type != CONV_NONE)
5043 vim_free(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044}
5045#endif /* FEAT_TITLE */
5046
5047#if defined(FEAT_MENU) || defined(PROTO)
5048 void
5049gui_mch_enable_menu(int showit)
5050{
5051 GtkWidget *widget;
5052
5053# ifdef FEAT_GUI_GNOME
5054 if (using_gnome)
5055 widget = gui.menubar_h;
5056 else
5057# endif
5058 widget = gui.menubar;
5059
Bram Moolenaar18144c82006-04-12 21:52:12 +00005060 /* Do not disable the menu while starting up, otherwise F10 doesn't work. */
Bram Moolenaar98921892016-02-23 17:14:37 +01005061# if GTK_CHECK_VERSION(3,0,0)
5062 if (!showit != !gtk_widget_get_visible(widget) && !gui.starting)
5063# else
Bram Moolenaar18144c82006-04-12 21:52:12 +00005064 if (!showit != !GTK_WIDGET_VISIBLE(widget) && !gui.starting)
Bram Moolenaar98921892016-02-23 17:14:37 +01005065# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 {
5067 if (showit)
5068 gtk_widget_show(widget);
5069 else
5070 gtk_widget_hide(widget);
5071
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00005072 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 }
5074}
5075#endif /* FEAT_MENU */
5076
5077#if defined(FEAT_TOOLBAR) || defined(PROTO)
5078 void
5079gui_mch_show_toolbar(int showit)
5080{
5081 GtkWidget *widget;
5082
5083 if (gui.toolbar == NULL)
5084 return;
5085
5086# ifdef FEAT_GUI_GNOME
5087 if (using_gnome)
5088 widget = gui.toolbar_h;
5089 else
5090# endif
5091 widget = gui.toolbar;
5092
5093 if (showit)
5094 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
5095
Bram Moolenaar98921892016-02-23 17:14:37 +01005096# if GTK_CHECK_VERSION(3,0,0)
5097 if (!showit != !gtk_widget_get_visible(widget))
5098# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 if (!showit != !GTK_WIDGET_VISIBLE(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01005100# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 {
5102 if (showit)
5103 gtk_widget_show(widget);
5104 else
5105 gtk_widget_hide(widget);
5106
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00005107 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
5109}
5110#endif /* FEAT_TOOLBAR */
5111
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112/*
5113 * Check if a given font is a CJK font. This is done in a very crude manner. It
5114 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
5115 * font. Consequently, this function cannot be used as a general purpose check
5116 * for CJK-ness for which fontconfig APIs should be used. This is only used by
5117 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
5118 */
5119 static int
5120is_cjk_font(PangoFontDescription *font_desc)
5121{
5122 static const char * const cjk_langs[] =
5123 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
5124
5125 PangoFont *font;
5126 unsigned i;
5127 int is_cjk = FALSE;
5128
5129 font = pango_context_load_font(gui.text_context, font_desc);
5130
5131 if (font == NULL)
5132 return FALSE;
5133
5134 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i)
5135 {
5136 PangoCoverage *coverage;
5137 gunichar uc;
5138
5139 coverage = pango_font_get_coverage(
5140 font, pango_language_from_string(cjk_langs[i]));
5141
5142 if (coverage != NULL)
5143 {
5144 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00;
5145 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT);
5146 pango_coverage_unref(coverage);
5147 }
5148 }
5149
5150 g_object_unref(font);
5151
5152 return is_cjk;
5153}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154
Bram Moolenaar231334e2005-07-25 20:46:57 +00005155/*
5156 * Adjust gui.char_height (after 'linespace' was changed).
5157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 int
Bram Moolenaar231334e2005-07-25 20:46:57 +00005159gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 PangoFontMetrics *metrics;
5162 int ascent;
5163 int descent;
5164
5165 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font,
5166 pango_context_get_language(gui.text_context));
5167 ascent = pango_font_metrics_get_ascent(metrics);
5168 descent = pango_font_metrics_get_descent(metrics);
5169
5170 pango_font_metrics_unref(metrics);
5171
5172 gui.char_height = (ascent + descent + PANGO_SCALE - 1) / PANGO_SCALE
Bram Moolenaar231334e2005-07-25 20:46:57 +00005173 + p_linespace;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005174 /* LINTED: avoid warning: bitwise operation on signed value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2);
5176
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 /* A not-positive value of char_height may crash Vim. Only happens
5178 * if 'linespace' is negative (which does make sense sometimes). */
5179 gui.char_ascent = MAX(gui.char_ascent, 0);
5180 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
5181
5182 return OK;
5183}
5184
Bram Moolenaar98921892016-02-23 17:14:37 +01005185#if GTK_CHECK_VERSION(3,0,0)
5186/* Callback function used in gui_mch_font_dialog() */
5187 static gboolean
5188font_filter(const PangoFontFamily *family,
5189 const PangoFontFace *face UNUSED,
5190 gpointer data UNUSED)
5191{
5192 return pango_font_family_is_monospace((PangoFontFamily *)family);
5193}
5194#endif
5195
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196/*
5197 * Put up a font dialog and return the selected font name in allocated memory.
5198 * "oldval" is the previous value. Return NULL when cancelled.
5199 * This should probably go into gui_gtk.c. Hmm.
5200 * FIXME:
5201 * The GTK2 font selection dialog has no filtering API. So we could either
5202 * a) implement our own (possibly copying the code from somewhere else) or
5203 * b) just live with it.
5204 */
5205 char_u *
5206gui_mch_font_dialog(char_u *oldval)
5207{
5208 GtkWidget *dialog;
5209 int response;
5210 char_u *fontname = NULL;
5211 char_u *oldname;
5212
Bram Moolenaar98921892016-02-23 17:14:37 +01005213#if GTK_CHECK_VERSION(3,2,0)
5214 dialog = gtk_font_chooser_dialog_new(NULL, NULL);
5215 gtk_font_chooser_set_filter_func(GTK_FONT_CHOOSER(dialog), font_filter,
5216 NULL, NULL);
5217#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 dialog = gtk_font_selection_dialog_new(NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01005219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220
5221 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
5222 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
5223
5224 if (oldval != NULL && oldval[0] != NUL)
5225 {
5226 if (output_conv.vc_type != CONV_NONE)
5227 oldname = string_convert(&output_conv, oldval, NULL);
5228 else
5229 oldname = oldval;
5230
5231 /* Annoying bug in GTK (or Pango): if the font name does not include a
5232 * size, zero is used. Use default point size ten. */
5233 if (!vim_isdigit(oldname[STRLEN(oldname) - 1]))
5234 {
5235 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3);
5236
5237 if (p != NULL)
5238 {
5239 STRCPY(p + STRLEN(p), " 10");
5240 if (oldname != oldval)
5241 vim_free(oldname);
5242 oldname = p;
5243 }
5244 }
5245
Bram Moolenaar98921892016-02-23 17:14:37 +01005246#if GTK_CHECK_VERSION(3,2,0)
5247 gtk_font_chooser_set_font(
5248 GTK_FONT_CHOOSER(dialog), (const gchar *)oldname);
5249#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 gtk_font_selection_dialog_set_font_name(
5251 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname);
Bram Moolenaar98921892016-02-23 17:14:37 +01005252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253
5254 if (oldname != oldval)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005255 vim_free(oldname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 }
Bram Moolenaarbef9d832009-09-11 13:46:41 +00005257 else
Bram Moolenaar98921892016-02-23 17:14:37 +01005258#if GTK_CHECK_VERSION(3,2,0)
5259 gtk_font_chooser_set_font(
5260 GTK_FONT_CHOOSER(dialog), DEFAULT_FONT);
5261#else
Bram Moolenaarbef9d832009-09-11 13:46:41 +00005262 gtk_font_selection_dialog_set_font_name(
5263 GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT);
Bram Moolenaar98921892016-02-23 17:14:37 +01005264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265
5266 response = gtk_dialog_run(GTK_DIALOG(dialog));
5267
5268 if (response == GTK_RESPONSE_OK)
5269 {
5270 char *name;
5271
Bram Moolenaar98921892016-02-23 17:14:37 +01005272#if GTK_CHECK_VERSION(3,2,0)
5273 name = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(dialog));
5274#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 name = gtk_font_selection_dialog_get_font_name(
5276 GTK_FONT_SELECTION_DIALOG(dialog));
Bram Moolenaar98921892016-02-23 17:14:37 +01005277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 if (name != NULL)
5279 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005280 char_u *p;
5281
5282 /* Apparently some font names include a comma, need to escape
5283 * that, because in 'guifont' it separates names. */
5284 p = vim_strsave_escaped((char_u *)name, (char_u *)",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 g_free(name);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005286 if (p != NULL && input_conv.vc_type != CONV_NONE)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005287 {
5288 fontname = string_convert(&input_conv, p, NULL);
5289 vim_free(p);
5290 }
5291 else
5292 fontname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 }
5294 }
5295
5296 if (response != GTK_RESPONSE_NONE)
5297 gtk_widget_destroy(dialog);
5298
5299 return fontname;
5300}
5301
5302/*
5303 * Some monospace fonts don't support a bold weight, and fall back
5304 * silently to the regular weight. But this is no good since our text
5305 * drawing function can emulate bold by overstriking. So let's try
5306 * to detect whether bold weight is actually available and emulate it
5307 * otherwise.
5308 *
5309 * Note that we don't need to check for italic style since Xft can
5310 * emulate italic on its own, provided you have a proper fontconfig
5311 * setup. We wouldn't be able to emulate it in Vim anyway.
5312 */
5313 static void
5314get_styled_font_variants(void)
5315{
5316 PangoFontDescription *bold_font_desc;
5317 PangoFont *plain_font;
5318 PangoFont *bold_font;
5319
5320 gui.font_can_bold = FALSE;
5321
5322 plain_font = pango_context_load_font(gui.text_context, gui.norm_font);
5323
5324 if (plain_font == NULL)
5325 return;
5326
5327 bold_font_desc = pango_font_description_copy_static(gui.norm_font);
5328 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD);
5329
5330 bold_font = pango_context_load_font(gui.text_context, bold_font_desc);
5331 /*
5332 * The comparison relies on the unique handle nature of a PangoFont*,
5333 * i.e. it's assumed that a different PangoFont* won't refer to the
5334 * same font. Seems to work, and failing here isn't critical anyway.
5335 */
5336 if (bold_font != NULL)
5337 {
5338 gui.font_can_bold = (bold_font != plain_font);
5339 g_object_unref(bold_font);
5340 }
5341
5342 pango_font_description_free(bold_font_desc);
5343 g_object_unref(plain_font);
5344}
5345
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346static PangoEngineShape *default_shape_engine = NULL;
5347
5348/*
5349 * Create a map from ASCII characters in the range [32,126] to glyphs
5350 * of the current font. This is used by gui_gtk2_draw_string() to skip
5351 * the itemize and shaping process for the most common case.
5352 */
5353 static void
5354ascii_glyph_table_init(void)
5355{
Bram Moolenaar16350cb2016-08-14 20:27:34 +02005356 char_u ascii_chars[2 * 128];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 PangoAttrList *attr_list;
5358 GList *item_list;
5359 int i;
5360
5361 if (gui.ascii_glyphs != NULL)
5362 pango_glyph_string_free(gui.ascii_glyphs);
5363 if (gui.ascii_font != NULL)
5364 g_object_unref(gui.ascii_font);
5365
5366 gui.ascii_glyphs = NULL;
5367 gui.ascii_font = NULL;
5368
Bram Moolenaar16350cb2016-08-14 20:27:34 +02005369 /* For safety, fill in question marks for the control characters.
5370 * Put a space between characters to avoid shaping. */
5371 for (i = 0; i < 128; ++i)
5372 {
5373 if (i >= 32 && i < 127)
5374 ascii_chars[2 * i] = i;
5375 else
5376 ascii_chars[2 * i] = '?';
5377 ascii_chars[2 * i + 1] = ' ';
5378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379
5380 attr_list = pango_attr_list_new();
5381 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars,
5382 0, sizeof(ascii_chars), attr_list, NULL);
5383
5384 if (item_list != NULL && item_list->next == NULL) /* play safe */
5385 {
5386 PangoItem *item;
5387 int width;
5388
5389 item = (PangoItem *)item_list->data;
5390 width = gui.char_width * PANGO_SCALE;
5391
5392 /* Remember the shape engine used for ASCII. */
5393 default_shape_engine = item->analysis.shape_engine;
5394
5395 gui.ascii_font = item->analysis.font;
5396 g_object_ref(gui.ascii_font);
5397
5398 gui.ascii_glyphs = pango_glyph_string_new();
5399
5400 pango_shape((const char *)ascii_chars, sizeof(ascii_chars),
5401 &item->analysis, gui.ascii_glyphs);
5402
5403 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars));
5404
5405 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i)
5406 {
5407 PangoGlyphGeometry *geom;
5408
5409 geom = &gui.ascii_glyphs->glyphs[i].geometry;
5410 geom->x_offset += MAX(0, width - geom->width) / 2;
5411 geom->width = width;
5412 }
5413 }
5414
5415 g_list_foreach(item_list, (GFunc)&pango_item_free, NULL);
5416 g_list_free(item_list);
5417 pango_attr_list_unref(attr_list);
5418}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419
5420/*
5421 * Initialize Vim to use the font or fontset with the given name.
5422 * Return FAIL if the font could not be loaded, OK otherwise.
5423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 int
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005425gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 PangoFontDescription *font_desc;
5428 PangoLayout *layout;
5429 int width;
5430
5431 /* If font_name is NULL, this means to use the default, which should
5432 * be present on all proper Pango/fontconfig installations. */
5433 if (font_name == NULL)
5434 font_name = (char_u *)DEFAULT_FONT;
5435
5436 font_desc = gui_mch_get_font(font_name, FALSE);
5437
5438 if (font_desc == NULL)
5439 return FAIL;
5440
5441 gui_mch_free_font(gui.norm_font);
5442 gui.norm_font = font_desc;
5443
5444 pango_context_set_font_description(gui.text_context, font_desc);
5445
5446 layout = pango_layout_new(gui.text_context);
5447 pango_layout_set_text(layout, "MW", 2);
5448 pango_layout_get_size(layout, &width, NULL);
5449 /*
5450 * Set char_width to half the width obtained from pango_layout_get_size()
5451 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
5452 * Pango to use the same width for both non-CJK characters (e.g. Latin
5453 * letters and numbers) and CJK characters. This results in 's p a c e d
5454 * o u t' rendering when a CJK 'fixed width' font is used. To work around
5455 * that, divide the width returned by Pango by 2 if cjk_width is equal to
5456 * width for CJK fonts.
5457 *
5458 * For related bugs, see:
5459 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
5460 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
5461 *
5462 * With this, for all four of the following cases, Vim works fine:
5463 * guifont=CJK_fixed_width_font
5464 * guifont=Non_CJK_fixed_font
5465 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
5466 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
5467 */
5468 if (is_cjk_font(gui.norm_font))
5469 {
5470 int cjk_width;
5471
5472 /* Measure the text extent of U+4E00 and U+4E8C */
5473 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1);
5474 pango_layout_get_size(layout, &cjk_width, NULL);
5475
5476 if (width == cjk_width) /* Xft not patched */
5477 width /= 2;
5478 }
5479 g_object_unref(layout);
5480
5481 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE;
5482
5483 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
5484 if (gui.char_width <= 0)
5485 gui.char_width = 8;
5486
Bram Moolenaar231334e2005-07-25 20:46:57 +00005487 gui_mch_adjust_charheight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488
5489 /* Set the fontname, which will be used for information purposes */
5490 hl_set_font_name(font_name);
5491
5492 get_styled_font_variants();
5493 ascii_glyph_table_init();
5494
5495 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
5496 if (gui.wide_font != NULL
5497 && pango_font_description_equal(gui.norm_font, gui.wide_font))
5498 {
5499 pango_font_description_free(gui.wide_font);
5500 gui.wide_font = NULL;
5501 }
5502
Bram Moolenaare161c792009-11-03 17:13:59 +00005503 if (gui_mch_maximized())
5504 {
Bram Moolenaare161c792009-11-03 17:13:59 +00005505 /* Update lines and columns in accordance with the new font, keep the
5506 * window maximized. */
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02005507 gui_mch_newfont();
Bram Moolenaare161c792009-11-03 17:13:59 +00005508 }
5509 else
Bram Moolenaare161c792009-11-03 17:13:59 +00005510 {
5511 /* Preserve the logical dimensions of the screen. */
5512 update_window_manager_hints(0, 0);
5513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514
5515 return OK;
5516}
5517
5518/*
5519 * Get a reference to the font "name".
5520 * Return zero for failure.
5521 */
5522 GuiFont
5523gui_mch_get_font(char_u *name, int report_error)
5524{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 PangoFontDescription *font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526
5527 /* can't do this when GUI is not running */
5528 if (!gui.in_use || name == NULL)
5529 return NULL;
5530
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 if (output_conv.vc_type != CONV_NONE)
5532 {
5533 char_u *buf;
5534
5535 buf = string_convert(&output_conv, name, NULL);
5536 if (buf != NULL)
5537 {
5538 font = pango_font_description_from_string((const char *)buf);
5539 vim_free(buf);
5540 }
5541 else
5542 font = NULL;
5543 }
5544 else
5545 font = pango_font_description_from_string((const char *)name);
5546
5547 if (font != NULL)
5548 {
5549 PangoFont *real_font;
5550
5551 /* pango_context_load_font() bails out if no font size is set */
5552 if (pango_font_description_get_size(font) <= 0)
5553 pango_font_description_set_size(font, 10 * PANGO_SCALE);
5554
5555 real_font = pango_context_load_font(gui.text_context, font);
5556
5557 if (real_font == NULL)
5558 {
5559 pango_font_description_free(font);
5560 font = NULL;
5561 }
5562 else
5563 g_object_unref(real_font);
5564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565
5566 if (font == NULL)
5567 {
5568 if (report_error)
Bram Moolenaarc93e7912008-07-08 10:46:08 +00005569 EMSG2(_((char *)e_font), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 return NULL;
5571 }
5572
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 return font;
5574}
5575
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005576#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005577/*
5578 * Return the name of font "font" in allocated memory.
5579 */
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005580 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005581gui_mch_get_fontname(GuiFont font, char_u *name UNUSED)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005582{
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005583 if (font != NOFONT)
5584 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005585 char *pangoname = pango_font_description_to_string(font);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005586
Bram Moolenaar89d40322006-08-29 15:30:07 +00005587 if (pangoname != NULL)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005588 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005589 char_u *s = vim_strsave((char_u *)pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005590
Bram Moolenaar89d40322006-08-29 15:30:07 +00005591 g_free(pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005592 return s;
5593 }
5594 }
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005595 return NULL;
5596}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005597#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005598
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599/*
5600 * If a font is not going to be used, free its structure.
5601 */
5602 void
5603gui_mch_free_font(GuiFont font)
5604{
5605 if (font != NOFONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 pango_font_description_free(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607}
5608
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609/*
Bram Moolenaar36edf062016-07-21 22:10:12 +02005610 * Return the Pixel value (color) for the given color name.
5611 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 * Return INVALCOLOR for error.
5613 */
5614 guicolor_T
5615gui_mch_get_color(char_u *name)
5616{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 if (!gui.in_use) /* can't do this when GUI not running */
5618 return INVALCOLOR;
5619
Bram Moolenaar98921892016-02-23 17:14:37 +01005620#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02005621 return name != NULL ? gui_get_color_cmn(name) : INVALCOLOR;
Bram Moolenaar98921892016-02-23 17:14:37 +01005622#else
Bram Moolenaar36edf062016-07-21 22:10:12 +02005623 guicolor_T color;
5624 GdkColor gcolor;
5625 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626
Bram Moolenaar36edf062016-07-21 22:10:12 +02005627 color = (name != NULL) ? gui_get_color_cmn(name) : INVALCOLOR;
5628 if (color == INVALCOLOR)
5629 return INVALCOLOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630
Bram Moolenaar36edf062016-07-21 22:10:12 +02005631 gcolor.red = (guint16)(((color & 0xff0000) >> 16) / 255.0 * 65535 + 0.5);
5632 gcolor.green = (guint16)(((color & 0xff00) >> 8) / 255.0 * 65535 + 0.5);
5633 gcolor.blue = (guint16)((color & 0xff) / 255.0 * 65535 + 0.5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634
Bram Moolenaar36edf062016-07-21 22:10:12 +02005635 ret = gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea),
5636 &gcolor, FALSE, TRUE);
5637
5638 return ret != 0 ? (guicolor_T)gcolor.pixel : INVALCOLOR;
5639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640}
5641
5642/*
5643 * Set the current text foreground color.
5644 */
5645 void
5646gui_mch_set_fg_color(guicolor_T color)
5647{
Bram Moolenaar36edf062016-07-21 22:10:12 +02005648#if GTK_CHECK_VERSION(3,0,0)
5649 *gui.fgcolor = color_to_rgba(color);
5650#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 gui.fgcolor->pixel = (unsigned long)color;
Bram Moolenaar36edf062016-07-21 22:10:12 +02005652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653}
5654
5655/*
5656 * Set the current text background color.
5657 */
5658 void
5659gui_mch_set_bg_color(guicolor_T color)
5660{
Bram Moolenaar36edf062016-07-21 22:10:12 +02005661#if GTK_CHECK_VERSION(3,0,0)
5662 *gui.bgcolor = color_to_rgba(color);
5663#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 gui.bgcolor->pixel = (unsigned long)color;
Bram Moolenaar36edf062016-07-21 22:10:12 +02005665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666}
5667
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005668/*
5669 * Set the current text special color.
5670 */
5671 void
5672gui_mch_set_sp_color(guicolor_T color)
5673{
Bram Moolenaar36edf062016-07-21 22:10:12 +02005674#if GTK_CHECK_VERSION(3,0,0)
5675 *gui.spcolor = color_to_rgba(color);
5676#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005677 gui.spcolor->pixel = (unsigned long)color;
Bram Moolenaar36edf062016-07-21 22:10:12 +02005678#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005679}
5680
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681/*
5682 * Function-like convenience macro for the sake of efficiency.
5683 */
5684#define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
5685 G_STMT_START{ \
5686 PangoAttribute *tmp_attr_; \
5687 tmp_attr_ = (Attribute); \
5688 tmp_attr_->start_index = (Start); \
5689 tmp_attr_->end_index = (End); \
5690 pango_attr_list_insert((AttrList), tmp_attr_); \
5691 }G_STMT_END
5692
5693 static void
5694apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list)
5695{
5696 char_u *start = NULL;
5697 char_u *p;
5698 int uc;
5699
5700 for (p = s; p < s + len; p += utf_byte2len(*p))
5701 {
5702 uc = utf_ptr2char(p);
5703
5704 if (start == NULL)
5705 {
5706 if (uc >= 0x80 && utf_char2cells(uc) == 2)
5707 start = p;
5708 }
5709 else if (uc < 0x80 /* optimization shortcut */
5710 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc)))
5711 {
5712 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5713 attr_list, start - s, p - s);
5714 start = NULL;
5715 }
5716 }
5717
5718 if (start != NULL)
5719 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5720 attr_list, start - s, len);
5721}
5722
5723 static int
5724count_cluster_cells(char_u *s, PangoItem *item,
5725 PangoGlyphString* glyphs, int i,
5726 int *cluster_width,
5727 int *last_glyph_rbearing)
5728{
5729 char_u *p;
5730 int next; /* glyph start index of next cluster */
5731 int start, end; /* string segment of current cluster */
5732 int width; /* real cluster width in Pango units */
5733 int uc;
5734 int cellcount = 0;
5735
5736 width = glyphs->glyphs[i].geometry.width;
5737
5738 for (next = i + 1; next < glyphs->num_glyphs; ++next)
5739 {
5740 if (glyphs->glyphs[next].attr.is_cluster_start)
5741 break;
5742 else if (glyphs->glyphs[next].geometry.width > width)
5743 width = glyphs->glyphs[next].geometry.width;
5744 }
5745
5746 start = item->offset + glyphs->log_clusters[i];
5747 end = item->offset + ((next < glyphs->num_glyphs) ?
5748 glyphs->log_clusters[next] : item->length);
5749
5750 for (p = s + start; p < s + end; p += utf_byte2len(*p))
5751 {
5752 uc = utf_ptr2char(p);
5753 if (uc < 0x80)
5754 ++cellcount;
5755 else if (!utf_iscomposing(uc))
5756 cellcount += utf_char2cells(uc);
5757 }
5758
5759 if (last_glyph_rbearing != NULL
5760 && cellcount > 0 && next == glyphs->num_glyphs)
5761 {
5762 PangoRectangle ink_rect;
5763 /*
5764 * If a certain combining mark had to be taken from a non-monospace
5765 * font, we have to compensate manually by adapting x_offset according
5766 * to the ink extents of the previous glyph.
5767 */
5768 pango_font_get_glyph_extents(item->analysis.font,
5769 glyphs->glyphs[i].glyph,
5770 &ink_rect, NULL);
5771
5772 if (PANGO_RBEARING(ink_rect) > 0)
5773 *last_glyph_rbearing = PANGO_RBEARING(ink_rect);
5774 }
5775
5776 if (cellcount > 0)
5777 *cluster_width = width;
5778
5779 return cellcount;
5780}
5781
5782/*
5783 * If there are only combining characters in the cluster, we cannot just
5784 * change the width of the previous glyph since there is none. Therefore
5785 * some guesswork is needed.
5786 *
5787 * If ink_rect.x is negative Pango apparently has taken care of the composing
5788 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
5789 * to problems with composing from different fonts we still need to fine-tune
Bram Moolenaar64404472010-06-26 06:24:45 +02005790 * x_offset to avoid ugliness.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 *
5792 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
5793 * the position of the previous glyph. Apparently this happens only with old
5794 * X fonts which don't provide the special combining information needed by
5795 * Pango.
5796 */
5797 static void
5798setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph,
5799 int last_cellcount, int last_cluster_width,
5800 int last_glyph_rbearing)
5801{
5802 PangoRectangle ink_rect;
5803 PangoRectangle logical_rect;
5804 int width;
5805
5806 width = last_cellcount * gui.char_width * PANGO_SCALE;
5807 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2;
5808 glyph->geometry.width = 0;
5809
5810 pango_font_get_glyph_extents(item->analysis.font,
5811 glyph->glyph,
5812 &ink_rect, &logical_rect);
5813 if (ink_rect.x < 0)
5814 {
5815 glyph->geometry.x_offset += last_glyph_rbearing;
5816 glyph->geometry.y_offset = logical_rect.height
5817 - (gui.char_height - p_linespace) * PANGO_SCALE;
5818 }
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005819 else
5820 /* If the accent width is smaller than the cluster width, position it
5821 * in the middle. */
5822 glyph->geometry.x_offset = -width + MAX(0, width - ink_rect.width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823}
5824
Bram Moolenaar98921892016-02-23 17:14:37 +01005825#if GTK_CHECK_VERSION(3,0,0)
5826 static void
5827draw_glyph_string(int row, int col, int num_cells, int flags,
5828 PangoFont *font, PangoGlyphString *glyphs,
5829 cairo_t *cr)
5830#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 static void
5832draw_glyph_string(int row, int col, int num_cells, int flags,
5833 PangoFont *font, PangoGlyphString *glyphs)
Bram Moolenaar98921892016-02-23 17:14:37 +01005834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835{
5836 if (!(flags & DRAW_TRANSP))
5837 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005838#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02005839 cairo_set_source_rgba(cr,
5840 gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue,
5841 gui.bgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005842 cairo_rectangle(cr,
5843 FILL_X(col), FILL_Y(row),
5844 num_cells * gui.char_width, gui.char_height);
5845 cairo_fill(cr);
5846#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
5848
5849 gdk_draw_rectangle(gui.drawarea->window,
5850 gui.text_gc,
5851 TRUE,
5852 FILL_X(col),
5853 FILL_Y(row),
5854 num_cells * gui.char_width,
5855 gui.char_height);
Bram Moolenaar98921892016-02-23 17:14:37 +01005856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 }
5858
Bram Moolenaar98921892016-02-23 17:14:37 +01005859#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02005860 cairo_set_source_rgba(cr,
5861 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5862 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005863 cairo_move_to(cr, TEXT_X(col), TEXT_Y(row));
5864 pango_cairo_show_glyph_string(cr, font, glyphs);
5865#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5867
5868 gdk_draw_glyphs(gui.drawarea->window,
5869 gui.text_gc,
5870 font,
5871 TEXT_X(col),
5872 TEXT_Y(row),
5873 glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01005874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875
5876 /* redraw the contents with an offset of 1 to emulate bold */
5877 if ((flags & DRAW_BOLD) && !gui.font_can_bold)
Bram Moolenaar98921892016-02-23 17:14:37 +01005878#if GTK_CHECK_VERSION(3,0,0)
5879 {
Bram Moolenaar36edf062016-07-21 22:10:12 +02005880 cairo_set_source_rgba(cr,
5881 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5882 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005883 cairo_move_to(cr, TEXT_X(col) + 1, TEXT_Y(row));
5884 pango_cairo_show_glyph_string(cr, font, glyphs);
5885 }
5886#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 gdk_draw_glyphs(gui.drawarea->window,
5888 gui.text_gc,
5889 font,
5890 TEXT_X(col) + 1,
5891 TEXT_Y(row),
5892 glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01005893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894}
5895
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005896/*
5897 * Draw underline and undercurl at the bottom of the character cell.
5898 */
Bram Moolenaar98921892016-02-23 17:14:37 +01005899#if GTK_CHECK_VERSION(3,0,0)
5900 static void
5901draw_under(int flags, int row, int col, int cells, cairo_t *cr)
5902#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005903 static void
5904draw_under(int flags, int row, int col, int cells)
Bram Moolenaar98921892016-02-23 17:14:37 +01005905#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005906{
5907 int i;
5908 int offset;
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005909 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005910 int y = FILL_Y(row + 1) - 1;
5911
5912 /* Undercurl: draw curl at the bottom of the character cell. */
5913 if (flags & DRAW_UNDERC)
5914 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005915#if GTK_CHECK_VERSION(3,0,0)
5916 cairo_set_line_width(cr, 1.0);
5917 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
Bram Moolenaar36edf062016-07-21 22:10:12 +02005918 cairo_set_source_rgba(cr,
5919 gui.spcolor->red, gui.spcolor->green, gui.spcolor->blue,
5920 gui.spcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005921 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
5922 {
5923 offset = val[i % 8];
5924 cairo_line_to(cr, i, y - offset + 0.5);
5925 }
5926 cairo_stroke(cr);
5927#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005928 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
5929 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
5930 {
5931 offset = val[i % 8];
5932 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset);
5933 }
5934 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar98921892016-02-23 17:14:37 +01005935#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005936 }
5937
5938 /* Underline: draw a line at the bottom of the character cell. */
5939 if (flags & DRAW_UNDERL)
5940 {
5941 /* When p_linespace is 0, overwrite the bottom row of pixels.
5942 * Otherwise put the line just below the character. */
5943 if (p_linespace > 1)
5944 y -= p_linespace - 1;
Bram Moolenaar98921892016-02-23 17:14:37 +01005945#if GTK_CHECK_VERSION(3,0,0)
5946 {
5947 cairo_set_line_width(cr, 1.0);
5948 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
Bram Moolenaar36edf062016-07-21 22:10:12 +02005949 cairo_set_source_rgba(cr,
5950 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5951 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005952 cairo_move_to(cr, FILL_X(col), y + 0.5);
5953 cairo_line_to(cr, FILL_X(col + cells), y + 0.5);
5954 cairo_stroke(cr);
5955 }
5956#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005957 gdk_draw_line(gui.drawarea->window, gui.text_gc,
5958 FILL_X(col), y,
5959 FILL_X(col + cells) - 1, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01005960#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005961 }
5962}
5963
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 int
5965gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
5966{
5967 GdkRectangle area; /* area for clip mask */
5968 PangoGlyphString *glyphs; /* glyphs of current item */
5969 int column_offset = 0; /* column offset in cells */
5970 int i;
5971 char_u *conv_buf = NULL; /* result of UTF-8 conversion */
5972 char_u *new_conv_buf;
5973 int convlen;
5974 char_u *sp, *bp;
5975 int plen;
Bram Moolenaar98921892016-02-23 17:14:37 +01005976#if GTK_CHECK_VERSION(3,0,0)
5977 cairo_t *cr;
5978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979
Bram Moolenaar98921892016-02-23 17:14:37 +01005980#if GTK_CHECK_VERSION(3,0,0)
5981 if (gui.text_context == NULL || gtk_widget_get_window(gui.drawarea) == NULL)
5982#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 if (gui.text_context == NULL || gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01005984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 return len;
5986
5987 if (output_conv.vc_type != CONV_NONE)
5988 {
5989 /*
5990 * Convert characters from 'encoding' to 'termencoding', which is set
5991 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
5992 * prohibits changing this to something else than UTF-8 if the GUI is
5993 * in use.
5994 */
5995 convlen = len;
5996 conv_buf = string_convert(&output_conv, s, &convlen);
5997 g_return_val_if_fail(conv_buf != NULL, len);
5998
5999 /* Correct for differences in char width: some chars are
6000 * double-wide in 'encoding' but single-wide in utf-8. Add a space to
6001 * compensate for that. */
6002 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; )
6003 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006004 plen = utf_ptr2len(bp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1)
6006 {
6007 new_conv_buf = alloc(convlen + 2);
6008 if (new_conv_buf == NULL)
6009 return len;
6010 plen += bp - conv_buf;
6011 mch_memmove(new_conv_buf, conv_buf, plen);
6012 new_conv_buf[plen] = ' ';
6013 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen,
6014 convlen - plen + 1);
6015 vim_free(conv_buf);
6016 conv_buf = new_conv_buf;
6017 ++convlen;
6018 bp = conv_buf + plen;
6019 plen = 1;
6020 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006021 sp += (*mb_ptr2len)(sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 bp += plen;
6023 }
6024 s = conv_buf;
6025 len = convlen;
6026 }
6027
6028 /*
6029 * Restrict all drawing to the current screen line in order to prevent
6030 * fuzzy font lookups from messing up the screen.
6031 */
6032 area.x = gui.border_offset;
6033 area.y = FILL_Y(row);
6034 area.width = gui.num_cols * gui.char_width;
6035 area.height = gui.char_height;
6036
Bram Moolenaar98921892016-02-23 17:14:37 +01006037#if GTK_CHECK_VERSION(3,0,0)
6038 cr = cairo_create(gui.surface);
6039 cairo_rectangle(cr, area.x, area.y, area.width, area.height);
6040 cairo_clip(cr);
6041#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 gdk_gc_set_clip_origin(gui.text_gc, 0, 0);
6043 gdk_gc_set_clip_rectangle(gui.text_gc, &area);
Bram Moolenaar98921892016-02-23 17:14:37 +01006044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045
6046 glyphs = pango_glyph_string_new();
6047
6048 /*
6049 * Optimization hack: If possible, skip the itemize and shaping process
6050 * for pure ASCII strings. This optimization is particularly effective
6051 * because Vim draws space characters to clear parts of the screen.
6052 */
6053 if (!(flags & DRAW_ITALIC)
6054 && !((flags & DRAW_BOLD) && gui.font_can_bold)
6055 && gui.ascii_glyphs != NULL)
6056 {
6057 char_u *p;
6058
6059 for (p = s; p < s + len; ++p)
6060 if (*p & 0x80)
6061 goto not_ascii;
6062
6063 pango_glyph_string_set_size(glyphs, len);
6064
6065 for (i = 0; i < len; ++i)
6066 {
Bram Moolenaar16350cb2016-08-14 20:27:34 +02006067 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[2 * s[i]];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 glyphs->log_clusters[i] = i;
6069 }
6070
Bram Moolenaar98921892016-02-23 17:14:37 +01006071#if GTK_CHECK_VERSION(3,0,0)
6072 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs, cr);
6073#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01006075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076
6077 column_offset = len;
6078 }
6079 else
6080not_ascii:
6081 {
6082 PangoAttrList *attr_list;
6083 GList *item_list;
6084 int cluster_width;
6085 int last_glyph_rbearing;
6086 int cells = 0; /* cells occupied by current cluster */
6087
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006088 /* Safety check: pango crashes when invoked with invalid utf-8
6089 * characters. */
6090 if (!utf_valid_string(s, s + len))
6091 {
6092 column_offset = len;
6093 goto skipitall;
6094 }
6095
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 /* original width of the current cluster */
6097 cluster_width = PANGO_SCALE * gui.char_width;
6098
6099 /* right bearing of the last non-composing glyph */
6100 last_glyph_rbearing = PANGO_SCALE * gui.char_width;
6101
6102 attr_list = pango_attr_list_new();
6103
6104 /* If 'guifontwide' is set then use that for double-width characters.
6105 * Otherwise just go with 'guifont' and let Pango do its thing. */
6106 if (gui.wide_font != NULL)
6107 apply_wide_font_attr(s, len, attr_list);
6108
6109 if ((flags & DRAW_BOLD) && gui.font_can_bold)
6110 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD),
6111 attr_list, 0, len);
6112 if (flags & DRAW_ITALIC)
6113 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC),
6114 attr_list, 0, len);
6115 /*
6116 * Break the text into segments with consistent directional level
6117 * and shaping engine. Pure Latin text needs only a single segment,
6118 * so there's no need to worry about the loop's efficiency. Better
6119 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
6120 */
6121 item_list = pango_itemize(gui.text_context,
6122 (const char *)s, 0, len, attr_list, NULL);
6123
6124 while (item_list != NULL)
6125 {
6126 PangoItem *item;
6127 int item_cells = 0; /* item length in cells */
6128
6129 item = (PangoItem *)item_list->data;
6130 item_list = g_list_delete_link(item_list, item_list);
6131 /*
6132 * Increment the bidirectional embedding level by 1 if it is not
6133 * even. An odd number means the output will be RTL, but we don't
6134 * want that since Vim handles right-to-left text on its own. It
6135 * would probably be sufficient to just set level = 0, but you can
6136 * never know :)
6137 *
6138 * Unfortunately we can't take advantage of Pango's ability to
6139 * render both LTR and RTL at the same time. In order to support
6140 * that, Vim's main screen engine would have to make use of Pango
6141 * functionality.
6142 */
6143 item->analysis.level = (item->analysis.level + 1) & (~1U);
6144
6145 /* HACK: Overrule the shape engine, we don't want shaping to be
6146 * done, because drawing the cursor would change the display. */
6147 item->analysis.shape_engine = default_shape_engine;
6148
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02006149#ifdef HAVE_PANGO_SHAPE_FULL
Bram Moolenaar7e2ec002015-09-08 16:31:06 +02006150 pango_shape_full((const char *)s + item->offset, item->length,
6151 (const char *)s, len, &item->analysis, glyphs);
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02006152#else
6153 pango_shape((const char *)s + item->offset, item->length,
6154 &item->analysis, glyphs);
6155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 /*
6157 * Fixed-width hack: iterate over the array and assign a fixed
6158 * width to each glyph, thus overriding the choice made by the
6159 * shaping engine. We use utf_char2cells() to determine the
6160 * number of cells needed.
6161 *
6162 * Also perform all kind of dark magic to get composing
6163 * characters right (and pretty too of course).
6164 */
6165 for (i = 0; i < glyphs->num_glyphs; ++i)
6166 {
6167 PangoGlyphInfo *glyph;
6168
6169 glyph = &glyphs->glyphs[i];
6170
6171 if (glyph->attr.is_cluster_start)
6172 {
6173 int cellcount;
6174
6175 cellcount = count_cluster_cells(
6176 s, item, glyphs, i, &cluster_width,
6177 (item_list != NULL) ? &last_glyph_rbearing : NULL);
6178
6179 if (cellcount > 0)
6180 {
6181 int width;
6182
6183 width = cellcount * gui.char_width * PANGO_SCALE;
6184 glyph->geometry.x_offset +=
6185 MAX(0, width - cluster_width) / 2;
6186 glyph->geometry.width = width;
6187 }
6188 else
6189 {
6190 /* If there are only combining characters in the
6191 * cluster, we cannot just change the width of the
6192 * previous glyph since there is none. Therefore
6193 * some guesswork is needed. */
6194 setup_zero_width_cluster(item, glyph, cells,
6195 cluster_width,
6196 last_glyph_rbearing);
6197 }
6198
6199 item_cells += cellcount;
6200 cells = cellcount;
6201 }
6202 else if (i > 0)
6203 {
6204 int width;
6205
6206 /* There is a previous glyph, so we deal with combining
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006207 * characters the canonical way.
Bram Moolenaar96118f32010-08-08 14:40:37 +02006208 * In some circumstances Pango uses a positive x_offset,
6209 * then use the width of the previous glyph for this one
6210 * and set the previous width to zero.
6211 * Otherwise we get a negative x_offset, Pango has already
6212 * positioned the combining char, keep the widths as they
6213 * are.
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006214 * For both adjust the x_offset to position the glyph in
Bram Moolenaar96118f32010-08-08 14:40:37 +02006215 * the middle. */
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006216 if (glyph->geometry.x_offset >= 0)
Bram Moolenaar96118f32010-08-08 14:40:37 +02006217 {
6218 glyphs->glyphs[i].geometry.width =
6219 glyphs->glyphs[i - 1].geometry.width;
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006220 glyphs->glyphs[i - 1].geometry.width = 0;
Bram Moolenaar96118f32010-08-08 14:40:37 +02006221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 width = cells * gui.char_width * PANGO_SCALE;
6223 glyph->geometry.x_offset +=
6224 MAX(0, width - cluster_width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 }
6226 else /* i == 0 "cannot happen" */
6227 {
6228 glyph->geometry.width = 0;
6229 }
6230 }
6231
6232 /*** Aaaaand action! ***/
Bram Moolenaar98921892016-02-23 17:14:37 +01006233#if GTK_CHECK_VERSION(3,0,0)
6234 draw_glyph_string(row, col + column_offset, item_cells,
6235 flags, item->analysis.font, glyphs,
6236 cr);
6237#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 draw_glyph_string(row, col + column_offset, item_cells,
6239 flags, item->analysis.font, glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01006240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241
6242 pango_item_free(item);
6243
6244 column_offset += item_cells;
6245 }
6246
6247 pango_attr_list_unref(attr_list);
6248 }
6249
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006250skipitall:
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006251 /* Draw underline and undercurl. */
Bram Moolenaar98921892016-02-23 17:14:37 +01006252#if GTK_CHECK_VERSION(3,0,0)
6253 draw_under(flags, row, col, column_offset, cr);
6254#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006255 draw_under(flags, row, col, column_offset);
Bram Moolenaar98921892016-02-23 17:14:37 +01006256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257
6258 pango_glyph_string_free(glyphs);
6259 vim_free(conv_buf);
6260
Bram Moolenaar98921892016-02-23 17:14:37 +01006261#if GTK_CHECK_VERSION(3,0,0)
6262 cairo_destroy(cr);
6263 if (!gui.by_signal)
6264 gdk_window_invalidate_rect(gtk_widget_get_window(gui.drawarea),
6265 &area, FALSE);
6266#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 gdk_gc_set_clip_rectangle(gui.text_gc, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01006268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269
6270 return column_offset;
6271}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272
6273/*
6274 * Return OK if the key with the termcap name "name" is supported.
6275 */
6276 int
6277gui_mch_haskey(char_u *name)
6278{
6279 int i;
6280
6281 for (i = 0; special_keys[i].key_sym != 0; i++)
6282 if (name[0] == special_keys[i].code0
6283 && name[1] == special_keys[i].code1)
6284 return OK;
6285 return FAIL;
6286}
6287
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006288#if defined(FEAT_TITLE) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289/*
6290 * Return the text window-id and display. Only required for X-based GUI's
6291 */
6292 int
6293gui_get_x11_windis(Window *win, Display **dis)
6294{
Bram Moolenaar98921892016-02-23 17:14:37 +01006295#if GTK_CHECK_VERSION(3,0,0)
6296 if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL)
6297#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01006299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 {
Bram Moolenaar98921892016-02-23 17:14:37 +01006301#if GTK_CHECK_VERSION(3,0,0)
6302 *dis = GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin));
6303 *win = GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin));
6304#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 *dis = GDK_WINDOW_XDISPLAY(gui.mainwin->window);
6306 *win = GDK_WINDOW_XWINDOW(gui.mainwin->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01006307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 return OK;
6309 }
6310
6311 *dis = NULL;
6312 *win = 0;
6313 return FAIL;
6314}
6315#endif
6316
6317#if defined(FEAT_CLIENTSERVER) \
6318 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
6319
6320 Display *
6321gui_mch_get_display(void)
6322{
Bram Moolenaar98921892016-02-23 17:14:37 +01006323#if GTK_CHECK_VERSION(3,0,0)
6324 if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL)
6325 return GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin));
6326#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
6328 return GDK_WINDOW_XDISPLAY(gui.mainwin->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01006329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 else
6331 return NULL;
6332}
6333#endif
6334
6335 void
6336gui_mch_beep(void)
6337{
6338#ifdef HAVE_GTK_MULTIHEAD
6339 GdkDisplay *display;
6340
Bram Moolenaar98921892016-02-23 17:14:37 +01006341# if GTK_CHECK_VERSION(3,0,0)
6342 if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin))
6343# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
Bram Moolenaar98921892016-02-23 17:14:37 +01006345# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 display = gtk_widget_get_display(gui.mainwin);
6347 else
6348 display = gdk_display_get_default();
6349
6350 if (display != NULL)
6351 gdk_display_beep(display);
6352#else
6353 gdk_beep();
6354#endif
6355}
6356
6357 void
6358gui_mch_flash(int msec)
6359{
Bram Moolenaar98921892016-02-23 17:14:37 +01006360#if GTK_CHECK_VERSION(3,0,0)
6361 /* TODO Replace GdkGC with Cairo */
6362 (void)msec;
6363#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 GdkGCValues values;
6365 GdkGC *invert_gc;
6366
6367 if (gui.drawarea->window == NULL)
6368 return;
6369
6370 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6371 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
6372 values.function = GDK_XOR;
6373 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6374 &values,
6375 GDK_GC_FOREGROUND |
6376 GDK_GC_BACKGROUND |
6377 GDK_GC_FUNCTION);
6378 gdk_gc_set_exposures(invert_gc,
6379 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
6380 /*
6381 * Do a visual beep by changing back and forth in some undetermined way,
6382 * the foreground and background colors. This is due to the fact that
6383 * there can't be really any prediction about the effects of XOR on
6384 * arbitrary X11 servers. However this seems to be enough for what we
6385 * intend it to do.
6386 */
6387 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6388 TRUE,
6389 0, 0,
6390 FILL_X((int)Columns) + gui.border_offset,
6391 FILL_Y((int)Rows) + gui.border_offset);
6392
6393 gui_mch_flush();
6394 ui_delay((long)msec, TRUE); /* wait so many msec */
6395
6396 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6397 TRUE,
6398 0, 0,
6399 FILL_X((int)Columns) + gui.border_offset,
6400 FILL_Y((int)Rows) + gui.border_offset);
6401
6402 gdk_gc_destroy(invert_gc);
Bram Moolenaar98921892016-02-23 17:14:37 +01006403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404}
6405
6406/*
6407 * Invert a rectangle from row r, column c, for nr rows and nc columns.
6408 */
6409 void
6410gui_mch_invert_rectangle(int r, int c, int nr, int nc)
6411{
Bram Moolenaar98921892016-02-23 17:14:37 +01006412#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar4fc563b2016-03-12 12:40:58 +01006413 const GdkRectangle rect = {
6414 FILL_X(c), FILL_Y(r), nc * gui.char_width, nr * gui.char_height
6415 };
6416 cairo_t * const cr = cairo_create(gui.surface);
6417
Bram Moolenaar36edf062016-07-21 22:10:12 +02006418 set_cairo_source_rgba_from_color(cr, gui.norm_pixel ^ gui.back_pixel);
Bram Moolenaar4fc563b2016-03-12 12:40:58 +01006419# if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,9,2)
6420 cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE);
6421# else
6422 /* Give an implementation for older cairo versions if necessary. */
6423# endif
6424 gdk_cairo_rectangle(cr, &rect);
6425 cairo_fill(cr);
6426
6427 cairo_destroy(cr);
6428
6429 if (!gui.by_signal)
6430 gtk_widget_queue_draw_area(gui.drawarea, rect.x, rect.y,
6431 rect.width, rect.height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006432#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 GdkGCValues values;
6434 GdkGC *invert_gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435
6436 if (gui.drawarea->window == NULL)
6437 return;
6438
Bram Moolenaar89d40322006-08-29 15:30:07 +00006439 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6440 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 values.function = GDK_XOR;
6442 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6443 &values,
6444 GDK_GC_FOREGROUND |
6445 GDK_GC_BACKGROUND |
6446 GDK_GC_FUNCTION);
Bram Moolenaar89d40322006-08-29 15:30:07 +00006447 gdk_gc_set_exposures(invert_gc, gui.visibility !=
6448 GDK_VISIBILITY_UNOBSCURED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6450 TRUE,
6451 FILL_X(c), FILL_Y(r),
6452 (nc) * gui.char_width, (nr) * gui.char_height);
6453 gdk_gc_destroy(invert_gc);
Bram Moolenaar98921892016-02-23 17:14:37 +01006454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455}
6456
6457/*
6458 * Iconify the GUI window.
6459 */
6460 void
6461gui_mch_iconify(void)
6462{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 gtk_window_iconify(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464}
6465
6466#if defined(FEAT_EVAL) || defined(PROTO)
6467/*
6468 * Bring the Vim window to the foreground.
6469 */
6470 void
6471gui_mch_set_foreground(void)
6472{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 gtk_window_present(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474}
6475#endif
6476
6477/*
6478 * Draw a cursor without focus.
6479 */
6480 void
6481gui_mch_draw_hollow_cursor(guicolor_T color)
6482{
6483 int i = 1;
Bram Moolenaar98921892016-02-23 17:14:37 +01006484#if GTK_CHECK_VERSION(3,0,0)
6485 cairo_t *cr;
6486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487
Bram Moolenaar98921892016-02-23 17:14:37 +01006488#if GTK_CHECK_VERSION(3,0,0)
6489 if (gtk_widget_get_window(gui.drawarea) == NULL)
6490#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 if (gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01006492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 return;
6494
Bram Moolenaar98921892016-02-23 17:14:37 +01006495#if GTK_CHECK_VERSION(3,0,0)
6496 cr = cairo_create(gui.surface);
6497#endif
6498
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 gui_mch_set_fg_color(color);
6500
Bram Moolenaar98921892016-02-23 17:14:37 +01006501#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02006502 cairo_set_source_rgba(cr,
6503 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
6504 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01006505#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar98921892016-02-23 17:14:37 +01006507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 if (mb_lefthalve(gui.row, gui.col))
6509 i = 2;
Bram Moolenaar98921892016-02-23 17:14:37 +01006510#if GTK_CHECK_VERSION(3,0,0)
6511 cairo_set_line_width(cr, 1.0);
6512 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
6513 cairo_rectangle(cr,
6514 FILL_X(gui.col) + 0.5, FILL_Y(gui.row) + 0.5,
6515 i * gui.char_width - 1, gui.char_height - 1);
6516 cairo_stroke(cr);
6517 cairo_destroy(cr);
6518#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6520 FALSE,
6521 FILL_X(gui.col), FILL_Y(gui.row),
6522 i * gui.char_width - 1, gui.char_height - 1);
Bram Moolenaar98921892016-02-23 17:14:37 +01006523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524}
6525
6526/*
6527 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
6528 * color "color".
6529 */
6530 void
6531gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
6532{
Bram Moolenaar98921892016-02-23 17:14:37 +01006533#if GTK_CHECK_VERSION(3,0,0)
6534 if (gtk_widget_get_window(gui.drawarea) == NULL)
6535#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 if (gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01006537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 return;
6539
6540 gui_mch_set_fg_color(color);
6541
Bram Moolenaar98921892016-02-23 17:14:37 +01006542#if GTK_CHECK_VERSION(3,0,0)
6543 {
6544 cairo_t *cr;
6545
6546 cr = cairo_create(gui.surface);
Bram Moolenaar36edf062016-07-21 22:10:12 +02006547 cairo_set_source_rgba(cr,
6548 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
6549 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01006550 cairo_rectangle(cr,
6551# ifdef FEAT_RIGHTLEFT
6552 /* vertical line should be on the right of current point */
6553 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
6554# endif
6555 FILL_X(gui.col), FILL_Y(gui.row) + gui.char_height - h,
6556 w, h);
6557 cairo_fill(cr);
6558 cairo_destroy(cr);
6559 }
6560#else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6562 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6563 TRUE,
Bram Moolenaar98921892016-02-23 17:14:37 +01006564# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 /* vertical line should be on the right of current point */
6566 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
Bram Moolenaar98921892016-02-23 17:14:37 +01006567# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 FILL_X(gui.col),
6569 FILL_Y(gui.row) + gui.char_height - h,
6570 w, h);
Bram Moolenaar98921892016-02-23 17:14:37 +01006571#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572}
6573
6574
6575/*
6576 * Catch up with any queued X11 events. This may put keyboard input into the
6577 * input buffer, call resize call-backs, trigger timers etc. If there is
6578 * nothing in the X11 event queue (& no timers pending), then we return
6579 * immediately.
6580 */
6581 void
6582gui_mch_update(void)
6583{
Bram Moolenaara3f41662010-07-11 19:01:06 +02006584 while (g_main_context_pending(NULL) && !vim_is_input_buf_full())
6585 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586}
6587
Bram Moolenaar98921892016-02-23 17:14:37 +01006588#if GTK_CHECK_VERSION(3,0,0)
6589 static gboolean
6590#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +01006592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593input_timer_cb(gpointer data)
6594{
6595 int *timed_out = (int *) data;
6596
Bram Moolenaar49325942007-05-10 19:19:59 +00006597 /* Just inform the caller about the occurrence of it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 *timed_out = TRUE;
6599
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 return FALSE; /* don't happen again */
6601}
6602
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603/*
6604 * GUI input routine called by gui_wait_for_chars(). Waits for a character
6605 * from the keyboard.
6606 * wtime == -1 Wait forever.
6607 * wtime == 0 This should never happen.
6608 * wtime > 0 Wait wtime milliseconds for a character.
6609 * Returns OK if a character was found to be available within the given time,
6610 * or FAIL otherwise.
6611 */
6612 int
6613gui_mch_wait_for_chars(long wtime)
6614{
Bram Moolenaar4231da42016-06-02 14:30:04 +02006615 int focus;
6616 guint timer;
6617 static int timed_out;
6618 int retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619
6620 timed_out = FALSE;
6621
6622 /* this timeout makes sure that we will return if no characters arrived in
6623 * time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 if (wtime > 0)
Bram Moolenaar98921892016-02-23 17:14:37 +01006625#if GTK_CHECK_VERSION(3,0,0)
6626 timer = g_timeout_add((guint)wtime, input_timer_cb, &timed_out);
6627#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 timer = gtk_timeout_add((guint32)wtime, input_timer_cb, &timed_out);
Bram Moolenaar98921892016-02-23 17:14:37 +01006629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 else
6631 timer = 0;
6632
6633 focus = gui.in_focus;
6634
6635 do
6636 {
6637 /* Stop or start blinking when focus changes */
6638 if (gui.in_focus != focus)
6639 {
6640 if (gui.in_focus)
6641 gui_mch_start_blink();
6642 else
6643 gui_mch_stop_blink();
6644 focus = gui.in_focus;
6645 }
6646
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006647#ifdef MESSAGE_QUEUE
Bram Moolenaar4231da42016-06-02 14:30:04 +02006648# ifdef FEAT_TIMERS
6649 did_add_timer = FALSE;
6650# endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006651 parse_queued_messages();
Bram Moolenaar4231da42016-06-02 14:30:04 +02006652# ifdef FEAT_TIMERS
6653 if (did_add_timer)
6654 /* Need to recompute the waiting time. */
6655 goto theend;
6656# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006657#endif
6658
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 /*
6660 * Loop in GTK+ processing until a timeout or input occurs.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006661 * Skip this if input is available anyway (can happen in rare
6662 * situations, sort of race condition).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006664 if (!input_available())
Bram Moolenaara3f41662010-07-11 19:01:06 +02006665 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666
6667 /* Got char, return immediately */
6668 if (input_available())
6669 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02006670 retval = OK;
6671 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 }
6673 } while (wtime < 0 || !timed_out);
6674
6675 /*
6676 * Flush all eventually pending (drawing) events.
6677 */
6678 gui_mch_update();
6679
Bram Moolenaar4231da42016-06-02 14:30:04 +02006680theend:
6681 if (timer != 0 && !timed_out)
6682#if GTK_CHECK_VERSION(3,0,0)
6683 g_source_remove(timer);
6684#else
6685 gtk_timeout_remove(timer);
6686#endif
6687
6688 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689}
6690
6691
6692/****************************************************************************
6693 * Output drawing routines.
6694 ****************************************************************************/
6695
6696
6697/* Flush any output to the screen */
6698 void
6699gui_mch_flush(void)
6700{
6701#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar98921892016-02-23 17:14:37 +01006702# if GTK_CHECK_VERSION(3,0,0)
6703 if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin))
6704# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
Bram Moolenaar98921892016-02-23 17:14:37 +01006706# endif
Bram Moolenaarfdadad92016-07-15 17:49:58 +02006707 gdk_display_flush(gtk_widget_get_display(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708#else
6709 gdk_flush(); /* historical misnomer: calls XSync(), not XFlush() */
6710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711}
6712
6713/*
6714 * Clear a rectangular region of the screen from text pos (row1, col1) to
6715 * (row2, col2) inclusive.
6716 */
6717 void
Bram Moolenaarfe344a92017-02-23 12:20:35 +01006718gui_mch_clear_block(int row1arg, int col1arg, int row2arg, int col2arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719{
Bram Moolenaarfe344a92017-02-23 12:20:35 +01006720
6721 int col1 = check_col(col1arg);
6722 int col2 = check_col(col2arg);
6723 int row1 = check_row(row1arg);
6724 int row2 = check_row(row2arg);
6725
Bram Moolenaar98921892016-02-23 17:14:37 +01006726#if GTK_CHECK_VERSION(3,0,0)
6727 if (gtk_widget_get_window(gui.drawarea) == NULL)
6728 return;
6729#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 GdkColor color;
6731
6732 if (gui.drawarea->window == NULL)
6733 return;
6734
6735 color.pixel = gui.back_pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01006736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737
Bram Moolenaar98921892016-02-23 17:14:37 +01006738#if GTK_CHECK_VERSION(3,0,0)
6739 {
6740 /* Add one pixel to the far right column in case a double-stroked
6741 * bold glyph may sit there. */
6742 const GdkRectangle rect = {
6743 FILL_X(col1), FILL_Y(row1),
6744 (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1),
6745 (row2 - row1 + 1) * gui.char_height
6746 };
6747 GdkWindow * const win = gtk_widget_get_window(gui.drawarea);
6748 cairo_t * const cr = cairo_create(gui.surface);
Bram Moolenaara859f042016-11-17 19:11:55 +01006749# if GTK_CHECK_VERSION(3,22,2)
6750 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
6751# else
Bram Moolenaar98921892016-02-23 17:14:37 +01006752 cairo_pattern_t * const pat = gdk_window_get_background_pattern(win);
6753 if (pat != NULL)
6754 cairo_set_source(cr, pat);
6755 else
Bram Moolenaar36edf062016-07-21 22:10:12 +02006756 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
Bram Moolenaara859f042016-11-17 19:11:55 +01006757# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01006758 gdk_cairo_rectangle(cr, &rect);
6759 cairo_fill(cr);
6760 cairo_destroy(cr);
6761
6762 if (!gui.by_signal)
6763 gdk_window_invalidate_rect(win, &rect, FALSE);
6764 }
6765#else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 gdk_gc_set_foreground(gui.text_gc, &color);
6767
6768 /* Clear one extra pixel at the far right, for when bold characters have
6769 * spilled over to the window border. */
6770 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE,
6771 FILL_X(col1), FILL_Y(row1),
6772 (col2 - col1 + 1) * gui.char_width
6773 + (col2 == Columns - 1),
6774 (row2 - row1 + 1) * gui.char_height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006775#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776}
6777
Bram Moolenaar98921892016-02-23 17:14:37 +01006778#if GTK_CHECK_VERSION(3,0,0)
6779 static void
6780gui_gtk_window_clear(GdkWindow *win)
6781{
6782 const GdkRectangle rect = {
6783 0, 0, gdk_window_get_width(win), gdk_window_get_height(win)
6784 };
6785 cairo_t * const cr = cairo_create(gui.surface);
Bram Moolenaara859f042016-11-17 19:11:55 +01006786# if GTK_CHECK_VERSION(3,22,2)
6787 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
6788# else
Bram Moolenaar98921892016-02-23 17:14:37 +01006789 cairo_pattern_t * const pat = gdk_window_get_background_pattern(win);
6790 if (pat != NULL)
6791 cairo_set_source(cr, pat);
6792 else
Bram Moolenaar36edf062016-07-21 22:10:12 +02006793 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
Bram Moolenaara859f042016-11-17 19:11:55 +01006794# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01006795 gdk_cairo_rectangle(cr, &rect);
6796 cairo_fill(cr);
6797 cairo_destroy(cr);
6798
6799 if (!gui.by_signal)
6800 gdk_window_invalidate_rect(win, &rect, FALSE);
6801}
6802#endif
6803
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 void
6805gui_mch_clear_all(void)
6806{
Bram Moolenaar98921892016-02-23 17:14:37 +01006807#if GTK_CHECK_VERSION(3,0,0)
6808 if (gtk_widget_get_window(gui.drawarea) != NULL)
6809 gui_gtk_window_clear(gtk_widget_get_window(gui.drawarea));
6810#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 if (gui.drawarea->window != NULL)
6812 gdk_window_clear(gui.drawarea->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01006813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814}
6815
Bram Moolenaar98921892016-02-23 17:14:37 +01006816#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817/*
6818 * Redraw any text revealed by scrolling up/down.
6819 */
6820 static void
6821check_copy_area(void)
6822{
6823 GdkEvent *event;
6824 int expose_count;
6825
6826 if (gui.visibility != GDK_VISIBILITY_PARTIAL)
6827 return;
6828
6829 /* Avoid redrawing the cursor while scrolling or it'll end up where
6830 * we don't want it to be. I'm not sure if it's correct to call
6831 * gui_dont_update_cursor() at this point but it works as a quick
6832 * fix for now. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02006833 gui_dont_update_cursor(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834
6835 do
6836 {
6837 /* Wait to check whether the scroll worked or not. */
6838 event = gdk_event_get_graphics_expose(gui.drawarea->window);
6839
6840 if (event == NULL)
6841 break; /* received NoExpose event */
6842
6843 gui_redraw(event->expose.area.x, event->expose.area.y,
6844 event->expose.area.width, event->expose.area.height);
6845
6846 expose_count = event->expose.count;
6847 gdk_event_free(event);
6848 }
6849 while (expose_count > 0); /* more events follow */
6850
6851 gui_can_update_cursor();
6852}
Bram Moolenaar98921892016-02-23 17:14:37 +01006853#endif /* !GTK_CHECK_VERSION(3,0,0) */
6854
6855#if GTK_CHECK_VERSION(3,0,0)
6856 static void
6857gui_gtk_surface_copy_rect(int dest_x, int dest_y,
6858 int src_x, int src_y,
6859 int width, int height)
6860{
6861 cairo_t * const cr = cairo_create(gui.surface);
6862
6863 cairo_rectangle(cr, dest_x, dest_y, width, height);
6864 cairo_clip(cr);
6865 cairo_push_group(cr);
6866 cairo_set_source_surface(cr, gui.surface, dest_x - src_x, dest_y - src_y);
6867 cairo_paint(cr);
6868 cairo_pop_group_to_source(cr);
6869 cairo_paint(cr);
6870
6871 cairo_destroy(cr);
6872}
6873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874
6875/*
6876 * Delete the given number of lines from the given row, scrolling up any
6877 * text further down within the scroll region.
6878 */
6879 void
6880gui_mch_delete_lines(int row, int num_lines)
6881{
Bram Moolenaar98921892016-02-23 17:14:37 +01006882#if GTK_CHECK_VERSION(3,0,0)
6883 const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1;
6884 const int nrows = gui.scroll_region_bot - row + 1;
6885 const int src_nrows = nrows - num_lines;
6886
6887 gui_gtk_surface_copy_rect(
6888 FILL_X(gui.scroll_region_left), FILL_Y(row),
6889 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6890 gui.char_width * ncols + 1, gui.char_height * src_nrows);
6891 gui_clear_block(
6892 gui.scroll_region_bot - num_lines + 1, gui.scroll_region_left,
6893 gui.scroll_region_bot, gui.scroll_region_right);
6894 gui_gtk3_redraw(
6895 FILL_X(gui.scroll_region_left), FILL_Y(row),
6896 gui.char_width * ncols + 1, gui.char_height * nrows);
6897 if (!gui.by_signal)
6898 gtk_widget_queue_draw_area(gui.drawarea,
6899 FILL_X(gui.scroll_region_left), FILL_Y(row),
6900 gui.char_width * ncols + 1, gui.char_height * nrows);
6901#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
6903 return; /* Can't see the window */
6904
6905 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6906 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6907
6908 /* copy one extra pixel, for when bold has spilled over */
6909 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6910 FILL_X(gui.scroll_region_left), FILL_Y(row),
6911 gui.drawarea->window,
6912 FILL_X(gui.scroll_region_left),
6913 FILL_Y(row + num_lines),
6914 gui.char_width * (gui.scroll_region_right
6915 - gui.scroll_region_left + 1) + 1,
6916 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6917
6918 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
6919 gui.scroll_region_left,
6920 gui.scroll_region_bot, gui.scroll_region_right);
6921 check_copy_area();
Bram Moolenaar98921892016-02-23 17:14:37 +01006922#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923}
6924
6925/*
6926 * Insert the given number of lines before the given row, scrolling down any
6927 * following text within the scroll region.
6928 */
6929 void
6930gui_mch_insert_lines(int row, int num_lines)
6931{
Bram Moolenaar98921892016-02-23 17:14:37 +01006932#if GTK_CHECK_VERSION(3,0,0)
6933 const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1;
6934 const int nrows = gui.scroll_region_bot - row + 1;
6935 const int src_nrows = nrows - num_lines;
6936
6937 gui_gtk_surface_copy_rect(
6938 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6939 FILL_X(gui.scroll_region_left), FILL_Y(row),
6940 gui.char_width * ncols + 1, gui.char_height * src_nrows);
6941 gui_mch_clear_block(
6942 row, gui.scroll_region_left,
6943 row + num_lines - 1, gui.scroll_region_right);
6944 gui_gtk3_redraw(
6945 FILL_X(gui.scroll_region_left), FILL_Y(row),
6946 gui.char_width * ncols + 1, gui.char_height * nrows);
6947 if (!gui.by_signal)
6948 gtk_widget_queue_draw_area(gui.drawarea,
6949 FILL_X(gui.scroll_region_left), FILL_Y(row),
6950 gui.char_width * ncols + 1, gui.char_height * nrows);
6951#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
6953 return; /* Can't see the window */
6954
6955 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6956 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6957
6958 /* copy one extra pixel, for when bold has spilled over */
6959 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6960 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6961 gui.drawarea->window,
6962 FILL_X(gui.scroll_region_left), FILL_Y(row),
6963 gui.char_width * (gui.scroll_region_right
6964 - gui.scroll_region_left + 1) + 1,
6965 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6966
6967 gui_clear_block(row, gui.scroll_region_left,
6968 row + num_lines - 1, gui.scroll_region_right);
6969 check_copy_area();
Bram Moolenaar98921892016-02-23 17:14:37 +01006970#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971}
6972
6973/*
6974 * X Selection stuff, for cutting and pasting text to other windows.
6975 */
6976 void
6977clip_mch_request_selection(VimClipboard *cbd)
6978{
6979 GdkAtom target;
6980 unsigned i;
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00006981 time_t start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982
6983 for (i = 0; i < N_SELECTION_TARGETS; ++i)
6984 {
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006985 if (!clip_html && selection_targets[i].info == TARGET_HTML)
6986 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 received_selection = RS_NONE;
6988 target = gdk_atom_intern(selection_targets[i].target, FALSE);
6989
6990 gtk_selection_convert(gui.drawarea,
6991 cbd->gtk_sel_atom, target,
6992 (guint32)GDK_CURRENT_TIME);
6993
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00006994 /* Hack: Wait up to three seconds for the selection. A hang was
6995 * noticed here when using the netrw plugin combined with ":gui"
6996 * during the FocusGained event. */
6997 start = time(NULL);
6998 while (received_selection == RS_NONE && time(NULL) < start + 3)
Bram Moolenaara3f41662010-07-11 19:01:06 +02006999 g_main_context_iteration(NULL, TRUE); /* wait for selection_received_cb */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000
7001 if (received_selection != RS_FAIL)
7002 return;
7003 }
7004
7005 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaar98921892016-02-23 17:14:37 +01007006#if GTK_CHECK_VERSION(3,0,0)
7007 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)),
7008 cbd);
7009#else
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00007010 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gui.mainwin->window), cbd);
Bram Moolenaar98921892016-02-23 17:14:37 +01007011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012}
7013
7014/*
7015 * Disown the selection.
7016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00007018clip_mch_lose_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019{
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01007020 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022}
7023
7024/*
7025 * Own the selection and return OK if it worked.
7026 */
7027 int
7028clip_mch_own_selection(VimClipboard *cbd)
7029{
7030 int success;
7031
7032 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom,
Bram Moolenaar20892c12011-06-26 04:49:00 +02007033 gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 gui_mch_update();
7035 return (success) ? OK : FAIL;
7036}
7037
7038/*
7039 * Send the current selection to the clipboard. Do nothing for X because we
7040 * will fill in the selection only when requested by another app.
7041 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00007043clip_mch_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044{
7045}
7046
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01007047 int
7048clip_gtk_owner_exists(VimClipboard *cbd)
7049{
7050 return gdk_selection_owner_get(cbd->gtk_sel_atom) != NULL;
7051}
7052
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053
7054#if defined(FEAT_MENU) || defined(PROTO)
7055/*
7056 * Make a menu item appear either active or not active (grey or not grey).
7057 */
7058 void
7059gui_mch_menu_grey(vimmenu_T *menu, int grey)
7060{
7061 if (menu->id == NULL)
7062 return;
7063
7064 if (menu_is_separator(menu->name))
7065 grey = TRUE;
7066
7067 gui_mch_menu_hidden(menu, FALSE);
7068 /* Be clever about bitfields versus true booleans here! */
Bram Moolenaar98921892016-02-23 17:14:37 +01007069# if GTK_CHECK_VERSION(3,0,0)
7070 if (!gtk_widget_get_sensitive(menu->id) == !grey)
7071# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 if (!GTK_WIDGET_SENSITIVE(menu->id) == !grey)
Bram Moolenaar98921892016-02-23 17:14:37 +01007073# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 {
7075 gtk_widget_set_sensitive(menu->id, !grey);
7076 gui_mch_update();
7077 }
7078}
7079
7080/*
7081 * Make menu item hidden or not hidden.
7082 */
7083 void
7084gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
7085{
7086 if (menu->id == 0)
7087 return;
7088
7089 if (hidden)
7090 {
Bram Moolenaar98921892016-02-23 17:14:37 +01007091# if GTK_CHECK_VERSION(3,0,0)
7092 if (gtk_widget_get_visible(menu->id))
7093# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 if (GTK_WIDGET_VISIBLE(menu->id))
Bram Moolenaar98921892016-02-23 17:14:37 +01007095# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 {
7097 gtk_widget_hide(menu->id);
7098 gui_mch_update();
7099 }
7100 }
7101 else
7102 {
Bram Moolenaar98921892016-02-23 17:14:37 +01007103# if GTK_CHECK_VERSION(3,0,0)
7104 if (!gtk_widget_get_visible(menu->id))
7105# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 if (!GTK_WIDGET_VISIBLE(menu->id))
Bram Moolenaar98921892016-02-23 17:14:37 +01007107# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 {
7109 gtk_widget_show(menu->id);
7110 gui_mch_update();
7111 }
7112 }
7113}
7114
7115/*
7116 * This is called after setting all the menus to grey/hidden or not.
7117 */
7118 void
7119gui_mch_draw_menubar(void)
7120{
7121 /* just make sure that the visual changes get effect immediately */
7122 gui_mch_update();
7123}
7124#endif /* FEAT_MENU */
7125
7126/*
7127 * Scrollbar stuff.
7128 */
7129 void
7130gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
7131{
7132 if (sb->id == NULL)
7133 return;
7134
Bram Moolenaar98921892016-02-23 17:14:37 +01007135#if GTK_CHECK_VERSION(3,0,0)
7136 gtk_widget_set_visible(sb->id, flag);
7137#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 if (flag)
7139 gtk_widget_show(sb->id);
7140 else
7141 gtk_widget_hide(sb->id);
Bram Moolenaar98921892016-02-23 17:14:37 +01007142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00007144 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145}
7146
7147
7148/*
7149 * Return the RGB value of a pixel as long.
7150 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007151 guicolor_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152gui_mch_get_rgb(guicolor_T pixel)
7153{
Bram Moolenaar98921892016-02-23 17:14:37 +01007154#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02007155 return (long_u)pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01007156#else
Bram Moolenaar36edf062016-07-21 22:10:12 +02007157 GdkColor color;
7158
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
7160 (unsigned long)pixel, &color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007162 return (guicolor_T)(
7163 (((unsigned)color.red & 0xff00) << 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 | ((unsigned)color.green & 0xff00)
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007165 | (((unsigned)color.blue & 0xff00) >> 8));
Bram Moolenaar36edf062016-07-21 22:10:12 +02007166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167}
7168
7169/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007170 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007172 void
7173gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174{
Bram Moolenaar98921892016-02-23 17:14:37 +01007175#if GTK_CHECK_VERSION(3,0,0)
7176 gui_gtk_get_pointer(gui.drawarea, x, y, NULL);
7177#else
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007178 gdk_window_get_pointer(gui.drawarea->window, x, y, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01007179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180}
7181
7182 void
7183gui_mch_setmouse(int x, int y)
7184{
7185 /* Sorry for the Xlib call, but we can't avoid it, since there is no
7186 * internal GDK mechanism present to accomplish this. (and for good
7187 * reason...) */
Bram Moolenaar98921892016-02-23 17:14:37 +01007188#if GTK_CHECK_VERSION(3,0,0)
7189 XWarpPointer(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.drawarea)),
7190 (Window)0, GDK_WINDOW_XID(gtk_widget_get_window(gui.drawarea)),
7191 0, 0, 0U, 0U, x, y);
7192#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 XWarpPointer(GDK_WINDOW_XDISPLAY(gui.drawarea->window),
7194 (Window)0, GDK_WINDOW_XWINDOW(gui.drawarea->window),
7195 0, 0, 0U, 0U, x, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01007196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197}
7198
7199
7200#ifdef FEAT_MOUSESHAPE
7201/* The last set mouse pointer shape is remembered, to be used when it goes
7202 * from hidden to not hidden. */
7203static int last_shape = 0;
7204#endif
7205
7206/*
7207 * Use the blank mouse pointer or not.
7208 *
7209 * hide: TRUE = use blank ptr, FALSE = use parent ptr
7210 */
7211 void
7212gui_mch_mousehide(int hide)
7213{
7214 if (gui.pointer_hidden != hide)
7215 {
7216 gui.pointer_hidden = hide;
Bram Moolenaar98921892016-02-23 17:14:37 +01007217#if GTK_CHECK_VERSION(3,0,0)
7218 if (gtk_widget_get_window(gui.drawarea) && gui.blank_pointer != NULL)
7219#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 if (gui.drawarea->window && gui.blank_pointer != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01007221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 {
7223 if (hide)
Bram Moolenaar98921892016-02-23 17:14:37 +01007224#if GTK_CHECK_VERSION(3,0,0)
7225 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
7226 gui.blank_pointer);
7227#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01007229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 else
7231#ifdef FEAT_MOUSESHAPE
7232 mch_set_mouse_shape(last_shape);
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01007233#elif GTK_CHECK_VERSION(3,0,0)
7234 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235#else
7236 gdk_window_set_cursor(gui.drawarea->window, NULL);
7237#endif
7238 }
7239 }
7240}
7241
7242#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
7243
7244/* Table for shape IDs. Keep in sync with the mshape_names[] table in
7245 * misc2.c! */
7246static const int mshape_ids[] =
7247{
7248 GDK_LEFT_PTR, /* arrow */
7249 GDK_CURSOR_IS_PIXMAP, /* blank */
7250 GDK_XTERM, /* beam */
7251 GDK_SB_V_DOUBLE_ARROW, /* updown */
7252 GDK_SIZING, /* udsizing */
7253 GDK_SB_H_DOUBLE_ARROW, /* leftright */
7254 GDK_SIZING, /* lrsizing */
7255 GDK_WATCH, /* busy */
7256 GDK_X_CURSOR, /* no */
7257 GDK_CROSSHAIR, /* crosshair */
7258 GDK_HAND1, /* hand1 */
7259 GDK_HAND2, /* hand2 */
7260 GDK_PENCIL, /* pencil */
7261 GDK_QUESTION_ARROW, /* question */
7262 GDK_RIGHT_PTR, /* right-arrow */
7263 GDK_CENTER_PTR, /* up-arrow */
7264 GDK_LEFT_PTR /* last one */
7265};
7266
7267 void
7268mch_set_mouse_shape(int shape)
7269{
7270 int id;
7271 GdkCursor *c;
7272
Bram Moolenaar98921892016-02-23 17:14:37 +01007273# if GTK_CHECK_VERSION(3,0,0)
7274 if (gtk_widget_get_window(gui.drawarea) == NULL)
7275# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 if (gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01007277# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 return;
7279
7280 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
Bram Moolenaar98921892016-02-23 17:14:37 +01007281# if GTK_CHECK_VERSION(3,0,0)
7282 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
7283 gui.blank_pointer);
7284# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01007286# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 else
7288 {
7289 if (shape >= MSHAPE_NUMBERED)
7290 {
7291 id = shape - MSHAPE_NUMBERED;
7292 if (id >= GDK_LAST_CURSOR)
7293 id = GDK_LEFT_PTR;
7294 else
7295 id &= ~1; /* they are always even (why?) */
7296 }
Bram Moolenaarb85cb212009-05-17 14:24:23 +00007297 else if (shape < (int)(sizeof(mshape_ids) / sizeof(int)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 id = mshape_ids[shape];
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007299 else
7300 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301# ifdef HAVE_GTK_MULTIHEAD
7302 c = gdk_cursor_new_for_display(
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00007303 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304# else
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00007305 c = gdk_cursor_new((GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01007307# if GTK_CHECK_VERSION(3,0,0)
7308 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), c);
7309# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 gdk_window_set_cursor(gui.drawarea->window, c);
Bram Moolenaar98921892016-02-23 17:14:37 +01007311# endif
7312# if GTK_CHECK_VERSION(3,0,0)
7313 g_object_unref(G_OBJECT(c));
7314# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 gdk_cursor_destroy(c); /* Unref, actually. Bloody GTK+ 1. */
Bram Moolenaar98921892016-02-23 17:14:37 +01007316# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 }
7318 if (shape != MSHAPE_HIDE)
7319 last_shape = shape;
7320}
7321#endif /* FEAT_MOUSESHAPE */
7322
7323
7324#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
7325/*
7326 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
7327 * scaled down if the current font is not big enough, or scaled up if the image
7328 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
7329 * will be cut off if the current font is not big enough, or centered if it's
7330 * too small.
7331 */
7332# define SIGN_WIDTH (2 * gui.char_width)
7333# define SIGN_HEIGHT (gui.char_height)
7334# define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
7335
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 void
7337gui_mch_drawsign(int row, int col, int typenr)
7338{
7339 GdkPixbuf *sign;
7340
7341 sign = (GdkPixbuf *)sign_get_image(typenr);
7342
Bram Moolenaar98921892016-02-23 17:14:37 +01007343# if GTK_CHECK_VERSION(3,0,0)
7344 if (sign != NULL && gui.drawarea != NULL
7345 && gtk_widget_get_window(gui.drawarea) != NULL)
7346# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 if (sign != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01007348# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 {
7350 int width;
7351 int height;
7352 int xoffset;
7353 int yoffset;
7354 int need_scale;
7355
7356 width = gdk_pixbuf_get_width(sign);
7357 height = gdk_pixbuf_get_height(sign);
7358 /*
7359 * Decide whether we need to scale. Allow one pixel of border
7360 * width to be cut off, in order to avoid excessive scaling for
7361 * tiny differences in font size.
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007362 * Do scale to fit the height to avoid gaps because of linespacing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 */
7364 need_scale = (width > SIGN_WIDTH + 2
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007365 || height != SIGN_HEIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 || (width < 3 * SIGN_WIDTH / 4
7367 && height < 3 * SIGN_HEIGHT / 4));
7368 if (need_scale)
7369 {
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007370 double aspect;
7371 int w = width;
7372 int h = height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373
7374 /* Keep the original aspect ratio */
7375 aspect = (double)height / (double)width;
7376 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect;
7377 width = MIN(width, SIGN_WIDTH);
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007378 if (((double)(MAX(height, SIGN_HEIGHT)) /
7379 (double)(MIN(height, SIGN_HEIGHT))) < 1.15)
7380 {
7381 /* Change the aspect ratio by at most 15% to fill the
7382 * available space completly. */
7383 height = (double)SIGN_HEIGHT * SIGN_ASPECT / aspect;
7384 height = MIN(height, SIGN_HEIGHT);
7385 }
7386 else
7387 height = (double)width * aspect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007389 if (w == width && h == height)
7390 {
7391 /* no change in dimensions; don't decrease reference counter
7392 * (below) */
7393 need_scale = FALSE;
7394 }
7395 else
7396 {
7397 /* This doesn't seem to be worth caching, and doing so would
7398 * complicate the code quite a bit. */
7399 sign = gdk_pixbuf_scale_simple(sign, width, height,
7400 GDK_INTERP_BILINEAR);
7401 if (sign == NULL)
7402 return; /* out of memory */
7403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 }
7405
7406 /* The origin is the upper-left corner of the pixmap. Therefore
7407 * these offset may become negative if the pixmap is smaller than
7408 * the 2x1 cells reserved for the sign icon. */
7409 xoffset = (width - SIGN_WIDTH) / 2;
7410 yoffset = (height - SIGN_HEIGHT) / 2;
7411
Bram Moolenaar98921892016-02-23 17:14:37 +01007412# if GTK_CHECK_VERSION(3,0,0)
7413 {
7414 cairo_t *cr;
7415 cairo_surface_t *bg_surf;
7416 cairo_t *bg_cr;
7417 cairo_surface_t *sign_surf;
7418 cairo_t *sign_cr;
7419
7420 cr = cairo_create(gui.surface);
7421
7422 bg_surf = cairo_surface_create_similar(gui.surface,
7423 cairo_surface_get_content(gui.surface),
7424 SIGN_WIDTH, SIGN_HEIGHT);
7425 bg_cr = cairo_create(bg_surf);
Bram Moolenaar36edf062016-07-21 22:10:12 +02007426 cairo_set_source_rgba(bg_cr,
7427 gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue,
7428 gui.bgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01007429 cairo_paint(bg_cr);
7430
7431 sign_surf = cairo_surface_create_similar(gui.surface,
7432 cairo_surface_get_content(gui.surface),
7433 SIGN_WIDTH, SIGN_HEIGHT);
7434 sign_cr = cairo_create(sign_surf);
7435 gdk_cairo_set_source_pixbuf(sign_cr, sign, -xoffset, -yoffset);
7436 cairo_paint(sign_cr);
7437
7438 cairo_set_operator(sign_cr, CAIRO_OPERATOR_DEST_OVER);
7439 cairo_set_source_surface(sign_cr, bg_surf, 0, 0);
7440 cairo_paint(sign_cr);
7441
7442 cairo_set_source_surface(cr, sign_surf, FILL_X(col), FILL_Y(row));
7443 cairo_paint(cr);
7444
7445 cairo_destroy(sign_cr);
7446 cairo_surface_destroy(sign_surf);
7447 cairo_destroy(bg_cr);
7448 cairo_surface_destroy(bg_surf);
7449 cairo_destroy(cr);
7450
7451 if (!gui.by_signal)
7452 gtk_widget_queue_draw_area(gui.drawarea,
7453 FILL_X(col), FILL_Y(col), width, height);
7454
7455 }
7456# else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
7458
7459 gdk_draw_rectangle(gui.drawarea->window,
7460 gui.text_gc,
7461 TRUE,
7462 FILL_X(col),
7463 FILL_Y(row),
7464 SIGN_WIDTH,
7465 SIGN_HEIGHT);
7466
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 gdk_pixbuf_render_to_drawable_alpha(sign,
7468 gui.drawarea->window,
7469 MAX(0, xoffset),
7470 MAX(0, yoffset),
7471 FILL_X(col) - MIN(0, xoffset),
7472 FILL_Y(row) - MIN(0, yoffset),
7473 MIN(width, SIGN_WIDTH),
7474 MIN(height, SIGN_HEIGHT),
7475 GDK_PIXBUF_ALPHA_BILEVEL,
7476 127,
7477 GDK_RGB_DITHER_NORMAL,
7478 0, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01007479# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 if (need_scale)
7481 g_object_unref(sign);
7482 }
7483}
7484
7485 void *
7486gui_mch_register_sign(char_u *signfile)
7487{
7488 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use)
7489 {
7490 GdkPixbuf *sign;
7491 GError *error = NULL;
7492 char_u *message;
7493
7494 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error);
7495
7496 if (error == NULL)
7497 return sign;
7498
7499 message = (char_u *)error->message;
7500
7501 if (message != NULL && input_conv.vc_type != CONV_NONE)
7502 message = string_convert(&input_conv, message, NULL);
7503
7504 if (message != NULL)
7505 {
7506 /* The error message is already translated and will be more
7507 * descriptive than anything we could possibly do ourselves. */
7508 EMSG2("E255: %s", message);
7509
7510 if (input_conv.vc_type != CONV_NONE)
7511 vim_free(message);
7512 }
7513 g_error_free(error);
7514 }
7515
7516 return NULL;
7517}
7518
7519 void
7520gui_mch_destroy_sign(void *sign)
7521{
7522 if (sign != NULL)
7523 g_object_unref(sign);
7524}
7525
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526#endif /* FEAT_SIGN_ICONS */