blob: 54723f7d074f488cefab2923c0580ce6e5778c0c [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;
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100620static gboolean blink_mode = TRUE;
Bram Moolenaar98921892016-02-23 17:14:37 +0100621
622static gboolean gui_gtk_is_blink_on(void);
Bram Moolenaar98921892016-02-23 17:14:37 +0100623static 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;
Bram Moolenaar4fc563b2016-03-12 12:40:58 +0100639 if (State & CMDLINE)
640 gui_update_cursor(TRUE, FALSE);
641 else
642 gui_update_cursor(TRUE, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +0100643 gui.by_signal = FALSE;
644 cairo_paint(cr);
645 }
646}
647
648 static gboolean
649gui_gtk3_should_draw_cursor(void)
650{
651 unsigned int cond = 0;
652 cond |= gui_gtk_is_blink_on();
653 cond |= is_key_pressed;
654 cond |= gui.in_focus == FALSE;
Bram Moolenaar98921892016-02-23 17:14:37 +0100655 return cond;
656}
657
658 static gboolean
659draw_event(GtkWidget *widget,
660 cairo_t *cr,
661 gpointer user_data UNUSED)
662{
663 /* Skip this when the GUI isn't set up yet, will redraw later. */
664 if (gui.starting)
665 return FALSE;
666
667 out_flush(); /* make sure all output has been processed */
668 /* for GTK+ 3, may induce other draw events. */
669
670 cairo_set_source_surface(cr, gui.surface, 0, 0);
671
672 /* Draw the window without the cursor. */
673 gui.by_signal = TRUE;
674 {
675 cairo_rectangle_list_t *list = NULL;
676
677 gui_gtk_window_clear(gtk_widget_get_window(widget));
678
679 list = cairo_copy_clip_rectangle_list(cr);
680 if (list->status != CAIRO_STATUS_CLIP_NOT_REPRESENTABLE)
681 {
682 int i;
683 for (i = 0; i < list->num_rectangles; i++)
684 {
685 const cairo_rectangle_t rect = list->rectangles[i];
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100686 if (blink_mode)
687 gui_gtk3_redraw(rect.x, rect.y, rect.width, rect.height);
688 else
689 gui_redraw(rect.x, rect.y, rect.width, rect.height);
Bram Moolenaar98921892016-02-23 17:14:37 +0100690 }
691 }
692 cairo_rectangle_list_destroy(list);
693
694 cairo_paint(cr);
695 }
696 gui.by_signal = FALSE;
697
698 /* Add the cursor to the window if necessary.*/
699 if (gui_gtk3_should_draw_cursor())
700 gui_gtk3_update_cursor(cr);
701
702 return FALSE;
703}
704#else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000706expose_event(GtkWidget *widget UNUSED,
707 GdkEventExpose *event,
708 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709{
710 /* Skip this when the GUI isn't set up yet, will redraw later. */
711 if (gui.starting)
712 return FALSE;
713
714 out_flush(); /* make sure all output has been processed */
715 gui_redraw(event->area.x, event->area.y,
716 event->area.width, event->area.height);
717
718 /* Clear the border areas if needed */
719 if (event->area.x < FILL_X(0))
720 gdk_window_clear_area(gui.drawarea->window, 0, 0, FILL_X(0), 0);
721 if (event->area.y < FILL_Y(0))
722 gdk_window_clear_area(gui.drawarea->window, 0, 0, 0, FILL_Y(0));
723 if (event->area.x > FILL_X(Columns))
724 gdk_window_clear_area(gui.drawarea->window,
725 FILL_X((int)Columns), 0, 0, 0);
726 if (event->area.y > FILL_Y(Rows))
727 gdk_window_clear_area(gui.drawarea->window, 0, FILL_Y((int)Rows), 0, 0);
728
729 return FALSE;
730}
Bram Moolenaar98921892016-02-23 17:14:37 +0100731#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
733#ifdef FEAT_CLIENTSERVER
734/*
735 * Handle changes to the "Comm" property
736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000738property_event(GtkWidget *widget,
739 GdkEventProperty *event,
740 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741{
742 if (event->type == GDK_PROPERTY_NOTIFY
743 && event->state == (int)GDK_PROPERTY_NEW_VALUE
Bram Moolenaar98921892016-02-23 17:14:37 +0100744# if GTK_CHECK_VERSION(3,0,0)
745 && GDK_WINDOW_XID(event->window) == commWindow
746# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 && GDK_WINDOW_XWINDOW(event->window) == commWindow
Bram Moolenaar98921892016-02-23 17:14:37 +0100748# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 && GET_X_ATOM(event->atom) == commProperty)
750 {
751 XEvent xev;
752
753 /* Translate to XLib */
754 xev.xproperty.type = PropertyNotify;
755 xev.xproperty.atom = commProperty;
756 xev.xproperty.window = commWindow;
757 xev.xproperty.state = PropertyNewValue;
Bram Moolenaar98921892016-02-23 17:14:37 +0100758# if GTK_CHECK_VERSION(3,0,0)
759 serverEventProc(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(widget)),
760 &xev, 0);
761# else
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200762 serverEventProc(GDK_WINDOW_XDISPLAY(widget->window), &xev, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +0100763# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 }
765 return FALSE;
766}
Bram Moolenaar98921892016-02-23 17:14:37 +0100767#endif /* defined(FEAT_CLIENTSERVER) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768
769
770/****************************************************************************
771 * Focus handlers:
772 */
773
774
775/*
776 * This is a simple state machine:
777 * BLINK_NONE not blinking at all
778 * BLINK_OFF blinking, cursor is not shown
779 * BLINK_ON blinking, cursor is shown
780 */
781
782#define BLINK_NONE 0
783#define BLINK_OFF 1
784#define BLINK_ON 2
785
786static int blink_state = BLINK_NONE;
787static long_u blink_waittime = 700;
788static long_u blink_ontime = 400;
789static long_u blink_offtime = 250;
790static guint blink_timer = 0;
791
Bram Moolenaar98921892016-02-23 17:14:37 +0100792#if GTK_CHECK_VERSION(3,0,0)
793 static gboolean
794gui_gtk_is_blink_on(void)
795{
796 return blink_state == BLINK_ON;
797}
Bram Moolenaar98921892016-02-23 17:14:37 +0100798#endif
799
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 void
801gui_mch_set_blinking(long waittime, long on, long off)
802{
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100803#if GTK_CHECK_VERSION(3,0,0)
804 if (waittime == 0 || on == 0 || off == 0)
805 {
806 blink_mode = FALSE;
807
808 blink_waittime = 700;
809 blink_ontime = 400;
810 blink_offtime = 250;
811 }
812 else
813 {
814 blink_mode = TRUE;
815
816 blink_waittime = waittime;
817 blink_ontime = on;
818 blink_offtime = off;
819 }
820#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 blink_waittime = waittime;
822 blink_ontime = on;
823 blink_offtime = off;
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825}
826
827/*
828 * Stop the cursor blinking. Show the cursor if it wasn't shown.
829 */
830 void
831gui_mch_stop_blink(void)
832{
833 if (blink_timer)
834 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100835#if GTK_CHECK_VERSION(3,0,0)
836 g_source_remove(blink_timer);
837#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 gtk_timeout_remove(blink_timer);
Bram Moolenaar98921892016-02-23 17:14:37 +0100839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 blink_timer = 0;
841 }
842 if (blink_state == BLINK_OFF)
843 gui_update_cursor(TRUE, FALSE);
844 blink_state = BLINK_NONE;
845}
846
Bram Moolenaar98921892016-02-23 17:14:37 +0100847#if GTK_CHECK_VERSION(3,0,0)
848 static gboolean
849#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +0100851#endif
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000852blink_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853{
854 if (blink_state == BLINK_ON)
855 {
856 gui_undraw_cursor();
857 blink_state = BLINK_OFF;
Bram Moolenaar98921892016-02-23 17:14:37 +0100858#if GTK_CHECK_VERSION(3,0,0)
859 blink_timer = g_timeout_add((guint)blink_offtime,
860 (GSourceFunc) blink_cb, NULL);
861#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 blink_timer = gtk_timeout_add((guint32)blink_offtime,
863 (GtkFunction) blink_cb, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +0100864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 }
866 else
867 {
868 gui_update_cursor(TRUE, FALSE);
869 blink_state = BLINK_ON;
Bram Moolenaar98921892016-02-23 17:14:37 +0100870#if GTK_CHECK_VERSION(3,0,0)
871 blink_timer = g_timeout_add((guint)blink_ontime,
872 (GSourceFunc) blink_cb, NULL);
873#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 blink_timer = gtk_timeout_add((guint32)blink_ontime,
875 (GtkFunction) blink_cb, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +0100876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 }
878
879 return FALSE; /* don't happen again */
880}
881
882/*
883 * Start the cursor blinking. If it was already blinking, this restarts the
884 * waiting time and shows the cursor.
885 */
886 void
887gui_mch_start_blink(void)
888{
889 if (blink_timer)
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +0200890 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100891#if GTK_CHECK_VERSION(3,0,0)
892 g_source_remove(blink_timer);
893#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 gtk_timeout_remove(blink_timer);
Bram Moolenaar98921892016-02-23 17:14:37 +0100895#endif
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +0200896 blink_timer = 0;
897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 /* Only switch blinking on if none of the times is zero */
899 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
900 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100901#if GTK_CHECK_VERSION(3,0,0)
902 blink_timer = g_timeout_add((guint)blink_waittime,
903 (GSourceFunc) blink_cb, NULL);
904#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 blink_timer = gtk_timeout_add((guint32)blink_waittime,
906 (GtkFunction) blink_cb, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +0100907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 blink_state = BLINK_ON;
909 gui_update_cursor(TRUE, FALSE);
910 }
911}
912
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000914enter_notify_event(GtkWidget *widget UNUSED,
915 GdkEventCrossing *event UNUSED,
916 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917{
918 if (blink_state == BLINK_NONE)
919 gui_mch_start_blink();
920
921 /* make sure keyboard input goes there */
Bram Moolenaar98921892016-02-23 17:14:37 +0100922#if GTK_CHECK_VERSION(3,0,0)
923 if (gtk_socket_id == 0 || !gtk_widget_has_focus(gui.drawarea))
924#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 if (gtk_socket_id == 0 || !GTK_WIDGET_HAS_FOCUS(gui.drawarea))
Bram Moolenaar98921892016-02-23 17:14:37 +0100926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 gtk_widget_grab_focus(gui.drawarea);
928
929 return FALSE;
930}
931
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000933leave_notify_event(GtkWidget *widget UNUSED,
934 GdkEventCrossing *event UNUSED,
935 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936{
937 if (blink_state != BLINK_NONE)
938 gui_mch_stop_blink();
939
940 return FALSE;
941}
942
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000944focus_in_event(GtkWidget *widget,
945 GdkEventFocus *event UNUSED,
946 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947{
948 gui_focus_change(TRUE);
949
950 if (blink_state == BLINK_NONE)
951 gui_mch_start_blink();
952
Bram Moolenaar9c8791f2007-09-05 19:47:23 +0000953 /* make sure keyboard input goes to the draw area (if this is focus for a
954 * window) */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000955 if (widget != gui.drawarea)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000956 gtk_widget_grab_focus(gui.drawarea);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957
958 return TRUE;
959}
960
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000962focus_out_event(GtkWidget *widget UNUSED,
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200963 GdkEventFocus *event UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000964 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965{
966 gui_focus_change(FALSE);
967
968 if (blink_state != BLINK_NONE)
969 gui_mch_stop_blink();
970
971 return TRUE;
972}
973
974
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975/*
976 * Translate a GDK key value to UTF-8 independently of the current locale.
977 * The output is written to string, which must have room for at least 6 bytes
978 * plus the NUL terminator. Returns the length in bytes.
979 *
980 * This function is used in the GTK+ 2 GUI only. The GTK+ 1 code makes use
981 * of GdkEventKey::string instead. But event->string is evil; see here why:
982 * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey
983 */
984 static int
985keyval_to_string(unsigned int keyval, unsigned int state, char_u *string)
986{
987 int len;
988 guint32 uc;
989
990 uc = gdk_keyval_to_unicode(keyval);
991 if (uc != 0)
992 {
993 /* Check for CTRL-foo */
994 if ((state & GDK_CONTROL_MASK) && uc >= 0x20 && uc < 0x80)
995 {
996 /* These mappings look arbitrary at the first glance, but in fact
997 * resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
998 * machine. The only difference is BS vs. DEL for CTRL-8 (makes
999 * more sense and is consistent with usual terminal behaviour). */
1000 if (uc >= '@')
1001 string[0] = uc & 0x1F;
1002 else if (uc == '2')
1003 string[0] = NUL;
1004 else if (uc >= '3' && uc <= '7')
1005 string[0] = uc ^ 0x28;
1006 else if (uc == '8')
1007 string[0] = BS;
1008 else if (uc == '?')
1009 string[0] = DEL;
1010 else
1011 string[0] = uc;
1012 len = 1;
1013 }
1014 else
1015 {
1016 /* Translate a normal key to UTF-8. This doesn't work for dead
1017 * keys of course, you _have_ to use an input method for that. */
1018 len = utf_char2bytes((int)uc, string);
1019 }
1020 }
1021 else
1022 {
1023 /* Translate keys which are represented by ASCII control codes in Vim.
1024 * There are only a few of those; most control keys are translated to
1025 * special terminal-like control sequences. */
1026 len = 1;
1027 switch (keyval)
1028 {
1029 case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab:
1030 string[0] = TAB;
1031 break;
1032 case GDK_Linefeed:
1033 string[0] = NL;
1034 break;
1035 case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter:
1036 string[0] = CAR;
1037 break;
1038 case GDK_Escape:
1039 string[0] = ESC;
1040 break;
1041 default:
1042 len = 0;
1043 break;
1044 }
1045 }
1046 string[len] = NUL;
1047
1048 return len;
1049}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001051 static int
1052modifiers_gdk2vim(guint state)
1053{
1054 int modifiers = 0;
1055
1056 if (state & GDK_SHIFT_MASK)
1057 modifiers |= MOD_MASK_SHIFT;
1058 if (state & GDK_CONTROL_MASK)
1059 modifiers |= MOD_MASK_CTRL;
1060 if (state & GDK_MOD1_MASK)
1061 modifiers |= MOD_MASK_ALT;
Bram Moolenaar580061a2010-08-13 13:57:13 +02001062#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001063 if (state & GDK_SUPER_MASK)
1064 modifiers |= MOD_MASK_META;
1065#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001066 if (state & GDK_MOD4_MASK)
1067 modifiers |= MOD_MASK_META;
1068
1069 return modifiers;
1070}
1071
1072 static int
1073modifiers_gdk2mouse(guint state)
1074{
1075 int modifiers = 0;
1076
1077 if (state & GDK_SHIFT_MASK)
1078 modifiers |= MOUSE_SHIFT;
1079 if (state & GDK_CONTROL_MASK)
1080 modifiers |= MOUSE_CTRL;
1081 if (state & GDK_MOD1_MASK)
1082 modifiers |= MOUSE_ALT;
1083
1084 return modifiers;
1085}
1086
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087/*
1088 * Main keyboard handler:
1089 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001091key_press_event(GtkWidget *widget UNUSED,
1092 GdkEventKey *event,
1093 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001095 /* For GTK+ 2 we know for sure how large the string might get.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 * (That is, up to 6 bytes + NUL + CSI escapes + safety measure.) */
1097 char_u string[32], string2[32];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 guint key_sym;
1099 int len;
1100 int i;
1101 int modifiers;
1102 int key;
1103 guint state;
1104 char_u *s, *d;
1105
Bram Moolenaar98921892016-02-23 17:14:37 +01001106#if GTK_CHECK_VERSION(3,0,0)
1107 is_key_pressed = TRUE;
1108 gui_mch_stop_blink();
1109#endif
1110
Bram Moolenaar20892c12011-06-26 04:49:00 +02001111 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 key_sym = event->keyval;
1113 state = event->state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114
1115#ifdef FEAT_XIM
1116 if (xim_queue_key_press_event(event, TRUE))
1117 return TRUE;
1118#endif
1119
1120#ifdef FEAT_HANGULIN
1121 if (key_sym == GDK_space && (state & GDK_SHIFT_MASK))
1122 {
1123 hangul_input_state_toggle();
1124 return TRUE;
1125 }
1126#endif
1127
1128#ifdef SunXK_F36
1129 /*
1130 * These keys have bogus lookup strings, and trapping them here is
1131 * easier than trying to XRebindKeysym() on them with every possible
1132 * combination of modifiers.
1133 */
1134 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
1135 len = 0;
1136 else
1137#endif
1138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 len = keyval_to_string(key_sym, state, string2);
1140
1141 /* Careful: convert_input() doesn't handle the NUL character.
1142 * No need to convert pure ASCII anyway, thus the len > 1 check. */
1143 if (len > 1 && input_conv.vc_type != CONV_NONE)
1144 len = convert_input(string2, len, sizeof(string2));
1145
1146 s = string2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 d = string;
1148 for (i = 0; i < len; ++i)
1149 {
1150 *d++ = s[i];
1151 if (d[-1] == CSI && d + 2 < string + sizeof(string))
1152 {
1153 /* Turn CSI into K_CSI. */
1154 *d++ = KS_EXTRA;
1155 *d++ = (int)KE_CSI;
1156 }
1157 }
1158 len = d - string;
1159 }
1160
1161 /* Shift-Tab results in Left_Tab, but we want <S-Tab> */
1162 if (key_sym == GDK_ISO_Left_Tab)
1163 {
1164 key_sym = GDK_Tab;
1165 state |= GDK_SHIFT_MASK;
1166 }
1167
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168#ifdef FEAT_MENU
1169 /* If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key
1170 * is a menu shortcut, we ignore everything with the ALT modifier. */
1171 if ((state & GDK_MOD1_MASK)
1172 && gui.menu_is_active
1173 && (*p_wak == 'y'
1174 || (*p_wak == 'm'
1175 && len == 1
1176 && gui_is_menu_shortcut(string[0]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 /* For GTK2 we return false to signify that we haven't handled the
1178 * keypress, so that gtk will handle the mnemonic or accelerator. */
1179 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180#endif
1181
1182 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
1183 * that already has the 8th bit set.
1184 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
1185 * Don't do this for double-byte encodings, it turns the char into a lead
1186 * byte. */
1187 if (len == 1
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001188 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001189#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001190 || (state & GDK_SUPER_MASK)
1191#endif
1192 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 && !(key_sym == GDK_BackSpace || key_sym == GDK_Delete)
1194 && (string[0] & 0x80) == 0
1195 && !(key_sym == GDK_Tab && (state & GDK_SHIFT_MASK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 )
1198 {
1199 string[0] |= 0x80;
1200 state &= ~GDK_MOD1_MASK; /* don't use it again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 if (enc_utf8) /* convert to utf-8 */
1202 {
1203 string[1] = string[0] & 0xbf;
1204 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
1205 if (string[1] == CSI)
1206 {
1207 string[2] = KS_EXTRA;
1208 string[3] = (int)KE_CSI;
1209 len = 4;
1210 }
1211 else
1212 len = 2;
1213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 }
1215
1216 /* Check for special keys. Also do this when len == 1 (key has an ASCII
1217 * value) to detect backspace, delete and keypad keys. */
1218 if (len == 0 || len == 1)
1219 {
1220 for (i = 0; special_keys[i].key_sym != 0; i++)
1221 {
1222 if (special_keys[i].key_sym == key_sym)
1223 {
1224 string[0] = CSI;
1225 string[1] = special_keys[i].code0;
1226 string[2] = special_keys[i].code1;
1227 len = -3;
1228 break;
1229 }
1230 }
1231 }
1232
1233 if (len == 0) /* Unrecognized key */
1234 return TRUE;
1235
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 /* Special keys (and a few others) may have modifiers. Also when using a
1237 * double-byte encoding (can't set the 8th bit). */
1238 if (len == -3 || key_sym == GDK_space || key_sym == GDK_Tab
1239 || key_sym == GDK_Return || key_sym == GDK_Linefeed
1240 || key_sym == GDK_Escape || key_sym == GDK_KP_Tab
1241 || key_sym == GDK_ISO_Enter || key_sym == GDK_3270_Enter
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001242 || (enc_dbcs && len == 1 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001243#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001244 || (state & GDK_SUPER_MASK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245#endif
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001246 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001248 modifiers = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249
1250 /*
1251 * For some keys a shift modifier is translated into another key
1252 * code.
1253 */
1254 if (len == -3)
1255 key = TO_SPECIAL(string[1], string[2]);
1256 else
1257 key = string[0];
1258
1259 key = simplify_key(key, &modifiers);
1260 if (key == CSI)
1261 key = K_CSI;
1262 if (IS_SPECIAL(key))
1263 {
1264 string[0] = CSI;
1265 string[1] = K_SECOND(key);
1266 string[2] = K_THIRD(key);
1267 len = 3;
1268 }
1269 else
1270 {
1271 string[0] = key;
1272 len = 1;
1273 }
1274
1275 if (modifiers != 0)
1276 {
1277 string2[0] = CSI;
1278 string2[1] = KS_MODIFIER;
1279 string2[2] = modifiers;
1280 add_to_input_buf(string2, 3);
1281 }
1282 }
1283
1284 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1285 || (string[0] == intr_char && intr_char != Ctrl_C)))
1286 {
1287 trash_input_buf();
1288 got_int = TRUE;
1289 }
1290
1291 add_to_input_buf(string, len);
1292
1293 /* blank out the pointer if necessary */
1294 if (p_mh)
1295 gui_mch_mousehide(TRUE);
1296
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 return TRUE;
1298}
1299
Bram Moolenaar98921892016-02-23 17:14:37 +01001300#if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001302key_release_event(GtkWidget *widget UNUSED,
1303 GdkEventKey *event,
1304 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305{
Bram Moolenaar98921892016-02-23 17:14:37 +01001306# if GTK_CHECK_VERSION(3,0,0)
1307 is_key_pressed = FALSE;
1308 gui_mch_start_blink();
1309# endif
1310# if defined(FEAT_XIM)
Bram Moolenaar20892c12011-06-26 04:49:00 +02001311 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 /*
1313 * GTK+ 2 input methods may do fancy stuff on key release events too.
1314 * With the default IM for instance, you can enter any UCS code point
1315 * by holding down CTRL-SHIFT and typing hexadecimal digits.
1316 */
1317 return xim_queue_key_press_event(event, FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001318# else
1319 return TRUE;
1320# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321}
1322#endif
1323
1324
1325/****************************************************************************
1326 * Selection handlers:
1327 */
1328
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001330selection_clear_event(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 GdkEventSelection *event,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001332 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333{
1334 if (event->selection == clip_plus.gtk_sel_atom)
1335 clip_lose_selection(&clip_plus);
1336 else
1337 clip_lose_selection(&clip_star);
1338
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 return TRUE;
1340}
1341
1342#define RS_NONE 0 /* selection_received_cb() not called yet */
1343#define RS_OK 1 /* selection_received_cb() called and OK */
1344#define RS_FAIL 2 /* selection_received_cb() called and failed */
1345static int received_selection = RS_NONE;
1346
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001348selection_received_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 GtkSelectionData *data,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001350 guint time_ UNUSED,
1351 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352{
1353 VimClipboard *cbd;
1354 char_u *text;
1355 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 guchar *tmpbuf_utf8 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 int len;
Bram Moolenaard44347f2011-06-19 01:14:29 +02001358 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359
Bram Moolenaar98921892016-02-23 17:14:37 +01001360#if GTK_CHECK_VERSION(3,0,0)
1361 if (gtk_selection_data_get_selection(data) == clip_plus.gtk_sel_atom)
1362#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 if (data->selection == clip_plus.gtk_sel_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 cbd = &clip_plus;
1366 else
1367 cbd = &clip_star;
1368
Bram Moolenaar98921892016-02-23 17:14:37 +01001369#if GTK_CHECK_VERSION(3,0,0)
1370 text = (char_u *)gtk_selection_data_get_data(data);
1371 len = gtk_selection_data_get_length(data);
1372#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 text = (char_u *)data->data;
1374 len = data->length;
Bram Moolenaar98921892016-02-23 17:14:37 +01001375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376
1377 if (text == NULL || len <= 0)
1378 {
1379 received_selection = RS_FAIL;
1380 /* clip_free_selection(cbd); ??? */
1381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 return;
1383 }
1384
Bram Moolenaar98921892016-02-23 17:14:37 +01001385#if GTK_CHECK_VERSION(3,0,0)
1386 if (gtk_selection_data_get_data_type(data) == vim_atom)
1387#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 if (data->type == vim_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 {
1391 motion_type = *text++;
1392 --len;
1393 }
Bram Moolenaar98921892016-02-23 17:14:37 +01001394#if GTK_CHECK_VERSION(3,0,0)
1395 else if (gtk_selection_data_get_data_type(data) == vimenc_atom)
1396#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 else if (data->type == vimenc_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 {
1400 char_u *enc;
1401 vimconv_T conv;
1402
1403 motion_type = *text++;
1404 --len;
1405
1406 enc = text;
1407 text += STRLEN(text) + 1;
1408 len -= text - enc;
1409
1410 /* If the encoding of the text is different from 'encoding', attempt
1411 * converting it. */
1412 conv.vc_type = CONV_NONE;
1413 convert_setup(&conv, enc, p_enc);
1414 if (conv.vc_type != CONV_NONE)
1415 {
1416 tmpbuf = string_convert(&conv, text, &len);
1417 if (tmpbuf != NULL)
1418 text = tmpbuf;
1419 convert_setup(&conv, NULL, NULL);
1420 }
1421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 /* gtk_selection_data_get_text() handles all the nasty details
1424 * and targets and encodings etc. This rocks so hard. */
1425 else
1426 {
1427 tmpbuf_utf8 = gtk_selection_data_get_text(data);
1428 if (tmpbuf_utf8 != NULL)
1429 {
1430 len = STRLEN(tmpbuf_utf8);
1431 if (input_conv.vc_type != CONV_NONE)
1432 {
1433 tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len);
1434 if (tmpbuf != NULL)
1435 text = tmpbuf;
1436 }
1437 else
1438 text = tmpbuf_utf8;
1439 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001440 else if (len >= 2 && text[0] == 0xff && text[1] == 0xfe)
1441 {
1442 vimconv_T conv;
1443
1444 /* UTF-16, we get this for HTML */
1445 conv.vc_type = CONV_NONE;
1446 convert_setup_ext(&conv, (char_u *)"utf-16le", FALSE, p_enc, TRUE);
1447
1448 if (conv.vc_type != CONV_NONE)
1449 {
1450 text += 2;
1451 len -= 2;
1452 tmpbuf = string_convert(&conv, text, &len);
1453 convert_setup(&conv, NULL, NULL);
1454 }
1455 if (tmpbuf != NULL)
1456 text = tmpbuf;
1457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001460 /* Chop off any trailing NUL bytes. OpenOffice sends these. */
Bram Moolenaara76638f2010-06-05 12:49:46 +02001461 while (len > 0 && text[len - 1] == NUL)
1462 --len;
1463
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 clip_yank_selection(motion_type, text, (long)len, cbd);
1465 received_selection = RS_OK;
1466 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 g_free(tmpbuf_utf8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468}
1469
1470/*
1471 * Prepare our selection data for passing it to the external selection
1472 * client.
1473 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001475selection_get_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 GtkSelectionData *selection_data,
1477 guint info,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001478 guint time_ UNUSED,
1479 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480{
1481 char_u *string;
1482 char_u *tmpbuf;
1483 long_u tmplen;
1484 int length;
1485 int motion_type;
1486 GdkAtom type;
1487 VimClipboard *cbd;
1488
Bram Moolenaar98921892016-02-23 17:14:37 +01001489#if GTK_CHECK_VERSION(3,0,0)
1490 if (gtk_selection_data_get_selection(selection_data)
1491 == clip_plus.gtk_sel_atom)
1492#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 if (selection_data->selection == clip_plus.gtk_sel_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 cbd = &clip_plus;
1496 else
1497 cbd = &clip_star;
1498
1499 if (!cbd->owned)
1500 return; /* Shouldn't ever happen */
1501
1502 if (info != (guint)TARGET_STRING
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001503 && (!clip_html || info != (guint)TARGET_HTML)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 && info != (guint)TARGET_UTF8_STRING
1505 && info != (guint)TARGET_VIMENC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 && info != (guint)TARGET_VIM
1507 && info != (guint)TARGET_COMPOUND_TEXT
1508 && info != (guint)TARGET_TEXT)
1509 return;
1510
1511 /* get the selection from the '*'/'+' register */
1512 clip_get_selection(cbd);
1513
1514 motion_type = clip_convert_selection(&string, &tmplen, cbd);
1515 if (motion_type < 0 || string == NULL)
1516 return;
1517 /* Due to int arguments we can't handle more than G_MAXINT. Also
1518 * reserve one extra byte for NUL or the motion type; just in case.
1519 * (Not that pasting 2G of text is ever going to work, but... ;-) */
1520 length = MIN(tmplen, (long_u)(G_MAXINT - 1));
1521
1522 if (info == (guint)TARGET_VIM)
1523 {
1524 tmpbuf = alloc((unsigned)length + 1);
1525 if (tmpbuf != NULL)
1526 {
1527 tmpbuf[0] = motion_type;
1528 mch_memmove(tmpbuf + 1, string, (size_t)length);
1529 }
1530 /* For our own format, the first byte contains the motion type */
1531 ++length;
1532 vim_free(string);
1533 string = tmpbuf;
1534 type = vim_atom;
1535 }
1536
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001537 else if (info == (guint)TARGET_HTML)
1538 {
1539 vimconv_T conv;
1540
1541 /* Since we get utf-16, we probably should set it as well. */
1542 conv.vc_type = CONV_NONE;
1543 convert_setup_ext(&conv, p_enc, TRUE, (char_u *)"utf-16le", FALSE);
1544 if (conv.vc_type != CONV_NONE)
1545 {
1546 tmpbuf = string_convert(&conv, string, &length);
1547 convert_setup(&conv, NULL, NULL);
1548 vim_free(string);
1549 string = tmpbuf;
1550 }
1551
1552 /* Prepend the BOM: "fffe" */
1553 if (string != NULL)
1554 {
1555 tmpbuf = alloc(length + 2);
1556 tmpbuf[0] = 0xff;
1557 tmpbuf[1] = 0xfe;
1558 mch_memmove(tmpbuf + 2, string, (size_t)length);
1559 vim_free(string);
1560 string = tmpbuf;
1561 length += 2;
1562
Bram Moolenaar98921892016-02-23 17:14:37 +01001563#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01001564 /* Looks redundant even for GTK2 because these values are
Bram Moolenaar98921892016-02-23 17:14:37 +01001565 * overwritten by gtk_selection_data_set() that follows. */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001566 selection_data->type = selection_data->target;
1567 selection_data->format = 16; /* 16 bits per char */
Bram Moolenaar98921892016-02-23 17:14:37 +01001568#endif
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001569 gtk_selection_data_set(selection_data, html_atom, 16,
1570 string, length);
1571 vim_free(string);
1572 }
1573 return;
1574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 else if (info == (guint)TARGET_VIMENC)
1576 {
1577 int l = STRLEN(p_enc);
1578
1579 /* contents: motion_type 'encoding' NUL text */
1580 tmpbuf = alloc((unsigned)length + l + 2);
1581 if (tmpbuf != NULL)
1582 {
1583 tmpbuf[0] = motion_type;
1584 STRCPY(tmpbuf + 1, p_enc);
1585 mch_memmove(tmpbuf + l + 2, string, (size_t)length);
1586 }
1587 length += l + 2;
1588 vim_free(string);
1589 string = tmpbuf;
1590 type = vimenc_atom;
1591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 /* gtk_selection_data_set_text() handles everything for us. This is
1594 * so easy and simple and cool, it'd be insane not to use it. */
1595 else
1596 {
1597 if (output_conv.vc_type != CONV_NONE)
1598 {
1599 tmpbuf = string_convert(&output_conv, string, &length);
1600 vim_free(string);
1601 if (tmpbuf == NULL)
1602 return;
1603 string = tmpbuf;
1604 }
1605 /* Validate the string to avoid runtime warnings */
1606 if (g_utf8_validate((const char *)string, (gssize)length, NULL))
1607 {
1608 gtk_selection_data_set_text(selection_data,
1609 (const char *)string, length);
1610 }
1611 vim_free(string);
1612 return;
1613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614
1615 if (string != NULL)
1616 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001617#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01001618 /* Looks redundant even for GTK2 because these values are
Bram Moolenaar98921892016-02-23 17:14:37 +01001619 * overwritten by gtk_selection_data_set() that follows. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 selection_data->type = selection_data->target;
1621 selection_data->format = 8; /* 8 bits per char */
Bram Moolenaar98921892016-02-23 17:14:37 +01001622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 gtk_selection_data_set(selection_data, type, 8, string, length);
1624 vim_free(string);
1625 }
1626}
1627
1628/*
Bram Moolenaar29695702012-05-18 17:03:18 +02001629 * Check if the GUI can be started. Called before gvimrc is sourced and
1630 * before fork().
1631 * Return OK or FAIL.
1632 */
1633 int
1634gui_mch_early_init_check(void)
1635{
1636 char_u *p;
1637
1638 /* Guess that when $DISPLAY isn't set the GUI can't start. */
1639 p = mch_getenv((char_u *)"DISPLAY");
1640 if (p == NULL || *p == NUL)
1641 {
1642 gui.dying = TRUE;
1643 EMSG(_((char *)e_opendisp));
1644 return FAIL;
1645 }
1646 return OK;
1647}
1648
1649/*
1650 * Check if the GUI can be started. Called before gvimrc is sourced but after
1651 * fork().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 * Return OK or FAIL.
1653 */
1654 int
1655gui_mch_init_check(void)
1656{
Bram Moolenaar3a466a82016-01-19 17:47:25 +01001657#ifdef USE_GRESOURCE
1658 static int res_registered = FALSE;
1659
1660 if (!res_registered)
1661 {
1662 /* Call this function in the GUI process; otherwise, the resources
1663 * won't be available. Don't call it twice. */
1664 res_registered = TRUE;
1665 gui_gtk_register_resource();
1666 }
1667#endif
1668
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669#ifdef FEAT_GUI_GNOME
1670 if (gtk_socket_id == 0)
1671 using_gnome = 1;
1672#endif
1673
Bram Moolenaar8dd79012013-05-21 12:52:04 +02001674 /* This defaults to argv[0], but we want it to match the name of the
1675 * shipped gvim.desktop so that Vim's windows can be associated with this
1676 * file. */
1677 g_set_prgname("gvim");
1678
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 /* Don't use gtk_init() or gnome_init(), it exits on failure. */
1680 if (!gtk_init_check(&gui_argc, &gui_argv))
1681 {
1682 gui.dying = TRUE;
Bram Moolenaarc93e7912008-07-08 10:46:08 +00001683 EMSG(_((char *)e_opendisp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 return FAIL;
1685 }
1686
1687 return OK;
1688}
1689
1690
1691/****************************************************************************
1692 * Mouse handling callbacks
1693 */
1694
1695
1696static guint mouse_click_timer = 0;
1697static int mouse_timed_out = TRUE;
1698
1699/*
1700 * Timer used to recognize multiple clicks of the mouse button
1701 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001702#if GTK_CHECK_VERSION(3,0,0)
1703 static gboolean
1704#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +01001706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707mouse_click_timer_cb(gpointer data)
1708{
1709 /* we don't use this information currently */
1710 int *timed_out = (int *) data;
1711
1712 *timed_out = TRUE;
1713 return FALSE; /* don't happen again */
1714}
1715
1716static guint motion_repeat_timer = 0;
1717static int motion_repeat_offset = FALSE;
Bram Moolenaar98921892016-02-23 17:14:37 +01001718#ifdef GTK_DEST_DEFAULT_ALL
1719static gboolean motion_repeat_timer_cb(gpointer);
1720#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721static gint motion_repeat_timer_cb(gpointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01001722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723
1724 static void
1725process_motion_notify(int x, int y, GdkModifierType state)
1726{
1727 int button;
1728 int_u vim_modifiers;
Bram Moolenaar98921892016-02-23 17:14:37 +01001729#if GTK_CHECK_VERSION(3,0,0)
1730 GtkAllocation allocation;
1731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
1733 button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1734 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1735 GDK_BUTTON5_MASK))
1736 ? MOUSE_DRAG : ' ';
1737
1738 /* If our pointer is currently hidden, then we should show it. */
1739 gui_mch_mousehide(FALSE);
1740
1741 /* Just moving the rodent above the drawing area without any button
1742 * being pressed. */
1743 if (button != MOUSE_DRAG)
1744 {
1745 gui_mouse_moved(x, y);
1746 return;
1747 }
1748
1749 /* translate modifier coding between the main engine and GTK */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001750 vim_modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
Bram Moolenaar49325942007-05-10 19:19:59 +00001752 /* inform the editor engine about the occurrence of this event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 gui_send_mouse_event(button, x, y, FALSE, vim_modifiers);
1754
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 /*
1756 * Auto repeat timer handling.
1757 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001758#if GTK_CHECK_VERSION(3,0,0)
1759 gtk_widget_get_allocation(gui.drawarea, &allocation);
1760
1761 if (x < 0 || y < 0
1762 || x >= allocation.width
1763 || y >= allocation.height)
1764#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 if (x < 0 || y < 0
1766 || x >= gui.drawarea->allocation.width
1767 || y >= gui.drawarea->allocation.height)
Bram Moolenaar98921892016-02-23 17:14:37 +01001768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 {
1770
1771 int dx;
1772 int dy;
1773 int offshoot;
1774 int delay = 10;
1775
1776 /* Calculate the maximal distance of the cursor from the drawing area.
1777 * (offshoot can't become negative here!).
1778 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001779#if GTK_CHECK_VERSION(3,0,0)
1780 dx = x < 0 ? -x : x - allocation.width;
1781 dy = y < 0 ? -y : y - allocation.height;
1782#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 dx = x < 0 ? -x : x - gui.drawarea->allocation.width;
1784 dy = y < 0 ? -y : y - gui.drawarea->allocation.height;
Bram Moolenaar98921892016-02-23 17:14:37 +01001785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
1787 offshoot = dx > dy ? dx : dy;
1788
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001789 /* Make a linearly decaying timer delay with a threshold of 5 at a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 * distance of 127 pixels from the main window.
1791 *
1792 * One could think endlessly about the most ergonomic variant here.
1793 * For example it could make sense to calculate the distance from the
1794 * drags start instead...
1795 *
1796 * Maybe a parabolic interpolation would suite us better here too...
1797 */
1798 if (offshoot > 127)
1799 {
1800 /* 5 appears to be somehow near to my perceptual limits :-). */
1801 delay = 5;
1802 }
1803 else
1804 {
1805 delay = (130 * (127 - offshoot)) / 127 + 5;
1806 }
1807
1808 /* shoot again */
1809 if (!motion_repeat_timer)
Bram Moolenaar98921892016-02-23 17:14:37 +01001810#if GTK_CHECK_VERSION(3,0,0)
1811 motion_repeat_timer = g_timeout_add((guint)delay,
1812 motion_repeat_timer_cb, NULL);
1813#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 motion_repeat_timer = gtk_timeout_add((guint32)delay,
1815 motion_repeat_timer_cb, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01001816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 }
1818}
1819
Bram Moolenaar98921892016-02-23 17:14:37 +01001820#if GTK_CHECK_VERSION(3,0,0)
1821 static GdkDevice *
1822gui_gtk_get_pointer_device(GtkWidget *widget)
1823{
1824 GdkWindow * const win = gtk_widget_get_window(widget);
1825 GdkDisplay * const dpy = gdk_window_get_display(win);
1826 GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy);
1827 return gdk_device_manager_get_client_pointer(mngr);
1828}
1829
1830 static GdkWindow *
1831gui_gtk_get_pointer(GtkWidget *widget,
1832 gint *x,
1833 gint *y,
1834 GdkModifierType *state)
1835{
1836 GdkWindow * const win = gtk_widget_get_window(widget);
1837 GdkDevice * const dev = gui_gtk_get_pointer_device(widget);
1838 return gdk_window_get_device_position(win, dev , x, y, state);
1839}
1840
Bram Moolenaar2369c152016-03-04 23:08:25 +01001841# if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar98921892016-02-23 17:14:37 +01001842 static GdkWindow *
1843gui_gtk_window_at_position(GtkWidget *widget,
1844 gint *x,
1845 gint *y)
1846{
1847 GdkDevice * const dev = gui_gtk_get_pointer_device(widget);
1848 return gdk_device_get_window_at_position(dev, x, y);
1849}
Bram Moolenaar2369c152016-03-04 23:08:25 +01001850# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01001851#endif
1852
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853/*
1854 * Timer used to recognize multiple clicks of the mouse button.
1855 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001856#if GTK_CHECK_VERSION(3,0,0)
1857 static gboolean
1858#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +01001860#endif
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001861motion_repeat_timer_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862{
1863 int x;
1864 int y;
1865 GdkModifierType state;
1866
Bram Moolenaar98921892016-02-23 17:14:37 +01001867#if GTK_CHECK_VERSION(3,0,0)
1868 gui_gtk_get_pointer(gui.drawarea, &x, &y, &state);
1869#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 gdk_window_get_pointer(gui.drawarea->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +01001871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872
1873 if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1874 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1875 GDK_BUTTON5_MASK)))
1876 {
1877 motion_repeat_timer = 0;
1878 return FALSE;
1879 }
1880
1881 /* If there already is a mouse click in the input buffer, wait another
1882 * time (otherwise we would create a backlog of clicks) */
1883 if (vim_used_in_input_buf() > 10)
1884 return TRUE;
1885
1886 motion_repeat_timer = 0;
1887
1888 /*
1889 * Fake a motion event.
1890 * Trick: Pretend the mouse moved to the next character on every other
1891 * event, otherwise drag events will be discarded, because they are still
1892 * in the same character.
1893 */
1894 if (motion_repeat_offset)
1895 x += gui.char_width;
1896
1897 motion_repeat_offset = !motion_repeat_offset;
1898 process_motion_notify(x, y, state);
1899
1900 /* Don't happen again. We will get reinstalled in the synthetic event
1901 * if needed -- thus repeating should still work. */
1902 return FALSE;
1903}
1904
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001906motion_notify_event(GtkWidget *widget,
1907 GdkEventMotion *event,
1908 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909{
1910 if (event->is_hint)
1911 {
1912 int x;
1913 int y;
1914 GdkModifierType state;
1915
Bram Moolenaar98921892016-02-23 17:14:37 +01001916#if GTK_CHECK_VERSION(3,0,0)
1917 gui_gtk_get_pointer(widget, &x, &y, &state);
1918#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 gdk_window_get_pointer(widget->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +01001920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 process_motion_notify(x, y, state);
1922 }
1923 else
1924 {
1925 process_motion_notify((int)event->x, (int)event->y,
1926 (GdkModifierType)event->state);
1927 }
1928
1929 return TRUE; /* handled */
1930}
1931
1932
1933/*
1934 * Mouse button handling. Note please that we are capturing multiple click's
1935 * by our own timeout mechanism instead of the one provided by GTK+ itself.
1936 * This is due to the way the generic VIM code is recognizing multiple clicks.
1937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001939button_press_event(GtkWidget *widget,
1940 GdkEventButton *event,
1941 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942{
1943 int button;
1944 int repeated_click = FALSE;
1945 int x, y;
1946 int_u vim_modifiers;
1947
Bram Moolenaar20892c12011-06-26 04:49:00 +02001948 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001949
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 /* Make sure we have focus now we've been selected */
Bram Moolenaar98921892016-02-23 17:14:37 +01001951#if GTK_CHECK_VERSION(3,0,0)
1952 if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget))
1953#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01001955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 gtk_widget_grab_focus(widget);
1957
1958 /*
1959 * Don't let additional events about multiple clicks send by GTK to us
1960 * after the initial button press event confuse us.
1961 */
1962 if (event->type != GDK_BUTTON_PRESS)
1963 return FALSE;
1964
1965 x = event->x;
1966 y = event->y;
1967
1968 /* Handle multiple clicks */
1969 if (!mouse_timed_out && mouse_click_timer)
1970 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001971#if GTK_CHECK_VERSION(3,0,0)
1972 g_source_remove(mouse_click_timer);
1973#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 gtk_timeout_remove(mouse_click_timer);
Bram Moolenaar98921892016-02-23 17:14:37 +01001975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 mouse_click_timer = 0;
1977 repeated_click = TRUE;
1978 }
1979
1980 mouse_timed_out = FALSE;
Bram Moolenaar98921892016-02-23 17:14:37 +01001981#if GTK_CHECK_VERSION(3,0,0)
1982 mouse_click_timer = g_timeout_add((guint)p_mouset,
1983 mouse_click_timer_cb, &mouse_timed_out);
1984#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 mouse_click_timer = gtk_timeout_add((guint32)p_mouset,
1986 mouse_click_timer_cb, &mouse_timed_out);
Bram Moolenaar98921892016-02-23 17:14:37 +01001987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988
1989 switch (event->button)
1990 {
Bram Moolenaar88e484b2015-11-24 15:38:44 +01001991 /* Keep in sync with gui_x11.c.
1992 * Buttons 4-7 are handled in scroll_event() */
1993 case 1: button = MOUSE_LEFT; break;
1994 case 2: button = MOUSE_MIDDLE; break;
1995 case 3: button = MOUSE_RIGHT; break;
1996 case 8: button = MOUSE_X1; break;
1997 case 9: button = MOUSE_X2; break;
1998 default:
1999 return FALSE; /* Unknown button */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 }
2001
2002#ifdef FEAT_XIM
2003 /* cancel any preediting */
2004 if (im_is_preediting())
2005 xim_reset();
2006#endif
2007
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002008 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009
2010 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011
2012 return TRUE;
2013}
2014
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015/*
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002016 * GTK+ 2 abstracts scrolling via the GdkEventScroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002019scroll_event(GtkWidget *widget,
2020 GdkEventScroll *event,
2021 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022{
2023 int button;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002024 int_u vim_modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
Bram Moolenaar98921892016-02-23 17:14:37 +01002026#if GTK_CHECK_VERSION(3,0,0)
2027 if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget))
2028#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01002030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 gtk_widget_grab_focus(widget);
2032
2033 switch (event->direction)
2034 {
2035 case GDK_SCROLL_UP:
2036 button = MOUSE_4;
2037 break;
2038 case GDK_SCROLL_DOWN:
2039 button = MOUSE_5;
2040 break;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002041 case GDK_SCROLL_LEFT:
2042 button = MOUSE_7;
2043 break;
2044 case GDK_SCROLL_RIGHT:
2045 button = MOUSE_6;
2046 break;
2047 default: /* This shouldn't happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 return FALSE;
2049 }
2050
2051# ifdef FEAT_XIM
2052 /* cancel any preediting */
2053 if (im_is_preediting())
2054 xim_reset();
2055# endif
2056
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002057 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058
2059 gui_send_mouse_event(button, (int)event->x, (int)event->y,
2060 FALSE, vim_modifiers);
2061
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 return TRUE;
2063}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
2065
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002067button_release_event(GtkWidget *widget UNUSED,
2068 GdkEventButton *event,
2069 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070{
2071 int x, y;
2072 int_u vim_modifiers;
2073
Bram Moolenaar20892c12011-06-26 04:49:00 +02002074 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002075
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 /* Remove any motion "machine gun" timers used for automatic further
2077 extension of allocation areas if outside of the applications window
2078 area .*/
2079 if (motion_repeat_timer)
2080 {
Bram Moolenaar98921892016-02-23 17:14:37 +01002081#if GTK_CHECK_VERSION(3,0,0)
2082 g_source_remove(motion_repeat_timer);
2083#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 gtk_timeout_remove(motion_repeat_timer);
Bram Moolenaar98921892016-02-23 17:14:37 +01002085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 motion_repeat_timer = 0;
2087 }
2088
2089 x = event->x;
2090 y = event->y;
2091
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002092 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093
2094 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095
2096 return TRUE;
2097}
2098
2099
2100#ifdef FEAT_DND
2101/****************************************************************************
2102 * Drag aNd Drop support handlers.
2103 */
2104
2105/*
2106 * Count how many items there may be and separate them with a NUL.
2107 * Apparently the items are separated with \r\n. This is not documented,
2108 * thus be careful not to go past the end. Also allow separation with
2109 * NUL characters.
2110 */
2111 static int
2112count_and_decode_uri_list(char_u *out, char_u *raw, int len)
2113{
2114 int i;
2115 char_u *p = out;
2116 int count = 0;
2117
2118 for (i = 0; i < len; ++i)
2119 {
2120 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r')
2121 {
2122 if (p > out && p[-1] != NUL)
2123 {
2124 ++count;
2125 *p++ = NUL;
2126 }
2127 }
2128 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0)
2129 {
2130 *p++ = hexhex2nr(raw + i + 1);
2131 i += 2;
2132 }
2133 else
2134 *p++ = raw[i];
2135 }
2136 if (p > out && p[-1] != NUL)
2137 {
2138 *p = NUL; /* last item didn't have \r or \n */
2139 ++count;
2140 }
2141 return count;
2142}
2143
2144/*
2145 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
2146 * this process, URI which protocol is not "file:" are removed. Return
2147 * length of array (less than "max").
2148 */
2149 static int
2150filter_uri_list(char_u **outlist, int max, char_u *src)
2151{
2152 int i, j;
2153
2154 for (i = j = 0; i < max; ++i)
2155 {
2156 outlist[i] = NULL;
2157 if (STRNCMP(src, "file:", 5) == 0)
2158 {
2159 src += 5;
2160 if (STRNCMP(src, "//localhost", 11) == 0)
2161 src += 11;
2162 while (src[0] == '/' && src[1] == '/')
2163 ++src;
2164 outlist[j++] = vim_strsave(src);
2165 }
2166 src += STRLEN(src) + 1;
2167 }
2168 return j;
2169}
2170
2171 static char_u **
2172parse_uri_list(int *count, char_u *data, int len)
2173{
2174 int n = 0;
2175 char_u *tmp = NULL;
2176 char_u **array = NULL;;
2177
2178 if (data != NULL && len > 0 && (tmp = (char_u *)alloc(len + 1)) != NULL)
2179 {
2180 n = count_and_decode_uri_list(tmp, data, len);
2181 if (n > 0 && (array = (char_u **)alloc(n * sizeof(char_u *))) != NULL)
2182 n = filter_uri_list(array, n, tmp);
2183 }
2184 vim_free(tmp);
2185 *count = n;
2186 return array;
2187}
2188
2189 static void
2190drag_handle_uri_list(GdkDragContext *context,
2191 GtkSelectionData *data,
2192 guint time_,
2193 GdkModifierType state,
2194 gint x,
2195 gint y)
2196{
2197 char_u **fnames;
2198 int nfiles = 0;
2199
Bram Moolenaar98921892016-02-23 17:14:37 +01002200# if GTK_CHECK_VERSION(3,0,0)
2201 fnames = parse_uri_list(&nfiles,
2202 (char_u *)gtk_selection_data_get_data(data),
2203 gtk_selection_data_get_length(data));
2204# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 fnames = parse_uri_list(&nfiles, data->data, data->length);
Bram Moolenaar98921892016-02-23 17:14:37 +01002206# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207
2208 if (fnames != NULL && nfiles > 0)
2209 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002210 int_u modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
2212 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
2213
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002214 modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215
2216 gui_handle_drop(x, y, modifiers, fnames, nfiles);
2217 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002218 else
2219 vim_free(fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220}
2221
2222 static void
2223drag_handle_text(GdkDragContext *context,
2224 GtkSelectionData *data,
2225 guint time_,
2226 GdkModifierType state)
2227{
2228 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP};
2229 char_u *text;
2230 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232
Bram Moolenaar98921892016-02-23 17:14:37 +01002233# if GTK_CHECK_VERSION(3,0,0)
2234 text = (char_u *)gtk_selection_data_get_data(data);
2235 len = gtk_selection_data_get_length(data);
2236# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 text = data->data;
2238 len = data->length;
Bram Moolenaar98921892016-02-23 17:14:37 +01002239# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240
Bram Moolenaar98921892016-02-23 17:14:37 +01002241# if GTK_CHECK_VERSION(3,0,0)
2242 if (gtk_selection_data_get_data_type(data) == utf8_string_atom)
2243# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 if (data->type == utf8_string_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01002245# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 if (input_conv.vc_type != CONV_NONE)
2248 tmpbuf = string_convert(&input_conv, text, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 if (tmpbuf != NULL)
2250 text = tmpbuf;
2251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
2253 dnd_yank_drag_data(text, (long)len);
2254 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002257 dropkey[2] = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258
2259 if (dropkey[2] != 0)
2260 add_to_input_buf(dropkey, (int)sizeof(dropkey));
2261 else
2262 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263}
2264
2265/*
2266 * DND receiver.
2267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 static void
2269drag_data_received_cb(GtkWidget *widget,
2270 GdkDragContext *context,
2271 gint x,
2272 gint y,
2273 GtkSelectionData *data,
2274 guint info,
2275 guint time_,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002276 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277{
2278 GdkModifierType state;
2279
2280 /* Guard against trash */
Bram Moolenaar98921892016-02-23 17:14:37 +01002281# if GTK_CHECK_VERSION(3,0,0)
2282 const guchar * const data_data = gtk_selection_data_get_data(data);
2283 const gint data_length = gtk_selection_data_get_length(data);
2284 const gint data_format = gtk_selection_data_get_format(data);
2285
2286 if (data_data == NULL
2287 || data_length <= 0
2288 || data_format != 8
2289 || data_data[data_length] != '\0')
2290# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 if (data->data == NULL
2292 || data->length <= 0
2293 || data->format != 8
2294 || data->data[data->length] != '\0')
Bram Moolenaar98921892016-02-23 17:14:37 +01002295# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
2297 gtk_drag_finish(context, FALSE, FALSE, time_);
2298 return;
2299 }
2300
2301 /* Get the current modifier state for proper distinguishment between
2302 * different operations later. */
Bram Moolenaar98921892016-02-23 17:14:37 +01002303#if GTK_CHECK_VERSION(3,0,0)
2304 gui_gtk_get_pointer(widget, NULL, NULL, &state);
2305# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 gdk_window_get_pointer(widget->window, NULL, NULL, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +01002307# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308
2309 /* Not sure about the role of "text/plain" here... */
2310 if (info == (guint)TARGET_TEXT_URI_LIST)
2311 drag_handle_uri_list(context, data, time_, state, x, y);
2312 else
2313 drag_handle_text(context, data, time_, state);
2314
2315}
2316#endif /* FEAT_DND */
2317
2318
2319#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2320/*
2321 * GnomeClient interact callback. Check for unsaved buffers that cannot
2322 * be abandoned and pop up a dialog asking the user for confirmation if
2323 * necessary.
2324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002326sm_client_check_changed_any(GnomeClient *client UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 gint key,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002328 GnomeDialogType type UNUSED,
2329 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330{
2331 cmdmod_T save_cmdmod;
2332 gboolean shutdown_cancelled;
2333
2334 save_cmdmod = cmdmod;
2335
2336# ifdef FEAT_BROWSE
2337 cmdmod.browse = TRUE;
2338# endif
2339# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2340 cmdmod.confirm = TRUE;
2341# endif
2342 /*
2343 * If there are changed buffers, present the user with
2344 * a dialog if possible, otherwise give an error message.
2345 */
Bram Moolenaar027387f2016-01-02 22:25:52 +01002346 shutdown_cancelled = check_changed_any(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347
2348 exiting = FALSE;
2349 cmdmod = save_cmdmod;
2350 setcursor(); /* position the cursor */
2351 out_flush();
2352 /*
2353 * If the user hit the [Cancel] button the whole shutdown
2354 * will be cancelled. Wow, quite powerful feature (:
2355 */
2356 gnome_interaction_key_return(key, shutdown_cancelled);
2357}
2358
2359/*
2360 * Generate a script that can be used to restore the current editing session.
2361 * Save the value of v:this_session before running :mksession in order to make
2362 * automagic session save fully transparent. Return TRUE on success.
2363 */
2364 static int
2365write_session_file(char_u *filename)
2366{
2367 char_u *escaped_filename;
2368 char *mksession_cmdline;
2369 unsigned int save_ssop_flags;
2370 int failed;
2371
2372 /*
2373 * Build an ex command line to create a script that restores the current
2374 * session if executed. Escape the filename to avoid nasty surprises.
2375 */
2376 escaped_filename = vim_strsave_escaped(filename, escape_chars);
2377 if (escaped_filename == NULL)
2378 return FALSE;
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002379 mksession_cmdline = g_strconcat("mksession ", (char *)escaped_filename,
2380 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 vim_free(escaped_filename);
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002382
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 /*
2384 * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
2385 * unpredictable effects when the session is saved automatically. Also,
2386 * we definitely need SSOP_GLOBALS to be able to restore v:this_session.
2387 * Don't use SSOP_BUFFERS to prevent the buffer list from becoming
2388 * enormously large if the GNOME session feature is used regularly.
2389 */
2390 save_ssop_flags = ssop_flags;
2391 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002392 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393
2394 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
2395 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
2396 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
Bram Moolenaar485db9b2005-01-31 19:23:41 +00002397 do_unlet((char_u *)"Save_VV_this_session", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
2399 ssop_flags = save_ssop_flags;
2400 g_free(mksession_cmdline);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002401
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 /*
2403 * Reopen the file and append a command to restore v:this_session,
2404 * as if this save never happened. This is to avoid conflicts with
2405 * the user's own sessions. FIXME: It's probably less hackish to add
2406 * a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
2407 */
2408 if (!failed)
2409 {
2410 FILE *fd;
2411
2412 fd = open_exfile(filename, TRUE, APPENDBIN);
2413
2414 failed = (fd == NULL
2415 || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL
2416 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
2417
2418 if (fd != NULL && fclose(fd) != 0)
2419 failed = TRUE;
2420
2421 if (failed)
2422 mch_remove(filename);
2423 }
2424
2425 return !failed;
2426}
2427
2428/*
2429 * "save_yourself" signal handler. Initiate an interaction to ask the user
2430 * for confirmation if necessary. Save the current editing session and tell
2431 * the session manager how to restart Vim.
2432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 static gboolean
2434sm_client_save_yourself(GnomeClient *client,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002435 gint phase UNUSED,
2436 GnomeSaveStyle save_style UNUSED,
2437 gboolean shutdown UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 GnomeInteractStyle interact_style,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002439 gboolean fast UNUSED,
2440 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441{
2442 static const char suffix[] = "-session.vim";
2443 char *session_file;
2444 unsigned int len;
2445 gboolean success;
2446
2447 /* Always request an interaction if possible. check_changed_any()
2448 * won't actually show a dialog unless any buffers have been modified.
2449 * There doesn't seem to be an obvious way to check that without
2450 * automatically firing the dialog. Anyway, it works just fine. */
2451 if (interact_style == GNOME_INTERACT_ANY)
2452 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL,
2453 &sm_client_check_changed_any,
2454 NULL);
2455 out_flush();
2456 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2457
2458 /* The path is unique for each session save. We do neither know nor care
2459 * which session script will actually be used later. This decision is in
2460 * the domain of the session manager. */
2461 session_file = gnome_config_get_real_path(
2462 gnome_client_get_config_prefix(client));
2463 len = strlen(session_file);
2464
2465 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR)
2466 --len; /* get rid of the superfluous trailing '/' */
2467
2468 session_file = g_renew(char, session_file, len + sizeof(suffix));
2469 memcpy(session_file + len, suffix, sizeof(suffix));
2470
2471 success = write_session_file((char_u *)session_file);
2472
2473 if (success)
2474 {
2475 const char *argv[8];
2476 int i;
2477
2478 /* Tell the session manager how to wipe out the stored session data.
2479 * This isn't as dangerous as it looks, don't worry :) session_file
2480 * is a unique absolute filename. Usually it'll be something like
2481 * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */
2482 i = 0;
2483 argv[i++] = "rm";
2484 argv[i++] = session_file;
2485 argv[i] = NULL;
2486
2487 gnome_client_set_discard_command(client, i, (char **)argv);
2488
2489 /* Tell the session manager how to restore the just saved session.
2490 * This is easily done thanks to Vim's -S option. Pass the -f flag
2491 * since there's no need to fork -- it might even cause confusion.
2492 * Also pass the window role to give the WM something to match on.
2493 * The role is set in gui_mch_open(), thus should _never_ be NULL. */
2494 i = 0;
2495 argv[i++] = restart_command;
2496 argv[i++] = "-f";
2497 argv[i++] = "-g";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 argv[i++] = "--role";
2499 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 argv[i++] = "-S";
2501 argv[i++] = session_file;
2502 argv[i] = NULL;
2503
2504 gnome_client_set_restart_command(client, i, (char **)argv);
2505 gnome_client_set_clone_command(client, 0, NULL);
2506 }
2507
2508 g_free(session_file);
2509
2510 return success;
2511}
2512
2513/*
2514 * Called when the session manager wants us to die. There isn't much to save
2515 * here since "save_yourself" has been emitted before (unless serious trouble
2516 * is happening).
2517 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002519sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520{
2521 /* Don't write messages to the GUI anymore */
2522 full_screen = FALSE;
2523
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002524 vim_strncpy(IObuff, (char_u *)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002525 _("Vim: Received \"die\" request from session manager\n"),
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002526 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 preserve_exit();
2528}
2529
2530/*
2531 * Connect our signal handlers to be notified on session save and shutdown.
2532 */
2533 static void
2534setup_save_yourself(void)
2535{
2536 GnomeClient *client;
2537
2538 client = gnome_master_client();
2539
2540 if (client != NULL)
2541 {
2542 /* Must use the deprecated gtk_signal_connect() for compatibility
2543 * with GNOME 1. Arrgh, zombies! */
2544 gtk_signal_connect(GTK_OBJECT(client), "save_yourself",
2545 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL);
2546 gtk_signal_connect(GTK_OBJECT(client), "die",
2547 GTK_SIGNAL_FUNC(&sm_client_die), NULL);
2548 }
2549}
2550
2551#else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2552
2553# ifdef USE_XSMP
2554/*
2555 * GTK tells us that XSMP needs attention
2556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 static gboolean
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002558local_xsmp_handle_requests(
2559 GIOChannel *source UNUSED,
2560 GIOCondition condition,
2561 gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562{
2563 if (condition == G_IO_IN)
2564 {
2565 /* Do stuff; maybe close connection */
2566 if (xsmp_handle_requests() == FAIL)
2567 g_io_channel_unref((GIOChannel *)data);
2568 return TRUE;
2569 }
2570 /* Error */
2571 g_io_channel_unref((GIOChannel *)data);
2572 xsmp_close();
2573 return TRUE;
2574}
2575# endif /* USE_XSMP */
2576
2577/*
2578 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2579 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2580 */
2581 static void
2582setup_save_yourself(void)
2583{
2584 Atom *existing_atoms = NULL;
2585 int count = 0;
2586
Bram Moolenaar98921892016-02-23 17:14:37 +01002587# ifdef USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 if (xsmp_icefd != -1)
2589 {
2590 /*
2591 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2592 * set up GTK IO monitor
2593 */
2594 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd);
2595
2596 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP,
2597 local_xsmp_handle_requests, (gpointer)g_io);
Bram Moolenaar98921892016-02-23 17:14:37 +01002598 g_io_channel_unref(g_io);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 }
2600 else
Bram Moolenaar98921892016-02-23 17:14:37 +01002601# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 {
2603 /* Fall back to old method */
2604
2605 /* first get the existing value */
Bram Moolenaar98921892016-02-23 17:14:37 +01002606# if GTK_CHECK_VERSION(3,0,0)
2607 GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin);
2608
2609 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win),
2610 GDK_WINDOW_XID(mainwin_win),
2611 &existing_atoms, &count))
2612# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2614 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2615 &existing_atoms, &count))
Bram Moolenaar98921892016-02-23 17:14:37 +01002616# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 {
2618 Atom *new_atoms;
2619 Atom save_yourself_xatom;
2620 int i;
2621
2622 save_yourself_xatom = GET_X_ATOM(save_yourself_atom);
2623
2624 /* check if WM_SAVE_YOURSELF isn't there yet */
2625 for (i = 0; i < count; ++i)
2626 if (existing_atoms[i] == save_yourself_xatom)
2627 break;
2628
2629 if (i == count)
2630 {
2631 /* allocate an Atoms array which is one item longer */
2632 new_atoms = (Atom *)alloc((unsigned)((count + 1)
2633 * sizeof(Atom)));
2634 if (new_atoms != NULL)
2635 {
2636 memcpy(new_atoms, existing_atoms, count * sizeof(Atom));
2637 new_atoms[count] = save_yourself_xatom;
Bram Moolenaar98921892016-02-23 17:14:37 +01002638# if GTK_CHECK_VERSION(3,0,0)
2639 XSetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win),
2640 GDK_WINDOW_XID(mainwin_win),
2641# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 XSetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2643 GDK_WINDOW_XWINDOW(gui.mainwin->window),
Bram Moolenaar98921892016-02-23 17:14:37 +01002644# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 new_atoms, count + 1);
2646 vim_free(new_atoms);
2647 }
2648 }
2649 XFree(existing_atoms);
2650 }
2651 }
2652}
2653
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654/*
2655 * Installing a global event filter seems to be the only way to catch
2656 * client messages of type WM_PROTOCOLS without overriding GDK's own
2657 * client message event filter. Well, that's still better than trying
2658 * to guess what the GDK filter had done if it had been invoked instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 *
2660 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2661 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2662 * I have the nasty feeling modern session managers just don't send this
2663 * deprecated message anymore. Addition: confirmed by several people.
2664 *
2665 * The GNOME session support is much cooler anyway. Unlike this ugly
2666 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2667 * it should work with KDE as well.
2668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 static GdkFilterReturn
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002670global_event_filter(GdkXEvent *xev,
2671 GdkEvent *event UNUSED,
2672 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
2674 XEvent *xevent = (XEvent *)xev;
2675
2676 if (xevent != NULL
2677 && xevent->type == ClientMessage
2678 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002679 && (long_u)xevent->xclient.data.l[0]
2680 == GET_X_ATOM(save_yourself_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 {
2682 out_flush();
2683 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2684 /*
2685 * Set the window's WM_COMMAND property, to let the window manager
2686 * know we are done saving ourselves. We don't want to be
2687 * restarted, thus set argv to NULL.
2688 */
Bram Moolenaar98921892016-02-23 17:14:37 +01002689# if GTK_CHECK_VERSION(3,0,0)
2690 XSetCommand(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)),
2691 GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin)),
2692# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2694 GDK_WINDOW_XWINDOW(gui.mainwin->window),
Bram Moolenaar98921892016-02-23 17:14:37 +01002695# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 NULL, 0);
2697 return GDK_FILTER_REMOVE;
2698 }
2699
2700 return GDK_FILTER_CONTINUE;
2701}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702#endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2703
2704
2705/*
2706 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2707 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002709mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710{
2711/* If you get an error message here, you still need to unpack the runtime
2712 * archive! */
2713#ifdef magick
2714# undef magick
2715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 /* A bit hackish, but avoids casting later and allows optimization */
2717# define static static const
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718#define magick vim32x32
2719#include "../runtime/vim32x32.xpm"
2720#undef magick
2721#define magick vim16x16
2722#include "../runtime/vim16x16.xpm"
2723#undef magick
2724#define magick vim48x48
2725#include "../runtime/vim48x48.xpm"
2726#undef magick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727# undef static
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
Bram Moolenaar98921892016-02-23 17:14:37 +01002729#if GTK_CHECK_VERSION(3,0,0)
2730 GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin);
2731#endif
2732
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 /* When started with "--echo-wid" argument, write window ID on stdout. */
2734 if (echo_wid_arg)
2735 {
Bram Moolenaar98921892016-02-23 17:14:37 +01002736#if GTK_CHECK_VERSION(3,0,0)
2737 printf("WID: %ld\n", (long)GDK_WINDOW_XID(mainwin_win));
2738#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 printf("WID: %ld\n", (long)GDK_WINDOW_XWINDOW(gui.mainwin->window));
Bram Moolenaar98921892016-02-23 17:14:37 +01002740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 fflush(stdout);
2742 }
2743
2744 if (vim_strchr(p_go, GO_ICON) != NULL)
2745 {
2746 /*
2747 * Add an icon to the main window. For fun and convenience of the user.
2748 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 GList *icons = NULL;
2750
2751 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16));
2752 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32));
2753 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48));
2754
2755 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons);
2756
2757 g_list_foreach(icons, (GFunc)&g_object_unref, NULL);
2758 g_list_free(icons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 }
2760
2761#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
2762 /* Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 gdk_window_add_filter(NULL, &global_event_filter, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764#endif
2765 /* Setup to indicate to the window manager that we want to catch the
2766 * WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2767 * manager instead. */
2768#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2769 if (using_gnome)
2770#endif
2771 setup_save_yourself();
2772
2773#ifdef FEAT_CLIENTSERVER
2774 if (serverName == NULL && serverDelayedStartName != NULL)
2775 {
2776 /* This is a :gui command in a plain vim with no previous server */
Bram Moolenaar98921892016-02-23 17:14:37 +01002777# if GTK_CHECK_VERSION(3,0,0)
2778 commWindow = GDK_WINDOW_XID(mainwin_win);
2779
2780 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(mainwin_win),
2781 serverDelayedStartName);
2782# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 commWindow = GDK_WINDOW_XWINDOW(gui.mainwin->window);
2784
2785 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2786 serverDelayedStartName);
Bram Moolenaar98921892016-02-23 17:14:37 +01002787# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 }
2789 else
2790 {
2791 /*
2792 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2793 * have to change the "server" registration to that of the main window
2794 * If we have not registered a name yet, remember the window
2795 */
Bram Moolenaar98921892016-02-23 17:14:37 +01002796# if GTK_CHECK_VERSION(3,0,0)
2797 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(mainwin_win),
2798 GDK_WINDOW_XID(mainwin_win));
2799# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2801 GDK_WINDOW_XWINDOW(gui.mainwin->window));
Bram Moolenaar98921892016-02-23 17:14:37 +01002802# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
2804 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK);
Bram Moolenaar98921892016-02-23 17:14:37 +01002805# if GTK_CHECK_VERSION(3,0,0)
2806 g_signal_connect(G_OBJECT(gui.mainwin), "property-notify-event",
2807 G_CALLBACK(property_event), NULL);
2808# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "property_notify_event",
2810 GTK_SIGNAL_FUNC(property_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01002811# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812#endif
2813}
2814
2815 static GdkCursor *
2816create_blank_pointer(void)
2817{
2818 GdkWindow *root_window = NULL;
Bram Moolenaar98921892016-02-23 17:14:37 +01002819#if GTK_CHECK_VERSION(3,0,0)
2820 GdkPixbuf *blank_mask;
2821#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 GdkPixmap *blank_mask;
Bram Moolenaar98921892016-02-23 17:14:37 +01002823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 GdkCursor *cursor;
2825 GdkColor color = { 0, 0, 0, 0 };
Bram Moolenaar98921892016-02-23 17:14:37 +01002826#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 char blank_data[] = { 0x0 };
Bram Moolenaar98921892016-02-23 17:14:37 +01002828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829
2830#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar98921892016-02-23 17:14:37 +01002831# if GTK_CHECK_VERSION(3,12,0)
2832 {
2833 GdkWindow * const win = gtk_widget_get_window(gui.mainwin);
2834 GdkScreen * const scrn = gdk_window_get_screen(win);
2835 root_window = gdk_screen_get_root_window(scrn);
2836 }
2837# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 root_window = gtk_widget_get_root_window(gui.mainwin);
Bram Moolenaar98921892016-02-23 17:14:37 +01002839# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840#endif
2841
2842 /* Create a pseudo blank pointer, which is in fact one pixel by one pixel
2843 * in size. */
Bram Moolenaar98921892016-02-23 17:14:37 +01002844#if GTK_CHECK_VERSION(3,0,0)
2845 {
2846 cairo_surface_t *surf;
2847 cairo_t *cr;
2848
2849 surf = cairo_image_surface_create(CAIRO_FORMAT_A1, 1, 1);
2850 cr = cairo_create(surf);
2851
2852 cairo_set_source_rgb(cr,
2853 color.red / 65535.0,
2854 color.green / 65535.0,
2855 color.blue / 65535.0);
2856 cairo_rectangle(cr, 0, 0, 1, 1);
2857 cairo_fill(cr);
2858 cairo_destroy(cr);
2859
2860 blank_mask = gdk_pixbuf_get_from_surface(surf, 0, 0, 1, 1);
2861 cairo_surface_destroy(surf);
2862
2863 cursor = gdk_cursor_new_from_pixbuf(gdk_window_get_display(root_window),
2864 blank_mask, 0, 0);
2865 g_object_unref(blank_mask);
2866 }
2867#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1);
2869 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask,
2870 &color, &color, 0, 0);
2871 gdk_bitmap_unref(blank_mask);
Bram Moolenaar98921892016-02-23 17:14:37 +01002872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873
2874 return cursor;
2875}
2876
2877#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 static void
2879mainwin_screen_changed_cb(GtkWidget *widget,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002880 GdkScreen *previous_screen UNUSED,
2881 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882{
2883 if (!gtk_widget_has_screen(widget))
2884 return;
2885
2886 /*
Bram Moolenaar49325942007-05-10 19:19:59 +00002887 * Recreate the invisible mouse cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 */
2889 if (gui.blank_pointer != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01002890# if GTK_CHECK_VERSION(3,0,0)
2891 g_object_unref(G_OBJECT(gui.blank_pointer));
2892# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 gdk_cursor_unref(gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01002894# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895
2896 gui.blank_pointer = create_blank_pointer();
2897
Bram Moolenaar98921892016-02-23 17:14:37 +01002898# if GTK_CHECK_VERSION(3,0,0)
2899 if (gui.pointer_hidden && gtk_widget_get_window(gui.drawarea) != NULL)
2900 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
2901 gui.blank_pointer);
2902# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 if (gui.pointer_hidden && gui.drawarea->window != NULL)
2904 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01002905# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906
2907 /*
2908 * Create a new PangoContext for this screen, and initialize it
2909 * with the current font if necessary.
2910 */
2911 if (gui.text_context != NULL)
2912 g_object_unref(gui.text_context);
2913
2914 gui.text_context = gtk_widget_create_pango_context(widget);
2915 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
2916
2917 if (gui.norm_font != NULL)
2918 {
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002919 gui_mch_init_font(p_guifont, FALSE);
Bram Moolenaar04a9d452006-03-27 21:03:26 +00002920 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 }
2922}
2923#endif /* HAVE_GTK_MULTIHEAD */
2924
2925/*
2926 * After the drawing area comes up, we calculate all colors and create the
2927 * dummy blank cursor.
2928 *
2929 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2930 * fact that the main VIM engine doesn't take them into account anywhere.
2931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002933drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934{
2935 GtkWidget *sbar;
Bram Moolenaar98921892016-02-23 17:14:37 +01002936#if GTK_CHECK_VERSION(3,0,0)
2937 GtkAllocation allocation;
2938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939
2940#ifdef FEAT_XIM
2941 xim_init();
2942#endif
2943 gui_mch_new_colors();
Bram Moolenaar98921892016-02-23 17:14:37 +01002944#if GTK_CHECK_VERSION(3,0,0)
2945 gui.surface = gdk_window_create_similar_surface(
2946 gtk_widget_get_window(widget),
2947 CAIRO_CONTENT_COLOR_ALPHA,
2948 gtk_widget_get_allocated_width(widget),
2949 gtk_widget_get_allocated_height(widget));
2950#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 gui.text_gc = gdk_gc_new(gui.drawarea->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01002952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953
2954 gui.blank_pointer = create_blank_pointer();
2955 if (gui.pointer_hidden)
Bram Moolenaar98921892016-02-23 17:14:37 +01002956#if GTK_CHECK_VERSION(3,0,0)
2957 gdk_window_set_cursor(gtk_widget_get_window(widget), gui.blank_pointer);
2958#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 gdk_window_set_cursor(widget->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01002960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961
2962 /* get the actual size of the scrollbars, if they are realized */
2963 sbar = firstwin->w_scrollbars[SBAR_LEFT].id;
2964 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT]
2965 && firstwin->w_scrollbars[SBAR_RIGHT].id))
2966 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id;
Bram Moolenaar98921892016-02-23 17:14:37 +01002967#if GTK_CHECK_VERSION(3,0,0)
2968 gtk_widget_get_allocation(sbar, &allocation);
2969 if (sbar && gtk_widget_get_realized(sbar) && allocation.width)
2970 gui.scrollbar_width = allocation.width;
2971#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.width)
2973 gui.scrollbar_width = sbar->allocation.width;
Bram Moolenaar98921892016-02-23 17:14:37 +01002974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975
2976 sbar = gui.bottom_sbar.id;
Bram Moolenaar98921892016-02-23 17:14:37 +01002977#if GTK_CHECK_VERSION(3,0,0)
2978 if (sbar && gtk_widget_get_realized(sbar) && allocation.height)
2979 gui.scrollbar_height = allocation.height;
2980#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.height)
2982 gui.scrollbar_height = sbar->allocation.height;
Bram Moolenaar98921892016-02-23 17:14:37 +01002983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984}
2985
2986/*
2987 * Properly clean up on shutdown.
2988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002990drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991{
2992 /* Don't write messages to the GUI anymore */
2993 full_screen = FALSE;
2994
2995#ifdef FEAT_XIM
2996 im_shutdown();
2997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 if (gui.ascii_glyphs != NULL)
2999 {
3000 pango_glyph_string_free(gui.ascii_glyphs);
3001 gui.ascii_glyphs = NULL;
3002 }
3003 if (gui.ascii_font != NULL)
3004 {
3005 g_object_unref(gui.ascii_font);
3006 gui.ascii_font = NULL;
3007 }
3008 g_object_unref(gui.text_context);
3009 gui.text_context = NULL;
3010
Bram Moolenaar98921892016-02-23 17:14:37 +01003011#if GTK_CHECK_VERSION(3,0,0)
3012 if (gui.surface != NULL)
3013 {
3014 cairo_surface_destroy(gui.surface);
3015 gui.surface = NULL;
3016 }
3017#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 g_object_unref(gui.text_gc);
3019 gui.text_gc = NULL;
Bram Moolenaar98921892016-02-23 17:14:37 +01003020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
Bram Moolenaar98921892016-02-23 17:14:37 +01003022#if GTK_CHECK_VERSION(3,0,0)
3023 g_object_unref(G_OBJECT(gui.blank_pointer));
3024#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 gdk_cursor_unref(gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01003026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 gui.blank_pointer = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028}
3029
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003031drawarea_style_set_cb(GtkWidget *widget UNUSED,
3032 GtkStyle *previous_style UNUSED,
3033 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034{
3035 gui_mch_new_colors();
3036}
3037
Bram Moolenaar98921892016-02-23 17:14:37 +01003038#if GTK_CHECK_VERSION(3,0,0)
3039 static gboolean
3040drawarea_configure_event_cb(GtkWidget *widget,
3041 GdkEventConfigure *event,
3042 gpointer data UNUSED)
3043{
3044 static int cur_width = 0;
3045 static int cur_height = 0;
3046
3047 g_return_val_if_fail(event
3048 && event->width >= 1 && event->height >= 1, TRUE);
3049
3050 if (event->width == cur_width && event->height == cur_height)
3051 return TRUE;
3052
3053 cur_width = event->width;
3054 cur_height = event->height;
3055
3056 if (gui.surface != NULL)
3057 cairo_surface_destroy(gui.surface);
3058
3059 gui.surface = gdk_window_create_similar_surface(
3060 gtk_widget_get_window(widget),
3061 CAIRO_CONTENT_COLOR_ALPHA,
3062 event->width, event->height);
3063
3064 gtk_widget_queue_draw(widget);
3065
3066 return TRUE;
3067}
3068#endif
3069
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070/*
3071 * Callback routine for the "delete_event" signal on the toplevel window.
3072 * Tries to vim gracefully, or refuses to exit with changed buffers.
3073 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003075delete_event_cb(GtkWidget *widget UNUSED,
3076 GdkEventAny *event UNUSED,
3077 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078{
3079 gui_shell_closed();
3080 return TRUE;
3081}
3082
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003083#if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
3084 static int
3085get_item_dimensions(GtkWidget *widget, GtkOrientation orientation)
3086{
3087 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL;
3088
Bram Moolenaar98921892016-02-23 17:14:37 +01003089# ifdef FEAT_GUI_GNOME
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003090 if (using_gnome && widget != NULL)
3091 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003092 GtkWidget *parent;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003093 BonoboDockItem *dockitem;
3094
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003095 parent = gtk_widget_get_parent(widget);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003096 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM)
3097 {
3098 /* Only menu & toolbar are dock items. Could tabline be?
3099 * Seem to be only the 2 defined in GNOME */
3100 widget = parent;
3101 dockitem = BONOBO_DOCK_ITEM(widget);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003102
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003103 if (dockitem == NULL || dockitem->is_floating)
3104 return 0;
3105 item_orientation = bonobo_dock_item_get_orientation(dockitem);
3106 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003107 }
Bram Moolenaar98921892016-02-23 17:14:37 +01003108# endif
3109# if GTK_CHECK_VERSION(3,0,0)
3110 if (widget != NULL
3111 && item_orientation == orientation
3112 && gtk_widget_get_realized(widget)
3113 && gtk_widget_get_visible(widget))
3114# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003115 if (widget != NULL
3116 && item_orientation == orientation
3117 && GTK_WIDGET_REALIZED(widget)
3118 && GTK_WIDGET_VISIBLE(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01003119# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003120 {
Bram Moolenaar98921892016-02-23 17:14:37 +01003121# if GTK_CHECK_VERSION(3,0,0)
3122 GtkAllocation allocation;
3123
3124 gtk_widget_get_allocation(widget, &allocation);
3125
3126 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3127 return allocation.height;
3128 else
3129 return allocation.width;
3130# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003131 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3132 return widget->allocation.height;
3133 else
3134 return widget->allocation.width;
Bram Moolenaar98921892016-02-23 17:14:37 +01003135# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003136 }
3137 return 0;
3138}
3139#endif
3140
3141 static int
3142get_menu_tool_width(void)
3143{
3144 int width = 0;
3145
3146#ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */
3147# ifdef FEAT_MENU
3148 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL);
3149# endif
3150# ifdef FEAT_TOOLBAR
3151 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL);
3152# endif
3153# ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003154 if (gui.tabline != NULL)
3155 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003156# endif
3157#endif
3158
3159 return width;
3160}
3161
3162 static int
3163get_menu_tool_height(void)
3164{
3165 int height = 0;
3166
3167#ifdef FEAT_MENU
3168 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL);
3169#endif
3170#ifdef FEAT_TOOLBAR
3171 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL);
3172#endif
3173#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003174 if (gui.tabline != NULL)
3175 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003176#endif
3177
3178 return height;
3179}
3180
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003181/* This controls whether we can set the real window hints at
3182 * start-up when in a GtkPlug.
3183 * 0 = normal processing (default)
3184 * 1 = init. hints set, no-one's tried to reset since last check
3185 * 2 = init. hints set, attempt made to change hints
3186 */
3187static int init_window_hints_state = 0;
3188
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003189 static void
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003190update_window_manager_hints(int force_width, int force_height)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003191{
3192 static int old_width = 0;
3193 static int old_height = 0;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003194 static int old_min_width = 0;
3195 static int old_min_height = 0;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003196 static int old_char_width = 0;
3197 static int old_char_height = 0;
3198
3199 int width;
3200 int height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003201 int min_width;
3202 int min_height;
3203
3204 /* At start-up, don't try to set the hints until the initial
3205 * values have been used (those that dictate our initial size)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003206 * Let forced (i.e., correct) values through always.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003207 */
3208 if (!(force_width && force_height) && init_window_hints_state > 0)
3209 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003210 /* Don't do it! */
3211 init_window_hints_state = 2;
3212 return;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003213 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003214
3215 /* This also needs to be done when the main window isn't there yet,
3216 * otherwise the hints don't work. */
3217 width = gui_get_base_width();
3218 height = gui_get_base_height();
3219# ifdef FEAT_MENU
3220 height += tabline_height() * gui.char_height;
3221# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003222 width += get_menu_tool_width();
3223 height += get_menu_tool_height();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003224
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003225 /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
Bram Moolenaar49325942007-05-10 19:19:59 +00003226 * their actual widget size. When we set our size ourselves (e.g.,
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003227 * 'set columns=' or init. -geom) we briefly set the min. to the size
3228 * we wish to be instead of the legitimate minimum so that we actually
3229 * resize correctly.
3230 */
3231 if (force_width && force_height)
3232 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003233 min_width = force_width;
3234 min_height = force_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003235 }
3236 else
3237 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003238 min_width = width + MIN_COLUMNS * gui.char_width;
3239 min_height = height + MIN_LINES * gui.char_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003240 }
3241
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003242 /* Avoid an expose event when the size didn't change. */
3243 if (width != old_width
3244 || height != old_height
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003245 || min_width != old_min_width
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003246 || min_height != old_min_height
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003247 || gui.char_width != old_char_width
3248 || gui.char_height != old_char_height)
3249 {
3250 GdkGeometry geometry;
3251 GdkWindowHints geometry_mask;
3252
3253 geometry.width_inc = gui.char_width;
3254 geometry.height_inc = gui.char_height;
3255 geometry.base_width = width;
3256 geometry.base_height = height;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003257 geometry.min_width = min_width;
3258 geometry.min_height = min_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003259 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC
3260 |GDK_HINT_MIN_SIZE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003261 /* Using gui.formwin as geometry widget doesn't work as expected
3262 * with GTK+ 2 -- dunno why. Presumably all the resizing hacks
3263 * in Vim confuse GTK+. */
3264 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin,
3265 &geometry, geometry_mask);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003266 old_width = width;
3267 old_height = height;
3268 old_min_width = min_width;
3269 old_min_height = min_height;
3270 old_char_width = gui.char_width;
3271 old_char_height = gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003272 }
3273}
3274
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275#ifdef FEAT_TOOLBAR
3276
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277/*
3278 * This extra effort wouldn't be necessary if we only used stock icons in the
3279 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
3280 * shouldn't be treated differently, thus we do need this.
3281 */
3282 static void
3283icon_size_changed_foreach(GtkWidget *widget, gpointer user_data)
3284{
3285 if (GTK_IS_IMAGE(widget))
3286 {
3287 GtkImage *image = (GtkImage *)widget;
3288
Bram Moolenaar98921892016-02-23 17:14:37 +01003289# if GTK_CHECK_VERSION(3,10,0)
3290 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_NAME)
3291 {
3292 const GtkIconSize icon_size = GPOINTER_TO_INT(user_data);
3293 const gchar *icon_name;
3294
3295 gtk_image_get_icon_name(image, &icon_name, NULL);
3296
3297 gtk_image_set_from_icon_name(image, icon_name, icon_size);
3298 }
3299# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 /* User-defined icons are stored in a GtkIconSet */
3301 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET)
3302 {
3303 GtkIconSet *icon_set;
3304 GtkIconSize icon_size;
3305
3306 gtk_image_get_icon_set(image, &icon_set, &icon_size);
3307 icon_size = (GtkIconSize)(long)user_data;
3308
3309 gtk_icon_set_ref(icon_set);
3310 gtk_image_set_from_icon_set(image, icon_set, icon_size);
3311 gtk_icon_set_unref(icon_set);
3312 }
Bram Moolenaar98921892016-02-23 17:14:37 +01003313# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 }
3315 else if (GTK_IS_CONTAINER(widget))
3316 {
3317 gtk_container_foreach((GtkContainer *)widget,
3318 &icon_size_changed_foreach,
3319 user_data);
3320 }
3321}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322
3323 static void
3324set_toolbar_style(GtkToolbar *toolbar)
3325{
3326 GtkToolbarStyle style;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 GtkIconSize size;
3328 GtkIconSize oldsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3331 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3332 style = GTK_TOOLBAR_BOTH_HORIZ;
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003333 else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 == (TOOLBAR_TEXT | TOOLBAR_ICONS))
3335 style = GTK_TOOLBAR_BOTH;
3336 else if (toolbar_flags & TOOLBAR_TEXT)
3337 style = GTK_TOOLBAR_TEXT;
3338 else
3339 style = GTK_TOOLBAR_ICONS;
3340
3341 gtk_toolbar_set_style(toolbar, style);
Bram Moolenaar98921892016-02-23 17:14:37 +01003342# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003344# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 switch (tbis_flags)
3347 {
3348 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break;
3349 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break;
3350 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break;
3351 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
Bram Moolenaarbeb003b2016-03-08 22:47:17 +01003352 case TBIS_HUGE: size = GTK_ICON_SIZE_DND; break;
3353 case TBIS_GIANT: size = GTK_ICON_SIZE_DIALOG; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 default: size = GTK_ICON_SIZE_INVALID; break;
3355 }
3356 oldsize = gtk_toolbar_get_icon_size(toolbar);
3357
3358 if (size == GTK_ICON_SIZE_INVALID)
3359 {
3360 /* Let global user preferences decide the icon size. */
3361 gtk_toolbar_unset_icon_size(toolbar);
3362 size = gtk_toolbar_get_icon_size(toolbar);
3363 }
3364 if (size != oldsize)
3365 {
3366 gtk_container_foreach(GTK_CONTAINER(toolbar),
3367 &icon_size_changed_foreach,
3368 GINT_TO_POINTER((int)size));
3369 }
3370 gtk_toolbar_set_icon_size(toolbar, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371}
3372
3373#endif /* FEAT_TOOLBAR */
3374
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003375#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3376static int ignore_tabline_evt = FALSE;
Bram Moolenaara5621492006-02-25 21:55:24 +00003377static GtkWidget *tabline_menu;
Bram Moolenaar98921892016-02-23 17:14:37 +01003378# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003379static GtkTooltips *tabline_tooltip;
Bram Moolenaar98921892016-02-23 17:14:37 +01003380# endif
Bram Moolenaara5621492006-02-25 21:55:24 +00003381static int clicked_page; /* page clicked in tab line */
3382
3383/*
3384 * Handle selecting an item in the tab line popup menu.
3385 */
Bram Moolenaara5621492006-02-25 21:55:24 +00003386 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003387tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data)
Bram Moolenaara5621492006-02-25 21:55:24 +00003388{
Bram Moolenaara5621492006-02-25 21:55:24 +00003389 /* Add the string cmd into input buffer */
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003390 send_tabline_menu_event(clicked_page, (int)(long)user_data);
Bram Moolenaara5621492006-02-25 21:55:24 +00003391}
3392
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003393 static void
3394add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp)
3395{
3396 GtkWidget *item;
3397 char_u *utf_text;
3398
3399 utf_text = CONVERT_TO_UTF8(text);
3400 item = gtk_menu_item_new_with_label((const char *)utf_text);
3401 gtk_widget_show(item);
3402 CONVERT_TO_UTF8_FREE(utf_text);
3403
3404 gtk_container_add(GTK_CONTAINER(menu), item);
Bram Moolenaar98921892016-02-23 17:14:37 +01003405# if GTK_CHECK_VERSION(3,0,0)
3406 g_signal_connect(G_OBJECT(item), "activate",
3407 G_CALLBACK(tabline_menu_handler),
3408 GINT_TO_POINTER(resp));
3409# else
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003410 gtk_signal_connect(GTK_OBJECT(item), "activate",
3411 GTK_SIGNAL_FUNC(tabline_menu_handler),
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003412 (gpointer)(long)resp);
Bram Moolenaar98921892016-02-23 17:14:37 +01003413# endif
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003414}
3415
Bram Moolenaara5621492006-02-25 21:55:24 +00003416/*
Bram Moolenaara5621492006-02-25 21:55:24 +00003417 * Create a menu for the tab line.
3418 */
3419 static GtkWidget *
3420create_tabline_menu(void)
3421{
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003422 GtkWidget *menu;
Bram Moolenaara5621492006-02-25 21:55:24 +00003423
3424 menu = gtk_menu_new();
Bram Moolenaar7098ee52015-06-09 19:14:24 +02003425 if (first_tabpage->tp_next != NULL)
3426 add_tabline_menu_item(menu, (char_u *)_("Close tab"),
3427 TABLINE_MENU_CLOSE);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003428 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW);
3429 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN);
Bram Moolenaara5621492006-02-25 21:55:24 +00003430
3431 return menu;
3432}
3433
3434 static gboolean
3435on_tabline_menu(GtkWidget *widget, GdkEvent *event)
3436{
3437 /* Was this button press event ? */
3438 if (event->type == GDK_BUTTON_PRESS)
3439 {
3440 GdkEventButton *bevent = (GdkEventButton *)event;
3441 int x = bevent->x;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003442 int y = bevent->y;
3443 GtkWidget *tabwidget;
3444 GdkWindow *tabwin;
Bram Moolenaara5621492006-02-25 21:55:24 +00003445
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003446 /* When ignoring events return TRUE so that the selected page doesn't
3447 * change. */
3448 if (hold_gui_events
3449# ifdef FEAT_CMDWIN
3450 || cmdwin_type != 0
3451# endif
3452 )
3453 return TRUE;
3454
Bram Moolenaar98921892016-02-23 17:14:37 +01003455# if GTK_CHECK_VERSION(3,0,0)
3456 tabwin = gui_gtk_window_at_position(gui.mainwin, &x, &y);
3457# else
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003458 tabwin = gdk_window_at_pointer(&x, &y);
Bram Moolenaar98921892016-02-23 17:14:37 +01003459# endif
3460
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003461 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget);
Bram Moolenaar98921892016-02-23 17:14:37 +01003462# if GTK_CHECK_VERSION(3,0,0)
3463 clicked_page = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(tabwidget),
3464 "tab_num"));
3465# else
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003466 clicked_page = (int)(long)gtk_object_get_user_data(
3467 GTK_OBJECT(tabwidget));
Bram Moolenaar98921892016-02-23 17:14:37 +01003468# endif
Bram Moolenaara5621492006-02-25 21:55:24 +00003469
3470 /* If the event was generated for 3rd button popup the menu. */
3471 if (bevent->button == 3)
3472 {
3473 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
3474 bevent->button, bevent->time);
3475 /* We handled the event. */
3476 return TRUE;
3477 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003478 else if (bevent->button == 1)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003479 {
Bram Moolenaar96351572006-05-05 21:16:59 +00003480 if (clicked_page == 0)
3481 {
Bram Moolenaar07354542007-09-15 12:07:46 +00003482 /* Click after all tabs moves to next tab page. When "x" is
3483 * small guess it's the left button. */
Bram Moolenaara3f41662010-07-11 19:01:06 +02003484 send_tabline_event(x < 50 ? -1 : 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00003485 }
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003486 }
Bram Moolenaara5621492006-02-25 21:55:24 +00003487 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003488
Bram Moolenaara5621492006-02-25 21:55:24 +00003489 /* We didn't handle the event. */
3490 return FALSE;
3491}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003492
3493/*
3494 * Handle selecting one of the tabs.
3495 */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003496 static void
3497on_select_tab(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003498 GtkNotebook *notebook UNUSED,
Bram Moolenaar98921892016-02-23 17:14:37 +01003499# if GTK_CHECK_VERSION(3,0,0)
3500 gpointer *page UNUSED,
3501# else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003502 GtkNotebookPage *page UNUSED,
Bram Moolenaar98921892016-02-23 17:14:37 +01003503# endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00003504 gint idx,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003505 gpointer data UNUSED)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003506{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003507 if (!ignore_tabline_evt)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003508 {
Bram Moolenaara3f41662010-07-11 19:01:06 +02003509 send_tabline_event(idx + 1);
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003510 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003511}
3512
3513/*
3514 * Show or hide the tabline.
3515 */
3516 void
3517gui_mch_show_tabline(int showit)
3518{
3519 if (gui.tabline == NULL)
3520 return;
3521
3522 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)))
3523 {
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003524 /* Note: this may cause a resize event */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003525 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003526 update_window_manager_hints(0, 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00003527 if (showit)
Bram Moolenaar98921892016-02-23 17:14:37 +01003528# if GTK_CHECK_VERSION(3,0,0)
3529 gtk_widget_set_can_focus(GTK_WIDGET(gui.tabline), FALSE);
3530# else
Bram Moolenaar96351572006-05-05 21:16:59 +00003531 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui.tabline), GTK_CAN_FOCUS);
Bram Moolenaar98921892016-02-23 17:14:37 +01003532# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003533 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003534
3535 gui_mch_update();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003536}
3537
3538/*
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003539 * Return TRUE when tabline is displayed.
3540 */
3541 int
3542gui_mch_showing_tabline(void)
3543{
3544 return gui.tabline != NULL
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003545 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline));
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003546}
3547
3548/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003549 * Update the labels of the tabline.
3550 */
3551 void
3552gui_mch_update_tabline(void)
3553{
3554 GtkWidget *page;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003555 GtkWidget *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003556 GtkWidget *label;
3557 tabpage_T *tp;
3558 int nr = 0;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003559 int tab_num;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003560 int curtabidx = 0;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003561 char_u *labeltext;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003562
3563 if (gui.tabline == NULL)
3564 return;
3565
3566 ignore_tabline_evt = TRUE;
3567
3568 /* Add a label for each tab page. They all contain the same text area. */
3569 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3570 {
3571 if (tp == curtab)
3572 curtabidx = nr;
3573
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003574 tab_num = nr + 1;
3575
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003576 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr);
3577 if (page == NULL)
3578 {
3579 /* Add notebook page */
Bram Moolenaar98921892016-02-23 17:14:37 +01003580# if GTK_CHECK_VERSION(3,2,0)
3581 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3582 gtk_box_set_homogeneous(GTK_BOX(page), FALSE);
3583# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003584 page = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003585# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003586 gtk_widget_show(page);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003587 event_box = gtk_event_box_new();
3588 gtk_widget_show(event_box);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003589 label = gtk_label_new("-Empty-");
Bram Moolenaar98921892016-02-23 17:14:37 +01003590# if !GTK_CHECK_VERSION(3,14,0)
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003591 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01003592# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003593 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003594 gtk_widget_show(label);
3595 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline),
3596 page,
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003597 event_box,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003598 nr++);
3599 }
3600
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003601 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page);
Bram Moolenaar98921892016-02-23 17:14:37 +01003602# if GTK_CHECK_VERSION(3,0,0)
3603 g_object_set_data(G_OBJECT(event_box), "tab_num",
3604 GINT_TO_POINTER(tab_num));
3605# else
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003606 gtk_object_set_user_data(GTK_OBJECT(event_box),
3607 (gpointer)(long)tab_num);
Bram Moolenaar98921892016-02-23 17:14:37 +01003608# endif
3609# if GTK_CHECK_VERSION(3,0,0)
3610 label = gtk_bin_get_child(GTK_BIN(event_box));
3611# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003612 label = GTK_BIN(event_box)->child;
Bram Moolenaar98921892016-02-23 17:14:37 +01003613# endif
Bram Moolenaar57657d82006-04-21 22:12:41 +00003614 get_tabline_label(tp, FALSE);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003615 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003616 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003617 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003618
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003619 get_tabline_label(tp, TRUE);
3620 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar98921892016-02-23 17:14:37 +01003621# if GTK_CHECK_VERSION(3,0,0)
3622 gtk_widget_set_tooltip_text(event_box, (const gchar *)labeltext);
3623# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003624 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box,
3625 (const char *)labeltext, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01003626# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003627 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003628 }
3629
3630 /* Remove any old labels. */
3631 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL)
3632 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr);
3633
Bram Moolenaar98921892016-02-23 17:14:37 +01003634# if GTK_CHECK_VERSION(3,0,0)
3635 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
3636 gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
3637# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003638 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003639 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
Bram Moolenaar98921892016-02-23 17:14:37 +01003640# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003641
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003642 /* Make sure everything is in place before drawing text. */
3643 gui_mch_update();
3644
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003645 ignore_tabline_evt = FALSE;
3646}
3647
3648/*
3649 * Set the current tab to "nr". First tab is 1.
3650 */
3651 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003652gui_mch_set_curtab(int nr)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003653{
3654 if (gui.tabline == NULL)
3655 return;
3656
3657 ignore_tabline_evt = TRUE;
Bram Moolenaar98921892016-02-23 17:14:37 +01003658# if GTK_CHECK_VERSION(3,0,0)
3659 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
3660 gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
3661# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003662 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003663 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
Bram Moolenaar98921892016-02-23 17:14:37 +01003664# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003665 ignore_tabline_evt = FALSE;
3666}
3667
3668#endif /* FEAT_GUI_TABLINE */
3669
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670/*
Bram Moolenaara76638f2010-06-05 12:49:46 +02003671 * Add selection targets for PRIMARY and CLIPBOARD selections.
3672 */
3673 void
3674gui_gtk_set_selection_targets(void)
3675{
3676 int i, j = 0;
3677 int n_targets = N_SELECTION_TARGETS;
3678 GtkTargetEntry targets[N_SELECTION_TARGETS];
3679
3680 for (i = 0; i < (int)N_SELECTION_TARGETS; ++i)
3681 {
Bram Moolenaar29695702012-05-18 17:03:18 +02003682 /* OpenOffice tries to use TARGET_HTML and fails when we don't
Bram Moolenaara76638f2010-06-05 12:49:46 +02003683 * return something, instead of trying another target. Therefore only
3684 * offer TARGET_HTML when it works. */
3685 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3686 n_targets--;
3687 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003688 targets[j++] = selection_targets[i];
3689 }
3690
3691 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)GDK_SELECTION_PRIMARY);
3692 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)clip_plus.gtk_sel_atom);
3693 gtk_selection_add_targets(gui.drawarea,
3694 (GdkAtom)GDK_SELECTION_PRIMARY,
3695 targets, n_targets);
3696 gtk_selection_add_targets(gui.drawarea,
3697 (GdkAtom)clip_plus.gtk_sel_atom,
3698 targets, n_targets);
3699}
3700
3701/*
3702 * Set up for receiving DND items.
3703 */
3704 void
3705gui_gtk_set_dnd_targets(void)
3706{
3707 int i, j = 0;
3708 int n_targets = N_DND_TARGETS;
3709 GtkTargetEntry targets[N_DND_TARGETS];
3710
3711 for (i = 0; i < (int)N_DND_TARGETS; ++i)
3712 {
Bram Moolenaarb931d742011-10-26 11:36:25 +02003713 if (!clip_html && dnd_targets[i].info == TARGET_HTML)
Bram Moolenaara76638f2010-06-05 12:49:46 +02003714 n_targets--;
3715 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003716 targets[j++] = dnd_targets[i];
3717 }
3718
3719 gtk_drag_dest_unset(gui.drawarea);
3720 gtk_drag_dest_set(gui.drawarea,
3721 GTK_DEST_DEFAULT_ALL,
3722 targets, n_targets,
Bram Moolenaarba7cc9f2011-02-25 17:10:27 +01003723 GDK_ACTION_COPY | GDK_ACTION_MOVE);
Bram Moolenaara76638f2010-06-05 12:49:46 +02003724}
3725
3726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3728 * Returns OK for success, FAIL when the GUI can't be started.
3729 */
3730 int
3731gui_mch_init(void)
3732{
3733 GtkWidget *vbox;
3734
3735#ifdef FEAT_GUI_GNOME
3736 /* Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3737 * exits on failure, but that's a non-issue because we already called
3738 * gtk_init_check() in gui_mch_init_check(). */
3739 if (using_gnome)
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003740 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT,
3742 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL);
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003743# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
Bram Moolenaar1ff32c52014-04-29 15:11:43 +02003744 {
3745 char *p = setlocale(LC_NUMERIC, NULL);
3746
3747 /* Make sure strtod() uses a decimal point, not a comma. Gnome
3748 * init may change it. */
3749 if (p == NULL || strcmp(p, "C") != 0)
3750 setlocale(LC_NUMERIC, "C");
3751 }
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003752# endif
3753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754#endif
3755 vim_free(gui_argv);
3756 gui_argv = NULL;
3757
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003758#if GLIB_CHECK_VERSION(2,1,3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 /* Set the human-readable application name */
3760 g_set_application_name("Vim");
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 /*
3763 * Force UTF-8 output no matter what the value of 'encoding' is.
3764 * did_set_string_option() in option.c prohibits changing 'termencoding'
3765 * to something else than UTF-8 if the GUI is in use.
3766 */
3767 set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0);
3768
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003769#ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 gui_gtk_register_stock_icons();
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 /* FIXME: Need to install the classic icons and a gtkrc.classic file.
3773 * The hard part is deciding install locations and the Makefile magic. */
Bram Moolenaar98921892016-02-23 17:14:37 +01003774#if !GTK_CHECK_VERSION(3,0,0)
3775# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 gtk_rc_parse("gtkrc");
Bram Moolenaar98921892016-02-23 17:14:37 +01003777# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778#endif
3779
3780 /* Initialize values */
3781 gui.border_width = 2;
3782 gui.scrollbar_width = SB_DEFAULT_WIDTH;
3783 gui.scrollbar_height = SB_DEFAULT_WIDTH;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003784 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 gui.fgcolor = g_new0(GdkColor, 1);
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003786 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 gui.bgcolor = g_new0(GdkColor, 1);
Bram Moolenaarf36d3692005-03-15 22:48:14 +00003788 /* LINTED: avoid warning: conversion to 'unsigned long' */
3789 gui.spcolor = g_new0(GdkColor, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790
3791 /* Initialise atoms */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00003792 html_atom = gdk_atom_intern("text/html", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794
3795 /* Set default foreground and background colors. */
3796 gui.norm_pixel = gui.def_norm_pixel;
3797 gui.back_pixel = gui.def_back_pixel;
3798
3799 if (gtk_socket_id != 0)
3800 {
3801 GtkWidget *plug;
3802
3803 /* Use GtkSocket from another app. */
3804#ifdef HAVE_GTK_MULTIHEAD
3805 plug = gtk_plug_new_for_display(gdk_display_get_default(),
3806 gtk_socket_id);
3807#else
3808 plug = gtk_plug_new(gtk_socket_id);
3809#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01003810#if GTK_CHECK_VERSION(3,0,0)
3811 if (plug != NULL && gtk_plug_get_socket_window(GTK_PLUG(plug)) != NULL)
3812#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 if (plug != NULL && GTK_PLUG(plug)->socket_window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01003814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 {
3816 gui.mainwin = plug;
3817 }
3818 else
3819 {
3820 g_warning("Connection to GTK+ socket (ID %u) failed",
3821 (unsigned int)gtk_socket_id);
3822 /* Pretend we never wanted it if it failed (get own window) */
3823 gtk_socket_id = 0;
3824 }
3825 }
3826
3827 if (gtk_socket_id == 0)
3828 {
3829#ifdef FEAT_GUI_GNOME
3830 if (using_gnome)
3831 {
3832 gui.mainwin = gnome_app_new("Vim", NULL);
3833# ifdef USE_XSMP
3834 /* Use the GNOME save-yourself functionality now. */
3835 xsmp_close();
3836# endif
3837 }
3838 else
3839#endif
3840 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3841 }
3842
3843 gtk_widget_set_name(gui.mainwin, "vim-main-window");
3844
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 /* Create the PangoContext used for drawing all text. */
3846 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
3847 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848
Bram Moolenaar98921892016-02-23 17:14:37 +01003849#if GTK_CHECK_VERSION(3,0,0)
3850 gtk_container_set_border_width(GTK_CONTAINER(gui.mainwin), 0);
3851#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 gtk_container_border_width(GTK_CONTAINER(gui.mainwin), 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK);
3855
Bram Moolenaar98921892016-02-23 17:14:37 +01003856#if GTK_CHECK_VERSION(3,0,0)
3857 g_signal_connect(G_OBJECT(gui.mainwin), "delete-event",
3858 G_CALLBACK(&delete_event_cb), NULL);
3859
3860 g_signal_connect(G_OBJECT(gui.mainwin), "realize",
3861 G_CALLBACK(&mainwin_realize), NULL);
3862#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "delete_event",
3864 GTK_SIGNAL_FUNC(&delete_event_cb), NULL);
3865
3866 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "realize",
3867 GTK_SIGNAL_FUNC(&mainwin_realize), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01003868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869#ifdef HAVE_GTK_MULTIHEAD
3870 g_signal_connect(G_OBJECT(gui.mainwin), "screen_changed",
3871 G_CALLBACK(&mainwin_screen_changed_cb), NULL);
3872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 gui.accel_group = gtk_accel_group_new();
3874 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003876 /* A vertical box holds the menubar, toolbar and main text window. */
Bram Moolenaar98921892016-02-23 17:14:37 +01003877#if GTK_CHECK_VERSION(3,2,0)
3878 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3879 gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE);
3880#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 vbox = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883
3884#ifdef FEAT_GUI_GNOME
3885 if (using_gnome)
3886 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003887# if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 /* automagically restore menubar/toolbar placement */
3889 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE);
3890# endif
3891 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox);
3892 }
3893 else
3894#endif
3895 {
3896 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox);
3897 gtk_widget_show(vbox);
3898 }
3899
3900#ifdef FEAT_MENU
3901 /*
3902 * Create the menubar and handle
3903 */
3904 gui.menubar = gtk_menu_bar_new();
3905 gtk_widget_set_name(gui.menubar, "vim-menubar");
3906
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003907 /* Avoid that GTK takes <F10> away from us. */
3908 {
3909 GtkSettings *gtk_settings;
3910
3911 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
3912 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
3913 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003914
3915
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916# ifdef FEAT_GUI_GNOME
3917 if (using_gnome)
3918 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 BonoboDockItem *dockitem;
3920
3921 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar));
3922 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3923 GNOME_APP_MENUBAR_NAME);
Bram Moolenaardb552d602006-03-23 22:59:57 +00003924 /* We don't want the menu to float. */
3925 bonobo_dock_item_set_behavior(dockitem,
3926 bonobo_dock_item_get_behavior(dockitem)
3927 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 gui.menubar_h = GTK_WIDGET(dockitem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 }
3930 else
3931# endif /* FEAT_GUI_GNOME */
3932 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00003933 /* Always show the menubar, otherwise <F10> doesn't work. It may be
3934 * disabled in gui_init() later. */
3935 gtk_widget_show(gui.menubar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0);
3937 }
3938#endif /* FEAT_MENU */
3939
3940#ifdef FEAT_TOOLBAR
3941 /*
3942 * Create the toolbar and handle
3943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 /* some aesthetics on the toolbar */
Bram Moolenaar98921892016-02-23 17:14:37 +01003945# ifdef USE_GTK3
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01003946 /* TODO: Add GTK+ 3 code here using GtkCssProvider if necessary. */
Bram Moolenaar98921892016-02-23 17:14:37 +01003947 /* N.B. Since the default value of GtkToolbar::button-relief is
3948 * GTK_RELIEF_NONE, there's no need to specify that, probably. */
3949# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 gtk_rc_parse_string(
3951 "style \"vim-toolbar-style\" {\n"
3952 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
3953 "}\n"
3954 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
Bram Moolenaar98921892016-02-23 17:14:37 +01003955# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 gui.toolbar = gtk_toolbar_new();
3957 gtk_widget_set_name(gui.toolbar, "vim-toolbar");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
3959
3960# ifdef FEAT_GUI_GNOME
3961 if (using_gnome)
3962 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 BonoboDockItem *dockitem;
3964
3965 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar));
3966 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3967 GNOME_APP_TOOLBAR_NAME);
3968 gui.toolbar_h = GTK_WIDGET(dockitem);
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003969 /* When the toolbar is floating it gets stuck. So long as that isn't
Bram Moolenaardb552d602006-03-23 22:59:57 +00003970 * fixed let's disallow floating. */
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003971 bonobo_dock_item_set_behavior(dockitem,
Bram Moolenaardb552d602006-03-23 22:59:57 +00003972 bonobo_dock_item_get_behavior(dockitem)
3973 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 }
3976 else
3977# endif /* FEAT_GUI_GNOME */
3978 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 if (vim_strchr(p_go, GO_TOOLBAR) != NULL
3980 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3981 gtk_widget_show(gui.toolbar);
3982 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0);
3983 }
3984#endif /* FEAT_TOOLBAR */
3985
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003986#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003987 /*
3988 * Use a Notebook for the tab pages labels. The labels are hidden by
3989 * default.
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003990 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003991 gui.tabline = gtk_notebook_new();
3992 gtk_widget_show(gui.tabline);
3993 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0);
3994 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
3995 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003996 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01003997# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar96351572006-05-05 21:16:59 +00003998 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01003999# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004000
Bram Moolenaar98921892016-02-23 17:14:37 +01004001# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004002 tabline_tooltip = gtk_tooltips_new();
4003 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip));
Bram Moolenaar98921892016-02-23 17:14:37 +01004004# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004005
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004006 {
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004007 GtkWidget *page, *label, *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004008
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004009 /* Add the first tab. */
Bram Moolenaar98921892016-02-23 17:14:37 +01004010# if GTK_CHECK_VERSION(3,2,0)
4011 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
4012 gtk_box_set_homogeneous(GTK_BOX(page), FALSE);
4013# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004014 page = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01004015# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004016 gtk_widget_show(page);
4017 gtk_container_add(GTK_CONTAINER(gui.tabline), page);
4018 label = gtk_label_new("-Empty-");
4019 gtk_widget_show(label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004020 event_box = gtk_event_box_new();
4021 gtk_widget_show(event_box);
Bram Moolenaar98921892016-02-23 17:14:37 +01004022# if GTK_CHECK_VERSION(3,0,0)
4023 g_object_set_data(G_OBJECT(event_box), "tab_num", GINT_TO_POINTER(1L));
4024# else
Bram Moolenaar47b46d72008-07-02 19:05:48 +00004025 gtk_object_set_user_data(GTK_OBJECT(event_box), (gpointer)1L);
Bram Moolenaar98921892016-02-23 17:14:37 +01004026# endif
4027# if !GTK_CHECK_VERSION(3,14,0)
Bram Moolenaar8ea91232006-04-28 22:41:43 +00004028 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01004029# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004030 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004031 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004032 }
Bram Moolenaar54a709e2006-05-04 21:57:11 +00004033
Bram Moolenaar98921892016-02-23 17:14:37 +01004034# if GTK_CHECK_VERSION(3,0,0)
4035 g_signal_connect(G_OBJECT(gui.tabline), "switch-page",
4036 G_CALLBACK(on_select_tab), NULL);
4037# else
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004038 gtk_signal_connect(GTK_OBJECT(gui.tabline), "switch_page",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004039 GTK_SIGNAL_FUNC(on_select_tab), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004040# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004041
4042 /* Create a popup menu for the tab line and connect it. */
4043 tabline_menu = create_tabline_menu();
Bram Moolenaar98921892016-02-23 17:14:37 +01004044# if GTK_CHECK_VERSION(3,0,0)
4045 g_signal_connect_swapped(G_OBJECT(gui.tabline), "button-press-event",
4046 G_CALLBACK(on_tabline_menu), G_OBJECT(tabline_menu));
4047# else
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004048 gtk_signal_connect_object(GTK_OBJECT(gui.tabline), "button_press_event",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004049 GTK_SIGNAL_FUNC(on_tabline_menu), GTK_OBJECT(tabline_menu));
Bram Moolenaar98921892016-02-23 17:14:37 +01004050# endif
4051#endif /* FEAT_GUI_TABLINE */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004052
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 gui.formwin = gtk_form_new();
Bram Moolenaar98921892016-02-23 17:14:37 +01004054#if GTK_CHECK_VERSION(3,0,0)
4055 gtk_container_set_border_width(GTK_CONTAINER(gui.formwin), 0);
4056#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 gtk_container_border_width(GTK_CONTAINER(gui.formwin), 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01004058#endif
4059#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK);
Bram Moolenaar98921892016-02-23 17:14:37 +01004061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
4063 gui.drawarea = gtk_drawing_area_new();
Bram Moolenaar98921892016-02-23 17:14:37 +01004064#if GTK_CHECK_VERSION(3,0,0)
4065 gui.surface = NULL;
4066 gui.by_signal = FALSE;
4067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068
4069 /* Determine which events we will filter. */
4070 gtk_widget_set_events(gui.drawarea,
4071 GDK_EXPOSURE_MASK |
4072 GDK_ENTER_NOTIFY_MASK |
4073 GDK_LEAVE_NOTIFY_MASK |
4074 GDK_BUTTON_PRESS_MASK |
4075 GDK_BUTTON_RELEASE_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 GDK_SCROLL_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 GDK_KEY_PRESS_MASK |
4078 GDK_KEY_RELEASE_MASK |
4079 GDK_POINTER_MOTION_MASK |
4080 GDK_POINTER_MOTION_HINT_MASK);
4081
4082 gtk_widget_show(gui.drawarea);
4083 gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0);
4084 gtk_widget_show(gui.formwin);
4085 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0);
4086
4087 /* For GtkSockets, key-presses must go to the focus widget (drawarea)
4088 * and not the window. */
Bram Moolenaar98921892016-02-23 17:14:37 +01004089#if GTK_CHECK_VERSION(3,0,0)
4090 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
4091 : G_OBJECT(gui.drawarea),
4092 "key-press-event",
4093 G_CALLBACK(key_press_event), NULL);
4094#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 gtk_signal_connect((gtk_socket_id == 0) ? GTK_OBJECT(gui.mainwin)
4096 : GTK_OBJECT(gui.drawarea),
4097 "key_press_event",
4098 GTK_SIGNAL_FUNC(key_press_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004099#endif
4100#if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 /* Also forward key release events for the benefit of GTK+ 2 input
4102 * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */
4103 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
4104 : G_OBJECT(gui.drawarea),
Bram Moolenaar98921892016-02-23 17:14:37 +01004105 "key-release-event",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 G_CALLBACK(&key_release_event), NULL);
4107#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004108#if GTK_CHECK_VERSION(3,0,0)
4109 g_signal_connect(G_OBJECT(gui.drawarea), "realize",
4110 G_CALLBACK(drawarea_realize_cb), NULL);
4111 g_signal_connect(G_OBJECT(gui.drawarea), "unrealize",
4112 G_CALLBACK(drawarea_unrealize_cb), NULL);
4113 g_signal_connect(G_OBJECT(gui.drawarea), "configure-event",
4114 G_CALLBACK(drawarea_configure_event_cb), NULL);
4115 g_signal_connect_after(G_OBJECT(gui.drawarea), "style-set",
4116 G_CALLBACK(&drawarea_style_set_cb), NULL);
4117#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "realize",
4119 GTK_SIGNAL_FUNC(drawarea_realize_cb), NULL);
4120 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "unrealize",
4121 GTK_SIGNAL_FUNC(drawarea_unrealize_cb), NULL);
4122
4123 gtk_signal_connect_after(GTK_OBJECT(gui.drawarea), "style_set",
4124 GTK_SIGNAL_FUNC(&drawarea_style_set_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126
Bram Moolenaar98921892016-02-23 17:14:37 +01004127#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 gui.visibility = GDK_VISIBILITY_UNOBSCURED;
Bram Moolenaar98921892016-02-23 17:14:37 +01004129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130
4131#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
4132 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE);
4133 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE);
4134#endif
4135
4136 if (gtk_socket_id != 0)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004137 /* make sure keyboard input can go to the drawarea */
Bram Moolenaar98921892016-02-23 17:14:37 +01004138#if GTK_CHECK_VERSION(3,0,0)
4139 gtk_widget_set_can_focus(gui.drawarea, TRUE);
4140#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 GTK_WIDGET_SET_FLAGS(gui.drawarea, GTK_CAN_FOCUS);
Bram Moolenaar98921892016-02-23 17:14:37 +01004142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143
4144 /*
4145 * Set clipboard specific atoms
4146 */
4147 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY;
4150 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE);
4151
4152 /*
4153 * Start out by adding the configured border width into the border offset.
4154 */
4155 gui.border_offset = gui.border_width;
4156
Bram Moolenaar98921892016-02-23 17:14:37 +01004157#if GTK_CHECK_VERSION(3,0,0)
4158 g_signal_connect(G_OBJECT(gui.drawarea), "draw",
4159 G_CALLBACK(draw_event), NULL);
4160#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event",
4162 GTK_SIGNAL_FUNC(visibility_event), NULL);
4163 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event",
4164 GTK_SIGNAL_FUNC(expose_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166
4167 /*
4168 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
4169 * Only needed for some window managers.
4170 */
4171 if (vim_strchr(p_go, GO_POINTER) != NULL)
4172 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004173#if GTK_CHECK_VERSION(3,0,0)
4174 g_signal_connect(G_OBJECT(gui.drawarea), "leave-notify-event",
4175 G_CALLBACK(leave_notify_event), NULL);
4176 g_signal_connect(G_OBJECT(gui.drawarea), "enter-notify-event",
4177 G_CALLBACK(enter_notify_event), NULL);
4178#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "leave_notify_event",
4180 GTK_SIGNAL_FUNC(leave_notify_event), NULL);
4181 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "enter_notify_event",
4182 GTK_SIGNAL_FUNC(enter_notify_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004186 /* Real windows can get focus ... GtkPlug, being a mere container can't,
4187 * only its widgets. Arguably, this could be common code and we not use
4188 * the window focus at all, but let's be safe.
4189 */
4190 if (gtk_socket_id == 0)
4191 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004192#if GTK_CHECK_VERSION(3,0,0)
4193 g_signal_connect(G_OBJECT(gui.mainwin), "focus-out-event",
4194 G_CALLBACK(focus_out_event), NULL);
4195 g_signal_connect(G_OBJECT(gui.mainwin), "focus-in-event",
4196 G_CALLBACK(focus_in_event), NULL);
4197#else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004198 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_out_event",
4199 GTK_SIGNAL_FUNC(focus_out_event), NULL);
4200 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_in_event",
4201 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004202#endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004203 }
4204 else
4205 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004206#if GTK_CHECK_VERSION(3,0,0)
4207 g_signal_connect(G_OBJECT(gui.drawarea), "focus-out-event",
4208 G_CALLBACK(focus_out_event), NULL);
4209 g_signal_connect(G_OBJECT(gui.drawarea), "focus-in-event",
4210 G_CALLBACK(focus_in_event), NULL);
4211#else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004212 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_out_event",
4213 GTK_SIGNAL_FUNC(focus_out_event), NULL);
4214 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_in_event",
4215 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004216#endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004217#ifdef FEAT_GUI_TABLINE
Bram Moolenaar98921892016-02-23 17:14:37 +01004218# if GTK_CHECK_VERSION(3,0,0)
4219 g_signal_connect(G_OBJECT(gui.tabline), "focus-out-event",
4220 G_CALLBACK(focus_out_event), NULL);
4221 g_signal_connect(G_OBJECT(gui.tabline), "focus-in-event",
4222 G_CALLBACK(focus_in_event), NULL);
4223# else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004224 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_out_event",
4225 GTK_SIGNAL_FUNC(focus_out_event), NULL);
4226 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_in_event",
4227 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004228# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004229#endif /* FEAT_GUI_TABLINE */
4230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231
Bram Moolenaar98921892016-02-23 17:14:37 +01004232#if GTK_CHECK_VERSION(3,0,0)
4233 g_signal_connect(G_OBJECT(gui.drawarea), "motion-notify-event",
4234 G_CALLBACK(motion_notify_event), NULL);
4235 g_signal_connect(G_OBJECT(gui.drawarea), "button-press-event",
4236 G_CALLBACK(button_press_event), NULL);
4237 g_signal_connect(G_OBJECT(gui.drawarea), "button-release-event",
4238 G_CALLBACK(button_release_event), NULL);
4239 g_signal_connect(G_OBJECT(gui.drawarea), "scroll-event",
4240 G_CALLBACK(&scroll_event), NULL);
4241#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "motion_notify_event",
4243 GTK_SIGNAL_FUNC(motion_notify_event), NULL);
4244 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_press_event",
4245 GTK_SIGNAL_FUNC(button_press_event), NULL);
4246 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_release_event",
4247 GTK_SIGNAL_FUNC(button_release_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 g_signal_connect(G_OBJECT(gui.drawarea), "scroll_event",
4249 G_CALLBACK(&scroll_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
4252 /*
4253 * Add selection handler functions.
4254 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004255#if GTK_CHECK_VERSION(3,0,0)
4256 g_signal_connect(G_OBJECT(gui.drawarea), "selection-clear-event",
4257 G_CALLBACK(selection_clear_event), NULL);
4258 g_signal_connect(G_OBJECT(gui.drawarea), "selection-received",
4259 G_CALLBACK(selection_received_cb), NULL);
4260#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_clear_event",
4262 GTK_SIGNAL_FUNC(selection_clear_event), NULL);
4263 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_received",
4264 GTK_SIGNAL_FUNC(selection_received_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
Bram Moolenaara76638f2010-06-05 12:49:46 +02004267 gui_gtk_set_selection_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268
Bram Moolenaar98921892016-02-23 17:14:37 +01004269#if GTK_CHECK_VERSION(3,0,0)
4270 g_signal_connect(G_OBJECT(gui.drawarea), "selection-get",
4271 G_CALLBACK(selection_get_cb), NULL);
4272#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_get",
4274 GTK_SIGNAL_FUNC(selection_get_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276
4277 /* Pretend we don't have input focus, we will get an event if we do. */
4278 gui.in_focus = FALSE;
4279
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 return OK;
4281}
4282
4283#if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO)
4284/*
4285 * This is called from gui_start() after a fork() has been done.
4286 * We have to tell the session manager our new PID.
4287 */
4288 void
4289gui_mch_forked(void)
4290{
4291 if (using_gnome)
4292 {
4293 GnomeClient *client;
4294
4295 client = gnome_master_client();
4296
4297 if (client != NULL)
4298 gnome_client_set_process_id(client, getpid());
4299 }
4300}
4301#endif /* FEAT_GUI_GNOME && FEAT_SESSION */
4302
Bram Moolenaar98921892016-02-23 17:14:37 +01004303#if GTK_CHECK_VERSION(3,0,0)
4304 static void
4305gui_gtk_get_rgb_from_pixel(guint32 pixel, GdkRGBA *result)
4306{
4307 GdkVisual * const visual = gtk_widget_get_visual(gui.drawarea);
4308 guint32 r_mask, g_mask, b_mask;
4309 gint r_shift, g_shift, b_shift;
4310
4311 if (visual == NULL)
4312 {
4313 result->red = 0.0;
4314 result->green = 0.0;
4315 result->blue = 0.0;
4316 result->alpha = 0.0;
4317 return;
4318 }
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 result->red = ((pixel & r_mask) >> r_shift) / 255.0;
4325 result->green = ((pixel & g_mask) >> g_shift) / 255.0;
4326 result->blue = ((pixel & b_mask) >> b_shift) / 255.0;
4327 result->alpha = 1.0;
4328}
4329
4330/* Convert a GdRGBA into a pixel value using drawarea's visual */
4331 static guint32
4332gui_gtk_get_pixel_from_rgb(const GdkRGBA *rgba)
4333{
4334 GdkVisual * const visual = gtk_widget_get_visual(gui.drawarea);
4335 guint32 r_mask, g_mask, b_mask;
4336 gint r_shift, g_shift, b_shift;
4337 guint32 r, g, b;
4338
4339 if (visual == NULL)
4340 return 0;
4341
4342 gdk_visual_get_red_pixel_details(visual, &r_mask, &r_shift, NULL);
4343 gdk_visual_get_green_pixel_details(visual, &g_mask, &g_shift, NULL);
4344 gdk_visual_get_blue_pixel_details(visual, &b_mask, &b_shift, NULL);
4345
4346 r = rgba->red * 65535;
4347 g = rgba->green * 65535;
4348 b = rgba->blue * 65535;
4349
4350 return ((r << r_shift) & r_mask) |
4351 ((g << g_shift) & g_mask) |
4352 ((b << b_shift) & b_mask);
4353}
4354
4355 static void
4356set_cairo_source_rgb_from_pixel(cairo_t *cr, guint32 pixel)
4357{
4358 GdkRGBA result;
4359 gui_gtk_get_rgb_from_pixel(pixel, &result);
4360 cairo_set_source_rgb(cr, result.red, result.green, result.blue);
4361}
4362#endif /* GTK_CHECK_VERSION(3,0,0) */
4363
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364/*
4365 * Called when the foreground or background color has been changed.
4366 * This used to change the graphics contexts directly but we are
4367 * currently manipulating them where desired.
4368 */
4369 void
4370gui_mch_new_colors(void)
4371{
Bram Moolenaar98921892016-02-23 17:14:37 +01004372#if GTK_CHECK_VERSION(3,0,0)
4373 GdkWindow * const da_win = gtk_widget_get_window(gui.drawarea);
4374
4375 if (gui.drawarea != NULL && gtk_widget_get_window(gui.drawarea) != NULL)
4376#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01004378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004380#if GTK_CHECK_VERSION(3,4,0)
4381 GdkRGBA color;
4382
4383 gui_gtk_get_rgb_from_pixel(gui.back_pixel, &color);
4384 {
4385 cairo_pattern_t * const pat = cairo_pattern_create_rgba(
4386 color.red, color.green, color.blue, color.alpha);
4387 if (pat != NULL)
4388 {
4389 gdk_window_set_background_pattern(da_win, pat);
4390 cairo_pattern_destroy(pat);
4391 }
4392 else
4393 gdk_window_set_background_rgba(da_win, &color);
4394 }
4395#else /* !GTK_CHECK_VERSION(3,4,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 GdkColor color = { 0, 0, 0, 0 };
4397
4398 color.pixel = gui.back_pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01004399# if GTK_CHECK_VERSION(3,0,0)
4400 gdk_window_set_background(da_win, &color);
4401# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 gdk_window_set_background(gui.drawarea->window, &color);
Bram Moolenaar98921892016-02-23 17:14:37 +01004403# endif
4404#endif /* !GTK_CHECK_VERSION(3,4,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 }
4406}
4407
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408/*
4409 * This signal informs us about the need to rearrange our sub-widgets.
4410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004412form_configure_event(GtkWidget *widget UNUSED,
4413 GdkEventConfigure *event,
4414 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415{
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004416 int usable_height = event->height;
4417
4418 /* When in a GtkPlug, we can't guarantee valid heights (as a round
4419 * no. of char-heights), so we have to manually sanitise them.
4420 * Widths seem to sort themselves out, don't ask me why.
4421 */
4422 if (gtk_socket_id != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004423 usable_height -= (gui.char_height - (gui.char_height/2)); /* sic. */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004424
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 gtk_form_freeze(GTK_FORM(gui.formwin));
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004426 gui_resize_shell(event->width, usable_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 gtk_form_thaw(GTK_FORM(gui.formwin));
4428
4429 return TRUE;
4430}
4431
4432/*
4433 * Function called when window already closed.
4434 * We can't do much more here than to trying to preserve what had been done,
4435 * since the window is already inevitably going away.
4436 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004437#if GTK_CHECK_VERSION(3,0,0)
4438 static void
4439mainwin_destroy_cb(GObject *object UNUSED, gpointer data UNUSED)
4440#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004442mainwin_destroy_cb(GtkObject *object UNUSED, gpointer data UNUSED)
Bram Moolenaar98921892016-02-23 17:14:37 +01004443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444{
4445 /* Don't write messages to the GUI anymore */
4446 full_screen = FALSE;
4447
4448 gui.mainwin = NULL;
4449 gui.drawarea = NULL;
4450
4451 if (!exiting) /* only do anything if the destroy was unexpected */
4452 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004453 vim_strncpy(IObuff,
4454 (char_u *)_("Vim: Main window unexpectedly destroyed\n"),
4455 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 preserve_exit();
4457 }
Bram Moolenaar36e294c2015-12-29 18:55:46 +01004458#ifdef USE_GRESOURCE
4459 gui_gtk_unregister_resource();
4460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461}
4462
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004463
4464/*
4465 * Bit of a hack to ensure we start GtkPlug windows with the correct window
4466 * hints (and thus the required size from -geom), but that after that we
4467 * put the hints back to normal (the actual minimum size) so we may
4468 * subsequently be resized smaller. GtkSocket (the parent end) uses the
Bram Moolenaar49325942007-05-10 19:19:59 +00004469 * plug's window 'min hints to set *it's* minimum size, but that's also the
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004470 * only way we have of making ourselves bigger (by set lines/columns).
4471 * Thus set hints at start-up to ensure correct init. size, then a
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01004472 * second after the final attempt to reset the real minimum hints (done by
4473 * scrollbar init.), actually do the standard hints and stop the timer.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004474 * We'll not let the default hints be set while this timer's active.
4475 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004476 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004477check_startup_plug_hints(gpointer data UNUSED)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004478{
4479 if (init_window_hints_state == 1)
4480 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004481 /* Safe to use normal hints now */
4482 init_window_hints_state = 0;
4483 update_window_manager_hints(0, 0);
4484 return FALSE; /* stop timer */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004485 }
4486
4487 /* Keep on trying */
4488 init_window_hints_state = 1;
4489 return TRUE;
4490}
4491
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492/*
4493 * Open the GUI window which was created by a call to gui_mch_init().
4494 */
4495 int
4496gui_mch_open(void)
4497{
4498 guicolor_T fg_pixel = INVALCOLOR;
4499 guicolor_T bg_pixel = INVALCOLOR;
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004500 guint pixel_width;
4501 guint pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 /*
4504 * Allow setting a window role on the command line, or invent one
4505 * if none was specified. This is mainly useful for GNOME session
4506 * support; allowing the WM to restore window placement.
4507 */
4508 if (role_argument != NULL)
4509 {
4510 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument);
4511 }
4512 else
4513 {
4514 char *role;
4515
4516 /* Invent a unique-enough ID string for the role */
4517 role = g_strdup_printf("vim-%u-%u-%u",
4518 (unsigned)mch_get_pid(),
4519 (unsigned)g_random_int(),
4520 (unsigned)time(NULL));
4521
4522 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role);
4523 g_free(role);
4524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525
4526 if (gui_win_x != -1 && gui_win_y != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528
4529 /* Determine user specified geometry, if present. */
4530 if (gui.geom != NULL)
4531 {
4532 int mask;
4533 unsigned int w, h;
4534 int x = 0;
4535 int y = 0;
4536
4537 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
4538
4539 if (mask & WidthValue)
4540 Columns = w;
4541 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00004542 {
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004543 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00004544 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00004546 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004547 limit_screen_size();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004548
4549 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4550 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
4551
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004552 pixel_width += get_menu_tool_width();
4553 pixel_height += get_menu_tool_height();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004554
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 if (mask & (XValue | YValue))
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004556 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004557 int ww, hh;
4558 gui_mch_get_screen_dimensions(&ww, &hh);
4559 hh += p_ghr + get_menu_tool_height();
4560 ww += get_menu_tool_width();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004561 if (mask & XNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004562 x += ww - pixel_width;
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004563 if (mask & YNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004564 y += hh - pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 vim_free(gui.geom);
4568 gui.geom = NULL;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004569
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004570 /* From now until everyone's stopped trying to set the window hints
4571 * to their correct minimum values, stop them being set as we need
4572 * them to remain at our required size for the parent GtkSocket to
4573 * give us the right initial size.
4574 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004575 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004576 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004577 update_window_manager_hints(pixel_width, pixel_height);
4578 init_window_hints_state = 1;
4579 g_timeout_add(1000, check_startup_plug_hints, NULL);
4580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 }
4582
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004583 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4584 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004585 /* For GTK2 changing the size of the form widget doesn't cause window
4586 * resizing. */
Bram Moolenaardebe25a2010-06-06 17:41:24 +02004587 if (gtk_socket_id == 0)
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004588 gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004589 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590
4591 if (foreground_argument != NULL)
4592 fg_pixel = gui_get_color((char_u *)foreground_argument);
4593 if (fg_pixel == INVALCOLOR)
4594 fg_pixel = gui_get_color((char_u *)"Black");
4595
4596 if (background_argument != NULL)
4597 bg_pixel = gui_get_color((char_u *)background_argument);
4598 if (bg_pixel == INVALCOLOR)
4599 bg_pixel = gui_get_color((char_u *)"White");
4600
4601 if (found_reverse_arg)
4602 {
4603 gui.def_norm_pixel = bg_pixel;
4604 gui.def_back_pixel = fg_pixel;
4605 }
4606 else
4607 {
4608 gui.def_norm_pixel = fg_pixel;
4609 gui.def_back_pixel = bg_pixel;
4610 }
4611
4612 /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or
4613 * in a vimrc file) */
4614 set_normal_colors();
4615
4616 /* Check that none of the colors are the same as the background color */
4617 gui_check_colors();
4618
4619 /* Get the colors for the highlight groups (gui_check_colors() might have
4620 * changed them). */
4621 highlight_gui_started(); /* re-init colors and fonts */
4622
Bram Moolenaar98921892016-02-23 17:14:37 +01004623#if GTK_CHECK_VERSION(3,0,0)
4624 g_signal_connect(G_OBJECT(gui.mainwin), "destroy",
4625 G_CALLBACK(mainwin_destroy_cb), NULL);
4626#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "destroy",
4628 GTK_SIGNAL_FUNC(mainwin_destroy_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630
4631#ifdef FEAT_HANGULIN
4632 hangul_keyboard_set();
4633#endif
4634
4635 /*
4636 * Notify the fixed area about the need to resize the contents of the
4637 * gui.formwin, which we use for random positioning of the included
4638 * components.
4639 *
4640 * We connect this signal deferred finally after anything is in place,
4641 * since this is intended to handle resizements coming from the window
4642 * manager upon us and should not interfere with what VIM is requesting
4643 * upon startup.
4644 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004645#if GTK_CHECK_VERSION(3,0,0)
4646 g_signal_connect(G_OBJECT(gui.formwin), "configure-event",
4647 G_CALLBACK(form_configure_event), NULL);
4648#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 gtk_signal_connect(GTK_OBJECT(gui.formwin), "configure_event",
4650 GTK_SIGNAL_FUNC(form_configure_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652
4653#ifdef FEAT_DND
Bram Moolenaara76638f2010-06-05 12:49:46 +02004654 /* Set up for receiving DND items. */
4655 gui_gtk_set_dnd_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656
Bram Moolenaar98921892016-02-23 17:14:37 +01004657# if GTK_CHECK_VERSION(3,0,0)
4658 g_signal_connect(G_OBJECT(gui.drawarea), "drag-data-received",
4659 G_CALLBACK(drag_data_received_cb), NULL);
4660# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "drag_data_received",
4662 GTK_SIGNAL_FUNC(drag_data_received_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004663# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664#endif
4665
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 /* With GTK+ 2, we need to iconify the window before calling show()
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004667 * to avoid mapping the window for a short time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 if (found_iconic_arg && gtk_socket_id == 0)
4669 gui_mch_iconify();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670
4671 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004672#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 unsigned long menu_handler = 0;
4674# ifdef FEAT_TOOLBAR
4675 unsigned long tool_handler = 0;
4676# endif
4677 /*
4678 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
4679 * show when restoring the saved layout configuration. We can't just
4680 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
4681 * a toplevel window and thus will be realized immediately. Instead,
4682 * connect signal handlers to hide the widgets just after they've been
4683 * marked visible, but before the main window is realized.
4684 */
4685 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL)
4686 menu_handler = g_signal_connect_after(gui.menubar_h, "show",
4687 G_CALLBACK(&gtk_widget_hide),
4688 NULL);
4689# ifdef FEAT_TOOLBAR
4690 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL
4691 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
4692 tool_handler = g_signal_connect_after(gui.toolbar_h, "show",
4693 G_CALLBACK(&gtk_widget_hide),
4694 NULL);
4695# endif
4696#endif
4697 gtk_widget_show(gui.mainwin);
4698
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004699#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 if (menu_handler != 0)
4701 g_signal_handler_disconnect(gui.menubar_h, menu_handler);
4702# ifdef FEAT_TOOLBAR
4703 if (tool_handler != 0)
4704 g_signal_handler_disconnect(gui.toolbar_h, tool_handler);
4705# endif
4706#endif
4707 }
4708
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 return OK;
4710}
4711
4712
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004714gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715{
4716 if (gui.mainwin != NULL)
4717 gtk_widget_destroy(gui.mainwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718}
4719
4720/*
4721 * Get the position of the top left corner of the window.
4722 */
4723 int
4724gui_mch_get_winpos(int *x, int *y)
4725{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 return OK;
4728}
4729
4730/*
4731 * Set the position of the top left corner of the window to the given
4732 * coordinates.
4733 */
4734 void
4735gui_mch_set_winpos(int x, int y)
4736{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738}
4739
Bram Moolenaar98921892016-02-23 17:14:37 +01004740#if !GTK_CHECK_VERSION(3,0,0)
4741# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742static int resize_idle_installed = FALSE;
4743/*
4744 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
4745 * the shell size doesn't exceed the window size, i.e. if the window manager
4746 * ignored our size request. Usually this happens if the window is maximized.
4747 *
4748 * FIXME: It'd be nice if we could find a little more orthodox solution.
4749 * See also the remark below in gui_mch_set_shellsize().
4750 *
4751 * DISABLED: When doing ":set lines+=1" this function would first invoke
4752 * gui_resize_shell() with the old size, then the normal callback would
4753 * report the new size through form_configure_event(). That caused the window
4754 * layout to be messed up.
4755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 static gboolean
4757force_shell_resize_idle(gpointer data)
4758{
4759 if (gui.mainwin != NULL
4760 && GTK_WIDGET_REALIZED(gui.mainwin)
4761 && GTK_WIDGET_VISIBLE(gui.mainwin))
4762 {
4763 int width;
4764 int height;
4765
4766 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height);
4767
4768 width -= get_menu_tool_width();
4769 height -= get_menu_tool_height();
4770
4771 gui_resize_shell(width, height);
4772 }
4773
4774 resize_idle_installed = FALSE;
4775 return FALSE; /* don't call me again */
4776}
Bram Moolenaar98921892016-02-23 17:14:37 +01004777# endif
4778#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779
Bram Moolenaar09736232009-09-23 16:14:49 +00004780/*
4781 * Return TRUE if the main window is maximized.
4782 */
4783 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004784gui_mch_maximized(void)
Bram Moolenaar09736232009-09-23 16:14:49 +00004785{
Bram Moolenaar98921892016-02-23 17:14:37 +01004786#if GTK_CHECK_VERSION(3,0,0)
4787 return (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL
4788 && (gdk_window_get_state(gtk_widget_get_window(gui.mainwin))
4789 & GDK_WINDOW_STATE_MAXIMIZED));
4790#else
Bram Moolenaar09736232009-09-23 16:14:49 +00004791 return (gui.mainwin != NULL && gui.mainwin->window != NULL
4792 && (gdk_window_get_state(gui.mainwin->window)
4793 & GDK_WINDOW_STATE_MAXIMIZED));
Bram Moolenaar98921892016-02-23 17:14:37 +01004794#endif
Bram Moolenaar09736232009-09-23 16:14:49 +00004795}
4796
4797/*
4798 * Unmaximize the main window
4799 */
4800 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004801gui_mch_unmaximize(void)
Bram Moolenaar09736232009-09-23 16:14:49 +00004802{
4803 if (gui.mainwin != NULL)
4804 gtk_window_unmaximize(GTK_WINDOW(gui.mainwin));
4805}
Bram Moolenaar09736232009-09-23 16:14:49 +00004806
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807/*
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004808 * Called when the font changed while the window is maximized. Compute the
4809 * new Rows and Columns. This is like resizing the window.
4810 */
4811 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004812gui_mch_newfont(void)
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004813{
4814 int w, h;
4815
4816 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
4817 w -= get_menu_tool_width();
4818 h -= get_menu_tool_height();
4819 gui_resize_shell(w, h);
4820}
4821
4822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 * Set the windows size.
4824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 void
4826gui_mch_set_shellsize(int width, int height,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004827 int min_width UNUSED, int min_height UNUSED,
4828 int base_width UNUSED, int base_height UNUSED,
4829 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 /* give GTK+ a chance to put all widget's into place */
4832 gui_mch_update();
4833
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004834 /* this will cause the proper resizement to happen too */
4835 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004836 update_window_manager_hints(0, 0);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004837
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 /* With GTK+ 2, changing the size of the form widget doesn't resize
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004839 * the window. So let's do it the other way around and resize the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 * main window instead. */
4841 width += get_menu_tool_width();
4842 height += get_menu_tool_height();
4843
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004844 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004845 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004846 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004847 update_window_manager_hints(width, height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848
Bram Moolenaar98921892016-02-23 17:14:37 +01004849# if !GTK_CHECK_VERSION(3,0,0)
4850# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 if (!resize_idle_installed)
4852 {
4853 g_idle_add_full(GDK_PRIORITY_EVENTS + 10,
4854 &force_shell_resize_idle, NULL, NULL);
4855 resize_idle_installed = TRUE;
4856 }
Bram Moolenaar98921892016-02-23 17:14:37 +01004857# endif
4858# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 /*
4860 * Wait until all events are processed to prevent a crash because the
4861 * real size of the drawing area doesn't reflect Vim's internal ideas.
4862 *
4863 * This is a bit of a hack, since Vim is a terminal application with a GUI
4864 * on top, while the GUI expects to be the boss.
4865 */
4866 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867}
4868
4869
4870/*
4871 * The screen size is used to make sure the initial window doesn't get bigger
4872 * than the screen. This subtracts some room for menubar, toolbar and window
4873 * decorations.
4874 */
4875 void
4876gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
4877{
4878#ifdef HAVE_GTK_MULTIHEAD
4879 GdkScreen* screen;
4880
4881 if (gui.mainwin != NULL && gtk_widget_has_screen(gui.mainwin))
4882 screen = gtk_widget_get_screen(gui.mainwin);
4883 else
4884 screen = gdk_screen_get_default();
4885
4886 *screen_w = gdk_screen_get_width(screen);
4887 *screen_h = gdk_screen_get_height(screen) - p_ghr;
4888#else
4889 *screen_w = gdk_screen_width();
4890 /* Subtract 'guiheadroom' from the height to allow some room for the
4891 * window manager (task list and window title bar). */
4892 *screen_h = gdk_screen_height() - p_ghr;
4893#endif
4894
4895 /*
4896 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
4897 * the toolbar and menubar for GTK, we subtract them from the screen
Bram Moolenaar64404472010-06-26 06:24:45 +02004898 * height, so that the window size can be made to fit on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 * This should be completely changed later.
4900 */
4901 *screen_w -= get_menu_tool_width();
4902 *screen_h -= get_menu_tool_height();
4903}
4904
4905#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004907gui_mch_settitle(char_u *title, char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 if (title != NULL && output_conv.vc_type != CONV_NONE)
4910 title = string_convert(&output_conv, title, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911
4912 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
4913
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 if (output_conv.vc_type != CONV_NONE)
4915 vim_free(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916}
4917#endif /* FEAT_TITLE */
4918
4919#if defined(FEAT_MENU) || defined(PROTO)
4920 void
4921gui_mch_enable_menu(int showit)
4922{
4923 GtkWidget *widget;
4924
4925# ifdef FEAT_GUI_GNOME
4926 if (using_gnome)
4927 widget = gui.menubar_h;
4928 else
4929# endif
4930 widget = gui.menubar;
4931
Bram Moolenaar18144c82006-04-12 21:52:12 +00004932 /* Do not disable the menu while starting up, otherwise F10 doesn't work. */
Bram Moolenaar98921892016-02-23 17:14:37 +01004933# if GTK_CHECK_VERSION(3,0,0)
4934 if (!showit != !gtk_widget_get_visible(widget) && !gui.starting)
4935# else
Bram Moolenaar18144c82006-04-12 21:52:12 +00004936 if (!showit != !GTK_WIDGET_VISIBLE(widget) && !gui.starting)
Bram Moolenaar98921892016-02-23 17:14:37 +01004937# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 {
4939 if (showit)
4940 gtk_widget_show(widget);
4941 else
4942 gtk_widget_hide(widget);
4943
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004944 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
4946}
4947#endif /* FEAT_MENU */
4948
4949#if defined(FEAT_TOOLBAR) || defined(PROTO)
4950 void
4951gui_mch_show_toolbar(int showit)
4952{
4953 GtkWidget *widget;
4954
4955 if (gui.toolbar == NULL)
4956 return;
4957
4958# ifdef FEAT_GUI_GNOME
4959 if (using_gnome)
4960 widget = gui.toolbar_h;
4961 else
4962# endif
4963 widget = gui.toolbar;
4964
4965 if (showit)
4966 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
4967
Bram Moolenaar98921892016-02-23 17:14:37 +01004968# if GTK_CHECK_VERSION(3,0,0)
4969 if (!showit != !gtk_widget_get_visible(widget))
4970# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 if (!showit != !GTK_WIDGET_VISIBLE(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01004972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 {
4974 if (showit)
4975 gtk_widget_show(widget);
4976 else
4977 gtk_widget_hide(widget);
4978
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004979 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 }
4981}
4982#endif /* FEAT_TOOLBAR */
4983
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984/*
4985 * Check if a given font is a CJK font. This is done in a very crude manner. It
4986 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
4987 * font. Consequently, this function cannot be used as a general purpose check
4988 * for CJK-ness for which fontconfig APIs should be used. This is only used by
4989 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
4990 */
4991 static int
4992is_cjk_font(PangoFontDescription *font_desc)
4993{
4994 static const char * const cjk_langs[] =
4995 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
4996
4997 PangoFont *font;
4998 unsigned i;
4999 int is_cjk = FALSE;
5000
5001 font = pango_context_load_font(gui.text_context, font_desc);
5002
5003 if (font == NULL)
5004 return FALSE;
5005
5006 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i)
5007 {
5008 PangoCoverage *coverage;
5009 gunichar uc;
5010
5011 coverage = pango_font_get_coverage(
5012 font, pango_language_from_string(cjk_langs[i]));
5013
5014 if (coverage != NULL)
5015 {
5016 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00;
5017 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT);
5018 pango_coverage_unref(coverage);
5019 }
5020 }
5021
5022 g_object_unref(font);
5023
5024 return is_cjk;
5025}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026
Bram Moolenaar231334e2005-07-25 20:46:57 +00005027/*
5028 * Adjust gui.char_height (after 'linespace' was changed).
5029 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 int
Bram Moolenaar231334e2005-07-25 20:46:57 +00005031gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 PangoFontMetrics *metrics;
5034 int ascent;
5035 int descent;
5036
5037 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font,
5038 pango_context_get_language(gui.text_context));
5039 ascent = pango_font_metrics_get_ascent(metrics);
5040 descent = pango_font_metrics_get_descent(metrics);
5041
5042 pango_font_metrics_unref(metrics);
5043
5044 gui.char_height = (ascent + descent + PANGO_SCALE - 1) / PANGO_SCALE
Bram Moolenaar231334e2005-07-25 20:46:57 +00005045 + p_linespace;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005046 /* LINTED: avoid warning: bitwise operation on signed value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2);
5048
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 /* A not-positive value of char_height may crash Vim. Only happens
5050 * if 'linespace' is negative (which does make sense sometimes). */
5051 gui.char_ascent = MAX(gui.char_ascent, 0);
5052 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
5053
5054 return OK;
5055}
5056
Bram Moolenaar98921892016-02-23 17:14:37 +01005057#if GTK_CHECK_VERSION(3,0,0)
5058/* Callback function used in gui_mch_font_dialog() */
5059 static gboolean
5060font_filter(const PangoFontFamily *family,
5061 const PangoFontFace *face UNUSED,
5062 gpointer data UNUSED)
5063{
5064 return pango_font_family_is_monospace((PangoFontFamily *)family);
5065}
5066#endif
5067
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068/*
5069 * Put up a font dialog and return the selected font name in allocated memory.
5070 * "oldval" is the previous value. Return NULL when cancelled.
5071 * This should probably go into gui_gtk.c. Hmm.
5072 * FIXME:
5073 * The GTK2 font selection dialog has no filtering API. So we could either
5074 * a) implement our own (possibly copying the code from somewhere else) or
5075 * b) just live with it.
5076 */
5077 char_u *
5078gui_mch_font_dialog(char_u *oldval)
5079{
5080 GtkWidget *dialog;
5081 int response;
5082 char_u *fontname = NULL;
5083 char_u *oldname;
5084
Bram Moolenaar98921892016-02-23 17:14:37 +01005085#if GTK_CHECK_VERSION(3,2,0)
5086 dialog = gtk_font_chooser_dialog_new(NULL, NULL);
5087 gtk_font_chooser_set_filter_func(GTK_FONT_CHOOSER(dialog), font_filter,
5088 NULL, NULL);
5089#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 dialog = gtk_font_selection_dialog_new(NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01005091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092
5093 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
5094 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
5095
5096 if (oldval != NULL && oldval[0] != NUL)
5097 {
5098 if (output_conv.vc_type != CONV_NONE)
5099 oldname = string_convert(&output_conv, oldval, NULL);
5100 else
5101 oldname = oldval;
5102
5103 /* Annoying bug in GTK (or Pango): if the font name does not include a
5104 * size, zero is used. Use default point size ten. */
5105 if (!vim_isdigit(oldname[STRLEN(oldname) - 1]))
5106 {
5107 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3);
5108
5109 if (p != NULL)
5110 {
5111 STRCPY(p + STRLEN(p), " 10");
5112 if (oldname != oldval)
5113 vim_free(oldname);
5114 oldname = p;
5115 }
5116 }
5117
Bram Moolenaar98921892016-02-23 17:14:37 +01005118#if GTK_CHECK_VERSION(3,2,0)
5119 gtk_font_chooser_set_font(
5120 GTK_FONT_CHOOSER(dialog), (const gchar *)oldname);
5121#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 gtk_font_selection_dialog_set_font_name(
5123 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname);
Bram Moolenaar98921892016-02-23 17:14:37 +01005124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125
5126 if (oldname != oldval)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127 vim_free(oldname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 }
Bram Moolenaarbef9d832009-09-11 13:46:41 +00005129 else
Bram Moolenaar98921892016-02-23 17:14:37 +01005130#if GTK_CHECK_VERSION(3,2,0)
5131 gtk_font_chooser_set_font(
5132 GTK_FONT_CHOOSER(dialog), DEFAULT_FONT);
5133#else
Bram Moolenaarbef9d832009-09-11 13:46:41 +00005134 gtk_font_selection_dialog_set_font_name(
5135 GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT);
Bram Moolenaar98921892016-02-23 17:14:37 +01005136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137
5138 response = gtk_dialog_run(GTK_DIALOG(dialog));
5139
5140 if (response == GTK_RESPONSE_OK)
5141 {
5142 char *name;
5143
Bram Moolenaar98921892016-02-23 17:14:37 +01005144#if GTK_CHECK_VERSION(3,2,0)
5145 name = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(dialog));
5146#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 name = gtk_font_selection_dialog_get_font_name(
5148 GTK_FONT_SELECTION_DIALOG(dialog));
Bram Moolenaar98921892016-02-23 17:14:37 +01005149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 if (name != NULL)
5151 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005152 char_u *p;
5153
5154 /* Apparently some font names include a comma, need to escape
5155 * that, because in 'guifont' it separates names. */
5156 p = vim_strsave_escaped((char_u *)name, (char_u *)",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 g_free(name);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005158 if (p != NULL && input_conv.vc_type != CONV_NONE)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005159 {
5160 fontname = string_convert(&input_conv, p, NULL);
5161 vim_free(p);
5162 }
5163 else
5164 fontname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 }
5166 }
5167
5168 if (response != GTK_RESPONSE_NONE)
5169 gtk_widget_destroy(dialog);
5170
5171 return fontname;
5172}
5173
5174/*
5175 * Some monospace fonts don't support a bold weight, and fall back
5176 * silently to the regular weight. But this is no good since our text
5177 * drawing function can emulate bold by overstriking. So let's try
5178 * to detect whether bold weight is actually available and emulate it
5179 * otherwise.
5180 *
5181 * Note that we don't need to check for italic style since Xft can
5182 * emulate italic on its own, provided you have a proper fontconfig
5183 * setup. We wouldn't be able to emulate it in Vim anyway.
5184 */
5185 static void
5186get_styled_font_variants(void)
5187{
5188 PangoFontDescription *bold_font_desc;
5189 PangoFont *plain_font;
5190 PangoFont *bold_font;
5191
5192 gui.font_can_bold = FALSE;
5193
5194 plain_font = pango_context_load_font(gui.text_context, gui.norm_font);
5195
5196 if (plain_font == NULL)
5197 return;
5198
5199 bold_font_desc = pango_font_description_copy_static(gui.norm_font);
5200 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD);
5201
5202 bold_font = pango_context_load_font(gui.text_context, bold_font_desc);
5203 /*
5204 * The comparison relies on the unique handle nature of a PangoFont*,
5205 * i.e. it's assumed that a different PangoFont* won't refer to the
5206 * same font. Seems to work, and failing here isn't critical anyway.
5207 */
5208 if (bold_font != NULL)
5209 {
5210 gui.font_can_bold = (bold_font != plain_font);
5211 g_object_unref(bold_font);
5212 }
5213
5214 pango_font_description_free(bold_font_desc);
5215 g_object_unref(plain_font);
5216}
5217
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218static PangoEngineShape *default_shape_engine = NULL;
5219
5220/*
5221 * Create a map from ASCII characters in the range [32,126] to glyphs
5222 * of the current font. This is used by gui_gtk2_draw_string() to skip
5223 * the itemize and shaping process for the most common case.
5224 */
5225 static void
5226ascii_glyph_table_init(void)
5227{
5228 char_u ascii_chars[128];
5229 PangoAttrList *attr_list;
5230 GList *item_list;
5231 int i;
5232
5233 if (gui.ascii_glyphs != NULL)
5234 pango_glyph_string_free(gui.ascii_glyphs);
5235 if (gui.ascii_font != NULL)
5236 g_object_unref(gui.ascii_font);
5237
5238 gui.ascii_glyphs = NULL;
5239 gui.ascii_font = NULL;
5240
5241 /* For safety, fill in question marks for the control characters. */
5242 for (i = 0; i < 32; ++i)
5243 ascii_chars[i] = '?';
5244 for (; i < 127; ++i)
5245 ascii_chars[i] = i;
5246 ascii_chars[i] = '?';
5247
5248 attr_list = pango_attr_list_new();
5249 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars,
5250 0, sizeof(ascii_chars), attr_list, NULL);
5251
5252 if (item_list != NULL && item_list->next == NULL) /* play safe */
5253 {
5254 PangoItem *item;
5255 int width;
5256
5257 item = (PangoItem *)item_list->data;
5258 width = gui.char_width * PANGO_SCALE;
5259
5260 /* Remember the shape engine used for ASCII. */
5261 default_shape_engine = item->analysis.shape_engine;
5262
5263 gui.ascii_font = item->analysis.font;
5264 g_object_ref(gui.ascii_font);
5265
5266 gui.ascii_glyphs = pango_glyph_string_new();
5267
5268 pango_shape((const char *)ascii_chars, sizeof(ascii_chars),
5269 &item->analysis, gui.ascii_glyphs);
5270
5271 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars));
5272
5273 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i)
5274 {
5275 PangoGlyphGeometry *geom;
5276
5277 geom = &gui.ascii_glyphs->glyphs[i].geometry;
5278 geom->x_offset += MAX(0, width - geom->width) / 2;
5279 geom->width = width;
5280 }
5281 }
5282
5283 g_list_foreach(item_list, (GFunc)&pango_item_free, NULL);
5284 g_list_free(item_list);
5285 pango_attr_list_unref(attr_list);
5286}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287
5288/*
5289 * Initialize Vim to use the font or fontset with the given name.
5290 * Return FAIL if the font could not be loaded, OK otherwise.
5291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 int
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005293gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 PangoFontDescription *font_desc;
5296 PangoLayout *layout;
5297 int width;
5298
5299 /* If font_name is NULL, this means to use the default, which should
5300 * be present on all proper Pango/fontconfig installations. */
5301 if (font_name == NULL)
5302 font_name = (char_u *)DEFAULT_FONT;
5303
5304 font_desc = gui_mch_get_font(font_name, FALSE);
5305
5306 if (font_desc == NULL)
5307 return FAIL;
5308
5309 gui_mch_free_font(gui.norm_font);
5310 gui.norm_font = font_desc;
5311
5312 pango_context_set_font_description(gui.text_context, font_desc);
5313
5314 layout = pango_layout_new(gui.text_context);
5315 pango_layout_set_text(layout, "MW", 2);
5316 pango_layout_get_size(layout, &width, NULL);
5317 /*
5318 * Set char_width to half the width obtained from pango_layout_get_size()
5319 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
5320 * Pango to use the same width for both non-CJK characters (e.g. Latin
5321 * letters and numbers) and CJK characters. This results in 's p a c e d
5322 * o u t' rendering when a CJK 'fixed width' font is used. To work around
5323 * that, divide the width returned by Pango by 2 if cjk_width is equal to
5324 * width for CJK fonts.
5325 *
5326 * For related bugs, see:
5327 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
5328 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
5329 *
5330 * With this, for all four of the following cases, Vim works fine:
5331 * guifont=CJK_fixed_width_font
5332 * guifont=Non_CJK_fixed_font
5333 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
5334 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
5335 */
5336 if (is_cjk_font(gui.norm_font))
5337 {
5338 int cjk_width;
5339
5340 /* Measure the text extent of U+4E00 and U+4E8C */
5341 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1);
5342 pango_layout_get_size(layout, &cjk_width, NULL);
5343
5344 if (width == cjk_width) /* Xft not patched */
5345 width /= 2;
5346 }
5347 g_object_unref(layout);
5348
5349 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE;
5350
5351 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
5352 if (gui.char_width <= 0)
5353 gui.char_width = 8;
5354
Bram Moolenaar231334e2005-07-25 20:46:57 +00005355 gui_mch_adjust_charheight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356
5357 /* Set the fontname, which will be used for information purposes */
5358 hl_set_font_name(font_name);
5359
5360 get_styled_font_variants();
5361 ascii_glyph_table_init();
5362
5363 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
5364 if (gui.wide_font != NULL
5365 && pango_font_description_equal(gui.norm_font, gui.wide_font))
5366 {
5367 pango_font_description_free(gui.wide_font);
5368 gui.wide_font = NULL;
5369 }
5370
Bram Moolenaare161c792009-11-03 17:13:59 +00005371 if (gui_mch_maximized())
5372 {
Bram Moolenaare161c792009-11-03 17:13:59 +00005373 /* Update lines and columns in accordance with the new font, keep the
5374 * window maximized. */
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02005375 gui_mch_newfont();
Bram Moolenaare161c792009-11-03 17:13:59 +00005376 }
5377 else
Bram Moolenaare161c792009-11-03 17:13:59 +00005378 {
5379 /* Preserve the logical dimensions of the screen. */
5380 update_window_manager_hints(0, 0);
5381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
5383 return OK;
5384}
5385
5386/*
5387 * Get a reference to the font "name".
5388 * Return zero for failure.
5389 */
5390 GuiFont
5391gui_mch_get_font(char_u *name, int report_error)
5392{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 PangoFontDescription *font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394
5395 /* can't do this when GUI is not running */
5396 if (!gui.in_use || name == NULL)
5397 return NULL;
5398
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 if (output_conv.vc_type != CONV_NONE)
5400 {
5401 char_u *buf;
5402
5403 buf = string_convert(&output_conv, name, NULL);
5404 if (buf != NULL)
5405 {
5406 font = pango_font_description_from_string((const char *)buf);
5407 vim_free(buf);
5408 }
5409 else
5410 font = NULL;
5411 }
5412 else
5413 font = pango_font_description_from_string((const char *)name);
5414
5415 if (font != NULL)
5416 {
5417 PangoFont *real_font;
5418
5419 /* pango_context_load_font() bails out if no font size is set */
5420 if (pango_font_description_get_size(font) <= 0)
5421 pango_font_description_set_size(font, 10 * PANGO_SCALE);
5422
5423 real_font = pango_context_load_font(gui.text_context, font);
5424
5425 if (real_font == NULL)
5426 {
5427 pango_font_description_free(font);
5428 font = NULL;
5429 }
5430 else
5431 g_object_unref(real_font);
5432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433
5434 if (font == NULL)
5435 {
5436 if (report_error)
Bram Moolenaarc93e7912008-07-08 10:46:08 +00005437 EMSG2(_((char *)e_font), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 return NULL;
5439 }
5440
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 return font;
5442}
5443
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005444#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005445/*
5446 * Return the name of font "font" in allocated memory.
5447 */
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005448 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005449gui_mch_get_fontname(GuiFont font, char_u *name UNUSED)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005450{
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005451 if (font != NOFONT)
5452 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005453 char *pangoname = pango_font_description_to_string(font);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005454
Bram Moolenaar89d40322006-08-29 15:30:07 +00005455 if (pangoname != NULL)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005456 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005457 char_u *s = vim_strsave((char_u *)pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005458
Bram Moolenaar89d40322006-08-29 15:30:07 +00005459 g_free(pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005460 return s;
5461 }
5462 }
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005463 return NULL;
5464}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005465#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005466
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467/*
5468 * If a font is not going to be used, free its structure.
5469 */
5470 void
5471gui_mch_free_font(GuiFont font)
5472{
5473 if (font != NOFONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 pango_font_description_free(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475}
5476
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477/*
5478 * Return the Pixel value (color) for the given color name. This routine was
5479 * pretty much taken from example code in the Silicon Graphics OSF/Motif
5480 * Programmer's Guide.
5481 * Return INVALCOLOR for error.
5482 */
5483 guicolor_T
5484gui_mch_get_color(char_u *name)
5485{
5486 /* A number of colors that some X11 systems don't have */
5487 static const char *const vimnames[][2] =
5488 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00005489 {"LightRed", "#FFBBBB"},
5490 {"LightGreen", "#88FF88"},
5491 {"LightMagenta","#FFBBFF"},
5492 {"DarkCyan", "#008888"},
5493 {"DarkBlue", "#0000BB"},
5494 {"DarkRed", "#BB0000"},
5495 {"DarkMagenta", "#BB00BB"},
5496 {"DarkGrey", "#BBBBBB"},
5497 {"DarkYellow", "#BBBB00"},
5498 {"Gray10", "#1A1A1A"},
5499 {"Grey10", "#1A1A1A"},
5500 {"Gray20", "#333333"},
5501 {"Grey20", "#333333"},
5502 {"Gray30", "#4D4D4D"},
5503 {"Grey30", "#4D4D4D"},
5504 {"Gray40", "#666666"},
5505 {"Grey40", "#666666"},
5506 {"Gray50", "#7F7F7F"},
5507 {"Grey50", "#7F7F7F"},
5508 {"Gray60", "#999999"},
5509 {"Grey60", "#999999"},
5510 {"Gray70", "#B3B3B3"},
5511 {"Grey70", "#B3B3B3"},
5512 {"Gray80", "#CCCCCC"},
5513 {"Grey80", "#CCCCCC"},
5514 {"Gray90", "#E5E5E5"},
5515 {"Grey90", "#E5E5E5"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 {NULL, NULL}
5517 };
5518
5519 if (!gui.in_use) /* can't do this when GUI not running */
5520 return INVALCOLOR;
5521
5522 while (name != NULL)
5523 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005524#if GTK_CHECK_VERSION(3,0,0)
5525 GdkRGBA color;
5526#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 GdkColor color;
Bram Moolenaar98921892016-02-23 17:14:37 +01005528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 int parsed;
5530 int i;
5531
Bram Moolenaar98921892016-02-23 17:14:37 +01005532#if GTK_CHECK_VERSION(3,0,0)
5533 parsed = gdk_rgba_parse(&color, (const gchar *)name);
5534#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 parsed = gdk_color_parse((const char *)name, &color);
Bram Moolenaar98921892016-02-23 17:14:37 +01005536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 if (parsed)
5539 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005540#if GTK_CHECK_VERSION(3,0,0)
5541 return (guicolor_T)gui_gtk_get_pixel_from_rgb(&color);
5542#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea),
5544 &color, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 return (guicolor_T)color.pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01005546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 }
5548 /* add a few builtin names and try again */
5549 for (i = 0; ; ++i)
5550 {
5551 if (vimnames[i][0] == NULL)
5552 {
5553 name = NULL;
5554 break;
5555 }
5556 if (STRICMP(name, vimnames[i][0]) == 0)
5557 {
5558 name = (char_u *)vimnames[i][1];
5559 break;
5560 }
5561 }
5562 }
5563
5564 return INVALCOLOR;
5565}
5566
5567/*
5568 * Set the current text foreground color.
5569 */
5570 void
5571gui_mch_set_fg_color(guicolor_T color)
5572{
5573 gui.fgcolor->pixel = (unsigned long)color;
5574}
5575
5576/*
5577 * Set the current text background color.
5578 */
5579 void
5580gui_mch_set_bg_color(guicolor_T color)
5581{
5582 gui.bgcolor->pixel = (unsigned long)color;
5583}
5584
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005585/*
5586 * Set the current text special color.
5587 */
5588 void
5589gui_mch_set_sp_color(guicolor_T color)
5590{
5591 gui.spcolor->pixel = (unsigned long)color;
5592}
5593
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594/*
5595 * Function-like convenience macro for the sake of efficiency.
5596 */
5597#define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
5598 G_STMT_START{ \
5599 PangoAttribute *tmp_attr_; \
5600 tmp_attr_ = (Attribute); \
5601 tmp_attr_->start_index = (Start); \
5602 tmp_attr_->end_index = (End); \
5603 pango_attr_list_insert((AttrList), tmp_attr_); \
5604 }G_STMT_END
5605
5606 static void
5607apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list)
5608{
5609 char_u *start = NULL;
5610 char_u *p;
5611 int uc;
5612
5613 for (p = s; p < s + len; p += utf_byte2len(*p))
5614 {
5615 uc = utf_ptr2char(p);
5616
5617 if (start == NULL)
5618 {
5619 if (uc >= 0x80 && utf_char2cells(uc) == 2)
5620 start = p;
5621 }
5622 else if (uc < 0x80 /* optimization shortcut */
5623 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc)))
5624 {
5625 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5626 attr_list, start - s, p - s);
5627 start = NULL;
5628 }
5629 }
5630
5631 if (start != NULL)
5632 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5633 attr_list, start - s, len);
5634}
5635
5636 static int
5637count_cluster_cells(char_u *s, PangoItem *item,
5638 PangoGlyphString* glyphs, int i,
5639 int *cluster_width,
5640 int *last_glyph_rbearing)
5641{
5642 char_u *p;
5643 int next; /* glyph start index of next cluster */
5644 int start, end; /* string segment of current cluster */
5645 int width; /* real cluster width in Pango units */
5646 int uc;
5647 int cellcount = 0;
5648
5649 width = glyphs->glyphs[i].geometry.width;
5650
5651 for (next = i + 1; next < glyphs->num_glyphs; ++next)
5652 {
5653 if (glyphs->glyphs[next].attr.is_cluster_start)
5654 break;
5655 else if (glyphs->glyphs[next].geometry.width > width)
5656 width = glyphs->glyphs[next].geometry.width;
5657 }
5658
5659 start = item->offset + glyphs->log_clusters[i];
5660 end = item->offset + ((next < glyphs->num_glyphs) ?
5661 glyphs->log_clusters[next] : item->length);
5662
5663 for (p = s + start; p < s + end; p += utf_byte2len(*p))
5664 {
5665 uc = utf_ptr2char(p);
5666 if (uc < 0x80)
5667 ++cellcount;
5668 else if (!utf_iscomposing(uc))
5669 cellcount += utf_char2cells(uc);
5670 }
5671
5672 if (last_glyph_rbearing != NULL
5673 && cellcount > 0 && next == glyphs->num_glyphs)
5674 {
5675 PangoRectangle ink_rect;
5676 /*
5677 * If a certain combining mark had to be taken from a non-monospace
5678 * font, we have to compensate manually by adapting x_offset according
5679 * to the ink extents of the previous glyph.
5680 */
5681 pango_font_get_glyph_extents(item->analysis.font,
5682 glyphs->glyphs[i].glyph,
5683 &ink_rect, NULL);
5684
5685 if (PANGO_RBEARING(ink_rect) > 0)
5686 *last_glyph_rbearing = PANGO_RBEARING(ink_rect);
5687 }
5688
5689 if (cellcount > 0)
5690 *cluster_width = width;
5691
5692 return cellcount;
5693}
5694
5695/*
5696 * If there are only combining characters in the cluster, we cannot just
5697 * change the width of the previous glyph since there is none. Therefore
5698 * some guesswork is needed.
5699 *
5700 * If ink_rect.x is negative Pango apparently has taken care of the composing
5701 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
5702 * to problems with composing from different fonts we still need to fine-tune
Bram Moolenaar64404472010-06-26 06:24:45 +02005703 * x_offset to avoid ugliness.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 *
5705 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
5706 * the position of the previous glyph. Apparently this happens only with old
5707 * X fonts which don't provide the special combining information needed by
5708 * Pango.
5709 */
5710 static void
5711setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph,
5712 int last_cellcount, int last_cluster_width,
5713 int last_glyph_rbearing)
5714{
5715 PangoRectangle ink_rect;
5716 PangoRectangle logical_rect;
5717 int width;
5718
5719 width = last_cellcount * gui.char_width * PANGO_SCALE;
5720 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2;
5721 glyph->geometry.width = 0;
5722
5723 pango_font_get_glyph_extents(item->analysis.font,
5724 glyph->glyph,
5725 &ink_rect, &logical_rect);
5726 if (ink_rect.x < 0)
5727 {
5728 glyph->geometry.x_offset += last_glyph_rbearing;
5729 glyph->geometry.y_offset = logical_rect.height
5730 - (gui.char_height - p_linespace) * PANGO_SCALE;
5731 }
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005732 else
5733 /* If the accent width is smaller than the cluster width, position it
5734 * in the middle. */
5735 glyph->geometry.x_offset = -width + MAX(0, width - ink_rect.width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736}
5737
Bram Moolenaar98921892016-02-23 17:14:37 +01005738#if GTK_CHECK_VERSION(3,0,0)
5739 static void
5740draw_glyph_string(int row, int col, int num_cells, int flags,
5741 PangoFont *font, PangoGlyphString *glyphs,
5742 cairo_t *cr)
5743#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 static void
5745draw_glyph_string(int row, int col, int num_cells, int flags,
5746 PangoFont *font, PangoGlyphString *glyphs)
Bram Moolenaar98921892016-02-23 17:14:37 +01005747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748{
5749 if (!(flags & DRAW_TRANSP))
5750 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005751#if GTK_CHECK_VERSION(3,0,0)
5752 set_cairo_source_rgb_from_pixel(cr, gui.bgcolor->pixel);
5753 cairo_rectangle(cr,
5754 FILL_X(col), FILL_Y(row),
5755 num_cells * gui.char_width, gui.char_height);
5756 cairo_fill(cr);
5757#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
5759
5760 gdk_draw_rectangle(gui.drawarea->window,
5761 gui.text_gc,
5762 TRUE,
5763 FILL_X(col),
5764 FILL_Y(row),
5765 num_cells * gui.char_width,
5766 gui.char_height);
Bram Moolenaar98921892016-02-23 17:14:37 +01005767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 }
5769
Bram Moolenaar98921892016-02-23 17:14:37 +01005770#if GTK_CHECK_VERSION(3,0,0)
5771 set_cairo_source_rgb_from_pixel(cr, gui.fgcolor->pixel);
5772 cairo_move_to(cr, TEXT_X(col), TEXT_Y(row));
5773 pango_cairo_show_glyph_string(cr, font, glyphs);
5774#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5776
5777 gdk_draw_glyphs(gui.drawarea->window,
5778 gui.text_gc,
5779 font,
5780 TEXT_X(col),
5781 TEXT_Y(row),
5782 glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01005783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
5785 /* redraw the contents with an offset of 1 to emulate bold */
5786 if ((flags & DRAW_BOLD) && !gui.font_can_bold)
Bram Moolenaar98921892016-02-23 17:14:37 +01005787#if GTK_CHECK_VERSION(3,0,0)
5788 {
5789 set_cairo_source_rgb_from_pixel(cr, gui.fgcolor->pixel);
5790 cairo_move_to(cr, TEXT_X(col) + 1, TEXT_Y(row));
5791 pango_cairo_show_glyph_string(cr, font, glyphs);
5792 }
5793#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 gdk_draw_glyphs(gui.drawarea->window,
5795 gui.text_gc,
5796 font,
5797 TEXT_X(col) + 1,
5798 TEXT_Y(row),
5799 glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01005800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801}
5802
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005803/*
5804 * Draw underline and undercurl at the bottom of the character cell.
5805 */
Bram Moolenaar98921892016-02-23 17:14:37 +01005806#if GTK_CHECK_VERSION(3,0,0)
5807 static void
5808draw_under(int flags, int row, int col, int cells, cairo_t *cr)
5809#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005810 static void
5811draw_under(int flags, int row, int col, int cells)
Bram Moolenaar98921892016-02-23 17:14:37 +01005812#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005813{
5814 int i;
5815 int offset;
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005816 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005817 int y = FILL_Y(row + 1) - 1;
5818
5819 /* Undercurl: draw curl at the bottom of the character cell. */
5820 if (flags & DRAW_UNDERC)
5821 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005822#if GTK_CHECK_VERSION(3,0,0)
5823 cairo_set_line_width(cr, 1.0);
5824 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
5825 set_cairo_source_rgb_from_pixel(cr, gui.spcolor->pixel);
5826 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
5827 {
5828 offset = val[i % 8];
5829 cairo_line_to(cr, i, y - offset + 0.5);
5830 }
5831 cairo_stroke(cr);
5832#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005833 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
5834 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
5835 {
5836 offset = val[i % 8];
5837 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset);
5838 }
5839 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar98921892016-02-23 17:14:37 +01005840#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005841 }
5842
5843 /* Underline: draw a line at the bottom of the character cell. */
5844 if (flags & DRAW_UNDERL)
5845 {
5846 /* When p_linespace is 0, overwrite the bottom row of pixels.
5847 * Otherwise put the line just below the character. */
5848 if (p_linespace > 1)
5849 y -= p_linespace - 1;
Bram Moolenaar98921892016-02-23 17:14:37 +01005850#if GTK_CHECK_VERSION(3,0,0)
5851 {
5852 cairo_set_line_width(cr, 1.0);
5853 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
5854 set_cairo_source_rgb_from_pixel(cr, gui.fgcolor->pixel);
5855 cairo_move_to(cr, FILL_X(col), y + 0.5);
5856 cairo_line_to(cr, FILL_X(col + cells), y + 0.5);
5857 cairo_stroke(cr);
5858 }
5859#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005860 gdk_draw_line(gui.drawarea->window, gui.text_gc,
5861 FILL_X(col), y,
5862 FILL_X(col + cells) - 1, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01005863#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005864 }
5865}
5866
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 int
5868gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
5869{
5870 GdkRectangle area; /* area for clip mask */
5871 PangoGlyphString *glyphs; /* glyphs of current item */
5872 int column_offset = 0; /* column offset in cells */
5873 int i;
5874 char_u *conv_buf = NULL; /* result of UTF-8 conversion */
5875 char_u *new_conv_buf;
5876 int convlen;
5877 char_u *sp, *bp;
5878 int plen;
Bram Moolenaar98921892016-02-23 17:14:37 +01005879#if GTK_CHECK_VERSION(3,0,0)
5880 cairo_t *cr;
5881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882
Bram Moolenaar98921892016-02-23 17:14:37 +01005883#if GTK_CHECK_VERSION(3,0,0)
5884 if (gui.text_context == NULL || gtk_widget_get_window(gui.drawarea) == NULL)
5885#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 if (gui.text_context == NULL || gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01005887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 return len;
5889
5890 if (output_conv.vc_type != CONV_NONE)
5891 {
5892 /*
5893 * Convert characters from 'encoding' to 'termencoding', which is set
5894 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
5895 * prohibits changing this to something else than UTF-8 if the GUI is
5896 * in use.
5897 */
5898 convlen = len;
5899 conv_buf = string_convert(&output_conv, s, &convlen);
5900 g_return_val_if_fail(conv_buf != NULL, len);
5901
5902 /* Correct for differences in char width: some chars are
5903 * double-wide in 'encoding' but single-wide in utf-8. Add a space to
5904 * compensate for that. */
5905 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; )
5906 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005907 plen = utf_ptr2len(bp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1)
5909 {
5910 new_conv_buf = alloc(convlen + 2);
5911 if (new_conv_buf == NULL)
5912 return len;
5913 plen += bp - conv_buf;
5914 mch_memmove(new_conv_buf, conv_buf, plen);
5915 new_conv_buf[plen] = ' ';
5916 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen,
5917 convlen - plen + 1);
5918 vim_free(conv_buf);
5919 conv_buf = new_conv_buf;
5920 ++convlen;
5921 bp = conv_buf + plen;
5922 plen = 1;
5923 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005924 sp += (*mb_ptr2len)(sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 bp += plen;
5926 }
5927 s = conv_buf;
5928 len = convlen;
5929 }
5930
5931 /*
5932 * Restrict all drawing to the current screen line in order to prevent
5933 * fuzzy font lookups from messing up the screen.
5934 */
5935 area.x = gui.border_offset;
5936 area.y = FILL_Y(row);
5937 area.width = gui.num_cols * gui.char_width;
5938 area.height = gui.char_height;
5939
Bram Moolenaar98921892016-02-23 17:14:37 +01005940#if GTK_CHECK_VERSION(3,0,0)
5941 cr = cairo_create(gui.surface);
5942 cairo_rectangle(cr, area.x, area.y, area.width, area.height);
5943 cairo_clip(cr);
5944#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 gdk_gc_set_clip_origin(gui.text_gc, 0, 0);
5946 gdk_gc_set_clip_rectangle(gui.text_gc, &area);
Bram Moolenaar98921892016-02-23 17:14:37 +01005947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948
5949 glyphs = pango_glyph_string_new();
5950
5951 /*
5952 * Optimization hack: If possible, skip the itemize and shaping process
5953 * for pure ASCII strings. This optimization is particularly effective
5954 * because Vim draws space characters to clear parts of the screen.
5955 */
5956 if (!(flags & DRAW_ITALIC)
5957 && !((flags & DRAW_BOLD) && gui.font_can_bold)
5958 && gui.ascii_glyphs != NULL)
5959 {
5960 char_u *p;
5961
5962 for (p = s; p < s + len; ++p)
5963 if (*p & 0x80)
5964 goto not_ascii;
5965
5966 pango_glyph_string_set_size(glyphs, len);
5967
5968 for (i = 0; i < len; ++i)
5969 {
5970 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[s[i]];
5971 glyphs->log_clusters[i] = i;
5972 }
5973
Bram Moolenaar98921892016-02-23 17:14:37 +01005974#if GTK_CHECK_VERSION(3,0,0)
5975 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs, cr);
5976#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01005978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979
5980 column_offset = len;
5981 }
5982 else
5983not_ascii:
5984 {
5985 PangoAttrList *attr_list;
5986 GList *item_list;
5987 int cluster_width;
5988 int last_glyph_rbearing;
5989 int cells = 0; /* cells occupied by current cluster */
5990
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005991 /* Safety check: pango crashes when invoked with invalid utf-8
5992 * characters. */
5993 if (!utf_valid_string(s, s + len))
5994 {
5995 column_offset = len;
5996 goto skipitall;
5997 }
5998
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 /* original width of the current cluster */
6000 cluster_width = PANGO_SCALE * gui.char_width;
6001
6002 /* right bearing of the last non-composing glyph */
6003 last_glyph_rbearing = PANGO_SCALE * gui.char_width;
6004
6005 attr_list = pango_attr_list_new();
6006
6007 /* If 'guifontwide' is set then use that for double-width characters.
6008 * Otherwise just go with 'guifont' and let Pango do its thing. */
6009 if (gui.wide_font != NULL)
6010 apply_wide_font_attr(s, len, attr_list);
6011
6012 if ((flags & DRAW_BOLD) && gui.font_can_bold)
6013 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD),
6014 attr_list, 0, len);
6015 if (flags & DRAW_ITALIC)
6016 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC),
6017 attr_list, 0, len);
6018 /*
6019 * Break the text into segments with consistent directional level
6020 * and shaping engine. Pure Latin text needs only a single segment,
6021 * so there's no need to worry about the loop's efficiency. Better
6022 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
6023 */
6024 item_list = pango_itemize(gui.text_context,
6025 (const char *)s, 0, len, attr_list, NULL);
6026
6027 while (item_list != NULL)
6028 {
6029 PangoItem *item;
6030 int item_cells = 0; /* item length in cells */
6031
6032 item = (PangoItem *)item_list->data;
6033 item_list = g_list_delete_link(item_list, item_list);
6034 /*
6035 * Increment the bidirectional embedding level by 1 if it is not
6036 * even. An odd number means the output will be RTL, but we don't
6037 * want that since Vim handles right-to-left text on its own. It
6038 * would probably be sufficient to just set level = 0, but you can
6039 * never know :)
6040 *
6041 * Unfortunately we can't take advantage of Pango's ability to
6042 * render both LTR and RTL at the same time. In order to support
6043 * that, Vim's main screen engine would have to make use of Pango
6044 * functionality.
6045 */
6046 item->analysis.level = (item->analysis.level + 1) & (~1U);
6047
6048 /* HACK: Overrule the shape engine, we don't want shaping to be
6049 * done, because drawing the cursor would change the display. */
6050 item->analysis.shape_engine = default_shape_engine;
6051
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02006052#ifdef HAVE_PANGO_SHAPE_FULL
Bram Moolenaar7e2ec002015-09-08 16:31:06 +02006053 pango_shape_full((const char *)s + item->offset, item->length,
6054 (const char *)s, len, &item->analysis, glyphs);
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02006055#else
6056 pango_shape((const char *)s + item->offset, item->length,
6057 &item->analysis, glyphs);
6058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 /*
6060 * Fixed-width hack: iterate over the array and assign a fixed
6061 * width to each glyph, thus overriding the choice made by the
6062 * shaping engine. We use utf_char2cells() to determine the
6063 * number of cells needed.
6064 *
6065 * Also perform all kind of dark magic to get composing
6066 * characters right (and pretty too of course).
6067 */
6068 for (i = 0; i < glyphs->num_glyphs; ++i)
6069 {
6070 PangoGlyphInfo *glyph;
6071
6072 glyph = &glyphs->glyphs[i];
6073
6074 if (glyph->attr.is_cluster_start)
6075 {
6076 int cellcount;
6077
6078 cellcount = count_cluster_cells(
6079 s, item, glyphs, i, &cluster_width,
6080 (item_list != NULL) ? &last_glyph_rbearing : NULL);
6081
6082 if (cellcount > 0)
6083 {
6084 int width;
6085
6086 width = cellcount * gui.char_width * PANGO_SCALE;
6087 glyph->geometry.x_offset +=
6088 MAX(0, width - cluster_width) / 2;
6089 glyph->geometry.width = width;
6090 }
6091 else
6092 {
6093 /* If there are only combining characters in the
6094 * cluster, we cannot just change the width of the
6095 * previous glyph since there is none. Therefore
6096 * some guesswork is needed. */
6097 setup_zero_width_cluster(item, glyph, cells,
6098 cluster_width,
6099 last_glyph_rbearing);
6100 }
6101
6102 item_cells += cellcount;
6103 cells = cellcount;
6104 }
6105 else if (i > 0)
6106 {
6107 int width;
6108
6109 /* There is a previous glyph, so we deal with combining
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006110 * characters the canonical way.
Bram Moolenaar96118f32010-08-08 14:40:37 +02006111 * In some circumstances Pango uses a positive x_offset,
6112 * then use the width of the previous glyph for this one
6113 * and set the previous width to zero.
6114 * Otherwise we get a negative x_offset, Pango has already
6115 * positioned the combining char, keep the widths as they
6116 * are.
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006117 * For both adjust the x_offset to position the glyph in
Bram Moolenaar96118f32010-08-08 14:40:37 +02006118 * the middle. */
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006119 if (glyph->geometry.x_offset >= 0)
Bram Moolenaar96118f32010-08-08 14:40:37 +02006120 {
6121 glyphs->glyphs[i].geometry.width =
6122 glyphs->glyphs[i - 1].geometry.width;
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006123 glyphs->glyphs[i - 1].geometry.width = 0;
Bram Moolenaar96118f32010-08-08 14:40:37 +02006124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 width = cells * gui.char_width * PANGO_SCALE;
6126 glyph->geometry.x_offset +=
6127 MAX(0, width - cluster_width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 }
6129 else /* i == 0 "cannot happen" */
6130 {
6131 glyph->geometry.width = 0;
6132 }
6133 }
6134
6135 /*** Aaaaand action! ***/
Bram Moolenaar98921892016-02-23 17:14:37 +01006136#if GTK_CHECK_VERSION(3,0,0)
6137 draw_glyph_string(row, col + column_offset, item_cells,
6138 flags, item->analysis.font, glyphs,
6139 cr);
6140#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 draw_glyph_string(row, col + column_offset, item_cells,
6142 flags, item->analysis.font, glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01006143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144
6145 pango_item_free(item);
6146
6147 column_offset += item_cells;
6148 }
6149
6150 pango_attr_list_unref(attr_list);
6151 }
6152
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006153skipitall:
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006154 /* Draw underline and undercurl. */
Bram Moolenaar98921892016-02-23 17:14:37 +01006155#if GTK_CHECK_VERSION(3,0,0)
6156 draw_under(flags, row, col, column_offset, cr);
6157#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006158 draw_under(flags, row, col, column_offset);
Bram Moolenaar98921892016-02-23 17:14:37 +01006159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160
6161 pango_glyph_string_free(glyphs);
6162 vim_free(conv_buf);
6163
Bram Moolenaar98921892016-02-23 17:14:37 +01006164#if GTK_CHECK_VERSION(3,0,0)
6165 cairo_destroy(cr);
6166 if (!gui.by_signal)
6167 gdk_window_invalidate_rect(gtk_widget_get_window(gui.drawarea),
6168 &area, FALSE);
6169#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 gdk_gc_set_clip_rectangle(gui.text_gc, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01006171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172
6173 return column_offset;
6174}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175
6176/*
6177 * Return OK if the key with the termcap name "name" is supported.
6178 */
6179 int
6180gui_mch_haskey(char_u *name)
6181{
6182 int i;
6183
6184 for (i = 0; special_keys[i].key_sym != 0; i++)
6185 if (name[0] == special_keys[i].code0
6186 && name[1] == special_keys[i].code1)
6187 return OK;
6188 return FAIL;
6189}
6190
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006191#if defined(FEAT_TITLE) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192/*
6193 * Return the text window-id and display. Only required for X-based GUI's
6194 */
6195 int
6196gui_get_x11_windis(Window *win, Display **dis)
6197{
Bram Moolenaar98921892016-02-23 17:14:37 +01006198#if GTK_CHECK_VERSION(3,0,0)
6199 if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL)
6200#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01006202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 {
Bram Moolenaar98921892016-02-23 17:14:37 +01006204#if GTK_CHECK_VERSION(3,0,0)
6205 *dis = GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin));
6206 *win = GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin));
6207#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 *dis = GDK_WINDOW_XDISPLAY(gui.mainwin->window);
6209 *win = GDK_WINDOW_XWINDOW(gui.mainwin->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01006210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 return OK;
6212 }
6213
6214 *dis = NULL;
6215 *win = 0;
6216 return FAIL;
6217}
6218#endif
6219
6220#if defined(FEAT_CLIENTSERVER) \
6221 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
6222
6223 Display *
6224gui_mch_get_display(void)
6225{
Bram Moolenaar98921892016-02-23 17:14:37 +01006226#if GTK_CHECK_VERSION(3,0,0)
6227 if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL)
6228 return GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin));
6229#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
6231 return GDK_WINDOW_XDISPLAY(gui.mainwin->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01006232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 else
6234 return NULL;
6235}
6236#endif
6237
6238 void
6239gui_mch_beep(void)
6240{
6241#ifdef HAVE_GTK_MULTIHEAD
6242 GdkDisplay *display;
6243
Bram Moolenaar98921892016-02-23 17:14:37 +01006244# if GTK_CHECK_VERSION(3,0,0)
6245 if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin))
6246# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
Bram Moolenaar98921892016-02-23 17:14:37 +01006248# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 display = gtk_widget_get_display(gui.mainwin);
6250 else
6251 display = gdk_display_get_default();
6252
6253 if (display != NULL)
6254 gdk_display_beep(display);
6255#else
6256 gdk_beep();
6257#endif
6258}
6259
6260 void
6261gui_mch_flash(int msec)
6262{
Bram Moolenaar98921892016-02-23 17:14:37 +01006263#if GTK_CHECK_VERSION(3,0,0)
6264 /* TODO Replace GdkGC with Cairo */
6265 (void)msec;
6266#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 GdkGCValues values;
6268 GdkGC *invert_gc;
6269
6270 if (gui.drawarea->window == NULL)
6271 return;
6272
6273 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6274 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
6275 values.function = GDK_XOR;
6276 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6277 &values,
6278 GDK_GC_FOREGROUND |
6279 GDK_GC_BACKGROUND |
6280 GDK_GC_FUNCTION);
6281 gdk_gc_set_exposures(invert_gc,
6282 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
6283 /*
6284 * Do a visual beep by changing back and forth in some undetermined way,
6285 * the foreground and background colors. This is due to the fact that
6286 * there can't be really any prediction about the effects of XOR on
6287 * arbitrary X11 servers. However this seems to be enough for what we
6288 * intend it to do.
6289 */
6290 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6291 TRUE,
6292 0, 0,
6293 FILL_X((int)Columns) + gui.border_offset,
6294 FILL_Y((int)Rows) + gui.border_offset);
6295
6296 gui_mch_flush();
6297 ui_delay((long)msec, TRUE); /* wait so many msec */
6298
6299 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6300 TRUE,
6301 0, 0,
6302 FILL_X((int)Columns) + gui.border_offset,
6303 FILL_Y((int)Rows) + gui.border_offset);
6304
6305 gdk_gc_destroy(invert_gc);
Bram Moolenaar98921892016-02-23 17:14:37 +01006306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307}
6308
6309/*
6310 * Invert a rectangle from row r, column c, for nr rows and nc columns.
6311 */
6312 void
6313gui_mch_invert_rectangle(int r, int c, int nr, int nc)
6314{
Bram Moolenaar98921892016-02-23 17:14:37 +01006315#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar4fc563b2016-03-12 12:40:58 +01006316 const GdkRectangle rect = {
6317 FILL_X(c), FILL_Y(r), nc * gui.char_width, nr * gui.char_height
6318 };
6319 cairo_t * const cr = cairo_create(gui.surface);
6320
6321 set_cairo_source_rgb_from_pixel(cr, gui.norm_pixel ^ gui.back_pixel);
6322# if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,9,2)
6323 cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE);
6324# else
6325 /* Give an implementation for older cairo versions if necessary. */
6326# endif
6327 gdk_cairo_rectangle(cr, &rect);
6328 cairo_fill(cr);
6329
6330 cairo_destroy(cr);
6331
6332 if (!gui.by_signal)
6333 gtk_widget_queue_draw_area(gui.drawarea, rect.x, rect.y,
6334 rect.width, rect.height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006335#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 GdkGCValues values;
6337 GdkGC *invert_gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338
6339 if (gui.drawarea->window == NULL)
6340 return;
6341
Bram Moolenaar89d40322006-08-29 15:30:07 +00006342 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6343 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 values.function = GDK_XOR;
6345 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6346 &values,
6347 GDK_GC_FOREGROUND |
6348 GDK_GC_BACKGROUND |
6349 GDK_GC_FUNCTION);
Bram Moolenaar89d40322006-08-29 15:30:07 +00006350 gdk_gc_set_exposures(invert_gc, gui.visibility !=
6351 GDK_VISIBILITY_UNOBSCURED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6353 TRUE,
6354 FILL_X(c), FILL_Y(r),
6355 (nc) * gui.char_width, (nr) * gui.char_height);
6356 gdk_gc_destroy(invert_gc);
Bram Moolenaar98921892016-02-23 17:14:37 +01006357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358}
6359
6360/*
6361 * Iconify the GUI window.
6362 */
6363 void
6364gui_mch_iconify(void)
6365{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 gtk_window_iconify(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367}
6368
6369#if defined(FEAT_EVAL) || defined(PROTO)
6370/*
6371 * Bring the Vim window to the foreground.
6372 */
6373 void
6374gui_mch_set_foreground(void)
6375{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 gtk_window_present(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377}
6378#endif
6379
6380/*
6381 * Draw a cursor without focus.
6382 */
6383 void
6384gui_mch_draw_hollow_cursor(guicolor_T color)
6385{
6386 int i = 1;
Bram Moolenaar98921892016-02-23 17:14:37 +01006387#if GTK_CHECK_VERSION(3,0,0)
6388 cairo_t *cr;
6389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390
Bram Moolenaar98921892016-02-23 17:14:37 +01006391#if GTK_CHECK_VERSION(3,0,0)
6392 if (gtk_widget_get_window(gui.drawarea) == NULL)
6393#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 if (gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01006395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 return;
6397
Bram Moolenaar98921892016-02-23 17:14:37 +01006398#if GTK_CHECK_VERSION(3,0,0)
6399 cr = cairo_create(gui.surface);
6400#endif
6401
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 gui_mch_set_fg_color(color);
6403
Bram Moolenaar98921892016-02-23 17:14:37 +01006404#if GTK_CHECK_VERSION(3,0,0)
6405 set_cairo_source_rgb_from_pixel(cr, gui.fgcolor->pixel);
6406#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar98921892016-02-23 17:14:37 +01006408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 if (mb_lefthalve(gui.row, gui.col))
6410 i = 2;
Bram Moolenaar98921892016-02-23 17:14:37 +01006411#if GTK_CHECK_VERSION(3,0,0)
6412 cairo_set_line_width(cr, 1.0);
6413 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
6414 cairo_rectangle(cr,
6415 FILL_X(gui.col) + 0.5, FILL_Y(gui.row) + 0.5,
6416 i * gui.char_width - 1, gui.char_height - 1);
6417 cairo_stroke(cr);
6418 cairo_destroy(cr);
6419#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6421 FALSE,
6422 FILL_X(gui.col), FILL_Y(gui.row),
6423 i * gui.char_width - 1, gui.char_height - 1);
Bram Moolenaar98921892016-02-23 17:14:37 +01006424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425}
6426
6427/*
6428 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
6429 * color "color".
6430 */
6431 void
6432gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
6433{
Bram Moolenaar98921892016-02-23 17:14:37 +01006434#if GTK_CHECK_VERSION(3,0,0)
6435 if (gtk_widget_get_window(gui.drawarea) == NULL)
6436#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 if (gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01006438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 return;
6440
6441 gui_mch_set_fg_color(color);
6442
Bram Moolenaar98921892016-02-23 17:14:37 +01006443#if GTK_CHECK_VERSION(3,0,0)
6444 {
6445 cairo_t *cr;
6446
6447 cr = cairo_create(gui.surface);
6448 set_cairo_source_rgb_from_pixel(cr, gui.fgcolor->pixel);
6449 cairo_rectangle(cr,
6450# ifdef FEAT_RIGHTLEFT
6451 /* vertical line should be on the right of current point */
6452 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
6453# endif
6454 FILL_X(gui.col), FILL_Y(gui.row) + gui.char_height - h,
6455 w, h);
6456 cairo_fill(cr);
6457 cairo_destroy(cr);
6458 }
6459#else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6461 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6462 TRUE,
Bram Moolenaar98921892016-02-23 17:14:37 +01006463# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 /* vertical line should be on the right of current point */
6465 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
Bram Moolenaar98921892016-02-23 17:14:37 +01006466# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 FILL_X(gui.col),
6468 FILL_Y(gui.row) + gui.char_height - h,
6469 w, h);
Bram Moolenaar98921892016-02-23 17:14:37 +01006470#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471}
6472
6473
6474/*
6475 * Catch up with any queued X11 events. This may put keyboard input into the
6476 * input buffer, call resize call-backs, trigger timers etc. If there is
6477 * nothing in the X11 event queue (& no timers pending), then we return
6478 * immediately.
6479 */
6480 void
6481gui_mch_update(void)
6482{
Bram Moolenaara3f41662010-07-11 19:01:06 +02006483 while (g_main_context_pending(NULL) && !vim_is_input_buf_full())
6484 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485}
6486
Bram Moolenaar98921892016-02-23 17:14:37 +01006487#if GTK_CHECK_VERSION(3,0,0)
6488 static gboolean
6489#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +01006491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492input_timer_cb(gpointer data)
6493{
6494 int *timed_out = (int *) data;
6495
Bram Moolenaar49325942007-05-10 19:19:59 +00006496 /* Just inform the caller about the occurrence of it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 *timed_out = TRUE;
6498
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 return FALSE; /* don't happen again */
6500}
6501
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502/*
6503 * GUI input routine called by gui_wait_for_chars(). Waits for a character
6504 * from the keyboard.
6505 * wtime == -1 Wait forever.
6506 * wtime == 0 This should never happen.
6507 * wtime > 0 Wait wtime milliseconds for a character.
6508 * Returns OK if a character was found to be available within the given time,
6509 * or FAIL otherwise.
6510 */
6511 int
6512gui_mch_wait_for_chars(long wtime)
6513{
6514 int focus;
6515 guint timer;
6516 static int timed_out;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517
6518 timed_out = FALSE;
6519
6520 /* this timeout makes sure that we will return if no characters arrived in
6521 * time */
6522
6523 if (wtime > 0)
Bram Moolenaar98921892016-02-23 17:14:37 +01006524#if GTK_CHECK_VERSION(3,0,0)
6525 timer = g_timeout_add((guint)wtime, input_timer_cb, &timed_out);
6526#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 timer = gtk_timeout_add((guint32)wtime, input_timer_cb, &timed_out);
Bram Moolenaar98921892016-02-23 17:14:37 +01006528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 else
6530 timer = 0;
6531
6532 focus = gui.in_focus;
6533
6534 do
6535 {
6536 /* Stop or start blinking when focus changes */
6537 if (gui.in_focus != focus)
6538 {
6539 if (gui.in_focus)
6540 gui_mch_start_blink();
6541 else
6542 gui_mch_stop_blink();
6543 focus = gui.in_focus;
6544 }
6545
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006546#ifdef MESSAGE_QUEUE
6547 parse_queued_messages();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006548#endif
6549
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 /*
6551 * Loop in GTK+ processing until a timeout or input occurs.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006552 * Skip this if input is available anyway (can happen in rare
6553 * situations, sort of race condition).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006555 if (!input_available())
Bram Moolenaara3f41662010-07-11 19:01:06 +02006556 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557
6558 /* Got char, return immediately */
6559 if (input_available())
6560 {
6561 if (timer != 0 && !timed_out)
Bram Moolenaar98921892016-02-23 17:14:37 +01006562#if GTK_CHECK_VERSION(3,0,0)
6563 g_source_remove(timer);
6564#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 gtk_timeout_remove(timer);
Bram Moolenaar98921892016-02-23 17:14:37 +01006566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 return OK;
6568 }
6569 } while (wtime < 0 || !timed_out);
6570
6571 /*
6572 * Flush all eventually pending (drawing) events.
6573 */
6574 gui_mch_update();
6575
6576 return FAIL;
6577}
6578
6579
6580/****************************************************************************
6581 * Output drawing routines.
6582 ****************************************************************************/
6583
6584
6585/* Flush any output to the screen */
6586 void
6587gui_mch_flush(void)
6588{
6589#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar98921892016-02-23 17:14:37 +01006590# if GTK_CHECK_VERSION(3,0,0)
6591 if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin))
6592# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
Bram Moolenaar98921892016-02-23 17:14:37 +01006594# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 gdk_display_sync(gtk_widget_get_display(gui.mainwin));
6596#else
6597 gdk_flush(); /* historical misnomer: calls XSync(), not XFlush() */
6598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 /* This happens to actually do what gui_mch_flush() is supposed to do,
6600 * according to the comment above. */
Bram Moolenaar98921892016-02-23 17:14:37 +01006601#if GTK_CHECK_VERSION(3,0,0)
6602 if (gui.drawarea != NULL && gtk_widget_get_window(gui.drawarea) != NULL)
6603 gdk_window_process_updates(gtk_widget_get_window(gui.drawarea), FALSE);
6604#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
6606 gdk_window_process_updates(gui.drawarea->window, FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01006607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608}
6609
6610/*
6611 * Clear a rectangular region of the screen from text pos (row1, col1) to
6612 * (row2, col2) inclusive.
6613 */
6614 void
6615gui_mch_clear_block(int row1, int col1, int row2, int col2)
6616{
Bram Moolenaar98921892016-02-23 17:14:37 +01006617#if GTK_CHECK_VERSION(3,0,0)
6618 if (gtk_widget_get_window(gui.drawarea) == NULL)
6619 return;
6620#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 GdkColor color;
6622
6623 if (gui.drawarea->window == NULL)
6624 return;
6625
6626 color.pixel = gui.back_pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01006627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628
Bram Moolenaar98921892016-02-23 17:14:37 +01006629#if GTK_CHECK_VERSION(3,0,0)
6630 {
6631 /* Add one pixel to the far right column in case a double-stroked
6632 * bold glyph may sit there. */
6633 const GdkRectangle rect = {
6634 FILL_X(col1), FILL_Y(row1),
6635 (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1),
6636 (row2 - row1 + 1) * gui.char_height
6637 };
6638 GdkWindow * const win = gtk_widget_get_window(gui.drawarea);
6639 cairo_t * const cr = cairo_create(gui.surface);
6640 cairo_pattern_t * const pat = gdk_window_get_background_pattern(win);
6641 if (pat != NULL)
6642 cairo_set_source(cr, pat);
6643 else
6644 set_cairo_source_rgb_from_pixel(cr, gui.back_pixel);
6645 gdk_cairo_rectangle(cr, &rect);
6646 cairo_fill(cr);
6647 cairo_destroy(cr);
6648
6649 if (!gui.by_signal)
6650 gdk_window_invalidate_rect(win, &rect, FALSE);
6651 }
6652#else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 gdk_gc_set_foreground(gui.text_gc, &color);
6654
6655 /* Clear one extra pixel at the far right, for when bold characters have
6656 * spilled over to the window border. */
6657 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE,
6658 FILL_X(col1), FILL_Y(row1),
6659 (col2 - col1 + 1) * gui.char_width
6660 + (col2 == Columns - 1),
6661 (row2 - row1 + 1) * gui.char_height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006662#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663}
6664
Bram Moolenaar98921892016-02-23 17:14:37 +01006665#if GTK_CHECK_VERSION(3,0,0)
6666 static void
6667gui_gtk_window_clear(GdkWindow *win)
6668{
6669 const GdkRectangle rect = {
6670 0, 0, gdk_window_get_width(win), gdk_window_get_height(win)
6671 };
6672 cairo_t * const cr = cairo_create(gui.surface);
6673 cairo_pattern_t * const pat = gdk_window_get_background_pattern(win);
6674 if (pat != NULL)
6675 cairo_set_source(cr, pat);
6676 else
6677 set_cairo_source_rgb_from_pixel(cr, gui.back_pixel);
6678 gdk_cairo_rectangle(cr, &rect);
6679 cairo_fill(cr);
6680 cairo_destroy(cr);
6681
6682 if (!gui.by_signal)
6683 gdk_window_invalidate_rect(win, &rect, FALSE);
6684}
6685#endif
6686
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 void
6688gui_mch_clear_all(void)
6689{
Bram Moolenaar98921892016-02-23 17:14:37 +01006690#if GTK_CHECK_VERSION(3,0,0)
6691 if (gtk_widget_get_window(gui.drawarea) != NULL)
6692 gui_gtk_window_clear(gtk_widget_get_window(gui.drawarea));
6693#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 if (gui.drawarea->window != NULL)
6695 gdk_window_clear(gui.drawarea->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01006696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697}
6698
Bram Moolenaar98921892016-02-23 17:14:37 +01006699#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700/*
6701 * Redraw any text revealed by scrolling up/down.
6702 */
6703 static void
6704check_copy_area(void)
6705{
6706 GdkEvent *event;
6707 int expose_count;
6708
6709 if (gui.visibility != GDK_VISIBILITY_PARTIAL)
6710 return;
6711
6712 /* Avoid redrawing the cursor while scrolling or it'll end up where
6713 * we don't want it to be. I'm not sure if it's correct to call
6714 * gui_dont_update_cursor() at this point but it works as a quick
6715 * fix for now. */
6716 gui_dont_update_cursor();
6717
6718 do
6719 {
6720 /* Wait to check whether the scroll worked or not. */
6721 event = gdk_event_get_graphics_expose(gui.drawarea->window);
6722
6723 if (event == NULL)
6724 break; /* received NoExpose event */
6725
6726 gui_redraw(event->expose.area.x, event->expose.area.y,
6727 event->expose.area.width, event->expose.area.height);
6728
6729 expose_count = event->expose.count;
6730 gdk_event_free(event);
6731 }
6732 while (expose_count > 0); /* more events follow */
6733
6734 gui_can_update_cursor();
6735}
Bram Moolenaar98921892016-02-23 17:14:37 +01006736#endif /* !GTK_CHECK_VERSION(3,0,0) */
6737
6738#if GTK_CHECK_VERSION(3,0,0)
6739 static void
6740gui_gtk_surface_copy_rect(int dest_x, int dest_y,
6741 int src_x, int src_y,
6742 int width, int height)
6743{
6744 cairo_t * const cr = cairo_create(gui.surface);
6745
6746 cairo_rectangle(cr, dest_x, dest_y, width, height);
6747 cairo_clip(cr);
6748 cairo_push_group(cr);
6749 cairo_set_source_surface(cr, gui.surface, dest_x - src_x, dest_y - src_y);
6750 cairo_paint(cr);
6751 cairo_pop_group_to_source(cr);
6752 cairo_paint(cr);
6753
6754 cairo_destroy(cr);
6755}
6756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757
6758/*
6759 * Delete the given number of lines from the given row, scrolling up any
6760 * text further down within the scroll region.
6761 */
6762 void
6763gui_mch_delete_lines(int row, int num_lines)
6764{
Bram Moolenaar98921892016-02-23 17:14:37 +01006765#if GTK_CHECK_VERSION(3,0,0)
6766 const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1;
6767 const int nrows = gui.scroll_region_bot - row + 1;
6768 const int src_nrows = nrows - num_lines;
6769
6770 gui_gtk_surface_copy_rect(
6771 FILL_X(gui.scroll_region_left), FILL_Y(row),
6772 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6773 gui.char_width * ncols + 1, gui.char_height * src_nrows);
6774 gui_clear_block(
6775 gui.scroll_region_bot - num_lines + 1, gui.scroll_region_left,
6776 gui.scroll_region_bot, gui.scroll_region_right);
6777 gui_gtk3_redraw(
6778 FILL_X(gui.scroll_region_left), FILL_Y(row),
6779 gui.char_width * ncols + 1, gui.char_height * nrows);
6780 if (!gui.by_signal)
6781 gtk_widget_queue_draw_area(gui.drawarea,
6782 FILL_X(gui.scroll_region_left), FILL_Y(row),
6783 gui.char_width * ncols + 1, gui.char_height * nrows);
6784#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
6786 return; /* Can't see the window */
6787
6788 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6789 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6790
6791 /* copy one extra pixel, for when bold has spilled over */
6792 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6793 FILL_X(gui.scroll_region_left), FILL_Y(row),
6794 gui.drawarea->window,
6795 FILL_X(gui.scroll_region_left),
6796 FILL_Y(row + num_lines),
6797 gui.char_width * (gui.scroll_region_right
6798 - gui.scroll_region_left + 1) + 1,
6799 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6800
6801 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
6802 gui.scroll_region_left,
6803 gui.scroll_region_bot, gui.scroll_region_right);
6804 check_copy_area();
Bram Moolenaar98921892016-02-23 17:14:37 +01006805#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806}
6807
6808/*
6809 * Insert the given number of lines before the given row, scrolling down any
6810 * following text within the scroll region.
6811 */
6812 void
6813gui_mch_insert_lines(int row, int num_lines)
6814{
Bram Moolenaar98921892016-02-23 17:14:37 +01006815#if GTK_CHECK_VERSION(3,0,0)
6816 const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1;
6817 const int nrows = gui.scroll_region_bot - row + 1;
6818 const int src_nrows = nrows - num_lines;
6819
6820 gui_gtk_surface_copy_rect(
6821 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6822 FILL_X(gui.scroll_region_left), FILL_Y(row),
6823 gui.char_width * ncols + 1, gui.char_height * src_nrows);
6824 gui_mch_clear_block(
6825 row, gui.scroll_region_left,
6826 row + num_lines - 1, gui.scroll_region_right);
6827 gui_gtk3_redraw(
6828 FILL_X(gui.scroll_region_left), FILL_Y(row),
6829 gui.char_width * ncols + 1, gui.char_height * nrows);
6830 if (!gui.by_signal)
6831 gtk_widget_queue_draw_area(gui.drawarea,
6832 FILL_X(gui.scroll_region_left), FILL_Y(row),
6833 gui.char_width * ncols + 1, gui.char_height * nrows);
6834#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
6836 return; /* Can't see the window */
6837
6838 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6839 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6840
6841 /* copy one extra pixel, for when bold has spilled over */
6842 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6843 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6844 gui.drawarea->window,
6845 FILL_X(gui.scroll_region_left), FILL_Y(row),
6846 gui.char_width * (gui.scroll_region_right
6847 - gui.scroll_region_left + 1) + 1,
6848 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6849
6850 gui_clear_block(row, gui.scroll_region_left,
6851 row + num_lines - 1, gui.scroll_region_right);
6852 check_copy_area();
Bram Moolenaar98921892016-02-23 17:14:37 +01006853#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854}
6855
6856/*
6857 * X Selection stuff, for cutting and pasting text to other windows.
6858 */
6859 void
6860clip_mch_request_selection(VimClipboard *cbd)
6861{
6862 GdkAtom target;
6863 unsigned i;
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00006864 time_t start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865
6866 for (i = 0; i < N_SELECTION_TARGETS; ++i)
6867 {
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006868 if (!clip_html && selection_targets[i].info == TARGET_HTML)
6869 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 received_selection = RS_NONE;
6871 target = gdk_atom_intern(selection_targets[i].target, FALSE);
6872
6873 gtk_selection_convert(gui.drawarea,
6874 cbd->gtk_sel_atom, target,
6875 (guint32)GDK_CURRENT_TIME);
6876
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00006877 /* Hack: Wait up to three seconds for the selection. A hang was
6878 * noticed here when using the netrw plugin combined with ":gui"
6879 * during the FocusGained event. */
6880 start = time(NULL);
6881 while (received_selection == RS_NONE && time(NULL) < start + 3)
Bram Moolenaara3f41662010-07-11 19:01:06 +02006882 g_main_context_iteration(NULL, TRUE); /* wait for selection_received_cb */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883
6884 if (received_selection != RS_FAIL)
6885 return;
6886 }
6887
6888 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaar98921892016-02-23 17:14:37 +01006889#if GTK_CHECK_VERSION(3,0,0)
6890 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)),
6891 cbd);
6892#else
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006893 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gui.mainwin->window), cbd);
Bram Moolenaar98921892016-02-23 17:14:37 +01006894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895}
6896
6897/*
6898 * Disown the selection.
6899 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00006901clip_mch_lose_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902{
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01006903 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905}
6906
6907/*
6908 * Own the selection and return OK if it worked.
6909 */
6910 int
6911clip_mch_own_selection(VimClipboard *cbd)
6912{
6913 int success;
6914
6915 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom,
Bram Moolenaar20892c12011-06-26 04:49:00 +02006916 gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 gui_mch_update();
6918 return (success) ? OK : FAIL;
6919}
6920
6921/*
6922 * Send the current selection to the clipboard. Do nothing for X because we
6923 * will fill in the selection only when requested by another app.
6924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00006926clip_mch_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927{
6928}
6929
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01006930 int
6931clip_gtk_owner_exists(VimClipboard *cbd)
6932{
6933 return gdk_selection_owner_get(cbd->gtk_sel_atom) != NULL;
6934}
6935
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936
6937#if defined(FEAT_MENU) || defined(PROTO)
6938/*
6939 * Make a menu item appear either active or not active (grey or not grey).
6940 */
6941 void
6942gui_mch_menu_grey(vimmenu_T *menu, int grey)
6943{
6944 if (menu->id == NULL)
6945 return;
6946
6947 if (menu_is_separator(menu->name))
6948 grey = TRUE;
6949
6950 gui_mch_menu_hidden(menu, FALSE);
6951 /* Be clever about bitfields versus true booleans here! */
Bram Moolenaar98921892016-02-23 17:14:37 +01006952# if GTK_CHECK_VERSION(3,0,0)
6953 if (!gtk_widget_get_sensitive(menu->id) == !grey)
6954# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 if (!GTK_WIDGET_SENSITIVE(menu->id) == !grey)
Bram Moolenaar98921892016-02-23 17:14:37 +01006956# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 {
6958 gtk_widget_set_sensitive(menu->id, !grey);
6959 gui_mch_update();
6960 }
6961}
6962
6963/*
6964 * Make menu item hidden or not hidden.
6965 */
6966 void
6967gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
6968{
6969 if (menu->id == 0)
6970 return;
6971
6972 if (hidden)
6973 {
Bram Moolenaar98921892016-02-23 17:14:37 +01006974# if GTK_CHECK_VERSION(3,0,0)
6975 if (gtk_widget_get_visible(menu->id))
6976# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 if (GTK_WIDGET_VISIBLE(menu->id))
Bram Moolenaar98921892016-02-23 17:14:37 +01006978# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 {
6980 gtk_widget_hide(menu->id);
6981 gui_mch_update();
6982 }
6983 }
6984 else
6985 {
Bram Moolenaar98921892016-02-23 17:14:37 +01006986# if GTK_CHECK_VERSION(3,0,0)
6987 if (!gtk_widget_get_visible(menu->id))
6988# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 if (!GTK_WIDGET_VISIBLE(menu->id))
Bram Moolenaar98921892016-02-23 17:14:37 +01006990# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 {
6992 gtk_widget_show(menu->id);
6993 gui_mch_update();
6994 }
6995 }
6996}
6997
6998/*
6999 * This is called after setting all the menus to grey/hidden or not.
7000 */
7001 void
7002gui_mch_draw_menubar(void)
7003{
7004 /* just make sure that the visual changes get effect immediately */
7005 gui_mch_update();
7006}
7007#endif /* FEAT_MENU */
7008
7009/*
7010 * Scrollbar stuff.
7011 */
7012 void
7013gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
7014{
7015 if (sb->id == NULL)
7016 return;
7017
Bram Moolenaar98921892016-02-23 17:14:37 +01007018#if GTK_CHECK_VERSION(3,0,0)
7019 gtk_widget_set_visible(sb->id, flag);
7020#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 if (flag)
7022 gtk_widget_show(sb->id);
7023 else
7024 gtk_widget_hide(sb->id);
Bram Moolenaar98921892016-02-23 17:14:37 +01007025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00007027 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028}
7029
7030
7031/*
7032 * Return the RGB value of a pixel as long.
7033 */
7034 long_u
7035gui_mch_get_rgb(guicolor_T pixel)
7036{
7037 GdkColor color;
Bram Moolenaar98921892016-02-23 17:14:37 +01007038#if GTK_CHECK_VERSION(3,0,0)
7039 GdkRGBA rgba;
7040
7041 gui_gtk_get_rgb_from_pixel(pixel, &rgba);
7042
7043 color.red = rgba.red * 65535;
7044 color.green = rgba.green * 65535;
7045 color.blue = rgba.blue * 65535;
7046#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
7048 (unsigned long)pixel, &color);
Bram Moolenaar98921892016-02-23 17:14:37 +01007049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050
7051 return (((unsigned)color.red & 0xff00) << 8)
7052 | ((unsigned)color.green & 0xff00)
7053 | (((unsigned)color.blue & 0xff00) >> 8);
7054}
7055
7056/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007057 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007059 void
7060gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061{
Bram Moolenaar98921892016-02-23 17:14:37 +01007062#if GTK_CHECK_VERSION(3,0,0)
7063 gui_gtk_get_pointer(gui.drawarea, x, y, NULL);
7064#else
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007065 gdk_window_get_pointer(gui.drawarea->window, x, y, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01007066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067}
7068
7069 void
7070gui_mch_setmouse(int x, int y)
7071{
7072 /* Sorry for the Xlib call, but we can't avoid it, since there is no
7073 * internal GDK mechanism present to accomplish this. (and for good
7074 * reason...) */
Bram Moolenaar98921892016-02-23 17:14:37 +01007075#if GTK_CHECK_VERSION(3,0,0)
7076 XWarpPointer(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.drawarea)),
7077 (Window)0, GDK_WINDOW_XID(gtk_widget_get_window(gui.drawarea)),
7078 0, 0, 0U, 0U, x, y);
7079#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 XWarpPointer(GDK_WINDOW_XDISPLAY(gui.drawarea->window),
7081 (Window)0, GDK_WINDOW_XWINDOW(gui.drawarea->window),
7082 0, 0, 0U, 0U, x, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01007083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084}
7085
7086
7087#ifdef FEAT_MOUSESHAPE
7088/* The last set mouse pointer shape is remembered, to be used when it goes
7089 * from hidden to not hidden. */
7090static int last_shape = 0;
7091#endif
7092
7093/*
7094 * Use the blank mouse pointer or not.
7095 *
7096 * hide: TRUE = use blank ptr, FALSE = use parent ptr
7097 */
7098 void
7099gui_mch_mousehide(int hide)
7100{
7101 if (gui.pointer_hidden != hide)
7102 {
7103 gui.pointer_hidden = hide;
Bram Moolenaar98921892016-02-23 17:14:37 +01007104#if GTK_CHECK_VERSION(3,0,0)
7105 if (gtk_widget_get_window(gui.drawarea) && gui.blank_pointer != NULL)
7106#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 if (gui.drawarea->window && gui.blank_pointer != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01007108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 {
7110 if (hide)
Bram Moolenaar98921892016-02-23 17:14:37 +01007111#if GTK_CHECK_VERSION(3,0,0)
7112 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
7113 gui.blank_pointer);
7114#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01007116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 else
7118#ifdef FEAT_MOUSESHAPE
7119 mch_set_mouse_shape(last_shape);
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01007120#elif GTK_CHECK_VERSION(3,0,0)
7121 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122#else
7123 gdk_window_set_cursor(gui.drawarea->window, NULL);
7124#endif
7125 }
7126 }
7127}
7128
7129#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
7130
7131/* Table for shape IDs. Keep in sync with the mshape_names[] table in
7132 * misc2.c! */
7133static const int mshape_ids[] =
7134{
7135 GDK_LEFT_PTR, /* arrow */
7136 GDK_CURSOR_IS_PIXMAP, /* blank */
7137 GDK_XTERM, /* beam */
7138 GDK_SB_V_DOUBLE_ARROW, /* updown */
7139 GDK_SIZING, /* udsizing */
7140 GDK_SB_H_DOUBLE_ARROW, /* leftright */
7141 GDK_SIZING, /* lrsizing */
7142 GDK_WATCH, /* busy */
7143 GDK_X_CURSOR, /* no */
7144 GDK_CROSSHAIR, /* crosshair */
7145 GDK_HAND1, /* hand1 */
7146 GDK_HAND2, /* hand2 */
7147 GDK_PENCIL, /* pencil */
7148 GDK_QUESTION_ARROW, /* question */
7149 GDK_RIGHT_PTR, /* right-arrow */
7150 GDK_CENTER_PTR, /* up-arrow */
7151 GDK_LEFT_PTR /* last one */
7152};
7153
7154 void
7155mch_set_mouse_shape(int shape)
7156{
7157 int id;
7158 GdkCursor *c;
7159
Bram Moolenaar98921892016-02-23 17:14:37 +01007160# if GTK_CHECK_VERSION(3,0,0)
7161 if (gtk_widget_get_window(gui.drawarea) == NULL)
7162# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 if (gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01007164# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 return;
7166
7167 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
Bram Moolenaar98921892016-02-23 17:14:37 +01007168# if GTK_CHECK_VERSION(3,0,0)
7169 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
7170 gui.blank_pointer);
7171# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01007173# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 else
7175 {
7176 if (shape >= MSHAPE_NUMBERED)
7177 {
7178 id = shape - MSHAPE_NUMBERED;
7179 if (id >= GDK_LAST_CURSOR)
7180 id = GDK_LEFT_PTR;
7181 else
7182 id &= ~1; /* they are always even (why?) */
7183 }
Bram Moolenaarb85cb212009-05-17 14:24:23 +00007184 else if (shape < (int)(sizeof(mshape_ids) / sizeof(int)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 id = mshape_ids[shape];
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007186 else
7187 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188# ifdef HAVE_GTK_MULTIHEAD
7189 c = gdk_cursor_new_for_display(
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00007190 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191# else
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00007192 c = gdk_cursor_new((GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01007194# if GTK_CHECK_VERSION(3,0,0)
7195 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), c);
7196# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 gdk_window_set_cursor(gui.drawarea->window, c);
Bram Moolenaar98921892016-02-23 17:14:37 +01007198# endif
7199# if GTK_CHECK_VERSION(3,0,0)
7200 g_object_unref(G_OBJECT(c));
7201# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 gdk_cursor_destroy(c); /* Unref, actually. Bloody GTK+ 1. */
Bram Moolenaar98921892016-02-23 17:14:37 +01007203# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 }
7205 if (shape != MSHAPE_HIDE)
7206 last_shape = shape;
7207}
7208#endif /* FEAT_MOUSESHAPE */
7209
7210
7211#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
7212/*
7213 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
7214 * scaled down if the current font is not big enough, or scaled up if the image
7215 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
7216 * will be cut off if the current font is not big enough, or centered if it's
7217 * too small.
7218 */
7219# define SIGN_WIDTH (2 * gui.char_width)
7220# define SIGN_HEIGHT (gui.char_height)
7221# define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
7222
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 void
7224gui_mch_drawsign(int row, int col, int typenr)
7225{
7226 GdkPixbuf *sign;
7227
7228 sign = (GdkPixbuf *)sign_get_image(typenr);
7229
Bram Moolenaar98921892016-02-23 17:14:37 +01007230# if GTK_CHECK_VERSION(3,0,0)
7231 if (sign != NULL && gui.drawarea != NULL
7232 && gtk_widget_get_window(gui.drawarea) != NULL)
7233# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 if (sign != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01007235# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 {
7237 int width;
7238 int height;
7239 int xoffset;
7240 int yoffset;
7241 int need_scale;
7242
7243 width = gdk_pixbuf_get_width(sign);
7244 height = gdk_pixbuf_get_height(sign);
7245 /*
7246 * Decide whether we need to scale. Allow one pixel of border
7247 * width to be cut off, in order to avoid excessive scaling for
7248 * tiny differences in font size.
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007249 * Do scale to fit the height to avoid gaps because of linespacing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 */
7251 need_scale = (width > SIGN_WIDTH + 2
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007252 || height != SIGN_HEIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 || (width < 3 * SIGN_WIDTH / 4
7254 && height < 3 * SIGN_HEIGHT / 4));
7255 if (need_scale)
7256 {
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007257 double aspect;
7258 int w = width;
7259 int h = height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
7261 /* Keep the original aspect ratio */
7262 aspect = (double)height / (double)width;
7263 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect;
7264 width = MIN(width, SIGN_WIDTH);
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007265 if (((double)(MAX(height, SIGN_HEIGHT)) /
7266 (double)(MIN(height, SIGN_HEIGHT))) < 1.15)
7267 {
7268 /* Change the aspect ratio by at most 15% to fill the
7269 * available space completly. */
7270 height = (double)SIGN_HEIGHT * SIGN_ASPECT / aspect;
7271 height = MIN(height, SIGN_HEIGHT);
7272 }
7273 else
7274 height = (double)width * aspect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007276 if (w == width && h == height)
7277 {
7278 /* no change in dimensions; don't decrease reference counter
7279 * (below) */
7280 need_scale = FALSE;
7281 }
7282 else
7283 {
7284 /* This doesn't seem to be worth caching, and doing so would
7285 * complicate the code quite a bit. */
7286 sign = gdk_pixbuf_scale_simple(sign, width, height,
7287 GDK_INTERP_BILINEAR);
7288 if (sign == NULL)
7289 return; /* out of memory */
7290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 }
7292
7293 /* The origin is the upper-left corner of the pixmap. Therefore
7294 * these offset may become negative if the pixmap is smaller than
7295 * the 2x1 cells reserved for the sign icon. */
7296 xoffset = (width - SIGN_WIDTH) / 2;
7297 yoffset = (height - SIGN_HEIGHT) / 2;
7298
Bram Moolenaar98921892016-02-23 17:14:37 +01007299# if GTK_CHECK_VERSION(3,0,0)
7300 {
7301 cairo_t *cr;
7302 cairo_surface_t *bg_surf;
7303 cairo_t *bg_cr;
7304 cairo_surface_t *sign_surf;
7305 cairo_t *sign_cr;
7306
7307 cr = cairo_create(gui.surface);
7308
7309 bg_surf = cairo_surface_create_similar(gui.surface,
7310 cairo_surface_get_content(gui.surface),
7311 SIGN_WIDTH, SIGN_HEIGHT);
7312 bg_cr = cairo_create(bg_surf);
7313 set_cairo_source_rgb_from_pixel(bg_cr, gui.bgcolor->pixel);
7314 cairo_paint(bg_cr);
7315
7316 sign_surf = cairo_surface_create_similar(gui.surface,
7317 cairo_surface_get_content(gui.surface),
7318 SIGN_WIDTH, SIGN_HEIGHT);
7319 sign_cr = cairo_create(sign_surf);
7320 gdk_cairo_set_source_pixbuf(sign_cr, sign, -xoffset, -yoffset);
7321 cairo_paint(sign_cr);
7322
7323 cairo_set_operator(sign_cr, CAIRO_OPERATOR_DEST_OVER);
7324 cairo_set_source_surface(sign_cr, bg_surf, 0, 0);
7325 cairo_paint(sign_cr);
7326
7327 cairo_set_source_surface(cr, sign_surf, FILL_X(col), FILL_Y(row));
7328 cairo_paint(cr);
7329
7330 cairo_destroy(sign_cr);
7331 cairo_surface_destroy(sign_surf);
7332 cairo_destroy(bg_cr);
7333 cairo_surface_destroy(bg_surf);
7334 cairo_destroy(cr);
7335
7336 if (!gui.by_signal)
7337 gtk_widget_queue_draw_area(gui.drawarea,
7338 FILL_X(col), FILL_Y(col), width, height);
7339
7340 }
7341# else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
7343
7344 gdk_draw_rectangle(gui.drawarea->window,
7345 gui.text_gc,
7346 TRUE,
7347 FILL_X(col),
7348 FILL_Y(row),
7349 SIGN_WIDTH,
7350 SIGN_HEIGHT);
7351
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 gdk_pixbuf_render_to_drawable_alpha(sign,
7353 gui.drawarea->window,
7354 MAX(0, xoffset),
7355 MAX(0, yoffset),
7356 FILL_X(col) - MIN(0, xoffset),
7357 FILL_Y(row) - MIN(0, yoffset),
7358 MIN(width, SIGN_WIDTH),
7359 MIN(height, SIGN_HEIGHT),
7360 GDK_PIXBUF_ALPHA_BILEVEL,
7361 127,
7362 GDK_RGB_DITHER_NORMAL,
7363 0, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01007364# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 if (need_scale)
7366 g_object_unref(sign);
7367 }
7368}
7369
7370 void *
7371gui_mch_register_sign(char_u *signfile)
7372{
7373 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use)
7374 {
7375 GdkPixbuf *sign;
7376 GError *error = NULL;
7377 char_u *message;
7378
7379 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error);
7380
7381 if (error == NULL)
7382 return sign;
7383
7384 message = (char_u *)error->message;
7385
7386 if (message != NULL && input_conv.vc_type != CONV_NONE)
7387 message = string_convert(&input_conv, message, NULL);
7388
7389 if (message != NULL)
7390 {
7391 /* The error message is already translated and will be more
7392 * descriptive than anything we could possibly do ourselves. */
7393 EMSG2("E255: %s", message);
7394
7395 if (input_conv.vc_type != CONV_NONE)
7396 vim_free(message);
7397 }
7398 g_error_free(error);
7399 }
7400
7401 return NULL;
7402}
7403
7404 void
7405gui_mch_destroy_sign(void *sign)
7406{
7407 if (sign != NULL)
7408 g_object_unref(sign);
7409}
7410
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411#endif /* FEAT_SIGN_ICONS */