blob: d1a705b992fad46fcd2806e3423c8eb0b0a9723b [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 && GDK_WINDOW_XID(event->window) == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 && GET_X_ATOM(event->atom) == commProperty)
768 {
769 XEvent xev;
770
771 /* Translate to XLib */
772 xev.xproperty.type = PropertyNotify;
773 xev.xproperty.atom = commProperty;
774 xev.xproperty.window = commWindow;
775 xev.xproperty.state = PropertyNewValue;
Bram Moolenaar98921892016-02-23 17:14:37 +0100776 serverEventProc(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(widget)),
777 &xev, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 }
779 return FALSE;
780}
Bram Moolenaar98921892016-02-23 17:14:37 +0100781#endif /* defined(FEAT_CLIENTSERVER) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782
Bram Moolenaar7ebf4e12018-08-07 20:01:40 +0200783/*
784 * Handle changes to the "Xft/DPI" setting
785 */
786 static void
787gtk_settings_xft_dpi_changed_cb(GtkSettings *gtk_settings UNUSED,
788 GParamSpec *pspec UNUSED,
789 gpointer data UNUSED)
790{
791 // Create a new PangoContext for this screen, and initialize it
792 // with the current font if necessary.
793 if (gui.text_context != NULL)
794 g_object_unref(gui.text_context);
795
796 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
797 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
798
799 if (gui.norm_font != NULL)
800 {
801 // force default font
802 gui_mch_init_font(*p_guifont == NUL ? NULL : p_guifont, FALSE);
803 gui_set_shellsize(TRUE, FALSE, RESIZE_BOTH);
804 }
805}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200807#if GTK_CHECK_VERSION(3,0,0)
808typedef gboolean timeout_cb_type;
809#else
810typedef gint timeout_cb_type;
811#endif
812
813/*
814 * Start a timer that will invoke the specified callback.
815 * Returns the ID of the timer.
816 */
817 static guint
818timeout_add(int time, timeout_cb_type (*callback)(gpointer), int *flagp)
819{
820#if GTK_CHECK_VERSION(3,0,0)
821 return g_timeout_add((guint)time, (GSourceFunc)callback, flagp);
822#else
823 return gtk_timeout_add((guint32)time, (GtkFunction)callback, flagp);
824#endif
825}
826
827 static void
828timeout_remove(guint timer)
829{
830#if GTK_CHECK_VERSION(3,0,0)
831 g_source_remove(timer);
832#else
833 gtk_timeout_remove(timer);
834#endif
835}
836
837
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838/****************************************************************************
839 * Focus handlers:
840 */
841
842
843/*
844 * This is a simple state machine:
845 * BLINK_NONE not blinking at all
846 * BLINK_OFF blinking, cursor is not shown
847 * BLINK_ON blinking, cursor is shown
848 */
849
850#define BLINK_NONE 0
851#define BLINK_OFF 1
852#define BLINK_ON 2
853
854static int blink_state = BLINK_NONE;
855static long_u blink_waittime = 700;
856static long_u blink_ontime = 400;
857static long_u blink_offtime = 250;
858static guint blink_timer = 0;
859
Bram Moolenaar98921892016-02-23 17:14:37 +0100860#if GTK_CHECK_VERSION(3,0,0)
861 static gboolean
862gui_gtk_is_blink_on(void)
863{
864 return blink_state == BLINK_ON;
865}
Bram Moolenaar98921892016-02-23 17:14:37 +0100866#endif
867
Bram Moolenaar703a8042016-06-04 16:24:32 +0200868 int
869gui_mch_is_blinking(void)
870{
871 return blink_state != BLINK_NONE;
872}
873
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200874 int
875gui_mch_is_blink_off(void)
876{
877 return blink_state == BLINK_OFF;
878}
879
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 void
881gui_mch_set_blinking(long waittime, long on, long off)
882{
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100883#if GTK_CHECK_VERSION(3,0,0)
884 if (waittime == 0 || on == 0 || off == 0)
885 {
886 blink_mode = FALSE;
887
888 blink_waittime = 700;
889 blink_ontime = 400;
890 blink_offtime = 250;
891 }
892 else
893 {
894 blink_mode = TRUE;
895
896 blink_waittime = waittime;
897 blink_ontime = on;
898 blink_offtime = off;
899 }
900#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 blink_waittime = waittime;
902 blink_ontime = on;
903 blink_offtime = off;
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905}
906
907/*
908 * Stop the cursor blinking. Show the cursor if it wasn't shown.
909 */
910 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100911gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912{
913 if (blink_timer)
914 {
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200915 timeout_remove(blink_timer);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 blink_timer = 0;
917 }
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100918 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200921 gui_mch_flush();
922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 blink_state = BLINK_NONE;
924}
925
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200926 static timeout_cb_type
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000927blink_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928{
929 if (blink_state == BLINK_ON)
930 {
931 gui_undraw_cursor();
932 blink_state = BLINK_OFF;
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200933 blink_timer = timeout_add(blink_offtime, blink_cb, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 }
935 else
936 {
937 gui_update_cursor(TRUE, FALSE);
938 blink_state = BLINK_ON;
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200939 blink_timer = timeout_add(blink_ontime, blink_cb, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 }
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200941 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942
943 return FALSE; /* don't happen again */
944}
945
946/*
947 * Start the cursor blinking. If it was already blinking, this restarts the
948 * waiting time and shows the cursor.
949 */
950 void
951gui_mch_start_blink(void)
952{
953 if (blink_timer)
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +0200954 {
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200955 timeout_remove(blink_timer);
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +0200956 blink_timer = 0;
957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 /* Only switch blinking on if none of the times is zero */
959 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
960 {
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200961 blink_timer = timeout_add(blink_waittime, blink_cb, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 blink_state = BLINK_ON;
963 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200964 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 }
966}
967
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000969enter_notify_event(GtkWidget *widget UNUSED,
970 GdkEventCrossing *event UNUSED,
971 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972{
973 if (blink_state == BLINK_NONE)
974 gui_mch_start_blink();
975
976 /* make sure keyboard input goes there */
Bram Moolenaar98921892016-02-23 17:14:37 +0100977#if GTK_CHECK_VERSION(3,0,0)
978 if (gtk_socket_id == 0 || !gtk_widget_has_focus(gui.drawarea))
979#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 if (gtk_socket_id == 0 || !GTK_WIDGET_HAS_FOCUS(gui.drawarea))
Bram Moolenaar98921892016-02-23 17:14:37 +0100981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 gtk_widget_grab_focus(gui.drawarea);
983
984 return FALSE;
985}
986
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000988leave_notify_event(GtkWidget *widget UNUSED,
989 GdkEventCrossing *event UNUSED,
990 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991{
992 if (blink_state != BLINK_NONE)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100993 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994
995 return FALSE;
996}
997
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000999focus_in_event(GtkWidget *widget,
1000 GdkEventFocus *event UNUSED,
1001 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002{
1003 gui_focus_change(TRUE);
1004
1005 if (blink_state == BLINK_NONE)
1006 gui_mch_start_blink();
1007
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00001008 /* make sure keyboard input goes to the draw area (if this is focus for a
1009 * window) */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001010 if (widget != gui.drawarea)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001011 gtk_widget_grab_focus(gui.drawarea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012
1013 return TRUE;
1014}
1015
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001017focus_out_event(GtkWidget *widget UNUSED,
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001018 GdkEventFocus *event UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001019 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
1021 gui_focus_change(FALSE);
1022
1023 if (blink_state != BLINK_NONE)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001024 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025
1026 return TRUE;
1027}
1028
1029
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030/*
1031 * Translate a GDK key value to UTF-8 independently of the current locale.
1032 * The output is written to string, which must have room for at least 6 bytes
1033 * plus the NUL terminator. Returns the length in bytes.
1034 *
1035 * This function is used in the GTK+ 2 GUI only. The GTK+ 1 code makes use
1036 * of GdkEventKey::string instead. But event->string is evil; see here why:
1037 * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey
1038 */
1039 static int
1040keyval_to_string(unsigned int keyval, unsigned int state, char_u *string)
1041{
1042 int len;
1043 guint32 uc;
1044
1045 uc = gdk_keyval_to_unicode(keyval);
1046 if (uc != 0)
1047 {
1048 /* Check for CTRL-foo */
1049 if ((state & GDK_CONTROL_MASK) && uc >= 0x20 && uc < 0x80)
1050 {
1051 /* These mappings look arbitrary at the first glance, but in fact
1052 * resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1053 * machine. The only difference is BS vs. DEL for CTRL-8 (makes
1054 * more sense and is consistent with usual terminal behaviour). */
1055 if (uc >= '@')
1056 string[0] = uc & 0x1F;
1057 else if (uc == '2')
1058 string[0] = NUL;
1059 else if (uc >= '3' && uc <= '7')
1060 string[0] = uc ^ 0x28;
1061 else if (uc == '8')
1062 string[0] = BS;
1063 else if (uc == '?')
1064 string[0] = DEL;
1065 else
1066 string[0] = uc;
1067 len = 1;
1068 }
1069 else
1070 {
1071 /* Translate a normal key to UTF-8. This doesn't work for dead
1072 * keys of course, you _have_ to use an input method for that. */
1073 len = utf_char2bytes((int)uc, string);
1074 }
1075 }
1076 else
1077 {
1078 /* Translate keys which are represented by ASCII control codes in Vim.
1079 * There are only a few of those; most control keys are translated to
1080 * special terminal-like control sequences. */
1081 len = 1;
1082 switch (keyval)
1083 {
1084 case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab:
1085 string[0] = TAB;
1086 break;
1087 case GDK_Linefeed:
1088 string[0] = NL;
1089 break;
1090 case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter:
1091 string[0] = CAR;
1092 break;
1093 case GDK_Escape:
1094 string[0] = ESC;
1095 break;
1096 default:
1097 len = 0;
1098 break;
1099 }
1100 }
1101 string[len] = NUL;
1102
1103 return len;
1104}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001106 static int
1107modifiers_gdk2vim(guint state)
1108{
1109 int modifiers = 0;
1110
1111 if (state & GDK_SHIFT_MASK)
1112 modifiers |= MOD_MASK_SHIFT;
1113 if (state & GDK_CONTROL_MASK)
1114 modifiers |= MOD_MASK_CTRL;
1115 if (state & GDK_MOD1_MASK)
1116 modifiers |= MOD_MASK_ALT;
Bram Moolenaar580061a2010-08-13 13:57:13 +02001117#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001118 if (state & GDK_SUPER_MASK)
1119 modifiers |= MOD_MASK_META;
1120#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001121 if (state & GDK_MOD4_MASK)
1122 modifiers |= MOD_MASK_META;
1123
1124 return modifiers;
1125}
1126
1127 static int
1128modifiers_gdk2mouse(guint state)
1129{
1130 int modifiers = 0;
1131
1132 if (state & GDK_SHIFT_MASK)
1133 modifiers |= MOUSE_SHIFT;
1134 if (state & GDK_CONTROL_MASK)
1135 modifiers |= MOUSE_CTRL;
1136 if (state & GDK_MOD1_MASK)
1137 modifiers |= MOUSE_ALT;
1138
1139 return modifiers;
1140}
1141
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142/*
1143 * Main keyboard handler:
1144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001146key_press_event(GtkWidget *widget UNUSED,
1147 GdkEventKey *event,
1148 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149{
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001150 /* For GTK+ 2 we know for sure how large the string might get.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 * (That is, up to 6 bytes + NUL + CSI escapes + safety measure.) */
1152 char_u string[32], string2[32];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 guint key_sym;
1154 int len;
1155 int i;
1156 int modifiers;
1157 int key;
1158 guint state;
1159 char_u *s, *d;
1160
Bram Moolenaar98921892016-02-23 17:14:37 +01001161#if GTK_CHECK_VERSION(3,0,0)
1162 is_key_pressed = TRUE;
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001163 gui_mch_stop_blink(TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001164#endif
1165
Bram Moolenaar20892c12011-06-26 04:49:00 +02001166 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 key_sym = event->keyval;
1168 state = event->state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169
1170#ifdef FEAT_XIM
1171 if (xim_queue_key_press_event(event, TRUE))
1172 return TRUE;
1173#endif
1174
1175#ifdef FEAT_HANGULIN
1176 if (key_sym == GDK_space && (state & GDK_SHIFT_MASK))
1177 {
1178 hangul_input_state_toggle();
1179 return TRUE;
1180 }
1181#endif
1182
1183#ifdef SunXK_F36
1184 /*
1185 * These keys have bogus lookup strings, and trapping them here is
1186 * easier than trying to XRebindKeysym() on them with every possible
1187 * combination of modifiers.
1188 */
1189 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
1190 len = 0;
1191 else
1192#endif
1193 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 len = keyval_to_string(key_sym, state, string2);
1195
1196 /* Careful: convert_input() doesn't handle the NUL character.
1197 * No need to convert pure ASCII anyway, thus the len > 1 check. */
1198 if (len > 1 && input_conv.vc_type != CONV_NONE)
1199 len = convert_input(string2, len, sizeof(string2));
1200
1201 s = string2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 d = string;
1203 for (i = 0; i < len; ++i)
1204 {
1205 *d++ = s[i];
1206 if (d[-1] == CSI && d + 2 < string + sizeof(string))
1207 {
1208 /* Turn CSI into K_CSI. */
1209 *d++ = KS_EXTRA;
1210 *d++ = (int)KE_CSI;
1211 }
1212 }
1213 len = d - string;
1214 }
1215
1216 /* Shift-Tab results in Left_Tab, but we want <S-Tab> */
1217 if (key_sym == GDK_ISO_Left_Tab)
1218 {
1219 key_sym = GDK_Tab;
1220 state |= GDK_SHIFT_MASK;
1221 }
1222
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223#ifdef FEAT_MENU
1224 /* If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key
1225 * is a menu shortcut, we ignore everything with the ALT modifier. */
1226 if ((state & GDK_MOD1_MASK)
1227 && gui.menu_is_active
1228 && (*p_wak == 'y'
1229 || (*p_wak == 'm'
1230 && len == 1
1231 && gui_is_menu_shortcut(string[0]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 /* For GTK2 we return false to signify that we haven't handled the
1233 * keypress, so that gtk will handle the mnemonic or accelerator. */
1234 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235#endif
1236
1237 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
1238 * that already has the 8th bit set.
1239 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
1240 * Don't do this for double-byte encodings, it turns the char into a lead
1241 * byte. */
1242 if (len == 1
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001243 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001244#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001245 || (state & GDK_SUPER_MASK)
1246#endif
1247 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 && !(key_sym == GDK_BackSpace || key_sym == GDK_Delete)
1249 && (string[0] & 0x80) == 0
1250 && !(key_sym == GDK_Tab && (state & GDK_SHIFT_MASK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 )
1253 {
1254 string[0] |= 0x80;
1255 state &= ~GDK_MOD1_MASK; /* don't use it again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 if (enc_utf8) /* convert to utf-8 */
1257 {
1258 string[1] = string[0] & 0xbf;
1259 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
1260 if (string[1] == CSI)
1261 {
1262 string[2] = KS_EXTRA;
1263 string[3] = (int)KE_CSI;
1264 len = 4;
1265 }
1266 else
1267 len = 2;
1268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 }
1270
1271 /* Check for special keys. Also do this when len == 1 (key has an ASCII
1272 * value) to detect backspace, delete and keypad keys. */
1273 if (len == 0 || len == 1)
1274 {
1275 for (i = 0; special_keys[i].key_sym != 0; i++)
1276 {
1277 if (special_keys[i].key_sym == key_sym)
1278 {
1279 string[0] = CSI;
1280 string[1] = special_keys[i].code0;
1281 string[2] = special_keys[i].code1;
1282 len = -3;
1283 break;
1284 }
1285 }
1286 }
1287
1288 if (len == 0) /* Unrecognized key */
1289 return TRUE;
1290
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 /* Special keys (and a few others) may have modifiers. Also when using a
1292 * double-byte encoding (can't set the 8th bit). */
1293 if (len == -3 || key_sym == GDK_space || key_sym == GDK_Tab
1294 || key_sym == GDK_Return || key_sym == GDK_Linefeed
1295 || key_sym == GDK_Escape || key_sym == GDK_KP_Tab
1296 || key_sym == GDK_ISO_Enter || key_sym == GDK_3270_Enter
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001297 || (enc_dbcs && len == 1 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001298#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001299 || (state & GDK_SUPER_MASK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300#endif
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001301 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001303 modifiers = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304
1305 /*
1306 * For some keys a shift modifier is translated into another key
1307 * code.
1308 */
1309 if (len == -3)
1310 key = TO_SPECIAL(string[1], string[2]);
1311 else
1312 key = string[0];
1313
1314 key = simplify_key(key, &modifiers);
1315 if (key == CSI)
1316 key = K_CSI;
1317 if (IS_SPECIAL(key))
1318 {
1319 string[0] = CSI;
1320 string[1] = K_SECOND(key);
1321 string[2] = K_THIRD(key);
1322 len = 3;
1323 }
1324 else
1325 {
1326 string[0] = key;
1327 len = 1;
1328 }
1329
1330 if (modifiers != 0)
1331 {
1332 string2[0] = CSI;
1333 string2[1] = KS_MODIFIER;
1334 string2[2] = modifiers;
1335 add_to_input_buf(string2, 3);
1336 }
1337 }
1338
1339 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1340 || (string[0] == intr_char && intr_char != Ctrl_C)))
1341 {
1342 trash_input_buf();
1343 got_int = TRUE;
1344 }
1345
1346 add_to_input_buf(string, len);
1347
1348 /* blank out the pointer if necessary */
1349 if (p_mh)
1350 gui_mch_mousehide(TRUE);
1351
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 return TRUE;
1353}
1354
Bram Moolenaar98921892016-02-23 17:14:37 +01001355#if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001357key_release_event(GtkWidget *widget UNUSED,
1358 GdkEventKey *event,
1359 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360{
Bram Moolenaar98921892016-02-23 17:14:37 +01001361# if GTK_CHECK_VERSION(3,0,0)
1362 is_key_pressed = FALSE;
1363 gui_mch_start_blink();
1364# endif
1365# if defined(FEAT_XIM)
Bram Moolenaar20892c12011-06-26 04:49:00 +02001366 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 /*
1368 * GTK+ 2 input methods may do fancy stuff on key release events too.
1369 * With the default IM for instance, you can enter any UCS code point
1370 * by holding down CTRL-SHIFT and typing hexadecimal digits.
1371 */
1372 return xim_queue_key_press_event(event, FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001373# else
1374 return TRUE;
1375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376}
1377#endif
1378
1379
1380/****************************************************************************
1381 * Selection handlers:
1382 */
1383
Bram Moolenaar29dfa5a2018-03-20 21:24:45 +01001384/* Remember when clip_lose_selection was called from here, we must not call
1385 * gtk_selection_owner_set() then. */
1386static int in_selection_clear_event = FALSE;
1387
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001389selection_clear_event(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 GdkEventSelection *event,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001391 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392{
Bram Moolenaar29dfa5a2018-03-20 21:24:45 +01001393 in_selection_clear_event = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 if (event->selection == clip_plus.gtk_sel_atom)
1395 clip_lose_selection(&clip_plus);
1396 else
1397 clip_lose_selection(&clip_star);
Bram Moolenaar29dfa5a2018-03-20 21:24:45 +01001398 in_selection_clear_event = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 return TRUE;
1401}
1402
1403#define RS_NONE 0 /* selection_received_cb() not called yet */
1404#define RS_OK 1 /* selection_received_cb() called and OK */
1405#define RS_FAIL 2 /* selection_received_cb() called and failed */
1406static int received_selection = RS_NONE;
1407
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001409selection_received_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 GtkSelectionData *data,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001411 guint time_ UNUSED,
1412 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413{
1414 VimClipboard *cbd;
1415 char_u *text;
1416 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 guchar *tmpbuf_utf8 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 int len;
Bram Moolenaard44347f2011-06-19 01:14:29 +02001419 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
Bram Moolenaar98921892016-02-23 17:14:37 +01001421#if GTK_CHECK_VERSION(3,0,0)
1422 if (gtk_selection_data_get_selection(data) == clip_plus.gtk_sel_atom)
1423#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 if (data->selection == clip_plus.gtk_sel_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 cbd = &clip_plus;
1427 else
1428 cbd = &clip_star;
1429
Bram Moolenaar98921892016-02-23 17:14:37 +01001430#if GTK_CHECK_VERSION(3,0,0)
1431 text = (char_u *)gtk_selection_data_get_data(data);
1432 len = gtk_selection_data_get_length(data);
1433#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 text = (char_u *)data->data;
1435 len = data->length;
Bram Moolenaar98921892016-02-23 17:14:37 +01001436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
1438 if (text == NULL || len <= 0)
1439 {
1440 received_selection = RS_FAIL;
1441 /* clip_free_selection(cbd); ??? */
1442
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 return;
1444 }
1445
Bram Moolenaar98921892016-02-23 17:14:37 +01001446#if GTK_CHECK_VERSION(3,0,0)
1447 if (gtk_selection_data_get_data_type(data) == vim_atom)
1448#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 if (data->type == vim_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 {
1452 motion_type = *text++;
1453 --len;
1454 }
Bram Moolenaar98921892016-02-23 17:14:37 +01001455#if GTK_CHECK_VERSION(3,0,0)
1456 else if (gtk_selection_data_get_data_type(data) == vimenc_atom)
1457#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 else if (data->type == vimenc_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 {
1461 char_u *enc;
1462 vimconv_T conv;
1463
1464 motion_type = *text++;
1465 --len;
1466
1467 enc = text;
1468 text += STRLEN(text) + 1;
1469 len -= text - enc;
1470
1471 /* If the encoding of the text is different from 'encoding', attempt
1472 * converting it. */
1473 conv.vc_type = CONV_NONE;
1474 convert_setup(&conv, enc, p_enc);
1475 if (conv.vc_type != CONV_NONE)
1476 {
1477 tmpbuf = string_convert(&conv, text, &len);
1478 if (tmpbuf != NULL)
1479 text = tmpbuf;
1480 convert_setup(&conv, NULL, NULL);
1481 }
1482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 /* gtk_selection_data_get_text() handles all the nasty details
1485 * and targets and encodings etc. This rocks so hard. */
1486 else
1487 {
1488 tmpbuf_utf8 = gtk_selection_data_get_text(data);
1489 if (tmpbuf_utf8 != NULL)
1490 {
1491 len = STRLEN(tmpbuf_utf8);
1492 if (input_conv.vc_type != CONV_NONE)
1493 {
1494 tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len);
1495 if (tmpbuf != NULL)
1496 text = tmpbuf;
1497 }
1498 else
1499 text = tmpbuf_utf8;
1500 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001501 else if (len >= 2 && text[0] == 0xff && text[1] == 0xfe)
1502 {
1503 vimconv_T conv;
1504
1505 /* UTF-16, we get this for HTML */
1506 conv.vc_type = CONV_NONE;
1507 convert_setup_ext(&conv, (char_u *)"utf-16le", FALSE, p_enc, TRUE);
1508
1509 if (conv.vc_type != CONV_NONE)
1510 {
1511 text += 2;
1512 len -= 2;
1513 tmpbuf = string_convert(&conv, text, &len);
1514 convert_setup(&conv, NULL, NULL);
1515 }
1516 if (tmpbuf != NULL)
1517 text = tmpbuf;
1518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001521 /* Chop off any trailing NUL bytes. OpenOffice sends these. */
Bram Moolenaara76638f2010-06-05 12:49:46 +02001522 while (len > 0 && text[len - 1] == NUL)
1523 --len;
1524
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 clip_yank_selection(motion_type, text, (long)len, cbd);
1526 received_selection = RS_OK;
1527 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 g_free(tmpbuf_utf8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529}
1530
1531/*
1532 * Prepare our selection data for passing it to the external selection
1533 * client.
1534 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001536selection_get_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 GtkSelectionData *selection_data,
1538 guint info,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001539 guint time_ UNUSED,
1540 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541{
1542 char_u *string;
1543 char_u *tmpbuf;
1544 long_u tmplen;
1545 int length;
1546 int motion_type;
1547 GdkAtom type;
1548 VimClipboard *cbd;
1549
Bram Moolenaar98921892016-02-23 17:14:37 +01001550#if GTK_CHECK_VERSION(3,0,0)
1551 if (gtk_selection_data_get_selection(selection_data)
1552 == clip_plus.gtk_sel_atom)
1553#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 if (selection_data->selection == clip_plus.gtk_sel_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 cbd = &clip_plus;
1557 else
1558 cbd = &clip_star;
1559
1560 if (!cbd->owned)
1561 return; /* Shouldn't ever happen */
1562
1563 if (info != (guint)TARGET_STRING
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001564 && (!clip_html || info != (guint)TARGET_HTML)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 && info != (guint)TARGET_UTF8_STRING
1566 && info != (guint)TARGET_VIMENC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 && info != (guint)TARGET_VIM
1568 && info != (guint)TARGET_COMPOUND_TEXT
1569 && info != (guint)TARGET_TEXT)
1570 return;
1571
1572 /* get the selection from the '*'/'+' register */
1573 clip_get_selection(cbd);
1574
1575 motion_type = clip_convert_selection(&string, &tmplen, cbd);
1576 if (motion_type < 0 || string == NULL)
1577 return;
1578 /* Due to int arguments we can't handle more than G_MAXINT. Also
1579 * reserve one extra byte for NUL or the motion type; just in case.
1580 * (Not that pasting 2G of text is ever going to work, but... ;-) */
1581 length = MIN(tmplen, (long_u)(G_MAXINT - 1));
1582
1583 if (info == (guint)TARGET_VIM)
1584 {
1585 tmpbuf = alloc((unsigned)length + 1);
1586 if (tmpbuf != NULL)
1587 {
1588 tmpbuf[0] = motion_type;
1589 mch_memmove(tmpbuf + 1, string, (size_t)length);
1590 }
1591 /* For our own format, the first byte contains the motion type */
1592 ++length;
1593 vim_free(string);
1594 string = tmpbuf;
1595 type = vim_atom;
1596 }
1597
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001598 else if (info == (guint)TARGET_HTML)
1599 {
1600 vimconv_T conv;
1601
1602 /* Since we get utf-16, we probably should set it as well. */
1603 conv.vc_type = CONV_NONE;
1604 convert_setup_ext(&conv, p_enc, TRUE, (char_u *)"utf-16le", FALSE);
1605 if (conv.vc_type != CONV_NONE)
1606 {
1607 tmpbuf = string_convert(&conv, string, &length);
1608 convert_setup(&conv, NULL, NULL);
1609 vim_free(string);
1610 string = tmpbuf;
1611 }
1612
1613 /* Prepend the BOM: "fffe" */
1614 if (string != NULL)
1615 {
1616 tmpbuf = alloc(length + 2);
1617 tmpbuf[0] = 0xff;
1618 tmpbuf[1] = 0xfe;
1619 mch_memmove(tmpbuf + 2, string, (size_t)length);
1620 vim_free(string);
1621 string = tmpbuf;
1622 length += 2;
1623
Bram Moolenaar98921892016-02-23 17:14:37 +01001624#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01001625 /* Looks redundant even for GTK2 because these values are
Bram Moolenaar98921892016-02-23 17:14:37 +01001626 * overwritten by gtk_selection_data_set() that follows. */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001627 selection_data->type = selection_data->target;
1628 selection_data->format = 16; /* 16 bits per char */
Bram Moolenaar98921892016-02-23 17:14:37 +01001629#endif
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001630 gtk_selection_data_set(selection_data, html_atom, 16,
1631 string, length);
1632 vim_free(string);
1633 }
1634 return;
1635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 else if (info == (guint)TARGET_VIMENC)
1637 {
1638 int l = STRLEN(p_enc);
1639
1640 /* contents: motion_type 'encoding' NUL text */
1641 tmpbuf = alloc((unsigned)length + l + 2);
1642 if (tmpbuf != NULL)
1643 {
1644 tmpbuf[0] = motion_type;
1645 STRCPY(tmpbuf + 1, p_enc);
1646 mch_memmove(tmpbuf + l + 2, string, (size_t)length);
1647 }
1648 length += l + 2;
1649 vim_free(string);
1650 string = tmpbuf;
1651 type = vimenc_atom;
1652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 /* gtk_selection_data_set_text() handles everything for us. This is
1655 * so easy and simple and cool, it'd be insane not to use it. */
1656 else
1657 {
1658 if (output_conv.vc_type != CONV_NONE)
1659 {
1660 tmpbuf = string_convert(&output_conv, string, &length);
1661 vim_free(string);
1662 if (tmpbuf == NULL)
1663 return;
1664 string = tmpbuf;
1665 }
1666 /* Validate the string to avoid runtime warnings */
1667 if (g_utf8_validate((const char *)string, (gssize)length, NULL))
1668 {
1669 gtk_selection_data_set_text(selection_data,
1670 (const char *)string, length);
1671 }
1672 vim_free(string);
1673 return;
1674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
1676 if (string != NULL)
1677 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001678#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01001679 /* Looks redundant even for GTK2 because these values are
Bram Moolenaar98921892016-02-23 17:14:37 +01001680 * overwritten by gtk_selection_data_set() that follows. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 selection_data->type = selection_data->target;
1682 selection_data->format = 8; /* 8 bits per char */
Bram Moolenaar98921892016-02-23 17:14:37 +01001683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 gtk_selection_data_set(selection_data, type, 8, string, length);
1685 vim_free(string);
1686 }
1687}
1688
1689/*
Bram Moolenaar29695702012-05-18 17:03:18 +02001690 * Check if the GUI can be started. Called before gvimrc is sourced and
1691 * before fork().
1692 * Return OK or FAIL.
1693 */
1694 int
Bram Moolenaar717e1962016-08-10 21:28:44 +02001695gui_mch_early_init_check(int give_message)
Bram Moolenaar29695702012-05-18 17:03:18 +02001696{
1697 char_u *p;
1698
1699 /* Guess that when $DISPLAY isn't set the GUI can't start. */
1700 p = mch_getenv((char_u *)"DISPLAY");
1701 if (p == NULL || *p == NUL)
1702 {
1703 gui.dying = TRUE;
Bram Moolenaar717e1962016-08-10 21:28:44 +02001704 if (give_message)
1705 EMSG(_((char *)e_opendisp));
Bram Moolenaar29695702012-05-18 17:03:18 +02001706 return FAIL;
1707 }
1708 return OK;
1709}
1710
1711/*
1712 * Check if the GUI can be started. Called before gvimrc is sourced but after
1713 * fork().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 * Return OK or FAIL.
1715 */
1716 int
1717gui_mch_init_check(void)
1718{
Bram Moolenaar3a466a82016-01-19 17:47:25 +01001719#ifdef USE_GRESOURCE
1720 static int res_registered = FALSE;
1721
1722 if (!res_registered)
1723 {
1724 /* Call this function in the GUI process; otherwise, the resources
1725 * won't be available. Don't call it twice. */
1726 res_registered = TRUE;
1727 gui_gtk_register_resource();
1728 }
1729#endif
1730
Bram Moolenaarf80663f2016-04-05 21:56:06 +02001731#if GTK_CHECK_VERSION(3,10,0)
1732 /* Vim currently assumes that Gtk means X11, so it cannot use native Gtk
1733 * support for other backends such as Wayland. */
1734 gdk_set_allowed_backends ("x11");
1735#endif
1736
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737#ifdef FEAT_GUI_GNOME
1738 if (gtk_socket_id == 0)
1739 using_gnome = 1;
1740#endif
1741
Bram Moolenaar8dd79012013-05-21 12:52:04 +02001742 /* This defaults to argv[0], but we want it to match the name of the
1743 * shipped gvim.desktop so that Vim's windows can be associated with this
1744 * file. */
1745 g_set_prgname("gvim");
1746
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 /* Don't use gtk_init() or gnome_init(), it exits on failure. */
1748 if (!gtk_init_check(&gui_argc, &gui_argv))
1749 {
1750 gui.dying = TRUE;
Bram Moolenaarc93e7912008-07-08 10:46:08 +00001751 EMSG(_((char *)e_opendisp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 return FAIL;
1753 }
1754
1755 return OK;
1756}
1757
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758/****************************************************************************
1759 * Mouse handling callbacks
1760 */
1761
1762
1763static guint mouse_click_timer = 0;
1764static int mouse_timed_out = TRUE;
1765
1766/*
1767 * Timer used to recognize multiple clicks of the mouse button
1768 */
Bram Moolenaar4ab79682017-08-27 14:50:47 +02001769 static timeout_cb_type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770mouse_click_timer_cb(gpointer data)
1771{
1772 /* we don't use this information currently */
1773 int *timed_out = (int *) data;
1774
1775 *timed_out = TRUE;
1776 return FALSE; /* don't happen again */
1777}
1778
Bram Moolenaar4ab79682017-08-27 14:50:47 +02001779static guint motion_repeat_timer = 0;
1780static int motion_repeat_offset = FALSE;
1781static timeout_cb_type motion_repeat_timer_cb(gpointer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783 static void
1784process_motion_notify(int x, int y, GdkModifierType state)
1785{
1786 int button;
1787 int_u vim_modifiers;
Bram Moolenaar98921892016-02-23 17:14:37 +01001788#if GTK_CHECK_VERSION(3,0,0)
1789 GtkAllocation allocation;
1790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791
1792 button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1793 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1794 GDK_BUTTON5_MASK))
1795 ? MOUSE_DRAG : ' ';
1796
1797 /* If our pointer is currently hidden, then we should show it. */
1798 gui_mch_mousehide(FALSE);
1799
1800 /* Just moving the rodent above the drawing area without any button
1801 * being pressed. */
1802 if (button != MOUSE_DRAG)
1803 {
1804 gui_mouse_moved(x, y);
1805 return;
1806 }
1807
1808 /* translate modifier coding between the main engine and GTK */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001809 vim_modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
Bram Moolenaar49325942007-05-10 19:19:59 +00001811 /* inform the editor engine about the occurrence of this event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 gui_send_mouse_event(button, x, y, FALSE, vim_modifiers);
1813
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 /*
1815 * Auto repeat timer handling.
1816 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001817#if GTK_CHECK_VERSION(3,0,0)
1818 gtk_widget_get_allocation(gui.drawarea, &allocation);
1819
1820 if (x < 0 || y < 0
1821 || x >= allocation.width
1822 || y >= allocation.height)
1823#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 if (x < 0 || y < 0
1825 || x >= gui.drawarea->allocation.width
1826 || y >= gui.drawarea->allocation.height)
Bram Moolenaar98921892016-02-23 17:14:37 +01001827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 {
1829
1830 int dx;
1831 int dy;
1832 int offshoot;
1833 int delay = 10;
1834
1835 /* Calculate the maximal distance of the cursor from the drawing area.
1836 * (offshoot can't become negative here!).
1837 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001838#if GTK_CHECK_VERSION(3,0,0)
1839 dx = x < 0 ? -x : x - allocation.width;
1840 dy = y < 0 ? -y : y - allocation.height;
1841#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 dx = x < 0 ? -x : x - gui.drawarea->allocation.width;
1843 dy = y < 0 ? -y : y - gui.drawarea->allocation.height;
Bram Moolenaar98921892016-02-23 17:14:37 +01001844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
1846 offshoot = dx > dy ? dx : dy;
1847
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001848 /* Make a linearly decaying timer delay with a threshold of 5 at a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 * distance of 127 pixels from the main window.
1850 *
1851 * One could think endlessly about the most ergonomic variant here.
1852 * For example it could make sense to calculate the distance from the
1853 * drags start instead...
1854 *
1855 * Maybe a parabolic interpolation would suite us better here too...
1856 */
1857 if (offshoot > 127)
1858 {
1859 /* 5 appears to be somehow near to my perceptual limits :-). */
1860 delay = 5;
1861 }
1862 else
1863 {
1864 delay = (130 * (127 - offshoot)) / 127 + 5;
1865 }
1866
1867 /* shoot again */
1868 if (!motion_repeat_timer)
Bram Moolenaar4ab79682017-08-27 14:50:47 +02001869 motion_repeat_timer = timeout_add(delay, motion_repeat_timer_cb,
1870 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 }
1872}
1873
Bram Moolenaar98921892016-02-23 17:14:37 +01001874#if GTK_CHECK_VERSION(3,0,0)
1875 static GdkDevice *
1876gui_gtk_get_pointer_device(GtkWidget *widget)
1877{
1878 GdkWindow * const win = gtk_widget_get_window(widget);
1879 GdkDisplay * const dpy = gdk_window_get_display(win);
Bram Moolenaar30e12d22016-04-17 20:49:53 +02001880# if GTK_CHECK_VERSION(3,20,0)
1881 GdkSeat * const seat = gdk_display_get_default_seat(dpy);
1882 return gdk_seat_get_pointer(seat);
1883# else
Bram Moolenaar98921892016-02-23 17:14:37 +01001884 GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy);
1885 return gdk_device_manager_get_client_pointer(mngr);
Bram Moolenaar30e12d22016-04-17 20:49:53 +02001886# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01001887}
1888
1889 static GdkWindow *
1890gui_gtk_get_pointer(GtkWidget *widget,
1891 gint *x,
1892 gint *y,
1893 GdkModifierType *state)
1894{
1895 GdkWindow * const win = gtk_widget_get_window(widget);
1896 GdkDevice * const dev = gui_gtk_get_pointer_device(widget);
1897 return gdk_window_get_device_position(win, dev , x, y, state);
1898}
1899
Bram Moolenaar2369c152016-03-04 23:08:25 +01001900# if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar98921892016-02-23 17:14:37 +01001901 static GdkWindow *
1902gui_gtk_window_at_position(GtkWidget *widget,
1903 gint *x,
1904 gint *y)
1905{
1906 GdkDevice * const dev = gui_gtk_get_pointer_device(widget);
1907 return gdk_device_get_window_at_position(dev, x, y);
1908}
Bram Moolenaar2369c152016-03-04 23:08:25 +01001909# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01001910#endif
1911
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912/*
1913 * Timer used to recognize multiple clicks of the mouse button.
1914 */
Bram Moolenaar4ab79682017-08-27 14:50:47 +02001915 static timeout_cb_type
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001916motion_repeat_timer_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917{
1918 int x;
1919 int y;
1920 GdkModifierType state;
1921
Bram Moolenaar98921892016-02-23 17:14:37 +01001922#if GTK_CHECK_VERSION(3,0,0)
1923 gui_gtk_get_pointer(gui.drawarea, &x, &y, &state);
1924#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 gdk_window_get_pointer(gui.drawarea->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +01001926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
1928 if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1929 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1930 GDK_BUTTON5_MASK)))
1931 {
1932 motion_repeat_timer = 0;
1933 return FALSE;
1934 }
1935
1936 /* If there already is a mouse click in the input buffer, wait another
1937 * time (otherwise we would create a backlog of clicks) */
1938 if (vim_used_in_input_buf() > 10)
1939 return TRUE;
1940
1941 motion_repeat_timer = 0;
1942
1943 /*
1944 * Fake a motion event.
1945 * Trick: Pretend the mouse moved to the next character on every other
1946 * event, otherwise drag events will be discarded, because they are still
1947 * in the same character.
1948 */
1949 if (motion_repeat_offset)
1950 x += gui.char_width;
1951
1952 motion_repeat_offset = !motion_repeat_offset;
1953 process_motion_notify(x, y, state);
1954
1955 /* Don't happen again. We will get reinstalled in the synthetic event
1956 * if needed -- thus repeating should still work. */
1957 return FALSE;
1958}
1959
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001961motion_notify_event(GtkWidget *widget,
1962 GdkEventMotion *event,
1963 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964{
1965 if (event->is_hint)
1966 {
1967 int x;
1968 int y;
1969 GdkModifierType state;
1970
Bram Moolenaar98921892016-02-23 17:14:37 +01001971#if GTK_CHECK_VERSION(3,0,0)
1972 gui_gtk_get_pointer(widget, &x, &y, &state);
1973#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 gdk_window_get_pointer(widget->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +01001975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 process_motion_notify(x, y, state);
1977 }
1978 else
1979 {
1980 process_motion_notify((int)event->x, (int)event->y,
1981 (GdkModifierType)event->state);
1982 }
1983
1984 return TRUE; /* handled */
1985}
1986
1987
1988/*
1989 * Mouse button handling. Note please that we are capturing multiple click's
1990 * by our own timeout mechanism instead of the one provided by GTK+ itself.
1991 * This is due to the way the generic VIM code is recognizing multiple clicks.
1992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001994button_press_event(GtkWidget *widget,
1995 GdkEventButton *event,
1996 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997{
1998 int button;
1999 int repeated_click = FALSE;
2000 int x, y;
2001 int_u vim_modifiers;
2002
Bram Moolenaar20892c12011-06-26 04:49:00 +02002003 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002004
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 /* Make sure we have focus now we've been selected */
Bram Moolenaar98921892016-02-23 17:14:37 +01002006#if GTK_CHECK_VERSION(3,0,0)
2007 if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget))
2008#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01002010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 gtk_widget_grab_focus(widget);
2012
2013 /*
2014 * Don't let additional events about multiple clicks send by GTK to us
2015 * after the initial button press event confuse us.
2016 */
2017 if (event->type != GDK_BUTTON_PRESS)
2018 return FALSE;
2019
2020 x = event->x;
2021 y = event->y;
2022
2023 /* Handle multiple clicks */
2024 if (!mouse_timed_out && mouse_click_timer)
2025 {
Bram Moolenaar4ab79682017-08-27 14:50:47 +02002026 timeout_remove(mouse_click_timer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 mouse_click_timer = 0;
2028 repeated_click = TRUE;
2029 }
2030
2031 mouse_timed_out = FALSE;
Bram Moolenaar4ab79682017-08-27 14:50:47 +02002032 mouse_click_timer = timeout_add(p_mouset, mouse_click_timer_cb,
2033 &mouse_timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034
2035 switch (event->button)
2036 {
Bram Moolenaar88e484b2015-11-24 15:38:44 +01002037 /* Keep in sync with gui_x11.c.
2038 * Buttons 4-7 are handled in scroll_event() */
2039 case 1: button = MOUSE_LEFT; break;
2040 case 2: button = MOUSE_MIDDLE; break;
2041 case 3: button = MOUSE_RIGHT; break;
2042 case 8: button = MOUSE_X1; break;
2043 case 9: button = MOUSE_X2; break;
2044 default:
2045 return FALSE; /* Unknown button */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 }
2047
2048#ifdef FEAT_XIM
2049 /* cancel any preediting */
2050 if (im_is_preediting())
2051 xim_reset();
2052#endif
2053
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002054 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055
2056 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057
2058 return TRUE;
2059}
2060
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061/*
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002062 * GTK+ 2 abstracts scrolling via the GdkEventScroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002065scroll_event(GtkWidget *widget,
2066 GdkEventScroll *event,
2067 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068{
2069 int button;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002070 int_u vim_modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
Bram Moolenaar98921892016-02-23 17:14:37 +01002072#if GTK_CHECK_VERSION(3,0,0)
2073 if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget))
2074#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01002076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 gtk_widget_grab_focus(widget);
2078
2079 switch (event->direction)
2080 {
2081 case GDK_SCROLL_UP:
2082 button = MOUSE_4;
2083 break;
2084 case GDK_SCROLL_DOWN:
2085 button = MOUSE_5;
2086 break;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002087 case GDK_SCROLL_LEFT:
2088 button = MOUSE_7;
2089 break;
2090 case GDK_SCROLL_RIGHT:
2091 button = MOUSE_6;
2092 break;
2093 default: /* This shouldn't happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 return FALSE;
2095 }
2096
2097# ifdef FEAT_XIM
2098 /* cancel any preediting */
2099 if (im_is_preediting())
2100 xim_reset();
2101# endif
2102
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002103 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
2105 gui_send_mouse_event(button, (int)event->x, (int)event->y,
2106 FALSE, vim_modifiers);
2107
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 return TRUE;
2109}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110
2111
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002113button_release_event(GtkWidget *widget UNUSED,
2114 GdkEventButton *event,
2115 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116{
2117 int x, y;
2118 int_u vim_modifiers;
2119
Bram Moolenaar20892c12011-06-26 04:49:00 +02002120 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002121
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 /* Remove any motion "machine gun" timers used for automatic further
2123 extension of allocation areas if outside of the applications window
2124 area .*/
2125 if (motion_repeat_timer)
2126 {
Bram Moolenaar4ab79682017-08-27 14:50:47 +02002127 timeout_remove(motion_repeat_timer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 motion_repeat_timer = 0;
2129 }
2130
2131 x = event->x;
2132 y = event->y;
2133
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002134 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135
2136 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137
2138 return TRUE;
2139}
2140
2141
2142#ifdef FEAT_DND
2143/****************************************************************************
2144 * Drag aNd Drop support handlers.
2145 */
2146
2147/*
2148 * Count how many items there may be and separate them with a NUL.
2149 * Apparently the items are separated with \r\n. This is not documented,
2150 * thus be careful not to go past the end. Also allow separation with
2151 * NUL characters.
2152 */
2153 static int
2154count_and_decode_uri_list(char_u *out, char_u *raw, int len)
2155{
2156 int i;
2157 char_u *p = out;
2158 int count = 0;
2159
2160 for (i = 0; i < len; ++i)
2161 {
2162 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r')
2163 {
2164 if (p > out && p[-1] != NUL)
2165 {
2166 ++count;
2167 *p++ = NUL;
2168 }
2169 }
2170 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0)
2171 {
2172 *p++ = hexhex2nr(raw + i + 1);
2173 i += 2;
2174 }
2175 else
2176 *p++ = raw[i];
2177 }
2178 if (p > out && p[-1] != NUL)
2179 {
2180 *p = NUL; /* last item didn't have \r or \n */
2181 ++count;
2182 }
2183 return count;
2184}
2185
2186/*
2187 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
2188 * this process, URI which protocol is not "file:" are removed. Return
2189 * length of array (less than "max").
2190 */
2191 static int
2192filter_uri_list(char_u **outlist, int max, char_u *src)
2193{
2194 int i, j;
2195
2196 for (i = j = 0; i < max; ++i)
2197 {
2198 outlist[i] = NULL;
2199 if (STRNCMP(src, "file:", 5) == 0)
2200 {
2201 src += 5;
2202 if (STRNCMP(src, "//localhost", 11) == 0)
2203 src += 11;
2204 while (src[0] == '/' && src[1] == '/')
2205 ++src;
2206 outlist[j++] = vim_strsave(src);
2207 }
2208 src += STRLEN(src) + 1;
2209 }
2210 return j;
2211}
2212
2213 static char_u **
2214parse_uri_list(int *count, char_u *data, int len)
2215{
2216 int n = 0;
2217 char_u *tmp = NULL;
Bram Moolenaar945ec092016-06-08 21:17:43 +02002218 char_u **array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219
2220 if (data != NULL && len > 0 && (tmp = (char_u *)alloc(len + 1)) != NULL)
2221 {
2222 n = count_and_decode_uri_list(tmp, data, len);
2223 if (n > 0 && (array = (char_u **)alloc(n * sizeof(char_u *))) != NULL)
2224 n = filter_uri_list(array, n, tmp);
2225 }
2226 vim_free(tmp);
2227 *count = n;
2228 return array;
2229}
2230
2231 static void
2232drag_handle_uri_list(GdkDragContext *context,
2233 GtkSelectionData *data,
2234 guint time_,
2235 GdkModifierType state,
2236 gint x,
2237 gint y)
2238{
2239 char_u **fnames;
2240 int nfiles = 0;
2241
Bram Moolenaar98921892016-02-23 17:14:37 +01002242# if GTK_CHECK_VERSION(3,0,0)
2243 fnames = parse_uri_list(&nfiles,
2244 (char_u *)gtk_selection_data_get_data(data),
2245 gtk_selection_data_get_length(data));
2246# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 fnames = parse_uri_list(&nfiles, data->data, data->length);
Bram Moolenaar98921892016-02-23 17:14:37 +01002248# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249
2250 if (fnames != NULL && nfiles > 0)
2251 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002252 int_u modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253
2254 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
2255
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002256 modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257
2258 gui_handle_drop(x, y, modifiers, fnames, nfiles);
2259 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002260 else
2261 vim_free(fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262}
2263
2264 static void
2265drag_handle_text(GdkDragContext *context,
2266 GtkSelectionData *data,
2267 guint time_,
2268 GdkModifierType state)
2269{
2270 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP};
2271 char_u *text;
2272 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274
Bram Moolenaar98921892016-02-23 17:14:37 +01002275# if GTK_CHECK_VERSION(3,0,0)
2276 text = (char_u *)gtk_selection_data_get_data(data);
2277 len = gtk_selection_data_get_length(data);
2278# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 text = data->data;
2280 len = data->length;
Bram Moolenaar98921892016-02-23 17:14:37 +01002281# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
Bram Moolenaar98921892016-02-23 17:14:37 +01002283# if GTK_CHECK_VERSION(3,0,0)
2284 if (gtk_selection_data_get_data_type(data) == utf8_string_atom)
2285# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 if (data->type == utf8_string_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01002287# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 if (input_conv.vc_type != CONV_NONE)
2290 tmpbuf = string_convert(&input_conv, text, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 if (tmpbuf != NULL)
2292 text = tmpbuf;
2293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294
2295 dnd_yank_drag_data(text, (long)len);
2296 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002299 dropkey[2] = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300
2301 if (dropkey[2] != 0)
2302 add_to_input_buf(dropkey, (int)sizeof(dropkey));
2303 else
2304 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305}
2306
2307/*
2308 * DND receiver.
2309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 static void
2311drag_data_received_cb(GtkWidget *widget,
2312 GdkDragContext *context,
2313 gint x,
2314 gint y,
2315 GtkSelectionData *data,
2316 guint info,
2317 guint time_,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002318 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319{
2320 GdkModifierType state;
2321
2322 /* Guard against trash */
Bram Moolenaar98921892016-02-23 17:14:37 +01002323# if GTK_CHECK_VERSION(3,0,0)
2324 const guchar * const data_data = gtk_selection_data_get_data(data);
2325 const gint data_length = gtk_selection_data_get_length(data);
2326 const gint data_format = gtk_selection_data_get_format(data);
2327
2328 if (data_data == NULL
2329 || data_length <= 0
2330 || data_format != 8
2331 || data_data[data_length] != '\0')
2332# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 if (data->data == NULL
2334 || data->length <= 0
2335 || data->format != 8
2336 || data->data[data->length] != '\0')
Bram Moolenaar98921892016-02-23 17:14:37 +01002337# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
2339 gtk_drag_finish(context, FALSE, FALSE, time_);
2340 return;
2341 }
2342
2343 /* Get the current modifier state for proper distinguishment between
2344 * different operations later. */
Bram Moolenaar98921892016-02-23 17:14:37 +01002345#if GTK_CHECK_VERSION(3,0,0)
2346 gui_gtk_get_pointer(widget, NULL, NULL, &state);
2347# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 gdk_window_get_pointer(widget->window, NULL, NULL, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +01002349# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350
2351 /* Not sure about the role of "text/plain" here... */
2352 if (info == (guint)TARGET_TEXT_URI_LIST)
2353 drag_handle_uri_list(context, data, time_, state, x, y);
2354 else
2355 drag_handle_text(context, data, time_, state);
2356
2357}
2358#endif /* FEAT_DND */
2359
2360
2361#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2362/*
2363 * GnomeClient interact callback. Check for unsaved buffers that cannot
2364 * be abandoned and pop up a dialog asking the user for confirmation if
2365 * necessary.
2366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002368sm_client_check_changed_any(GnomeClient *client UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 gint key,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002370 GnomeDialogType type UNUSED,
2371 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372{
2373 cmdmod_T save_cmdmod;
2374 gboolean shutdown_cancelled;
2375
2376 save_cmdmod = cmdmod;
2377
2378# ifdef FEAT_BROWSE
2379 cmdmod.browse = TRUE;
2380# endif
2381# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2382 cmdmod.confirm = TRUE;
2383# endif
2384 /*
2385 * If there are changed buffers, present the user with
2386 * a dialog if possible, otherwise give an error message.
2387 */
Bram Moolenaar027387f2016-01-02 22:25:52 +01002388 shutdown_cancelled = check_changed_any(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389
2390 exiting = FALSE;
2391 cmdmod = save_cmdmod;
2392 setcursor(); /* position the cursor */
2393 out_flush();
2394 /*
2395 * If the user hit the [Cancel] button the whole shutdown
2396 * will be cancelled. Wow, quite powerful feature (:
2397 */
2398 gnome_interaction_key_return(key, shutdown_cancelled);
2399}
2400
2401/*
2402 * Generate a script that can be used to restore the current editing session.
2403 * Save the value of v:this_session before running :mksession in order to make
2404 * automagic session save fully transparent. Return TRUE on success.
2405 */
2406 static int
2407write_session_file(char_u *filename)
2408{
2409 char_u *escaped_filename;
2410 char *mksession_cmdline;
2411 unsigned int save_ssop_flags;
2412 int failed;
2413
2414 /*
2415 * Build an ex command line to create a script that restores the current
2416 * session if executed. Escape the filename to avoid nasty surprises.
2417 */
2418 escaped_filename = vim_strsave_escaped(filename, escape_chars);
2419 if (escaped_filename == NULL)
2420 return FALSE;
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002421 mksession_cmdline = g_strconcat("mksession ", (char *)escaped_filename,
2422 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 vim_free(escaped_filename);
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002424
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 /*
2426 * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
2427 * unpredictable effects when the session is saved automatically. Also,
2428 * we definitely need SSOP_GLOBALS to be able to restore v:this_session.
2429 * Don't use SSOP_BUFFERS to prevent the buffer list from becoming
2430 * enormously large if the GNOME session feature is used regularly.
2431 */
2432 save_ssop_flags = ssop_flags;
2433 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002434 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435
2436 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
2437 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
2438 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
Bram Moolenaar485db9b2005-01-31 19:23:41 +00002439 do_unlet((char_u *)"Save_VV_this_session", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440
2441 ssop_flags = save_ssop_flags;
2442 g_free(mksession_cmdline);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002443
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 /*
2445 * Reopen the file and append a command to restore v:this_session,
2446 * as if this save never happened. This is to avoid conflicts with
2447 * the user's own sessions. FIXME: It's probably less hackish to add
2448 * a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
2449 */
2450 if (!failed)
2451 {
2452 FILE *fd;
2453
2454 fd = open_exfile(filename, TRUE, APPENDBIN);
2455
2456 failed = (fd == NULL
2457 || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL
2458 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
2459
2460 if (fd != NULL && fclose(fd) != 0)
2461 failed = TRUE;
2462
2463 if (failed)
2464 mch_remove(filename);
2465 }
2466
2467 return !failed;
2468}
2469
2470/*
2471 * "save_yourself" signal handler. Initiate an interaction to ask the user
2472 * for confirmation if necessary. Save the current editing session and tell
2473 * the session manager how to restart Vim.
2474 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 static gboolean
2476sm_client_save_yourself(GnomeClient *client,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002477 gint phase UNUSED,
2478 GnomeSaveStyle save_style UNUSED,
2479 gboolean shutdown UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 GnomeInteractStyle interact_style,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002481 gboolean fast UNUSED,
2482 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483{
2484 static const char suffix[] = "-session.vim";
2485 char *session_file;
2486 unsigned int len;
2487 gboolean success;
2488
2489 /* Always request an interaction if possible. check_changed_any()
2490 * won't actually show a dialog unless any buffers have been modified.
2491 * There doesn't seem to be an obvious way to check that without
2492 * automatically firing the dialog. Anyway, it works just fine. */
2493 if (interact_style == GNOME_INTERACT_ANY)
2494 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL,
2495 &sm_client_check_changed_any,
2496 NULL);
2497 out_flush();
2498 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2499
2500 /* The path is unique for each session save. We do neither know nor care
2501 * which session script will actually be used later. This decision is in
2502 * the domain of the session manager. */
2503 session_file = gnome_config_get_real_path(
2504 gnome_client_get_config_prefix(client));
2505 len = strlen(session_file);
2506
2507 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR)
2508 --len; /* get rid of the superfluous trailing '/' */
2509
2510 session_file = g_renew(char, session_file, len + sizeof(suffix));
2511 memcpy(session_file + len, suffix, sizeof(suffix));
2512
2513 success = write_session_file((char_u *)session_file);
2514
2515 if (success)
2516 {
2517 const char *argv[8];
2518 int i;
2519
2520 /* Tell the session manager how to wipe out the stored session data.
2521 * This isn't as dangerous as it looks, don't worry :) session_file
2522 * is a unique absolute filename. Usually it'll be something like
2523 * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */
2524 i = 0;
2525 argv[i++] = "rm";
2526 argv[i++] = session_file;
2527 argv[i] = NULL;
2528
2529 gnome_client_set_discard_command(client, i, (char **)argv);
2530
2531 /* Tell the session manager how to restore the just saved session.
2532 * This is easily done thanks to Vim's -S option. Pass the -f flag
2533 * since there's no need to fork -- it might even cause confusion.
2534 * Also pass the window role to give the WM something to match on.
2535 * The role is set in gui_mch_open(), thus should _never_ be NULL. */
2536 i = 0;
2537 argv[i++] = restart_command;
2538 argv[i++] = "-f";
2539 argv[i++] = "-g";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 argv[i++] = "--role";
2541 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 argv[i++] = "-S";
2543 argv[i++] = session_file;
2544 argv[i] = NULL;
2545
2546 gnome_client_set_restart_command(client, i, (char **)argv);
2547 gnome_client_set_clone_command(client, 0, NULL);
2548 }
2549
2550 g_free(session_file);
2551
2552 return success;
2553}
2554
2555/*
2556 * Called when the session manager wants us to die. There isn't much to save
2557 * here since "save_yourself" has been emitted before (unless serious trouble
2558 * is happening).
2559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002561sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562{
2563 /* Don't write messages to the GUI anymore */
2564 full_screen = FALSE;
2565
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002566 vim_strncpy(IObuff, (char_u *)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002567 _("Vim: Received \"die\" request from session manager\n"),
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002568 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 preserve_exit();
2570}
2571
2572/*
2573 * Connect our signal handlers to be notified on session save and shutdown.
2574 */
2575 static void
2576setup_save_yourself(void)
2577{
2578 GnomeClient *client;
2579
2580 client = gnome_master_client();
2581
2582 if (client != NULL)
2583 {
2584 /* Must use the deprecated gtk_signal_connect() for compatibility
2585 * with GNOME 1. Arrgh, zombies! */
2586 gtk_signal_connect(GTK_OBJECT(client), "save_yourself",
2587 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL);
2588 gtk_signal_connect(GTK_OBJECT(client), "die",
2589 GTK_SIGNAL_FUNC(&sm_client_die), NULL);
2590 }
2591}
2592
2593#else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2594
2595# ifdef USE_XSMP
2596/*
2597 * GTK tells us that XSMP needs attention
2598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 static gboolean
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002600local_xsmp_handle_requests(
2601 GIOChannel *source UNUSED,
2602 GIOCondition condition,
2603 gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604{
2605 if (condition == G_IO_IN)
2606 {
2607 /* Do stuff; maybe close connection */
2608 if (xsmp_handle_requests() == FAIL)
2609 g_io_channel_unref((GIOChannel *)data);
2610 return TRUE;
2611 }
2612 /* Error */
2613 g_io_channel_unref((GIOChannel *)data);
2614 xsmp_close();
2615 return TRUE;
2616}
2617# endif /* USE_XSMP */
2618
2619/*
2620 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2621 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2622 */
2623 static void
2624setup_save_yourself(void)
2625{
2626 Atom *existing_atoms = NULL;
2627 int count = 0;
2628
Bram Moolenaar98921892016-02-23 17:14:37 +01002629# ifdef USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 if (xsmp_icefd != -1)
2631 {
2632 /*
2633 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2634 * set up GTK IO monitor
2635 */
2636 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd);
2637
2638 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP,
2639 local_xsmp_handle_requests, (gpointer)g_io);
Bram Moolenaar98921892016-02-23 17:14:37 +01002640 g_io_channel_unref(g_io);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 }
2642 else
Bram Moolenaar98921892016-02-23 17:14:37 +01002643# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 {
2645 /* Fall back to old method */
2646
2647 /* first get the existing value */
Bram Moolenaar98921892016-02-23 17:14:37 +01002648 GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin);
2649
2650 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win),
2651 GDK_WINDOW_XID(mainwin_win),
2652 &existing_atoms, &count))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 {
2654 Atom *new_atoms;
2655 Atom save_yourself_xatom;
2656 int i;
2657
2658 save_yourself_xatom = GET_X_ATOM(save_yourself_atom);
2659
2660 /* check if WM_SAVE_YOURSELF isn't there yet */
2661 for (i = 0; i < count; ++i)
2662 if (existing_atoms[i] == save_yourself_xatom)
2663 break;
2664
2665 if (i == count)
2666 {
2667 /* allocate an Atoms array which is one item longer */
2668 new_atoms = (Atom *)alloc((unsigned)((count + 1)
2669 * sizeof(Atom)));
2670 if (new_atoms != NULL)
2671 {
2672 memcpy(new_atoms, existing_atoms, count * sizeof(Atom));
2673 new_atoms[count] = save_yourself_xatom;
Bram Moolenaar98921892016-02-23 17:14:37 +01002674 XSetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win),
2675 GDK_WINDOW_XID(mainwin_win),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 new_atoms, count + 1);
2677 vim_free(new_atoms);
2678 }
2679 }
2680 XFree(existing_atoms);
2681 }
2682 }
2683}
2684
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685/*
2686 * Installing a global event filter seems to be the only way to catch
2687 * client messages of type WM_PROTOCOLS without overriding GDK's own
2688 * client message event filter. Well, that's still better than trying
2689 * to guess what the GDK filter had done if it had been invoked instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 *
2691 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2692 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2693 * I have the nasty feeling modern session managers just don't send this
2694 * deprecated message anymore. Addition: confirmed by several people.
2695 *
2696 * The GNOME session support is much cooler anyway. Unlike this ugly
2697 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2698 * it should work with KDE as well.
2699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 static GdkFilterReturn
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002701global_event_filter(GdkXEvent *xev,
2702 GdkEvent *event UNUSED,
2703 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704{
2705 XEvent *xevent = (XEvent *)xev;
2706
2707 if (xevent != NULL
2708 && xevent->type == ClientMessage
2709 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002710 && (long_u)xevent->xclient.data.l[0]
2711 == GET_X_ATOM(save_yourself_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 {
2713 out_flush();
2714 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2715 /*
2716 * Set the window's WM_COMMAND property, to let the window manager
2717 * know we are done saving ourselves. We don't want to be
2718 * restarted, thus set argv to NULL.
2719 */
Bram Moolenaar98921892016-02-23 17:14:37 +01002720 XSetCommand(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)),
2721 GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 NULL, 0);
2723 return GDK_FILTER_REMOVE;
2724 }
2725
2726 return GDK_FILTER_CONTINUE;
2727}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728#endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2729
2730
2731/*
2732 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2733 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002735mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736{
2737/* If you get an error message here, you still need to unpack the runtime
2738 * archive! */
2739#ifdef magick
2740# undef magick
2741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 /* A bit hackish, but avoids casting later and allows optimization */
2743# define static static const
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744#define magick vim32x32
2745#include "../runtime/vim32x32.xpm"
2746#undef magick
2747#define magick vim16x16
2748#include "../runtime/vim16x16.xpm"
2749#undef magick
2750#define magick vim48x48
2751#include "../runtime/vim48x48.xpm"
2752#undef magick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753# undef static
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754
Bram Moolenaar98921892016-02-23 17:14:37 +01002755 GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin);
Bram Moolenaar98921892016-02-23 17:14:37 +01002756
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 /* When started with "--echo-wid" argument, write window ID on stdout. */
2758 if (echo_wid_arg)
2759 {
Bram Moolenaar98921892016-02-23 17:14:37 +01002760 printf("WID: %ld\n", (long)GDK_WINDOW_XID(mainwin_win));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 fflush(stdout);
2762 }
2763
2764 if (vim_strchr(p_go, GO_ICON) != NULL)
2765 {
2766 /*
2767 * Add an icon to the main window. For fun and convenience of the user.
2768 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 GList *icons = NULL;
2770
2771 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16));
2772 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32));
2773 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48));
2774
2775 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons);
2776
2777 g_list_foreach(icons, (GFunc)&g_object_unref, NULL);
2778 g_list_free(icons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 }
2780
2781#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
2782 /* Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 gdk_window_add_filter(NULL, &global_event_filter, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784#endif
2785 /* Setup to indicate to the window manager that we want to catch the
2786 * WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2787 * manager instead. */
2788#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2789 if (using_gnome)
2790#endif
2791 setup_save_yourself();
2792
2793#ifdef FEAT_CLIENTSERVER
2794 if (serverName == NULL && serverDelayedStartName != NULL)
2795 {
2796 /* This is a :gui command in a plain vim with no previous server */
Bram Moolenaar98921892016-02-23 17:14:37 +01002797 commWindow = GDK_WINDOW_XID(mainwin_win);
2798
2799 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(mainwin_win),
2800 serverDelayedStartName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 }
2802 else
2803 {
2804 /*
2805 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2806 * have to change the "server" registration to that of the main window
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002807 * If we have not registered a name yet, remember the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 */
Bram Moolenaar98921892016-02-23 17:14:37 +01002809 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(mainwin_win),
2810 GDK_WINDOW_XID(mainwin_win));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 }
2812 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK);
Bram Moolenaar98921892016-02-23 17:14:37 +01002813# if GTK_CHECK_VERSION(3,0,0)
2814 g_signal_connect(G_OBJECT(gui.mainwin), "property-notify-event",
2815 G_CALLBACK(property_event), NULL);
2816# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "property_notify_event",
2818 GTK_SIGNAL_FUNC(property_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01002819# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820#endif
2821}
2822
2823 static GdkCursor *
2824create_blank_pointer(void)
2825{
2826 GdkWindow *root_window = NULL;
Bram Moolenaar98921892016-02-23 17:14:37 +01002827#if GTK_CHECK_VERSION(3,0,0)
2828 GdkPixbuf *blank_mask;
2829#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 GdkPixmap *blank_mask;
Bram Moolenaar98921892016-02-23 17:14:37 +01002831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 GdkCursor *cursor;
Bram Moolenaar36edf062016-07-21 22:10:12 +02002833#if GTK_CHECK_VERSION(3,0,0)
2834 GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
2835#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 GdkColor color = { 0, 0, 0, 0 };
2837 char blank_data[] = { 0x0 };
Bram Moolenaar98921892016-02-23 17:14:37 +01002838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002840#if GTK_CHECK_VERSION(3,12,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01002841 {
2842 GdkWindow * const win = gtk_widget_get_window(gui.mainwin);
2843 GdkScreen * const scrn = gdk_window_get_screen(win);
2844 root_window = gdk_screen_get_root_window(scrn);
2845 }
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002846#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 root_window = gtk_widget_get_root_window(gui.mainwin);
2848#endif
2849
2850 /* Create a pseudo blank pointer, which is in fact one pixel by one pixel
2851 * in size. */
Bram Moolenaar98921892016-02-23 17:14:37 +01002852#if GTK_CHECK_VERSION(3,0,0)
2853 {
2854 cairo_surface_t *surf;
2855 cairo_t *cr;
2856
2857 surf = cairo_image_surface_create(CAIRO_FORMAT_A1, 1, 1);
2858 cr = cairo_create(surf);
2859
Bram Moolenaar36edf062016-07-21 22:10:12 +02002860 cairo_set_source_rgba(cr,
2861 color.red,
2862 color.green,
2863 color.blue,
2864 color.alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01002865 cairo_rectangle(cr, 0, 0, 1, 1);
2866 cairo_fill(cr);
2867 cairo_destroy(cr);
2868
2869 blank_mask = gdk_pixbuf_get_from_surface(surf, 0, 0, 1, 1);
2870 cairo_surface_destroy(surf);
2871
2872 cursor = gdk_cursor_new_from_pixbuf(gdk_window_get_display(root_window),
2873 blank_mask, 0, 0);
2874 g_object_unref(blank_mask);
2875 }
2876#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1);
2878 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask,
2879 &color, &color, 0, 0);
2880 gdk_bitmap_unref(blank_mask);
Bram Moolenaar98921892016-02-23 17:14:37 +01002881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882
2883 return cursor;
2884}
2885
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 static void
2887mainwin_screen_changed_cb(GtkWidget *widget,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002888 GdkScreen *previous_screen UNUSED,
2889 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890{
2891 if (!gtk_widget_has_screen(widget))
2892 return;
2893
2894 /*
Bram Moolenaar49325942007-05-10 19:19:59 +00002895 * Recreate the invisible mouse cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 */
2897 if (gui.blank_pointer != NULL)
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002898#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01002899 g_object_unref(G_OBJECT(gui.blank_pointer));
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002900#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 gdk_cursor_unref(gui.blank_pointer);
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903
2904 gui.blank_pointer = create_blank_pointer();
2905
Bram Moolenaar98921892016-02-23 17:14:37 +01002906 if (gui.pointer_hidden && gtk_widget_get_window(gui.drawarea) != NULL)
2907 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
2908 gui.blank_pointer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909
2910 /*
2911 * Create a new PangoContext for this screen, and initialize it
2912 * with the current font if necessary.
2913 */
2914 if (gui.text_context != NULL)
2915 g_object_unref(gui.text_context);
2916
2917 gui.text_context = gtk_widget_create_pango_context(widget);
2918 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
2919
2920 if (gui.norm_font != NULL)
2921 {
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002922 gui_mch_init_font(p_guifont, FALSE);
Bram Moolenaar8ac44152017-11-09 18:33:29 +01002923 gui_set_shellsize(TRUE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 }
2925}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926
2927/*
2928 * After the drawing area comes up, we calculate all colors and create the
2929 * dummy blank cursor.
2930 *
2931 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2932 * fact that the main VIM engine doesn't take them into account anywhere.
2933 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002935drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936{
2937 GtkWidget *sbar;
Bram Moolenaar98921892016-02-23 17:14:37 +01002938#if GTK_CHECK_VERSION(3,0,0)
2939 GtkAllocation allocation;
2940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941
2942#ifdef FEAT_XIM
2943 xim_init();
2944#endif
2945 gui_mch_new_colors();
Bram Moolenaar98921892016-02-23 17:14:37 +01002946#if GTK_CHECK_VERSION(3,0,0)
2947 gui.surface = gdk_window_create_similar_surface(
2948 gtk_widget_get_window(widget),
2949 CAIRO_CONTENT_COLOR_ALPHA,
2950 gtk_widget_get_allocated_width(widget),
2951 gtk_widget_get_allocated_height(widget));
2952#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 gui.text_gc = gdk_gc_new(gui.drawarea->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01002954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955
2956 gui.blank_pointer = create_blank_pointer();
2957 if (gui.pointer_hidden)
Bram Moolenaar98921892016-02-23 17:14:37 +01002958 gdk_window_set_cursor(gtk_widget_get_window(widget), gui.blank_pointer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959
2960 /* get the actual size of the scrollbars, if they are realized */
2961 sbar = firstwin->w_scrollbars[SBAR_LEFT].id;
2962 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT]
2963 && firstwin->w_scrollbars[SBAR_RIGHT].id))
2964 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id;
Bram Moolenaar98921892016-02-23 17:14:37 +01002965#if GTK_CHECK_VERSION(3,0,0)
2966 gtk_widget_get_allocation(sbar, &allocation);
2967 if (sbar && gtk_widget_get_realized(sbar) && allocation.width)
2968 gui.scrollbar_width = allocation.width;
2969#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.width)
2971 gui.scrollbar_width = sbar->allocation.width;
Bram Moolenaar98921892016-02-23 17:14:37 +01002972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973
2974 sbar = gui.bottom_sbar.id;
Bram Moolenaar98921892016-02-23 17:14:37 +01002975#if GTK_CHECK_VERSION(3,0,0)
2976 if (sbar && gtk_widget_get_realized(sbar) && allocation.height)
2977 gui.scrollbar_height = allocation.height;
2978#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.height)
2980 gui.scrollbar_height = sbar->allocation.height;
Bram Moolenaar98921892016-02-23 17:14:37 +01002981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982}
2983
2984/*
2985 * Properly clean up on shutdown.
2986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002988drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989{
2990 /* Don't write messages to the GUI anymore */
2991 full_screen = FALSE;
2992
2993#ifdef FEAT_XIM
2994 im_shutdown();
2995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 if (gui.ascii_glyphs != NULL)
2997 {
2998 pango_glyph_string_free(gui.ascii_glyphs);
2999 gui.ascii_glyphs = NULL;
3000 }
3001 if (gui.ascii_font != NULL)
3002 {
3003 g_object_unref(gui.ascii_font);
3004 gui.ascii_font = NULL;
3005 }
3006 g_object_unref(gui.text_context);
3007 gui.text_context = NULL;
3008
Bram Moolenaar98921892016-02-23 17:14:37 +01003009#if GTK_CHECK_VERSION(3,0,0)
3010 if (gui.surface != NULL)
3011 {
3012 cairo_surface_destroy(gui.surface);
3013 gui.surface = NULL;
3014 }
3015#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 g_object_unref(gui.text_gc);
3017 gui.text_gc = NULL;
Bram Moolenaar98921892016-02-23 17:14:37 +01003018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019
Bram Moolenaar98921892016-02-23 17:14:37 +01003020#if GTK_CHECK_VERSION(3,0,0)
3021 g_object_unref(G_OBJECT(gui.blank_pointer));
3022#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 gdk_cursor_unref(gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01003024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 gui.blank_pointer = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026}
3027
Bram Moolenaara859f042016-11-17 19:11:55 +01003028#if GTK_CHECK_VERSION(3,22,2)
3029 static void
3030drawarea_style_updated_cb(GtkWidget *widget UNUSED,
3031 gpointer data UNUSED)
3032#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003034drawarea_style_set_cb(GtkWidget *widget UNUSED,
3035 GtkStyle *previous_style UNUSED,
3036 gpointer data UNUSED)
Bram Moolenaara859f042016-11-17 19:11:55 +01003037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038{
3039 gui_mch_new_colors();
3040}
3041
Bram Moolenaar98921892016-02-23 17:14:37 +01003042#if GTK_CHECK_VERSION(3,0,0)
3043 static gboolean
3044drawarea_configure_event_cb(GtkWidget *widget,
3045 GdkEventConfigure *event,
3046 gpointer data UNUSED)
3047{
3048 static int cur_width = 0;
3049 static int cur_height = 0;
3050
3051 g_return_val_if_fail(event
3052 && event->width >= 1 && event->height >= 1, TRUE);
3053
Bram Moolenaar182707a2016-11-21 20:55:58 +01003054# if GTK_CHECK_VERSION(3,22,2) && !GTK_CHECK_VERSION(3,22,4)
Bram Moolenaara859f042016-11-17 19:11:55 +01003055 /* As of 3.22.2, GdkWindows have started distributing configure events to
3056 * their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0).
3057 *
3058 * As can be seen from the implementation of move_native_children() and
3059 * configure_native_child() in gdkwindow.c, those functions actually
3060 * propagate configure events to every child, failing to distinguish
3061 * "native" one from non-native one.
3062 *
3063 * Naturally, configure events propagated to here like that are fallacious
3064 * and, as a matter of fact, they trigger a geometric collapse of
3065 * gui.drawarea in fullscreen and miximized modes.
3066 *
3067 * To filter out such nuisance events, we are making use of the fact that
3068 * the field send_event of such GdkEventConfigures is set to FALSE in
3069 * configure_native_child().
3070 *
3071 * Obviously, this is a terrible hack making GVim depend on GTK's
3072 * implementation details. Therefore, watch out any relevant internal
3073 * changes happening in GTK in the feature (sigh).
3074 */
Bram Moolenaar182707a2016-11-21 20:55:58 +01003075 /* Follow-up
3076 * After a few weeks later, the GdkWindow change mentioned above was
3077 * reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155).
3078 * The corresponding official release is 3.22.4. */
Bram Moolenaara859f042016-11-17 19:11:55 +01003079 if (event->send_event == FALSE)
3080 return TRUE;
3081# endif
3082
Bram Moolenaar98921892016-02-23 17:14:37 +01003083 if (event->width == cur_width && event->height == cur_height)
3084 return TRUE;
3085
3086 cur_width = event->width;
3087 cur_height = event->height;
3088
3089 if (gui.surface != NULL)
3090 cairo_surface_destroy(gui.surface);
3091
3092 gui.surface = gdk_window_create_similar_surface(
3093 gtk_widget_get_window(widget),
3094 CAIRO_CONTENT_COLOR_ALPHA,
3095 event->width, event->height);
3096
3097 gtk_widget_queue_draw(widget);
3098
3099 return TRUE;
3100}
3101#endif
3102
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103/*
3104 * Callback routine for the "delete_event" signal on the toplevel window.
3105 * Tries to vim gracefully, or refuses to exit with changed buffers.
3106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003108delete_event_cb(GtkWidget *widget UNUSED,
3109 GdkEventAny *event UNUSED,
3110 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
3112 gui_shell_closed();
3113 return TRUE;
3114}
3115
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003116#if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
3117 static int
3118get_item_dimensions(GtkWidget *widget, GtkOrientation orientation)
3119{
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003120# ifdef FEAT_GUI_GNOME
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003121 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL;
3122
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003123 if (using_gnome && widget != NULL)
3124 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003125 GtkWidget *parent;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003126 BonoboDockItem *dockitem;
3127
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003128 parent = gtk_widget_get_parent(widget);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003129 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM)
3130 {
3131 /* Only menu & toolbar are dock items. Could tabline be?
3132 * Seem to be only the 2 defined in GNOME */
3133 widget = parent;
3134 dockitem = BONOBO_DOCK_ITEM(widget);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003135
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003136 if (dockitem == NULL || dockitem->is_floating)
3137 return 0;
3138 item_orientation = bonobo_dock_item_get_orientation(dockitem);
3139 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003140 }
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003141# else
3142# define item_orientation GTK_ORIENTATION_HORIZONTAL
Bram Moolenaar98921892016-02-23 17:14:37 +01003143# endif
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003144
Bram Moolenaar98921892016-02-23 17:14:37 +01003145# if GTK_CHECK_VERSION(3,0,0)
3146 if (widget != NULL
3147 && item_orientation == orientation
3148 && gtk_widget_get_realized(widget)
3149 && gtk_widget_get_visible(widget))
3150# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003151 if (widget != NULL
3152 && item_orientation == orientation
3153 && GTK_WIDGET_REALIZED(widget)
3154 && GTK_WIDGET_VISIBLE(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01003155# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003156 {
Bram Moolenaar98921892016-02-23 17:14:37 +01003157# if GTK_CHECK_VERSION(3,0,0)
3158 GtkAllocation allocation;
3159
3160 gtk_widget_get_allocation(widget, &allocation);
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003161 return allocation.height;
Bram Moolenaar98921892016-02-23 17:14:37 +01003162# else
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003163# ifdef FEAT_GUI_GNOME
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003164 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3165 return widget->allocation.height;
3166 else
3167 return widget->allocation.width;
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003168# else
3169 return widget->allocation.height;
3170# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01003171# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003172 }
3173 return 0;
3174}
3175#endif
3176
3177 static int
3178get_menu_tool_width(void)
3179{
3180 int width = 0;
3181
3182#ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */
3183# ifdef FEAT_MENU
3184 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL);
3185# endif
3186# ifdef FEAT_TOOLBAR
3187 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL);
3188# endif
3189# ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003190 if (gui.tabline != NULL)
3191 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003192# endif
3193#endif
3194
3195 return width;
3196}
3197
3198 static int
3199get_menu_tool_height(void)
3200{
3201 int height = 0;
3202
3203#ifdef FEAT_MENU
3204 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL);
3205#endif
3206#ifdef FEAT_TOOLBAR
3207 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL);
3208#endif
3209#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003210 if (gui.tabline != NULL)
3211 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003212#endif
3213
3214 return height;
3215}
3216
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003217/* This controls whether we can set the real window hints at
3218 * start-up when in a GtkPlug.
3219 * 0 = normal processing (default)
3220 * 1 = init. hints set, no-one's tried to reset since last check
3221 * 2 = init. hints set, attempt made to change hints
3222 */
3223static int init_window_hints_state = 0;
3224
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003225 static void
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003226update_window_manager_hints(int force_width, int force_height)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003227{
3228 static int old_width = 0;
3229 static int old_height = 0;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003230 static int old_min_width = 0;
3231 static int old_min_height = 0;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003232 static int old_char_width = 0;
3233 static int old_char_height = 0;
3234
3235 int width;
3236 int height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003237 int min_width;
3238 int min_height;
3239
3240 /* At start-up, don't try to set the hints until the initial
3241 * values have been used (those that dictate our initial size)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003242 * Let forced (i.e., correct) values through always.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003243 */
3244 if (!(force_width && force_height) && init_window_hints_state > 0)
3245 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003246 /* Don't do it! */
3247 init_window_hints_state = 2;
3248 return;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003249 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003250
3251 /* This also needs to be done when the main window isn't there yet,
3252 * otherwise the hints don't work. */
3253 width = gui_get_base_width();
3254 height = gui_get_base_height();
3255# ifdef FEAT_MENU
3256 height += tabline_height() * gui.char_height;
3257# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003258 width += get_menu_tool_width();
3259 height += get_menu_tool_height();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003260
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003261 /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
Bram Moolenaar49325942007-05-10 19:19:59 +00003262 * their actual widget size. When we set our size ourselves (e.g.,
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003263 * 'set columns=' or init. -geom) we briefly set the min. to the size
3264 * we wish to be instead of the legitimate minimum so that we actually
3265 * resize correctly.
3266 */
3267 if (force_width && force_height)
3268 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003269 min_width = force_width;
3270 min_height = force_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003271 }
3272 else
3273 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003274 min_width = width + MIN_COLUMNS * gui.char_width;
3275 min_height = height + MIN_LINES * gui.char_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003276 }
3277
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003278 /* Avoid an expose event when the size didn't change. */
3279 if (width != old_width
3280 || height != old_height
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003281 || min_width != old_min_width
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003282 || min_height != old_min_height
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003283 || gui.char_width != old_char_width
3284 || gui.char_height != old_char_height)
3285 {
3286 GdkGeometry geometry;
3287 GdkWindowHints geometry_mask;
3288
3289 geometry.width_inc = gui.char_width;
3290 geometry.height_inc = gui.char_height;
3291 geometry.base_width = width;
3292 geometry.base_height = height;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003293 geometry.min_width = min_width;
3294 geometry.min_height = min_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003295 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC
3296 |GDK_HINT_MIN_SIZE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003297 /* Using gui.formwin as geometry widget doesn't work as expected
3298 * with GTK+ 2 -- dunno why. Presumably all the resizing hacks
3299 * in Vim confuse GTK+. */
3300 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin,
3301 &geometry, geometry_mask);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003302 old_width = width;
3303 old_height = height;
3304 old_min_width = min_width;
3305 old_min_height = min_height;
3306 old_char_width = gui.char_width;
3307 old_char_height = gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003308 }
3309}
3310
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311#ifdef FEAT_TOOLBAR
3312
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313/*
3314 * This extra effort wouldn't be necessary if we only used stock icons in the
3315 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
3316 * shouldn't be treated differently, thus we do need this.
3317 */
3318 static void
3319icon_size_changed_foreach(GtkWidget *widget, gpointer user_data)
3320{
3321 if (GTK_IS_IMAGE(widget))
3322 {
3323 GtkImage *image = (GtkImage *)widget;
3324
Bram Moolenaar98921892016-02-23 17:14:37 +01003325# if GTK_CHECK_VERSION(3,10,0)
3326 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_NAME)
3327 {
3328 const GtkIconSize icon_size = GPOINTER_TO_INT(user_data);
3329 const gchar *icon_name;
3330
3331 gtk_image_get_icon_name(image, &icon_name, NULL);
3332
3333 gtk_image_set_from_icon_name(image, icon_name, icon_size);
3334 }
3335# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 /* User-defined icons are stored in a GtkIconSet */
3337 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET)
3338 {
3339 GtkIconSet *icon_set;
3340 GtkIconSize icon_size;
3341
3342 gtk_image_get_icon_set(image, &icon_set, &icon_size);
3343 icon_size = (GtkIconSize)(long)user_data;
3344
3345 gtk_icon_set_ref(icon_set);
3346 gtk_image_set_from_icon_set(image, icon_set, icon_size);
3347 gtk_icon_set_unref(icon_set);
3348 }
Bram Moolenaar98921892016-02-23 17:14:37 +01003349# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 }
3351 else if (GTK_IS_CONTAINER(widget))
3352 {
3353 gtk_container_foreach((GtkContainer *)widget,
3354 &icon_size_changed_foreach,
3355 user_data);
3356 }
3357}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
3359 static void
3360set_toolbar_style(GtkToolbar *toolbar)
3361{
3362 GtkToolbarStyle style;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 GtkIconSize size;
3364 GtkIconSize oldsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3367 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3368 style = GTK_TOOLBAR_BOTH_HORIZ;
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003369 else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 == (TOOLBAR_TEXT | TOOLBAR_ICONS))
3371 style = GTK_TOOLBAR_BOTH;
3372 else if (toolbar_flags & TOOLBAR_TEXT)
3373 style = GTK_TOOLBAR_TEXT;
3374 else
3375 style = GTK_TOOLBAR_ICONS;
3376
3377 gtk_toolbar_set_style(toolbar, style);
Bram Moolenaar98921892016-02-23 17:14:37 +01003378# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003380# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 switch (tbis_flags)
3383 {
3384 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break;
3385 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break;
3386 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break;
3387 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
Bram Moolenaarbeb003b2016-03-08 22:47:17 +01003388 case TBIS_HUGE: size = GTK_ICON_SIZE_DND; break;
3389 case TBIS_GIANT: size = GTK_ICON_SIZE_DIALOG; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 default: size = GTK_ICON_SIZE_INVALID; break;
3391 }
3392 oldsize = gtk_toolbar_get_icon_size(toolbar);
3393
3394 if (size == GTK_ICON_SIZE_INVALID)
3395 {
3396 /* Let global user preferences decide the icon size. */
3397 gtk_toolbar_unset_icon_size(toolbar);
3398 size = gtk_toolbar_get_icon_size(toolbar);
3399 }
3400 if (size != oldsize)
3401 {
3402 gtk_container_foreach(GTK_CONTAINER(toolbar),
3403 &icon_size_changed_foreach,
3404 GINT_TO_POINTER((int)size));
3405 }
3406 gtk_toolbar_set_icon_size(toolbar, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407}
3408
3409#endif /* FEAT_TOOLBAR */
3410
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003411#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3412static int ignore_tabline_evt = FALSE;
Bram Moolenaara5621492006-02-25 21:55:24 +00003413static GtkWidget *tabline_menu;
Bram Moolenaar98921892016-02-23 17:14:37 +01003414# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003415static GtkTooltips *tabline_tooltip;
Bram Moolenaar98921892016-02-23 17:14:37 +01003416# endif
Bram Moolenaara5621492006-02-25 21:55:24 +00003417static int clicked_page; /* page clicked in tab line */
3418
3419/*
3420 * Handle selecting an item in the tab line popup menu.
3421 */
Bram Moolenaara5621492006-02-25 21:55:24 +00003422 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003423tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data)
Bram Moolenaara5621492006-02-25 21:55:24 +00003424{
Bram Moolenaara5621492006-02-25 21:55:24 +00003425 /* Add the string cmd into input buffer */
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003426 send_tabline_menu_event(clicked_page, (int)(long)user_data);
Bram Moolenaara5621492006-02-25 21:55:24 +00003427}
3428
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003429 static void
3430add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp)
3431{
3432 GtkWidget *item;
3433 char_u *utf_text;
3434
3435 utf_text = CONVERT_TO_UTF8(text);
3436 item = gtk_menu_item_new_with_label((const char *)utf_text);
3437 gtk_widget_show(item);
3438 CONVERT_TO_UTF8_FREE(utf_text);
3439
3440 gtk_container_add(GTK_CONTAINER(menu), item);
Bram Moolenaar98921892016-02-23 17:14:37 +01003441# if GTK_CHECK_VERSION(3,0,0)
3442 g_signal_connect(G_OBJECT(item), "activate",
3443 G_CALLBACK(tabline_menu_handler),
3444 GINT_TO_POINTER(resp));
3445# else
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003446 gtk_signal_connect(GTK_OBJECT(item), "activate",
3447 GTK_SIGNAL_FUNC(tabline_menu_handler),
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003448 (gpointer)(long)resp);
Bram Moolenaar98921892016-02-23 17:14:37 +01003449# endif
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003450}
3451
Bram Moolenaara5621492006-02-25 21:55:24 +00003452/*
Bram Moolenaara5621492006-02-25 21:55:24 +00003453 * Create a menu for the tab line.
3454 */
3455 static GtkWidget *
3456create_tabline_menu(void)
3457{
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003458 GtkWidget *menu;
Bram Moolenaara5621492006-02-25 21:55:24 +00003459
3460 menu = gtk_menu_new();
Bram Moolenaar7098ee52015-06-09 19:14:24 +02003461 if (first_tabpage->tp_next != NULL)
3462 add_tabline_menu_item(menu, (char_u *)_("Close tab"),
3463 TABLINE_MENU_CLOSE);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003464 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW);
3465 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN);
Bram Moolenaara5621492006-02-25 21:55:24 +00003466
3467 return menu;
3468}
3469
3470 static gboolean
3471on_tabline_menu(GtkWidget *widget, GdkEvent *event)
3472{
3473 /* Was this button press event ? */
3474 if (event->type == GDK_BUTTON_PRESS)
3475 {
3476 GdkEventButton *bevent = (GdkEventButton *)event;
3477 int x = bevent->x;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003478 int y = bevent->y;
3479 GtkWidget *tabwidget;
3480 GdkWindow *tabwin;
Bram Moolenaara5621492006-02-25 21:55:24 +00003481
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003482 /* When ignoring events return TRUE so that the selected page doesn't
3483 * change. */
3484 if (hold_gui_events
3485# ifdef FEAT_CMDWIN
3486 || cmdwin_type != 0
3487# endif
3488 )
3489 return TRUE;
3490
Bram Moolenaar98921892016-02-23 17:14:37 +01003491# if GTK_CHECK_VERSION(3,0,0)
3492 tabwin = gui_gtk_window_at_position(gui.mainwin, &x, &y);
3493# else
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003494 tabwin = gdk_window_at_pointer(&x, &y);
Bram Moolenaar98921892016-02-23 17:14:37 +01003495# endif
3496
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003497 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget);
Bram Moolenaar98921892016-02-23 17:14:37 +01003498# if GTK_CHECK_VERSION(3,0,0)
3499 clicked_page = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(tabwidget),
3500 "tab_num"));
3501# else
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003502 clicked_page = (int)(long)gtk_object_get_user_data(
3503 GTK_OBJECT(tabwidget));
Bram Moolenaar98921892016-02-23 17:14:37 +01003504# endif
Bram Moolenaara5621492006-02-25 21:55:24 +00003505
3506 /* If the event was generated for 3rd button popup the menu. */
3507 if (bevent->button == 3)
3508 {
Bram Moolenaara859f042016-11-17 19:11:55 +01003509# if GTK_CHECK_VERSION(3,22,2)
3510 gtk_menu_popup_at_pointer(GTK_MENU(widget), event);
3511# else
Bram Moolenaara5621492006-02-25 21:55:24 +00003512 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
3513 bevent->button, bevent->time);
Bram Moolenaara859f042016-11-17 19:11:55 +01003514# endif
Bram Moolenaara5621492006-02-25 21:55:24 +00003515 /* We handled the event. */
3516 return TRUE;
3517 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003518 else if (bevent->button == 1)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003519 {
Bram Moolenaar96351572006-05-05 21:16:59 +00003520 if (clicked_page == 0)
3521 {
Bram Moolenaar07354542007-09-15 12:07:46 +00003522 /* Click after all tabs moves to next tab page. When "x" is
3523 * small guess it's the left button. */
Bram Moolenaara3f41662010-07-11 19:01:06 +02003524 send_tabline_event(x < 50 ? -1 : 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00003525 }
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003526 }
Bram Moolenaara5621492006-02-25 21:55:24 +00003527 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003528
Bram Moolenaara5621492006-02-25 21:55:24 +00003529 /* We didn't handle the event. */
3530 return FALSE;
3531}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003532
3533/*
3534 * Handle selecting one of the tabs.
3535 */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003536 static void
3537on_select_tab(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003538 GtkNotebook *notebook UNUSED,
Bram Moolenaar98921892016-02-23 17:14:37 +01003539# if GTK_CHECK_VERSION(3,0,0)
3540 gpointer *page UNUSED,
3541# else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003542 GtkNotebookPage *page UNUSED,
Bram Moolenaar98921892016-02-23 17:14:37 +01003543# endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00003544 gint idx,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003545 gpointer data UNUSED)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003546{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003547 if (!ignore_tabline_evt)
Bram Moolenaara3f41662010-07-11 19:01:06 +02003548 send_tabline_event(idx + 1);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02003549}
3550
3551/*
3552 * Handle reordering the tabs (using D&D).
3553 */
3554 static void
3555on_tab_reordered(
3556 GtkNotebook *notebook UNUSED,
3557# if GTK_CHECK_VERSION(3,0,0)
3558 gpointer *page UNUSED,
3559# else
3560 GtkNotebookPage *page UNUSED,
3561# endif
3562 gint idx,
3563 gpointer data UNUSED)
3564{
3565 if (!ignore_tabline_evt)
3566 {
3567 if ((tabpage_index(curtab) - 1) < idx)
3568 tabpage_move(idx + 1);
3569 else
3570 tabpage_move(idx);
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003571 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003572}
3573
3574/*
3575 * Show or hide the tabline.
3576 */
3577 void
3578gui_mch_show_tabline(int showit)
3579{
3580 if (gui.tabline == NULL)
3581 return;
3582
3583 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)))
3584 {
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003585 /* Note: this may cause a resize event */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003586 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003587 update_window_manager_hints(0, 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00003588 if (showit)
Bram Moolenaar98921892016-02-23 17:14:37 +01003589# if GTK_CHECK_VERSION(3,0,0)
3590 gtk_widget_set_can_focus(GTK_WIDGET(gui.tabline), FALSE);
3591# else
Bram Moolenaar96351572006-05-05 21:16:59 +00003592 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui.tabline), GTK_CAN_FOCUS);
Bram Moolenaar98921892016-02-23 17:14:37 +01003593# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003594 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003595
3596 gui_mch_update();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003597}
3598
3599/*
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003600 * Return TRUE when tabline is displayed.
3601 */
3602 int
3603gui_mch_showing_tabline(void)
3604{
3605 return gui.tabline != NULL
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003606 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline));
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003607}
3608
3609/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003610 * Update the labels of the tabline.
3611 */
3612 void
3613gui_mch_update_tabline(void)
3614{
3615 GtkWidget *page;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003616 GtkWidget *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003617 GtkWidget *label;
3618 tabpage_T *tp;
3619 int nr = 0;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003620 int tab_num;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003621 int curtabidx = 0;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003622 char_u *labeltext;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003623
3624 if (gui.tabline == NULL)
3625 return;
3626
3627 ignore_tabline_evt = TRUE;
3628
3629 /* Add a label for each tab page. They all contain the same text area. */
3630 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3631 {
3632 if (tp == curtab)
3633 curtabidx = nr;
3634
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003635 tab_num = nr + 1;
3636
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003637 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr);
3638 if (page == NULL)
3639 {
3640 /* Add notebook page */
Bram Moolenaar98921892016-02-23 17:14:37 +01003641# if GTK_CHECK_VERSION(3,2,0)
3642 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3643 gtk_box_set_homogeneous(GTK_BOX(page), FALSE);
3644# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003645 page = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003646# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003647 gtk_widget_show(page);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003648 event_box = gtk_event_box_new();
3649 gtk_widget_show(event_box);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003650 label = gtk_label_new("-Empty-");
Bram Moolenaar98921892016-02-23 17:14:37 +01003651# if !GTK_CHECK_VERSION(3,14,0)
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003652 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01003653# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003654 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003655 gtk_widget_show(label);
3656 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline),
3657 page,
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003658 event_box,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003659 nr++);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02003660 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(gui.tabline),
3661 page,
3662 TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003663 }
3664
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003665 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page);
Bram Moolenaar98921892016-02-23 17:14:37 +01003666# if GTK_CHECK_VERSION(3,0,0)
3667 g_object_set_data(G_OBJECT(event_box), "tab_num",
3668 GINT_TO_POINTER(tab_num));
3669# else
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003670 gtk_object_set_user_data(GTK_OBJECT(event_box),
3671 (gpointer)(long)tab_num);
Bram Moolenaar98921892016-02-23 17:14:37 +01003672# endif
3673# if GTK_CHECK_VERSION(3,0,0)
3674 label = gtk_bin_get_child(GTK_BIN(event_box));
3675# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003676 label = GTK_BIN(event_box)->child;
Bram Moolenaar98921892016-02-23 17:14:37 +01003677# endif
Bram Moolenaar57657d82006-04-21 22:12:41 +00003678 get_tabline_label(tp, FALSE);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003679 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003680 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003681 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003682
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003683 get_tabline_label(tp, TRUE);
3684 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar98921892016-02-23 17:14:37 +01003685# if GTK_CHECK_VERSION(3,0,0)
3686 gtk_widget_set_tooltip_text(event_box, (const gchar *)labeltext);
3687# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003688 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box,
3689 (const char *)labeltext, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01003690# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003691 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003692 }
3693
3694 /* Remove any old labels. */
3695 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL)
3696 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr);
3697
Bram Moolenaar98921892016-02-23 17:14:37 +01003698# if GTK_CHECK_VERSION(3,0,0)
3699 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
3700 gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
3701# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003702 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003703 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
Bram Moolenaar98921892016-02-23 17:14:37 +01003704# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003705
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003706 /* Make sure everything is in place before drawing text. */
3707 gui_mch_update();
3708
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003709 ignore_tabline_evt = FALSE;
3710}
3711
3712/*
3713 * Set the current tab to "nr". First tab is 1.
3714 */
3715 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003716gui_mch_set_curtab(int nr)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003717{
3718 if (gui.tabline == NULL)
3719 return;
3720
3721 ignore_tabline_evt = TRUE;
Bram Moolenaar98921892016-02-23 17:14:37 +01003722# if GTK_CHECK_VERSION(3,0,0)
3723 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
3724 gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
3725# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003726 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003727 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
Bram Moolenaar98921892016-02-23 17:14:37 +01003728# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003729 ignore_tabline_evt = FALSE;
3730}
3731
3732#endif /* FEAT_GUI_TABLINE */
3733
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734/*
Bram Moolenaara76638f2010-06-05 12:49:46 +02003735 * Add selection targets for PRIMARY and CLIPBOARD selections.
3736 */
3737 void
3738gui_gtk_set_selection_targets(void)
3739{
3740 int i, j = 0;
3741 int n_targets = N_SELECTION_TARGETS;
3742 GtkTargetEntry targets[N_SELECTION_TARGETS];
3743
3744 for (i = 0; i < (int)N_SELECTION_TARGETS; ++i)
3745 {
Bram Moolenaar29695702012-05-18 17:03:18 +02003746 /* OpenOffice tries to use TARGET_HTML and fails when we don't
Bram Moolenaara76638f2010-06-05 12:49:46 +02003747 * return something, instead of trying another target. Therefore only
3748 * offer TARGET_HTML when it works. */
3749 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3750 n_targets--;
3751 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003752 targets[j++] = selection_targets[i];
3753 }
3754
3755 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)GDK_SELECTION_PRIMARY);
3756 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)clip_plus.gtk_sel_atom);
3757 gtk_selection_add_targets(gui.drawarea,
3758 (GdkAtom)GDK_SELECTION_PRIMARY,
3759 targets, n_targets);
3760 gtk_selection_add_targets(gui.drawarea,
3761 (GdkAtom)clip_plus.gtk_sel_atom,
3762 targets, n_targets);
3763}
3764
3765/*
3766 * Set up for receiving DND items.
3767 */
3768 void
3769gui_gtk_set_dnd_targets(void)
3770{
3771 int i, j = 0;
3772 int n_targets = N_DND_TARGETS;
3773 GtkTargetEntry targets[N_DND_TARGETS];
3774
3775 for (i = 0; i < (int)N_DND_TARGETS; ++i)
3776 {
Bram Moolenaarb931d742011-10-26 11:36:25 +02003777 if (!clip_html && dnd_targets[i].info == TARGET_HTML)
Bram Moolenaara76638f2010-06-05 12:49:46 +02003778 n_targets--;
3779 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003780 targets[j++] = dnd_targets[i];
3781 }
3782
3783 gtk_drag_dest_unset(gui.drawarea);
3784 gtk_drag_dest_set(gui.drawarea,
3785 GTK_DEST_DEFAULT_ALL,
3786 targets, n_targets,
Bram Moolenaarba7cc9f2011-02-25 17:10:27 +01003787 GDK_ACTION_COPY | GDK_ACTION_MOVE);
Bram Moolenaara76638f2010-06-05 12:49:46 +02003788}
3789
3790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3792 * Returns OK for success, FAIL when the GUI can't be started.
3793 */
3794 int
3795gui_mch_init(void)
3796{
3797 GtkWidget *vbox;
3798
3799#ifdef FEAT_GUI_GNOME
3800 /* Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3801 * exits on failure, but that's a non-issue because we already called
3802 * gtk_init_check() in gui_mch_init_check(). */
3803 if (using_gnome)
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003804 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT,
3806 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL);
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003807# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
Bram Moolenaar1ff32c52014-04-29 15:11:43 +02003808 {
3809 char *p = setlocale(LC_NUMERIC, NULL);
3810
3811 /* Make sure strtod() uses a decimal point, not a comma. Gnome
3812 * init may change it. */
3813 if (p == NULL || strcmp(p, "C") != 0)
3814 setlocale(LC_NUMERIC, "C");
3815 }
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003816# endif
3817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003819 VIM_CLEAR(gui_argv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003821#if GLIB_CHECK_VERSION(2,1,3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 /* Set the human-readable application name */
3823 g_set_application_name("Vim");
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 /*
3826 * Force UTF-8 output no matter what the value of 'encoding' is.
3827 * did_set_string_option() in option.c prohibits changing 'termencoding'
3828 * to something else than UTF-8 if the GUI is in use.
3829 */
3830 set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0);
3831
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003832#ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 gui_gtk_register_stock_icons();
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 /* FIXME: Need to install the classic icons and a gtkrc.classic file.
3836 * The hard part is deciding install locations and the Makefile magic. */
Bram Moolenaar98921892016-02-23 17:14:37 +01003837#if !GTK_CHECK_VERSION(3,0,0)
3838# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 gtk_rc_parse("gtkrc");
Bram Moolenaar98921892016-02-23 17:14:37 +01003840# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841#endif
3842
3843 /* Initialize values */
3844 gui.border_width = 2;
3845 gui.scrollbar_width = SB_DEFAULT_WIDTH;
3846 gui.scrollbar_height = SB_DEFAULT_WIDTH;
Bram Moolenaar36edf062016-07-21 22:10:12 +02003847#if GTK_CHECK_VERSION(3,0,0)
3848 gui.fgcolor = g_new(GdkRGBA, 1);
3849 gui.bgcolor = g_new(GdkRGBA, 1);
3850 gui.spcolor = g_new(GdkRGBA, 1);
3851#else
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003852 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 gui.fgcolor = g_new0(GdkColor, 1);
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003854 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 gui.bgcolor = g_new0(GdkColor, 1);
Bram Moolenaarf36d3692005-03-15 22:48:14 +00003856 /* LINTED: avoid warning: conversion to 'unsigned long' */
3857 gui.spcolor = g_new0(GdkColor, 1);
Bram Moolenaar36edf062016-07-21 22:10:12 +02003858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859
3860 /* Initialise atoms */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00003861 html_atom = gdk_atom_intern("text/html", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863
3864 /* Set default foreground and background colors. */
3865 gui.norm_pixel = gui.def_norm_pixel;
3866 gui.back_pixel = gui.def_back_pixel;
3867
3868 if (gtk_socket_id != 0)
3869 {
3870 GtkWidget *plug;
3871
3872 /* Use GtkSocket from another app. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 plug = gtk_plug_new_for_display(gdk_display_get_default(),
3874 gtk_socket_id);
Bram Moolenaar98921892016-02-23 17:14:37 +01003875#if GTK_CHECK_VERSION(3,0,0)
3876 if (plug != NULL && gtk_plug_get_socket_window(GTK_PLUG(plug)) != NULL)
3877#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 if (plug != NULL && GTK_PLUG(plug)->socket_window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01003879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 {
3881 gui.mainwin = plug;
3882 }
3883 else
3884 {
3885 g_warning("Connection to GTK+ socket (ID %u) failed",
3886 (unsigned int)gtk_socket_id);
3887 /* Pretend we never wanted it if it failed (get own window) */
3888 gtk_socket_id = 0;
3889 }
3890 }
3891
3892 if (gtk_socket_id == 0)
3893 {
3894#ifdef FEAT_GUI_GNOME
3895 if (using_gnome)
3896 {
3897 gui.mainwin = gnome_app_new("Vim", NULL);
3898# ifdef USE_XSMP
3899 /* Use the GNOME save-yourself functionality now. */
3900 xsmp_close();
3901# endif
3902 }
3903 else
3904#endif
3905 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3906 }
3907
3908 gtk_widget_set_name(gui.mainwin, "vim-main-window");
3909
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 /* Create the PangoContext used for drawing all text. */
3911 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
3912 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913
Bram Moolenaar98921892016-02-23 17:14:37 +01003914#if GTK_CHECK_VERSION(3,0,0)
3915 gtk_container_set_border_width(GTK_CONTAINER(gui.mainwin), 0);
3916#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 gtk_container_border_width(GTK_CONTAINER(gui.mainwin), 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK);
3920
Bram Moolenaar98921892016-02-23 17:14:37 +01003921#if GTK_CHECK_VERSION(3,0,0)
3922 g_signal_connect(G_OBJECT(gui.mainwin), "delete-event",
3923 G_CALLBACK(&delete_event_cb), NULL);
3924
3925 g_signal_connect(G_OBJECT(gui.mainwin), "realize",
3926 G_CALLBACK(&mainwin_realize), NULL);
3927#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "delete_event",
3929 GTK_SIGNAL_FUNC(&delete_event_cb), NULL);
3930
3931 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "realize",
3932 GTK_SIGNAL_FUNC(&mainwin_realize), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01003933#endif
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02003934
3935 g_signal_connect(G_OBJECT(gui.mainwin), "screen-changed",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 G_CALLBACK(&mainwin_screen_changed_cb), NULL);
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02003937
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 gui.accel_group = gtk_accel_group_new();
3939 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003941 /* A vertical box holds the menubar, toolbar and main text window. */
Bram Moolenaar98921892016-02-23 17:14:37 +01003942#if GTK_CHECK_VERSION(3,2,0)
3943 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3944 gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE);
3945#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 vbox = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948
3949#ifdef FEAT_GUI_GNOME
3950 if (using_gnome)
3951 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003952# if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 /* automagically restore menubar/toolbar placement */
3954 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE);
3955# endif
3956 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox);
3957 }
3958 else
3959#endif
3960 {
3961 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox);
3962 gtk_widget_show(vbox);
3963 }
3964
3965#ifdef FEAT_MENU
3966 /*
3967 * Create the menubar and handle
3968 */
3969 gui.menubar = gtk_menu_bar_new();
3970 gtk_widget_set_name(gui.menubar, "vim-menubar");
3971
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003972 /* Avoid that GTK takes <F10> away from us. */
3973 {
3974 GtkSettings *gtk_settings;
3975
3976 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
3977 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
3978 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003979
3980
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981# ifdef FEAT_GUI_GNOME
3982 if (using_gnome)
3983 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 BonoboDockItem *dockitem;
3985
3986 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar));
3987 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3988 GNOME_APP_MENUBAR_NAME);
Bram Moolenaardb552d602006-03-23 22:59:57 +00003989 /* We don't want the menu to float. */
3990 bonobo_dock_item_set_behavior(dockitem,
3991 bonobo_dock_item_get_behavior(dockitem)
3992 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 gui.menubar_h = GTK_WIDGET(dockitem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 }
3995 else
3996# endif /* FEAT_GUI_GNOME */
3997 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00003998 /* Always show the menubar, otherwise <F10> doesn't work. It may be
3999 * disabled in gui_init() later. */
4000 gtk_widget_show(gui.menubar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0);
4002 }
4003#endif /* FEAT_MENU */
4004
4005#ifdef FEAT_TOOLBAR
4006 /*
4007 * Create the toolbar and handle
4008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 /* some aesthetics on the toolbar */
Bram Moolenaar98921892016-02-23 17:14:37 +01004010# ifdef USE_GTK3
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01004011 /* TODO: Add GTK+ 3 code here using GtkCssProvider if necessary. */
Bram Moolenaar98921892016-02-23 17:14:37 +01004012 /* N.B. Since the default value of GtkToolbar::button-relief is
4013 * GTK_RELIEF_NONE, there's no need to specify that, probably. */
4014# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 gtk_rc_parse_string(
4016 "style \"vim-toolbar-style\" {\n"
4017 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
4018 "}\n"
4019 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
Bram Moolenaar98921892016-02-23 17:14:37 +01004020# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 gui.toolbar = gtk_toolbar_new();
4022 gtk_widget_set_name(gui.toolbar, "vim-toolbar");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
4024
4025# ifdef FEAT_GUI_GNOME
4026 if (using_gnome)
4027 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 BonoboDockItem *dockitem;
4029
4030 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar));
4031 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
4032 GNOME_APP_TOOLBAR_NAME);
4033 gui.toolbar_h = GTK_WIDGET(dockitem);
Bram Moolenaare580b0c2006-03-21 21:33:03 +00004034 /* When the toolbar is floating it gets stuck. So long as that isn't
Bram Moolenaardb552d602006-03-23 22:59:57 +00004035 * fixed let's disallow floating. */
Bram Moolenaare580b0c2006-03-21 21:33:03 +00004036 bonobo_dock_item_set_behavior(dockitem,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004037 bonobo_dock_item_get_behavior(dockitem)
4038 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 }
4041 else
4042# endif /* FEAT_GUI_GNOME */
4043 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 if (vim_strchr(p_go, GO_TOOLBAR) != NULL
4045 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
4046 gtk_widget_show(gui.toolbar);
4047 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0);
4048 }
4049#endif /* FEAT_TOOLBAR */
4050
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004051#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004052 /*
4053 * Use a Notebook for the tab pages labels. The labels are hidden by
4054 * default.
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004055 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004056 gui.tabline = gtk_notebook_new();
4057 gtk_widget_show(gui.tabline);
4058 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0);
4059 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
4060 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004061 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01004062# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar96351572006-05-05 21:16:59 +00004063 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01004064# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004065
Bram Moolenaar98921892016-02-23 17:14:37 +01004066# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004067 tabline_tooltip = gtk_tooltips_new();
4068 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip));
Bram Moolenaar98921892016-02-23 17:14:37 +01004069# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004070
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004071 {
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004072 GtkWidget *page, *label, *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004073
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004074 /* Add the first tab. */
Bram Moolenaar98921892016-02-23 17:14:37 +01004075# if GTK_CHECK_VERSION(3,2,0)
4076 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
4077 gtk_box_set_homogeneous(GTK_BOX(page), FALSE);
4078# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004079 page = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01004080# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004081 gtk_widget_show(page);
4082 gtk_container_add(GTK_CONTAINER(gui.tabline), page);
4083 label = gtk_label_new("-Empty-");
4084 gtk_widget_show(label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004085 event_box = gtk_event_box_new();
4086 gtk_widget_show(event_box);
Bram Moolenaar98921892016-02-23 17:14:37 +01004087# if GTK_CHECK_VERSION(3,0,0)
4088 g_object_set_data(G_OBJECT(event_box), "tab_num", GINT_TO_POINTER(1L));
4089# else
Bram Moolenaar47b46d72008-07-02 19:05:48 +00004090 gtk_object_set_user_data(GTK_OBJECT(event_box), (gpointer)1L);
Bram Moolenaar98921892016-02-23 17:14:37 +01004091# endif
4092# if !GTK_CHECK_VERSION(3,14,0)
Bram Moolenaar8ea91232006-04-28 22:41:43 +00004093 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01004094# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004095 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004096 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02004097 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(gui.tabline), page, TRUE);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004098 }
Bram Moolenaar54a709e2006-05-04 21:57:11 +00004099
Bram Moolenaar98921892016-02-23 17:14:37 +01004100# if GTK_CHECK_VERSION(3,0,0)
4101 g_signal_connect(G_OBJECT(gui.tabline), "switch-page",
4102 G_CALLBACK(on_select_tab), NULL);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02004103 g_signal_connect(G_OBJECT(gui.tabline), "page-reordered",
4104 G_CALLBACK(on_tab_reordered), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004105# else
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004106 gtk_signal_connect(GTK_OBJECT(gui.tabline), "switch_page",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004107 GTK_SIGNAL_FUNC(on_select_tab), NULL);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02004108 gtk_signal_connect(GTK_OBJECT(gui.tabline), "page-reordered",
4109 GTK_SIGNAL_FUNC(on_tab_reordered), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004110# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004111
4112 /* Create a popup menu for the tab line and connect it. */
4113 tabline_menu = create_tabline_menu();
Bram Moolenaar98921892016-02-23 17:14:37 +01004114# if GTK_CHECK_VERSION(3,0,0)
4115 g_signal_connect_swapped(G_OBJECT(gui.tabline), "button-press-event",
4116 G_CALLBACK(on_tabline_menu), G_OBJECT(tabline_menu));
4117# else
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004118 gtk_signal_connect_object(GTK_OBJECT(gui.tabline), "button_press_event",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004119 GTK_SIGNAL_FUNC(on_tabline_menu), GTK_OBJECT(tabline_menu));
Bram Moolenaar98921892016-02-23 17:14:37 +01004120# endif
4121#endif /* FEAT_GUI_TABLINE */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004122
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 gui.formwin = gtk_form_new();
Bram Moolenaar98921892016-02-23 17:14:37 +01004124#if GTK_CHECK_VERSION(3,0,0)
4125 gtk_container_set_border_width(GTK_CONTAINER(gui.formwin), 0);
4126#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 gtk_container_border_width(GTK_CONTAINER(gui.formwin), 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01004128#endif
4129#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK);
Bram Moolenaar98921892016-02-23 17:14:37 +01004131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
4133 gui.drawarea = gtk_drawing_area_new();
Bram Moolenaara859f042016-11-17 19:11:55 +01004134#if GTK_CHECK_VERSION(3,22,2)
4135 gtk_widget_set_name(gui.drawarea, "vim-gui-drawarea");
4136#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004137#if GTK_CHECK_VERSION(3,0,0)
4138 gui.surface = NULL;
4139 gui.by_signal = FALSE;
4140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
4142 /* Determine which events we will filter. */
4143 gtk_widget_set_events(gui.drawarea,
4144 GDK_EXPOSURE_MASK |
4145 GDK_ENTER_NOTIFY_MASK |
4146 GDK_LEAVE_NOTIFY_MASK |
4147 GDK_BUTTON_PRESS_MASK |
4148 GDK_BUTTON_RELEASE_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 GDK_SCROLL_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 GDK_KEY_PRESS_MASK |
4151 GDK_KEY_RELEASE_MASK |
4152 GDK_POINTER_MOTION_MASK |
4153 GDK_POINTER_MOTION_HINT_MASK);
4154
4155 gtk_widget_show(gui.drawarea);
4156 gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0);
4157 gtk_widget_show(gui.formwin);
4158 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0);
4159
4160 /* For GtkSockets, key-presses must go to the focus widget (drawarea)
4161 * and not the window. */
Bram Moolenaar98921892016-02-23 17:14:37 +01004162#if GTK_CHECK_VERSION(3,0,0)
4163 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
4164 : G_OBJECT(gui.drawarea),
4165 "key-press-event",
4166 G_CALLBACK(key_press_event), NULL);
4167#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 gtk_signal_connect((gtk_socket_id == 0) ? GTK_OBJECT(gui.mainwin)
4169 : GTK_OBJECT(gui.drawarea),
4170 "key_press_event",
4171 GTK_SIGNAL_FUNC(key_press_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004172#endif
4173#if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 /* Also forward key release events for the benefit of GTK+ 2 input
4175 * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */
4176 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
4177 : G_OBJECT(gui.drawarea),
Bram Moolenaar98921892016-02-23 17:14:37 +01004178 "key-release-event",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 G_CALLBACK(&key_release_event), NULL);
4180#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004181#if GTK_CHECK_VERSION(3,0,0)
4182 g_signal_connect(G_OBJECT(gui.drawarea), "realize",
4183 G_CALLBACK(drawarea_realize_cb), NULL);
4184 g_signal_connect(G_OBJECT(gui.drawarea), "unrealize",
4185 G_CALLBACK(drawarea_unrealize_cb), NULL);
4186 g_signal_connect(G_OBJECT(gui.drawarea), "configure-event",
4187 G_CALLBACK(drawarea_configure_event_cb), NULL);
Bram Moolenaara859f042016-11-17 19:11:55 +01004188# if GTK_CHECK_VERSION(3,22,2)
4189 g_signal_connect_after(G_OBJECT(gui.drawarea), "style-updated",
4190 G_CALLBACK(&drawarea_style_updated_cb), NULL);
4191# else
Bram Moolenaar98921892016-02-23 17:14:37 +01004192 g_signal_connect_after(G_OBJECT(gui.drawarea), "style-set",
4193 G_CALLBACK(&drawarea_style_set_cb), NULL);
Bram Moolenaara859f042016-11-17 19:11:55 +01004194# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004195#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "realize",
4197 GTK_SIGNAL_FUNC(drawarea_realize_cb), NULL);
4198 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "unrealize",
4199 GTK_SIGNAL_FUNC(drawarea_unrealize_cb), NULL);
4200
4201 gtk_signal_connect_after(GTK_OBJECT(gui.drawarea), "style_set",
4202 GTK_SIGNAL_FUNC(&drawarea_style_set_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204
Bram Moolenaar98921892016-02-23 17:14:37 +01004205#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 gui.visibility = GDK_VISIBILITY_UNOBSCURED;
Bram Moolenaar98921892016-02-23 17:14:37 +01004207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208
4209#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
4210 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE);
4211 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE);
4212#endif
4213
4214 if (gtk_socket_id != 0)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004215 /* make sure keyboard input can go to the drawarea */
Bram Moolenaar98921892016-02-23 17:14:37 +01004216#if GTK_CHECK_VERSION(3,0,0)
4217 gtk_widget_set_can_focus(gui.drawarea, TRUE);
4218#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 GTK_WIDGET_SET_FLAGS(gui.drawarea, GTK_CAN_FOCUS);
Bram Moolenaar98921892016-02-23 17:14:37 +01004220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221
4222 /*
4223 * Set clipboard specific atoms
4224 */
4225 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY;
4228 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE);
4229
4230 /*
4231 * Start out by adding the configured border width into the border offset.
4232 */
4233 gui.border_offset = gui.border_width;
4234
Bram Moolenaar98921892016-02-23 17:14:37 +01004235#if GTK_CHECK_VERSION(3,0,0)
4236 g_signal_connect(G_OBJECT(gui.drawarea), "draw",
4237 G_CALLBACK(draw_event), NULL);
4238#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event",
4240 GTK_SIGNAL_FUNC(visibility_event), NULL);
4241 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event",
4242 GTK_SIGNAL_FUNC(expose_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
4245 /*
4246 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
4247 * Only needed for some window managers.
4248 */
4249 if (vim_strchr(p_go, GO_POINTER) != NULL)
4250 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004251#if GTK_CHECK_VERSION(3,0,0)
4252 g_signal_connect(G_OBJECT(gui.drawarea), "leave-notify-event",
4253 G_CALLBACK(leave_notify_event), NULL);
4254 g_signal_connect(G_OBJECT(gui.drawarea), "enter-notify-event",
4255 G_CALLBACK(enter_notify_event), NULL);
4256#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "leave_notify_event",
4258 GTK_SIGNAL_FUNC(leave_notify_event), NULL);
4259 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "enter_notify_event",
4260 GTK_SIGNAL_FUNC(enter_notify_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 }
4263
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004264 /* Real windows can get focus ... GtkPlug, being a mere container can't,
4265 * only its widgets. Arguably, this could be common code and we not use
4266 * the window focus at all, but let's be safe.
4267 */
4268 if (gtk_socket_id == 0)
4269 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004270#if GTK_CHECK_VERSION(3,0,0)
4271 g_signal_connect(G_OBJECT(gui.mainwin), "focus-out-event",
4272 G_CALLBACK(focus_out_event), NULL);
4273 g_signal_connect(G_OBJECT(gui.mainwin), "focus-in-event",
4274 G_CALLBACK(focus_in_event), NULL);
4275#else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004276 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_out_event",
4277 GTK_SIGNAL_FUNC(focus_out_event), NULL);
4278 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_in_event",
4279 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004280#endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004281 }
4282 else
4283 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004284#if GTK_CHECK_VERSION(3,0,0)
4285 g_signal_connect(G_OBJECT(gui.drawarea), "focus-out-event",
4286 G_CALLBACK(focus_out_event), NULL);
4287 g_signal_connect(G_OBJECT(gui.drawarea), "focus-in-event",
4288 G_CALLBACK(focus_in_event), NULL);
4289#else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004290 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_out_event",
4291 GTK_SIGNAL_FUNC(focus_out_event), NULL);
4292 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_in_event",
4293 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004294#endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004295#ifdef FEAT_GUI_TABLINE
Bram Moolenaar98921892016-02-23 17:14:37 +01004296# if GTK_CHECK_VERSION(3,0,0)
4297 g_signal_connect(G_OBJECT(gui.tabline), "focus-out-event",
4298 G_CALLBACK(focus_out_event), NULL);
4299 g_signal_connect(G_OBJECT(gui.tabline), "focus-in-event",
4300 G_CALLBACK(focus_in_event), NULL);
4301# else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004302 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_out_event",
4303 GTK_SIGNAL_FUNC(focus_out_event), NULL);
4304 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_in_event",
4305 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004306# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004307#endif /* FEAT_GUI_TABLINE */
4308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
Bram Moolenaar98921892016-02-23 17:14:37 +01004310#if GTK_CHECK_VERSION(3,0,0)
4311 g_signal_connect(G_OBJECT(gui.drawarea), "motion-notify-event",
4312 G_CALLBACK(motion_notify_event), NULL);
4313 g_signal_connect(G_OBJECT(gui.drawarea), "button-press-event",
4314 G_CALLBACK(button_press_event), NULL);
4315 g_signal_connect(G_OBJECT(gui.drawarea), "button-release-event",
4316 G_CALLBACK(button_release_event), NULL);
4317 g_signal_connect(G_OBJECT(gui.drawarea), "scroll-event",
4318 G_CALLBACK(&scroll_event), NULL);
4319#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "motion_notify_event",
4321 GTK_SIGNAL_FUNC(motion_notify_event), NULL);
4322 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_press_event",
4323 GTK_SIGNAL_FUNC(button_press_event), NULL);
4324 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_release_event",
4325 GTK_SIGNAL_FUNC(button_release_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 g_signal_connect(G_OBJECT(gui.drawarea), "scroll_event",
4327 G_CALLBACK(&scroll_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329
4330 /*
4331 * Add selection handler functions.
4332 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004333#if GTK_CHECK_VERSION(3,0,0)
4334 g_signal_connect(G_OBJECT(gui.drawarea), "selection-clear-event",
4335 G_CALLBACK(selection_clear_event), NULL);
4336 g_signal_connect(G_OBJECT(gui.drawarea), "selection-received",
4337 G_CALLBACK(selection_received_cb), NULL);
4338#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_clear_event",
4340 GTK_SIGNAL_FUNC(selection_clear_event), NULL);
4341 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_received",
4342 GTK_SIGNAL_FUNC(selection_received_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344
Bram Moolenaara76638f2010-06-05 12:49:46 +02004345 gui_gtk_set_selection_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346
Bram Moolenaar98921892016-02-23 17:14:37 +01004347#if GTK_CHECK_VERSION(3,0,0)
4348 g_signal_connect(G_OBJECT(gui.drawarea), "selection-get",
4349 G_CALLBACK(selection_get_cb), NULL);
4350#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_get",
4352 GTK_SIGNAL_FUNC(selection_get_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
4355 /* Pretend we don't have input focus, we will get an event if we do. */
4356 gui.in_focus = FALSE;
4357
Bram Moolenaar7ebf4e12018-08-07 20:01:40 +02004358 // Handle changes to the "Xft/DPI" setting.
4359 {
4360 GtkSettings *gtk_settings =
4361 gtk_settings_get_for_screen(gdk_screen_get_default());
4362
4363 g_signal_connect(gtk_settings, "notify::gtk-xft-dpi",
4364 G_CALLBACK(gtk_settings_xft_dpi_changed_cb), NULL);
4365 }
4366
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 return OK;
4368}
4369
4370#if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO)
4371/*
4372 * This is called from gui_start() after a fork() has been done.
4373 * We have to tell the session manager our new PID.
4374 */
4375 void
4376gui_mch_forked(void)
4377{
4378 if (using_gnome)
4379 {
4380 GnomeClient *client;
4381
4382 client = gnome_master_client();
4383
4384 if (client != NULL)
4385 gnome_client_set_process_id(client, getpid());
4386 }
4387}
4388#endif /* FEAT_GUI_GNOME && FEAT_SESSION */
4389
Bram Moolenaar98921892016-02-23 17:14:37 +01004390#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02004391 static GdkRGBA
4392color_to_rgba(guicolor_T color)
Bram Moolenaar98921892016-02-23 17:14:37 +01004393{
Bram Moolenaar36edf062016-07-21 22:10:12 +02004394 GdkRGBA rgba;
4395 rgba.red = ((color & 0xff0000) >> 16) / 255.0;
4396 rgba.green = ((color & 0xff00) >> 8) / 255.0;
4397 rgba.blue = ((color & 0xff)) / 255.0;
4398 rgba.alpha = 1.0;
4399 return rgba;
Bram Moolenaar98921892016-02-23 17:14:37 +01004400}
4401
4402 static void
Bram Moolenaar36edf062016-07-21 22:10:12 +02004403set_cairo_source_rgba_from_color(cairo_t *cr, guicolor_T color)
Bram Moolenaar98921892016-02-23 17:14:37 +01004404{
Bram Moolenaar36edf062016-07-21 22:10:12 +02004405 const GdkRGBA rgba = color_to_rgba(color);
4406 cairo_set_source_rgba(cr, rgba.red, rgba.green, rgba.blue, rgba.alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01004407}
4408#endif /* GTK_CHECK_VERSION(3,0,0) */
4409
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410/*
4411 * Called when the foreground or background color has been changed.
4412 * This used to change the graphics contexts directly but we are
4413 * currently manipulating them where desired.
4414 */
4415 void
4416gui_mch_new_colors(void)
4417{
Bram Moolenaar98921892016-02-23 17:14:37 +01004418#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaara859f042016-11-17 19:11:55 +01004419# if !GTK_CHECK_VERSION(3,22,2)
Bram Moolenaar98921892016-02-23 17:14:37 +01004420 GdkWindow * const da_win = gtk_widget_get_window(gui.drawarea);
Bram Moolenaara859f042016-11-17 19:11:55 +01004421# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004422
4423 if (gui.drawarea != NULL && gtk_widget_get_window(gui.drawarea) != NULL)
4424#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01004426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 {
Bram Moolenaara859f042016-11-17 19:11:55 +01004428#if GTK_CHECK_VERSION(3,22,2)
4429 GtkStyleContext * const context
4430 = gtk_widget_get_style_context(gui.drawarea);
4431 GtkCssProvider * const provider = gtk_css_provider_new();
4432 gchar * const css = g_strdup_printf(
4433 "widget#vim-gui-drawarea {\n"
4434 " background-color: #%.2lx%.2lx%.2lx;\n"
4435 "}\n",
4436 (gui.back_pixel >> 16) & 0xff,
4437 (gui.back_pixel >> 8) & 0xff,
4438 gui.back_pixel & 0xff);
4439
4440 gtk_css_provider_load_from_data(provider, css, -1, NULL);
4441 gtk_style_context_add_provider(context,
4442 GTK_STYLE_PROVIDER(provider), G_MAXUINT);
4443
4444 g_free(css);
4445 g_object_unref(provider);
4446#elif GTK_CHECK_VERSION(3,4,0) /* !GTK_CHECK_VERSION(3,22,2) */
Bram Moolenaar36edf062016-07-21 22:10:12 +02004447 GdkRGBA rgba;
Bram Moolenaar98921892016-02-23 17:14:37 +01004448
Bram Moolenaar36edf062016-07-21 22:10:12 +02004449 rgba = color_to_rgba(gui.back_pixel);
Bram Moolenaar98921892016-02-23 17:14:37 +01004450 {
4451 cairo_pattern_t * const pat = cairo_pattern_create_rgba(
Bram Moolenaar36edf062016-07-21 22:10:12 +02004452 rgba.red, rgba.green, rgba.blue, rgba.alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01004453 if (pat != NULL)
4454 {
4455 gdk_window_set_background_pattern(da_win, pat);
4456 cairo_pattern_destroy(pat);
4457 }
4458 else
Bram Moolenaar36edf062016-07-21 22:10:12 +02004459 gdk_window_set_background_rgba(da_win, &rgba);
Bram Moolenaar98921892016-02-23 17:14:37 +01004460 }
4461#else /* !GTK_CHECK_VERSION(3,4,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 GdkColor color = { 0, 0, 0, 0 };
4463
4464 color.pixel = gui.back_pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01004465# if GTK_CHECK_VERSION(3,0,0)
4466 gdk_window_set_background(da_win, &color);
4467# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 gdk_window_set_background(gui.drawarea->window, &color);
Bram Moolenaar98921892016-02-23 17:14:37 +01004469# endif
Bram Moolenaara859f042016-11-17 19:11:55 +01004470#endif /* !GTK_CHECK_VERSION(3,22,2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 }
4472}
4473
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474/*
4475 * This signal informs us about the need to rearrange our sub-widgets.
4476 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004478form_configure_event(GtkWidget *widget UNUSED,
4479 GdkEventConfigure *event,
4480 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481{
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004482 int usable_height = event->height;
4483
Bram Moolenaar182707a2016-11-21 20:55:58 +01004484#if GTK_CHECK_VERSION(3,22,2) && !GTK_CHECK_VERSION(3,22,4)
Bram Moolenaara859f042016-11-17 19:11:55 +01004485 /* As of 3.22.2, GdkWindows have started distributing configure events to
4486 * their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0).
4487 *
4488 * As can be seen from the implementation of move_native_children() and
4489 * configure_native_child() in gdkwindow.c, those functions actually
4490 * propagate configure events to every child, failing to distinguish
4491 * "native" one from non-native one.
4492 *
4493 * Naturally, configure events propagated to here like that are fallacious
4494 * and, as a matter of fact, they trigger a geometric collapse of
4495 * gui.formwin.
4496 *
4497 * To filter out such fallacious events, check if the given event is the
4498 * one that was sent out to the right place. Ignore it if not.
4499 */
Bram Moolenaar182707a2016-11-21 20:55:58 +01004500 /* Follow-up
4501 * After a few weeks later, the GdkWindow change mentioned above was
4502 * reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155).
4503 * The corresponding official release is 3.22.4. */
Bram Moolenaara859f042016-11-17 19:11:55 +01004504 if (event->window != gtk_widget_get_window(gui.formwin))
4505 return TRUE;
4506#endif
4507
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004508 /* When in a GtkPlug, we can't guarantee valid heights (as a round
4509 * no. of char-heights), so we have to manually sanitise them.
4510 * Widths seem to sort themselves out, don't ask me why.
4511 */
4512 if (gtk_socket_id != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004513 usable_height -= (gui.char_height - (gui.char_height/2)); /* sic. */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004514
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 gtk_form_freeze(GTK_FORM(gui.formwin));
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004516 gui_resize_shell(event->width, usable_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 gtk_form_thaw(GTK_FORM(gui.formwin));
4518
4519 return TRUE;
4520}
4521
4522/*
4523 * Function called when window already closed.
4524 * We can't do much more here than to trying to preserve what had been done,
4525 * since the window is already inevitably going away.
4526 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004527#if GTK_CHECK_VERSION(3,0,0)
4528 static void
4529mainwin_destroy_cb(GObject *object UNUSED, gpointer data UNUSED)
4530#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004532mainwin_destroy_cb(GtkObject *object UNUSED, gpointer data UNUSED)
Bram Moolenaar98921892016-02-23 17:14:37 +01004533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534{
4535 /* Don't write messages to the GUI anymore */
4536 full_screen = FALSE;
4537
4538 gui.mainwin = NULL;
4539 gui.drawarea = NULL;
4540
4541 if (!exiting) /* only do anything if the destroy was unexpected */
4542 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004543 vim_strncpy(IObuff,
4544 (char_u *)_("Vim: Main window unexpectedly destroyed\n"),
4545 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 preserve_exit();
4547 }
Bram Moolenaar36e294c2015-12-29 18:55:46 +01004548#ifdef USE_GRESOURCE
4549 gui_gtk_unregister_resource();
4550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551}
4552
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004553
4554/*
4555 * Bit of a hack to ensure we start GtkPlug windows with the correct window
4556 * hints (and thus the required size from -geom), but that after that we
4557 * put the hints back to normal (the actual minimum size) so we may
4558 * subsequently be resized smaller. GtkSocket (the parent end) uses the
Bram Moolenaar49325942007-05-10 19:19:59 +00004559 * plug's window 'min hints to set *it's* minimum size, but that's also the
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004560 * only way we have of making ourselves bigger (by set lines/columns).
4561 * Thus set hints at start-up to ensure correct init. size, then a
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01004562 * second after the final attempt to reset the real minimum hints (done by
4563 * scrollbar init.), actually do the standard hints and stop the timer.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004564 * We'll not let the default hints be set while this timer's active.
4565 */
Bram Moolenaar4ab79682017-08-27 14:50:47 +02004566 static timeout_cb_type
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004567check_startup_plug_hints(gpointer data UNUSED)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004568{
4569 if (init_window_hints_state == 1)
4570 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004571 /* Safe to use normal hints now */
4572 init_window_hints_state = 0;
4573 update_window_manager_hints(0, 0);
4574 return FALSE; /* stop timer */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004575 }
4576
4577 /* Keep on trying */
4578 init_window_hints_state = 1;
4579 return TRUE;
4580}
4581
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582/*
4583 * Open the GUI window which was created by a call to gui_mch_init().
4584 */
4585 int
4586gui_mch_open(void)
4587{
4588 guicolor_T fg_pixel = INVALCOLOR;
4589 guicolor_T bg_pixel = INVALCOLOR;
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004590 guint pixel_width;
4591 guint pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 /*
4594 * Allow setting a window role on the command line, or invent one
4595 * if none was specified. This is mainly useful for GNOME session
4596 * support; allowing the WM to restore window placement.
4597 */
4598 if (role_argument != NULL)
4599 {
4600 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument);
4601 }
4602 else
4603 {
4604 char *role;
4605
4606 /* Invent a unique-enough ID string for the role */
4607 role = g_strdup_printf("vim-%u-%u-%u",
4608 (unsigned)mch_get_pid(),
4609 (unsigned)g_random_int(),
4610 (unsigned)time(NULL));
4611
4612 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role);
4613 g_free(role);
4614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615
4616 if (gui_win_x != -1 && gui_win_y != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618
4619 /* Determine user specified geometry, if present. */
4620 if (gui.geom != NULL)
4621 {
4622 int mask;
4623 unsigned int w, h;
4624 int x = 0;
4625 int y = 0;
4626
4627 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
4628
4629 if (mask & WidthValue)
4630 Columns = w;
4631 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00004632 {
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004633 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00004634 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00004636 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004637 limit_screen_size();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004638
4639 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4640 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
4641
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004642 pixel_width += get_menu_tool_width();
4643 pixel_height += get_menu_tool_height();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004644
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 if (mask & (XValue | YValue))
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004646 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004647 int ww, hh;
4648 gui_mch_get_screen_dimensions(&ww, &hh);
4649 hh += p_ghr + get_menu_tool_height();
4650 ww += get_menu_tool_width();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004651 if (mask & XNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004652 x += ww - pixel_width;
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004653 if (mask & YNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004654 y += hh - pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004656 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01004657 VIM_CLEAR(gui.geom);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004658
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004659 /* From now until everyone's stopped trying to set the window hints
4660 * to their correct minimum values, stop them being set as we need
4661 * them to remain at our required size for the parent GtkSocket to
4662 * give us the right initial size.
4663 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004664 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004665 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004666 update_window_manager_hints(pixel_width, pixel_height);
4667 init_window_hints_state = 1;
Bram Moolenaar4ab79682017-08-27 14:50:47 +02004668 timeout_add(1000, check_startup_plug_hints, NULL);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 }
4671
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004672 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4673 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004674 /* For GTK2 changing the size of the form widget doesn't cause window
4675 * resizing. */
Bram Moolenaardebe25a2010-06-06 17:41:24 +02004676 if (gtk_socket_id == 0)
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004677 gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004678 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679
4680 if (foreground_argument != NULL)
4681 fg_pixel = gui_get_color((char_u *)foreground_argument);
4682 if (fg_pixel == INVALCOLOR)
4683 fg_pixel = gui_get_color((char_u *)"Black");
4684
4685 if (background_argument != NULL)
4686 bg_pixel = gui_get_color((char_u *)background_argument);
4687 if (bg_pixel == INVALCOLOR)
4688 bg_pixel = gui_get_color((char_u *)"White");
4689
4690 if (found_reverse_arg)
4691 {
4692 gui.def_norm_pixel = bg_pixel;
4693 gui.def_back_pixel = fg_pixel;
4694 }
4695 else
4696 {
4697 gui.def_norm_pixel = fg_pixel;
4698 gui.def_back_pixel = bg_pixel;
4699 }
4700
4701 /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or
4702 * in a vimrc file) */
4703 set_normal_colors();
4704
4705 /* Check that none of the colors are the same as the background color */
4706 gui_check_colors();
4707
4708 /* Get the colors for the highlight groups (gui_check_colors() might have
4709 * changed them). */
4710 highlight_gui_started(); /* re-init colors and fonts */
4711
Bram Moolenaar98921892016-02-23 17:14:37 +01004712#if GTK_CHECK_VERSION(3,0,0)
4713 g_signal_connect(G_OBJECT(gui.mainwin), "destroy",
4714 G_CALLBACK(mainwin_destroy_cb), NULL);
4715#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "destroy",
4717 GTK_SIGNAL_FUNC(mainwin_destroy_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719
4720#ifdef FEAT_HANGULIN
4721 hangul_keyboard_set();
4722#endif
4723
4724 /*
4725 * Notify the fixed area about the need to resize the contents of the
4726 * gui.formwin, which we use for random positioning of the included
4727 * components.
4728 *
4729 * We connect this signal deferred finally after anything is in place,
4730 * since this is intended to handle resizements coming from the window
4731 * manager upon us and should not interfere with what VIM is requesting
4732 * upon startup.
4733 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004734#if GTK_CHECK_VERSION(3,0,0)
4735 g_signal_connect(G_OBJECT(gui.formwin), "configure-event",
4736 G_CALLBACK(form_configure_event), NULL);
4737#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 gtk_signal_connect(GTK_OBJECT(gui.formwin), "configure_event",
4739 GTK_SIGNAL_FUNC(form_configure_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741
4742#ifdef FEAT_DND
Bram Moolenaara76638f2010-06-05 12:49:46 +02004743 /* Set up for receiving DND items. */
4744 gui_gtk_set_dnd_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745
Bram Moolenaar98921892016-02-23 17:14:37 +01004746# if GTK_CHECK_VERSION(3,0,0)
4747 g_signal_connect(G_OBJECT(gui.drawarea), "drag-data-received",
4748 G_CALLBACK(drag_data_received_cb), NULL);
4749# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "drag_data_received",
4751 GTK_SIGNAL_FUNC(drag_data_received_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004752# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753#endif
4754
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 /* With GTK+ 2, we need to iconify the window before calling show()
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004756 * to avoid mapping the window for a short time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 if (found_iconic_arg && gtk_socket_id == 0)
4758 gui_mch_iconify();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759
4760 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004761#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 unsigned long menu_handler = 0;
4763# ifdef FEAT_TOOLBAR
4764 unsigned long tool_handler = 0;
4765# endif
4766 /*
4767 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
4768 * show when restoring the saved layout configuration. We can't just
4769 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
4770 * a toplevel window and thus will be realized immediately. Instead,
4771 * connect signal handlers to hide the widgets just after they've been
4772 * marked visible, but before the main window is realized.
4773 */
4774 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL)
4775 menu_handler = g_signal_connect_after(gui.menubar_h, "show",
4776 G_CALLBACK(&gtk_widget_hide),
4777 NULL);
4778# ifdef FEAT_TOOLBAR
4779 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL
4780 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
4781 tool_handler = g_signal_connect_after(gui.toolbar_h, "show",
4782 G_CALLBACK(&gtk_widget_hide),
4783 NULL);
4784# endif
4785#endif
4786 gtk_widget_show(gui.mainwin);
4787
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004788#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 if (menu_handler != 0)
4790 g_signal_handler_disconnect(gui.menubar_h, menu_handler);
4791# ifdef FEAT_TOOLBAR
4792 if (tool_handler != 0)
4793 g_signal_handler_disconnect(gui.toolbar_h, tool_handler);
4794# endif
4795#endif
4796 }
4797
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 return OK;
4799}
4800
4801
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004803gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804{
4805 if (gui.mainwin != NULL)
4806 gtk_widget_destroy(gui.mainwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807}
4808
4809/*
4810 * Get the position of the top left corner of the window.
4811 */
4812 int
4813gui_mch_get_winpos(int *x, int *y)
4814{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 return OK;
4817}
4818
4819/*
4820 * Set the position of the top left corner of the window to the given
4821 * coordinates.
4822 */
4823 void
4824gui_mch_set_winpos(int x, int y)
4825{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827}
4828
Bram Moolenaar98921892016-02-23 17:14:37 +01004829#if !GTK_CHECK_VERSION(3,0,0)
4830# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831static int resize_idle_installed = FALSE;
4832/*
4833 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
4834 * the shell size doesn't exceed the window size, i.e. if the window manager
4835 * ignored our size request. Usually this happens if the window is maximized.
4836 *
4837 * FIXME: It'd be nice if we could find a little more orthodox solution.
4838 * See also the remark below in gui_mch_set_shellsize().
4839 *
4840 * DISABLED: When doing ":set lines+=1" this function would first invoke
4841 * gui_resize_shell() with the old size, then the normal callback would
4842 * report the new size through form_configure_event(). That caused the window
4843 * layout to be messed up.
4844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 static gboolean
4846force_shell_resize_idle(gpointer data)
4847{
4848 if (gui.mainwin != NULL
4849 && GTK_WIDGET_REALIZED(gui.mainwin)
4850 && GTK_WIDGET_VISIBLE(gui.mainwin))
4851 {
4852 int width;
4853 int height;
4854
4855 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height);
4856
4857 width -= get_menu_tool_width();
4858 height -= get_menu_tool_height();
4859
4860 gui_resize_shell(width, height);
4861 }
4862
4863 resize_idle_installed = FALSE;
4864 return FALSE; /* don't call me again */
4865}
Bram Moolenaar98921892016-02-23 17:14:37 +01004866# endif
4867#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868
Bram Moolenaar09736232009-09-23 16:14:49 +00004869/*
4870 * Return TRUE if the main window is maximized.
4871 */
4872 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004873gui_mch_maximized(void)
Bram Moolenaar09736232009-09-23 16:14:49 +00004874{
Bram Moolenaar98921892016-02-23 17:14:37 +01004875 return (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL
4876 && (gdk_window_get_state(gtk_widget_get_window(gui.mainwin))
4877 & GDK_WINDOW_STATE_MAXIMIZED));
Bram Moolenaar09736232009-09-23 16:14:49 +00004878}
4879
4880/*
4881 * Unmaximize the main window
4882 */
4883 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004884gui_mch_unmaximize(void)
Bram Moolenaar09736232009-09-23 16:14:49 +00004885{
4886 if (gui.mainwin != NULL)
4887 gtk_window_unmaximize(GTK_WINDOW(gui.mainwin));
4888}
Bram Moolenaar09736232009-09-23 16:14:49 +00004889
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01004891 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
4892 * is set. Compute the new Rows and Columns. This is like resizing the
4893 * window.
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004894 */
4895 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004896gui_mch_newfont(void)
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004897{
4898 int w, h;
4899
4900 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
4901 w -= get_menu_tool_width();
4902 h -= get_menu_tool_height();
4903 gui_resize_shell(w, h);
4904}
4905
4906/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 * Set the windows size.
4908 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 void
4910gui_mch_set_shellsize(int width, int height,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004911 int min_width UNUSED, int min_height UNUSED,
4912 int base_width UNUSED, int base_height UNUSED,
4913 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 /* give GTK+ a chance to put all widget's into place */
4916 gui_mch_update();
4917
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004918 /* this will cause the proper resizement to happen too */
4919 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004920 update_window_manager_hints(0, 0);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004921
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 /* With GTK+ 2, changing the size of the form widget doesn't resize
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004923 * the window. So let's do it the other way around and resize the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 * main window instead. */
4925 width += get_menu_tool_width();
4926 height += get_menu_tool_height();
4927
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004928 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004929 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004930 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004931 update_window_manager_hints(width, height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932
Bram Moolenaar98921892016-02-23 17:14:37 +01004933# if !GTK_CHECK_VERSION(3,0,0)
4934# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 if (!resize_idle_installed)
4936 {
4937 g_idle_add_full(GDK_PRIORITY_EVENTS + 10,
4938 &force_shell_resize_idle, NULL, NULL);
4939 resize_idle_installed = TRUE;
4940 }
Bram Moolenaar98921892016-02-23 17:14:37 +01004941# endif
4942# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 /*
4944 * Wait until all events are processed to prevent a crash because the
4945 * real size of the drawing area doesn't reflect Vim's internal ideas.
4946 *
4947 * This is a bit of a hack, since Vim is a terminal application with a GUI
4948 * on top, while the GUI expects to be the boss.
4949 */
4950 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951}
4952
Bram Moolenaar7be9b502017-09-09 18:45:26 +02004953 void
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +02004954gui_gtk_get_screen_geom_of_win(
4955 GtkWidget *wid,
4956 int *screen_x,
4957 int *screen_y,
4958 int *width,
4959 int *height)
Bram Moolenaar7be9b502017-09-09 18:45:26 +02004960{
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +02004961 GdkRectangle geometry;
4962 GdkWindow *win = gtk_widget_get_window(wid);
Bram Moolenaar7be9b502017-09-09 18:45:26 +02004963#if GTK_CHECK_VERSION(3,22,0)
Bram Moolenaar8696bba2017-09-09 23:00:56 +02004964 GdkDisplay *dpy = gtk_widget_get_display(wid);
Bram Moolenaar7be9b502017-09-09 18:45:26 +02004965 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
Bram Moolenaar7be9b502017-09-09 18:45:26 +02004966
4967 gdk_monitor_get_geometry(monitor, &geometry);
Bram Moolenaar7be9b502017-09-09 18:45:26 +02004968#else
4969 GdkScreen* screen;
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +02004970 int monitor;
Bram Moolenaar7be9b502017-09-09 18:45:26 +02004971
Bram Moolenaar8696bba2017-09-09 23:00:56 +02004972 if (wid != NULL && gtk_widget_has_screen(wid))
4973 screen = gtk_widget_get_screen(wid);
Bram Moolenaar7be9b502017-09-09 18:45:26 +02004974 else
4975 screen = gdk_screen_get_default();
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +02004976 monitor = gdk_screen_get_monitor_at_window(screen, win);
4977 gdk_screen_get_monitor_geometry(screen, monitor, &geometry);
Bram Moolenaar7be9b502017-09-09 18:45:26 +02004978#endif
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +02004979 *screen_x = geometry.x;
4980 *screen_y = geometry.y;
4981 *width = geometry.width;
4982 *height = geometry.height;
Bram Moolenaar7be9b502017-09-09 18:45:26 +02004983}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984
4985/*
4986 * The screen size is used to make sure the initial window doesn't get bigger
4987 * than the screen. This subtracts some room for menubar, toolbar and window
4988 * decorations.
4989 */
4990 void
4991gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
4992{
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +02004993 int x, y;
4994
4995 gui_gtk_get_screen_geom_of_win(gui.mainwin, &x, &y, screen_w, screen_h);
Bram Moolenaara859f042016-11-17 19:11:55 +01004996
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02004997 /* Subtract 'guiheadroom' from the height to allow some room for the
4998 * window manager (task list and window title bar). */
Bram Moolenaar7be9b502017-09-09 18:45:26 +02004999 *screen_h -= p_ghr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000
5001 /*
5002 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
5003 * the toolbar and menubar for GTK, we subtract them from the screen
Bram Moolenaar64404472010-06-26 06:24:45 +02005004 * height, so that the window size can be made to fit on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 * This should be completely changed later.
5006 */
5007 *screen_w -= get_menu_tool_width();
5008 *screen_h -= get_menu_tool_height();
5009}
5010
5011#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005013gui_mch_settitle(char_u *title, char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 if (title != NULL && output_conv.vc_type != CONV_NONE)
5016 title = string_convert(&output_conv, title, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017
5018 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
5019
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 if (output_conv.vc_type != CONV_NONE)
5021 vim_free(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022}
5023#endif /* FEAT_TITLE */
5024
5025#if defined(FEAT_MENU) || defined(PROTO)
5026 void
5027gui_mch_enable_menu(int showit)
5028{
5029 GtkWidget *widget;
5030
5031# ifdef FEAT_GUI_GNOME
5032 if (using_gnome)
5033 widget = gui.menubar_h;
5034 else
5035# endif
5036 widget = gui.menubar;
5037
Bram Moolenaar18144c82006-04-12 21:52:12 +00005038 /* Do not disable the menu while starting up, otherwise F10 doesn't work. */
Bram Moolenaar98921892016-02-23 17:14:37 +01005039# if GTK_CHECK_VERSION(3,0,0)
5040 if (!showit != !gtk_widget_get_visible(widget) && !gui.starting)
5041# else
Bram Moolenaar18144c82006-04-12 21:52:12 +00005042 if (!showit != !GTK_WIDGET_VISIBLE(widget) && !gui.starting)
Bram Moolenaar98921892016-02-23 17:14:37 +01005043# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
5045 if (showit)
5046 gtk_widget_show(widget);
5047 else
5048 gtk_widget_hide(widget);
5049
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00005050 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 }
5052}
5053#endif /* FEAT_MENU */
5054
5055#if defined(FEAT_TOOLBAR) || defined(PROTO)
5056 void
5057gui_mch_show_toolbar(int showit)
5058{
5059 GtkWidget *widget;
5060
5061 if (gui.toolbar == NULL)
5062 return;
5063
5064# ifdef FEAT_GUI_GNOME
5065 if (using_gnome)
5066 widget = gui.toolbar_h;
5067 else
5068# endif
5069 widget = gui.toolbar;
5070
5071 if (showit)
5072 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
5073
Bram Moolenaar98921892016-02-23 17:14:37 +01005074# if GTK_CHECK_VERSION(3,0,0)
5075 if (!showit != !gtk_widget_get_visible(widget))
5076# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 if (!showit != !GTK_WIDGET_VISIBLE(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01005078# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 {
5080 if (showit)
5081 gtk_widget_show(widget);
5082 else
5083 gtk_widget_hide(widget);
5084
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00005085 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 }
5087}
5088#endif /* FEAT_TOOLBAR */
5089
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090/*
5091 * Check if a given font is a CJK font. This is done in a very crude manner. It
5092 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
5093 * font. Consequently, this function cannot be used as a general purpose check
5094 * for CJK-ness for which fontconfig APIs should be used. This is only used by
5095 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
5096 */
5097 static int
5098is_cjk_font(PangoFontDescription *font_desc)
5099{
5100 static const char * const cjk_langs[] =
5101 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
5102
5103 PangoFont *font;
5104 unsigned i;
5105 int is_cjk = FALSE;
5106
5107 font = pango_context_load_font(gui.text_context, font_desc);
5108
5109 if (font == NULL)
5110 return FALSE;
5111
5112 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i)
5113 {
5114 PangoCoverage *coverage;
5115 gunichar uc;
5116
5117 coverage = pango_font_get_coverage(
5118 font, pango_language_from_string(cjk_langs[i]));
5119
5120 if (coverage != NULL)
5121 {
5122 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00;
5123 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT);
5124 pango_coverage_unref(coverage);
5125 }
5126 }
5127
5128 g_object_unref(font);
5129
5130 return is_cjk;
5131}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132
Bram Moolenaar231334e2005-07-25 20:46:57 +00005133/*
5134 * Adjust gui.char_height (after 'linespace' was changed).
5135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 int
Bram Moolenaar231334e2005-07-25 20:46:57 +00005137gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 PangoFontMetrics *metrics;
5140 int ascent;
5141 int descent;
5142
5143 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font,
5144 pango_context_get_language(gui.text_context));
5145 ascent = pango_font_metrics_get_ascent(metrics);
5146 descent = pango_font_metrics_get_descent(metrics);
5147
5148 pango_font_metrics_unref(metrics);
5149
5150 gui.char_height = (ascent + descent + PANGO_SCALE - 1) / PANGO_SCALE
Bram Moolenaar231334e2005-07-25 20:46:57 +00005151 + p_linespace;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005152 /* LINTED: avoid warning: bitwise operation on signed value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2);
5154
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 /* A not-positive value of char_height may crash Vim. Only happens
5156 * if 'linespace' is negative (which does make sense sometimes). */
5157 gui.char_ascent = MAX(gui.char_ascent, 0);
5158 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
5159
5160 return OK;
5161}
5162
Bram Moolenaar98921892016-02-23 17:14:37 +01005163#if GTK_CHECK_VERSION(3,0,0)
5164/* Callback function used in gui_mch_font_dialog() */
5165 static gboolean
5166font_filter(const PangoFontFamily *family,
5167 const PangoFontFace *face UNUSED,
5168 gpointer data UNUSED)
5169{
5170 return pango_font_family_is_monospace((PangoFontFamily *)family);
5171}
5172#endif
5173
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174/*
5175 * Put up a font dialog and return the selected font name in allocated memory.
5176 * "oldval" is the previous value. Return NULL when cancelled.
5177 * This should probably go into gui_gtk.c. Hmm.
5178 * FIXME:
5179 * The GTK2 font selection dialog has no filtering API. So we could either
5180 * a) implement our own (possibly copying the code from somewhere else) or
5181 * b) just live with it.
5182 */
5183 char_u *
5184gui_mch_font_dialog(char_u *oldval)
5185{
5186 GtkWidget *dialog;
5187 int response;
5188 char_u *fontname = NULL;
5189 char_u *oldname;
5190
Bram Moolenaar98921892016-02-23 17:14:37 +01005191#if GTK_CHECK_VERSION(3,2,0)
5192 dialog = gtk_font_chooser_dialog_new(NULL, NULL);
5193 gtk_font_chooser_set_filter_func(GTK_FONT_CHOOSER(dialog), font_filter,
5194 NULL, NULL);
5195#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 dialog = gtk_font_selection_dialog_new(NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01005197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198
5199 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
5200 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
5201
5202 if (oldval != NULL && oldval[0] != NUL)
5203 {
5204 if (output_conv.vc_type != CONV_NONE)
5205 oldname = string_convert(&output_conv, oldval, NULL);
5206 else
5207 oldname = oldval;
5208
5209 /* Annoying bug in GTK (or Pango): if the font name does not include a
5210 * size, zero is used. Use default point size ten. */
5211 if (!vim_isdigit(oldname[STRLEN(oldname) - 1]))
5212 {
5213 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3);
5214
5215 if (p != NULL)
5216 {
5217 STRCPY(p + STRLEN(p), " 10");
5218 if (oldname != oldval)
5219 vim_free(oldname);
5220 oldname = p;
5221 }
5222 }
5223
Bram Moolenaar98921892016-02-23 17:14:37 +01005224#if GTK_CHECK_VERSION(3,2,0)
5225 gtk_font_chooser_set_font(
5226 GTK_FONT_CHOOSER(dialog), (const gchar *)oldname);
5227#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 gtk_font_selection_dialog_set_font_name(
5229 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname);
Bram Moolenaar98921892016-02-23 17:14:37 +01005230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231
5232 if (oldname != oldval)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005233 vim_free(oldname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 }
Bram Moolenaarbef9d832009-09-11 13:46:41 +00005235 else
Bram Moolenaar98921892016-02-23 17:14:37 +01005236#if GTK_CHECK_VERSION(3,2,0)
5237 gtk_font_chooser_set_font(
5238 GTK_FONT_CHOOSER(dialog), DEFAULT_FONT);
5239#else
Bram Moolenaarbef9d832009-09-11 13:46:41 +00005240 gtk_font_selection_dialog_set_font_name(
5241 GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT);
Bram Moolenaar98921892016-02-23 17:14:37 +01005242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243
5244 response = gtk_dialog_run(GTK_DIALOG(dialog));
5245
5246 if (response == GTK_RESPONSE_OK)
5247 {
5248 char *name;
5249
Bram Moolenaar98921892016-02-23 17:14:37 +01005250#if GTK_CHECK_VERSION(3,2,0)
5251 name = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(dialog));
5252#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 name = gtk_font_selection_dialog_get_font_name(
5254 GTK_FONT_SELECTION_DIALOG(dialog));
Bram Moolenaar98921892016-02-23 17:14:37 +01005255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 if (name != NULL)
5257 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005258 char_u *p;
5259
5260 /* Apparently some font names include a comma, need to escape
5261 * that, because in 'guifont' it separates names. */
5262 p = vim_strsave_escaped((char_u *)name, (char_u *)",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 g_free(name);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005264 if (p != NULL && input_conv.vc_type != CONV_NONE)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005265 {
5266 fontname = string_convert(&input_conv, p, NULL);
5267 vim_free(p);
5268 }
5269 else
5270 fontname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 }
5272 }
5273
5274 if (response != GTK_RESPONSE_NONE)
5275 gtk_widget_destroy(dialog);
5276
5277 return fontname;
5278}
5279
5280/*
5281 * Some monospace fonts don't support a bold weight, and fall back
5282 * silently to the regular weight. But this is no good since our text
5283 * drawing function can emulate bold by overstriking. So let's try
5284 * to detect whether bold weight is actually available and emulate it
5285 * otherwise.
5286 *
5287 * Note that we don't need to check for italic style since Xft can
5288 * emulate italic on its own, provided you have a proper fontconfig
5289 * setup. We wouldn't be able to emulate it in Vim anyway.
5290 */
5291 static void
5292get_styled_font_variants(void)
5293{
5294 PangoFontDescription *bold_font_desc;
5295 PangoFont *plain_font;
5296 PangoFont *bold_font;
5297
5298 gui.font_can_bold = FALSE;
5299
5300 plain_font = pango_context_load_font(gui.text_context, gui.norm_font);
5301
5302 if (plain_font == NULL)
5303 return;
5304
5305 bold_font_desc = pango_font_description_copy_static(gui.norm_font);
5306 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD);
5307
5308 bold_font = pango_context_load_font(gui.text_context, bold_font_desc);
5309 /*
5310 * The comparison relies on the unique handle nature of a PangoFont*,
5311 * i.e. it's assumed that a different PangoFont* won't refer to the
5312 * same font. Seems to work, and failing here isn't critical anyway.
5313 */
5314 if (bold_font != NULL)
5315 {
5316 gui.font_can_bold = (bold_font != plain_font);
5317 g_object_unref(bold_font);
5318 }
5319
5320 pango_font_description_free(bold_font_desc);
5321 g_object_unref(plain_font);
5322}
5323
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324static PangoEngineShape *default_shape_engine = NULL;
5325
5326/*
5327 * Create a map from ASCII characters in the range [32,126] to glyphs
5328 * of the current font. This is used by gui_gtk2_draw_string() to skip
5329 * the itemize and shaping process for the most common case.
5330 */
5331 static void
5332ascii_glyph_table_init(void)
5333{
Bram Moolenaar16350cb2016-08-14 20:27:34 +02005334 char_u ascii_chars[2 * 128];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 PangoAttrList *attr_list;
5336 GList *item_list;
5337 int i;
5338
5339 if (gui.ascii_glyphs != NULL)
5340 pango_glyph_string_free(gui.ascii_glyphs);
5341 if (gui.ascii_font != NULL)
5342 g_object_unref(gui.ascii_font);
5343
5344 gui.ascii_glyphs = NULL;
5345 gui.ascii_font = NULL;
5346
Bram Moolenaar16350cb2016-08-14 20:27:34 +02005347 /* For safety, fill in question marks for the control characters.
5348 * Put a space between characters to avoid shaping. */
5349 for (i = 0; i < 128; ++i)
5350 {
5351 if (i >= 32 && i < 127)
5352 ascii_chars[2 * i] = i;
5353 else
5354 ascii_chars[2 * i] = '?';
5355 ascii_chars[2 * i + 1] = ' ';
5356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357
5358 attr_list = pango_attr_list_new();
5359 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars,
5360 0, sizeof(ascii_chars), attr_list, NULL);
5361
5362 if (item_list != NULL && item_list->next == NULL) /* play safe */
5363 {
5364 PangoItem *item;
5365 int width;
5366
5367 item = (PangoItem *)item_list->data;
5368 width = gui.char_width * PANGO_SCALE;
5369
5370 /* Remember the shape engine used for ASCII. */
5371 default_shape_engine = item->analysis.shape_engine;
5372
5373 gui.ascii_font = item->analysis.font;
5374 g_object_ref(gui.ascii_font);
5375
5376 gui.ascii_glyphs = pango_glyph_string_new();
5377
5378 pango_shape((const char *)ascii_chars, sizeof(ascii_chars),
5379 &item->analysis, gui.ascii_glyphs);
5380
5381 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars));
5382
5383 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i)
5384 {
5385 PangoGlyphGeometry *geom;
5386
5387 geom = &gui.ascii_glyphs->glyphs[i].geometry;
5388 geom->x_offset += MAX(0, width - geom->width) / 2;
5389 geom->width = width;
5390 }
5391 }
5392
5393 g_list_foreach(item_list, (GFunc)&pango_item_free, NULL);
5394 g_list_free(item_list);
5395 pango_attr_list_unref(attr_list);
5396}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397
5398/*
5399 * Initialize Vim to use the font or fontset with the given name.
5400 * Return FAIL if the font could not be loaded, OK otherwise.
5401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 int
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005403gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 PangoFontDescription *font_desc;
5406 PangoLayout *layout;
5407 int width;
5408
5409 /* If font_name is NULL, this means to use the default, which should
5410 * be present on all proper Pango/fontconfig installations. */
5411 if (font_name == NULL)
5412 font_name = (char_u *)DEFAULT_FONT;
5413
5414 font_desc = gui_mch_get_font(font_name, FALSE);
5415
5416 if (font_desc == NULL)
5417 return FAIL;
5418
5419 gui_mch_free_font(gui.norm_font);
5420 gui.norm_font = font_desc;
5421
5422 pango_context_set_font_description(gui.text_context, font_desc);
5423
5424 layout = pango_layout_new(gui.text_context);
5425 pango_layout_set_text(layout, "MW", 2);
5426 pango_layout_get_size(layout, &width, NULL);
5427 /*
5428 * Set char_width to half the width obtained from pango_layout_get_size()
5429 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
5430 * Pango to use the same width for both non-CJK characters (e.g. Latin
5431 * letters and numbers) and CJK characters. This results in 's p a c e d
5432 * o u t' rendering when a CJK 'fixed width' font is used. To work around
5433 * that, divide the width returned by Pango by 2 if cjk_width is equal to
5434 * width for CJK fonts.
5435 *
5436 * For related bugs, see:
5437 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
5438 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
5439 *
5440 * With this, for all four of the following cases, Vim works fine:
5441 * guifont=CJK_fixed_width_font
5442 * guifont=Non_CJK_fixed_font
5443 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
5444 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
5445 */
5446 if (is_cjk_font(gui.norm_font))
5447 {
5448 int cjk_width;
5449
5450 /* Measure the text extent of U+4E00 and U+4E8C */
5451 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1);
5452 pango_layout_get_size(layout, &cjk_width, NULL);
5453
5454 if (width == cjk_width) /* Xft not patched */
5455 width /= 2;
5456 }
5457 g_object_unref(layout);
5458
5459 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE;
5460
5461 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
5462 if (gui.char_width <= 0)
5463 gui.char_width = 8;
5464
Bram Moolenaar231334e2005-07-25 20:46:57 +00005465 gui_mch_adjust_charheight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466
5467 /* Set the fontname, which will be used for information purposes */
5468 hl_set_font_name(font_name);
5469
5470 get_styled_font_variants();
5471 ascii_glyph_table_init();
5472
5473 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
5474 if (gui.wide_font != NULL
5475 && pango_font_description_equal(gui.norm_font, gui.wide_font))
5476 {
5477 pango_font_description_free(gui.wide_font);
5478 gui.wide_font = NULL;
5479 }
5480
Bram Moolenaare161c792009-11-03 17:13:59 +00005481 if (gui_mch_maximized())
5482 {
Bram Moolenaare161c792009-11-03 17:13:59 +00005483 /* Update lines and columns in accordance with the new font, keep the
5484 * window maximized. */
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02005485 gui_mch_newfont();
Bram Moolenaare161c792009-11-03 17:13:59 +00005486 }
5487 else
Bram Moolenaare161c792009-11-03 17:13:59 +00005488 {
5489 /* Preserve the logical dimensions of the screen. */
5490 update_window_manager_hints(0, 0);
5491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492
5493 return OK;
5494}
5495
5496/*
5497 * Get a reference to the font "name".
5498 * Return zero for failure.
5499 */
5500 GuiFont
5501gui_mch_get_font(char_u *name, int report_error)
5502{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 PangoFontDescription *font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504
5505 /* can't do this when GUI is not running */
5506 if (!gui.in_use || name == NULL)
5507 return NULL;
5508
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 if (output_conv.vc_type != CONV_NONE)
5510 {
5511 char_u *buf;
5512
5513 buf = string_convert(&output_conv, name, NULL);
5514 if (buf != NULL)
5515 {
5516 font = pango_font_description_from_string((const char *)buf);
5517 vim_free(buf);
5518 }
5519 else
5520 font = NULL;
5521 }
5522 else
5523 font = pango_font_description_from_string((const char *)name);
5524
5525 if (font != NULL)
5526 {
5527 PangoFont *real_font;
5528
5529 /* pango_context_load_font() bails out if no font size is set */
5530 if (pango_font_description_get_size(font) <= 0)
5531 pango_font_description_set_size(font, 10 * PANGO_SCALE);
5532
5533 real_font = pango_context_load_font(gui.text_context, font);
5534
5535 if (real_font == NULL)
5536 {
5537 pango_font_description_free(font);
5538 font = NULL;
5539 }
5540 else
5541 g_object_unref(real_font);
5542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543
5544 if (font == NULL)
5545 {
5546 if (report_error)
Bram Moolenaarc93e7912008-07-08 10:46:08 +00005547 EMSG2(_((char *)e_font), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 return NULL;
5549 }
5550
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 return font;
5552}
5553
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005554#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005555/*
5556 * Return the name of font "font" in allocated memory.
5557 */
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005558 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005559gui_mch_get_fontname(GuiFont font, char_u *name UNUSED)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005560{
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005561 if (font != NOFONT)
5562 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005563 char *pangoname = pango_font_description_to_string(font);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005564
Bram Moolenaar89d40322006-08-29 15:30:07 +00005565 if (pangoname != NULL)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005566 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005567 char_u *s = vim_strsave((char_u *)pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005568
Bram Moolenaar89d40322006-08-29 15:30:07 +00005569 g_free(pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005570 return s;
5571 }
5572 }
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005573 return NULL;
5574}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005575#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005576
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577/*
5578 * If a font is not going to be used, free its structure.
5579 */
5580 void
5581gui_mch_free_font(GuiFont font)
5582{
5583 if (font != NOFONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 pango_font_description_free(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585}
5586
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587/*
Bram Moolenaar36edf062016-07-21 22:10:12 +02005588 * Return the Pixel value (color) for the given color name.
5589 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 * Return INVALCOLOR for error.
5591 */
5592 guicolor_T
5593gui_mch_get_color(char_u *name)
5594{
Bram Moolenaar2e324952018-04-14 14:37:07 +02005595 guicolor_T color = INVALCOLOR;
5596
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 if (!gui.in_use) /* can't do this when GUI not running */
Bram Moolenaar2e324952018-04-14 14:37:07 +02005598 return color;
5599
5600 if (name != NULL)
5601 color = gui_get_color_cmn(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602
Bram Moolenaar98921892016-02-23 17:14:37 +01005603#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar2e324952018-04-14 14:37:07 +02005604 return color;
Bram Moolenaar98921892016-02-23 17:14:37 +01005605#else
Bram Moolenaar36edf062016-07-21 22:10:12 +02005606 if (color == INVALCOLOR)
5607 return INVALCOLOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608
Bram Moolenaar26af85d2017-07-23 16:45:10 +02005609 return gui_mch_get_rgb_color(
5610 (color & 0xff0000) >> 16,
5611 (color & 0xff00) >> 8,
5612 color & 0xff);
5613#endif
5614}
5615
5616/*
5617 * Return the Pixel value (color) for the given RGB values.
5618 * Return INVALCOLOR for error.
5619 */
5620 guicolor_T
5621gui_mch_get_rgb_color(int r, int g, int b)
5622{
5623#if GTK_CHECK_VERSION(3,0,0)
5624 return gui_get_rgb_color_cmn(r, g, b);
5625#else
5626 GdkColor gcolor;
5627 int ret;
5628
5629 gcolor.red = (guint16)(r / 255.0 * 65535 + 0.5);
5630 gcolor.green = (guint16)(g / 255.0 * 65535 + 0.5);
5631 gcolor.blue = (guint16)(b / 255.0 * 65535 + 0.5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632
Bram Moolenaar36edf062016-07-21 22:10:12 +02005633 ret = gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea),
5634 &gcolor, FALSE, TRUE);
5635
5636 return ret != 0 ? (guicolor_T)gcolor.pixel : INVALCOLOR;
5637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638}
5639
5640/*
5641 * Set the current text foreground color.
5642 */
5643 void
5644gui_mch_set_fg_color(guicolor_T color)
5645{
Bram Moolenaar36edf062016-07-21 22:10:12 +02005646#if GTK_CHECK_VERSION(3,0,0)
5647 *gui.fgcolor = color_to_rgba(color);
5648#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 gui.fgcolor->pixel = (unsigned long)color;
Bram Moolenaar36edf062016-07-21 22:10:12 +02005650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651}
5652
5653/*
5654 * Set the current text background color.
5655 */
5656 void
5657gui_mch_set_bg_color(guicolor_T color)
5658{
Bram Moolenaar36edf062016-07-21 22:10:12 +02005659#if GTK_CHECK_VERSION(3,0,0)
5660 *gui.bgcolor = color_to_rgba(color);
5661#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 gui.bgcolor->pixel = (unsigned long)color;
Bram Moolenaar36edf062016-07-21 22:10:12 +02005663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664}
5665
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005666/*
5667 * Set the current text special color.
5668 */
5669 void
5670gui_mch_set_sp_color(guicolor_T color)
5671{
Bram Moolenaar36edf062016-07-21 22:10:12 +02005672#if GTK_CHECK_VERSION(3,0,0)
5673 *gui.spcolor = color_to_rgba(color);
5674#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005675 gui.spcolor->pixel = (unsigned long)color;
Bram Moolenaar36edf062016-07-21 22:10:12 +02005676#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005677}
5678
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679/*
5680 * Function-like convenience macro for the sake of efficiency.
5681 */
5682#define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
5683 G_STMT_START{ \
5684 PangoAttribute *tmp_attr_; \
5685 tmp_attr_ = (Attribute); \
5686 tmp_attr_->start_index = (Start); \
5687 tmp_attr_->end_index = (End); \
5688 pango_attr_list_insert((AttrList), tmp_attr_); \
5689 }G_STMT_END
5690
5691 static void
5692apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list)
5693{
5694 char_u *start = NULL;
5695 char_u *p;
5696 int uc;
5697
5698 for (p = s; p < s + len; p += utf_byte2len(*p))
5699 {
5700 uc = utf_ptr2char(p);
5701
5702 if (start == NULL)
5703 {
5704 if (uc >= 0x80 && utf_char2cells(uc) == 2)
5705 start = p;
5706 }
5707 else if (uc < 0x80 /* optimization shortcut */
5708 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc)))
5709 {
5710 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5711 attr_list, start - s, p - s);
5712 start = NULL;
5713 }
5714 }
5715
5716 if (start != NULL)
5717 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5718 attr_list, start - s, len);
5719}
5720
5721 static int
5722count_cluster_cells(char_u *s, PangoItem *item,
5723 PangoGlyphString* glyphs, int i,
5724 int *cluster_width,
5725 int *last_glyph_rbearing)
5726{
5727 char_u *p;
5728 int next; /* glyph start index of next cluster */
5729 int start, end; /* string segment of current cluster */
5730 int width; /* real cluster width in Pango units */
5731 int uc;
5732 int cellcount = 0;
5733
5734 width = glyphs->glyphs[i].geometry.width;
5735
5736 for (next = i + 1; next < glyphs->num_glyphs; ++next)
5737 {
5738 if (glyphs->glyphs[next].attr.is_cluster_start)
5739 break;
5740 else if (glyphs->glyphs[next].geometry.width > width)
5741 width = glyphs->glyphs[next].geometry.width;
5742 }
5743
5744 start = item->offset + glyphs->log_clusters[i];
5745 end = item->offset + ((next < glyphs->num_glyphs) ?
5746 glyphs->log_clusters[next] : item->length);
5747
5748 for (p = s + start; p < s + end; p += utf_byte2len(*p))
5749 {
5750 uc = utf_ptr2char(p);
5751 if (uc < 0x80)
5752 ++cellcount;
5753 else if (!utf_iscomposing(uc))
5754 cellcount += utf_char2cells(uc);
5755 }
5756
5757 if (last_glyph_rbearing != NULL
5758 && cellcount > 0 && next == glyphs->num_glyphs)
5759 {
5760 PangoRectangle ink_rect;
5761 /*
5762 * If a certain combining mark had to be taken from a non-monospace
5763 * font, we have to compensate manually by adapting x_offset according
5764 * to the ink extents of the previous glyph.
5765 */
5766 pango_font_get_glyph_extents(item->analysis.font,
5767 glyphs->glyphs[i].glyph,
5768 &ink_rect, NULL);
5769
5770 if (PANGO_RBEARING(ink_rect) > 0)
5771 *last_glyph_rbearing = PANGO_RBEARING(ink_rect);
5772 }
5773
5774 if (cellcount > 0)
5775 *cluster_width = width;
5776
5777 return cellcount;
5778}
5779
5780/*
5781 * If there are only combining characters in the cluster, we cannot just
5782 * change the width of the previous glyph since there is none. Therefore
5783 * some guesswork is needed.
5784 *
5785 * If ink_rect.x is negative Pango apparently has taken care of the composing
5786 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
5787 * to problems with composing from different fonts we still need to fine-tune
Bram Moolenaar64404472010-06-26 06:24:45 +02005788 * x_offset to avoid ugliness.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 *
5790 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
5791 * the position of the previous glyph. Apparently this happens only with old
5792 * X fonts which don't provide the special combining information needed by
5793 * Pango.
5794 */
5795 static void
5796setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph,
5797 int last_cellcount, int last_cluster_width,
5798 int last_glyph_rbearing)
5799{
5800 PangoRectangle ink_rect;
5801 PangoRectangle logical_rect;
5802 int width;
5803
5804 width = last_cellcount * gui.char_width * PANGO_SCALE;
5805 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2;
5806 glyph->geometry.width = 0;
5807
5808 pango_font_get_glyph_extents(item->analysis.font,
5809 glyph->glyph,
5810 &ink_rect, &logical_rect);
5811 if (ink_rect.x < 0)
5812 {
5813 glyph->geometry.x_offset += last_glyph_rbearing;
5814 glyph->geometry.y_offset = logical_rect.height
5815 - (gui.char_height - p_linespace) * PANGO_SCALE;
5816 }
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005817 else
5818 /* If the accent width is smaller than the cluster width, position it
5819 * in the middle. */
5820 glyph->geometry.x_offset = -width + MAX(0, width - ink_rect.width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821}
5822
Bram Moolenaar98921892016-02-23 17:14:37 +01005823#if GTK_CHECK_VERSION(3,0,0)
5824 static void
5825draw_glyph_string(int row, int col, int num_cells, int flags,
5826 PangoFont *font, PangoGlyphString *glyphs,
5827 cairo_t *cr)
5828#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 static void
5830draw_glyph_string(int row, int col, int num_cells, int flags,
5831 PangoFont *font, PangoGlyphString *glyphs)
Bram Moolenaar98921892016-02-23 17:14:37 +01005832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833{
5834 if (!(flags & DRAW_TRANSP))
5835 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005836#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02005837 cairo_set_source_rgba(cr,
5838 gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue,
5839 gui.bgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005840 cairo_rectangle(cr,
5841 FILL_X(col), FILL_Y(row),
5842 num_cells * gui.char_width, gui.char_height);
5843 cairo_fill(cr);
5844#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
5846
5847 gdk_draw_rectangle(gui.drawarea->window,
5848 gui.text_gc,
5849 TRUE,
5850 FILL_X(col),
5851 FILL_Y(row),
5852 num_cells * gui.char_width,
5853 gui.char_height);
Bram Moolenaar98921892016-02-23 17:14:37 +01005854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 }
5856
Bram Moolenaar98921892016-02-23 17:14:37 +01005857#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02005858 cairo_set_source_rgba(cr,
5859 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5860 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005861 cairo_move_to(cr, TEXT_X(col), TEXT_Y(row));
5862 pango_cairo_show_glyph_string(cr, font, glyphs);
5863#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5865
5866 gdk_draw_glyphs(gui.drawarea->window,
5867 gui.text_gc,
5868 font,
5869 TEXT_X(col),
5870 TEXT_Y(row),
5871 glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01005872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873
5874 /* redraw the contents with an offset of 1 to emulate bold */
5875 if ((flags & DRAW_BOLD) && !gui.font_can_bold)
Bram Moolenaar98921892016-02-23 17:14:37 +01005876#if GTK_CHECK_VERSION(3,0,0)
5877 {
Bram Moolenaar36edf062016-07-21 22:10:12 +02005878 cairo_set_source_rgba(cr,
5879 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5880 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005881 cairo_move_to(cr, TEXT_X(col) + 1, TEXT_Y(row));
5882 pango_cairo_show_glyph_string(cr, font, glyphs);
5883 }
5884#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 gdk_draw_glyphs(gui.drawarea->window,
5886 gui.text_gc,
5887 font,
5888 TEXT_X(col) + 1,
5889 TEXT_Y(row),
5890 glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01005891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892}
5893
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005894/*
5895 * Draw underline and undercurl at the bottom of the character cell.
5896 */
Bram Moolenaar98921892016-02-23 17:14:37 +01005897#if GTK_CHECK_VERSION(3,0,0)
5898 static void
5899draw_under(int flags, int row, int col, int cells, cairo_t *cr)
5900#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005901 static void
5902draw_under(int flags, int row, int col, int cells)
Bram Moolenaar98921892016-02-23 17:14:37 +01005903#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005904{
5905 int i;
5906 int offset;
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005907 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005908 int y = FILL_Y(row + 1) - 1;
5909
5910 /* Undercurl: draw curl at the bottom of the character cell. */
5911 if (flags & DRAW_UNDERC)
5912 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005913#if GTK_CHECK_VERSION(3,0,0)
5914 cairo_set_line_width(cr, 1.0);
5915 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
Bram Moolenaar36edf062016-07-21 22:10:12 +02005916 cairo_set_source_rgba(cr,
5917 gui.spcolor->red, gui.spcolor->green, gui.spcolor->blue,
5918 gui.spcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005919 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
5920 {
5921 offset = val[i % 8];
5922 cairo_line_to(cr, i, y - offset + 0.5);
5923 }
5924 cairo_stroke(cr);
5925#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005926 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
5927 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
5928 {
5929 offset = val[i % 8];
5930 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset);
5931 }
5932 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar98921892016-02-23 17:14:37 +01005933#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005934 }
5935
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02005936 /* Draw a strikethrough line */
5937 if (flags & DRAW_STRIKE)
5938 {
5939#if GTK_CHECK_VERSION(3,0,0)
5940 cairo_set_line_width(cr, 1.0);
5941 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
5942 cairo_set_source_rgba(cr,
5943 gui.spcolor->red, gui.spcolor->green, gui.spcolor->blue,
5944 gui.spcolor->alpha);
5945 cairo_move_to(cr, FILL_X(col), y + 1 - gui.char_height/2 + 0.5);
5946 cairo_line_to(cr, FILL_X(col + cells), y + 1 - gui.char_height/2 + 0.5);
5947 cairo_stroke(cr);
5948#else
5949 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
5950 gdk_draw_line(gui.drawarea->window, gui.text_gc,
5951 FILL_X(col), y + 1 - gui.char_height/2,
5952 FILL_X(col + cells), y + 1 - gui.char_height/2);
5953 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5954#endif
5955 }
5956
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005957 /* Underline: draw a line at the bottom of the character cell. */
5958 if (flags & DRAW_UNDERL)
5959 {
5960 /* When p_linespace is 0, overwrite the bottom row of pixels.
5961 * Otherwise put the line just below the character. */
5962 if (p_linespace > 1)
5963 y -= p_linespace - 1;
Bram Moolenaar98921892016-02-23 17:14:37 +01005964#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02005965 cairo_set_line_width(cr, 1.0);
5966 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
5967 cairo_set_source_rgba(cr,
5968 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5969 gui.fgcolor->alpha);
5970 cairo_move_to(cr, FILL_X(col), y + 0.5);
5971 cairo_line_to(cr, FILL_X(col + cells), y + 0.5);
5972 cairo_stroke(cr);
Bram Moolenaar98921892016-02-23 17:14:37 +01005973#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005974 gdk_draw_line(gui.drawarea->window, gui.text_gc,
5975 FILL_X(col), y,
5976 FILL_X(col + cells) - 1, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01005977#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005978 }
5979}
5980
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 int
5982gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
5983{
5984 GdkRectangle area; /* area for clip mask */
5985 PangoGlyphString *glyphs; /* glyphs of current item */
5986 int column_offset = 0; /* column offset in cells */
5987 int i;
5988 char_u *conv_buf = NULL; /* result of UTF-8 conversion */
5989 char_u *new_conv_buf;
5990 int convlen;
5991 char_u *sp, *bp;
5992 int plen;
Bram Moolenaar98921892016-02-23 17:14:37 +01005993#if GTK_CHECK_VERSION(3,0,0)
5994 cairo_t *cr;
5995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996
Bram Moolenaar98921892016-02-23 17:14:37 +01005997 if (gui.text_context == NULL || gtk_widget_get_window(gui.drawarea) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 return len;
5999
6000 if (output_conv.vc_type != CONV_NONE)
6001 {
6002 /*
6003 * Convert characters from 'encoding' to 'termencoding', which is set
6004 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
6005 * prohibits changing this to something else than UTF-8 if the GUI is
6006 * in use.
6007 */
6008 convlen = len;
6009 conv_buf = string_convert(&output_conv, s, &convlen);
6010 g_return_val_if_fail(conv_buf != NULL, len);
6011
6012 /* Correct for differences in char width: some chars are
6013 * double-wide in 'encoding' but single-wide in utf-8. Add a space to
6014 * compensate for that. */
6015 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; )
6016 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006017 plen = utf_ptr2len(bp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1)
6019 {
6020 new_conv_buf = alloc(convlen + 2);
6021 if (new_conv_buf == NULL)
6022 return len;
6023 plen += bp - conv_buf;
6024 mch_memmove(new_conv_buf, conv_buf, plen);
6025 new_conv_buf[plen] = ' ';
6026 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen,
6027 convlen - plen + 1);
6028 vim_free(conv_buf);
6029 conv_buf = new_conv_buf;
6030 ++convlen;
6031 bp = conv_buf + plen;
6032 plen = 1;
6033 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006034 sp += (*mb_ptr2len)(sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 bp += plen;
6036 }
6037 s = conv_buf;
6038 len = convlen;
6039 }
6040
6041 /*
6042 * Restrict all drawing to the current screen line in order to prevent
6043 * fuzzy font lookups from messing up the screen.
6044 */
6045 area.x = gui.border_offset;
6046 area.y = FILL_Y(row);
6047 area.width = gui.num_cols * gui.char_width;
6048 area.height = gui.char_height;
6049
Bram Moolenaar98921892016-02-23 17:14:37 +01006050#if GTK_CHECK_VERSION(3,0,0)
6051 cr = cairo_create(gui.surface);
6052 cairo_rectangle(cr, area.x, area.y, area.width, area.height);
6053 cairo_clip(cr);
6054#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 gdk_gc_set_clip_origin(gui.text_gc, 0, 0);
6056 gdk_gc_set_clip_rectangle(gui.text_gc, &area);
Bram Moolenaar98921892016-02-23 17:14:37 +01006057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058
6059 glyphs = pango_glyph_string_new();
6060
6061 /*
6062 * Optimization hack: If possible, skip the itemize and shaping process
6063 * for pure ASCII strings. This optimization is particularly effective
6064 * because Vim draws space characters to clear parts of the screen.
6065 */
6066 if (!(flags & DRAW_ITALIC)
6067 && !((flags & DRAW_BOLD) && gui.font_can_bold)
6068 && gui.ascii_glyphs != NULL)
6069 {
6070 char_u *p;
6071
6072 for (p = s; p < s + len; ++p)
6073 if (*p & 0x80)
6074 goto not_ascii;
6075
6076 pango_glyph_string_set_size(glyphs, len);
6077
6078 for (i = 0; i < len; ++i)
6079 {
Bram Moolenaar16350cb2016-08-14 20:27:34 +02006080 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[2 * s[i]];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 glyphs->log_clusters[i] = i;
6082 }
6083
Bram Moolenaar98921892016-02-23 17:14:37 +01006084#if GTK_CHECK_VERSION(3,0,0)
6085 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs, cr);
6086#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01006088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089
6090 column_offset = len;
6091 }
6092 else
6093not_ascii:
6094 {
6095 PangoAttrList *attr_list;
6096 GList *item_list;
6097 int cluster_width;
6098 int last_glyph_rbearing;
6099 int cells = 0; /* cells occupied by current cluster */
6100
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006101 /* Safety check: pango crashes when invoked with invalid utf-8
6102 * characters. */
6103 if (!utf_valid_string(s, s + len))
6104 {
6105 column_offset = len;
6106 goto skipitall;
6107 }
6108
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 /* original width of the current cluster */
6110 cluster_width = PANGO_SCALE * gui.char_width;
6111
6112 /* right bearing of the last non-composing glyph */
6113 last_glyph_rbearing = PANGO_SCALE * gui.char_width;
6114
6115 attr_list = pango_attr_list_new();
6116
6117 /* If 'guifontwide' is set then use that for double-width characters.
6118 * Otherwise just go with 'guifont' and let Pango do its thing. */
6119 if (gui.wide_font != NULL)
6120 apply_wide_font_attr(s, len, attr_list);
6121
6122 if ((flags & DRAW_BOLD) && gui.font_can_bold)
6123 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD),
6124 attr_list, 0, len);
6125 if (flags & DRAW_ITALIC)
6126 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC),
6127 attr_list, 0, len);
6128 /*
6129 * Break the text into segments with consistent directional level
6130 * and shaping engine. Pure Latin text needs only a single segment,
6131 * so there's no need to worry about the loop's efficiency. Better
6132 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
6133 */
6134 item_list = pango_itemize(gui.text_context,
6135 (const char *)s, 0, len, attr_list, NULL);
6136
6137 while (item_list != NULL)
6138 {
6139 PangoItem *item;
6140 int item_cells = 0; /* item length in cells */
6141
6142 item = (PangoItem *)item_list->data;
6143 item_list = g_list_delete_link(item_list, item_list);
6144 /*
6145 * Increment the bidirectional embedding level by 1 if it is not
6146 * even. An odd number means the output will be RTL, but we don't
6147 * want that since Vim handles right-to-left text on its own. It
6148 * would probably be sufficient to just set level = 0, but you can
6149 * never know :)
6150 *
6151 * Unfortunately we can't take advantage of Pango's ability to
6152 * render both LTR and RTL at the same time. In order to support
6153 * that, Vim's main screen engine would have to make use of Pango
6154 * functionality.
6155 */
6156 item->analysis.level = (item->analysis.level + 1) & (~1U);
6157
6158 /* HACK: Overrule the shape engine, we don't want shaping to be
6159 * done, because drawing the cursor would change the display. */
6160 item->analysis.shape_engine = default_shape_engine;
6161
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02006162#ifdef HAVE_PANGO_SHAPE_FULL
Bram Moolenaar7e2ec002015-09-08 16:31:06 +02006163 pango_shape_full((const char *)s + item->offset, item->length,
6164 (const char *)s, len, &item->analysis, glyphs);
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02006165#else
6166 pango_shape((const char *)s + item->offset, item->length,
6167 &item->analysis, glyphs);
6168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 /*
6170 * Fixed-width hack: iterate over the array and assign a fixed
6171 * width to each glyph, thus overriding the choice made by the
6172 * shaping engine. We use utf_char2cells() to determine the
6173 * number of cells needed.
6174 *
6175 * Also perform all kind of dark magic to get composing
6176 * characters right (and pretty too of course).
6177 */
6178 for (i = 0; i < glyphs->num_glyphs; ++i)
6179 {
6180 PangoGlyphInfo *glyph;
6181
6182 glyph = &glyphs->glyphs[i];
6183
6184 if (glyph->attr.is_cluster_start)
6185 {
6186 int cellcount;
6187
6188 cellcount = count_cluster_cells(
6189 s, item, glyphs, i, &cluster_width,
6190 (item_list != NULL) ? &last_glyph_rbearing : NULL);
6191
6192 if (cellcount > 0)
6193 {
6194 int width;
6195
6196 width = cellcount * gui.char_width * PANGO_SCALE;
6197 glyph->geometry.x_offset +=
6198 MAX(0, width - cluster_width) / 2;
6199 glyph->geometry.width = width;
6200 }
6201 else
6202 {
6203 /* If there are only combining characters in the
6204 * cluster, we cannot just change the width of the
6205 * previous glyph since there is none. Therefore
6206 * some guesswork is needed. */
6207 setup_zero_width_cluster(item, glyph, cells,
6208 cluster_width,
6209 last_glyph_rbearing);
6210 }
6211
6212 item_cells += cellcount;
6213 cells = cellcount;
6214 }
6215 else if (i > 0)
6216 {
6217 int width;
6218
6219 /* There is a previous glyph, so we deal with combining
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006220 * characters the canonical way.
Bram Moolenaar96118f32010-08-08 14:40:37 +02006221 * In some circumstances Pango uses a positive x_offset,
6222 * then use the width of the previous glyph for this one
6223 * and set the previous width to zero.
6224 * Otherwise we get a negative x_offset, Pango has already
6225 * positioned the combining char, keep the widths as they
6226 * are.
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006227 * For both adjust the x_offset to position the glyph in
Bram Moolenaar96118f32010-08-08 14:40:37 +02006228 * the middle. */
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006229 if (glyph->geometry.x_offset >= 0)
Bram Moolenaar96118f32010-08-08 14:40:37 +02006230 {
6231 glyphs->glyphs[i].geometry.width =
6232 glyphs->glyphs[i - 1].geometry.width;
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006233 glyphs->glyphs[i - 1].geometry.width = 0;
Bram Moolenaar96118f32010-08-08 14:40:37 +02006234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 width = cells * gui.char_width * PANGO_SCALE;
6236 glyph->geometry.x_offset +=
6237 MAX(0, width - cluster_width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 }
6239 else /* i == 0 "cannot happen" */
6240 {
6241 glyph->geometry.width = 0;
6242 }
6243 }
6244
6245 /*** Aaaaand action! ***/
Bram Moolenaar98921892016-02-23 17:14:37 +01006246#if GTK_CHECK_VERSION(3,0,0)
6247 draw_glyph_string(row, col + column_offset, item_cells,
6248 flags, item->analysis.font, glyphs,
6249 cr);
6250#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 draw_glyph_string(row, col + column_offset, item_cells,
6252 flags, item->analysis.font, glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01006253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254
6255 pango_item_free(item);
6256
6257 column_offset += item_cells;
6258 }
6259
6260 pango_attr_list_unref(attr_list);
6261 }
6262
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006263skipitall:
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006264 /* Draw underline and undercurl. */
Bram Moolenaar98921892016-02-23 17:14:37 +01006265#if GTK_CHECK_VERSION(3,0,0)
6266 draw_under(flags, row, col, column_offset, cr);
6267#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006268 draw_under(flags, row, col, column_offset);
Bram Moolenaar98921892016-02-23 17:14:37 +01006269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270
6271 pango_glyph_string_free(glyphs);
6272 vim_free(conv_buf);
6273
Bram Moolenaar98921892016-02-23 17:14:37 +01006274#if GTK_CHECK_VERSION(3,0,0)
6275 cairo_destroy(cr);
6276 if (!gui.by_signal)
6277 gdk_window_invalidate_rect(gtk_widget_get_window(gui.drawarea),
6278 &area, FALSE);
6279#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 gdk_gc_set_clip_rectangle(gui.text_gc, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01006281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282
6283 return column_offset;
6284}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285
6286/*
6287 * Return OK if the key with the termcap name "name" is supported.
6288 */
6289 int
6290gui_mch_haskey(char_u *name)
6291{
6292 int i;
6293
6294 for (i = 0; special_keys[i].key_sym != 0; i++)
6295 if (name[0] == special_keys[i].code0
6296 && name[1] == special_keys[i].code1)
6297 return OK;
6298 return FAIL;
6299}
6300
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006301#if defined(FEAT_TITLE) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302/*
6303 * Return the text window-id and display. Only required for X-based GUI's
6304 */
6305 int
6306gui_get_x11_windis(Window *win, Display **dis)
6307{
Bram Moolenaar98921892016-02-23 17:14:37 +01006308 if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 {
Bram Moolenaar98921892016-02-23 17:14:37 +01006310 *dis = GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin));
6311 *win = GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 return OK;
6313 }
6314
6315 *dis = NULL;
6316 *win = 0;
6317 return FAIL;
6318}
6319#endif
6320
6321#if defined(FEAT_CLIENTSERVER) \
6322 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
6323
6324 Display *
6325gui_mch_get_display(void)
6326{
Bram Moolenaar98921892016-02-23 17:14:37 +01006327 if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL)
6328 return GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 else
6330 return NULL;
6331}
6332#endif
6333
6334 void
6335gui_mch_beep(void)
6336{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 GdkDisplay *display;
6338
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02006339#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01006340 if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin))
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02006341#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02006343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 display = gtk_widget_get_display(gui.mainwin);
6345 else
6346 display = gdk_display_get_default();
6347
6348 if (display != NULL)
6349 gdk_display_beep(display);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350}
6351
6352 void
6353gui_mch_flash(int msec)
6354{
Bram Moolenaar98921892016-02-23 17:14:37 +01006355#if GTK_CHECK_VERSION(3,0,0)
6356 /* TODO Replace GdkGC with Cairo */
6357 (void)msec;
6358#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 GdkGCValues values;
6360 GdkGC *invert_gc;
6361
6362 if (gui.drawarea->window == NULL)
6363 return;
6364
6365 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6366 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
6367 values.function = GDK_XOR;
6368 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6369 &values,
6370 GDK_GC_FOREGROUND |
6371 GDK_GC_BACKGROUND |
6372 GDK_GC_FUNCTION);
6373 gdk_gc_set_exposures(invert_gc,
6374 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
6375 /*
6376 * Do a visual beep by changing back and forth in some undetermined way,
6377 * the foreground and background colors. This is due to the fact that
6378 * there can't be really any prediction about the effects of XOR on
6379 * arbitrary X11 servers. However this seems to be enough for what we
6380 * intend it to do.
6381 */
6382 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6383 TRUE,
6384 0, 0,
6385 FILL_X((int)Columns) + gui.border_offset,
6386 FILL_Y((int)Rows) + gui.border_offset);
6387
6388 gui_mch_flush();
6389 ui_delay((long)msec, TRUE); /* wait so many msec */
6390
6391 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6392 TRUE,
6393 0, 0,
6394 FILL_X((int)Columns) + gui.border_offset,
6395 FILL_Y((int)Rows) + gui.border_offset);
6396
6397 gdk_gc_destroy(invert_gc);
Bram Moolenaar98921892016-02-23 17:14:37 +01006398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399}
6400
6401/*
6402 * Invert a rectangle from row r, column c, for nr rows and nc columns.
6403 */
6404 void
6405gui_mch_invert_rectangle(int r, int c, int nr, int nc)
6406{
Bram Moolenaar98921892016-02-23 17:14:37 +01006407#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar4fc563b2016-03-12 12:40:58 +01006408 const GdkRectangle rect = {
6409 FILL_X(c), FILL_Y(r), nc * gui.char_width, nr * gui.char_height
6410 };
6411 cairo_t * const cr = cairo_create(gui.surface);
6412
Bram Moolenaar36edf062016-07-21 22:10:12 +02006413 set_cairo_source_rgba_from_color(cr, gui.norm_pixel ^ gui.back_pixel);
Bram Moolenaar4fc563b2016-03-12 12:40:58 +01006414# if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,9,2)
6415 cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE);
6416# else
6417 /* Give an implementation for older cairo versions if necessary. */
6418# endif
6419 gdk_cairo_rectangle(cr, &rect);
6420 cairo_fill(cr);
6421
6422 cairo_destroy(cr);
6423
6424 if (!gui.by_signal)
6425 gtk_widget_queue_draw_area(gui.drawarea, rect.x, rect.y,
6426 rect.width, rect.height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006427#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 GdkGCValues values;
6429 GdkGC *invert_gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430
6431 if (gui.drawarea->window == NULL)
6432 return;
6433
Bram Moolenaar89d40322006-08-29 15:30:07 +00006434 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6435 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 values.function = GDK_XOR;
6437 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6438 &values,
6439 GDK_GC_FOREGROUND |
6440 GDK_GC_BACKGROUND |
6441 GDK_GC_FUNCTION);
Bram Moolenaar89d40322006-08-29 15:30:07 +00006442 gdk_gc_set_exposures(invert_gc, gui.visibility !=
6443 GDK_VISIBILITY_UNOBSCURED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6445 TRUE,
6446 FILL_X(c), FILL_Y(r),
6447 (nc) * gui.char_width, (nr) * gui.char_height);
6448 gdk_gc_destroy(invert_gc);
Bram Moolenaar98921892016-02-23 17:14:37 +01006449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450}
6451
6452/*
6453 * Iconify the GUI window.
6454 */
6455 void
6456gui_mch_iconify(void)
6457{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 gtk_window_iconify(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459}
6460
6461#if defined(FEAT_EVAL) || defined(PROTO)
6462/*
6463 * Bring the Vim window to the foreground.
6464 */
6465 void
6466gui_mch_set_foreground(void)
6467{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 gtk_window_present(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469}
6470#endif
6471
6472/*
6473 * Draw a cursor without focus.
6474 */
6475 void
6476gui_mch_draw_hollow_cursor(guicolor_T color)
6477{
6478 int i = 1;
Bram Moolenaar98921892016-02-23 17:14:37 +01006479#if GTK_CHECK_VERSION(3,0,0)
6480 cairo_t *cr;
6481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482
Bram Moolenaar98921892016-02-23 17:14:37 +01006483 if (gtk_widget_get_window(gui.drawarea) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 return;
6485
Bram Moolenaar98921892016-02-23 17:14:37 +01006486#if GTK_CHECK_VERSION(3,0,0)
6487 cr = cairo_create(gui.surface);
6488#endif
6489
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 gui_mch_set_fg_color(color);
6491
Bram Moolenaar98921892016-02-23 17:14:37 +01006492#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02006493 cairo_set_source_rgba(cr,
6494 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
6495 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01006496#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar98921892016-02-23 17:14:37 +01006498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 if (mb_lefthalve(gui.row, gui.col))
6500 i = 2;
Bram Moolenaar98921892016-02-23 17:14:37 +01006501#if GTK_CHECK_VERSION(3,0,0)
6502 cairo_set_line_width(cr, 1.0);
6503 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
6504 cairo_rectangle(cr,
6505 FILL_X(gui.col) + 0.5, FILL_Y(gui.row) + 0.5,
6506 i * gui.char_width - 1, gui.char_height - 1);
6507 cairo_stroke(cr);
6508 cairo_destroy(cr);
6509#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6511 FALSE,
6512 FILL_X(gui.col), FILL_Y(gui.row),
6513 i * gui.char_width - 1, gui.char_height - 1);
Bram Moolenaar98921892016-02-23 17:14:37 +01006514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515}
6516
6517/*
6518 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
6519 * color "color".
6520 */
6521 void
6522gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
6523{
Bram Moolenaar98921892016-02-23 17:14:37 +01006524 if (gtk_widget_get_window(gui.drawarea) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 return;
6526
6527 gui_mch_set_fg_color(color);
6528
Bram Moolenaar98921892016-02-23 17:14:37 +01006529#if GTK_CHECK_VERSION(3,0,0)
6530 {
6531 cairo_t *cr;
6532
6533 cr = cairo_create(gui.surface);
Bram Moolenaar36edf062016-07-21 22:10:12 +02006534 cairo_set_source_rgba(cr,
6535 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
6536 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01006537 cairo_rectangle(cr,
6538# ifdef FEAT_RIGHTLEFT
6539 /* vertical line should be on the right of current point */
6540 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
6541# endif
6542 FILL_X(gui.col), FILL_Y(gui.row) + gui.char_height - h,
6543 w, h);
6544 cairo_fill(cr);
6545 cairo_destroy(cr);
6546 }
6547#else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6549 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6550 TRUE,
Bram Moolenaar98921892016-02-23 17:14:37 +01006551# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 /* vertical line should be on the right of current point */
6553 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
Bram Moolenaar98921892016-02-23 17:14:37 +01006554# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 FILL_X(gui.col),
6556 FILL_Y(gui.row) + gui.char_height - h,
6557 w, h);
Bram Moolenaar98921892016-02-23 17:14:37 +01006558#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559}
6560
6561
6562/*
6563 * Catch up with any queued X11 events. This may put keyboard input into the
6564 * input buffer, call resize call-backs, trigger timers etc. If there is
6565 * nothing in the X11 event queue (& no timers pending), then we return
6566 * immediately.
6567 */
6568 void
6569gui_mch_update(void)
6570{
Bram Moolenaara3f41662010-07-11 19:01:06 +02006571 while (g_main_context_pending(NULL) && !vim_is_input_buf_full())
6572 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573}
6574
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006575 static timeout_cb_type
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576input_timer_cb(gpointer data)
6577{
6578 int *timed_out = (int *) data;
6579
Bram Moolenaar49325942007-05-10 19:19:59 +00006580 /* Just inform the caller about the occurrence of it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 *timed_out = TRUE;
6582
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 return FALSE; /* don't happen again */
6584}
6585
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006586#ifdef FEAT_JOB_CHANNEL
6587 static timeout_cb_type
6588channel_poll_cb(gpointer data UNUSED)
6589{
6590 /* Using an event handler for a channel that may be disconnected does
6591 * not work, it hangs. Instead poll for messages. */
6592 channel_handle_events(TRUE);
6593 parse_queued_messages();
6594
6595 return TRUE; /* repeat */
6596}
6597#endif
6598
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599/*
6600 * GUI input routine called by gui_wait_for_chars(). Waits for a character
6601 * from the keyboard.
6602 * wtime == -1 Wait forever.
6603 * wtime == 0 This should never happen.
6604 * wtime > 0 Wait wtime milliseconds for a character.
6605 * Returns OK if a character was found to be available within the given time,
6606 * or FAIL otherwise.
6607 */
6608 int
6609gui_mch_wait_for_chars(long wtime)
6610{
Bram Moolenaar4231da42016-06-02 14:30:04 +02006611 int focus;
6612 guint timer;
6613 static int timed_out;
6614 int retval = FAIL;
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006615#ifdef FEAT_JOB_CHANNEL
6616 guint channel_timer = 0;
6617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618
6619 timed_out = FALSE;
6620
6621 /* this timeout makes sure that we will return if no characters arrived in
6622 * time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 if (wtime > 0)
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006624 timer = timeout_add(wtime, input_timer_cb, &timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 else
6626 timer = 0;
6627
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006628#ifdef FEAT_JOB_CHANNEL
6629 /* If there is a channel with the keep_open flag we need to poll for input
6630 * on them. */
6631 if (channel_any_keep_open())
6632 channel_timer = timeout_add(20, channel_poll_cb, NULL);
6633#endif
6634
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 focus = gui.in_focus;
6636
6637 do
6638 {
6639 /* Stop or start blinking when focus changes */
6640 if (gui.in_focus != focus)
6641 {
6642 if (gui.in_focus)
6643 gui_mch_start_blink();
6644 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01006645 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 focus = gui.in_focus;
6647 }
6648
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006649#ifdef MESSAGE_QUEUE
Bram Moolenaar4231da42016-06-02 14:30:04 +02006650# ifdef FEAT_TIMERS
6651 did_add_timer = FALSE;
6652# endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006653 parse_queued_messages();
Bram Moolenaar4231da42016-06-02 14:30:04 +02006654# ifdef FEAT_TIMERS
6655 if (did_add_timer)
6656 /* Need to recompute the waiting time. */
6657 goto theend;
6658# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006659#endif
6660
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 /*
6662 * Loop in GTK+ processing until a timeout or input occurs.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006663 * Skip this if input is available anyway (can happen in rare
6664 * situations, sort of race condition).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006666 if (!input_available())
Bram Moolenaara3f41662010-07-11 19:01:06 +02006667 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668
6669 /* Got char, return immediately */
6670 if (input_available())
6671 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02006672 retval = OK;
6673 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 }
6675 } while (wtime < 0 || !timed_out);
6676
6677 /*
6678 * Flush all eventually pending (drawing) events.
6679 */
6680 gui_mch_update();
6681
Bram Moolenaar4231da42016-06-02 14:30:04 +02006682theend:
6683 if (timer != 0 && !timed_out)
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006684 timeout_remove(timer);
6685#ifdef FEAT_JOB_CHANNEL
6686 if (channel_timer != 0)
6687 timeout_remove(channel_timer);
Bram Moolenaar4231da42016-06-02 14:30:04 +02006688#endif
6689
6690 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691}
6692
6693
6694/****************************************************************************
6695 * Output drawing routines.
6696 ****************************************************************************/
6697
6698
6699/* Flush any output to the screen */
6700 void
6701gui_mch_flush(void)
6702{
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02006703#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01006704 if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705#else
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02006706 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707#endif
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02006708 gdk_display_flush(gtk_widget_get_display(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709}
6710
6711/*
6712 * Clear a rectangular region of the screen from text pos (row1, col1) to
6713 * (row2, col2) inclusive.
6714 */
6715 void
Bram Moolenaarfe344a92017-02-23 12:20:35 +01006716gui_mch_clear_block(int row1arg, int col1arg, int row2arg, int col2arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717{
Bram Moolenaarfe344a92017-02-23 12:20:35 +01006718
6719 int col1 = check_col(col1arg);
6720 int col2 = check_col(col2arg);
6721 int row1 = check_row(row1arg);
6722 int row2 = check_row(row2arg);
6723
Bram Moolenaar98921892016-02-23 17:14:37 +01006724#if GTK_CHECK_VERSION(3,0,0)
6725 if (gtk_widget_get_window(gui.drawarea) == NULL)
6726 return;
6727#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 GdkColor color;
6729
6730 if (gui.drawarea->window == NULL)
6731 return;
6732
6733 color.pixel = gui.back_pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01006734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735
Bram Moolenaar98921892016-02-23 17:14:37 +01006736#if GTK_CHECK_VERSION(3,0,0)
6737 {
6738 /* Add one pixel to the far right column in case a double-stroked
6739 * bold glyph may sit there. */
6740 const GdkRectangle rect = {
6741 FILL_X(col1), FILL_Y(row1),
6742 (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1),
6743 (row2 - row1 + 1) * gui.char_height
6744 };
6745 GdkWindow * const win = gtk_widget_get_window(gui.drawarea);
6746 cairo_t * const cr = cairo_create(gui.surface);
Bram Moolenaara859f042016-11-17 19:11:55 +01006747# if GTK_CHECK_VERSION(3,22,2)
6748 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
6749# else
Bram Moolenaar98921892016-02-23 17:14:37 +01006750 cairo_pattern_t * const pat = gdk_window_get_background_pattern(win);
6751 if (pat != NULL)
6752 cairo_set_source(cr, pat);
6753 else
Bram Moolenaar36edf062016-07-21 22:10:12 +02006754 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
Bram Moolenaara859f042016-11-17 19:11:55 +01006755# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01006756 gdk_cairo_rectangle(cr, &rect);
6757 cairo_fill(cr);
6758 cairo_destroy(cr);
6759
6760 if (!gui.by_signal)
6761 gdk_window_invalidate_rect(win, &rect, FALSE);
6762 }
6763#else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 gdk_gc_set_foreground(gui.text_gc, &color);
6765
6766 /* Clear one extra pixel at the far right, for when bold characters have
6767 * spilled over to the window border. */
6768 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE,
6769 FILL_X(col1), FILL_Y(row1),
6770 (col2 - col1 + 1) * gui.char_width
6771 + (col2 == Columns - 1),
6772 (row2 - row1 + 1) * gui.char_height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006773#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774}
6775
Bram Moolenaar98921892016-02-23 17:14:37 +01006776#if GTK_CHECK_VERSION(3,0,0)
6777 static void
6778gui_gtk_window_clear(GdkWindow *win)
6779{
6780 const GdkRectangle rect = {
6781 0, 0, gdk_window_get_width(win), gdk_window_get_height(win)
6782 };
6783 cairo_t * const cr = cairo_create(gui.surface);
Bram Moolenaara859f042016-11-17 19:11:55 +01006784# if GTK_CHECK_VERSION(3,22,2)
6785 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
6786# else
Bram Moolenaar98921892016-02-23 17:14:37 +01006787 cairo_pattern_t * const pat = gdk_window_get_background_pattern(win);
6788 if (pat != NULL)
6789 cairo_set_source(cr, pat);
6790 else
Bram Moolenaar36edf062016-07-21 22:10:12 +02006791 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
Bram Moolenaara859f042016-11-17 19:11:55 +01006792# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01006793 gdk_cairo_rectangle(cr, &rect);
6794 cairo_fill(cr);
6795 cairo_destroy(cr);
6796
6797 if (!gui.by_signal)
6798 gdk_window_invalidate_rect(win, &rect, FALSE);
6799}
Bram Moolenaar25328e32018-09-11 21:30:09 +02006800#else
6801# define gui_gtk_window_clear(win) gdk_window_clear(win)
Bram Moolenaar98921892016-02-23 17:14:37 +01006802#endif
6803
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 void
6805gui_mch_clear_all(void)
6806{
Bram Moolenaar98921892016-02-23 17:14:37 +01006807 if (gtk_widget_get_window(gui.drawarea) != NULL)
6808 gui_gtk_window_clear(gtk_widget_get_window(gui.drawarea));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809}
6810
Bram Moolenaar98921892016-02-23 17:14:37 +01006811#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812/*
6813 * Redraw any text revealed by scrolling up/down.
6814 */
6815 static void
6816check_copy_area(void)
6817{
6818 GdkEvent *event;
6819 int expose_count;
6820
6821 if (gui.visibility != GDK_VISIBILITY_PARTIAL)
6822 return;
6823
6824 /* Avoid redrawing the cursor while scrolling or it'll end up where
6825 * we don't want it to be. I'm not sure if it's correct to call
6826 * gui_dont_update_cursor() at this point but it works as a quick
6827 * fix for now. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02006828 gui_dont_update_cursor(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829
6830 do
6831 {
6832 /* Wait to check whether the scroll worked or not. */
6833 event = gdk_event_get_graphics_expose(gui.drawarea->window);
6834
6835 if (event == NULL)
6836 break; /* received NoExpose event */
6837
6838 gui_redraw(event->expose.area.x, event->expose.area.y,
6839 event->expose.area.width, event->expose.area.height);
6840
6841 expose_count = event->expose.count;
6842 gdk_event_free(event);
6843 }
6844 while (expose_count > 0); /* more events follow */
6845
6846 gui_can_update_cursor();
6847}
Bram Moolenaar98921892016-02-23 17:14:37 +01006848#endif /* !GTK_CHECK_VERSION(3,0,0) */
6849
6850#if GTK_CHECK_VERSION(3,0,0)
6851 static void
6852gui_gtk_surface_copy_rect(int dest_x, int dest_y,
6853 int src_x, int src_y,
6854 int width, int height)
6855{
6856 cairo_t * const cr = cairo_create(gui.surface);
6857
6858 cairo_rectangle(cr, dest_x, dest_y, width, height);
6859 cairo_clip(cr);
6860 cairo_push_group(cr);
6861 cairo_set_source_surface(cr, gui.surface, dest_x - src_x, dest_y - src_y);
6862 cairo_paint(cr);
6863 cairo_pop_group_to_source(cr);
6864 cairo_paint(cr);
6865
6866 cairo_destroy(cr);
6867}
6868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869
6870/*
6871 * Delete the given number of lines from the given row, scrolling up any
6872 * text further down within the scroll region.
6873 */
6874 void
6875gui_mch_delete_lines(int row, int num_lines)
6876{
Bram Moolenaar98921892016-02-23 17:14:37 +01006877#if GTK_CHECK_VERSION(3,0,0)
6878 const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1;
6879 const int nrows = gui.scroll_region_bot - row + 1;
6880 const int src_nrows = nrows - num_lines;
6881
6882 gui_gtk_surface_copy_rect(
6883 FILL_X(gui.scroll_region_left), FILL_Y(row),
6884 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6885 gui.char_width * ncols + 1, gui.char_height * src_nrows);
6886 gui_clear_block(
6887 gui.scroll_region_bot - num_lines + 1, gui.scroll_region_left,
6888 gui.scroll_region_bot, gui.scroll_region_right);
6889 gui_gtk3_redraw(
6890 FILL_X(gui.scroll_region_left), FILL_Y(row),
6891 gui.char_width * ncols + 1, gui.char_height * nrows);
6892 if (!gui.by_signal)
6893 gtk_widget_queue_draw_area(gui.drawarea,
6894 FILL_X(gui.scroll_region_left), FILL_Y(row),
6895 gui.char_width * ncols + 1, gui.char_height * nrows);
6896#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
6898 return; /* Can't see the window */
6899
6900 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6901 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6902
6903 /* copy one extra pixel, for when bold has spilled over */
6904 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6905 FILL_X(gui.scroll_region_left), FILL_Y(row),
6906 gui.drawarea->window,
6907 FILL_X(gui.scroll_region_left),
6908 FILL_Y(row + num_lines),
6909 gui.char_width * (gui.scroll_region_right
6910 - gui.scroll_region_left + 1) + 1,
6911 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6912
6913 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
6914 gui.scroll_region_left,
6915 gui.scroll_region_bot, gui.scroll_region_right);
6916 check_copy_area();
Bram Moolenaar98921892016-02-23 17:14:37 +01006917#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918}
6919
6920/*
6921 * Insert the given number of lines before the given row, scrolling down any
6922 * following text within the scroll region.
6923 */
6924 void
6925gui_mch_insert_lines(int row, int num_lines)
6926{
Bram Moolenaar98921892016-02-23 17:14:37 +01006927#if GTK_CHECK_VERSION(3,0,0)
6928 const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1;
6929 const int nrows = gui.scroll_region_bot - row + 1;
6930 const int src_nrows = nrows - num_lines;
6931
6932 gui_gtk_surface_copy_rect(
6933 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6934 FILL_X(gui.scroll_region_left), FILL_Y(row),
6935 gui.char_width * ncols + 1, gui.char_height * src_nrows);
6936 gui_mch_clear_block(
6937 row, gui.scroll_region_left,
6938 row + num_lines - 1, gui.scroll_region_right);
6939 gui_gtk3_redraw(
6940 FILL_X(gui.scroll_region_left), FILL_Y(row),
6941 gui.char_width * ncols + 1, gui.char_height * nrows);
6942 if (!gui.by_signal)
6943 gtk_widget_queue_draw_area(gui.drawarea,
6944 FILL_X(gui.scroll_region_left), FILL_Y(row),
6945 gui.char_width * ncols + 1, gui.char_height * nrows);
6946#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
6948 return; /* Can't see the window */
6949
6950 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6951 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6952
6953 /* copy one extra pixel, for when bold has spilled over */
6954 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6955 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6956 gui.drawarea->window,
6957 FILL_X(gui.scroll_region_left), FILL_Y(row),
6958 gui.char_width * (gui.scroll_region_right
6959 - gui.scroll_region_left + 1) + 1,
6960 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6961
6962 gui_clear_block(row, gui.scroll_region_left,
6963 row + num_lines - 1, gui.scroll_region_right);
6964 check_copy_area();
Bram Moolenaar98921892016-02-23 17:14:37 +01006965#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966}
6967
6968/*
6969 * X Selection stuff, for cutting and pasting text to other windows.
6970 */
6971 void
6972clip_mch_request_selection(VimClipboard *cbd)
6973{
6974 GdkAtom target;
6975 unsigned i;
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00006976 time_t start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977
6978 for (i = 0; i < N_SELECTION_TARGETS; ++i)
6979 {
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006980 if (!clip_html && selection_targets[i].info == TARGET_HTML)
6981 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 received_selection = RS_NONE;
6983 target = gdk_atom_intern(selection_targets[i].target, FALSE);
6984
6985 gtk_selection_convert(gui.drawarea,
6986 cbd->gtk_sel_atom, target,
6987 (guint32)GDK_CURRENT_TIME);
6988
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00006989 /* Hack: Wait up to three seconds for the selection. A hang was
6990 * noticed here when using the netrw plugin combined with ":gui"
6991 * during the FocusGained event. */
6992 start = time(NULL);
6993 while (received_selection == RS_NONE && time(NULL) < start + 3)
Bram Moolenaara3f41662010-07-11 19:01:06 +02006994 g_main_context_iteration(NULL, TRUE); /* wait for selection_received_cb */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995
6996 if (received_selection != RS_FAIL)
6997 return;
6998 }
6999
7000 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaar98921892016-02-23 17:14:37 +01007001 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)),
7002 cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003}
7004
7005/*
7006 * Disown the selection.
7007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00007009clip_mch_lose_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010{
Bram Moolenaar29dfa5a2018-03-20 21:24:45 +01007011 if (!in_selection_clear_event)
7012 {
7013 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, gui.event_time);
7014 gui_mch_update();
7015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016}
7017
7018/*
7019 * Own the selection and return OK if it worked.
7020 */
7021 int
7022clip_mch_own_selection(VimClipboard *cbd)
7023{
7024 int success;
7025
7026 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom,
Bram Moolenaar20892c12011-06-26 04:49:00 +02007027 gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 gui_mch_update();
7029 return (success) ? OK : FAIL;
7030}
7031
7032/*
7033 * Send the current selection to the clipboard. Do nothing for X because we
7034 * will fill in the selection only when requested by another app.
7035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00007037clip_mch_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038{
7039}
7040
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01007041 int
7042clip_gtk_owner_exists(VimClipboard *cbd)
7043{
7044 return gdk_selection_owner_get(cbd->gtk_sel_atom) != NULL;
7045}
7046
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047
7048#if defined(FEAT_MENU) || defined(PROTO)
7049/*
7050 * Make a menu item appear either active or not active (grey or not grey).
7051 */
7052 void
7053gui_mch_menu_grey(vimmenu_T *menu, int grey)
7054{
7055 if (menu->id == NULL)
7056 return;
7057
7058 if (menu_is_separator(menu->name))
7059 grey = TRUE;
7060
7061 gui_mch_menu_hidden(menu, FALSE);
7062 /* Be clever about bitfields versus true booleans here! */
Bram Moolenaar98921892016-02-23 17:14:37 +01007063# if GTK_CHECK_VERSION(3,0,0)
7064 if (!gtk_widget_get_sensitive(menu->id) == !grey)
7065# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 if (!GTK_WIDGET_SENSITIVE(menu->id) == !grey)
Bram Moolenaar98921892016-02-23 17:14:37 +01007067# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 {
7069 gtk_widget_set_sensitive(menu->id, !grey);
7070 gui_mch_update();
7071 }
7072}
7073
7074/*
7075 * Make menu item hidden or not hidden.
7076 */
7077 void
7078gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
7079{
7080 if (menu->id == 0)
7081 return;
7082
7083 if (hidden)
7084 {
Bram Moolenaar98921892016-02-23 17:14:37 +01007085# if GTK_CHECK_VERSION(3,0,0)
7086 if (gtk_widget_get_visible(menu->id))
7087# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 if (GTK_WIDGET_VISIBLE(menu->id))
Bram Moolenaar98921892016-02-23 17:14:37 +01007089# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 {
7091 gtk_widget_hide(menu->id);
7092 gui_mch_update();
7093 }
7094 }
7095 else
7096 {
Bram Moolenaar98921892016-02-23 17:14:37 +01007097# if GTK_CHECK_VERSION(3,0,0)
7098 if (!gtk_widget_get_visible(menu->id))
7099# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 if (!GTK_WIDGET_VISIBLE(menu->id))
Bram Moolenaar98921892016-02-23 17:14:37 +01007101# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 {
7103 gtk_widget_show(menu->id);
7104 gui_mch_update();
7105 }
7106 }
7107}
7108
7109/*
7110 * This is called after setting all the menus to grey/hidden or not.
7111 */
7112 void
7113gui_mch_draw_menubar(void)
7114{
7115 /* just make sure that the visual changes get effect immediately */
7116 gui_mch_update();
7117}
7118#endif /* FEAT_MENU */
7119
7120/*
7121 * Scrollbar stuff.
7122 */
7123 void
7124gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
7125{
7126 if (sb->id == NULL)
7127 return;
7128
Bram Moolenaar98921892016-02-23 17:14:37 +01007129#if GTK_CHECK_VERSION(3,0,0)
7130 gtk_widget_set_visible(sb->id, flag);
7131#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 if (flag)
7133 gtk_widget_show(sb->id);
7134 else
7135 gtk_widget_hide(sb->id);
Bram Moolenaar98921892016-02-23 17:14:37 +01007136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00007138 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139}
7140
7141
7142/*
7143 * Return the RGB value of a pixel as long.
7144 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007145 guicolor_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146gui_mch_get_rgb(guicolor_T pixel)
7147{
Bram Moolenaar98921892016-02-23 17:14:37 +01007148#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02007149 return (long_u)pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01007150#else
Bram Moolenaar36edf062016-07-21 22:10:12 +02007151 GdkColor color;
7152
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
7154 (unsigned long)pixel, &color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007156 return (guicolor_T)(
7157 (((unsigned)color.red & 0xff00) << 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 | ((unsigned)color.green & 0xff00)
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007159 | (((unsigned)color.blue & 0xff00) >> 8));
Bram Moolenaar36edf062016-07-21 22:10:12 +02007160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161}
7162
7163/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007164 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007166 void
7167gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168{
Bram Moolenaar98921892016-02-23 17:14:37 +01007169#if GTK_CHECK_VERSION(3,0,0)
7170 gui_gtk_get_pointer(gui.drawarea, x, y, NULL);
7171#else
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007172 gdk_window_get_pointer(gui.drawarea->window, x, y, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01007173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174}
7175
7176 void
7177gui_mch_setmouse(int x, int y)
7178{
7179 /* Sorry for the Xlib call, but we can't avoid it, since there is no
7180 * internal GDK mechanism present to accomplish this. (and for good
7181 * reason...) */
Bram Moolenaar98921892016-02-23 17:14:37 +01007182 XWarpPointer(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.drawarea)),
7183 (Window)0, GDK_WINDOW_XID(gtk_widget_get_window(gui.drawarea)),
7184 0, 0, 0U, 0U, x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185}
7186
7187
7188#ifdef FEAT_MOUSESHAPE
7189/* The last set mouse pointer shape is remembered, to be used when it goes
7190 * from hidden to not hidden. */
7191static int last_shape = 0;
7192#endif
7193
7194/*
7195 * Use the blank mouse pointer or not.
7196 *
7197 * hide: TRUE = use blank ptr, FALSE = use parent ptr
7198 */
7199 void
7200gui_mch_mousehide(int hide)
7201{
7202 if (gui.pointer_hidden != hide)
7203 {
7204 gui.pointer_hidden = hide;
Bram Moolenaar98921892016-02-23 17:14:37 +01007205 if (gtk_widget_get_window(gui.drawarea) && gui.blank_pointer != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 {
7207 if (hide)
Bram Moolenaar98921892016-02-23 17:14:37 +01007208 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
7209 gui.blank_pointer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 else
7211#ifdef FEAT_MOUSESHAPE
7212 mch_set_mouse_shape(last_shape);
7213#else
Bram Moolenaar25328e32018-09-11 21:30:09 +02007214 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215#endif
7216 }
7217 }
7218}
7219
7220#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
7221
7222/* Table for shape IDs. Keep in sync with the mshape_names[] table in
7223 * misc2.c! */
7224static const int mshape_ids[] =
7225{
7226 GDK_LEFT_PTR, /* arrow */
7227 GDK_CURSOR_IS_PIXMAP, /* blank */
7228 GDK_XTERM, /* beam */
7229 GDK_SB_V_DOUBLE_ARROW, /* updown */
7230 GDK_SIZING, /* udsizing */
7231 GDK_SB_H_DOUBLE_ARROW, /* leftright */
7232 GDK_SIZING, /* lrsizing */
7233 GDK_WATCH, /* busy */
7234 GDK_X_CURSOR, /* no */
7235 GDK_CROSSHAIR, /* crosshair */
7236 GDK_HAND1, /* hand1 */
7237 GDK_HAND2, /* hand2 */
7238 GDK_PENCIL, /* pencil */
7239 GDK_QUESTION_ARROW, /* question */
7240 GDK_RIGHT_PTR, /* right-arrow */
7241 GDK_CENTER_PTR, /* up-arrow */
7242 GDK_LEFT_PTR /* last one */
7243};
7244
7245 void
7246mch_set_mouse_shape(int shape)
7247{
7248 int id;
7249 GdkCursor *c;
7250
Bram Moolenaar98921892016-02-23 17:14:37 +01007251 if (gtk_widget_get_window(gui.drawarea) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 return;
7253
7254 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
Bram Moolenaar98921892016-02-23 17:14:37 +01007255 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
7256 gui.blank_pointer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 else
7258 {
7259 if (shape >= MSHAPE_NUMBERED)
7260 {
7261 id = shape - MSHAPE_NUMBERED;
7262 if (id >= GDK_LAST_CURSOR)
7263 id = GDK_LEFT_PTR;
7264 else
7265 id &= ~1; /* they are always even (why?) */
7266 }
Bram Moolenaarb85cb212009-05-17 14:24:23 +00007267 else if (shape < (int)(sizeof(mshape_ids) / sizeof(int)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 id = mshape_ids[shape];
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007269 else
7270 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 c = gdk_cursor_new_for_display(
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00007272 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id);
Bram Moolenaar98921892016-02-23 17:14:37 +01007273 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), c);
Bram Moolenaar98921892016-02-23 17:14:37 +01007274# if GTK_CHECK_VERSION(3,0,0)
7275 g_object_unref(G_OBJECT(c));
7276# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 gdk_cursor_destroy(c); /* Unref, actually. Bloody GTK+ 1. */
Bram Moolenaar98921892016-02-23 17:14:37 +01007278# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 }
7280 if (shape != MSHAPE_HIDE)
7281 last_shape = shape;
7282}
7283#endif /* FEAT_MOUSESHAPE */
7284
7285
7286#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
7287/*
7288 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
7289 * scaled down if the current font is not big enough, or scaled up if the image
7290 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
7291 * will be cut off if the current font is not big enough, or centered if it's
7292 * too small.
7293 */
7294# define SIGN_WIDTH (2 * gui.char_width)
7295# define SIGN_HEIGHT (gui.char_height)
7296# define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
7297
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 void
7299gui_mch_drawsign(int row, int col, int typenr)
7300{
7301 GdkPixbuf *sign;
7302
7303 sign = (GdkPixbuf *)sign_get_image(typenr);
7304
Bram Moolenaar98921892016-02-23 17:14:37 +01007305 if (sign != NULL && gui.drawarea != NULL
7306 && gtk_widget_get_window(gui.drawarea) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 {
7308 int width;
7309 int height;
7310 int xoffset;
7311 int yoffset;
7312 int need_scale;
7313
7314 width = gdk_pixbuf_get_width(sign);
7315 height = gdk_pixbuf_get_height(sign);
7316 /*
7317 * Decide whether we need to scale. Allow one pixel of border
7318 * width to be cut off, in order to avoid excessive scaling for
7319 * tiny differences in font size.
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007320 * Do scale to fit the height to avoid gaps because of linespacing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 */
7322 need_scale = (width > SIGN_WIDTH + 2
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007323 || height != SIGN_HEIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 || (width < 3 * SIGN_WIDTH / 4
7325 && height < 3 * SIGN_HEIGHT / 4));
7326 if (need_scale)
7327 {
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007328 double aspect;
7329 int w = width;
7330 int h = height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331
7332 /* Keep the original aspect ratio */
7333 aspect = (double)height / (double)width;
7334 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect;
7335 width = MIN(width, SIGN_WIDTH);
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007336 if (((double)(MAX(height, SIGN_HEIGHT)) /
7337 (double)(MIN(height, SIGN_HEIGHT))) < 1.15)
7338 {
7339 /* Change the aspect ratio by at most 15% to fill the
7340 * available space completly. */
7341 height = (double)SIGN_HEIGHT * SIGN_ASPECT / aspect;
7342 height = MIN(height, SIGN_HEIGHT);
7343 }
7344 else
7345 height = (double)width * aspect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007347 if (w == width && h == height)
7348 {
7349 /* no change in dimensions; don't decrease reference counter
7350 * (below) */
7351 need_scale = FALSE;
7352 }
7353 else
7354 {
7355 /* This doesn't seem to be worth caching, and doing so would
7356 * complicate the code quite a bit. */
7357 sign = gdk_pixbuf_scale_simple(sign, width, height,
7358 GDK_INTERP_BILINEAR);
7359 if (sign == NULL)
7360 return; /* out of memory */
7361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 }
7363
7364 /* The origin is the upper-left corner of the pixmap. Therefore
7365 * these offset may become negative if the pixmap is smaller than
7366 * the 2x1 cells reserved for the sign icon. */
7367 xoffset = (width - SIGN_WIDTH) / 2;
7368 yoffset = (height - SIGN_HEIGHT) / 2;
7369
Bram Moolenaar98921892016-02-23 17:14:37 +01007370# if GTK_CHECK_VERSION(3,0,0)
7371 {
7372 cairo_t *cr;
7373 cairo_surface_t *bg_surf;
7374 cairo_t *bg_cr;
7375 cairo_surface_t *sign_surf;
7376 cairo_t *sign_cr;
7377
7378 cr = cairo_create(gui.surface);
7379
7380 bg_surf = cairo_surface_create_similar(gui.surface,
7381 cairo_surface_get_content(gui.surface),
7382 SIGN_WIDTH, SIGN_HEIGHT);
7383 bg_cr = cairo_create(bg_surf);
Bram Moolenaar36edf062016-07-21 22:10:12 +02007384 cairo_set_source_rgba(bg_cr,
7385 gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue,
7386 gui.bgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01007387 cairo_paint(bg_cr);
7388
7389 sign_surf = cairo_surface_create_similar(gui.surface,
7390 cairo_surface_get_content(gui.surface),
7391 SIGN_WIDTH, SIGN_HEIGHT);
7392 sign_cr = cairo_create(sign_surf);
7393 gdk_cairo_set_source_pixbuf(sign_cr, sign, -xoffset, -yoffset);
7394 cairo_paint(sign_cr);
7395
7396 cairo_set_operator(sign_cr, CAIRO_OPERATOR_DEST_OVER);
7397 cairo_set_source_surface(sign_cr, bg_surf, 0, 0);
7398 cairo_paint(sign_cr);
7399
7400 cairo_set_source_surface(cr, sign_surf, FILL_X(col), FILL_Y(row));
7401 cairo_paint(cr);
7402
7403 cairo_destroy(sign_cr);
7404 cairo_surface_destroy(sign_surf);
7405 cairo_destroy(bg_cr);
7406 cairo_surface_destroy(bg_surf);
7407 cairo_destroy(cr);
7408
7409 if (!gui.by_signal)
7410 gtk_widget_queue_draw_area(gui.drawarea,
7411 FILL_X(col), FILL_Y(col), width, height);
7412
7413 }
7414# else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
7416
7417 gdk_draw_rectangle(gui.drawarea->window,
7418 gui.text_gc,
7419 TRUE,
7420 FILL_X(col),
7421 FILL_Y(row),
7422 SIGN_WIDTH,
7423 SIGN_HEIGHT);
7424
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 gdk_pixbuf_render_to_drawable_alpha(sign,
7426 gui.drawarea->window,
7427 MAX(0, xoffset),
7428 MAX(0, yoffset),
7429 FILL_X(col) - MIN(0, xoffset),
7430 FILL_Y(row) - MIN(0, yoffset),
7431 MIN(width, SIGN_WIDTH),
7432 MIN(height, SIGN_HEIGHT),
7433 GDK_PIXBUF_ALPHA_BILEVEL,
7434 127,
7435 GDK_RGB_DITHER_NORMAL,
7436 0, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01007437# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 if (need_scale)
7439 g_object_unref(sign);
7440 }
7441}
7442
7443 void *
7444gui_mch_register_sign(char_u *signfile)
7445{
7446 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use)
7447 {
7448 GdkPixbuf *sign;
7449 GError *error = NULL;
7450 char_u *message;
7451
7452 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error);
7453
7454 if (error == NULL)
7455 return sign;
7456
7457 message = (char_u *)error->message;
7458
7459 if (message != NULL && input_conv.vc_type != CONV_NONE)
7460 message = string_convert(&input_conv, message, NULL);
7461
7462 if (message != NULL)
7463 {
7464 /* The error message is already translated and will be more
7465 * descriptive than anything we could possibly do ourselves. */
7466 EMSG2("E255: %s", message);
7467
7468 if (input_conv.vc_type != CONV_NONE)
7469 vim_free(message);
7470 }
7471 g_error_free(error);
7472 }
7473
7474 return NULL;
7475}
7476
7477 void
7478gui_mch_destroy_sign(void *sign)
7479{
7480 if (sign != NULL)
7481 g_object_unref(sign);
7482}
7483
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484#endif /* FEAT_SIGN_ICONS */