blob: 69e4e1824e82e6eda08239ac4bee5b155a28c6ae [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Porting to GTK+ was done by:
12 *
Bram Moolenaar0a56cb82005-01-04 21:45:14 +000013 * (C) 1998,1999,2000 by Marcin Dalecki <martin@dalecki.de>
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * With GREAT support and continuous encouragements by Andy Kahn and of
16 * course Bram Moolenaar!
17 *
18 * Support for GTK+ 2 was added by:
19 *
20 * (C) 2002,2003 Jason Hildebrand <jason@peaceworks.ca>
21 * Daniel Elstner <daniel.elstner@gmx.net>
Bram Moolenaar98921892016-02-23 17:14:37 +010022 *
23 * Support for GTK+ 3 was added by:
24 *
25 * 2016 Kazunobu Kuriyama <kazunobu.kuriyama@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
27
28#include "vim.h"
Bram Moolenaar36e294c2015-12-29 18:55:46 +010029#ifdef USE_GRESOURCE
30#include "auto/gui_gtk_gresources.h"
31#endif
Bram Moolenaarc93e7912008-07-08 10:46:08 +000032
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef FEAT_GUI_GNOME
34/* Gnome redefines _() and N_(). Grrr... */
35# ifdef _
36# undef _
37# endif
38# ifdef N_
39# undef N_
40# endif
41# ifdef textdomain
42# undef textdomain
43# endif
44# ifdef bindtextdomain
45# undef bindtextdomain
46# endif
Bram Moolenaara2dd9002007-05-14 17:38:30 +000047# ifdef bind_textdomain_codeset
48# undef bind_textdomain_codeset
Bram Moolenaar49325942007-05-10 19:19:59 +000049# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000050# if defined(FEAT_GETTEXT) && !defined(ENABLE_NLS)
51# define ENABLE_NLS /* so the texts in the dialog boxes are translated */
52# endif
53# include <gnome.h>
54# include "version.h"
Bram Moolenaardb552d602006-03-23 22:59:57 +000055/* missing prototype in bonobo-dock-item.h */
56extern void bonobo_dock_item_set_behavior(BonoboDockItem *dock_item, BonoboDockItemBehavior beh);
Bram Moolenaar071d4272004-06-13 20:20:40 +000057#endif
58
59#if !defined(FEAT_GUI_GTK) && defined(PROTO)
60/* When generating prototypes we don't want syntax errors. */
61# define GdkAtom int
62# define GdkEventExpose int
63# define GdkEventFocus int
64# define GdkEventVisibility int
65# define GdkEventProperty int
66# define GtkContainer int
67# define GtkTargetEntry int
68# define GtkType int
69# define GtkWidget int
70# define gint int
71# define gpointer int
72# define guint int
73# define GdkEventKey int
74# define GdkEventSelection int
75# define GtkSelectionData int
76# define GdkEventMotion int
77# define GdkEventButton int
78# define GdkDragContext int
79# define GdkEventConfigure int
80# define GdkEventClient int
81#else
Bram Moolenaar98921892016-02-23 17:14:37 +010082# if GTK_CHECK_VERSION(3,0,0)
83# include <gdk/gdkkeysyms-compat.h>
84# include <gtk/gtkx.h>
85# else
86# include <gdk/gdkkeysyms.h>
87# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000088# include <gdk/gdk.h>
89# ifdef WIN3264
90# include <gdk/gdkwin32.h>
91# else
92# include <gdk/gdkx.h>
93# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000094# include <gtk/gtk.h>
95# include "gui_gtk_f.h"
96#endif
97
98#ifdef HAVE_X11_SUNKEYSYM_H
99# include <X11/Sunkeysym.h>
100#endif
101
102/*
103 * Easy-to-use macro for multihead support.
104 */
105#ifdef HAVE_GTK_MULTIHEAD
106# define GET_X_ATOM(atom) gdk_x11_atom_to_xatom_for_display( \
107 gtk_widget_get_display(gui.mainwin), atom)
108#else
109# define GET_X_ATOM(atom) ((Atom)(atom))
110#endif
111
112/* Selection type distinguishers */
113enum
114{
115 TARGET_TYPE_NONE,
116 TARGET_UTF8_STRING,
117 TARGET_STRING,
118 TARGET_COMPOUND_TEXT,
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000119 TARGET_HTML,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 TARGET_TEXT,
121 TARGET_TEXT_URI_LIST,
122 TARGET_TEXT_PLAIN,
123 TARGET_VIM,
124 TARGET_VIMENC
125};
126
127/*
128 * Table of selection targets supported by Vim.
129 * Note: Order matters, preferred types should come first.
130 */
131static const GtkTargetEntry selection_targets[] =
132{
133 {VIMENC_ATOM_NAME, 0, TARGET_VIMENC},
134 {VIM_ATOM_NAME, 0, TARGET_VIM},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000135 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 {"COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT},
138 {"TEXT", 0, TARGET_TEXT},
139 {"STRING", 0, TARGET_STRING}
140};
141#define N_SELECTION_TARGETS (sizeof(selection_targets) / sizeof(selection_targets[0]))
142
143#ifdef FEAT_DND
144/*
145 * Table of DnD targets supported by Vim.
146 * Note: Order matters, preferred types should come first.
147 */
148static const GtkTargetEntry dnd_targets[] =
149{
150 {"text/uri-list", 0, TARGET_TEXT_URI_LIST},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000151 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 {"STRING", 0, TARGET_STRING},
154 {"text/plain", 0, TARGET_TEXT_PLAIN}
155};
156# define N_DND_TARGETS (sizeof(dnd_targets) / sizeof(dnd_targets[0]))
157#endif
158
159
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160/*
161 * "Monospace" is a standard font alias that should be present
162 * on all proper Pango/fontconfig installations.
163 */
164# define DEFAULT_FONT "Monospace 10"
165
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
167/*
168 * Atoms used to communicate save-yourself from the X11 session manager. There
169 * is no need to move them into the GUI struct, since they should be constant.
170 */
171static GdkAtom wm_protocols_atom = GDK_NONE;
172static GdkAtom save_yourself_atom = GDK_NONE;
173#endif
174
175/*
176 * Atoms used to control/reference X11 selections.
177 */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000178static GdkAtom html_atom = GDK_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179static GdkAtom utf8_string_atom = GDK_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180static GdkAtom vim_atom = GDK_NONE; /* Vim's own special selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181static GdkAtom vimenc_atom = GDK_NONE; /* Vim's extended selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182
183/*
184 * Keycodes recognized by vim.
185 * NOTE: when changing this, the table in gui_x11.c probably needs the same
186 * change!
187 */
188static struct special_key
189{
190 guint key_sym;
191 char_u code0;
192 char_u code1;
193}
194const special_keys[] =
195{
196 {GDK_Up, 'k', 'u'},
197 {GDK_Down, 'k', 'd'},
198 {GDK_Left, 'k', 'l'},
199 {GDK_Right, 'k', 'r'},
200 {GDK_F1, 'k', '1'},
201 {GDK_F2, 'k', '2'},
202 {GDK_F3, 'k', '3'},
203 {GDK_F4, 'k', '4'},
204 {GDK_F5, 'k', '5'},
205 {GDK_F6, 'k', '6'},
206 {GDK_F7, 'k', '7'},
207 {GDK_F8, 'k', '8'},
208 {GDK_F9, 'k', '9'},
209 {GDK_F10, 'k', ';'},
210 {GDK_F11, 'F', '1'},
211 {GDK_F12, 'F', '2'},
212 {GDK_F13, 'F', '3'},
213 {GDK_F14, 'F', '4'},
214 {GDK_F15, 'F', '5'},
215 {GDK_F16, 'F', '6'},
216 {GDK_F17, 'F', '7'},
217 {GDK_F18, 'F', '8'},
218 {GDK_F19, 'F', '9'},
219 {GDK_F20, 'F', 'A'},
220 {GDK_F21, 'F', 'B'},
221 {GDK_Pause, 'F', 'B'}, /* Pause == F21 according to netbeans.txt */
222 {GDK_F22, 'F', 'C'},
223 {GDK_F23, 'F', 'D'},
224 {GDK_F24, 'F', 'E'},
225 {GDK_F25, 'F', 'F'},
226 {GDK_F26, 'F', 'G'},
227 {GDK_F27, 'F', 'H'},
228 {GDK_F28, 'F', 'I'},
229 {GDK_F29, 'F', 'J'},
230 {GDK_F30, 'F', 'K'},
231 {GDK_F31, 'F', 'L'},
232 {GDK_F32, 'F', 'M'},
233 {GDK_F33, 'F', 'N'},
234 {GDK_F34, 'F', 'O'},
235 {GDK_F35, 'F', 'P'},
236#ifdef SunXK_F36
237 {SunXK_F36, 'F', 'Q'},
238 {SunXK_F37, 'F', 'R'},
239#endif
240 {GDK_Help, '%', '1'},
241 {GDK_Undo, '&', '8'},
242 {GDK_BackSpace, 'k', 'b'},
243 {GDK_Insert, 'k', 'I'},
244 {GDK_Delete, 'k', 'D'},
245 {GDK_3270_BackTab, 'k', 'B'},
246 {GDK_Clear, 'k', 'C'},
247 {GDK_Home, 'k', 'h'},
248 {GDK_End, '@', '7'},
249 {GDK_Prior, 'k', 'P'},
250 {GDK_Next, 'k', 'N'},
251 {GDK_Print, '%', '9'},
252 /* Keypad keys: */
253 {GDK_KP_Left, 'k', 'l'},
254 {GDK_KP_Right, 'k', 'r'},
255 {GDK_KP_Up, 'k', 'u'},
256 {GDK_KP_Down, 'k', 'd'},
257 {GDK_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
258 {GDK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
259 {GDK_KP_Home, 'K', '1'},
260 {GDK_KP_End, 'K', '4'},
261 {GDK_KP_Prior, 'K', '3'}, /* page up */
262 {GDK_KP_Next, 'K', '5'}, /* page down */
263
264 {GDK_KP_Add, 'K', '6'},
265 {GDK_KP_Subtract, 'K', '7'},
266 {GDK_KP_Divide, 'K', '8'},
267 {GDK_KP_Multiply, 'K', '9'},
268 {GDK_KP_Enter, 'K', 'A'},
269 {GDK_KP_Decimal, 'K', 'B'},
270
271 {GDK_KP_0, 'K', 'C'},
272 {GDK_KP_1, 'K', 'D'},
273 {GDK_KP_2, 'K', 'E'},
274 {GDK_KP_3, 'K', 'F'},
275 {GDK_KP_4, 'K', 'G'},
276 {GDK_KP_5, 'K', 'H'},
277 {GDK_KP_6, 'K', 'I'},
278 {GDK_KP_7, 'K', 'J'},
279 {GDK_KP_8, 'K', 'K'},
280 {GDK_KP_9, 'K', 'L'},
281
282 /* End of list marker: */
283 {0, 0, 0}
284};
285
286/*
287 * Flags for command line options table below.
288 */
289#define ARG_FONT 1
290#define ARG_GEOMETRY 2
291#define ARG_REVERSE 3
292#define ARG_NOREVERSE 4
293#define ARG_BACKGROUND 5
294#define ARG_FOREGROUND 6
295#define ARG_ICONIC 7
296#define ARG_ROLE 8
297#define ARG_NETBEANS 9
298#define ARG_XRM 10 /* ignored */
299#define ARG_MENUFONT 11 /* ignored */
300#define ARG_INDEX_MASK 0x00ff
301#define ARG_HAS_VALUE 0x0100 /* a value is expected after the argument */
302#define ARG_NEEDS_GUI 0x0200 /* need to initialize the GUI for this */
303#define ARG_FOR_GTK 0x0400 /* argument is handled by GTK+ or GNOME */
304#define ARG_COMPAT_LONG 0x0800 /* accept -foo but substitute with --foo */
305#define ARG_KEEP 0x1000 /* don't remove argument from argv[] */
306
307/*
308 * This table holds all the X GUI command line options allowed. This includes
309 * the standard ones so that we can skip them when Vim is started without the
310 * GUI (but the GUI might start up later).
311 *
312 * When changing this, also update doc/gui_x11.txt and the usage message!!!
313 */
314typedef struct
315{
316 const char *name;
317 unsigned int flags;
318}
319cmdline_option_T;
320
321static const cmdline_option_T cmdline_options[] =
322{
323 /* We handle these options ourselves */
324 {"-fn", ARG_FONT|ARG_HAS_VALUE},
325 {"-font", ARG_FONT|ARG_HAS_VALUE},
326 {"-geom", ARG_GEOMETRY|ARG_HAS_VALUE},
327 {"-geometry", ARG_GEOMETRY|ARG_HAS_VALUE},
328 {"-rv", ARG_REVERSE},
329 {"-reverse", ARG_REVERSE},
330 {"+rv", ARG_NOREVERSE},
331 {"+reverse", ARG_NOREVERSE},
332 {"-bg", ARG_BACKGROUND|ARG_HAS_VALUE},
333 {"-background", ARG_BACKGROUND|ARG_HAS_VALUE},
334 {"-fg", ARG_FOREGROUND|ARG_HAS_VALUE},
335 {"-foreground", ARG_FOREGROUND|ARG_HAS_VALUE},
336 {"-iconic", ARG_ICONIC},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 {"--role", ARG_ROLE|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338#ifdef FEAT_NETBEANS_INTG
339 {"-nb", ARG_NETBEANS}, /* non-standard value format */
340 {"-xrm", ARG_XRM|ARG_HAS_VALUE}, /* not implemented */
341 {"-mf", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
342 {"-menufont", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 /* Arguments handled by GTK (and GNOME) internally. */
345 {"--g-fatal-warnings", ARG_FOR_GTK},
346 {"--gdk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
347 {"--gdk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
348 {"--gtk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
349 {"--gtk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
350 {"--gtk-module", ARG_FOR_GTK|ARG_HAS_VALUE},
351 {"--sync", ARG_FOR_GTK},
352 {"--display", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
353 {"--name", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
354 {"--class", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 {"--screen", ARG_FOR_GTK|ARG_HAS_VALUE},
356 {"--gxid-host", ARG_FOR_GTK|ARG_HAS_VALUE},
357 {"--gxid-port", ARG_FOR_GTK|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifdef FEAT_GUI_GNOME
359 {"--load-modules", ARG_FOR_GTK|ARG_HAS_VALUE},
360 {"--sm-client-id", ARG_FOR_GTK|ARG_HAS_VALUE},
361 {"--sm-config-prefix", ARG_FOR_GTK|ARG_HAS_VALUE},
362 {"--sm-disable", ARG_FOR_GTK},
363 {"--oaf-ior-fd", ARG_FOR_GTK|ARG_HAS_VALUE},
364 {"--oaf-activate-iid", ARG_FOR_GTK|ARG_HAS_VALUE},
365 {"--oaf-private", ARG_FOR_GTK},
366 {"--enable-sound", ARG_FOR_GTK},
367 {"--disable-sound", ARG_FOR_GTK},
368 {"--espeaker", ARG_FOR_GTK|ARG_HAS_VALUE},
369 {"-?", ARG_FOR_GTK|ARG_NEEDS_GUI},
370 {"--help", ARG_FOR_GTK|ARG_NEEDS_GUI|ARG_KEEP},
371 {"--usage", ARG_FOR_GTK|ARG_NEEDS_GUI},
372# if 0 /* conflicts with Vim's own --version argument */
373 {"--version", ARG_FOR_GTK|ARG_NEEDS_GUI},
374# endif
375 {"--disable-crash-dialog", ARG_FOR_GTK},
376#endif
377 {NULL, 0}
378};
379
380static int gui_argc = 0;
381static char **gui_argv = NULL;
382
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383static const char *role_argument = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
385static const char *restart_command = NULL;
Bram Moolenaar9085f802009-06-03 14:20:21 +0000386static char *abs_restart_command = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387#endif
388static int found_iconic_arg = FALSE;
389
390#ifdef FEAT_GUI_GNOME
391/*
392 * Can't use Gnome if --socketid given
393 */
394static int using_gnome = 0;
395#else
396# define using_gnome 0
397#endif
398
399/*
400 * Parse the GUI related command-line arguments. Any arguments used are
401 * deleted from argv, and *argc is decremented accordingly. This is called
402 * when vim is started, whether or not the GUI has been started.
403 */
404 void
405gui_mch_prepare(int *argc, char **argv)
406{
407 const cmdline_option_T *option;
408 int i = 0;
409 int len = 0;
410
411#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
412 /*
413 * Determine the command used to invoke Vim, to be passed as restart
414 * command to the session manager. If argv[0] contains any directory
415 * components try building an absolute path, otherwise leave it as is.
416 */
417 restart_command = argv[0];
418
419 if (strchr(argv[0], G_DIR_SEPARATOR) != NULL)
420 {
421 char_u buf[MAXPATHL];
422
423 if (mch_FullName((char_u *)argv[0], buf, (int)sizeof(buf), TRUE) == OK)
Bram Moolenaar9085f802009-06-03 14:20:21 +0000424 {
425 abs_restart_command = (char *)vim_strsave(buf);
426 restart_command = abs_restart_command;
427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 }
429#endif
430
431 /*
432 * Move all the entries in argv which are relevant to GTK+ and GNOME
433 * into gui_argv. Freed later in gui_mch_init().
434 */
435 gui_argc = 0;
436 gui_argv = (char **)alloc((unsigned)((*argc + 1) * sizeof(char *)));
437
438 g_return_if_fail(gui_argv != NULL);
439
440 gui_argv[gui_argc++] = argv[i++];
441
442 while (i < *argc)
443 {
444 /* Don't waste CPU cycles on non-option arguments. */
445 if (argv[i][0] != '-' && argv[i][0] != '+')
446 {
447 ++i;
448 continue;
449 }
450
451 /* Look for argv[i] in cmdline_options[] table. */
452 for (option = &cmdline_options[0]; option->name != NULL; ++option)
453 {
454 len = strlen(option->name);
455
456 if (strncmp(argv[i], option->name, len) == 0)
457 {
458 if (argv[i][len] == '\0')
459 break;
460 /* allow --foo=bar style */
461 if (argv[i][len] == '=' && (option->flags & ARG_HAS_VALUE))
462 break;
463#ifdef FEAT_NETBEANS_INTG
464 /* darn, -nb has non-standard syntax */
465 if (vim_strchr((char_u *)":=", argv[i][len]) != NULL
466 && (option->flags & ARG_INDEX_MASK) == ARG_NETBEANS)
467 break;
468#endif
469 }
470 else if ((option->flags & ARG_COMPAT_LONG)
471 && strcmp(argv[i], option->name + 1) == 0)
472 {
473 /* Replace the standard X arguments "-name" and "-display"
474 * with their GNU-style long option counterparts. */
475 argv[i] = (char *)option->name;
476 break;
477 }
478 }
479 if (option->name == NULL) /* no match */
480 {
481 ++i;
482 continue;
483 }
484
485 if (option->flags & ARG_FOR_GTK)
486 {
487 /* Move the argument into gui_argv, which
488 * will later be passed to gtk_init_check() */
489 gui_argv[gui_argc++] = argv[i];
490 }
491 else
492 {
493 char *value = NULL;
494
495 /* Extract the option's value if there is one.
496 * Accept both "--foo bar" and "--foo=bar" style. */
497 if (option->flags & ARG_HAS_VALUE)
498 {
499 if (argv[i][len] == '=')
500 value = &argv[i][len + 1];
501 else if (i + 1 < *argc && strcmp(argv[i + 1], "--") != 0)
502 value = argv[i + 1];
503 }
504
505 /* Check for options handled by Vim itself */
506 switch (option->flags & ARG_INDEX_MASK)
507 {
508 case ARG_REVERSE:
509 found_reverse_arg = TRUE;
510 break;
511 case ARG_NOREVERSE:
512 found_reverse_arg = FALSE;
513 break;
514 case ARG_FONT:
515 font_argument = value;
516 break;
517 case ARG_GEOMETRY:
518 if (value != NULL)
519 gui.geom = vim_strsave((char_u *)value);
520 break;
521 case ARG_BACKGROUND:
522 background_argument = value;
523 break;
524 case ARG_FOREGROUND:
525 foreground_argument = value;
526 break;
527 case ARG_ICONIC:
528 found_iconic_arg = TRUE;
529 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 case ARG_ROLE:
531 role_argument = value; /* used later in gui_mch_open() */
532 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533#ifdef FEAT_NETBEANS_INTG
534 case ARG_NETBEANS:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 gui.dofork = FALSE; /* don't fork() when starting GUI */
536 netbeansArg = argv[i];
537 break;
538#endif
539 default:
540 break;
541 }
542 }
543
544 /* These arguments make gnome_program_init() print a message and exit.
545 * Must start the GUI for this, otherwise ":gui" will exit later! */
546 if (option->flags & ARG_NEEDS_GUI)
547 gui.starting = TRUE;
548
549 if (option->flags & ARG_KEEP)
550 ++i;
551 else
552 {
553 /* Remove the flag from the argument vector. */
554 if (--*argc > i)
555 {
556 int n_strip = 1;
557
558 /* Move the argument's value as well, if there is one. */
559 if ((option->flags & ARG_HAS_VALUE)
560 && argv[i][len] != '='
561 && strcmp(argv[i + 1], "--") != 0)
562 {
563 ++n_strip;
564 --*argc;
565 if (option->flags & ARG_FOR_GTK)
566 gui_argv[gui_argc++] = argv[i + 1];
567 }
568
569 if (*argc > i)
570 mch_memmove(&argv[i], &argv[i + n_strip],
571 (*argc - i) * sizeof(char *));
572 }
573 argv[*argc] = NULL;
574 }
575 }
576
577 gui_argv[gui_argc] = NULL;
578}
579
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000580#if defined(EXITFREE) || defined(PROTO)
581 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100582gui_mch_free_all(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000583{
584 vim_free(gui_argv);
Bram Moolenaar9085f802009-06-03 14:20:21 +0000585#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
586 vim_free(abs_restart_command);
587#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000588}
589#endif
590
Bram Moolenaar98921892016-02-23 17:14:37 +0100591#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592/*
593 * This should be maybe completely removed.
594 * Doesn't seem possible, since check_copy_area() relies on
595 * this information. --danielk
596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000598visibility_event(GtkWidget *widget UNUSED,
599 GdkEventVisibility *event,
600 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601{
602 gui.visibility = event->state;
603 /*
604 * When we do an gdk_window_copy_area(), and the window is partially
605 * obscured, we want to receive an event to tell us whether it worked
606 * or not.
607 */
608 if (gui.text_gc != NULL)
609 gdk_gc_set_exposures(gui.text_gc,
610 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
611 return FALSE;
612}
Bram Moolenaar98921892016-02-23 17:14:37 +0100613#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614
615/*
616 * Redraw the corresponding portions of the screen.
617 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100618#if GTK_CHECK_VERSION(3,0,0)
619static gboolean is_key_pressed = FALSE;
620
621static gboolean gui_gtk_is_blink_on(void);
622static gboolean gui_gtk_is_no_blink(void);
623static void gui_gtk_window_clear(GdkWindow *win);
624
625 static void
626gui_gtk3_redraw(int x, int y, int width, int height)
627{
628 gui_redraw_block(Y_2_ROW(y), X_2_COL(x),
629 Y_2_ROW(y + height - 1), X_2_COL(x + width - 1),
630 GUI_MON_NOCLEAR);
631}
632
633 static void
634gui_gtk3_update_cursor(cairo_t *cr)
635{
636 if (gui.row == gui.cursor_row)
637 {
638 gui.by_signal = TRUE;
639 gui_update_cursor(TRUE, TRUE);
640 gui.by_signal = FALSE;
641 cairo_paint(cr);
642 }
643}
644
645 static gboolean
646gui_gtk3_should_draw_cursor(void)
647{
648 unsigned int cond = 0;
649 cond |= gui_gtk_is_blink_on();
650 cond |= is_key_pressed;
651 cond |= gui.in_focus == FALSE;
652 cond |= gui_gtk_is_no_blink();
653 return cond;
654}
655
656 static gboolean
657draw_event(GtkWidget *widget,
658 cairo_t *cr,
659 gpointer user_data UNUSED)
660{
661 /* Skip this when the GUI isn't set up yet, will redraw later. */
662 if (gui.starting)
663 return FALSE;
664
665 out_flush(); /* make sure all output has been processed */
666 /* for GTK+ 3, may induce other draw events. */
667
668 cairo_set_source_surface(cr, gui.surface, 0, 0);
669
670 /* Draw the window without the cursor. */
671 gui.by_signal = TRUE;
672 {
673 cairo_rectangle_list_t *list = NULL;
674
675 gui_gtk_window_clear(gtk_widget_get_window(widget));
676
677 list = cairo_copy_clip_rectangle_list(cr);
678 if (list->status != CAIRO_STATUS_CLIP_NOT_REPRESENTABLE)
679 {
680 int i;
681 for (i = 0; i < list->num_rectangles; i++)
682 {
683 const cairo_rectangle_t rect = list->rectangles[i];
684 gui_gtk3_redraw(rect.x, rect.y, rect.width, rect.height);
685 }
686 }
687 cairo_rectangle_list_destroy(list);
688
689 cairo_paint(cr);
690 }
691 gui.by_signal = FALSE;
692
693 /* Add the cursor to the window if necessary.*/
694 if (gui_gtk3_should_draw_cursor())
695 gui_gtk3_update_cursor(cr);
696
697 return FALSE;
698}
699#else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000701expose_event(GtkWidget *widget UNUSED,
702 GdkEventExpose *event,
703 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704{
705 /* Skip this when the GUI isn't set up yet, will redraw later. */
706 if (gui.starting)
707 return FALSE;
708
709 out_flush(); /* make sure all output has been processed */
710 gui_redraw(event->area.x, event->area.y,
711 event->area.width, event->area.height);
712
713 /* Clear the border areas if needed */
714 if (event->area.x < FILL_X(0))
715 gdk_window_clear_area(gui.drawarea->window, 0, 0, FILL_X(0), 0);
716 if (event->area.y < FILL_Y(0))
717 gdk_window_clear_area(gui.drawarea->window, 0, 0, 0, FILL_Y(0));
718 if (event->area.x > FILL_X(Columns))
719 gdk_window_clear_area(gui.drawarea->window,
720 FILL_X((int)Columns), 0, 0, 0);
721 if (event->area.y > FILL_Y(Rows))
722 gdk_window_clear_area(gui.drawarea->window, 0, FILL_Y((int)Rows), 0, 0);
723
724 return FALSE;
725}
Bram Moolenaar98921892016-02-23 17:14:37 +0100726#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727
728#ifdef FEAT_CLIENTSERVER
729/*
730 * Handle changes to the "Comm" property
731 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000733property_event(GtkWidget *widget,
734 GdkEventProperty *event,
735 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736{
737 if (event->type == GDK_PROPERTY_NOTIFY
738 && event->state == (int)GDK_PROPERTY_NEW_VALUE
Bram Moolenaar98921892016-02-23 17:14:37 +0100739# if GTK_CHECK_VERSION(3,0,0)
740 && GDK_WINDOW_XID(event->window) == commWindow
741# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 && GDK_WINDOW_XWINDOW(event->window) == commWindow
Bram Moolenaar98921892016-02-23 17:14:37 +0100743# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 && GET_X_ATOM(event->atom) == commProperty)
745 {
746 XEvent xev;
747
748 /* Translate to XLib */
749 xev.xproperty.type = PropertyNotify;
750 xev.xproperty.atom = commProperty;
751 xev.xproperty.window = commWindow;
752 xev.xproperty.state = PropertyNewValue;
Bram Moolenaar98921892016-02-23 17:14:37 +0100753# if GTK_CHECK_VERSION(3,0,0)
754 serverEventProc(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(widget)),
755 &xev, 0);
756# else
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200757 serverEventProc(GDK_WINDOW_XDISPLAY(widget->window), &xev, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +0100758# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 }
760 return FALSE;
761}
Bram Moolenaar98921892016-02-23 17:14:37 +0100762#endif /* defined(FEAT_CLIENTSERVER) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763
764
765/****************************************************************************
766 * Focus handlers:
767 */
768
769
770/*
771 * This is a simple state machine:
772 * BLINK_NONE not blinking at all
773 * BLINK_OFF blinking, cursor is not shown
774 * BLINK_ON blinking, cursor is shown
775 */
776
777#define BLINK_NONE 0
778#define BLINK_OFF 1
779#define BLINK_ON 2
780
781static int blink_state = BLINK_NONE;
782static long_u blink_waittime = 700;
783static long_u blink_ontime = 400;
784static long_u blink_offtime = 250;
785static guint blink_timer = 0;
786
Bram Moolenaar98921892016-02-23 17:14:37 +0100787#if GTK_CHECK_VERSION(3,0,0)
788 static gboolean
789gui_gtk_is_blink_on(void)
790{
791 return blink_state == BLINK_ON;
792}
793
794 static gboolean
795gui_gtk_is_no_blink(void)
796{
797 return blink_waittime == 0 || blink_ontime == 0 || blink_offtime == 0;
798}
799#endif
800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 void
802gui_mch_set_blinking(long waittime, long on, long off)
803{
804 blink_waittime = waittime;
805 blink_ontime = on;
806 blink_offtime = off;
807}
808
809/*
810 * Stop the cursor blinking. Show the cursor if it wasn't shown.
811 */
812 void
813gui_mch_stop_blink(void)
814{
815 if (blink_timer)
816 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100817#if GTK_CHECK_VERSION(3,0,0)
818 g_source_remove(blink_timer);
819#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 gtk_timeout_remove(blink_timer);
Bram Moolenaar98921892016-02-23 17:14:37 +0100821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 blink_timer = 0;
823 }
824 if (blink_state == BLINK_OFF)
825 gui_update_cursor(TRUE, FALSE);
826 blink_state = BLINK_NONE;
827}
828
Bram Moolenaar98921892016-02-23 17:14:37 +0100829#if GTK_CHECK_VERSION(3,0,0)
830 static gboolean
831#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +0100833#endif
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000834blink_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835{
836 if (blink_state == BLINK_ON)
837 {
838 gui_undraw_cursor();
839 blink_state = BLINK_OFF;
Bram Moolenaar98921892016-02-23 17:14:37 +0100840#if GTK_CHECK_VERSION(3,0,0)
841 blink_timer = g_timeout_add((guint)blink_offtime,
842 (GSourceFunc) blink_cb, NULL);
843#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 blink_timer = gtk_timeout_add((guint32)blink_offtime,
845 (GtkFunction) blink_cb, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +0100846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 }
848 else
849 {
850 gui_update_cursor(TRUE, FALSE);
851 blink_state = BLINK_ON;
Bram Moolenaar98921892016-02-23 17:14:37 +0100852#if GTK_CHECK_VERSION(3,0,0)
853 blink_timer = g_timeout_add((guint)blink_ontime,
854 (GSourceFunc) blink_cb, NULL);
855#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 blink_timer = gtk_timeout_add((guint32)blink_ontime,
857 (GtkFunction) blink_cb, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +0100858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 }
860
861 return FALSE; /* don't happen again */
862}
863
864/*
865 * Start the cursor blinking. If it was already blinking, this restarts the
866 * waiting time and shows the cursor.
867 */
868 void
869gui_mch_start_blink(void)
870{
871 if (blink_timer)
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +0200872 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100873#if GTK_CHECK_VERSION(3,0,0)
874 g_source_remove(blink_timer);
875#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 gtk_timeout_remove(blink_timer);
Bram Moolenaar98921892016-02-23 17:14:37 +0100877#endif
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +0200878 blink_timer = 0;
879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 /* Only switch blinking on if none of the times is zero */
881 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
882 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100883#if GTK_CHECK_VERSION(3,0,0)
884 blink_timer = g_timeout_add((guint)blink_waittime,
885 (GSourceFunc) blink_cb, NULL);
886#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 blink_timer = gtk_timeout_add((guint32)blink_waittime,
888 (GtkFunction) blink_cb, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +0100889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 blink_state = BLINK_ON;
891 gui_update_cursor(TRUE, FALSE);
892 }
893}
894
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000896enter_notify_event(GtkWidget *widget UNUSED,
897 GdkEventCrossing *event UNUSED,
898 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899{
900 if (blink_state == BLINK_NONE)
901 gui_mch_start_blink();
902
903 /* make sure keyboard input goes there */
Bram Moolenaar98921892016-02-23 17:14:37 +0100904#if GTK_CHECK_VERSION(3,0,0)
905 if (gtk_socket_id == 0 || !gtk_widget_has_focus(gui.drawarea))
906#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 if (gtk_socket_id == 0 || !GTK_WIDGET_HAS_FOCUS(gui.drawarea))
Bram Moolenaar98921892016-02-23 17:14:37 +0100908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 gtk_widget_grab_focus(gui.drawarea);
910
911 return FALSE;
912}
913
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000915leave_notify_event(GtkWidget *widget UNUSED,
916 GdkEventCrossing *event UNUSED,
917 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918{
919 if (blink_state != BLINK_NONE)
920 gui_mch_stop_blink();
921
922 return FALSE;
923}
924
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000926focus_in_event(GtkWidget *widget,
927 GdkEventFocus *event UNUSED,
928 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929{
930 gui_focus_change(TRUE);
931
932 if (blink_state == BLINK_NONE)
933 gui_mch_start_blink();
934
Bram Moolenaar9c8791f2007-09-05 19:47:23 +0000935 /* make sure keyboard input goes to the draw area (if this is focus for a
936 * window) */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000937 if (widget != gui.drawarea)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000938 gtk_widget_grab_focus(gui.drawarea);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939
940 return TRUE;
941}
942
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000944focus_out_event(GtkWidget *widget UNUSED,
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200945 GdkEventFocus *event UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000946 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947{
948 gui_focus_change(FALSE);
949
950 if (blink_state != BLINK_NONE)
951 gui_mch_stop_blink();
952
953 return TRUE;
954}
955
956
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957/*
958 * Translate a GDK key value to UTF-8 independently of the current locale.
959 * The output is written to string, which must have room for at least 6 bytes
960 * plus the NUL terminator. Returns the length in bytes.
961 *
962 * This function is used in the GTK+ 2 GUI only. The GTK+ 1 code makes use
963 * of GdkEventKey::string instead. But event->string is evil; see here why:
964 * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey
965 */
966 static int
967keyval_to_string(unsigned int keyval, unsigned int state, char_u *string)
968{
969 int len;
970 guint32 uc;
971
972 uc = gdk_keyval_to_unicode(keyval);
973 if (uc != 0)
974 {
975 /* Check for CTRL-foo */
976 if ((state & GDK_CONTROL_MASK) && uc >= 0x20 && uc < 0x80)
977 {
978 /* These mappings look arbitrary at the first glance, but in fact
979 * resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
980 * machine. The only difference is BS vs. DEL for CTRL-8 (makes
981 * more sense and is consistent with usual terminal behaviour). */
982 if (uc >= '@')
983 string[0] = uc & 0x1F;
984 else if (uc == '2')
985 string[0] = NUL;
986 else if (uc >= '3' && uc <= '7')
987 string[0] = uc ^ 0x28;
988 else if (uc == '8')
989 string[0] = BS;
990 else if (uc == '?')
991 string[0] = DEL;
992 else
993 string[0] = uc;
994 len = 1;
995 }
996 else
997 {
998 /* Translate a normal key to UTF-8. This doesn't work for dead
999 * keys of course, you _have_ to use an input method for that. */
1000 len = utf_char2bytes((int)uc, string);
1001 }
1002 }
1003 else
1004 {
1005 /* Translate keys which are represented by ASCII control codes in Vim.
1006 * There are only a few of those; most control keys are translated to
1007 * special terminal-like control sequences. */
1008 len = 1;
1009 switch (keyval)
1010 {
1011 case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab:
1012 string[0] = TAB;
1013 break;
1014 case GDK_Linefeed:
1015 string[0] = NL;
1016 break;
1017 case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter:
1018 string[0] = CAR;
1019 break;
1020 case GDK_Escape:
1021 string[0] = ESC;
1022 break;
1023 default:
1024 len = 0;
1025 break;
1026 }
1027 }
1028 string[len] = NUL;
1029
1030 return len;
1031}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001033 static int
1034modifiers_gdk2vim(guint state)
1035{
1036 int modifiers = 0;
1037
1038 if (state & GDK_SHIFT_MASK)
1039 modifiers |= MOD_MASK_SHIFT;
1040 if (state & GDK_CONTROL_MASK)
1041 modifiers |= MOD_MASK_CTRL;
1042 if (state & GDK_MOD1_MASK)
1043 modifiers |= MOD_MASK_ALT;
Bram Moolenaar580061a2010-08-13 13:57:13 +02001044#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001045 if (state & GDK_SUPER_MASK)
1046 modifiers |= MOD_MASK_META;
1047#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001048 if (state & GDK_MOD4_MASK)
1049 modifiers |= MOD_MASK_META;
1050
1051 return modifiers;
1052}
1053
1054 static int
1055modifiers_gdk2mouse(guint state)
1056{
1057 int modifiers = 0;
1058
1059 if (state & GDK_SHIFT_MASK)
1060 modifiers |= MOUSE_SHIFT;
1061 if (state & GDK_CONTROL_MASK)
1062 modifiers |= MOUSE_CTRL;
1063 if (state & GDK_MOD1_MASK)
1064 modifiers |= MOUSE_ALT;
1065
1066 return modifiers;
1067}
1068
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069/*
1070 * Main keyboard handler:
1071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001073key_press_event(GtkWidget *widget UNUSED,
1074 GdkEventKey *event,
1075 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001077 /* For GTK+ 2 we know for sure how large the string might get.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 * (That is, up to 6 bytes + NUL + CSI escapes + safety measure.) */
1079 char_u string[32], string2[32];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 guint key_sym;
1081 int len;
1082 int i;
1083 int modifiers;
1084 int key;
1085 guint state;
1086 char_u *s, *d;
1087
Bram Moolenaar98921892016-02-23 17:14:37 +01001088#if GTK_CHECK_VERSION(3,0,0)
1089 is_key_pressed = TRUE;
1090 gui_mch_stop_blink();
1091#endif
1092
Bram Moolenaar20892c12011-06-26 04:49:00 +02001093 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 key_sym = event->keyval;
1095 state = event->state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096
1097#ifdef FEAT_XIM
1098 if (xim_queue_key_press_event(event, TRUE))
1099 return TRUE;
1100#endif
1101
1102#ifdef FEAT_HANGULIN
1103 if (key_sym == GDK_space && (state & GDK_SHIFT_MASK))
1104 {
1105 hangul_input_state_toggle();
1106 return TRUE;
1107 }
1108#endif
1109
1110#ifdef SunXK_F36
1111 /*
1112 * These keys have bogus lookup strings, and trapping them here is
1113 * easier than trying to XRebindKeysym() on them with every possible
1114 * combination of modifiers.
1115 */
1116 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
1117 len = 0;
1118 else
1119#endif
1120 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 len = keyval_to_string(key_sym, state, string2);
1122
1123 /* Careful: convert_input() doesn't handle the NUL character.
1124 * No need to convert pure ASCII anyway, thus the len > 1 check. */
1125 if (len > 1 && input_conv.vc_type != CONV_NONE)
1126 len = convert_input(string2, len, sizeof(string2));
1127
1128 s = string2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 d = string;
1130 for (i = 0; i < len; ++i)
1131 {
1132 *d++ = s[i];
1133 if (d[-1] == CSI && d + 2 < string + sizeof(string))
1134 {
1135 /* Turn CSI into K_CSI. */
1136 *d++ = KS_EXTRA;
1137 *d++ = (int)KE_CSI;
1138 }
1139 }
1140 len = d - string;
1141 }
1142
1143 /* Shift-Tab results in Left_Tab, but we want <S-Tab> */
1144 if (key_sym == GDK_ISO_Left_Tab)
1145 {
1146 key_sym = GDK_Tab;
1147 state |= GDK_SHIFT_MASK;
1148 }
1149
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150#ifdef FEAT_MENU
1151 /* If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key
1152 * is a menu shortcut, we ignore everything with the ALT modifier. */
1153 if ((state & GDK_MOD1_MASK)
1154 && gui.menu_is_active
1155 && (*p_wak == 'y'
1156 || (*p_wak == 'm'
1157 && len == 1
1158 && gui_is_menu_shortcut(string[0]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 /* For GTK2 we return false to signify that we haven't handled the
1160 * keypress, so that gtk will handle the mnemonic or accelerator. */
1161 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162#endif
1163
1164 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
1165 * that already has the 8th bit set.
1166 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
1167 * Don't do this for double-byte encodings, it turns the char into a lead
1168 * byte. */
1169 if (len == 1
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001170 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001171#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001172 || (state & GDK_SUPER_MASK)
1173#endif
1174 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 && !(key_sym == GDK_BackSpace || key_sym == GDK_Delete)
1176 && (string[0] & 0x80) == 0
1177 && !(key_sym == GDK_Tab && (state & GDK_SHIFT_MASK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 )
1180 {
1181 string[0] |= 0x80;
1182 state &= ~GDK_MOD1_MASK; /* don't use it again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 if (enc_utf8) /* convert to utf-8 */
1184 {
1185 string[1] = string[0] & 0xbf;
1186 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
1187 if (string[1] == CSI)
1188 {
1189 string[2] = KS_EXTRA;
1190 string[3] = (int)KE_CSI;
1191 len = 4;
1192 }
1193 else
1194 len = 2;
1195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 }
1197
1198 /* Check for special keys. Also do this when len == 1 (key has an ASCII
1199 * value) to detect backspace, delete and keypad keys. */
1200 if (len == 0 || len == 1)
1201 {
1202 for (i = 0; special_keys[i].key_sym != 0; i++)
1203 {
1204 if (special_keys[i].key_sym == key_sym)
1205 {
1206 string[0] = CSI;
1207 string[1] = special_keys[i].code0;
1208 string[2] = special_keys[i].code1;
1209 len = -3;
1210 break;
1211 }
1212 }
1213 }
1214
1215 if (len == 0) /* Unrecognized key */
1216 return TRUE;
1217
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 /* Special keys (and a few others) may have modifiers. Also when using a
1219 * double-byte encoding (can't set the 8th bit). */
1220 if (len == -3 || key_sym == GDK_space || key_sym == GDK_Tab
1221 || key_sym == GDK_Return || key_sym == GDK_Linefeed
1222 || key_sym == GDK_Escape || key_sym == GDK_KP_Tab
1223 || key_sym == GDK_ISO_Enter || key_sym == GDK_3270_Enter
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001224 || (enc_dbcs && len == 1 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001225#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001226 || (state & GDK_SUPER_MASK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227#endif
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001228 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001230 modifiers = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231
1232 /*
1233 * For some keys a shift modifier is translated into another key
1234 * code.
1235 */
1236 if (len == -3)
1237 key = TO_SPECIAL(string[1], string[2]);
1238 else
1239 key = string[0];
1240
1241 key = simplify_key(key, &modifiers);
1242 if (key == CSI)
1243 key = K_CSI;
1244 if (IS_SPECIAL(key))
1245 {
1246 string[0] = CSI;
1247 string[1] = K_SECOND(key);
1248 string[2] = K_THIRD(key);
1249 len = 3;
1250 }
1251 else
1252 {
1253 string[0] = key;
1254 len = 1;
1255 }
1256
1257 if (modifiers != 0)
1258 {
1259 string2[0] = CSI;
1260 string2[1] = KS_MODIFIER;
1261 string2[2] = modifiers;
1262 add_to_input_buf(string2, 3);
1263 }
1264 }
1265
1266 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1267 || (string[0] == intr_char && intr_char != Ctrl_C)))
1268 {
1269 trash_input_buf();
1270 got_int = TRUE;
1271 }
1272
1273 add_to_input_buf(string, len);
1274
1275 /* blank out the pointer if necessary */
1276 if (p_mh)
1277 gui_mch_mousehide(TRUE);
1278
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 return TRUE;
1280}
1281
Bram Moolenaar98921892016-02-23 17:14:37 +01001282#if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001284key_release_event(GtkWidget *widget UNUSED,
1285 GdkEventKey *event,
1286 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287{
Bram Moolenaar98921892016-02-23 17:14:37 +01001288# if GTK_CHECK_VERSION(3,0,0)
1289 is_key_pressed = FALSE;
1290 gui_mch_start_blink();
1291# endif
1292# if defined(FEAT_XIM)
Bram Moolenaar20892c12011-06-26 04:49:00 +02001293 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 /*
1295 * GTK+ 2 input methods may do fancy stuff on key release events too.
1296 * With the default IM for instance, you can enter any UCS code point
1297 * by holding down CTRL-SHIFT and typing hexadecimal digits.
1298 */
1299 return xim_queue_key_press_event(event, FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001300# else
1301 return TRUE;
1302# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303}
1304#endif
1305
1306
1307/****************************************************************************
1308 * Selection handlers:
1309 */
1310
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001312selection_clear_event(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 GdkEventSelection *event,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001314 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315{
1316 if (event->selection == clip_plus.gtk_sel_atom)
1317 clip_lose_selection(&clip_plus);
1318 else
1319 clip_lose_selection(&clip_star);
1320
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 return TRUE;
1322}
1323
1324#define RS_NONE 0 /* selection_received_cb() not called yet */
1325#define RS_OK 1 /* selection_received_cb() called and OK */
1326#define RS_FAIL 2 /* selection_received_cb() called and failed */
1327static int received_selection = RS_NONE;
1328
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001330selection_received_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 GtkSelectionData *data,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001332 guint time_ UNUSED,
1333 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334{
1335 VimClipboard *cbd;
1336 char_u *text;
1337 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 guchar *tmpbuf_utf8 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 int len;
Bram Moolenaard44347f2011-06-19 01:14:29 +02001340 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341
Bram Moolenaar98921892016-02-23 17:14:37 +01001342#if GTK_CHECK_VERSION(3,0,0)
1343 if (gtk_selection_data_get_selection(data) == clip_plus.gtk_sel_atom)
1344#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 if (data->selection == clip_plus.gtk_sel_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 cbd = &clip_plus;
1348 else
1349 cbd = &clip_star;
1350
Bram Moolenaar98921892016-02-23 17:14:37 +01001351#if GTK_CHECK_VERSION(3,0,0)
1352 text = (char_u *)gtk_selection_data_get_data(data);
1353 len = gtk_selection_data_get_length(data);
1354#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 text = (char_u *)data->data;
1356 len = data->length;
Bram Moolenaar98921892016-02-23 17:14:37 +01001357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358
1359 if (text == NULL || len <= 0)
1360 {
1361 received_selection = RS_FAIL;
1362 /* clip_free_selection(cbd); ??? */
1363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 return;
1365 }
1366
Bram Moolenaar98921892016-02-23 17:14:37 +01001367#if GTK_CHECK_VERSION(3,0,0)
1368 if (gtk_selection_data_get_data_type(data) == vim_atom)
1369#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 if (data->type == vim_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 {
1373 motion_type = *text++;
1374 --len;
1375 }
Bram Moolenaar98921892016-02-23 17:14:37 +01001376#if GTK_CHECK_VERSION(3,0,0)
1377 else if (gtk_selection_data_get_data_type(data) == vimenc_atom)
1378#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 else if (data->type == vimenc_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 {
1382 char_u *enc;
1383 vimconv_T conv;
1384
1385 motion_type = *text++;
1386 --len;
1387
1388 enc = text;
1389 text += STRLEN(text) + 1;
1390 len -= text - enc;
1391
1392 /* If the encoding of the text is different from 'encoding', attempt
1393 * converting it. */
1394 conv.vc_type = CONV_NONE;
1395 convert_setup(&conv, enc, p_enc);
1396 if (conv.vc_type != CONV_NONE)
1397 {
1398 tmpbuf = string_convert(&conv, text, &len);
1399 if (tmpbuf != NULL)
1400 text = tmpbuf;
1401 convert_setup(&conv, NULL, NULL);
1402 }
1403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 /* gtk_selection_data_get_text() handles all the nasty details
1406 * and targets and encodings etc. This rocks so hard. */
1407 else
1408 {
1409 tmpbuf_utf8 = gtk_selection_data_get_text(data);
1410 if (tmpbuf_utf8 != NULL)
1411 {
1412 len = STRLEN(tmpbuf_utf8);
1413 if (input_conv.vc_type != CONV_NONE)
1414 {
1415 tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len);
1416 if (tmpbuf != NULL)
1417 text = tmpbuf;
1418 }
1419 else
1420 text = tmpbuf_utf8;
1421 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001422 else if (len >= 2 && text[0] == 0xff && text[1] == 0xfe)
1423 {
1424 vimconv_T conv;
1425
1426 /* UTF-16, we get this for HTML */
1427 conv.vc_type = CONV_NONE;
1428 convert_setup_ext(&conv, (char_u *)"utf-16le", FALSE, p_enc, TRUE);
1429
1430 if (conv.vc_type != CONV_NONE)
1431 {
1432 text += 2;
1433 len -= 2;
1434 tmpbuf = string_convert(&conv, text, &len);
1435 convert_setup(&conv, NULL, NULL);
1436 }
1437 if (tmpbuf != NULL)
1438 text = tmpbuf;
1439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001442 /* Chop off any trailing NUL bytes. OpenOffice sends these. */
Bram Moolenaara76638f2010-06-05 12:49:46 +02001443 while (len > 0 && text[len - 1] == NUL)
1444 --len;
1445
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 clip_yank_selection(motion_type, text, (long)len, cbd);
1447 received_selection = RS_OK;
1448 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 g_free(tmpbuf_utf8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450}
1451
1452/*
1453 * Prepare our selection data for passing it to the external selection
1454 * client.
1455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001457selection_get_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 GtkSelectionData *selection_data,
1459 guint info,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001460 guint time_ UNUSED,
1461 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462{
1463 char_u *string;
1464 char_u *tmpbuf;
1465 long_u tmplen;
1466 int length;
1467 int motion_type;
1468 GdkAtom type;
1469 VimClipboard *cbd;
1470
Bram Moolenaar98921892016-02-23 17:14:37 +01001471#if GTK_CHECK_VERSION(3,0,0)
1472 if (gtk_selection_data_get_selection(selection_data)
1473 == clip_plus.gtk_sel_atom)
1474#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 if (selection_data->selection == clip_plus.gtk_sel_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 cbd = &clip_plus;
1478 else
1479 cbd = &clip_star;
1480
1481 if (!cbd->owned)
1482 return; /* Shouldn't ever happen */
1483
1484 if (info != (guint)TARGET_STRING
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001485 && (!clip_html || info != (guint)TARGET_HTML)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 && info != (guint)TARGET_UTF8_STRING
1487 && info != (guint)TARGET_VIMENC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 && info != (guint)TARGET_VIM
1489 && info != (guint)TARGET_COMPOUND_TEXT
1490 && info != (guint)TARGET_TEXT)
1491 return;
1492
1493 /* get the selection from the '*'/'+' register */
1494 clip_get_selection(cbd);
1495
1496 motion_type = clip_convert_selection(&string, &tmplen, cbd);
1497 if (motion_type < 0 || string == NULL)
1498 return;
1499 /* Due to int arguments we can't handle more than G_MAXINT. Also
1500 * reserve one extra byte for NUL or the motion type; just in case.
1501 * (Not that pasting 2G of text is ever going to work, but... ;-) */
1502 length = MIN(tmplen, (long_u)(G_MAXINT - 1));
1503
1504 if (info == (guint)TARGET_VIM)
1505 {
1506 tmpbuf = alloc((unsigned)length + 1);
1507 if (tmpbuf != NULL)
1508 {
1509 tmpbuf[0] = motion_type;
1510 mch_memmove(tmpbuf + 1, string, (size_t)length);
1511 }
1512 /* For our own format, the first byte contains the motion type */
1513 ++length;
1514 vim_free(string);
1515 string = tmpbuf;
1516 type = vim_atom;
1517 }
1518
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001519 else if (info == (guint)TARGET_HTML)
1520 {
1521 vimconv_T conv;
1522
1523 /* Since we get utf-16, we probably should set it as well. */
1524 conv.vc_type = CONV_NONE;
1525 convert_setup_ext(&conv, p_enc, TRUE, (char_u *)"utf-16le", FALSE);
1526 if (conv.vc_type != CONV_NONE)
1527 {
1528 tmpbuf = string_convert(&conv, string, &length);
1529 convert_setup(&conv, NULL, NULL);
1530 vim_free(string);
1531 string = tmpbuf;
1532 }
1533
1534 /* Prepend the BOM: "fffe" */
1535 if (string != NULL)
1536 {
1537 tmpbuf = alloc(length + 2);
1538 tmpbuf[0] = 0xff;
1539 tmpbuf[1] = 0xfe;
1540 mch_memmove(tmpbuf + 2, string, (size_t)length);
1541 vim_free(string);
1542 string = tmpbuf;
1543 length += 2;
1544
Bram Moolenaar98921892016-02-23 17:14:37 +01001545#if !GTK_CHECK_VERSION(3,0,0)
1546 /* Looks redandunt even for GTK2 because these values are
1547 * overwritten by gtk_selection_data_set() that follows. */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001548 selection_data->type = selection_data->target;
1549 selection_data->format = 16; /* 16 bits per char */
Bram Moolenaar98921892016-02-23 17:14:37 +01001550#endif
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001551 gtk_selection_data_set(selection_data, html_atom, 16,
1552 string, length);
1553 vim_free(string);
1554 }
1555 return;
1556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 else if (info == (guint)TARGET_VIMENC)
1558 {
1559 int l = STRLEN(p_enc);
1560
1561 /* contents: motion_type 'encoding' NUL text */
1562 tmpbuf = alloc((unsigned)length + l + 2);
1563 if (tmpbuf != NULL)
1564 {
1565 tmpbuf[0] = motion_type;
1566 STRCPY(tmpbuf + 1, p_enc);
1567 mch_memmove(tmpbuf + l + 2, string, (size_t)length);
1568 }
1569 length += l + 2;
1570 vim_free(string);
1571 string = tmpbuf;
1572 type = vimenc_atom;
1573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 /* gtk_selection_data_set_text() handles everything for us. This is
1576 * so easy and simple and cool, it'd be insane not to use it. */
1577 else
1578 {
1579 if (output_conv.vc_type != CONV_NONE)
1580 {
1581 tmpbuf = string_convert(&output_conv, string, &length);
1582 vim_free(string);
1583 if (tmpbuf == NULL)
1584 return;
1585 string = tmpbuf;
1586 }
1587 /* Validate the string to avoid runtime warnings */
1588 if (g_utf8_validate((const char *)string, (gssize)length, NULL))
1589 {
1590 gtk_selection_data_set_text(selection_data,
1591 (const char *)string, length);
1592 }
1593 vim_free(string);
1594 return;
1595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596
1597 if (string != NULL)
1598 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001599#if !GTK_CHECK_VERSION(3,0,0)
1600 /* Looks redandunt even for GTK2 because these values are
1601 * overwritten by gtk_selection_data_set() that follows. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 selection_data->type = selection_data->target;
1603 selection_data->format = 8; /* 8 bits per char */
Bram Moolenaar98921892016-02-23 17:14:37 +01001604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 gtk_selection_data_set(selection_data, type, 8, string, length);
1606 vim_free(string);
1607 }
1608}
1609
1610/*
Bram Moolenaar29695702012-05-18 17:03:18 +02001611 * Check if the GUI can be started. Called before gvimrc is sourced and
1612 * before fork().
1613 * Return OK or FAIL.
1614 */
1615 int
1616gui_mch_early_init_check(void)
1617{
1618 char_u *p;
1619
1620 /* Guess that when $DISPLAY isn't set the GUI can't start. */
1621 p = mch_getenv((char_u *)"DISPLAY");
1622 if (p == NULL || *p == NUL)
1623 {
1624 gui.dying = TRUE;
1625 EMSG(_((char *)e_opendisp));
1626 return FAIL;
1627 }
1628 return OK;
1629}
1630
1631/*
1632 * Check if the GUI can be started. Called before gvimrc is sourced but after
1633 * fork().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 * Return OK or FAIL.
1635 */
1636 int
1637gui_mch_init_check(void)
1638{
Bram Moolenaar3a466a82016-01-19 17:47:25 +01001639#ifdef USE_GRESOURCE
1640 static int res_registered = FALSE;
1641
1642 if (!res_registered)
1643 {
1644 /* Call this function in the GUI process; otherwise, the resources
1645 * won't be available. Don't call it twice. */
1646 res_registered = TRUE;
1647 gui_gtk_register_resource();
1648 }
1649#endif
1650
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651#ifdef FEAT_GUI_GNOME
1652 if (gtk_socket_id == 0)
1653 using_gnome = 1;
1654#endif
1655
Bram Moolenaar8dd79012013-05-21 12:52:04 +02001656 /* This defaults to argv[0], but we want it to match the name of the
1657 * shipped gvim.desktop so that Vim's windows can be associated with this
1658 * file. */
1659 g_set_prgname("gvim");
1660
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 /* Don't use gtk_init() or gnome_init(), it exits on failure. */
1662 if (!gtk_init_check(&gui_argc, &gui_argv))
1663 {
1664 gui.dying = TRUE;
Bram Moolenaarc93e7912008-07-08 10:46:08 +00001665 EMSG(_((char *)e_opendisp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 return FAIL;
1667 }
1668
1669 return OK;
1670}
1671
1672
1673/****************************************************************************
1674 * Mouse handling callbacks
1675 */
1676
1677
1678static guint mouse_click_timer = 0;
1679static int mouse_timed_out = TRUE;
1680
1681/*
1682 * Timer used to recognize multiple clicks of the mouse button
1683 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001684#if GTK_CHECK_VERSION(3,0,0)
1685 static gboolean
1686#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +01001688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689mouse_click_timer_cb(gpointer data)
1690{
1691 /* we don't use this information currently */
1692 int *timed_out = (int *) data;
1693
1694 *timed_out = TRUE;
1695 return FALSE; /* don't happen again */
1696}
1697
1698static guint motion_repeat_timer = 0;
1699static int motion_repeat_offset = FALSE;
Bram Moolenaar98921892016-02-23 17:14:37 +01001700#ifdef GTK_DEST_DEFAULT_ALL
1701static gboolean motion_repeat_timer_cb(gpointer);
1702#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703static gint motion_repeat_timer_cb(gpointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01001704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
1706 static void
1707process_motion_notify(int x, int y, GdkModifierType state)
1708{
1709 int button;
1710 int_u vim_modifiers;
Bram Moolenaar98921892016-02-23 17:14:37 +01001711#if GTK_CHECK_VERSION(3,0,0)
1712 GtkAllocation allocation;
1713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
1715 button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1716 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1717 GDK_BUTTON5_MASK))
1718 ? MOUSE_DRAG : ' ';
1719
1720 /* If our pointer is currently hidden, then we should show it. */
1721 gui_mch_mousehide(FALSE);
1722
1723 /* Just moving the rodent above the drawing area without any button
1724 * being pressed. */
1725 if (button != MOUSE_DRAG)
1726 {
1727 gui_mouse_moved(x, y);
1728 return;
1729 }
1730
1731 /* translate modifier coding between the main engine and GTK */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001732 vim_modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
Bram Moolenaar49325942007-05-10 19:19:59 +00001734 /* inform the editor engine about the occurrence of this event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 gui_send_mouse_event(button, x, y, FALSE, vim_modifiers);
1736
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 /*
1738 * Auto repeat timer handling.
1739 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001740#if GTK_CHECK_VERSION(3,0,0)
1741 gtk_widget_get_allocation(gui.drawarea, &allocation);
1742
1743 if (x < 0 || y < 0
1744 || x >= allocation.width
1745 || y >= allocation.height)
1746#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 if (x < 0 || y < 0
1748 || x >= gui.drawarea->allocation.width
1749 || y >= gui.drawarea->allocation.height)
Bram Moolenaar98921892016-02-23 17:14:37 +01001750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 {
1752
1753 int dx;
1754 int dy;
1755 int offshoot;
1756 int delay = 10;
1757
1758 /* Calculate the maximal distance of the cursor from the drawing area.
1759 * (offshoot can't become negative here!).
1760 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001761#if GTK_CHECK_VERSION(3,0,0)
1762 dx = x < 0 ? -x : x - allocation.width;
1763 dy = y < 0 ? -y : y - allocation.height;
1764#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 dx = x < 0 ? -x : x - gui.drawarea->allocation.width;
1766 dy = y < 0 ? -y : y - gui.drawarea->allocation.height;
Bram Moolenaar98921892016-02-23 17:14:37 +01001767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
1769 offshoot = dx > dy ? dx : dy;
1770
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001771 /* Make a linearly decaying timer delay with a threshold of 5 at a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 * distance of 127 pixels from the main window.
1773 *
1774 * One could think endlessly about the most ergonomic variant here.
1775 * For example it could make sense to calculate the distance from the
1776 * drags start instead...
1777 *
1778 * Maybe a parabolic interpolation would suite us better here too...
1779 */
1780 if (offshoot > 127)
1781 {
1782 /* 5 appears to be somehow near to my perceptual limits :-). */
1783 delay = 5;
1784 }
1785 else
1786 {
1787 delay = (130 * (127 - offshoot)) / 127 + 5;
1788 }
1789
1790 /* shoot again */
1791 if (!motion_repeat_timer)
Bram Moolenaar98921892016-02-23 17:14:37 +01001792#if GTK_CHECK_VERSION(3,0,0)
1793 motion_repeat_timer = g_timeout_add((guint)delay,
1794 motion_repeat_timer_cb, NULL);
1795#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 motion_repeat_timer = gtk_timeout_add((guint32)delay,
1797 motion_repeat_timer_cb, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01001798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 }
1800}
1801
Bram Moolenaar98921892016-02-23 17:14:37 +01001802#if GTK_CHECK_VERSION(3,0,0)
1803 static GdkDevice *
1804gui_gtk_get_pointer_device(GtkWidget *widget)
1805{
1806 GdkWindow * const win = gtk_widget_get_window(widget);
1807 GdkDisplay * const dpy = gdk_window_get_display(win);
1808 GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy);
1809 return gdk_device_manager_get_client_pointer(mngr);
1810}
1811
1812 static GdkWindow *
1813gui_gtk_get_pointer(GtkWidget *widget,
1814 gint *x,
1815 gint *y,
1816 GdkModifierType *state)
1817{
1818 GdkWindow * const win = gtk_widget_get_window(widget);
1819 GdkDevice * const dev = gui_gtk_get_pointer_device(widget);
1820 return gdk_window_get_device_position(win, dev , x, y, state);
1821}
1822
1823 static GdkWindow *
1824gui_gtk_window_at_position(GtkWidget *widget,
1825 gint *x,
1826 gint *y)
1827{
1828 GdkDevice * const dev = gui_gtk_get_pointer_device(widget);
1829 return gdk_device_get_window_at_position(dev, x, y);
1830}
1831#endif
1832
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833/*
1834 * Timer used to recognize multiple clicks of the mouse button.
1835 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001836#if GTK_CHECK_VERSION(3,0,0)
1837 static gboolean
1838#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +01001840#endif
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001841motion_repeat_timer_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842{
1843 int x;
1844 int y;
1845 GdkModifierType state;
1846
Bram Moolenaar98921892016-02-23 17:14:37 +01001847#if GTK_CHECK_VERSION(3,0,0)
1848 gui_gtk_get_pointer(gui.drawarea, &x, &y, &state);
1849#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 gdk_window_get_pointer(gui.drawarea->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +01001851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852
1853 if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1854 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1855 GDK_BUTTON5_MASK)))
1856 {
1857 motion_repeat_timer = 0;
1858 return FALSE;
1859 }
1860
1861 /* If there already is a mouse click in the input buffer, wait another
1862 * time (otherwise we would create a backlog of clicks) */
1863 if (vim_used_in_input_buf() > 10)
1864 return TRUE;
1865
1866 motion_repeat_timer = 0;
1867
1868 /*
1869 * Fake a motion event.
1870 * Trick: Pretend the mouse moved to the next character on every other
1871 * event, otherwise drag events will be discarded, because they are still
1872 * in the same character.
1873 */
1874 if (motion_repeat_offset)
1875 x += gui.char_width;
1876
1877 motion_repeat_offset = !motion_repeat_offset;
1878 process_motion_notify(x, y, state);
1879
1880 /* Don't happen again. We will get reinstalled in the synthetic event
1881 * if needed -- thus repeating should still work. */
1882 return FALSE;
1883}
1884
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001886motion_notify_event(GtkWidget *widget,
1887 GdkEventMotion *event,
1888 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889{
1890 if (event->is_hint)
1891 {
1892 int x;
1893 int y;
1894 GdkModifierType state;
1895
Bram Moolenaar98921892016-02-23 17:14:37 +01001896#if GTK_CHECK_VERSION(3,0,0)
1897 gui_gtk_get_pointer(widget, &x, &y, &state);
1898#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 gdk_window_get_pointer(widget->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +01001900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 process_motion_notify(x, y, state);
1902 }
1903 else
1904 {
1905 process_motion_notify((int)event->x, (int)event->y,
1906 (GdkModifierType)event->state);
1907 }
1908
1909 return TRUE; /* handled */
1910}
1911
1912
1913/*
1914 * Mouse button handling. Note please that we are capturing multiple click's
1915 * by our own timeout mechanism instead of the one provided by GTK+ itself.
1916 * This is due to the way the generic VIM code is recognizing multiple clicks.
1917 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001919button_press_event(GtkWidget *widget,
1920 GdkEventButton *event,
1921 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922{
1923 int button;
1924 int repeated_click = FALSE;
1925 int x, y;
1926 int_u vim_modifiers;
1927
Bram Moolenaar20892c12011-06-26 04:49:00 +02001928 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001929
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 /* Make sure we have focus now we've been selected */
Bram Moolenaar98921892016-02-23 17:14:37 +01001931#if GTK_CHECK_VERSION(3,0,0)
1932 if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget))
1933#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01001935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 gtk_widget_grab_focus(widget);
1937
1938 /*
1939 * Don't let additional events about multiple clicks send by GTK to us
1940 * after the initial button press event confuse us.
1941 */
1942 if (event->type != GDK_BUTTON_PRESS)
1943 return FALSE;
1944
1945 x = event->x;
1946 y = event->y;
1947
1948 /* Handle multiple clicks */
1949 if (!mouse_timed_out && mouse_click_timer)
1950 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001951#if GTK_CHECK_VERSION(3,0,0)
1952 g_source_remove(mouse_click_timer);
1953#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 gtk_timeout_remove(mouse_click_timer);
Bram Moolenaar98921892016-02-23 17:14:37 +01001955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 mouse_click_timer = 0;
1957 repeated_click = TRUE;
1958 }
1959
1960 mouse_timed_out = FALSE;
Bram Moolenaar98921892016-02-23 17:14:37 +01001961#if GTK_CHECK_VERSION(3,0,0)
1962 mouse_click_timer = g_timeout_add((guint)p_mouset,
1963 mouse_click_timer_cb, &mouse_timed_out);
1964#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 mouse_click_timer = gtk_timeout_add((guint32)p_mouset,
1966 mouse_click_timer_cb, &mouse_timed_out);
Bram Moolenaar98921892016-02-23 17:14:37 +01001967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968
1969 switch (event->button)
1970 {
Bram Moolenaar88e484b2015-11-24 15:38:44 +01001971 /* Keep in sync with gui_x11.c.
1972 * Buttons 4-7 are handled in scroll_event() */
1973 case 1: button = MOUSE_LEFT; break;
1974 case 2: button = MOUSE_MIDDLE; break;
1975 case 3: button = MOUSE_RIGHT; break;
1976 case 8: button = MOUSE_X1; break;
1977 case 9: button = MOUSE_X2; break;
1978 default:
1979 return FALSE; /* Unknown button */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 }
1981
1982#ifdef FEAT_XIM
1983 /* cancel any preediting */
1984 if (im_is_preediting())
1985 xim_reset();
1986#endif
1987
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001988 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989
1990 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991
1992 return TRUE;
1993}
1994
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995/*
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001996 * GTK+ 2 abstracts scrolling via the GdkEventScroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001999scroll_event(GtkWidget *widget,
2000 GdkEventScroll *event,
2001 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002{
2003 int button;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002004 int_u vim_modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005
Bram Moolenaar98921892016-02-23 17:14:37 +01002006#if GTK_CHECK_VERSION(3,0,0)
2007 if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget))
2008#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01002010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 gtk_widget_grab_focus(widget);
2012
2013 switch (event->direction)
2014 {
2015 case GDK_SCROLL_UP:
2016 button = MOUSE_4;
2017 break;
2018 case GDK_SCROLL_DOWN:
2019 button = MOUSE_5;
2020 break;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002021 case GDK_SCROLL_LEFT:
2022 button = MOUSE_7;
2023 break;
2024 case GDK_SCROLL_RIGHT:
2025 button = MOUSE_6;
2026 break;
2027 default: /* This shouldn't happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 return FALSE;
2029 }
2030
2031# ifdef FEAT_XIM
2032 /* cancel any preediting */
2033 if (im_is_preediting())
2034 xim_reset();
2035# endif
2036
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002037 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038
2039 gui_send_mouse_event(button, (int)event->x, (int)event->y,
2040 FALSE, vim_modifiers);
2041
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 return TRUE;
2043}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044
2045
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002047button_release_event(GtkWidget *widget UNUSED,
2048 GdkEventButton *event,
2049 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050{
2051 int x, y;
2052 int_u vim_modifiers;
2053
Bram Moolenaar20892c12011-06-26 04:49:00 +02002054 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002055
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 /* Remove any motion "machine gun" timers used for automatic further
2057 extension of allocation areas if outside of the applications window
2058 area .*/
2059 if (motion_repeat_timer)
2060 {
Bram Moolenaar98921892016-02-23 17:14:37 +01002061#if GTK_CHECK_VERSION(3,0,0)
2062 g_source_remove(motion_repeat_timer);
2063#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 gtk_timeout_remove(motion_repeat_timer);
Bram Moolenaar98921892016-02-23 17:14:37 +01002065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 motion_repeat_timer = 0;
2067 }
2068
2069 x = event->x;
2070 y = event->y;
2071
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002072 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
2074 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075
2076 return TRUE;
2077}
2078
2079
2080#ifdef FEAT_DND
2081/****************************************************************************
2082 * Drag aNd Drop support handlers.
2083 */
2084
2085/*
2086 * Count how many items there may be and separate them with a NUL.
2087 * Apparently the items are separated with \r\n. This is not documented,
2088 * thus be careful not to go past the end. Also allow separation with
2089 * NUL characters.
2090 */
2091 static int
2092count_and_decode_uri_list(char_u *out, char_u *raw, int len)
2093{
2094 int i;
2095 char_u *p = out;
2096 int count = 0;
2097
2098 for (i = 0; i < len; ++i)
2099 {
2100 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r')
2101 {
2102 if (p > out && p[-1] != NUL)
2103 {
2104 ++count;
2105 *p++ = NUL;
2106 }
2107 }
2108 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0)
2109 {
2110 *p++ = hexhex2nr(raw + i + 1);
2111 i += 2;
2112 }
2113 else
2114 *p++ = raw[i];
2115 }
2116 if (p > out && p[-1] != NUL)
2117 {
2118 *p = NUL; /* last item didn't have \r or \n */
2119 ++count;
2120 }
2121 return count;
2122}
2123
2124/*
2125 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
2126 * this process, URI which protocol is not "file:" are removed. Return
2127 * length of array (less than "max").
2128 */
2129 static int
2130filter_uri_list(char_u **outlist, int max, char_u *src)
2131{
2132 int i, j;
2133
2134 for (i = j = 0; i < max; ++i)
2135 {
2136 outlist[i] = NULL;
2137 if (STRNCMP(src, "file:", 5) == 0)
2138 {
2139 src += 5;
2140 if (STRNCMP(src, "//localhost", 11) == 0)
2141 src += 11;
2142 while (src[0] == '/' && src[1] == '/')
2143 ++src;
2144 outlist[j++] = vim_strsave(src);
2145 }
2146 src += STRLEN(src) + 1;
2147 }
2148 return j;
2149}
2150
2151 static char_u **
2152parse_uri_list(int *count, char_u *data, int len)
2153{
2154 int n = 0;
2155 char_u *tmp = NULL;
2156 char_u **array = NULL;;
2157
2158 if (data != NULL && len > 0 && (tmp = (char_u *)alloc(len + 1)) != NULL)
2159 {
2160 n = count_and_decode_uri_list(tmp, data, len);
2161 if (n > 0 && (array = (char_u **)alloc(n * sizeof(char_u *))) != NULL)
2162 n = filter_uri_list(array, n, tmp);
2163 }
2164 vim_free(tmp);
2165 *count = n;
2166 return array;
2167}
2168
2169 static void
2170drag_handle_uri_list(GdkDragContext *context,
2171 GtkSelectionData *data,
2172 guint time_,
2173 GdkModifierType state,
2174 gint x,
2175 gint y)
2176{
2177 char_u **fnames;
2178 int nfiles = 0;
2179
Bram Moolenaar98921892016-02-23 17:14:37 +01002180# if GTK_CHECK_VERSION(3,0,0)
2181 fnames = parse_uri_list(&nfiles,
2182 (char_u *)gtk_selection_data_get_data(data),
2183 gtk_selection_data_get_length(data));
2184# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 fnames = parse_uri_list(&nfiles, data->data, data->length);
Bram Moolenaar98921892016-02-23 17:14:37 +01002186# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
2188 if (fnames != NULL && nfiles > 0)
2189 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002190 int_u modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
2192 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
2193
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002194 modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195
2196 gui_handle_drop(x, y, modifiers, fnames, nfiles);
2197 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002198 else
2199 vim_free(fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200}
2201
2202 static void
2203drag_handle_text(GdkDragContext *context,
2204 GtkSelectionData *data,
2205 guint time_,
2206 GdkModifierType state)
2207{
2208 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP};
2209 char_u *text;
2210 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212
Bram Moolenaar98921892016-02-23 17:14:37 +01002213# if GTK_CHECK_VERSION(3,0,0)
2214 text = (char_u *)gtk_selection_data_get_data(data);
2215 len = gtk_selection_data_get_length(data);
2216# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 text = data->data;
2218 len = data->length;
Bram Moolenaar98921892016-02-23 17:14:37 +01002219# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220
Bram Moolenaar98921892016-02-23 17:14:37 +01002221# if GTK_CHECK_VERSION(3,0,0)
2222 if (gtk_selection_data_get_data_type(data) == utf8_string_atom)
2223# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 if (data->type == utf8_string_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01002225# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 if (input_conv.vc_type != CONV_NONE)
2228 tmpbuf = string_convert(&input_conv, text, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 if (tmpbuf != NULL)
2230 text = tmpbuf;
2231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232
2233 dnd_yank_drag_data(text, (long)len);
2234 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002237 dropkey[2] = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238
2239 if (dropkey[2] != 0)
2240 add_to_input_buf(dropkey, (int)sizeof(dropkey));
2241 else
2242 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243}
2244
2245/*
2246 * DND receiver.
2247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 static void
2249drag_data_received_cb(GtkWidget *widget,
2250 GdkDragContext *context,
2251 gint x,
2252 gint y,
2253 GtkSelectionData *data,
2254 guint info,
2255 guint time_,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002256 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257{
2258 GdkModifierType state;
2259
2260 /* Guard against trash */
Bram Moolenaar98921892016-02-23 17:14:37 +01002261# if GTK_CHECK_VERSION(3,0,0)
2262 const guchar * const data_data = gtk_selection_data_get_data(data);
2263 const gint data_length = gtk_selection_data_get_length(data);
2264 const gint data_format = gtk_selection_data_get_format(data);
2265
2266 if (data_data == NULL
2267 || data_length <= 0
2268 || data_format != 8
2269 || data_data[data_length] != '\0')
2270# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 if (data->data == NULL
2272 || data->length <= 0
2273 || data->format != 8
2274 || data->data[data->length] != '\0')
Bram Moolenaar98921892016-02-23 17:14:37 +01002275# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 {
2277 gtk_drag_finish(context, FALSE, FALSE, time_);
2278 return;
2279 }
2280
2281 /* Get the current modifier state for proper distinguishment between
2282 * different operations later. */
Bram Moolenaar98921892016-02-23 17:14:37 +01002283#if GTK_CHECK_VERSION(3,0,0)
2284 gui_gtk_get_pointer(widget, NULL, NULL, &state);
2285# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 gdk_window_get_pointer(widget->window, NULL, NULL, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +01002287# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288
2289 /* Not sure about the role of "text/plain" here... */
2290 if (info == (guint)TARGET_TEXT_URI_LIST)
2291 drag_handle_uri_list(context, data, time_, state, x, y);
2292 else
2293 drag_handle_text(context, data, time_, state);
2294
2295}
2296#endif /* FEAT_DND */
2297
2298
2299#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2300/*
2301 * GnomeClient interact callback. Check for unsaved buffers that cannot
2302 * be abandoned and pop up a dialog asking the user for confirmation if
2303 * necessary.
2304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002306sm_client_check_changed_any(GnomeClient *client UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 gint key,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002308 GnomeDialogType type UNUSED,
2309 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310{
2311 cmdmod_T save_cmdmod;
2312 gboolean shutdown_cancelled;
2313
2314 save_cmdmod = cmdmod;
2315
2316# ifdef FEAT_BROWSE
2317 cmdmod.browse = TRUE;
2318# endif
2319# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2320 cmdmod.confirm = TRUE;
2321# endif
2322 /*
2323 * If there are changed buffers, present the user with
2324 * a dialog if possible, otherwise give an error message.
2325 */
Bram Moolenaar027387f2016-01-02 22:25:52 +01002326 shutdown_cancelled = check_changed_any(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327
2328 exiting = FALSE;
2329 cmdmod = save_cmdmod;
2330 setcursor(); /* position the cursor */
2331 out_flush();
2332 /*
2333 * If the user hit the [Cancel] button the whole shutdown
2334 * will be cancelled. Wow, quite powerful feature (:
2335 */
2336 gnome_interaction_key_return(key, shutdown_cancelled);
2337}
2338
2339/*
2340 * Generate a script that can be used to restore the current editing session.
2341 * Save the value of v:this_session before running :mksession in order to make
2342 * automagic session save fully transparent. Return TRUE on success.
2343 */
2344 static int
2345write_session_file(char_u *filename)
2346{
2347 char_u *escaped_filename;
2348 char *mksession_cmdline;
2349 unsigned int save_ssop_flags;
2350 int failed;
2351
2352 /*
2353 * Build an ex command line to create a script that restores the current
2354 * session if executed. Escape the filename to avoid nasty surprises.
2355 */
2356 escaped_filename = vim_strsave_escaped(filename, escape_chars);
2357 if (escaped_filename == NULL)
2358 return FALSE;
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002359 mksession_cmdline = g_strconcat("mksession ", (char *)escaped_filename,
2360 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 vim_free(escaped_filename);
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002362
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 /*
2364 * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
2365 * unpredictable effects when the session is saved automatically. Also,
2366 * we definitely need SSOP_GLOBALS to be able to restore v:this_session.
2367 * Don't use SSOP_BUFFERS to prevent the buffer list from becoming
2368 * enormously large if the GNOME session feature is used regularly.
2369 */
2370 save_ssop_flags = ssop_flags;
2371 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002372 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373
2374 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
2375 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
2376 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
Bram Moolenaar485db9b2005-01-31 19:23:41 +00002377 do_unlet((char_u *)"Save_VV_this_session", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
2379 ssop_flags = save_ssop_flags;
2380 g_free(mksession_cmdline);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002381
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 /*
2383 * Reopen the file and append a command to restore v:this_session,
2384 * as if this save never happened. This is to avoid conflicts with
2385 * the user's own sessions. FIXME: It's probably less hackish to add
2386 * a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
2387 */
2388 if (!failed)
2389 {
2390 FILE *fd;
2391
2392 fd = open_exfile(filename, TRUE, APPENDBIN);
2393
2394 failed = (fd == NULL
2395 || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL
2396 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
2397
2398 if (fd != NULL && fclose(fd) != 0)
2399 failed = TRUE;
2400
2401 if (failed)
2402 mch_remove(filename);
2403 }
2404
2405 return !failed;
2406}
2407
2408/*
2409 * "save_yourself" signal handler. Initiate an interaction to ask the user
2410 * for confirmation if necessary. Save the current editing session and tell
2411 * the session manager how to restart Vim.
2412 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 static gboolean
2414sm_client_save_yourself(GnomeClient *client,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002415 gint phase UNUSED,
2416 GnomeSaveStyle save_style UNUSED,
2417 gboolean shutdown UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 GnomeInteractStyle interact_style,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002419 gboolean fast UNUSED,
2420 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421{
2422 static const char suffix[] = "-session.vim";
2423 char *session_file;
2424 unsigned int len;
2425 gboolean success;
2426
2427 /* Always request an interaction if possible. check_changed_any()
2428 * won't actually show a dialog unless any buffers have been modified.
2429 * There doesn't seem to be an obvious way to check that without
2430 * automatically firing the dialog. Anyway, it works just fine. */
2431 if (interact_style == GNOME_INTERACT_ANY)
2432 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL,
2433 &sm_client_check_changed_any,
2434 NULL);
2435 out_flush();
2436 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2437
2438 /* The path is unique for each session save. We do neither know nor care
2439 * which session script will actually be used later. This decision is in
2440 * the domain of the session manager. */
2441 session_file = gnome_config_get_real_path(
2442 gnome_client_get_config_prefix(client));
2443 len = strlen(session_file);
2444
2445 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR)
2446 --len; /* get rid of the superfluous trailing '/' */
2447
2448 session_file = g_renew(char, session_file, len + sizeof(suffix));
2449 memcpy(session_file + len, suffix, sizeof(suffix));
2450
2451 success = write_session_file((char_u *)session_file);
2452
2453 if (success)
2454 {
2455 const char *argv[8];
2456 int i;
2457
2458 /* Tell the session manager how to wipe out the stored session data.
2459 * This isn't as dangerous as it looks, don't worry :) session_file
2460 * is a unique absolute filename. Usually it'll be something like
2461 * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */
2462 i = 0;
2463 argv[i++] = "rm";
2464 argv[i++] = session_file;
2465 argv[i] = NULL;
2466
2467 gnome_client_set_discard_command(client, i, (char **)argv);
2468
2469 /* Tell the session manager how to restore the just saved session.
2470 * This is easily done thanks to Vim's -S option. Pass the -f flag
2471 * since there's no need to fork -- it might even cause confusion.
2472 * Also pass the window role to give the WM something to match on.
2473 * The role is set in gui_mch_open(), thus should _never_ be NULL. */
2474 i = 0;
2475 argv[i++] = restart_command;
2476 argv[i++] = "-f";
2477 argv[i++] = "-g";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 argv[i++] = "--role";
2479 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 argv[i++] = "-S";
2481 argv[i++] = session_file;
2482 argv[i] = NULL;
2483
2484 gnome_client_set_restart_command(client, i, (char **)argv);
2485 gnome_client_set_clone_command(client, 0, NULL);
2486 }
2487
2488 g_free(session_file);
2489
2490 return success;
2491}
2492
2493/*
2494 * Called when the session manager wants us to die. There isn't much to save
2495 * here since "save_yourself" has been emitted before (unless serious trouble
2496 * is happening).
2497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002499sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500{
2501 /* Don't write messages to the GUI anymore */
2502 full_screen = FALSE;
2503
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002504 vim_strncpy(IObuff, (char_u *)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002505 _("Vim: Received \"die\" request from session manager\n"),
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002506 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 preserve_exit();
2508}
2509
2510/*
2511 * Connect our signal handlers to be notified on session save and shutdown.
2512 */
2513 static void
2514setup_save_yourself(void)
2515{
2516 GnomeClient *client;
2517
2518 client = gnome_master_client();
2519
2520 if (client != NULL)
2521 {
2522 /* Must use the deprecated gtk_signal_connect() for compatibility
2523 * with GNOME 1. Arrgh, zombies! */
2524 gtk_signal_connect(GTK_OBJECT(client), "save_yourself",
2525 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL);
2526 gtk_signal_connect(GTK_OBJECT(client), "die",
2527 GTK_SIGNAL_FUNC(&sm_client_die), NULL);
2528 }
2529}
2530
2531#else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2532
2533# ifdef USE_XSMP
2534/*
2535 * GTK tells us that XSMP needs attention
2536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 static gboolean
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002538local_xsmp_handle_requests(
2539 GIOChannel *source UNUSED,
2540 GIOCondition condition,
2541 gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542{
2543 if (condition == G_IO_IN)
2544 {
2545 /* Do stuff; maybe close connection */
2546 if (xsmp_handle_requests() == FAIL)
2547 g_io_channel_unref((GIOChannel *)data);
2548 return TRUE;
2549 }
2550 /* Error */
2551 g_io_channel_unref((GIOChannel *)data);
2552 xsmp_close();
2553 return TRUE;
2554}
2555# endif /* USE_XSMP */
2556
2557/*
2558 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2559 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2560 */
2561 static void
2562setup_save_yourself(void)
2563{
2564 Atom *existing_atoms = NULL;
2565 int count = 0;
2566
Bram Moolenaar98921892016-02-23 17:14:37 +01002567# ifdef USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 if (xsmp_icefd != -1)
2569 {
2570 /*
2571 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2572 * set up GTK IO monitor
2573 */
2574 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd);
2575
2576 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP,
2577 local_xsmp_handle_requests, (gpointer)g_io);
Bram Moolenaar98921892016-02-23 17:14:37 +01002578 g_io_channel_unref(g_io);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 }
2580 else
Bram Moolenaar98921892016-02-23 17:14:37 +01002581# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 {
2583 /* Fall back to old method */
2584
2585 /* first get the existing value */
Bram Moolenaar98921892016-02-23 17:14:37 +01002586# if GTK_CHECK_VERSION(3,0,0)
2587 GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin);
2588
2589 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win),
2590 GDK_WINDOW_XID(mainwin_win),
2591 &existing_atoms, &count))
2592# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2594 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2595 &existing_atoms, &count))
Bram Moolenaar98921892016-02-23 17:14:37 +01002596# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 {
2598 Atom *new_atoms;
2599 Atom save_yourself_xatom;
2600 int i;
2601
2602 save_yourself_xatom = GET_X_ATOM(save_yourself_atom);
2603
2604 /* check if WM_SAVE_YOURSELF isn't there yet */
2605 for (i = 0; i < count; ++i)
2606 if (existing_atoms[i] == save_yourself_xatom)
2607 break;
2608
2609 if (i == count)
2610 {
2611 /* allocate an Atoms array which is one item longer */
2612 new_atoms = (Atom *)alloc((unsigned)((count + 1)
2613 * sizeof(Atom)));
2614 if (new_atoms != NULL)
2615 {
2616 memcpy(new_atoms, existing_atoms, count * sizeof(Atom));
2617 new_atoms[count] = save_yourself_xatom;
Bram Moolenaar98921892016-02-23 17:14:37 +01002618# if GTK_CHECK_VERSION(3,0,0)
2619 XSetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win),
2620 GDK_WINDOW_XID(mainwin_win),
2621# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 XSetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2623 GDK_WINDOW_XWINDOW(gui.mainwin->window),
Bram Moolenaar98921892016-02-23 17:14:37 +01002624# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 new_atoms, count + 1);
2626 vim_free(new_atoms);
2627 }
2628 }
2629 XFree(existing_atoms);
2630 }
2631 }
2632}
2633
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634/*
2635 * Installing a global event filter seems to be the only way to catch
2636 * client messages of type WM_PROTOCOLS without overriding GDK's own
2637 * client message event filter. Well, that's still better than trying
2638 * to guess what the GDK filter had done if it had been invoked instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 *
2640 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2641 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2642 * I have the nasty feeling modern session managers just don't send this
2643 * deprecated message anymore. Addition: confirmed by several people.
2644 *
2645 * The GNOME session support is much cooler anyway. Unlike this ugly
2646 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2647 * it should work with KDE as well.
2648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 static GdkFilterReturn
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002650global_event_filter(GdkXEvent *xev,
2651 GdkEvent *event UNUSED,
2652 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653{
2654 XEvent *xevent = (XEvent *)xev;
2655
2656 if (xevent != NULL
2657 && xevent->type == ClientMessage
2658 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002659 && (long_u)xevent->xclient.data.l[0]
2660 == GET_X_ATOM(save_yourself_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 {
2662 out_flush();
2663 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2664 /*
2665 * Set the window's WM_COMMAND property, to let the window manager
2666 * know we are done saving ourselves. We don't want to be
2667 * restarted, thus set argv to NULL.
2668 */
Bram Moolenaar98921892016-02-23 17:14:37 +01002669# if GTK_CHECK_VERSION(3,0,0)
2670 XSetCommand(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)),
2671 GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin)),
2672# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2674 GDK_WINDOW_XWINDOW(gui.mainwin->window),
Bram Moolenaar98921892016-02-23 17:14:37 +01002675# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 NULL, 0);
2677 return GDK_FILTER_REMOVE;
2678 }
2679
2680 return GDK_FILTER_CONTINUE;
2681}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682#endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2683
2684
2685/*
2686 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002689mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690{
2691/* If you get an error message here, you still need to unpack the runtime
2692 * archive! */
2693#ifdef magick
2694# undef magick
2695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 /* A bit hackish, but avoids casting later and allows optimization */
2697# define static static const
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698#define magick vim32x32
2699#include "../runtime/vim32x32.xpm"
2700#undef magick
2701#define magick vim16x16
2702#include "../runtime/vim16x16.xpm"
2703#undef magick
2704#define magick vim48x48
2705#include "../runtime/vim48x48.xpm"
2706#undef magick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707# undef static
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708
Bram Moolenaar98921892016-02-23 17:14:37 +01002709#if GTK_CHECK_VERSION(3,0,0)
2710 GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin);
2711#endif
2712
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 /* When started with "--echo-wid" argument, write window ID on stdout. */
2714 if (echo_wid_arg)
2715 {
Bram Moolenaar98921892016-02-23 17:14:37 +01002716#if GTK_CHECK_VERSION(3,0,0)
2717 printf("WID: %ld\n", (long)GDK_WINDOW_XID(mainwin_win));
2718#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 printf("WID: %ld\n", (long)GDK_WINDOW_XWINDOW(gui.mainwin->window));
Bram Moolenaar98921892016-02-23 17:14:37 +01002720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 fflush(stdout);
2722 }
2723
2724 if (vim_strchr(p_go, GO_ICON) != NULL)
2725 {
2726 /*
2727 * Add an icon to the main window. For fun and convenience of the user.
2728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 GList *icons = NULL;
2730
2731 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16));
2732 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32));
2733 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48));
2734
2735 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons);
2736
2737 g_list_foreach(icons, (GFunc)&g_object_unref, NULL);
2738 g_list_free(icons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 }
2740
2741#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
2742 /* Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 gdk_window_add_filter(NULL, &global_event_filter, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744#endif
2745 /* Setup to indicate to the window manager that we want to catch the
2746 * WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2747 * manager instead. */
2748#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2749 if (using_gnome)
2750#endif
2751 setup_save_yourself();
2752
2753#ifdef FEAT_CLIENTSERVER
2754 if (serverName == NULL && serverDelayedStartName != NULL)
2755 {
2756 /* This is a :gui command in a plain vim with no previous server */
Bram Moolenaar98921892016-02-23 17:14:37 +01002757# if GTK_CHECK_VERSION(3,0,0)
2758 commWindow = GDK_WINDOW_XID(mainwin_win);
2759
2760 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(mainwin_win),
2761 serverDelayedStartName);
2762# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 commWindow = GDK_WINDOW_XWINDOW(gui.mainwin->window);
2764
2765 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2766 serverDelayedStartName);
Bram Moolenaar98921892016-02-23 17:14:37 +01002767# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
2769 else
2770 {
2771 /*
2772 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2773 * have to change the "server" registration to that of the main window
2774 * If we have not registered a name yet, remember the window
2775 */
Bram Moolenaar98921892016-02-23 17:14:37 +01002776# if GTK_CHECK_VERSION(3,0,0)
2777 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(mainwin_win),
2778 GDK_WINDOW_XID(mainwin_win));
2779# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2781 GDK_WINDOW_XWINDOW(gui.mainwin->window));
Bram Moolenaar98921892016-02-23 17:14:37 +01002782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 }
2784 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK);
Bram Moolenaar98921892016-02-23 17:14:37 +01002785# if GTK_CHECK_VERSION(3,0,0)
2786 g_signal_connect(G_OBJECT(gui.mainwin), "property-notify-event",
2787 G_CALLBACK(property_event), NULL);
2788# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "property_notify_event",
2790 GTK_SIGNAL_FUNC(property_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01002791# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792#endif
2793}
2794
2795 static GdkCursor *
2796create_blank_pointer(void)
2797{
2798 GdkWindow *root_window = NULL;
Bram Moolenaar98921892016-02-23 17:14:37 +01002799#if GTK_CHECK_VERSION(3,0,0)
2800 GdkPixbuf *blank_mask;
2801#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 GdkPixmap *blank_mask;
Bram Moolenaar98921892016-02-23 17:14:37 +01002803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 GdkCursor *cursor;
2805 GdkColor color = { 0, 0, 0, 0 };
Bram Moolenaar98921892016-02-23 17:14:37 +01002806#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 char blank_data[] = { 0x0 };
Bram Moolenaar98921892016-02-23 17:14:37 +01002808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809
2810#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar98921892016-02-23 17:14:37 +01002811# if GTK_CHECK_VERSION(3,12,0)
2812 {
2813 GdkWindow * const win = gtk_widget_get_window(gui.mainwin);
2814 GdkScreen * const scrn = gdk_window_get_screen(win);
2815 root_window = gdk_screen_get_root_window(scrn);
2816 }
2817# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 root_window = gtk_widget_get_root_window(gui.mainwin);
Bram Moolenaar98921892016-02-23 17:14:37 +01002819# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820#endif
2821
2822 /* Create a pseudo blank pointer, which is in fact one pixel by one pixel
2823 * in size. */
Bram Moolenaar98921892016-02-23 17:14:37 +01002824#if GTK_CHECK_VERSION(3,0,0)
2825 {
2826 cairo_surface_t *surf;
2827 cairo_t *cr;
2828
2829 surf = cairo_image_surface_create(CAIRO_FORMAT_A1, 1, 1);
2830 cr = cairo_create(surf);
2831
2832 cairo_set_source_rgb(cr,
2833 color.red / 65535.0,
2834 color.green / 65535.0,
2835 color.blue / 65535.0);
2836 cairo_rectangle(cr, 0, 0, 1, 1);
2837 cairo_fill(cr);
2838 cairo_destroy(cr);
2839
2840 blank_mask = gdk_pixbuf_get_from_surface(surf, 0, 0, 1, 1);
2841 cairo_surface_destroy(surf);
2842
2843 cursor = gdk_cursor_new_from_pixbuf(gdk_window_get_display(root_window),
2844 blank_mask, 0, 0);
2845 g_object_unref(blank_mask);
2846 }
2847#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1);
2849 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask,
2850 &color, &color, 0, 0);
2851 gdk_bitmap_unref(blank_mask);
Bram Moolenaar98921892016-02-23 17:14:37 +01002852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853
2854 return cursor;
2855}
2856
2857#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 static void
2859mainwin_screen_changed_cb(GtkWidget *widget,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002860 GdkScreen *previous_screen UNUSED,
2861 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862{
2863 if (!gtk_widget_has_screen(widget))
2864 return;
2865
2866 /*
Bram Moolenaar49325942007-05-10 19:19:59 +00002867 * Recreate the invisible mouse cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 */
2869 if (gui.blank_pointer != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01002870# if GTK_CHECK_VERSION(3,0,0)
2871 g_object_unref(G_OBJECT(gui.blank_pointer));
2872# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 gdk_cursor_unref(gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01002874# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875
2876 gui.blank_pointer = create_blank_pointer();
2877
Bram Moolenaar98921892016-02-23 17:14:37 +01002878# if GTK_CHECK_VERSION(3,0,0)
2879 if (gui.pointer_hidden && gtk_widget_get_window(gui.drawarea) != NULL)
2880 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
2881 gui.blank_pointer);
2882# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 if (gui.pointer_hidden && gui.drawarea->window != NULL)
2884 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01002885# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886
2887 /*
2888 * Create a new PangoContext for this screen, and initialize it
2889 * with the current font if necessary.
2890 */
2891 if (gui.text_context != NULL)
2892 g_object_unref(gui.text_context);
2893
2894 gui.text_context = gtk_widget_create_pango_context(widget);
2895 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
2896
2897 if (gui.norm_font != NULL)
2898 {
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002899 gui_mch_init_font(p_guifont, FALSE);
Bram Moolenaar04a9d452006-03-27 21:03:26 +00002900 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 }
2902}
2903#endif /* HAVE_GTK_MULTIHEAD */
2904
2905/*
2906 * After the drawing area comes up, we calculate all colors and create the
2907 * dummy blank cursor.
2908 *
2909 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2910 * fact that the main VIM engine doesn't take them into account anywhere.
2911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002913drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914{
2915 GtkWidget *sbar;
Bram Moolenaar98921892016-02-23 17:14:37 +01002916#if GTK_CHECK_VERSION(3,0,0)
2917 GtkAllocation allocation;
2918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919
2920#ifdef FEAT_XIM
2921 xim_init();
2922#endif
2923 gui_mch_new_colors();
Bram Moolenaar98921892016-02-23 17:14:37 +01002924#if GTK_CHECK_VERSION(3,0,0)
2925 gui.surface = gdk_window_create_similar_surface(
2926 gtk_widget_get_window(widget),
2927 CAIRO_CONTENT_COLOR_ALPHA,
2928 gtk_widget_get_allocated_width(widget),
2929 gtk_widget_get_allocated_height(widget));
2930#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 gui.text_gc = gdk_gc_new(gui.drawarea->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01002932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933
2934 gui.blank_pointer = create_blank_pointer();
2935 if (gui.pointer_hidden)
Bram Moolenaar98921892016-02-23 17:14:37 +01002936#if GTK_CHECK_VERSION(3,0,0)
2937 gdk_window_set_cursor(gtk_widget_get_window(widget), gui.blank_pointer);
2938#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 gdk_window_set_cursor(widget->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01002940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941
2942 /* get the actual size of the scrollbars, if they are realized */
2943 sbar = firstwin->w_scrollbars[SBAR_LEFT].id;
2944 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT]
2945 && firstwin->w_scrollbars[SBAR_RIGHT].id))
2946 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id;
Bram Moolenaar98921892016-02-23 17:14:37 +01002947#if GTK_CHECK_VERSION(3,0,0)
2948 gtk_widget_get_allocation(sbar, &allocation);
2949 if (sbar && gtk_widget_get_realized(sbar) && allocation.width)
2950 gui.scrollbar_width = allocation.width;
2951#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.width)
2953 gui.scrollbar_width = sbar->allocation.width;
Bram Moolenaar98921892016-02-23 17:14:37 +01002954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955
2956 sbar = gui.bottom_sbar.id;
Bram Moolenaar98921892016-02-23 17:14:37 +01002957#if GTK_CHECK_VERSION(3,0,0)
2958 if (sbar && gtk_widget_get_realized(sbar) && allocation.height)
2959 gui.scrollbar_height = allocation.height;
2960#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.height)
2962 gui.scrollbar_height = sbar->allocation.height;
Bram Moolenaar98921892016-02-23 17:14:37 +01002963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964}
2965
2966/*
2967 * Properly clean up on shutdown.
2968 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002970drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971{
2972 /* Don't write messages to the GUI anymore */
2973 full_screen = FALSE;
2974
2975#ifdef FEAT_XIM
2976 im_shutdown();
2977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 if (gui.ascii_glyphs != NULL)
2979 {
2980 pango_glyph_string_free(gui.ascii_glyphs);
2981 gui.ascii_glyphs = NULL;
2982 }
2983 if (gui.ascii_font != NULL)
2984 {
2985 g_object_unref(gui.ascii_font);
2986 gui.ascii_font = NULL;
2987 }
2988 g_object_unref(gui.text_context);
2989 gui.text_context = NULL;
2990
Bram Moolenaar98921892016-02-23 17:14:37 +01002991#if GTK_CHECK_VERSION(3,0,0)
2992 if (gui.surface != NULL)
2993 {
2994 cairo_surface_destroy(gui.surface);
2995 gui.surface = NULL;
2996 }
2997#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 g_object_unref(gui.text_gc);
2999 gui.text_gc = NULL;
Bram Moolenaar98921892016-02-23 17:14:37 +01003000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001
Bram Moolenaar98921892016-02-23 17:14:37 +01003002#if GTK_CHECK_VERSION(3,0,0)
3003 g_object_unref(G_OBJECT(gui.blank_pointer));
3004#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 gdk_cursor_unref(gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01003006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 gui.blank_pointer = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008}
3009
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003011drawarea_style_set_cb(GtkWidget *widget UNUSED,
3012 GtkStyle *previous_style UNUSED,
3013 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014{
3015 gui_mch_new_colors();
3016}
3017
Bram Moolenaar98921892016-02-23 17:14:37 +01003018#if GTK_CHECK_VERSION(3,0,0)
3019 static gboolean
3020drawarea_configure_event_cb(GtkWidget *widget,
3021 GdkEventConfigure *event,
3022 gpointer data UNUSED)
3023{
3024 static int cur_width = 0;
3025 static int cur_height = 0;
3026
3027 g_return_val_if_fail(event
3028 && event->width >= 1 && event->height >= 1, TRUE);
3029
3030 if (event->width == cur_width && event->height == cur_height)
3031 return TRUE;
3032
3033 cur_width = event->width;
3034 cur_height = event->height;
3035
3036 if (gui.surface != NULL)
3037 cairo_surface_destroy(gui.surface);
3038
3039 gui.surface = gdk_window_create_similar_surface(
3040 gtk_widget_get_window(widget),
3041 CAIRO_CONTENT_COLOR_ALPHA,
3042 event->width, event->height);
3043
3044 gtk_widget_queue_draw(widget);
3045
3046 return TRUE;
3047}
3048#endif
3049
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050/*
3051 * Callback routine for the "delete_event" signal on the toplevel window.
3052 * Tries to vim gracefully, or refuses to exit with changed buffers.
3053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003055delete_event_cb(GtkWidget *widget UNUSED,
3056 GdkEventAny *event UNUSED,
3057 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058{
3059 gui_shell_closed();
3060 return TRUE;
3061}
3062
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003063#if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
3064 static int
3065get_item_dimensions(GtkWidget *widget, GtkOrientation orientation)
3066{
3067 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL;
3068
Bram Moolenaar98921892016-02-23 17:14:37 +01003069# ifdef FEAT_GUI_GNOME
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003070 if (using_gnome && widget != NULL)
3071 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003072 GtkWidget *parent;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003073 BonoboDockItem *dockitem;
3074
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003075 parent = gtk_widget_get_parent(widget);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003076 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM)
3077 {
3078 /* Only menu & toolbar are dock items. Could tabline be?
3079 * Seem to be only the 2 defined in GNOME */
3080 widget = parent;
3081 dockitem = BONOBO_DOCK_ITEM(widget);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003082
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003083 if (dockitem == NULL || dockitem->is_floating)
3084 return 0;
3085 item_orientation = bonobo_dock_item_get_orientation(dockitem);
3086 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003087 }
Bram Moolenaar98921892016-02-23 17:14:37 +01003088# endif
3089# if GTK_CHECK_VERSION(3,0,0)
3090 if (widget != NULL
3091 && item_orientation == orientation
3092 && gtk_widget_get_realized(widget)
3093 && gtk_widget_get_visible(widget))
3094# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003095 if (widget != NULL
3096 && item_orientation == orientation
3097 && GTK_WIDGET_REALIZED(widget)
3098 && GTK_WIDGET_VISIBLE(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01003099# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003100 {
Bram Moolenaar98921892016-02-23 17:14:37 +01003101# if GTK_CHECK_VERSION(3,0,0)
3102 GtkAllocation allocation;
3103
3104 gtk_widget_get_allocation(widget, &allocation);
3105
3106 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3107 return allocation.height;
3108 else
3109 return allocation.width;
3110# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003111 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3112 return widget->allocation.height;
3113 else
3114 return widget->allocation.width;
Bram Moolenaar98921892016-02-23 17:14:37 +01003115# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003116 }
3117 return 0;
3118}
3119#endif
3120
3121 static int
3122get_menu_tool_width(void)
3123{
3124 int width = 0;
3125
3126#ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */
3127# ifdef FEAT_MENU
3128 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL);
3129# endif
3130# ifdef FEAT_TOOLBAR
3131 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL);
3132# endif
3133# ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003134 if (gui.tabline != NULL)
3135 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003136# endif
3137#endif
3138
3139 return width;
3140}
3141
3142 static int
3143get_menu_tool_height(void)
3144{
3145 int height = 0;
3146
3147#ifdef FEAT_MENU
3148 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL);
3149#endif
3150#ifdef FEAT_TOOLBAR
3151 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL);
3152#endif
3153#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003154 if (gui.tabline != NULL)
3155 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003156#endif
3157
3158 return height;
3159}
3160
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003161/* This controls whether we can set the real window hints at
3162 * start-up when in a GtkPlug.
3163 * 0 = normal processing (default)
3164 * 1 = init. hints set, no-one's tried to reset since last check
3165 * 2 = init. hints set, attempt made to change hints
3166 */
3167static int init_window_hints_state = 0;
3168
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003169 static void
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003170update_window_manager_hints(int force_width, int force_height)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003171{
3172 static int old_width = 0;
3173 static int old_height = 0;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003174 static int old_min_width = 0;
3175 static int old_min_height = 0;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003176 static int old_char_width = 0;
3177 static int old_char_height = 0;
3178
3179 int width;
3180 int height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003181 int min_width;
3182 int min_height;
3183
3184 /* At start-up, don't try to set the hints until the initial
3185 * values have been used (those that dictate our initial size)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003186 * Let forced (i.e., correct) values through always.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003187 */
3188 if (!(force_width && force_height) && init_window_hints_state > 0)
3189 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003190 /* Don't do it! */
3191 init_window_hints_state = 2;
3192 return;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003193 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003194
3195 /* This also needs to be done when the main window isn't there yet,
3196 * otherwise the hints don't work. */
3197 width = gui_get_base_width();
3198 height = gui_get_base_height();
3199# ifdef FEAT_MENU
3200 height += tabline_height() * gui.char_height;
3201# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003202 width += get_menu_tool_width();
3203 height += get_menu_tool_height();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003204
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003205 /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
Bram Moolenaar49325942007-05-10 19:19:59 +00003206 * their actual widget size. When we set our size ourselves (e.g.,
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003207 * 'set columns=' or init. -geom) we briefly set the min. to the size
3208 * we wish to be instead of the legitimate minimum so that we actually
3209 * resize correctly.
3210 */
3211 if (force_width && force_height)
3212 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003213 min_width = force_width;
3214 min_height = force_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003215 }
3216 else
3217 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003218 min_width = width + MIN_COLUMNS * gui.char_width;
3219 min_height = height + MIN_LINES * gui.char_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003220 }
3221
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003222 /* Avoid an expose event when the size didn't change. */
3223 if (width != old_width
3224 || height != old_height
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003225 || min_width != old_min_width
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003226 || min_height != old_min_height
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003227 || gui.char_width != old_char_width
3228 || gui.char_height != old_char_height)
3229 {
3230 GdkGeometry geometry;
3231 GdkWindowHints geometry_mask;
3232
3233 geometry.width_inc = gui.char_width;
3234 geometry.height_inc = gui.char_height;
3235 geometry.base_width = width;
3236 geometry.base_height = height;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003237 geometry.min_width = min_width;
3238 geometry.min_height = min_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003239 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC
3240 |GDK_HINT_MIN_SIZE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003241 /* Using gui.formwin as geometry widget doesn't work as expected
3242 * with GTK+ 2 -- dunno why. Presumably all the resizing hacks
3243 * in Vim confuse GTK+. */
3244 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin,
3245 &geometry, geometry_mask);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003246 old_width = width;
3247 old_height = height;
3248 old_min_width = min_width;
3249 old_min_height = min_height;
3250 old_char_width = gui.char_width;
3251 old_char_height = gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003252 }
3253}
3254
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255#ifdef FEAT_TOOLBAR
3256
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257/*
3258 * This extra effort wouldn't be necessary if we only used stock icons in the
3259 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
3260 * shouldn't be treated differently, thus we do need this.
3261 */
3262 static void
3263icon_size_changed_foreach(GtkWidget *widget, gpointer user_data)
3264{
3265 if (GTK_IS_IMAGE(widget))
3266 {
3267 GtkImage *image = (GtkImage *)widget;
3268
Bram Moolenaar98921892016-02-23 17:14:37 +01003269# if GTK_CHECK_VERSION(3,10,0)
3270 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_NAME)
3271 {
3272 const GtkIconSize icon_size = GPOINTER_TO_INT(user_data);
3273 const gchar *icon_name;
3274
3275 gtk_image_get_icon_name(image, &icon_name, NULL);
3276
3277 gtk_image_set_from_icon_name(image, icon_name, icon_size);
3278 }
3279# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 /* User-defined icons are stored in a GtkIconSet */
3281 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET)
3282 {
3283 GtkIconSet *icon_set;
3284 GtkIconSize icon_size;
3285
3286 gtk_image_get_icon_set(image, &icon_set, &icon_size);
3287 icon_size = (GtkIconSize)(long)user_data;
3288
3289 gtk_icon_set_ref(icon_set);
3290 gtk_image_set_from_icon_set(image, icon_set, icon_size);
3291 gtk_icon_set_unref(icon_set);
3292 }
Bram Moolenaar98921892016-02-23 17:14:37 +01003293# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 }
3295 else if (GTK_IS_CONTAINER(widget))
3296 {
3297 gtk_container_foreach((GtkContainer *)widget,
3298 &icon_size_changed_foreach,
3299 user_data);
3300 }
3301}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302
3303 static void
3304set_toolbar_style(GtkToolbar *toolbar)
3305{
3306 GtkToolbarStyle style;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 GtkIconSize size;
3308 GtkIconSize oldsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3311 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3312 style = GTK_TOOLBAR_BOTH_HORIZ;
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003313 else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 == (TOOLBAR_TEXT | TOOLBAR_ICONS))
3315 style = GTK_TOOLBAR_BOTH;
3316 else if (toolbar_flags & TOOLBAR_TEXT)
3317 style = GTK_TOOLBAR_TEXT;
3318 else
3319 style = GTK_TOOLBAR_ICONS;
3320
3321 gtk_toolbar_set_style(toolbar, style);
Bram Moolenaar98921892016-02-23 17:14:37 +01003322# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003324# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 switch (tbis_flags)
3327 {
3328 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break;
3329 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break;
3330 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break;
3331 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
3332 default: size = GTK_ICON_SIZE_INVALID; break;
3333 }
3334 oldsize = gtk_toolbar_get_icon_size(toolbar);
3335
3336 if (size == GTK_ICON_SIZE_INVALID)
3337 {
3338 /* Let global user preferences decide the icon size. */
3339 gtk_toolbar_unset_icon_size(toolbar);
3340 size = gtk_toolbar_get_icon_size(toolbar);
3341 }
3342 if (size != oldsize)
3343 {
3344 gtk_container_foreach(GTK_CONTAINER(toolbar),
3345 &icon_size_changed_foreach,
3346 GINT_TO_POINTER((int)size));
3347 }
3348 gtk_toolbar_set_icon_size(toolbar, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349}
3350
3351#endif /* FEAT_TOOLBAR */
3352
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003353#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3354static int ignore_tabline_evt = FALSE;
Bram Moolenaara5621492006-02-25 21:55:24 +00003355static GtkWidget *tabline_menu;
Bram Moolenaar98921892016-02-23 17:14:37 +01003356# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003357static GtkTooltips *tabline_tooltip;
Bram Moolenaar98921892016-02-23 17:14:37 +01003358# endif
Bram Moolenaara5621492006-02-25 21:55:24 +00003359static int clicked_page; /* page clicked in tab line */
3360
3361/*
3362 * Handle selecting an item in the tab line popup menu.
3363 */
Bram Moolenaara5621492006-02-25 21:55:24 +00003364 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003365tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data)
Bram Moolenaara5621492006-02-25 21:55:24 +00003366{
Bram Moolenaara5621492006-02-25 21:55:24 +00003367 /* Add the string cmd into input buffer */
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003368 send_tabline_menu_event(clicked_page, (int)(long)user_data);
Bram Moolenaara5621492006-02-25 21:55:24 +00003369}
3370
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003371 static void
3372add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp)
3373{
3374 GtkWidget *item;
3375 char_u *utf_text;
3376
3377 utf_text = CONVERT_TO_UTF8(text);
3378 item = gtk_menu_item_new_with_label((const char *)utf_text);
3379 gtk_widget_show(item);
3380 CONVERT_TO_UTF8_FREE(utf_text);
3381
3382 gtk_container_add(GTK_CONTAINER(menu), item);
Bram Moolenaar98921892016-02-23 17:14:37 +01003383# if GTK_CHECK_VERSION(3,0,0)
3384 g_signal_connect(G_OBJECT(item), "activate",
3385 G_CALLBACK(tabline_menu_handler),
3386 GINT_TO_POINTER(resp));
3387# else
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003388 gtk_signal_connect(GTK_OBJECT(item), "activate",
3389 GTK_SIGNAL_FUNC(tabline_menu_handler),
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003390 (gpointer)(long)resp);
Bram Moolenaar98921892016-02-23 17:14:37 +01003391# endif
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003392}
3393
Bram Moolenaara5621492006-02-25 21:55:24 +00003394/*
Bram Moolenaara5621492006-02-25 21:55:24 +00003395 * Create a menu for the tab line.
3396 */
3397 static GtkWidget *
3398create_tabline_menu(void)
3399{
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003400 GtkWidget *menu;
Bram Moolenaara5621492006-02-25 21:55:24 +00003401
3402 menu = gtk_menu_new();
Bram Moolenaar7098ee52015-06-09 19:14:24 +02003403 if (first_tabpage->tp_next != NULL)
3404 add_tabline_menu_item(menu, (char_u *)_("Close tab"),
3405 TABLINE_MENU_CLOSE);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003406 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW);
3407 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN);
Bram Moolenaara5621492006-02-25 21:55:24 +00003408
3409 return menu;
3410}
3411
3412 static gboolean
3413on_tabline_menu(GtkWidget *widget, GdkEvent *event)
3414{
3415 /* Was this button press event ? */
3416 if (event->type == GDK_BUTTON_PRESS)
3417 {
3418 GdkEventButton *bevent = (GdkEventButton *)event;
3419 int x = bevent->x;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003420 int y = bevent->y;
3421 GtkWidget *tabwidget;
3422 GdkWindow *tabwin;
Bram Moolenaara5621492006-02-25 21:55:24 +00003423
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003424 /* When ignoring events return TRUE so that the selected page doesn't
3425 * change. */
3426 if (hold_gui_events
3427# ifdef FEAT_CMDWIN
3428 || cmdwin_type != 0
3429# endif
3430 )
3431 return TRUE;
3432
Bram Moolenaar98921892016-02-23 17:14:37 +01003433# if GTK_CHECK_VERSION(3,0,0)
3434 tabwin = gui_gtk_window_at_position(gui.mainwin, &x, &y);
3435# else
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003436 tabwin = gdk_window_at_pointer(&x, &y);
Bram Moolenaar98921892016-02-23 17:14:37 +01003437# endif
3438
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003439 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget);
Bram Moolenaar98921892016-02-23 17:14:37 +01003440# if GTK_CHECK_VERSION(3,0,0)
3441 clicked_page = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(tabwidget),
3442 "tab_num"));
3443# else
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003444 clicked_page = (int)(long)gtk_object_get_user_data(
3445 GTK_OBJECT(tabwidget));
Bram Moolenaar98921892016-02-23 17:14:37 +01003446# endif
Bram Moolenaara5621492006-02-25 21:55:24 +00003447
3448 /* If the event was generated for 3rd button popup the menu. */
3449 if (bevent->button == 3)
3450 {
3451 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
3452 bevent->button, bevent->time);
3453 /* We handled the event. */
3454 return TRUE;
3455 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003456 else if (bevent->button == 1)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003457 {
Bram Moolenaar96351572006-05-05 21:16:59 +00003458 if (clicked_page == 0)
3459 {
Bram Moolenaar07354542007-09-15 12:07:46 +00003460 /* Click after all tabs moves to next tab page. When "x" is
3461 * small guess it's the left button. */
Bram Moolenaara3f41662010-07-11 19:01:06 +02003462 send_tabline_event(x < 50 ? -1 : 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00003463 }
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003464 }
Bram Moolenaara5621492006-02-25 21:55:24 +00003465 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003466
Bram Moolenaara5621492006-02-25 21:55:24 +00003467 /* We didn't handle the event. */
3468 return FALSE;
3469}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003470
3471/*
3472 * Handle selecting one of the tabs.
3473 */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003474 static void
3475on_select_tab(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003476 GtkNotebook *notebook UNUSED,
Bram Moolenaar98921892016-02-23 17:14:37 +01003477# if GTK_CHECK_VERSION(3,0,0)
3478 gpointer *page UNUSED,
3479# else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003480 GtkNotebookPage *page UNUSED,
Bram Moolenaar98921892016-02-23 17:14:37 +01003481# endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00003482 gint idx,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003483 gpointer data UNUSED)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003484{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003485 if (!ignore_tabline_evt)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003486 {
Bram Moolenaara3f41662010-07-11 19:01:06 +02003487 send_tabline_event(idx + 1);
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003488 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003489}
3490
3491/*
3492 * Show or hide the tabline.
3493 */
3494 void
3495gui_mch_show_tabline(int showit)
3496{
3497 if (gui.tabline == NULL)
3498 return;
3499
3500 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)))
3501 {
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003502 /* Note: this may cause a resize event */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003503 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003504 update_window_manager_hints(0, 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00003505 if (showit)
Bram Moolenaar98921892016-02-23 17:14:37 +01003506# if GTK_CHECK_VERSION(3,0,0)
3507 gtk_widget_set_can_focus(GTK_WIDGET(gui.tabline), FALSE);
3508# else
Bram Moolenaar96351572006-05-05 21:16:59 +00003509 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui.tabline), GTK_CAN_FOCUS);
Bram Moolenaar98921892016-02-23 17:14:37 +01003510# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003511 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003512
3513 gui_mch_update();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003514}
3515
3516/*
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003517 * Return TRUE when tabline is displayed.
3518 */
3519 int
3520gui_mch_showing_tabline(void)
3521{
3522 return gui.tabline != NULL
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003523 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline));
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003524}
3525
3526/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003527 * Update the labels of the tabline.
3528 */
3529 void
3530gui_mch_update_tabline(void)
3531{
3532 GtkWidget *page;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003533 GtkWidget *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003534 GtkWidget *label;
3535 tabpage_T *tp;
3536 int nr = 0;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003537 int tab_num;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003538 int curtabidx = 0;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003539 char_u *labeltext;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003540
3541 if (gui.tabline == NULL)
3542 return;
3543
3544 ignore_tabline_evt = TRUE;
3545
3546 /* Add a label for each tab page. They all contain the same text area. */
3547 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3548 {
3549 if (tp == curtab)
3550 curtabidx = nr;
3551
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003552 tab_num = nr + 1;
3553
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003554 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr);
3555 if (page == NULL)
3556 {
3557 /* Add notebook page */
Bram Moolenaar98921892016-02-23 17:14:37 +01003558# if GTK_CHECK_VERSION(3,2,0)
3559 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3560 gtk_box_set_homogeneous(GTK_BOX(page), FALSE);
3561# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003562 page = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003563# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003564 gtk_widget_show(page);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003565 event_box = gtk_event_box_new();
3566 gtk_widget_show(event_box);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003567 label = gtk_label_new("-Empty-");
Bram Moolenaar98921892016-02-23 17:14:37 +01003568# if !GTK_CHECK_VERSION(3,14,0)
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003569 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01003570# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003571 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003572 gtk_widget_show(label);
3573 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline),
3574 page,
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003575 event_box,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003576 nr++);
3577 }
3578
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003579 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page);
Bram Moolenaar98921892016-02-23 17:14:37 +01003580# if GTK_CHECK_VERSION(3,0,0)
3581 g_object_set_data(G_OBJECT(event_box), "tab_num",
3582 GINT_TO_POINTER(tab_num));
3583# else
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003584 gtk_object_set_user_data(GTK_OBJECT(event_box),
3585 (gpointer)(long)tab_num);
Bram Moolenaar98921892016-02-23 17:14:37 +01003586# endif
3587# if GTK_CHECK_VERSION(3,0,0)
3588 label = gtk_bin_get_child(GTK_BIN(event_box));
3589# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003590 label = GTK_BIN(event_box)->child;
Bram Moolenaar98921892016-02-23 17:14:37 +01003591# endif
Bram Moolenaar57657d82006-04-21 22:12:41 +00003592 get_tabline_label(tp, FALSE);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003593 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003594 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003595 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003596
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003597 get_tabline_label(tp, TRUE);
3598 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar98921892016-02-23 17:14:37 +01003599# if GTK_CHECK_VERSION(3,0,0)
3600 gtk_widget_set_tooltip_text(event_box, (const gchar *)labeltext);
3601# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003602 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box,
3603 (const char *)labeltext, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01003604# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003605 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003606 }
3607
3608 /* Remove any old labels. */
3609 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL)
3610 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr);
3611
Bram Moolenaar98921892016-02-23 17:14:37 +01003612# if GTK_CHECK_VERSION(3,0,0)
3613 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
3614 gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
3615# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003616 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003617 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
Bram Moolenaar98921892016-02-23 17:14:37 +01003618# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003619
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003620 /* Make sure everything is in place before drawing text. */
3621 gui_mch_update();
3622
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003623 ignore_tabline_evt = FALSE;
3624}
3625
3626/*
3627 * Set the current tab to "nr". First tab is 1.
3628 */
3629 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003630gui_mch_set_curtab(int nr)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003631{
3632 if (gui.tabline == NULL)
3633 return;
3634
3635 ignore_tabline_evt = TRUE;
Bram Moolenaar98921892016-02-23 17:14:37 +01003636# if GTK_CHECK_VERSION(3,0,0)
3637 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
3638 gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
3639# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003640 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003641 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
Bram Moolenaar98921892016-02-23 17:14:37 +01003642# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003643 ignore_tabline_evt = FALSE;
3644}
3645
3646#endif /* FEAT_GUI_TABLINE */
3647
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648/*
Bram Moolenaara76638f2010-06-05 12:49:46 +02003649 * Add selection targets for PRIMARY and CLIPBOARD selections.
3650 */
3651 void
3652gui_gtk_set_selection_targets(void)
3653{
3654 int i, j = 0;
3655 int n_targets = N_SELECTION_TARGETS;
3656 GtkTargetEntry targets[N_SELECTION_TARGETS];
3657
3658 for (i = 0; i < (int)N_SELECTION_TARGETS; ++i)
3659 {
Bram Moolenaar29695702012-05-18 17:03:18 +02003660 /* OpenOffice tries to use TARGET_HTML and fails when we don't
Bram Moolenaara76638f2010-06-05 12:49:46 +02003661 * return something, instead of trying another target. Therefore only
3662 * offer TARGET_HTML when it works. */
3663 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3664 n_targets--;
3665 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003666 targets[j++] = selection_targets[i];
3667 }
3668
3669 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)GDK_SELECTION_PRIMARY);
3670 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)clip_plus.gtk_sel_atom);
3671 gtk_selection_add_targets(gui.drawarea,
3672 (GdkAtom)GDK_SELECTION_PRIMARY,
3673 targets, n_targets);
3674 gtk_selection_add_targets(gui.drawarea,
3675 (GdkAtom)clip_plus.gtk_sel_atom,
3676 targets, n_targets);
3677}
3678
3679/*
3680 * Set up for receiving DND items.
3681 */
3682 void
3683gui_gtk_set_dnd_targets(void)
3684{
3685 int i, j = 0;
3686 int n_targets = N_DND_TARGETS;
3687 GtkTargetEntry targets[N_DND_TARGETS];
3688
3689 for (i = 0; i < (int)N_DND_TARGETS; ++i)
3690 {
Bram Moolenaarb931d742011-10-26 11:36:25 +02003691 if (!clip_html && dnd_targets[i].info == TARGET_HTML)
Bram Moolenaara76638f2010-06-05 12:49:46 +02003692 n_targets--;
3693 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003694 targets[j++] = dnd_targets[i];
3695 }
3696
3697 gtk_drag_dest_unset(gui.drawarea);
3698 gtk_drag_dest_set(gui.drawarea,
3699 GTK_DEST_DEFAULT_ALL,
3700 targets, n_targets,
Bram Moolenaarba7cc9f2011-02-25 17:10:27 +01003701 GDK_ACTION_COPY | GDK_ACTION_MOVE);
Bram Moolenaara76638f2010-06-05 12:49:46 +02003702}
3703
3704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3706 * Returns OK for success, FAIL when the GUI can't be started.
3707 */
3708 int
3709gui_mch_init(void)
3710{
3711 GtkWidget *vbox;
3712
3713#ifdef FEAT_GUI_GNOME
3714 /* Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3715 * exits on failure, but that's a non-issue because we already called
3716 * gtk_init_check() in gui_mch_init_check(). */
3717 if (using_gnome)
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003718 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT,
3720 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL);
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003721# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
Bram Moolenaar1ff32c52014-04-29 15:11:43 +02003722 {
3723 char *p = setlocale(LC_NUMERIC, NULL);
3724
3725 /* Make sure strtod() uses a decimal point, not a comma. Gnome
3726 * init may change it. */
3727 if (p == NULL || strcmp(p, "C") != 0)
3728 setlocale(LC_NUMERIC, "C");
3729 }
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003730# endif
3731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732#endif
3733 vim_free(gui_argv);
3734 gui_argv = NULL;
3735
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003736#if GLIB_CHECK_VERSION(2,1,3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 /* Set the human-readable application name */
3738 g_set_application_name("Vim");
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 /*
3741 * Force UTF-8 output no matter what the value of 'encoding' is.
3742 * did_set_string_option() in option.c prohibits changing 'termencoding'
3743 * to something else than UTF-8 if the GUI is in use.
3744 */
3745 set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0);
3746
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003747#ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 gui_gtk_register_stock_icons();
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 /* FIXME: Need to install the classic icons and a gtkrc.classic file.
3751 * The hard part is deciding install locations and the Makefile magic. */
Bram Moolenaar98921892016-02-23 17:14:37 +01003752#if !GTK_CHECK_VERSION(3,0,0)
3753# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 gtk_rc_parse("gtkrc");
Bram Moolenaar98921892016-02-23 17:14:37 +01003755# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756#endif
3757
3758 /* Initialize values */
3759 gui.border_width = 2;
3760 gui.scrollbar_width = SB_DEFAULT_WIDTH;
3761 gui.scrollbar_height = SB_DEFAULT_WIDTH;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003762 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 gui.fgcolor = g_new0(GdkColor, 1);
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003764 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 gui.bgcolor = g_new0(GdkColor, 1);
Bram Moolenaarf36d3692005-03-15 22:48:14 +00003766 /* LINTED: avoid warning: conversion to 'unsigned long' */
3767 gui.spcolor = g_new0(GdkColor, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768
3769 /* Initialise atoms */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00003770 html_atom = gdk_atom_intern("text/html", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
3773 /* Set default foreground and background colors. */
3774 gui.norm_pixel = gui.def_norm_pixel;
3775 gui.back_pixel = gui.def_back_pixel;
3776
3777 if (gtk_socket_id != 0)
3778 {
3779 GtkWidget *plug;
3780
3781 /* Use GtkSocket from another app. */
3782#ifdef HAVE_GTK_MULTIHEAD
3783 plug = gtk_plug_new_for_display(gdk_display_get_default(),
3784 gtk_socket_id);
3785#else
3786 plug = gtk_plug_new(gtk_socket_id);
3787#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01003788#if GTK_CHECK_VERSION(3,0,0)
3789 if (plug != NULL && gtk_plug_get_socket_window(GTK_PLUG(plug)) != NULL)
3790#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 if (plug != NULL && GTK_PLUG(plug)->socket_window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01003792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 {
3794 gui.mainwin = plug;
3795 }
3796 else
3797 {
3798 g_warning("Connection to GTK+ socket (ID %u) failed",
3799 (unsigned int)gtk_socket_id);
3800 /* Pretend we never wanted it if it failed (get own window) */
3801 gtk_socket_id = 0;
3802 }
3803 }
3804
3805 if (gtk_socket_id == 0)
3806 {
3807#ifdef FEAT_GUI_GNOME
3808 if (using_gnome)
3809 {
3810 gui.mainwin = gnome_app_new("Vim", NULL);
3811# ifdef USE_XSMP
3812 /* Use the GNOME save-yourself functionality now. */
3813 xsmp_close();
3814# endif
3815 }
3816 else
3817#endif
3818 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3819 }
3820
3821 gtk_widget_set_name(gui.mainwin, "vim-main-window");
3822
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 /* Create the PangoContext used for drawing all text. */
3824 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
3825 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826
Bram Moolenaar98921892016-02-23 17:14:37 +01003827#if GTK_CHECK_VERSION(3,0,0)
3828 gtk_container_set_border_width(GTK_CONTAINER(gui.mainwin), 0);
3829#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 gtk_container_border_width(GTK_CONTAINER(gui.mainwin), 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK);
3833
Bram Moolenaar98921892016-02-23 17:14:37 +01003834#if GTK_CHECK_VERSION(3,0,0)
3835 g_signal_connect(G_OBJECT(gui.mainwin), "delete-event",
3836 G_CALLBACK(&delete_event_cb), NULL);
3837
3838 g_signal_connect(G_OBJECT(gui.mainwin), "realize",
3839 G_CALLBACK(&mainwin_realize), NULL);
3840#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "delete_event",
3842 GTK_SIGNAL_FUNC(&delete_event_cb), NULL);
3843
3844 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "realize",
3845 GTK_SIGNAL_FUNC(&mainwin_realize), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01003846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847#ifdef HAVE_GTK_MULTIHEAD
3848 g_signal_connect(G_OBJECT(gui.mainwin), "screen_changed",
3849 G_CALLBACK(&mainwin_screen_changed_cb), NULL);
3850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 gui.accel_group = gtk_accel_group_new();
3852 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003854 /* A vertical box holds the menubar, toolbar and main text window. */
Bram Moolenaar98921892016-02-23 17:14:37 +01003855#if GTK_CHECK_VERSION(3,2,0)
3856 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3857 gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE);
3858#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 vbox = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861
3862#ifdef FEAT_GUI_GNOME
3863 if (using_gnome)
3864 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003865# if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 /* automagically restore menubar/toolbar placement */
3867 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE);
3868# endif
3869 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox);
3870 }
3871 else
3872#endif
3873 {
3874 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox);
3875 gtk_widget_show(vbox);
3876 }
3877
3878#ifdef FEAT_MENU
3879 /*
3880 * Create the menubar and handle
3881 */
3882 gui.menubar = gtk_menu_bar_new();
3883 gtk_widget_set_name(gui.menubar, "vim-menubar");
3884
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003885 /* Avoid that GTK takes <F10> away from us. */
3886 {
3887 GtkSettings *gtk_settings;
3888
3889 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
3890 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
3891 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003892
3893
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894# ifdef FEAT_GUI_GNOME
3895 if (using_gnome)
3896 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 BonoboDockItem *dockitem;
3898
3899 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar));
3900 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3901 GNOME_APP_MENUBAR_NAME);
Bram Moolenaardb552d602006-03-23 22:59:57 +00003902 /* We don't want the menu to float. */
3903 bonobo_dock_item_set_behavior(dockitem,
3904 bonobo_dock_item_get_behavior(dockitem)
3905 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 gui.menubar_h = GTK_WIDGET(dockitem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 }
3908 else
3909# endif /* FEAT_GUI_GNOME */
3910 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00003911 /* Always show the menubar, otherwise <F10> doesn't work. It may be
3912 * disabled in gui_init() later. */
3913 gtk_widget_show(gui.menubar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0);
3915 }
3916#endif /* FEAT_MENU */
3917
3918#ifdef FEAT_TOOLBAR
3919 /*
3920 * Create the toolbar and handle
3921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 /* some aesthetics on the toolbar */
Bram Moolenaar98921892016-02-23 17:14:37 +01003923# ifdef USE_GTK3
3924 /* TODO: Add GTK+ 3 code here using GtkCssProvider if neccessary. */
3925 /* N.B. Since the default value of GtkToolbar::button-relief is
3926 * GTK_RELIEF_NONE, there's no need to specify that, probably. */
3927# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 gtk_rc_parse_string(
3929 "style \"vim-toolbar-style\" {\n"
3930 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
3931 "}\n"
3932 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
Bram Moolenaar98921892016-02-23 17:14:37 +01003933# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 gui.toolbar = gtk_toolbar_new();
3935 gtk_widget_set_name(gui.toolbar, "vim-toolbar");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
3937
3938# ifdef FEAT_GUI_GNOME
3939 if (using_gnome)
3940 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 BonoboDockItem *dockitem;
3942
3943 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar));
3944 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3945 GNOME_APP_TOOLBAR_NAME);
3946 gui.toolbar_h = GTK_WIDGET(dockitem);
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003947 /* When the toolbar is floating it gets stuck. So long as that isn't
Bram Moolenaardb552d602006-03-23 22:59:57 +00003948 * fixed let's disallow floating. */
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003949 bonobo_dock_item_set_behavior(dockitem,
Bram Moolenaardb552d602006-03-23 22:59:57 +00003950 bonobo_dock_item_get_behavior(dockitem)
3951 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 }
3954 else
3955# endif /* FEAT_GUI_GNOME */
3956 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 if (vim_strchr(p_go, GO_TOOLBAR) != NULL
3958 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3959 gtk_widget_show(gui.toolbar);
3960 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0);
3961 }
3962#endif /* FEAT_TOOLBAR */
3963
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003964#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003965 /*
3966 * Use a Notebook for the tab pages labels. The labels are hidden by
3967 * default.
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003968 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003969 gui.tabline = gtk_notebook_new();
3970 gtk_widget_show(gui.tabline);
3971 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0);
3972 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
3973 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003974 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01003975# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar96351572006-05-05 21:16:59 +00003976 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01003977# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003978
Bram Moolenaar98921892016-02-23 17:14:37 +01003979# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003980 tabline_tooltip = gtk_tooltips_new();
3981 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip));
Bram Moolenaar98921892016-02-23 17:14:37 +01003982# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003983
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003984 {
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003985 GtkWidget *page, *label, *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003986
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003987 /* Add the first tab. */
Bram Moolenaar98921892016-02-23 17:14:37 +01003988# if GTK_CHECK_VERSION(3,2,0)
3989 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3990 gtk_box_set_homogeneous(GTK_BOX(page), FALSE);
3991# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003992 page = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003993# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003994 gtk_widget_show(page);
3995 gtk_container_add(GTK_CONTAINER(gui.tabline), page);
3996 label = gtk_label_new("-Empty-");
3997 gtk_widget_show(label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003998 event_box = gtk_event_box_new();
3999 gtk_widget_show(event_box);
Bram Moolenaar98921892016-02-23 17:14:37 +01004000# if GTK_CHECK_VERSION(3,0,0)
4001 g_object_set_data(G_OBJECT(event_box), "tab_num", GINT_TO_POINTER(1L));
4002# else
Bram Moolenaar47b46d72008-07-02 19:05:48 +00004003 gtk_object_set_user_data(GTK_OBJECT(event_box), (gpointer)1L);
Bram Moolenaar98921892016-02-23 17:14:37 +01004004# endif
4005# if !GTK_CHECK_VERSION(3,14,0)
Bram Moolenaar8ea91232006-04-28 22:41:43 +00004006 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01004007# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004008 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004009 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004010 }
Bram Moolenaar54a709e2006-05-04 21:57:11 +00004011
Bram Moolenaar98921892016-02-23 17:14:37 +01004012# if GTK_CHECK_VERSION(3,0,0)
4013 g_signal_connect(G_OBJECT(gui.tabline), "switch-page",
4014 G_CALLBACK(on_select_tab), NULL);
4015# else
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004016 gtk_signal_connect(GTK_OBJECT(gui.tabline), "switch_page",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004017 GTK_SIGNAL_FUNC(on_select_tab), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004018# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004019
4020 /* Create a popup menu for the tab line and connect it. */
4021 tabline_menu = create_tabline_menu();
Bram Moolenaar98921892016-02-23 17:14:37 +01004022# if GTK_CHECK_VERSION(3,0,0)
4023 g_signal_connect_swapped(G_OBJECT(gui.tabline), "button-press-event",
4024 G_CALLBACK(on_tabline_menu), G_OBJECT(tabline_menu));
4025# else
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004026 gtk_signal_connect_object(GTK_OBJECT(gui.tabline), "button_press_event",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004027 GTK_SIGNAL_FUNC(on_tabline_menu), GTK_OBJECT(tabline_menu));
Bram Moolenaar98921892016-02-23 17:14:37 +01004028# endif
4029#endif /* FEAT_GUI_TABLINE */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004030
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 gui.formwin = gtk_form_new();
Bram Moolenaar98921892016-02-23 17:14:37 +01004032#if GTK_CHECK_VERSION(3,0,0)
4033 gtk_container_set_border_width(GTK_CONTAINER(gui.formwin), 0);
4034#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 gtk_container_border_width(GTK_CONTAINER(gui.formwin), 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01004036#endif
4037#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK);
Bram Moolenaar98921892016-02-23 17:14:37 +01004039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040
4041 gui.drawarea = gtk_drawing_area_new();
Bram Moolenaar98921892016-02-23 17:14:37 +01004042#if GTK_CHECK_VERSION(3,0,0)
4043 gui.surface = NULL;
4044 gui.by_signal = FALSE;
4045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046
4047 /* Determine which events we will filter. */
4048 gtk_widget_set_events(gui.drawarea,
4049 GDK_EXPOSURE_MASK |
4050 GDK_ENTER_NOTIFY_MASK |
4051 GDK_LEAVE_NOTIFY_MASK |
4052 GDK_BUTTON_PRESS_MASK |
4053 GDK_BUTTON_RELEASE_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 GDK_SCROLL_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 GDK_KEY_PRESS_MASK |
4056 GDK_KEY_RELEASE_MASK |
4057 GDK_POINTER_MOTION_MASK |
4058 GDK_POINTER_MOTION_HINT_MASK);
4059
4060 gtk_widget_show(gui.drawarea);
4061 gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0);
4062 gtk_widget_show(gui.formwin);
4063 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0);
4064
4065 /* For GtkSockets, key-presses must go to the focus widget (drawarea)
4066 * and not the window. */
Bram Moolenaar98921892016-02-23 17:14:37 +01004067#if GTK_CHECK_VERSION(3,0,0)
4068 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
4069 : G_OBJECT(gui.drawarea),
4070 "key-press-event",
4071 G_CALLBACK(key_press_event), NULL);
4072#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 gtk_signal_connect((gtk_socket_id == 0) ? GTK_OBJECT(gui.mainwin)
4074 : GTK_OBJECT(gui.drawarea),
4075 "key_press_event",
4076 GTK_SIGNAL_FUNC(key_press_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004077#endif
4078#if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 /* Also forward key release events for the benefit of GTK+ 2 input
4080 * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */
4081 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
4082 : G_OBJECT(gui.drawarea),
Bram Moolenaar98921892016-02-23 17:14:37 +01004083 "key-release-event",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 G_CALLBACK(&key_release_event), NULL);
4085#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004086#if GTK_CHECK_VERSION(3,0,0)
4087 g_signal_connect(G_OBJECT(gui.drawarea), "realize",
4088 G_CALLBACK(drawarea_realize_cb), NULL);
4089 g_signal_connect(G_OBJECT(gui.drawarea), "unrealize",
4090 G_CALLBACK(drawarea_unrealize_cb), NULL);
4091 g_signal_connect(G_OBJECT(gui.drawarea), "configure-event",
4092 G_CALLBACK(drawarea_configure_event_cb), NULL);
4093 g_signal_connect_after(G_OBJECT(gui.drawarea), "style-set",
4094 G_CALLBACK(&drawarea_style_set_cb), NULL);
4095#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "realize",
4097 GTK_SIGNAL_FUNC(drawarea_realize_cb), NULL);
4098 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "unrealize",
4099 GTK_SIGNAL_FUNC(drawarea_unrealize_cb), NULL);
4100
4101 gtk_signal_connect_after(GTK_OBJECT(gui.drawarea), "style_set",
4102 GTK_SIGNAL_FUNC(&drawarea_style_set_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104
Bram Moolenaar98921892016-02-23 17:14:37 +01004105#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 gui.visibility = GDK_VISIBILITY_UNOBSCURED;
Bram Moolenaar98921892016-02-23 17:14:37 +01004107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108
4109#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
4110 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE);
4111 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE);
4112#endif
4113
4114 if (gtk_socket_id != 0)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004115 /* make sure keyboard input can go to the drawarea */
Bram Moolenaar98921892016-02-23 17:14:37 +01004116#if GTK_CHECK_VERSION(3,0,0)
4117 gtk_widget_set_can_focus(gui.drawarea, TRUE);
4118#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 GTK_WIDGET_SET_FLAGS(gui.drawarea, GTK_CAN_FOCUS);
Bram Moolenaar98921892016-02-23 17:14:37 +01004120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121
4122 /*
4123 * Set clipboard specific atoms
4124 */
4125 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY;
4128 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE);
4129
4130 /*
4131 * Start out by adding the configured border width into the border offset.
4132 */
4133 gui.border_offset = gui.border_width;
4134
Bram Moolenaar98921892016-02-23 17:14:37 +01004135#if GTK_CHECK_VERSION(3,0,0)
4136 g_signal_connect(G_OBJECT(gui.drawarea), "draw",
4137 G_CALLBACK(draw_event), NULL);
4138#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event",
4140 GTK_SIGNAL_FUNC(visibility_event), NULL);
4141 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event",
4142 GTK_SIGNAL_FUNC(expose_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
4145 /*
4146 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
4147 * Only needed for some window managers.
4148 */
4149 if (vim_strchr(p_go, GO_POINTER) != NULL)
4150 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004151#if GTK_CHECK_VERSION(3,0,0)
4152 g_signal_connect(G_OBJECT(gui.drawarea), "leave-notify-event",
4153 G_CALLBACK(leave_notify_event), NULL);
4154 g_signal_connect(G_OBJECT(gui.drawarea), "enter-notify-event",
4155 G_CALLBACK(enter_notify_event), NULL);
4156#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "leave_notify_event",
4158 GTK_SIGNAL_FUNC(leave_notify_event), NULL);
4159 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "enter_notify_event",
4160 GTK_SIGNAL_FUNC(enter_notify_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 }
4163
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004164 /* Real windows can get focus ... GtkPlug, being a mere container can't,
4165 * only its widgets. Arguably, this could be common code and we not use
4166 * the window focus at all, but let's be safe.
4167 */
4168 if (gtk_socket_id == 0)
4169 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004170#if GTK_CHECK_VERSION(3,0,0)
4171 g_signal_connect(G_OBJECT(gui.mainwin), "focus-out-event",
4172 G_CALLBACK(focus_out_event), NULL);
4173 g_signal_connect(G_OBJECT(gui.mainwin), "focus-in-event",
4174 G_CALLBACK(focus_in_event), NULL);
4175#else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004176 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_out_event",
4177 GTK_SIGNAL_FUNC(focus_out_event), NULL);
4178 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_in_event",
4179 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004180#endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004181 }
4182 else
4183 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004184#if GTK_CHECK_VERSION(3,0,0)
4185 g_signal_connect(G_OBJECT(gui.drawarea), "focus-out-event",
4186 G_CALLBACK(focus_out_event), NULL);
4187 g_signal_connect(G_OBJECT(gui.drawarea), "focus-in-event",
4188 G_CALLBACK(focus_in_event), NULL);
4189#else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004190 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_out_event",
4191 GTK_SIGNAL_FUNC(focus_out_event), NULL);
4192 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_in_event",
4193 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004194#endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004195#ifdef FEAT_GUI_TABLINE
Bram Moolenaar98921892016-02-23 17:14:37 +01004196# if GTK_CHECK_VERSION(3,0,0)
4197 g_signal_connect(G_OBJECT(gui.tabline), "focus-out-event",
4198 G_CALLBACK(focus_out_event), NULL);
4199 g_signal_connect(G_OBJECT(gui.tabline), "focus-in-event",
4200 G_CALLBACK(focus_in_event), NULL);
4201# else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004202 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_out_event",
4203 GTK_SIGNAL_FUNC(focus_out_event), NULL);
4204 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_in_event",
4205 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004206# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004207#endif /* FEAT_GUI_TABLINE */
4208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209
Bram Moolenaar98921892016-02-23 17:14:37 +01004210#if GTK_CHECK_VERSION(3,0,0)
4211 g_signal_connect(G_OBJECT(gui.drawarea), "motion-notify-event",
4212 G_CALLBACK(motion_notify_event), NULL);
4213 g_signal_connect(G_OBJECT(gui.drawarea), "button-press-event",
4214 G_CALLBACK(button_press_event), NULL);
4215 g_signal_connect(G_OBJECT(gui.drawarea), "button-release-event",
4216 G_CALLBACK(button_release_event), NULL);
4217 g_signal_connect(G_OBJECT(gui.drawarea), "scroll-event",
4218 G_CALLBACK(&scroll_event), NULL);
4219#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "motion_notify_event",
4221 GTK_SIGNAL_FUNC(motion_notify_event), NULL);
4222 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_press_event",
4223 GTK_SIGNAL_FUNC(button_press_event), NULL);
4224 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_release_event",
4225 GTK_SIGNAL_FUNC(button_release_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 g_signal_connect(G_OBJECT(gui.drawarea), "scroll_event",
4227 G_CALLBACK(&scroll_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229
4230 /*
4231 * Add selection handler functions.
4232 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004233#if GTK_CHECK_VERSION(3,0,0)
4234 g_signal_connect(G_OBJECT(gui.drawarea), "selection-clear-event",
4235 G_CALLBACK(selection_clear_event), NULL);
4236 g_signal_connect(G_OBJECT(gui.drawarea), "selection-received",
4237 G_CALLBACK(selection_received_cb), NULL);
4238#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_clear_event",
4240 GTK_SIGNAL_FUNC(selection_clear_event), NULL);
4241 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_received",
4242 GTK_SIGNAL_FUNC(selection_received_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
Bram Moolenaara76638f2010-06-05 12:49:46 +02004245 gui_gtk_set_selection_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246
Bram Moolenaar98921892016-02-23 17:14:37 +01004247#if GTK_CHECK_VERSION(3,0,0)
4248 g_signal_connect(G_OBJECT(gui.drawarea), "selection-get",
4249 G_CALLBACK(selection_get_cb), NULL);
4250#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_get",
4252 GTK_SIGNAL_FUNC(selection_get_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254
4255 /* Pretend we don't have input focus, we will get an event if we do. */
4256 gui.in_focus = FALSE;
4257
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 return OK;
4259}
4260
4261#if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO)
4262/*
4263 * This is called from gui_start() after a fork() has been done.
4264 * We have to tell the session manager our new PID.
4265 */
4266 void
4267gui_mch_forked(void)
4268{
4269 if (using_gnome)
4270 {
4271 GnomeClient *client;
4272
4273 client = gnome_master_client();
4274
4275 if (client != NULL)
4276 gnome_client_set_process_id(client, getpid());
4277 }
4278}
4279#endif /* FEAT_GUI_GNOME && FEAT_SESSION */
4280
Bram Moolenaar98921892016-02-23 17:14:37 +01004281#if GTK_CHECK_VERSION(3,0,0)
4282 static void
4283gui_gtk_get_rgb_from_pixel(guint32 pixel, GdkRGBA *result)
4284{
4285 GdkVisual * const visual = gtk_widget_get_visual(gui.drawarea);
4286 guint32 r_mask, g_mask, b_mask;
4287 gint r_shift, g_shift, b_shift;
4288
4289 if (visual == NULL)
4290 {
4291 result->red = 0.0;
4292 result->green = 0.0;
4293 result->blue = 0.0;
4294 result->alpha = 0.0;
4295 return;
4296 }
4297
4298 gdk_visual_get_red_pixel_details(visual, &r_mask, &r_shift, NULL);
4299 gdk_visual_get_green_pixel_details(visual, &g_mask, &g_shift, NULL);
4300 gdk_visual_get_blue_pixel_details(visual, &b_mask, &b_shift, NULL);
4301
4302 result->red = ((pixel & r_mask) >> r_shift) / 255.0;
4303 result->green = ((pixel & g_mask) >> g_shift) / 255.0;
4304 result->blue = ((pixel & b_mask) >> b_shift) / 255.0;
4305 result->alpha = 1.0;
4306}
4307
4308/* Convert a GdRGBA into a pixel value using drawarea's visual */
4309 static guint32
4310gui_gtk_get_pixel_from_rgb(const GdkRGBA *rgba)
4311{
4312 GdkVisual * const visual = gtk_widget_get_visual(gui.drawarea);
4313 guint32 r_mask, g_mask, b_mask;
4314 gint r_shift, g_shift, b_shift;
4315 guint32 r, g, b;
4316
4317 if (visual == NULL)
4318 return 0;
4319
4320 gdk_visual_get_red_pixel_details(visual, &r_mask, &r_shift, NULL);
4321 gdk_visual_get_green_pixel_details(visual, &g_mask, &g_shift, NULL);
4322 gdk_visual_get_blue_pixel_details(visual, &b_mask, &b_shift, NULL);
4323
4324 r = rgba->red * 65535;
4325 g = rgba->green * 65535;
4326 b = rgba->blue * 65535;
4327
4328 return ((r << r_shift) & r_mask) |
4329 ((g << g_shift) & g_mask) |
4330 ((b << b_shift) & b_mask);
4331}
4332
4333 static void
4334set_cairo_source_rgb_from_pixel(cairo_t *cr, guint32 pixel)
4335{
4336 GdkRGBA result;
4337 gui_gtk_get_rgb_from_pixel(pixel, &result);
4338 cairo_set_source_rgb(cr, result.red, result.green, result.blue);
4339}
4340#endif /* GTK_CHECK_VERSION(3,0,0) */
4341
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342/*
4343 * Called when the foreground or background color has been changed.
4344 * This used to change the graphics contexts directly but we are
4345 * currently manipulating them where desired.
4346 */
4347 void
4348gui_mch_new_colors(void)
4349{
Bram Moolenaar98921892016-02-23 17:14:37 +01004350#if GTK_CHECK_VERSION(3,0,0)
4351 GdkWindow * const da_win = gtk_widget_get_window(gui.drawarea);
4352
4353 if (gui.drawarea != NULL && gtk_widget_get_window(gui.drawarea) != NULL)
4354#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01004356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004358#if GTK_CHECK_VERSION(3,4,0)
4359 GdkRGBA color;
4360
4361 gui_gtk_get_rgb_from_pixel(gui.back_pixel, &color);
4362 {
4363 cairo_pattern_t * const pat = cairo_pattern_create_rgba(
4364 color.red, color.green, color.blue, color.alpha);
4365 if (pat != NULL)
4366 {
4367 gdk_window_set_background_pattern(da_win, pat);
4368 cairo_pattern_destroy(pat);
4369 }
4370 else
4371 gdk_window_set_background_rgba(da_win, &color);
4372 }
4373#else /* !GTK_CHECK_VERSION(3,4,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 GdkColor color = { 0, 0, 0, 0 };
4375
4376 color.pixel = gui.back_pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01004377# if GTK_CHECK_VERSION(3,0,0)
4378 gdk_window_set_background(da_win, &color);
4379# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 gdk_window_set_background(gui.drawarea->window, &color);
Bram Moolenaar98921892016-02-23 17:14:37 +01004381# endif
4382#endif /* !GTK_CHECK_VERSION(3,4,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 }
4384}
4385
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386/*
4387 * This signal informs us about the need to rearrange our sub-widgets.
4388 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004390form_configure_event(GtkWidget *widget UNUSED,
4391 GdkEventConfigure *event,
4392 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393{
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004394 int usable_height = event->height;
4395
4396 /* When in a GtkPlug, we can't guarantee valid heights (as a round
4397 * no. of char-heights), so we have to manually sanitise them.
4398 * Widths seem to sort themselves out, don't ask me why.
4399 */
4400 if (gtk_socket_id != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004401 usable_height -= (gui.char_height - (gui.char_height/2)); /* sic. */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004402
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 gtk_form_freeze(GTK_FORM(gui.formwin));
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004404 gui_resize_shell(event->width, usable_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 gtk_form_thaw(GTK_FORM(gui.formwin));
4406
4407 return TRUE;
4408}
4409
4410/*
4411 * Function called when window already closed.
4412 * We can't do much more here than to trying to preserve what had been done,
4413 * since the window is already inevitably going away.
4414 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004415#if GTK_CHECK_VERSION(3,0,0)
4416 static void
4417mainwin_destroy_cb(GObject *object UNUSED, gpointer data UNUSED)
4418#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004420mainwin_destroy_cb(GtkObject *object UNUSED, gpointer data UNUSED)
Bram Moolenaar98921892016-02-23 17:14:37 +01004421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422{
4423 /* Don't write messages to the GUI anymore */
4424 full_screen = FALSE;
4425
4426 gui.mainwin = NULL;
4427 gui.drawarea = NULL;
4428
4429 if (!exiting) /* only do anything if the destroy was unexpected */
4430 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004431 vim_strncpy(IObuff,
4432 (char_u *)_("Vim: Main window unexpectedly destroyed\n"),
4433 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 preserve_exit();
4435 }
Bram Moolenaar36e294c2015-12-29 18:55:46 +01004436#ifdef USE_GRESOURCE
4437 gui_gtk_unregister_resource();
4438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439}
4440
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004441
4442/*
4443 * Bit of a hack to ensure we start GtkPlug windows with the correct window
4444 * hints (and thus the required size from -geom), but that after that we
4445 * put the hints back to normal (the actual minimum size) so we may
4446 * subsequently be resized smaller. GtkSocket (the parent end) uses the
Bram Moolenaar49325942007-05-10 19:19:59 +00004447 * plug's window 'min hints to set *it's* minimum size, but that's also the
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004448 * only way we have of making ourselves bigger (by set lines/columns).
4449 * Thus set hints at start-up to ensure correct init. size, then a
4450 * second after the final attempt to reset the real minimum hinst (done by
Bram Moolenaar49325942007-05-10 19:19:59 +00004451 * scrollbar init.), actually do the standard hinst and stop the timer.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004452 * We'll not let the default hints be set while this timer's active.
4453 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004454 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004455check_startup_plug_hints(gpointer data UNUSED)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004456{
4457 if (init_window_hints_state == 1)
4458 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004459 /* Safe to use normal hints now */
4460 init_window_hints_state = 0;
4461 update_window_manager_hints(0, 0);
4462 return FALSE; /* stop timer */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004463 }
4464
4465 /* Keep on trying */
4466 init_window_hints_state = 1;
4467 return TRUE;
4468}
4469
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470/*
4471 * Open the GUI window which was created by a call to gui_mch_init().
4472 */
4473 int
4474gui_mch_open(void)
4475{
4476 guicolor_T fg_pixel = INVALCOLOR;
4477 guicolor_T bg_pixel = INVALCOLOR;
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004478 guint pixel_width;
4479 guint pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 /*
4482 * Allow setting a window role on the command line, or invent one
4483 * if none was specified. This is mainly useful for GNOME session
4484 * support; allowing the WM to restore window placement.
4485 */
4486 if (role_argument != NULL)
4487 {
4488 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument);
4489 }
4490 else
4491 {
4492 char *role;
4493
4494 /* Invent a unique-enough ID string for the role */
4495 role = g_strdup_printf("vim-%u-%u-%u",
4496 (unsigned)mch_get_pid(),
4497 (unsigned)g_random_int(),
4498 (unsigned)time(NULL));
4499
4500 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role);
4501 g_free(role);
4502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
4504 if (gui_win_x != -1 && gui_win_y != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506
4507 /* Determine user specified geometry, if present. */
4508 if (gui.geom != NULL)
4509 {
4510 int mask;
4511 unsigned int w, h;
4512 int x = 0;
4513 int y = 0;
4514
4515 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
4516
4517 if (mask & WidthValue)
4518 Columns = w;
4519 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00004520 {
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004521 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00004522 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00004524 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004525 limit_screen_size();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004526
4527 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4528 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
4529
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004530 pixel_width += get_menu_tool_width();
4531 pixel_height += get_menu_tool_height();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004532
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 if (mask & (XValue | YValue))
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004534 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004535 int ww, hh;
4536 gui_mch_get_screen_dimensions(&ww, &hh);
4537 hh += p_ghr + get_menu_tool_height();
4538 ww += get_menu_tool_width();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004539 if (mask & XNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004540 x += ww - pixel_width;
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004541 if (mask & YNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004542 y += hh - pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 vim_free(gui.geom);
4546 gui.geom = NULL;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004547
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004548 /* From now until everyone's stopped trying to set the window hints
4549 * to their correct minimum values, stop them being set as we need
4550 * them to remain at our required size for the parent GtkSocket to
4551 * give us the right initial size.
4552 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004553 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004554 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004555 update_window_manager_hints(pixel_width, pixel_height);
4556 init_window_hints_state = 1;
4557 g_timeout_add(1000, check_startup_plug_hints, NULL);
4558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 }
4560
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004561 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4562 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004563 /* For GTK2 changing the size of the form widget doesn't cause window
4564 * resizing. */
Bram Moolenaardebe25a2010-06-06 17:41:24 +02004565 if (gtk_socket_id == 0)
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004566 gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004567 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
4569 if (foreground_argument != NULL)
4570 fg_pixel = gui_get_color((char_u *)foreground_argument);
4571 if (fg_pixel == INVALCOLOR)
4572 fg_pixel = gui_get_color((char_u *)"Black");
4573
4574 if (background_argument != NULL)
4575 bg_pixel = gui_get_color((char_u *)background_argument);
4576 if (bg_pixel == INVALCOLOR)
4577 bg_pixel = gui_get_color((char_u *)"White");
4578
4579 if (found_reverse_arg)
4580 {
4581 gui.def_norm_pixel = bg_pixel;
4582 gui.def_back_pixel = fg_pixel;
4583 }
4584 else
4585 {
4586 gui.def_norm_pixel = fg_pixel;
4587 gui.def_back_pixel = bg_pixel;
4588 }
4589
4590 /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or
4591 * in a vimrc file) */
4592 set_normal_colors();
4593
4594 /* Check that none of the colors are the same as the background color */
4595 gui_check_colors();
4596
4597 /* Get the colors for the highlight groups (gui_check_colors() might have
4598 * changed them). */
4599 highlight_gui_started(); /* re-init colors and fonts */
4600
Bram Moolenaar98921892016-02-23 17:14:37 +01004601#if GTK_CHECK_VERSION(3,0,0)
4602 g_signal_connect(G_OBJECT(gui.mainwin), "destroy",
4603 G_CALLBACK(mainwin_destroy_cb), NULL);
4604#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "destroy",
4606 GTK_SIGNAL_FUNC(mainwin_destroy_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608
4609#ifdef FEAT_HANGULIN
4610 hangul_keyboard_set();
4611#endif
4612
4613 /*
4614 * Notify the fixed area about the need to resize the contents of the
4615 * gui.formwin, which we use for random positioning of the included
4616 * components.
4617 *
4618 * We connect this signal deferred finally after anything is in place,
4619 * since this is intended to handle resizements coming from the window
4620 * manager upon us and should not interfere with what VIM is requesting
4621 * upon startup.
4622 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004623#if GTK_CHECK_VERSION(3,0,0)
4624 g_signal_connect(G_OBJECT(gui.formwin), "configure-event",
4625 G_CALLBACK(form_configure_event), NULL);
4626#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 gtk_signal_connect(GTK_OBJECT(gui.formwin), "configure_event",
4628 GTK_SIGNAL_FUNC(form_configure_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630
4631#ifdef FEAT_DND
Bram Moolenaara76638f2010-06-05 12:49:46 +02004632 /* Set up for receiving DND items. */
4633 gui_gtk_set_dnd_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634
Bram Moolenaar98921892016-02-23 17:14:37 +01004635# if GTK_CHECK_VERSION(3,0,0)
4636 g_signal_connect(G_OBJECT(gui.drawarea), "drag-data-received",
4637 G_CALLBACK(drag_data_received_cb), NULL);
4638# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "drag_data_received",
4640 GTK_SIGNAL_FUNC(drag_data_received_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004641# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642#endif
4643
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 /* With GTK+ 2, we need to iconify the window before calling show()
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004645 * to avoid mapping the window for a short time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 if (found_iconic_arg && gtk_socket_id == 0)
4647 gui_mch_iconify();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648
4649 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004650#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 unsigned long menu_handler = 0;
4652# ifdef FEAT_TOOLBAR
4653 unsigned long tool_handler = 0;
4654# endif
4655 /*
4656 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
4657 * show when restoring the saved layout configuration. We can't just
4658 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
4659 * a toplevel window and thus will be realized immediately. Instead,
4660 * connect signal handlers to hide the widgets just after they've been
4661 * marked visible, but before the main window is realized.
4662 */
4663 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL)
4664 menu_handler = g_signal_connect_after(gui.menubar_h, "show",
4665 G_CALLBACK(&gtk_widget_hide),
4666 NULL);
4667# ifdef FEAT_TOOLBAR
4668 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL
4669 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
4670 tool_handler = g_signal_connect_after(gui.toolbar_h, "show",
4671 G_CALLBACK(&gtk_widget_hide),
4672 NULL);
4673# endif
4674#endif
4675 gtk_widget_show(gui.mainwin);
4676
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004677#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 if (menu_handler != 0)
4679 g_signal_handler_disconnect(gui.menubar_h, menu_handler);
4680# ifdef FEAT_TOOLBAR
4681 if (tool_handler != 0)
4682 g_signal_handler_disconnect(gui.toolbar_h, tool_handler);
4683# endif
4684#endif
4685 }
4686
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 return OK;
4688}
4689
4690
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004692gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693{
4694 if (gui.mainwin != NULL)
4695 gtk_widget_destroy(gui.mainwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696}
4697
4698/*
4699 * Get the position of the top left corner of the window.
4700 */
4701 int
4702gui_mch_get_winpos(int *x, int *y)
4703{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 return OK;
4706}
4707
4708/*
4709 * Set the position of the top left corner of the window to the given
4710 * coordinates.
4711 */
4712 void
4713gui_mch_set_winpos(int x, int y)
4714{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716}
4717
Bram Moolenaar98921892016-02-23 17:14:37 +01004718#if !GTK_CHECK_VERSION(3,0,0)
4719# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720static int resize_idle_installed = FALSE;
4721/*
4722 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
4723 * the shell size doesn't exceed the window size, i.e. if the window manager
4724 * ignored our size request. Usually this happens if the window is maximized.
4725 *
4726 * FIXME: It'd be nice if we could find a little more orthodox solution.
4727 * See also the remark below in gui_mch_set_shellsize().
4728 *
4729 * DISABLED: When doing ":set lines+=1" this function would first invoke
4730 * gui_resize_shell() with the old size, then the normal callback would
4731 * report the new size through form_configure_event(). That caused the window
4732 * layout to be messed up.
4733 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 static gboolean
4735force_shell_resize_idle(gpointer data)
4736{
4737 if (gui.mainwin != NULL
4738 && GTK_WIDGET_REALIZED(gui.mainwin)
4739 && GTK_WIDGET_VISIBLE(gui.mainwin))
4740 {
4741 int width;
4742 int height;
4743
4744 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height);
4745
4746 width -= get_menu_tool_width();
4747 height -= get_menu_tool_height();
4748
4749 gui_resize_shell(width, height);
4750 }
4751
4752 resize_idle_installed = FALSE;
4753 return FALSE; /* don't call me again */
4754}
Bram Moolenaar98921892016-02-23 17:14:37 +01004755# endif
4756#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757
Bram Moolenaar09736232009-09-23 16:14:49 +00004758/*
4759 * Return TRUE if the main window is maximized.
4760 */
4761 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004762gui_mch_maximized(void)
Bram Moolenaar09736232009-09-23 16:14:49 +00004763{
Bram Moolenaar98921892016-02-23 17:14:37 +01004764#if GTK_CHECK_VERSION(3,0,0)
4765 return (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL
4766 && (gdk_window_get_state(gtk_widget_get_window(gui.mainwin))
4767 & GDK_WINDOW_STATE_MAXIMIZED));
4768#else
Bram Moolenaar09736232009-09-23 16:14:49 +00004769 return (gui.mainwin != NULL && gui.mainwin->window != NULL
4770 && (gdk_window_get_state(gui.mainwin->window)
4771 & GDK_WINDOW_STATE_MAXIMIZED));
Bram Moolenaar98921892016-02-23 17:14:37 +01004772#endif
Bram Moolenaar09736232009-09-23 16:14:49 +00004773}
4774
4775/*
4776 * Unmaximize the main window
4777 */
4778 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004779gui_mch_unmaximize(void)
Bram Moolenaar09736232009-09-23 16:14:49 +00004780{
4781 if (gui.mainwin != NULL)
4782 gtk_window_unmaximize(GTK_WINDOW(gui.mainwin));
4783}
Bram Moolenaar09736232009-09-23 16:14:49 +00004784
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785/*
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004786 * Called when the font changed while the window is maximized. Compute the
4787 * new Rows and Columns. This is like resizing the window.
4788 */
4789 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004790gui_mch_newfont(void)
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004791{
4792 int w, h;
4793
4794 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
4795 w -= get_menu_tool_width();
4796 h -= get_menu_tool_height();
4797 gui_resize_shell(w, h);
4798}
4799
4800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 * Set the windows size.
4802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 void
4804gui_mch_set_shellsize(int width, int height,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004805 int min_width UNUSED, int min_height UNUSED,
4806 int base_width UNUSED, int base_height UNUSED,
4807 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 /* give GTK+ a chance to put all widget's into place */
4810 gui_mch_update();
4811
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004812 /* this will cause the proper resizement to happen too */
4813 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004814 update_window_manager_hints(0, 0);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004815
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 /* With GTK+ 2, changing the size of the form widget doesn't resize
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004817 * the window. So let's do it the other way around and resize the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 * main window instead. */
4819 width += get_menu_tool_width();
4820 height += get_menu_tool_height();
4821
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004822 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004823 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004824 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004825 update_window_manager_hints(width, height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826
Bram Moolenaar98921892016-02-23 17:14:37 +01004827# if !GTK_CHECK_VERSION(3,0,0)
4828# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 if (!resize_idle_installed)
4830 {
4831 g_idle_add_full(GDK_PRIORITY_EVENTS + 10,
4832 &force_shell_resize_idle, NULL, NULL);
4833 resize_idle_installed = TRUE;
4834 }
Bram Moolenaar98921892016-02-23 17:14:37 +01004835# endif
4836# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 /*
4838 * Wait until all events are processed to prevent a crash because the
4839 * real size of the drawing area doesn't reflect Vim's internal ideas.
4840 *
4841 * This is a bit of a hack, since Vim is a terminal application with a GUI
4842 * on top, while the GUI expects to be the boss.
4843 */
4844 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845}
4846
4847
4848/*
4849 * The screen size is used to make sure the initial window doesn't get bigger
4850 * than the screen. This subtracts some room for menubar, toolbar and window
4851 * decorations.
4852 */
4853 void
4854gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
4855{
4856#ifdef HAVE_GTK_MULTIHEAD
4857 GdkScreen* screen;
4858
4859 if (gui.mainwin != NULL && gtk_widget_has_screen(gui.mainwin))
4860 screen = gtk_widget_get_screen(gui.mainwin);
4861 else
4862 screen = gdk_screen_get_default();
4863
4864 *screen_w = gdk_screen_get_width(screen);
4865 *screen_h = gdk_screen_get_height(screen) - p_ghr;
4866#else
4867 *screen_w = gdk_screen_width();
4868 /* Subtract 'guiheadroom' from the height to allow some room for the
4869 * window manager (task list and window title bar). */
4870 *screen_h = gdk_screen_height() - p_ghr;
4871#endif
4872
4873 /*
4874 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
4875 * the toolbar and menubar for GTK, we subtract them from the screen
Bram Moolenaar64404472010-06-26 06:24:45 +02004876 * height, so that the window size can be made to fit on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 * This should be completely changed later.
4878 */
4879 *screen_w -= get_menu_tool_width();
4880 *screen_h -= get_menu_tool_height();
4881}
4882
4883#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004885gui_mch_settitle(char_u *title, char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 if (title != NULL && output_conv.vc_type != CONV_NONE)
4888 title = string_convert(&output_conv, title, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889
4890 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
4891
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 if (output_conv.vc_type != CONV_NONE)
4893 vim_free(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894}
4895#endif /* FEAT_TITLE */
4896
4897#if defined(FEAT_MENU) || defined(PROTO)
4898 void
4899gui_mch_enable_menu(int showit)
4900{
4901 GtkWidget *widget;
4902
4903# ifdef FEAT_GUI_GNOME
4904 if (using_gnome)
4905 widget = gui.menubar_h;
4906 else
4907# endif
4908 widget = gui.menubar;
4909
Bram Moolenaar18144c82006-04-12 21:52:12 +00004910 /* Do not disable the menu while starting up, otherwise F10 doesn't work. */
Bram Moolenaar98921892016-02-23 17:14:37 +01004911# if GTK_CHECK_VERSION(3,0,0)
4912 if (!showit != !gtk_widget_get_visible(widget) && !gui.starting)
4913# else
Bram Moolenaar18144c82006-04-12 21:52:12 +00004914 if (!showit != !GTK_WIDGET_VISIBLE(widget) && !gui.starting)
Bram Moolenaar98921892016-02-23 17:14:37 +01004915# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 {
4917 if (showit)
4918 gtk_widget_show(widget);
4919 else
4920 gtk_widget_hide(widget);
4921
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004922 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 }
4924}
4925#endif /* FEAT_MENU */
4926
4927#if defined(FEAT_TOOLBAR) || defined(PROTO)
4928 void
4929gui_mch_show_toolbar(int showit)
4930{
4931 GtkWidget *widget;
4932
4933 if (gui.toolbar == NULL)
4934 return;
4935
4936# ifdef FEAT_GUI_GNOME
4937 if (using_gnome)
4938 widget = gui.toolbar_h;
4939 else
4940# endif
4941 widget = gui.toolbar;
4942
4943 if (showit)
4944 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
4945
Bram Moolenaar98921892016-02-23 17:14:37 +01004946# if GTK_CHECK_VERSION(3,0,0)
4947 if (!showit != !gtk_widget_get_visible(widget))
4948# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 if (!showit != !GTK_WIDGET_VISIBLE(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01004950# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 {
4952 if (showit)
4953 gtk_widget_show(widget);
4954 else
4955 gtk_widget_hide(widget);
4956
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004957 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 }
4959}
4960#endif /* FEAT_TOOLBAR */
4961
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962/*
4963 * Check if a given font is a CJK font. This is done in a very crude manner. It
4964 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
4965 * font. Consequently, this function cannot be used as a general purpose check
4966 * for CJK-ness for which fontconfig APIs should be used. This is only used by
4967 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
4968 */
4969 static int
4970is_cjk_font(PangoFontDescription *font_desc)
4971{
4972 static const char * const cjk_langs[] =
4973 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
4974
4975 PangoFont *font;
4976 unsigned i;
4977 int is_cjk = FALSE;
4978
4979 font = pango_context_load_font(gui.text_context, font_desc);
4980
4981 if (font == NULL)
4982 return FALSE;
4983
4984 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i)
4985 {
4986 PangoCoverage *coverage;
4987 gunichar uc;
4988
4989 coverage = pango_font_get_coverage(
4990 font, pango_language_from_string(cjk_langs[i]));
4991
4992 if (coverage != NULL)
4993 {
4994 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00;
4995 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT);
4996 pango_coverage_unref(coverage);
4997 }
4998 }
4999
5000 g_object_unref(font);
5001
5002 return is_cjk;
5003}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004
Bram Moolenaar231334e2005-07-25 20:46:57 +00005005/*
5006 * Adjust gui.char_height (after 'linespace' was changed).
5007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 int
Bram Moolenaar231334e2005-07-25 20:46:57 +00005009gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 PangoFontMetrics *metrics;
5012 int ascent;
5013 int descent;
5014
5015 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font,
5016 pango_context_get_language(gui.text_context));
5017 ascent = pango_font_metrics_get_ascent(metrics);
5018 descent = pango_font_metrics_get_descent(metrics);
5019
5020 pango_font_metrics_unref(metrics);
5021
5022 gui.char_height = (ascent + descent + PANGO_SCALE - 1) / PANGO_SCALE
Bram Moolenaar231334e2005-07-25 20:46:57 +00005023 + p_linespace;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005024 /* LINTED: avoid warning: bitwise operation on signed value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2);
5026
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 /* A not-positive value of char_height may crash Vim. Only happens
5028 * if 'linespace' is negative (which does make sense sometimes). */
5029 gui.char_ascent = MAX(gui.char_ascent, 0);
5030 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
5031
5032 return OK;
5033}
5034
Bram Moolenaar98921892016-02-23 17:14:37 +01005035#if GTK_CHECK_VERSION(3,0,0)
5036/* Callback function used in gui_mch_font_dialog() */
5037 static gboolean
5038font_filter(const PangoFontFamily *family,
5039 const PangoFontFace *face UNUSED,
5040 gpointer data UNUSED)
5041{
5042 return pango_font_family_is_monospace((PangoFontFamily *)family);
5043}
5044#endif
5045
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046/*
5047 * Put up a font dialog and return the selected font name in allocated memory.
5048 * "oldval" is the previous value. Return NULL when cancelled.
5049 * This should probably go into gui_gtk.c. Hmm.
5050 * FIXME:
5051 * The GTK2 font selection dialog has no filtering API. So we could either
5052 * a) implement our own (possibly copying the code from somewhere else) or
5053 * b) just live with it.
5054 */
5055 char_u *
5056gui_mch_font_dialog(char_u *oldval)
5057{
5058 GtkWidget *dialog;
5059 int response;
5060 char_u *fontname = NULL;
5061 char_u *oldname;
5062
Bram Moolenaar98921892016-02-23 17:14:37 +01005063#if GTK_CHECK_VERSION(3,2,0)
5064 dialog = gtk_font_chooser_dialog_new(NULL, NULL);
5065 gtk_font_chooser_set_filter_func(GTK_FONT_CHOOSER(dialog), font_filter,
5066 NULL, NULL);
5067#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 dialog = gtk_font_selection_dialog_new(NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01005069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070
5071 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
5072 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
5073
5074 if (oldval != NULL && oldval[0] != NUL)
5075 {
5076 if (output_conv.vc_type != CONV_NONE)
5077 oldname = string_convert(&output_conv, oldval, NULL);
5078 else
5079 oldname = oldval;
5080
5081 /* Annoying bug in GTK (or Pango): if the font name does not include a
5082 * size, zero is used. Use default point size ten. */
5083 if (!vim_isdigit(oldname[STRLEN(oldname) - 1]))
5084 {
5085 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3);
5086
5087 if (p != NULL)
5088 {
5089 STRCPY(p + STRLEN(p), " 10");
5090 if (oldname != oldval)
5091 vim_free(oldname);
5092 oldname = p;
5093 }
5094 }
5095
Bram Moolenaar98921892016-02-23 17:14:37 +01005096#if GTK_CHECK_VERSION(3,2,0)
5097 gtk_font_chooser_set_font(
5098 GTK_FONT_CHOOSER(dialog), (const gchar *)oldname);
5099#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 gtk_font_selection_dialog_set_font_name(
5101 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname);
Bram Moolenaar98921892016-02-23 17:14:37 +01005102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103
5104 if (oldname != oldval)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005105 vim_free(oldname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 }
Bram Moolenaarbef9d832009-09-11 13:46:41 +00005107 else
Bram Moolenaar98921892016-02-23 17:14:37 +01005108#if GTK_CHECK_VERSION(3,2,0)
5109 gtk_font_chooser_set_font(
5110 GTK_FONT_CHOOSER(dialog), DEFAULT_FONT);
5111#else
Bram Moolenaarbef9d832009-09-11 13:46:41 +00005112 gtk_font_selection_dialog_set_font_name(
5113 GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT);
Bram Moolenaar98921892016-02-23 17:14:37 +01005114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115
5116 response = gtk_dialog_run(GTK_DIALOG(dialog));
5117
5118 if (response == GTK_RESPONSE_OK)
5119 {
5120 char *name;
5121
Bram Moolenaar98921892016-02-23 17:14:37 +01005122#if GTK_CHECK_VERSION(3,2,0)
5123 name = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(dialog));
5124#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 name = gtk_font_selection_dialog_get_font_name(
5126 GTK_FONT_SELECTION_DIALOG(dialog));
Bram Moolenaar98921892016-02-23 17:14:37 +01005127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 if (name != NULL)
5129 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005130 char_u *p;
5131
5132 /* Apparently some font names include a comma, need to escape
5133 * that, because in 'guifont' it separates names. */
5134 p = vim_strsave_escaped((char_u *)name, (char_u *)",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 g_free(name);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005136 if (p != NULL && input_conv.vc_type != CONV_NONE)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005137 {
5138 fontname = string_convert(&input_conv, p, NULL);
5139 vim_free(p);
5140 }
5141 else
5142 fontname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 }
5144 }
5145
5146 if (response != GTK_RESPONSE_NONE)
5147 gtk_widget_destroy(dialog);
5148
5149 return fontname;
5150}
5151
5152/*
5153 * Some monospace fonts don't support a bold weight, and fall back
5154 * silently to the regular weight. But this is no good since our text
5155 * drawing function can emulate bold by overstriking. So let's try
5156 * to detect whether bold weight is actually available and emulate it
5157 * otherwise.
5158 *
5159 * Note that we don't need to check for italic style since Xft can
5160 * emulate italic on its own, provided you have a proper fontconfig
5161 * setup. We wouldn't be able to emulate it in Vim anyway.
5162 */
5163 static void
5164get_styled_font_variants(void)
5165{
5166 PangoFontDescription *bold_font_desc;
5167 PangoFont *plain_font;
5168 PangoFont *bold_font;
5169
5170 gui.font_can_bold = FALSE;
5171
5172 plain_font = pango_context_load_font(gui.text_context, gui.norm_font);
5173
5174 if (plain_font == NULL)
5175 return;
5176
5177 bold_font_desc = pango_font_description_copy_static(gui.norm_font);
5178 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD);
5179
5180 bold_font = pango_context_load_font(gui.text_context, bold_font_desc);
5181 /*
5182 * The comparison relies on the unique handle nature of a PangoFont*,
5183 * i.e. it's assumed that a different PangoFont* won't refer to the
5184 * same font. Seems to work, and failing here isn't critical anyway.
5185 */
5186 if (bold_font != NULL)
5187 {
5188 gui.font_can_bold = (bold_font != plain_font);
5189 g_object_unref(bold_font);
5190 }
5191
5192 pango_font_description_free(bold_font_desc);
5193 g_object_unref(plain_font);
5194}
5195
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196static PangoEngineShape *default_shape_engine = NULL;
5197
5198/*
5199 * Create a map from ASCII characters in the range [32,126] to glyphs
5200 * of the current font. This is used by gui_gtk2_draw_string() to skip
5201 * the itemize and shaping process for the most common case.
5202 */
5203 static void
5204ascii_glyph_table_init(void)
5205{
5206 char_u ascii_chars[128];
5207 PangoAttrList *attr_list;
5208 GList *item_list;
5209 int i;
5210
5211 if (gui.ascii_glyphs != NULL)
5212 pango_glyph_string_free(gui.ascii_glyphs);
5213 if (gui.ascii_font != NULL)
5214 g_object_unref(gui.ascii_font);
5215
5216 gui.ascii_glyphs = NULL;
5217 gui.ascii_font = NULL;
5218
5219 /* For safety, fill in question marks for the control characters. */
5220 for (i = 0; i < 32; ++i)
5221 ascii_chars[i] = '?';
5222 for (; i < 127; ++i)
5223 ascii_chars[i] = i;
5224 ascii_chars[i] = '?';
5225
5226 attr_list = pango_attr_list_new();
5227 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars,
5228 0, sizeof(ascii_chars), attr_list, NULL);
5229
5230 if (item_list != NULL && item_list->next == NULL) /* play safe */
5231 {
5232 PangoItem *item;
5233 int width;
5234
5235 item = (PangoItem *)item_list->data;
5236 width = gui.char_width * PANGO_SCALE;
5237
5238 /* Remember the shape engine used for ASCII. */
5239 default_shape_engine = item->analysis.shape_engine;
5240
5241 gui.ascii_font = item->analysis.font;
5242 g_object_ref(gui.ascii_font);
5243
5244 gui.ascii_glyphs = pango_glyph_string_new();
5245
5246 pango_shape((const char *)ascii_chars, sizeof(ascii_chars),
5247 &item->analysis, gui.ascii_glyphs);
5248
5249 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars));
5250
5251 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i)
5252 {
5253 PangoGlyphGeometry *geom;
5254
5255 geom = &gui.ascii_glyphs->glyphs[i].geometry;
5256 geom->x_offset += MAX(0, width - geom->width) / 2;
5257 geom->width = width;
5258 }
5259 }
5260
5261 g_list_foreach(item_list, (GFunc)&pango_item_free, NULL);
5262 g_list_free(item_list);
5263 pango_attr_list_unref(attr_list);
5264}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265
5266/*
5267 * Initialize Vim to use the font or fontset with the given name.
5268 * Return FAIL if the font could not be loaded, OK otherwise.
5269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 int
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005271gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 PangoFontDescription *font_desc;
5274 PangoLayout *layout;
5275 int width;
5276
5277 /* If font_name is NULL, this means to use the default, which should
5278 * be present on all proper Pango/fontconfig installations. */
5279 if (font_name == NULL)
5280 font_name = (char_u *)DEFAULT_FONT;
5281
5282 font_desc = gui_mch_get_font(font_name, FALSE);
5283
5284 if (font_desc == NULL)
5285 return FAIL;
5286
5287 gui_mch_free_font(gui.norm_font);
5288 gui.norm_font = font_desc;
5289
5290 pango_context_set_font_description(gui.text_context, font_desc);
5291
5292 layout = pango_layout_new(gui.text_context);
5293 pango_layout_set_text(layout, "MW", 2);
5294 pango_layout_get_size(layout, &width, NULL);
5295 /*
5296 * Set char_width to half the width obtained from pango_layout_get_size()
5297 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
5298 * Pango to use the same width for both non-CJK characters (e.g. Latin
5299 * letters and numbers) and CJK characters. This results in 's p a c e d
5300 * o u t' rendering when a CJK 'fixed width' font is used. To work around
5301 * that, divide the width returned by Pango by 2 if cjk_width is equal to
5302 * width for CJK fonts.
5303 *
5304 * For related bugs, see:
5305 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
5306 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
5307 *
5308 * With this, for all four of the following cases, Vim works fine:
5309 * guifont=CJK_fixed_width_font
5310 * guifont=Non_CJK_fixed_font
5311 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
5312 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
5313 */
5314 if (is_cjk_font(gui.norm_font))
5315 {
5316 int cjk_width;
5317
5318 /* Measure the text extent of U+4E00 and U+4E8C */
5319 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1);
5320 pango_layout_get_size(layout, &cjk_width, NULL);
5321
5322 if (width == cjk_width) /* Xft not patched */
5323 width /= 2;
5324 }
5325 g_object_unref(layout);
5326
5327 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE;
5328
5329 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
5330 if (gui.char_width <= 0)
5331 gui.char_width = 8;
5332
Bram Moolenaar231334e2005-07-25 20:46:57 +00005333 gui_mch_adjust_charheight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334
5335 /* Set the fontname, which will be used for information purposes */
5336 hl_set_font_name(font_name);
5337
5338 get_styled_font_variants();
5339 ascii_glyph_table_init();
5340
5341 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
5342 if (gui.wide_font != NULL
5343 && pango_font_description_equal(gui.norm_font, gui.wide_font))
5344 {
5345 pango_font_description_free(gui.wide_font);
5346 gui.wide_font = NULL;
5347 }
5348
Bram Moolenaare161c792009-11-03 17:13:59 +00005349 if (gui_mch_maximized())
5350 {
Bram Moolenaare161c792009-11-03 17:13:59 +00005351 /* Update lines and columns in accordance with the new font, keep the
5352 * window maximized. */
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02005353 gui_mch_newfont();
Bram Moolenaare161c792009-11-03 17:13:59 +00005354 }
5355 else
Bram Moolenaare161c792009-11-03 17:13:59 +00005356 {
5357 /* Preserve the logical dimensions of the screen. */
5358 update_window_manager_hints(0, 0);
5359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360
5361 return OK;
5362}
5363
5364/*
5365 * Get a reference to the font "name".
5366 * Return zero for failure.
5367 */
5368 GuiFont
5369gui_mch_get_font(char_u *name, int report_error)
5370{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 PangoFontDescription *font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372
5373 /* can't do this when GUI is not running */
5374 if (!gui.in_use || name == NULL)
5375 return NULL;
5376
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 if (output_conv.vc_type != CONV_NONE)
5378 {
5379 char_u *buf;
5380
5381 buf = string_convert(&output_conv, name, NULL);
5382 if (buf != NULL)
5383 {
5384 font = pango_font_description_from_string((const char *)buf);
5385 vim_free(buf);
5386 }
5387 else
5388 font = NULL;
5389 }
5390 else
5391 font = pango_font_description_from_string((const char *)name);
5392
5393 if (font != NULL)
5394 {
5395 PangoFont *real_font;
5396
5397 /* pango_context_load_font() bails out if no font size is set */
5398 if (pango_font_description_get_size(font) <= 0)
5399 pango_font_description_set_size(font, 10 * PANGO_SCALE);
5400
5401 real_font = pango_context_load_font(gui.text_context, font);
5402
5403 if (real_font == NULL)
5404 {
5405 pango_font_description_free(font);
5406 font = NULL;
5407 }
5408 else
5409 g_object_unref(real_font);
5410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411
5412 if (font == NULL)
5413 {
5414 if (report_error)
Bram Moolenaarc93e7912008-07-08 10:46:08 +00005415 EMSG2(_((char *)e_font), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 return NULL;
5417 }
5418
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 return font;
5420}
5421
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005422#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005423/*
5424 * Return the name of font "font" in allocated memory.
5425 */
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005426 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005427gui_mch_get_fontname(GuiFont font, char_u *name UNUSED)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005428{
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005429 if (font != NOFONT)
5430 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005431 char *pangoname = pango_font_description_to_string(font);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005432
Bram Moolenaar89d40322006-08-29 15:30:07 +00005433 if (pangoname != NULL)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005434 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005435 char_u *s = vim_strsave((char_u *)pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005436
Bram Moolenaar89d40322006-08-29 15:30:07 +00005437 g_free(pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005438 return s;
5439 }
5440 }
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005441 return NULL;
5442}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005443#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005444
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445/*
5446 * If a font is not going to be used, free its structure.
5447 */
5448 void
5449gui_mch_free_font(GuiFont font)
5450{
5451 if (font != NOFONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 pango_font_description_free(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453}
5454
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455/*
5456 * Return the Pixel value (color) for the given color name. This routine was
5457 * pretty much taken from example code in the Silicon Graphics OSF/Motif
5458 * Programmer's Guide.
5459 * Return INVALCOLOR for error.
5460 */
5461 guicolor_T
5462gui_mch_get_color(char_u *name)
5463{
5464 /* A number of colors that some X11 systems don't have */
5465 static const char *const vimnames[][2] =
5466 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00005467 {"LightRed", "#FFBBBB"},
5468 {"LightGreen", "#88FF88"},
5469 {"LightMagenta","#FFBBFF"},
5470 {"DarkCyan", "#008888"},
5471 {"DarkBlue", "#0000BB"},
5472 {"DarkRed", "#BB0000"},
5473 {"DarkMagenta", "#BB00BB"},
5474 {"DarkGrey", "#BBBBBB"},
5475 {"DarkYellow", "#BBBB00"},
5476 {"Gray10", "#1A1A1A"},
5477 {"Grey10", "#1A1A1A"},
5478 {"Gray20", "#333333"},
5479 {"Grey20", "#333333"},
5480 {"Gray30", "#4D4D4D"},
5481 {"Grey30", "#4D4D4D"},
5482 {"Gray40", "#666666"},
5483 {"Grey40", "#666666"},
5484 {"Gray50", "#7F7F7F"},
5485 {"Grey50", "#7F7F7F"},
5486 {"Gray60", "#999999"},
5487 {"Grey60", "#999999"},
5488 {"Gray70", "#B3B3B3"},
5489 {"Grey70", "#B3B3B3"},
5490 {"Gray80", "#CCCCCC"},
5491 {"Grey80", "#CCCCCC"},
5492 {"Gray90", "#E5E5E5"},
5493 {"Grey90", "#E5E5E5"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 {NULL, NULL}
5495 };
5496
5497 if (!gui.in_use) /* can't do this when GUI not running */
5498 return INVALCOLOR;
5499
5500 while (name != NULL)
5501 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005502#if GTK_CHECK_VERSION(3,0,0)
5503 GdkRGBA color;
5504#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 GdkColor color;
Bram Moolenaar98921892016-02-23 17:14:37 +01005506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 int parsed;
5508 int i;
5509
Bram Moolenaar98921892016-02-23 17:14:37 +01005510#if GTK_CHECK_VERSION(3,0,0)
5511 parsed = gdk_rgba_parse(&color, (const gchar *)name);
5512#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 parsed = gdk_color_parse((const char *)name, &color);
Bram Moolenaar98921892016-02-23 17:14:37 +01005514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 if (parsed)
5517 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005518#if GTK_CHECK_VERSION(3,0,0)
5519 return (guicolor_T)gui_gtk_get_pixel_from_rgb(&color);
5520#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea),
5522 &color, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 return (guicolor_T)color.pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01005524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 }
5526 /* add a few builtin names and try again */
5527 for (i = 0; ; ++i)
5528 {
5529 if (vimnames[i][0] == NULL)
5530 {
5531 name = NULL;
5532 break;
5533 }
5534 if (STRICMP(name, vimnames[i][0]) == 0)
5535 {
5536 name = (char_u *)vimnames[i][1];
5537 break;
5538 }
5539 }
5540 }
5541
5542 return INVALCOLOR;
5543}
5544
5545/*
5546 * Set the current text foreground color.
5547 */
5548 void
5549gui_mch_set_fg_color(guicolor_T color)
5550{
5551 gui.fgcolor->pixel = (unsigned long)color;
5552}
5553
5554/*
5555 * Set the current text background color.
5556 */
5557 void
5558gui_mch_set_bg_color(guicolor_T color)
5559{
5560 gui.bgcolor->pixel = (unsigned long)color;
5561}
5562
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005563/*
5564 * Set the current text special color.
5565 */
5566 void
5567gui_mch_set_sp_color(guicolor_T color)
5568{
5569 gui.spcolor->pixel = (unsigned long)color;
5570}
5571
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572/*
5573 * Function-like convenience macro for the sake of efficiency.
5574 */
5575#define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
5576 G_STMT_START{ \
5577 PangoAttribute *tmp_attr_; \
5578 tmp_attr_ = (Attribute); \
5579 tmp_attr_->start_index = (Start); \
5580 tmp_attr_->end_index = (End); \
5581 pango_attr_list_insert((AttrList), tmp_attr_); \
5582 }G_STMT_END
5583
5584 static void
5585apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list)
5586{
5587 char_u *start = NULL;
5588 char_u *p;
5589 int uc;
5590
5591 for (p = s; p < s + len; p += utf_byte2len(*p))
5592 {
5593 uc = utf_ptr2char(p);
5594
5595 if (start == NULL)
5596 {
5597 if (uc >= 0x80 && utf_char2cells(uc) == 2)
5598 start = p;
5599 }
5600 else if (uc < 0x80 /* optimization shortcut */
5601 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc)))
5602 {
5603 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5604 attr_list, start - s, p - s);
5605 start = NULL;
5606 }
5607 }
5608
5609 if (start != NULL)
5610 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5611 attr_list, start - s, len);
5612}
5613
5614 static int
5615count_cluster_cells(char_u *s, PangoItem *item,
5616 PangoGlyphString* glyphs, int i,
5617 int *cluster_width,
5618 int *last_glyph_rbearing)
5619{
5620 char_u *p;
5621 int next; /* glyph start index of next cluster */
5622 int start, end; /* string segment of current cluster */
5623 int width; /* real cluster width in Pango units */
5624 int uc;
5625 int cellcount = 0;
5626
5627 width = glyphs->glyphs[i].geometry.width;
5628
5629 for (next = i + 1; next < glyphs->num_glyphs; ++next)
5630 {
5631 if (glyphs->glyphs[next].attr.is_cluster_start)
5632 break;
5633 else if (glyphs->glyphs[next].geometry.width > width)
5634 width = glyphs->glyphs[next].geometry.width;
5635 }
5636
5637 start = item->offset + glyphs->log_clusters[i];
5638 end = item->offset + ((next < glyphs->num_glyphs) ?
5639 glyphs->log_clusters[next] : item->length);
5640
5641 for (p = s + start; p < s + end; p += utf_byte2len(*p))
5642 {
5643 uc = utf_ptr2char(p);
5644 if (uc < 0x80)
5645 ++cellcount;
5646 else if (!utf_iscomposing(uc))
5647 cellcount += utf_char2cells(uc);
5648 }
5649
5650 if (last_glyph_rbearing != NULL
5651 && cellcount > 0 && next == glyphs->num_glyphs)
5652 {
5653 PangoRectangle ink_rect;
5654 /*
5655 * If a certain combining mark had to be taken from a non-monospace
5656 * font, we have to compensate manually by adapting x_offset according
5657 * to the ink extents of the previous glyph.
5658 */
5659 pango_font_get_glyph_extents(item->analysis.font,
5660 glyphs->glyphs[i].glyph,
5661 &ink_rect, NULL);
5662
5663 if (PANGO_RBEARING(ink_rect) > 0)
5664 *last_glyph_rbearing = PANGO_RBEARING(ink_rect);
5665 }
5666
5667 if (cellcount > 0)
5668 *cluster_width = width;
5669
5670 return cellcount;
5671}
5672
5673/*
5674 * If there are only combining characters in the cluster, we cannot just
5675 * change the width of the previous glyph since there is none. Therefore
5676 * some guesswork is needed.
5677 *
5678 * If ink_rect.x is negative Pango apparently has taken care of the composing
5679 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
5680 * to problems with composing from different fonts we still need to fine-tune
Bram Moolenaar64404472010-06-26 06:24:45 +02005681 * x_offset to avoid ugliness.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 *
5683 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
5684 * the position of the previous glyph. Apparently this happens only with old
5685 * X fonts which don't provide the special combining information needed by
5686 * Pango.
5687 */
5688 static void
5689setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph,
5690 int last_cellcount, int last_cluster_width,
5691 int last_glyph_rbearing)
5692{
5693 PangoRectangle ink_rect;
5694 PangoRectangle logical_rect;
5695 int width;
5696
5697 width = last_cellcount * gui.char_width * PANGO_SCALE;
5698 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2;
5699 glyph->geometry.width = 0;
5700
5701 pango_font_get_glyph_extents(item->analysis.font,
5702 glyph->glyph,
5703 &ink_rect, &logical_rect);
5704 if (ink_rect.x < 0)
5705 {
5706 glyph->geometry.x_offset += last_glyph_rbearing;
5707 glyph->geometry.y_offset = logical_rect.height
5708 - (gui.char_height - p_linespace) * PANGO_SCALE;
5709 }
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005710 else
5711 /* If the accent width is smaller than the cluster width, position it
5712 * in the middle. */
5713 glyph->geometry.x_offset = -width + MAX(0, width - ink_rect.width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714}
5715
Bram Moolenaar98921892016-02-23 17:14:37 +01005716#if GTK_CHECK_VERSION(3,0,0)
5717 static void
5718draw_glyph_string(int row, int col, int num_cells, int flags,
5719 PangoFont *font, PangoGlyphString *glyphs,
5720 cairo_t *cr)
5721#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 static void
5723draw_glyph_string(int row, int col, int num_cells, int flags,
5724 PangoFont *font, PangoGlyphString *glyphs)
Bram Moolenaar98921892016-02-23 17:14:37 +01005725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726{
5727 if (!(flags & DRAW_TRANSP))
5728 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005729#if GTK_CHECK_VERSION(3,0,0)
5730 set_cairo_source_rgb_from_pixel(cr, gui.bgcolor->pixel);
5731 cairo_rectangle(cr,
5732 FILL_X(col), FILL_Y(row),
5733 num_cells * gui.char_width, gui.char_height);
5734 cairo_fill(cr);
5735#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
5737
5738 gdk_draw_rectangle(gui.drawarea->window,
5739 gui.text_gc,
5740 TRUE,
5741 FILL_X(col),
5742 FILL_Y(row),
5743 num_cells * gui.char_width,
5744 gui.char_height);
Bram Moolenaar98921892016-02-23 17:14:37 +01005745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 }
5747
Bram Moolenaar98921892016-02-23 17:14:37 +01005748#if GTK_CHECK_VERSION(3,0,0)
5749 set_cairo_source_rgb_from_pixel(cr, gui.fgcolor->pixel);
5750 cairo_move_to(cr, TEXT_X(col), TEXT_Y(row));
5751 pango_cairo_show_glyph_string(cr, font, glyphs);
5752#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5754
5755 gdk_draw_glyphs(gui.drawarea->window,
5756 gui.text_gc,
5757 font,
5758 TEXT_X(col),
5759 TEXT_Y(row),
5760 glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01005761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762
5763 /* redraw the contents with an offset of 1 to emulate bold */
5764 if ((flags & DRAW_BOLD) && !gui.font_can_bold)
Bram Moolenaar98921892016-02-23 17:14:37 +01005765#if GTK_CHECK_VERSION(3,0,0)
5766 {
5767 set_cairo_source_rgb_from_pixel(cr, gui.fgcolor->pixel);
5768 cairo_move_to(cr, TEXT_X(col) + 1, TEXT_Y(row));
5769 pango_cairo_show_glyph_string(cr, font, glyphs);
5770 }
5771#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 gdk_draw_glyphs(gui.drawarea->window,
5773 gui.text_gc,
5774 font,
5775 TEXT_X(col) + 1,
5776 TEXT_Y(row),
5777 glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01005778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779}
5780
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005781/*
5782 * Draw underline and undercurl at the bottom of the character cell.
5783 */
Bram Moolenaar98921892016-02-23 17:14:37 +01005784#if GTK_CHECK_VERSION(3,0,0)
5785 static void
5786draw_under(int flags, int row, int col, int cells, cairo_t *cr)
5787#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005788 static void
5789draw_under(int flags, int row, int col, int cells)
Bram Moolenaar98921892016-02-23 17:14:37 +01005790#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005791{
5792 int i;
5793 int offset;
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005794 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005795 int y = FILL_Y(row + 1) - 1;
5796
5797 /* Undercurl: draw curl at the bottom of the character cell. */
5798 if (flags & DRAW_UNDERC)
5799 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005800#if GTK_CHECK_VERSION(3,0,0)
5801 cairo_set_line_width(cr, 1.0);
5802 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
5803 set_cairo_source_rgb_from_pixel(cr, gui.spcolor->pixel);
5804 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
5805 {
5806 offset = val[i % 8];
5807 cairo_line_to(cr, i, y - offset + 0.5);
5808 }
5809 cairo_stroke(cr);
5810#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005811 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
5812 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
5813 {
5814 offset = val[i % 8];
5815 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset);
5816 }
5817 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar98921892016-02-23 17:14:37 +01005818#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005819 }
5820
5821 /* Underline: draw a line at the bottom of the character cell. */
5822 if (flags & DRAW_UNDERL)
5823 {
5824 /* When p_linespace is 0, overwrite the bottom row of pixels.
5825 * Otherwise put the line just below the character. */
5826 if (p_linespace > 1)
5827 y -= p_linespace - 1;
Bram Moolenaar98921892016-02-23 17:14:37 +01005828#if GTK_CHECK_VERSION(3,0,0)
5829 {
5830 cairo_set_line_width(cr, 1.0);
5831 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
5832 set_cairo_source_rgb_from_pixel(cr, gui.fgcolor->pixel);
5833 cairo_move_to(cr, FILL_X(col), y + 0.5);
5834 cairo_line_to(cr, FILL_X(col + cells), y + 0.5);
5835 cairo_stroke(cr);
5836 }
5837#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005838 gdk_draw_line(gui.drawarea->window, gui.text_gc,
5839 FILL_X(col), y,
5840 FILL_X(col + cells) - 1, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01005841#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005842 }
5843}
5844
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 int
5846gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
5847{
5848 GdkRectangle area; /* area for clip mask */
5849 PangoGlyphString *glyphs; /* glyphs of current item */
5850 int column_offset = 0; /* column offset in cells */
5851 int i;
5852 char_u *conv_buf = NULL; /* result of UTF-8 conversion */
5853 char_u *new_conv_buf;
5854 int convlen;
5855 char_u *sp, *bp;
5856 int plen;
Bram Moolenaar98921892016-02-23 17:14:37 +01005857#if GTK_CHECK_VERSION(3,0,0)
5858 cairo_t *cr;
5859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860
Bram Moolenaar98921892016-02-23 17:14:37 +01005861#if GTK_CHECK_VERSION(3,0,0)
5862 if (gui.text_context == NULL || gtk_widget_get_window(gui.drawarea) == NULL)
5863#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 if (gui.text_context == NULL || gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01005865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 return len;
5867
5868 if (output_conv.vc_type != CONV_NONE)
5869 {
5870 /*
5871 * Convert characters from 'encoding' to 'termencoding', which is set
5872 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
5873 * prohibits changing this to something else than UTF-8 if the GUI is
5874 * in use.
5875 */
5876 convlen = len;
5877 conv_buf = string_convert(&output_conv, s, &convlen);
5878 g_return_val_if_fail(conv_buf != NULL, len);
5879
5880 /* Correct for differences in char width: some chars are
5881 * double-wide in 'encoding' but single-wide in utf-8. Add a space to
5882 * compensate for that. */
5883 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; )
5884 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005885 plen = utf_ptr2len(bp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1)
5887 {
5888 new_conv_buf = alloc(convlen + 2);
5889 if (new_conv_buf == NULL)
5890 return len;
5891 plen += bp - conv_buf;
5892 mch_memmove(new_conv_buf, conv_buf, plen);
5893 new_conv_buf[plen] = ' ';
5894 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen,
5895 convlen - plen + 1);
5896 vim_free(conv_buf);
5897 conv_buf = new_conv_buf;
5898 ++convlen;
5899 bp = conv_buf + plen;
5900 plen = 1;
5901 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005902 sp += (*mb_ptr2len)(sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 bp += plen;
5904 }
5905 s = conv_buf;
5906 len = convlen;
5907 }
5908
5909 /*
5910 * Restrict all drawing to the current screen line in order to prevent
5911 * fuzzy font lookups from messing up the screen.
5912 */
5913 area.x = gui.border_offset;
5914 area.y = FILL_Y(row);
5915 area.width = gui.num_cols * gui.char_width;
5916 area.height = gui.char_height;
5917
Bram Moolenaar98921892016-02-23 17:14:37 +01005918#if GTK_CHECK_VERSION(3,0,0)
5919 cr = cairo_create(gui.surface);
5920 cairo_rectangle(cr, area.x, area.y, area.width, area.height);
5921 cairo_clip(cr);
5922#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 gdk_gc_set_clip_origin(gui.text_gc, 0, 0);
5924 gdk_gc_set_clip_rectangle(gui.text_gc, &area);
Bram Moolenaar98921892016-02-23 17:14:37 +01005925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926
5927 glyphs = pango_glyph_string_new();
5928
5929 /*
5930 * Optimization hack: If possible, skip the itemize and shaping process
5931 * for pure ASCII strings. This optimization is particularly effective
5932 * because Vim draws space characters to clear parts of the screen.
5933 */
5934 if (!(flags & DRAW_ITALIC)
5935 && !((flags & DRAW_BOLD) && gui.font_can_bold)
5936 && gui.ascii_glyphs != NULL)
5937 {
5938 char_u *p;
5939
5940 for (p = s; p < s + len; ++p)
5941 if (*p & 0x80)
5942 goto not_ascii;
5943
5944 pango_glyph_string_set_size(glyphs, len);
5945
5946 for (i = 0; i < len; ++i)
5947 {
5948 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[s[i]];
5949 glyphs->log_clusters[i] = i;
5950 }
5951
Bram Moolenaar98921892016-02-23 17:14:37 +01005952#if GTK_CHECK_VERSION(3,0,0)
5953 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs, cr);
5954#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01005956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957
5958 column_offset = len;
5959 }
5960 else
5961not_ascii:
5962 {
5963 PangoAttrList *attr_list;
5964 GList *item_list;
5965 int cluster_width;
5966 int last_glyph_rbearing;
5967 int cells = 0; /* cells occupied by current cluster */
5968
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005969 /* Safety check: pango crashes when invoked with invalid utf-8
5970 * characters. */
5971 if (!utf_valid_string(s, s + len))
5972 {
5973 column_offset = len;
5974 goto skipitall;
5975 }
5976
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 /* original width of the current cluster */
5978 cluster_width = PANGO_SCALE * gui.char_width;
5979
5980 /* right bearing of the last non-composing glyph */
5981 last_glyph_rbearing = PANGO_SCALE * gui.char_width;
5982
5983 attr_list = pango_attr_list_new();
5984
5985 /* If 'guifontwide' is set then use that for double-width characters.
5986 * Otherwise just go with 'guifont' and let Pango do its thing. */
5987 if (gui.wide_font != NULL)
5988 apply_wide_font_attr(s, len, attr_list);
5989
5990 if ((flags & DRAW_BOLD) && gui.font_can_bold)
5991 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD),
5992 attr_list, 0, len);
5993 if (flags & DRAW_ITALIC)
5994 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC),
5995 attr_list, 0, len);
5996 /*
5997 * Break the text into segments with consistent directional level
5998 * and shaping engine. Pure Latin text needs only a single segment,
5999 * so there's no need to worry about the loop's efficiency. Better
6000 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
6001 */
6002 item_list = pango_itemize(gui.text_context,
6003 (const char *)s, 0, len, attr_list, NULL);
6004
6005 while (item_list != NULL)
6006 {
6007 PangoItem *item;
6008 int item_cells = 0; /* item length in cells */
6009
6010 item = (PangoItem *)item_list->data;
6011 item_list = g_list_delete_link(item_list, item_list);
6012 /*
6013 * Increment the bidirectional embedding level by 1 if it is not
6014 * even. An odd number means the output will be RTL, but we don't
6015 * want that since Vim handles right-to-left text on its own. It
6016 * would probably be sufficient to just set level = 0, but you can
6017 * never know :)
6018 *
6019 * Unfortunately we can't take advantage of Pango's ability to
6020 * render both LTR and RTL at the same time. In order to support
6021 * that, Vim's main screen engine would have to make use of Pango
6022 * functionality.
6023 */
6024 item->analysis.level = (item->analysis.level + 1) & (~1U);
6025
6026 /* HACK: Overrule the shape engine, we don't want shaping to be
6027 * done, because drawing the cursor would change the display. */
6028 item->analysis.shape_engine = default_shape_engine;
6029
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02006030#ifdef HAVE_PANGO_SHAPE_FULL
Bram Moolenaar7e2ec002015-09-08 16:31:06 +02006031 pango_shape_full((const char *)s + item->offset, item->length,
6032 (const char *)s, len, &item->analysis, glyphs);
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02006033#else
6034 pango_shape((const char *)s + item->offset, item->length,
6035 &item->analysis, glyphs);
6036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 /*
6038 * Fixed-width hack: iterate over the array and assign a fixed
6039 * width to each glyph, thus overriding the choice made by the
6040 * shaping engine. We use utf_char2cells() to determine the
6041 * number of cells needed.
6042 *
6043 * Also perform all kind of dark magic to get composing
6044 * characters right (and pretty too of course).
6045 */
6046 for (i = 0; i < glyphs->num_glyphs; ++i)
6047 {
6048 PangoGlyphInfo *glyph;
6049
6050 glyph = &glyphs->glyphs[i];
6051
6052 if (glyph->attr.is_cluster_start)
6053 {
6054 int cellcount;
6055
6056 cellcount = count_cluster_cells(
6057 s, item, glyphs, i, &cluster_width,
6058 (item_list != NULL) ? &last_glyph_rbearing : NULL);
6059
6060 if (cellcount > 0)
6061 {
6062 int width;
6063
6064 width = cellcount * gui.char_width * PANGO_SCALE;
6065 glyph->geometry.x_offset +=
6066 MAX(0, width - cluster_width) / 2;
6067 glyph->geometry.width = width;
6068 }
6069 else
6070 {
6071 /* If there are only combining characters in the
6072 * cluster, we cannot just change the width of the
6073 * previous glyph since there is none. Therefore
6074 * some guesswork is needed. */
6075 setup_zero_width_cluster(item, glyph, cells,
6076 cluster_width,
6077 last_glyph_rbearing);
6078 }
6079
6080 item_cells += cellcount;
6081 cells = cellcount;
6082 }
6083 else if (i > 0)
6084 {
6085 int width;
6086
6087 /* There is a previous glyph, so we deal with combining
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006088 * characters the canonical way.
Bram Moolenaar96118f32010-08-08 14:40:37 +02006089 * In some circumstances Pango uses a positive x_offset,
6090 * then use the width of the previous glyph for this one
6091 * and set the previous width to zero.
6092 * Otherwise we get a negative x_offset, Pango has already
6093 * positioned the combining char, keep the widths as they
6094 * are.
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006095 * For both adjust the x_offset to position the glyph in
Bram Moolenaar96118f32010-08-08 14:40:37 +02006096 * the middle. */
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006097 if (glyph->geometry.x_offset >= 0)
Bram Moolenaar96118f32010-08-08 14:40:37 +02006098 {
6099 glyphs->glyphs[i].geometry.width =
6100 glyphs->glyphs[i - 1].geometry.width;
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006101 glyphs->glyphs[i - 1].geometry.width = 0;
Bram Moolenaar96118f32010-08-08 14:40:37 +02006102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 width = cells * gui.char_width * PANGO_SCALE;
6104 glyph->geometry.x_offset +=
6105 MAX(0, width - cluster_width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 }
6107 else /* i == 0 "cannot happen" */
6108 {
6109 glyph->geometry.width = 0;
6110 }
6111 }
6112
6113 /*** Aaaaand action! ***/
Bram Moolenaar98921892016-02-23 17:14:37 +01006114#if GTK_CHECK_VERSION(3,0,0)
6115 draw_glyph_string(row, col + column_offset, item_cells,
6116 flags, item->analysis.font, glyphs,
6117 cr);
6118#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 draw_glyph_string(row, col + column_offset, item_cells,
6120 flags, item->analysis.font, glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01006121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122
6123 pango_item_free(item);
6124
6125 column_offset += item_cells;
6126 }
6127
6128 pango_attr_list_unref(attr_list);
6129 }
6130
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006131skipitall:
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006132 /* Draw underline and undercurl. */
Bram Moolenaar98921892016-02-23 17:14:37 +01006133#if GTK_CHECK_VERSION(3,0,0)
6134 draw_under(flags, row, col, column_offset, cr);
6135#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006136 draw_under(flags, row, col, column_offset);
Bram Moolenaar98921892016-02-23 17:14:37 +01006137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138
6139 pango_glyph_string_free(glyphs);
6140 vim_free(conv_buf);
6141
Bram Moolenaar98921892016-02-23 17:14:37 +01006142#if GTK_CHECK_VERSION(3,0,0)
6143 cairo_destroy(cr);
6144 if (!gui.by_signal)
6145 gdk_window_invalidate_rect(gtk_widget_get_window(gui.drawarea),
6146 &area, FALSE);
6147#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 gdk_gc_set_clip_rectangle(gui.text_gc, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01006149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150
6151 return column_offset;
6152}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153
6154/*
6155 * Return OK if the key with the termcap name "name" is supported.
6156 */
6157 int
6158gui_mch_haskey(char_u *name)
6159{
6160 int i;
6161
6162 for (i = 0; special_keys[i].key_sym != 0; i++)
6163 if (name[0] == special_keys[i].code0
6164 && name[1] == special_keys[i].code1)
6165 return OK;
6166 return FAIL;
6167}
6168
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006169#if defined(FEAT_TITLE) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170/*
6171 * Return the text window-id and display. Only required for X-based GUI's
6172 */
6173 int
6174gui_get_x11_windis(Window *win, Display **dis)
6175{
Bram Moolenaar98921892016-02-23 17:14:37 +01006176#if GTK_CHECK_VERSION(3,0,0)
6177 if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL)
6178#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01006180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 {
Bram Moolenaar98921892016-02-23 17:14:37 +01006182#if GTK_CHECK_VERSION(3,0,0)
6183 *dis = GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin));
6184 *win = GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin));
6185#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 *dis = GDK_WINDOW_XDISPLAY(gui.mainwin->window);
6187 *win = GDK_WINDOW_XWINDOW(gui.mainwin->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01006188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 return OK;
6190 }
6191
6192 *dis = NULL;
6193 *win = 0;
6194 return FAIL;
6195}
6196#endif
6197
6198#if defined(FEAT_CLIENTSERVER) \
6199 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
6200
6201 Display *
6202gui_mch_get_display(void)
6203{
Bram Moolenaar98921892016-02-23 17:14:37 +01006204#if GTK_CHECK_VERSION(3,0,0)
6205 if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL)
6206 return GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin));
6207#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
6209 return GDK_WINDOW_XDISPLAY(gui.mainwin->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01006210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 else
6212 return NULL;
6213}
6214#endif
6215
6216 void
6217gui_mch_beep(void)
6218{
6219#ifdef HAVE_GTK_MULTIHEAD
6220 GdkDisplay *display;
6221
Bram Moolenaar98921892016-02-23 17:14:37 +01006222# if GTK_CHECK_VERSION(3,0,0)
6223 if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin))
6224# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
Bram Moolenaar98921892016-02-23 17:14:37 +01006226# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 display = gtk_widget_get_display(gui.mainwin);
6228 else
6229 display = gdk_display_get_default();
6230
6231 if (display != NULL)
6232 gdk_display_beep(display);
6233#else
6234 gdk_beep();
6235#endif
6236}
6237
6238 void
6239gui_mch_flash(int msec)
6240{
Bram Moolenaar98921892016-02-23 17:14:37 +01006241#if GTK_CHECK_VERSION(3,0,0)
6242 /* TODO Replace GdkGC with Cairo */
6243 (void)msec;
6244#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 GdkGCValues values;
6246 GdkGC *invert_gc;
6247
6248 if (gui.drawarea->window == NULL)
6249 return;
6250
6251 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6252 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
6253 values.function = GDK_XOR;
6254 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6255 &values,
6256 GDK_GC_FOREGROUND |
6257 GDK_GC_BACKGROUND |
6258 GDK_GC_FUNCTION);
6259 gdk_gc_set_exposures(invert_gc,
6260 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
6261 /*
6262 * Do a visual beep by changing back and forth in some undetermined way,
6263 * the foreground and background colors. This is due to the fact that
6264 * there can't be really any prediction about the effects of XOR on
6265 * arbitrary X11 servers. However this seems to be enough for what we
6266 * intend it to do.
6267 */
6268 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6269 TRUE,
6270 0, 0,
6271 FILL_X((int)Columns) + gui.border_offset,
6272 FILL_Y((int)Rows) + gui.border_offset);
6273
6274 gui_mch_flush();
6275 ui_delay((long)msec, TRUE); /* wait so many msec */
6276
6277 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6278 TRUE,
6279 0, 0,
6280 FILL_X((int)Columns) + gui.border_offset,
6281 FILL_Y((int)Rows) + gui.border_offset);
6282
6283 gdk_gc_destroy(invert_gc);
Bram Moolenaar98921892016-02-23 17:14:37 +01006284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285}
6286
6287/*
6288 * Invert a rectangle from row r, column c, for nr rows and nc columns.
6289 */
6290 void
6291gui_mch_invert_rectangle(int r, int c, int nr, int nc)
6292{
Bram Moolenaar98921892016-02-23 17:14:37 +01006293#if GTK_CHECK_VERSION(3,0,0)
6294 /* TODO Replace GdkGC with Cairo */
6295 (void)r; (void)c; (void)nr; (void)nc;
6296#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 GdkGCValues values;
6298 GdkGC *invert_gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299
6300 if (gui.drawarea->window == NULL)
6301 return;
6302
Bram Moolenaar89d40322006-08-29 15:30:07 +00006303 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6304 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 values.function = GDK_XOR;
6306 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6307 &values,
6308 GDK_GC_FOREGROUND |
6309 GDK_GC_BACKGROUND |
6310 GDK_GC_FUNCTION);
Bram Moolenaar89d40322006-08-29 15:30:07 +00006311 gdk_gc_set_exposures(invert_gc, gui.visibility !=
6312 GDK_VISIBILITY_UNOBSCURED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6314 TRUE,
6315 FILL_X(c), FILL_Y(r),
6316 (nc) * gui.char_width, (nr) * gui.char_height);
6317 gdk_gc_destroy(invert_gc);
Bram Moolenaar98921892016-02-23 17:14:37 +01006318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319}
6320
6321/*
6322 * Iconify the GUI window.
6323 */
6324 void
6325gui_mch_iconify(void)
6326{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 gtk_window_iconify(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328}
6329
6330#if defined(FEAT_EVAL) || defined(PROTO)
6331/*
6332 * Bring the Vim window to the foreground.
6333 */
6334 void
6335gui_mch_set_foreground(void)
6336{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 gtk_window_present(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338}
6339#endif
6340
6341/*
6342 * Draw a cursor without focus.
6343 */
6344 void
6345gui_mch_draw_hollow_cursor(guicolor_T color)
6346{
6347 int i = 1;
Bram Moolenaar98921892016-02-23 17:14:37 +01006348#if GTK_CHECK_VERSION(3,0,0)
6349 cairo_t *cr;
6350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351
Bram Moolenaar98921892016-02-23 17:14:37 +01006352#if GTK_CHECK_VERSION(3,0,0)
6353 if (gtk_widget_get_window(gui.drawarea) == NULL)
6354#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 if (gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01006356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 return;
6358
Bram Moolenaar98921892016-02-23 17:14:37 +01006359#if GTK_CHECK_VERSION(3,0,0)
6360 cr = cairo_create(gui.surface);
6361#endif
6362
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 gui_mch_set_fg_color(color);
6364
Bram Moolenaar98921892016-02-23 17:14:37 +01006365#if GTK_CHECK_VERSION(3,0,0)
6366 set_cairo_source_rgb_from_pixel(cr, gui.fgcolor->pixel);
6367#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar98921892016-02-23 17:14:37 +01006369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 if (mb_lefthalve(gui.row, gui.col))
6371 i = 2;
Bram Moolenaar98921892016-02-23 17:14:37 +01006372#if GTK_CHECK_VERSION(3,0,0)
6373 cairo_set_line_width(cr, 1.0);
6374 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
6375 cairo_rectangle(cr,
6376 FILL_X(gui.col) + 0.5, FILL_Y(gui.row) + 0.5,
6377 i * gui.char_width - 1, gui.char_height - 1);
6378 cairo_stroke(cr);
6379 cairo_destroy(cr);
6380#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6382 FALSE,
6383 FILL_X(gui.col), FILL_Y(gui.row),
6384 i * gui.char_width - 1, gui.char_height - 1);
Bram Moolenaar98921892016-02-23 17:14:37 +01006385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386}
6387
6388/*
6389 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
6390 * color "color".
6391 */
6392 void
6393gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
6394{
Bram Moolenaar98921892016-02-23 17:14:37 +01006395#if GTK_CHECK_VERSION(3,0,0)
6396 if (gtk_widget_get_window(gui.drawarea) == NULL)
6397#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 if (gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01006399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 return;
6401
6402 gui_mch_set_fg_color(color);
6403
Bram Moolenaar98921892016-02-23 17:14:37 +01006404#if GTK_CHECK_VERSION(3,0,0)
6405 {
6406 cairo_t *cr;
6407
6408 cr = cairo_create(gui.surface);
6409 set_cairo_source_rgb_from_pixel(cr, gui.fgcolor->pixel);
6410 cairo_rectangle(cr,
6411# ifdef FEAT_RIGHTLEFT
6412 /* vertical line should be on the right of current point */
6413 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
6414# endif
6415 FILL_X(gui.col), FILL_Y(gui.row) + gui.char_height - h,
6416 w, h);
6417 cairo_fill(cr);
6418 cairo_destroy(cr);
6419 }
6420#else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6422 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6423 TRUE,
Bram Moolenaar98921892016-02-23 17:14:37 +01006424# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 /* vertical line should be on the right of current point */
6426 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
Bram Moolenaar98921892016-02-23 17:14:37 +01006427# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 FILL_X(gui.col),
6429 FILL_Y(gui.row) + gui.char_height - h,
6430 w, h);
Bram Moolenaar98921892016-02-23 17:14:37 +01006431#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432}
6433
6434
6435/*
6436 * Catch up with any queued X11 events. This may put keyboard input into the
6437 * input buffer, call resize call-backs, trigger timers etc. If there is
6438 * nothing in the X11 event queue (& no timers pending), then we return
6439 * immediately.
6440 */
6441 void
6442gui_mch_update(void)
6443{
Bram Moolenaara3f41662010-07-11 19:01:06 +02006444 while (g_main_context_pending(NULL) && !vim_is_input_buf_full())
6445 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446}
6447
Bram Moolenaar98921892016-02-23 17:14:37 +01006448#if GTK_CHECK_VERSION(3,0,0)
6449 static gboolean
6450#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +01006452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453input_timer_cb(gpointer data)
6454{
6455 int *timed_out = (int *) data;
6456
Bram Moolenaar49325942007-05-10 19:19:59 +00006457 /* Just inform the caller about the occurrence of it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 *timed_out = TRUE;
6459
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 return FALSE; /* don't happen again */
6461}
6462
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463/*
6464 * GUI input routine called by gui_wait_for_chars(). Waits for a character
6465 * from the keyboard.
6466 * wtime == -1 Wait forever.
6467 * wtime == 0 This should never happen.
6468 * wtime > 0 Wait wtime milliseconds for a character.
6469 * Returns OK if a character was found to be available within the given time,
6470 * or FAIL otherwise.
6471 */
6472 int
6473gui_mch_wait_for_chars(long wtime)
6474{
6475 int focus;
6476 guint timer;
6477 static int timed_out;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478
6479 timed_out = FALSE;
6480
6481 /* this timeout makes sure that we will return if no characters arrived in
6482 * time */
6483
6484 if (wtime > 0)
Bram Moolenaar98921892016-02-23 17:14:37 +01006485#if GTK_CHECK_VERSION(3,0,0)
6486 timer = g_timeout_add((guint)wtime, input_timer_cb, &timed_out);
6487#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 timer = gtk_timeout_add((guint32)wtime, input_timer_cb, &timed_out);
Bram Moolenaar98921892016-02-23 17:14:37 +01006489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 else
6491 timer = 0;
6492
6493 focus = gui.in_focus;
6494
6495 do
6496 {
6497 /* Stop or start blinking when focus changes */
6498 if (gui.in_focus != focus)
6499 {
6500 if (gui.in_focus)
6501 gui_mch_start_blink();
6502 else
6503 gui_mch_stop_blink();
6504 focus = gui.in_focus;
6505 }
6506
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006507#ifdef MESSAGE_QUEUE
6508 parse_queued_messages();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006509#endif
6510
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 /*
6512 * Loop in GTK+ processing until a timeout or input occurs.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006513 * Skip this if input is available anyway (can happen in rare
6514 * situations, sort of race condition).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006516 if (!input_available())
Bram Moolenaara3f41662010-07-11 19:01:06 +02006517 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518
6519 /* Got char, return immediately */
6520 if (input_available())
6521 {
6522 if (timer != 0 && !timed_out)
Bram Moolenaar98921892016-02-23 17:14:37 +01006523#if GTK_CHECK_VERSION(3,0,0)
6524 g_source_remove(timer);
6525#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 gtk_timeout_remove(timer);
Bram Moolenaar98921892016-02-23 17:14:37 +01006527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 return OK;
6529 }
6530 } while (wtime < 0 || !timed_out);
6531
6532 /*
6533 * Flush all eventually pending (drawing) events.
6534 */
6535 gui_mch_update();
6536
6537 return FAIL;
6538}
6539
6540
6541/****************************************************************************
6542 * Output drawing routines.
6543 ****************************************************************************/
6544
6545
6546/* Flush any output to the screen */
6547 void
6548gui_mch_flush(void)
6549{
6550#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar98921892016-02-23 17:14:37 +01006551# if GTK_CHECK_VERSION(3,0,0)
6552 if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin))
6553# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
Bram Moolenaar98921892016-02-23 17:14:37 +01006555# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 gdk_display_sync(gtk_widget_get_display(gui.mainwin));
6557#else
6558 gdk_flush(); /* historical misnomer: calls XSync(), not XFlush() */
6559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 /* This happens to actually do what gui_mch_flush() is supposed to do,
6561 * according to the comment above. */
Bram Moolenaar98921892016-02-23 17:14:37 +01006562#if GTK_CHECK_VERSION(3,0,0)
6563 if (gui.drawarea != NULL && gtk_widget_get_window(gui.drawarea) != NULL)
6564 gdk_window_process_updates(gtk_widget_get_window(gui.drawarea), FALSE);
6565#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
6567 gdk_window_process_updates(gui.drawarea->window, FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01006568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569}
6570
6571/*
6572 * Clear a rectangular region of the screen from text pos (row1, col1) to
6573 * (row2, col2) inclusive.
6574 */
6575 void
6576gui_mch_clear_block(int row1, int col1, int row2, int col2)
6577{
Bram Moolenaar98921892016-02-23 17:14:37 +01006578#if GTK_CHECK_VERSION(3,0,0)
6579 if (gtk_widget_get_window(gui.drawarea) == NULL)
6580 return;
6581#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 GdkColor color;
6583
6584 if (gui.drawarea->window == NULL)
6585 return;
6586
6587 color.pixel = gui.back_pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01006588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589
Bram Moolenaar98921892016-02-23 17:14:37 +01006590#if GTK_CHECK_VERSION(3,0,0)
6591 {
6592 /* Add one pixel to the far right column in case a double-stroked
6593 * bold glyph may sit there. */
6594 const GdkRectangle rect = {
6595 FILL_X(col1), FILL_Y(row1),
6596 (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1),
6597 (row2 - row1 + 1) * gui.char_height
6598 };
6599 GdkWindow * const win = gtk_widget_get_window(gui.drawarea);
6600 cairo_t * const cr = cairo_create(gui.surface);
6601 cairo_pattern_t * const pat = gdk_window_get_background_pattern(win);
6602 if (pat != NULL)
6603 cairo_set_source(cr, pat);
6604 else
6605 set_cairo_source_rgb_from_pixel(cr, gui.back_pixel);
6606 gdk_cairo_rectangle(cr, &rect);
6607 cairo_fill(cr);
6608 cairo_destroy(cr);
6609
6610 if (!gui.by_signal)
6611 gdk_window_invalidate_rect(win, &rect, FALSE);
6612 }
6613#else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 gdk_gc_set_foreground(gui.text_gc, &color);
6615
6616 /* Clear one extra pixel at the far right, for when bold characters have
6617 * spilled over to the window border. */
6618 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE,
6619 FILL_X(col1), FILL_Y(row1),
6620 (col2 - col1 + 1) * gui.char_width
6621 + (col2 == Columns - 1),
6622 (row2 - row1 + 1) * gui.char_height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006623#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624}
6625
Bram Moolenaar98921892016-02-23 17:14:37 +01006626#if GTK_CHECK_VERSION(3,0,0)
6627 static void
6628gui_gtk_window_clear(GdkWindow *win)
6629{
6630 const GdkRectangle rect = {
6631 0, 0, gdk_window_get_width(win), gdk_window_get_height(win)
6632 };
6633 cairo_t * const cr = cairo_create(gui.surface);
6634 cairo_pattern_t * const pat = gdk_window_get_background_pattern(win);
6635 if (pat != NULL)
6636 cairo_set_source(cr, pat);
6637 else
6638 set_cairo_source_rgb_from_pixel(cr, gui.back_pixel);
6639 gdk_cairo_rectangle(cr, &rect);
6640 cairo_fill(cr);
6641 cairo_destroy(cr);
6642
6643 if (!gui.by_signal)
6644 gdk_window_invalidate_rect(win, &rect, FALSE);
6645}
6646#endif
6647
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 void
6649gui_mch_clear_all(void)
6650{
Bram Moolenaar98921892016-02-23 17:14:37 +01006651#if GTK_CHECK_VERSION(3,0,0)
6652 if (gtk_widget_get_window(gui.drawarea) != NULL)
6653 gui_gtk_window_clear(gtk_widget_get_window(gui.drawarea));
6654#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 if (gui.drawarea->window != NULL)
6656 gdk_window_clear(gui.drawarea->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01006657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658}
6659
Bram Moolenaar98921892016-02-23 17:14:37 +01006660#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661/*
6662 * Redraw any text revealed by scrolling up/down.
6663 */
6664 static void
6665check_copy_area(void)
6666{
6667 GdkEvent *event;
6668 int expose_count;
6669
6670 if (gui.visibility != GDK_VISIBILITY_PARTIAL)
6671 return;
6672
6673 /* Avoid redrawing the cursor while scrolling or it'll end up where
6674 * we don't want it to be. I'm not sure if it's correct to call
6675 * gui_dont_update_cursor() at this point but it works as a quick
6676 * fix for now. */
6677 gui_dont_update_cursor();
6678
6679 do
6680 {
6681 /* Wait to check whether the scroll worked or not. */
6682 event = gdk_event_get_graphics_expose(gui.drawarea->window);
6683
6684 if (event == NULL)
6685 break; /* received NoExpose event */
6686
6687 gui_redraw(event->expose.area.x, event->expose.area.y,
6688 event->expose.area.width, event->expose.area.height);
6689
6690 expose_count = event->expose.count;
6691 gdk_event_free(event);
6692 }
6693 while (expose_count > 0); /* more events follow */
6694
6695 gui_can_update_cursor();
6696}
Bram Moolenaar98921892016-02-23 17:14:37 +01006697#endif /* !GTK_CHECK_VERSION(3,0,0) */
6698
6699#if GTK_CHECK_VERSION(3,0,0)
6700 static void
6701gui_gtk_surface_copy_rect(int dest_x, int dest_y,
6702 int src_x, int src_y,
6703 int width, int height)
6704{
6705 cairo_t * const cr = cairo_create(gui.surface);
6706
6707 cairo_rectangle(cr, dest_x, dest_y, width, height);
6708 cairo_clip(cr);
6709 cairo_push_group(cr);
6710 cairo_set_source_surface(cr, gui.surface, dest_x - src_x, dest_y - src_y);
6711 cairo_paint(cr);
6712 cairo_pop_group_to_source(cr);
6713 cairo_paint(cr);
6714
6715 cairo_destroy(cr);
6716}
6717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718
6719/*
6720 * Delete the given number of lines from the given row, scrolling up any
6721 * text further down within the scroll region.
6722 */
6723 void
6724gui_mch_delete_lines(int row, int num_lines)
6725{
Bram Moolenaar98921892016-02-23 17:14:37 +01006726#if GTK_CHECK_VERSION(3,0,0)
6727 const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1;
6728 const int nrows = gui.scroll_region_bot - row + 1;
6729 const int src_nrows = nrows - num_lines;
6730
6731 gui_gtk_surface_copy_rect(
6732 FILL_X(gui.scroll_region_left), FILL_Y(row),
6733 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6734 gui.char_width * ncols + 1, gui.char_height * src_nrows);
6735 gui_clear_block(
6736 gui.scroll_region_bot - num_lines + 1, gui.scroll_region_left,
6737 gui.scroll_region_bot, gui.scroll_region_right);
6738 gui_gtk3_redraw(
6739 FILL_X(gui.scroll_region_left), FILL_Y(row),
6740 gui.char_width * ncols + 1, gui.char_height * nrows);
6741 if (!gui.by_signal)
6742 gtk_widget_queue_draw_area(gui.drawarea,
6743 FILL_X(gui.scroll_region_left), FILL_Y(row),
6744 gui.char_width * ncols + 1, gui.char_height * nrows);
6745#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
6747 return; /* Can't see the window */
6748
6749 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6750 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6751
6752 /* copy one extra pixel, for when bold has spilled over */
6753 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6754 FILL_X(gui.scroll_region_left), FILL_Y(row),
6755 gui.drawarea->window,
6756 FILL_X(gui.scroll_region_left),
6757 FILL_Y(row + num_lines),
6758 gui.char_width * (gui.scroll_region_right
6759 - gui.scroll_region_left + 1) + 1,
6760 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6761
6762 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
6763 gui.scroll_region_left,
6764 gui.scroll_region_bot, gui.scroll_region_right);
6765 check_copy_area();
Bram Moolenaar98921892016-02-23 17:14:37 +01006766#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767}
6768
6769/*
6770 * Insert the given number of lines before the given row, scrolling down any
6771 * following text within the scroll region.
6772 */
6773 void
6774gui_mch_insert_lines(int row, int num_lines)
6775{
Bram Moolenaar98921892016-02-23 17:14:37 +01006776#if GTK_CHECK_VERSION(3,0,0)
6777 const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1;
6778 const int nrows = gui.scroll_region_bot - row + 1;
6779 const int src_nrows = nrows - num_lines;
6780
6781 gui_gtk_surface_copy_rect(
6782 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6783 FILL_X(gui.scroll_region_left), FILL_Y(row),
6784 gui.char_width * ncols + 1, gui.char_height * src_nrows);
6785 gui_mch_clear_block(
6786 row, gui.scroll_region_left,
6787 row + num_lines - 1, gui.scroll_region_right);
6788 gui_gtk3_redraw(
6789 FILL_X(gui.scroll_region_left), FILL_Y(row),
6790 gui.char_width * ncols + 1, gui.char_height * nrows);
6791 if (!gui.by_signal)
6792 gtk_widget_queue_draw_area(gui.drawarea,
6793 FILL_X(gui.scroll_region_left), FILL_Y(row),
6794 gui.char_width * ncols + 1, gui.char_height * nrows);
6795#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
6797 return; /* Can't see the window */
6798
6799 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6800 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6801
6802 /* copy one extra pixel, for when bold has spilled over */
6803 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6804 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6805 gui.drawarea->window,
6806 FILL_X(gui.scroll_region_left), FILL_Y(row),
6807 gui.char_width * (gui.scroll_region_right
6808 - gui.scroll_region_left + 1) + 1,
6809 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6810
6811 gui_clear_block(row, gui.scroll_region_left,
6812 row + num_lines - 1, gui.scroll_region_right);
6813 check_copy_area();
Bram Moolenaar98921892016-02-23 17:14:37 +01006814#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815}
6816
6817/*
6818 * X Selection stuff, for cutting and pasting text to other windows.
6819 */
6820 void
6821clip_mch_request_selection(VimClipboard *cbd)
6822{
6823 GdkAtom target;
6824 unsigned i;
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00006825 time_t start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826
6827 for (i = 0; i < N_SELECTION_TARGETS; ++i)
6828 {
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006829 if (!clip_html && selection_targets[i].info == TARGET_HTML)
6830 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 received_selection = RS_NONE;
6832 target = gdk_atom_intern(selection_targets[i].target, FALSE);
6833
6834 gtk_selection_convert(gui.drawarea,
6835 cbd->gtk_sel_atom, target,
6836 (guint32)GDK_CURRENT_TIME);
6837
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00006838 /* Hack: Wait up to three seconds for the selection. A hang was
6839 * noticed here when using the netrw plugin combined with ":gui"
6840 * during the FocusGained event. */
6841 start = time(NULL);
6842 while (received_selection == RS_NONE && time(NULL) < start + 3)
Bram Moolenaara3f41662010-07-11 19:01:06 +02006843 g_main_context_iteration(NULL, TRUE); /* wait for selection_received_cb */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844
6845 if (received_selection != RS_FAIL)
6846 return;
6847 }
6848
6849 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaar98921892016-02-23 17:14:37 +01006850#if GTK_CHECK_VERSION(3,0,0)
6851 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)),
6852 cbd);
6853#else
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006854 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gui.mainwin->window), cbd);
Bram Moolenaar98921892016-02-23 17:14:37 +01006855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856}
6857
6858/*
6859 * Disown the selection.
6860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00006862clip_mch_lose_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863{
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01006864 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866}
6867
6868/*
6869 * Own the selection and return OK if it worked.
6870 */
6871 int
6872clip_mch_own_selection(VimClipboard *cbd)
6873{
6874 int success;
6875
6876 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom,
Bram Moolenaar20892c12011-06-26 04:49:00 +02006877 gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 gui_mch_update();
6879 return (success) ? OK : FAIL;
6880}
6881
6882/*
6883 * Send the current selection to the clipboard. Do nothing for X because we
6884 * will fill in the selection only when requested by another app.
6885 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00006887clip_mch_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888{
6889}
6890
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01006891 int
6892clip_gtk_owner_exists(VimClipboard *cbd)
6893{
6894 return gdk_selection_owner_get(cbd->gtk_sel_atom) != NULL;
6895}
6896
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897
6898#if defined(FEAT_MENU) || defined(PROTO)
6899/*
6900 * Make a menu item appear either active or not active (grey or not grey).
6901 */
6902 void
6903gui_mch_menu_grey(vimmenu_T *menu, int grey)
6904{
6905 if (menu->id == NULL)
6906 return;
6907
6908 if (menu_is_separator(menu->name))
6909 grey = TRUE;
6910
6911 gui_mch_menu_hidden(menu, FALSE);
6912 /* Be clever about bitfields versus true booleans here! */
Bram Moolenaar98921892016-02-23 17:14:37 +01006913# if GTK_CHECK_VERSION(3,0,0)
6914 if (!gtk_widget_get_sensitive(menu->id) == !grey)
6915# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 if (!GTK_WIDGET_SENSITIVE(menu->id) == !grey)
Bram Moolenaar98921892016-02-23 17:14:37 +01006917# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 {
6919 gtk_widget_set_sensitive(menu->id, !grey);
6920 gui_mch_update();
6921 }
6922}
6923
6924/*
6925 * Make menu item hidden or not hidden.
6926 */
6927 void
6928gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
6929{
6930 if (menu->id == 0)
6931 return;
6932
6933 if (hidden)
6934 {
Bram Moolenaar98921892016-02-23 17:14:37 +01006935# if GTK_CHECK_VERSION(3,0,0)
6936 if (gtk_widget_get_visible(menu->id))
6937# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 if (GTK_WIDGET_VISIBLE(menu->id))
Bram Moolenaar98921892016-02-23 17:14:37 +01006939# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 {
6941 gtk_widget_hide(menu->id);
6942 gui_mch_update();
6943 }
6944 }
6945 else
6946 {
Bram Moolenaar98921892016-02-23 17:14:37 +01006947# if GTK_CHECK_VERSION(3,0,0)
6948 if (!gtk_widget_get_visible(menu->id))
6949# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 if (!GTK_WIDGET_VISIBLE(menu->id))
Bram Moolenaar98921892016-02-23 17:14:37 +01006951# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 {
6953 gtk_widget_show(menu->id);
6954 gui_mch_update();
6955 }
6956 }
6957}
6958
6959/*
6960 * This is called after setting all the menus to grey/hidden or not.
6961 */
6962 void
6963gui_mch_draw_menubar(void)
6964{
6965 /* just make sure that the visual changes get effect immediately */
6966 gui_mch_update();
6967}
6968#endif /* FEAT_MENU */
6969
6970/*
6971 * Scrollbar stuff.
6972 */
6973 void
6974gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
6975{
6976 if (sb->id == NULL)
6977 return;
6978
Bram Moolenaar98921892016-02-23 17:14:37 +01006979#if GTK_CHECK_VERSION(3,0,0)
6980 gtk_widget_set_visible(sb->id, flag);
6981#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 if (flag)
6983 gtk_widget_show(sb->id);
6984 else
6985 gtk_widget_hide(sb->id);
Bram Moolenaar98921892016-02-23 17:14:37 +01006986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00006988 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989}
6990
6991
6992/*
6993 * Return the RGB value of a pixel as long.
6994 */
6995 long_u
6996gui_mch_get_rgb(guicolor_T pixel)
6997{
6998 GdkColor color;
Bram Moolenaar98921892016-02-23 17:14:37 +01006999#if GTK_CHECK_VERSION(3,0,0)
7000 GdkRGBA rgba;
7001
7002 gui_gtk_get_rgb_from_pixel(pixel, &rgba);
7003
7004 color.red = rgba.red * 65535;
7005 color.green = rgba.green * 65535;
7006 color.blue = rgba.blue * 65535;
7007#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
7009 (unsigned long)pixel, &color);
Bram Moolenaar98921892016-02-23 17:14:37 +01007010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011
7012 return (((unsigned)color.red & 0xff00) << 8)
7013 | ((unsigned)color.green & 0xff00)
7014 | (((unsigned)color.blue & 0xff00) >> 8);
7015}
7016
7017/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007018 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007020 void
7021gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022{
Bram Moolenaar98921892016-02-23 17:14:37 +01007023#if GTK_CHECK_VERSION(3,0,0)
7024 gui_gtk_get_pointer(gui.drawarea, x, y, NULL);
7025#else
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007026 gdk_window_get_pointer(gui.drawarea->window, x, y, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01007027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028}
7029
7030 void
7031gui_mch_setmouse(int x, int y)
7032{
7033 /* Sorry for the Xlib call, but we can't avoid it, since there is no
7034 * internal GDK mechanism present to accomplish this. (and for good
7035 * reason...) */
Bram Moolenaar98921892016-02-23 17:14:37 +01007036#if GTK_CHECK_VERSION(3,0,0)
7037 XWarpPointer(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.drawarea)),
7038 (Window)0, GDK_WINDOW_XID(gtk_widget_get_window(gui.drawarea)),
7039 0, 0, 0U, 0U, x, y);
7040#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 XWarpPointer(GDK_WINDOW_XDISPLAY(gui.drawarea->window),
7042 (Window)0, GDK_WINDOW_XWINDOW(gui.drawarea->window),
7043 0, 0, 0U, 0U, x, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01007044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045}
7046
7047
7048#ifdef FEAT_MOUSESHAPE
7049/* The last set mouse pointer shape is remembered, to be used when it goes
7050 * from hidden to not hidden. */
7051static int last_shape = 0;
7052#endif
7053
7054/*
7055 * Use the blank mouse pointer or not.
7056 *
7057 * hide: TRUE = use blank ptr, FALSE = use parent ptr
7058 */
7059 void
7060gui_mch_mousehide(int hide)
7061{
7062 if (gui.pointer_hidden != hide)
7063 {
7064 gui.pointer_hidden = hide;
Bram Moolenaar98921892016-02-23 17:14:37 +01007065#if GTK_CHECK_VERSION(3,0,0)
7066 if (gtk_widget_get_window(gui.drawarea) && gui.blank_pointer != NULL)
7067#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 if (gui.drawarea->window && gui.blank_pointer != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01007069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 {
7071 if (hide)
Bram Moolenaar98921892016-02-23 17:14:37 +01007072#if GTK_CHECK_VERSION(3,0,0)
7073 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
7074 gui.blank_pointer);
7075#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01007077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 else
7079#ifdef FEAT_MOUSESHAPE
7080 mch_set_mouse_shape(last_shape);
7081#else
7082 gdk_window_set_cursor(gui.drawarea->window, NULL);
7083#endif
7084 }
7085 }
7086}
7087
7088#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
7089
7090/* Table for shape IDs. Keep in sync with the mshape_names[] table in
7091 * misc2.c! */
7092static const int mshape_ids[] =
7093{
7094 GDK_LEFT_PTR, /* arrow */
7095 GDK_CURSOR_IS_PIXMAP, /* blank */
7096 GDK_XTERM, /* beam */
7097 GDK_SB_V_DOUBLE_ARROW, /* updown */
7098 GDK_SIZING, /* udsizing */
7099 GDK_SB_H_DOUBLE_ARROW, /* leftright */
7100 GDK_SIZING, /* lrsizing */
7101 GDK_WATCH, /* busy */
7102 GDK_X_CURSOR, /* no */
7103 GDK_CROSSHAIR, /* crosshair */
7104 GDK_HAND1, /* hand1 */
7105 GDK_HAND2, /* hand2 */
7106 GDK_PENCIL, /* pencil */
7107 GDK_QUESTION_ARROW, /* question */
7108 GDK_RIGHT_PTR, /* right-arrow */
7109 GDK_CENTER_PTR, /* up-arrow */
7110 GDK_LEFT_PTR /* last one */
7111};
7112
7113 void
7114mch_set_mouse_shape(int shape)
7115{
7116 int id;
7117 GdkCursor *c;
7118
Bram Moolenaar98921892016-02-23 17:14:37 +01007119# if GTK_CHECK_VERSION(3,0,0)
7120 if (gtk_widget_get_window(gui.drawarea) == NULL)
7121# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 if (gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01007123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 return;
7125
7126 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
Bram Moolenaar98921892016-02-23 17:14:37 +01007127# if GTK_CHECK_VERSION(3,0,0)
7128 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
7129 gui.blank_pointer);
7130# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01007132# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 else
7134 {
7135 if (shape >= MSHAPE_NUMBERED)
7136 {
7137 id = shape - MSHAPE_NUMBERED;
7138 if (id >= GDK_LAST_CURSOR)
7139 id = GDK_LEFT_PTR;
7140 else
7141 id &= ~1; /* they are always even (why?) */
7142 }
Bram Moolenaarb85cb212009-05-17 14:24:23 +00007143 else if (shape < (int)(sizeof(mshape_ids) / sizeof(int)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 id = mshape_ids[shape];
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007145 else
7146 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147# ifdef HAVE_GTK_MULTIHEAD
7148 c = gdk_cursor_new_for_display(
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00007149 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150# else
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00007151 c = gdk_cursor_new((GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01007153# if GTK_CHECK_VERSION(3,0,0)
7154 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), c);
7155# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 gdk_window_set_cursor(gui.drawarea->window, c);
Bram Moolenaar98921892016-02-23 17:14:37 +01007157# endif
7158# if GTK_CHECK_VERSION(3,0,0)
7159 g_object_unref(G_OBJECT(c));
7160# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 gdk_cursor_destroy(c); /* Unref, actually. Bloody GTK+ 1. */
Bram Moolenaar98921892016-02-23 17:14:37 +01007162# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 }
7164 if (shape != MSHAPE_HIDE)
7165 last_shape = shape;
7166}
7167#endif /* FEAT_MOUSESHAPE */
7168
7169
7170#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
7171/*
7172 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
7173 * scaled down if the current font is not big enough, or scaled up if the image
7174 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
7175 * will be cut off if the current font is not big enough, or centered if it's
7176 * too small.
7177 */
7178# define SIGN_WIDTH (2 * gui.char_width)
7179# define SIGN_HEIGHT (gui.char_height)
7180# define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
7181
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 void
7183gui_mch_drawsign(int row, int col, int typenr)
7184{
7185 GdkPixbuf *sign;
7186
7187 sign = (GdkPixbuf *)sign_get_image(typenr);
7188
Bram Moolenaar98921892016-02-23 17:14:37 +01007189# if GTK_CHECK_VERSION(3,0,0)
7190 if (sign != NULL && gui.drawarea != NULL
7191 && gtk_widget_get_window(gui.drawarea) != NULL)
7192# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 if (sign != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01007194# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 {
7196 int width;
7197 int height;
7198 int xoffset;
7199 int yoffset;
7200 int need_scale;
7201
7202 width = gdk_pixbuf_get_width(sign);
7203 height = gdk_pixbuf_get_height(sign);
7204 /*
7205 * Decide whether we need to scale. Allow one pixel of border
7206 * width to be cut off, in order to avoid excessive scaling for
7207 * tiny differences in font size.
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007208 * Do scale to fit the height to avoid gaps because of linespacing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 */
7210 need_scale = (width > SIGN_WIDTH + 2
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007211 || height != SIGN_HEIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 || (width < 3 * SIGN_WIDTH / 4
7213 && height < 3 * SIGN_HEIGHT / 4));
7214 if (need_scale)
7215 {
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007216 double aspect;
7217 int w = width;
7218 int h = height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219
7220 /* Keep the original aspect ratio */
7221 aspect = (double)height / (double)width;
7222 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect;
7223 width = MIN(width, SIGN_WIDTH);
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007224 if (((double)(MAX(height, SIGN_HEIGHT)) /
7225 (double)(MIN(height, SIGN_HEIGHT))) < 1.15)
7226 {
7227 /* Change the aspect ratio by at most 15% to fill the
7228 * available space completly. */
7229 height = (double)SIGN_HEIGHT * SIGN_ASPECT / aspect;
7230 height = MIN(height, SIGN_HEIGHT);
7231 }
7232 else
7233 height = (double)width * aspect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007235 if (w == width && h == height)
7236 {
7237 /* no change in dimensions; don't decrease reference counter
7238 * (below) */
7239 need_scale = FALSE;
7240 }
7241 else
7242 {
7243 /* This doesn't seem to be worth caching, and doing so would
7244 * complicate the code quite a bit. */
7245 sign = gdk_pixbuf_scale_simple(sign, width, height,
7246 GDK_INTERP_BILINEAR);
7247 if (sign == NULL)
7248 return; /* out of memory */
7249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 }
7251
7252 /* The origin is the upper-left corner of the pixmap. Therefore
7253 * these offset may become negative if the pixmap is smaller than
7254 * the 2x1 cells reserved for the sign icon. */
7255 xoffset = (width - SIGN_WIDTH) / 2;
7256 yoffset = (height - SIGN_HEIGHT) / 2;
7257
Bram Moolenaar98921892016-02-23 17:14:37 +01007258# if GTK_CHECK_VERSION(3,0,0)
7259 {
7260 cairo_t *cr;
7261 cairo_surface_t *bg_surf;
7262 cairo_t *bg_cr;
7263 cairo_surface_t *sign_surf;
7264 cairo_t *sign_cr;
7265
7266 cr = cairo_create(gui.surface);
7267
7268 bg_surf = cairo_surface_create_similar(gui.surface,
7269 cairo_surface_get_content(gui.surface),
7270 SIGN_WIDTH, SIGN_HEIGHT);
7271 bg_cr = cairo_create(bg_surf);
7272 set_cairo_source_rgb_from_pixel(bg_cr, gui.bgcolor->pixel);
7273 cairo_paint(bg_cr);
7274
7275 sign_surf = cairo_surface_create_similar(gui.surface,
7276 cairo_surface_get_content(gui.surface),
7277 SIGN_WIDTH, SIGN_HEIGHT);
7278 sign_cr = cairo_create(sign_surf);
7279 gdk_cairo_set_source_pixbuf(sign_cr, sign, -xoffset, -yoffset);
7280 cairo_paint(sign_cr);
7281
7282 cairo_set_operator(sign_cr, CAIRO_OPERATOR_DEST_OVER);
7283 cairo_set_source_surface(sign_cr, bg_surf, 0, 0);
7284 cairo_paint(sign_cr);
7285
7286 cairo_set_source_surface(cr, sign_surf, FILL_X(col), FILL_Y(row));
7287 cairo_paint(cr);
7288
7289 cairo_destroy(sign_cr);
7290 cairo_surface_destroy(sign_surf);
7291 cairo_destroy(bg_cr);
7292 cairo_surface_destroy(bg_surf);
7293 cairo_destroy(cr);
7294
7295 if (!gui.by_signal)
7296 gtk_widget_queue_draw_area(gui.drawarea,
7297 FILL_X(col), FILL_Y(col), width, height);
7298
7299 }
7300# else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
7302
7303 gdk_draw_rectangle(gui.drawarea->window,
7304 gui.text_gc,
7305 TRUE,
7306 FILL_X(col),
7307 FILL_Y(row),
7308 SIGN_WIDTH,
7309 SIGN_HEIGHT);
7310
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 gdk_pixbuf_render_to_drawable_alpha(sign,
7312 gui.drawarea->window,
7313 MAX(0, xoffset),
7314 MAX(0, yoffset),
7315 FILL_X(col) - MIN(0, xoffset),
7316 FILL_Y(row) - MIN(0, yoffset),
7317 MIN(width, SIGN_WIDTH),
7318 MIN(height, SIGN_HEIGHT),
7319 GDK_PIXBUF_ALPHA_BILEVEL,
7320 127,
7321 GDK_RGB_DITHER_NORMAL,
7322 0, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01007323# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 if (need_scale)
7325 g_object_unref(sign);
7326 }
7327}
7328
7329 void *
7330gui_mch_register_sign(char_u *signfile)
7331{
7332 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use)
7333 {
7334 GdkPixbuf *sign;
7335 GError *error = NULL;
7336 char_u *message;
7337
7338 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error);
7339
7340 if (error == NULL)
7341 return sign;
7342
7343 message = (char_u *)error->message;
7344
7345 if (message != NULL && input_conv.vc_type != CONV_NONE)
7346 message = string_convert(&input_conv, message, NULL);
7347
7348 if (message != NULL)
7349 {
7350 /* The error message is already translated and will be more
7351 * descriptive than anything we could possibly do ourselves. */
7352 EMSG2("E255: %s", message);
7353
7354 if (input_conv.vc_type != CONV_NONE)
7355 vim_free(message);
7356 }
7357 g_error_free(error);
7358 }
7359
7360 return NULL;
7361}
7362
7363 void
7364gui_mch_destroy_sign(void *sign)
7365{
7366 if (sign != NULL)
7367 g_object_unref(sign);
7368}
7369
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370#endif /* FEAT_SIGN_ICONS */