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