blob: cfbb36ce933a80938bc73eb6dd112a85fbe71553 [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 */
Bram Moolenaarb463e8d2017-06-05 15:07:09 +0200105#define GET_X_ATOM(atom) gdk_x11_atom_to_xatom_for_display( \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 gtk_widget_get_display(gui.mainwin), atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
108/* Selection type distinguishers */
109enum
110{
111 TARGET_TYPE_NONE,
112 TARGET_UTF8_STRING,
113 TARGET_STRING,
114 TARGET_COMPOUND_TEXT,
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000115 TARGET_HTML,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116 TARGET_TEXT,
117 TARGET_TEXT_URI_LIST,
118 TARGET_TEXT_PLAIN,
119 TARGET_VIM,
120 TARGET_VIMENC
121};
122
123/*
124 * Table of selection targets supported by Vim.
125 * Note: Order matters, preferred types should come first.
126 */
127static const GtkTargetEntry selection_targets[] =
128{
129 {VIMENC_ATOM_NAME, 0, TARGET_VIMENC},
130 {VIM_ATOM_NAME, 0, TARGET_VIM},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000131 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 {"COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT},
134 {"TEXT", 0, TARGET_TEXT},
135 {"STRING", 0, TARGET_STRING}
136};
137#define N_SELECTION_TARGETS (sizeof(selection_targets) / sizeof(selection_targets[0]))
138
139#ifdef FEAT_DND
140/*
141 * Table of DnD targets supported by Vim.
142 * Note: Order matters, preferred types should come first.
143 */
144static const GtkTargetEntry dnd_targets[] =
145{
146 {"text/uri-list", 0, TARGET_TEXT_URI_LIST},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000147 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 {"STRING", 0, TARGET_STRING},
150 {"text/plain", 0, TARGET_TEXT_PLAIN}
151};
152# define N_DND_TARGETS (sizeof(dnd_targets) / sizeof(dnd_targets[0]))
153#endif
154
155
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156/*
157 * "Monospace" is a standard font alias that should be present
158 * on all proper Pango/fontconfig installations.
159 */
160# define DEFAULT_FONT "Monospace 10"
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
163/*
164 * Atoms used to communicate save-yourself from the X11 session manager. There
165 * is no need to move them into the GUI struct, since they should be constant.
166 */
167static GdkAtom wm_protocols_atom = GDK_NONE;
168static GdkAtom save_yourself_atom = GDK_NONE;
169#endif
170
171/*
172 * Atoms used to control/reference X11 selections.
173 */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000174static GdkAtom html_atom = GDK_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175static GdkAtom utf8_string_atom = GDK_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176static GdkAtom vim_atom = GDK_NONE; /* Vim's own special selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177static GdkAtom vimenc_atom = GDK_NONE; /* Vim's extended selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
179/*
180 * Keycodes recognized by vim.
181 * NOTE: when changing this, the table in gui_x11.c probably needs the same
182 * change!
183 */
184static struct special_key
185{
186 guint key_sym;
187 char_u code0;
188 char_u code1;
189}
190const special_keys[] =
191{
192 {GDK_Up, 'k', 'u'},
193 {GDK_Down, 'k', 'd'},
194 {GDK_Left, 'k', 'l'},
195 {GDK_Right, 'k', 'r'},
196 {GDK_F1, 'k', '1'},
197 {GDK_F2, 'k', '2'},
198 {GDK_F3, 'k', '3'},
199 {GDK_F4, 'k', '4'},
200 {GDK_F5, 'k', '5'},
201 {GDK_F6, 'k', '6'},
202 {GDK_F7, 'k', '7'},
203 {GDK_F8, 'k', '8'},
204 {GDK_F9, 'k', '9'},
205 {GDK_F10, 'k', ';'},
206 {GDK_F11, 'F', '1'},
207 {GDK_F12, 'F', '2'},
208 {GDK_F13, 'F', '3'},
209 {GDK_F14, 'F', '4'},
210 {GDK_F15, 'F', '5'},
211 {GDK_F16, 'F', '6'},
212 {GDK_F17, 'F', '7'},
213 {GDK_F18, 'F', '8'},
214 {GDK_F19, 'F', '9'},
215 {GDK_F20, 'F', 'A'},
216 {GDK_F21, 'F', 'B'},
217 {GDK_Pause, 'F', 'B'}, /* Pause == F21 according to netbeans.txt */
218 {GDK_F22, 'F', 'C'},
219 {GDK_F23, 'F', 'D'},
220 {GDK_F24, 'F', 'E'},
221 {GDK_F25, 'F', 'F'},
222 {GDK_F26, 'F', 'G'},
223 {GDK_F27, 'F', 'H'},
224 {GDK_F28, 'F', 'I'},
225 {GDK_F29, 'F', 'J'},
226 {GDK_F30, 'F', 'K'},
227 {GDK_F31, 'F', 'L'},
228 {GDK_F32, 'F', 'M'},
229 {GDK_F33, 'F', 'N'},
230 {GDK_F34, 'F', 'O'},
231 {GDK_F35, 'F', 'P'},
232#ifdef SunXK_F36
233 {SunXK_F36, 'F', 'Q'},
234 {SunXK_F37, 'F', 'R'},
235#endif
236 {GDK_Help, '%', '1'},
237 {GDK_Undo, '&', '8'},
238 {GDK_BackSpace, 'k', 'b'},
239 {GDK_Insert, 'k', 'I'},
240 {GDK_Delete, 'k', 'D'},
241 {GDK_3270_BackTab, 'k', 'B'},
242 {GDK_Clear, 'k', 'C'},
243 {GDK_Home, 'k', 'h'},
244 {GDK_End, '@', '7'},
245 {GDK_Prior, 'k', 'P'},
246 {GDK_Next, 'k', 'N'},
247 {GDK_Print, '%', '9'},
248 /* Keypad keys: */
249 {GDK_KP_Left, 'k', 'l'},
250 {GDK_KP_Right, 'k', 'r'},
251 {GDK_KP_Up, 'k', 'u'},
252 {GDK_KP_Down, 'k', 'd'},
253 {GDK_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
254 {GDK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
255 {GDK_KP_Home, 'K', '1'},
256 {GDK_KP_End, 'K', '4'},
257 {GDK_KP_Prior, 'K', '3'}, /* page up */
258 {GDK_KP_Next, 'K', '5'}, /* page down */
259
260 {GDK_KP_Add, 'K', '6'},
261 {GDK_KP_Subtract, 'K', '7'},
262 {GDK_KP_Divide, 'K', '8'},
263 {GDK_KP_Multiply, 'K', '9'},
264 {GDK_KP_Enter, 'K', 'A'},
265 {GDK_KP_Decimal, 'K', 'B'},
266
267 {GDK_KP_0, 'K', 'C'},
268 {GDK_KP_1, 'K', 'D'},
269 {GDK_KP_2, 'K', 'E'},
270 {GDK_KP_3, 'K', 'F'},
271 {GDK_KP_4, 'K', 'G'},
272 {GDK_KP_5, 'K', 'H'},
273 {GDK_KP_6, 'K', 'I'},
274 {GDK_KP_7, 'K', 'J'},
275 {GDK_KP_8, 'K', 'K'},
276 {GDK_KP_9, 'K', 'L'},
277
278 /* End of list marker: */
279 {0, 0, 0}
280};
281
282/*
283 * Flags for command line options table below.
284 */
285#define ARG_FONT 1
286#define ARG_GEOMETRY 2
287#define ARG_REVERSE 3
288#define ARG_NOREVERSE 4
289#define ARG_BACKGROUND 5
290#define ARG_FOREGROUND 6
291#define ARG_ICONIC 7
292#define ARG_ROLE 8
293#define ARG_NETBEANS 9
294#define ARG_XRM 10 /* ignored */
295#define ARG_MENUFONT 11 /* ignored */
296#define ARG_INDEX_MASK 0x00ff
297#define ARG_HAS_VALUE 0x0100 /* a value is expected after the argument */
298#define ARG_NEEDS_GUI 0x0200 /* need to initialize the GUI for this */
299#define ARG_FOR_GTK 0x0400 /* argument is handled by GTK+ or GNOME */
300#define ARG_COMPAT_LONG 0x0800 /* accept -foo but substitute with --foo */
301#define ARG_KEEP 0x1000 /* don't remove argument from argv[] */
302
303/*
304 * This table holds all the X GUI command line options allowed. This includes
305 * the standard ones so that we can skip them when Vim is started without the
306 * GUI (but the GUI might start up later).
307 *
308 * When changing this, also update doc/gui_x11.txt and the usage message!!!
309 */
310typedef struct
311{
312 const char *name;
313 unsigned int flags;
314}
315cmdline_option_T;
316
317static const cmdline_option_T cmdline_options[] =
318{
319 /* We handle these options ourselves */
320 {"-fn", ARG_FONT|ARG_HAS_VALUE},
321 {"-font", ARG_FONT|ARG_HAS_VALUE},
322 {"-geom", ARG_GEOMETRY|ARG_HAS_VALUE},
323 {"-geometry", ARG_GEOMETRY|ARG_HAS_VALUE},
324 {"-rv", ARG_REVERSE},
325 {"-reverse", ARG_REVERSE},
326 {"+rv", ARG_NOREVERSE},
327 {"+reverse", ARG_NOREVERSE},
328 {"-bg", ARG_BACKGROUND|ARG_HAS_VALUE},
329 {"-background", ARG_BACKGROUND|ARG_HAS_VALUE},
330 {"-fg", ARG_FOREGROUND|ARG_HAS_VALUE},
331 {"-foreground", ARG_FOREGROUND|ARG_HAS_VALUE},
332 {"-iconic", ARG_ICONIC},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 {"--role", ARG_ROLE|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#ifdef FEAT_NETBEANS_INTG
335 {"-nb", ARG_NETBEANS}, /* non-standard value format */
336 {"-xrm", ARG_XRM|ARG_HAS_VALUE}, /* not implemented */
337 {"-mf", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
338 {"-menufont", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 /* Arguments handled by GTK (and GNOME) internally. */
341 {"--g-fatal-warnings", ARG_FOR_GTK},
342 {"--gdk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
343 {"--gdk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
344 {"--gtk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
345 {"--gtk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
346 {"--gtk-module", ARG_FOR_GTK|ARG_HAS_VALUE},
347 {"--sync", ARG_FOR_GTK},
348 {"--display", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
349 {"--name", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
350 {"--class", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 {"--screen", ARG_FOR_GTK|ARG_HAS_VALUE},
352 {"--gxid-host", ARG_FOR_GTK|ARG_HAS_VALUE},
353 {"--gxid-port", ARG_FOR_GTK|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354#ifdef FEAT_GUI_GNOME
355 {"--load-modules", ARG_FOR_GTK|ARG_HAS_VALUE},
356 {"--sm-client-id", ARG_FOR_GTK|ARG_HAS_VALUE},
357 {"--sm-config-prefix", ARG_FOR_GTK|ARG_HAS_VALUE},
358 {"--sm-disable", ARG_FOR_GTK},
359 {"--oaf-ior-fd", ARG_FOR_GTK|ARG_HAS_VALUE},
360 {"--oaf-activate-iid", ARG_FOR_GTK|ARG_HAS_VALUE},
361 {"--oaf-private", ARG_FOR_GTK},
362 {"--enable-sound", ARG_FOR_GTK},
363 {"--disable-sound", ARG_FOR_GTK},
364 {"--espeaker", ARG_FOR_GTK|ARG_HAS_VALUE},
365 {"-?", ARG_FOR_GTK|ARG_NEEDS_GUI},
366 {"--help", ARG_FOR_GTK|ARG_NEEDS_GUI|ARG_KEEP},
367 {"--usage", ARG_FOR_GTK|ARG_NEEDS_GUI},
368# if 0 /* conflicts with Vim's own --version argument */
369 {"--version", ARG_FOR_GTK|ARG_NEEDS_GUI},
370# endif
371 {"--disable-crash-dialog", ARG_FOR_GTK},
372#endif
373 {NULL, 0}
374};
375
376static int gui_argc = 0;
377static char **gui_argv = NULL;
378
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379static const char *role_argument = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
381static const char *restart_command = NULL;
Bram Moolenaar9085f802009-06-03 14:20:21 +0000382static char *abs_restart_command = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383#endif
384static int found_iconic_arg = FALSE;
385
386#ifdef FEAT_GUI_GNOME
387/*
388 * Can't use Gnome if --socketid given
389 */
390static int using_gnome = 0;
391#else
392# define using_gnome 0
393#endif
394
395/*
396 * Parse the GUI related command-line arguments. Any arguments used are
397 * deleted from argv, and *argc is decremented accordingly. This is called
398 * when vim is started, whether or not the GUI has been started.
399 */
400 void
401gui_mch_prepare(int *argc, char **argv)
402{
403 const cmdline_option_T *option;
404 int i = 0;
405 int len = 0;
406
407#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
408 /*
409 * Determine the command used to invoke Vim, to be passed as restart
410 * command to the session manager. If argv[0] contains any directory
411 * components try building an absolute path, otherwise leave it as is.
412 */
413 restart_command = argv[0];
414
415 if (strchr(argv[0], G_DIR_SEPARATOR) != NULL)
416 {
417 char_u buf[MAXPATHL];
418
419 if (mch_FullName((char_u *)argv[0], buf, (int)sizeof(buf), TRUE) == OK)
Bram Moolenaar9085f802009-06-03 14:20:21 +0000420 {
421 abs_restart_command = (char *)vim_strsave(buf);
422 restart_command = abs_restart_command;
423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 }
425#endif
426
427 /*
428 * Move all the entries in argv which are relevant to GTK+ and GNOME
429 * into gui_argv. Freed later in gui_mch_init().
430 */
431 gui_argc = 0;
432 gui_argv = (char **)alloc((unsigned)((*argc + 1) * sizeof(char *)));
433
434 g_return_if_fail(gui_argv != NULL);
435
436 gui_argv[gui_argc++] = argv[i++];
437
438 while (i < *argc)
439 {
440 /* Don't waste CPU cycles on non-option arguments. */
441 if (argv[i][0] != '-' && argv[i][0] != '+')
442 {
443 ++i;
444 continue;
445 }
446
447 /* Look for argv[i] in cmdline_options[] table. */
448 for (option = &cmdline_options[0]; option->name != NULL; ++option)
449 {
450 len = strlen(option->name);
451
452 if (strncmp(argv[i], option->name, len) == 0)
453 {
454 if (argv[i][len] == '\0')
455 break;
456 /* allow --foo=bar style */
457 if (argv[i][len] == '=' && (option->flags & ARG_HAS_VALUE))
458 break;
459#ifdef FEAT_NETBEANS_INTG
460 /* darn, -nb has non-standard syntax */
461 if (vim_strchr((char_u *)":=", argv[i][len]) != NULL
462 && (option->flags & ARG_INDEX_MASK) == ARG_NETBEANS)
463 break;
464#endif
465 }
466 else if ((option->flags & ARG_COMPAT_LONG)
467 && strcmp(argv[i], option->name + 1) == 0)
468 {
469 /* Replace the standard X arguments "-name" and "-display"
470 * with their GNU-style long option counterparts. */
471 argv[i] = (char *)option->name;
472 break;
473 }
474 }
475 if (option->name == NULL) /* no match */
476 {
477 ++i;
478 continue;
479 }
480
481 if (option->flags & ARG_FOR_GTK)
482 {
483 /* Move the argument into gui_argv, which
484 * will later be passed to gtk_init_check() */
485 gui_argv[gui_argc++] = argv[i];
486 }
487 else
488 {
489 char *value = NULL;
490
491 /* Extract the option's value if there is one.
492 * Accept both "--foo bar" and "--foo=bar" style. */
493 if (option->flags & ARG_HAS_VALUE)
494 {
495 if (argv[i][len] == '=')
496 value = &argv[i][len + 1];
497 else if (i + 1 < *argc && strcmp(argv[i + 1], "--") != 0)
498 value = argv[i + 1];
499 }
500
501 /* Check for options handled by Vim itself */
502 switch (option->flags & ARG_INDEX_MASK)
503 {
504 case ARG_REVERSE:
505 found_reverse_arg = TRUE;
506 break;
507 case ARG_NOREVERSE:
508 found_reverse_arg = FALSE;
509 break;
510 case ARG_FONT:
511 font_argument = value;
512 break;
513 case ARG_GEOMETRY:
514 if (value != NULL)
515 gui.geom = vim_strsave((char_u *)value);
516 break;
517 case ARG_BACKGROUND:
518 background_argument = value;
519 break;
520 case ARG_FOREGROUND:
521 foreground_argument = value;
522 break;
523 case ARG_ICONIC:
524 found_iconic_arg = TRUE;
525 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 case ARG_ROLE:
527 role_argument = value; /* used later in gui_mch_open() */
528 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529#ifdef FEAT_NETBEANS_INTG
530 case ARG_NETBEANS:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 gui.dofork = FALSE; /* don't fork() when starting GUI */
532 netbeansArg = argv[i];
533 break;
534#endif
535 default:
536 break;
537 }
538 }
539
540 /* These arguments make gnome_program_init() print a message and exit.
Bram Moolenaar717e1962016-08-10 21:28:44 +0200541 * Must start the GUI for this, otherwise ":gui" will exit later!
542 * Only when the GUI can start. */
543 if ((option->flags & ARG_NEEDS_GUI)
544 && gui_mch_early_init_check(FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 gui.starting = TRUE;
546
547 if (option->flags & ARG_KEEP)
548 ++i;
549 else
550 {
551 /* Remove the flag from the argument vector. */
552 if (--*argc > i)
553 {
554 int n_strip = 1;
555
556 /* Move the argument's value as well, if there is one. */
557 if ((option->flags & ARG_HAS_VALUE)
558 && argv[i][len] != '='
559 && strcmp(argv[i + 1], "--") != 0)
560 {
561 ++n_strip;
562 --*argc;
563 if (option->flags & ARG_FOR_GTK)
564 gui_argv[gui_argc++] = argv[i + 1];
565 }
566
567 if (*argc > i)
568 mch_memmove(&argv[i], &argv[i + n_strip],
569 (*argc - i) * sizeof(char *));
570 }
571 argv[*argc] = NULL;
572 }
573 }
574
575 gui_argv[gui_argc] = NULL;
576}
577
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000578#if defined(EXITFREE) || defined(PROTO)
579 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100580gui_mch_free_all(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000581{
582 vim_free(gui_argv);
Bram Moolenaar9085f802009-06-03 14:20:21 +0000583#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
584 vim_free(abs_restart_command);
585#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000586}
587#endif
588
Bram Moolenaar98921892016-02-23 17:14:37 +0100589#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590/*
591 * This should be maybe completely removed.
592 * Doesn't seem possible, since check_copy_area() relies on
593 * this information. --danielk
594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000596visibility_event(GtkWidget *widget UNUSED,
597 GdkEventVisibility *event,
598 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599{
600 gui.visibility = event->state;
601 /*
602 * When we do an gdk_window_copy_area(), and the window is partially
603 * obscured, we want to receive an event to tell us whether it worked
604 * or not.
605 */
606 if (gui.text_gc != NULL)
607 gdk_gc_set_exposures(gui.text_gc,
608 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
609 return FALSE;
610}
Bram Moolenaar98921892016-02-23 17:14:37 +0100611#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
613/*
614 * Redraw the corresponding portions of the screen.
615 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100616#if GTK_CHECK_VERSION(3,0,0)
617static gboolean is_key_pressed = FALSE;
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100618static gboolean blink_mode = TRUE;
Bram Moolenaar98921892016-02-23 17:14:37 +0100619
620static gboolean gui_gtk_is_blink_on(void);
Bram Moolenaar98921892016-02-23 17:14:37 +0100621static void gui_gtk_window_clear(GdkWindow *win);
622
623 static void
624gui_gtk3_redraw(int x, int y, int width, int height)
625{
Bram Moolenaarfe344a92017-02-23 12:20:35 +0100626 /* Range checks are left to gui_redraw_block() */
Bram Moolenaar98921892016-02-23 17:14:37 +0100627 gui_redraw_block(Y_2_ROW(y), X_2_COL(x),
628 Y_2_ROW(y + height - 1), X_2_COL(x + width - 1),
629 GUI_MON_NOCLEAR);
630}
631
632 static void
633gui_gtk3_update_cursor(cairo_t *cr)
634{
635 if (gui.row == gui.cursor_row)
636 {
637 gui.by_signal = TRUE;
Bram Moolenaar4fc563b2016-03-12 12:40:58 +0100638 if (State & CMDLINE)
639 gui_update_cursor(TRUE, FALSE);
640 else
641 gui_update_cursor(TRUE, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +0100642 gui.by_signal = FALSE;
643 cairo_paint(cr);
644 }
645}
646
647 static gboolean
648gui_gtk3_should_draw_cursor(void)
649{
650 unsigned int cond = 0;
651 cond |= gui_gtk_is_blink_on();
Bram Moolenaarb4ebf9a2016-03-12 16:28:18 +0100652 if (gui.cursor_col >= gui.col)
653 cond |= is_key_pressed;
Bram Moolenaar98921892016-02-23 17:14:37 +0100654 cond |= gui.in_focus == FALSE;
Bram Moolenaar98921892016-02-23 17:14:37 +0100655 return cond;
656}
657
658 static gboolean
Bram Moolenaar8e31fd52016-06-04 22:18:13 +0200659draw_event(GtkWidget *widget UNUSED,
Bram Moolenaar98921892016-02-23 17:14:37 +0100660 cairo_t *cr,
661 gpointer user_data UNUSED)
662{
663 /* Skip this when the GUI isn't set up yet, will redraw later. */
664 if (gui.starting)
665 return FALSE;
666
667 out_flush(); /* make sure all output has been processed */
668 /* for GTK+ 3, may induce other draw events. */
669
670 cairo_set_source_surface(cr, gui.surface, 0, 0);
671
672 /* Draw the window without the cursor. */
673 gui.by_signal = TRUE;
674 {
675 cairo_rectangle_list_t *list = NULL;
676
Bram Moolenaar98921892016-02-23 17:14:37 +0100677 list = cairo_copy_clip_rectangle_list(cr);
678 if (list->status != CAIRO_STATUS_CLIP_NOT_REPRESENTABLE)
679 {
680 int i;
Bram Moolenaarfe344a92017-02-23 12:20:35 +0100681
682 /* First clear all the blocks and then redraw them. Just in case
683 * some blocks overlap. */
Bram Moolenaar98921892016-02-23 17:14:37 +0100684 for (i = 0; i < list->num_rectangles; i++)
685 {
686 const cairo_rectangle_t rect = list->rectangles[i];
Bram Moolenaar8e31fd52016-06-04 22:18:13 +0200687
Bram Moolenaarfe344a92017-02-23 12:20:35 +0100688 gui_mch_clear_block(Y_2_ROW((int)rect.y), 0,
689 Y_2_ROW((int)(rect.y + rect.height)) - 1, Columns - 1);
690 }
691
692 for (i = 0; i < list->num_rectangles; i++)
693 {
694 const cairo_rectangle_t rect = list->rectangles[i];
Bram Moolenaar8e31fd52016-06-04 22:18:13 +0200695
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100696 if (blink_mode)
697 gui_gtk3_redraw(rect.x, rect.y, rect.width, rect.height);
698 else
Bram Moolenaarb4ebf9a2016-03-12 16:28:18 +0100699 {
700 if (get_real_state() & VISUAL)
701 gui_gtk3_redraw(rect.x, rect.y,
702 rect.width, rect.height);
703 else
704 gui_redraw(rect.x, rect.y, rect.width, rect.height);
705 }
Bram Moolenaar98921892016-02-23 17:14:37 +0100706 }
707 }
708 cairo_rectangle_list_destroy(list);
709
Bram Moolenaarb4ebf9a2016-03-12 16:28:18 +0100710 if (get_real_state() & VISUAL)
711 {
712 if (gui.cursor_row == gui.row && gui.cursor_col >= gui.col)
713 gui_update_cursor(TRUE, TRUE);
714 }
715
Bram Moolenaar98921892016-02-23 17:14:37 +0100716 cairo_paint(cr);
717 }
718 gui.by_signal = FALSE;
719
720 /* Add the cursor to the window if necessary.*/
Bram Moolenaarb4ebf9a2016-03-12 16:28:18 +0100721 if (gui_gtk3_should_draw_cursor() && blink_mode)
Bram Moolenaar98921892016-02-23 17:14:37 +0100722 gui_gtk3_update_cursor(cr);
723
724 return FALSE;
725}
726#else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000728expose_event(GtkWidget *widget UNUSED,
729 GdkEventExpose *event,
730 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731{
732 /* Skip this when the GUI isn't set up yet, will redraw later. */
733 if (gui.starting)
734 return FALSE;
735
736 out_flush(); /* make sure all output has been processed */
737 gui_redraw(event->area.x, event->area.y,
738 event->area.width, event->area.height);
739
740 /* Clear the border areas if needed */
741 if (event->area.x < FILL_X(0))
742 gdk_window_clear_area(gui.drawarea->window, 0, 0, FILL_X(0), 0);
743 if (event->area.y < FILL_Y(0))
744 gdk_window_clear_area(gui.drawarea->window, 0, 0, 0, FILL_Y(0));
745 if (event->area.x > FILL_X(Columns))
746 gdk_window_clear_area(gui.drawarea->window,
747 FILL_X((int)Columns), 0, 0, 0);
748 if (event->area.y > FILL_Y(Rows))
749 gdk_window_clear_area(gui.drawarea->window, 0, FILL_Y((int)Rows), 0, 0);
750
751 return FALSE;
752}
Bram Moolenaar98921892016-02-23 17:14:37 +0100753#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
755#ifdef FEAT_CLIENTSERVER
756/*
757 * Handle changes to the "Comm" property
758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000760property_event(GtkWidget *widget,
761 GdkEventProperty *event,
762 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763{
764 if (event->type == GDK_PROPERTY_NOTIFY
765 && event->state == (int)GDK_PROPERTY_NEW_VALUE
Bram Moolenaar98921892016-02-23 17:14:37 +0100766# if GTK_CHECK_VERSION(3,0,0)
767 && GDK_WINDOW_XID(event->window) == commWindow
768# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 && GDK_WINDOW_XWINDOW(event->window) == commWindow
Bram Moolenaar98921892016-02-23 17:14:37 +0100770# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 && GET_X_ATOM(event->atom) == commProperty)
772 {
773 XEvent xev;
774
775 /* Translate to XLib */
776 xev.xproperty.type = PropertyNotify;
777 xev.xproperty.atom = commProperty;
778 xev.xproperty.window = commWindow;
779 xev.xproperty.state = PropertyNewValue;
Bram Moolenaar98921892016-02-23 17:14:37 +0100780# if GTK_CHECK_VERSION(3,0,0)
781 serverEventProc(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(widget)),
782 &xev, 0);
783# else
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200784 serverEventProc(GDK_WINDOW_XDISPLAY(widget->window), &xev, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +0100785# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 }
787 return FALSE;
788}
Bram Moolenaar98921892016-02-23 17:14:37 +0100789#endif /* defined(FEAT_CLIENTSERVER) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
791
792/****************************************************************************
793 * Focus handlers:
794 */
795
796
797/*
798 * This is a simple state machine:
799 * BLINK_NONE not blinking at all
800 * BLINK_OFF blinking, cursor is not shown
801 * BLINK_ON blinking, cursor is shown
802 */
803
804#define BLINK_NONE 0
805#define BLINK_OFF 1
806#define BLINK_ON 2
807
808static int blink_state = BLINK_NONE;
809static long_u blink_waittime = 700;
810static long_u blink_ontime = 400;
811static long_u blink_offtime = 250;
812static guint blink_timer = 0;
813
Bram Moolenaar98921892016-02-23 17:14:37 +0100814#if GTK_CHECK_VERSION(3,0,0)
815 static gboolean
816gui_gtk_is_blink_on(void)
817{
818 return blink_state == BLINK_ON;
819}
Bram Moolenaar98921892016-02-23 17:14:37 +0100820#endif
821
Bram Moolenaar703a8042016-06-04 16:24:32 +0200822 int
823gui_mch_is_blinking(void)
824{
825 return blink_state != BLINK_NONE;
826}
827
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200828 int
829gui_mch_is_blink_off(void)
830{
831 return blink_state == BLINK_OFF;
832}
833
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 void
835gui_mch_set_blinking(long waittime, long on, long off)
836{
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100837#if GTK_CHECK_VERSION(3,0,0)
838 if (waittime == 0 || on == 0 || off == 0)
839 {
840 blink_mode = FALSE;
841
842 blink_waittime = 700;
843 blink_ontime = 400;
844 blink_offtime = 250;
845 }
846 else
847 {
848 blink_mode = TRUE;
849
850 blink_waittime = waittime;
851 blink_ontime = on;
852 blink_offtime = off;
853 }
854#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 blink_waittime = waittime;
856 blink_ontime = on;
857 blink_offtime = off;
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859}
860
861/*
862 * Stop the cursor blinking. Show the cursor if it wasn't shown.
863 */
864 void
865gui_mch_stop_blink(void)
866{
867 if (blink_timer)
868 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100869#if GTK_CHECK_VERSION(3,0,0)
870 g_source_remove(blink_timer);
871#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 gtk_timeout_remove(blink_timer);
Bram Moolenaar98921892016-02-23 17:14:37 +0100873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 blink_timer = 0;
875 }
876 if (blink_state == BLINK_OFF)
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200877 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200879 gui_mch_flush();
880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 blink_state = BLINK_NONE;
882}
883
Bram Moolenaar98921892016-02-23 17:14:37 +0100884#if GTK_CHECK_VERSION(3,0,0)
885 static gboolean
886#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +0100888#endif
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000889blink_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890{
891 if (blink_state == BLINK_ON)
892 {
893 gui_undraw_cursor();
894 blink_state = BLINK_OFF;
Bram Moolenaar98921892016-02-23 17:14:37 +0100895#if GTK_CHECK_VERSION(3,0,0)
896 blink_timer = g_timeout_add((guint)blink_offtime,
897 (GSourceFunc) blink_cb, NULL);
898#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 blink_timer = gtk_timeout_add((guint32)blink_offtime,
900 (GtkFunction) blink_cb, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +0100901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 }
903 else
904 {
905 gui_update_cursor(TRUE, FALSE);
906 blink_state = BLINK_ON;
Bram Moolenaar98921892016-02-23 17:14:37 +0100907#if GTK_CHECK_VERSION(3,0,0)
908 blink_timer = g_timeout_add((guint)blink_ontime,
909 (GSourceFunc) blink_cb, NULL);
910#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 blink_timer = gtk_timeout_add((guint32)blink_ontime,
912 (GtkFunction) blink_cb, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +0100913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 }
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200915 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
917 return FALSE; /* don't happen again */
918}
919
920/*
921 * Start the cursor blinking. If it was already blinking, this restarts the
922 * waiting time and shows the cursor.
923 */
924 void
925gui_mch_start_blink(void)
926{
927 if (blink_timer)
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +0200928 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100929#if GTK_CHECK_VERSION(3,0,0)
930 g_source_remove(blink_timer);
931#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 gtk_timeout_remove(blink_timer);
Bram Moolenaar98921892016-02-23 17:14:37 +0100933#endif
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +0200934 blink_timer = 0;
935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 /* Only switch blinking on if none of the times is zero */
937 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
938 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100939#if GTK_CHECK_VERSION(3,0,0)
940 blink_timer = g_timeout_add((guint)blink_waittime,
941 (GSourceFunc) blink_cb, NULL);
942#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 blink_timer = gtk_timeout_add((guint32)blink_waittime,
944 (GtkFunction) blink_cb, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +0100945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 blink_state = BLINK_ON;
947 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200948 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 }
950}
951
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000953enter_notify_event(GtkWidget *widget UNUSED,
954 GdkEventCrossing *event UNUSED,
955 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956{
957 if (blink_state == BLINK_NONE)
958 gui_mch_start_blink();
959
960 /* make sure keyboard input goes there */
Bram Moolenaar98921892016-02-23 17:14:37 +0100961#if GTK_CHECK_VERSION(3,0,0)
962 if (gtk_socket_id == 0 || !gtk_widget_has_focus(gui.drawarea))
963#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 if (gtk_socket_id == 0 || !GTK_WIDGET_HAS_FOCUS(gui.drawarea))
Bram Moolenaar98921892016-02-23 17:14:37 +0100965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 gtk_widget_grab_focus(gui.drawarea);
967
968 return FALSE;
969}
970
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000972leave_notify_event(GtkWidget *widget UNUSED,
973 GdkEventCrossing *event UNUSED,
974 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975{
976 if (blink_state != BLINK_NONE)
977 gui_mch_stop_blink();
978
979 return FALSE;
980}
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000983focus_in_event(GtkWidget *widget,
984 GdkEventFocus *event UNUSED,
985 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986{
987 gui_focus_change(TRUE);
988
989 if (blink_state == BLINK_NONE)
990 gui_mch_start_blink();
991
Bram Moolenaar9c8791f2007-09-05 19:47:23 +0000992 /* make sure keyboard input goes to the draw area (if this is focus for a
993 * window) */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000994 if (widget != gui.drawarea)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000995 gtk_widget_grab_focus(gui.drawarea);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996
997 return TRUE;
998}
999
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001001focus_out_event(GtkWidget *widget UNUSED,
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001002 GdkEventFocus *event UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001003 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004{
1005 gui_focus_change(FALSE);
1006
1007 if (blink_state != BLINK_NONE)
1008 gui_mch_stop_blink();
1009
1010 return TRUE;
1011}
1012
1013
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014/*
1015 * Translate a GDK key value to UTF-8 independently of the current locale.
1016 * The output is written to string, which must have room for at least 6 bytes
1017 * plus the NUL terminator. Returns the length in bytes.
1018 *
1019 * This function is used in the GTK+ 2 GUI only. The GTK+ 1 code makes use
1020 * of GdkEventKey::string instead. But event->string is evil; see here why:
1021 * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey
1022 */
1023 static int
1024keyval_to_string(unsigned int keyval, unsigned int state, char_u *string)
1025{
1026 int len;
1027 guint32 uc;
1028
1029 uc = gdk_keyval_to_unicode(keyval);
1030 if (uc != 0)
1031 {
1032 /* Check for CTRL-foo */
1033 if ((state & GDK_CONTROL_MASK) && uc >= 0x20 && uc < 0x80)
1034 {
1035 /* These mappings look arbitrary at the first glance, but in fact
1036 * resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1037 * machine. The only difference is BS vs. DEL for CTRL-8 (makes
1038 * more sense and is consistent with usual terminal behaviour). */
1039 if (uc >= '@')
1040 string[0] = uc & 0x1F;
1041 else if (uc == '2')
1042 string[0] = NUL;
1043 else if (uc >= '3' && uc <= '7')
1044 string[0] = uc ^ 0x28;
1045 else if (uc == '8')
1046 string[0] = BS;
1047 else if (uc == '?')
1048 string[0] = DEL;
1049 else
1050 string[0] = uc;
1051 len = 1;
1052 }
1053 else
1054 {
1055 /* Translate a normal key to UTF-8. This doesn't work for dead
1056 * keys of course, you _have_ to use an input method for that. */
1057 len = utf_char2bytes((int)uc, string);
1058 }
1059 }
1060 else
1061 {
1062 /* Translate keys which are represented by ASCII control codes in Vim.
1063 * There are only a few of those; most control keys are translated to
1064 * special terminal-like control sequences. */
1065 len = 1;
1066 switch (keyval)
1067 {
1068 case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab:
1069 string[0] = TAB;
1070 break;
1071 case GDK_Linefeed:
1072 string[0] = NL;
1073 break;
1074 case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter:
1075 string[0] = CAR;
1076 break;
1077 case GDK_Escape:
1078 string[0] = ESC;
1079 break;
1080 default:
1081 len = 0;
1082 break;
1083 }
1084 }
1085 string[len] = NUL;
1086
1087 return len;
1088}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001090 static int
1091modifiers_gdk2vim(guint state)
1092{
1093 int modifiers = 0;
1094
1095 if (state & GDK_SHIFT_MASK)
1096 modifiers |= MOD_MASK_SHIFT;
1097 if (state & GDK_CONTROL_MASK)
1098 modifiers |= MOD_MASK_CTRL;
1099 if (state & GDK_MOD1_MASK)
1100 modifiers |= MOD_MASK_ALT;
Bram Moolenaar580061a2010-08-13 13:57:13 +02001101#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001102 if (state & GDK_SUPER_MASK)
1103 modifiers |= MOD_MASK_META;
1104#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001105 if (state & GDK_MOD4_MASK)
1106 modifiers |= MOD_MASK_META;
1107
1108 return modifiers;
1109}
1110
1111 static int
1112modifiers_gdk2mouse(guint state)
1113{
1114 int modifiers = 0;
1115
1116 if (state & GDK_SHIFT_MASK)
1117 modifiers |= MOUSE_SHIFT;
1118 if (state & GDK_CONTROL_MASK)
1119 modifiers |= MOUSE_CTRL;
1120 if (state & GDK_MOD1_MASK)
1121 modifiers |= MOUSE_ALT;
1122
1123 return modifiers;
1124}
1125
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126/*
1127 * Main keyboard handler:
1128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001130key_press_event(GtkWidget *widget UNUSED,
1131 GdkEventKey *event,
1132 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133{
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001134 /* For GTK+ 2 we know for sure how large the string might get.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 * (That is, up to 6 bytes + NUL + CSI escapes + safety measure.) */
1136 char_u string[32], string2[32];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 guint key_sym;
1138 int len;
1139 int i;
1140 int modifiers;
1141 int key;
1142 guint state;
1143 char_u *s, *d;
1144
Bram Moolenaar98921892016-02-23 17:14:37 +01001145#if GTK_CHECK_VERSION(3,0,0)
1146 is_key_pressed = TRUE;
1147 gui_mch_stop_blink();
1148#endif
1149
Bram Moolenaar20892c12011-06-26 04:49:00 +02001150 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 key_sym = event->keyval;
1152 state = event->state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153
1154#ifdef FEAT_XIM
1155 if (xim_queue_key_press_event(event, TRUE))
1156 return TRUE;
1157#endif
1158
1159#ifdef FEAT_HANGULIN
1160 if (key_sym == GDK_space && (state & GDK_SHIFT_MASK))
1161 {
1162 hangul_input_state_toggle();
1163 return TRUE;
1164 }
1165#endif
1166
1167#ifdef SunXK_F36
1168 /*
1169 * These keys have bogus lookup strings, and trapping them here is
1170 * easier than trying to XRebindKeysym() on them with every possible
1171 * combination of modifiers.
1172 */
1173 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
1174 len = 0;
1175 else
1176#endif
1177 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 len = keyval_to_string(key_sym, state, string2);
1179
1180 /* Careful: convert_input() doesn't handle the NUL character.
1181 * No need to convert pure ASCII anyway, thus the len > 1 check. */
1182 if (len > 1 && input_conv.vc_type != CONV_NONE)
1183 len = convert_input(string2, len, sizeof(string2));
1184
1185 s = string2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 d = string;
1187 for (i = 0; i < len; ++i)
1188 {
1189 *d++ = s[i];
1190 if (d[-1] == CSI && d + 2 < string + sizeof(string))
1191 {
1192 /* Turn CSI into K_CSI. */
1193 *d++ = KS_EXTRA;
1194 *d++ = (int)KE_CSI;
1195 }
1196 }
1197 len = d - string;
1198 }
1199
1200 /* Shift-Tab results in Left_Tab, but we want <S-Tab> */
1201 if (key_sym == GDK_ISO_Left_Tab)
1202 {
1203 key_sym = GDK_Tab;
1204 state |= GDK_SHIFT_MASK;
1205 }
1206
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207#ifdef FEAT_MENU
1208 /* If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key
1209 * is a menu shortcut, we ignore everything with the ALT modifier. */
1210 if ((state & GDK_MOD1_MASK)
1211 && gui.menu_is_active
1212 && (*p_wak == 'y'
1213 || (*p_wak == 'm'
1214 && len == 1
1215 && gui_is_menu_shortcut(string[0]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 /* For GTK2 we return false to signify that we haven't handled the
1217 * keypress, so that gtk will handle the mnemonic or accelerator. */
1218 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219#endif
1220
1221 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
1222 * that already has the 8th bit set.
1223 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
1224 * Don't do this for double-byte encodings, it turns the char into a lead
1225 * byte. */
1226 if (len == 1
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001227 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001228#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001229 || (state & GDK_SUPER_MASK)
1230#endif
1231 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 && !(key_sym == GDK_BackSpace || key_sym == GDK_Delete)
1233 && (string[0] & 0x80) == 0
1234 && !(key_sym == GDK_Tab && (state & GDK_SHIFT_MASK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 )
1237 {
1238 string[0] |= 0x80;
1239 state &= ~GDK_MOD1_MASK; /* don't use it again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 if (enc_utf8) /* convert to utf-8 */
1241 {
1242 string[1] = string[0] & 0xbf;
1243 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
1244 if (string[1] == CSI)
1245 {
1246 string[2] = KS_EXTRA;
1247 string[3] = (int)KE_CSI;
1248 len = 4;
1249 }
1250 else
1251 len = 2;
1252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 }
1254
1255 /* Check for special keys. Also do this when len == 1 (key has an ASCII
1256 * value) to detect backspace, delete and keypad keys. */
1257 if (len == 0 || len == 1)
1258 {
1259 for (i = 0; special_keys[i].key_sym != 0; i++)
1260 {
1261 if (special_keys[i].key_sym == key_sym)
1262 {
1263 string[0] = CSI;
1264 string[1] = special_keys[i].code0;
1265 string[2] = special_keys[i].code1;
1266 len = -3;
1267 break;
1268 }
1269 }
1270 }
1271
1272 if (len == 0) /* Unrecognized key */
1273 return TRUE;
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 /* Special keys (and a few others) may have modifiers. Also when using a
1276 * double-byte encoding (can't set the 8th bit). */
1277 if (len == -3 || key_sym == GDK_space || key_sym == GDK_Tab
1278 || key_sym == GDK_Return || key_sym == GDK_Linefeed
1279 || key_sym == GDK_Escape || key_sym == GDK_KP_Tab
1280 || key_sym == GDK_ISO_Enter || key_sym == GDK_3270_Enter
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001281 || (enc_dbcs && len == 1 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001282#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001283 || (state & GDK_SUPER_MASK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284#endif
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001285 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001287 modifiers = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288
1289 /*
1290 * For some keys a shift modifier is translated into another key
1291 * code.
1292 */
1293 if (len == -3)
1294 key = TO_SPECIAL(string[1], string[2]);
1295 else
1296 key = string[0];
1297
1298 key = simplify_key(key, &modifiers);
1299 if (key == CSI)
1300 key = K_CSI;
1301 if (IS_SPECIAL(key))
1302 {
1303 string[0] = CSI;
1304 string[1] = K_SECOND(key);
1305 string[2] = K_THIRD(key);
1306 len = 3;
1307 }
1308 else
1309 {
1310 string[0] = key;
1311 len = 1;
1312 }
1313
1314 if (modifiers != 0)
1315 {
1316 string2[0] = CSI;
1317 string2[1] = KS_MODIFIER;
1318 string2[2] = modifiers;
1319 add_to_input_buf(string2, 3);
1320 }
1321 }
1322
1323 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1324 || (string[0] == intr_char && intr_char != Ctrl_C)))
1325 {
1326 trash_input_buf();
1327 got_int = TRUE;
1328 }
1329
1330 add_to_input_buf(string, len);
1331
1332 /* blank out the pointer if necessary */
1333 if (p_mh)
1334 gui_mch_mousehide(TRUE);
1335
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 return TRUE;
1337}
1338
Bram Moolenaar98921892016-02-23 17:14:37 +01001339#if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001341key_release_event(GtkWidget *widget UNUSED,
1342 GdkEventKey *event,
1343 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
Bram Moolenaar98921892016-02-23 17:14:37 +01001345# if GTK_CHECK_VERSION(3,0,0)
1346 is_key_pressed = FALSE;
1347 gui_mch_start_blink();
1348# endif
1349# if defined(FEAT_XIM)
Bram Moolenaar20892c12011-06-26 04:49:00 +02001350 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 /*
1352 * GTK+ 2 input methods may do fancy stuff on key release events too.
1353 * With the default IM for instance, you can enter any UCS code point
1354 * by holding down CTRL-SHIFT and typing hexadecimal digits.
1355 */
1356 return xim_queue_key_press_event(event, FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001357# else
1358 return TRUE;
1359# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360}
1361#endif
1362
1363
1364/****************************************************************************
1365 * Selection handlers:
1366 */
1367
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001369selection_clear_event(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 GdkEventSelection *event,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001371 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372{
1373 if (event->selection == clip_plus.gtk_sel_atom)
1374 clip_lose_selection(&clip_plus);
1375 else
1376 clip_lose_selection(&clip_star);
1377
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 return TRUE;
1379}
1380
1381#define RS_NONE 0 /* selection_received_cb() not called yet */
1382#define RS_OK 1 /* selection_received_cb() called and OK */
1383#define RS_FAIL 2 /* selection_received_cb() called and failed */
1384static int received_selection = RS_NONE;
1385
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001387selection_received_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 GtkSelectionData *data,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001389 guint time_ UNUSED,
1390 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
1392 VimClipboard *cbd;
1393 char_u *text;
1394 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 guchar *tmpbuf_utf8 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 int len;
Bram Moolenaard44347f2011-06-19 01:14:29 +02001397 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398
Bram Moolenaar98921892016-02-23 17:14:37 +01001399#if GTK_CHECK_VERSION(3,0,0)
1400 if (gtk_selection_data_get_selection(data) == clip_plus.gtk_sel_atom)
1401#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 if (data->selection == clip_plus.gtk_sel_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 cbd = &clip_plus;
1405 else
1406 cbd = &clip_star;
1407
Bram Moolenaar98921892016-02-23 17:14:37 +01001408#if GTK_CHECK_VERSION(3,0,0)
1409 text = (char_u *)gtk_selection_data_get_data(data);
1410 len = gtk_selection_data_get_length(data);
1411#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 text = (char_u *)data->data;
1413 len = data->length;
Bram Moolenaar98921892016-02-23 17:14:37 +01001414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
1416 if (text == NULL || len <= 0)
1417 {
1418 received_selection = RS_FAIL;
1419 /* clip_free_selection(cbd); ??? */
1420
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 return;
1422 }
1423
Bram Moolenaar98921892016-02-23 17:14:37 +01001424#if GTK_CHECK_VERSION(3,0,0)
1425 if (gtk_selection_data_get_data_type(data) == vim_atom)
1426#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 if (data->type == vim_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 {
1430 motion_type = *text++;
1431 --len;
1432 }
Bram Moolenaar98921892016-02-23 17:14:37 +01001433#if GTK_CHECK_VERSION(3,0,0)
1434 else if (gtk_selection_data_get_data_type(data) == vimenc_atom)
1435#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 else if (data->type == vimenc_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 {
1439 char_u *enc;
1440 vimconv_T conv;
1441
1442 motion_type = *text++;
1443 --len;
1444
1445 enc = text;
1446 text += STRLEN(text) + 1;
1447 len -= text - enc;
1448
1449 /* If the encoding of the text is different from 'encoding', attempt
1450 * converting it. */
1451 conv.vc_type = CONV_NONE;
1452 convert_setup(&conv, enc, p_enc);
1453 if (conv.vc_type != CONV_NONE)
1454 {
1455 tmpbuf = string_convert(&conv, text, &len);
1456 if (tmpbuf != NULL)
1457 text = tmpbuf;
1458 convert_setup(&conv, NULL, NULL);
1459 }
1460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 /* gtk_selection_data_get_text() handles all the nasty details
1463 * and targets and encodings etc. This rocks so hard. */
1464 else
1465 {
1466 tmpbuf_utf8 = gtk_selection_data_get_text(data);
1467 if (tmpbuf_utf8 != NULL)
1468 {
1469 len = STRLEN(tmpbuf_utf8);
1470 if (input_conv.vc_type != CONV_NONE)
1471 {
1472 tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len);
1473 if (tmpbuf != NULL)
1474 text = tmpbuf;
1475 }
1476 else
1477 text = tmpbuf_utf8;
1478 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001479 else if (len >= 2 && text[0] == 0xff && text[1] == 0xfe)
1480 {
1481 vimconv_T conv;
1482
1483 /* UTF-16, we get this for HTML */
1484 conv.vc_type = CONV_NONE;
1485 convert_setup_ext(&conv, (char_u *)"utf-16le", FALSE, p_enc, TRUE);
1486
1487 if (conv.vc_type != CONV_NONE)
1488 {
1489 text += 2;
1490 len -= 2;
1491 tmpbuf = string_convert(&conv, text, &len);
1492 convert_setup(&conv, NULL, NULL);
1493 }
1494 if (tmpbuf != NULL)
1495 text = tmpbuf;
1496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001499 /* Chop off any trailing NUL bytes. OpenOffice sends these. */
Bram Moolenaara76638f2010-06-05 12:49:46 +02001500 while (len > 0 && text[len - 1] == NUL)
1501 --len;
1502
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 clip_yank_selection(motion_type, text, (long)len, cbd);
1504 received_selection = RS_OK;
1505 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 g_free(tmpbuf_utf8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507}
1508
1509/*
1510 * Prepare our selection data for passing it to the external selection
1511 * client.
1512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001514selection_get_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 GtkSelectionData *selection_data,
1516 guint info,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001517 guint time_ UNUSED,
1518 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
1520 char_u *string;
1521 char_u *tmpbuf;
1522 long_u tmplen;
1523 int length;
1524 int motion_type;
1525 GdkAtom type;
1526 VimClipboard *cbd;
1527
Bram Moolenaar98921892016-02-23 17:14:37 +01001528#if GTK_CHECK_VERSION(3,0,0)
1529 if (gtk_selection_data_get_selection(selection_data)
1530 == clip_plus.gtk_sel_atom)
1531#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 if (selection_data->selection == clip_plus.gtk_sel_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 cbd = &clip_plus;
1535 else
1536 cbd = &clip_star;
1537
1538 if (!cbd->owned)
1539 return; /* Shouldn't ever happen */
1540
1541 if (info != (guint)TARGET_STRING
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001542 && (!clip_html || info != (guint)TARGET_HTML)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 && info != (guint)TARGET_UTF8_STRING
1544 && info != (guint)TARGET_VIMENC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 && info != (guint)TARGET_VIM
1546 && info != (guint)TARGET_COMPOUND_TEXT
1547 && info != (guint)TARGET_TEXT)
1548 return;
1549
1550 /* get the selection from the '*'/'+' register */
1551 clip_get_selection(cbd);
1552
1553 motion_type = clip_convert_selection(&string, &tmplen, cbd);
1554 if (motion_type < 0 || string == NULL)
1555 return;
1556 /* Due to int arguments we can't handle more than G_MAXINT. Also
1557 * reserve one extra byte for NUL or the motion type; just in case.
1558 * (Not that pasting 2G of text is ever going to work, but... ;-) */
1559 length = MIN(tmplen, (long_u)(G_MAXINT - 1));
1560
1561 if (info == (guint)TARGET_VIM)
1562 {
1563 tmpbuf = alloc((unsigned)length + 1);
1564 if (tmpbuf != NULL)
1565 {
1566 tmpbuf[0] = motion_type;
1567 mch_memmove(tmpbuf + 1, string, (size_t)length);
1568 }
1569 /* For our own format, the first byte contains the motion type */
1570 ++length;
1571 vim_free(string);
1572 string = tmpbuf;
1573 type = vim_atom;
1574 }
1575
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001576 else if (info == (guint)TARGET_HTML)
1577 {
1578 vimconv_T conv;
1579
1580 /* Since we get utf-16, we probably should set it as well. */
1581 conv.vc_type = CONV_NONE;
1582 convert_setup_ext(&conv, p_enc, TRUE, (char_u *)"utf-16le", FALSE);
1583 if (conv.vc_type != CONV_NONE)
1584 {
1585 tmpbuf = string_convert(&conv, string, &length);
1586 convert_setup(&conv, NULL, NULL);
1587 vim_free(string);
1588 string = tmpbuf;
1589 }
1590
1591 /* Prepend the BOM: "fffe" */
1592 if (string != NULL)
1593 {
1594 tmpbuf = alloc(length + 2);
1595 tmpbuf[0] = 0xff;
1596 tmpbuf[1] = 0xfe;
1597 mch_memmove(tmpbuf + 2, string, (size_t)length);
1598 vim_free(string);
1599 string = tmpbuf;
1600 length += 2;
1601
Bram Moolenaar98921892016-02-23 17:14:37 +01001602#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01001603 /* Looks redundant even for GTK2 because these values are
Bram Moolenaar98921892016-02-23 17:14:37 +01001604 * overwritten by gtk_selection_data_set() that follows. */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001605 selection_data->type = selection_data->target;
1606 selection_data->format = 16; /* 16 bits per char */
Bram Moolenaar98921892016-02-23 17:14:37 +01001607#endif
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001608 gtk_selection_data_set(selection_data, html_atom, 16,
1609 string, length);
1610 vim_free(string);
1611 }
1612 return;
1613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 else if (info == (guint)TARGET_VIMENC)
1615 {
1616 int l = STRLEN(p_enc);
1617
1618 /* contents: motion_type 'encoding' NUL text */
1619 tmpbuf = alloc((unsigned)length + l + 2);
1620 if (tmpbuf != NULL)
1621 {
1622 tmpbuf[0] = motion_type;
1623 STRCPY(tmpbuf + 1, p_enc);
1624 mch_memmove(tmpbuf + l + 2, string, (size_t)length);
1625 }
1626 length += l + 2;
1627 vim_free(string);
1628 string = tmpbuf;
1629 type = vimenc_atom;
1630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 /* gtk_selection_data_set_text() handles everything for us. This is
1633 * so easy and simple and cool, it'd be insane not to use it. */
1634 else
1635 {
1636 if (output_conv.vc_type != CONV_NONE)
1637 {
1638 tmpbuf = string_convert(&output_conv, string, &length);
1639 vim_free(string);
1640 if (tmpbuf == NULL)
1641 return;
1642 string = tmpbuf;
1643 }
1644 /* Validate the string to avoid runtime warnings */
1645 if (g_utf8_validate((const char *)string, (gssize)length, NULL))
1646 {
1647 gtk_selection_data_set_text(selection_data,
1648 (const char *)string, length);
1649 }
1650 vim_free(string);
1651 return;
1652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653
1654 if (string != NULL)
1655 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001656#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01001657 /* Looks redundant even for GTK2 because these values are
Bram Moolenaar98921892016-02-23 17:14:37 +01001658 * overwritten by gtk_selection_data_set() that follows. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 selection_data->type = selection_data->target;
1660 selection_data->format = 8; /* 8 bits per char */
Bram Moolenaar98921892016-02-23 17:14:37 +01001661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 gtk_selection_data_set(selection_data, type, 8, string, length);
1663 vim_free(string);
1664 }
1665}
1666
1667/*
Bram Moolenaar29695702012-05-18 17:03:18 +02001668 * Check if the GUI can be started. Called before gvimrc is sourced and
1669 * before fork().
1670 * Return OK or FAIL.
1671 */
1672 int
Bram Moolenaar717e1962016-08-10 21:28:44 +02001673gui_mch_early_init_check(int give_message)
Bram Moolenaar29695702012-05-18 17:03:18 +02001674{
1675 char_u *p;
1676
1677 /* Guess that when $DISPLAY isn't set the GUI can't start. */
1678 p = mch_getenv((char_u *)"DISPLAY");
1679 if (p == NULL || *p == NUL)
1680 {
1681 gui.dying = TRUE;
Bram Moolenaar717e1962016-08-10 21:28:44 +02001682 if (give_message)
1683 EMSG(_((char *)e_opendisp));
Bram Moolenaar29695702012-05-18 17:03:18 +02001684 return FAIL;
1685 }
1686 return OK;
1687}
1688
1689/*
1690 * Check if the GUI can be started. Called before gvimrc is sourced but after
1691 * fork().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 * Return OK or FAIL.
1693 */
1694 int
1695gui_mch_init_check(void)
1696{
Bram Moolenaar3a466a82016-01-19 17:47:25 +01001697#ifdef USE_GRESOURCE
1698 static int res_registered = FALSE;
1699
1700 if (!res_registered)
1701 {
1702 /* Call this function in the GUI process; otherwise, the resources
1703 * won't be available. Don't call it twice. */
1704 res_registered = TRUE;
1705 gui_gtk_register_resource();
1706 }
1707#endif
1708
Bram Moolenaarf80663f2016-04-05 21:56:06 +02001709#if GTK_CHECK_VERSION(3,10,0)
1710 /* Vim currently assumes that Gtk means X11, so it cannot use native Gtk
1711 * support for other backends such as Wayland. */
1712 gdk_set_allowed_backends ("x11");
1713#endif
1714
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715#ifdef FEAT_GUI_GNOME
1716 if (gtk_socket_id == 0)
1717 using_gnome = 1;
1718#endif
1719
Bram Moolenaar8dd79012013-05-21 12:52:04 +02001720 /* This defaults to argv[0], but we want it to match the name of the
1721 * shipped gvim.desktop so that Vim's windows can be associated with this
1722 * file. */
1723 g_set_prgname("gvim");
1724
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 /* Don't use gtk_init() or gnome_init(), it exits on failure. */
1726 if (!gtk_init_check(&gui_argc, &gui_argv))
1727 {
1728 gui.dying = TRUE;
Bram Moolenaarc93e7912008-07-08 10:46:08 +00001729 EMSG(_((char *)e_opendisp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 return FAIL;
1731 }
1732
1733 return OK;
1734}
1735
1736
1737/****************************************************************************
1738 * Mouse handling callbacks
1739 */
1740
1741
1742static guint mouse_click_timer = 0;
1743static int mouse_timed_out = TRUE;
1744
1745/*
1746 * Timer used to recognize multiple clicks of the mouse button
1747 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001748#if GTK_CHECK_VERSION(3,0,0)
1749 static gboolean
1750#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +01001752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753mouse_click_timer_cb(gpointer data)
1754{
1755 /* we don't use this information currently */
1756 int *timed_out = (int *) data;
1757
1758 *timed_out = TRUE;
1759 return FALSE; /* don't happen again */
1760}
1761
1762static guint motion_repeat_timer = 0;
1763static int motion_repeat_offset = FALSE;
Bram Moolenaar98921892016-02-23 17:14:37 +01001764#ifdef GTK_DEST_DEFAULT_ALL
1765static gboolean motion_repeat_timer_cb(gpointer);
1766#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767static gint motion_repeat_timer_cb(gpointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01001768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
1770 static void
1771process_motion_notify(int x, int y, GdkModifierType state)
1772{
1773 int button;
1774 int_u vim_modifiers;
Bram Moolenaar98921892016-02-23 17:14:37 +01001775#if GTK_CHECK_VERSION(3,0,0)
1776 GtkAllocation allocation;
1777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778
1779 button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1780 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1781 GDK_BUTTON5_MASK))
1782 ? MOUSE_DRAG : ' ';
1783
1784 /* If our pointer is currently hidden, then we should show it. */
1785 gui_mch_mousehide(FALSE);
1786
1787 /* Just moving the rodent above the drawing area without any button
1788 * being pressed. */
1789 if (button != MOUSE_DRAG)
1790 {
1791 gui_mouse_moved(x, y);
1792 return;
1793 }
1794
1795 /* translate modifier coding between the main engine and GTK */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001796 vim_modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
Bram Moolenaar49325942007-05-10 19:19:59 +00001798 /* inform the editor engine about the occurrence of this event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 gui_send_mouse_event(button, x, y, FALSE, vim_modifiers);
1800
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 /*
1802 * Auto repeat timer handling.
1803 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001804#if GTK_CHECK_VERSION(3,0,0)
1805 gtk_widget_get_allocation(gui.drawarea, &allocation);
1806
1807 if (x < 0 || y < 0
1808 || x >= allocation.width
1809 || y >= allocation.height)
1810#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 if (x < 0 || y < 0
1812 || x >= gui.drawarea->allocation.width
1813 || y >= gui.drawarea->allocation.height)
Bram Moolenaar98921892016-02-23 17:14:37 +01001814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 {
1816
1817 int dx;
1818 int dy;
1819 int offshoot;
1820 int delay = 10;
1821
1822 /* Calculate the maximal distance of the cursor from the drawing area.
1823 * (offshoot can't become negative here!).
1824 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001825#if GTK_CHECK_VERSION(3,0,0)
1826 dx = x < 0 ? -x : x - allocation.width;
1827 dy = y < 0 ? -y : y - allocation.height;
1828#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 dx = x < 0 ? -x : x - gui.drawarea->allocation.width;
1830 dy = y < 0 ? -y : y - gui.drawarea->allocation.height;
Bram Moolenaar98921892016-02-23 17:14:37 +01001831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
1833 offshoot = dx > dy ? dx : dy;
1834
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001835 /* Make a linearly decaying timer delay with a threshold of 5 at a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 * distance of 127 pixels from the main window.
1837 *
1838 * One could think endlessly about the most ergonomic variant here.
1839 * For example it could make sense to calculate the distance from the
1840 * drags start instead...
1841 *
1842 * Maybe a parabolic interpolation would suite us better here too...
1843 */
1844 if (offshoot > 127)
1845 {
1846 /* 5 appears to be somehow near to my perceptual limits :-). */
1847 delay = 5;
1848 }
1849 else
1850 {
1851 delay = (130 * (127 - offshoot)) / 127 + 5;
1852 }
1853
1854 /* shoot again */
1855 if (!motion_repeat_timer)
Bram Moolenaar98921892016-02-23 17:14:37 +01001856#if GTK_CHECK_VERSION(3,0,0)
1857 motion_repeat_timer = g_timeout_add((guint)delay,
1858 motion_repeat_timer_cb, NULL);
1859#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 motion_repeat_timer = gtk_timeout_add((guint32)delay,
1861 motion_repeat_timer_cb, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01001862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 }
1864}
1865
Bram Moolenaar98921892016-02-23 17:14:37 +01001866#if GTK_CHECK_VERSION(3,0,0)
1867 static GdkDevice *
1868gui_gtk_get_pointer_device(GtkWidget *widget)
1869{
1870 GdkWindow * const win = gtk_widget_get_window(widget);
1871 GdkDisplay * const dpy = gdk_window_get_display(win);
Bram Moolenaar30e12d22016-04-17 20:49:53 +02001872# if GTK_CHECK_VERSION(3,20,0)
1873 GdkSeat * const seat = gdk_display_get_default_seat(dpy);
1874 return gdk_seat_get_pointer(seat);
1875# else
Bram Moolenaar98921892016-02-23 17:14:37 +01001876 GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy);
1877 return gdk_device_manager_get_client_pointer(mngr);
Bram Moolenaar30e12d22016-04-17 20:49:53 +02001878# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01001879}
1880
1881 static GdkWindow *
1882gui_gtk_get_pointer(GtkWidget *widget,
1883 gint *x,
1884 gint *y,
1885 GdkModifierType *state)
1886{
1887 GdkWindow * const win = gtk_widget_get_window(widget);
1888 GdkDevice * const dev = gui_gtk_get_pointer_device(widget);
1889 return gdk_window_get_device_position(win, dev , x, y, state);
1890}
1891
Bram Moolenaar2369c152016-03-04 23:08:25 +01001892# if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar98921892016-02-23 17:14:37 +01001893 static GdkWindow *
1894gui_gtk_window_at_position(GtkWidget *widget,
1895 gint *x,
1896 gint *y)
1897{
1898 GdkDevice * const dev = gui_gtk_get_pointer_device(widget);
1899 return gdk_device_get_window_at_position(dev, x, y);
1900}
Bram Moolenaar2369c152016-03-04 23:08:25 +01001901# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01001902#endif
1903
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904/*
1905 * Timer used to recognize multiple clicks of the mouse button.
1906 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001907#if GTK_CHECK_VERSION(3,0,0)
1908 static gboolean
1909#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +01001911#endif
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001912motion_repeat_timer_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913{
1914 int x;
1915 int y;
1916 GdkModifierType state;
1917
Bram Moolenaar98921892016-02-23 17:14:37 +01001918#if GTK_CHECK_VERSION(3,0,0)
1919 gui_gtk_get_pointer(gui.drawarea, &x, &y, &state);
1920#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 gdk_window_get_pointer(gui.drawarea->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +01001922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923
1924 if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1925 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1926 GDK_BUTTON5_MASK)))
1927 {
1928 motion_repeat_timer = 0;
1929 return FALSE;
1930 }
1931
1932 /* If there already is a mouse click in the input buffer, wait another
1933 * time (otherwise we would create a backlog of clicks) */
1934 if (vim_used_in_input_buf() > 10)
1935 return TRUE;
1936
1937 motion_repeat_timer = 0;
1938
1939 /*
1940 * Fake a motion event.
1941 * Trick: Pretend the mouse moved to the next character on every other
1942 * event, otherwise drag events will be discarded, because they are still
1943 * in the same character.
1944 */
1945 if (motion_repeat_offset)
1946 x += gui.char_width;
1947
1948 motion_repeat_offset = !motion_repeat_offset;
1949 process_motion_notify(x, y, state);
1950
1951 /* Don't happen again. We will get reinstalled in the synthetic event
1952 * if needed -- thus repeating should still work. */
1953 return FALSE;
1954}
1955
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001957motion_notify_event(GtkWidget *widget,
1958 GdkEventMotion *event,
1959 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960{
1961 if (event->is_hint)
1962 {
1963 int x;
1964 int y;
1965 GdkModifierType state;
1966
Bram Moolenaar98921892016-02-23 17:14:37 +01001967#if GTK_CHECK_VERSION(3,0,0)
1968 gui_gtk_get_pointer(widget, &x, &y, &state);
1969#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 gdk_window_get_pointer(widget->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +01001971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 process_motion_notify(x, y, state);
1973 }
1974 else
1975 {
1976 process_motion_notify((int)event->x, (int)event->y,
1977 (GdkModifierType)event->state);
1978 }
1979
1980 return TRUE; /* handled */
1981}
1982
1983
1984/*
1985 * Mouse button handling. Note please that we are capturing multiple click's
1986 * by our own timeout mechanism instead of the one provided by GTK+ itself.
1987 * This is due to the way the generic VIM code is recognizing multiple clicks.
1988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001990button_press_event(GtkWidget *widget,
1991 GdkEventButton *event,
1992 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993{
1994 int button;
1995 int repeated_click = FALSE;
1996 int x, y;
1997 int_u vim_modifiers;
1998
Bram Moolenaar20892c12011-06-26 04:49:00 +02001999 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 /* Make sure we have focus now we've been selected */
Bram Moolenaar98921892016-02-23 17:14:37 +01002002#if GTK_CHECK_VERSION(3,0,0)
2003 if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget))
2004#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01002006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 gtk_widget_grab_focus(widget);
2008
2009 /*
2010 * Don't let additional events about multiple clicks send by GTK to us
2011 * after the initial button press event confuse us.
2012 */
2013 if (event->type != GDK_BUTTON_PRESS)
2014 return FALSE;
2015
2016 x = event->x;
2017 y = event->y;
2018
2019 /* Handle multiple clicks */
2020 if (!mouse_timed_out && mouse_click_timer)
2021 {
Bram Moolenaar98921892016-02-23 17:14:37 +01002022#if GTK_CHECK_VERSION(3,0,0)
2023 g_source_remove(mouse_click_timer);
2024#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 gtk_timeout_remove(mouse_click_timer);
Bram Moolenaar98921892016-02-23 17:14:37 +01002026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 mouse_click_timer = 0;
2028 repeated_click = TRUE;
2029 }
2030
2031 mouse_timed_out = FALSE;
Bram Moolenaar98921892016-02-23 17:14:37 +01002032#if GTK_CHECK_VERSION(3,0,0)
2033 mouse_click_timer = g_timeout_add((guint)p_mouset,
2034 mouse_click_timer_cb, &mouse_timed_out);
2035#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 mouse_click_timer = gtk_timeout_add((guint32)p_mouset,
2037 mouse_click_timer_cb, &mouse_timed_out);
Bram Moolenaar98921892016-02-23 17:14:37 +01002038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039
2040 switch (event->button)
2041 {
Bram Moolenaar88e484b2015-11-24 15:38:44 +01002042 /* Keep in sync with gui_x11.c.
2043 * Buttons 4-7 are handled in scroll_event() */
2044 case 1: button = MOUSE_LEFT; break;
2045 case 2: button = MOUSE_MIDDLE; break;
2046 case 3: button = MOUSE_RIGHT; break;
2047 case 8: button = MOUSE_X1; break;
2048 case 9: button = MOUSE_X2; break;
2049 default:
2050 return FALSE; /* Unknown button */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 }
2052
2053#ifdef FEAT_XIM
2054 /* cancel any preediting */
2055 if (im_is_preediting())
2056 xim_reset();
2057#endif
2058
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002059 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060
2061 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062
2063 return TRUE;
2064}
2065
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066/*
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002067 * GTK+ 2 abstracts scrolling via the GdkEventScroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002070scroll_event(GtkWidget *widget,
2071 GdkEventScroll *event,
2072 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073{
2074 int button;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002075 int_u vim_modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076
Bram Moolenaar98921892016-02-23 17:14:37 +01002077#if GTK_CHECK_VERSION(3,0,0)
2078 if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget))
2079#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01002081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 gtk_widget_grab_focus(widget);
2083
2084 switch (event->direction)
2085 {
2086 case GDK_SCROLL_UP:
2087 button = MOUSE_4;
2088 break;
2089 case GDK_SCROLL_DOWN:
2090 button = MOUSE_5;
2091 break;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002092 case GDK_SCROLL_LEFT:
2093 button = MOUSE_7;
2094 break;
2095 case GDK_SCROLL_RIGHT:
2096 button = MOUSE_6;
2097 break;
2098 default: /* This shouldn't happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 return FALSE;
2100 }
2101
2102# ifdef FEAT_XIM
2103 /* cancel any preediting */
2104 if (im_is_preediting())
2105 xim_reset();
2106# endif
2107
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002108 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109
2110 gui_send_mouse_event(button, (int)event->x, (int)event->y,
2111 FALSE, vim_modifiers);
2112
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 return TRUE;
2114}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115
2116
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002118button_release_event(GtkWidget *widget UNUSED,
2119 GdkEventButton *event,
2120 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121{
2122 int x, y;
2123 int_u vim_modifiers;
2124
Bram Moolenaar20892c12011-06-26 04:49:00 +02002125 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002126
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 /* Remove any motion "machine gun" timers used for automatic further
2128 extension of allocation areas if outside of the applications window
2129 area .*/
2130 if (motion_repeat_timer)
2131 {
Bram Moolenaar98921892016-02-23 17:14:37 +01002132#if GTK_CHECK_VERSION(3,0,0)
2133 g_source_remove(motion_repeat_timer);
2134#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 gtk_timeout_remove(motion_repeat_timer);
Bram Moolenaar98921892016-02-23 17:14:37 +01002136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 motion_repeat_timer = 0;
2138 }
2139
2140 x = event->x;
2141 y = event->y;
2142
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002143 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144
2145 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146
2147 return TRUE;
2148}
2149
2150
2151#ifdef FEAT_DND
2152/****************************************************************************
2153 * Drag aNd Drop support handlers.
2154 */
2155
2156/*
2157 * Count how many items there may be and separate them with a NUL.
2158 * Apparently the items are separated with \r\n. This is not documented,
2159 * thus be careful not to go past the end. Also allow separation with
2160 * NUL characters.
2161 */
2162 static int
2163count_and_decode_uri_list(char_u *out, char_u *raw, int len)
2164{
2165 int i;
2166 char_u *p = out;
2167 int count = 0;
2168
2169 for (i = 0; i < len; ++i)
2170 {
2171 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r')
2172 {
2173 if (p > out && p[-1] != NUL)
2174 {
2175 ++count;
2176 *p++ = NUL;
2177 }
2178 }
2179 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0)
2180 {
2181 *p++ = hexhex2nr(raw + i + 1);
2182 i += 2;
2183 }
2184 else
2185 *p++ = raw[i];
2186 }
2187 if (p > out && p[-1] != NUL)
2188 {
2189 *p = NUL; /* last item didn't have \r or \n */
2190 ++count;
2191 }
2192 return count;
2193}
2194
2195/*
2196 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
2197 * this process, URI which protocol is not "file:" are removed. Return
2198 * length of array (less than "max").
2199 */
2200 static int
2201filter_uri_list(char_u **outlist, int max, char_u *src)
2202{
2203 int i, j;
2204
2205 for (i = j = 0; i < max; ++i)
2206 {
2207 outlist[i] = NULL;
2208 if (STRNCMP(src, "file:", 5) == 0)
2209 {
2210 src += 5;
2211 if (STRNCMP(src, "//localhost", 11) == 0)
2212 src += 11;
2213 while (src[0] == '/' && src[1] == '/')
2214 ++src;
2215 outlist[j++] = vim_strsave(src);
2216 }
2217 src += STRLEN(src) + 1;
2218 }
2219 return j;
2220}
2221
2222 static char_u **
2223parse_uri_list(int *count, char_u *data, int len)
2224{
2225 int n = 0;
2226 char_u *tmp = NULL;
Bram Moolenaar945ec092016-06-08 21:17:43 +02002227 char_u **array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228
2229 if (data != NULL && len > 0 && (tmp = (char_u *)alloc(len + 1)) != NULL)
2230 {
2231 n = count_and_decode_uri_list(tmp, data, len);
2232 if (n > 0 && (array = (char_u **)alloc(n * sizeof(char_u *))) != NULL)
2233 n = filter_uri_list(array, n, tmp);
2234 }
2235 vim_free(tmp);
2236 *count = n;
2237 return array;
2238}
2239
2240 static void
2241drag_handle_uri_list(GdkDragContext *context,
2242 GtkSelectionData *data,
2243 guint time_,
2244 GdkModifierType state,
2245 gint x,
2246 gint y)
2247{
2248 char_u **fnames;
2249 int nfiles = 0;
2250
Bram Moolenaar98921892016-02-23 17:14:37 +01002251# if GTK_CHECK_VERSION(3,0,0)
2252 fnames = parse_uri_list(&nfiles,
2253 (char_u *)gtk_selection_data_get_data(data),
2254 gtk_selection_data_get_length(data));
2255# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 fnames = parse_uri_list(&nfiles, data->data, data->length);
Bram Moolenaar98921892016-02-23 17:14:37 +01002257# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258
2259 if (fnames != NULL && nfiles > 0)
2260 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002261 int_u modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262
2263 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
2264
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002265 modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266
2267 gui_handle_drop(x, y, modifiers, fnames, nfiles);
2268 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002269 else
2270 vim_free(fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271}
2272
2273 static void
2274drag_handle_text(GdkDragContext *context,
2275 GtkSelectionData *data,
2276 guint time_,
2277 GdkModifierType state)
2278{
2279 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP};
2280 char_u *text;
2281 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283
Bram Moolenaar98921892016-02-23 17:14:37 +01002284# if GTK_CHECK_VERSION(3,0,0)
2285 text = (char_u *)gtk_selection_data_get_data(data);
2286 len = gtk_selection_data_get_length(data);
2287# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 text = data->data;
2289 len = data->length;
Bram Moolenaar98921892016-02-23 17:14:37 +01002290# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291
Bram Moolenaar98921892016-02-23 17:14:37 +01002292# if GTK_CHECK_VERSION(3,0,0)
2293 if (gtk_selection_data_get_data_type(data) == utf8_string_atom)
2294# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 if (data->type == utf8_string_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01002296# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 if (input_conv.vc_type != CONV_NONE)
2299 tmpbuf = string_convert(&input_conv, text, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 if (tmpbuf != NULL)
2301 text = tmpbuf;
2302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303
2304 dnd_yank_drag_data(text, (long)len);
2305 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002308 dropkey[2] = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309
2310 if (dropkey[2] != 0)
2311 add_to_input_buf(dropkey, (int)sizeof(dropkey));
2312 else
2313 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314}
2315
2316/*
2317 * DND receiver.
2318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 static void
2320drag_data_received_cb(GtkWidget *widget,
2321 GdkDragContext *context,
2322 gint x,
2323 gint y,
2324 GtkSelectionData *data,
2325 guint info,
2326 guint time_,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002327 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328{
2329 GdkModifierType state;
2330
2331 /* Guard against trash */
Bram Moolenaar98921892016-02-23 17:14:37 +01002332# if GTK_CHECK_VERSION(3,0,0)
2333 const guchar * const data_data = gtk_selection_data_get_data(data);
2334 const gint data_length = gtk_selection_data_get_length(data);
2335 const gint data_format = gtk_selection_data_get_format(data);
2336
2337 if (data_data == NULL
2338 || data_length <= 0
2339 || data_format != 8
2340 || data_data[data_length] != '\0')
2341# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 if (data->data == NULL
2343 || data->length <= 0
2344 || data->format != 8
2345 || data->data[data->length] != '\0')
Bram Moolenaar98921892016-02-23 17:14:37 +01002346# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
2348 gtk_drag_finish(context, FALSE, FALSE, time_);
2349 return;
2350 }
2351
2352 /* Get the current modifier state for proper distinguishment between
2353 * different operations later. */
Bram Moolenaar98921892016-02-23 17:14:37 +01002354#if GTK_CHECK_VERSION(3,0,0)
2355 gui_gtk_get_pointer(widget, NULL, NULL, &state);
2356# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 gdk_window_get_pointer(widget->window, NULL, NULL, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +01002358# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359
2360 /* Not sure about the role of "text/plain" here... */
2361 if (info == (guint)TARGET_TEXT_URI_LIST)
2362 drag_handle_uri_list(context, data, time_, state, x, y);
2363 else
2364 drag_handle_text(context, data, time_, state);
2365
2366}
2367#endif /* FEAT_DND */
2368
2369
2370#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2371/*
2372 * GnomeClient interact callback. Check for unsaved buffers that cannot
2373 * be abandoned and pop up a dialog asking the user for confirmation if
2374 * necessary.
2375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002377sm_client_check_changed_any(GnomeClient *client UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 gint key,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002379 GnomeDialogType type UNUSED,
2380 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
2382 cmdmod_T save_cmdmod;
2383 gboolean shutdown_cancelled;
2384
2385 save_cmdmod = cmdmod;
2386
2387# ifdef FEAT_BROWSE
2388 cmdmod.browse = TRUE;
2389# endif
2390# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2391 cmdmod.confirm = TRUE;
2392# endif
2393 /*
2394 * If there are changed buffers, present the user with
2395 * a dialog if possible, otherwise give an error message.
2396 */
Bram Moolenaar027387f2016-01-02 22:25:52 +01002397 shutdown_cancelled = check_changed_any(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
2399 exiting = FALSE;
2400 cmdmod = save_cmdmod;
2401 setcursor(); /* position the cursor */
2402 out_flush();
2403 /*
2404 * If the user hit the [Cancel] button the whole shutdown
2405 * will be cancelled. Wow, quite powerful feature (:
2406 */
2407 gnome_interaction_key_return(key, shutdown_cancelled);
2408}
2409
2410/*
2411 * Generate a script that can be used to restore the current editing session.
2412 * Save the value of v:this_session before running :mksession in order to make
2413 * automagic session save fully transparent. Return TRUE on success.
2414 */
2415 static int
2416write_session_file(char_u *filename)
2417{
2418 char_u *escaped_filename;
2419 char *mksession_cmdline;
2420 unsigned int save_ssop_flags;
2421 int failed;
2422
2423 /*
2424 * Build an ex command line to create a script that restores the current
2425 * session if executed. Escape the filename to avoid nasty surprises.
2426 */
2427 escaped_filename = vim_strsave_escaped(filename, escape_chars);
2428 if (escaped_filename == NULL)
2429 return FALSE;
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002430 mksession_cmdline = g_strconcat("mksession ", (char *)escaped_filename,
2431 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 vim_free(escaped_filename);
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002433
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 /*
2435 * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
2436 * unpredictable effects when the session is saved automatically. Also,
2437 * we definitely need SSOP_GLOBALS to be able to restore v:this_session.
2438 * Don't use SSOP_BUFFERS to prevent the buffer list from becoming
2439 * enormously large if the GNOME session feature is used regularly.
2440 */
2441 save_ssop_flags = ssop_flags;
2442 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002443 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
2445 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
2446 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
2447 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
Bram Moolenaar485db9b2005-01-31 19:23:41 +00002448 do_unlet((char_u *)"Save_VV_this_session", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449
2450 ssop_flags = save_ssop_flags;
2451 g_free(mksession_cmdline);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002452
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 /*
2454 * Reopen the file and append a command to restore v:this_session,
2455 * as if this save never happened. This is to avoid conflicts with
2456 * the user's own sessions. FIXME: It's probably less hackish to add
2457 * a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
2458 */
2459 if (!failed)
2460 {
2461 FILE *fd;
2462
2463 fd = open_exfile(filename, TRUE, APPENDBIN);
2464
2465 failed = (fd == NULL
2466 || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL
2467 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
2468
2469 if (fd != NULL && fclose(fd) != 0)
2470 failed = TRUE;
2471
2472 if (failed)
2473 mch_remove(filename);
2474 }
2475
2476 return !failed;
2477}
2478
2479/*
2480 * "save_yourself" signal handler. Initiate an interaction to ask the user
2481 * for confirmation if necessary. Save the current editing session and tell
2482 * the session manager how to restart Vim.
2483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 static gboolean
2485sm_client_save_yourself(GnomeClient *client,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002486 gint phase UNUSED,
2487 GnomeSaveStyle save_style UNUSED,
2488 gboolean shutdown UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 GnomeInteractStyle interact_style,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002490 gboolean fast UNUSED,
2491 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492{
2493 static const char suffix[] = "-session.vim";
2494 char *session_file;
2495 unsigned int len;
2496 gboolean success;
2497
2498 /* Always request an interaction if possible. check_changed_any()
2499 * won't actually show a dialog unless any buffers have been modified.
2500 * There doesn't seem to be an obvious way to check that without
2501 * automatically firing the dialog. Anyway, it works just fine. */
2502 if (interact_style == GNOME_INTERACT_ANY)
2503 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL,
2504 &sm_client_check_changed_any,
2505 NULL);
2506 out_flush();
2507 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2508
2509 /* The path is unique for each session save. We do neither know nor care
2510 * which session script will actually be used later. This decision is in
2511 * the domain of the session manager. */
2512 session_file = gnome_config_get_real_path(
2513 gnome_client_get_config_prefix(client));
2514 len = strlen(session_file);
2515
2516 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR)
2517 --len; /* get rid of the superfluous trailing '/' */
2518
2519 session_file = g_renew(char, session_file, len + sizeof(suffix));
2520 memcpy(session_file + len, suffix, sizeof(suffix));
2521
2522 success = write_session_file((char_u *)session_file);
2523
2524 if (success)
2525 {
2526 const char *argv[8];
2527 int i;
2528
2529 /* Tell the session manager how to wipe out the stored session data.
2530 * This isn't as dangerous as it looks, don't worry :) session_file
2531 * is a unique absolute filename. Usually it'll be something like
2532 * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */
2533 i = 0;
2534 argv[i++] = "rm";
2535 argv[i++] = session_file;
2536 argv[i] = NULL;
2537
2538 gnome_client_set_discard_command(client, i, (char **)argv);
2539
2540 /* Tell the session manager how to restore the just saved session.
2541 * This is easily done thanks to Vim's -S option. Pass the -f flag
2542 * since there's no need to fork -- it might even cause confusion.
2543 * Also pass the window role to give the WM something to match on.
2544 * The role is set in gui_mch_open(), thus should _never_ be NULL. */
2545 i = 0;
2546 argv[i++] = restart_command;
2547 argv[i++] = "-f";
2548 argv[i++] = "-g";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 argv[i++] = "--role";
2550 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 argv[i++] = "-S";
2552 argv[i++] = session_file;
2553 argv[i] = NULL;
2554
2555 gnome_client_set_restart_command(client, i, (char **)argv);
2556 gnome_client_set_clone_command(client, 0, NULL);
2557 }
2558
2559 g_free(session_file);
2560
2561 return success;
2562}
2563
2564/*
2565 * Called when the session manager wants us to die. There isn't much to save
2566 * here since "save_yourself" has been emitted before (unless serious trouble
2567 * is happening).
2568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002570sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571{
2572 /* Don't write messages to the GUI anymore */
2573 full_screen = FALSE;
2574
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002575 vim_strncpy(IObuff, (char_u *)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002576 _("Vim: Received \"die\" request from session manager\n"),
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002577 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 preserve_exit();
2579}
2580
2581/*
2582 * Connect our signal handlers to be notified on session save and shutdown.
2583 */
2584 static void
2585setup_save_yourself(void)
2586{
2587 GnomeClient *client;
2588
2589 client = gnome_master_client();
2590
2591 if (client != NULL)
2592 {
2593 /* Must use the deprecated gtk_signal_connect() for compatibility
2594 * with GNOME 1. Arrgh, zombies! */
2595 gtk_signal_connect(GTK_OBJECT(client), "save_yourself",
2596 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL);
2597 gtk_signal_connect(GTK_OBJECT(client), "die",
2598 GTK_SIGNAL_FUNC(&sm_client_die), NULL);
2599 }
2600}
2601
2602#else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2603
2604# ifdef USE_XSMP
2605/*
2606 * GTK tells us that XSMP needs attention
2607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 static gboolean
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002609local_xsmp_handle_requests(
2610 GIOChannel *source UNUSED,
2611 GIOCondition condition,
2612 gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613{
2614 if (condition == G_IO_IN)
2615 {
2616 /* Do stuff; maybe close connection */
2617 if (xsmp_handle_requests() == FAIL)
2618 g_io_channel_unref((GIOChannel *)data);
2619 return TRUE;
2620 }
2621 /* Error */
2622 g_io_channel_unref((GIOChannel *)data);
2623 xsmp_close();
2624 return TRUE;
2625}
2626# endif /* USE_XSMP */
2627
2628/*
2629 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2630 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2631 */
2632 static void
2633setup_save_yourself(void)
2634{
2635 Atom *existing_atoms = NULL;
2636 int count = 0;
2637
Bram Moolenaar98921892016-02-23 17:14:37 +01002638# ifdef USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 if (xsmp_icefd != -1)
2640 {
2641 /*
2642 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2643 * set up GTK IO monitor
2644 */
2645 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd);
2646
2647 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP,
2648 local_xsmp_handle_requests, (gpointer)g_io);
Bram Moolenaar98921892016-02-23 17:14:37 +01002649 g_io_channel_unref(g_io);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 }
2651 else
Bram Moolenaar98921892016-02-23 17:14:37 +01002652# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 {
2654 /* Fall back to old method */
2655
2656 /* first get the existing value */
Bram Moolenaar98921892016-02-23 17:14:37 +01002657# if GTK_CHECK_VERSION(3,0,0)
2658 GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin);
2659
2660 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win),
2661 GDK_WINDOW_XID(mainwin_win),
2662 &existing_atoms, &count))
2663# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2665 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2666 &existing_atoms, &count))
Bram Moolenaar98921892016-02-23 17:14:37 +01002667# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 {
2669 Atom *new_atoms;
2670 Atom save_yourself_xatom;
2671 int i;
2672
2673 save_yourself_xatom = GET_X_ATOM(save_yourself_atom);
2674
2675 /* check if WM_SAVE_YOURSELF isn't there yet */
2676 for (i = 0; i < count; ++i)
2677 if (existing_atoms[i] == save_yourself_xatom)
2678 break;
2679
2680 if (i == count)
2681 {
2682 /* allocate an Atoms array which is one item longer */
2683 new_atoms = (Atom *)alloc((unsigned)((count + 1)
2684 * sizeof(Atom)));
2685 if (new_atoms != NULL)
2686 {
2687 memcpy(new_atoms, existing_atoms, count * sizeof(Atom));
2688 new_atoms[count] = save_yourself_xatom;
Bram Moolenaar98921892016-02-23 17:14:37 +01002689# if GTK_CHECK_VERSION(3,0,0)
2690 XSetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win),
2691 GDK_WINDOW_XID(mainwin_win),
2692# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 XSetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2694 GDK_WINDOW_XWINDOW(gui.mainwin->window),
Bram Moolenaar98921892016-02-23 17:14:37 +01002695# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 new_atoms, count + 1);
2697 vim_free(new_atoms);
2698 }
2699 }
2700 XFree(existing_atoms);
2701 }
2702 }
2703}
2704
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705/*
2706 * Installing a global event filter seems to be the only way to catch
2707 * client messages of type WM_PROTOCOLS without overriding GDK's own
2708 * client message event filter. Well, that's still better than trying
2709 * to guess what the GDK filter had done if it had been invoked instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 *
2711 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2712 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2713 * I have the nasty feeling modern session managers just don't send this
2714 * deprecated message anymore. Addition: confirmed by several people.
2715 *
2716 * The GNOME session support is much cooler anyway. Unlike this ugly
2717 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2718 * it should work with KDE as well.
2719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 static GdkFilterReturn
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002721global_event_filter(GdkXEvent *xev,
2722 GdkEvent *event UNUSED,
2723 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724{
2725 XEvent *xevent = (XEvent *)xev;
2726
2727 if (xevent != NULL
2728 && xevent->type == ClientMessage
2729 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002730 && (long_u)xevent->xclient.data.l[0]
2731 == GET_X_ATOM(save_yourself_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 {
2733 out_flush();
2734 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2735 /*
2736 * Set the window's WM_COMMAND property, to let the window manager
2737 * know we are done saving ourselves. We don't want to be
2738 * restarted, thus set argv to NULL.
2739 */
Bram Moolenaar98921892016-02-23 17:14:37 +01002740# if GTK_CHECK_VERSION(3,0,0)
2741 XSetCommand(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)),
2742 GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin)),
2743# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2745 GDK_WINDOW_XWINDOW(gui.mainwin->window),
Bram Moolenaar98921892016-02-23 17:14:37 +01002746# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 NULL, 0);
2748 return GDK_FILTER_REMOVE;
2749 }
2750
2751 return GDK_FILTER_CONTINUE;
2752}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753#endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2754
2755
2756/*
2757 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002760mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761{
2762/* If you get an error message here, you still need to unpack the runtime
2763 * archive! */
2764#ifdef magick
2765# undef magick
2766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 /* A bit hackish, but avoids casting later and allows optimization */
2768# define static static const
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769#define magick vim32x32
2770#include "../runtime/vim32x32.xpm"
2771#undef magick
2772#define magick vim16x16
2773#include "../runtime/vim16x16.xpm"
2774#undef magick
2775#define magick vim48x48
2776#include "../runtime/vim48x48.xpm"
2777#undef magick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778# undef static
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779
Bram Moolenaar98921892016-02-23 17:14:37 +01002780#if GTK_CHECK_VERSION(3,0,0)
2781 GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin);
2782#endif
2783
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 /* When started with "--echo-wid" argument, write window ID on stdout. */
2785 if (echo_wid_arg)
2786 {
Bram Moolenaar98921892016-02-23 17:14:37 +01002787#if GTK_CHECK_VERSION(3,0,0)
2788 printf("WID: %ld\n", (long)GDK_WINDOW_XID(mainwin_win));
2789#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 printf("WID: %ld\n", (long)GDK_WINDOW_XWINDOW(gui.mainwin->window));
Bram Moolenaar98921892016-02-23 17:14:37 +01002791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 fflush(stdout);
2793 }
2794
2795 if (vim_strchr(p_go, GO_ICON) != NULL)
2796 {
2797 /*
2798 * Add an icon to the main window. For fun and convenience of the user.
2799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 GList *icons = NULL;
2801
2802 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16));
2803 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32));
2804 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48));
2805
2806 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons);
2807
2808 g_list_foreach(icons, (GFunc)&g_object_unref, NULL);
2809 g_list_free(icons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 }
2811
2812#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
2813 /* Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 gdk_window_add_filter(NULL, &global_event_filter, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815#endif
2816 /* Setup to indicate to the window manager that we want to catch the
2817 * WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2818 * manager instead. */
2819#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2820 if (using_gnome)
2821#endif
2822 setup_save_yourself();
2823
2824#ifdef FEAT_CLIENTSERVER
2825 if (serverName == NULL && serverDelayedStartName != NULL)
2826 {
2827 /* This is a :gui command in a plain vim with no previous server */
Bram Moolenaar98921892016-02-23 17:14:37 +01002828# if GTK_CHECK_VERSION(3,0,0)
2829 commWindow = GDK_WINDOW_XID(mainwin_win);
2830
2831 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(mainwin_win),
2832 serverDelayedStartName);
2833# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 commWindow = GDK_WINDOW_XWINDOW(gui.mainwin->window);
2835
2836 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2837 serverDelayedStartName);
Bram Moolenaar98921892016-02-23 17:14:37 +01002838# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 }
2840 else
2841 {
2842 /*
2843 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2844 * have to change the "server" registration to that of the main window
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002845 * If we have not registered a name yet, remember the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 */
Bram Moolenaar98921892016-02-23 17:14:37 +01002847# if GTK_CHECK_VERSION(3,0,0)
2848 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(mainwin_win),
2849 GDK_WINDOW_XID(mainwin_win));
2850# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2852 GDK_WINDOW_XWINDOW(gui.mainwin->window));
Bram Moolenaar98921892016-02-23 17:14:37 +01002853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 }
2855 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK);
Bram Moolenaar98921892016-02-23 17:14:37 +01002856# if GTK_CHECK_VERSION(3,0,0)
2857 g_signal_connect(G_OBJECT(gui.mainwin), "property-notify-event",
2858 G_CALLBACK(property_event), NULL);
2859# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "property_notify_event",
2861 GTK_SIGNAL_FUNC(property_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01002862# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863#endif
2864}
2865
2866 static GdkCursor *
2867create_blank_pointer(void)
2868{
2869 GdkWindow *root_window = NULL;
Bram Moolenaar98921892016-02-23 17:14:37 +01002870#if GTK_CHECK_VERSION(3,0,0)
2871 GdkPixbuf *blank_mask;
2872#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 GdkPixmap *blank_mask;
Bram Moolenaar98921892016-02-23 17:14:37 +01002874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 GdkCursor *cursor;
Bram Moolenaar36edf062016-07-21 22:10:12 +02002876#if GTK_CHECK_VERSION(3,0,0)
2877 GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
2878#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 GdkColor color = { 0, 0, 0, 0 };
2880 char blank_data[] = { 0x0 };
Bram Moolenaar98921892016-02-23 17:14:37 +01002881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002883#if GTK_CHECK_VERSION(3,12,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01002884 {
2885 GdkWindow * const win = gtk_widget_get_window(gui.mainwin);
2886 GdkScreen * const scrn = gdk_window_get_screen(win);
2887 root_window = gdk_screen_get_root_window(scrn);
2888 }
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002889#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 root_window = gtk_widget_get_root_window(gui.mainwin);
2891#endif
2892
2893 /* Create a pseudo blank pointer, which is in fact one pixel by one pixel
2894 * in size. */
Bram Moolenaar98921892016-02-23 17:14:37 +01002895#if GTK_CHECK_VERSION(3,0,0)
2896 {
2897 cairo_surface_t *surf;
2898 cairo_t *cr;
2899
2900 surf = cairo_image_surface_create(CAIRO_FORMAT_A1, 1, 1);
2901 cr = cairo_create(surf);
2902
Bram Moolenaar36edf062016-07-21 22:10:12 +02002903 cairo_set_source_rgba(cr,
2904 color.red,
2905 color.green,
2906 color.blue,
2907 color.alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01002908 cairo_rectangle(cr, 0, 0, 1, 1);
2909 cairo_fill(cr);
2910 cairo_destroy(cr);
2911
2912 blank_mask = gdk_pixbuf_get_from_surface(surf, 0, 0, 1, 1);
2913 cairo_surface_destroy(surf);
2914
2915 cursor = gdk_cursor_new_from_pixbuf(gdk_window_get_display(root_window),
2916 blank_mask, 0, 0);
2917 g_object_unref(blank_mask);
2918 }
2919#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1);
2921 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask,
2922 &color, &color, 0, 0);
2923 gdk_bitmap_unref(blank_mask);
Bram Moolenaar98921892016-02-23 17:14:37 +01002924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925
2926 return cursor;
2927}
2928
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 static void
2930mainwin_screen_changed_cb(GtkWidget *widget,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002931 GdkScreen *previous_screen UNUSED,
2932 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933{
2934 if (!gtk_widget_has_screen(widget))
2935 return;
2936
2937 /*
Bram Moolenaar49325942007-05-10 19:19:59 +00002938 * Recreate the invisible mouse cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 */
2940 if (gui.blank_pointer != NULL)
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002941#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01002942 g_object_unref(G_OBJECT(gui.blank_pointer));
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002943#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 gdk_cursor_unref(gui.blank_pointer);
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
2947 gui.blank_pointer = create_blank_pointer();
2948
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002949#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01002950 if (gui.pointer_hidden && gtk_widget_get_window(gui.drawarea) != NULL)
2951 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
2952 gui.blank_pointer);
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002953#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 if (gui.pointer_hidden && gui.drawarea->window != NULL)
2955 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957
2958 /*
2959 * Create a new PangoContext for this screen, and initialize it
2960 * with the current font if necessary.
2961 */
2962 if (gui.text_context != NULL)
2963 g_object_unref(gui.text_context);
2964
2965 gui.text_context = gtk_widget_create_pango_context(widget);
2966 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
2967
2968 if (gui.norm_font != NULL)
2969 {
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002970 gui_mch_init_font(p_guifont, FALSE);
Bram Moolenaar04a9d452006-03-27 21:03:26 +00002971 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 }
2973}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974
2975/*
2976 * After the drawing area comes up, we calculate all colors and create the
2977 * dummy blank cursor.
2978 *
2979 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2980 * fact that the main VIM engine doesn't take them into account anywhere.
2981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002983drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984{
2985 GtkWidget *sbar;
Bram Moolenaar98921892016-02-23 17:14:37 +01002986#if GTK_CHECK_VERSION(3,0,0)
2987 GtkAllocation allocation;
2988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989
2990#ifdef FEAT_XIM
2991 xim_init();
2992#endif
2993 gui_mch_new_colors();
Bram Moolenaar98921892016-02-23 17:14:37 +01002994#if GTK_CHECK_VERSION(3,0,0)
2995 gui.surface = gdk_window_create_similar_surface(
2996 gtk_widget_get_window(widget),
2997 CAIRO_CONTENT_COLOR_ALPHA,
2998 gtk_widget_get_allocated_width(widget),
2999 gtk_widget_get_allocated_height(widget));
3000#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 gui.text_gc = gdk_gc_new(gui.drawarea->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01003002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003
3004 gui.blank_pointer = create_blank_pointer();
3005 if (gui.pointer_hidden)
Bram Moolenaar98921892016-02-23 17:14:37 +01003006#if GTK_CHECK_VERSION(3,0,0)
3007 gdk_window_set_cursor(gtk_widget_get_window(widget), gui.blank_pointer);
3008#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 gdk_window_set_cursor(widget->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01003010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
3012 /* get the actual size of the scrollbars, if they are realized */
3013 sbar = firstwin->w_scrollbars[SBAR_LEFT].id;
3014 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT]
3015 && firstwin->w_scrollbars[SBAR_RIGHT].id))
3016 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id;
Bram Moolenaar98921892016-02-23 17:14:37 +01003017#if GTK_CHECK_VERSION(3,0,0)
3018 gtk_widget_get_allocation(sbar, &allocation);
3019 if (sbar && gtk_widget_get_realized(sbar) && allocation.width)
3020 gui.scrollbar_width = allocation.width;
3021#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.width)
3023 gui.scrollbar_width = sbar->allocation.width;
Bram Moolenaar98921892016-02-23 17:14:37 +01003024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025
3026 sbar = gui.bottom_sbar.id;
Bram Moolenaar98921892016-02-23 17:14:37 +01003027#if GTK_CHECK_VERSION(3,0,0)
3028 if (sbar && gtk_widget_get_realized(sbar) && allocation.height)
3029 gui.scrollbar_height = allocation.height;
3030#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.height)
3032 gui.scrollbar_height = sbar->allocation.height;
Bram Moolenaar98921892016-02-23 17:14:37 +01003033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034}
3035
3036/*
3037 * Properly clean up on shutdown.
3038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003040drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041{
3042 /* Don't write messages to the GUI anymore */
3043 full_screen = FALSE;
3044
3045#ifdef FEAT_XIM
3046 im_shutdown();
3047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 if (gui.ascii_glyphs != NULL)
3049 {
3050 pango_glyph_string_free(gui.ascii_glyphs);
3051 gui.ascii_glyphs = NULL;
3052 }
3053 if (gui.ascii_font != NULL)
3054 {
3055 g_object_unref(gui.ascii_font);
3056 gui.ascii_font = NULL;
3057 }
3058 g_object_unref(gui.text_context);
3059 gui.text_context = NULL;
3060
Bram Moolenaar98921892016-02-23 17:14:37 +01003061#if GTK_CHECK_VERSION(3,0,0)
3062 if (gui.surface != NULL)
3063 {
3064 cairo_surface_destroy(gui.surface);
3065 gui.surface = NULL;
3066 }
3067#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 g_object_unref(gui.text_gc);
3069 gui.text_gc = NULL;
Bram Moolenaar98921892016-02-23 17:14:37 +01003070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071
Bram Moolenaar98921892016-02-23 17:14:37 +01003072#if GTK_CHECK_VERSION(3,0,0)
3073 g_object_unref(G_OBJECT(gui.blank_pointer));
3074#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 gdk_cursor_unref(gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01003076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 gui.blank_pointer = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078}
3079
Bram Moolenaara859f042016-11-17 19:11:55 +01003080#if GTK_CHECK_VERSION(3,22,2)
3081 static void
3082drawarea_style_updated_cb(GtkWidget *widget UNUSED,
3083 gpointer data UNUSED)
3084#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003086drawarea_style_set_cb(GtkWidget *widget UNUSED,
3087 GtkStyle *previous_style UNUSED,
3088 gpointer data UNUSED)
Bram Moolenaara859f042016-11-17 19:11:55 +01003089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
3091 gui_mch_new_colors();
3092}
3093
Bram Moolenaar98921892016-02-23 17:14:37 +01003094#if GTK_CHECK_VERSION(3,0,0)
3095 static gboolean
3096drawarea_configure_event_cb(GtkWidget *widget,
3097 GdkEventConfigure *event,
3098 gpointer data UNUSED)
3099{
3100 static int cur_width = 0;
3101 static int cur_height = 0;
3102
3103 g_return_val_if_fail(event
3104 && event->width >= 1 && event->height >= 1, TRUE);
3105
Bram Moolenaar182707a2016-11-21 20:55:58 +01003106# if GTK_CHECK_VERSION(3,22,2) && !GTK_CHECK_VERSION(3,22,4)
Bram Moolenaara859f042016-11-17 19:11:55 +01003107 /* As of 3.22.2, GdkWindows have started distributing configure events to
3108 * their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0).
3109 *
3110 * As can be seen from the implementation of move_native_children() and
3111 * configure_native_child() in gdkwindow.c, those functions actually
3112 * propagate configure events to every child, failing to distinguish
3113 * "native" one from non-native one.
3114 *
3115 * Naturally, configure events propagated to here like that are fallacious
3116 * and, as a matter of fact, they trigger a geometric collapse of
3117 * gui.drawarea in fullscreen and miximized modes.
3118 *
3119 * To filter out such nuisance events, we are making use of the fact that
3120 * the field send_event of such GdkEventConfigures is set to FALSE in
3121 * configure_native_child().
3122 *
3123 * Obviously, this is a terrible hack making GVim depend on GTK's
3124 * implementation details. Therefore, watch out any relevant internal
3125 * changes happening in GTK in the feature (sigh).
3126 */
Bram Moolenaar182707a2016-11-21 20:55:58 +01003127 /* Follow-up
3128 * After a few weeks later, the GdkWindow change mentioned above was
3129 * reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155).
3130 * The corresponding official release is 3.22.4. */
Bram Moolenaara859f042016-11-17 19:11:55 +01003131 if (event->send_event == FALSE)
3132 return TRUE;
3133# endif
3134
Bram Moolenaar98921892016-02-23 17:14:37 +01003135 if (event->width == cur_width && event->height == cur_height)
3136 return TRUE;
3137
3138 cur_width = event->width;
3139 cur_height = event->height;
3140
3141 if (gui.surface != NULL)
3142 cairo_surface_destroy(gui.surface);
3143
3144 gui.surface = gdk_window_create_similar_surface(
3145 gtk_widget_get_window(widget),
3146 CAIRO_CONTENT_COLOR_ALPHA,
3147 event->width, event->height);
3148
3149 gtk_widget_queue_draw(widget);
3150
3151 return TRUE;
3152}
3153#endif
3154
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155/*
3156 * Callback routine for the "delete_event" signal on the toplevel window.
3157 * Tries to vim gracefully, or refuses to exit with changed buffers.
3158 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003160delete_event_cb(GtkWidget *widget UNUSED,
3161 GdkEventAny *event UNUSED,
3162 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163{
3164 gui_shell_closed();
3165 return TRUE;
3166}
3167
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003168#if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
3169 static int
3170get_item_dimensions(GtkWidget *widget, GtkOrientation orientation)
3171{
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003172# ifdef FEAT_GUI_GNOME
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003173 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL;
3174
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003175 if (using_gnome && widget != NULL)
3176 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003177 GtkWidget *parent;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003178 BonoboDockItem *dockitem;
3179
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003180 parent = gtk_widget_get_parent(widget);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003181 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM)
3182 {
3183 /* Only menu & toolbar are dock items. Could tabline be?
3184 * Seem to be only the 2 defined in GNOME */
3185 widget = parent;
3186 dockitem = BONOBO_DOCK_ITEM(widget);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003187
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003188 if (dockitem == NULL || dockitem->is_floating)
3189 return 0;
3190 item_orientation = bonobo_dock_item_get_orientation(dockitem);
3191 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003192 }
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003193# else
3194# define item_orientation GTK_ORIENTATION_HORIZONTAL
Bram Moolenaar98921892016-02-23 17:14:37 +01003195# endif
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003196
Bram Moolenaar98921892016-02-23 17:14:37 +01003197# if GTK_CHECK_VERSION(3,0,0)
3198 if (widget != NULL
3199 && item_orientation == orientation
3200 && gtk_widget_get_realized(widget)
3201 && gtk_widget_get_visible(widget))
3202# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003203 if (widget != NULL
3204 && item_orientation == orientation
3205 && GTK_WIDGET_REALIZED(widget)
3206 && GTK_WIDGET_VISIBLE(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01003207# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003208 {
Bram Moolenaar98921892016-02-23 17:14:37 +01003209# if GTK_CHECK_VERSION(3,0,0)
3210 GtkAllocation allocation;
3211
3212 gtk_widget_get_allocation(widget, &allocation);
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003213 return allocation.height;
Bram Moolenaar98921892016-02-23 17:14:37 +01003214# else
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003215# ifdef FEAT_GUI_GNOME
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003216 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3217 return widget->allocation.height;
3218 else
3219 return widget->allocation.width;
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003220# else
3221 return widget->allocation.height;
3222# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01003223# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003224 }
3225 return 0;
3226}
3227#endif
3228
3229 static int
3230get_menu_tool_width(void)
3231{
3232 int width = 0;
3233
3234#ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */
3235# ifdef FEAT_MENU
3236 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL);
3237# endif
3238# ifdef FEAT_TOOLBAR
3239 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL);
3240# endif
3241# ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003242 if (gui.tabline != NULL)
3243 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003244# endif
3245#endif
3246
3247 return width;
3248}
3249
3250 static int
3251get_menu_tool_height(void)
3252{
3253 int height = 0;
3254
3255#ifdef FEAT_MENU
3256 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL);
3257#endif
3258#ifdef FEAT_TOOLBAR
3259 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL);
3260#endif
3261#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003262 if (gui.tabline != NULL)
3263 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003264#endif
3265
3266 return height;
3267}
3268
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003269/* This controls whether we can set the real window hints at
3270 * start-up when in a GtkPlug.
3271 * 0 = normal processing (default)
3272 * 1 = init. hints set, no-one's tried to reset since last check
3273 * 2 = init. hints set, attempt made to change hints
3274 */
3275static int init_window_hints_state = 0;
3276
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003277 static void
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003278update_window_manager_hints(int force_width, int force_height)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003279{
3280 static int old_width = 0;
3281 static int old_height = 0;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003282 static int old_min_width = 0;
3283 static int old_min_height = 0;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003284 static int old_char_width = 0;
3285 static int old_char_height = 0;
3286
3287 int width;
3288 int height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003289 int min_width;
3290 int min_height;
3291
3292 /* At start-up, don't try to set the hints until the initial
3293 * values have been used (those that dictate our initial size)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003294 * Let forced (i.e., correct) values through always.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003295 */
3296 if (!(force_width && force_height) && init_window_hints_state > 0)
3297 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003298 /* Don't do it! */
3299 init_window_hints_state = 2;
3300 return;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003301 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003302
3303 /* This also needs to be done when the main window isn't there yet,
3304 * otherwise the hints don't work. */
3305 width = gui_get_base_width();
3306 height = gui_get_base_height();
3307# ifdef FEAT_MENU
3308 height += tabline_height() * gui.char_height;
3309# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003310 width += get_menu_tool_width();
3311 height += get_menu_tool_height();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003312
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003313 /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
Bram Moolenaar49325942007-05-10 19:19:59 +00003314 * their actual widget size. When we set our size ourselves (e.g.,
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003315 * 'set columns=' or init. -geom) we briefly set the min. to the size
3316 * we wish to be instead of the legitimate minimum so that we actually
3317 * resize correctly.
3318 */
3319 if (force_width && force_height)
3320 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003321 min_width = force_width;
3322 min_height = force_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003323 }
3324 else
3325 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003326 min_width = width + MIN_COLUMNS * gui.char_width;
3327 min_height = height + MIN_LINES * gui.char_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003328 }
3329
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003330 /* Avoid an expose event when the size didn't change. */
3331 if (width != old_width
3332 || height != old_height
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003333 || min_width != old_min_width
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003334 || min_height != old_min_height
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003335 || gui.char_width != old_char_width
3336 || gui.char_height != old_char_height)
3337 {
3338 GdkGeometry geometry;
3339 GdkWindowHints geometry_mask;
3340
3341 geometry.width_inc = gui.char_width;
3342 geometry.height_inc = gui.char_height;
3343 geometry.base_width = width;
3344 geometry.base_height = height;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003345 geometry.min_width = min_width;
3346 geometry.min_height = min_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003347 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC
3348 |GDK_HINT_MIN_SIZE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003349 /* Using gui.formwin as geometry widget doesn't work as expected
3350 * with GTK+ 2 -- dunno why. Presumably all the resizing hacks
3351 * in Vim confuse GTK+. */
3352 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin,
3353 &geometry, geometry_mask);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003354 old_width = width;
3355 old_height = height;
3356 old_min_width = min_width;
3357 old_min_height = min_height;
3358 old_char_width = gui.char_width;
3359 old_char_height = gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003360 }
3361}
3362
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363#ifdef FEAT_TOOLBAR
3364
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365/*
3366 * This extra effort wouldn't be necessary if we only used stock icons in the
3367 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
3368 * shouldn't be treated differently, thus we do need this.
3369 */
3370 static void
3371icon_size_changed_foreach(GtkWidget *widget, gpointer user_data)
3372{
3373 if (GTK_IS_IMAGE(widget))
3374 {
3375 GtkImage *image = (GtkImage *)widget;
3376
Bram Moolenaar98921892016-02-23 17:14:37 +01003377# if GTK_CHECK_VERSION(3,10,0)
3378 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_NAME)
3379 {
3380 const GtkIconSize icon_size = GPOINTER_TO_INT(user_data);
3381 const gchar *icon_name;
3382
3383 gtk_image_get_icon_name(image, &icon_name, NULL);
3384
3385 gtk_image_set_from_icon_name(image, icon_name, icon_size);
3386 }
3387# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 /* User-defined icons are stored in a GtkIconSet */
3389 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET)
3390 {
3391 GtkIconSet *icon_set;
3392 GtkIconSize icon_size;
3393
3394 gtk_image_get_icon_set(image, &icon_set, &icon_size);
3395 icon_size = (GtkIconSize)(long)user_data;
3396
3397 gtk_icon_set_ref(icon_set);
3398 gtk_image_set_from_icon_set(image, icon_set, icon_size);
3399 gtk_icon_set_unref(icon_set);
3400 }
Bram Moolenaar98921892016-02-23 17:14:37 +01003401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 }
3403 else if (GTK_IS_CONTAINER(widget))
3404 {
3405 gtk_container_foreach((GtkContainer *)widget,
3406 &icon_size_changed_foreach,
3407 user_data);
3408 }
3409}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410
3411 static void
3412set_toolbar_style(GtkToolbar *toolbar)
3413{
3414 GtkToolbarStyle style;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 GtkIconSize size;
3416 GtkIconSize oldsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3419 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3420 style = GTK_TOOLBAR_BOTH_HORIZ;
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003421 else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 == (TOOLBAR_TEXT | TOOLBAR_ICONS))
3423 style = GTK_TOOLBAR_BOTH;
3424 else if (toolbar_flags & TOOLBAR_TEXT)
3425 style = GTK_TOOLBAR_TEXT;
3426 else
3427 style = GTK_TOOLBAR_ICONS;
3428
3429 gtk_toolbar_set_style(toolbar, style);
Bram Moolenaar98921892016-02-23 17:14:37 +01003430# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003432# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 switch (tbis_flags)
3435 {
3436 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break;
3437 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break;
3438 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break;
3439 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
Bram Moolenaarbeb003b2016-03-08 22:47:17 +01003440 case TBIS_HUGE: size = GTK_ICON_SIZE_DND; break;
3441 case TBIS_GIANT: size = GTK_ICON_SIZE_DIALOG; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 default: size = GTK_ICON_SIZE_INVALID; break;
3443 }
3444 oldsize = gtk_toolbar_get_icon_size(toolbar);
3445
3446 if (size == GTK_ICON_SIZE_INVALID)
3447 {
3448 /* Let global user preferences decide the icon size. */
3449 gtk_toolbar_unset_icon_size(toolbar);
3450 size = gtk_toolbar_get_icon_size(toolbar);
3451 }
3452 if (size != oldsize)
3453 {
3454 gtk_container_foreach(GTK_CONTAINER(toolbar),
3455 &icon_size_changed_foreach,
3456 GINT_TO_POINTER((int)size));
3457 }
3458 gtk_toolbar_set_icon_size(toolbar, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459}
3460
3461#endif /* FEAT_TOOLBAR */
3462
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003463#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3464static int ignore_tabline_evt = FALSE;
Bram Moolenaara5621492006-02-25 21:55:24 +00003465static GtkWidget *tabline_menu;
Bram Moolenaar98921892016-02-23 17:14:37 +01003466# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003467static GtkTooltips *tabline_tooltip;
Bram Moolenaar98921892016-02-23 17:14:37 +01003468# endif
Bram Moolenaara5621492006-02-25 21:55:24 +00003469static int clicked_page; /* page clicked in tab line */
3470
3471/*
3472 * Handle selecting an item in the tab line popup menu.
3473 */
Bram Moolenaara5621492006-02-25 21:55:24 +00003474 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003475tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data)
Bram Moolenaara5621492006-02-25 21:55:24 +00003476{
Bram Moolenaara5621492006-02-25 21:55:24 +00003477 /* Add the string cmd into input buffer */
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003478 send_tabline_menu_event(clicked_page, (int)(long)user_data);
Bram Moolenaara5621492006-02-25 21:55:24 +00003479}
3480
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003481 static void
3482add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp)
3483{
3484 GtkWidget *item;
3485 char_u *utf_text;
3486
3487 utf_text = CONVERT_TO_UTF8(text);
3488 item = gtk_menu_item_new_with_label((const char *)utf_text);
3489 gtk_widget_show(item);
3490 CONVERT_TO_UTF8_FREE(utf_text);
3491
3492 gtk_container_add(GTK_CONTAINER(menu), item);
Bram Moolenaar98921892016-02-23 17:14:37 +01003493# if GTK_CHECK_VERSION(3,0,0)
3494 g_signal_connect(G_OBJECT(item), "activate",
3495 G_CALLBACK(tabline_menu_handler),
3496 GINT_TO_POINTER(resp));
3497# else
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003498 gtk_signal_connect(GTK_OBJECT(item), "activate",
3499 GTK_SIGNAL_FUNC(tabline_menu_handler),
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003500 (gpointer)(long)resp);
Bram Moolenaar98921892016-02-23 17:14:37 +01003501# endif
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003502}
3503
Bram Moolenaara5621492006-02-25 21:55:24 +00003504/*
Bram Moolenaara5621492006-02-25 21:55:24 +00003505 * Create a menu for the tab line.
3506 */
3507 static GtkWidget *
3508create_tabline_menu(void)
3509{
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003510 GtkWidget *menu;
Bram Moolenaara5621492006-02-25 21:55:24 +00003511
3512 menu = gtk_menu_new();
Bram Moolenaar7098ee52015-06-09 19:14:24 +02003513 if (first_tabpage->tp_next != NULL)
3514 add_tabline_menu_item(menu, (char_u *)_("Close tab"),
3515 TABLINE_MENU_CLOSE);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003516 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW);
3517 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN);
Bram Moolenaara5621492006-02-25 21:55:24 +00003518
3519 return menu;
3520}
3521
3522 static gboolean
3523on_tabline_menu(GtkWidget *widget, GdkEvent *event)
3524{
3525 /* Was this button press event ? */
3526 if (event->type == GDK_BUTTON_PRESS)
3527 {
3528 GdkEventButton *bevent = (GdkEventButton *)event;
3529 int x = bevent->x;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003530 int y = bevent->y;
3531 GtkWidget *tabwidget;
3532 GdkWindow *tabwin;
Bram Moolenaara5621492006-02-25 21:55:24 +00003533
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003534 /* When ignoring events return TRUE so that the selected page doesn't
3535 * change. */
3536 if (hold_gui_events
3537# ifdef FEAT_CMDWIN
3538 || cmdwin_type != 0
3539# endif
3540 )
3541 return TRUE;
3542
Bram Moolenaar98921892016-02-23 17:14:37 +01003543# if GTK_CHECK_VERSION(3,0,0)
3544 tabwin = gui_gtk_window_at_position(gui.mainwin, &x, &y);
3545# else
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003546 tabwin = gdk_window_at_pointer(&x, &y);
Bram Moolenaar98921892016-02-23 17:14:37 +01003547# endif
3548
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003549 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget);
Bram Moolenaar98921892016-02-23 17:14:37 +01003550# if GTK_CHECK_VERSION(3,0,0)
3551 clicked_page = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(tabwidget),
3552 "tab_num"));
3553# else
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003554 clicked_page = (int)(long)gtk_object_get_user_data(
3555 GTK_OBJECT(tabwidget));
Bram Moolenaar98921892016-02-23 17:14:37 +01003556# endif
Bram Moolenaara5621492006-02-25 21:55:24 +00003557
3558 /* If the event was generated for 3rd button popup the menu. */
3559 if (bevent->button == 3)
3560 {
Bram Moolenaara859f042016-11-17 19:11:55 +01003561# if GTK_CHECK_VERSION(3,22,2)
3562 gtk_menu_popup_at_pointer(GTK_MENU(widget), event);
3563# else
Bram Moolenaara5621492006-02-25 21:55:24 +00003564 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
3565 bevent->button, bevent->time);
Bram Moolenaara859f042016-11-17 19:11:55 +01003566# endif
Bram Moolenaara5621492006-02-25 21:55:24 +00003567 /* We handled the event. */
3568 return TRUE;
3569 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003570 else if (bevent->button == 1)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003571 {
Bram Moolenaar96351572006-05-05 21:16:59 +00003572 if (clicked_page == 0)
3573 {
Bram Moolenaar07354542007-09-15 12:07:46 +00003574 /* Click after all tabs moves to next tab page. When "x" is
3575 * small guess it's the left button. */
Bram Moolenaara3f41662010-07-11 19:01:06 +02003576 send_tabline_event(x < 50 ? -1 : 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00003577 }
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003578 }
Bram Moolenaara5621492006-02-25 21:55:24 +00003579 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003580
Bram Moolenaara5621492006-02-25 21:55:24 +00003581 /* We didn't handle the event. */
3582 return FALSE;
3583}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003584
3585/*
3586 * Handle selecting one of the tabs.
3587 */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003588 static void
3589on_select_tab(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003590 GtkNotebook *notebook UNUSED,
Bram Moolenaar98921892016-02-23 17:14:37 +01003591# if GTK_CHECK_VERSION(3,0,0)
3592 gpointer *page UNUSED,
3593# else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003594 GtkNotebookPage *page UNUSED,
Bram Moolenaar98921892016-02-23 17:14:37 +01003595# endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00003596 gint idx,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003597 gpointer data UNUSED)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003598{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003599 if (!ignore_tabline_evt)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003600 {
Bram Moolenaara3f41662010-07-11 19:01:06 +02003601 send_tabline_event(idx + 1);
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003602 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003603}
3604
3605/*
3606 * Show or hide the tabline.
3607 */
3608 void
3609gui_mch_show_tabline(int showit)
3610{
3611 if (gui.tabline == NULL)
3612 return;
3613
3614 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)))
3615 {
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003616 /* Note: this may cause a resize event */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003617 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003618 update_window_manager_hints(0, 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00003619 if (showit)
Bram Moolenaar98921892016-02-23 17:14:37 +01003620# if GTK_CHECK_VERSION(3,0,0)
3621 gtk_widget_set_can_focus(GTK_WIDGET(gui.tabline), FALSE);
3622# else
Bram Moolenaar96351572006-05-05 21:16:59 +00003623 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui.tabline), GTK_CAN_FOCUS);
Bram Moolenaar98921892016-02-23 17:14:37 +01003624# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003625 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003626
3627 gui_mch_update();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003628}
3629
3630/*
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003631 * Return TRUE when tabline is displayed.
3632 */
3633 int
3634gui_mch_showing_tabline(void)
3635{
3636 return gui.tabline != NULL
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003637 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline));
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003638}
3639
3640/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003641 * Update the labels of the tabline.
3642 */
3643 void
3644gui_mch_update_tabline(void)
3645{
3646 GtkWidget *page;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003647 GtkWidget *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003648 GtkWidget *label;
3649 tabpage_T *tp;
3650 int nr = 0;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003651 int tab_num;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003652 int curtabidx = 0;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003653 char_u *labeltext;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003654
3655 if (gui.tabline == NULL)
3656 return;
3657
3658 ignore_tabline_evt = TRUE;
3659
3660 /* Add a label for each tab page. They all contain the same text area. */
3661 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3662 {
3663 if (tp == curtab)
3664 curtabidx = nr;
3665
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003666 tab_num = nr + 1;
3667
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003668 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr);
3669 if (page == NULL)
3670 {
3671 /* Add notebook page */
Bram Moolenaar98921892016-02-23 17:14:37 +01003672# if GTK_CHECK_VERSION(3,2,0)
3673 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3674 gtk_box_set_homogeneous(GTK_BOX(page), FALSE);
3675# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003676 page = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003677# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003678 gtk_widget_show(page);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003679 event_box = gtk_event_box_new();
3680 gtk_widget_show(event_box);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003681 label = gtk_label_new("-Empty-");
Bram Moolenaar98921892016-02-23 17:14:37 +01003682# if !GTK_CHECK_VERSION(3,14,0)
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003683 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01003684# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003685 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003686 gtk_widget_show(label);
3687 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline),
3688 page,
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003689 event_box,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003690 nr++);
3691 }
3692
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003693 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page);
Bram Moolenaar98921892016-02-23 17:14:37 +01003694# if GTK_CHECK_VERSION(3,0,0)
3695 g_object_set_data(G_OBJECT(event_box), "tab_num",
3696 GINT_TO_POINTER(tab_num));
3697# else
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003698 gtk_object_set_user_data(GTK_OBJECT(event_box),
3699 (gpointer)(long)tab_num);
Bram Moolenaar98921892016-02-23 17:14:37 +01003700# endif
3701# if GTK_CHECK_VERSION(3,0,0)
3702 label = gtk_bin_get_child(GTK_BIN(event_box));
3703# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003704 label = GTK_BIN(event_box)->child;
Bram Moolenaar98921892016-02-23 17:14:37 +01003705# endif
Bram Moolenaar57657d82006-04-21 22:12:41 +00003706 get_tabline_label(tp, FALSE);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003707 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003708 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003709 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003710
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003711 get_tabline_label(tp, TRUE);
3712 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar98921892016-02-23 17:14:37 +01003713# if GTK_CHECK_VERSION(3,0,0)
3714 gtk_widget_set_tooltip_text(event_box, (const gchar *)labeltext);
3715# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003716 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box,
3717 (const char *)labeltext, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01003718# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003719 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003720 }
3721
3722 /* Remove any old labels. */
3723 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL)
3724 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr);
3725
Bram Moolenaar98921892016-02-23 17:14:37 +01003726# if GTK_CHECK_VERSION(3,0,0)
3727 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
3728 gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
3729# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003730 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003731 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
Bram Moolenaar98921892016-02-23 17:14:37 +01003732# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003733
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003734 /* Make sure everything is in place before drawing text. */
3735 gui_mch_update();
3736
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003737 ignore_tabline_evt = FALSE;
3738}
3739
3740/*
3741 * Set the current tab to "nr". First tab is 1.
3742 */
3743 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003744gui_mch_set_curtab(int nr)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003745{
3746 if (gui.tabline == NULL)
3747 return;
3748
3749 ignore_tabline_evt = TRUE;
Bram Moolenaar98921892016-02-23 17:14:37 +01003750# if GTK_CHECK_VERSION(3,0,0)
3751 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
3752 gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
3753# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003754 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003755 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
Bram Moolenaar98921892016-02-23 17:14:37 +01003756# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003757 ignore_tabline_evt = FALSE;
3758}
3759
3760#endif /* FEAT_GUI_TABLINE */
3761
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762/*
Bram Moolenaara76638f2010-06-05 12:49:46 +02003763 * Add selection targets for PRIMARY and CLIPBOARD selections.
3764 */
3765 void
3766gui_gtk_set_selection_targets(void)
3767{
3768 int i, j = 0;
3769 int n_targets = N_SELECTION_TARGETS;
3770 GtkTargetEntry targets[N_SELECTION_TARGETS];
3771
3772 for (i = 0; i < (int)N_SELECTION_TARGETS; ++i)
3773 {
Bram Moolenaar29695702012-05-18 17:03:18 +02003774 /* OpenOffice tries to use TARGET_HTML and fails when we don't
Bram Moolenaara76638f2010-06-05 12:49:46 +02003775 * return something, instead of trying another target. Therefore only
3776 * offer TARGET_HTML when it works. */
3777 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3778 n_targets--;
3779 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003780 targets[j++] = selection_targets[i];
3781 }
3782
3783 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)GDK_SELECTION_PRIMARY);
3784 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)clip_plus.gtk_sel_atom);
3785 gtk_selection_add_targets(gui.drawarea,
3786 (GdkAtom)GDK_SELECTION_PRIMARY,
3787 targets, n_targets);
3788 gtk_selection_add_targets(gui.drawarea,
3789 (GdkAtom)clip_plus.gtk_sel_atom,
3790 targets, n_targets);
3791}
3792
3793/*
3794 * Set up for receiving DND items.
3795 */
3796 void
3797gui_gtk_set_dnd_targets(void)
3798{
3799 int i, j = 0;
3800 int n_targets = N_DND_TARGETS;
3801 GtkTargetEntry targets[N_DND_TARGETS];
3802
3803 for (i = 0; i < (int)N_DND_TARGETS; ++i)
3804 {
Bram Moolenaarb931d742011-10-26 11:36:25 +02003805 if (!clip_html && dnd_targets[i].info == TARGET_HTML)
Bram Moolenaara76638f2010-06-05 12:49:46 +02003806 n_targets--;
3807 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003808 targets[j++] = dnd_targets[i];
3809 }
3810
3811 gtk_drag_dest_unset(gui.drawarea);
3812 gtk_drag_dest_set(gui.drawarea,
3813 GTK_DEST_DEFAULT_ALL,
3814 targets, n_targets,
Bram Moolenaarba7cc9f2011-02-25 17:10:27 +01003815 GDK_ACTION_COPY | GDK_ACTION_MOVE);
Bram Moolenaara76638f2010-06-05 12:49:46 +02003816}
3817
3818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3820 * Returns OK for success, FAIL when the GUI can't be started.
3821 */
3822 int
3823gui_mch_init(void)
3824{
3825 GtkWidget *vbox;
3826
3827#ifdef FEAT_GUI_GNOME
3828 /* Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3829 * exits on failure, but that's a non-issue because we already called
3830 * gtk_init_check() in gui_mch_init_check(). */
3831 if (using_gnome)
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003832 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT,
3834 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL);
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003835# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
Bram Moolenaar1ff32c52014-04-29 15:11:43 +02003836 {
3837 char *p = setlocale(LC_NUMERIC, NULL);
3838
3839 /* Make sure strtod() uses a decimal point, not a comma. Gnome
3840 * init may change it. */
3841 if (p == NULL || strcmp(p, "C") != 0)
3842 setlocale(LC_NUMERIC, "C");
3843 }
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003844# endif
3845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846#endif
3847 vim_free(gui_argv);
3848 gui_argv = NULL;
3849
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003850#if GLIB_CHECK_VERSION(2,1,3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 /* Set the human-readable application name */
3852 g_set_application_name("Vim");
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 /*
3855 * Force UTF-8 output no matter what the value of 'encoding' is.
3856 * did_set_string_option() in option.c prohibits changing 'termencoding'
3857 * to something else than UTF-8 if the GUI is in use.
3858 */
3859 set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0);
3860
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003861#ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 gui_gtk_register_stock_icons();
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 /* FIXME: Need to install the classic icons and a gtkrc.classic file.
3865 * The hard part is deciding install locations and the Makefile magic. */
Bram Moolenaar98921892016-02-23 17:14:37 +01003866#if !GTK_CHECK_VERSION(3,0,0)
3867# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 gtk_rc_parse("gtkrc");
Bram Moolenaar98921892016-02-23 17:14:37 +01003869# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870#endif
3871
3872 /* Initialize values */
3873 gui.border_width = 2;
3874 gui.scrollbar_width = SB_DEFAULT_WIDTH;
3875 gui.scrollbar_height = SB_DEFAULT_WIDTH;
Bram Moolenaar36edf062016-07-21 22:10:12 +02003876#if GTK_CHECK_VERSION(3,0,0)
3877 gui.fgcolor = g_new(GdkRGBA, 1);
3878 gui.bgcolor = g_new(GdkRGBA, 1);
3879 gui.spcolor = g_new(GdkRGBA, 1);
3880#else
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003881 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 gui.fgcolor = g_new0(GdkColor, 1);
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003883 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 gui.bgcolor = g_new0(GdkColor, 1);
Bram Moolenaarf36d3692005-03-15 22:48:14 +00003885 /* LINTED: avoid warning: conversion to 'unsigned long' */
3886 gui.spcolor = g_new0(GdkColor, 1);
Bram Moolenaar36edf062016-07-21 22:10:12 +02003887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888
3889 /* Initialise atoms */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00003890 html_atom = gdk_atom_intern("text/html", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892
3893 /* Set default foreground and background colors. */
3894 gui.norm_pixel = gui.def_norm_pixel;
3895 gui.back_pixel = gui.def_back_pixel;
3896
3897 if (gtk_socket_id != 0)
3898 {
3899 GtkWidget *plug;
3900
3901 /* Use GtkSocket from another app. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 plug = gtk_plug_new_for_display(gdk_display_get_default(),
3903 gtk_socket_id);
Bram Moolenaar98921892016-02-23 17:14:37 +01003904#if GTK_CHECK_VERSION(3,0,0)
3905 if (plug != NULL && gtk_plug_get_socket_window(GTK_PLUG(plug)) != NULL)
3906#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 if (plug != NULL && GTK_PLUG(plug)->socket_window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01003908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 {
3910 gui.mainwin = plug;
3911 }
3912 else
3913 {
3914 g_warning("Connection to GTK+ socket (ID %u) failed",
3915 (unsigned int)gtk_socket_id);
3916 /* Pretend we never wanted it if it failed (get own window) */
3917 gtk_socket_id = 0;
3918 }
3919 }
3920
3921 if (gtk_socket_id == 0)
3922 {
3923#ifdef FEAT_GUI_GNOME
3924 if (using_gnome)
3925 {
3926 gui.mainwin = gnome_app_new("Vim", NULL);
3927# ifdef USE_XSMP
3928 /* Use the GNOME save-yourself functionality now. */
3929 xsmp_close();
3930# endif
3931 }
3932 else
3933#endif
3934 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3935 }
3936
3937 gtk_widget_set_name(gui.mainwin, "vim-main-window");
3938
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 /* Create the PangoContext used for drawing all text. */
3940 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
3941 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942
Bram Moolenaar98921892016-02-23 17:14:37 +01003943#if GTK_CHECK_VERSION(3,0,0)
3944 gtk_container_set_border_width(GTK_CONTAINER(gui.mainwin), 0);
3945#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 gtk_container_border_width(GTK_CONTAINER(gui.mainwin), 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK);
3949
Bram Moolenaar98921892016-02-23 17:14:37 +01003950#if GTK_CHECK_VERSION(3,0,0)
3951 g_signal_connect(G_OBJECT(gui.mainwin), "delete-event",
3952 G_CALLBACK(&delete_event_cb), NULL);
3953
3954 g_signal_connect(G_OBJECT(gui.mainwin), "realize",
3955 G_CALLBACK(&mainwin_realize), NULL);
3956#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "delete_event",
3958 GTK_SIGNAL_FUNC(&delete_event_cb), NULL);
3959
3960 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "realize",
3961 GTK_SIGNAL_FUNC(&mainwin_realize), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01003962#endif
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02003963
3964 g_signal_connect(G_OBJECT(gui.mainwin), "screen-changed",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 G_CALLBACK(&mainwin_screen_changed_cb), NULL);
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02003966
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 gui.accel_group = gtk_accel_group_new();
3968 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003970 /* A vertical box holds the menubar, toolbar and main text window. */
Bram Moolenaar98921892016-02-23 17:14:37 +01003971#if GTK_CHECK_VERSION(3,2,0)
3972 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3973 gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE);
3974#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 vbox = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
3978#ifdef FEAT_GUI_GNOME
3979 if (using_gnome)
3980 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003981# if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 /* automagically restore menubar/toolbar placement */
3983 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE);
3984# endif
3985 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox);
3986 }
3987 else
3988#endif
3989 {
3990 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox);
3991 gtk_widget_show(vbox);
3992 }
3993
3994#ifdef FEAT_MENU
3995 /*
3996 * Create the menubar and handle
3997 */
3998 gui.menubar = gtk_menu_bar_new();
3999 gtk_widget_set_name(gui.menubar, "vim-menubar");
4000
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004001 /* Avoid that GTK takes <F10> away from us. */
4002 {
4003 GtkSettings *gtk_settings;
4004
4005 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
4006 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
4007 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004008
4009
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010# ifdef FEAT_GUI_GNOME
4011 if (using_gnome)
4012 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 BonoboDockItem *dockitem;
4014
4015 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar));
4016 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
4017 GNOME_APP_MENUBAR_NAME);
Bram Moolenaardb552d602006-03-23 22:59:57 +00004018 /* We don't want the menu to float. */
4019 bonobo_dock_item_set_behavior(dockitem,
4020 bonobo_dock_item_get_behavior(dockitem)
4021 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 gui.menubar_h = GTK_WIDGET(dockitem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 }
4024 else
4025# endif /* FEAT_GUI_GNOME */
4026 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00004027 /* Always show the menubar, otherwise <F10> doesn't work. It may be
4028 * disabled in gui_init() later. */
4029 gtk_widget_show(gui.menubar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0);
4031 }
4032#endif /* FEAT_MENU */
4033
4034#ifdef FEAT_TOOLBAR
4035 /*
4036 * Create the toolbar and handle
4037 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 /* some aesthetics on the toolbar */
Bram Moolenaar98921892016-02-23 17:14:37 +01004039# ifdef USE_GTK3
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01004040 /* TODO: Add GTK+ 3 code here using GtkCssProvider if necessary. */
Bram Moolenaar98921892016-02-23 17:14:37 +01004041 /* N.B. Since the default value of GtkToolbar::button-relief is
4042 * GTK_RELIEF_NONE, there's no need to specify that, probably. */
4043# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 gtk_rc_parse_string(
4045 "style \"vim-toolbar-style\" {\n"
4046 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
4047 "}\n"
4048 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
Bram Moolenaar98921892016-02-23 17:14:37 +01004049# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 gui.toolbar = gtk_toolbar_new();
4051 gtk_widget_set_name(gui.toolbar, "vim-toolbar");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
4053
4054# ifdef FEAT_GUI_GNOME
4055 if (using_gnome)
4056 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 BonoboDockItem *dockitem;
4058
4059 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar));
4060 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
4061 GNOME_APP_TOOLBAR_NAME);
4062 gui.toolbar_h = GTK_WIDGET(dockitem);
Bram Moolenaare580b0c2006-03-21 21:33:03 +00004063 /* When the toolbar is floating it gets stuck. So long as that isn't
Bram Moolenaardb552d602006-03-23 22:59:57 +00004064 * fixed let's disallow floating. */
Bram Moolenaare580b0c2006-03-21 21:33:03 +00004065 bonobo_dock_item_set_behavior(dockitem,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004066 bonobo_dock_item_get_behavior(dockitem)
4067 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
4070 else
4071# endif /* FEAT_GUI_GNOME */
4072 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 if (vim_strchr(p_go, GO_TOOLBAR) != NULL
4074 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
4075 gtk_widget_show(gui.toolbar);
4076 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0);
4077 }
4078#endif /* FEAT_TOOLBAR */
4079
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004080#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004081 /*
4082 * Use a Notebook for the tab pages labels. The labels are hidden by
4083 * default.
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004084 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004085 gui.tabline = gtk_notebook_new();
4086 gtk_widget_show(gui.tabline);
4087 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0);
4088 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
4089 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004090 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01004091# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar96351572006-05-05 21:16:59 +00004092 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01004093# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004094
Bram Moolenaar98921892016-02-23 17:14:37 +01004095# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004096 tabline_tooltip = gtk_tooltips_new();
4097 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip));
Bram Moolenaar98921892016-02-23 17:14:37 +01004098# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004099
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004100 {
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004101 GtkWidget *page, *label, *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004102
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004103 /* Add the first tab. */
Bram Moolenaar98921892016-02-23 17:14:37 +01004104# if GTK_CHECK_VERSION(3,2,0)
4105 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
4106 gtk_box_set_homogeneous(GTK_BOX(page), FALSE);
4107# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004108 page = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01004109# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004110 gtk_widget_show(page);
4111 gtk_container_add(GTK_CONTAINER(gui.tabline), page);
4112 label = gtk_label_new("-Empty-");
4113 gtk_widget_show(label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004114 event_box = gtk_event_box_new();
4115 gtk_widget_show(event_box);
Bram Moolenaar98921892016-02-23 17:14:37 +01004116# if GTK_CHECK_VERSION(3,0,0)
4117 g_object_set_data(G_OBJECT(event_box), "tab_num", GINT_TO_POINTER(1L));
4118# else
Bram Moolenaar47b46d72008-07-02 19:05:48 +00004119 gtk_object_set_user_data(GTK_OBJECT(event_box), (gpointer)1L);
Bram Moolenaar98921892016-02-23 17:14:37 +01004120# endif
4121# if !GTK_CHECK_VERSION(3,14,0)
Bram Moolenaar8ea91232006-04-28 22:41:43 +00004122 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01004123# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004124 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004125 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004126 }
Bram Moolenaar54a709e2006-05-04 21:57:11 +00004127
Bram Moolenaar98921892016-02-23 17:14:37 +01004128# if GTK_CHECK_VERSION(3,0,0)
4129 g_signal_connect(G_OBJECT(gui.tabline), "switch-page",
4130 G_CALLBACK(on_select_tab), NULL);
4131# else
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004132 gtk_signal_connect(GTK_OBJECT(gui.tabline), "switch_page",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004133 GTK_SIGNAL_FUNC(on_select_tab), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004134# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004135
4136 /* Create a popup menu for the tab line and connect it. */
4137 tabline_menu = create_tabline_menu();
Bram Moolenaar98921892016-02-23 17:14:37 +01004138# if GTK_CHECK_VERSION(3,0,0)
4139 g_signal_connect_swapped(G_OBJECT(gui.tabline), "button-press-event",
4140 G_CALLBACK(on_tabline_menu), G_OBJECT(tabline_menu));
4141# else
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004142 gtk_signal_connect_object(GTK_OBJECT(gui.tabline), "button_press_event",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004143 GTK_SIGNAL_FUNC(on_tabline_menu), GTK_OBJECT(tabline_menu));
Bram Moolenaar98921892016-02-23 17:14:37 +01004144# endif
4145#endif /* FEAT_GUI_TABLINE */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004146
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 gui.formwin = gtk_form_new();
Bram Moolenaar98921892016-02-23 17:14:37 +01004148#if GTK_CHECK_VERSION(3,0,0)
4149 gtk_container_set_border_width(GTK_CONTAINER(gui.formwin), 0);
4150#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 gtk_container_border_width(GTK_CONTAINER(gui.formwin), 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01004152#endif
4153#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK);
Bram Moolenaar98921892016-02-23 17:14:37 +01004155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156
4157 gui.drawarea = gtk_drawing_area_new();
Bram Moolenaara859f042016-11-17 19:11:55 +01004158#if GTK_CHECK_VERSION(3,22,2)
4159 gtk_widget_set_name(gui.drawarea, "vim-gui-drawarea");
4160#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004161#if GTK_CHECK_VERSION(3,0,0)
4162 gui.surface = NULL;
4163 gui.by_signal = FALSE;
4164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165
4166 /* Determine which events we will filter. */
4167 gtk_widget_set_events(gui.drawarea,
4168 GDK_EXPOSURE_MASK |
4169 GDK_ENTER_NOTIFY_MASK |
4170 GDK_LEAVE_NOTIFY_MASK |
4171 GDK_BUTTON_PRESS_MASK |
4172 GDK_BUTTON_RELEASE_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 GDK_SCROLL_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 GDK_KEY_PRESS_MASK |
4175 GDK_KEY_RELEASE_MASK |
4176 GDK_POINTER_MOTION_MASK |
4177 GDK_POINTER_MOTION_HINT_MASK);
4178
4179 gtk_widget_show(gui.drawarea);
4180 gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0);
4181 gtk_widget_show(gui.formwin);
4182 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0);
4183
4184 /* For GtkSockets, key-presses must go to the focus widget (drawarea)
4185 * and not the window. */
Bram Moolenaar98921892016-02-23 17:14:37 +01004186#if GTK_CHECK_VERSION(3,0,0)
4187 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
4188 : G_OBJECT(gui.drawarea),
4189 "key-press-event",
4190 G_CALLBACK(key_press_event), NULL);
4191#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 gtk_signal_connect((gtk_socket_id == 0) ? GTK_OBJECT(gui.mainwin)
4193 : GTK_OBJECT(gui.drawarea),
4194 "key_press_event",
4195 GTK_SIGNAL_FUNC(key_press_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004196#endif
4197#if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 /* Also forward key release events for the benefit of GTK+ 2 input
4199 * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */
4200 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
4201 : G_OBJECT(gui.drawarea),
Bram Moolenaar98921892016-02-23 17:14:37 +01004202 "key-release-event",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 G_CALLBACK(&key_release_event), NULL);
4204#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004205#if GTK_CHECK_VERSION(3,0,0)
4206 g_signal_connect(G_OBJECT(gui.drawarea), "realize",
4207 G_CALLBACK(drawarea_realize_cb), NULL);
4208 g_signal_connect(G_OBJECT(gui.drawarea), "unrealize",
4209 G_CALLBACK(drawarea_unrealize_cb), NULL);
4210 g_signal_connect(G_OBJECT(gui.drawarea), "configure-event",
4211 G_CALLBACK(drawarea_configure_event_cb), NULL);
Bram Moolenaara859f042016-11-17 19:11:55 +01004212# if GTK_CHECK_VERSION(3,22,2)
4213 g_signal_connect_after(G_OBJECT(gui.drawarea), "style-updated",
4214 G_CALLBACK(&drawarea_style_updated_cb), NULL);
4215# else
Bram Moolenaar98921892016-02-23 17:14:37 +01004216 g_signal_connect_after(G_OBJECT(gui.drawarea), "style-set",
4217 G_CALLBACK(&drawarea_style_set_cb), NULL);
Bram Moolenaara859f042016-11-17 19:11:55 +01004218# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004219#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "realize",
4221 GTK_SIGNAL_FUNC(drawarea_realize_cb), NULL);
4222 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "unrealize",
4223 GTK_SIGNAL_FUNC(drawarea_unrealize_cb), NULL);
4224
4225 gtk_signal_connect_after(GTK_OBJECT(gui.drawarea), "style_set",
4226 GTK_SIGNAL_FUNC(&drawarea_style_set_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228
Bram Moolenaar98921892016-02-23 17:14:37 +01004229#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 gui.visibility = GDK_VISIBILITY_UNOBSCURED;
Bram Moolenaar98921892016-02-23 17:14:37 +01004231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232
4233#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
4234 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE);
4235 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE);
4236#endif
4237
4238 if (gtk_socket_id != 0)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004239 /* make sure keyboard input can go to the drawarea */
Bram Moolenaar98921892016-02-23 17:14:37 +01004240#if GTK_CHECK_VERSION(3,0,0)
4241 gtk_widget_set_can_focus(gui.drawarea, TRUE);
4242#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 GTK_WIDGET_SET_FLAGS(gui.drawarea, GTK_CAN_FOCUS);
Bram Moolenaar98921892016-02-23 17:14:37 +01004244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245
4246 /*
4247 * Set clipboard specific atoms
4248 */
4249 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY;
4252 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE);
4253
4254 /*
4255 * Start out by adding the configured border width into the border offset.
4256 */
4257 gui.border_offset = gui.border_width;
4258
Bram Moolenaar98921892016-02-23 17:14:37 +01004259#if GTK_CHECK_VERSION(3,0,0)
4260 g_signal_connect(G_OBJECT(gui.drawarea), "draw",
4261 G_CALLBACK(draw_event), NULL);
4262#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event",
4264 GTK_SIGNAL_FUNC(visibility_event), NULL);
4265 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event",
4266 GTK_SIGNAL_FUNC(expose_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268
4269 /*
4270 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
4271 * Only needed for some window managers.
4272 */
4273 if (vim_strchr(p_go, GO_POINTER) != NULL)
4274 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004275#if GTK_CHECK_VERSION(3,0,0)
4276 g_signal_connect(G_OBJECT(gui.drawarea), "leave-notify-event",
4277 G_CALLBACK(leave_notify_event), NULL);
4278 g_signal_connect(G_OBJECT(gui.drawarea), "enter-notify-event",
4279 G_CALLBACK(enter_notify_event), NULL);
4280#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "leave_notify_event",
4282 GTK_SIGNAL_FUNC(leave_notify_event), NULL);
4283 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "enter_notify_event",
4284 GTK_SIGNAL_FUNC(enter_notify_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
4287
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004288 /* Real windows can get focus ... GtkPlug, being a mere container can't,
4289 * only its widgets. Arguably, this could be common code and we not use
4290 * the window focus at all, but let's be safe.
4291 */
4292 if (gtk_socket_id == 0)
4293 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004294#if GTK_CHECK_VERSION(3,0,0)
4295 g_signal_connect(G_OBJECT(gui.mainwin), "focus-out-event",
4296 G_CALLBACK(focus_out_event), NULL);
4297 g_signal_connect(G_OBJECT(gui.mainwin), "focus-in-event",
4298 G_CALLBACK(focus_in_event), NULL);
4299#else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004300 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_out_event",
4301 GTK_SIGNAL_FUNC(focus_out_event), NULL);
4302 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_in_event",
4303 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004304#endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004305 }
4306 else
4307 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004308#if GTK_CHECK_VERSION(3,0,0)
4309 g_signal_connect(G_OBJECT(gui.drawarea), "focus-out-event",
4310 G_CALLBACK(focus_out_event), NULL);
4311 g_signal_connect(G_OBJECT(gui.drawarea), "focus-in-event",
4312 G_CALLBACK(focus_in_event), NULL);
4313#else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004314 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_out_event",
4315 GTK_SIGNAL_FUNC(focus_out_event), NULL);
4316 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_in_event",
4317 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004318#endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004319#ifdef FEAT_GUI_TABLINE
Bram Moolenaar98921892016-02-23 17:14:37 +01004320# if GTK_CHECK_VERSION(3,0,0)
4321 g_signal_connect(G_OBJECT(gui.tabline), "focus-out-event",
4322 G_CALLBACK(focus_out_event), NULL);
4323 g_signal_connect(G_OBJECT(gui.tabline), "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.tabline), "focus_out_event",
4327 GTK_SIGNAL_FUNC(focus_out_event), NULL);
4328 gtk_signal_connect(GTK_OBJECT(gui.tabline), "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#endif /* FEAT_GUI_TABLINE */
4332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333
Bram Moolenaar98921892016-02-23 17:14:37 +01004334#if GTK_CHECK_VERSION(3,0,0)
4335 g_signal_connect(G_OBJECT(gui.drawarea), "motion-notify-event",
4336 G_CALLBACK(motion_notify_event), NULL);
4337 g_signal_connect(G_OBJECT(gui.drawarea), "button-press-event",
4338 G_CALLBACK(button_press_event), NULL);
4339 g_signal_connect(G_OBJECT(gui.drawarea), "button-release-event",
4340 G_CALLBACK(button_release_event), NULL);
4341 g_signal_connect(G_OBJECT(gui.drawarea), "scroll-event",
4342 G_CALLBACK(&scroll_event), NULL);
4343#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "motion_notify_event",
4345 GTK_SIGNAL_FUNC(motion_notify_event), NULL);
4346 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_press_event",
4347 GTK_SIGNAL_FUNC(button_press_event), NULL);
4348 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_release_event",
4349 GTK_SIGNAL_FUNC(button_release_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 g_signal_connect(G_OBJECT(gui.drawarea), "scroll_event",
4351 G_CALLBACK(&scroll_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353
4354 /*
4355 * Add selection handler functions.
4356 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004357#if GTK_CHECK_VERSION(3,0,0)
4358 g_signal_connect(G_OBJECT(gui.drawarea), "selection-clear-event",
4359 G_CALLBACK(selection_clear_event), NULL);
4360 g_signal_connect(G_OBJECT(gui.drawarea), "selection-received",
4361 G_CALLBACK(selection_received_cb), NULL);
4362#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_clear_event",
4364 GTK_SIGNAL_FUNC(selection_clear_event), NULL);
4365 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_received",
4366 GTK_SIGNAL_FUNC(selection_received_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368
Bram Moolenaara76638f2010-06-05 12:49:46 +02004369 gui_gtk_set_selection_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370
Bram Moolenaar98921892016-02-23 17:14:37 +01004371#if GTK_CHECK_VERSION(3,0,0)
4372 g_signal_connect(G_OBJECT(gui.drawarea), "selection-get",
4373 G_CALLBACK(selection_get_cb), NULL);
4374#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_get",
4376 GTK_SIGNAL_FUNC(selection_get_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
4379 /* Pretend we don't have input focus, we will get an event if we do. */
4380 gui.in_focus = FALSE;
4381
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 return OK;
4383}
4384
4385#if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO)
4386/*
4387 * This is called from gui_start() after a fork() has been done.
4388 * We have to tell the session manager our new PID.
4389 */
4390 void
4391gui_mch_forked(void)
4392{
4393 if (using_gnome)
4394 {
4395 GnomeClient *client;
4396
4397 client = gnome_master_client();
4398
4399 if (client != NULL)
4400 gnome_client_set_process_id(client, getpid());
4401 }
4402}
4403#endif /* FEAT_GUI_GNOME && FEAT_SESSION */
4404
Bram Moolenaar98921892016-02-23 17:14:37 +01004405#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02004406 static GdkRGBA
4407color_to_rgba(guicolor_T color)
Bram Moolenaar98921892016-02-23 17:14:37 +01004408{
Bram Moolenaar36edf062016-07-21 22:10:12 +02004409 GdkRGBA rgba;
4410 rgba.red = ((color & 0xff0000) >> 16) / 255.0;
4411 rgba.green = ((color & 0xff00) >> 8) / 255.0;
4412 rgba.blue = ((color & 0xff)) / 255.0;
4413 rgba.alpha = 1.0;
4414 return rgba;
Bram Moolenaar98921892016-02-23 17:14:37 +01004415}
4416
4417 static void
Bram Moolenaar36edf062016-07-21 22:10:12 +02004418set_cairo_source_rgba_from_color(cairo_t *cr, guicolor_T color)
Bram Moolenaar98921892016-02-23 17:14:37 +01004419{
Bram Moolenaar36edf062016-07-21 22:10:12 +02004420 const GdkRGBA rgba = color_to_rgba(color);
4421 cairo_set_source_rgba(cr, rgba.red, rgba.green, rgba.blue, rgba.alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01004422}
4423#endif /* GTK_CHECK_VERSION(3,0,0) */
4424
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425/*
4426 * Called when the foreground or background color has been changed.
4427 * This used to change the graphics contexts directly but we are
4428 * currently manipulating them where desired.
4429 */
4430 void
4431gui_mch_new_colors(void)
4432{
Bram Moolenaar98921892016-02-23 17:14:37 +01004433#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaara859f042016-11-17 19:11:55 +01004434# if !GTK_CHECK_VERSION(3,22,2)
Bram Moolenaar98921892016-02-23 17:14:37 +01004435 GdkWindow * const da_win = gtk_widget_get_window(gui.drawarea);
Bram Moolenaara859f042016-11-17 19:11:55 +01004436# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004437
4438 if (gui.drawarea != NULL && gtk_widget_get_window(gui.drawarea) != NULL)
4439#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01004441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 {
Bram Moolenaara859f042016-11-17 19:11:55 +01004443#if GTK_CHECK_VERSION(3,22,2)
4444 GtkStyleContext * const context
4445 = gtk_widget_get_style_context(gui.drawarea);
4446 GtkCssProvider * const provider = gtk_css_provider_new();
4447 gchar * const css = g_strdup_printf(
4448 "widget#vim-gui-drawarea {\n"
4449 " background-color: #%.2lx%.2lx%.2lx;\n"
4450 "}\n",
4451 (gui.back_pixel >> 16) & 0xff,
4452 (gui.back_pixel >> 8) & 0xff,
4453 gui.back_pixel & 0xff);
4454
4455 gtk_css_provider_load_from_data(provider, css, -1, NULL);
4456 gtk_style_context_add_provider(context,
4457 GTK_STYLE_PROVIDER(provider), G_MAXUINT);
4458
4459 g_free(css);
4460 g_object_unref(provider);
4461#elif GTK_CHECK_VERSION(3,4,0) /* !GTK_CHECK_VERSION(3,22,2) */
Bram Moolenaar36edf062016-07-21 22:10:12 +02004462 GdkRGBA rgba;
Bram Moolenaar98921892016-02-23 17:14:37 +01004463
Bram Moolenaar36edf062016-07-21 22:10:12 +02004464 rgba = color_to_rgba(gui.back_pixel);
Bram Moolenaar98921892016-02-23 17:14:37 +01004465 {
4466 cairo_pattern_t * const pat = cairo_pattern_create_rgba(
Bram Moolenaar36edf062016-07-21 22:10:12 +02004467 rgba.red, rgba.green, rgba.blue, rgba.alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01004468 if (pat != NULL)
4469 {
4470 gdk_window_set_background_pattern(da_win, pat);
4471 cairo_pattern_destroy(pat);
4472 }
4473 else
Bram Moolenaar36edf062016-07-21 22:10:12 +02004474 gdk_window_set_background_rgba(da_win, &rgba);
Bram Moolenaar98921892016-02-23 17:14:37 +01004475 }
4476#else /* !GTK_CHECK_VERSION(3,4,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 GdkColor color = { 0, 0, 0, 0 };
4478
4479 color.pixel = gui.back_pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01004480# if GTK_CHECK_VERSION(3,0,0)
4481 gdk_window_set_background(da_win, &color);
4482# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 gdk_window_set_background(gui.drawarea->window, &color);
Bram Moolenaar98921892016-02-23 17:14:37 +01004484# endif
Bram Moolenaara859f042016-11-17 19:11:55 +01004485#endif /* !GTK_CHECK_VERSION(3,22,2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 }
4487}
4488
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489/*
4490 * This signal informs us about the need to rearrange our sub-widgets.
4491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004493form_configure_event(GtkWidget *widget UNUSED,
4494 GdkEventConfigure *event,
4495 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496{
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004497 int usable_height = event->height;
4498
Bram Moolenaar182707a2016-11-21 20:55:58 +01004499#if GTK_CHECK_VERSION(3,22,2) && !GTK_CHECK_VERSION(3,22,4)
Bram Moolenaara859f042016-11-17 19:11:55 +01004500 /* As of 3.22.2, GdkWindows have started distributing configure events to
4501 * their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0).
4502 *
4503 * As can be seen from the implementation of move_native_children() and
4504 * configure_native_child() in gdkwindow.c, those functions actually
4505 * propagate configure events to every child, failing to distinguish
4506 * "native" one from non-native one.
4507 *
4508 * Naturally, configure events propagated to here like that are fallacious
4509 * and, as a matter of fact, they trigger a geometric collapse of
4510 * gui.formwin.
4511 *
4512 * To filter out such fallacious events, check if the given event is the
4513 * one that was sent out to the right place. Ignore it if not.
4514 */
Bram Moolenaar182707a2016-11-21 20:55:58 +01004515 /* Follow-up
4516 * After a few weeks later, the GdkWindow change mentioned above was
4517 * reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155).
4518 * The corresponding official release is 3.22.4. */
Bram Moolenaara859f042016-11-17 19:11:55 +01004519 if (event->window != gtk_widget_get_window(gui.formwin))
4520 return TRUE;
4521#endif
4522
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004523 /* When in a GtkPlug, we can't guarantee valid heights (as a round
4524 * no. of char-heights), so we have to manually sanitise them.
4525 * Widths seem to sort themselves out, don't ask me why.
4526 */
4527 if (gtk_socket_id != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004528 usable_height -= (gui.char_height - (gui.char_height/2)); /* sic. */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004529
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 gtk_form_freeze(GTK_FORM(gui.formwin));
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004531 gui_resize_shell(event->width, usable_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 gtk_form_thaw(GTK_FORM(gui.formwin));
4533
4534 return TRUE;
4535}
4536
4537/*
4538 * Function called when window already closed.
4539 * We can't do much more here than to trying to preserve what had been done,
4540 * since the window is already inevitably going away.
4541 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004542#if GTK_CHECK_VERSION(3,0,0)
4543 static void
4544mainwin_destroy_cb(GObject *object UNUSED, gpointer data UNUSED)
4545#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004547mainwin_destroy_cb(GtkObject *object UNUSED, gpointer data UNUSED)
Bram Moolenaar98921892016-02-23 17:14:37 +01004548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549{
4550 /* Don't write messages to the GUI anymore */
4551 full_screen = FALSE;
4552
4553 gui.mainwin = NULL;
4554 gui.drawarea = NULL;
4555
4556 if (!exiting) /* only do anything if the destroy was unexpected */
4557 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004558 vim_strncpy(IObuff,
4559 (char_u *)_("Vim: Main window unexpectedly destroyed\n"),
4560 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 preserve_exit();
4562 }
Bram Moolenaar36e294c2015-12-29 18:55:46 +01004563#ifdef USE_GRESOURCE
4564 gui_gtk_unregister_resource();
4565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566}
4567
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004568
4569/*
4570 * Bit of a hack to ensure we start GtkPlug windows with the correct window
4571 * hints (and thus the required size from -geom), but that after that we
4572 * put the hints back to normal (the actual minimum size) so we may
4573 * subsequently be resized smaller. GtkSocket (the parent end) uses the
Bram Moolenaar49325942007-05-10 19:19:59 +00004574 * plug's window 'min hints to set *it's* minimum size, but that's also the
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004575 * only way we have of making ourselves bigger (by set lines/columns).
4576 * Thus set hints at start-up to ensure correct init. size, then a
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01004577 * second after the final attempt to reset the real minimum hints (done by
4578 * scrollbar init.), actually do the standard hints and stop the timer.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004579 * We'll not let the default hints be set while this timer's active.
4580 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004581 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004582check_startup_plug_hints(gpointer data UNUSED)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004583{
4584 if (init_window_hints_state == 1)
4585 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004586 /* Safe to use normal hints now */
4587 init_window_hints_state = 0;
4588 update_window_manager_hints(0, 0);
4589 return FALSE; /* stop timer */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004590 }
4591
4592 /* Keep on trying */
4593 init_window_hints_state = 1;
4594 return TRUE;
4595}
4596
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597/*
4598 * Open the GUI window which was created by a call to gui_mch_init().
4599 */
4600 int
4601gui_mch_open(void)
4602{
4603 guicolor_T fg_pixel = INVALCOLOR;
4604 guicolor_T bg_pixel = INVALCOLOR;
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004605 guint pixel_width;
4606 guint pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 /*
4609 * Allow setting a window role on the command line, or invent one
4610 * if none was specified. This is mainly useful for GNOME session
4611 * support; allowing the WM to restore window placement.
4612 */
4613 if (role_argument != NULL)
4614 {
4615 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument);
4616 }
4617 else
4618 {
4619 char *role;
4620
4621 /* Invent a unique-enough ID string for the role */
4622 role = g_strdup_printf("vim-%u-%u-%u",
4623 (unsigned)mch_get_pid(),
4624 (unsigned)g_random_int(),
4625 (unsigned)time(NULL));
4626
4627 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role);
4628 g_free(role);
4629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630
4631 if (gui_win_x != -1 && gui_win_y != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633
4634 /* Determine user specified geometry, if present. */
4635 if (gui.geom != NULL)
4636 {
4637 int mask;
4638 unsigned int w, h;
4639 int x = 0;
4640 int y = 0;
4641
4642 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
4643
4644 if (mask & WidthValue)
4645 Columns = w;
4646 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00004647 {
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004648 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00004649 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00004651 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004652 limit_screen_size();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004653
4654 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4655 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
4656
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004657 pixel_width += get_menu_tool_width();
4658 pixel_height += get_menu_tool_height();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004659
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 if (mask & (XValue | YValue))
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004661 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004662 int ww, hh;
4663 gui_mch_get_screen_dimensions(&ww, &hh);
4664 hh += p_ghr + get_menu_tool_height();
4665 ww += get_menu_tool_width();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004666 if (mask & XNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004667 x += ww - pixel_width;
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004668 if (mask & YNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004669 y += hh - pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 vim_free(gui.geom);
4673 gui.geom = NULL;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004674
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004675 /* From now until everyone's stopped trying to set the window hints
4676 * to their correct minimum values, stop them being set as we need
4677 * them to remain at our required size for the parent GtkSocket to
4678 * give us the right initial size.
4679 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004680 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004681 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004682 update_window_manager_hints(pixel_width, pixel_height);
4683 init_window_hints_state = 1;
4684 g_timeout_add(1000, check_startup_plug_hints, NULL);
4685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 }
4687
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004688 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4689 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004690 /* For GTK2 changing the size of the form widget doesn't cause window
4691 * resizing. */
Bram Moolenaardebe25a2010-06-06 17:41:24 +02004692 if (gtk_socket_id == 0)
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004693 gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004694 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695
4696 if (foreground_argument != NULL)
4697 fg_pixel = gui_get_color((char_u *)foreground_argument);
4698 if (fg_pixel == INVALCOLOR)
4699 fg_pixel = gui_get_color((char_u *)"Black");
4700
4701 if (background_argument != NULL)
4702 bg_pixel = gui_get_color((char_u *)background_argument);
4703 if (bg_pixel == INVALCOLOR)
4704 bg_pixel = gui_get_color((char_u *)"White");
4705
4706 if (found_reverse_arg)
4707 {
4708 gui.def_norm_pixel = bg_pixel;
4709 gui.def_back_pixel = fg_pixel;
4710 }
4711 else
4712 {
4713 gui.def_norm_pixel = fg_pixel;
4714 gui.def_back_pixel = bg_pixel;
4715 }
4716
4717 /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or
4718 * in a vimrc file) */
4719 set_normal_colors();
4720
4721 /* Check that none of the colors are the same as the background color */
4722 gui_check_colors();
4723
4724 /* Get the colors for the highlight groups (gui_check_colors() might have
4725 * changed them). */
4726 highlight_gui_started(); /* re-init colors and fonts */
4727
Bram Moolenaar98921892016-02-23 17:14:37 +01004728#if GTK_CHECK_VERSION(3,0,0)
4729 g_signal_connect(G_OBJECT(gui.mainwin), "destroy",
4730 G_CALLBACK(mainwin_destroy_cb), NULL);
4731#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "destroy",
4733 GTK_SIGNAL_FUNC(mainwin_destroy_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735
4736#ifdef FEAT_HANGULIN
4737 hangul_keyboard_set();
4738#endif
4739
4740 /*
4741 * Notify the fixed area about the need to resize the contents of the
4742 * gui.formwin, which we use for random positioning of the included
4743 * components.
4744 *
4745 * We connect this signal deferred finally after anything is in place,
4746 * since this is intended to handle resizements coming from the window
4747 * manager upon us and should not interfere with what VIM is requesting
4748 * upon startup.
4749 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004750#if GTK_CHECK_VERSION(3,0,0)
4751 g_signal_connect(G_OBJECT(gui.formwin), "configure-event",
4752 G_CALLBACK(form_configure_event), NULL);
4753#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 gtk_signal_connect(GTK_OBJECT(gui.formwin), "configure_event",
4755 GTK_SIGNAL_FUNC(form_configure_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757
4758#ifdef FEAT_DND
Bram Moolenaara76638f2010-06-05 12:49:46 +02004759 /* Set up for receiving DND items. */
4760 gui_gtk_set_dnd_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761
Bram Moolenaar98921892016-02-23 17:14:37 +01004762# if GTK_CHECK_VERSION(3,0,0)
4763 g_signal_connect(G_OBJECT(gui.drawarea), "drag-data-received",
4764 G_CALLBACK(drag_data_received_cb), NULL);
4765# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "drag_data_received",
4767 GTK_SIGNAL_FUNC(drag_data_received_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004768# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769#endif
4770
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 /* With GTK+ 2, we need to iconify the window before calling show()
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004772 * to avoid mapping the window for a short time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 if (found_iconic_arg && gtk_socket_id == 0)
4774 gui_mch_iconify();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775
4776 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004777#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 unsigned long menu_handler = 0;
4779# ifdef FEAT_TOOLBAR
4780 unsigned long tool_handler = 0;
4781# endif
4782 /*
4783 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
4784 * show when restoring the saved layout configuration. We can't just
4785 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
4786 * a toplevel window and thus will be realized immediately. Instead,
4787 * connect signal handlers to hide the widgets just after they've been
4788 * marked visible, but before the main window is realized.
4789 */
4790 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL)
4791 menu_handler = g_signal_connect_after(gui.menubar_h, "show",
4792 G_CALLBACK(&gtk_widget_hide),
4793 NULL);
4794# ifdef FEAT_TOOLBAR
4795 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL
4796 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
4797 tool_handler = g_signal_connect_after(gui.toolbar_h, "show",
4798 G_CALLBACK(&gtk_widget_hide),
4799 NULL);
4800# endif
4801#endif
4802 gtk_widget_show(gui.mainwin);
4803
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004804#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 if (menu_handler != 0)
4806 g_signal_handler_disconnect(gui.menubar_h, menu_handler);
4807# ifdef FEAT_TOOLBAR
4808 if (tool_handler != 0)
4809 g_signal_handler_disconnect(gui.toolbar_h, tool_handler);
4810# endif
4811#endif
4812 }
4813
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 return OK;
4815}
4816
4817
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004819gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820{
4821 if (gui.mainwin != NULL)
4822 gtk_widget_destroy(gui.mainwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823}
4824
4825/*
4826 * Get the position of the top left corner of the window.
4827 */
4828 int
4829gui_mch_get_winpos(int *x, int *y)
4830{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 return OK;
4833}
4834
4835/*
4836 * Set the position of the top left corner of the window to the given
4837 * coordinates.
4838 */
4839 void
4840gui_mch_set_winpos(int x, int y)
4841{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843}
4844
Bram Moolenaar98921892016-02-23 17:14:37 +01004845#if !GTK_CHECK_VERSION(3,0,0)
4846# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847static int resize_idle_installed = FALSE;
4848/*
4849 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
4850 * the shell size doesn't exceed the window size, i.e. if the window manager
4851 * ignored our size request. Usually this happens if the window is maximized.
4852 *
4853 * FIXME: It'd be nice if we could find a little more orthodox solution.
4854 * See also the remark below in gui_mch_set_shellsize().
4855 *
4856 * DISABLED: When doing ":set lines+=1" this function would first invoke
4857 * gui_resize_shell() with the old size, then the normal callback would
4858 * report the new size through form_configure_event(). That caused the window
4859 * layout to be messed up.
4860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 static gboolean
4862force_shell_resize_idle(gpointer data)
4863{
4864 if (gui.mainwin != NULL
4865 && GTK_WIDGET_REALIZED(gui.mainwin)
4866 && GTK_WIDGET_VISIBLE(gui.mainwin))
4867 {
4868 int width;
4869 int height;
4870
4871 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height);
4872
4873 width -= get_menu_tool_width();
4874 height -= get_menu_tool_height();
4875
4876 gui_resize_shell(width, height);
4877 }
4878
4879 resize_idle_installed = FALSE;
4880 return FALSE; /* don't call me again */
4881}
Bram Moolenaar98921892016-02-23 17:14:37 +01004882# endif
4883#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884
Bram Moolenaar09736232009-09-23 16:14:49 +00004885/*
4886 * Return TRUE if the main window is maximized.
4887 */
4888 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004889gui_mch_maximized(void)
Bram Moolenaar09736232009-09-23 16:14:49 +00004890{
Bram Moolenaar98921892016-02-23 17:14:37 +01004891#if GTK_CHECK_VERSION(3,0,0)
4892 return (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL
4893 && (gdk_window_get_state(gtk_widget_get_window(gui.mainwin))
4894 & GDK_WINDOW_STATE_MAXIMIZED));
4895#else
Bram Moolenaar09736232009-09-23 16:14:49 +00004896 return (gui.mainwin != NULL && gui.mainwin->window != NULL
4897 && (gdk_window_get_state(gui.mainwin->window)
4898 & GDK_WINDOW_STATE_MAXIMIZED));
Bram Moolenaar98921892016-02-23 17:14:37 +01004899#endif
Bram Moolenaar09736232009-09-23 16:14:49 +00004900}
4901
4902/*
4903 * Unmaximize the main window
4904 */
4905 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004906gui_mch_unmaximize(void)
Bram Moolenaar09736232009-09-23 16:14:49 +00004907{
4908 if (gui.mainwin != NULL)
4909 gtk_window_unmaximize(GTK_WINDOW(gui.mainwin));
4910}
Bram Moolenaar09736232009-09-23 16:14:49 +00004911
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912/*
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004913 * Called when the font changed while the window is maximized. Compute the
4914 * new Rows and Columns. This is like resizing the window.
4915 */
4916 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004917gui_mch_newfont(void)
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004918{
4919 int w, h;
4920
4921 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
4922 w -= get_menu_tool_width();
4923 h -= get_menu_tool_height();
4924 gui_resize_shell(w, h);
4925}
4926
4927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 * Set the windows size.
4929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 void
4931gui_mch_set_shellsize(int width, int height,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004932 int min_width UNUSED, int min_height UNUSED,
4933 int base_width UNUSED, int base_height UNUSED,
4934 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 /* give GTK+ a chance to put all widget's into place */
4937 gui_mch_update();
4938
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004939 /* this will cause the proper resizement to happen too */
4940 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004941 update_window_manager_hints(0, 0);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004942
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 /* With GTK+ 2, changing the size of the form widget doesn't resize
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004944 * the window. So let's do it the other way around and resize the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 * main window instead. */
4946 width += get_menu_tool_width();
4947 height += get_menu_tool_height();
4948
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004949 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004950 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004951 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004952 update_window_manager_hints(width, height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
Bram Moolenaar98921892016-02-23 17:14:37 +01004954# if !GTK_CHECK_VERSION(3,0,0)
4955# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 if (!resize_idle_installed)
4957 {
4958 g_idle_add_full(GDK_PRIORITY_EVENTS + 10,
4959 &force_shell_resize_idle, NULL, NULL);
4960 resize_idle_installed = TRUE;
4961 }
Bram Moolenaar98921892016-02-23 17:14:37 +01004962# endif
4963# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 /*
4965 * Wait until all events are processed to prevent a crash because the
4966 * real size of the drawing area doesn't reflect Vim's internal ideas.
4967 *
4968 * This is a bit of a hack, since Vim is a terminal application with a GUI
4969 * on top, while the GUI expects to be the boss.
4970 */
4971 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972}
4973
4974
4975/*
4976 * The screen size is used to make sure the initial window doesn't get bigger
4977 * than the screen. This subtracts some room for menubar, toolbar and window
4978 * decorations.
4979 */
4980 void
4981gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
4982{
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02004983#if GTK_CHECK_VERSION(3,22,2)
Bram Moolenaara859f042016-11-17 19:11:55 +01004984 GdkRectangle rect;
4985 GdkMonitor * const mon = gdk_display_get_monitor_at_window(
4986 gtk_widget_get_display(gui.mainwin),
4987 gtk_widget_get_window(gui.mainwin));
4988 gdk_monitor_get_geometry(mon, &rect);
4989
4990 *screen_w = rect.width;
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02004991 /* Subtract 'guiheadroom' from the height to allow some room for the
4992 * window manager (task list and window title bar). */
Bram Moolenaara859f042016-11-17 19:11:55 +01004993 *screen_h = rect.height - p_ghr;
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02004994#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 GdkScreen* screen;
4996
4997 if (gui.mainwin != NULL && gtk_widget_has_screen(gui.mainwin))
4998 screen = gtk_widget_get_screen(gui.mainwin);
4999 else
5000 screen = gdk_screen_get_default();
5001
5002 *screen_w = gdk_screen_get_width(screen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 /* Subtract 'guiheadroom' from the height to allow some room for the
5004 * window manager (task list and window title bar). */
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02005005 *screen_h = gdk_screen_get_height(screen) - p_ghr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006#endif
5007
5008 /*
5009 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
5010 * the toolbar and menubar for GTK, we subtract them from the screen
Bram Moolenaar64404472010-06-26 06:24:45 +02005011 * height, so that the window size can be made to fit on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 * This should be completely changed later.
5013 */
5014 *screen_w -= get_menu_tool_width();
5015 *screen_h -= get_menu_tool_height();
5016}
5017
5018#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005020gui_mch_settitle(char_u *title, char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 if (title != NULL && output_conv.vc_type != CONV_NONE)
5023 title = string_convert(&output_conv, title, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024
5025 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
5026
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 if (output_conv.vc_type != CONV_NONE)
5028 vim_free(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029}
5030#endif /* FEAT_TITLE */
5031
5032#if defined(FEAT_MENU) || defined(PROTO)
5033 void
5034gui_mch_enable_menu(int showit)
5035{
5036 GtkWidget *widget;
5037
5038# ifdef FEAT_GUI_GNOME
5039 if (using_gnome)
5040 widget = gui.menubar_h;
5041 else
5042# endif
5043 widget = gui.menubar;
5044
Bram Moolenaar18144c82006-04-12 21:52:12 +00005045 /* Do not disable the menu while starting up, otherwise F10 doesn't work. */
Bram Moolenaar98921892016-02-23 17:14:37 +01005046# if GTK_CHECK_VERSION(3,0,0)
5047 if (!showit != !gtk_widget_get_visible(widget) && !gui.starting)
5048# else
Bram Moolenaar18144c82006-04-12 21:52:12 +00005049 if (!showit != !GTK_WIDGET_VISIBLE(widget) && !gui.starting)
Bram Moolenaar98921892016-02-23 17:14:37 +01005050# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 {
5052 if (showit)
5053 gtk_widget_show(widget);
5054 else
5055 gtk_widget_hide(widget);
5056
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00005057 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 }
5059}
5060#endif /* FEAT_MENU */
5061
5062#if defined(FEAT_TOOLBAR) || defined(PROTO)
5063 void
5064gui_mch_show_toolbar(int showit)
5065{
5066 GtkWidget *widget;
5067
5068 if (gui.toolbar == NULL)
5069 return;
5070
5071# ifdef FEAT_GUI_GNOME
5072 if (using_gnome)
5073 widget = gui.toolbar_h;
5074 else
5075# endif
5076 widget = gui.toolbar;
5077
5078 if (showit)
5079 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
5080
Bram Moolenaar98921892016-02-23 17:14:37 +01005081# if GTK_CHECK_VERSION(3,0,0)
5082 if (!showit != !gtk_widget_get_visible(widget))
5083# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 if (!showit != !GTK_WIDGET_VISIBLE(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01005085# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 {
5087 if (showit)
5088 gtk_widget_show(widget);
5089 else
5090 gtk_widget_hide(widget);
5091
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00005092 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 }
5094}
5095#endif /* FEAT_TOOLBAR */
5096
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097/*
5098 * Check if a given font is a CJK font. This is done in a very crude manner. It
5099 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
5100 * font. Consequently, this function cannot be used as a general purpose check
5101 * for CJK-ness for which fontconfig APIs should be used. This is only used by
5102 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
5103 */
5104 static int
5105is_cjk_font(PangoFontDescription *font_desc)
5106{
5107 static const char * const cjk_langs[] =
5108 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
5109
5110 PangoFont *font;
5111 unsigned i;
5112 int is_cjk = FALSE;
5113
5114 font = pango_context_load_font(gui.text_context, font_desc);
5115
5116 if (font == NULL)
5117 return FALSE;
5118
5119 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i)
5120 {
5121 PangoCoverage *coverage;
5122 gunichar uc;
5123
5124 coverage = pango_font_get_coverage(
5125 font, pango_language_from_string(cjk_langs[i]));
5126
5127 if (coverage != NULL)
5128 {
5129 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00;
5130 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT);
5131 pango_coverage_unref(coverage);
5132 }
5133 }
5134
5135 g_object_unref(font);
5136
5137 return is_cjk;
5138}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139
Bram Moolenaar231334e2005-07-25 20:46:57 +00005140/*
5141 * Adjust gui.char_height (after 'linespace' was changed).
5142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 int
Bram Moolenaar231334e2005-07-25 20:46:57 +00005144gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 PangoFontMetrics *metrics;
5147 int ascent;
5148 int descent;
5149
5150 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font,
5151 pango_context_get_language(gui.text_context));
5152 ascent = pango_font_metrics_get_ascent(metrics);
5153 descent = pango_font_metrics_get_descent(metrics);
5154
5155 pango_font_metrics_unref(metrics);
5156
5157 gui.char_height = (ascent + descent + PANGO_SCALE - 1) / PANGO_SCALE
Bram Moolenaar231334e2005-07-25 20:46:57 +00005158 + p_linespace;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005159 /* LINTED: avoid warning: bitwise operation on signed value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2);
5161
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 /* A not-positive value of char_height may crash Vim. Only happens
5163 * if 'linespace' is negative (which does make sense sometimes). */
5164 gui.char_ascent = MAX(gui.char_ascent, 0);
5165 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
5166
5167 return OK;
5168}
5169
Bram Moolenaar98921892016-02-23 17:14:37 +01005170#if GTK_CHECK_VERSION(3,0,0)
5171/* Callback function used in gui_mch_font_dialog() */
5172 static gboolean
5173font_filter(const PangoFontFamily *family,
5174 const PangoFontFace *face UNUSED,
5175 gpointer data UNUSED)
5176{
5177 return pango_font_family_is_monospace((PangoFontFamily *)family);
5178}
5179#endif
5180
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181/*
5182 * Put up a font dialog and return the selected font name in allocated memory.
5183 * "oldval" is the previous value. Return NULL when cancelled.
5184 * This should probably go into gui_gtk.c. Hmm.
5185 * FIXME:
5186 * The GTK2 font selection dialog has no filtering API. So we could either
5187 * a) implement our own (possibly copying the code from somewhere else) or
5188 * b) just live with it.
5189 */
5190 char_u *
5191gui_mch_font_dialog(char_u *oldval)
5192{
5193 GtkWidget *dialog;
5194 int response;
5195 char_u *fontname = NULL;
5196 char_u *oldname;
5197
Bram Moolenaar98921892016-02-23 17:14:37 +01005198#if GTK_CHECK_VERSION(3,2,0)
5199 dialog = gtk_font_chooser_dialog_new(NULL, NULL);
5200 gtk_font_chooser_set_filter_func(GTK_FONT_CHOOSER(dialog), font_filter,
5201 NULL, NULL);
5202#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 dialog = gtk_font_selection_dialog_new(NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01005204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205
5206 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
5207 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
5208
5209 if (oldval != NULL && oldval[0] != NUL)
5210 {
5211 if (output_conv.vc_type != CONV_NONE)
5212 oldname = string_convert(&output_conv, oldval, NULL);
5213 else
5214 oldname = oldval;
5215
5216 /* Annoying bug in GTK (or Pango): if the font name does not include a
5217 * size, zero is used. Use default point size ten. */
5218 if (!vim_isdigit(oldname[STRLEN(oldname) - 1]))
5219 {
5220 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3);
5221
5222 if (p != NULL)
5223 {
5224 STRCPY(p + STRLEN(p), " 10");
5225 if (oldname != oldval)
5226 vim_free(oldname);
5227 oldname = p;
5228 }
5229 }
5230
Bram Moolenaar98921892016-02-23 17:14:37 +01005231#if GTK_CHECK_VERSION(3,2,0)
5232 gtk_font_chooser_set_font(
5233 GTK_FONT_CHOOSER(dialog), (const gchar *)oldname);
5234#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 gtk_font_selection_dialog_set_font_name(
5236 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname);
Bram Moolenaar98921892016-02-23 17:14:37 +01005237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
5239 if (oldname != oldval)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005240 vim_free(oldname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 }
Bram Moolenaarbef9d832009-09-11 13:46:41 +00005242 else
Bram Moolenaar98921892016-02-23 17:14:37 +01005243#if GTK_CHECK_VERSION(3,2,0)
5244 gtk_font_chooser_set_font(
5245 GTK_FONT_CHOOSER(dialog), DEFAULT_FONT);
5246#else
Bram Moolenaarbef9d832009-09-11 13:46:41 +00005247 gtk_font_selection_dialog_set_font_name(
5248 GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT);
Bram Moolenaar98921892016-02-23 17:14:37 +01005249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250
5251 response = gtk_dialog_run(GTK_DIALOG(dialog));
5252
5253 if (response == GTK_RESPONSE_OK)
5254 {
5255 char *name;
5256
Bram Moolenaar98921892016-02-23 17:14:37 +01005257#if GTK_CHECK_VERSION(3,2,0)
5258 name = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(dialog));
5259#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 name = gtk_font_selection_dialog_get_font_name(
5261 GTK_FONT_SELECTION_DIALOG(dialog));
Bram Moolenaar98921892016-02-23 17:14:37 +01005262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 if (name != NULL)
5264 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005265 char_u *p;
5266
5267 /* Apparently some font names include a comma, need to escape
5268 * that, because in 'guifont' it separates names. */
5269 p = vim_strsave_escaped((char_u *)name, (char_u *)",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 g_free(name);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005271 if (p != NULL && input_conv.vc_type != CONV_NONE)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005272 {
5273 fontname = string_convert(&input_conv, p, NULL);
5274 vim_free(p);
5275 }
5276 else
5277 fontname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 }
5279 }
5280
5281 if (response != GTK_RESPONSE_NONE)
5282 gtk_widget_destroy(dialog);
5283
5284 return fontname;
5285}
5286
5287/*
5288 * Some monospace fonts don't support a bold weight, and fall back
5289 * silently to the regular weight. But this is no good since our text
5290 * drawing function can emulate bold by overstriking. So let's try
5291 * to detect whether bold weight is actually available and emulate it
5292 * otherwise.
5293 *
5294 * Note that we don't need to check for italic style since Xft can
5295 * emulate italic on its own, provided you have a proper fontconfig
5296 * setup. We wouldn't be able to emulate it in Vim anyway.
5297 */
5298 static void
5299get_styled_font_variants(void)
5300{
5301 PangoFontDescription *bold_font_desc;
5302 PangoFont *plain_font;
5303 PangoFont *bold_font;
5304
5305 gui.font_can_bold = FALSE;
5306
5307 plain_font = pango_context_load_font(gui.text_context, gui.norm_font);
5308
5309 if (plain_font == NULL)
5310 return;
5311
5312 bold_font_desc = pango_font_description_copy_static(gui.norm_font);
5313 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD);
5314
5315 bold_font = pango_context_load_font(gui.text_context, bold_font_desc);
5316 /*
5317 * The comparison relies on the unique handle nature of a PangoFont*,
5318 * i.e. it's assumed that a different PangoFont* won't refer to the
5319 * same font. Seems to work, and failing here isn't critical anyway.
5320 */
5321 if (bold_font != NULL)
5322 {
5323 gui.font_can_bold = (bold_font != plain_font);
5324 g_object_unref(bold_font);
5325 }
5326
5327 pango_font_description_free(bold_font_desc);
5328 g_object_unref(plain_font);
5329}
5330
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331static PangoEngineShape *default_shape_engine = NULL;
5332
5333/*
5334 * Create a map from ASCII characters in the range [32,126] to glyphs
5335 * of the current font. This is used by gui_gtk2_draw_string() to skip
5336 * the itemize and shaping process for the most common case.
5337 */
5338 static void
5339ascii_glyph_table_init(void)
5340{
Bram Moolenaar16350cb2016-08-14 20:27:34 +02005341 char_u ascii_chars[2 * 128];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 PangoAttrList *attr_list;
5343 GList *item_list;
5344 int i;
5345
5346 if (gui.ascii_glyphs != NULL)
5347 pango_glyph_string_free(gui.ascii_glyphs);
5348 if (gui.ascii_font != NULL)
5349 g_object_unref(gui.ascii_font);
5350
5351 gui.ascii_glyphs = NULL;
5352 gui.ascii_font = NULL;
5353
Bram Moolenaar16350cb2016-08-14 20:27:34 +02005354 /* For safety, fill in question marks for the control characters.
5355 * Put a space between characters to avoid shaping. */
5356 for (i = 0; i < 128; ++i)
5357 {
5358 if (i >= 32 && i < 127)
5359 ascii_chars[2 * i] = i;
5360 else
5361 ascii_chars[2 * i] = '?';
5362 ascii_chars[2 * i + 1] = ' ';
5363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364
5365 attr_list = pango_attr_list_new();
5366 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars,
5367 0, sizeof(ascii_chars), attr_list, NULL);
5368
5369 if (item_list != NULL && item_list->next == NULL) /* play safe */
5370 {
5371 PangoItem *item;
5372 int width;
5373
5374 item = (PangoItem *)item_list->data;
5375 width = gui.char_width * PANGO_SCALE;
5376
5377 /* Remember the shape engine used for ASCII. */
5378 default_shape_engine = item->analysis.shape_engine;
5379
5380 gui.ascii_font = item->analysis.font;
5381 g_object_ref(gui.ascii_font);
5382
5383 gui.ascii_glyphs = pango_glyph_string_new();
5384
5385 pango_shape((const char *)ascii_chars, sizeof(ascii_chars),
5386 &item->analysis, gui.ascii_glyphs);
5387
5388 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars));
5389
5390 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i)
5391 {
5392 PangoGlyphGeometry *geom;
5393
5394 geom = &gui.ascii_glyphs->glyphs[i].geometry;
5395 geom->x_offset += MAX(0, width - geom->width) / 2;
5396 geom->width = width;
5397 }
5398 }
5399
5400 g_list_foreach(item_list, (GFunc)&pango_item_free, NULL);
5401 g_list_free(item_list);
5402 pango_attr_list_unref(attr_list);
5403}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404
5405/*
5406 * Initialize Vim to use the font or fontset with the given name.
5407 * Return FAIL if the font could not be loaded, OK otherwise.
5408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 int
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005410gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 PangoFontDescription *font_desc;
5413 PangoLayout *layout;
5414 int width;
5415
5416 /* If font_name is NULL, this means to use the default, which should
5417 * be present on all proper Pango/fontconfig installations. */
5418 if (font_name == NULL)
5419 font_name = (char_u *)DEFAULT_FONT;
5420
5421 font_desc = gui_mch_get_font(font_name, FALSE);
5422
5423 if (font_desc == NULL)
5424 return FAIL;
5425
5426 gui_mch_free_font(gui.norm_font);
5427 gui.norm_font = font_desc;
5428
5429 pango_context_set_font_description(gui.text_context, font_desc);
5430
5431 layout = pango_layout_new(gui.text_context);
5432 pango_layout_set_text(layout, "MW", 2);
5433 pango_layout_get_size(layout, &width, NULL);
5434 /*
5435 * Set char_width to half the width obtained from pango_layout_get_size()
5436 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
5437 * Pango to use the same width for both non-CJK characters (e.g. Latin
5438 * letters and numbers) and CJK characters. This results in 's p a c e d
5439 * o u t' rendering when a CJK 'fixed width' font is used. To work around
5440 * that, divide the width returned by Pango by 2 if cjk_width is equal to
5441 * width for CJK fonts.
5442 *
5443 * For related bugs, see:
5444 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
5445 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
5446 *
5447 * With this, for all four of the following cases, Vim works fine:
5448 * guifont=CJK_fixed_width_font
5449 * guifont=Non_CJK_fixed_font
5450 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
5451 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
5452 */
5453 if (is_cjk_font(gui.norm_font))
5454 {
5455 int cjk_width;
5456
5457 /* Measure the text extent of U+4E00 and U+4E8C */
5458 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1);
5459 pango_layout_get_size(layout, &cjk_width, NULL);
5460
5461 if (width == cjk_width) /* Xft not patched */
5462 width /= 2;
5463 }
5464 g_object_unref(layout);
5465
5466 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE;
5467
5468 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
5469 if (gui.char_width <= 0)
5470 gui.char_width = 8;
5471
Bram Moolenaar231334e2005-07-25 20:46:57 +00005472 gui_mch_adjust_charheight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473
5474 /* Set the fontname, which will be used for information purposes */
5475 hl_set_font_name(font_name);
5476
5477 get_styled_font_variants();
5478 ascii_glyph_table_init();
5479
5480 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
5481 if (gui.wide_font != NULL
5482 && pango_font_description_equal(gui.norm_font, gui.wide_font))
5483 {
5484 pango_font_description_free(gui.wide_font);
5485 gui.wide_font = NULL;
5486 }
5487
Bram Moolenaare161c792009-11-03 17:13:59 +00005488 if (gui_mch_maximized())
5489 {
Bram Moolenaare161c792009-11-03 17:13:59 +00005490 /* Update lines and columns in accordance with the new font, keep the
5491 * window maximized. */
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02005492 gui_mch_newfont();
Bram Moolenaare161c792009-11-03 17:13:59 +00005493 }
5494 else
Bram Moolenaare161c792009-11-03 17:13:59 +00005495 {
5496 /* Preserve the logical dimensions of the screen. */
5497 update_window_manager_hints(0, 0);
5498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499
5500 return OK;
5501}
5502
5503/*
5504 * Get a reference to the font "name".
5505 * Return zero for failure.
5506 */
5507 GuiFont
5508gui_mch_get_font(char_u *name, int report_error)
5509{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 PangoFontDescription *font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511
5512 /* can't do this when GUI is not running */
5513 if (!gui.in_use || name == NULL)
5514 return NULL;
5515
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 if (output_conv.vc_type != CONV_NONE)
5517 {
5518 char_u *buf;
5519
5520 buf = string_convert(&output_conv, name, NULL);
5521 if (buf != NULL)
5522 {
5523 font = pango_font_description_from_string((const char *)buf);
5524 vim_free(buf);
5525 }
5526 else
5527 font = NULL;
5528 }
5529 else
5530 font = pango_font_description_from_string((const char *)name);
5531
5532 if (font != NULL)
5533 {
5534 PangoFont *real_font;
5535
5536 /* pango_context_load_font() bails out if no font size is set */
5537 if (pango_font_description_get_size(font) <= 0)
5538 pango_font_description_set_size(font, 10 * PANGO_SCALE);
5539
5540 real_font = pango_context_load_font(gui.text_context, font);
5541
5542 if (real_font == NULL)
5543 {
5544 pango_font_description_free(font);
5545 font = NULL;
5546 }
5547 else
5548 g_object_unref(real_font);
5549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550
5551 if (font == NULL)
5552 {
5553 if (report_error)
Bram Moolenaarc93e7912008-07-08 10:46:08 +00005554 EMSG2(_((char *)e_font), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 return NULL;
5556 }
5557
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 return font;
5559}
5560
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005561#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005562/*
5563 * Return the name of font "font" in allocated memory.
5564 */
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005565 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005566gui_mch_get_fontname(GuiFont font, char_u *name UNUSED)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005567{
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005568 if (font != NOFONT)
5569 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005570 char *pangoname = pango_font_description_to_string(font);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005571
Bram Moolenaar89d40322006-08-29 15:30:07 +00005572 if (pangoname != NULL)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005573 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005574 char_u *s = vim_strsave((char_u *)pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005575
Bram Moolenaar89d40322006-08-29 15:30:07 +00005576 g_free(pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005577 return s;
5578 }
5579 }
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005580 return NULL;
5581}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005582#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005583
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584/*
5585 * If a font is not going to be used, free its structure.
5586 */
5587 void
5588gui_mch_free_font(GuiFont font)
5589{
5590 if (font != NOFONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 pango_font_description_free(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592}
5593
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594/*
Bram Moolenaar36edf062016-07-21 22:10:12 +02005595 * Return the Pixel value (color) for the given color name.
5596 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 * Return INVALCOLOR for error.
5598 */
5599 guicolor_T
5600gui_mch_get_color(char_u *name)
5601{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 if (!gui.in_use) /* can't do this when GUI not running */
5603 return INVALCOLOR;
5604
Bram Moolenaar98921892016-02-23 17:14:37 +01005605#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02005606 return name != NULL ? gui_get_color_cmn(name) : INVALCOLOR;
Bram Moolenaar98921892016-02-23 17:14:37 +01005607#else
Bram Moolenaar36edf062016-07-21 22:10:12 +02005608 guicolor_T color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609
Bram Moolenaar36edf062016-07-21 22:10:12 +02005610 color = (name != NULL) ? gui_get_color_cmn(name) : INVALCOLOR;
5611 if (color == INVALCOLOR)
5612 return INVALCOLOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
Bram Moolenaar26af85d2017-07-23 16:45:10 +02005614 return gui_mch_get_rgb_color(
5615 (color & 0xff0000) >> 16,
5616 (color & 0xff00) >> 8,
5617 color & 0xff);
5618#endif
5619}
5620
5621/*
5622 * Return the Pixel value (color) for the given RGB values.
5623 * Return INVALCOLOR for error.
5624 */
5625 guicolor_T
5626gui_mch_get_rgb_color(int r, int g, int b)
5627{
5628#if GTK_CHECK_VERSION(3,0,0)
5629 return gui_get_rgb_color_cmn(r, g, b);
5630#else
5631 GdkColor gcolor;
5632 int ret;
5633
5634 gcolor.red = (guint16)(r / 255.0 * 65535 + 0.5);
5635 gcolor.green = (guint16)(g / 255.0 * 65535 + 0.5);
5636 gcolor.blue = (guint16)(b / 255.0 * 65535 + 0.5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637
Bram Moolenaar36edf062016-07-21 22:10:12 +02005638 ret = gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea),
5639 &gcolor, FALSE, TRUE);
5640
5641 return ret != 0 ? (guicolor_T)gcolor.pixel : INVALCOLOR;
5642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643}
5644
5645/*
5646 * Set the current text foreground color.
5647 */
5648 void
5649gui_mch_set_fg_color(guicolor_T color)
5650{
Bram Moolenaar36edf062016-07-21 22:10:12 +02005651#if GTK_CHECK_VERSION(3,0,0)
5652 *gui.fgcolor = color_to_rgba(color);
5653#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 gui.fgcolor->pixel = (unsigned long)color;
Bram Moolenaar36edf062016-07-21 22:10:12 +02005655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656}
5657
5658/*
5659 * Set the current text background color.
5660 */
5661 void
5662gui_mch_set_bg_color(guicolor_T color)
5663{
Bram Moolenaar36edf062016-07-21 22:10:12 +02005664#if GTK_CHECK_VERSION(3,0,0)
5665 *gui.bgcolor = color_to_rgba(color);
5666#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 gui.bgcolor->pixel = (unsigned long)color;
Bram Moolenaar36edf062016-07-21 22:10:12 +02005668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669}
5670
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005671/*
5672 * Set the current text special color.
5673 */
5674 void
5675gui_mch_set_sp_color(guicolor_T color)
5676{
Bram Moolenaar36edf062016-07-21 22:10:12 +02005677#if GTK_CHECK_VERSION(3,0,0)
5678 *gui.spcolor = color_to_rgba(color);
5679#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005680 gui.spcolor->pixel = (unsigned long)color;
Bram Moolenaar36edf062016-07-21 22:10:12 +02005681#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005682}
5683
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684/*
5685 * Function-like convenience macro for the sake of efficiency.
5686 */
5687#define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
5688 G_STMT_START{ \
5689 PangoAttribute *tmp_attr_; \
5690 tmp_attr_ = (Attribute); \
5691 tmp_attr_->start_index = (Start); \
5692 tmp_attr_->end_index = (End); \
5693 pango_attr_list_insert((AttrList), tmp_attr_); \
5694 }G_STMT_END
5695
5696 static void
5697apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list)
5698{
5699 char_u *start = NULL;
5700 char_u *p;
5701 int uc;
5702
5703 for (p = s; p < s + len; p += utf_byte2len(*p))
5704 {
5705 uc = utf_ptr2char(p);
5706
5707 if (start == NULL)
5708 {
5709 if (uc >= 0x80 && utf_char2cells(uc) == 2)
5710 start = p;
5711 }
5712 else if (uc < 0x80 /* optimization shortcut */
5713 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc)))
5714 {
5715 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5716 attr_list, start - s, p - s);
5717 start = NULL;
5718 }
5719 }
5720
5721 if (start != NULL)
5722 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5723 attr_list, start - s, len);
5724}
5725
5726 static int
5727count_cluster_cells(char_u *s, PangoItem *item,
5728 PangoGlyphString* glyphs, int i,
5729 int *cluster_width,
5730 int *last_glyph_rbearing)
5731{
5732 char_u *p;
5733 int next; /* glyph start index of next cluster */
5734 int start, end; /* string segment of current cluster */
5735 int width; /* real cluster width in Pango units */
5736 int uc;
5737 int cellcount = 0;
5738
5739 width = glyphs->glyphs[i].geometry.width;
5740
5741 for (next = i + 1; next < glyphs->num_glyphs; ++next)
5742 {
5743 if (glyphs->glyphs[next].attr.is_cluster_start)
5744 break;
5745 else if (glyphs->glyphs[next].geometry.width > width)
5746 width = glyphs->glyphs[next].geometry.width;
5747 }
5748
5749 start = item->offset + glyphs->log_clusters[i];
5750 end = item->offset + ((next < glyphs->num_glyphs) ?
5751 glyphs->log_clusters[next] : item->length);
5752
5753 for (p = s + start; p < s + end; p += utf_byte2len(*p))
5754 {
5755 uc = utf_ptr2char(p);
5756 if (uc < 0x80)
5757 ++cellcount;
5758 else if (!utf_iscomposing(uc))
5759 cellcount += utf_char2cells(uc);
5760 }
5761
5762 if (last_glyph_rbearing != NULL
5763 && cellcount > 0 && next == glyphs->num_glyphs)
5764 {
5765 PangoRectangle ink_rect;
5766 /*
5767 * If a certain combining mark had to be taken from a non-monospace
5768 * font, we have to compensate manually by adapting x_offset according
5769 * to the ink extents of the previous glyph.
5770 */
5771 pango_font_get_glyph_extents(item->analysis.font,
5772 glyphs->glyphs[i].glyph,
5773 &ink_rect, NULL);
5774
5775 if (PANGO_RBEARING(ink_rect) > 0)
5776 *last_glyph_rbearing = PANGO_RBEARING(ink_rect);
5777 }
5778
5779 if (cellcount > 0)
5780 *cluster_width = width;
5781
5782 return cellcount;
5783}
5784
5785/*
5786 * If there are only combining characters in the cluster, we cannot just
5787 * change the width of the previous glyph since there is none. Therefore
5788 * some guesswork is needed.
5789 *
5790 * If ink_rect.x is negative Pango apparently has taken care of the composing
5791 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
5792 * to problems with composing from different fonts we still need to fine-tune
Bram Moolenaar64404472010-06-26 06:24:45 +02005793 * x_offset to avoid ugliness.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 *
5795 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
5796 * the position of the previous glyph. Apparently this happens only with old
5797 * X fonts which don't provide the special combining information needed by
5798 * Pango.
5799 */
5800 static void
5801setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph,
5802 int last_cellcount, int last_cluster_width,
5803 int last_glyph_rbearing)
5804{
5805 PangoRectangle ink_rect;
5806 PangoRectangle logical_rect;
5807 int width;
5808
5809 width = last_cellcount * gui.char_width * PANGO_SCALE;
5810 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2;
5811 glyph->geometry.width = 0;
5812
5813 pango_font_get_glyph_extents(item->analysis.font,
5814 glyph->glyph,
5815 &ink_rect, &logical_rect);
5816 if (ink_rect.x < 0)
5817 {
5818 glyph->geometry.x_offset += last_glyph_rbearing;
5819 glyph->geometry.y_offset = logical_rect.height
5820 - (gui.char_height - p_linespace) * PANGO_SCALE;
5821 }
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005822 else
5823 /* If the accent width is smaller than the cluster width, position it
5824 * in the middle. */
5825 glyph->geometry.x_offset = -width + MAX(0, width - ink_rect.width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826}
5827
Bram Moolenaar98921892016-02-23 17:14:37 +01005828#if GTK_CHECK_VERSION(3,0,0)
5829 static void
5830draw_glyph_string(int row, int col, int num_cells, int flags,
5831 PangoFont *font, PangoGlyphString *glyphs,
5832 cairo_t *cr)
5833#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 static void
5835draw_glyph_string(int row, int col, int num_cells, int flags,
5836 PangoFont *font, PangoGlyphString *glyphs)
Bram Moolenaar98921892016-02-23 17:14:37 +01005837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838{
5839 if (!(flags & DRAW_TRANSP))
5840 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005841#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02005842 cairo_set_source_rgba(cr,
5843 gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue,
5844 gui.bgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005845 cairo_rectangle(cr,
5846 FILL_X(col), FILL_Y(row),
5847 num_cells * gui.char_width, gui.char_height);
5848 cairo_fill(cr);
5849#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
5851
5852 gdk_draw_rectangle(gui.drawarea->window,
5853 gui.text_gc,
5854 TRUE,
5855 FILL_X(col),
5856 FILL_Y(row),
5857 num_cells * gui.char_width,
5858 gui.char_height);
Bram Moolenaar98921892016-02-23 17:14:37 +01005859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 }
5861
Bram Moolenaar98921892016-02-23 17:14:37 +01005862#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02005863 cairo_set_source_rgba(cr,
5864 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5865 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005866 cairo_move_to(cr, TEXT_X(col), TEXT_Y(row));
5867 pango_cairo_show_glyph_string(cr, font, glyphs);
5868#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5870
5871 gdk_draw_glyphs(gui.drawarea->window,
5872 gui.text_gc,
5873 font,
5874 TEXT_X(col),
5875 TEXT_Y(row),
5876 glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01005877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878
5879 /* redraw the contents with an offset of 1 to emulate bold */
5880 if ((flags & DRAW_BOLD) && !gui.font_can_bold)
Bram Moolenaar98921892016-02-23 17:14:37 +01005881#if GTK_CHECK_VERSION(3,0,0)
5882 {
Bram Moolenaar36edf062016-07-21 22:10:12 +02005883 cairo_set_source_rgba(cr,
5884 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5885 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005886 cairo_move_to(cr, TEXT_X(col) + 1, TEXT_Y(row));
5887 pango_cairo_show_glyph_string(cr, font, glyphs);
5888 }
5889#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 gdk_draw_glyphs(gui.drawarea->window,
5891 gui.text_gc,
5892 font,
5893 TEXT_X(col) + 1,
5894 TEXT_Y(row),
5895 glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01005896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897}
5898
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005899/*
5900 * Draw underline and undercurl at the bottom of the character cell.
5901 */
Bram Moolenaar98921892016-02-23 17:14:37 +01005902#if GTK_CHECK_VERSION(3,0,0)
5903 static void
5904draw_under(int flags, int row, int col, int cells, cairo_t *cr)
5905#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005906 static void
5907draw_under(int flags, int row, int col, int cells)
Bram Moolenaar98921892016-02-23 17:14:37 +01005908#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005909{
5910 int i;
5911 int offset;
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005912 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005913 int y = FILL_Y(row + 1) - 1;
5914
5915 /* Undercurl: draw curl at the bottom of the character cell. */
5916 if (flags & DRAW_UNDERC)
5917 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005918#if GTK_CHECK_VERSION(3,0,0)
5919 cairo_set_line_width(cr, 1.0);
5920 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
Bram Moolenaar36edf062016-07-21 22:10:12 +02005921 cairo_set_source_rgba(cr,
5922 gui.spcolor->red, gui.spcolor->green, gui.spcolor->blue,
5923 gui.spcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005924 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
5925 {
5926 offset = val[i % 8];
5927 cairo_line_to(cr, i, y - offset + 0.5);
5928 }
5929 cairo_stroke(cr);
5930#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005931 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
5932 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
5933 {
5934 offset = val[i % 8];
5935 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset);
5936 }
5937 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar98921892016-02-23 17:14:37 +01005938#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005939 }
5940
5941 /* Underline: draw a line at the bottom of the character cell. */
5942 if (flags & DRAW_UNDERL)
5943 {
5944 /* When p_linespace is 0, overwrite the bottom row of pixels.
5945 * Otherwise put the line just below the character. */
5946 if (p_linespace > 1)
5947 y -= p_linespace - 1;
Bram Moolenaar98921892016-02-23 17:14:37 +01005948#if GTK_CHECK_VERSION(3,0,0)
5949 {
5950 cairo_set_line_width(cr, 1.0);
5951 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
Bram Moolenaar36edf062016-07-21 22:10:12 +02005952 cairo_set_source_rgba(cr,
5953 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5954 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005955 cairo_move_to(cr, FILL_X(col), y + 0.5);
5956 cairo_line_to(cr, FILL_X(col + cells), y + 0.5);
5957 cairo_stroke(cr);
5958 }
5959#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005960 gdk_draw_line(gui.drawarea->window, gui.text_gc,
5961 FILL_X(col), y,
5962 FILL_X(col + cells) - 1, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01005963#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005964 }
5965}
5966
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 int
5968gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
5969{
5970 GdkRectangle area; /* area for clip mask */
5971 PangoGlyphString *glyphs; /* glyphs of current item */
5972 int column_offset = 0; /* column offset in cells */
5973 int i;
5974 char_u *conv_buf = NULL; /* result of UTF-8 conversion */
5975 char_u *new_conv_buf;
5976 int convlen;
5977 char_u *sp, *bp;
5978 int plen;
Bram Moolenaar98921892016-02-23 17:14:37 +01005979#if GTK_CHECK_VERSION(3,0,0)
5980 cairo_t *cr;
5981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982
Bram Moolenaar98921892016-02-23 17:14:37 +01005983#if GTK_CHECK_VERSION(3,0,0)
5984 if (gui.text_context == NULL || gtk_widget_get_window(gui.drawarea) == NULL)
5985#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 if (gui.text_context == NULL || gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01005987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 return len;
5989
5990 if (output_conv.vc_type != CONV_NONE)
5991 {
5992 /*
5993 * Convert characters from 'encoding' to 'termencoding', which is set
5994 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
5995 * prohibits changing this to something else than UTF-8 if the GUI is
5996 * in use.
5997 */
5998 convlen = len;
5999 conv_buf = string_convert(&output_conv, s, &convlen);
6000 g_return_val_if_fail(conv_buf != NULL, len);
6001
6002 /* Correct for differences in char width: some chars are
6003 * double-wide in 'encoding' but single-wide in utf-8. Add a space to
6004 * compensate for that. */
6005 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; )
6006 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006007 plen = utf_ptr2len(bp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1)
6009 {
6010 new_conv_buf = alloc(convlen + 2);
6011 if (new_conv_buf == NULL)
6012 return len;
6013 plen += bp - conv_buf;
6014 mch_memmove(new_conv_buf, conv_buf, plen);
6015 new_conv_buf[plen] = ' ';
6016 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen,
6017 convlen - plen + 1);
6018 vim_free(conv_buf);
6019 conv_buf = new_conv_buf;
6020 ++convlen;
6021 bp = conv_buf + plen;
6022 plen = 1;
6023 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006024 sp += (*mb_ptr2len)(sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 bp += plen;
6026 }
6027 s = conv_buf;
6028 len = convlen;
6029 }
6030
6031 /*
6032 * Restrict all drawing to the current screen line in order to prevent
6033 * fuzzy font lookups from messing up the screen.
6034 */
6035 area.x = gui.border_offset;
6036 area.y = FILL_Y(row);
6037 area.width = gui.num_cols * gui.char_width;
6038 area.height = gui.char_height;
6039
Bram Moolenaar98921892016-02-23 17:14:37 +01006040#if GTK_CHECK_VERSION(3,0,0)
6041 cr = cairo_create(gui.surface);
6042 cairo_rectangle(cr, area.x, area.y, area.width, area.height);
6043 cairo_clip(cr);
6044#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 gdk_gc_set_clip_origin(gui.text_gc, 0, 0);
6046 gdk_gc_set_clip_rectangle(gui.text_gc, &area);
Bram Moolenaar98921892016-02-23 17:14:37 +01006047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048
6049 glyphs = pango_glyph_string_new();
6050
6051 /*
6052 * Optimization hack: If possible, skip the itemize and shaping process
6053 * for pure ASCII strings. This optimization is particularly effective
6054 * because Vim draws space characters to clear parts of the screen.
6055 */
6056 if (!(flags & DRAW_ITALIC)
6057 && !((flags & DRAW_BOLD) && gui.font_can_bold)
6058 && gui.ascii_glyphs != NULL)
6059 {
6060 char_u *p;
6061
6062 for (p = s; p < s + len; ++p)
6063 if (*p & 0x80)
6064 goto not_ascii;
6065
6066 pango_glyph_string_set_size(glyphs, len);
6067
6068 for (i = 0; i < len; ++i)
6069 {
Bram Moolenaar16350cb2016-08-14 20:27:34 +02006070 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[2 * s[i]];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 glyphs->log_clusters[i] = i;
6072 }
6073
Bram Moolenaar98921892016-02-23 17:14:37 +01006074#if GTK_CHECK_VERSION(3,0,0)
6075 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs, cr);
6076#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01006078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079
6080 column_offset = len;
6081 }
6082 else
6083not_ascii:
6084 {
6085 PangoAttrList *attr_list;
6086 GList *item_list;
6087 int cluster_width;
6088 int last_glyph_rbearing;
6089 int cells = 0; /* cells occupied by current cluster */
6090
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006091 /* Safety check: pango crashes when invoked with invalid utf-8
6092 * characters. */
6093 if (!utf_valid_string(s, s + len))
6094 {
6095 column_offset = len;
6096 goto skipitall;
6097 }
6098
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 /* original width of the current cluster */
6100 cluster_width = PANGO_SCALE * gui.char_width;
6101
6102 /* right bearing of the last non-composing glyph */
6103 last_glyph_rbearing = PANGO_SCALE * gui.char_width;
6104
6105 attr_list = pango_attr_list_new();
6106
6107 /* If 'guifontwide' is set then use that for double-width characters.
6108 * Otherwise just go with 'guifont' and let Pango do its thing. */
6109 if (gui.wide_font != NULL)
6110 apply_wide_font_attr(s, len, attr_list);
6111
6112 if ((flags & DRAW_BOLD) && gui.font_can_bold)
6113 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD),
6114 attr_list, 0, len);
6115 if (flags & DRAW_ITALIC)
6116 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC),
6117 attr_list, 0, len);
6118 /*
6119 * Break the text into segments with consistent directional level
6120 * and shaping engine. Pure Latin text needs only a single segment,
6121 * so there's no need to worry about the loop's efficiency. Better
6122 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
6123 */
6124 item_list = pango_itemize(gui.text_context,
6125 (const char *)s, 0, len, attr_list, NULL);
6126
6127 while (item_list != NULL)
6128 {
6129 PangoItem *item;
6130 int item_cells = 0; /* item length in cells */
6131
6132 item = (PangoItem *)item_list->data;
6133 item_list = g_list_delete_link(item_list, item_list);
6134 /*
6135 * Increment the bidirectional embedding level by 1 if it is not
6136 * even. An odd number means the output will be RTL, but we don't
6137 * want that since Vim handles right-to-left text on its own. It
6138 * would probably be sufficient to just set level = 0, but you can
6139 * never know :)
6140 *
6141 * Unfortunately we can't take advantage of Pango's ability to
6142 * render both LTR and RTL at the same time. In order to support
6143 * that, Vim's main screen engine would have to make use of Pango
6144 * functionality.
6145 */
6146 item->analysis.level = (item->analysis.level + 1) & (~1U);
6147
6148 /* HACK: Overrule the shape engine, we don't want shaping to be
6149 * done, because drawing the cursor would change the display. */
6150 item->analysis.shape_engine = default_shape_engine;
6151
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02006152#ifdef HAVE_PANGO_SHAPE_FULL
Bram Moolenaar7e2ec002015-09-08 16:31:06 +02006153 pango_shape_full((const char *)s + item->offset, item->length,
6154 (const char *)s, len, &item->analysis, glyphs);
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02006155#else
6156 pango_shape((const char *)s + item->offset, item->length,
6157 &item->analysis, glyphs);
6158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 /*
6160 * Fixed-width hack: iterate over the array and assign a fixed
6161 * width to each glyph, thus overriding the choice made by the
6162 * shaping engine. We use utf_char2cells() to determine the
6163 * number of cells needed.
6164 *
6165 * Also perform all kind of dark magic to get composing
6166 * characters right (and pretty too of course).
6167 */
6168 for (i = 0; i < glyphs->num_glyphs; ++i)
6169 {
6170 PangoGlyphInfo *glyph;
6171
6172 glyph = &glyphs->glyphs[i];
6173
6174 if (glyph->attr.is_cluster_start)
6175 {
6176 int cellcount;
6177
6178 cellcount = count_cluster_cells(
6179 s, item, glyphs, i, &cluster_width,
6180 (item_list != NULL) ? &last_glyph_rbearing : NULL);
6181
6182 if (cellcount > 0)
6183 {
6184 int width;
6185
6186 width = cellcount * gui.char_width * PANGO_SCALE;
6187 glyph->geometry.x_offset +=
6188 MAX(0, width - cluster_width) / 2;
6189 glyph->geometry.width = width;
6190 }
6191 else
6192 {
6193 /* If there are only combining characters in the
6194 * cluster, we cannot just change the width of the
6195 * previous glyph since there is none. Therefore
6196 * some guesswork is needed. */
6197 setup_zero_width_cluster(item, glyph, cells,
6198 cluster_width,
6199 last_glyph_rbearing);
6200 }
6201
6202 item_cells += cellcount;
6203 cells = cellcount;
6204 }
6205 else if (i > 0)
6206 {
6207 int width;
6208
6209 /* There is a previous glyph, so we deal with combining
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006210 * characters the canonical way.
Bram Moolenaar96118f32010-08-08 14:40:37 +02006211 * In some circumstances Pango uses a positive x_offset,
6212 * then use the width of the previous glyph for this one
6213 * and set the previous width to zero.
6214 * Otherwise we get a negative x_offset, Pango has already
6215 * positioned the combining char, keep the widths as they
6216 * are.
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006217 * For both adjust the x_offset to position the glyph in
Bram Moolenaar96118f32010-08-08 14:40:37 +02006218 * the middle. */
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006219 if (glyph->geometry.x_offset >= 0)
Bram Moolenaar96118f32010-08-08 14:40:37 +02006220 {
6221 glyphs->glyphs[i].geometry.width =
6222 glyphs->glyphs[i - 1].geometry.width;
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006223 glyphs->glyphs[i - 1].geometry.width = 0;
Bram Moolenaar96118f32010-08-08 14:40:37 +02006224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 width = cells * gui.char_width * PANGO_SCALE;
6226 glyph->geometry.x_offset +=
6227 MAX(0, width - cluster_width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 }
6229 else /* i == 0 "cannot happen" */
6230 {
6231 glyph->geometry.width = 0;
6232 }
6233 }
6234
6235 /*** Aaaaand action! ***/
Bram Moolenaar98921892016-02-23 17:14:37 +01006236#if GTK_CHECK_VERSION(3,0,0)
6237 draw_glyph_string(row, col + column_offset, item_cells,
6238 flags, item->analysis.font, glyphs,
6239 cr);
6240#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 draw_glyph_string(row, col + column_offset, item_cells,
6242 flags, item->analysis.font, glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01006243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244
6245 pango_item_free(item);
6246
6247 column_offset += item_cells;
6248 }
6249
6250 pango_attr_list_unref(attr_list);
6251 }
6252
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006253skipitall:
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006254 /* Draw underline and undercurl. */
Bram Moolenaar98921892016-02-23 17:14:37 +01006255#if GTK_CHECK_VERSION(3,0,0)
6256 draw_under(flags, row, col, column_offset, cr);
6257#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006258 draw_under(flags, row, col, column_offset);
Bram Moolenaar98921892016-02-23 17:14:37 +01006259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260
6261 pango_glyph_string_free(glyphs);
6262 vim_free(conv_buf);
6263
Bram Moolenaar98921892016-02-23 17:14:37 +01006264#if GTK_CHECK_VERSION(3,0,0)
6265 cairo_destroy(cr);
6266 if (!gui.by_signal)
6267 gdk_window_invalidate_rect(gtk_widget_get_window(gui.drawarea),
6268 &area, FALSE);
6269#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 gdk_gc_set_clip_rectangle(gui.text_gc, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01006271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272
6273 return column_offset;
6274}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275
6276/*
6277 * Return OK if the key with the termcap name "name" is supported.
6278 */
6279 int
6280gui_mch_haskey(char_u *name)
6281{
6282 int i;
6283
6284 for (i = 0; special_keys[i].key_sym != 0; i++)
6285 if (name[0] == special_keys[i].code0
6286 && name[1] == special_keys[i].code1)
6287 return OK;
6288 return FAIL;
6289}
6290
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006291#if defined(FEAT_TITLE) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292/*
6293 * Return the text window-id and display. Only required for X-based GUI's
6294 */
6295 int
6296gui_get_x11_windis(Window *win, Display **dis)
6297{
Bram Moolenaar98921892016-02-23 17:14:37 +01006298#if GTK_CHECK_VERSION(3,0,0)
6299 if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL)
6300#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01006302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 {
Bram Moolenaar98921892016-02-23 17:14:37 +01006304#if GTK_CHECK_VERSION(3,0,0)
6305 *dis = GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin));
6306 *win = GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin));
6307#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 *dis = GDK_WINDOW_XDISPLAY(gui.mainwin->window);
6309 *win = GDK_WINDOW_XWINDOW(gui.mainwin->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01006310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 return OK;
6312 }
6313
6314 *dis = NULL;
6315 *win = 0;
6316 return FAIL;
6317}
6318#endif
6319
6320#if defined(FEAT_CLIENTSERVER) \
6321 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
6322
6323 Display *
6324gui_mch_get_display(void)
6325{
Bram Moolenaar98921892016-02-23 17:14:37 +01006326#if GTK_CHECK_VERSION(3,0,0)
6327 if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL)
6328 return GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin));
6329#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
6331 return GDK_WINDOW_XDISPLAY(gui.mainwin->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01006332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 else
6334 return NULL;
6335}
6336#endif
6337
6338 void
6339gui_mch_beep(void)
6340{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 GdkDisplay *display;
6342
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02006343#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01006344 if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin))
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02006345#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02006347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 display = gtk_widget_get_display(gui.mainwin);
6349 else
6350 display = gdk_display_get_default();
6351
6352 if (display != NULL)
6353 gdk_display_beep(display);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354}
6355
6356 void
6357gui_mch_flash(int msec)
6358{
Bram Moolenaar98921892016-02-23 17:14:37 +01006359#if GTK_CHECK_VERSION(3,0,0)
6360 /* TODO Replace GdkGC with Cairo */
6361 (void)msec;
6362#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 GdkGCValues values;
6364 GdkGC *invert_gc;
6365
6366 if (gui.drawarea->window == NULL)
6367 return;
6368
6369 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6370 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
6371 values.function = GDK_XOR;
6372 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6373 &values,
6374 GDK_GC_FOREGROUND |
6375 GDK_GC_BACKGROUND |
6376 GDK_GC_FUNCTION);
6377 gdk_gc_set_exposures(invert_gc,
6378 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
6379 /*
6380 * Do a visual beep by changing back and forth in some undetermined way,
6381 * the foreground and background colors. This is due to the fact that
6382 * there can't be really any prediction about the effects of XOR on
6383 * arbitrary X11 servers. However this seems to be enough for what we
6384 * intend it to do.
6385 */
6386 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6387 TRUE,
6388 0, 0,
6389 FILL_X((int)Columns) + gui.border_offset,
6390 FILL_Y((int)Rows) + gui.border_offset);
6391
6392 gui_mch_flush();
6393 ui_delay((long)msec, TRUE); /* wait so many msec */
6394
6395 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6396 TRUE,
6397 0, 0,
6398 FILL_X((int)Columns) + gui.border_offset,
6399 FILL_Y((int)Rows) + gui.border_offset);
6400
6401 gdk_gc_destroy(invert_gc);
Bram Moolenaar98921892016-02-23 17:14:37 +01006402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403}
6404
6405/*
6406 * Invert a rectangle from row r, column c, for nr rows and nc columns.
6407 */
6408 void
6409gui_mch_invert_rectangle(int r, int c, int nr, int nc)
6410{
Bram Moolenaar98921892016-02-23 17:14:37 +01006411#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar4fc563b2016-03-12 12:40:58 +01006412 const GdkRectangle rect = {
6413 FILL_X(c), FILL_Y(r), nc * gui.char_width, nr * gui.char_height
6414 };
6415 cairo_t * const cr = cairo_create(gui.surface);
6416
Bram Moolenaar36edf062016-07-21 22:10:12 +02006417 set_cairo_source_rgba_from_color(cr, gui.norm_pixel ^ gui.back_pixel);
Bram Moolenaar4fc563b2016-03-12 12:40:58 +01006418# if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,9,2)
6419 cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE);
6420# else
6421 /* Give an implementation for older cairo versions if necessary. */
6422# endif
6423 gdk_cairo_rectangle(cr, &rect);
6424 cairo_fill(cr);
6425
6426 cairo_destroy(cr);
6427
6428 if (!gui.by_signal)
6429 gtk_widget_queue_draw_area(gui.drawarea, rect.x, rect.y,
6430 rect.width, rect.height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006431#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 GdkGCValues values;
6433 GdkGC *invert_gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434
6435 if (gui.drawarea->window == NULL)
6436 return;
6437
Bram Moolenaar89d40322006-08-29 15:30:07 +00006438 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6439 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 values.function = GDK_XOR;
6441 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6442 &values,
6443 GDK_GC_FOREGROUND |
6444 GDK_GC_BACKGROUND |
6445 GDK_GC_FUNCTION);
Bram Moolenaar89d40322006-08-29 15:30:07 +00006446 gdk_gc_set_exposures(invert_gc, gui.visibility !=
6447 GDK_VISIBILITY_UNOBSCURED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6449 TRUE,
6450 FILL_X(c), FILL_Y(r),
6451 (nc) * gui.char_width, (nr) * gui.char_height);
6452 gdk_gc_destroy(invert_gc);
Bram Moolenaar98921892016-02-23 17:14:37 +01006453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454}
6455
6456/*
6457 * Iconify the GUI window.
6458 */
6459 void
6460gui_mch_iconify(void)
6461{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 gtk_window_iconify(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463}
6464
6465#if defined(FEAT_EVAL) || defined(PROTO)
6466/*
6467 * Bring the Vim window to the foreground.
6468 */
6469 void
6470gui_mch_set_foreground(void)
6471{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 gtk_window_present(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473}
6474#endif
6475
6476/*
6477 * Draw a cursor without focus.
6478 */
6479 void
6480gui_mch_draw_hollow_cursor(guicolor_T color)
6481{
6482 int i = 1;
Bram Moolenaar98921892016-02-23 17:14:37 +01006483#if GTK_CHECK_VERSION(3,0,0)
6484 cairo_t *cr;
6485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486
Bram Moolenaar98921892016-02-23 17:14:37 +01006487#if GTK_CHECK_VERSION(3,0,0)
6488 if (gtk_widget_get_window(gui.drawarea) == NULL)
6489#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 if (gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01006491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 return;
6493
Bram Moolenaar98921892016-02-23 17:14:37 +01006494#if GTK_CHECK_VERSION(3,0,0)
6495 cr = cairo_create(gui.surface);
6496#endif
6497
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 gui_mch_set_fg_color(color);
6499
Bram Moolenaar98921892016-02-23 17:14:37 +01006500#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02006501 cairo_set_source_rgba(cr,
6502 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
6503 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01006504#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar98921892016-02-23 17:14:37 +01006506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 if (mb_lefthalve(gui.row, gui.col))
6508 i = 2;
Bram Moolenaar98921892016-02-23 17:14:37 +01006509#if GTK_CHECK_VERSION(3,0,0)
6510 cairo_set_line_width(cr, 1.0);
6511 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
6512 cairo_rectangle(cr,
6513 FILL_X(gui.col) + 0.5, FILL_Y(gui.row) + 0.5,
6514 i * gui.char_width - 1, gui.char_height - 1);
6515 cairo_stroke(cr);
6516 cairo_destroy(cr);
6517#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6519 FALSE,
6520 FILL_X(gui.col), FILL_Y(gui.row),
6521 i * gui.char_width - 1, gui.char_height - 1);
Bram Moolenaar98921892016-02-23 17:14:37 +01006522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523}
6524
6525/*
6526 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
6527 * color "color".
6528 */
6529 void
6530gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
6531{
Bram Moolenaar98921892016-02-23 17:14:37 +01006532#if GTK_CHECK_VERSION(3,0,0)
6533 if (gtk_widget_get_window(gui.drawarea) == NULL)
6534#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 if (gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01006536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 return;
6538
6539 gui_mch_set_fg_color(color);
6540
Bram Moolenaar98921892016-02-23 17:14:37 +01006541#if GTK_CHECK_VERSION(3,0,0)
6542 {
6543 cairo_t *cr;
6544
6545 cr = cairo_create(gui.surface);
Bram Moolenaar36edf062016-07-21 22:10:12 +02006546 cairo_set_source_rgba(cr,
6547 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
6548 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01006549 cairo_rectangle(cr,
6550# ifdef FEAT_RIGHTLEFT
6551 /* vertical line should be on the right of current point */
6552 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
6553# endif
6554 FILL_X(gui.col), FILL_Y(gui.row) + gui.char_height - h,
6555 w, h);
6556 cairo_fill(cr);
6557 cairo_destroy(cr);
6558 }
6559#else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6561 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6562 TRUE,
Bram Moolenaar98921892016-02-23 17:14:37 +01006563# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 /* vertical line should be on the right of current point */
6565 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
Bram Moolenaar98921892016-02-23 17:14:37 +01006566# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 FILL_X(gui.col),
6568 FILL_Y(gui.row) + gui.char_height - h,
6569 w, h);
Bram Moolenaar98921892016-02-23 17:14:37 +01006570#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571}
6572
6573
6574/*
6575 * Catch up with any queued X11 events. This may put keyboard input into the
6576 * input buffer, call resize call-backs, trigger timers etc. If there is
6577 * nothing in the X11 event queue (& no timers pending), then we return
6578 * immediately.
6579 */
6580 void
6581gui_mch_update(void)
6582{
Bram Moolenaara3f41662010-07-11 19:01:06 +02006583 while (g_main_context_pending(NULL) && !vim_is_input_buf_full())
6584 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585}
6586
Bram Moolenaar98921892016-02-23 17:14:37 +01006587#if GTK_CHECK_VERSION(3,0,0)
6588 static gboolean
6589#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +01006591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592input_timer_cb(gpointer data)
6593{
6594 int *timed_out = (int *) data;
6595
Bram Moolenaar49325942007-05-10 19:19:59 +00006596 /* Just inform the caller about the occurrence of it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 *timed_out = TRUE;
6598
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 return FALSE; /* don't happen again */
6600}
6601
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602/*
6603 * GUI input routine called by gui_wait_for_chars(). Waits for a character
6604 * from the keyboard.
6605 * wtime == -1 Wait forever.
6606 * wtime == 0 This should never happen.
6607 * wtime > 0 Wait wtime milliseconds for a character.
6608 * Returns OK if a character was found to be available within the given time,
6609 * or FAIL otherwise.
6610 */
6611 int
6612gui_mch_wait_for_chars(long wtime)
6613{
Bram Moolenaar4231da42016-06-02 14:30:04 +02006614 int focus;
6615 guint timer;
6616 static int timed_out;
6617 int retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618
6619 timed_out = FALSE;
6620
6621 /* this timeout makes sure that we will return if no characters arrived in
6622 * time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 if (wtime > 0)
Bram Moolenaar98921892016-02-23 17:14:37 +01006624#if GTK_CHECK_VERSION(3,0,0)
6625 timer = g_timeout_add((guint)wtime, input_timer_cb, &timed_out);
6626#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 timer = gtk_timeout_add((guint32)wtime, input_timer_cb, &timed_out);
Bram Moolenaar98921892016-02-23 17:14:37 +01006628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 else
6630 timer = 0;
6631
6632 focus = gui.in_focus;
6633
6634 do
6635 {
6636 /* Stop or start blinking when focus changes */
6637 if (gui.in_focus != focus)
6638 {
6639 if (gui.in_focus)
6640 gui_mch_start_blink();
6641 else
6642 gui_mch_stop_blink();
6643 focus = gui.in_focus;
6644 }
6645
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006646# if defined(FEAT_JOB_CHANNEL)
6647 /* Using an event handler for a channel that may be disconnected does
6648 * not work, it hangs. Instead poll for messages. */
6649 channel_handle_events(TRUE);
6650# endif
6651
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006652#ifdef MESSAGE_QUEUE
Bram Moolenaar4231da42016-06-02 14:30:04 +02006653# ifdef FEAT_TIMERS
6654 did_add_timer = FALSE;
6655# endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006656 parse_queued_messages();
Bram Moolenaar4231da42016-06-02 14:30:04 +02006657# ifdef FEAT_TIMERS
6658 if (did_add_timer)
6659 /* Need to recompute the waiting time. */
6660 goto theend;
6661# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006662#endif
6663
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 /*
6665 * Loop in GTK+ processing until a timeout or input occurs.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006666 * Skip this if input is available anyway (can happen in rare
6667 * situations, sort of race condition).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006669 if (!input_available())
Bram Moolenaara3f41662010-07-11 19:01:06 +02006670 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671
6672 /* Got char, return immediately */
6673 if (input_available())
6674 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02006675 retval = OK;
6676 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 }
6678 } while (wtime < 0 || !timed_out);
6679
6680 /*
6681 * Flush all eventually pending (drawing) events.
6682 */
6683 gui_mch_update();
6684
Bram Moolenaar4231da42016-06-02 14:30:04 +02006685theend:
6686 if (timer != 0 && !timed_out)
6687#if GTK_CHECK_VERSION(3,0,0)
6688 g_source_remove(timer);
6689#else
6690 gtk_timeout_remove(timer);
6691#endif
6692
6693 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694}
6695
6696
6697/****************************************************************************
6698 * Output drawing routines.
6699 ****************************************************************************/
6700
6701
6702/* Flush any output to the screen */
6703 void
6704gui_mch_flush(void)
6705{
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02006706#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01006707 if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708#else
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02006709 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710#endif
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02006711 gdk_display_flush(gtk_widget_get_display(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712}
6713
6714/*
6715 * Clear a rectangular region of the screen from text pos (row1, col1) to
6716 * (row2, col2) inclusive.
6717 */
6718 void
Bram Moolenaarfe344a92017-02-23 12:20:35 +01006719gui_mch_clear_block(int row1arg, int col1arg, int row2arg, int col2arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720{
Bram Moolenaarfe344a92017-02-23 12:20:35 +01006721
6722 int col1 = check_col(col1arg);
6723 int col2 = check_col(col2arg);
6724 int row1 = check_row(row1arg);
6725 int row2 = check_row(row2arg);
6726
Bram Moolenaar98921892016-02-23 17:14:37 +01006727#if GTK_CHECK_VERSION(3,0,0)
6728 if (gtk_widget_get_window(gui.drawarea) == NULL)
6729 return;
6730#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 GdkColor color;
6732
6733 if (gui.drawarea->window == NULL)
6734 return;
6735
6736 color.pixel = gui.back_pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01006737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738
Bram Moolenaar98921892016-02-23 17:14:37 +01006739#if GTK_CHECK_VERSION(3,0,0)
6740 {
6741 /* Add one pixel to the far right column in case a double-stroked
6742 * bold glyph may sit there. */
6743 const GdkRectangle rect = {
6744 FILL_X(col1), FILL_Y(row1),
6745 (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1),
6746 (row2 - row1 + 1) * gui.char_height
6747 };
6748 GdkWindow * const win = gtk_widget_get_window(gui.drawarea);
6749 cairo_t * const cr = cairo_create(gui.surface);
Bram Moolenaara859f042016-11-17 19:11:55 +01006750# if GTK_CHECK_VERSION(3,22,2)
6751 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
6752# else
Bram Moolenaar98921892016-02-23 17:14:37 +01006753 cairo_pattern_t * const pat = gdk_window_get_background_pattern(win);
6754 if (pat != NULL)
6755 cairo_set_source(cr, pat);
6756 else
Bram Moolenaar36edf062016-07-21 22:10:12 +02006757 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
Bram Moolenaara859f042016-11-17 19:11:55 +01006758# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01006759 gdk_cairo_rectangle(cr, &rect);
6760 cairo_fill(cr);
6761 cairo_destroy(cr);
6762
6763 if (!gui.by_signal)
6764 gdk_window_invalidate_rect(win, &rect, FALSE);
6765 }
6766#else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 gdk_gc_set_foreground(gui.text_gc, &color);
6768
6769 /* Clear one extra pixel at the far right, for when bold characters have
6770 * spilled over to the window border. */
6771 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE,
6772 FILL_X(col1), FILL_Y(row1),
6773 (col2 - col1 + 1) * gui.char_width
6774 + (col2 == Columns - 1),
6775 (row2 - row1 + 1) * gui.char_height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006776#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777}
6778
Bram Moolenaar98921892016-02-23 17:14:37 +01006779#if GTK_CHECK_VERSION(3,0,0)
6780 static void
6781gui_gtk_window_clear(GdkWindow *win)
6782{
6783 const GdkRectangle rect = {
6784 0, 0, gdk_window_get_width(win), gdk_window_get_height(win)
6785 };
6786 cairo_t * const cr = cairo_create(gui.surface);
Bram Moolenaara859f042016-11-17 19:11:55 +01006787# if GTK_CHECK_VERSION(3,22,2)
6788 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
6789# else
Bram Moolenaar98921892016-02-23 17:14:37 +01006790 cairo_pattern_t * const pat = gdk_window_get_background_pattern(win);
6791 if (pat != NULL)
6792 cairo_set_source(cr, pat);
6793 else
Bram Moolenaar36edf062016-07-21 22:10:12 +02006794 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
Bram Moolenaara859f042016-11-17 19:11:55 +01006795# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01006796 gdk_cairo_rectangle(cr, &rect);
6797 cairo_fill(cr);
6798 cairo_destroy(cr);
6799
6800 if (!gui.by_signal)
6801 gdk_window_invalidate_rect(win, &rect, FALSE);
6802}
6803#endif
6804
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 void
6806gui_mch_clear_all(void)
6807{
Bram Moolenaar98921892016-02-23 17:14:37 +01006808#if GTK_CHECK_VERSION(3,0,0)
6809 if (gtk_widget_get_window(gui.drawarea) != NULL)
6810 gui_gtk_window_clear(gtk_widget_get_window(gui.drawarea));
6811#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 if (gui.drawarea->window != NULL)
6813 gdk_window_clear(gui.drawarea->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01006814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815}
6816
Bram Moolenaar98921892016-02-23 17:14:37 +01006817#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818/*
6819 * Redraw any text revealed by scrolling up/down.
6820 */
6821 static void
6822check_copy_area(void)
6823{
6824 GdkEvent *event;
6825 int expose_count;
6826
6827 if (gui.visibility != GDK_VISIBILITY_PARTIAL)
6828 return;
6829
6830 /* Avoid redrawing the cursor while scrolling or it'll end up where
6831 * we don't want it to be. I'm not sure if it's correct to call
6832 * gui_dont_update_cursor() at this point but it works as a quick
6833 * fix for now. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02006834 gui_dont_update_cursor(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835
6836 do
6837 {
6838 /* Wait to check whether the scroll worked or not. */
6839 event = gdk_event_get_graphics_expose(gui.drawarea->window);
6840
6841 if (event == NULL)
6842 break; /* received NoExpose event */
6843
6844 gui_redraw(event->expose.area.x, event->expose.area.y,
6845 event->expose.area.width, event->expose.area.height);
6846
6847 expose_count = event->expose.count;
6848 gdk_event_free(event);
6849 }
6850 while (expose_count > 0); /* more events follow */
6851
6852 gui_can_update_cursor();
6853}
Bram Moolenaar98921892016-02-23 17:14:37 +01006854#endif /* !GTK_CHECK_VERSION(3,0,0) */
6855
6856#if GTK_CHECK_VERSION(3,0,0)
6857 static void
6858gui_gtk_surface_copy_rect(int dest_x, int dest_y,
6859 int src_x, int src_y,
6860 int width, int height)
6861{
6862 cairo_t * const cr = cairo_create(gui.surface);
6863
6864 cairo_rectangle(cr, dest_x, dest_y, width, height);
6865 cairo_clip(cr);
6866 cairo_push_group(cr);
6867 cairo_set_source_surface(cr, gui.surface, dest_x - src_x, dest_y - src_y);
6868 cairo_paint(cr);
6869 cairo_pop_group_to_source(cr);
6870 cairo_paint(cr);
6871
6872 cairo_destroy(cr);
6873}
6874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875
6876/*
6877 * Delete the given number of lines from the given row, scrolling up any
6878 * text further down within the scroll region.
6879 */
6880 void
6881gui_mch_delete_lines(int row, int num_lines)
6882{
Bram Moolenaar98921892016-02-23 17:14:37 +01006883#if GTK_CHECK_VERSION(3,0,0)
6884 const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1;
6885 const int nrows = gui.scroll_region_bot - row + 1;
6886 const int src_nrows = nrows - num_lines;
6887
6888 gui_gtk_surface_copy_rect(
6889 FILL_X(gui.scroll_region_left), FILL_Y(row),
6890 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6891 gui.char_width * ncols + 1, gui.char_height * src_nrows);
6892 gui_clear_block(
6893 gui.scroll_region_bot - num_lines + 1, gui.scroll_region_left,
6894 gui.scroll_region_bot, gui.scroll_region_right);
6895 gui_gtk3_redraw(
6896 FILL_X(gui.scroll_region_left), FILL_Y(row),
6897 gui.char_width * ncols + 1, gui.char_height * nrows);
6898 if (!gui.by_signal)
6899 gtk_widget_queue_draw_area(gui.drawarea,
6900 FILL_X(gui.scroll_region_left), FILL_Y(row),
6901 gui.char_width * ncols + 1, gui.char_height * nrows);
6902#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
6904 return; /* Can't see the window */
6905
6906 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6907 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6908
6909 /* copy one extra pixel, for when bold has spilled over */
6910 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6911 FILL_X(gui.scroll_region_left), FILL_Y(row),
6912 gui.drawarea->window,
6913 FILL_X(gui.scroll_region_left),
6914 FILL_Y(row + num_lines),
6915 gui.char_width * (gui.scroll_region_right
6916 - gui.scroll_region_left + 1) + 1,
6917 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6918
6919 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
6920 gui.scroll_region_left,
6921 gui.scroll_region_bot, gui.scroll_region_right);
6922 check_copy_area();
Bram Moolenaar98921892016-02-23 17:14:37 +01006923#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924}
6925
6926/*
6927 * Insert the given number of lines before the given row, scrolling down any
6928 * following text within the scroll region.
6929 */
6930 void
6931gui_mch_insert_lines(int row, int num_lines)
6932{
Bram Moolenaar98921892016-02-23 17:14:37 +01006933#if GTK_CHECK_VERSION(3,0,0)
6934 const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1;
6935 const int nrows = gui.scroll_region_bot - row + 1;
6936 const int src_nrows = nrows - num_lines;
6937
6938 gui_gtk_surface_copy_rect(
6939 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6940 FILL_X(gui.scroll_region_left), FILL_Y(row),
6941 gui.char_width * ncols + 1, gui.char_height * src_nrows);
6942 gui_mch_clear_block(
6943 row, gui.scroll_region_left,
6944 row + num_lines - 1, gui.scroll_region_right);
6945 gui_gtk3_redraw(
6946 FILL_X(gui.scroll_region_left), FILL_Y(row),
6947 gui.char_width * ncols + 1, gui.char_height * nrows);
6948 if (!gui.by_signal)
6949 gtk_widget_queue_draw_area(gui.drawarea,
6950 FILL_X(gui.scroll_region_left), FILL_Y(row),
6951 gui.char_width * ncols + 1, gui.char_height * nrows);
6952#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
6954 return; /* Can't see the window */
6955
6956 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6957 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6958
6959 /* copy one extra pixel, for when bold has spilled over */
6960 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6961 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6962 gui.drawarea->window,
6963 FILL_X(gui.scroll_region_left), FILL_Y(row),
6964 gui.char_width * (gui.scroll_region_right
6965 - gui.scroll_region_left + 1) + 1,
6966 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6967
6968 gui_clear_block(row, gui.scroll_region_left,
6969 row + num_lines - 1, gui.scroll_region_right);
6970 check_copy_area();
Bram Moolenaar98921892016-02-23 17:14:37 +01006971#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972}
6973
6974/*
6975 * X Selection stuff, for cutting and pasting text to other windows.
6976 */
6977 void
6978clip_mch_request_selection(VimClipboard *cbd)
6979{
6980 GdkAtom target;
6981 unsigned i;
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00006982 time_t start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983
6984 for (i = 0; i < N_SELECTION_TARGETS; ++i)
6985 {
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006986 if (!clip_html && selection_targets[i].info == TARGET_HTML)
6987 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 received_selection = RS_NONE;
6989 target = gdk_atom_intern(selection_targets[i].target, FALSE);
6990
6991 gtk_selection_convert(gui.drawarea,
6992 cbd->gtk_sel_atom, target,
6993 (guint32)GDK_CURRENT_TIME);
6994
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00006995 /* Hack: Wait up to three seconds for the selection. A hang was
6996 * noticed here when using the netrw plugin combined with ":gui"
6997 * during the FocusGained event. */
6998 start = time(NULL);
6999 while (received_selection == RS_NONE && time(NULL) < start + 3)
Bram Moolenaara3f41662010-07-11 19:01:06 +02007000 g_main_context_iteration(NULL, TRUE); /* wait for selection_received_cb */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001
7002 if (received_selection != RS_FAIL)
7003 return;
7004 }
7005
7006 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaar98921892016-02-23 17:14:37 +01007007#if GTK_CHECK_VERSION(3,0,0)
7008 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)),
7009 cbd);
7010#else
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00007011 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gui.mainwin->window), cbd);
Bram Moolenaar98921892016-02-23 17:14:37 +01007012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013}
7014
7015/*
7016 * Disown the selection.
7017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00007019clip_mch_lose_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020{
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01007021 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023}
7024
7025/*
7026 * Own the selection and return OK if it worked.
7027 */
7028 int
7029clip_mch_own_selection(VimClipboard *cbd)
7030{
7031 int success;
7032
7033 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom,
Bram Moolenaar20892c12011-06-26 04:49:00 +02007034 gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 gui_mch_update();
7036 return (success) ? OK : FAIL;
7037}
7038
7039/*
7040 * Send the current selection to the clipboard. Do nothing for X because we
7041 * will fill in the selection only when requested by another app.
7042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00007044clip_mch_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045{
7046}
7047
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01007048 int
7049clip_gtk_owner_exists(VimClipboard *cbd)
7050{
7051 return gdk_selection_owner_get(cbd->gtk_sel_atom) != NULL;
7052}
7053
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054
7055#if defined(FEAT_MENU) || defined(PROTO)
7056/*
7057 * Make a menu item appear either active or not active (grey or not grey).
7058 */
7059 void
7060gui_mch_menu_grey(vimmenu_T *menu, int grey)
7061{
7062 if (menu->id == NULL)
7063 return;
7064
7065 if (menu_is_separator(menu->name))
7066 grey = TRUE;
7067
7068 gui_mch_menu_hidden(menu, FALSE);
7069 /* Be clever about bitfields versus true booleans here! */
Bram Moolenaar98921892016-02-23 17:14:37 +01007070# if GTK_CHECK_VERSION(3,0,0)
7071 if (!gtk_widget_get_sensitive(menu->id) == !grey)
7072# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 if (!GTK_WIDGET_SENSITIVE(menu->id) == !grey)
Bram Moolenaar98921892016-02-23 17:14:37 +01007074# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 {
7076 gtk_widget_set_sensitive(menu->id, !grey);
7077 gui_mch_update();
7078 }
7079}
7080
7081/*
7082 * Make menu item hidden or not hidden.
7083 */
7084 void
7085gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
7086{
7087 if (menu->id == 0)
7088 return;
7089
7090 if (hidden)
7091 {
Bram Moolenaar98921892016-02-23 17:14:37 +01007092# if GTK_CHECK_VERSION(3,0,0)
7093 if (gtk_widget_get_visible(menu->id))
7094# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 if (GTK_WIDGET_VISIBLE(menu->id))
Bram Moolenaar98921892016-02-23 17:14:37 +01007096# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 {
7098 gtk_widget_hide(menu->id);
7099 gui_mch_update();
7100 }
7101 }
7102 else
7103 {
Bram Moolenaar98921892016-02-23 17:14:37 +01007104# if GTK_CHECK_VERSION(3,0,0)
7105 if (!gtk_widget_get_visible(menu->id))
7106# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 if (!GTK_WIDGET_VISIBLE(menu->id))
Bram Moolenaar98921892016-02-23 17:14:37 +01007108# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 {
7110 gtk_widget_show(menu->id);
7111 gui_mch_update();
7112 }
7113 }
7114}
7115
7116/*
7117 * This is called after setting all the menus to grey/hidden or not.
7118 */
7119 void
7120gui_mch_draw_menubar(void)
7121{
7122 /* just make sure that the visual changes get effect immediately */
7123 gui_mch_update();
7124}
7125#endif /* FEAT_MENU */
7126
7127/*
7128 * Scrollbar stuff.
7129 */
7130 void
7131gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
7132{
7133 if (sb->id == NULL)
7134 return;
7135
Bram Moolenaar98921892016-02-23 17:14:37 +01007136#if GTK_CHECK_VERSION(3,0,0)
7137 gtk_widget_set_visible(sb->id, flag);
7138#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 if (flag)
7140 gtk_widget_show(sb->id);
7141 else
7142 gtk_widget_hide(sb->id);
Bram Moolenaar98921892016-02-23 17:14:37 +01007143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00007145 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146}
7147
7148
7149/*
7150 * Return the RGB value of a pixel as long.
7151 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007152 guicolor_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153gui_mch_get_rgb(guicolor_T pixel)
7154{
Bram Moolenaar98921892016-02-23 17:14:37 +01007155#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02007156 return (long_u)pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01007157#else
Bram Moolenaar36edf062016-07-21 22:10:12 +02007158 GdkColor color;
7159
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
7161 (unsigned long)pixel, &color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007163 return (guicolor_T)(
7164 (((unsigned)color.red & 0xff00) << 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 | ((unsigned)color.green & 0xff00)
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007166 | (((unsigned)color.blue & 0xff00) >> 8));
Bram Moolenaar36edf062016-07-21 22:10:12 +02007167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168}
7169
7170/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007171 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007173 void
7174gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175{
Bram Moolenaar98921892016-02-23 17:14:37 +01007176#if GTK_CHECK_VERSION(3,0,0)
7177 gui_gtk_get_pointer(gui.drawarea, x, y, NULL);
7178#else
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007179 gdk_window_get_pointer(gui.drawarea->window, x, y, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01007180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181}
7182
7183 void
7184gui_mch_setmouse(int x, int y)
7185{
7186 /* Sorry for the Xlib call, but we can't avoid it, since there is no
7187 * internal GDK mechanism present to accomplish this. (and for good
7188 * reason...) */
Bram Moolenaar98921892016-02-23 17:14:37 +01007189#if GTK_CHECK_VERSION(3,0,0)
7190 XWarpPointer(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.drawarea)),
7191 (Window)0, GDK_WINDOW_XID(gtk_widget_get_window(gui.drawarea)),
7192 0, 0, 0U, 0U, x, y);
7193#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 XWarpPointer(GDK_WINDOW_XDISPLAY(gui.drawarea->window),
7195 (Window)0, GDK_WINDOW_XWINDOW(gui.drawarea->window),
7196 0, 0, 0U, 0U, x, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01007197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198}
7199
7200
7201#ifdef FEAT_MOUSESHAPE
7202/* The last set mouse pointer shape is remembered, to be used when it goes
7203 * from hidden to not hidden. */
7204static int last_shape = 0;
7205#endif
7206
7207/*
7208 * Use the blank mouse pointer or not.
7209 *
7210 * hide: TRUE = use blank ptr, FALSE = use parent ptr
7211 */
7212 void
7213gui_mch_mousehide(int hide)
7214{
7215 if (gui.pointer_hidden != hide)
7216 {
7217 gui.pointer_hidden = hide;
Bram Moolenaar98921892016-02-23 17:14:37 +01007218#if GTK_CHECK_VERSION(3,0,0)
7219 if (gtk_widget_get_window(gui.drawarea) && gui.blank_pointer != NULL)
7220#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 if (gui.drawarea->window && gui.blank_pointer != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01007222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 {
7224 if (hide)
Bram Moolenaar98921892016-02-23 17:14:37 +01007225#if GTK_CHECK_VERSION(3,0,0)
7226 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
7227 gui.blank_pointer);
7228#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01007230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 else
7232#ifdef FEAT_MOUSESHAPE
7233 mch_set_mouse_shape(last_shape);
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01007234#elif GTK_CHECK_VERSION(3,0,0)
7235 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236#else
7237 gdk_window_set_cursor(gui.drawarea->window, NULL);
7238#endif
7239 }
7240 }
7241}
7242
7243#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
7244
7245/* Table for shape IDs. Keep in sync with the mshape_names[] table in
7246 * misc2.c! */
7247static const int mshape_ids[] =
7248{
7249 GDK_LEFT_PTR, /* arrow */
7250 GDK_CURSOR_IS_PIXMAP, /* blank */
7251 GDK_XTERM, /* beam */
7252 GDK_SB_V_DOUBLE_ARROW, /* updown */
7253 GDK_SIZING, /* udsizing */
7254 GDK_SB_H_DOUBLE_ARROW, /* leftright */
7255 GDK_SIZING, /* lrsizing */
7256 GDK_WATCH, /* busy */
7257 GDK_X_CURSOR, /* no */
7258 GDK_CROSSHAIR, /* crosshair */
7259 GDK_HAND1, /* hand1 */
7260 GDK_HAND2, /* hand2 */
7261 GDK_PENCIL, /* pencil */
7262 GDK_QUESTION_ARROW, /* question */
7263 GDK_RIGHT_PTR, /* right-arrow */
7264 GDK_CENTER_PTR, /* up-arrow */
7265 GDK_LEFT_PTR /* last one */
7266};
7267
7268 void
7269mch_set_mouse_shape(int shape)
7270{
7271 int id;
7272 GdkCursor *c;
7273
Bram Moolenaar98921892016-02-23 17:14:37 +01007274# if GTK_CHECK_VERSION(3,0,0)
7275 if (gtk_widget_get_window(gui.drawarea) == NULL)
7276# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 if (gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01007278# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 return;
7280
7281 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
Bram Moolenaar98921892016-02-23 17:14:37 +01007282# if GTK_CHECK_VERSION(3,0,0)
7283 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
7284 gui.blank_pointer);
7285# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01007287# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 else
7289 {
7290 if (shape >= MSHAPE_NUMBERED)
7291 {
7292 id = shape - MSHAPE_NUMBERED;
7293 if (id >= GDK_LAST_CURSOR)
7294 id = GDK_LEFT_PTR;
7295 else
7296 id &= ~1; /* they are always even (why?) */
7297 }
Bram Moolenaarb85cb212009-05-17 14:24:23 +00007298 else if (shape < (int)(sizeof(mshape_ids) / sizeof(int)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 id = mshape_ids[shape];
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007300 else
7301 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 c = gdk_cursor_new_for_display(
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00007303 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id);
Bram Moolenaar98921892016-02-23 17:14:37 +01007304# if GTK_CHECK_VERSION(3,0,0)
7305 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), c);
7306# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 gdk_window_set_cursor(gui.drawarea->window, c);
Bram Moolenaar98921892016-02-23 17:14:37 +01007308# endif
7309# if GTK_CHECK_VERSION(3,0,0)
7310 g_object_unref(G_OBJECT(c));
7311# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 gdk_cursor_destroy(c); /* Unref, actually. Bloody GTK+ 1. */
Bram Moolenaar98921892016-02-23 17:14:37 +01007313# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 }
7315 if (shape != MSHAPE_HIDE)
7316 last_shape = shape;
7317}
7318#endif /* FEAT_MOUSESHAPE */
7319
7320
7321#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
7322/*
7323 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
7324 * scaled down if the current font is not big enough, or scaled up if the image
7325 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
7326 * will be cut off if the current font is not big enough, or centered if it's
7327 * too small.
7328 */
7329# define SIGN_WIDTH (2 * gui.char_width)
7330# define SIGN_HEIGHT (gui.char_height)
7331# define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
7332
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 void
7334gui_mch_drawsign(int row, int col, int typenr)
7335{
7336 GdkPixbuf *sign;
7337
7338 sign = (GdkPixbuf *)sign_get_image(typenr);
7339
Bram Moolenaar98921892016-02-23 17:14:37 +01007340# if GTK_CHECK_VERSION(3,0,0)
7341 if (sign != NULL && gui.drawarea != NULL
7342 && gtk_widget_get_window(gui.drawarea) != NULL)
7343# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 if (sign != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01007345# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 {
7347 int width;
7348 int height;
7349 int xoffset;
7350 int yoffset;
7351 int need_scale;
7352
7353 width = gdk_pixbuf_get_width(sign);
7354 height = gdk_pixbuf_get_height(sign);
7355 /*
7356 * Decide whether we need to scale. Allow one pixel of border
7357 * width to be cut off, in order to avoid excessive scaling for
7358 * tiny differences in font size.
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007359 * Do scale to fit the height to avoid gaps because of linespacing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 */
7361 need_scale = (width > SIGN_WIDTH + 2
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007362 || height != SIGN_HEIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 || (width < 3 * SIGN_WIDTH / 4
7364 && height < 3 * SIGN_HEIGHT / 4));
7365 if (need_scale)
7366 {
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007367 double aspect;
7368 int w = width;
7369 int h = height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370
7371 /* Keep the original aspect ratio */
7372 aspect = (double)height / (double)width;
7373 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect;
7374 width = MIN(width, SIGN_WIDTH);
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007375 if (((double)(MAX(height, SIGN_HEIGHT)) /
7376 (double)(MIN(height, SIGN_HEIGHT))) < 1.15)
7377 {
7378 /* Change the aspect ratio by at most 15% to fill the
7379 * available space completly. */
7380 height = (double)SIGN_HEIGHT * SIGN_ASPECT / aspect;
7381 height = MIN(height, SIGN_HEIGHT);
7382 }
7383 else
7384 height = (double)width * aspect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007386 if (w == width && h == height)
7387 {
7388 /* no change in dimensions; don't decrease reference counter
7389 * (below) */
7390 need_scale = FALSE;
7391 }
7392 else
7393 {
7394 /* This doesn't seem to be worth caching, and doing so would
7395 * complicate the code quite a bit. */
7396 sign = gdk_pixbuf_scale_simple(sign, width, height,
7397 GDK_INTERP_BILINEAR);
7398 if (sign == NULL)
7399 return; /* out of memory */
7400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 }
7402
7403 /* The origin is the upper-left corner of the pixmap. Therefore
7404 * these offset may become negative if the pixmap is smaller than
7405 * the 2x1 cells reserved for the sign icon. */
7406 xoffset = (width - SIGN_WIDTH) / 2;
7407 yoffset = (height - SIGN_HEIGHT) / 2;
7408
Bram Moolenaar98921892016-02-23 17:14:37 +01007409# if GTK_CHECK_VERSION(3,0,0)
7410 {
7411 cairo_t *cr;
7412 cairo_surface_t *bg_surf;
7413 cairo_t *bg_cr;
7414 cairo_surface_t *sign_surf;
7415 cairo_t *sign_cr;
7416
7417 cr = cairo_create(gui.surface);
7418
7419 bg_surf = cairo_surface_create_similar(gui.surface,
7420 cairo_surface_get_content(gui.surface),
7421 SIGN_WIDTH, SIGN_HEIGHT);
7422 bg_cr = cairo_create(bg_surf);
Bram Moolenaar36edf062016-07-21 22:10:12 +02007423 cairo_set_source_rgba(bg_cr,
7424 gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue,
7425 gui.bgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01007426 cairo_paint(bg_cr);
7427
7428 sign_surf = cairo_surface_create_similar(gui.surface,
7429 cairo_surface_get_content(gui.surface),
7430 SIGN_WIDTH, SIGN_HEIGHT);
7431 sign_cr = cairo_create(sign_surf);
7432 gdk_cairo_set_source_pixbuf(sign_cr, sign, -xoffset, -yoffset);
7433 cairo_paint(sign_cr);
7434
7435 cairo_set_operator(sign_cr, CAIRO_OPERATOR_DEST_OVER);
7436 cairo_set_source_surface(sign_cr, bg_surf, 0, 0);
7437 cairo_paint(sign_cr);
7438
7439 cairo_set_source_surface(cr, sign_surf, FILL_X(col), FILL_Y(row));
7440 cairo_paint(cr);
7441
7442 cairo_destroy(sign_cr);
7443 cairo_surface_destroy(sign_surf);
7444 cairo_destroy(bg_cr);
7445 cairo_surface_destroy(bg_surf);
7446 cairo_destroy(cr);
7447
7448 if (!gui.by_signal)
7449 gtk_widget_queue_draw_area(gui.drawarea,
7450 FILL_X(col), FILL_Y(col), width, height);
7451
7452 }
7453# else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
7455
7456 gdk_draw_rectangle(gui.drawarea->window,
7457 gui.text_gc,
7458 TRUE,
7459 FILL_X(col),
7460 FILL_Y(row),
7461 SIGN_WIDTH,
7462 SIGN_HEIGHT);
7463
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 gdk_pixbuf_render_to_drawable_alpha(sign,
7465 gui.drawarea->window,
7466 MAX(0, xoffset),
7467 MAX(0, yoffset),
7468 FILL_X(col) - MIN(0, xoffset),
7469 FILL_Y(row) - MIN(0, yoffset),
7470 MIN(width, SIGN_WIDTH),
7471 MIN(height, SIGN_HEIGHT),
7472 GDK_PIXBUF_ALPHA_BILEVEL,
7473 127,
7474 GDK_RGB_DITHER_NORMAL,
7475 0, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01007476# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 if (need_scale)
7478 g_object_unref(sign);
7479 }
7480}
7481
7482 void *
7483gui_mch_register_sign(char_u *signfile)
7484{
7485 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use)
7486 {
7487 GdkPixbuf *sign;
7488 GError *error = NULL;
7489 char_u *message;
7490
7491 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error);
7492
7493 if (error == NULL)
7494 return sign;
7495
7496 message = (char_u *)error->message;
7497
7498 if (message != NULL && input_conv.vc_type != CONV_NONE)
7499 message = string_convert(&input_conv, message, NULL);
7500
7501 if (message != NULL)
7502 {
7503 /* The error message is already translated and will be more
7504 * descriptive than anything we could possibly do ourselves. */
7505 EMSG2("E255: %s", message);
7506
7507 if (input_conv.vc_type != CONV_NONE)
7508 vim_free(message);
7509 }
7510 g_error_free(error);
7511 }
7512
7513 return NULL;
7514}
7515
7516 void
7517gui_mch_destroy_sign(void *sign)
7518{
7519 if (sign != NULL)
7520 g_object_unref(sign);
7521}
7522
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523#endif /* FEAT_SIGN_ICONS */